#include <userver/engine/wait_any.hpp>
Stores a set of awaitables and allows waiting for completion of any of the stored awaitables.
Works with different types of awaitables:
The methods are not thread-safe: they may be called from at most one task at a time.
No methods (except .dtor) should be called on a moved-out instance.
Definition at line 136 of file wait_any.hpp.
Classes | |
| class | Handle |
| Identifies an awaitable appended to the context, see Remove. More... | |
Public Member Functions | |
| WaitAnyContext (WaitAnyContext &&) noexcept | |
| WaitAnyContext & | operator= (WaitAnyContext &&other) noexcept |
| WaitAnyContext (const WaitAnyContext &)=delete | |
| WaitAnyContext & | operator= (const WaitAnyContext &)=delete |
| Handle | Append (engine::Awaitable auto &awaitable) |
| Appends a single awaitable to the context. | |
| Handle | Append (std::uint64_t id, engine::Awaitable auto &awaitable) |
| Appends the given awaitable to the context with an explicit id. | |
| utils::expected< std::uint64_t, WaitAnyError > | Wait () |
| Waits either for the completion of any of the awaitables stored in the context or for the cancellation of the caller. | |
| utils::expected< std::uint64_t, WaitAnyError > | WaitUntil (Deadline deadline) |
| Waits for the completion of any of the awaitables stored in the context or cancellation of the caller or deadline expiration. | |
| template<typename Rep, typename Period> | |
| utils::expected< std::uint64_t, WaitAnyError > | WaitFor (const std::chrono::duration< Rep, Period > &duration) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| template<typename Clock, typename Duration> | |
| utils::expected< std::uint64_t, WaitAnyError > | WaitUntil (const std::chrono::time_point< Clock, Duration > &until) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| void | Remove (Handle handle) noexcept |
| Removes the awaitable identified by the handle from the context. | |
| std::size_t | GetSize () const noexcept |
| Returns the number of awaitables stored in the context. | |
| std::uint64_t | GetNextId () const noexcept |
| Returns the next id that will be assigned by the next Append call. | |
| WaitAnyContext::Handle engine::WaitAnyContext::Append | ( | engine::Awaitable auto & | awaitable | ) |
Appends a single awaitable to the context.
The appended awaitable will be assigned the next id from the auto-incrementing counter; see also GetNextId.
Definition at line 261 of file wait_any.hpp.
| WaitAnyContext::Handle engine::WaitAnyContext::Append | ( | std::uint64_t | id, |
| engine::Awaitable auto & | awaitable ) |
Appends the given awaitable to the context with an explicit id.
The id is an arbitrary number that will be returned by Wait. It does not have to be sequential or starting from 0. Duplicate ids are allowed.
GetNextId will be unaffected by this call. When mixing the id-less Append and this overload, the id-less Append maintains a sequence only within its own calls.
Works well together with utils::SlotMap to process and erase tasks in completion order:
| id | the id that will be returned by Wait. |
| awaitable | the awaitable to append. |
Definition at line 265 of file wait_any.hpp.
|
noexcept |
|
noexcept |
|
noexcept |
Removes the awaitable identified by the handle from the context.
The removed awaitable will not be reported by Wait* and may be destroyed right after the call, no matter whether it is ready or not. An empty handle is ignored.
The awaitable must still be stored in the context: removing an awaitable that has already been reported by Wait* or removed is not allowed. Such a call aborts the program, but this detection is not guaranteed.
The complexity is constant.
| handle | the handle returned by Append. |
| utils::expected< std::uint64_t, WaitAnyError > engine::WaitAnyContext::Wait | ( | ) |
Waits either for the completion of any of the awaitables stored in the context or for the cancellation of the caller.
The completed awaitable is dropped out of the context.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 217 of file wait_any.hpp.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 223 of file wait_any.hpp.
| utils::expected< std::uint64_t, WaitAnyError > engine::WaitAnyContext::WaitUntil | ( | Deadline | deadline | ) |
Waits for the completion of any of the awaitables stored in the context or cancellation of the caller or deadline expiration.
The completed awaitable is dropped from the context.