Skip to content

Commit

Permalink
Merge pull request #153 from finger563/feature/hfsm-runtime-improve-b…
Browse files Browse the repository at this point in the history
…locking-behavior

feat(runtime): improve queue management APIs
  • Loading branch information
finger563 authored Aug 9, 2023
2 parents 2e2a3c4 + dd96307 commit 7e272b2
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/plugins/SoftwareGenerator/templates/uml/GeneratedStates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,25 @@ namespace {{{namespace}}}::{{{sanitizedName}}} {
// Blocks until an event is available
void wait_for_events(void) {
std::unique_lock<std::mutex> lock(queue_mutex_);
if (events_.size() > 0) {
return;
}
queue_cv_.wait(lock);
}

// Blocks until an event is available or the timeout is reached
void sleep_until_event(float seconds) {
std::unique_lock<std::mutex> lock(queue_mutex_);
if (events_.size() > 0) {
return;
}
queue_cv_.wait_for(lock, std::chrono::duration<float>(seconds));
}

// Blocks until an event is available
GeneratedEventBase *get_next_event_blocking(void) {
wait_for_events();
std::unique_lock<std::mutex> lock(queue_mutex_);
queue_cv_.wait(lock, [this]{ return events_.size() > 0; });
GeneratedEventBase *ptr = events_.front();
events_.pop_front(); // remove the event from the Q
return ptr;
Expand All @@ -129,10 +135,15 @@ namespace {{{namespace}}}::{{{sanitizedName}}} {

// Clears the event queue and frees all event memory
void clear_events(void) {
GeneratedEventBase *ptr = get_next_event();
while (ptr != nullptr) {
// copy the queue so we can free the memory without holding the lock
std::deque<GeneratedEventBase*> deq_copy;
{ std::lock_guard<std::mutex> lock(queue_mutex_);
deq_copy = events_;
events_.clear();
}
// make sure we don't hold the lock while freeing memory
for (auto ptr : deq_copy) {
consume_event(ptr);
ptr = get_next_event();
}
}

Expand Down

0 comments on commit 7e272b2

Please sign in to comment.