Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/c++/v1/__cxx03/future is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03_FUTURE
0011 #define _LIBCPP___CXX03_FUTURE
0012 
0013 /*
0014     future synopsis
0015 
0016 namespace std
0017 {
0018 
0019 enum class future_errc
0020 {
0021     future_already_retrieved = 1,
0022     promise_already_satisfied,
0023     no_state,
0024     broken_promise
0025 };
0026 
0027 enum class launch
0028 {
0029     async = 1,
0030     deferred = 2,
0031     any = async | deferred
0032 };
0033 
0034 enum class future_status
0035 {
0036     ready,
0037     timeout,
0038     deferred
0039 };
0040 
0041 template <> struct is_error_code_enum<future_errc> : public true_type { };
0042 error_code make_error_code(future_errc e) noexcept;
0043 error_condition make_error_condition(future_errc e) noexcept;
0044 
0045 const error_category& future_category() noexcept;
0046 
0047 class future_error : public logic_error {
0048 public:
0049     explicit future_error(future_errc e); // since C++17
0050 
0051     const error_code& code() const noexcept;
0052     const char*       what() const noexcept;
0053 
0054 private:
0055     error_code ec_;             // exposition only
0056 };
0057 
0058 template <class R>
0059 class promise
0060 {
0061 public:
0062     promise();
0063     template <class Allocator>
0064         promise(allocator_arg_t, const Allocator& a);
0065     promise(promise&& rhs) noexcept;
0066     promise(const promise& rhs) = delete;
0067     ~promise();
0068 
0069     // assignment
0070     promise& operator=(promise&& rhs) noexcept;
0071     promise& operator=(const promise& rhs) = delete;
0072     void swap(promise& other) noexcept;
0073 
0074     // retrieving the result
0075     future<R> get_future();
0076 
0077     // setting the result
0078     void set_value(const R& r);
0079     void set_value(R&& r);
0080     void set_exception(exception_ptr p);
0081 
0082     // setting the result with deferred notification
0083     void set_value_at_thread_exit(const R& r);
0084     void set_value_at_thread_exit(R&& r);
0085     void set_exception_at_thread_exit(exception_ptr p);
0086 };
0087 
0088 template <class R>
0089 class promise<R&>
0090 {
0091 public:
0092     promise();
0093     template <class Allocator>
0094         promise(allocator_arg_t, const Allocator& a);
0095     promise(promise&& rhs) noexcept;
0096     promise(const promise& rhs) = delete;
0097     ~promise();
0098 
0099     // assignment
0100     promise& operator=(promise&& rhs) noexcept;
0101     promise& operator=(const promise& rhs) = delete;
0102     void swap(promise& other) noexcept;
0103 
0104     // retrieving the result
0105     future<R&> get_future();
0106 
0107     // setting the result
0108     void set_value(R& r);
0109     void set_exception(exception_ptr p);
0110 
0111     // setting the result with deferred notification
0112     void set_value_at_thread_exit(R&);
0113     void set_exception_at_thread_exit(exception_ptr p);
0114 };
0115 
0116 template <>
0117 class promise<void>
0118 {
0119 public:
0120     promise();
0121     template <class Allocator>
0122         promise(allocator_arg_t, const Allocator& a);
0123     promise(promise&& rhs) noexcept;
0124     promise(const promise& rhs) = delete;
0125     ~promise();
0126 
0127     // assignment
0128     promise& operator=(promise&& rhs) noexcept;
0129     promise& operator=(const promise& rhs) = delete;
0130     void swap(promise& other) noexcept;
0131 
0132     // retrieving the result
0133     future<void> get_future();
0134 
0135     // setting the result
0136     void set_value();
0137     void set_exception(exception_ptr p);
0138 
0139     // setting the result with deferred notification
0140     void set_value_at_thread_exit();
0141     void set_exception_at_thread_exit(exception_ptr p);
0142 };
0143 
0144 template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
0145 
0146 template <class R, class Alloc>
0147     struct uses_allocator<promise<R>, Alloc> : public true_type {};
0148 
0149 template <class R>
0150 class future
0151 {
0152 public:
0153     future() noexcept;
0154     future(future&&) noexcept;
0155     future(const future& rhs) = delete;
0156     ~future();
0157     future& operator=(const future& rhs) = delete;
0158     future& operator=(future&&) noexcept;
0159     shared_future<R> share() noexcept;
0160 
0161     // retrieving the value
0162     R get();
0163 
0164     // functions to check state
0165     bool valid() const noexcept;
0166 
0167     void wait() const;
0168     template <class Rep, class Period>
0169         future_status
0170         wait_for(const chrono::duration<Rep, Period>& rel_time) const;
0171     template <class Clock, class Duration>
0172         future_status
0173         wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
0174 };
0175 
0176 template <class R>
0177 class future<R&>
0178 {
0179 public:
0180     future() noexcept;
0181     future(future&&) noexcept;
0182     future(const future& rhs) = delete;
0183     ~future();
0184     future& operator=(const future& rhs) = delete;
0185     future& operator=(future&&) noexcept;
0186     shared_future<R&> share() noexcept;
0187 
0188     // retrieving the value
0189     R& get();
0190 
0191     // functions to check state
0192     bool valid() const noexcept;
0193 
0194     void wait() const;
0195     template <class Rep, class Period>
0196         future_status
0197         wait_for(const chrono::duration<Rep, Period>& rel_time) const;
0198     template <class Clock, class Duration>
0199         future_status
0200         wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
0201 };
0202 
0203 template <>
0204 class future<void>
0205 {
0206 public:
0207     future() noexcept;
0208     future(future&&) noexcept;
0209     future(const future& rhs) = delete;
0210     ~future();
0211     future& operator=(const future& rhs) = delete;
0212     future& operator=(future&&) noexcept;
0213     shared_future<void> share() noexcept;
0214 
0215     // retrieving the value
0216     void get();
0217 
0218     // functions to check state
0219     bool valid() const noexcept;
0220 
0221     void wait() const;
0222     template <class Rep, class Period>
0223         future_status
0224         wait_for(const chrono::duration<Rep, Period>& rel_time) const;
0225     template <class Clock, class Duration>
0226         future_status
0227         wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
0228 };
0229 
0230 template <class R>
0231 class shared_future
0232 {
0233 public:
0234     shared_future() noexcept;
0235     shared_future(const shared_future& rhs);
0236     shared_future(future<R>&&) noexcept;
0237     shared_future(shared_future&& rhs) noexcept;
0238     ~shared_future();
0239     shared_future& operator=(const shared_future& rhs);
0240     shared_future& operator=(shared_future&& rhs) noexcept;
0241 
0242     // retrieving the value
0243     const R& get() const;
0244 
0245     // functions to check state
0246     bool valid() const noexcept;
0247 
0248     void wait() const;
0249     template <class Rep, class Period>
0250         future_status
0251         wait_for(const chrono::duration<Rep, Period>& rel_time) const;
0252     template <class Clock, class Duration>
0253         future_status
0254         wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
0255 };
0256 
0257 template <class R>
0258 class shared_future<R&>
0259 {
0260 public:
0261     shared_future() noexcept;
0262     shared_future(const shared_future& rhs);
0263     shared_future(future<R&>&&) noexcept;
0264     shared_future(shared_future&& rhs) noexcept;
0265     ~shared_future();
0266     shared_future& operator=(const shared_future& rhs);
0267     shared_future& operator=(shared_future&& rhs) noexcept;
0268 
0269     // retrieving the value
0270     R& get() const;
0271 
0272     // functions to check state
0273     bool valid() const noexcept;
0274 
0275     void wait() const;
0276     template <class Rep, class Period>
0277         future_status
0278         wait_for(const chrono::duration<Rep, Period>& rel_time) const;
0279     template <class Clock, class Duration>
0280         future_status
0281         wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
0282 };
0283 
0284 template <>
0285 class shared_future<void>
0286 {
0287 public:
0288     shared_future() noexcept;
0289     shared_future(const shared_future& rhs);
0290     shared_future(future<void>&&) noexcept;
0291     shared_future(shared_future&& rhs) noexcept;
0292     ~shared_future();
0293     shared_future& operator=(const shared_future& rhs);
0294     shared_future& operator=(shared_future&& rhs) noexcept;
0295 
0296     // retrieving the value
0297     void get() const;
0298 
0299     // functions to check state
0300     bool valid() const noexcept;
0301 
0302     void wait() const;
0303     template <class Rep, class Period>
0304         future_status
0305         wait_for(const chrono::duration<Rep, Period>& rel_time) const;
0306     template <class Clock, class Duration>
0307         future_status
0308         wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
0309 };
0310 
0311 template <class F, class... Args>
0312   future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
0313   async(F&& f, Args&&... args);
0314 
0315 template <class F, class... Args>
0316   future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
0317   async(launch policy, F&& f, Args&&... args);
0318 
0319 template <class> class packaged_task; // undefined
0320 
0321 template <class R, class... ArgTypes>
0322 class packaged_task<R(ArgTypes...)>
0323 {
0324 public:
0325     typedef R result_type; // extension
0326 
0327     // construction and destruction
0328     packaged_task() noexcept;
0329     template <class F>
0330         explicit packaged_task(F&& f);
0331     template <class F, class Allocator>
0332         packaged_task(allocator_arg_t, const Allocator& a, F&& f);
0333     ~packaged_task();
0334 
0335     // no copy
0336     packaged_task(const packaged_task&) = delete;
0337     packaged_task& operator=(const packaged_task&) = delete;
0338 
0339     // move support
0340     packaged_task(packaged_task&& other) noexcept;
0341     packaged_task& operator=(packaged_task&& other) noexcept;
0342     void swap(packaged_task& other) noexcept;
0343 
0344     bool valid() const noexcept;
0345 
0346     // result retrieval
0347     future<R> get_future();
0348 
0349     // execution
0350     void operator()(ArgTypes... );
0351     void make_ready_at_thread_exit(ArgTypes...);
0352 
0353     void reset();
0354 };
0355 
0356 template <class R>
0357   void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
0358 
0359 template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
0360 
0361 }  // std
0362 
0363 */
0364 
0365 #include <__cxx03/__config>
0366 
0367 #if !defined(_LIBCPP_HAS_NO_THREADS)
0368 
0369 #  include <__cxx03/__assert>
0370 #  include <__cxx03/__chrono/duration.h>
0371 #  include <__cxx03/__chrono/time_point.h>
0372 #  include <__cxx03/__exception/exception_ptr.h>
0373 #  include <__cxx03/__memory/addressof.h>
0374 #  include <__cxx03/__memory/allocator.h>
0375 #  include <__cxx03/__memory/allocator_arg_t.h>
0376 #  include <__cxx03/__memory/allocator_destructor.h>
0377 #  include <__cxx03/__memory/allocator_traits.h>
0378 #  include <__cxx03/__memory/compressed_pair.h>
0379 #  include <__cxx03/__memory/pointer_traits.h>
0380 #  include <__cxx03/__memory/shared_ptr.h>
0381 #  include <__cxx03/__memory/unique_ptr.h>
0382 #  include <__cxx03/__memory/uses_allocator.h>
0383 #  include <__cxx03/__system_error/error_category.h>
0384 #  include <__cxx03/__system_error/error_code.h>
0385 #  include <__cxx03/__system_error/error_condition.h>
0386 #  include <__cxx03/__type_traits/aligned_storage.h>
0387 #  include <__cxx03/__type_traits/strip_signature.h>
0388 #  include <__cxx03/__utility/auto_cast.h>
0389 #  include <__cxx03/__utility/forward.h>
0390 #  include <__cxx03/__utility/move.h>
0391 #  include <__cxx03/mutex>
0392 #  include <__cxx03/new>
0393 #  include <__cxx03/stdexcept>
0394 #  include <__cxx03/thread>
0395 #  include <__cxx03/version>
0396 
0397 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0398 #    pragma GCC system_header
0399 #  endif
0400 
0401 _LIBCPP_PUSH_MACROS
0402 #  include <__cxx03/__undef_macros>
0403 
0404 _LIBCPP_BEGIN_NAMESPACE_STD
0405 
0406 // enum class future_errc
0407 _LIBCPP_DECLARE_STRONG_ENUM(future_errc){
0408     future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};
0409 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
0410 
0411 template <>
0412 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
0413 
0414 #  ifdef _LIBCPP_CXX03_LANG
0415 template <>
0416 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type {};
0417 #  endif
0418 
0419 // enum class launch
0420 _LIBCPP_DECLARE_STRONG_ENUM(launch){async = 1, deferred = 2, any = async | deferred};
0421 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
0422 
0423 #  ifndef _LIBCPP_CXX03_LANG
0424 
0425 typedef underlying_type<launch>::type __launch_underlying_type;
0426 
0427 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator&(launch __x, launch __y) {
0428   return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));
0429 }
0430 
0431 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator|(launch __x, launch __y) {
0432   return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | static_cast<__launch_underlying_type>(__y));
0433 }
0434 
0435 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator^(launch __x, launch __y) {
0436   return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));
0437 }
0438 
0439 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator~(launch __x) {
0440   return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
0441 }
0442 
0443 inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {
0444   __x = __x & __y;
0445   return __x;
0446 }
0447 
0448 inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {
0449   __x = __x | __y;
0450   return __x;
0451 }
0452 
0453 inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {
0454   __x = __x ^ __y;
0455   return __x;
0456 }
0457 
0458 #  endif // !_LIBCPP_CXX03_LANG
0459 
0460 // enum class future_status
0461 _LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};
0462 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
0463 
0464 _LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
0465 
0466 inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {
0467   return error_code(static_cast<int>(__e), future_category());
0468 }
0469 
0470 inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {
0471   return error_condition(static_cast<int>(__e), future_category());
0472 }
0473 
0474 _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);
0475 
0476 class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {
0477   error_code __ec_;
0478 
0479   future_error(error_code);
0480   friend void __throw_future_error(future_errc);
0481   template <class>
0482   friend class promise;
0483 
0484 public:
0485 #  if _LIBCPP_STD_VER >= 17
0486   _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
0487 #  endif
0488 
0489   _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
0490 
0491   _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
0492   ~future_error() _NOEXCEPT override;
0493 };
0494 
0495 // Declared above std::future_error
0496 void __throw_future_error(future_errc __ev) {
0497 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0498   throw future_error(make_error_code(__ev));
0499 #  else
0500   (void)__ev;
0501   _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
0502 #  endif
0503 }
0504 
0505 class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {
0506 protected:
0507   exception_ptr __exception_;
0508   mutable mutex __mut_;
0509   mutable condition_variable __cv_;
0510   unsigned __state_;
0511 
0512   void __on_zero_shared() _NOEXCEPT override;
0513   void __sub_wait(unique_lock<mutex>& __lk);
0514 
0515 public:
0516   enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };
0517 
0518   _LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}
0519 
0520   _LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }
0521 
0522   _LIBCPP_HIDE_FROM_ABI void __attach_future() {
0523     lock_guard<mutex> __lk(__mut_);
0524     bool __has_future_attached = (__state_ & __future_attached) != 0;
0525     if (__has_future_attached)
0526       __throw_future_error(future_errc::future_already_retrieved);
0527     this->__add_shared();
0528     __state_ |= __future_attached;
0529   }
0530 
0531   _LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }
0532 
0533   void __make_ready();
0534   _LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }
0535 
0536   void set_value();
0537   void set_value_at_thread_exit();
0538 
0539   void set_exception(exception_ptr __p);
0540   void set_exception_at_thread_exit(exception_ptr __p);
0541 
0542   void copy();
0543 
0544   void wait();
0545   template <class _Rep, class _Period>
0546   future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
0547   template <class _Clock, class _Duration>
0548   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS future_status
0549   wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
0550 
0551   virtual void __execute();
0552 };
0553 
0554 template <class _Clock, class _Duration>
0555 future_status __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
0556   unique_lock<mutex> __lk(__mut_);
0557   if (__state_ & deferred)
0558     return future_status::deferred;
0559   while (!(__state_ & ready) && _Clock::now() < __abs_time)
0560     __cv_.wait_until(__lk, __abs_time);
0561   if (__state_ & ready)
0562     return future_status::ready;
0563   return future_status::timeout;
0564 }
0565 
0566 template <class _Rep, class _Period>
0567 inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
0568   return wait_until(chrono::steady_clock::now() + __rel_time);
0569 }
0570 
0571 template <class _Rp>
0572 class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {
0573   typedef __assoc_sub_state base;
0574   _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0575   typedef typename aligned_storage<sizeof(_Rp), _LIBCPP_ALIGNOF(_Rp)>::type _Up;
0576   _LIBCPP_SUPPRESS_DEPRECATED_POP
0577 
0578 protected:
0579   _Up __value_;
0580 
0581   _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
0582 
0583 public:
0584   template <class _Arg>
0585   _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
0586 
0587   template <class _Arg>
0588   _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
0589 
0590   _LIBCPP_HIDE_FROM_ABI _Rp move();
0591   _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<_Rp> copy();
0592 };
0593 
0594 template <class _Rp>
0595 void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {
0596   if (this->__state_ & base::__constructed)
0597     reinterpret_cast<_Rp*>(&__value_)->~_Rp();
0598   delete this;
0599 }
0600 
0601 template <class _Rp>
0602 template <class _Arg>
0603 void __assoc_state<_Rp>::set_value(_Arg&& __arg) {
0604   unique_lock<mutex> __lk(this->__mut_);
0605   if (this->__has_value())
0606     __throw_future_error(future_errc::promise_already_satisfied);
0607   ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
0608   this->__state_ |= base::__constructed | base::ready;
0609   __cv_.notify_all();
0610 }
0611 
0612 template <class _Rp>
0613 template <class _Arg>
0614 void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {
0615   unique_lock<mutex> __lk(this->__mut_);
0616   if (this->__has_value())
0617     __throw_future_error(future_errc::promise_already_satisfied);
0618   ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
0619   this->__state_ |= base::__constructed;
0620   __thread_local_data()->__make_ready_at_thread_exit(this);
0621 }
0622 
0623 template <class _Rp>
0624 _Rp __assoc_state<_Rp>::move() {
0625   unique_lock<mutex> __lk(this->__mut_);
0626   this->__sub_wait(__lk);
0627   if (this->__exception_ != nullptr)
0628     std::rethrow_exception(this->__exception_);
0629   return std::move(*reinterpret_cast<_Rp*>(&__value_));
0630 }
0631 
0632 template <class _Rp>
0633 __add_lvalue_reference_t<_Rp> __assoc_state<_Rp>::copy() {
0634   unique_lock<mutex> __lk(this->__mut_);
0635   this->__sub_wait(__lk);
0636   if (this->__exception_ != nullptr)
0637     std::rethrow_exception(this->__exception_);
0638   return *reinterpret_cast<_Rp*>(&__value_);
0639 }
0640 
0641 template <class _Rp>
0642 class __assoc_state<_Rp&> : public __assoc_sub_state {
0643   typedef __assoc_sub_state base;
0644   typedef _Rp* _Up;
0645 
0646 protected:
0647   _Up __value_;
0648 
0649   _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
0650 
0651 public:
0652   _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
0653   _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
0654 
0655   _LIBCPP_HIDE_FROM_ABI _Rp& copy();
0656 };
0657 
0658 template <class _Rp>
0659 void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {
0660   delete this;
0661 }
0662 
0663 template <class _Rp>
0664 void __assoc_state<_Rp&>::set_value(_Rp& __arg) {
0665   unique_lock<mutex> __lk(this->__mut_);
0666   if (this->__has_value())
0667     __throw_future_error(future_errc::promise_already_satisfied);
0668   __value_ = std::addressof(__arg);
0669   this->__state_ |= base::__constructed | base::ready;
0670   __cv_.notify_all();
0671 }
0672 
0673 template <class _Rp>
0674 void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {
0675   unique_lock<mutex> __lk(this->__mut_);
0676   if (this->__has_value())
0677     __throw_future_error(future_errc::promise_already_satisfied);
0678   __value_ = std::addressof(__arg);
0679   this->__state_ |= base::__constructed;
0680   __thread_local_data()->__make_ready_at_thread_exit(this);
0681 }
0682 
0683 template <class _Rp>
0684 _Rp& __assoc_state<_Rp&>::copy() {
0685   unique_lock<mutex> __lk(this->__mut_);
0686   this->__sub_wait(__lk);
0687   if (this->__exception_ != nullptr)
0688     std::rethrow_exception(this->__exception_);
0689   return *__value_;
0690 }
0691 
0692 template <class _Rp, class _Alloc>
0693 class __assoc_state_alloc : public __assoc_state<_Rp> {
0694   typedef __assoc_state<_Rp> base;
0695   _Alloc __alloc_;
0696 
0697   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
0698 
0699 public:
0700   _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
0701 };
0702 
0703 template <class _Rp, class _Alloc>
0704 void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {
0705   if (this->__state_ & base::__constructed)
0706     reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();
0707   typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
0708   typedef allocator_traits<_Al> _ATraits;
0709   typedef pointer_traits<typename _ATraits::pointer> _PTraits;
0710   _Al __a(__alloc_);
0711   this->~__assoc_state_alloc();
0712   __a.deallocate(_PTraits::pointer_to(*this), 1);
0713 }
0714 
0715 template <class _Rp, class _Alloc>
0716 class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {
0717   typedef __assoc_state<_Rp&> base;
0718   _Alloc __alloc_;
0719 
0720   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
0721 
0722 public:
0723   _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
0724 };
0725 
0726 template <class _Rp, class _Alloc>
0727 void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {
0728   typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
0729   typedef allocator_traits<_Al> _ATraits;
0730   typedef pointer_traits<typename _ATraits::pointer> _PTraits;
0731   _Al __a(__alloc_);
0732   this->~__assoc_state_alloc();
0733   __a.deallocate(_PTraits::pointer_to(*this), 1);
0734 }
0735 
0736 template <class _Alloc>
0737 class __assoc_sub_state_alloc : public __assoc_sub_state {
0738   typedef __assoc_sub_state base;
0739   _Alloc __alloc_;
0740 
0741   _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
0742 
0743 public:
0744   _LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
0745 };
0746 
0747 template <class _Alloc>
0748 void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {
0749   typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
0750   typedef allocator_traits<_Al> _ATraits;
0751   typedef pointer_traits<typename _ATraits::pointer> _PTraits;
0752   _Al __a(__alloc_);
0753   this->~__assoc_sub_state_alloc();
0754   __a.deallocate(_PTraits::pointer_to(*this), 1);
0755 }
0756 
0757 template <class _Rp, class _Fp>
0758 class __deferred_assoc_state : public __assoc_state<_Rp> {
0759   typedef __assoc_state<_Rp> base;
0760 
0761   _Fp __func_;
0762 
0763 public:
0764   _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
0765 
0766   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
0767 };
0768 
0769 template <class _Rp, class _Fp>
0770 inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
0771   this->__set_deferred();
0772 }
0773 
0774 template <class _Rp, class _Fp>
0775 void __deferred_assoc_state<_Rp, _Fp>::__execute() {
0776 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0777   try {
0778 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0779     this->set_value(__func_());
0780 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0781   } catch (...) {
0782     this->set_exception(current_exception());
0783   }
0784 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0785 }
0786 
0787 template <class _Fp>
0788 class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {
0789   typedef __assoc_sub_state base;
0790 
0791   _Fp __func_;
0792 
0793 public:
0794   _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
0795 
0796   _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
0797 };
0798 
0799 template <class _Fp>
0800 inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
0801   this->__set_deferred();
0802 }
0803 
0804 template <class _Fp>
0805 void __deferred_assoc_state<void, _Fp>::__execute() {
0806 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0807   try {
0808 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0809     __func_();
0810     this->set_value();
0811 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0812   } catch (...) {
0813     this->set_exception(current_exception());
0814   }
0815 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0816 }
0817 
0818 template <class _Rp, class _Fp>
0819 class __async_assoc_state : public __assoc_state<_Rp> {
0820   typedef __assoc_state<_Rp> base;
0821 
0822   _Fp __func_;
0823 
0824   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
0825 
0826 public:
0827   _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
0828 
0829   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
0830 };
0831 
0832 template <class _Rp, class _Fp>
0833 inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
0834 
0835 template <class _Rp, class _Fp>
0836 void __async_assoc_state<_Rp, _Fp>::__execute() {
0837 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0838   try {
0839 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0840     this->set_value(__func_());
0841 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0842   } catch (...) {
0843     this->set_exception(current_exception());
0844   }
0845 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0846 }
0847 
0848 template <class _Rp, class _Fp>
0849 void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {
0850   this->wait();
0851   base::__on_zero_shared();
0852 }
0853 
0854 template <class _Fp>
0855 class __async_assoc_state<void, _Fp> : public __assoc_sub_state {
0856   typedef __assoc_sub_state base;
0857 
0858   _Fp __func_;
0859 
0860   _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
0861 
0862 public:
0863   _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
0864 
0865   _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
0866 };
0867 
0868 template <class _Fp>
0869 inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
0870 
0871 template <class _Fp>
0872 void __async_assoc_state<void, _Fp>::__execute() {
0873 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0874   try {
0875 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0876     __func_();
0877     this->set_value();
0878 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0879   } catch (...) {
0880     this->set_exception(current_exception());
0881   }
0882 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0883 }
0884 
0885 template <class _Fp>
0886 void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {
0887   this->wait();
0888   base::__on_zero_shared();
0889 }
0890 
0891 template <class _Rp>
0892 class _LIBCPP_TEMPLATE_VIS promise;
0893 template <class _Rp>
0894 class _LIBCPP_TEMPLATE_VIS shared_future;
0895 
0896 // future
0897 
0898 template <class _Rp>
0899 class _LIBCPP_TEMPLATE_VIS future;
0900 
0901 template <class _Rp, class _Fp>
0902 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);
0903 
0904 template <class _Rp, class _Fp>
0905 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);
0906 
0907 template <class _Rp>
0908 class _LIBCPP_TEMPLATE_VIS future {
0909   __assoc_state<_Rp>* __state_;
0910 
0911   explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
0912 
0913   template <class>
0914   friend class promise;
0915   template <class>
0916   friend class shared_future;
0917 
0918   template <class _R1, class _Fp>
0919   friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
0920   template <class _R1, class _Fp>
0921   friend future<_R1> __make_async_assoc_state(_Fp&& __f);
0922 
0923 public:
0924   _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
0925   _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
0926   future(const future&)            = delete;
0927   future& operator=(const future&) = delete;
0928   _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
0929     future(std::move(__rhs)).swap(*this);
0930     return *this;
0931   }
0932 
0933   _LIBCPP_HIDE_FROM_ABI ~future();
0934   _LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;
0935 
0936   // retrieving the value
0937   _LIBCPP_HIDE_FROM_ABI _Rp get();
0938 
0939   _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
0940 
0941   // functions to check state
0942   _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
0943 
0944   _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
0945   template <class _Rep, class _Period>
0946   _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
0947     return __state_->wait_for(__rel_time);
0948   }
0949   template <class _Clock, class _Duration>
0950   _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
0951     return __state_->wait_until(__abs_time);
0952   }
0953 };
0954 
0955 template <class _Rp>
0956 future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {
0957   __state_->__attach_future();
0958 }
0959 
0960 struct __release_shared_count {
0961   _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }
0962 };
0963 
0964 template <class _Rp>
0965 future<_Rp>::~future() {
0966   if (__state_)
0967     __state_->__release_shared();
0968 }
0969 
0970 template <class _Rp>
0971 _Rp future<_Rp>::get() {
0972   unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
0973   __assoc_state<_Rp>* __s = __state_;
0974   __state_                = nullptr;
0975   return __s->move();
0976 }
0977 
0978 template <class _Rp>
0979 class _LIBCPP_TEMPLATE_VIS future<_Rp&> {
0980   __assoc_state<_Rp&>* __state_;
0981 
0982   explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
0983 
0984   template <class>
0985   friend class promise;
0986   template <class>
0987   friend class shared_future;
0988 
0989   template <class _R1, class _Fp>
0990   friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
0991   template <class _R1, class _Fp>
0992   friend future<_R1> __make_async_assoc_state(_Fp&& __f);
0993 
0994 public:
0995   _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
0996   _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
0997   future(const future&)            = delete;
0998   future& operator=(const future&) = delete;
0999   _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1000     future(std::move(__rhs)).swap(*this);
1001     return *this;
1002   }
1003 
1004   _LIBCPP_HIDE_FROM_ABI ~future();
1005   _LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;
1006 
1007   // retrieving the value
1008   _LIBCPP_HIDE_FROM_ABI _Rp& get();
1009 
1010   _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1011 
1012   // functions to check state
1013   _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1014 
1015   _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1016   template <class _Rep, class _Period>
1017   _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1018     return __state_->wait_for(__rel_time);
1019   }
1020   template <class _Clock, class _Duration>
1021   _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1022     return __state_->wait_until(__abs_time);
1023   }
1024 };
1025 
1026 template <class _Rp>
1027 future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {
1028   __state_->__attach_future();
1029 }
1030 
1031 template <class _Rp>
1032 future<_Rp&>::~future() {
1033   if (__state_)
1034     __state_->__release_shared();
1035 }
1036 
1037 template <class _Rp>
1038 _Rp& future<_Rp&>::get() {
1039   unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1040   __assoc_state<_Rp&>* __s = __state_;
1041   __state_                 = nullptr;
1042   return __s->copy();
1043 }
1044 
1045 template <>
1046 class _LIBCPP_EXPORTED_FROM_ABI future<void> {
1047   __assoc_sub_state* __state_;
1048 
1049   explicit future(__assoc_sub_state* __state);
1050 
1051   template <class>
1052   friend class promise;
1053   template <class>
1054   friend class shared_future;
1055 
1056   template <class _R1, class _Fp>
1057   friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1058   template <class _R1, class _Fp>
1059   friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1060 
1061 public:
1062   _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1063   _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1064   future(const future&)            = delete;
1065   future& operator=(const future&) = delete;
1066   _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1067     future(std::move(__rhs)).swap(*this);
1068     return *this;
1069   }
1070 
1071   ~future();
1072   _LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;
1073 
1074   // retrieving the value
1075   void get();
1076 
1077   _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1078 
1079   // functions to check state
1080   _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1081 
1082   _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1083   template <class _Rep, class _Period>
1084   _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1085     return __state_->wait_for(__rel_time);
1086   }
1087   template <class _Clock, class _Duration>
1088   _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1089     return __state_->wait_until(__abs_time);
1090   }
1091 };
1092 
1093 template <class _Rp>
1094 inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {
1095   __x.swap(__y);
1096 }
1097 
1098 // promise<R>
1099 
1100 template <class _Callable>
1101 class packaged_task;
1102 
1103 template <class _Rp>
1104 class _LIBCPP_TEMPLATE_VIS promise {
1105   __assoc_state<_Rp>* __state_;
1106 
1107   _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1108 
1109   template <class>
1110   friend class packaged_task;
1111 
1112 public:
1113   _LIBCPP_HIDE_FROM_ABI promise();
1114   template <class _Alloc>
1115   _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1116   _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1117   promise(const promise& __rhs) = delete;
1118   _LIBCPP_HIDE_FROM_ABI ~promise();
1119 
1120   // assignment
1121   _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1122     promise(std::move(__rhs)).swap(*this);
1123     return *this;
1124   }
1125   promise& operator=(const promise& __rhs) = delete;
1126 
1127   _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1128 
1129   // retrieving the result
1130   _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1131 
1132   // setting the result
1133   _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1134   _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1135   _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1136 
1137   // setting the result with deferred notification
1138   _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1139   _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1140   _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1141 };
1142 
1143 template <class _Rp>
1144 promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}
1145 
1146 template <class _Rp>
1147 template <class _Alloc>
1148 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {
1149   typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1150   typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1151   typedef __allocator_destructor<_A2> _D2;
1152   _A2 __a(__a0);
1153   unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1154   ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1155   __state_ = std::addressof(*__hold.release());
1156 }
1157 
1158 template <class _Rp>
1159 promise<_Rp>::~promise() {
1160   if (__state_) {
1161     if (!__state_->__has_value() && __state_->use_count() > 1)
1162       __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1163     __state_->__release_shared();
1164   }
1165 }
1166 
1167 template <class _Rp>
1168 future<_Rp> promise<_Rp>::get_future() {
1169   if (__state_ == nullptr)
1170     __throw_future_error(future_errc::no_state);
1171   return future<_Rp>(__state_);
1172 }
1173 
1174 template <class _Rp>
1175 void promise<_Rp>::set_value(const _Rp& __r) {
1176   if (__state_ == nullptr)
1177     __throw_future_error(future_errc::no_state);
1178   __state_->set_value(__r);
1179 }
1180 
1181 template <class _Rp>
1182 void promise<_Rp>::set_value(_Rp&& __r) {
1183   if (__state_ == nullptr)
1184     __throw_future_error(future_errc::no_state);
1185   __state_->set_value(std::move(__r));
1186 }
1187 
1188 template <class _Rp>
1189 void promise<_Rp>::set_exception(exception_ptr __p) {
1190   _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1191   if (__state_ == nullptr)
1192     __throw_future_error(future_errc::no_state);
1193   __state_->set_exception(__p);
1194 }
1195 
1196 template <class _Rp>
1197 void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {
1198   if (__state_ == nullptr)
1199     __throw_future_error(future_errc::no_state);
1200   __state_->set_value_at_thread_exit(__r);
1201 }
1202 
1203 template <class _Rp>
1204 void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {
1205   if (__state_ == nullptr)
1206     __throw_future_error(future_errc::no_state);
1207   __state_->set_value_at_thread_exit(std::move(__r));
1208 }
1209 
1210 template <class _Rp>
1211 void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {
1212   _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1213   if (__state_ == nullptr)
1214     __throw_future_error(future_errc::no_state);
1215   __state_->set_exception_at_thread_exit(__p);
1216 }
1217 
1218 // promise<R&>
1219 
1220 template <class _Rp>
1221 class _LIBCPP_TEMPLATE_VIS promise<_Rp&> {
1222   __assoc_state<_Rp&>* __state_;
1223 
1224   _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1225 
1226   template <class>
1227   friend class packaged_task;
1228 
1229 public:
1230   _LIBCPP_HIDE_FROM_ABI promise();
1231   template <class _Allocator>
1232   _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1233   _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1234   promise(const promise& __rhs) = delete;
1235   _LIBCPP_HIDE_FROM_ABI ~promise();
1236 
1237   // assignment
1238   _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1239     promise(std::move(__rhs)).swap(*this);
1240     return *this;
1241   }
1242   promise& operator=(const promise& __rhs) = delete;
1243 
1244   _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1245 
1246   // retrieving the result
1247   _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1248 
1249   // setting the result
1250   _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1251   _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1252 
1253   // setting the result with deferred notification
1254   _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1255   _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1256 };
1257 
1258 template <class _Rp>
1259 promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}
1260 
1261 template <class _Rp>
1262 template <class _Alloc>
1263 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {
1264   typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1265   typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1266   typedef __allocator_destructor<_A2> _D2;
1267   _A2 __a(__a0);
1268   unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1269   ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1270   __state_ = std::addressof(*__hold.release());
1271 }
1272 
1273 template <class _Rp>
1274 promise<_Rp&>::~promise() {
1275   if (__state_) {
1276     if (!__state_->__has_value() && __state_->use_count() > 1)
1277       __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1278     __state_->__release_shared();
1279   }
1280 }
1281 
1282 template <class _Rp>
1283 future<_Rp&> promise<_Rp&>::get_future() {
1284   if (__state_ == nullptr)
1285     __throw_future_error(future_errc::no_state);
1286   return future<_Rp&>(__state_);
1287 }
1288 
1289 template <class _Rp>
1290 void promise<_Rp&>::set_value(_Rp& __r) {
1291   if (__state_ == nullptr)
1292     __throw_future_error(future_errc::no_state);
1293   __state_->set_value(__r);
1294 }
1295 
1296 template <class _Rp>
1297 void promise<_Rp&>::set_exception(exception_ptr __p) {
1298   _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1299   if (__state_ == nullptr)
1300     __throw_future_error(future_errc::no_state);
1301   __state_->set_exception(__p);
1302 }
1303 
1304 template <class _Rp>
1305 void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {
1306   if (__state_ == nullptr)
1307     __throw_future_error(future_errc::no_state);
1308   __state_->set_value_at_thread_exit(__r);
1309 }
1310 
1311 template <class _Rp>
1312 void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {
1313   _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1314   if (__state_ == nullptr)
1315     __throw_future_error(future_errc::no_state);
1316   __state_->set_exception_at_thread_exit(__p);
1317 }
1318 
1319 // promise<void>
1320 
1321 template <>
1322 class _LIBCPP_EXPORTED_FROM_ABI promise<void> {
1323   __assoc_sub_state* __state_;
1324 
1325   _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1326 
1327   template <class>
1328   friend class packaged_task;
1329 
1330 public:
1331   promise();
1332   template <class _Allocator>
1333   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS promise(allocator_arg_t, const _Allocator& __a);
1334   _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1335   promise(const promise& __rhs) = delete;
1336   ~promise();
1337 
1338   // assignment
1339   _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1340     promise(std::move(__rhs)).swap(*this);
1341     return *this;
1342   }
1343   promise& operator=(const promise& __rhs) = delete;
1344 
1345   _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1346 
1347   // retrieving the result
1348   future<void> get_future();
1349 
1350   // setting the result
1351   void set_value();
1352   void set_exception(exception_ptr __p);
1353 
1354   // setting the result with deferred notification
1355   void set_value_at_thread_exit();
1356   void set_exception_at_thread_exit(exception_ptr __p);
1357 };
1358 
1359 template <class _Alloc>
1360 promise<void>::promise(allocator_arg_t, const _Alloc& __a0) {
1361   typedef __assoc_sub_state_alloc<_Alloc> _State;
1362   typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1363   typedef __allocator_destructor<_A2> _D2;
1364   _A2 __a(__a0);
1365   unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1366   ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1367   __state_ = std::addressof(*__hold.release());
1368 }
1369 
1370 template <class _Rp>
1371 inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {
1372   __x.swap(__y);
1373 }
1374 
1375 template <class _Rp, class _Alloc>
1376 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc> : public true_type {};
1377 
1378 // packaged_task
1379 
1380 template <class _Fp>
1381 class __packaged_task_base;
1382 
1383 template <class _Rp, class... _ArgTypes>
1384 class __packaged_task_base<_Rp(_ArgTypes...)> {
1385 public:
1386   _LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}
1387   __packaged_task_base(const __packaged_task_base&)            = delete;
1388   __packaged_task_base& operator=(const __packaged_task_base&) = delete;
1389   _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1390   virtual ~__packaged_task_base() {}
1391   virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1392   virtual void destroy()                                  = 0;
1393   virtual void destroy_deallocate()                       = 0;
1394   virtual _Rp operator()(_ArgTypes&&...)                  = 0;
1395 };
1396 
1397 template <class _FD, class _Alloc, class _FB>
1398 class __packaged_task_func;
1399 
1400 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1401 class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
1402   __compressed_pair<_Fp, _Alloc> __f_;
1403 
1404 public:
1405   _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
1406   _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __f_(std::move(__f), __default_init_tag()) {}
1407   _LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __f_(__f, __a) {}
1408   _LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __f_(std::move(__f), __a) {}
1409   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1410   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1411   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1412   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);
1413 };
1414 
1415 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1416 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1417     __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {
1418   ::new ((void*)__p) __packaged_task_func(std::move(__f_.first()), std::move(__f_.second()));
1419 }
1420 
1421 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1422 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
1423   __f_.~__compressed_pair<_Fp, _Alloc>();
1424 }
1425 
1426 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1427 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {
1428   typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1429   typedef allocator_traits<_Ap> _ATraits;
1430   typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1431   _Ap __a(__f_.second());
1432   __f_.~__compressed_pair<_Fp, _Alloc>();
1433   __a.deallocate(_PTraits::pointer_to(*this), 1);
1434 }
1435 
1436 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1437 _Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
1438   return std::__invoke(__f_.first(), std::forward<_ArgTypes>(__arg)...);
1439 }
1440 
1441 template <class _Callable>
1442 class __packaged_task_function;
1443 
1444 template <class _Rp, class... _ArgTypes>
1445 class __packaged_task_function<_Rp(_ArgTypes...)> {
1446   typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1447 
1448   _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }
1449 
1450   _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1451   typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1452   _LIBCPP_SUPPRESS_DEPRECATED_POP
1453   __base* __f_;
1454 
1455 public:
1456   typedef _Rp result_type;
1457 
1458   // construct/copy/destroy:
1459   _LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1460   template <class _Fp>
1461   _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1462   template <class _Fp, class _Alloc>
1463   _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1464 
1465   _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1466   _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1467 
1468   __packaged_task_function(const __packaged_task_function&)            = delete;
1469   __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1470 
1471   _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1472 
1473   _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1474 
1475   _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1476 };
1477 
1478 template <class _Rp, class... _ArgTypes>
1479 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {
1480   if (__f.__f_ == nullptr)
1481     __f_ = nullptr;
1482   else if (__f.__f_ == __f.__get_buf()) {
1483     __f.__f_->__move_to(__get_buf());
1484     __f_ = (__base*)&__buf_;
1485   } else {
1486     __f_     = __f.__f_;
1487     __f.__f_ = nullptr;
1488   }
1489 }
1490 
1491 template <class _Rp, class... _ArgTypes>
1492 template <class _Fp>
1493 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {
1494   typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1495   typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1496   if (sizeof(_FF) <= sizeof(__buf_)) {
1497     ::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));
1498     __f_ = (__base*)&__buf_;
1499   } else {
1500     typedef allocator<_FF> _Ap;
1501     _Ap __a;
1502     typedef __allocator_destructor<_Ap> _Dp;
1503     unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1504     ::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));
1505     __f_ = __hold.release();
1506   }
1507 }
1508 
1509 template <class _Rp, class... _ArgTypes>
1510 template <class _Fp, class _Alloc>
1511 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1512     : __f_(nullptr) {
1513   typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1514   typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1515   if (sizeof(_FF) <= sizeof(__buf_)) {
1516     __f_ = (__base*)&__buf_;
1517     ::new ((void*)__f_) _FF(std::forward<_Fp>(__f));
1518   } else {
1519     typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1520     _Ap __a(__a0);
1521     typedef __allocator_destructor<_Ap> _Dp;
1522     unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1523     ::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));
1524     __f_ = std::addressof(*__hold.release());
1525   }
1526 }
1527 
1528 template <class _Rp, class... _ArgTypes>
1529 __packaged_task_function<_Rp(_ArgTypes...)>&
1530 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {
1531   if (__f_ == __get_buf())
1532     __f_->destroy();
1533   else if (__f_)
1534     __f_->destroy_deallocate();
1535   __f_ = nullptr;
1536   if (__f.__f_ == nullptr)
1537     __f_ = nullptr;
1538   else if (__f.__f_ == __f.__get_buf()) {
1539     __f.__f_->__move_to(__get_buf());
1540     __f_ = __get_buf();
1541   } else {
1542     __f_     = __f.__f_;
1543     __f.__f_ = nullptr;
1544   }
1545   return *this;
1546 }
1547 
1548 template <class _Rp, class... _ArgTypes>
1549 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {
1550   if (__f_ == __get_buf())
1551     __f_->destroy();
1552   else if (__f_)
1553     __f_->destroy_deallocate();
1554 }
1555 
1556 template <class _Rp, class... _ArgTypes>
1557 _LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {
1558   if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {
1559     _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1560     typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1561     _LIBCPP_SUPPRESS_DEPRECATED_POP
1562     __base* __t = (__base*)&__tempbuf;
1563     __f_->__move_to(__t);
1564     __f_->destroy();
1565     __f_ = nullptr;
1566     __f.__f_->__move_to((__base*)&__buf_);
1567     __f.__f_->destroy();
1568     __f.__f_ = nullptr;
1569     __f_     = (__base*)&__buf_;
1570     __t->__move_to((__base*)&__f.__buf_);
1571     __t->destroy();
1572     __f.__f_ = (__base*)&__f.__buf_;
1573   } else if (__f_ == (__base*)&__buf_) {
1574     __f_->__move_to((__base*)&__f.__buf_);
1575     __f_->destroy();
1576     __f_     = __f.__f_;
1577     __f.__f_ = (__base*)&__f.__buf_;
1578   } else if (__f.__f_ == (__base*)&__f.__buf_) {
1579     __f.__f_->__move_to((__base*)&__buf_);
1580     __f.__f_->destroy();
1581     __f.__f_ = __f_;
1582     __f_     = (__base*)&__buf_;
1583   } else
1584     std::swap(__f_, __f.__f_);
1585 }
1586 
1587 template <class _Rp, class... _ArgTypes>
1588 inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
1589   return (*__f_)(std::forward<_ArgTypes>(__arg)...);
1590 }
1591 
1592 template <class _Rp, class... _ArgTypes>
1593 class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)> {
1594 public:
1595   typedef _Rp result_type; // extension
1596 
1597 private:
1598   __packaged_task_function<result_type(_ArgTypes...)> __f_;
1599   promise<result_type> __p_;
1600 
1601 public:
1602   // construction and destruction
1603   _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1604 
1605   template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1606   _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1607 
1608   template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1609   _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1610       : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1611   // ~packaged_task() = default;
1612 
1613   // no copy
1614   packaged_task(const packaged_task&)            = delete;
1615   packaged_task& operator=(const packaged_task&) = delete;
1616 
1617   // move support
1618   _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1619       : __f_(std::move(__other.__f_)),
1620         __p_(std::move(__other.__p_)) {}
1621   _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1622     __f_ = std::move(__other.__f_);
1623     __p_ = std::move(__other.__p_);
1624     return *this;
1625   }
1626   _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1627     __f_.swap(__other.__f_);
1628     __p_.swap(__other.__p_);
1629   }
1630 
1631   _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1632 
1633   // result retrieval
1634   _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1635 
1636   // execution
1637   _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1638   _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1639 
1640   _LIBCPP_HIDE_FROM_ABI void reset();
1641 };
1642 
1643 template <class _Rp, class... _ArgTypes>
1644 void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1645   if (__p_.__state_ == nullptr)
1646     __throw_future_error(future_errc::no_state);
1647   if (__p_.__state_->__has_value())
1648     __throw_future_error(future_errc::promise_already_satisfied);
1649 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1650   try {
1651 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
1652     __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
1653 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1654   } catch (...) {
1655     __p_.set_exception(current_exception());
1656   }
1657 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
1658 }
1659 
1660 template <class _Rp, class... _ArgTypes>
1661 void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1662   if (__p_.__state_ == nullptr)
1663     __throw_future_error(future_errc::no_state);
1664   if (__p_.__state_->__has_value())
1665     __throw_future_error(future_errc::promise_already_satisfied);
1666 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1667   try {
1668 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
1669     __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
1670 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1671   } catch (...) {
1672     __p_.set_exception_at_thread_exit(current_exception());
1673   }
1674 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
1675 }
1676 
1677 template <class _Rp, class... _ArgTypes>
1678 void packaged_task<_Rp(_ArgTypes...)>::reset() {
1679   if (!valid())
1680     __throw_future_error(future_errc::no_state);
1681   __p_ = promise<result_type>();
1682 }
1683 
1684 template <class... _ArgTypes>
1685 class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)> {
1686 public:
1687   typedef void result_type; // extension
1688 
1689 private:
1690   __packaged_task_function<result_type(_ArgTypes...)> __f_;
1691   promise<result_type> __p_;
1692 
1693 public:
1694   // construction and destruction
1695   _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1696   template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1697   _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1698   template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1699   _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1700       : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1701   // ~packaged_task() = default;
1702 
1703   // no copy
1704   packaged_task(const packaged_task&)            = delete;
1705   packaged_task& operator=(const packaged_task&) = delete;
1706 
1707   // move support
1708   _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1709       : __f_(std::move(__other.__f_)),
1710         __p_(std::move(__other.__p_)) {}
1711   _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1712     __f_ = std::move(__other.__f_);
1713     __p_ = std::move(__other.__p_);
1714     return *this;
1715   }
1716   _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1717     __f_.swap(__other.__f_);
1718     __p_.swap(__other.__p_);
1719   }
1720 
1721   _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1722 
1723   // result retrieval
1724   _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1725 
1726   // execution
1727   _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1728   _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1729 
1730   _LIBCPP_HIDE_FROM_ABI void reset();
1731 };
1732 
1733 #  if _LIBCPP_STD_VER >= 17
1734 
1735 template <class _Rp, class... _Args>
1736 packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
1737 
1738 template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1739 packaged_task(_Fp) -> packaged_task<_Stripped>;
1740 
1741 #  endif
1742 
1743 template <class... _ArgTypes>
1744 void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1745   if (__p_.__state_ == nullptr)
1746     __throw_future_error(future_errc::no_state);
1747   if (__p_.__state_->__has_value())
1748     __throw_future_error(future_errc::promise_already_satisfied);
1749 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1750   try {
1751 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
1752     __f_(std::forward<_ArgTypes>(__args)...);
1753     __p_.set_value();
1754 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1755   } catch (...) {
1756     __p_.set_exception(current_exception());
1757   }
1758 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
1759 }
1760 
1761 template <class... _ArgTypes>
1762 void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1763   if (__p_.__state_ == nullptr)
1764     __throw_future_error(future_errc::no_state);
1765   if (__p_.__state_->__has_value())
1766     __throw_future_error(future_errc::promise_already_satisfied);
1767 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1768   try {
1769 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
1770     __f_(std::forward<_ArgTypes>(__args)...);
1771     __p_.set_value_at_thread_exit();
1772 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1773   } catch (...) {
1774     __p_.set_exception_at_thread_exit(current_exception());
1775   }
1776 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
1777 }
1778 
1779 template <class... _ArgTypes>
1780 void packaged_task<void(_ArgTypes...)>::reset() {
1781   if (!valid())
1782     __throw_future_error(future_errc::no_state);
1783   __p_ = promise<result_type>();
1784 }
1785 
1786 template <class _Rp, class... _ArgTypes>
1787 inline _LIBCPP_HIDE_FROM_ABI void
1788 swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1789   __x.swap(__y);
1790 }
1791 
1792 template <class _Callable, class _Alloc>
1793 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
1794 
1795 template <class _Rp, class _Fp>
1796 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
1797   unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1798       new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1799   return future<_Rp>(__h.get());
1800 }
1801 
1802 template <class _Rp, class _Fp>
1803 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
1804   unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1805       new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1806   std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
1807   return future<_Rp>(__h.get());
1808 }
1809 
1810 #  ifndef _LIBCPP_CXX03_LANG
1811 
1812 template <class _Fp, class... _Args>
1813 class _LIBCPP_HIDDEN __async_func {
1814   tuple<_Fp, _Args...> __f_;
1815 
1816 public:
1817   typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
1818 
1819   _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Fp&& __f, _Args&&... __args)
1820       : __f_(std::move(__f), std::move(__args)...) {}
1821 
1822   _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
1823 
1824   _LIBCPP_HIDE_FROM_ABI _Rp operator()() {
1825     typedef typename __make_tuple_indices<1 + sizeof...(_Args), 1>::type _Index;
1826     return __execute(_Index());
1827   }
1828 
1829 private:
1830   template <size_t... _Indices>
1831   _LIBCPP_HIDE_FROM_ABI _Rp __execute(__tuple_indices<_Indices...>) {
1832     return std::__invoke(std::move(std::get<0>(__f_)), std::move(std::get<_Indices>(__f_))...);
1833   }
1834 };
1835 
1836 inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
1837   return (int(__policy) & int(__value)) != 0;
1838 }
1839 
1840 template <class _Fp, class... _Args>
1841 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1842 async(launch __policy, _Fp&& __f, _Args&&... __args) {
1843   typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
1844   typedef typename _BF::_Rp _Rp;
1845 
1846 #    ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1847   try {
1848 #    endif
1849     if (__does_policy_contain(__policy, launch::async))
1850       return std::__make_async_assoc_state<_Rp>(
1851           _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1852 #    ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1853   } catch (...) {
1854     if (__policy == launch::async)
1855       throw;
1856   }
1857 #    endif
1858 
1859   if (__does_policy_contain(__policy, launch::deferred))
1860     return std::__make_deferred_assoc_state<_Rp>(
1861         _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1862   return future<_Rp>{};
1863 }
1864 
1865 template <class _Fp, class... _Args>
1866 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1867 async(_Fp&& __f, _Args&&... __args) {
1868   return std::async(launch::any, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);
1869 }
1870 
1871 #  endif // C++03
1872 
1873 // shared_future
1874 
1875 template <class _Rp>
1876 class _LIBCPP_TEMPLATE_VIS shared_future {
1877   __assoc_state<_Rp>* __state_;
1878 
1879 public:
1880   _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1881   _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1882     if (__state_)
1883       __state_->__add_shared();
1884   }
1885   _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1886   _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1887     __rhs.__state_ = nullptr;
1888   }
1889   _LIBCPP_HIDE_FROM_ABI ~shared_future();
1890   _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
1891   _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1892     shared_future(std::move(__rhs)).swap(*this);
1893     return *this;
1894   }
1895 
1896   // retrieving the value
1897   _LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }
1898 
1899   _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1900 
1901   // functions to check state
1902   _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1903 
1904   _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1905   template <class _Rep, class _Period>
1906   _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1907     return __state_->wait_for(__rel_time);
1908   }
1909   template <class _Clock, class _Duration>
1910   _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1911     return __state_->wait_until(__abs_time);
1912   }
1913 };
1914 
1915 template <class _Rp>
1916 shared_future<_Rp>::~shared_future() {
1917   if (__state_)
1918     __state_->__release_shared();
1919 }
1920 
1921 template <class _Rp>
1922 shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {
1923   if (__rhs.__state_)
1924     __rhs.__state_->__add_shared();
1925   if (__state_)
1926     __state_->__release_shared();
1927   __state_ = __rhs.__state_;
1928   return *this;
1929 }
1930 
1931 template <class _Rp>
1932 class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&> {
1933   __assoc_state<_Rp&>* __state_;
1934 
1935 public:
1936   _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1937   _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
1938     if (__state_)
1939       __state_->__add_shared();
1940   }
1941   _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1942   _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1943     __rhs.__state_ = nullptr;
1944   }
1945   _LIBCPP_HIDE_FROM_ABI ~shared_future();
1946   _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
1947   _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1948     shared_future(std::move(__rhs)).swap(*this);
1949     return *this;
1950   }
1951 
1952   // retrieving the value
1953   _LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }
1954 
1955   _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1956 
1957   // functions to check state
1958   _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1959 
1960   _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1961   template <class _Rep, class _Period>
1962   _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1963     return __state_->wait_for(__rel_time);
1964   }
1965   template <class _Clock, class _Duration>
1966   _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1967     return __state_->wait_until(__abs_time);
1968   }
1969 };
1970 
1971 template <class _Rp>
1972 shared_future<_Rp&>::~shared_future() {
1973   if (__state_)
1974     __state_->__release_shared();
1975 }
1976 
1977 template <class _Rp>
1978 shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {
1979   if (__rhs.__state_)
1980     __rhs.__state_->__add_shared();
1981   if (__state_)
1982     __state_->__release_shared();
1983   __state_ = __rhs.__state_;
1984   return *this;
1985 }
1986 
1987 template <>
1988 class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
1989   __assoc_sub_state* __state_;
1990 
1991 public:
1992   _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1993   _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
1994     if (__state_)
1995       __state_->__add_shared();
1996   }
1997   _LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1998   _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1999     __rhs.__state_ = nullptr;
2000   }
2001   ~shared_future();
2002   shared_future& operator=(const shared_future& __rhs);
2003   _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
2004     shared_future(std::move(__rhs)).swap(*this);
2005     return *this;
2006   }
2007 
2008   // retrieving the value
2009   _LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }
2010 
2011   _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
2012 
2013   // functions to check state
2014   _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
2015 
2016   _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
2017   template <class _Rep, class _Period>
2018   _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
2019     return __state_->wait_for(__rel_time);
2020   }
2021   template <class _Clock, class _Duration>
2022   _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
2023     return __state_->wait_until(__abs_time);
2024   }
2025 };
2026 
2027 template <class _Rp>
2028 inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
2029   __x.swap(__y);
2030 }
2031 
2032 template <class _Rp>
2033 inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
2034   return shared_future<_Rp>(std::move(*this));
2035 }
2036 
2037 template <class _Rp>
2038 inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
2039   return shared_future<_Rp&>(std::move(*this));
2040 }
2041 
2042 inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
2043 
2044 _LIBCPP_END_NAMESPACE_STD
2045 
2046 _LIBCPP_POP_MACROS
2047 
2048 #endif // !defined(_LIBCPP_HAS_NO_THREADS)
2049 
2050 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2051 #  include <__cxx03/chrono>
2052 #endif
2053 
2054 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2055 #  include <__cxx03/atomic>
2056 #  include <__cxx03/cstdlib>
2057 #  include <__cxx03/exception>
2058 #  include <__cxx03/iosfwd>
2059 #  include <__cxx03/system_error>
2060 #endif
2061 
2062 #endif // _LIBCPP___CXX03_FUTURE