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