proxygen
|
#include <Baton.h>
Classes | |
class | WaitOperation |
Public Member Functions | |
Baton (bool initiallySignalled=false) noexcept | |
Initialise the Baton to either the signalled or non-signalled state. More... | |
~Baton () | |
bool | ready () const noexcept |
Query whether the Baton is currently in the signalled state. More... | |
WaitOperation | operator co_await () const noexcept |
void | post () noexcept |
void | reset () noexcept |
Private Member Functions | |
bool | waitImpl (WaitOperation *awaiter) const noexcept |
Private Attributes | |
std::atomic< void * > | state_ |
A baton is a synchronisation primitive for coroutines that allows one coroutine to co_await the baton and suspend until the baton is posted by some other thread via a call to .post().
The Baton supports being awaited by a single coroutine at a time. If the baton is not ready at the time it is awaited then the awaiting coroutine suspends and is later resumed when some thread calls .post().
Example usage:
folly::coro::Baton baton; std::string sharedValue;
folly::coro::Task<void> consumer() { // Wait until the baton is posted. co_await baton;
// Now safe to read shared state. std::cout << sharedValue << std::cout; }
void producer() { // Write to shared state sharedValue = "some result";
// Publish the value by 'posting' the baton. // This will resume the consumer if it was currently suspended. baton.post(); }
|
inlineexplicitnoexcept |
folly::coro::Baton::~Baton | ( | ) |
|
inlinenoexcept |
Asynchronously wait for the Baton to enter the signalled state.
The returned object must be co_awaited from a coroutine. If the Baton is already signalled then the awaiting coroutine will continue without suspending. Otherwise, if the Baton is not yet signalled then the awaiting coroutine will suspend execution and will be resumed when some thread later calls post().
You may optionally specify an executor on which to resume executing the awaiting coroutine if the baton was not already in the signalled state by chaining a .via(executor) call. If you do not specify an executor then the behaviour is as if an inline executor was specified. i.e. the coroutine will be resumed inside the call to .post() on the thread that next calls .post().
|
noexcept |
|
inlinenoexcept |
Query whether the Baton is currently in the signalled state.
Definition at line 135 of file Baton.h.
References state_.
Referenced by folly::coro::Baton::WaitOperation::await_ready().
|
inlinenoexcept |
Reset the baton back to the non-signalled state.
This method is not safe to be called concurrently with any other 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.
Definition at line 144 of file Baton.h.
References state_.
|
privatenoexcept |
Referenced by folly::coro::Baton::WaitOperation::await_suspend().
|
mutableprivate |