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