Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
PhysicsTaskPool.hpp File Reference

The worker pool Box2D solves across on a device. More...

#include <box2d/types.h>
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <mutex>
#include <thread>
#include <vector>
Include dependency graph for PhysicsTaskPool.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  esengine::physics::TaskPool

Namespaces

namespace  esengine
namespace  esengine::physics

Detailed Description

The worker pool Box2D solves across on a device.

Box2D partitions a step into tasks and asks a host-supplied pool to run them; given none it solves on the calling thread, which is what the web build does and must keep doing (a wasm side module has no threads to hand it). A device has cores sitting idle, so the same module builds this pool there instead. The simulation is unchanged either way: Box2D is deterministic under multithreading, and two threads give the same result as eight.

Two properties of Box2D's solver dictate the shape:

  • Enqueue must not run the task. Worker 0 drives the stage sync bits and every other worker spins on them, so a task executed inline would spin against a worker that has not started.
  • The calling thread IS worker 0, and finishing drains the queue rather than blocking. Box2D enqueues the island split alongside the whole worker set, so a pool that only waits can leave a solver worker queued behind it, never reaching the barrier its peers spin on. Helping also keeps each worker index on exactly one thread at a time, which Box2D requires — indices are its per-worker scratch, and two threads sharing one corrupts the step.

Definition in file PhysicsTaskPool.hpp.