Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
util::Coroutine Class Reference

Manages a coroutine execution context, allowing for cooperative multitasking and cancellation. More...

#include <Coroutine.hpp>

Public Types

using cancellable_yield_context_type
 Type alias for a yield_context that is bound to a cancellation slot. This allows asynchronous operations initiated with this context to be cancelled.
 

Public Member Functions

 ~Coroutine ()
 Destructor for the Coroutine. Handles cleanup, such as disconnecting from the cancellation signal.
 
 Coroutine (Coroutine const &)=delete
 
 Coroutine (Coroutine &&)=delete
 
Coroutineoperator== (Coroutine &&)=delete
 
Coroutineoperator== (Coroutine const &)=delete
 
template<CoroutineFunction Fn>
void spawnChild (Fn fn)
 Spawns a child coroutine from this coroutine. The child coroutine shares the same cancellation signal.
 
boost::system::error_code error () const
 Returns the error code, if any, associated with the last operation in this coroutine.
 
void cancelAll (boost::asio::cancellation_type_t cancellationType=boost::asio::cancellation_type::terminal)
 Cancels all coroutines sharing the same root cancellation signal.
 
bool isCancelled () const
 Checks if this coroutine has been cancelled.
 
cancellable_yield_context_type yieldContext () const
 Returns the cancellable yield context associated with this coroutine. This context should be used for Boost.Asio asynchronous operations within the coroutine to enable cancellation.
 
boost::asio::any_io_executor executor () const
 Returns the executor associated with this coroutine's yield context.
 
void yield () const
 Explicitly yields execution back to the scheduler. This can be used to allow other tasks to run.
 

Static Public Member Functions

template<typename ExecutionContext , CoroutineFunction Fn>
static void spawnNew (ExecutionContext &ioContext, Fn fn)
 Spawns a new top-level coroutine.
 

Detailed Description

Manages a coroutine execution context, allowing for cooperative multitasking and cancellation.

The Coroutine class wraps a Boost.Asio yield_context and provides mechanisms for spawning new coroutines, child coroutines, and managing their lifecycle, including cancellation. It integrates with a signal system to propagate cancellation requests across related coroutines.

Member Typedef Documentation

◆ cancellable_yield_context_type

Initial value:
boost::asio::cancellation_slot_binder<boost::asio::yield_context, boost::asio::cancellation_slot>

Type alias for a yield_context that is bound to a cancellation slot. This allows asynchronous operations initiated with this context to be cancelled.

Member Function Documentation

◆ cancelAll()

void util::Coroutine::cancelAll ( boost::asio::cancellation_type_t cancellationType = boost::asio::cancellation_type::terminal)

Cancels all coroutines sharing the same root cancellation signal.

Parameters
cancellationTypeThe type of cancellation to perform. Defaults to boost::asio::cancellation_type::terminal.

◆ error()

boost::system::error_code util::Coroutine::error ( ) const
nodiscard

Returns the error code, if any, associated with the last operation in this coroutine.

Returns
A boost::system::error_code indicating the status.

◆ executor()

boost::asio::any_io_executor util::Coroutine::executor ( ) const
nodiscard

Returns the executor associated with this coroutine's yield context.

Returns
The executor.

◆ isCancelled()

bool util::Coroutine::isCancelled ( ) const
nodiscard

Checks if this coroutine has been cancelled.

Returns
True if the coroutine is cancelled, false otherwise.

◆ spawnChild()

template<CoroutineFunction Fn>
void util::Coroutine::spawnChild ( Fn fn)
inline

Spawns a child coroutine from this coroutine. The child coroutine shares the same cancellation signal.

Template Parameters
FnThe type of the invocable function that represents the child coroutine body.
Parameters
fnThe function to be executed as the child coroutine. It will receive a Coroutine& argument.

◆ spawnNew()

template<typename ExecutionContext , CoroutineFunction Fn>
static void util::Coroutine::spawnNew ( ExecutionContext & ioContext,
Fn fn )
inlinestatic

Spawns a new top-level coroutine.

Template Parameters
ExecutionContextThe type of the I/O execution context (e.g., boost::asio::io_context).
FnThe type of the invocable function that represents the coroutine body.
Parameters
ioContextThe I/O execution context on which to spawn the coroutine.
fnThe function to be executed as the coroutine. It will receive a Coroutine& argument.

◆ yieldContext()

Coroutine::cancellable_yield_context_type util::Coroutine::yieldContext ( ) const
nodiscard

Returns the cancellable yield context associated with this coroutine. This context should be used for Boost.Asio asynchronous operations within the coroutine to enable cancellation.

Returns
A cancellable_yield_context_type object.

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