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