proxygen
folly::coro::Baton Class Reference

#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_
 

Detailed Description

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(); }

Definition at line 55 of file Baton.h.

Constructor & Destructor Documentation

folly::coro::Baton::Baton ( bool  initiallySignalled = false)
inlineexplicitnoexcept

Initialise the Baton to either the signalled or non-signalled state.

Definition at line 132 of file Baton.h.

133  : state_(initiallySignalled ? static_cast<void*>(this) : nullptr) {}
std::atomic< void * > state_
Definition: Baton.h:129
folly::coro::Baton::~Baton ( )

Member Function Documentation

Baton::WaitOperation folly::coro::Baton::operator co_await ( ) const
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().

Definition at line 140 of file Baton.h.

140  {
141  return Baton::WaitOperation{*this};
142 }
void folly::coro::Baton::post ( )
noexcept

Set the Baton to the signalled state if it is not already signalled.

This will resume any coroutines that are currently suspended waiting for the Baton inside 'co_await baton.waitAsync()'.

bool folly::coro::Baton::ready ( ) const
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().

135  {
136  return state_.load(std::memory_order_acquire) ==
137  static_cast<const void*>(this);
138 }
std::atomic< void * > state_
Definition: Baton.h:129
void folly::coro::Baton::reset ( )
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_.

144  {
145  state_.store(nullptr, std::memory_order_relaxed);
146 }
std::atomic< void * > state_
Definition: Baton.h:129
bool folly::coro::Baton::waitImpl ( WaitOperation awaiter) const
privatenoexcept

Member Data Documentation

std::atomic<void*> folly::coro::Baton::state_
mutableprivate

Definition at line 129 of file Baton.h.

Referenced by ready(), and reset().


The documentation for this class was generated from the following file: