|
13 | 13 |
|
14 | 14 | #include <userver/engine/awaitable.hpp> |
15 | 15 | #include <userver/engine/deadline.hpp> |
| 16 | +#include <userver/utils/expected.hpp> |
16 | 17 | #include <userver/utils/fast_pimpl.hpp> |
17 | 18 | #include <userver/utils/meta.hpp> |
18 | 19 | #include <userver/utils/span.hpp> |
@@ -113,6 +114,15 @@ std::optional<std::size_t> WaitAnyUntil(Deadline deadline, Tasks&... tasks) { |
113 | 114 | } |
114 | 115 | } |
115 | 116 |
|
| 117 | +/// @ingroup userver_concurrency |
| 118 | +/// |
| 119 | +/// @brief The reason why @ref WaitAnyContext::Wait and friends did not return the index of a completed awaitable. |
| 120 | +enum class WaitAnyError : std::uint8_t { |
| 121 | + kEmpty, ///< there were no awaitables to wait for |
| 122 | + kCancelled, ///< the wait operation was interrupted by task cancellation |
| 123 | + kTimeout, ///< the wait operation timed out (see also @ref FutureStatus) |
| 124 | +}; |
| 125 | + |
116 | 126 | /// @ingroup userver_concurrency |
117 | 127 | /// |
118 | 128 | /// @brief Stores a set of awaitables and allows waiting for completion of any of the stored awaitables. |
@@ -154,30 +164,32 @@ class WaitAnyContext final { |
154 | 164 | /// @brief Waits either for the completion of any of the awaitables stored in the context |
155 | 165 | /// or for the cancellation of the caller. |
156 | 166 | /// |
157 | | - /// @returns the index of the completed awaitable, or `std::nullopt` if there are no |
158 | | - /// completed awaitables (possible if current task was cancelled). |
159 | | - std::optional<std::uint64_t> Wait(); |
| 167 | + /// @returns the index of the completed awaitable, or a @ref WaitAnyError if there are no |
| 168 | + /// completed awaitables (possible if current task was cancelled or the context is empty). |
| 169 | + utils::expected<std::uint64_t, WaitAnyError> Wait(); |
160 | 170 |
|
161 | 171 | /// @brief Waits for the completion of any of the awaitables stored in the context |
162 | 172 | /// or cancellation of the caller or deadline expiration. |
163 | 173 | /// |
164 | | - /// @returns the index of the completed awaitable, or `std::nullopt` if there are no |
165 | | - /// completed awaitables (possible if current task was cancelled or the deadline was reached). |
166 | | - std::optional<std::uint64_t> WaitUntil(Deadline deadline); |
| 174 | + /// @returns the index of the completed awaitable, or a @ref WaitAnyError if there are no |
| 175 | + /// completed awaitables (possible if current task was cancelled, the deadline was reached, |
| 176 | + /// or the context is empty). |
| 177 | + utils::expected<std::uint64_t, WaitAnyError> WaitUntil(Deadline deadline); |
167 | 178 |
|
168 | 179 | /// @brief Waits for the completion of any of the awaitables stored in the context |
169 | 180 | /// or cancellation of the caller or expiration of the given duration. |
170 | 181 | /// |
171 | | - /// @returns the index of the completed awaitable, or `std::nullopt` if there are no |
172 | | - /// completed awaitables (possible if current task was cancelled or the given duration has passed). |
| 182 | + /// @returns the index of the completed awaitable, or a @ref WaitAnyError if there are no |
| 183 | + /// completed awaitables (possible if current task was cancelled, the given duration has passed, |
| 184 | + /// or the context is empty). |
173 | 185 | template <typename Rep, typename Period> |
174 | | - std::optional<std::uint64_t> WaitFor(const std::chrono::duration<Rep, Period>& duration) { |
| 186 | + utils::expected<std::uint64_t, WaitAnyError> WaitFor(const std::chrono::duration<Rep, Period>& duration) { |
175 | 187 | return WaitUntil(Deadline::FromDuration(duration)); |
176 | 188 | } |
177 | 189 |
|
178 | | - /// @overload std::optional<std::size_t> Wait() |
| 190 | + /// @overload utils::expected<std::uint64_t, WaitAnyError> Wait() |
179 | 191 | template <typename Clock, typename Duration> |
180 | | - std::optional<std::uint64_t> WaitUntil(const std::chrono::time_point<Clock, Duration>& until) { |
| 192 | + utils::expected<std::uint64_t, WaitAnyError> WaitUntil(const std::chrono::time_point<Clock, Duration>& until) { |
181 | 193 | return WaitUntil(Deadline::FromTimePoint(until)); |
182 | 194 | } |
183 | 195 |
|
|
0 commit comments