userver: userver/engine/task/shared_task.hpp Source File
Loading...
Searching...
No Matches
shared_task.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/task/shared_task.hpp
4/// @brief @copybrief engine::SharedTask
5
6#include <memory>
7#include <type_traits>
8#include <utility>
9
10#include <userver/engine/impl/task_context_holder.hpp>
11#include <userver/engine/task/task.hpp>
12#include <userver/engine/task/task_processor_fwd.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace engine {
17
18/// @brief Asynchronous task that has a shared ownership of the payload.
19///
20/// See engine::SharedTaskWithResult for a type that could return a value or
21/// report an exception from the payload.
22class [[nodiscard]] SharedTask : public TaskBase {
23public:
24 /// @brief Default constructor
25 ///
26 /// Creates an invalid task.
28
29 /// @brief If the task is still valid and is not finished and this is the last
30 /// shared owner of the payload, cancels the task and waits until it finishes.
32
33 /// @brief Assigns the other task into this.
34 SharedTask(const SharedTask& other) noexcept;
35
36 /// @brief If this task is still valid and is not finished and other task is
37 /// not the same task as this and this is the
38 /// last shared owner of the payload, cancels the task and waits until it
39 /// finishes before assigning the other. Otherwise just assigns the other task
40 /// into this.
41 SharedTask& operator=(const SharedTask& other) noexcept;
42
43 /// @brief Moves the other task into this, leaving the other in an invalid
44 /// state.
45 SharedTask(SharedTask&& other) noexcept;
46
47 /// @brief If this task is still valid and is not finished and other task is
48 /// not the same task as this and this is the
49 /// last shared owner of the payload, cancels the task and waits until it
50 /// finishes before move assigning the other. Otherwise just move assigns the
51 /// other task into this, leaving the other in an invalid state.
52 SharedTask& operator=(SharedTask&& other) noexcept;
53
54 /// @cond
55 static constexpr WaitMode kWaitMode = WaitMode::kMultipleWaiters;
56
57protected:
58 // For internal use only.
59 explicit SharedTask(impl::TaskContextHolder&& context);
60 /// @endcond
61
62private:
63 void DecrementSharedUsages() noexcept;
64 void IncrementSharedUsages() noexcept;
65};
66
67} // namespace engine
68
69USERVER_NAMESPACE_END