Skip to content

Pull model task API - #1133

Open
erincatto wants to merge 9 commits into
mainfrom
task_api
Open

erincatto wants to merge 9 commits into
mainfrom
task_api

Conversation

@erincatto

Copy link
Copy Markdown
Owner

No description provided.

@erincatto

erincatto commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner Author

Provides a new pull model task API. The current API is a push model. The pull model is more flexible and non-blocking.

Push model:

b2WorldDef def = b2DefaultWorldDef();
def.workerCount = 4; // [1, B2_MAX_WORKERS]
def.enqueueTask = MyEnqueueFunction;
def.finishTask = MyFinishTaskFunction;
def.userTaskContext = &MyTaskSystem;
myWorldId = b2CreateWorld(&def);

// later ...

while (running)
{
  // ...
  b2World_Step(myWorldId , timeStep, subStepCount);
  // ...
};

Pull model:

b2WorldDef def = b2DefaultWorldDef();
def.workerCount = 4; // [1, B2_MAX_WORKERS]
myWorldId = b2CreateWorld(&def);

// later ...

// Somewhere on any thread.
b2TaskBatch batch = b2World_BeginStep(myWorldId, timeStep, subStepCount);
MyDispatch(batch);
// ...

// Later once the batch is done. Any thread. Keep calling this until done.
b2TaskBatch nextBatch = b2World_ContinueStep( myWorldId );
if (nextBatch.count > 0)
{
  MyDispatch(nextBatch);
}
else
{
   stepDone = true;
}

Sometimes a batch has a deferred task that doesn't need to be joined until a later batch asks for it. Currently only one of these exist (the tree rebuild).

@erincatto

Copy link
Copy Markdown
Owner Author

@erincatto
erincatto marked this pull request as draft November 16, 2026 00:07
@erincatto
erincatto marked this pull request as ready for review November 16, 2026 00:07
Comment thread samples/main.cpp
s_context.showControls = s_context.newUser;

s_context.workerCount = b2MinInt( 8, GetNumberOfCores() / 2 );
s_context.workerCount = 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intended or leftover from testing? Sample will always run with a single thread.

Comment thread src/parallel_for.c
.deferred = false,
};

stepContext->batchCount += 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure of the intentions around batch and task counts but this seems out of place? Not sure why batch and task would both be increased here unconditionally.

@All8Up All8Up left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, a pass over this only noted the two items which looked questionable on the surface and I believe were only leading to informational reporting issues if they were actual errors. There were possibly a few documentation changes that don't match comments in code but that is always a bit difficult to verify in a diff view so might want to make a pass over them after the merge to be sure.

From what I can see, I should be able to get the concurrent execution model working and remove the hack I put in to spin on the physics completion. I'll give it a try as soon as possible and put it in my graphics perf testbed, figure if it can handle the 50k boids and allow overlapping concurrent work, that would be a pretty solid test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants