userver: userver/engine/task/cancel.hpp Source File
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
13USERVER_NAMESPACE_BEGIN
14
15namespace engine {
16namespace impl {
17class TaskContext;
18} // namespace impl
19
20/// Task cancellation reason
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
31class Task;
32class TaskCancellationToken;
33
34namespace 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
41bool IsCancelRequested() noexcept;
42
43/// Checks for pending *non-blocked* cancellation requests
44///
45/// @see engine::TaskCancellationBlocker
46/// @see @ref task_cancellation_intro
47bool ShouldCancel() noexcept;
48
49/// Returns task cancellation reason for the current task
50/// @see @ref task_cancellation_intro
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
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
72
73/// Set deadline for the current task.
74/// The task will be cancelled when the deadline is reached.
75void SetDeadline(Deadline deadline);
76
77/// @see engine::Task::RequestCancel
79
80/// @brief Return cancellation token for current coroutine.
81/// @note Prefer engine::current_task::RequestCancel in most cases.
82TaskCancellationToken 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.
88class TaskCancellationBlocker final {
89public:
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
98private:
99 impl::TaskContext& context_;
100 const bool was_allowed_;
101};
102
103/// Returns a string representation of a cancellation reason
104std::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.
114class TaskCancellationToken final {
115public:
116 /// Creates an invalid TaskCancellationToken
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
132
133 /// @see engine::Task::CancellationReason
134 /// This method should not be called on invalid TaskCancellationToken
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
145private:
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
155USERVER_NAMESPACE_END