Back to home page

EIC code displayed by LXR

 
 

    


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