Skip to content

Commit fe5e6c2

Browse files
committed
Fix race condition
even with a single worker, events might be scheduled asynchronously
1 parent 7728449 commit fe5e6c2

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

lib/scheduler.cc

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -363,35 +363,24 @@ void Scheduler::fill_action_list_pool() {
363363
}
364364

365365
auto Scheduler::insert_event_at(const Tag& tag) -> const ActionListPtr& {
366-
if (using_workers_) {
367-
auto shared_lock = std::shared_lock<std::shared_mutex>(mutex_event_queue_);
368-
369-
auto event_it = event_queue_.find(tag);
370-
if (event_it == event_queue_.end()) {
371-
shared_lock.unlock();
372-
{
373-
auto unique_lock = std::unique_lock<std::shared_mutex>(mutex_event_queue_);
374-
if (action_list_pool_.empty()) {
375-
fill_action_list_pool();
376-
}
377-
const auto& result = event_queue_.try_emplace(tag, std::move(action_list_pool_.back()));
378-
if (result.second) {
379-
action_list_pool_.pop_back();
380-
}
381-
return result.first->second;
366+
auto shared_lock = std::shared_lock<std::shared_mutex>(mutex_event_queue_);
367+
368+
auto event_it = event_queue_.find(tag);
369+
if (event_it == event_queue_.end()) {
370+
shared_lock.unlock();
371+
{
372+
auto unique_lock = std::unique_lock<std::shared_mutex>(mutex_event_queue_);
373+
if (action_list_pool_.empty()) {
374+
fill_action_list_pool();
375+
}
376+
const auto& result = event_queue_.try_emplace(tag, std::move(action_list_pool_.back()));
377+
if (result.second) {
378+
action_list_pool_.pop_back();
382379
}
383-
} else {
384-
return event_it->second;
380+
return result.first->second;
385381
}
386382
} else {
387-
if (action_list_pool_.empty()) {
388-
fill_action_list_pool();
389-
}
390-
const auto& result = event_queue_.try_emplace(tag, std::move(action_list_pool_.back()));
391-
if (result.second) {
392-
action_list_pool_.pop_back();
393-
}
394-
return result.first->second;
383+
return event_it->second;
395384
}
396385
}
397386

0 commit comments

Comments
 (0)