1 | /*
|
---|
2 | * Copyright (c) 2019 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_BITS_THREAD_ASYNC
|
---|
30 | #define LIBCPP_BITS_THREAD_ASYNC
|
---|
31 |
|
---|
32 | #include <__bits/thread/future.hpp>
|
---|
33 | #include <__bits/thread/future_common.hpp>
|
---|
34 | #include <__bits/thread/shared_state.hpp>
|
---|
35 | #include <__bits/type_traits/result_of.hpp>
|
---|
36 | #include <__bits/utility/forward_move.hpp>
|
---|
37 |
|
---|
38 | namespace std
|
---|
39 | {
|
---|
40 |
|
---|
41 | enum class launch
|
---|
42 | {
|
---|
43 | async = 1,
|
---|
44 | deferred
|
---|
45 | };
|
---|
46 |
|
---|
47 | namespace aux
|
---|
48 | {
|
---|
49 | /**
|
---|
50 | * Note: The reason we keep the actual function
|
---|
51 | * within the aux namespace is that were the non-policy
|
---|
52 | * version of the function call the other one in the std
|
---|
53 | * namespace, we'd get resolution conflicts. This way
|
---|
54 | * aux::async is properly called even if std::async is
|
---|
55 | * called either with or without a launch policy.
|
---|
56 | */
|
---|
57 | template<class F, class... Args>
|
---|
58 | future<result_of_t<decay_t<F>(decay_t<Args>...)>>
|
---|
59 | async(launch policy, F&& f, Args&&... args)
|
---|
60 | {
|
---|
61 | using result_t = result_of_t<decay_t<F>(decay_t<Args>...)>;
|
---|
62 |
|
---|
63 | bool async = (static_cast<int>(policy) &
|
---|
64 | static_cast<int>(launch::async)) != 0;
|
---|
65 | bool deferred = (static_cast<int>(policy) &
|
---|
66 | static_cast<int>(launch::deferred)) != 0;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Note: The case when async | deferred is set in policy
|
---|
70 | * is implementation defined, feel free to change.
|
---|
71 | * Rationale: We chose the 'deferred' policy, because unlike
|
---|
72 | * the 'async' policy it carries no possible
|
---|
73 | * surprise ('async' can fail due to thread
|
---|
74 | * creation error).
|
---|
75 | */
|
---|
76 | if (async && deferred)
|
---|
77 | {
|
---|
78 | return future<result_t>{
|
---|
79 | new aux::deferred_shared_state<
|
---|
80 | result_t, F, Args...
|
---|
81 | >{forward<F>(f), forward<Args>(args)...}
|
---|
82 | };
|
---|
83 | }
|
---|
84 | else if (async)
|
---|
85 | {
|
---|
86 | return future<result_t>{
|
---|
87 | new aux::async_shared_state<
|
---|
88 | result_t, F, Args...
|
---|
89 | >{forward<F>(f), forward<Args>(args)...}
|
---|
90 | };
|
---|
91 | }
|
---|
92 | else if (deferred)
|
---|
93 | {
|
---|
94 | /**
|
---|
95 | * Duplicated on purpose because of the default.
|
---|
96 | * Do not remove!
|
---|
97 | */
|
---|
98 | return future<result_t>{
|
---|
99 | new aux::deferred_shared_state<
|
---|
100 | result_t, F, Args...
|
---|
101 | >{forward<F>(f), forward<Args>(args)...}
|
---|
102 | };
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * This is undefined behaviour, let's be nice though ;)
|
---|
107 | */
|
---|
108 | return future<result_t>{
|
---|
109 | new aux::deferred_shared_state<
|
---|
110 | result_t, F, Args...
|
---|
111 | >{forward<F>(f), forward<Args>(args)...}
|
---|
112 | };
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | template<class F>
|
---|
117 | decltype(auto) async(F&& f)
|
---|
118 | {
|
---|
119 | launch policy = static_cast<launch>(
|
---|
120 | static_cast<int>(launch::async) |
|
---|
121 | static_cast<int>(launch::deferred)
|
---|
122 | );
|
---|
123 |
|
---|
124 | return aux::async(policy, forward<F>(f));
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * The async(launch, F, Args...) and async(F, Args...)
|
---|
129 | * overloards must not collide, so we check the first template
|
---|
130 | * argument and handle the special case of just a functor
|
---|
131 | * above.
|
---|
132 | */
|
---|
133 | template<class F, class Arg, class... Args>
|
---|
134 | decltype(auto) async(F&& f, Arg&& arg, Args&&... args)
|
---|
135 | {
|
---|
136 | if constexpr (is_same_v<decay_t<F>, launch>)
|
---|
137 | return aux::async(f, forward<Arg>(arg), forward<Args>(args)...);
|
---|
138 | else
|
---|
139 | {
|
---|
140 | launch policy = static_cast<launch>(
|
---|
141 | static_cast<int>(launch::async) |
|
---|
142 | static_cast<int>(launch::deferred)
|
---|
143 | );
|
---|
144 |
|
---|
145 | return aux::async(policy, forward<F>(f), forward<Arg>(arg), forward<Args>(args)...);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | #endif
|
---|