Threading, VecWorld & GIL Release¶
VecWorld Architecture (src/vec_world.cpp)¶
VecWorld(num_envs, num_threads, config) manages num_envs completely independent std::unique_ptr<World> instances, each with its own JPH::PhysicsSystem.
Worker count: When
num_threads == 0,VecWorldpicksmin(num_envs, std::thread::hardware_concurrency())(falling back to4ifhardware_concurrency()returns0).Per-worker scratch memory & job system: If
num_threads > 1,VecWorldspawnsnum_threadspersistentstd::threadworkers (worker_loop), each owning:A dedicated 16 MB
JPH::TempAllocatorImplWithMallocFallback(worker_allocators_).A
JPH::JobSystemSingleThreaded(worker_job_systems_). Because environments are partitioned contiguously across workers ([start, end)), workers never share aPhysicsSystemorTempAllocator, avoiding cross-environment lock contention.
Synchronization:
VecWorld::step()andVecWorld::raycast_batch()incrementstep_generation_undermutex_, wake workers viacv_work_.notify_all(), wait oncv_done_untilactive_workers_ == 0, and then gathercontacts(onstep).
Python GIL Release (src/python/module.cpp)¶
The following operations release the Python GIL (nb::gil_scoped_release) for their C++ execution:
World:step,raycast_batch,save_state,load_stateVecWorld:step,raycast_batch,reset_all,reset_envs,save_state,load_state
Thread-Safety Guarantees¶
Multiple independent
Worldinstances: Safe to step concurrently from Python threads (e.g.,concurrent.futures.ThreadPoolExecutor), becauseensure_jolt_initialized()is guarded bystd::call_onceand eachWorldowns its ownPhysicsSystem,TempAllocator, andJobSystem.Single
WorldorVecWorldinstance: Not internally mutex-locked for concurrent Python calls to the same object. Do not callstep(),add_body(), or mutatectrlon the sameWorld/VecWorldfrom multiple threads simultaneously.