brintos

brintos / llvm-project-archived public Read only

0
0
Text · 64.8 KiB · 0877d66 Raw
2074 lines · plain
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP_FUTURE11#define _LIBCPP_FUTURE12 13/*14    future synopsis15 16namespace std17{18 19enum class future_errc20{21    future_already_retrieved = 1,22    promise_already_satisfied,23    no_state,24    broken_promise25};26 27enum class launch28{29    async = 1,30    deferred = 2,31    any = async | deferred32};33 34enum class future_status35{36    ready,37    timeout,38    deferred39};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++1750 51    const error_code& code() const noexcept;52    const char*       what() const noexcept;53 54private:55    error_code ec_;             // exposition only56};57 58template <class R>59class promise60{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    // assignment70    promise& operator=(promise&& rhs) noexcept;71    promise& operator=(const promise& rhs) = delete;72    void swap(promise& other) noexcept;73 74    // retrieving the result75    future<R> get_future();76 77    // setting the result78    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 notification83    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    // assignment100    promise& operator=(promise&& rhs) noexcept;101    promise& operator=(const promise& rhs) = delete;102    void swap(promise& other) noexcept;103 104    // retrieving the result105    future<R&> get_future();106 107    // setting the result108    void set_value(R& r);109    void set_exception(exception_ptr p);110 111    // setting the result with deferred notification112    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    // assignment128    promise& operator=(promise&& rhs) noexcept;129    promise& operator=(const promise& rhs) = delete;130    void swap(promise& other) noexcept;131 132    // retrieving the result133    future<void> get_future();134 135    // setting the result136    void set_value();137    void set_exception(exception_ptr p);138 139    // setting the result with deferred notification140    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 future151{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 value162    R get();163 164    // functions to check state165    bool valid() const noexcept;166 167    void wait() const;168    template <class Rep, class Period>169        future_status170        wait_for(const chrono::duration<Rep, Period>& rel_time) const;171    template <class Clock, class Duration>172        future_status173        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 value189    R& get();190 191    // functions to check state192    bool valid() const noexcept;193 194    void wait() const;195    template <class Rep, class Period>196        future_status197        wait_for(const chrono::duration<Rep, Period>& rel_time) const;198    template <class Clock, class Duration>199        future_status200        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 value216    void get();217 218    // functions to check state219    bool valid() const noexcept;220 221    void wait() const;222    template <class Rep, class Period>223        future_status224        wait_for(const chrono::duration<Rep, Period>& rel_time) const;225    template <class Clock, class Duration>226        future_status227        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;228};229 230template <class R>231class shared_future232{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 value243    const R& get() const;244 245    // functions to check state246    bool valid() const noexcept;247 248    void wait() const;249    template <class Rep, class Period>250        future_status251        wait_for(const chrono::duration<Rep, Period>& rel_time) const;252    template <class Clock, class Duration>253        future_status254        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 value270    R& get() const;271 272    // functions to check state273    bool valid() const noexcept;274 275    void wait() const;276    template <class Rep, class Period>277        future_status278        wait_for(const chrono::duration<Rep, Period>& rel_time) const;279    template <class Clock, class Duration>280        future_status281        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 value297    void get() const;298 299    // functions to check state300    bool valid() const noexcept;301 302    void wait() const;303    template <class Rep, class Period>304        future_status305        wait_for(const chrono::duration<Rep, Period>& rel_time) const;306    template <class Clock, class Duration>307        future_status308        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; // undefined320 321template <class R, class... ArgTypes>322class packaged_task<R(ArgTypes...)>323{324public:325    // construction and destruction326    packaged_task() noexcept;327    template <class F>328        explicit packaged_task(F&& f);329    template <class F, class Allocator>330        packaged_task(allocator_arg_t, const Allocator& a, F&& f);              // removed in C++17331    ~packaged_task();332 333    // no copy334    packaged_task(const packaged_task&) = delete;335    packaged_task& operator=(const packaged_task&) = delete;336 337    // move support338    packaged_task(packaged_task&& other) noexcept;339    packaged_task& operator=(packaged_task&& other) noexcept;340    void swap(packaged_task& other) noexcept;341 342    bool valid() const noexcept;343 344    // result retrieval345    future<R> get_future();346 347    // execution348    void operator()(ArgTypes... );349    void make_ready_at_thread_exit(ArgTypes...);350 351    void reset();352};353 354template <class R>355  void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;356 357template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>; // removed in C++17358 359}  // std360 361*/362 363#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)364#  include <__cxx03/future>365#else366#  include <__config>367 368#  if _LIBCPP_HAS_THREADS369 370#    include <__assert>371#    include <__chrono/duration.h>372#    include <__chrono/steady_clock.h>373#    include <__chrono/time_point.h>374#    include <__condition_variable/condition_variable.h>375#    include <__cstddef/nullptr_t.h>376#    include <__exception/exception_ptr.h>377#    include <__memory/addressof.h>378#    include <__memory/allocator.h>379#    include <__memory/allocator_arg_t.h>380#    include <__memory/allocator_destructor.h>381#    include <__memory/allocator_traits.h>382#    include <__memory/compressed_pair.h>383#    include <__memory/pointer_traits.h>384#    include <__memory/shared_count.h>385#    include <__memory/unique_ptr.h>386#    include <__memory/uses_allocator.h>387#    include <__mutex/lock_guard.h>388#    include <__mutex/mutex.h>389#    include <__mutex/unique_lock.h>390#    include <__system_error/error_category.h>391#    include <__system_error/error_code.h>392#    include <__system_error/error_condition.h>393#    include <__thread/thread.h>394#    include <__type_traits/add_reference.h>395#    include <__type_traits/aligned_storage.h>396#    include <__type_traits/conditional.h>397#    include <__type_traits/decay.h>398#    include <__type_traits/enable_if.h>399#    include <__type_traits/invoke.h>400#    include <__type_traits/is_same.h>401#    include <__type_traits/remove_cvref.h>402#    include <__type_traits/remove_reference.h>403#    include <__type_traits/strip_signature.h>404#    include <__type_traits/underlying_type.h>405#    include <__utility/auto_cast.h>406#    include <__utility/exception_guard.h>407#    include <__utility/forward.h>408#    include <__utility/move.h>409#    include <__utility/swap.h>410#    include <stdexcept>411#    include <tuple>412#    include <version>413 414#    if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)415#      pragma GCC system_header416#    endif417 418_LIBCPP_PUSH_MACROS419#    include <__undef_macros>420 421_LIBCPP_BEGIN_NAMESPACE_STD422 423// enum class future_errc424_LIBCPP_DECLARE_STRONG_ENUM(future_errc){425    future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};426_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)427 428template <>429struct is_error_code_enum<future_errc> : public true_type {};430 431#    ifdef _LIBCPP_CXX03_LANG432template <>433struct is_error_code_enum<future_errc::__lx> : public true_type {};434#    endif435 436// enum class launch437_LIBCPP_DECLARE_STRONG_ENUM(launch){async = 1, deferred = 2, any = async | deferred};438_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)439 440#    ifndef _LIBCPP_CXX03_LANG441 442using __launch_underlying_type _LIBCPP_NODEBUG = __underlying_type_t<launch>;443 444inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator&(launch __x, launch __y) {445  return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));446}447 448inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator|(launch __x, launch __y) {449  return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | static_cast<__launch_underlying_type>(__y));450}451 452inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator^(launch __x, launch __y) {453  return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));454}455 456inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator~(launch __x) {457  return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);458}459 460inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {461  __x = __x & __y;462  return __x;463}464 465inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {466  __x = __x | __y;467  return __x;468}469 470inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {471  __x = __x ^ __y;472  return __x;473}474 475#    endif // !_LIBCPP_CXX03_LANG476 477// enum class future_status478_LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};479_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)480 481_LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;482 483inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {484  return error_code(static_cast<int>(__e), future_category());485}486 487inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {488  return error_condition(static_cast<int>(__e), future_category());489}490 491[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);492 493class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {494  error_code __ec_;495 496  future_error(error_code);497  friend void __throw_future_error(future_errc);498  template <class>499  friend class promise;500 501public:502#    if _LIBCPP_STD_VER >= 17503  _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}504#    endif505 506  _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }507 508  _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;509  ~future_error() _NOEXCEPT override;510};511 512// Declared above std::future_error513void __throw_future_error(future_errc __ev) {514#    if _LIBCPP_HAS_EXCEPTIONS515  throw future_error(make_error_code(__ev));516#    else517  (void)__ev;518  _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");519#    endif520}521 522class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {523protected:524  exception_ptr __exception_;525  mutable mutex __mut_;526  mutable condition_variable __cv_;527  unsigned __state_;528 529  void __on_zero_shared() _NOEXCEPT override;530  void __sub_wait(unique_lock<mutex>& __lk);531 532public:533  enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };534 535  _LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}536 537  _LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }538 539  _LIBCPP_HIDE_FROM_ABI void __attach_future() {540    lock_guard<mutex> __lk(__mut_);541    bool __has_future_attached = (__state_ & __future_attached) != 0;542    if (__has_future_attached)543      std::__throw_future_error(future_errc::future_already_retrieved);544    this->__add_shared();545    __state_ |= __future_attached;546  }547 548  _LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }549 550  void __make_ready();551  _LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }552 553  void set_value();554  void set_value_at_thread_exit();555 556  void set_exception(exception_ptr __p);557  void set_exception_at_thread_exit(exception_ptr __p);558 559  void copy();560 561  void wait();562  template <class _Rep, class _Period>563  future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;564  template <class _Clock, class _Duration>565  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {566    unique_lock<mutex> __lk(__mut_);567    if (__state_ & deferred)568      return future_status::deferred;569    while (!(__state_ & ready) && _Clock::now() < __abs_time)570      __cv_.wait_until(__lk, __abs_time);571    if (__state_ & ready)572      return future_status::ready;573    return future_status::timeout;574  }575 576  virtual void __execute();577};578 579template <class _Rep, class _Period>580inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {581  return wait_until(chrono::steady_clock::now() + __rel_time);582}583 584template <class _Rp>585class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {586  typedef __assoc_sub_state base;587 588protected:589  _ALIGNAS_TYPE(_Rp) char __value_[sizeof(_Rp)];590 591  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;592 593public:594  template <class _Arg>595  _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);596 597  template <class _Arg>598  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);599 600  _LIBCPP_HIDE_FROM_ABI _Rp move();601  _LIBCPP_HIDE_FROM_ABI _Rp& copy();602};603 604template <class _Rp>605void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {606  if (this->__state_ & base::__constructed)607    reinterpret_cast<_Rp*>(std::addressof(__value_))->~_Rp();608  delete this;609}610 611template <class _Rp>612template <class _Arg>613void __assoc_state<_Rp>::set_value(_Arg&& __arg) {614  unique_lock<mutex> __lk(this->__mut_);615  if (this->__has_value())616    std::__throw_future_error(future_errc::promise_already_satisfied);617  ::new ((void*)std::addressof(__value_)) _Rp(std::forward<_Arg>(__arg));618  this->__state_ |= base::__constructed | base::ready;619  __cv_.notify_all();620}621 622template <class _Rp>623template <class _Arg>624void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {625  unique_lock<mutex> __lk(this->__mut_);626  if (this->__has_value())627    std::__throw_future_error(future_errc::promise_already_satisfied);628  ::new ((void*)std::addressof(__value_)) _Rp(std::forward<_Arg>(__arg));629  this->__state_ |= base::__constructed;630  __thread_local_data()->__make_ready_at_thread_exit(this);631}632 633template <class _Rp>634_Rp __assoc_state<_Rp>::move() {635  unique_lock<mutex> __lk(this->__mut_);636  this->__sub_wait(__lk);637  if (this->__exception_ != nullptr)638    std::rethrow_exception(this->__exception_);639  return std::move(*reinterpret_cast<_Rp*>(std::addressof(__value_)));640}641 642template <class _Rp>643_Rp& __assoc_state<_Rp>::copy() {644  unique_lock<mutex> __lk(this->__mut_);645  this->__sub_wait(__lk);646  if (this->__exception_ != nullptr)647    std::rethrow_exception(this->__exception_);648  return *reinterpret_cast<_Rp*>(std::addressof(__value_));649}650 651template <class _Rp>652class __assoc_state<_Rp&> : public __assoc_sub_state {653  typedef __assoc_sub_state base;654  typedef _Rp* _Up;655 656protected:657  _Up __value_;658 659  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;660 661public:662  _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);663  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);664 665  _LIBCPP_HIDE_FROM_ABI _Rp& copy();666};667 668template <class _Rp>669void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {670  delete this;671}672 673template <class _Rp>674void __assoc_state<_Rp&>::set_value(_Rp& __arg) {675  unique_lock<mutex> __lk(this->__mut_);676  if (this->__has_value())677    std::__throw_future_error(future_errc::promise_already_satisfied);678  __value_ = std::addressof(__arg);679  this->__state_ |= base::__constructed | base::ready;680  __cv_.notify_all();681}682 683template <class _Rp>684void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {685  unique_lock<mutex> __lk(this->__mut_);686  if (this->__has_value())687    std::__throw_future_error(future_errc::promise_already_satisfied);688  __value_ = std::addressof(__arg);689  this->__state_ |= base::__constructed;690  __thread_local_data()->__make_ready_at_thread_exit(this);691}692 693template <class _Rp>694_Rp& __assoc_state<_Rp&>::copy() {695  unique_lock<mutex> __lk(this->__mut_);696  this->__sub_wait(__lk);697  if (this->__exception_ != nullptr)698    std::rethrow_exception(this->__exception_);699  return *__value_;700}701 702template <class _Rp, class _Alloc>703class __assoc_state_alloc : public __assoc_state<_Rp> {704  typedef __assoc_state<_Rp> base;705  _Alloc __alloc_;706 707  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;708 709public:710  _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}711};712 713template <class _Rp, class _Alloc>714void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {715  if (this->__state_ & base::__constructed)716    reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();717  typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;718  typedef allocator_traits<_Al> _ATraits;719  typedef pointer_traits<typename _ATraits::pointer> _PTraits;720  _Al __a(__alloc_);721  this->~__assoc_state_alloc();722  __a.deallocate(_PTraits::pointer_to(*this), 1);723}724 725template <class _Rp, class _Alloc>726class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {727  typedef __assoc_state<_Rp&> base;728  _Alloc __alloc_;729 730  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;731 732public:733  _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}734};735 736template <class _Rp, class _Alloc>737void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {738  typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;739  typedef allocator_traits<_Al> _ATraits;740  typedef pointer_traits<typename _ATraits::pointer> _PTraits;741  _Al __a(__alloc_);742  this->~__assoc_state_alloc();743  __a.deallocate(_PTraits::pointer_to(*this), 1);744}745 746template <class _Alloc>747class __assoc_sub_state_alloc : public __assoc_sub_state {748  typedef __assoc_sub_state base;749  _Alloc __alloc_;750 751  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;752 753public:754  _LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}755};756 757template <class _Alloc>758void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {759  typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;760  typedef allocator_traits<_Al> _ATraits;761  typedef pointer_traits<typename _ATraits::pointer> _PTraits;762  _Al __a(__alloc_);763  this->~__assoc_sub_state_alloc();764  __a.deallocate(_PTraits::pointer_to(*this), 1);765}766 767template <class _Rp, class _Fp>768class __deferred_assoc_state : public __assoc_state<_Rp> {769  typedef __assoc_state<_Rp> base;770 771  _Fp __func_;772 773public:774  _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);775 776  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();777};778 779template <class _Rp, class _Fp>780inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {781  this->__set_deferred();782}783 784template <class _Rp, class _Fp>785void __deferred_assoc_state<_Rp, _Fp>::__execute() {786#    if _LIBCPP_HAS_EXCEPTIONS787  try {788#    endif // _LIBCPP_HAS_EXCEPTIONS789    this->set_value(__func_());790#    if _LIBCPP_HAS_EXCEPTIONS791  } catch (...) {792    this->set_exception(current_exception());793  }794#    endif // _LIBCPP_HAS_EXCEPTIONS795}796 797template <class _Fp>798class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {799  typedef __assoc_sub_state base;800 801  _Fp __func_;802 803public:804  _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);805 806  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;807};808 809template <class _Fp>810inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {811  this->__set_deferred();812}813 814template <class _Fp>815void __deferred_assoc_state<void, _Fp>::__execute() {816#    if _LIBCPP_HAS_EXCEPTIONS817  try {818#    endif // _LIBCPP_HAS_EXCEPTIONS819    __func_();820    this->set_value();821#    if _LIBCPP_HAS_EXCEPTIONS822  } catch (...) {823    this->set_exception(current_exception());824  }825#    endif // _LIBCPP_HAS_EXCEPTIONS826}827 828template <class _Rp, class _Fp>829class __async_assoc_state : public __assoc_state<_Rp> {830  typedef __assoc_state<_Rp> base;831 832  _Fp __func_;833 834  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;835 836public:837  _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);838 839  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();840};841 842template <class _Rp, class _Fp>843inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}844 845template <class _Rp, class _Fp>846void __async_assoc_state<_Rp, _Fp>::__execute() {847#    if _LIBCPP_HAS_EXCEPTIONS848  try {849#    endif // _LIBCPP_HAS_EXCEPTIONS850    this->set_value(__func_());851#    if _LIBCPP_HAS_EXCEPTIONS852  } catch (...) {853    this->set_exception(current_exception());854  }855#    endif // _LIBCPP_HAS_EXCEPTIONS856}857 858template <class _Rp, class _Fp>859void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {860  this->wait();861  base::__on_zero_shared();862}863 864template <class _Fp>865class __async_assoc_state<void, _Fp> : public __assoc_sub_state {866  typedef __assoc_sub_state base;867 868  _Fp __func_;869 870  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;871 872public:873  _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);874 875  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;876};877 878template <class _Fp>879inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}880 881template <class _Fp>882void __async_assoc_state<void, _Fp>::__execute() {883#    if _LIBCPP_HAS_EXCEPTIONS884  try {885#    endif // _LIBCPP_HAS_EXCEPTIONS886    __func_();887    this->set_value();888#    if _LIBCPP_HAS_EXCEPTIONS889  } catch (...) {890    this->set_exception(current_exception());891  }892#    endif // _LIBCPP_HAS_EXCEPTIONS893}894 895template <class _Fp>896void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {897  this->wait();898  base::__on_zero_shared();899}900 901template <class _Rp>902class promise;903template <class _Rp>904class shared_future;905 906// future907 908template <class _Rp>909class future;910 911template <class _Rp, class _Fp>912_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);913 914template <class _Rp, class _Fp>915_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);916 917template <class _Rp>918class future {919  __assoc_state<_Rp>* __state_;920 921  explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);922 923  template <class>924  friend class promise;925  template <class>926  friend class shared_future;927 928  template <class _R1, class _Fp>929  friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);930  template <class _R1, class _Fp>931  friend future<_R1> __make_async_assoc_state(_Fp&& __f);932 933public:934  _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}935  _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }936  future(const future&)            = delete;937  future& operator=(const future&) = delete;938  _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {939    future(std::move(__rhs)).swap(*this);940    return *this;941  }942 943  _LIBCPP_HIDE_FROM_ABI ~future();944  _LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;945 946  // retrieving the value947  _LIBCPP_HIDE_FROM_ABI _Rp get();948 949  _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }950 951  // functions to check state952  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }953 954  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }955  template <class _Rep, class _Period>956  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {957    return __state_->wait_for(__rel_time);958  }959  template <class _Clock, class _Duration>960  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {961    return __state_->wait_until(__abs_time);962  }963};964 965template <class _Rp>966future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {967  __state_->__attach_future();968}969 970struct __release_shared_count {971  _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }972};973 974template <class _Rp>975future<_Rp>::~future() {976  if (__state_)977    __state_->__release_shared();978}979 980template <class _Rp>981_Rp future<_Rp>::get() {982  unique_ptr<__shared_count, __release_shared_count> __guard(__state_);983  __assoc_state<_Rp>* __s = __state_;984  __state_                = nullptr;985  return __s->move();986}987 988template <class _Rp>989class future<_Rp&> {990  __assoc_state<_Rp&>* __state_;991 992  explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);993 994  template <class>995  friend class promise;996  template <class>997  friend class shared_future;998 999  template <class _R1, class _Fp>1000  friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);1001  template <class _R1, class _Fp>1002  friend future<_R1> __make_async_assoc_state(_Fp&& __f);1003 1004public:1005  _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}1006  _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }1007  future(const future&)            = delete;1008  future& operator=(const future&) = delete;1009  _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {1010    future(std::move(__rhs)).swap(*this);1011    return *this;1012  }1013 1014  _LIBCPP_HIDE_FROM_ABI ~future();1015  _LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;1016 1017  // retrieving the value1018  _LIBCPP_HIDE_FROM_ABI _Rp& get();1019 1020  _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }1021 1022  // functions to check state1023  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }1024 1025  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }1026  template <class _Rep, class _Period>1027  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {1028    return __state_->wait_for(__rel_time);1029  }1030  template <class _Clock, class _Duration>1031  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {1032    return __state_->wait_until(__abs_time);1033  }1034};1035 1036template <class _Rp>1037future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {1038  __state_->__attach_future();1039}1040 1041template <class _Rp>1042future<_Rp&>::~future() {1043  if (__state_)1044    __state_->__release_shared();1045}1046 1047template <class _Rp>1048_Rp& future<_Rp&>::get() {1049  unique_ptr<__shared_count, __release_shared_count> __guard(__state_);1050  __assoc_state<_Rp&>* __s = __state_;1051  __state_                 = nullptr;1052  return __s->copy();1053}1054 1055template <>1056class _LIBCPP_EXPORTED_FROM_ABI future<void> {1057  __assoc_sub_state* __state_;1058 1059  explicit future(__assoc_sub_state* __state);1060 1061  template <class>1062  friend class promise;1063  template <class>1064  friend class shared_future;1065 1066  template <class _R1, class _Fp>1067  friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);1068  template <class _R1, class _Fp>1069  friend future<_R1> __make_async_assoc_state(_Fp&& __f);1070 1071public:1072  _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}1073  _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }1074  future(const future&)            = delete;1075  future& operator=(const future&) = delete;1076  _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {1077    future(std::move(__rhs)).swap(*this);1078    return *this;1079  }1080 1081  ~future();1082  _LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;1083 1084  // retrieving the value1085  void get();1086 1087  _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }1088 1089  // functions to check state1090  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }1091 1092  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }1093  template <class _Rep, class _Period>1094  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {1095    return __state_->wait_for(__rel_time);1096  }1097  template <class _Clock, class _Duration>1098  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {1099    return __state_->wait_until(__abs_time);1100  }1101};1102 1103template <class _Rp>1104inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {1105  __x.swap(__y);1106}1107 1108// promise<R>1109 1110template <class _Callable>1111class packaged_task;1112 1113template <class _Rp>1114class promise {1115  __assoc_state<_Rp>* __state_;1116 1117  _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}1118 1119  template <class>1120  friend class packaged_task;1121 1122public:1123  _LIBCPP_HIDE_FROM_ABI promise();1124  template <class _Alloc>1125  _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);1126  _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }1127  promise(const promise& __rhs) = delete;1128  _LIBCPP_HIDE_FROM_ABI ~promise();1129 1130  // assignment1131  _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {1132    promise(std::move(__rhs)).swap(*this);1133    return *this;1134  }1135  promise& operator=(const promise& __rhs) = delete;1136 1137  _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }1138 1139  // retrieving the result1140  _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();1141 1142  // setting the result1143  _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);1144  _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);1145  _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);1146 1147  // setting the result with deferred notification1148  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);1149  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);1150  _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);1151};1152 1153template <class _Rp>1154promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}1155 1156template <class _Rp>1157template <class _Alloc>1158promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {1159  typedef __assoc_state_alloc<_Rp, _Alloc> _State;1160  typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;1161  typedef __allocator_destructor<_A2> _D2;1162  _A2 __a(__a0);1163  unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));1164  ::new ((void*)std::addressof(*__hold.get())) _State(__a0);1165  __state_ = std::addressof(*__hold.release());1166}1167 1168template <class _Rp>1169promise<_Rp>::~promise() {1170  if (__state_) {1171    if (!__state_->__has_value() && __state_->use_count() > 1)1172      __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));1173    __state_->__release_shared();1174  }1175}1176 1177template <class _Rp>1178future<_Rp> promise<_Rp>::get_future() {1179  if (__state_ == nullptr)1180    std::__throw_future_error(future_errc::no_state);1181  return future<_Rp>(__state_);1182}1183 1184template <class _Rp>1185void promise<_Rp>::set_value(const _Rp& __r) {1186  if (__state_ == nullptr)1187    std::__throw_future_error(future_errc::no_state);1188  __state_->set_value(__r);1189}1190 1191template <class _Rp>1192void promise<_Rp>::set_value(_Rp&& __r) {1193  if (__state_ == nullptr)1194    std::__throw_future_error(future_errc::no_state);1195  __state_->set_value(std::move(__r));1196}1197 1198template <class _Rp>1199void promise<_Rp>::set_exception(exception_ptr __p) {1200  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");1201  if (__state_ == nullptr)1202    std::__throw_future_error(future_errc::no_state);1203  __state_->set_exception(__p);1204}1205 1206template <class _Rp>1207void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {1208  if (__state_ == nullptr)1209    std::__throw_future_error(future_errc::no_state);1210  __state_->set_value_at_thread_exit(__r);1211}1212 1213template <class _Rp>1214void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {1215  if (__state_ == nullptr)1216    std::__throw_future_error(future_errc::no_state);1217  __state_->set_value_at_thread_exit(std::move(__r));1218}1219 1220template <class _Rp>1221void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {1222  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");1223  if (__state_ == nullptr)1224    std::__throw_future_error(future_errc::no_state);1225  __state_->set_exception_at_thread_exit(__p);1226}1227 1228// promise<R&>1229 1230template <class _Rp>1231class promise<_Rp&> {1232  __assoc_state<_Rp&>* __state_;1233 1234  _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}1235 1236  template <class>1237  friend class packaged_task;1238 1239public:1240  _LIBCPP_HIDE_FROM_ABI promise();1241  template <class _Allocator>1242  _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);1243  _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }1244  promise(const promise& __rhs) = delete;1245  _LIBCPP_HIDE_FROM_ABI ~promise();1246 1247  // assignment1248  _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {1249    promise(std::move(__rhs)).swap(*this);1250    return *this;1251  }1252  promise& operator=(const promise& __rhs) = delete;1253 1254  _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }1255 1256  // retrieving the result1257  _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();1258 1259  // setting the result1260  _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);1261  _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);1262 1263  // setting the result with deferred notification1264  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);1265  _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);1266};1267 1268template <class _Rp>1269promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}1270 1271template <class _Rp>1272template <class _Alloc>1273promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {1274  typedef __assoc_state_alloc<_Rp&, _Alloc> _State;1275  typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;1276  typedef __allocator_destructor<_A2> _D2;1277  _A2 __a(__a0);1278  unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));1279  ::new ((void*)std::addressof(*__hold.get())) _State(__a0);1280  __state_ = std::addressof(*__hold.release());1281}1282 1283template <class _Rp>1284promise<_Rp&>::~promise() {1285  if (__state_) {1286    if (!__state_->__has_value() && __state_->use_count() > 1)1287      __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));1288    __state_->__release_shared();1289  }1290}1291 1292template <class _Rp>1293future<_Rp&> promise<_Rp&>::get_future() {1294  if (__state_ == nullptr)1295    std::__throw_future_error(future_errc::no_state);1296  return future<_Rp&>(__state_);1297}1298 1299template <class _Rp>1300void promise<_Rp&>::set_value(_Rp& __r) {1301  if (__state_ == nullptr)1302    std::__throw_future_error(future_errc::no_state);1303  __state_->set_value(__r);1304}1305 1306template <class _Rp>1307void promise<_Rp&>::set_exception(exception_ptr __p) {1308  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");1309  if (__state_ == nullptr)1310    std::__throw_future_error(future_errc::no_state);1311  __state_->set_exception(__p);1312}1313 1314template <class _Rp>1315void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {1316  if (__state_ == nullptr)1317    std::__throw_future_error(future_errc::no_state);1318  __state_->set_value_at_thread_exit(__r);1319}1320 1321template <class _Rp>1322void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {1323  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");1324  if (__state_ == nullptr)1325    std::__throw_future_error(future_errc::no_state);1326  __state_->set_exception_at_thread_exit(__p);1327}1328 1329// promise<void>1330 1331template <>1332class _LIBCPP_EXPORTED_FROM_ABI promise<void> {1333  __assoc_sub_state* __state_;1334 1335  _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}1336 1337  template <class>1338  friend class packaged_task;1339 1340public:1341  promise();1342  template <class _Alloc>1343  _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a0) {1344    typedef __assoc_sub_state_alloc<_Alloc> _State;1345    typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;1346    typedef __allocator_destructor<_A2> _D2;1347    _A2 __a(__a0);1348    unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));1349    ::new ((void*)std::addressof(*__hold.get())) _State(__a0);1350    __state_ = std::addressof(*__hold.release());1351  }1352 1353  _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }1354  promise(const promise& __rhs) = delete;1355  ~promise();1356 1357  // assignment1358  _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {1359    promise(std::move(__rhs)).swap(*this);1360    return *this;1361  }1362  promise& operator=(const promise& __rhs) = delete;1363 1364  _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }1365 1366  // retrieving the result1367  future<void> get_future();1368 1369  // setting the result1370  void set_value();1371  void set_exception(exception_ptr __p);1372 1373  // setting the result with deferred notification1374  void set_value_at_thread_exit();1375  void set_exception_at_thread_exit(exception_ptr __p);1376};1377 1378template <class _Rp>1379inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {1380  __x.swap(__y);1381}1382 1383template <class _Rp, class _Alloc>1384struct uses_allocator<promise<_Rp>, _Alloc> : public true_type {};1385 1386// packaged_task1387 1388template <class _Fp>1389class __packaged_task_base;1390 1391template <class _Rp, class... _ArgTypes>1392class __packaged_task_base<_Rp(_ArgTypes...)> {1393public:1394  _LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}1395  __packaged_task_base(const __packaged_task_base&)            = delete;1396  __packaged_task_base& operator=(const __packaged_task_base&) = delete;1397  _LIBCPP_HIDE_FROM_ABI_VIRTUAL1398  virtual ~__packaged_task_base() {}1399  virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;1400  virtual void destroy()                                  = 0;1401  virtual void destroy_deallocate()                       = 0;1402  virtual _Rp operator()(_ArgTypes&&...)                  = 0;1403};1404 1405template <class _FD, class _Alloc, class _FB>1406class __packaged_task_func;1407 1408template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>1409class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {1410  _LIBCPP_COMPRESSED_PAIR(_Fp, __func_, _Alloc, __alloc_);1411 1412public:1413  _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __func_(__f) {}1414  _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __func_(std::move(__f)) {}1415  _LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __func_(__f), __alloc_(__a) {}1416  _LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __func_(std::move(__f)), __alloc_(__a) {}1417  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;1418  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();1419  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();1420  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);1421};1422 1423template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>1424void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(1425    __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {1426  ::new ((void*)__p) __packaged_task_func(std::move(__func_), std::move(__alloc_));1427}1428 1429template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>1430void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {1431  __func_.~_Fp();1432  __alloc_.~_Alloc();1433}1434 1435template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>1436void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {1437  typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;1438  typedef allocator_traits<_Ap> _ATraits;1439  typedef pointer_traits<typename _ATraits::pointer> _PTraits;1440  _Ap __a(__alloc_);1441  __func_.~_Fp();1442  __alloc_.~_Alloc();1443  __a.deallocate(_PTraits::pointer_to(*this), 1);1444}1445 1446template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>1447_Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {1448  return std::__invoke(__func_, std::forward<_ArgTypes>(__arg)...);1449}1450 1451template <class _Callable>1452class __packaged_task_function;1453 1454template <class _Rp, class... _ArgTypes>1455class __packaged_task_function<_Rp(_ArgTypes...)> {1456  typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;1457 1458  _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }1459 1460  _LIBCPP_SUPPRESS_DEPRECATED_PUSH1461  typename aligned_storage<3 * sizeof(void*)>::type __buf_;1462  _LIBCPP_SUPPRESS_DEPRECATED_POP1463  __base* __f_;1464 1465public:1466  typedef _Rp result_type;1467 1468  // construct/copy/destroy:1469  _LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}1470  template <class _Fp>1471  _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);1472  template <class _Fp, class _Alloc>1473  _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);1474 1475  _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;1476  _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;1477 1478  __packaged_task_function(const __packaged_task_function&)            = delete;1479  __packaged_task_function& operator=(const __packaged_task_function&) = delete;1480 1481  _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();1482 1483  _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;1484 1485  _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;1486};1487 1488template <class _Rp, class... _ArgTypes>1489__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {1490  if (__f.__f_ == nullptr)1491    __f_ = nullptr;1492  else if (__f.__f_ == __f.__get_buf()) {1493    __f.__f_->__move_to(__get_buf());1494    __f_ = (__base*)&__buf_;1495  } else {1496    __f_     = __f.__f_;1497    __f.__f_ = nullptr;1498  }1499}1500 1501template <class _Rp, class... _ArgTypes>1502template <class _Fp>1503__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {1504  typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;1505  typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;1506  if (sizeof(_FF) <= sizeof(__buf_)) {1507    ::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));1508    __f_ = (__base*)&__buf_;1509  } else {1510    typedef allocator<_FF> _Ap;1511    _Ap __a;1512    typedef __allocator_destructor<_Ap> _Dp;1513    unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));1514    ::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));1515    __f_ = __hold.release();1516  }1517}1518 1519template <class _Rp, class... _ArgTypes>1520template <class _Fp, class _Alloc>1521__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)1522    : __f_(nullptr) {1523  typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;1524  typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;1525  if (sizeof(_FF) <= sizeof(__buf_)) {1526    __f_ = (__base*)&__buf_;1527    ::new ((void*)__f_) _FF(std::forward<_Fp>(__f));1528  } else {1529    typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;1530    _Ap __a(__a0);1531    typedef __allocator_destructor<_Ap> _Dp;1532    unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));1533    ::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));1534    __f_ = std::addressof(*__hold.release());1535  }1536}1537 1538template <class _Rp, class... _ArgTypes>1539__packaged_task_function<_Rp(_ArgTypes...)>&1540__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {1541  if (__f_ == __get_buf())1542    __f_->destroy();1543  else if (__f_)1544    __f_->destroy_deallocate();1545  __f_ = nullptr;1546  if (__f.__f_ == nullptr)1547    __f_ = nullptr;1548  else if (__f.__f_ == __f.__get_buf()) {1549    __f.__f_->__move_to(__get_buf());1550    __f_ = __get_buf();1551  } else {1552    __f_     = __f.__f_;1553    __f.__f_ = nullptr;1554  }1555  return *this;1556}1557 1558template <class _Rp, class... _ArgTypes>1559__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {1560  if (__f_ == __get_buf())1561    __f_->destroy();1562  else if (__f_)1563    __f_->destroy_deallocate();1564}1565 1566template <class _Rp, class... _ArgTypes>1567_LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {1568  if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {1569    _LIBCPP_SUPPRESS_DEPRECATED_PUSH1570    typename aligned_storage<sizeof(__buf_)>::type __tempbuf;1571    _LIBCPP_SUPPRESS_DEPRECATED_POP1572    __base* __t = (__base*)&__tempbuf;1573    __f_->__move_to(__t);1574    __f_->destroy();1575    __f_ = nullptr;1576    __f.__f_->__move_to((__base*)&__buf_);1577    __f.__f_->destroy();1578    __f.__f_ = nullptr;1579    __f_     = (__base*)&__buf_;1580    __t->__move_to((__base*)&__f.__buf_);1581    __t->destroy();1582    __f.__f_ = (__base*)&__f.__buf_;1583  } else if (__f_ == (__base*)&__buf_) {1584    __f_->__move_to((__base*)&__f.__buf_);1585    __f_->destroy();1586    __f_     = __f.__f_;1587    __f.__f_ = (__base*)&__f.__buf_;1588  } else if (__f.__f_ == (__base*)&__f.__buf_) {1589    __f.__f_->__move_to((__base*)&__buf_);1590    __f.__f_->destroy();1591    __f.__f_ = __f_;1592    __f_     = (__base*)&__buf_;1593  } else1594    std::swap(__f_, __f.__f_);1595}1596 1597template <class _Rp, class... _ArgTypes>1598inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {1599  return (*__f_)(std::forward<_ArgTypes>(__arg)...);1600}1601 1602template <class _Rp, class... _ArgTypes>1603class packaged_task<_Rp(_ArgTypes...)> {1604private:1605  __packaged_task_function<_Rp(_ArgTypes...)> __f_;1606  promise<_Rp> __p_;1607 1608public:1609  // construction and destruction1610  _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}1611 1612  template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>1613  _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}1614 1615#    if _LIBCPP_STD_VER <= 141616  template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>1617  _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)1618      : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}1619#    endif1620  // ~packaged_task() = default;1621 1622  // no copy1623  packaged_task(const packaged_task&)            = delete;1624  packaged_task& operator=(const packaged_task&) = delete;1625 1626  // move support1627  _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT1628      : __f_(std::move(__other.__f_)),1629        __p_(std::move(__other.__p_)) {}1630  _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {1631    __f_ = std::move(__other.__f_);1632    __p_ = std::move(__other.__p_);1633    return *this;1634  }1635  _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {1636    __f_.swap(__other.__f_);1637    __p_.swap(__other.__p_);1638  }1639 1640  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }1641 1642  // result retrieval1643  _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future() { return __p_.get_future(); }1644 1645  // execution1646  _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);1647  _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);1648 1649  _LIBCPP_HIDE_FROM_ABI void reset();1650};1651 1652template <class _Rp, class... _ArgTypes>1653void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {1654  if (__p_.__state_ == nullptr)1655    std::__throw_future_error(future_errc::no_state);1656  if (__p_.__state_->__has_value())1657    std::__throw_future_error(future_errc::promise_already_satisfied);1658#    if _LIBCPP_HAS_EXCEPTIONS1659  try {1660#    endif // _LIBCPP_HAS_EXCEPTIONS1661    __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));1662#    if _LIBCPP_HAS_EXCEPTIONS1663  } catch (...) {1664    __p_.set_exception(current_exception());1665  }1666#    endif // _LIBCPP_HAS_EXCEPTIONS1667}1668 1669template <class _Rp, class... _ArgTypes>1670void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {1671  if (__p_.__state_ == nullptr)1672    std::__throw_future_error(future_errc::no_state);1673  if (__p_.__state_->__has_value())1674    std::__throw_future_error(future_errc::promise_already_satisfied);1675#    if _LIBCPP_HAS_EXCEPTIONS1676  try {1677#    endif // _LIBCPP_HAS_EXCEPTIONS1678    __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));1679#    if _LIBCPP_HAS_EXCEPTIONS1680  } catch (...) {1681    __p_.set_exception_at_thread_exit(current_exception());1682  }1683#    endif // _LIBCPP_HAS_EXCEPTIONS1684}1685 1686template <class _Rp, class... _ArgTypes>1687void packaged_task<_Rp(_ArgTypes...)>::reset() {1688  if (!valid())1689    std::__throw_future_error(future_errc::no_state);1690  __p_ = promise<_Rp>();1691}1692 1693template <class... _ArgTypes>1694class packaged_task<void(_ArgTypes...)> {1695private:1696  __packaged_task_function<void(_ArgTypes...)> __f_;1697  promise<void> __p_;1698 1699public:1700  // construction and destruction1701  _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}1702  template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>1703  _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}1704#    if _LIBCPP_STD_VER <= 141705  template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>1706  _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)1707      : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}1708#    endif1709  // ~packaged_task() = default;1710 1711  // no copy1712  packaged_task(const packaged_task&)            = delete;1713  packaged_task& operator=(const packaged_task&) = delete;1714 1715  // move support1716  _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT1717      : __f_(std::move(__other.__f_)),1718        __p_(std::move(__other.__p_)) {}1719  _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {1720    __f_ = std::move(__other.__f_);1721    __p_ = std::move(__other.__p_);1722    return *this;1723  }1724  _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {1725    __f_.swap(__other.__f_);1726    __p_.swap(__other.__p_);1727  }1728 1729  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }1730 1731  // result retrieval1732  _LIBCPP_HIDE_FROM_ABI future<void> get_future() { return __p_.get_future(); }1733 1734  // execution1735  _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);1736  _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);1737 1738  _LIBCPP_HIDE_FROM_ABI void reset();1739};1740 1741#    if _LIBCPP_STD_VER >= 171742 1743template <class _Rp, class... _Args>1744packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;1745 1746template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>1747packaged_task(_Fp) -> packaged_task<_Stripped>;1748 1749#    endif1750 1751template <class... _ArgTypes>1752void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {1753  if (__p_.__state_ == nullptr)1754    std::__throw_future_error(future_errc::no_state);1755  if (__p_.__state_->__has_value())1756    std::__throw_future_error(future_errc::promise_already_satisfied);1757#    if _LIBCPP_HAS_EXCEPTIONS1758  try {1759#    endif // _LIBCPP_HAS_EXCEPTIONS1760    __f_(std::forward<_ArgTypes>(__args)...);1761    __p_.set_value();1762#    if _LIBCPP_HAS_EXCEPTIONS1763  } catch (...) {1764    __p_.set_exception(current_exception());1765  }1766#    endif // _LIBCPP_HAS_EXCEPTIONS1767}1768 1769template <class... _ArgTypes>1770void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {1771  if (__p_.__state_ == nullptr)1772    std::__throw_future_error(future_errc::no_state);1773  if (__p_.__state_->__has_value())1774    std::__throw_future_error(future_errc::promise_already_satisfied);1775#    if _LIBCPP_HAS_EXCEPTIONS1776  try {1777#    endif // _LIBCPP_HAS_EXCEPTIONS1778    __f_(std::forward<_ArgTypes>(__args)...);1779    __p_.set_value_at_thread_exit();1780#    if _LIBCPP_HAS_EXCEPTIONS1781  } catch (...) {1782    __p_.set_exception_at_thread_exit(current_exception());1783  }1784#    endif // _LIBCPP_HAS_EXCEPTIONS1785}1786 1787template <class... _ArgTypes>1788void packaged_task<void(_ArgTypes...)>::reset() {1789  if (!valid())1790    std::__throw_future_error(future_errc::no_state);1791  __p_ = promise<void>();1792}1793 1794template <class _Rp, class... _ArgTypes>1795inline _LIBCPP_HIDE_FROM_ABI void1796swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {1797  __x.swap(__y);1798}1799 1800#    if _LIBCPP_STD_VER <= 141801template <class _Callable, class _Alloc>1802struct uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};1803#    endif1804 1805template <class _Rp, class _Fp>1806_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {1807  unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(1808      new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));1809  return future<_Rp>(__h.get());1810}1811 1812template <class _Rp, class _Fp>1813_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {1814  unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(1815      new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));1816  auto __guard = std::__make_exception_guard([&] { __h->__make_ready(); });1817  std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();1818  __guard.__complete();1819  return future<_Rp>(__h.get());1820}1821 1822#    ifndef _LIBCPP_CXX03_LANG1823 1824template <class _Fp, class... _Args>1825class _LIBCPP_HIDDEN __async_func {1826  tuple<_Fp, _Args...> __f_;1827 1828public:1829  using _Rp _LIBCPP_NODEBUG = __invoke_result_t<_Fp, _Args...>;1830 1831  _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Fp&& __f, _Args&&... __args)1832      : __f_(std::move(__f), std::move(__args)...) {}1833 1834  _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}1835 1836  _LIBCPP_HIDE_FROM_ABI _Rp operator()() { return __execute(__make_index_sequence<sizeof...(_Args) + 1>()); }1837 1838private:1839  template <size_t... _Indices>1840  _LIBCPP_HIDE_FROM_ABI _Rp __execute(__index_sequence<_Indices...>) {1841    return std::__invoke(std::move(std::get<_Indices>(__f_))...);1842  }1843};1844 1845inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {1846  return (int(__policy) & int(__value)) != 0;1847}1848 1849template <class _Fp, class... _Args>1850[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<__invoke_result_t<__decay_t<_Fp>, __decay_t<_Args>...> >1851async(launch __policy, _Fp&& __f, _Args&&... __args) {1852  typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;1853  typedef typename _BF::_Rp _Rp;1854 1855#      if _LIBCPP_HAS_EXCEPTIONS1856  try {1857#      endif1858    if (__does_policy_contain(__policy, launch::async))1859      return std::__make_async_assoc_state<_Rp>(1860          _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));1861#      if _LIBCPP_HAS_EXCEPTIONS1862  } catch (...) {1863    if (__policy == launch::async)1864      throw;1865  }1866#      endif1867 1868  if (__does_policy_contain(__policy, launch::deferred))1869    return std::__make_deferred_assoc_state<_Rp>(1870        _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));1871  return future<_Rp>{};1872}1873 1874template <class _Fp, class... _Args>1875[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI future<__invoke_result_t<__decay_t<_Fp>, __decay_t<_Args>...> >1876async(_Fp&& __f, _Args&&... __args) {1877  return std::async(launch::any, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);1878}1879 1880#    endif // C++031881 1882// shared_future1883 1884template <class _Rp>1885class shared_future {1886  __assoc_state<_Rp>* __state_;1887 1888public:1889  _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}1890  _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {1891    if (__state_)1892      __state_->__add_shared();1893  }1894  _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }1895  _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {1896    __rhs.__state_ = nullptr;1897  }1898  _LIBCPP_HIDE_FROM_ABI ~shared_future();1899  _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;1900  _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {1901    shared_future(std::move(__rhs)).swap(*this);1902    return *this;1903  }1904 1905  // retrieving the value1906  _LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }1907 1908  _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }1909 1910  // functions to check state1911  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }1912 1913  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }1914  template <class _Rep, class _Period>1915  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {1916    return __state_->wait_for(__rel_time);1917  }1918  template <class _Clock, class _Duration>1919  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {1920    return __state_->wait_until(__abs_time);1921  }1922};1923 1924template <class _Rp>1925shared_future<_Rp>::~shared_future() {1926  if (__state_)1927    __state_->__release_shared();1928}1929 1930template <class _Rp>1931shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {1932  if (__rhs.__state_)1933    __rhs.__state_->__add_shared();1934  if (__state_)1935    __state_->__release_shared();1936  __state_ = __rhs.__state_;1937  return *this;1938}1939 1940template <class _Rp>1941class shared_future<_Rp&> {1942  __assoc_state<_Rp&>* __state_;1943 1944public:1945  _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}1946  _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {1947    if (__state_)1948      __state_->__add_shared();1949  }1950  _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }1951  _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {1952    __rhs.__state_ = nullptr;1953  }1954  _LIBCPP_HIDE_FROM_ABI ~shared_future();1955  _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);1956  _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {1957    shared_future(std::move(__rhs)).swap(*this);1958    return *this;1959  }1960 1961  // retrieving the value1962  _LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }1963 1964  _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }1965 1966  // functions to check state1967  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }1968 1969  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }1970  template <class _Rep, class _Period>1971  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {1972    return __state_->wait_for(__rel_time);1973  }1974  template <class _Clock, class _Duration>1975  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {1976    return __state_->wait_until(__abs_time);1977  }1978};1979 1980template <class _Rp>1981shared_future<_Rp&>::~shared_future() {1982  if (__state_)1983    __state_->__release_shared();1984}1985 1986template <class _Rp>1987shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {1988  if (__rhs.__state_)1989    __rhs.__state_->__add_shared();1990  if (__state_)1991    __state_->__release_shared();1992  __state_ = __rhs.__state_;1993  return *this;1994}1995 1996template <>1997class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {1998  __assoc_sub_state* __state_;1999 2000public:2001  _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}2002  _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {2003    if (__state_)2004      __state_->__add_shared();2005  }2006  _LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }2007  _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {2008    __rhs.__state_ = nullptr;2009  }2010  ~shared_future();2011  shared_future& operator=(const shared_future& __rhs);2012  _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {2013    shared_future(std::move(__rhs)).swap(*this);2014    return *this;2015  }2016 2017  // retrieving the value2018  _LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }2019 2020  _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }2021 2022  // functions to check state2023  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }2024 2025  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }2026  template <class _Rep, class _Period>2027  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {2028    return __state_->wait_for(__rel_time);2029  }2030  template <class _Clock, class _Duration>2031  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {2032    return __state_->wait_until(__abs_time);2033  }2034};2035 2036template <class _Rp>2037inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {2038  __x.swap(__y);2039}2040 2041template <class _Rp>2042inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {2043  return shared_future<_Rp>(std::move(*this));2044}2045 2046template <class _Rp>2047inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {2048  return shared_future<_Rp&>(std::move(*this));2049}2050 2051inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }2052 2053_LIBCPP_END_NAMESPACE_STD2054 2055_LIBCPP_POP_MACROS2056 2057#  endif // _LIBCPP_HAS_THREADS2058 2059#  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 172060#    include <chrono>2061#  endif2062 2063#  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 202064#    include <atomic>2065#    include <cstdlib>2066#    include <exception>2067#    include <iosfwd>2068#    include <system_error>2069#    include <thread>2070#  endif2071#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)2072 2073#endif // _LIBCPP_FUTURE2074