22#include "util/Spawn.hpp"
24#include <boost/asio/any_io_executor.hpp>
25#include <boost/asio/bind_cancellation_slot.hpp>
26#include <boost/asio/cancellation_signal.hpp>
27#include <boost/asio/cancellation_type.hpp>
28#include <boost/asio/error.hpp>
29#include <boost/asio/spawn.hpp>
30#include <boost/signals2/connection.hpp>
31#include <boost/signals2/signal.hpp>
32#include <boost/signals2/variadic_signal.hpp>
69 boost::asio::cancellation_slot_binder<boost::asio::yield_context, boost::asio::cancellation_slot>;
72 boost::asio::yield_context yield_;
73 boost::system::error_code error_;
74 boost::asio::cancellation_signal cancellationSignal_;
76 std::atomic_bool isCancelled_{
false};
78 using FamilyCancellationSignal = boost::signals2::signal<void(boost::asio::cancellation_type_t)>;
79 std::shared_ptr<FamilyCancellationSignal> familySignal_;
80 boost::signals2::connection connection_;
88 boost::asio::yield_context&&
yield,
89 std::shared_ptr<FamilyCancellationSignal> signal = std::make_shared<FamilyCancellationSignal>()
115 template <
typename ExecutionContext, CoroutineFunction Fn>
119 util::spawn(ioContext, [fn = std::move(fn)](boost::asio::yield_context
yield) {
131 template <CoroutineFunction Fn>
138 util::spawn(yield_, [signal = familySignal_, fn = std::move(fn)](boost::asio::yield_context
yield)
mutable {
148 [[nodiscard]] boost::system::error_code
157 cancelAll(boost::asio::cancellation_type_t cancellationType = boost::asio::cancellation_type::terminal);
179 [[nodiscard]] boost::asio::any_io_executor
Manages a coroutine execution context, allowing for cooperative multitasking and cancellation.
Definition Coroutine.hpp:62
static void spawnNew(ExecutionContext &ioContext, Fn fn)
Spawns a new top-level coroutine.
Definition Coroutine.hpp:117
boost::asio::cancellation_slot_binder< boost::asio::yield_context, boost::asio::cancellation_slot > cancellable_yield_context_type
Type alias for a yield_context that is bound to a cancellation slot. This allows asynchronous operati...
Definition Coroutine.hpp:68
~Coroutine()
Destructor for the Coroutine. Handles cleanup, such as disconnecting from the cancellation signal.
Definition Coroutine.cpp:47
boost::system::error_code error() const
Returns the error code, if any, associated with the last operation in this coroutine.
Definition Coroutine.cpp:53
bool isCancelled() const
Checks if this coroutine has been cancelled.
Definition Coroutine.cpp:67
boost::asio::any_io_executor executor() const
Returns the executor associated with this coroutine's yield context.
Definition Coroutine.cpp:79
void cancelAll(boost::asio::cancellation_type_t cancellationType=boost::asio::cancellation_type::terminal)
Cancels all coroutines sharing the same root cancellation signal.
Definition Coroutine.cpp:59
void spawnChild(Fn fn)
Spawns a child coroutine from this coroutine. The child coroutine shares the same cancellation signal...
Definition Coroutine.hpp:133
cancellable_yield_context_type yieldContext() const
Returns the cancellable yield context associated with this coroutine. This context should be used for...
Definition Coroutine.cpp:73
void yield() const
Explicitly yields execution back to the scheduler. This can be used to allow other tasks to run.
Definition Coroutine.cpp:85
Concept for functions that can be used as coroutine bodies. Such functions must be invocable with a C...
Definition Coroutine.hpp:51
This namespace contains various utilities.
Definition AccountUtils.hpp:30
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn
Definition Spawn.hpp:69