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