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
32
namespace
util::async::impl {
33
34
class
StopState
{
35
std::atomic_bool isStopRequested_{
false
};
36
37
public
:
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
51
using
SharedStopState = std::shared_ptr<StopState>;
52
53
class
YieldContextStopSource
{
54
SharedStopState shared_ = std::make_shared<StopState>();
55
56
public
:
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]]
operator
bool()
const
noexcept
80
{
81
return
isStopRequested();
82
}
83
84
[[nodiscard]]
operator
boost::asio::yield_context()
const
noexcept
85
{
86
return
yield_;
87
}
88
};
89
90
[[nodiscard]]
Token
91
operator[](boost::asio::yield_context yield)
noexcept
92
{
93
return
{
this
, yield};
94
}
95
96
void
97
requestStop() noexcept
98
{
99
shared_->requestStop();
100
}
101
};
102
103
class
BasicStopSource
{
104
SharedStopState shared_ = std::make_shared<StopState>();
105
106
public
:
107
class
Token
{
108
friend
class
BasicStopSource
;
109
SharedStopState shared_;
110
111
explicit
Token
(
BasicStopSource
* source) : shared_{source->shared_}
112
{
113
}
114
115
public
:
116
Token
(
Token
const
&) =
default
;
117
Token
(
Token
&&) =
default
;
118
[[nodiscard]]
bool
119
120
isStopRequested()
const
noexcept
121
{
122
return
shared_->isStopRequested();
123
}
124
125
[[nodiscard]]
operator
bool()
const
noexcept
126
{
127
return
isStopRequested();
128
}
129
};
130
131
[[nodiscard]]
Token
132
getToken()
133
{
134
return
Token
{
this
};
135
}
136
137
void
138
requestStop()
139
{
140
shared_->requestStop();
141
}
142
};
143
144
}
// namespace util::async::impl
util::async::impl::BasicStopSource::Token
Definition
Cancellation.hpp:107
util::async::impl::BasicStopSource
Definition
Cancellation.hpp:103
util::async::impl::StopState
Definition
Cancellation.hpp:34
util::async::impl::YieldContextStopSource::Token
Definition
Cancellation.hpp:57
util::async::impl::YieldContextStopSource
Definition
Cancellation.hpp:53
src
util
async
context
impl
Cancellation.hpp
Generated by
1.12.0