Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Execution.hpp
1#pragma once
2
3#include "util/Spawn.hpp"
4#include "util/async/Concepts.hpp"
5#include "util/async/context/impl/Timer.hpp"
6
7#include <boost/asio/spawn.hpp>
8#include <boost/asio/strand.hpp>
9#include <boost/asio/thread_pool.hpp>
10
11namespace util::async::impl {
12
14 template <typename ContextType, SomeOutcome OutcomeType, typename FnType>
15 [[nodiscard]] static auto
16 dispatch(ContextType& ctx, OutcomeType&& outcome, FnType&& fn)
17 {
18 auto op = outcome.getOperation();
19
22 ctx.getExecutor(),
23 [outcome = std::forward<OutcomeType>(outcome),
24 fn = std::forward<FnType>(fn)](auto yield) mutable {
25 if constexpr (SomeStoppableOutcome<OutcomeType>) {
26 auto& stopSource = outcome.getStopSource();
27 std::invoke(
28 std::forward<decltype(fn)>(fn), outcome, stopSource, stopSource[yield]
29 );
30 } else {
31 std::invoke(std::forward<decltype(fn)>(fn), outcome);
32 }
33 }
34 );
35 } else {
36 boost::asio::post(
37 ctx.getExecutor(),
38 [outcome = std::forward<OutcomeType>(outcome),
39 fn = std::forward<FnType>(fn)] mutable {
40 std::invoke(std::forward<decltype(fn)>(fn), outcome);
41 }
42 );
43 }
44
45 return op;
46 }
47
48 template <typename ContextType, typename FnType>
49 static void
50 post(ContextType& ctx, FnType&& fn)
51 {
52 boost::asio::post(ctx.getExecutor(), [fn = std::forward<FnType>(fn)] mutable {
53 std::invoke(std::forward<decltype(fn)>(fn));
54 });
55 }
56};
57
59 template <typename ContextType, SomeOutcome OutcomeType, typename FnType>
60 [[nodiscard]] static auto
61 dispatch(ContextType& ctx, OutcomeType&& outcome, FnType&& fn)
62 {
63 auto op = outcome.getOperation();
64
65 boost::asio::post(
66 ctx.getExecutor(),
67 [outcome = std::forward<OutcomeType>(outcome), fn = std::forward<FnType>(fn)] mutable {
68 if constexpr (SomeStoppableOutcome<OutcomeType>) {
69 auto& stopSource = outcome.getStopSource();
70 std::invoke(
71 std::forward<decltype(fn)>(fn), outcome, stopSource, stopSource.getToken()
72 );
73 } else {
74 std::invoke(std::forward<decltype(fn)>(fn), outcome);
75 }
76 }
77 );
78
79 return op;
80 }
81
82 template <typename ContextType, typename FnType>
83 static void
84 post(ContextType& ctx, FnType&& fn)
85 {
86 boost::asio::post(ctx.getExecutor(), std::forward<FnType>(fn));
87 }
88};
89
91 template <typename ContextType, SomeOutcome OutcomeType, typename FnType>
92 [[nodiscard]] static auto
93 dispatch([[maybe_unused]] ContextType& ctx, OutcomeType outcome, FnType&& fn)
94 {
95 auto op = outcome.getOperation();
96
98 auto& stopSource = outcome.getStopSource();
99 std::invoke(std::forward<FnType>(fn), outcome, stopSource, stopSource.getToken());
100 } else {
101 std::invoke(std::forward<FnType>(fn), outcome);
102 }
103
104 return op;
105 }
106
107 template <typename ContextType, typename FnType>
108 static void
109 post([[maybe_unused]] ContextType& ctx, FnType&& fn)
110 {
111 std::invoke(std::forward<FnType>(fn));
112 }
113};
114
115} // namespace util::async::impl
Specifies the interface for an outcome (promise) that can be stopped.
Definition Concepts.hpp:151
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn.
Definition Spawn.hpp:53
Definition Execution.hpp:58
Definition Execution.hpp:13
Definition Execution.hpp:90