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