Multithreaded physics update

Before: Single thread

With 2 threads 2 threads

Increasing the number of threads further yields no increases in performance, might be because I have only 4 cores, or because of contention.

TSAN and ASAN are clean (at least nothing related to this MR)

Technical details: Process mMovementQueue in multiple thread if Bullet is compiled with multithreading support. Otherwise, gracefully fallback to singlethreaded.

Assuming Bullet is compiled with multithread support, rayTest(), and by extention convexSweepTest() are thread safe. However modifications to btCollisionWorld is not. Change the API to follow the one imposed by Bullet and add the necessary locking around setWorldTransform().

mMovementResults and mStandingCollisions are not protected by a lock:

  • each element is accessed by the one thread currently processing the actor whose pointer is the key. There is no concurrent access to each element.
  • they are prefilled in the main thread, so the maps themselve are not modified from multiple threads.

The modified updateCollisionMask() is not strictly speaking thread safe, however it is called in only 2 contexts:

  • in the physics thread processing the affected actor. Other threads don't care about the value of this bit
  • in the main thread during mechanics update

Merge request reports

Loading