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