userver: userver/concurrent/background_task_storage.hpp Source File
Loading...
Searching...
No Matches
background_task_storage.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/concurrent/background_task_storage.hpp
4/// @brief @copybrief concurrent::BackgroundTaskStorage
5
6#include <cstdint>
7#include <utility>
8
9#include <userver/engine/impl/detached_tasks_sync_block.hpp>
10#include <userver/engine/task/task_processor_fwd.hpp>
11#include <userver/utils/async.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace concurrent {
16
17/// @ingroup userver_concurrency userver_containers
18///
19/// A version of concurrent::BackgroundTaskStorage for advanced use cases (e.g.
20/// driver internals) that can take the ownership of any kind of task.
21class BackgroundTaskStorageCore final {
22 public:
23 /// Creates an empty BTS.
25
26 BackgroundTaskStorageCore(BackgroundTaskStorageCore&&) = delete;
27 BackgroundTaskStorageCore& operator=(BackgroundTaskStorageCore&&) = delete;
28 ~BackgroundTaskStorageCore();
29
30 /// Explicitly cancel and wait for the tasks. New tasks must not be launched
31 /// after this call returns. Should be called no more than once.
32 void CancelAndWait() noexcept;
33
34 /// @brief Detaches task, allowing it to continue execution out of scope. It
35 /// will be cancelled and waited for on BTS destruction.
36 /// @note After detach, Task becomes invalid
37 void Detach(engine::Task&& task);
38
39 /// Approximate number of currently active tasks
40 std::int64_t ActiveTasksApprox() const noexcept;
41
42 private:
43 std::optional<engine::impl::DetachedTasksSyncBlock> sync_block_;
44};
45
46/// @ingroup userver_concurrency userver_containers
47///
48/// A storage that allows one to start detached tasks; cancels and waits for
49/// unfinished tasks completion at the destructor. Provides CancelAndWait to
50/// explicitly cancel tasks (recommended).
51///
52/// Usable for detached tasks that capture references to resources with a
53/// limited lifetime. You must guarantee that the resources are available while
54/// the BackgroundTaskStorage is alive.
55///
56/// ## Usage synopsis
57/// @snippet concurrent/background_task_storage_test.cpp Sample
58class BackgroundTaskStorage final {
59 public:
60 /// Creates a BTS that launches tasks in the engine::TaskProcessor used at the
61 /// BTS creation.
63
64 /// Creates a BTS that launches tasks in the specified engine::TaskProcessor.
65 explicit BackgroundTaskStorage(engine::TaskProcessor& task_processor);
66
67 BackgroundTaskStorage(const BackgroundTaskStorage&) = delete;
68 BackgroundTaskStorage& operator=(const BackgroundTaskStorage&) = delete;
69
70 /// Explicitly cancel and wait for the tasks. New tasks must not be launched
71 /// after this call returns. Should be called no more than once.
72 void CancelAndWait() noexcept;
73
74 /// @brief Launch a task that will be cancelled and waited for in the BTS
75 /// destructor.
76 ///
77 /// The task is started as non-Critical, it may be cancelled due to
78 /// `TaskProcessor` overload. engine::TaskInheritedVariable instances are not
79 /// inherited from the caller. See utils::AsyncBackground for details.
80 template <typename... Args>
81 void AsyncDetach(std::string name, Args&&... args) {
82 core_.Detach(utils::AsyncBackground(std::move(name), task_processor_,
83 std::forward<Args>(args)...));
84 }
85
86 /// Approximate number of currently active tasks
87 std::int64_t ActiveTasksApprox() const noexcept;
88
89 private:
90 BackgroundTaskStorageCore core_;
91 engine::TaskProcessor& task_processor_;
92};
93
94} // namespace concurrent
95
96USERVER_NAMESPACE_END