userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
cancel.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/engine/task/cancel.hpp
4
/// @brief Task cancellation helpers
5
6
#
include
<
cstdint
>
7
#
include
<
string_view
>
8
9
#
include
<
boost
/
smart_ptr
/
intrusive_ptr
.
hpp
>
10
11
#
include
<
userver
/
engine
/
deadline
.
hpp
>
12
13
USERVER_NAMESPACE_BEGIN
14
15
namespace
engine {
16
namespace
impl
{
17
class
TaskContext;
18
}
// namespace impl
19
20
/// Task cancellation reason
21
enum
class
TaskCancellationReason
:
std
::
uint8_t
{
22
kNone
,
///< Not cancelled
23
kUserRequest
,
///< User request
24
kDeadline
,
///< Deadline
25
kOverload
,
///< Task processor overload
26
kOOM
,
///< Not enough memory
27
kAbandoned
,
///< Task destructor is called before the payload finished
28
kShutdown
,
///< Task processor shutdown
29
};
30
31
class
Task;
32
class
TaskCancellationToken;
33
34
namespace
current_task
{
35
36
/// Checks for pending cancellation requests, use
37
/// engine::current_task::ShouldCancel() instead, as the latter respects
38
/// engine::TaskCancellationBlocker.
39
///
40
/// @see @ref task_cancellation_intro
41
bool
IsCancelRequested
()
noexcept
;
42
43
/// Checks for pending *non-blocked* cancellation requests
44
///
45
/// @see engine::TaskCancellationBlocker
46
/// @see @ref task_cancellation_intro
47
bool
ShouldCancel
()
noexcept
;
48
49
/// Returns task cancellation reason for the current task
50
/// @see @ref task_cancellation_intro
51
TaskCancellationReason
CancellationReason
()
noexcept
;
52
53
/// @brief \b Throws an exception if a cancellation request for this task is
54
/// pending.
55
///
56
/// @throws unspecified (non-std) exception if cancellation is pending and not
57
/// blocked
58
///
59
/// @warning catching this exception without a rethrow in the same scope leads
60
/// to undefined behavior.
61
/// @see @ref task_cancellation_intro
62
void
CancellationPoint
();
63
64
/// @brief Throw a standard exception if cancellation is pending and not blocked.
65
///
66
/// Unlike @ref engine::current_task::CancellationPoint, the exception can be handled
67
/// by `catch (const std::exception&)` without rethrowing. The cancellation request
68
/// remains pending; this function does not disable cancellation.
69
/// @throws engine::WaitInterruptedException If @ref engine::current_task::ShouldCancel returns true.
70
/// @see engine::TaskCancellationBlocker
71
void
CancellationPointWeak
();
72
73
/// Set deadline for the current task.
74
/// The task will be cancelled when the deadline is reached.
75
void
SetDeadline
(Deadline deadline);
76
77
/// @see engine::Task::RequestCancel
78
void
RequestCancel
();
79
80
/// @brief Return cancellation token for current coroutine.
81
/// @note Prefer engine::current_task::RequestCancel in most cases.
82
TaskCancellationToken
GetCancellationToken
();
83
84
}
// namespace current_task
85
86
/// Blocks cancellation for specific scopes, e.g. destructors.
87
/// Recursive, i.e. can be instantiated multiple times in a given call stack.
88
class
TaskCancellationBlocker
final
{
89
public
:
90
TaskCancellationBlocker();
91
~TaskCancellationBlocker();
92
93
TaskCancellationBlocker(
const
TaskCancellationBlocker&) =
delete
;
94
TaskCancellationBlocker(TaskCancellationBlocker&&) =
delete
;
95
TaskCancellationBlocker& operator=(
const
TaskCancellationBlocker&) =
delete
;
96
TaskCancellationBlocker& operator=(TaskCancellationBlocker&&) =
delete
;
97
98
private
:
99
impl
::TaskContext& context_;
100
const
bool
was_allowed_;
101
};
102
103
/// Returns a string representation of a cancellation reason
104
std::string_view
ToString
(
TaskCancellationReason
reason)
noexcept
;
105
106
/// @brief Cancellation token to given task object
107
///
108
/// Unlike Task, TaskCancellationToken object doesn't wait for task finish in
109
/// its destructor. It is allowed to outlive the task object it was created
110
/// from. However, as long as there is any cancellation token associated with
111
/// given task, some internal structures of a task will not be freed.
112
///
113
/// General rule: whenever possible, prefer using engine::Task object instead.
114
class
TaskCancellationToken
final
{
115
public
:
116
/// Creates an invalid TaskCancellationToken
117
TaskCancellationToken
()
noexcept
;
118
119
/// Creates a TaskCancellationToken associated with a task. The task must be
120
/// valid.
121
explicit
TaskCancellationToken
(Task& task);
122
123
TaskCancellationToken(
const
TaskCancellationToken&)
noexcept
;
124
TaskCancellationToken(TaskCancellationToken&&)
noexcept
;
125
TaskCancellationToken& operator=(
const
TaskCancellationToken&)
noexcept
;
126
TaskCancellationToken& operator=(TaskCancellationToken&&)
noexcept
;
127
~TaskCancellationToken();
128
129
/// @see engine::Task::RequestCancel
130
/// This method should not be called on invalid TaskCancellationToken
131
void
RequestCancel
();
132
133
/// @see engine::Task::CancellationReason
134
/// This method should not be called on invalid TaskCancellationToken
135
TaskCancellationReason
CancellationReason
()
const
noexcept
;
136
137
/// @see @ref task_cancellation_intro
138
/// True if there is pending cancellation request for the associated task
139
/// This method should not be called on invalid TaskCancellationToken
140
bool
IsCancelRequested
()
const
noexcept
;
141
142
/// True if this token is associated with a task
143
bool
IsValid
()
const
noexcept
;
144
145
private
:
146
friend
TaskCancellationToken
current_task
::
GetCancellationToken
();
147
148
explicit
TaskCancellationToken(
impl
::TaskContext& context)
noexcept
;
149
150
boost::intrusive_ptr<
impl
::TaskContext> context_;
151
};
152
153
}
// namespace engine
154
155
USERVER_NAMESPACE_END
userver
engine
task
cancel.hpp
Generated on
for userver by
Doxygen
1.17.0