[a95e75e] | 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_PACKAGED_TASK
|
---|
[7480341] | 30 | #define LIBCPP_BITS_THREAD_PACKAGED_TASK
|
---|
[a95e75e] | 31 |
|
---|
[4d68584] | 32 | #include <__bits/exception.hpp>
|
---|
[fda6dd4] | 33 | #include <__bits/functional/function.hpp>
|
---|
[5ea9dd2] | 34 | #include <__bits/memory/allocator_traits.hpp>
|
---|
[fda6dd4] | 35 | #include <__bits/thread/future.hpp>
|
---|
| 36 | #include <__bits/thread/future_common.hpp>
|
---|
| 37 | #include <__bits/thread/shared_state.hpp>
|
---|
| 38 | #include <type_traits>
|
---|
| 39 | #include <utility>
|
---|
| 40 |
|
---|
[a95e75e] | 41 | namespace std
|
---|
| 42 | {
|
---|
| 43 | /**
|
---|
| 44 | * 30.6.9, class template packaged_task:
|
---|
| 45 | */
|
---|
| 46 |
|
---|
[46c66f8] | 47 | /**
|
---|
| 48 | * Note: The base template is not defined because
|
---|
| 49 | * we require the template parameter to be
|
---|
| 50 | * a callable object (e.g. a function). This
|
---|
| 51 | * is achieved by the R(Args...) specialization
|
---|
| 52 | * below.
|
---|
| 53 | */
|
---|
[a95e75e] | 54 | template<class>
|
---|
[46c66f8] | 55 | class packaged_task;
|
---|
[a95e75e] | 56 |
|
---|
| 57 | template<class R, class... Args>
|
---|
| 58 | class packaged_task<R(Args...)>
|
---|
| 59 | {
|
---|
[fda6dd4] | 60 | public:
|
---|
| 61 | packaged_task() noexcept
|
---|
| 62 | : func_{}, state_{}
|
---|
| 63 | { /* DUMMY BODY */ }
|
---|
| 64 |
|
---|
| 65 | template<
|
---|
| 66 | class F, enable_if_t<
|
---|
| 67 | !is_same_v<
|
---|
| 68 | decay_t<F>, packaged_task<R(Args...)>
|
---|
| 69 | >, int
|
---|
| 70 | > = 0
|
---|
| 71 | >
|
---|
| 72 | explicit packaged_task(F&& f)
|
---|
| 73 | : func_{forward<F>(f)}, state_{new aux::shared_state<R>{}}
|
---|
| 74 | { /* DUMMY BODY */ }
|
---|
| 75 |
|
---|
| 76 | template<
|
---|
| 77 | class F, class Allocator, enable_if_t<
|
---|
| 78 | is_same_v<
|
---|
| 79 | decay_t<F>, packaged_task<R(Args...)>
|
---|
| 80 | >, int
|
---|
| 81 | > = 0
|
---|
| 82 | >
|
---|
| 83 | explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f)
|
---|
[5ea9dd2] | 84 | : func_{forward<F>(f)}, state_{}
|
---|
[fda6dd4] | 85 | {
|
---|
[d276424f] | 86 | typename allocator_traits<
|
---|
| 87 | Allocator
|
---|
| 88 | >::template rebind_alloc<aux::shared_state<R>> rebound{a};
|
---|
[5ea9dd2] | 89 |
|
---|
| 90 | state_ = rebound.allocate(1);
|
---|
| 91 | rebound.construct(state_);
|
---|
[fda6dd4] | 92 | }
|
---|
| 93 |
|
---|
| 94 | ~packaged_task()
|
---|
| 95 | {
|
---|
| 96 | if (state_)
|
---|
| 97 | {
|
---|
| 98 | if (!state_->is_set())
|
---|
| 99 | {
|
---|
[4d68584] | 100 | state_->set_exception(make_exception_ptr(
|
---|
| 101 | future_error{make_error_code(future_errc::broken_promise)}
|
---|
| 102 | ));
|
---|
[fda6dd4] | 103 | state_->mark_set(true);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | if (state_->decrement())
|
---|
| 107 | {
|
---|
| 108 | state_->destroy();
|
---|
| 109 | delete state_;
|
---|
| 110 | state_ = nullptr;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | packaged_task(const packaged_task&) = delete;
|
---|
| 116 | packaged_task& operator=(const packaged_task&) = delete;
|
---|
| 117 |
|
---|
| 118 | packaged_task(packaged_task&& rhs)
|
---|
| 119 | : func_{move(rhs.func_)}, state_{move(rhs.state_)}
|
---|
| 120 | { /* DUMMY BODY */ }
|
---|
| 121 |
|
---|
| 122 | packaged_task& operator=(packaged_task&& rhs)
|
---|
| 123 | {
|
---|
| 124 | if (state_)
|
---|
| 125 | {
|
---|
| 126 | if (state_->decrement())
|
---|
| 127 | {
|
---|
| 128 | state_->destroy();
|
---|
| 129 | delete state_;
|
---|
| 130 | state_ = nullptr;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | func_ = move(rhs.func_);
|
---|
| 135 | state_ = move(rhs.state_);
|
---|
[8e24583] | 136 | rhs.state_ = nullptr;
|
---|
[fda6dd4] | 137 |
|
---|
| 138 | return *this;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | void swap(packaged_task& other) noexcept
|
---|
| 142 | {
|
---|
| 143 | std::swap(func_, other.func_);
|
---|
| 144 | std::swap(state_, other.state_);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | bool valid() const noexcept
|
---|
| 148 | {
|
---|
| 149 | return state_ != nullptr;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | future<R> get_future()
|
---|
| 153 | {
|
---|
| 154 | if (!state_)
|
---|
| 155 | throw future_error{make_error_code(future_errc::no_state)};
|
---|
| 156 |
|
---|
| 157 | state_->increment();
|
---|
| 158 |
|
---|
| 159 | return future<R>{state_};
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
| 163 | * Note: This is how the signature is in the standard,
|
---|
| 164 | * should be investigated and verified.
|
---|
| 165 | */
|
---|
| 166 | void operator()(Args... args)
|
---|
| 167 | {
|
---|
| 168 | if (!state_)
|
---|
| 169 | throw future_error{make_error_code(future_errc::no_state)};
|
---|
| 170 | if (state_->is_set())
|
---|
| 171 | {
|
---|
| 172 | throw future_error{
|
---|
| 173 | make_error_code(future_errc::promise_already_satisfied)
|
---|
| 174 | };
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | try
|
---|
| 178 | {
|
---|
| 179 | state_->set_value(invoke(func_, args...));
|
---|
| 180 | }
|
---|
[4d68584] | 181 | catch(const exception& __exception)
|
---|
[fda6dd4] | 182 | {
|
---|
[4d68584] | 183 | state_->set_exception(make_exception_ptr(__exception));
|
---|
[fda6dd4] | 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[4dfb259] | 187 | void make_ready_at_thread_exit(Args... args)
|
---|
[fda6dd4] | 188 | {
|
---|
[72786f38] | 189 | if (!state_)
|
---|
| 190 | throw future_error{make_error_code(future_errc::no_state)};
|
---|
| 191 | if (state_->is_set())
|
---|
| 192 | {
|
---|
| 193 | throw future_error{
|
---|
| 194 | make_error_code(future_errc::promise_already_satisfied)
|
---|
| 195 | };
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | try
|
---|
| 199 | {
|
---|
| 200 | state_->set_value(invoke(func_, args...), false);
|
---|
[5ab9df4] | 201 | aux::set_state_value_at_thread_exit(this->state_);
|
---|
[72786f38] | 202 | }
|
---|
| 203 | catch(const exception& __exception)
|
---|
| 204 | {
|
---|
| 205 | state_->set_exception(make_exception_ptr(__exception), false);
|
---|
[5ab9df4] | 206 | aux::set_state_exception_at_thread_exit(this->state_);
|
---|
[72786f38] | 207 | }
|
---|
[fda6dd4] | 208 | }
|
---|
| 209 |
|
---|
| 210 | void reset()
|
---|
| 211 | {
|
---|
| 212 | if (!state_)
|
---|
| 213 | throw future_error{make_error_code(future_errc::no_state)};
|
---|
| 214 |
|
---|
| 215 | *this = packaged_task{move(func_)};
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | private:
|
---|
| 219 | function<R(Args...)> func_;
|
---|
| 220 |
|
---|
| 221 | aux::shared_state<R>* state_;
|
---|
[a95e75e] | 222 | };
|
---|
| 223 |
|
---|
| 224 | template<class R, class... Args>
|
---|
| 225 | void swap(packaged_task<R(Args...)>& lhs, packaged_task<R(Args...)>& rhs) noexcept
|
---|
| 226 | {
|
---|
| 227 | lhs.swap(rhs);
|
---|
| 228 | };
|
---|
| 229 |
|
---|
| 230 | template<class R, class Alloc>
|
---|
| 231 | struct uses_allocator<packaged_task<R>, Alloc>: true_type
|
---|
| 232 | { /* DUMMY BODY */ };
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | #endif
|
---|