| 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 | */
|
|---|
| 72 | if (async && deferred)
|
|---|
| 73 | {
|
|---|
| 74 | return future<result_t>{
|
|---|
| 75 | new aux::deferred_shared_state<
|
|---|
| 76 | result_t, F, Args...
|
|---|
| 77 | >{forward<F>(f), forward<Args>(args)...}
|
|---|
| 78 | };
|
|---|
| 79 | }
|
|---|
| 80 | else if (async)
|
|---|
| 81 | {
|
|---|
| 82 | return future<result_t>{
|
|---|
| 83 | new aux::async_shared_state<
|
|---|
| 84 | result_t, F, Args...
|
|---|
| 85 | >{forward<F>(f), forward<Args>(args)...}
|
|---|
| 86 | };
|
|---|
| 87 | }
|
|---|
| 88 | else if (deferred)
|
|---|
| 89 | {
|
|---|
| 90 | /**
|
|---|
| 91 | * Duplicated on purpose because of the default.
|
|---|
| 92 | * Do not remove!
|
|---|
| 93 | */
|
|---|
| 94 | return future<result_t>{
|
|---|
| 95 | new aux::deferred_shared_state<
|
|---|
| 96 | result_t, F, Args...
|
|---|
| 97 | >{forward<F>(f), forward<Args>(args)...}
|
|---|
| 98 | };
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * This is undefined behaviour, let's be nice though ;)
|
|---|
| 103 | */
|
|---|
| 104 | return future<result_t>{
|
|---|
| 105 | new aux::deferred_shared_state<
|
|---|
| 106 | result_t, F, Args...
|
|---|
| 107 | >{forward<F>(f), forward<Args>(args)...}
|
|---|
| 108 | };
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | template<class F>
|
|---|
| 113 | decltype(auto) async(F&& f)
|
|---|
| 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));
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * The async(launch, F, Args...) and async(F, Args...)
|
|---|
| 125 | * overloards must not collide, so we check the first template
|
|---|
| 126 | * argument and handle the special case of just a functor
|
|---|
| 127 | * above.
|
|---|
| 128 | */
|
|---|
| 129 | template<class F, class Arg, class... Args>
|
|---|
| 130 | decltype(auto) async(F&& f, Arg&& arg, Args&&... args)
|
|---|
| 131 | {
|
|---|
| 132 | if constexpr (is_same_v<decay_t<F>, launch>)
|
|---|
| 133 | return aux::async(f, forward<Arg>(arg), forward<Args>(args)...);
|
|---|
| 134 | else
|
|---|
| 135 | {
|
|---|
| 136 | launch policy = static_cast<launch>(
|
|---|
| 137 | static_cast<int>(launch::async) |
|
|---|
| 138 | static_cast<int>(launch::deferred)
|
|---|
| 139 | );
|
|---|
| 140 |
|
|---|
| 141 | return aux::async(policy, forward<F>(f), forward<Arg>(arg), forward<Args>(args)...);
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | #endif
|
|---|