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