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),
68 fn = std::forward<FnType>(fn)]() mutable {
69 if constexpr (SomeStoppableOutcome<OutcomeType>) {
70 auto& stopSource = outcome.getStopSource();
71 std::invoke(
72 std::forward<decltype(fn)>(fn), outcome, stopSource, stopSource.getToken()
73 );
74 } else {
75 std::invoke(std::forward<decltype(fn)>(fn), outcome);
76 }
77 }
78 );
79
80 return op;
81 }
82
83 template <typename ContextType, typename FnType>
84 static void
85 post(ContextType& ctx, FnType&& fn)
86 {
87 boost::asio::post(ctx.getExecutor(), std::forward<FnType>(fn));
88 }
89};
90
92 template <typename ContextType, SomeOutcome OutcomeType, typename FnType>
93 [[nodiscard]] static auto
94 dispatch([[maybe_unused]] ContextType& ctx, OutcomeType outcome, FnType&& fn)
95 {
96 auto op = outcome.getOperation();
97
99 auto& stopSource = outcome.getStopSource();
100 std::invoke(std::forward<FnType>(fn), outcome, stopSource, stopSource.getToken());
101 } else {
102 std::invoke(std::forward<FnType>(fn), outcome);
103 }
104
105 return op;
106 }
107
108 template <typename ContextType, typename FnType>
109 static void
110 post([[maybe_unused]] ContextType& ctx, FnType&& fn)
111 {
112 std::invoke(std::forward<FnType>(fn));
113 }
114};
115
116} // 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:91