Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
CoroutineGroup.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include <boost/asio/spawn.hpp>
23#include <boost/asio/steady_timer.hpp>
24
25#include <atomic>
26#include <cstddef>
27#include <functional>
28#include <optional>
29
30namespace util {
31
38 boost::asio::steady_timer timer_;
39 std::optional<size_t> maxChildren_;
40 std::atomic_size_t childrenCounter_{0};
41
42public:
50 CoroutineGroup(boost::asio::yield_context yield, std::optional<size_t> maxChildren = std::nullopt);
51
58
68 bool
69 spawn(boost::asio::yield_context yield, std::function<void(boost::asio::yield_context)> fn);
70
78 std::optional<std::function<void()>>
80
88 void
89 asyncWait(boost::asio::yield_context yield);
90
96 size_t
97 size() const;
98
104 bool
105 isFull() const;
106
107private:
108 void
109 onCoroutineCompleted();
110};
111
112} // namespace util
CoroutineGroup is a helper class to manage a group of coroutines. It allows to spawn multiple corouti...
Definition CoroutineGroup.hpp:37
bool isFull() const
Check if the group is full.
Definition CoroutineGroup.cpp:85
void asyncWait(boost::asio::yield_context yield)
Wait for all the coroutines in the group to finish.
Definition CoroutineGroup.cpp:69
std::optional< std::function< void()> > registerForeign()
Register a foreign coroutine this group should wait for.
Definition CoroutineGroup.cpp:59
CoroutineGroup(boost::asio::yield_context yield, std::optional< size_t > maxChildren=std::nullopt)
Construct a new Coroutine Group object.
Definition CoroutineGroup.cpp:34
bool spawn(boost::asio::yield_context yield, std::function< void(boost::asio::yield_context)> fn)
Spawn a new coroutine in the group.
Definition CoroutineGroup.cpp:45
~CoroutineGroup()
Destroy the Coroutine Group object.
Definition CoroutineGroup.cpp:39
size_t size() const
Get the number of coroutines in the group.
Definition CoroutineGroup.cpp:79
This namespace contains various utilities.
Definition AccountUtils.hpp:30