[13f7525] | 1 | /*
|
---|
[ad40b74b] | 2 | * Copyright (c) 2019 Jaroslav Jindrak
|
---|
[13f7525] | 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 |
|
---|
[b57a3ee] | 29 | #ifndef LIBCPP_BITS_THREAD_FUTURE
|
---|
| 30 | #define LIBCPP_BITS_THREAD_FUTURE
|
---|
[13f7525] | 31 |
|
---|
[6e97265] | 32 | #include <__bits/thread/future_common.hpp>
|
---|
| 33 | #include <__bits/thread/shared_state.hpp>
|
---|
| 34 | #include <__bits/utility/forward_move.hpp>
|
---|
[ad40b74b] | 35 | #include <cassert>
|
---|
[4727aacd] | 36 |
|
---|
| 37 | namespace std
|
---|
| 38 | {
|
---|
| 39 | /**
|
---|
[6e97265] | 40 | * 30.6.6, class template future:
|
---|
[4727aacd] | 41 | */
|
---|
| 42 |
|
---|
[6e97265] | 43 | namespace aux
|
---|
[4727aacd] | 44 | {
|
---|
[0d299c93] | 45 | /**
|
---|
| 46 | * Note: Because of shared_future, this base class
|
---|
| 47 | * does implement copy constructor and copy
|
---|
| 48 | * assignment operator. This means that the
|
---|
| 49 | * children (std::future) need to delete this
|
---|
| 50 | * constructor and operator themselves.
|
---|
| 51 | */
|
---|
[6e97265] | 52 | template<class R>
|
---|
| 53 | class future_base
|
---|
| 54 | {
|
---|
| 55 | public:
|
---|
| 56 | future_base() noexcept
|
---|
| 57 | : state_{nullptr}
|
---|
| 58 | { /* DUMMY BODY */ }
|
---|
| 59 |
|
---|
[0d299c93] | 60 | future_base(const future_base& rhs)
|
---|
| 61 | : state_{rhs.state_}
|
---|
| 62 | {
|
---|
| 63 | state_->increment();
|
---|
| 64 | }
|
---|
[6e97265] | 65 |
|
---|
| 66 | future_base(future_base&& rhs) noexcept
|
---|
[bd6ad4b] | 67 | : state_{move(rhs.state_)}
|
---|
[6e97265] | 68 | {
|
---|
| 69 | rhs.state_ = nullptr;
|
---|
| 70 | }
|
---|
[4727aacd] | 71 |
|
---|
[6e97265] | 72 | future_base(aux::shared_state<R>* state)
|
---|
| 73 | : state_{state}
|
---|
| 74 | {
|
---|
| 75 | /**
|
---|
| 76 | * Note: This is a custom non-standard constructor that allows
|
---|
| 77 | * us to create a future directly from a shared state. This
|
---|
| 78 | * should never be a problem as aux::shared_state is a private
|
---|
| 79 | * type and future has no constructor templates.
|
---|
| 80 | */
|
---|
| 81 | }
|
---|
[4727aacd] | 82 |
|
---|
[6e97265] | 83 | virtual ~future_base()
|
---|
| 84 | {
|
---|
| 85 | release_state_();
|
---|
| 86 | }
|
---|
[4727aacd] | 87 |
|
---|
[0d299c93] | 88 | future_base& operator=(const future_base& rhs)
|
---|
| 89 | {
|
---|
| 90 | release_state_();
|
---|
| 91 | state_ = rhs.state_;
|
---|
| 92 |
|
---|
| 93 | state_->increment();
|
---|
| 94 |
|
---|
| 95 | return *this;
|
---|
| 96 | }
|
---|
[4727aacd] | 97 |
|
---|
[6e97265] | 98 | future_base& operator=(future_base&& rhs) noexcept
|
---|
| 99 | {
|
---|
| 100 | release_state_();
|
---|
[bd6ad4b] | 101 | state_ = move(rhs.state_);
|
---|
[6e97265] | 102 | rhs.state_ = nullptr;
|
---|
[8e24583] | 103 |
|
---|
| 104 | return *this;
|
---|
[6e97265] | 105 | }
|
---|
[4727aacd] | 106 |
|
---|
[6e97265] | 107 | bool valid() const noexcept
|
---|
| 108 | {
|
---|
| 109 | return state_ != nullptr;
|
---|
| 110 | }
|
---|
[4727aacd] | 111 |
|
---|
[6e97265] | 112 | void wait() const noexcept
|
---|
| 113 | {
|
---|
| 114 | assert(state_);
|
---|
[4727aacd] | 115 |
|
---|
[6e97265] | 116 | state_->wait();
|
---|
| 117 | }
|
---|
[4727aacd] | 118 |
|
---|
[6e97265] | 119 | template<class Rep, class Period>
|
---|
| 120 | future_status
|
---|
| 121 | wait_for(const chrono::duration<Rep, Period>& rel_time) const
|
---|
| 122 | {
|
---|
| 123 | assert(state_);
|
---|
[4727aacd] | 124 |
|
---|
[6e97265] | 125 | return state_->wait_for(rel_time);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | template<class Clock, class Duration>
|
---|
| 129 | future_status
|
---|
| 130 | wait_until(
|
---|
| 131 | const chrono::time_point<Clock, Duration>& abs_time
|
---|
| 132 | ) const
|
---|
| 133 | {
|
---|
| 134 | assert(state_);
|
---|
| 135 |
|
---|
| 136 | return state_->wait_until(abs_time);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | protected:
|
---|
| 140 | void release_state_()
|
---|
| 141 | {
|
---|
| 142 | if (!state_)
|
---|
| 143 | return;
|
---|
| 144 |
|
---|
| 145 | /**
|
---|
| 146 | * Note: This is the 'release' move described in
|
---|
| 147 | * 30.6.4 (5).
|
---|
| 148 | * Last reference to state -> destroy state.
|
---|
| 149 | * Decrement refcount of state otherwise.
|
---|
| 150 | * Will not block, unless all following hold:
|
---|
| 151 | * 1) State was created by call to std::async.
|
---|
| 152 | * 2) State is not yet ready.
|
---|
| 153 | * 3) This was the last reference to the shared state.
|
---|
| 154 | */
|
---|
| 155 | if (state_->decrement())
|
---|
| 156 | {
|
---|
| 157 | /**
|
---|
| 158 | * The destroy call handles the special case
|
---|
| 159 | * when 1) - 3) hold.
|
---|
| 160 | */
|
---|
| 161 | state_->destroy();
|
---|
| 162 | delete state_;
|
---|
| 163 | state_ = nullptr;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | aux::shared_state<R>* state_;
|
---|
| 168 | };
|
---|
| 169 | }
|
---|
[4727aacd] | 170 |
|
---|
[04c0fc5] | 171 | template<class R>
|
---|
| 172 | class shared_future;
|
---|
| 173 |
|
---|
[4727aacd] | 174 | template<class R>
|
---|
[8660ad0] | 175 | class future: public aux::future_base<aux::future_inner_t<R>>
|
---|
[4727aacd] | 176 | {
|
---|
[0d299c93] | 177 | friend class shared_future<R>;
|
---|
| 178 |
|
---|
[04c0fc5] | 179 | public:
|
---|
| 180 | future() noexcept
|
---|
[8660ad0] | 181 | : aux::future_base<aux::future_inner_t<R>>{}
|
---|
[04c0fc5] | 182 | { /* DUMMY BODY */ }
|
---|
| 183 |
|
---|
| 184 | future(const future&) = delete;
|
---|
| 185 |
|
---|
| 186 | future(future&& rhs) noexcept
|
---|
[8e24583] | 187 | : aux::future_base<aux::future_inner_t<R>>{move(rhs)}
|
---|
[6e97265] | 188 | { /* DUMMY BODY */ }
|
---|
[04c0fc5] | 189 |
|
---|
[8660ad0] | 190 | future(aux::shared_state<aux::future_inner_t<R>>* state)
|
---|
| 191 | : aux::future_base<aux::future_inner_t<R>>{state}
|
---|
[6e97265] | 192 | { /* DUMMY BODY */ }
|
---|
[04c0fc5] | 193 |
|
---|
[6e97265] | 194 | future& operator=(const future&) = delete;
|
---|
[04c0fc5] | 195 |
|
---|
[87efcb1] | 196 | future& operator=(future&& rhs) noexcept = default;
|
---|
[04c0fc5] | 197 |
|
---|
| 198 | shared_future<R> share()
|
---|
| 199 | {
|
---|
[0d299c93] | 200 | return shared_future<R>{move(*this)};
|
---|
[04c0fc5] | 201 | }
|
---|
| 202 |
|
---|
[5d71d09] | 203 | R get()
|
---|
[04c0fc5] | 204 | {
|
---|
[6e97265] | 205 | assert(this->state_);
|
---|
[04c0fc5] | 206 |
|
---|
[6e97265] | 207 | this->wait();
|
---|
[04c0fc5] | 208 |
|
---|
[6e97265] | 209 | if (this->state_->has_exception())
|
---|
| 210 | this->state_->throw_stored_exception();
|
---|
[627dc41] | 211 |
|
---|
[8660ad0] | 212 | if constexpr (!is_same_v<R, void>)
|
---|
| 213 | {
|
---|
| 214 | if constexpr (is_reference_v<R>)
|
---|
| 215 | {
|
---|
| 216 | assert(this->state_->get());
|
---|
[bd6ad4b] | 217 |
|
---|
[8660ad0] | 218 | return *this->state_->get();
|
---|
| 219 | }
|
---|
| 220 | else
|
---|
| 221 | return this->state_->get();
|
---|
| 222 | }
|
---|
[bd6ad4b] | 223 | }
|
---|
[8e24583] | 224 |
|
---|
| 225 | /**
|
---|
| 226 | * Useful for testing as we can check some information
|
---|
| 227 | * otherwise unavailable to us without waiting, e.g.
|
---|
| 228 | * to check whether the state is ready, its reference
|
---|
| 229 | * count etc.
|
---|
| 230 | */
|
---|
| 231 | aux::shared_state<aux::future_inner_t<R>>* __state() noexcept
|
---|
| 232 | {
|
---|
| 233 | return this->state_;
|
---|
| 234 | }
|
---|
[4727aacd] | 235 | };
|
---|
| 236 | }
|
---|
[13f7525] | 237 |
|
---|
| 238 | #endif
|
---|