Commit 9fac201a authored by Lewis Baker's avatar Lewis Baker Committed by Facebook Github Bot

Make folly::coro::Baton::reset() thread-safe

Summary: Allow calling `baton.reset()` concurrently with `co_await`.

Reviewed By: yfeldblum

Differential Revision: D15058504

fbshipit-source-id: af7ff69ddc2c8a4663e0f7ef03121c01502a984d
parent 9738cee3
...@@ -86,12 +86,9 @@ class Baton { ...@@ -86,12 +86,9 @@ class Baton {
/// for the Baton inside 'co_await baton.waitAsync()'. /// for the Baton inside 'co_await baton.waitAsync()'.
void post() noexcept; void post() noexcept;
/// Reset the baton back to the non-signalled state. /// Atomically reset the baton back to the non-signalled state.
/// ///
/// This method is not safe to be called concurrently with any other /// This is a no-op if the baton was already in the non-signalled state.
/// method on the Baton. The caller must ensure that there are no coroutines
/// currently waiting on the Baton and that there are no threads currently
/// calling .post() when .reset() is called.
void reset() noexcept; void reset() noexcept;
class WaitOperation { class WaitOperation {
...@@ -142,7 +139,10 @@ inline Baton::WaitOperation Baton::operator co_await() const noexcept { ...@@ -142,7 +139,10 @@ inline Baton::WaitOperation Baton::operator co_await() const noexcept {
} }
inline void Baton::reset() noexcept { inline void Baton::reset() noexcept {
state_.store(nullptr, std::memory_order_relaxed); // Transition from 'signalled' (ie. 'this') to not-signalled (ie. nullptr).
void* oldState = this;
(void)state_.compare_exchange_strong(
oldState, nullptr, std::memory_order_acq_rel, std::memory_order_relaxed);
} }
} // namespace coro } // namespace coro
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment