diff --git a/platforms/posix/src/px4/common/drv_hrt.cpp b/platforms/posix/src/px4/common/drv_hrt.cpp index c4a1d9b9dc..9634511bf3 100644 --- a/platforms/posix/src/px4/common/drv_hrt.cpp +++ b/platforms/posix/src/px4/common/drv_hrt.cpp @@ -53,7 +53,7 @@ #if defined(ENABLE_LOCKSTEP_SCHEDULER) #include -static LockstepScheduler lockstep_scheduler {}; +static LockstepScheduler lockstep_scheduler {true}; #endif // Intervals in usec diff --git a/platforms/posix/src/px4/common/lockstep_scheduler/include/lockstep_scheduler/lockstep_components.h b/platforms/posix/src/px4/common/lockstep_scheduler/include/lockstep_scheduler/lockstep_components.h index 6767d1389d..3dc032e5a9 100644 --- a/platforms/posix/src/px4/common/lockstep_scheduler/include/lockstep_scheduler/lockstep_components.h +++ b/platforms/posix/src/px4/common/lockstep_scheduler/include/lockstep_scheduler/lockstep_components.h @@ -46,7 +46,7 @@ class LockstepComponents { public: - LockstepComponents(); + LockstepComponents(bool no_cleanup_on_destroy = false); ~LockstepComponents(); /** @@ -69,6 +69,7 @@ public: void wait_for_components(); private: + const bool _no_cleanup_on_destroy; px4_sem_t _components_sem; diff --git a/platforms/posix/src/px4/common/lockstep_scheduler/include/lockstep_scheduler/lockstep_scheduler.h b/platforms/posix/src/px4/common/lockstep_scheduler/include/lockstep_scheduler/lockstep_scheduler.h index 05cf64665a..711fd8d897 100644 --- a/platforms/posix/src/px4/common/lockstep_scheduler/include/lockstep_scheduler/lockstep_scheduler.h +++ b/platforms/posix/src/px4/common/lockstep_scheduler/include/lockstep_scheduler/lockstep_scheduler.h @@ -46,6 +46,7 @@ class LockstepScheduler { public: + LockstepScheduler(bool no_cleanup_on_destroy = false) : _components(no_cleanup_on_destroy) {} ~LockstepScheduler(); void set_absolute_time(uint64_t time_us); diff --git a/platforms/posix/src/px4/common/lockstep_scheduler/src/lockstep_components.cpp b/platforms/posix/src/px4/common/lockstep_scheduler/src/lockstep_components.cpp index 5492f72b28..730f7c8517 100644 --- a/platforms/posix/src/px4/common/lockstep_scheduler/src/lockstep_components.cpp +++ b/platforms/posix/src/px4/common/lockstep_scheduler/src/lockstep_components.cpp @@ -42,14 +42,19 @@ #include #include -LockstepComponents::LockstepComponents() +LockstepComponents::LockstepComponents(bool no_cleanup_on_destroy) + : _no_cleanup_on_destroy(no_cleanup_on_destroy) { px4_sem_init(&_components_sem, 0, 0); } LockstepComponents::~LockstepComponents() { - px4_sem_destroy(&_components_sem); + // Trying to destroy a condition variable with threads currently blocked on it results in undefined behavior. + // Therefore we allow the caller not to cleanup and let the OS take care of that. + if (!_no_cleanup_on_destroy) { + px4_sem_destroy(&_components_sem); + } } int LockstepComponents::register_component()