#include <userver/concurrent/background_task_storage.hpp>
A storage that allows one to start detached tasks; cancels and waits for unfinished tasks completion at the destructor. Provides CancelAndWait to explicitly cancel tasks (recommended).
Usable for detached tasks that capture references to resources with a limited lifetime. You must guarantee that the resources are available while the BackgroundTaskStorage is alive.
Tasks remove themselves from the BackgroundTaskStorage on completion, so there is no memory leak.
All the advice from utils::Async is applicable here.
BackgroundTaskStorage is always stored as a class field. Tasks that are launched inside it (or moved inside it, for BackgroundTaskStorageCore) can safely access fields declared before it, but not after it:
Generally, it's a good idea to declare bts_ after most other fields to avoid lifetime bugs. An example of fool-proof code:
Components and their clients can always be safely captured by reference:
So for a BackgroundTaskStorage stored in a component, its tasks can only safely use the fields declared before the BTS field, as well as everything from the components, on which the current component depends.
Definition at line 116 of file background_task_storage.hpp.
Public Member Functions | |
| BackgroundTaskStorage () | |
| BackgroundTaskStorage (engine::TaskProcessor &task_processor) | |
| Creates a BTS that launches tasks in the specified engine::TaskProcessor. | |
| BackgroundTaskStorage (const BackgroundTaskStorage &)=delete | |
| BackgroundTaskStorage & | operator= (const BackgroundTaskStorage &)=delete |
| void | CancelAndWait () noexcept |
| Explicitly cancel and wait for the tasks. New tasks must not be launched after this call returns. | |
| void | WaitAndDisposeSlow () noexcept |
| Wait for the remaining tasks to complete without cancelling them. New tasks must not be launched after this call returns. | |
| template<typename... Args> | |
| void | AsyncDetach (std::string name, Args &&... args) |
| Launch a task that will be cancelled and waited for in the BTS destructor. | |
| template<typename... Args> | |
| void | CriticalAsyncDetach (std::string name, Args &&... args) |
| Launch a task that will be cancelled and waited for in the BTS destructor. | |
| std::int64_t | ActiveTasksApprox () const noexcept |
| concurrent::BackgroundTaskStorage::BackgroundTaskStorage | ( | ) |
Creates a BTS that launches tasks in the engine::TaskProcessor used at the BTS creation.
|
noexcept |
Can be called from a coroutine or a non-coroutine thread. Cannot be called after CancelAndWait or WaitAndDisposeSlow has been called.
|
inline |
Launch a task that will be cancelled and waited for in the BTS destructor.
The task is started as non-Critical, it may be cancelled due to TaskProcessor overload. engine::TaskInheritedVariable instances are not inherited from the caller except baggage::Baggage. See utils::AsyncBackground for details.
Can be called from a coroutine or a non-coroutine thread.
Definition at line 162 of file background_task_storage.hpp.
|
noexcept |
Explicitly cancel and wait for the tasks. New tasks must not be launched after this call returns.
More precisely, new tasks must not be launched once this call is completed, but can be launched in the process of waiting for the remaining tasks. This means that one detached task is allowed to spawn another detached task into the same BTS, which will immediately be cancelled and for which this call will wait as well.
Can only be called from a coroutine.
|
inline |
Launch a task that will be cancelled and waited for in the BTS destructor.
Execution of function is guaranteed to start regardless of engine::TaskProcessor load limits. engine::TaskInheritedVariable instances are not inherited from the caller except baggage::Baggage. See utils::CriticalAsyncBackground for details.
Can be called from a coroutine or a non-coroutine thread.
Definition at line 177 of file background_task_storage.hpp.
|
noexcept |
Wait for the remaining tasks to complete without cancelling them. New tasks must not be launched after this call returns.
More precisely, new tasks must not be launched once this call is completed, but can be launched in the process of waiting for the remaining tasks. This means that one detached task is allowed to spawn another detached task into the same BTS, for which this call will wait as well.
Can only be called from a coroutine.