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