Skip to content

Commit

Permalink
Make AsyncQueue thread safe
Browse files Browse the repository at this point in the history
Mutex should avoid concurrent appending and flushing of the queue.

It would be probably better to use a lockless priority queue, good enough for now.
  • Loading branch information
ktf committed Aug 10, 2024
1 parent 0abf8fb commit f11e270
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Framework/Core/src/AsyncQueue.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

O2_DECLARE_DYNAMIC_LOG(async_queue);

std::mutex gAsyncQueueMutex;

namespace o2::framework
{
auto AsyncQueueHelpers::create(AsyncQueue& queue, AsyncTaskSpec spec) -> AsyncTaskId
{
std::lock_guard<std::mutex> guard(gAsyncQueueMutex);
AsyncTaskId id;
id.value = queue.prototypes.size();
queue.prototypes.push_back(spec);
Expand All @@ -32,11 +35,14 @@ auto AsyncQueueHelpers::post(AsyncQueue& queue, AsyncTaskId id, AsyncCallback ta
taskToPost.id = id;
taskToPost.timeslice = timeslice;
taskToPost.debounce = debounce;

std::lock_guard<std::mutex> guard(gAsyncQueueMutex);
queue.tasks.push_back(taskToPost);
}

auto AsyncQueueHelpers::run(AsyncQueue& queue, TimesliceId oldestPossible) -> void
{
std::lock_guard<std::mutex> guard(gAsyncQueueMutex);
if (queue.tasks.empty()) {
return;
}
Expand Down Expand Up @@ -126,6 +132,7 @@ auto AsyncQueueHelpers::run(AsyncQueue& queue, TimesliceId oldestPossible) -> vo

auto AsyncQueueHelpers::reset(AsyncQueue& queue) -> void
{
std::lock_guard<std::mutex> guard(gAsyncQueueMutex);
queue.tasks.clear();
queue.iteration = 0;
}
Expand Down

0 comments on commit f11e270

Please sign in to comment.