Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Cancellation.hpp
1#pragma once
2
3#include <boost/asio/any_io_executor.hpp>
4#include <boost/asio/associated_executor.hpp>
5#include <boost/asio/post.hpp>
6#include <boost/asio/spawn.hpp>
7#include <boost/asio/steady_timer.hpp>
8
9#include <atomic>
10#include <memory>
11#include <utility>
12
13namespace util::async::impl {
14
15class StopState {
16 std::atomic_bool isStopRequested_{false};
17
18public:
19 void
20 requestStop() noexcept
21 {
22 isStopRequested_ = true;
23 }
24
25 [[nodiscard]] bool
26 isStopRequested() const noexcept
27 {
28 return isStopRequested_;
29 }
30};
31
32using SharedStopState = std::shared_ptr<StopState>;
33
35 SharedStopState shared_ = std::make_shared<StopState>();
36
37public:
38 class Token {
39 friend class YieldContextStopSource;
40 SharedStopState shared_;
41 boost::asio::yield_context yield_;
42
43 Token(YieldContextStopSource* source, boost::asio::yield_context yield)
44 : shared_{source->shared_}, yield_{std::move(yield)}
45 {
46 }
47
48 public:
49 Token(Token const&) = default;
50 Token(Token&&) = default;
51
52 [[nodiscard]] bool
53 isStopRequested() const noexcept
54 {
55 // yield explicitly
56 boost::asio::post(yield_);
57 return shared_->isStopRequested();
58 }
59
60 [[nodiscard]]
61 operator bool() const noexcept
62 {
63 return isStopRequested();
64 }
65
66 [[nodiscard]]
67 operator boost::asio::yield_context() const noexcept
68 {
69 return yield_;
70 }
71 };
72
73 [[nodiscard]] Token
74 operator[](boost::asio::yield_context yield) noexcept
75 {
76 return {this, yield};
77 }
78
79 void
80 requestStop() noexcept
81 {
82 shared_->requestStop();
83 }
84};
85
87 SharedStopState shared_ = std::make_shared<StopState>();
88
89public:
90 class Token {
91 friend class BasicStopSource;
92 SharedStopState shared_;
93
94 explicit Token(BasicStopSource* source) : shared_{source->shared_}
95 {
96 }
97
98 public:
99 Token(Token const&) = default;
100 Token(Token&&) = default;
101 [[nodiscard]] bool
102
103 isStopRequested() const noexcept
104 {
105 return shared_->isStopRequested();
106 }
107
108 [[nodiscard]]
109 operator bool() const noexcept
110 {
111 return isStopRequested();
112 }
113 };
114
115 [[nodiscard]] Token
116 getToken()
117 {
118 return Token{this};
119 }
120
121 void
122 requestStop()
123 {
124 shared_->requestStop();
125 }
126};
127
128} // namespace util::async::impl
Definition Cancellation.hpp:90
Definition Cancellation.hpp:86
Definition Cancellation.hpp:15
Definition Cancellation.hpp:34