userver: userver/engine/async.hpp Source File
Loading...
Searching...
No Matches
async.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/async.hpp
4/// @brief TaskWithResult creation helpers
5
6#include <userver/engine/deadline.hpp>
7#include <userver/engine/impl/task_context_factory.hpp>
8#include <userver/engine/task/shared_task_with_result.hpp>
9#include <userver/engine/task/task_processor_fwd.hpp>
10#include <userver/engine/task/task_with_result.hpp>
11#include <userver/utils/impl/wrapped_call.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace engine {
16
17namespace impl {
18
19template <template <typename> typename TaskType, typename Function,
20 typename... Args>
21[[nodiscard]] auto MakeTaskWithResult(TaskProcessor& task_processor,
22 Task::Importance importance,
23 Deadline deadline, Function&& f,
24 Args&&... args) {
25 using ResultType =
26 typename utils::impl::WrappedCallImplType<Function, Args...>::ResultType;
27 constexpr auto kWaitMode = TaskType<ResultType>::kWaitMode;
28
29 return TaskType<ResultType>{
30 MakeTask({task_processor, importance, kWaitMode, deadline},
31 std::forward<Function>(f), std::forward<Args>(args)...)};
32}
33
34} // namespace impl
35
36/// Runs an asynchronous function call using specified task processor
37template <typename Function, typename... Args>
38[[nodiscard]] auto AsyncNoSpan(TaskProcessor& task_processor, Function&& f,
39 Args&&... args) {
40 return impl::MakeTaskWithResult<TaskWithResult>(
41 task_processor, Task::Importance::kNormal, {}, std::forward<Function>(f),
42 std::forward<Args>(args)...);
43}
44
45/// Runs an asynchronous function call using specified task processor
46template <typename Function, typename... Args>
47[[nodiscard]] auto SharedAsyncNoSpan(TaskProcessor& task_processor,
48 Function&& f, Args&&... args) {
49 return impl::MakeTaskWithResult<SharedTaskWithResult>(
50 task_processor, Task::Importance::kNormal, {}, std::forward<Function>(f),
51 std::forward<Args>(args)...);
52}
53
54/// Runs an asynchronous function call with deadline using specified task
55/// processor
56template <typename Function, typename... Args>
57[[nodiscard]] auto AsyncNoSpan(TaskProcessor& task_processor, Deadline deadline,
58 Function&& f, Args&&... args) {
59 return impl::MakeTaskWithResult<TaskWithResult>(
60 task_processor, Task::Importance::kNormal, deadline,
61 std::forward<Function>(f), std::forward<Args>(args)...);
62}
63
64/// Runs an asynchronous function call with deadline using specified task
65/// processor
66template <typename Function, typename... Args>
67[[nodiscard]] auto SharedAsyncNoSpan(TaskProcessor& task_processor,
68 Deadline deadline, Function&& f,
69 Args&&... args) {
70 return impl::MakeTaskWithResult<SharedTaskWithResult>(
71 task_processor, Task::Importance::kNormal, deadline,
72 std::forward<Function>(f), std::forward<Args>(args)...);
73}
74
75/// Runs an asynchronous function call using task processor of the caller
76template <typename Function, typename... Args>
77[[nodiscard]] auto AsyncNoSpan(Function&& f, Args&&... args) {
78 return AsyncNoSpan(current_task::GetTaskProcessor(),
79 std::forward<Function>(f), std::forward<Args>(args)...);
80}
81
82/// Runs an asynchronous function call using task processor of the caller
83template <typename Function, typename... Args>
84[[nodiscard]] auto SharedAsyncNoSpan(Function&& f, Args&&... args) {
85 return SharedAsyncNoSpan(current_task::GetTaskProcessor(),
86 std::forward<Function>(f),
87 std::forward<Args>(args)...);
88}
89
90/// Runs an asynchronous function call with deadline using task processor of the
91/// caller
92template <typename Function, typename... Args>
93[[nodiscard]] auto AsyncNoSpan(Deadline deadline, Function&& f,
94 Args&&... args) {
95 return AsyncNoSpan(current_task::GetTaskProcessor(), deadline,
96 std::forward<Function>(f), std::forward<Args>(args)...);
97}
98
99/// Runs an asynchronous function call with deadline using task processor of the
100/// caller
101template <typename Function, typename... Args>
102[[nodiscard]] auto SharedAsyncNoSpan(Deadline deadline, Function&& f,
103 Args&&... args) {
104 return SharedAsyncNoSpan(current_task::GetTaskProcessor(), deadline,
105 std::forward<Function>(f),
106 std::forward<Args>(args)...);
107}
108
109/// @brief Runs an asynchronous function call that will start regardless of
110/// cancellations using specified task processor
111/// @see Task::Importance::Critical
112template <typename Function, typename... Args>
113[[nodiscard]] auto CriticalAsyncNoSpan(TaskProcessor& task_processor,
114 Function&& f, Args&&... args) {
115 return impl::MakeTaskWithResult<TaskWithResult>(
116 task_processor, Task::Importance::kCritical, {},
117 std::forward<Function>(f), std::forward<Args>(args)...);
118}
119
120/// @brief Runs an asynchronous function call that will start regardless of
121/// cancellations using specified task processor
122/// @see Task::Importance::Critical
123template <typename Function, typename... Args>
124[[nodiscard]] auto SharedCriticalAsyncNoSpan(TaskProcessor& task_processor,
125 Function&& f, Args&&... args) {
126 return impl::MakeTaskWithResult<SharedTaskWithResult>(
127 task_processor, Task::Importance::kCritical, {},
128 std::forward<Function>(f), std::forward<Args>(args)...);
129}
130
131/// @brief Runs an asynchronous function call that will start regardless of
132/// cancellations using task processor of the caller
133/// @see Task::Importance::Critical
134template <typename Function, typename... Args>
135[[nodiscard]] auto CriticalAsyncNoSpan(Function&& f, Args&&... args) {
136 return CriticalAsyncNoSpan(current_task::GetTaskProcessor(),
137 std::forward<Function>(f),
138 std::forward<Args>(args)...);
139}
140
141/// @brief Runs an asynchronous function call that will start regardless of
142/// cancellations using task processor of the caller
143/// @see Task::Importance::Critical
144template <typename Function, typename... Args>
145[[nodiscard]] auto SharedCriticalAsyncNoSpan(Function&& f, Args&&... args) {
146 return SharedCriticalAsyncNoSpan(current_task::GetTaskProcessor(),
147 std::forward<Function>(f),
148 std::forward<Args>(args)...);
149}
150
151/// @brief Runs an asynchronous function call that will start regardless of
152/// cancellations, using task processor of the caller, with deadline
153/// @see Task::Importance::Critical
154template <typename Function, typename... Args>
155[[nodiscard]] auto CriticalAsyncNoSpan(Deadline deadline, Function&& f,
156 Args&&... args) {
157 return impl::MakeTaskWithResult<TaskWithResult>(
159 std::forward<Function>(f), std::forward<Args>(args)...);
160}
161
162} // namespace engine
163
164USERVER_NAMESPACE_END