source: mainline/uspace/lib/cpp/include/__bits/thread/async.hpp@ c6f23a7

Last change on this file since c6f23a7 was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2019 Jaroslav Jindrak
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef LIBCPP_BITS_THREAD_ASYNC
8#define LIBCPP_BITS_THREAD_ASYNC
9
10#include <__bits/thread/future.hpp>
11#include <__bits/thread/future_common.hpp>
12#include <__bits/thread/shared_state.hpp>
13#include <__bits/type_traits/result_of.hpp>
14#include <__bits/utility/forward_move.hpp>
15#include <cassert>
16
17namespace std
18{
19
20 enum class launch
21 {
22 async = 1,
23 deferred
24 };
25
26 namespace aux
27 {
28 /**
29 * Note: The reason we keep the actual function
30 * within the aux namespace is that were the non-policy
31 * version of the function call the other one in the std
32 * namespace, we'd get resolution conflicts. This way
33 * aux::async is properly called even if std::async is
34 * called either with or without a launch policy.
35 */
36 template<class F, class... Args>
37 future<result_of_t<decay_t<F>(decay_t<Args>...)>>
38 async(launch policy, F&& f, Args&&... args)
39 {
40 using result_t = result_of_t<decay_t<F>(decay_t<Args>...)>;
41
42 bool async = (static_cast<int>(policy) &
43 static_cast<int>(launch::async)) != 0;
44 bool deferred = (static_cast<int>(policy) &
45 static_cast<int>(launch::deferred)) != 0;
46
47 /**
48 * Note: The case when async | deferred is set in policy
49 * is implementation defined, feel free to change.
50 * Rationale: We chose the 'deferred' policy, because unlike
51 * the 'async' policy it carries no possible
52 * surprise ('async' can fail due to thread
53 * creation error).
54 */
55 if (async && deferred)
56 {
57 return future<result_t>{
58 new aux::deferred_shared_state<
59 result_t, F, Args...
60 >{forward<F>(f), forward<Args>(args)...}
61 };
62 }
63 else if (async)
64 {
65 return future<result_t>{
66 new aux::async_shared_state<
67 result_t, F, Args...
68 >{forward<F>(f), forward<Args>(args)...}
69 };
70 }
71 else if (deferred)
72 {
73 /**
74 * Duplicated on purpose because of the default.
75 * Do not remove!
76 */
77 return future<result_t>{
78 new aux::deferred_shared_state<
79 result_t, F, Args...
80 >{forward<F>(f), forward<Args>(args)...}
81 };
82 }
83
84 /**
85 * This is undefined behaviour, abandon ship!
86 */
87 abort();
88 }
89 }
90
91 template<class F>
92 decltype(auto) async(F&& f)
93 {
94 launch policy = static_cast<launch>(
95 static_cast<int>(launch::async) |
96 static_cast<int>(launch::deferred)
97 );
98
99 return aux::async(policy, forward<F>(f));
100 }
101
102 /**
103 * The async(launch, F, Args...) and async(F, Args...)
104 * overloards must not collide, so we check the first template
105 * argument and handle the special case of just a functor
106 * above.
107 */
108 template<class F, class Arg, class... Args>
109 decltype(auto) async(F&& f, Arg&& arg, Args&&... args)
110 {
111 if constexpr (is_same_v<decay_t<F>, launch>)
112 return aux::async(f, forward<Arg>(arg), forward<Args>(args)...);
113 else
114 {
115 launch policy = static_cast<launch>(
116 static_cast<int>(launch::async) |
117 static_cast<int>(launch::deferred)
118 );
119
120 return aux::async(policy, forward<F>(f), forward<Arg>(arg), forward<Args>(args)...);
121 }
122 }
123}
124
125#endif
Note: See TracBrowser for help on using the repository browser.