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, typename... Args>
20[[nodiscard]] auto MakeTaskWithResult(
21 TaskProcessor& task_processor,
22 Task::Importance importance,
23 Deadline deadline,
24 Function&& f,
25 Args&&... args
26) {
27 using ResultType = typename utils::impl::WrappedCallImplType<Function, Args...>::ResultType;
28 constexpr auto kWaitMode = TaskType<ResultType>::kWaitMode;
29
30 return TaskType<ResultType>{MakeTask(
31 {task_processor, importance, kWaitMode, deadline}, std::forward<Function>(f), std::forward<Args>(args)...
32 )};
33}
34
35} // namespace impl
36
37/// Runs an asynchronous function call using specified task processor
38template <typename Function, typename... Args>
39[[nodiscard]] auto AsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
40 return impl::MakeTaskWithResult<TaskWithResult>(
41 task_processor, Task::Importance::kNormal, {}, std::forward<Function>(f), std::forward<Args>(args)...
42 );
43}
44
45/// Runs an asynchronous function call using specified task processor
46template <typename Function, typename... Args>
47[[nodiscard]] auto SharedAsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
48 return impl::MakeTaskWithResult<SharedTaskWithResult>(
49 task_processor, Task::Importance::kNormal, {}, std::forward<Function>(f), std::forward<Args>(args)...
50 );
51}
52
53/// Runs an asynchronous function call with deadline using specified task
54/// processor
55template <typename Function, typename... Args>
56[[nodiscard]] auto AsyncNoSpan(TaskProcessor& task_processor, Deadline deadline, Function&& f, Args&&... args) {
57 return impl::MakeTaskWithResult<TaskWithResult>(
58 task_processor, Task::Importance::kNormal, deadline, std::forward<Function>(f), std::forward<Args>(args)...
59 );
60}
61
62/// Runs an asynchronous function call with deadline using specified task
63/// processor
64template <typename Function, typename... Args>
65[[nodiscard]] auto SharedAsyncNoSpan(TaskProcessor& task_processor, Deadline deadline, Function&& f, Args&&... args) {
66 return impl::MakeTaskWithResult<SharedTaskWithResult>(
67 task_processor, Task::Importance::kNormal, deadline, std::forward<Function>(f), std::forward<Args>(args)...
68 );
69}
70
71/// Runs an asynchronous function call using task processor of the caller
72template <typename Function, typename... Args>
73[[nodiscard]] auto AsyncNoSpan(Function&& f, Args&&... args) {
74 return AsyncNoSpan(current_task::GetTaskProcessor(), std::forward<Function>(f), std::forward<Args>(args)...);
75}
76
77/// Runs an asynchronous function call using task processor of the caller
78template <typename Function, typename... Args>
79[[nodiscard]] auto SharedAsyncNoSpan(Function&& f, Args&&... args) {
80 return SharedAsyncNoSpan(current_task::GetTaskProcessor(), std::forward<Function>(f), std::forward<Args>(args)...);
81}
82
83/// Runs an asynchronous function call with deadline using task processor of the
84/// caller
85template <typename Function, typename... Args>
86[[nodiscard]] auto AsyncNoSpan(Deadline deadline, Function&& f, Args&&... args) {
87 return AsyncNoSpan(
88 current_task::GetTaskProcessor(), deadline, std::forward<Function>(f), std::forward<Args>(args)...
89 );
90}
91
92/// Runs an asynchronous function call with deadline using task processor of the
93/// caller
94template <typename Function, typename... Args>
95[[nodiscard]] auto SharedAsyncNoSpan(Deadline deadline, Function&& f, Args&&... args) {
96 return SharedAsyncNoSpan(
97 current_task::GetTaskProcessor(), deadline, std::forward<Function>(f), std::forward<Args>(args)...
98 );
99}
100
101/// @brief Runs an asynchronous function call that will start regardless of
102/// cancellations using specified task processor
103/// @see Task::Importance::Critical
104template <typename Function, typename... Args>
105[[nodiscard]] auto CriticalAsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
106 return impl::MakeTaskWithResult<TaskWithResult>(
107 task_processor, Task::Importance::kCritical, {}, std::forward<Function>(f), std::forward<Args>(args)...
108 );
109}
110
111/// @brief Runs an asynchronous function call that will start regardless of
112/// cancellations using specified task processor
113/// @see Task::Importance::Critical
114template <typename Function, typename... Args>
115[[nodiscard]] auto SharedCriticalAsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
116 return impl::MakeTaskWithResult<SharedTaskWithResult>(
117 task_processor, Task::Importance::kCritical, {}, std::forward<Function>(f), std::forward<Args>(args)...
118 );
119}
120
121/// @brief Runs an asynchronous function call that will start regardless of
122/// cancellations using task processor of the caller
123/// @see Task::Importance::Critical
124template <typename Function, typename... Args>
125[[nodiscard]] auto CriticalAsyncNoSpan(Function&& f, Args&&... args) {
126 return CriticalAsyncNoSpan(
127 current_task::GetTaskProcessor(), std::forward<Function>(f), std::forward<Args>(args)...
128 );
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 SharedCriticalAsyncNoSpan(Function&& f, Args&&... args) {
136 return SharedCriticalAsyncNoSpan(
137 current_task::GetTaskProcessor(), std::forward<Function>(f), std::forward<Args>(args)...
138 );
139}
140
141/// @brief Runs an asynchronous function call that will start regardless of
142/// cancellations, using task processor of the caller, with deadline
143/// @see Task::Importance::Critical
144template <typename Function, typename... Args>
145[[nodiscard]] auto CriticalAsyncNoSpan(Deadline deadline, Function&& f, Args&&... args) {
146 return impl::MakeTaskWithResult<TaskWithResult>(
149 deadline,
150 std::forward<Function>(f),
151 std::forward<Args>(args)...
152 );
153}
154
155} // namespace engine
156
157USERVER_NAMESPACE_END