Skip to content

Commit

Permalink
Fix a race condition in cti::transforms::wait()
Browse files Browse the repository at this point in the history
* Thanks to p4654545 for reporting this issue and providing a reproducible example
* Closes #38
  • Loading branch information
Naios committed Nov 3, 2020
1 parent 48c6abf commit 6bffb44
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions include/continuable/detail/transforms/wait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,26 @@ Result wait_relaxed(continuable_base<Data, Annotation>&& continuable) {
return std::move(continuable).unpack();
}

std::mutex cv_mutex;
condition_variable_t cv;
std::atomic_bool ready{false};
std::mutex cv_mutex;

bool ready{false};
Result sync_result;

std::move(continuable)
.next([&](auto&&... args) {
sync_result = Result::from(std::forward<decltype(args)>(args)...);

ready.store(true, std::memory_order_release);
lock_t lock(cv_mutex);
ready = true;
cv.notify_all();
})
.done();

if (!ready.load(std::memory_order_acquire)) {
lock_t lock(cv_mutex);
lock_t lock(cv_mutex);
if (!ready) {
cv.wait(lock, [&] {
return ready.load(std::memory_order_acquire);
return ready;
});
}

Expand Down

0 comments on commit 6bffb44

Please sign in to comment.