Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- future -----------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_FUTURE |
| 11 | #define _LIBCPP_FUTURE |
| 12 | |
| 13 | /* |
| 14 | future synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
| 18 | |
| 19 | enum class future_errc |
| 20 | { |
Howard Hinnant | f2fd64d | 2013-09-14 18:20:10 +0000 | [diff] [blame] | 21 | future_already_retrieved = 1, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | promise_already_satisfied, |
Howard Hinnant | f2fd64d | 2013-09-14 18:20:10 +0000 | [diff] [blame] | 23 | no_state, |
| 24 | broken_promise |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | enum class launch |
| 28 | { |
Howard Hinnant | e3df4ea | 2010-11-23 18:33:54 +0000 | [diff] [blame] | 29 | async = 1, |
| 30 | deferred = 2, |
| 31 | any = async | deferred |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | enum class future_status |
| 35 | { |
| 36 | ready, |
| 37 | timeout, |
| 38 | deferred |
| 39 | }; |
| 40 | |
| 41 | template <> struct is_error_code_enum<future_errc> : public true_type { }; |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 42 | error_code make_error_code(future_errc e) noexcept; |
| 43 | error_condition make_error_condition(future_errc e) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 44 | |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 45 | const error_category& future_category() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 46 | |
| 47 | class future_error |
| 48 | : public logic_error |
| 49 | { |
| 50 | public: |
| 51 | future_error(error_code ec); // exposition only |
Marshall Clow | d4a669c | 2016-11-14 18:56:24 +0000 | [diff] [blame] | 52 | explicit future_error(future_errc); // C++17 |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 53 | const error_code& code() const noexcept; |
| 54 | const char* what() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | template <class R> |
| 58 | class promise |
| 59 | { |
| 60 | public: |
| 61 | promise(); |
| 62 | template <class Allocator> |
| 63 | promise(allocator_arg_t, const Allocator& a); |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 64 | promise(promise&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 65 | promise(const promise& rhs) = delete; |
| 66 | ~promise(); |
| 67 | |
| 68 | // assignment |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 69 | promise& operator=(promise&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | promise& operator=(const promise& rhs) = delete; |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 71 | void swap(promise& other) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 72 | |
| 73 | // retrieving the result |
| 74 | future<R> get_future(); |
| 75 | |
| 76 | // setting the result |
| 77 | void set_value(const R& r); |
| 78 | void set_value(R&& r); |
| 79 | void set_exception(exception_ptr p); |
| 80 | |
| 81 | // setting the result with deferred notification |
| 82 | void set_value_at_thread_exit(const R& r); |
| 83 | void set_value_at_thread_exit(R&& r); |
| 84 | void set_exception_at_thread_exit(exception_ptr p); |
| 85 | }; |
| 86 | |
| 87 | template <class R> |
| 88 | class promise<R&> |
| 89 | { |
| 90 | public: |
| 91 | promise(); |
| 92 | template <class Allocator> |
| 93 | promise(allocator_arg_t, const Allocator& a); |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 94 | promise(promise&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 95 | promise(const promise& rhs) = delete; |
| 96 | ~promise(); |
| 97 | |
| 98 | // assignment |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 99 | promise& operator=(promise&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 100 | promise& operator=(const promise& rhs) = delete; |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 101 | void swap(promise& other) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 102 | |
| 103 | // retrieving the result |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 104 | future<R&> get_future(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 105 | |
| 106 | // setting the result |
| 107 | void set_value(R& r); |
| 108 | void set_exception(exception_ptr p); |
| 109 | |
| 110 | // setting the result with deferred notification |
| 111 | void set_value_at_thread_exit(R&); |
| 112 | void set_exception_at_thread_exit(exception_ptr p); |
| 113 | }; |
| 114 | |
| 115 | template <> |
| 116 | class promise<void> |
| 117 | { |
| 118 | public: |
| 119 | promise(); |
| 120 | template <class Allocator> |
| 121 | promise(allocator_arg_t, const Allocator& a); |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 122 | promise(promise&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 123 | promise(const promise& rhs) = delete; |
| 124 | ~promise(); |
| 125 | |
| 126 | // assignment |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 127 | promise& operator=(promise&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 128 | promise& operator=(const promise& rhs) = delete; |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 129 | void swap(promise& other) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 130 | |
| 131 | // retrieving the result |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 132 | future<void> get_future(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 133 | |
| 134 | // setting the result |
| 135 | void set_value(); |
| 136 | void set_exception(exception_ptr p); |
| 137 | |
| 138 | // setting the result with deferred notification |
| 139 | void set_value_at_thread_exit(); |
| 140 | void set_exception_at_thread_exit(exception_ptr p); |
| 141 | }; |
| 142 | |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 143 | template <class R> void swap(promise<R>& x, promise<R>& y) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 144 | |
| 145 | template <class R, class Alloc> |
| 146 | struct uses_allocator<promise<R>, Alloc> : public true_type {}; |
| 147 | |
| 148 | template <class R> |
| 149 | class future |
| 150 | { |
| 151 | public: |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 152 | future() noexcept; |
| 153 | future(future&&) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 154 | future(const future& rhs) = delete; |
| 155 | ~future(); |
| 156 | future& operator=(const future& rhs) = delete; |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 157 | future& operator=(future&&) noexcept; |
Marshall Clow | 597881d | 2017-01-25 20:14:03 +0000 | [diff] [blame] | 158 | shared_future<R> share() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 159 | |
| 160 | // retrieving the value |
| 161 | R get(); |
| 162 | |
| 163 | // functions to check state |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 164 | bool valid() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 165 | |
| 166 | void wait() const; |
| 167 | template <class Rep, class Period> |
| 168 | future_status |
| 169 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 170 | template <class Clock, class Duration> |
| 171 | future_status |
| 172 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 173 | }; |
| 174 | |
| 175 | template <class R> |
| 176 | class future<R&> |
| 177 | { |
| 178 | public: |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 179 | future() noexcept; |
| 180 | future(future&&) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 181 | future(const future& rhs) = delete; |
| 182 | ~future(); |
| 183 | future& operator=(const future& rhs) = delete; |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 184 | future& operator=(future&&) noexcept; |
Marshall Clow | 79e0211 | 2017-01-24 23:28:25 +0000 | [diff] [blame] | 185 | shared_future<R&> share() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 186 | |
| 187 | // retrieving the value |
| 188 | R& get(); |
| 189 | |
| 190 | // functions to check state |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 191 | bool valid() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 192 | |
| 193 | void wait() const; |
| 194 | template <class Rep, class Period> |
| 195 | future_status |
| 196 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 197 | template <class Clock, class Duration> |
| 198 | future_status |
| 199 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 200 | }; |
| 201 | |
| 202 | template <> |
| 203 | class future<void> |
| 204 | { |
| 205 | public: |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 206 | future() noexcept; |
| 207 | future(future&&) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 208 | future(const future& rhs) = delete; |
| 209 | ~future(); |
| 210 | future& operator=(const future& rhs) = delete; |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 211 | future& operator=(future&&) noexcept; |
Marshall Clow | 79e0211 | 2017-01-24 23:28:25 +0000 | [diff] [blame] | 212 | shared_future<void> share() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 213 | |
| 214 | // retrieving the value |
| 215 | void get(); |
| 216 | |
| 217 | // functions to check state |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 218 | bool valid() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 219 | |
| 220 | void wait() const; |
| 221 | template <class Rep, class Period> |
| 222 | future_status |
| 223 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 224 | template <class Clock, class Duration> |
| 225 | future_status |
| 226 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 227 | }; |
| 228 | |
| 229 | template <class R> |
| 230 | class shared_future |
| 231 | { |
| 232 | public: |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 233 | shared_future() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 234 | shared_future(const shared_future& rhs); |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 235 | shared_future(future<R>&&) noexcept; |
| 236 | shared_future(shared_future&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 237 | ~shared_future(); |
| 238 | shared_future& operator=(const shared_future& rhs); |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 239 | shared_future& operator=(shared_future&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 240 | |
| 241 | // retrieving the value |
| 242 | const R& get() const; |
| 243 | |
| 244 | // functions to check state |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 245 | bool valid() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 246 | |
| 247 | void wait() const; |
| 248 | template <class Rep, class Period> |
| 249 | future_status |
| 250 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 251 | template <class Clock, class Duration> |
| 252 | future_status |
| 253 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 254 | }; |
| 255 | |
| 256 | template <class R> |
| 257 | class shared_future<R&> |
| 258 | { |
| 259 | public: |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 260 | shared_future() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 261 | shared_future(const shared_future& rhs); |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 262 | shared_future(future<R&>&&) noexcept; |
| 263 | shared_future(shared_future&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 264 | ~shared_future(); |
| 265 | shared_future& operator=(const shared_future& rhs); |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 266 | shared_future& operator=(shared_future&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 267 | |
| 268 | // retrieving the value |
| 269 | R& get() const; |
| 270 | |
| 271 | // functions to check state |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 272 | bool valid() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 273 | |
| 274 | void wait() const; |
| 275 | template <class Rep, class Period> |
| 276 | future_status |
| 277 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 278 | template <class Clock, class Duration> |
| 279 | future_status |
| 280 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 281 | }; |
| 282 | |
| 283 | template <> |
| 284 | class shared_future<void> |
| 285 | { |
| 286 | public: |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 287 | shared_future() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 288 | shared_future(const shared_future& rhs); |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 289 | shared_future(future<void>&&) noexcept; |
| 290 | shared_future(shared_future&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 291 | ~shared_future(); |
| 292 | shared_future& operator=(const shared_future& rhs); |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 293 | shared_future& operator=(shared_future&& rhs) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 294 | |
| 295 | // retrieving the value |
| 296 | void get() const; |
| 297 | |
| 298 | // functions to check state |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 299 | bool valid() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 300 | |
| 301 | void wait() const; |
| 302 | template <class Rep, class Period> |
| 303 | future_status |
| 304 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 305 | template <class Clock, class Duration> |
| 306 | future_status |
| 307 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 308 | }; |
| 309 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 310 | template <class F, class... Args> |
Howard Hinnant | 15579d2 | 2013-09-21 18:17:23 +0000 | [diff] [blame] | 311 | future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 312 | async(F&& f, Args&&... args); |
| 313 | |
| 314 | template <class F, class... Args> |
Howard Hinnant | 15579d2 | 2013-09-21 18:17:23 +0000 | [diff] [blame] | 315 | future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 316 | async(launch policy, F&& f, Args&&... args); |
| 317 | |
Howard Hinnant | c566dc3 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 318 | template <class> class packaged_task; // undefined |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 319 | |
| 320 | template <class R, class... ArgTypes> |
| 321 | class packaged_task<R(ArgTypes...)> |
| 322 | { |
| 323 | public: |
Eric Fiselier | 17f39ed | 2016-06-01 21:05:53 +0000 | [diff] [blame] | 324 | typedef R result_type; // extension |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 325 | |
| 326 | // construction and destruction |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 327 | packaged_task() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 328 | template <class F> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 329 | explicit packaged_task(F&& f); |
Marshall Clow | a0a057e | 2017-11-27 20:47:54 +0000 | [diff] [blame] | 330 | template <class F, class Allocator> |
| 331 | packaged_task(allocator_arg_t, const Allocator& a, F&& f); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 332 | ~packaged_task(); |
| 333 | |
| 334 | // no copy |
Howard Hinnant | ab50007 | 2012-07-21 19:34:12 +0000 | [diff] [blame] | 335 | packaged_task(const packaged_task&) = delete; |
| 336 | packaged_task& operator=(const packaged_task&) = delete; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 337 | |
| 338 | // move support |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 339 | packaged_task(packaged_task&& other) noexcept; |
| 340 | packaged_task& operator=(packaged_task&& other) noexcept; |
| 341 | void swap(packaged_task& other) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 342 | |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 343 | bool valid() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 344 | |
| 345 | // result retrieval |
| 346 | future<R> get_future(); |
| 347 | |
| 348 | // execution |
| 349 | void operator()(ArgTypes... ); |
| 350 | void make_ready_at_thread_exit(ArgTypes...); |
| 351 | |
| 352 | void reset(); |
| 353 | }; |
| 354 | |
| 355 | template <class R> |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 356 | void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 357 | |
Marshall Clow | a0a057e | 2017-11-27 20:47:54 +0000 | [diff] [blame] | 358 | template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>; |
| 359 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 360 | } // std |
| 361 | |
| 362 | */ |
| 363 | |
| 364 | #include <__config> |
| 365 | #include <system_error> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 366 | #include <memory> |
| 367 | #include <chrono> |
| 368 | #include <exception> |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 369 | #include <mutex> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 370 | #include <thread> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 371 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 372 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 373 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 374 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 375 | |
Jonathan Roelofs | 067218a | 2014-09-05 20:28:44 +0000 | [diff] [blame] | 376 | #ifdef _LIBCPP_HAS_NO_THREADS |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 377 | #error <future> is not supported on this single threaded system |
| 378 | #else // !_LIBCPP_HAS_NO_THREADS |
| 379 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 380 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 381 | |
| 382 | //enum class future_errc |
Howard Hinnant | 09bc0c9 | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 383 | _LIBCPP_DECLARE_STRONG_ENUM(future_errc) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 384 | { |
Howard Hinnant | f2fd64d | 2013-09-14 18:20:10 +0000 | [diff] [blame] | 385 | future_already_retrieved = 1, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 386 | promise_already_satisfied, |
Howard Hinnant | f2fd64d | 2013-09-14 18:20:10 +0000 | [diff] [blame] | 387 | no_state, |
| 388 | broken_promise |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 389 | }; |
Howard Hinnant | 09bc0c9 | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 390 | _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 391 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 392 | template <> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 393 | struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {}; |
Howard Hinnant | 96803d9 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 394 | |
Howard Hinnant | 09bc0c9 | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 395 | #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS |
| 396 | template <> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 397 | struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { }; |
Howard Hinnant | 09bc0c9 | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 398 | #endif |
| 399 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 400 | //enum class launch |
Howard Hinnant | 09bc0c9 | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 401 | _LIBCPP_DECLARE_STRONG_ENUM(launch) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 402 | { |
Howard Hinnant | e3df4ea | 2010-11-23 18:33:54 +0000 | [diff] [blame] | 403 | async = 1, |
| 404 | deferred = 2, |
| 405 | any = async | deferred |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 406 | }; |
Howard Hinnant | 09bc0c9 | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 407 | _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 408 | |
Howard Hinnant | da760a8 | 2013-06-29 18:38:17 +0000 | [diff] [blame] | 409 | #ifndef _LIBCPP_HAS_NO_STRONG_ENUMS |
| 410 | |
Howard Hinnant | da760a8 | 2013-06-29 18:38:17 +0000 | [diff] [blame] | 411 | typedef underlying_type<launch>::type __launch_underlying_type; |
Howard Hinnant | da760a8 | 2013-06-29 18:38:17 +0000 | [diff] [blame] | 412 | |
| 413 | inline _LIBCPP_INLINE_VISIBILITY |
| 414 | _LIBCPP_CONSTEXPR |
| 415 | launch |
| 416 | operator&(launch __x, launch __y) |
| 417 | { |
| 418 | return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & |
| 419 | static_cast<__launch_underlying_type>(__y)); |
| 420 | } |
| 421 | |
| 422 | inline _LIBCPP_INLINE_VISIBILITY |
| 423 | _LIBCPP_CONSTEXPR |
| 424 | launch |
| 425 | operator|(launch __x, launch __y) |
| 426 | { |
| 427 | return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | |
| 428 | static_cast<__launch_underlying_type>(__y)); |
| 429 | } |
| 430 | |
| 431 | inline _LIBCPP_INLINE_VISIBILITY |
| 432 | _LIBCPP_CONSTEXPR |
| 433 | launch |
| 434 | operator^(launch __x, launch __y) |
| 435 | { |
| 436 | return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ |
| 437 | static_cast<__launch_underlying_type>(__y)); |
| 438 | } |
| 439 | |
| 440 | inline _LIBCPP_INLINE_VISIBILITY |
| 441 | _LIBCPP_CONSTEXPR |
| 442 | launch |
| 443 | operator~(launch __x) |
| 444 | { |
Howard Hinnant | 373d760 | 2013-07-02 18:01:41 +0000 | [diff] [blame] | 445 | return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3); |
Howard Hinnant | da760a8 | 2013-06-29 18:38:17 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | inline _LIBCPP_INLINE_VISIBILITY |
| 449 | launch& |
| 450 | operator&=(launch& __x, launch __y) |
| 451 | { |
| 452 | __x = __x & __y; return __x; |
| 453 | } |
| 454 | |
| 455 | inline _LIBCPP_INLINE_VISIBILITY |
| 456 | launch& |
| 457 | operator|=(launch& __x, launch __y) |
| 458 | { |
| 459 | __x = __x | __y; return __x; |
| 460 | } |
| 461 | |
| 462 | inline _LIBCPP_INLINE_VISIBILITY |
| 463 | launch& |
| 464 | operator^=(launch& __x, launch __y) |
| 465 | { |
| 466 | __x = __x ^ __y; return __x; |
| 467 | } |
| 468 | |
| 469 | #endif // !_LIBCPP_HAS_NO_STRONG_ENUMS |
| 470 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 471 | //enum class future_status |
Howard Hinnant | 09bc0c9 | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 472 | _LIBCPP_DECLARE_STRONG_ENUM(future_status) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 473 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 474 | ready, |
| 475 | timeout, |
| 476 | deferred |
| 477 | }; |
Howard Hinnant | 09bc0c9 | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 478 | _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 479 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 480 | _LIBCPP_FUNC_VIS |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 481 | const error_category& future_category() _NOEXCEPT; |
Howard Hinnant | 96803d9 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 482 | |
| 483 | inline _LIBCPP_INLINE_VISIBILITY |
| 484 | error_code |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 485 | make_error_code(future_errc __e) _NOEXCEPT |
Howard Hinnant | 96803d9 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 486 | { |
| 487 | return error_code(static_cast<int>(__e), future_category()); |
| 488 | } |
| 489 | |
| 490 | inline _LIBCPP_INLINE_VISIBILITY |
| 491 | error_condition |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 492 | make_error_condition(future_errc __e) _NOEXCEPT |
Howard Hinnant | 96803d9 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 493 | { |
| 494 | return error_condition(static_cast<int>(__e), future_category()); |
| 495 | } |
| 496 | |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 497 | class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error |
Howard Hinnant | 96803d9 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 498 | : public logic_error |
| 499 | { |
| 500 | error_code __ec_; |
| 501 | public: |
| 502 | future_error(error_code __ec); |
Marshall Clow | d4a669c | 2016-11-14 18:56:24 +0000 | [diff] [blame] | 503 | #if _LIBCPP_STD_VERS > 14 |
| 504 | explicit future_error(future_errc _Ev) : logic_error(), __ec_(make_error_code(_Ev)) {} |
| 505 | #endif |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 506 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 507 | const error_code& code() const _NOEXCEPT {return __ec_;} |
Howard Hinnant | 0dcdf02 | 2011-07-07 21:03:52 +0000 | [diff] [blame] | 508 | |
| 509 | virtual ~future_error() _NOEXCEPT; |
Howard Hinnant | 96803d9 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 510 | }; |
| 511 | |
Louis Dionne | 16fe295 | 2018-07-11 23:14:33 +0000 | [diff] [blame] | 512 | _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 513 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 514 | _LIBCPP_AVAILABILITY_FUTURE_ERROR |
| 515 | #endif |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 516 | void __throw_future_error(future_errc _Ev) |
Marshall Clow | 7b3742d | 2015-09-03 15:11:32 +0000 | [diff] [blame] | 517 | { |
| 518 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 519 | throw future_error(make_error_code(_Ev)); |
| 520 | #else |
Eric Fiselier | b2c89f2 | 2016-12-24 01:56:25 +0000 | [diff] [blame] | 521 | ((void)_Ev); |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 522 | _VSTD::abort(); |
Marshall Clow | 7b3742d | 2015-09-03 15:11:32 +0000 | [diff] [blame] | 523 | #endif |
| 524 | } |
| 525 | |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 526 | class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 527 | : public __shared_count |
| 528 | { |
| 529 | protected: |
| 530 | exception_ptr __exception_; |
| 531 | mutable mutex __mut_; |
| 532 | mutable condition_variable __cv_; |
| 533 | unsigned __state_; |
| 534 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 535 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 536 | void __sub_wait(unique_lock<mutex>& __lk); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 537 | public: |
| 538 | enum |
| 539 | { |
| 540 | __constructed = 1, |
| 541 | __future_attached = 2, |
| 542 | ready = 4, |
| 543 | deferred = 8 |
| 544 | }; |
| 545 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 546 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 547 | __assoc_sub_state() : __state_(0) {} |
| 548 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 549 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 550 | bool __has_value() const |
| 551 | {return (__state_ & __constructed) || (__exception_ != nullptr);} |
| 552 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 553 | _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | 2c0d226 | 2018-08-24 14:00:59 +0000 | [diff] [blame] | 554 | void __attach_future() { |
Howard Hinnant | e667ecc | 2013-01-14 20:01:24 +0000 | [diff] [blame] | 555 | lock_guard<mutex> __lk(__mut_); |
Louis Dionne | 2c0d226 | 2018-08-24 14:00:59 +0000 | [diff] [blame] | 556 | bool __has_future_attached = (__state_ & __future_attached) != 0; |
| 557 | if (__has_future_attached) |
| 558 | __throw_future_error(future_errc::future_already_retrieved); |
| 559 | this->__add_shared(); |
Howard Hinnant | e667ecc | 2013-01-14 20:01:24 +0000 | [diff] [blame] | 560 | __state_ |= __future_attached; |
| 561 | } |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 562 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 563 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 564 | void __set_deferred() {__state_ |= deferred;} |
| 565 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 566 | void __make_ready(); |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 567 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | dfcbb43 | 2013-10-13 01:02:45 +0000 | [diff] [blame] | 568 | bool __is_ready() const {return (__state_ & ready) != 0;} |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 569 | |
| 570 | void set_value(); |
| 571 | void set_value_at_thread_exit(); |
| 572 | |
| 573 | void set_exception(exception_ptr __p); |
| 574 | void set_exception_at_thread_exit(exception_ptr __p); |
| 575 | |
| 576 | void copy(); |
| 577 | |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 578 | void wait(); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 579 | template <class _Rep, class _Period> |
| 580 | future_status |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 581 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 582 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const; |
| 583 | template <class _Clock, class _Duration> |
Shoaib Meenai | 55f3a46 | 2017-03-02 03:22:18 +0000 | [diff] [blame] | 584 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 585 | future_status |
| 586 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 587 | |
| 588 | virtual void __execute(); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 589 | }; |
| 590 | |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 591 | template <class _Clock, class _Duration> |
| 592 | future_status |
| 593 | __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 594 | { |
| 595 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 596 | if (__state_ & deferred) |
| 597 | return future_status::deferred; |
| 598 | while (!(__state_ & ready) && _Clock::now() < __abs_time) |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 599 | __cv_.wait_until(__lk, __abs_time); |
| 600 | if (__state_ & ready) |
| 601 | return future_status::ready; |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 602 | return future_status::timeout; |
| 603 | } |
| 604 | |
| 605 | template <class _Rep, class _Period> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 606 | inline |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 607 | future_status |
| 608 | __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 609 | { |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 610 | return wait_until(chrono::steady_clock::now() + __rel_time); |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 613 | template <class _Rp> |
Louis Dionne | 3b967d0 | 2019-12-10 18:00:42 -0500 | [diff] [blame^] | 614 | class _LIBCPP_AVAILABILITY_FUTURE _LIBCPP_HIDDEN __assoc_state |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 615 | : public __assoc_sub_state |
| 616 | { |
| 617 | typedef __assoc_sub_state base; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 618 | typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 619 | protected: |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 620 | _Up __value_; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 621 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 622 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 623 | public: |
| 624 | |
| 625 | template <class _Arg> |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 626 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 627 | void set_value(_Arg&& __arg); |
| 628 | #else |
| 629 | void set_value(_Arg& __arg); |
| 630 | #endif |
| 631 | |
| 632 | template <class _Arg> |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 633 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 634 | void set_value_at_thread_exit(_Arg&& __arg); |
| 635 | #else |
| 636 | void set_value_at_thread_exit(_Arg& __arg); |
| 637 | #endif |
| 638 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 639 | _Rp move(); |
| 640 | typename add_lvalue_reference<_Rp>::type copy(); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 641 | }; |
| 642 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 643 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 644 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 645 | __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 646 | { |
| 647 | if (this->__state_ & base::__constructed) |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 648 | reinterpret_cast<_Rp*>(&__value_)->~_Rp(); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 649 | delete this; |
| 650 | } |
| 651 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 652 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 653 | template <class _Arg> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 654 | _LIBCPP_AVAILABILITY_FUTURE |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 655 | void |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 656 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 657 | __assoc_state<_Rp>::set_value(_Arg&& __arg) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 658 | #else |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 659 | __assoc_state<_Rp>::set_value(_Arg& __arg) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 660 | #endif |
| 661 | { |
| 662 | unique_lock<mutex> __lk(this->__mut_); |
| 663 | if (this->__has_value()) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 664 | __throw_future_error(future_errc::promise_already_satisfied); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 665 | ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg)); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 666 | this->__state_ |= base::__constructed | base::ready; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 667 | __cv_.notify_all(); |
| 668 | } |
| 669 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 670 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 671 | template <class _Arg> |
| 672 | void |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 673 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 674 | __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 675 | #else |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 676 | __assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 677 | #endif |
| 678 | { |
| 679 | unique_lock<mutex> __lk(this->__mut_); |
| 680 | if (this->__has_value()) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 681 | __throw_future_error(future_errc::promise_already_satisfied); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 682 | ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg)); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 683 | this->__state_ |= base::__constructed; |
Howard Hinnant | 15d5505 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 684 | __thread_local_data()->__make_ready_at_thread_exit(this); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 687 | template <class _Rp> |
| 688 | _Rp |
| 689 | __assoc_state<_Rp>::move() |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 690 | { |
| 691 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 692 | this->__sub_wait(__lk); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 693 | if (this->__exception_ != nullptr) |
| 694 | rethrow_exception(this->__exception_); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 695 | return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_)); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 698 | template <class _Rp> |
| 699 | typename add_lvalue_reference<_Rp>::type |
| 700 | __assoc_state<_Rp>::copy() |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 701 | { |
| 702 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 703 | this->__sub_wait(__lk); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 704 | if (this->__exception_ != nullptr) |
| 705 | rethrow_exception(this->__exception_); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 706 | return *reinterpret_cast<_Rp*>(&__value_); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 709 | template <class _Rp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 710 | class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&> |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 711 | : public __assoc_sub_state |
| 712 | { |
| 713 | typedef __assoc_sub_state base; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 714 | typedef _Rp* _Up; |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 715 | protected: |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 716 | _Up __value_; |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 717 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 718 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 719 | public: |
| 720 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 721 | void set_value(_Rp& __arg); |
| 722 | void set_value_at_thread_exit(_Rp& __arg); |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 723 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 724 | _Rp& copy(); |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 725 | }; |
| 726 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 727 | template <class _Rp> |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 728 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 729 | __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 730 | { |
| 731 | delete this; |
| 732 | } |
| 733 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 734 | template <class _Rp> |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 735 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 736 | __assoc_state<_Rp&>::set_value(_Rp& __arg) |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 737 | { |
| 738 | unique_lock<mutex> __lk(this->__mut_); |
| 739 | if (this->__has_value()) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 740 | __throw_future_error(future_errc::promise_already_satisfied); |
Howard Hinnant | 8bea6da | 2013-08-08 18:38:55 +0000 | [diff] [blame] | 741 | __value_ = _VSTD::addressof(__arg); |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 742 | this->__state_ |= base::__constructed | base::ready; |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 743 | __cv_.notify_all(); |
| 744 | } |
| 745 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 746 | template <class _Rp> |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 747 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 748 | __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 749 | { |
| 750 | unique_lock<mutex> __lk(this->__mut_); |
| 751 | if (this->__has_value()) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 752 | __throw_future_error(future_errc::promise_already_satisfied); |
Howard Hinnant | 8bea6da | 2013-08-08 18:38:55 +0000 | [diff] [blame] | 753 | __value_ = _VSTD::addressof(__arg); |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 754 | this->__state_ |= base::__constructed; |
Howard Hinnant | 15d5505 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 755 | __thread_local_data()->__make_ready_at_thread_exit(this); |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 758 | template <class _Rp> |
| 759 | _Rp& |
| 760 | __assoc_state<_Rp&>::copy() |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 761 | { |
| 762 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 763 | this->__sub_wait(__lk); |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 764 | if (this->__exception_ != nullptr) |
| 765 | rethrow_exception(this->__exception_); |
| 766 | return *__value_; |
| 767 | } |
| 768 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 769 | template <class _Rp, class _Alloc> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 770 | class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 771 | : public __assoc_state<_Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 772 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 773 | typedef __assoc_state<_Rp> base; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 774 | _Alloc __alloc_; |
| 775 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 776 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 777 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 778 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 779 | explicit __assoc_state_alloc(const _Alloc& __a) |
| 780 | : __alloc_(__a) {} |
| 781 | }; |
| 782 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 783 | template <class _Rp, class _Alloc> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 784 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 785 | __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 786 | { |
| 787 | if (this->__state_ & base::__constructed) |
Howard Hinnant | 8bea6da | 2013-08-08 18:38:55 +0000 | [diff] [blame] | 788 | reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp(); |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 789 | typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al; |
| 790 | typedef allocator_traits<_Al> _ATraits; |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 791 | typedef pointer_traits<typename _ATraits::pointer> _PTraits; |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 792 | _Al __a(__alloc_); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 793 | this->~__assoc_state_alloc(); |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 794 | __a.deallocate(_PTraits::pointer_to(*this), 1); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 797 | template <class _Rp, class _Alloc> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 798 | class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc> |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 799 | : public __assoc_state<_Rp&> |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 800 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 801 | typedef __assoc_state<_Rp&> base; |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 802 | _Alloc __alloc_; |
| 803 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 804 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 805 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 806 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 807 | explicit __assoc_state_alloc(const _Alloc& __a) |
| 808 | : __alloc_(__a) {} |
| 809 | }; |
| 810 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 811 | template <class _Rp, class _Alloc> |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 812 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 813 | __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 814 | { |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 815 | typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al; |
| 816 | typedef allocator_traits<_Al> _ATraits; |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 817 | typedef pointer_traits<typename _ATraits::pointer> _PTraits; |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 818 | _Al __a(__alloc_); |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 819 | this->~__assoc_state_alloc(); |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 820 | __a.deallocate(_PTraits::pointer_to(*this), 1); |
Howard Hinnant | f4712b9 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 823 | template <class _Alloc> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 824 | class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 825 | : public __assoc_sub_state |
| 826 | { |
| 827 | typedef __assoc_sub_state base; |
| 828 | _Alloc __alloc_; |
| 829 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 830 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 831 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 832 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 833 | explicit __assoc_sub_state_alloc(const _Alloc& __a) |
| 834 | : __alloc_(__a) {} |
| 835 | }; |
| 836 | |
| 837 | template <class _Alloc> |
| 838 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 839 | __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 840 | { |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 841 | typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al; |
| 842 | typedef allocator_traits<_Al> _ATraits; |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 843 | typedef pointer_traits<typename _ATraits::pointer> _PTraits; |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 844 | _Al __a(__alloc_); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 845 | this->~__assoc_sub_state_alloc(); |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 846 | __a.deallocate(_PTraits::pointer_to(*this), 1); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 849 | template <class _Rp, class _Fp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 850 | class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 851 | : public __assoc_state<_Rp> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 852 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 853 | typedef __assoc_state<_Rp> base; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 854 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 855 | _Fp __func_; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 856 | |
| 857 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 858 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 859 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 860 | explicit __deferred_assoc_state(_Fp&& __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 861 | #endif |
| 862 | |
| 863 | virtual void __execute(); |
| 864 | }; |
| 865 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 866 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 867 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 868 | template <class _Rp, class _Fp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 869 | inline |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 870 | __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) |
| 871 | : __func_(_VSTD::forward<_Fp>(__f)) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 872 | { |
| 873 | this->__set_deferred(); |
| 874 | } |
| 875 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 876 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 877 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 878 | template <class _Rp, class _Fp> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 879 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 880 | __deferred_assoc_state<_Rp, _Fp>::__execute() |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 881 | { |
| 882 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 883 | try |
| 884 | { |
| 885 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 886 | this->set_value(__func_()); |
| 887 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 888 | } |
| 889 | catch (...) |
| 890 | { |
| 891 | this->set_exception(current_exception()); |
| 892 | } |
| 893 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 894 | } |
| 895 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 896 | template <class _Fp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 897 | class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 898 | : public __assoc_sub_state |
| 899 | { |
| 900 | typedef __assoc_sub_state base; |
| 901 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 902 | _Fp __func_; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 903 | |
| 904 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 905 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 906 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 907 | explicit __deferred_assoc_state(_Fp&& __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 908 | #endif |
| 909 | |
| 910 | virtual void __execute(); |
| 911 | }; |
| 912 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 913 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 914 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 915 | template <class _Fp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 916 | inline |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 917 | __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) |
| 918 | : __func_(_VSTD::forward<_Fp>(__f)) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 919 | { |
| 920 | this->__set_deferred(); |
| 921 | } |
| 922 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 923 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 924 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 925 | template <class _Fp> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 926 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 927 | __deferred_assoc_state<void, _Fp>::__execute() |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 928 | { |
| 929 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 930 | try |
| 931 | { |
| 932 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 933 | __func_(); |
| 934 | this->set_value(); |
| 935 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 936 | } |
| 937 | catch (...) |
| 938 | { |
| 939 | this->set_exception(current_exception()); |
| 940 | } |
| 941 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 942 | } |
| 943 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 944 | template <class _Rp, class _Fp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 945 | class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 946 | : public __assoc_state<_Rp> |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 947 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 948 | typedef __assoc_state<_Rp> base; |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 949 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 950 | _Fp __func_; |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 951 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 952 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 953 | public: |
| 954 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 955 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 956 | explicit __async_assoc_state(_Fp&& __f); |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 957 | #endif |
| 958 | |
| 959 | virtual void __execute(); |
| 960 | }; |
| 961 | |
| 962 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 963 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 964 | template <class _Rp, class _Fp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 965 | inline |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 966 | __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) |
| 967 | : __func_(_VSTD::forward<_Fp>(__f)) |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 968 | { |
| 969 | } |
| 970 | |
| 971 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 972 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 973 | template <class _Rp, class _Fp> |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 974 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 975 | __async_assoc_state<_Rp, _Fp>::__execute() |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 976 | { |
| 977 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 978 | try |
| 979 | { |
| 980 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 981 | this->set_value(__func_()); |
| 982 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 983 | } |
| 984 | catch (...) |
| 985 | { |
| 986 | this->set_exception(current_exception()); |
| 987 | } |
| 988 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 989 | } |
| 990 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 991 | template <class _Rp, class _Fp> |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 992 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 993 | __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 994 | { |
| 995 | this->wait(); |
| 996 | base::__on_zero_shared(); |
| 997 | } |
| 998 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 999 | template <class _Fp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1000 | class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp> |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1001 | : public __assoc_sub_state |
| 1002 | { |
| 1003 | typedef __assoc_sub_state base; |
| 1004 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1005 | _Fp __func_; |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1006 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1007 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1008 | public: |
| 1009 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1010 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1011 | explicit __async_assoc_state(_Fp&& __f); |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1012 | #endif |
| 1013 | |
| 1014 | virtual void __execute(); |
| 1015 | }; |
| 1016 | |
| 1017 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1018 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1019 | template <class _Fp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1020 | inline |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1021 | __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) |
| 1022 | : __func_(_VSTD::forward<_Fp>(__f)) |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1023 | { |
| 1024 | } |
| 1025 | |
| 1026 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1027 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1028 | template <class _Fp> |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1029 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1030 | __async_assoc_state<void, _Fp>::__execute() |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1031 | { |
| 1032 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1033 | try |
| 1034 | { |
| 1035 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1036 | __func_(); |
| 1037 | this->set_value(); |
| 1038 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1039 | } |
| 1040 | catch (...) |
| 1041 | { |
| 1042 | this->set_exception(current_exception()); |
| 1043 | } |
| 1044 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1045 | } |
| 1046 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1047 | template <class _Fp> |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1048 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1049 | __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1050 | { |
| 1051 | this->wait(); |
| 1052 | base::__on_zero_shared(); |
| 1053 | } |
| 1054 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1055 | template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise; |
| 1056 | template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1057 | |
| 1058 | // future |
| 1059 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1060 | template <class _Rp> class _LIBCPP_TEMPLATE_VIS future; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1061 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1062 | template <class _Rp, class _Fp> |
Louis Dionne | 3b967d0 | 2019-12-10 18:00:42 -0500 | [diff] [blame^] | 1063 | _LIBCPP_INLINE_VISIBILITY future<_Rp> |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1064 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1065 | __make_deferred_assoc_state(_Fp&& __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1066 | #else |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1067 | __make_deferred_assoc_state(_Fp __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1068 | #endif |
| 1069 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1070 | template <class _Rp, class _Fp> |
Louis Dionne | 3b967d0 | 2019-12-10 18:00:42 -0500 | [diff] [blame^] | 1071 | _LIBCPP_INLINE_VISIBILITY future<_Rp> |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1072 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1073 | __make_async_assoc_state(_Fp&& __f); |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1074 | #else |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1075 | __make_async_assoc_state(_Fp __f); |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 1076 | #endif |
| 1077 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1078 | template <class _Rp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1079 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1080 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1081 | __assoc_state<_Rp>* __state_; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1082 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1083 | explicit future(__assoc_state<_Rp>* __state); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1084 | |
| 1085 | template <class> friend class promise; |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1086 | template <class> friend class shared_future; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1087 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1088 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1089 | template <class _R1, class _Fp> |
| 1090 | friend future<_R1> __make_deferred_assoc_state(_Fp&& __f); |
| 1091 | template <class _R1, class _Fp> |
| 1092 | friend future<_R1> __make_async_assoc_state(_Fp&& __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1093 | #else |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1094 | template <class _R1, class _Fp> |
| 1095 | friend future<_R1> __make_deferred_assoc_state(_Fp __f); |
| 1096 | template <class _R1, class _Fp> |
| 1097 | friend future<_R1> __make_async_assoc_state(_Fp __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1098 | #endif |
| 1099 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1100 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1101 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1102 | future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1103 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1104 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1105 | future(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1106 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1107 | future(const future&) = delete; |
| 1108 | future& operator=(const future&) = delete; |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1109 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1110 | future& operator=(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1111 | { |
| 1112 | future(std::move(__rhs)).swap(*this); |
| 1113 | return *this; |
| 1114 | } |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1115 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1116 | private: |
| 1117 | future(const future&); |
| 1118 | future& operator=(const future&); |
| 1119 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1120 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1121 | ~future(); |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1122 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 79e0211 | 2017-01-24 23:28:25 +0000 | [diff] [blame] | 1123 | shared_future<_Rp> share() _NOEXCEPT; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1124 | |
| 1125 | // retrieving the value |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1126 | _Rp get(); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1127 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1128 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1129 | void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1130 | |
| 1131 | // functions to check state |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1132 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1133 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1134 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1135 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1136 | void wait() const {__state_->wait();} |
| 1137 | template <class _Rep, class _Period> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1138 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1139 | future_status |
| 1140 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 1141 | {return __state_->wait_for(__rel_time);} |
| 1142 | template <class _Clock, class _Duration> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1143 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1144 | future_status |
| 1145 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 1146 | {return __state_->wait_until(__abs_time);} |
| 1147 | }; |
| 1148 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1149 | template <class _Rp> |
| 1150 | future<_Rp>::future(__assoc_state<_Rp>* __state) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1151 | : __state_(__state) |
| 1152 | { |
Louis Dionne | 2c0d226 | 2018-08-24 14:00:59 +0000 | [diff] [blame] | 1153 | __state_->__attach_future(); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1156 | struct __release_shared_count |
| 1157 | { |
| 1158 | void operator()(__shared_count* p) {p->__release_shared();} |
| 1159 | }; |
| 1160 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1161 | template <class _Rp> |
| 1162 | future<_Rp>::~future() |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1163 | { |
| 1164 | if (__state_) |
| 1165 | __state_->__release_shared(); |
| 1166 | } |
| 1167 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1168 | template <class _Rp> |
| 1169 | _Rp |
| 1170 | future<_Rp>::get() |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1171 | { |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1172 | unique_ptr<__shared_count, __release_shared_count> __(__state_); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1173 | __assoc_state<_Rp>* __s = __state_; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1174 | __state_ = nullptr; |
| 1175 | return __s->move(); |
| 1176 | } |
| 1177 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1178 | template <class _Rp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1179 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1180 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1181 | __assoc_state<_Rp&>* __state_; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1182 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1183 | explicit future(__assoc_state<_Rp&>* __state); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1184 | |
| 1185 | template <class> friend class promise; |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1186 | template <class> friend class shared_future; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1187 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1188 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1189 | template <class _R1, class _Fp> |
| 1190 | friend future<_R1> __make_deferred_assoc_state(_Fp&& __f); |
| 1191 | template <class _R1, class _Fp> |
| 1192 | friend future<_R1> __make_async_assoc_state(_Fp&& __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1193 | #else |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1194 | template <class _R1, class _Fp> |
| 1195 | friend future<_R1> __make_deferred_assoc_state(_Fp __f); |
| 1196 | template <class _R1, class _Fp> |
| 1197 | friend future<_R1> __make_async_assoc_state(_Fp __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1198 | #endif |
| 1199 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1200 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1201 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1202 | future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1203 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1204 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1205 | future(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1206 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1207 | future(const future&) = delete; |
| 1208 | future& operator=(const future&) = delete; |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1209 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1210 | future& operator=(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1211 | { |
| 1212 | future(std::move(__rhs)).swap(*this); |
| 1213 | return *this; |
| 1214 | } |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1215 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1216 | private: |
| 1217 | future(const future&); |
| 1218 | future& operator=(const future&); |
| 1219 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1220 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1221 | ~future(); |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1222 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 79e0211 | 2017-01-24 23:28:25 +0000 | [diff] [blame] | 1223 | shared_future<_Rp&> share() _NOEXCEPT; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1224 | |
| 1225 | // retrieving the value |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1226 | _Rp& get(); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1227 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1228 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1229 | void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1230 | |
| 1231 | // functions to check state |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1232 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1233 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1234 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1235 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1236 | void wait() const {__state_->wait();} |
| 1237 | template <class _Rep, class _Period> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1238 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1239 | future_status |
| 1240 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 1241 | {return __state_->wait_for(__rel_time);} |
| 1242 | template <class _Clock, class _Duration> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1243 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1244 | future_status |
| 1245 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 1246 | {return __state_->wait_until(__abs_time);} |
| 1247 | }; |
| 1248 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1249 | template <class _Rp> |
| 1250 | future<_Rp&>::future(__assoc_state<_Rp&>* __state) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1251 | : __state_(__state) |
| 1252 | { |
Louis Dionne | 2c0d226 | 2018-08-24 14:00:59 +0000 | [diff] [blame] | 1253 | __state_->__attach_future(); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1256 | template <class _Rp> |
| 1257 | future<_Rp&>::~future() |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1258 | { |
| 1259 | if (__state_) |
| 1260 | __state_->__release_shared(); |
| 1261 | } |
| 1262 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1263 | template <class _Rp> |
| 1264 | _Rp& |
| 1265 | future<_Rp&>::get() |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1266 | { |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1267 | unique_ptr<__shared_count, __release_shared_count> __(__state_); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1268 | __assoc_state<_Rp&>* __s = __state_; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1269 | __state_ = nullptr; |
| 1270 | return __s->copy(); |
| 1271 | } |
| 1272 | |
| 1273 | template <> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1274 | class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE future<void> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1275 | { |
| 1276 | __assoc_sub_state* __state_; |
| 1277 | |
| 1278 | explicit future(__assoc_sub_state* __state); |
| 1279 | |
| 1280 | template <class> friend class promise; |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1281 | template <class> friend class shared_future; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1282 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1283 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1284 | template <class _R1, class _Fp> |
| 1285 | friend future<_R1> __make_deferred_assoc_state(_Fp&& __f); |
| 1286 | template <class _R1, class _Fp> |
| 1287 | friend future<_R1> __make_async_assoc_state(_Fp&& __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1288 | #else |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1289 | template <class _R1, class _Fp> |
| 1290 | friend future<_R1> __make_deferred_assoc_state(_Fp __f); |
| 1291 | template <class _R1, class _Fp> |
| 1292 | friend future<_R1> __make_async_assoc_state(_Fp __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1293 | #endif |
| 1294 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1295 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1296 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1297 | future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1298 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1299 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1300 | future(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1301 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1302 | future(const future&) = delete; |
| 1303 | future& operator=(const future&) = delete; |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1304 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1305 | future& operator=(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1306 | { |
| 1307 | future(std::move(__rhs)).swap(*this); |
| 1308 | return *this; |
| 1309 | } |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1310 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1311 | private: |
| 1312 | future(const future&); |
| 1313 | future& operator=(const future&); |
| 1314 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1315 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1316 | ~future(); |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1317 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 79e0211 | 2017-01-24 23:28:25 +0000 | [diff] [blame] | 1318 | shared_future<void> share() _NOEXCEPT; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1319 | |
| 1320 | // retrieving the value |
| 1321 | void get(); |
| 1322 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1323 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1324 | void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1325 | |
| 1326 | // functions to check state |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1327 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1328 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1329 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1330 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1331 | void wait() const {__state_->wait();} |
| 1332 | template <class _Rep, class _Period> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1333 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1334 | future_status |
| 1335 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 1336 | {return __state_->wait_for(__rel_time);} |
| 1337 | template <class _Clock, class _Duration> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1338 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1339 | future_status |
| 1340 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 1341 | {return __state_->wait_until(__abs_time);} |
| 1342 | }; |
| 1343 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1344 | template <class _Rp> |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1345 | inline _LIBCPP_INLINE_VISIBILITY |
| 1346 | void |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1347 | swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1348 | { |
| 1349 | __x.swap(__y); |
| 1350 | } |
| 1351 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1352 | // promise<R> |
| 1353 | |
Howard Hinnant | 944510a | 2011-06-14 19:58:17 +0000 | [diff] [blame] | 1354 | template <class _Callable> class packaged_task; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1355 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1356 | template <class _Rp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1357 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1358 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1359 | __assoc_state<_Rp>* __state_; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1360 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1361 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1362 | explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1363 | |
| 1364 | template <class> friend class packaged_task; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1365 | public: |
| 1366 | promise(); |
| 1367 | template <class _Alloc> |
| 1368 | promise(allocator_arg_t, const _Alloc& __a); |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1369 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1370 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1371 | promise(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1372 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1373 | promise(const promise& __rhs) = delete; |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1374 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1375 | private: |
| 1376 | promise(const promise& __rhs); |
| 1377 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1378 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1379 | ~promise(); |
| 1380 | |
| 1381 | // assignment |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1382 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1383 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1384 | promise& operator=(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1385 | { |
| 1386 | promise(std::move(__rhs)).swap(*this); |
| 1387 | return *this; |
| 1388 | } |
| 1389 | promise& operator=(const promise& __rhs) = delete; |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1390 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1391 | private: |
| 1392 | promise& operator=(const promise& __rhs); |
| 1393 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1394 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1395 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1396 | void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1397 | |
| 1398 | // retrieving the result |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1399 | future<_Rp> get_future(); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1400 | |
| 1401 | // setting the result |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1402 | void set_value(const _Rp& __r); |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1403 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1404 | void set_value(_Rp&& __r); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1405 | #endif |
| 1406 | void set_exception(exception_ptr __p); |
| 1407 | |
| 1408 | // setting the result with deferred notification |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1409 | void set_value_at_thread_exit(const _Rp& __r); |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1410 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1411 | void set_value_at_thread_exit(_Rp&& __r); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1412 | #endif |
| 1413 | void set_exception_at_thread_exit(exception_ptr __p); |
| 1414 | }; |
| 1415 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1416 | template <class _Rp> |
| 1417 | promise<_Rp>::promise() |
| 1418 | : __state_(new __assoc_state<_Rp>) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1419 | { |
| 1420 | } |
| 1421 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1422 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1423 | template <class _Alloc> |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1424 | promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1425 | { |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 1426 | typedef __assoc_state_alloc<_Rp, _Alloc> _State; |
| 1427 | typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1428 | typedef __allocator_destructor<_A2> _D2; |
| 1429 | _A2 __a(__a0); |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 1430 | unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1)); |
| 1431 | ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0); |
| 1432 | __state_ = _VSTD::addressof(*__hold.release()); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1435 | template <class _Rp> |
| 1436 | promise<_Rp>::~promise() |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1437 | { |
| 1438 | if (__state_) |
| 1439 | { |
| 1440 | if (!__state_->__has_value() && __state_->use_count() > 1) |
| 1441 | __state_->set_exception(make_exception_ptr( |
| 1442 | future_error(make_error_code(future_errc::broken_promise)) |
| 1443 | )); |
| 1444 | __state_->__release_shared(); |
| 1445 | } |
| 1446 | } |
| 1447 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1448 | template <class _Rp> |
| 1449 | future<_Rp> |
| 1450 | promise<_Rp>::get_future() |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1451 | { |
| 1452 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1453 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1454 | return future<_Rp>(__state_); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1457 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1458 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1459 | promise<_Rp>::set_value(const _Rp& __r) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1460 | { |
| 1461 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1462 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1463 | __state_->set_value(__r); |
| 1464 | } |
| 1465 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1466 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1467 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1468 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1469 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1470 | promise<_Rp>::set_value(_Rp&& __r) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1471 | { |
| 1472 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1473 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1474 | __state_->set_value(_VSTD::move(__r)); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1477 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1478 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1479 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1480 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1481 | promise<_Rp>::set_exception(exception_ptr __p) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1482 | { |
Marshall Clow | 2b36f57 | 2016-05-16 16:55:32 +0000 | [diff] [blame] | 1483 | _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" ); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1484 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1485 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1486 | __state_->set_exception(__p); |
| 1487 | } |
| 1488 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1489 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1490 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1491 | promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1492 | { |
| 1493 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1494 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1495 | __state_->set_value_at_thread_exit(__r); |
| 1496 | } |
| 1497 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1498 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1499 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1500 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1501 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1502 | promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1503 | { |
| 1504 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1505 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1506 | __state_->set_value_at_thread_exit(_VSTD::move(__r)); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1509 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1510 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1511 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1512 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1513 | promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1514 | { |
Eric Fiselier | 9dce206 | 2016-05-31 01:50:55 +0000 | [diff] [blame] | 1515 | _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" ); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1516 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1517 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1518 | __state_->set_exception_at_thread_exit(__p); |
| 1519 | } |
| 1520 | |
| 1521 | // promise<R&> |
| 1522 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1523 | template <class _Rp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1524 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1525 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1526 | __assoc_state<_Rp&>* __state_; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1527 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1528 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1529 | explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1530 | |
| 1531 | template <class> friend class packaged_task; |
| 1532 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1533 | public: |
| 1534 | promise(); |
| 1535 | template <class _Allocator> |
| 1536 | promise(allocator_arg_t, const _Allocator& __a); |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1537 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1538 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1539 | promise(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1540 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1541 | promise(const promise& __rhs) = delete; |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1542 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1543 | private: |
| 1544 | promise(const promise& __rhs); |
| 1545 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1546 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1547 | ~promise(); |
| 1548 | |
| 1549 | // assignment |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1550 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1551 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1552 | promise& operator=(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1553 | { |
| 1554 | promise(std::move(__rhs)).swap(*this); |
| 1555 | return *this; |
| 1556 | } |
| 1557 | promise& operator=(const promise& __rhs) = delete; |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1558 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1559 | private: |
| 1560 | promise& operator=(const promise& __rhs); |
| 1561 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1562 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1563 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1564 | void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1565 | |
| 1566 | // retrieving the result |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1567 | future<_Rp&> get_future(); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1568 | |
| 1569 | // setting the result |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1570 | void set_value(_Rp& __r); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1571 | void set_exception(exception_ptr __p); |
| 1572 | |
| 1573 | // setting the result with deferred notification |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1574 | void set_value_at_thread_exit(_Rp&); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1575 | void set_exception_at_thread_exit(exception_ptr __p); |
| 1576 | }; |
| 1577 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1578 | template <class _Rp> |
| 1579 | promise<_Rp&>::promise() |
| 1580 | : __state_(new __assoc_state<_Rp&>) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1581 | { |
| 1582 | } |
| 1583 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1584 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1585 | template <class _Alloc> |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1586 | promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1587 | { |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 1588 | typedef __assoc_state_alloc<_Rp&, _Alloc> _State; |
| 1589 | typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1590 | typedef __allocator_destructor<_A2> _D2; |
| 1591 | _A2 __a(__a0); |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 1592 | unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1)); |
| 1593 | ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0); |
| 1594 | __state_ = _VSTD::addressof(*__hold.release()); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1597 | template <class _Rp> |
| 1598 | promise<_Rp&>::~promise() |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1599 | { |
| 1600 | if (__state_) |
| 1601 | { |
| 1602 | if (!__state_->__has_value() && __state_->use_count() > 1) |
| 1603 | __state_->set_exception(make_exception_ptr( |
| 1604 | future_error(make_error_code(future_errc::broken_promise)) |
| 1605 | )); |
| 1606 | __state_->__release_shared(); |
| 1607 | } |
| 1608 | } |
| 1609 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1610 | template <class _Rp> |
| 1611 | future<_Rp&> |
| 1612 | promise<_Rp&>::get_future() |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1613 | { |
| 1614 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1615 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1616 | return future<_Rp&>(__state_); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1619 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1620 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1621 | promise<_Rp&>::set_value(_Rp& __r) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1622 | { |
| 1623 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1624 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1625 | __state_->set_value(__r); |
| 1626 | } |
| 1627 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1628 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1629 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1630 | promise<_Rp&>::set_exception(exception_ptr __p) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1631 | { |
Marshall Clow | 2b36f57 | 2016-05-16 16:55:32 +0000 | [diff] [blame] | 1632 | _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" ); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1633 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1634 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1635 | __state_->set_exception(__p); |
| 1636 | } |
| 1637 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1638 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1639 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1640 | promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1641 | { |
| 1642 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1643 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1644 | __state_->set_value_at_thread_exit(__r); |
| 1645 | } |
| 1646 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1647 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1648 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1649 | promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1650 | { |
Eric Fiselier | 9dce206 | 2016-05-31 01:50:55 +0000 | [diff] [blame] | 1651 | _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" ); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1652 | if (__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 1653 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1654 | __state_->set_exception_at_thread_exit(__p); |
| 1655 | } |
| 1656 | |
| 1657 | // promise<void> |
| 1658 | |
| 1659 | template <> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1660 | class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<void> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1661 | { |
| 1662 | __assoc_sub_state* __state_; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1663 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1664 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1665 | explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1666 | |
| 1667 | template <class> friend class packaged_task; |
| 1668 | |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1669 | public: |
| 1670 | promise(); |
| 1671 | template <class _Allocator> |
Shoaib Meenai | 55f3a46 | 2017-03-02 03:22:18 +0000 | [diff] [blame] | 1672 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1673 | promise(allocator_arg_t, const _Allocator& __a); |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1674 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1675 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1676 | promise(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1677 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1678 | promise(const promise& __rhs) = delete; |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1679 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1680 | private: |
| 1681 | promise(const promise& __rhs); |
| 1682 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1683 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1684 | ~promise(); |
| 1685 | |
| 1686 | // assignment |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1687 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1688 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1689 | promise& operator=(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1690 | { |
| 1691 | promise(std::move(__rhs)).swap(*this); |
| 1692 | return *this; |
| 1693 | } |
| 1694 | promise& operator=(const promise& __rhs) = delete; |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1695 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1696 | private: |
| 1697 | promise& operator=(const promise& __rhs); |
| 1698 | public: |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1699 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1700 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1701 | void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1702 | |
| 1703 | // retrieving the result |
| 1704 | future<void> get_future(); |
| 1705 | |
| 1706 | // setting the result |
| 1707 | void set_value(); |
| 1708 | void set_exception(exception_ptr __p); |
| 1709 | |
| 1710 | // setting the result with deferred notification |
| 1711 | void set_value_at_thread_exit(); |
| 1712 | void set_exception_at_thread_exit(exception_ptr __p); |
| 1713 | }; |
| 1714 | |
| 1715 | template <class _Alloc> |
| 1716 | promise<void>::promise(allocator_arg_t, const _Alloc& __a0) |
| 1717 | { |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 1718 | typedef __assoc_sub_state_alloc<_Alloc> _State; |
| 1719 | typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1720 | typedef __allocator_destructor<_A2> _D2; |
| 1721 | _A2 __a(__a0); |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 1722 | unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1)); |
| 1723 | ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0); |
| 1724 | __state_ = _VSTD::addressof(*__hold.release()); |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1727 | template <class _Rp> |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1728 | inline _LIBCPP_INLINE_VISIBILITY |
| 1729 | void |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1730 | swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1731 | { |
| 1732 | __x.swap(__y); |
| 1733 | } |
| 1734 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1735 | template <class _Rp, class _Alloc> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1736 | struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1737 | : public true_type {}; |
Howard Hinnant | 3820f6b | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1738 | |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1739 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 1740 | |
| 1741 | // packaged_task |
| 1742 | |
| 1743 | template<class _Fp> class __packaged_task_base; |
| 1744 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1745 | template<class _Rp, class ..._ArgTypes> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1746 | class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1747 | { |
| 1748 | __packaged_task_base(const __packaged_task_base&); |
| 1749 | __packaged_task_base& operator=(const __packaged_task_base&); |
| 1750 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1751 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1752 | __packaged_task_base() {} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1753 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1754 | virtual ~__packaged_task_base() {} |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1755 | virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1756 | virtual void destroy() = 0; |
| 1757 | virtual void destroy_deallocate() = 0; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1758 | virtual _Rp operator()(_ArgTypes&& ...) = 0; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1759 | }; |
| 1760 | |
| 1761 | template<class _FD, class _Alloc, class _FB> class __packaged_task_func; |
| 1762 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1763 | template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1764 | class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1765 | : public __packaged_task_base<_Rp(_ArgTypes...)> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1766 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1767 | __compressed_pair<_Fp, _Alloc> __f_; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1768 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1769 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1770 | explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1771 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1772 | explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1773 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1774 | __packaged_task_func(const _Fp& __f, const _Alloc& __a) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1775 | : __f_(__f, __a) {} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1776 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1777 | __packaged_task_func(_Fp&& __f, const _Alloc& __a) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1778 | : __f_(_VSTD::move(__f), __a) {} |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1779 | virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1780 | virtual void destroy(); |
| 1781 | virtual void destroy_deallocate(); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1782 | virtual _Rp operator()(_ArgTypes&& ... __args); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1783 | }; |
| 1784 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1785 | template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1786 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1787 | __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to( |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1788 | __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1789 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1790 | ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second())); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1793 | template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1794 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1795 | __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1796 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1797 | __f_.~__compressed_pair<_Fp, _Alloc>(); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1800 | template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1801 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1802 | __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1803 | { |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 1804 | typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap; |
| 1805 | typedef allocator_traits<_Ap> _ATraits; |
| 1806 | typedef pointer_traits<typename _ATraits::pointer> _PTraits; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1807 | _Ap __a(__f_.second()); |
| 1808 | __f_.~__compressed_pair<_Fp, _Alloc>(); |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 1809 | __a.deallocate(_PTraits::pointer_to(*this), 1); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1812 | template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> |
| 1813 | _Rp |
| 1814 | __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1815 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1816 | return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Howard Hinnant | 944510a | 2011-06-14 19:58:17 +0000 | [diff] [blame] | 1819 | template <class _Callable> class __packaged_task_function; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1820 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1821 | template<class _Rp, class ..._ArgTypes> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1822 | class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1823 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1824 | typedef __packaged_task_base<_Rp(_ArgTypes...)> __base; |
Howard Hinnant | 022c748 | 2013-01-21 17:26:55 +0000 | [diff] [blame] | 1825 | typename aligned_storage<3*sizeof(void*)>::type __buf_; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1826 | __base* __f_; |
| 1827 | |
| 1828 | public: |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1829 | typedef _Rp result_type; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1830 | |
| 1831 | // construct/copy/destroy: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1832 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1833 | __packaged_task_function() _NOEXCEPT : __f_(nullptr) {} |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1834 | template<class _Fp> |
| 1835 | __packaged_task_function(_Fp&& __f); |
| 1836 | template<class _Fp, class _Alloc> |
| 1837 | __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1838 | |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1839 | __packaged_task_function(__packaged_task_function&&) _NOEXCEPT; |
| 1840 | __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1841 | |
| 1842 | __packaged_task_function(const __packaged_task_function&) = delete; |
| 1843 | __packaged_task_function& operator=(const __packaged_task_function&) = delete; |
| 1844 | |
| 1845 | ~__packaged_task_function(); |
| 1846 | |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1847 | void swap(__packaged_task_function&) _NOEXCEPT; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1848 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1849 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1850 | _Rp operator()(_ArgTypes...) const; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1851 | }; |
| 1852 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1853 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1854 | __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1855 | { |
| 1856 | if (__f.__f_ == nullptr) |
| 1857 | __f_ = nullptr; |
| 1858 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1859 | { |
| 1860 | __f_ = (__base*)&__buf_; |
| 1861 | __f.__f_->__move_to(__f_); |
| 1862 | } |
| 1863 | else |
| 1864 | { |
| 1865 | __f_ = __f.__f_; |
| 1866 | __f.__f_ = nullptr; |
| 1867 | } |
| 1868 | } |
| 1869 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1870 | template<class _Rp, class ..._ArgTypes> |
| 1871 | template <class _Fp> |
| 1872 | __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1873 | : __f_(nullptr) |
| 1874 | { |
Marshall Clow | 733d60e | 2014-04-07 13:32:26 +0000 | [diff] [blame] | 1875 | typedef typename remove_reference<typename decay<_Fp>::type>::type _FR; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1876 | typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1877 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1878 | { |
| 1879 | __f_ = (__base*)&__buf_; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1880 | ::new (__f_) _FF(_VSTD::forward<_Fp>(__f)); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1881 | } |
| 1882 | else |
| 1883 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1884 | typedef allocator<_FF> _Ap; |
| 1885 | _Ap __a; |
| 1886 | typedef __allocator_destructor<_Ap> _Dp; |
| 1887 | unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); |
| 1888 | ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a)); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1889 | __f_ = __hold.release(); |
| 1890 | } |
| 1891 | } |
| 1892 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1893 | template<class _Rp, class ..._ArgTypes> |
| 1894 | template <class _Fp, class _Alloc> |
| 1895 | __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function( |
| 1896 | allocator_arg_t, const _Alloc& __a0, _Fp&& __f) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1897 | : __f_(nullptr) |
| 1898 | { |
Marshall Clow | 733d60e | 2014-04-07 13:32:26 +0000 | [diff] [blame] | 1899 | typedef typename remove_reference<typename decay<_Fp>::type>::type _FR; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1900 | typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1901 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1902 | { |
| 1903 | __f_ = (__base*)&__buf_; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1904 | ::new (__f_) _FF(_VSTD::forward<_Fp>(__f)); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1905 | } |
| 1906 | else |
| 1907 | { |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 1908 | typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1909 | _Ap __a(__a0); |
| 1910 | typedef __allocator_destructor<_Ap> _Dp; |
| 1911 | unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); |
Eric Fiselier | 0d10927 | 2014-10-23 06:24:45 +0000 | [diff] [blame] | 1912 | ::new (static_cast<void*>(_VSTD::addressof(*__hold.get()))) |
| 1913 | _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a)); |
| 1914 | __f_ = _VSTD::addressof(*__hold.release()); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1918 | template<class _Rp, class ..._ArgTypes> |
| 1919 | __packaged_task_function<_Rp(_ArgTypes...)>& |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1920 | __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1921 | { |
| 1922 | if (__f_ == (__base*)&__buf_) |
| 1923 | __f_->destroy(); |
| 1924 | else if (__f_) |
| 1925 | __f_->destroy_deallocate(); |
| 1926 | __f_ = nullptr; |
| 1927 | if (__f.__f_ == nullptr) |
| 1928 | __f_ = nullptr; |
| 1929 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1930 | { |
| 1931 | __f_ = (__base*)&__buf_; |
| 1932 | __f.__f_->__move_to(__f_); |
| 1933 | } |
| 1934 | else |
| 1935 | { |
| 1936 | __f_ = __f.__f_; |
| 1937 | __f.__f_ = nullptr; |
| 1938 | } |
Argyrios Kyrtzidis | af90465 | 2012-10-13 02:03:45 +0000 | [diff] [blame] | 1939 | return *this; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1942 | template<class _Rp, class ..._ArgTypes> |
| 1943 | __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1944 | { |
| 1945 | if (__f_ == (__base*)&__buf_) |
| 1946 | __f_->destroy(); |
| 1947 | else if (__f_) |
| 1948 | __f_->destroy_deallocate(); |
| 1949 | } |
| 1950 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1951 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1952 | void |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1953 | __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1954 | { |
| 1955 | if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) |
| 1956 | { |
| 1957 | typename aligned_storage<sizeof(__buf_)>::type __tempbuf; |
| 1958 | __base* __t = (__base*)&__tempbuf; |
| 1959 | __f_->__move_to(__t); |
| 1960 | __f_->destroy(); |
| 1961 | __f_ = nullptr; |
| 1962 | __f.__f_->__move_to((__base*)&__buf_); |
| 1963 | __f.__f_->destroy(); |
| 1964 | __f.__f_ = nullptr; |
| 1965 | __f_ = (__base*)&__buf_; |
| 1966 | __t->__move_to((__base*)&__f.__buf_); |
| 1967 | __t->destroy(); |
| 1968 | __f.__f_ = (__base*)&__f.__buf_; |
| 1969 | } |
| 1970 | else if (__f_ == (__base*)&__buf_) |
| 1971 | { |
| 1972 | __f_->__move_to((__base*)&__f.__buf_); |
| 1973 | __f_->destroy(); |
| 1974 | __f_ = __f.__f_; |
| 1975 | __f.__f_ = (__base*)&__f.__buf_; |
| 1976 | } |
| 1977 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1978 | { |
| 1979 | __f.__f_->__move_to((__base*)&__buf_); |
| 1980 | __f.__f_->destroy(); |
| 1981 | __f.__f_ = __f_; |
| 1982 | __f_ = (__base*)&__buf_; |
| 1983 | } |
| 1984 | else |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1985 | _VSTD::swap(__f_, __f.__f_); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1988 | template<class _Rp, class ..._ArgTypes> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1989 | inline |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1990 | _Rp |
| 1991 | __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1992 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1993 | return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1996 | template<class _Rp, class ..._ArgTypes> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 1997 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1998 | { |
| 1999 | public: |
Eric Fiselier | 17f39ed | 2016-06-01 21:05:53 +0000 | [diff] [blame] | 2000 | typedef _Rp result_type; // extension |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2001 | |
| 2002 | private: |
| 2003 | __packaged_task_function<result_type(_ArgTypes...)> __f_; |
| 2004 | promise<result_type> __p_; |
| 2005 | |
| 2006 | public: |
| 2007 | // construction and destruction |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2008 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2009 | packaged_task() _NOEXCEPT : __p_(nullptr) {} |
Marshall Clow | 27a55c0 | 2013-10-12 22:49:17 +0000 | [diff] [blame] | 2010 | template <class _Fp, |
| 2011 | class = typename enable_if |
| 2012 | < |
| 2013 | !is_same< |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2014 | typename __uncvref<_Fp>::type, |
Marshall Clow | 27a55c0 | 2013-10-12 22:49:17 +0000 | [diff] [blame] | 2015 | packaged_task |
| 2016 | >::value |
| 2017 | >::type |
| 2018 | > |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2019 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2020 | explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {} |
Marshall Clow | a0a057e | 2017-11-27 20:47:54 +0000 | [diff] [blame] | 2021 | template <class _Fp, class _Allocator, |
| 2022 | class = typename enable_if |
| 2023 | < |
| 2024 | !is_same< |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2025 | typename __uncvref<_Fp>::type, |
Marshall Clow | a0a057e | 2017-11-27 20:47:54 +0000 | [diff] [blame] | 2026 | packaged_task |
| 2027 | >::value |
| 2028 | >::type |
| 2029 | > |
| 2030 | _LIBCPP_INLINE_VISIBILITY |
| 2031 | packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f) |
| 2032 | : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)), |
| 2033 | __p_(allocator_arg, __a) {} |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2034 | // ~packaged_task() = default; |
| 2035 | |
| 2036 | // no copy |
Howard Hinnant | ab50007 | 2012-07-21 19:34:12 +0000 | [diff] [blame] | 2037 | packaged_task(const packaged_task&) = delete; |
| 2038 | packaged_task& operator=(const packaged_task&) = delete; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2039 | |
| 2040 | // move support |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2041 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2042 | packaged_task(packaged_task&& __other) _NOEXCEPT |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2043 | : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2044 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2045 | packaged_task& operator=(packaged_task&& __other) _NOEXCEPT |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2046 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2047 | __f_ = _VSTD::move(__other.__f_); |
| 2048 | __p_ = _VSTD::move(__other.__p_); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2049 | return *this; |
| 2050 | } |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2051 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2052 | void swap(packaged_task& __other) _NOEXCEPT |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2053 | { |
| 2054 | __f_.swap(__other.__f_); |
| 2055 | __p_.swap(__other.__p_); |
| 2056 | } |
| 2057 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2058 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2059 | bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;} |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2060 | |
| 2061 | // result retrieval |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2062 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2063 | future<result_type> get_future() {return __p_.get_future();} |
| 2064 | |
| 2065 | // execution |
| 2066 | void operator()(_ArgTypes... __args); |
| 2067 | void make_ready_at_thread_exit(_ArgTypes... __args); |
| 2068 | |
| 2069 | void reset(); |
| 2070 | }; |
| 2071 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2072 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2073 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2074 | packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2075 | { |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2076 | if (__p_.__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 2077 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2078 | if (__p_.__state_->__has_value()) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 2079 | __throw_future_error(future_errc::promise_already_satisfied); |
Marshall Clow | 7b3742d | 2015-09-03 15:11:32 +0000 | [diff] [blame] | 2080 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2081 | try |
| 2082 | { |
| 2083 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2084 | __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...)); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2085 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2086 | } |
| 2087 | catch (...) |
| 2088 | { |
| 2089 | __p_.set_exception(current_exception()); |
| 2090 | } |
| 2091 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2092 | } |
| 2093 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2094 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2095 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2096 | packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2097 | { |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2098 | if (__p_.__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 2099 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2100 | if (__p_.__state_->__has_value()) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 2101 | __throw_future_error(future_errc::promise_already_satisfied); |
Marshall Clow | 7b3742d | 2015-09-03 15:11:32 +0000 | [diff] [blame] | 2102 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2103 | try |
| 2104 | { |
| 2105 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2106 | __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...)); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2107 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2108 | } |
| 2109 | catch (...) |
| 2110 | { |
| 2111 | __p_.set_exception_at_thread_exit(current_exception()); |
| 2112 | } |
| 2113 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2114 | } |
| 2115 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2116 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2117 | void |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2118 | packaged_task<_Rp(_ArgTypes...)>::reset() |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2119 | { |
Howard Hinnant | 9a7d09c | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 2120 | if (!valid()) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 2121 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2122 | __p_ = promise<result_type>(); |
| 2123 | } |
| 2124 | |
| 2125 | template<class ..._ArgTypes> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 2126 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)> |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2127 | { |
| 2128 | public: |
Eric Fiselier | 17f39ed | 2016-06-01 21:05:53 +0000 | [diff] [blame] | 2129 | typedef void result_type; // extension |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2130 | |
| 2131 | private: |
| 2132 | __packaged_task_function<result_type(_ArgTypes...)> __f_; |
| 2133 | promise<result_type> __p_; |
| 2134 | |
| 2135 | public: |
| 2136 | // construction and destruction |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2137 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2138 | packaged_task() _NOEXCEPT : __p_(nullptr) {} |
Marshall Clow | 27a55c0 | 2013-10-12 22:49:17 +0000 | [diff] [blame] | 2139 | template <class _Fp, |
| 2140 | class = typename enable_if |
| 2141 | < |
| 2142 | !is_same< |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2143 | typename __uncvref<_Fp>::type, |
Marshall Clow | 27a55c0 | 2013-10-12 22:49:17 +0000 | [diff] [blame] | 2144 | packaged_task |
| 2145 | >::value |
| 2146 | >::type |
| 2147 | > |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2148 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2149 | explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {} |
Marshall Clow | a0a057e | 2017-11-27 20:47:54 +0000 | [diff] [blame] | 2150 | template <class _Fp, class _Allocator, |
| 2151 | class = typename enable_if |
| 2152 | < |
| 2153 | !is_same< |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2154 | typename __uncvref<_Fp>::type, |
Marshall Clow | a0a057e | 2017-11-27 20:47:54 +0000 | [diff] [blame] | 2155 | packaged_task |
| 2156 | >::value |
| 2157 | >::type |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 2158 | > |
Marshall Clow | a0a057e | 2017-11-27 20:47:54 +0000 | [diff] [blame] | 2159 | _LIBCPP_INLINE_VISIBILITY |
| 2160 | packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f) |
| 2161 | : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)), |
| 2162 | __p_(allocator_arg, __a) {} |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2163 | // ~packaged_task() = default; |
| 2164 | |
| 2165 | // no copy |
Howard Hinnant | ab50007 | 2012-07-21 19:34:12 +0000 | [diff] [blame] | 2166 | packaged_task(const packaged_task&) = delete; |
| 2167 | packaged_task& operator=(const packaged_task&) = delete; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2168 | |
| 2169 | // move support |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2170 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2171 | packaged_task(packaged_task&& __other) _NOEXCEPT |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2172 | : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2173 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2174 | packaged_task& operator=(packaged_task&& __other) _NOEXCEPT |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2175 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2176 | __f_ = _VSTD::move(__other.__f_); |
| 2177 | __p_ = _VSTD::move(__other.__p_); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2178 | return *this; |
| 2179 | } |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2180 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2181 | void swap(packaged_task& __other) _NOEXCEPT |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2182 | { |
| 2183 | __f_.swap(__other.__f_); |
| 2184 | __p_.swap(__other.__p_); |
| 2185 | } |
| 2186 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2187 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2188 | bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;} |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2189 | |
| 2190 | // result retrieval |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2191 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2192 | future<result_type> get_future() {return __p_.get_future();} |
| 2193 | |
| 2194 | // execution |
| 2195 | void operator()(_ArgTypes... __args); |
| 2196 | void make_ready_at_thread_exit(_ArgTypes... __args); |
| 2197 | |
| 2198 | void reset(); |
| 2199 | }; |
| 2200 | |
| 2201 | template<class ..._ArgTypes> |
| 2202 | void |
| 2203 | packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) |
| 2204 | { |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2205 | if (__p_.__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 2206 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2207 | if (__p_.__state_->__has_value()) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 2208 | __throw_future_error(future_errc::promise_already_satisfied); |
Marshall Clow | 7b3742d | 2015-09-03 15:11:32 +0000 | [diff] [blame] | 2209 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2210 | try |
| 2211 | { |
| 2212 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2213 | __f_(_VSTD::forward<_ArgTypes>(__args)...); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2214 | __p_.set_value(); |
| 2215 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2216 | } |
| 2217 | catch (...) |
| 2218 | { |
| 2219 | __p_.set_exception(current_exception()); |
| 2220 | } |
| 2221 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2222 | } |
| 2223 | |
| 2224 | template<class ..._ArgTypes> |
| 2225 | void |
| 2226 | packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) |
| 2227 | { |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2228 | if (__p_.__state_ == nullptr) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 2229 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2230 | if (__p_.__state_->__has_value()) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 2231 | __throw_future_error(future_errc::promise_already_satisfied); |
Marshall Clow | 7b3742d | 2015-09-03 15:11:32 +0000 | [diff] [blame] | 2232 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2233 | try |
| 2234 | { |
| 2235 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2236 | __f_(_VSTD::forward<_ArgTypes>(__args)...); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2237 | __p_.set_value_at_thread_exit(); |
| 2238 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2239 | } |
| 2240 | catch (...) |
| 2241 | { |
| 2242 | __p_.set_exception_at_thread_exit(current_exception()); |
| 2243 | } |
| 2244 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2245 | } |
| 2246 | |
| 2247 | template<class ..._ArgTypes> |
| 2248 | void |
| 2249 | packaged_task<void(_ArgTypes...)>::reset() |
| 2250 | { |
Howard Hinnant | 9a7d09c | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 2251 | if (!valid()) |
Eric Fiselier | 4364159 | 2015-10-02 21:25:15 +0000 | [diff] [blame] | 2252 | __throw_future_error(future_errc::no_state); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2253 | __p_ = promise<result_type>(); |
| 2254 | } |
| 2255 | |
| 2256 | template <class _Callable> |
| 2257 | inline _LIBCPP_INLINE_VISIBILITY |
| 2258 | void |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2259 | swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2260 | { |
| 2261 | __x.swap(__y); |
| 2262 | } |
| 2263 | |
Marshall Clow | a0a057e | 2017-11-27 20:47:54 +0000 | [diff] [blame] | 2264 | template <class _Callable, class _Alloc> |
| 2265 | struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc> |
| 2266 | : public true_type {}; |
| 2267 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2268 | template <class _Rp, class _Fp> |
Louis Dionne | 3b967d0 | 2019-12-10 18:00:42 -0500 | [diff] [blame^] | 2269 | _LIBCPP_INLINE_VISIBILITY future<_Rp> |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2270 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2271 | __make_deferred_assoc_state(_Fp&& __f) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2272 | #else |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2273 | __make_deferred_assoc_state(_Fp __f) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2274 | #endif |
| 2275 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2276 | unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> |
| 2277 | __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f))); |
| 2278 | return future<_Rp>(__h.get()); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2281 | template <class _Rp, class _Fp> |
Louis Dionne | 3b967d0 | 2019-12-10 18:00:42 -0500 | [diff] [blame^] | 2282 | _LIBCPP_INLINE_VISIBILITY future<_Rp> |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2283 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2284 | __make_async_assoc_state(_Fp&& __f) |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2285 | #else |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2286 | __make_async_assoc_state(_Fp __f) |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2287 | #endif |
| 2288 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2289 | unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> |
| 2290 | __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f))); |
| 2291 | _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach(); |
| 2292 | return future<_Rp>(__h.get()); |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2295 | template <class _Fp, class... _Args> |
Louis Dionne | 3b967d0 | 2019-12-10 18:00:42 -0500 | [diff] [blame^] | 2296 | class _LIBCPP_HIDDEN __async_func |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2297 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2298 | tuple<_Fp, _Args...> __f_; |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2299 | |
| 2300 | public: |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2301 | typedef typename __invoke_of<_Fp, _Args...>::type _Rp; |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2302 | |
| 2303 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2304 | explicit __async_func(_Fp&& __f, _Args&&... __args) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2305 | : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {} |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2306 | |
| 2307 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2308 | __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {} |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2309 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2310 | _Rp operator()() |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2311 | { |
| 2312 | typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index; |
| 2313 | return __execute(_Index()); |
| 2314 | } |
| 2315 | private: |
| 2316 | template <size_t ..._Indices> |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2317 | _Rp |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2318 | __execute(__tuple_indices<_Indices...>) |
| 2319 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2320 | return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...); |
Howard Hinnant | 95cfd87 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2321 | } |
| 2322 | }; |
| 2323 | |
Marshall Clow | d56a5b6 | 2013-11-03 22:06:53 +0000 | [diff] [blame] | 2324 | inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value ) |
Marshall Clow | 335b799 | 2013-11-03 15:43:35 +0000 | [diff] [blame] | 2325 | { return (int(__policy) & int(__value)) != 0; } |
| 2326 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2327 | template <class _Fp, class... _Args> |
Marshall Clow | 6ac349c | 2017-11-23 01:25:03 +0000 | [diff] [blame] | 2328 | _LIBCPP_NODISCARD_AFTER_CXX17 |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2329 | future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type> |
| 2330 | async(launch __policy, _Fp&& __f, _Args&&... __args) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2331 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2332 | typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF; |
| 2333 | typedef typename _BF::_Rp _Rp; |
Marshall Clow | 335b799 | 2013-11-03 15:43:35 +0000 | [diff] [blame] | 2334 | |
| 2335 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2336 | try |
| 2337 | { |
| 2338 | #endif |
| 2339 | if (__does_policy_contain(__policy, launch::async)) |
| 2340 | return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)), |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2341 | __decay_copy(_VSTD::forward<_Args>(__args))...)); |
Marshall Clow | 335b799 | 2013-11-03 15:43:35 +0000 | [diff] [blame] | 2342 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2343 | } |
| 2344 | catch ( ... ) { if (__policy == launch::async) throw ; } |
| 2345 | #endif |
| 2346 | |
| 2347 | if (__does_policy_contain(__policy, launch::deferred)) |
| 2348 | return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)), |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2349 | __decay_copy(_VSTD::forward<_Args>(__args))...)); |
Marshall Clow | 335b799 | 2013-11-03 15:43:35 +0000 | [diff] [blame] | 2350 | return future<_Rp>{}; |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2353 | template <class _Fp, class... _Args> |
Marshall Clow | 6ac349c | 2017-11-23 01:25:03 +0000 | [diff] [blame] | 2354 | _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2355 | future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type> |
| 2356 | async(_Fp&& __f, _Args&&... __args) |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2357 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2358 | return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f), |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2359 | _VSTD::forward<_Args>(__args)...); |
Howard Hinnant | ccdd203 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 2363 | |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2364 | // shared_future |
| 2365 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2366 | template <class _Rp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2367 | class _LIBCPP_TEMPLATE_VIS shared_future |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2368 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2369 | __assoc_state<_Rp>* __state_; |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2370 | |
| 2371 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2372 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2373 | shared_future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2374 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 6f9c48b | 2016-11-14 19:58:05 +0000 | [diff] [blame] | 2375 | shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2376 | {if (__state_) __state_->__add_shared();} |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2377 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2378 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2379 | shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2380 | {__f.__state_ = nullptr;} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2381 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2382 | shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2383 | {__rhs.__state_ = nullptr;} |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2384 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2385 | ~shared_future(); |
Marshall Clow | 6f9c48b | 2016-11-14 19:58:05 +0000 | [diff] [blame] | 2386 | shared_future& operator=(const shared_future& __rhs) _NOEXCEPT; |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2387 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2388 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2389 | shared_future& operator=(shared_future&& __rhs) _NOEXCEPT |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2390 | { |
| 2391 | shared_future(std::move(__rhs)).swap(*this); |
| 2392 | return *this; |
| 2393 | } |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2394 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2395 | |
| 2396 | // retrieving the value |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2397 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2398 | const _Rp& get() const {return __state_->copy();} |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2399 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2400 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2401 | void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2402 | |
| 2403 | // functions to check state |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2404 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2405 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2406 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2407 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2408 | void wait() const {__state_->wait();} |
| 2409 | template <class _Rep, class _Period> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2410 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2411 | future_status |
| 2412 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 2413 | {return __state_->wait_for(__rel_time);} |
| 2414 | template <class _Clock, class _Duration> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2415 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2416 | future_status |
| 2417 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 2418 | {return __state_->wait_until(__abs_time);} |
| 2419 | }; |
| 2420 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2421 | template <class _Rp> |
| 2422 | shared_future<_Rp>::~shared_future() |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2423 | { |
| 2424 | if (__state_) |
| 2425 | __state_->__release_shared(); |
| 2426 | } |
| 2427 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2428 | template <class _Rp> |
| 2429 | shared_future<_Rp>& |
Marshall Clow | 6f9c48b | 2016-11-14 19:58:05 +0000 | [diff] [blame] | 2430 | shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2431 | { |
| 2432 | if (__rhs.__state_) |
| 2433 | __rhs.__state_->__add_shared(); |
| 2434 | if (__state_) |
| 2435 | __state_->__release_shared(); |
| 2436 | __state_ = __rhs.__state_; |
| 2437 | return *this; |
| 2438 | } |
| 2439 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2440 | template <class _Rp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2441 | class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&> |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2442 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2443 | __assoc_state<_Rp&>* __state_; |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2444 | |
| 2445 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2446 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2447 | shared_future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2448 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2449 | shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) |
| 2450 | {if (__state_) __state_->__add_shared();} |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2451 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2452 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2453 | shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2454 | {__f.__state_ = nullptr;} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2455 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2456 | shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2457 | {__rhs.__state_ = nullptr;} |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2458 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2459 | ~shared_future(); |
| 2460 | shared_future& operator=(const shared_future& __rhs); |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2461 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2462 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2463 | shared_future& operator=(shared_future&& __rhs) _NOEXCEPT |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2464 | { |
| 2465 | shared_future(std::move(__rhs)).swap(*this); |
| 2466 | return *this; |
| 2467 | } |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2468 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2469 | |
| 2470 | // retrieving the value |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2471 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2472 | _Rp& get() const {return __state_->copy();} |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2473 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2474 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2475 | void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2476 | |
| 2477 | // functions to check state |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2478 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2479 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2480 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2481 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2482 | void wait() const {__state_->wait();} |
| 2483 | template <class _Rep, class _Period> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2484 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2485 | future_status |
| 2486 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 2487 | {return __state_->wait_for(__rel_time);} |
| 2488 | template <class _Clock, class _Duration> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2489 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2490 | future_status |
| 2491 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 2492 | {return __state_->wait_until(__abs_time);} |
| 2493 | }; |
| 2494 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2495 | template <class _Rp> |
| 2496 | shared_future<_Rp&>::~shared_future() |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2497 | { |
| 2498 | if (__state_) |
| 2499 | __state_->__release_shared(); |
| 2500 | } |
| 2501 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2502 | template <class _Rp> |
| 2503 | shared_future<_Rp&>& |
| 2504 | shared_future<_Rp&>::operator=(const shared_future& __rhs) |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2505 | { |
| 2506 | if (__rhs.__state_) |
| 2507 | __rhs.__state_->__add_shared(); |
| 2508 | if (__state_) |
| 2509 | __state_->__release_shared(); |
| 2510 | __state_ = __rhs.__state_; |
| 2511 | return *this; |
| 2512 | } |
| 2513 | |
| 2514 | template <> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 2515 | class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void> |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2516 | { |
| 2517 | __assoc_sub_state* __state_; |
| 2518 | |
| 2519 | public: |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2520 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2521 | shared_future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2522 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2523 | shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) |
| 2524 | {if (__state_) __state_->__add_shared();} |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2525 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2526 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2527 | shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2528 | {__f.__state_ = nullptr;} |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2529 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2530 | shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2531 | {__rhs.__state_ = nullptr;} |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2532 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2533 | ~shared_future(); |
| 2534 | shared_future& operator=(const shared_future& __rhs); |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2535 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2536 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2537 | shared_future& operator=(shared_future&& __rhs) _NOEXCEPT |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2538 | { |
| 2539 | shared_future(std::move(__rhs)).swap(*this); |
| 2540 | return *this; |
| 2541 | } |
Howard Hinnant | 74279a5 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2542 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2543 | |
| 2544 | // retrieving the value |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2545 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2546 | void get() const {__state_->copy();} |
| 2547 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2548 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2549 | void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2550 | |
| 2551 | // functions to check state |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2552 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2553 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2554 | |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2555 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2556 | void wait() const {__state_->wait();} |
| 2557 | template <class _Rep, class _Period> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2558 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2559 | future_status |
| 2560 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 2561 | {return __state_->wait_for(__rel_time);} |
| 2562 | template <class _Clock, class _Duration> |
Howard Hinnant | 684902d | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2563 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2564 | future_status |
| 2565 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 2566 | {return __state_->wait_until(__abs_time);} |
| 2567 | }; |
| 2568 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2569 | template <class _Rp> |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2570 | inline _LIBCPP_INLINE_VISIBILITY |
| 2571 | void |
Howard Hinnant | 2244804 | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2572 | swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT |
Howard Hinnant | 92646c4 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2573 | { |
| 2574 | __x.swap(__y); |
| 2575 | } |
| 2576 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2577 | template <class _Rp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2578 | inline |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2579 | shared_future<_Rp> |
Marshall Clow | 79e0211 | 2017-01-24 23:28:25 +0000 | [diff] [blame] | 2580 | future<_Rp>::share() _NOEXCEPT |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2581 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2582 | return shared_future<_Rp>(_VSTD::move(*this)); |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2585 | template <class _Rp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2586 | inline |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2587 | shared_future<_Rp&> |
Marshall Clow | 79e0211 | 2017-01-24 23:28:25 +0000 | [diff] [blame] | 2588 | future<_Rp&>::share() _NOEXCEPT |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2589 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2590 | return shared_future<_Rp&>(_VSTD::move(*this)); |
Howard Hinnant | 9a7d09c | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Howard Hinnant | e65e8e3 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 2593 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 2594 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2595 | inline |
Howard Hinnant | 9a7d09c | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 2596 | shared_future<void> |
Marshall Clow | 79e0211 | 2017-01-24 23:28:25 +0000 | [diff] [blame] | 2597 | future<void>::share() _NOEXCEPT |
Howard Hinnant | 9a7d09c | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 2598 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2599 | return shared_future<void>(_VSTD::move(*this)); |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
Howard Hinnant | e65e8e3 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 2602 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 2603 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2604 | _LIBCPP_END_NAMESPACE_STD |
| 2605 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 2606 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 2607 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2608 | #endif // _LIBCPP_FUTURE |