Skip to content

Commit

Permalink
make sure shutdown is thread safe and can only be called once
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
cmnrd committed Dec 14, 2022
1 parent 30c96e2 commit 8dfe7fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/reactor-cpp/environment.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
16 changes: 13 additions & 3 deletions lib/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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();
Expand Down

0 comments on commit 8dfe7fc

Please # to comment.