diff --git a/include/reactor-cpp/environment.hh b/include/reactor-cpp/environment.hh index 9acaa30c..f86ad9fb 100644 --- a/include/reactor-cpp/environment.hh +++ b/include/reactor-cpp/environment.hh @@ -48,6 +48,8 @@ private: void build_dependency_graph(Reactor* reactor); void calculate_indexes(); + std::mutex shutdown_mutex_{}; + public: explicit Environment(unsigned int num_workers, bool run_forever = default_run_forever, bool fast_fwd_execution = default_fast_fwd_execution, const Duration& timeout = Duration::max()) diff --git a/lib/environment.cc b/lib/environment.cc index 23fa4c7c..f021f933 100644 --- a/lib/environment.cc +++ b/lib/environment.cc @@ -88,10 +88,20 @@ void Environment::build_dependency_graph(Reactor* reactor) { // NOLINT } void Environment::sync_shutdown() { - validate(this->phase() == Phase::Execution, "sync_shutdown() may only be called during execution phase!"); - phase_ = Phase::Shutdown; + { + std::lock_guard lock(shutdown_mutex_); - log::Info() << "Terminating the execution"; + if (phase_ >= Phase::Shutdown) { + // sync_shutdown() was already called -> abort + return; + } + + validate(phase_ == Phase::Execution, "sync_shutdown() may only be called during execution phase!"); + phase_ = Phase::Shutdown; + } + + // the following will only be executed once + log::Debug() << "Terminating the execution"; for (auto* reactor : top_level_reactors_) { reactor->shutdown();