Skip to content

Commit

Permalink
GzOdeCollisionDetector: Use static mutex in create (#675)
Browse files Browse the repository at this point in the history
This is done to avoid that non-thread-safe ODE functions are called at the same time, causing errors such as 
> ODE INTERNAL ERROR 1: assertion "!colliders_initialized" failed in dInitColliders() [collision_kernel.cpp:168]

Signed-off-by: Silvio Traversaro <[email protected]>
Co-authored-by: Ian Chen <[email protected]>
  • Loading branch information
traversaro and iche033 authored Oct 17, 2024
1 parent 2ba401c commit 70a72cd
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dartsim/src/GzOdeCollisionDetector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include <memory>
#include <mutex>
#include <unordered_map>
#include <utility>

Expand Down Expand Up @@ -43,6 +44,13 @@ GzOdeCollisionDetector::Registrar<GzOdeCollisionDetector>
/////////////////////////////////////////////////
std::shared_ptr<GzOdeCollisionDetector> GzOdeCollisionDetector::create()
{
// GzOdeCollisionDetector constructor calls the OdeCollisionDetector
// constructor, that calls the non-thread safe dInitODE2(0).
// To mitigate this problem, we use a static mutex to ensure that
// each GzOdeCollisionDetector constructor is called not at the same time.
// See https://github.com/gazebosim/gz-sim/issues/18 for more info.
static std::mutex odeInitMutex;
std::unique_lock<std::mutex> lock(odeInitMutex);
return std::shared_ptr<GzOdeCollisionDetector>(new GzOdeCollisionDetector());
}

Expand Down

0 comments on commit 70a72cd

Please sign in to comment.