xref: /freebsd/contrib/llvm-project/libcxx/include/future (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_FUTURE
11#define _LIBCPP_FUTURE
12
13/*
14    future synopsis
15
16namespace std
17{
18
19enum class future_errc
20{
21    future_already_retrieved = 1,
22    promise_already_satisfied,
23    no_state,
24    broken_promise
25};
26
27enum class launch
28{
29    async = 1,
30    deferred = 2,
31    any = async | deferred
32};
33
34enum class future_status
35{
36    ready,
37    timeout,
38    deferred
39};
40
41template <> struct is_error_code_enum<future_errc> : public true_type { };
42error_code make_error_code(future_errc e) noexcept;
43error_condition make_error_condition(future_errc e) noexcept;
44
45const error_category& future_category() noexcept;
46
47class future_error : public logic_error {
48public:
49    explicit future_error(future_errc e); // since C++17
50
51    const error_code& code() const noexcept;
52    const char*       what() const noexcept;
53
54private:
55    error_code ec_;             // exposition only
56};
57
58template <class R>
59class promise
60{
61public:
62    promise();
63    template <class Allocator>
64        promise(allocator_arg_t, const Allocator& a);
65    promise(promise&& rhs) noexcept;
66    promise(const promise& rhs) = delete;
67    ~promise();
68
69    // assignment
70    promise& operator=(promise&& rhs) noexcept;
71    promise& operator=(const promise& rhs) = delete;
72    void swap(promise& other) noexcept;
73
74    // retrieving the result
75    future<R> get_future();
76
77    // setting the result
78    void set_value(const R& r);
79    void set_value(R&& r);
80    void set_exception(exception_ptr p);
81
82    // setting the result with deferred notification
83    void set_value_at_thread_exit(const R& r);
84    void set_value_at_thread_exit(R&& r);
85    void set_exception_at_thread_exit(exception_ptr p);
86};
87
88template <class R>
89class promise<R&>
90{
91public:
92    promise();
93    template <class Allocator>
94        promise(allocator_arg_t, const Allocator& a);
95    promise(promise&& rhs) noexcept;
96    promise(const promise& rhs) = delete;
97    ~promise();
98
99    // assignment
100    promise& operator=(promise&& rhs) noexcept;
101    promise& operator=(const promise& rhs) = delete;
102    void swap(promise& other) noexcept;
103
104    // retrieving the result
105    future<R&> get_future();
106
107    // setting the result
108    void set_value(R& r);
109    void set_exception(exception_ptr p);
110
111    // setting the result with deferred notification
112    void set_value_at_thread_exit(R&);
113    void set_exception_at_thread_exit(exception_ptr p);
114};
115
116template <>
117class promise<void>
118{
119public:
120    promise();
121    template <class Allocator>
122        promise(allocator_arg_t, const Allocator& a);
123    promise(promise&& rhs) noexcept;
124    promise(const promise& rhs) = delete;
125    ~promise();
126
127    // assignment
128    promise& operator=(promise&& rhs) noexcept;
129    promise& operator=(const promise& rhs) = delete;
130    void swap(promise& other) noexcept;
131
132    // retrieving the result
133    future<void> get_future();
134
135    // setting the result
136    void set_value();
137    void set_exception(exception_ptr p);
138
139    // setting the result with deferred notification
140    void set_value_at_thread_exit();
141    void set_exception_at_thread_exit(exception_ptr p);
142};
143
144template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
145
146template <class R, class Alloc>
147    struct uses_allocator<promise<R>, Alloc> : public true_type {};
148
149template <class R>
150class future
151{
152public:
153    future() noexcept;
154    future(future&&) noexcept;
155    future(const future& rhs) = delete;
156    ~future();
157    future& operator=(const future& rhs) = delete;
158    future& operator=(future&&) noexcept;
159    shared_future<R> share() noexcept;
160
161    // retrieving the value
162    R get();
163
164    // functions to check state
165    bool valid() const noexcept;
166
167    void wait() const;
168    template <class Rep, class Period>
169        future_status
170        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171    template <class Clock, class Duration>
172        future_status
173        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
174};
175
176template <class R>
177class future<R&>
178{
179public:
180    future() noexcept;
181    future(future&&) noexcept;
182    future(const future& rhs) = delete;
183    ~future();
184    future& operator=(const future& rhs) = delete;
185    future& operator=(future&&) noexcept;
186    shared_future<R&> share() noexcept;
187
188    // retrieving the value
189    R& get();
190
191    // functions to check state
192    bool valid() const noexcept;
193
194    void wait() const;
195    template <class Rep, class Period>
196        future_status
197        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198    template <class Clock, class Duration>
199        future_status
200        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
201};
202
203template <>
204class future<void>
205{
206public:
207    future() noexcept;
208    future(future&&) noexcept;
209    future(const future& rhs) = delete;
210    ~future();
211    future& operator=(const future& rhs) = delete;
212    future& operator=(future&&) noexcept;
213    shared_future<void> share() noexcept;
214
215    // retrieving the value
216    void get();
217
218    // functions to check state
219    bool valid() const noexcept;
220
221    void wait() const;
222    template <class Rep, class Period>
223        future_status
224        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225    template <class Clock, class Duration>
226        future_status
227        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
228};
229
230template <class R>
231class shared_future
232{
233public:
234    shared_future() noexcept;
235    shared_future(const shared_future& rhs);
236    shared_future(future<R>&&) noexcept;
237    shared_future(shared_future&& rhs) noexcept;
238    ~shared_future();
239    shared_future& operator=(const shared_future& rhs);
240    shared_future& operator=(shared_future&& rhs) noexcept;
241
242    // retrieving the value
243    const R& get() const;
244
245    // functions to check state
246    bool valid() const noexcept;
247
248    void wait() const;
249    template <class Rep, class Period>
250        future_status
251        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252    template <class Clock, class Duration>
253        future_status
254        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
255};
256
257template <class R>
258class shared_future<R&>
259{
260public:
261    shared_future() noexcept;
262    shared_future(const shared_future& rhs);
263    shared_future(future<R&>&&) noexcept;
264    shared_future(shared_future&& rhs) noexcept;
265    ~shared_future();
266    shared_future& operator=(const shared_future& rhs);
267    shared_future& operator=(shared_future&& rhs) noexcept;
268
269    // retrieving the value
270    R& get() const;
271
272    // functions to check state
273    bool valid() const noexcept;
274
275    void wait() const;
276    template <class Rep, class Period>
277        future_status
278        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279    template <class Clock, class Duration>
280        future_status
281        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
282};
283
284template <>
285class shared_future<void>
286{
287public:
288    shared_future() noexcept;
289    shared_future(const shared_future& rhs);
290    shared_future(future<void>&&) noexcept;
291    shared_future(shared_future&& rhs) noexcept;
292    ~shared_future();
293    shared_future& operator=(const shared_future& rhs);
294    shared_future& operator=(shared_future&& rhs) noexcept;
295
296    // retrieving the value
297    void get() const;
298
299    // functions to check state
300    bool valid() const noexcept;
301
302    void wait() const;
303    template <class Rep, class Period>
304        future_status
305        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306    template <class Clock, class Duration>
307        future_status
308        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
309};
310
311template <class F, class... Args>
312  future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
313  async(F&& f, Args&&... args);
314
315template <class F, class... Args>
316  future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
317  async(launch policy, F&& f, Args&&... args);
318
319template <class> class packaged_task; // undefined
320
321template <class R, class... ArgTypes>
322class packaged_task<R(ArgTypes...)>
323{
324public:
325    typedef R result_type; // extension
326
327    // construction and destruction
328    packaged_task() noexcept;
329    template <class F>
330        explicit packaged_task(F&& f);
331    template <class F, class Allocator>
332        packaged_task(allocator_arg_t, const Allocator& a, F&& f);
333    ~packaged_task();
334
335    // no copy
336    packaged_task(const packaged_task&) = delete;
337    packaged_task& operator=(const packaged_task&) = delete;
338
339    // move support
340    packaged_task(packaged_task&& other) noexcept;
341    packaged_task& operator=(packaged_task&& other) noexcept;
342    void swap(packaged_task& other) noexcept;
343
344    bool valid() const noexcept;
345
346    // result retrieval
347    future<R> get_future();
348
349    // execution
350    void operator()(ArgTypes... );
351    void make_ready_at_thread_exit(ArgTypes...);
352
353    void reset();
354};
355
356template <class R>
357  void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
358
359template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
360
361}  // std
362
363*/
364
365#include <__assert> // all public C++ headers provide the assertion handler
366#include <__availability>
367#include <__chrono/duration.h>
368#include <__chrono/time_point.h>
369#include <__config>
370#include <__exception/exception_ptr.h>
371#include <__memory/addressof.h>
372#include <__memory/allocator.h>
373#include <__memory/allocator_arg_t.h>
374#include <__memory/allocator_destructor.h>
375#include <__memory/allocator_traits.h>
376#include <__memory/compressed_pair.h>
377#include <__memory/pointer_traits.h>
378#include <__memory/shared_ptr.h>
379#include <__memory/unique_ptr.h>
380#include <__memory/uses_allocator.h>
381#include <__system_error/error_category.h>
382#include <__system_error/error_code.h>
383#include <__system_error/error_condition.h>
384#include <__type_traits/aligned_storage.h>
385#include <__type_traits/strip_signature.h>
386#include <__utility/auto_cast.h>
387#include <__utility/forward.h>
388#include <__utility/move.h>
389#include <mutex>
390#include <new>
391#include <stdexcept>
392#include <thread>
393#include <version>
394
395#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
396#  pragma GCC system_header
397#endif
398
399#ifdef _LIBCPP_HAS_NO_THREADS
400#  error "<future> is not supported since libc++ has been configured without support for threads."
401#endif
402
403_LIBCPP_BEGIN_NAMESPACE_STD
404
405// enum class future_errc
406_LIBCPP_DECLARE_STRONG_ENUM(future_errc){
407    future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};
408_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
409
410template <>
411struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
412
413#ifdef _LIBCPP_CXX03_LANG
414template <>
415struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type {};
416#endif
417
418// enum class launch
419_LIBCPP_DECLARE_STRONG_ENUM(launch){async = 1, deferred = 2, any = async | deferred};
420_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
421
422#ifndef _LIBCPP_CXX03_LANG
423
424typedef underlying_type<launch>::type __launch_underlying_type;
425
426inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator&(launch __x, launch __y) {
427  return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));
428}
429
430inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator|(launch __x, launch __y) {
431  return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | static_cast<__launch_underlying_type>(__y));
432}
433
434inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator^(launch __x, launch __y) {
435  return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));
436}
437
438inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator~(launch __x) {
439  return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
440}
441
442inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {
443  __x = __x & __y;
444  return __x;
445}
446
447inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {
448  __x = __x | __y;
449  return __x;
450}
451
452inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {
453  __x = __x ^ __y;
454  return __x;
455}
456
457#endif // !_LIBCPP_CXX03_LANG
458
459// enum class future_status
460_LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};
461_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
462
463_LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
464
465inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {
466  return error_code(static_cast<int>(__e), future_category());
467}
468
469inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {
470  return error_condition(static_cast<int>(__e), future_category());
471}
472
473_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);
474
475class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {
476  error_code __ec_;
477
478  future_error(error_code);
479  friend void __throw_future_error(future_errc);
480  template <class>
481  friend class promise;
482
483public:
484#if _LIBCPP_STD_VER >= 17
485  _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
486#endif
487
488  _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
489
490  _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
491  ~future_error() _NOEXCEPT override;
492};
493
494// Declared above std::future_error
495void __throw_future_error(future_errc __ev) {
496#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
497  throw future_error(make_error_code(__ev));
498#else
499  (void)__ev;
500  _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
501#endif
502}
503
504class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {
505protected:
506  exception_ptr __exception_;
507  mutable mutex __mut_;
508  mutable condition_variable __cv_;
509  unsigned __state_;
510
511  void __on_zero_shared() _NOEXCEPT override;
512  void __sub_wait(unique_lock<mutex>& __lk);
513
514public:
515  enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };
516
517  _LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}
518
519  _LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }
520
521  _LIBCPP_HIDE_FROM_ABI void __attach_future() {
522    lock_guard<mutex> __lk(__mut_);
523    bool __has_future_attached = (__state_ & __future_attached) != 0;
524    if (__has_future_attached)
525      __throw_future_error(future_errc::future_already_retrieved);
526    this->__add_shared();
527    __state_ |= __future_attached;
528  }
529
530  _LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }
531
532  void __make_ready();
533  _LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }
534
535  void set_value();
536  void set_value_at_thread_exit();
537
538  void set_exception(exception_ptr __p);
539  void set_exception_at_thread_exit(exception_ptr __p);
540
541  void copy();
542
543  void wait();
544  template <class _Rep, class _Period>
545  future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
546  template <class _Clock, class _Duration>
547  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS future_status
548  wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
549
550  virtual void __execute();
551};
552
553template <class _Clock, class _Duration>
554future_status __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
555  unique_lock<mutex> __lk(__mut_);
556  if (__state_ & deferred)
557    return future_status::deferred;
558  while (!(__state_ & ready) && _Clock::now() < __abs_time)
559    __cv_.wait_until(__lk, __abs_time);
560  if (__state_ & ready)
561    return future_status::ready;
562  return future_status::timeout;
563}
564
565template <class _Rep, class _Period>
566inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
567  return wait_until(chrono::steady_clock::now() + __rel_time);
568}
569
570template <class _Rp>
571class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {
572  typedef __assoc_sub_state base;
573  _LIBCPP_SUPPRESS_DEPRECATED_PUSH
574  typedef typename aligned_storage<sizeof(_Rp), _LIBCPP_ALIGNOF(_Rp)>::type _Up;
575  _LIBCPP_SUPPRESS_DEPRECATED_POP
576
577protected:
578  _Up __value_;
579
580  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
581
582public:
583  template <class _Arg>
584  _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
585
586  template <class _Arg>
587  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
588
589  _LIBCPP_HIDE_FROM_ABI _Rp move();
590  _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<_Rp> copy();
591};
592
593template <class _Rp>
594void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {
595  if (this->__state_ & base::__constructed)
596    reinterpret_cast<_Rp*>(&__value_)->~_Rp();
597  delete this;
598}
599
600template <class _Rp>
601template <class _Arg>
602void __assoc_state<_Rp>::set_value(_Arg&& __arg) {
603  unique_lock<mutex> __lk(this->__mut_);
604  if (this->__has_value())
605    __throw_future_error(future_errc::promise_already_satisfied);
606  ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
607  this->__state_ |= base::__constructed | base::ready;
608  __cv_.notify_all();
609}
610
611template <class _Rp>
612template <class _Arg>
613void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {
614  unique_lock<mutex> __lk(this->__mut_);
615  if (this->__has_value())
616    __throw_future_error(future_errc::promise_already_satisfied);
617  ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
618  this->__state_ |= base::__constructed;
619  __thread_local_data()->__make_ready_at_thread_exit(this);
620}
621
622template <class _Rp>
623_Rp __assoc_state<_Rp>::move() {
624  unique_lock<mutex> __lk(this->__mut_);
625  this->__sub_wait(__lk);
626  if (this->__exception_ != nullptr)
627    std::rethrow_exception(this->__exception_);
628  return std::move(*reinterpret_cast<_Rp*>(&__value_));
629}
630
631template <class _Rp>
632__add_lvalue_reference_t<_Rp> __assoc_state<_Rp>::copy() {
633  unique_lock<mutex> __lk(this->__mut_);
634  this->__sub_wait(__lk);
635  if (this->__exception_ != nullptr)
636    std::rethrow_exception(this->__exception_);
637  return *reinterpret_cast<_Rp*>(&__value_);
638}
639
640template <class _Rp>
641class __assoc_state<_Rp&> : public __assoc_sub_state {
642  typedef __assoc_sub_state base;
643  typedef _Rp* _Up;
644
645protected:
646  _Up __value_;
647
648  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
649
650public:
651  _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
652  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
653
654  _LIBCPP_HIDE_FROM_ABI _Rp& copy();
655};
656
657template <class _Rp>
658void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {
659  delete this;
660}
661
662template <class _Rp>
663void __assoc_state<_Rp&>::set_value(_Rp& __arg) {
664  unique_lock<mutex> __lk(this->__mut_);
665  if (this->__has_value())
666    __throw_future_error(future_errc::promise_already_satisfied);
667  __value_ = std::addressof(__arg);
668  this->__state_ |= base::__constructed | base::ready;
669  __cv_.notify_all();
670}
671
672template <class _Rp>
673void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {
674  unique_lock<mutex> __lk(this->__mut_);
675  if (this->__has_value())
676    __throw_future_error(future_errc::promise_already_satisfied);
677  __value_ = std::addressof(__arg);
678  this->__state_ |= base::__constructed;
679  __thread_local_data()->__make_ready_at_thread_exit(this);
680}
681
682template <class _Rp>
683_Rp& __assoc_state<_Rp&>::copy() {
684  unique_lock<mutex> __lk(this->__mut_);
685  this->__sub_wait(__lk);
686  if (this->__exception_ != nullptr)
687    std::rethrow_exception(this->__exception_);
688  return *__value_;
689}
690
691template <class _Rp, class _Alloc>
692class __assoc_state_alloc : public __assoc_state<_Rp> {
693  typedef __assoc_state<_Rp> base;
694  _Alloc __alloc_;
695
696  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
697
698public:
699  _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
700};
701
702template <class _Rp, class _Alloc>
703void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {
704  if (this->__state_ & base::__constructed)
705    reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();
706  typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
707  typedef allocator_traits<_Al> _ATraits;
708  typedef pointer_traits<typename _ATraits::pointer> _PTraits;
709  _Al __a(__alloc_);
710  this->~__assoc_state_alloc();
711  __a.deallocate(_PTraits::pointer_to(*this), 1);
712}
713
714template <class _Rp, class _Alloc>
715class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {
716  typedef __assoc_state<_Rp&> base;
717  _Alloc __alloc_;
718
719  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
720
721public:
722  _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
723};
724
725template <class _Rp, class _Alloc>
726void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {
727  typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
728  typedef allocator_traits<_Al> _ATraits;
729  typedef pointer_traits<typename _ATraits::pointer> _PTraits;
730  _Al __a(__alloc_);
731  this->~__assoc_state_alloc();
732  __a.deallocate(_PTraits::pointer_to(*this), 1);
733}
734
735template <class _Alloc>
736class __assoc_sub_state_alloc : public __assoc_sub_state {
737  typedef __assoc_sub_state base;
738  _Alloc __alloc_;
739
740  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
741
742public:
743  _LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
744};
745
746template <class _Alloc>
747void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {
748  typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
749  typedef allocator_traits<_Al> _ATraits;
750  typedef pointer_traits<typename _ATraits::pointer> _PTraits;
751  _Al __a(__alloc_);
752  this->~__assoc_sub_state_alloc();
753  __a.deallocate(_PTraits::pointer_to(*this), 1);
754}
755
756template <class _Rp, class _Fp>
757class __deferred_assoc_state : public __assoc_state<_Rp> {
758  typedef __assoc_state<_Rp> base;
759
760  _Fp __func_;
761
762public:
763  _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
764
765  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
766};
767
768template <class _Rp, class _Fp>
769inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
770  this->__set_deferred();
771}
772
773template <class _Rp, class _Fp>
774void __deferred_assoc_state<_Rp, _Fp>::__execute() {
775#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
776  try {
777#endif // _LIBCPP_HAS_NO_EXCEPTIONS
778    this->set_value(__func_());
779#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
780  } catch (...) {
781    this->set_exception(current_exception());
782  }
783#endif // _LIBCPP_HAS_NO_EXCEPTIONS
784}
785
786template <class _Fp>
787class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {
788  typedef __assoc_sub_state base;
789
790  _Fp __func_;
791
792public:
793  _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
794
795  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
796};
797
798template <class _Fp>
799inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
800  this->__set_deferred();
801}
802
803template <class _Fp>
804void __deferred_assoc_state<void, _Fp>::__execute() {
805#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
806  try {
807#endif // _LIBCPP_HAS_NO_EXCEPTIONS
808    __func_();
809    this->set_value();
810#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
811  } catch (...) {
812    this->set_exception(current_exception());
813  }
814#endif // _LIBCPP_HAS_NO_EXCEPTIONS
815}
816
817template <class _Rp, class _Fp>
818class __async_assoc_state : public __assoc_state<_Rp> {
819  typedef __assoc_state<_Rp> base;
820
821  _Fp __func_;
822
823  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
824
825public:
826  _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
827
828  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
829};
830
831template <class _Rp, class _Fp>
832inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
833
834template <class _Rp, class _Fp>
835void __async_assoc_state<_Rp, _Fp>::__execute() {
836#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
837  try {
838#endif // _LIBCPP_HAS_NO_EXCEPTIONS
839    this->set_value(__func_());
840#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
841  } catch (...) {
842    this->set_exception(current_exception());
843  }
844#endif // _LIBCPP_HAS_NO_EXCEPTIONS
845}
846
847template <class _Rp, class _Fp>
848void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {
849  this->wait();
850  base::__on_zero_shared();
851}
852
853template <class _Fp>
854class __async_assoc_state<void, _Fp> : public __assoc_sub_state {
855  typedef __assoc_sub_state base;
856
857  _Fp __func_;
858
859  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
860
861public:
862  _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
863
864  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
865};
866
867template <class _Fp>
868inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
869
870template <class _Fp>
871void __async_assoc_state<void, _Fp>::__execute() {
872#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
873  try {
874#endif // _LIBCPP_HAS_NO_EXCEPTIONS
875    __func_();
876    this->set_value();
877#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
878  } catch (...) {
879    this->set_exception(current_exception());
880  }
881#endif // _LIBCPP_HAS_NO_EXCEPTIONS
882}
883
884template <class _Fp>
885void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {
886  this->wait();
887  base::__on_zero_shared();
888}
889
890template <class _Rp>
891class _LIBCPP_TEMPLATE_VIS promise;
892template <class _Rp>
893class _LIBCPP_TEMPLATE_VIS shared_future;
894
895// future
896
897template <class _Rp>
898class _LIBCPP_TEMPLATE_VIS future;
899
900template <class _Rp, class _Fp>
901_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);
902
903template <class _Rp, class _Fp>
904_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);
905
906template <class _Rp>
907class _LIBCPP_TEMPLATE_VIS future {
908  __assoc_state<_Rp>* __state_;
909
910  explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
911
912  template <class>
913  friend class promise;
914  template <class>
915  friend class shared_future;
916
917  template <class _R1, class _Fp>
918  friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
919  template <class _R1, class _Fp>
920  friend future<_R1> __make_async_assoc_state(_Fp&& __f);
921
922public:
923  _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
924  _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
925  future(const future&)            = delete;
926  future& operator=(const future&) = delete;
927  _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
928    future(std::move(__rhs)).swap(*this);
929    return *this;
930  }
931
932  _LIBCPP_HIDE_FROM_ABI ~future();
933  _LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;
934
935  // retrieving the value
936  _LIBCPP_HIDE_FROM_ABI _Rp get();
937
938  _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
939
940  // functions to check state
941  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
942
943  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
944  template <class _Rep, class _Period>
945  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
946    return __state_->wait_for(__rel_time);
947  }
948  template <class _Clock, class _Duration>
949  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
950    return __state_->wait_until(__abs_time);
951  }
952};
953
954template <class _Rp>
955future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {
956  __state_->__attach_future();
957}
958
959struct __release_shared_count {
960  _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }
961};
962
963template <class _Rp>
964future<_Rp>::~future() {
965  if (__state_)
966    __state_->__release_shared();
967}
968
969template <class _Rp>
970_Rp future<_Rp>::get() {
971  unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
972  __assoc_state<_Rp>* __s = __state_;
973  __state_                = nullptr;
974  return __s->move();
975}
976
977template <class _Rp>
978class _LIBCPP_TEMPLATE_VIS future<_Rp&> {
979  __assoc_state<_Rp&>* __state_;
980
981  explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
982
983  template <class>
984  friend class promise;
985  template <class>
986  friend class shared_future;
987
988  template <class _R1, class _Fp>
989  friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
990  template <class _R1, class _Fp>
991  friend future<_R1> __make_async_assoc_state(_Fp&& __f);
992
993public:
994  _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
995  _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
996  future(const future&)            = delete;
997  future& operator=(const future&) = delete;
998  _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
999    future(std::move(__rhs)).swap(*this);
1000    return *this;
1001  }
1002
1003  _LIBCPP_HIDE_FROM_ABI ~future();
1004  _LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;
1005
1006  // retrieving the value
1007  _LIBCPP_HIDE_FROM_ABI _Rp& get();
1008
1009  _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1010
1011  // functions to check state
1012  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1013
1014  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1015  template <class _Rep, class _Period>
1016  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1017    return __state_->wait_for(__rel_time);
1018  }
1019  template <class _Clock, class _Duration>
1020  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1021    return __state_->wait_until(__abs_time);
1022  }
1023};
1024
1025template <class _Rp>
1026future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {
1027  __state_->__attach_future();
1028}
1029
1030template <class _Rp>
1031future<_Rp&>::~future() {
1032  if (__state_)
1033    __state_->__release_shared();
1034}
1035
1036template <class _Rp>
1037_Rp& future<_Rp&>::get() {
1038  unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1039  __assoc_state<_Rp&>* __s = __state_;
1040  __state_                 = nullptr;
1041  return __s->copy();
1042}
1043
1044template <>
1045class _LIBCPP_EXPORTED_FROM_ABI future<void> {
1046  __assoc_sub_state* __state_;
1047
1048  explicit future(__assoc_sub_state* __state);
1049
1050  template <class>
1051  friend class promise;
1052  template <class>
1053  friend class shared_future;
1054
1055  template <class _R1, class _Fp>
1056  friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1057  template <class _R1, class _Fp>
1058  friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1059
1060public:
1061  _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1062  _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1063  future(const future&)            = delete;
1064  future& operator=(const future&) = delete;
1065  _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1066    future(std::move(__rhs)).swap(*this);
1067    return *this;
1068  }
1069
1070  ~future();
1071  _LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;
1072
1073  // retrieving the value
1074  void get();
1075
1076  _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1077
1078  // functions to check state
1079  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1080
1081  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1082  template <class _Rep, class _Period>
1083  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1084    return __state_->wait_for(__rel_time);
1085  }
1086  template <class _Clock, class _Duration>
1087  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1088    return __state_->wait_until(__abs_time);
1089  }
1090};
1091
1092template <class _Rp>
1093inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {
1094  __x.swap(__y);
1095}
1096
1097// promise<R>
1098
1099template <class _Callable>
1100class packaged_task;
1101
1102template <class _Rp>
1103class _LIBCPP_TEMPLATE_VIS promise {
1104  __assoc_state<_Rp>* __state_;
1105
1106  _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1107
1108  template <class>
1109  friend class packaged_task;
1110
1111public:
1112  _LIBCPP_HIDE_FROM_ABI promise();
1113  template <class _Alloc>
1114  _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1115  _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1116  promise(const promise& __rhs) = delete;
1117  _LIBCPP_HIDE_FROM_ABI ~promise();
1118
1119  // assignment
1120  _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1121    promise(std::move(__rhs)).swap(*this);
1122    return *this;
1123  }
1124  promise& operator=(const promise& __rhs) = delete;
1125
1126  _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1127
1128  // retrieving the result
1129  _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1130
1131  // setting the result
1132  _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1133  _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1134  _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1135
1136  // setting the result with deferred notification
1137  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1138  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1139  _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1140};
1141
1142template <class _Rp>
1143promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}
1144
1145template <class _Rp>
1146template <class _Alloc>
1147promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {
1148  typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1149  typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1150  typedef __allocator_destructor<_A2> _D2;
1151  _A2 __a(__a0);
1152  unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1153  ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1154  __state_ = std::addressof(*__hold.release());
1155}
1156
1157template <class _Rp>
1158promise<_Rp>::~promise() {
1159  if (__state_) {
1160    if (!__state_->__has_value() && __state_->use_count() > 1)
1161      __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1162    __state_->__release_shared();
1163  }
1164}
1165
1166template <class _Rp>
1167future<_Rp> promise<_Rp>::get_future() {
1168  if (__state_ == nullptr)
1169    __throw_future_error(future_errc::no_state);
1170  return future<_Rp>(__state_);
1171}
1172
1173template <class _Rp>
1174void promise<_Rp>::set_value(const _Rp& __r) {
1175  if (__state_ == nullptr)
1176    __throw_future_error(future_errc::no_state);
1177  __state_->set_value(__r);
1178}
1179
1180template <class _Rp>
1181void promise<_Rp>::set_value(_Rp&& __r) {
1182  if (__state_ == nullptr)
1183    __throw_future_error(future_errc::no_state);
1184  __state_->set_value(std::move(__r));
1185}
1186
1187template <class _Rp>
1188void promise<_Rp>::set_exception(exception_ptr __p) {
1189  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1190  if (__state_ == nullptr)
1191    __throw_future_error(future_errc::no_state);
1192  __state_->set_exception(__p);
1193}
1194
1195template <class _Rp>
1196void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {
1197  if (__state_ == nullptr)
1198    __throw_future_error(future_errc::no_state);
1199  __state_->set_value_at_thread_exit(__r);
1200}
1201
1202template <class _Rp>
1203void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {
1204  if (__state_ == nullptr)
1205    __throw_future_error(future_errc::no_state);
1206  __state_->set_value_at_thread_exit(std::move(__r));
1207}
1208
1209template <class _Rp>
1210void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {
1211  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1212  if (__state_ == nullptr)
1213    __throw_future_error(future_errc::no_state);
1214  __state_->set_exception_at_thread_exit(__p);
1215}
1216
1217// promise<R&>
1218
1219template <class _Rp>
1220class _LIBCPP_TEMPLATE_VIS promise<_Rp&> {
1221  __assoc_state<_Rp&>* __state_;
1222
1223  _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1224
1225  template <class>
1226  friend class packaged_task;
1227
1228public:
1229  _LIBCPP_HIDE_FROM_ABI promise();
1230  template <class _Allocator>
1231  _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1232  _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1233  promise(const promise& __rhs) = delete;
1234  _LIBCPP_HIDE_FROM_ABI ~promise();
1235
1236  // assignment
1237  _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1238    promise(std::move(__rhs)).swap(*this);
1239    return *this;
1240  }
1241  promise& operator=(const promise& __rhs) = delete;
1242
1243  _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1244
1245  // retrieving the result
1246  _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1247
1248  // setting the result
1249  _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1250  _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1251
1252  // setting the result with deferred notification
1253  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1254  _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1255};
1256
1257template <class _Rp>
1258promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}
1259
1260template <class _Rp>
1261template <class _Alloc>
1262promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {
1263  typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1264  typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1265  typedef __allocator_destructor<_A2> _D2;
1266  _A2 __a(__a0);
1267  unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1268  ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1269  __state_ = std::addressof(*__hold.release());
1270}
1271
1272template <class _Rp>
1273promise<_Rp&>::~promise() {
1274  if (__state_) {
1275    if (!__state_->__has_value() && __state_->use_count() > 1)
1276      __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1277    __state_->__release_shared();
1278  }
1279}
1280
1281template <class _Rp>
1282future<_Rp&> promise<_Rp&>::get_future() {
1283  if (__state_ == nullptr)
1284    __throw_future_error(future_errc::no_state);
1285  return future<_Rp&>(__state_);
1286}
1287
1288template <class _Rp>
1289void promise<_Rp&>::set_value(_Rp& __r) {
1290  if (__state_ == nullptr)
1291    __throw_future_error(future_errc::no_state);
1292  __state_->set_value(__r);
1293}
1294
1295template <class _Rp>
1296void promise<_Rp&>::set_exception(exception_ptr __p) {
1297  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1298  if (__state_ == nullptr)
1299    __throw_future_error(future_errc::no_state);
1300  __state_->set_exception(__p);
1301}
1302
1303template <class _Rp>
1304void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {
1305  if (__state_ == nullptr)
1306    __throw_future_error(future_errc::no_state);
1307  __state_->set_value_at_thread_exit(__r);
1308}
1309
1310template <class _Rp>
1311void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {
1312  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1313  if (__state_ == nullptr)
1314    __throw_future_error(future_errc::no_state);
1315  __state_->set_exception_at_thread_exit(__p);
1316}
1317
1318// promise<void>
1319
1320template <>
1321class _LIBCPP_EXPORTED_FROM_ABI promise<void> {
1322  __assoc_sub_state* __state_;
1323
1324  _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1325
1326  template <class>
1327  friend class packaged_task;
1328
1329public:
1330  promise();
1331  template <class _Allocator>
1332  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS promise(allocator_arg_t, const _Allocator& __a);
1333  _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1334  promise(const promise& __rhs) = delete;
1335  ~promise();
1336
1337  // assignment
1338  _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1339    promise(std::move(__rhs)).swap(*this);
1340    return *this;
1341  }
1342  promise& operator=(const promise& __rhs) = delete;
1343
1344  _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1345
1346  // retrieving the result
1347  future<void> get_future();
1348
1349  // setting the result
1350  void set_value();
1351  void set_exception(exception_ptr __p);
1352
1353  // setting the result with deferred notification
1354  void set_value_at_thread_exit();
1355  void set_exception_at_thread_exit(exception_ptr __p);
1356};
1357
1358template <class _Alloc>
1359promise<void>::promise(allocator_arg_t, const _Alloc& __a0) {
1360  typedef __assoc_sub_state_alloc<_Alloc> _State;
1361  typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1362  typedef __allocator_destructor<_A2> _D2;
1363  _A2 __a(__a0);
1364  unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1365  ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1366  __state_ = std::addressof(*__hold.release());
1367}
1368
1369template <class _Rp>
1370inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {
1371  __x.swap(__y);
1372}
1373
1374template <class _Rp, class _Alloc>
1375struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc> : public true_type {};
1376
1377// packaged_task
1378
1379template <class _Fp>
1380class __packaged_task_base;
1381
1382template <class _Rp, class... _ArgTypes>
1383class __packaged_task_base<_Rp(_ArgTypes...)> {
1384  __packaged_task_base(const __packaged_task_base&);
1385  __packaged_task_base& operator=(const __packaged_task_base&);
1386
1387public:
1388  _LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}
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
1397template <class _FD, class _Alloc, class _FB>
1398class __packaged_task_func;
1399
1400template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1401class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
1402  __compressed_pair<_Fp, _Alloc> __f_;
1403
1404public:
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
1415template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1416void __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
1421template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1422void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
1423  __f_.~__compressed_pair<_Fp, _Alloc>();
1424}
1425
1426template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1427void __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
1436template <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
1441template <class _Callable>
1442class __packaged_task_function;
1443
1444template <class _Rp, class... _ArgTypes>
1445class __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
1455public:
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 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1476};
1477
1478template <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
1491template <class _Rp, class... _ArgTypes>
1492template <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
1509template <class _Rp, class... _ArgTypes>
1510template <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
1528template <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
1548template <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
1556template <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
1587template <class _Rp, class... _ArgTypes>
1588inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
1589  return (*__f_)(std::forward<_ArgTypes>(__arg)...);
1590}
1591
1592template <class _Rp, class... _ArgTypes>
1593class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)> {
1594public:
1595  typedef _Rp result_type; // extension
1596
1597private:
1598  __packaged_task_function<result_type(_ArgTypes...)> __f_;
1599  promise<result_type> __p_;
1600
1601public:
1602  // construction and destruction
1603  _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1604  template <class _Fp, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1605  _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1606  template <class _Fp, class _Allocator, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1607  _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1608      : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1609  // ~packaged_task() = default;
1610
1611  // no copy
1612  packaged_task(const packaged_task&)            = delete;
1613  packaged_task& operator=(const packaged_task&) = delete;
1614
1615  // move support
1616  _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1617      : __f_(std::move(__other.__f_)),
1618        __p_(std::move(__other.__p_)) {}
1619  _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1620    __f_ = std::move(__other.__f_);
1621    __p_ = std::move(__other.__p_);
1622    return *this;
1623  }
1624  _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1625    __f_.swap(__other.__f_);
1626    __p_.swap(__other.__p_);
1627  }
1628
1629  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1630
1631  // result retrieval
1632  _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1633
1634  // execution
1635  _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1636  _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1637
1638  _LIBCPP_HIDE_FROM_ABI void reset();
1639};
1640
1641template <class _Rp, class... _ArgTypes>
1642void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1643  if (__p_.__state_ == nullptr)
1644    __throw_future_error(future_errc::no_state);
1645  if (__p_.__state_->__has_value())
1646    __throw_future_error(future_errc::promise_already_satisfied);
1647#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1648  try {
1649#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1650    __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
1651#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1652  } catch (...) {
1653    __p_.set_exception(current_exception());
1654  }
1655#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1656}
1657
1658template <class _Rp, class... _ArgTypes>
1659void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1660  if (__p_.__state_ == nullptr)
1661    __throw_future_error(future_errc::no_state);
1662  if (__p_.__state_->__has_value())
1663    __throw_future_error(future_errc::promise_already_satisfied);
1664#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1665  try {
1666#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1667    __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
1668#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1669  } catch (...) {
1670    __p_.set_exception_at_thread_exit(current_exception());
1671  }
1672#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1673}
1674
1675template <class _Rp, class... _ArgTypes>
1676void packaged_task<_Rp(_ArgTypes...)>::reset() {
1677  if (!valid())
1678    __throw_future_error(future_errc::no_state);
1679  __p_ = promise<result_type>();
1680}
1681
1682template <class... _ArgTypes>
1683class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)> {
1684public:
1685  typedef void result_type; // extension
1686
1687private:
1688  __packaged_task_function<result_type(_ArgTypes...)> __f_;
1689  promise<result_type> __p_;
1690
1691public:
1692  // construction and destruction
1693  _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1694  template <class _Fp, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1695  _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1696  template <class _Fp, class _Allocator, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1697  _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1698      : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1699  // ~packaged_task() = default;
1700
1701  // no copy
1702  packaged_task(const packaged_task&)            = delete;
1703  packaged_task& operator=(const packaged_task&) = delete;
1704
1705  // move support
1706  _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1707      : __f_(std::move(__other.__f_)),
1708        __p_(std::move(__other.__p_)) {}
1709  _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1710    __f_ = std::move(__other.__f_);
1711    __p_ = std::move(__other.__p_);
1712    return *this;
1713  }
1714  _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1715    __f_.swap(__other.__f_);
1716    __p_.swap(__other.__p_);
1717  }
1718
1719  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1720
1721  // result retrieval
1722  _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1723
1724  // execution
1725  _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1726  _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1727
1728  _LIBCPP_HIDE_FROM_ABI void reset();
1729};
1730
1731#if _LIBCPP_STD_VER >= 17
1732
1733template <class _Rp, class... _Args>
1734packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
1735
1736template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1737packaged_task(_Fp) -> packaged_task<_Stripped>;
1738
1739#endif
1740
1741template <class... _ArgTypes>
1742void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1743  if (__p_.__state_ == nullptr)
1744    __throw_future_error(future_errc::no_state);
1745  if (__p_.__state_->__has_value())
1746    __throw_future_error(future_errc::promise_already_satisfied);
1747#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1748  try {
1749#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1750    __f_(std::forward<_ArgTypes>(__args)...);
1751    __p_.set_value();
1752#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1753  } catch (...) {
1754    __p_.set_exception(current_exception());
1755  }
1756#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1757}
1758
1759template <class... _ArgTypes>
1760void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1761  if (__p_.__state_ == nullptr)
1762    __throw_future_error(future_errc::no_state);
1763  if (__p_.__state_->__has_value())
1764    __throw_future_error(future_errc::promise_already_satisfied);
1765#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1766  try {
1767#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1768    __f_(std::forward<_ArgTypes>(__args)...);
1769    __p_.set_value_at_thread_exit();
1770#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1771  } catch (...) {
1772    __p_.set_exception_at_thread_exit(current_exception());
1773  }
1774#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1775}
1776
1777template <class... _ArgTypes>
1778void packaged_task<void(_ArgTypes...)>::reset() {
1779  if (!valid())
1780    __throw_future_error(future_errc::no_state);
1781  __p_ = promise<result_type>();
1782}
1783
1784template <class _Rp, class... _ArgTypes>
1785inline _LIBCPP_HIDE_FROM_ABI void
1786swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1787  __x.swap(__y);
1788}
1789
1790template <class _Callable, class _Alloc>
1791struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
1792
1793template <class _Rp, class _Fp>
1794_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
1795  unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1796      new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1797  return future<_Rp>(__h.get());
1798}
1799
1800template <class _Rp, class _Fp>
1801_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
1802  unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1803      new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1804  std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
1805  return future<_Rp>(__h.get());
1806}
1807
1808#ifndef _LIBCPP_CXX03_LANG
1809
1810template <class _Fp, class... _Args>
1811class _LIBCPP_HIDDEN __async_func {
1812  tuple<_Fp, _Args...> __f_;
1813
1814public:
1815  typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
1816
1817  _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Fp&& __f, _Args&&... __args)
1818      : __f_(std::move(__f), std::move(__args)...) {}
1819
1820  _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
1821
1822  _LIBCPP_HIDE_FROM_ABI _Rp operator()() {
1823    typedef typename __make_tuple_indices<1 + sizeof...(_Args), 1>::type _Index;
1824    return __execute(_Index());
1825  }
1826
1827private:
1828  template <size_t... _Indices>
1829  _LIBCPP_HIDE_FROM_ABI _Rp __execute(__tuple_indices<_Indices...>) {
1830    return std::__invoke(std::move(std::get<0>(__f_)), std::move(std::get<_Indices>(__f_))...);
1831  }
1832};
1833
1834inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
1835  return (int(__policy) & int(__value)) != 0;
1836}
1837
1838template <class _Fp, class... _Args>
1839_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
1840    future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1841    async(launch __policy, _Fp&& __f, _Args&&... __args) {
1842  typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
1843  typedef typename _BF::_Rp _Rp;
1844
1845#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1846  try {
1847#  endif
1848    if (__does_policy_contain(__policy, launch::async))
1849      return std::__make_async_assoc_state<_Rp>(
1850          _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1851#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1852  } catch (...) {
1853    if (__policy == launch::async)
1854      throw;
1855  }
1856#  endif
1857
1858  if (__does_policy_contain(__policy, launch::deferred))
1859    return std::__make_deferred_assoc_state<_Rp>(
1860        _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1861  return future<_Rp>{};
1862}
1863
1864template <class _Fp, class... _Args>
1865_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
1866    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
1875template <class _Rp>
1876class _LIBCPP_TEMPLATE_VIS shared_future {
1877  __assoc_state<_Rp>* __state_;
1878
1879public:
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
1915template <class _Rp>
1916shared_future<_Rp>::~shared_future() {
1917  if (__state_)
1918    __state_->__release_shared();
1919}
1920
1921template <class _Rp>
1922shared_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
1931template <class _Rp>
1932class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&> {
1933  __assoc_state<_Rp&>* __state_;
1934
1935public:
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
1971template <class _Rp>
1972shared_future<_Rp&>::~shared_future() {
1973  if (__state_)
1974    __state_->__release_shared();
1975}
1976
1977template <class _Rp>
1978shared_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
1987template <>
1988class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
1989  __assoc_sub_state* __state_;
1990
1991public:
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
2027template <class _Rp>
2028inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
2029  __x.swap(__y);
2030}
2031
2032template <class _Rp>
2033inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
2034  return shared_future<_Rp>(std::move(*this));
2035}
2036
2037template <class _Rp>
2038inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
2039  return shared_future<_Rp&>(std::move(*this));
2040}
2041
2042inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
2043
2044_LIBCPP_END_NAMESPACE_STD
2045
2046#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2047#  include <chrono>
2048#endif
2049
2050#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2051#  include <atomic>
2052#  include <cstdlib>
2053#  include <exception>
2054#  include <iosfwd>
2055#  include <system_error>
2056#endif
2057
2058#endif // _LIBCPP_FUTURE
2059