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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a95e75e was a95e75e, checked in by Jaroslav Jindrak <dzejrou@…>, 6 years ago

cpp: split future.hpp into subheaders for each type

  • Property mode set to 100644
File size: 5.1 KB
Line 
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/shared_state.hpp>
34#include <__bits/type_traits/result_of.hpp>
35#include <__bits/utility/forward_move.hpp>
36
37namespace std
38{
39
40 enum class launch
41 {
42 async,
43 deferred
44 };
45
46 namespace aux
47 {
48 /**
49 * Note: The reason we keep the actual function
50 * within the aux namespace is that were the non-policy
51 * version of the function call the other one in the std
52 * namespace, we'd get resolution conflicts. This way
53 * aux::async is properly called even if std::async is
54 * called either with or without a launch policy.
55 */
56 template<class F, class... Args>
57 future<result_of_t<decay_t<F>(decay_t<Args>...)>>
58 async(launch policy, F&& f, Args&&... args)
59 {
60 using result_t = result_of_t<decay_t<F>(decay_t<Args>...)>;
61
62 bool async = (static_cast<int>(policy) &
63 static_cast<int>(launch::async)) != 0;
64 bool deferred = (static_cast<int>(policy) &
65 static_cast<int>(launch::deferred)) != 0;
66
67 /**
68 * Note: The case when async | deferred is set in policy
69 * is implementation defined, feel free to change.
70 */
71 if (async && deferred)
72 {
73 return future<result_t>{
74 new aux::deferred_shared_state<
75 result_t, F, Args...
76 >{forward<F>(f), forward<Args>(args)...}
77 };
78 }
79 else if (async)
80 {
81 return future<result_t>{
82 new aux::async_shared_state<
83 result_t, F, Args...
84 >{forward<F>(f), forward<Args>(args)...}
85 };
86 }
87 else if (deferred)
88 {
89 /**
90 * Duplicated on purpose because of the default.
91 * Do not remove!
92 */
93 return future<result_t>{
94 new aux::deferred_shared_state<
95 result_t, F, Args...
96 >{forward<F>(f), forward<Args>(args)...}
97 };
98 }
99
100 /**
101 * This is undefined behaviour, let's be nice though ;)
102 */
103 return future<result_t>{
104 new aux::deferred_shared_state<
105 result_t, F, Args...
106 >{forward<F>(f), forward<Args>(args)...}
107 };
108 }
109 }
110
111 template<class F>
112 decltype(auto) async(F&& f)
113 {
114 launch policy = static_cast<launch>(
115 static_cast<int>(launch::async) |
116 static_cast<int>(launch::deferred)
117 );
118
119 return aux::async(policy, forward<F>(f));
120 }
121
122 /**
123 * The async(launch, F, Args...) and async(F, Args...)
124 * overloards must not collide, so we check the first template
125 * argument and handle the special case of just a functor
126 * above.
127 */
128 template<class F, class Arg, class... Args>
129 decltype(auto) async(F&& f, Arg&& arg, Args&&... args)
130 {
131 if constexpr (is_same_v<decay_t<F>, launch>)
132 return aux::async(f, forward<Arg>(arg), forward<Args>(args)...);
133 else
134 {
135 launch policy = static_cast<launch>(
136 static_cast<int>(launch::async) |
137 static_cast<int>(launch::deferred)
138 );
139
140 return aux::async(policy, forward<F>(f), forward<Arg>(arg), forward<Args>(args)...);
141 }
142 }
143}
144
145#endif
Note: See TracBrowser for help on using the repository browser.