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

CoroutineGroup is a helper class to manage a group of coroutines. It allows to spawn multiple coroutines and wait for all of them to finish. More...

#include <CoroutineGroup.hpp>

Public Member Functions

 CoroutineGroup (boost::asio::yield_context yield, std::optional< size_t > maxChildren=std::nullopt)
 Construct a new Coroutine Group object.
 
 ~CoroutineGroup ()
 Destroy the Coroutine Group object.
 
bool spawn (boost::asio::yield_context yield, std::function< void(boost::asio::yield_context)> fn)
 Spawn a new coroutine in the group.
 
std::optional< std::function< void()> > registerForeign ()
 Register a foreign coroutine this group should wait for.
 
void asyncWait (boost::asio::yield_context yield)
 Wait for all the coroutines in the group to finish.
 
size_t size () const
 Get the number of coroutines in the group.
 
bool isFull () const
 Check if the group is full.
 

Detailed Description

CoroutineGroup is a helper class to manage a group of coroutines. It allows to spawn multiple coroutines and wait for all of them to finish.

Note
This class is safe to use from multiple threads.

Constructor & Destructor Documentation

◆ CoroutineGroup()

util::CoroutineGroup::CoroutineGroup ( boost::asio::yield_context yield,
std::optional< size_t > maxChildren = std::nullopt )

Construct a new Coroutine Group object.

Parameters
yieldThe yield context to use for the internal timer
maxChildrenThe maximum number of coroutines that can be spawned at the same time. If not provided, there is no limit

◆ ~CoroutineGroup()

util::CoroutineGroup::~CoroutineGroup ( )

Destroy the Coroutine Group object.

Note
asyncWait() must be called before the object is destroyed

Member Function Documentation

◆ asyncWait()

void util::CoroutineGroup::asyncWait ( boost::asio::yield_context yield)

Wait for all the coroutines in the group to finish.

Note
This method must be called before the object is destroyed
Parameters
yieldThe yield context to use for the internal timer

◆ isFull()

bool util::CoroutineGroup::isFull ( ) const

Check if the group is full.

Returns
true If the group is full false otherwise

◆ registerForeign()

std::optional< std::function< void()> > util::CoroutineGroup::registerForeign ( )

Register a foreign coroutine this group should wait for.

Note
A foreign coroutine is still counted as a child one, i.e. calling this method increases the size of the group.
Returns
A callback to call on foreign coroutine completes or std::nullopt if the group is already full.

◆ size()

size_t util::CoroutineGroup::size ( ) const

Get the number of coroutines in the group.

Returns
size_t The number of coroutines in the group

◆ spawn()

bool util::CoroutineGroup::spawn ( boost::asio::yield_context yield,
std::function< void(boost::asio::yield_context)> fn )

Spawn a new coroutine in the group.

Parameters
yieldThe yield context to use for the coroutine (it should be the same as the one used in the constructor)
fnThe function to execute
Returns
true If the coroutine was spawned successfully. false if the maximum number of coroutines has been reached

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