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 <__config>
366
367#ifdef _LIBCPP_HAS_NO_THREADS
368#  error "<future> is not supported since libc++ has been configured without support for threads."
369#endif
370
371#include <__assert>
372#include <__availability>
373#include <__chrono/duration.h>
374#include <__chrono/time_point.h>
375#include <__exception/exception_ptr.h>
376#include <__memory/addressof.h>
377#include <__memory/allocator.h>
378#include <__memory/allocator_arg_t.h>
379#include <__memory/allocator_destructor.h>
380#include <__memory/allocator_traits.h>
381#include <__memory/compressed_pair.h>
382#include <__memory/pointer_traits.h>
383#include <__memory/shared_ptr.h>
384#include <__memory/unique_ptr.h>
385#include <__memory/uses_allocator.h>
386#include <__system_error/error_category.h>
387#include <__system_error/error_code.h>
388#include <__system_error/error_condition.h>
389#include <__type_traits/aligned_storage.h>
390#include <__type_traits/strip_signature.h>
391#include <__utility/auto_cast.h>
392#include <__utility/forward.h>
393#include <__utility/move.h>
394#include <mutex>
395#include <new>
396#include <stdexcept>
397#include <thread>
398#include <version>
399
400#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
401#  pragma GCC system_header
402#endif
403
404_LIBCPP_PUSH_MACROS
405#include <__undef_macros>
406
407_LIBCPP_BEGIN_NAMESPACE_STD
408
409// enum class future_errc
410_LIBCPP_DECLARE_STRONG_ENUM(future_errc){
411    future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};
412_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
413
414template <>
415struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
416
417#ifdef _LIBCPP_CXX03_LANG
418template <>
419struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type {};
420#endif
421
422// enum class launch
423_LIBCPP_DECLARE_STRONG_ENUM(launch){async = 1, deferred = 2, any = async | deferred};
424_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
425
426#ifndef _LIBCPP_CXX03_LANG
427
428typedef underlying_type<launch>::type __launch_underlying_type;
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, launch __y) {
439  return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));
440}
441
442inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator~(launch __x) {
443  return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
444}
445
446inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {
447  __x = __x & __y;
448  return __x;
449}
450
451inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {
452  __x = __x | __y;
453  return __x;
454}
455
456inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {
457  __x = __x ^ __y;
458  return __x;
459}
460
461#endif // !_LIBCPP_CXX03_LANG
462
463// enum class future_status
464_LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};
465_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
466
467_LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
468
469inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {
470  return error_code(static_cast<int>(__e), future_category());
471}
472
473inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {
474  return error_condition(static_cast<int>(__e), future_category());
475}
476
477_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);
478
479class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {
480  error_code __ec_;
481
482  future_error(error_code);
483  friend void __throw_future_error(future_errc);
484  template <class>
485  friend class promise;
486
487public:
488#if _LIBCPP_STD_VER >= 17
489  _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
490#endif
491
492  _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
493
494  _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
495  ~future_error() _NOEXCEPT override;
496};
497
498// Declared above std::future_error
499void __throw_future_error(future_errc __ev) {
500#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
501  throw future_error(make_error_code(__ev));
502#else
503  (void)__ev;
504  _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
505#endif
506}
507
508class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {
509protected:
510  exception_ptr __exception_;
511  mutable mutex __mut_;
512  mutable condition_variable __cv_;
513  unsigned __state_;
514
515  void __on_zero_shared() _NOEXCEPT override;
516  void __sub_wait(unique_lock<mutex>& __lk);
517
518public:
519  enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };
520
521  _LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}
522
523  _LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }
524
525  _LIBCPP_HIDE_FROM_ABI void __attach_future() {
526    lock_guard<mutex> __lk(__mut_);
527    bool __has_future_attached = (__state_ & __future_attached) != 0;
528    if (__has_future_attached)
529      __throw_future_error(future_errc::future_already_retrieved);
530    this->__add_shared();
531    __state_ |= __future_attached;
532  }
533
534  _LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }
535
536  void __make_ready();
537  _LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }
538
539  void set_value();
540  void set_value_at_thread_exit();
541
542  void set_exception(exception_ptr __p);
543  void set_exception_at_thread_exit(exception_ptr __p);
544
545  void copy();
546
547  void wait();
548  template <class _Rep, class _Period>
549  future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
550  template <class _Clock, class _Duration>
551  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS future_status
552  wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
553
554  virtual void __execute();
555};
556
557template <class _Clock, class _Duration>
558future_status __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
559  unique_lock<mutex> __lk(__mut_);
560  if (__state_ & deferred)
561    return future_status::deferred;
562  while (!(__state_ & ready) && _Clock::now() < __abs_time)
563    __cv_.wait_until(__lk, __abs_time);
564  if (__state_ & ready)
565    return future_status::ready;
566  return future_status::timeout;
567}
568
569template <class _Rep, class _Period>
570inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
571  return wait_until(chrono::steady_clock::now() + __rel_time);
572}
573
574template <class _Rp>
575class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {
576  typedef __assoc_sub_state base;
577  _LIBCPP_SUPPRESS_DEPRECATED_PUSH
578  typedef typename aligned_storage<sizeof(_Rp), _LIBCPP_ALIGNOF(_Rp)>::type _Up;
579  _LIBCPP_SUPPRESS_DEPRECATED_POP
580
581protected:
582  _Up __value_;
583
584  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
585
586public:
587  template <class _Arg>
588  _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
589
590  template <class _Arg>
591  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
592
593  _LIBCPP_HIDE_FROM_ABI _Rp move();
594  _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<_Rp> copy();
595};
596
597template <class _Rp>
598void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {
599  if (this->__state_ & base::__constructed)
600    reinterpret_cast<_Rp*>(&__value_)->~_Rp();
601  delete this;
602}
603
604template <class _Rp>
605template <class _Arg>
606void __assoc_state<_Rp>::set_value(_Arg&& __arg) {
607  unique_lock<mutex> __lk(this->__mut_);
608  if (this->__has_value())
609    __throw_future_error(future_errc::promise_already_satisfied);
610  ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
611  this->__state_ |= base::__constructed | base::ready;
612  __cv_.notify_all();
613}
614
615template <class _Rp>
616template <class _Arg>
617void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {
618  unique_lock<mutex> __lk(this->__mut_);
619  if (this->__has_value())
620    __throw_future_error(future_errc::promise_already_satisfied);
621  ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
622  this->__state_ |= base::__constructed;
623  __thread_local_data()->__make_ready_at_thread_exit(this);
624}
625
626template <class _Rp>
627_Rp __assoc_state<_Rp>::move() {
628  unique_lock<mutex> __lk(this->__mut_);
629  this->__sub_wait(__lk);
630  if (this->__exception_ != nullptr)
631    std::rethrow_exception(this->__exception_);
632  return std::move(*reinterpret_cast<_Rp*>(&__value_));
633}
634
635template <class _Rp>
636__add_lvalue_reference_t<_Rp> __assoc_state<_Rp>::copy() {
637  unique_lock<mutex> __lk(this->__mut_);
638  this->__sub_wait(__lk);
639  if (this->__exception_ != nullptr)
640    std::rethrow_exception(this->__exception_);
641  return *reinterpret_cast<_Rp*>(&__value_);
642}
643
644template <class _Rp>
645class __assoc_state<_Rp&> : public __assoc_sub_state {
646  typedef __assoc_sub_state base;
647  typedef _Rp* _Up;
648
649protected:
650  _Up __value_;
651
652  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
653
654public:
655  _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
656  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
657
658  _LIBCPP_HIDE_FROM_ABI _Rp& copy();
659};
660
661template <class _Rp>
662void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {
663  delete this;
664}
665
666template <class _Rp>
667void __assoc_state<_Rp&>::set_value(_Rp& __arg) {
668  unique_lock<mutex> __lk(this->__mut_);
669  if (this->__has_value())
670    __throw_future_error(future_errc::promise_already_satisfied);
671  __value_ = std::addressof(__arg);
672  this->__state_ |= base::__constructed | base::ready;
673  __cv_.notify_all();
674}
675
676template <class _Rp>
677void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {
678  unique_lock<mutex> __lk(this->__mut_);
679  if (this->__has_value())
680    __throw_future_error(future_errc::promise_already_satisfied);
681  __value_ = std::addressof(__arg);
682  this->__state_ |= base::__constructed;
683  __thread_local_data()->__make_ready_at_thread_exit(this);
684}
685
686template <class _Rp>
687_Rp& __assoc_state<_Rp&>::copy() {
688  unique_lock<mutex> __lk(this->__mut_);
689  this->__sub_wait(__lk);
690  if (this->__exception_ != nullptr)
691    std::rethrow_exception(this->__exception_);
692  return *__value_;
693}
694
695template <class _Rp, class _Alloc>
696class __assoc_state_alloc : public __assoc_state<_Rp> {
697  typedef __assoc_state<_Rp> base;
698  _Alloc __alloc_;
699
700  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
701
702public:
703  _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
704};
705
706template <class _Rp, class _Alloc>
707void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {
708  if (this->__state_ & base::__constructed)
709    reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();
710  typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
711  typedef allocator_traits<_Al> _ATraits;
712  typedef pointer_traits<typename _ATraits::pointer> _PTraits;
713  _Al __a(__alloc_);
714  this->~__assoc_state_alloc();
715  __a.deallocate(_PTraits::pointer_to(*this), 1);
716}
717
718template <class _Rp, class _Alloc>
719class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {
720  typedef __assoc_state<_Rp&> base;
721  _Alloc __alloc_;
722
723  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
724
725public:
726  _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
727};
728
729template <class _Rp, class _Alloc>
730void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {
731  typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
732  typedef allocator_traits<_Al> _ATraits;
733  typedef pointer_traits<typename _ATraits::pointer> _PTraits;
734  _Al __a(__alloc_);
735  this->~__assoc_state_alloc();
736  __a.deallocate(_PTraits::pointer_to(*this), 1);
737}
738
739template <class _Alloc>
740class __assoc_sub_state_alloc : public __assoc_sub_state {
741  typedef __assoc_sub_state base;
742  _Alloc __alloc_;
743
744  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
745
746public:
747  _LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
748};
749
750template <class _Alloc>
751void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {
752  typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
753  typedef allocator_traits<_Al> _ATraits;
754  typedef pointer_traits<typename _ATraits::pointer> _PTraits;
755  _Al __a(__alloc_);
756  this->~__assoc_sub_state_alloc();
757  __a.deallocate(_PTraits::pointer_to(*this), 1);
758}
759
760template <class _Rp, class _Fp>
761class __deferred_assoc_state : public __assoc_state<_Rp> {
762  typedef __assoc_state<_Rp> base;
763
764  _Fp __func_;
765
766public:
767  _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
768
769  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
770};
771
772template <class _Rp, class _Fp>
773inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
774  this->__set_deferred();
775}
776
777template <class _Rp, class _Fp>
778void __deferred_assoc_state<_Rp, _Fp>::__execute() {
779#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
780  try {
781#endif // _LIBCPP_HAS_NO_EXCEPTIONS
782    this->set_value(__func_());
783#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
784  } catch (...) {
785    this->set_exception(current_exception());
786  }
787#endif // _LIBCPP_HAS_NO_EXCEPTIONS
788}
789
790template <class _Fp>
791class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {
792  typedef __assoc_sub_state base;
793
794  _Fp __func_;
795
796public:
797  _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
798
799  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
800};
801
802template <class _Fp>
803inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
804  this->__set_deferred();
805}
806
807template <class _Fp>
808void __deferred_assoc_state<void, _Fp>::__execute() {
809#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
810  try {
811#endif // _LIBCPP_HAS_NO_EXCEPTIONS
812    __func_();
813    this->set_value();
814#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
815  } catch (...) {
816    this->set_exception(current_exception());
817  }
818#endif // _LIBCPP_HAS_NO_EXCEPTIONS
819}
820
821template <class _Rp, class _Fp>
822class __async_assoc_state : public __assoc_state<_Rp> {
823  typedef __assoc_state<_Rp> base;
824
825  _Fp __func_;
826
827  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
828
829public:
830  _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
831
832  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
833};
834
835template <class _Rp, class _Fp>
836inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
837
838template <class _Rp, class _Fp>
839void __async_assoc_state<_Rp, _Fp>::__execute() {
840#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
841  try {
842#endif // _LIBCPP_HAS_NO_EXCEPTIONS
843    this->set_value(__func_());
844#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
845  } catch (...) {
846    this->set_exception(current_exception());
847  }
848#endif // _LIBCPP_HAS_NO_EXCEPTIONS
849}
850
851template <class _Rp, class _Fp>
852void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {
853  this->wait();
854  base::__on_zero_shared();
855}
856
857template <class _Fp>
858class __async_assoc_state<void, _Fp> : public __assoc_sub_state {
859  typedef __assoc_sub_state base;
860
861  _Fp __func_;
862
863  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
864
865public:
866  _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
867
868  _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
869};
870
871template <class _Fp>
872inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
873
874template <class _Fp>
875void __async_assoc_state<void, _Fp>::__execute() {
876#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
877  try {
878#endif // _LIBCPP_HAS_NO_EXCEPTIONS
879    __func_();
880    this->set_value();
881#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
882  } catch (...) {
883    this->set_exception(current_exception());
884  }
885#endif // _LIBCPP_HAS_NO_EXCEPTIONS
886}
887
888template <class _Fp>
889void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {
890  this->wait();
891  base::__on_zero_shared();
892}
893
894template <class _Rp>
895class _LIBCPP_TEMPLATE_VIS promise;
896template <class _Rp>
897class _LIBCPP_TEMPLATE_VIS shared_future;
898
899// future
900
901template <class _Rp>
902class _LIBCPP_TEMPLATE_VIS future;
903
904template <class _Rp, class _Fp>
905_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);
906
907template <class _Rp, class _Fp>
908_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);
909
910template <class _Rp>
911class _LIBCPP_TEMPLATE_VIS future {
912  __assoc_state<_Rp>* __state_;
913
914  explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
915
916  template <class>
917  friend class promise;
918  template <class>
919  friend class shared_future;
920
921  template <class _R1, class _Fp>
922  friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
923  template <class _R1, class _Fp>
924  friend future<_R1> __make_async_assoc_state(_Fp&& __f);
925
926public:
927  _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
928  _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
929  future(const future&)            = delete;
930  future& operator=(const future&) = delete;
931  _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
932    future(std::move(__rhs)).swap(*this);
933    return *this;
934  }
935
936  _LIBCPP_HIDE_FROM_ABI ~future();
937  _LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;
938
939  // retrieving the value
940  _LIBCPP_HIDE_FROM_ABI _Rp get();
941
942  _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
943
944  // functions to check state
945  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
946
947  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
948  template <class _Rep, class _Period>
949  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
950    return __state_->wait_for(__rel_time);
951  }
952  template <class _Clock, class _Duration>
953  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
954    return __state_->wait_until(__abs_time);
955  }
956};
957
958template <class _Rp>
959future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {
960  __state_->__attach_future();
961}
962
963struct __release_shared_count {
964  _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }
965};
966
967template <class _Rp>
968future<_Rp>::~future() {
969  if (__state_)
970    __state_->__release_shared();
971}
972
973template <class _Rp>
974_Rp future<_Rp>::get() {
975  unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
976  __assoc_state<_Rp>* __s = __state_;
977  __state_                = nullptr;
978  return __s->move();
979}
980
981template <class _Rp>
982class _LIBCPP_TEMPLATE_VIS future<_Rp&> {
983  __assoc_state<_Rp&>* __state_;
984
985  explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
986
987  template <class>
988  friend class promise;
989  template <class>
990  friend class shared_future;
991
992  template <class _R1, class _Fp>
993  friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
994  template <class _R1, class _Fp>
995  friend future<_R1> __make_async_assoc_state(_Fp&& __f);
996
997public:
998  _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
999  _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1000  future(const future&)            = delete;
1001  future& operator=(const future&) = delete;
1002  _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1003    future(std::move(__rhs)).swap(*this);
1004    return *this;
1005  }
1006
1007  _LIBCPP_HIDE_FROM_ABI ~future();
1008  _LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;
1009
1010  // retrieving the value
1011  _LIBCPP_HIDE_FROM_ABI _Rp& get();
1012
1013  _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1014
1015  // functions to check state
1016  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1017
1018  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1019  template <class _Rep, class _Period>
1020  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1021    return __state_->wait_for(__rel_time);
1022  }
1023  template <class _Clock, class _Duration>
1024  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1025    return __state_->wait_until(__abs_time);
1026  }
1027};
1028
1029template <class _Rp>
1030future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {
1031  __state_->__attach_future();
1032}
1033
1034template <class _Rp>
1035future<_Rp&>::~future() {
1036  if (__state_)
1037    __state_->__release_shared();
1038}
1039
1040template <class _Rp>
1041_Rp& future<_Rp&>::get() {
1042  unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1043  __assoc_state<_Rp&>* __s = __state_;
1044  __state_                 = nullptr;
1045  return __s->copy();
1046}
1047
1048template <>
1049class _LIBCPP_EXPORTED_FROM_ABI future<void> {
1050  __assoc_sub_state* __state_;
1051
1052  explicit future(__assoc_sub_state* __state);
1053
1054  template <class>
1055  friend class promise;
1056  template <class>
1057  friend class shared_future;
1058
1059  template <class _R1, class _Fp>
1060  friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1061  template <class _R1, class _Fp>
1062  friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1063
1064public:
1065  _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1066  _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1067  future(const future&)            = delete;
1068  future& operator=(const future&) = delete;
1069  _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1070    future(std::move(__rhs)).swap(*this);
1071    return *this;
1072  }
1073
1074  ~future();
1075  _LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;
1076
1077  // retrieving the value
1078  void get();
1079
1080  _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1081
1082  // functions to check state
1083  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1084
1085  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1086  template <class _Rep, class _Period>
1087  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1088    return __state_->wait_for(__rel_time);
1089  }
1090  template <class _Clock, class _Duration>
1091  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1092    return __state_->wait_until(__abs_time);
1093  }
1094};
1095
1096template <class _Rp>
1097inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {
1098  __x.swap(__y);
1099}
1100
1101// promise<R>
1102
1103template <class _Callable>
1104class packaged_task;
1105
1106template <class _Rp>
1107class _LIBCPP_TEMPLATE_VIS promise {
1108  __assoc_state<_Rp>* __state_;
1109
1110  _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1111
1112  template <class>
1113  friend class packaged_task;
1114
1115public:
1116  _LIBCPP_HIDE_FROM_ABI promise();
1117  template <class _Alloc>
1118  _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1119  _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1120  promise(const promise& __rhs) = delete;
1121  _LIBCPP_HIDE_FROM_ABI ~promise();
1122
1123  // assignment
1124  _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1125    promise(std::move(__rhs)).swap(*this);
1126    return *this;
1127  }
1128  promise& operator=(const promise& __rhs) = delete;
1129
1130  _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1131
1132  // retrieving the result
1133  _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1134
1135  // setting the result
1136  _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1137  _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1138  _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1139
1140  // setting the result with deferred notification
1141  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1142  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1143  _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1144};
1145
1146template <class _Rp>
1147promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}
1148
1149template <class _Rp>
1150template <class _Alloc>
1151promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {
1152  typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1153  typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1154  typedef __allocator_destructor<_A2> _D2;
1155  _A2 __a(__a0);
1156  unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1157  ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1158  __state_ = std::addressof(*__hold.release());
1159}
1160
1161template <class _Rp>
1162promise<_Rp>::~promise() {
1163  if (__state_) {
1164    if (!__state_->__has_value() && __state_->use_count() > 1)
1165      __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1166    __state_->__release_shared();
1167  }
1168}
1169
1170template <class _Rp>
1171future<_Rp> promise<_Rp>::get_future() {
1172  if (__state_ == nullptr)
1173    __throw_future_error(future_errc::no_state);
1174  return future<_Rp>(__state_);
1175}
1176
1177template <class _Rp>
1178void promise<_Rp>::set_value(const _Rp& __r) {
1179  if (__state_ == nullptr)
1180    __throw_future_error(future_errc::no_state);
1181  __state_->set_value(__r);
1182}
1183
1184template <class _Rp>
1185void promise<_Rp>::set_value(_Rp&& __r) {
1186  if (__state_ == nullptr)
1187    __throw_future_error(future_errc::no_state);
1188  __state_->set_value(std::move(__r));
1189}
1190
1191template <class _Rp>
1192void promise<_Rp>::set_exception(exception_ptr __p) {
1193  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1194  if (__state_ == nullptr)
1195    __throw_future_error(future_errc::no_state);
1196  __state_->set_exception(__p);
1197}
1198
1199template <class _Rp>
1200void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {
1201  if (__state_ == nullptr)
1202    __throw_future_error(future_errc::no_state);
1203  __state_->set_value_at_thread_exit(__r);
1204}
1205
1206template <class _Rp>
1207void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {
1208  if (__state_ == nullptr)
1209    __throw_future_error(future_errc::no_state);
1210  __state_->set_value_at_thread_exit(std::move(__r));
1211}
1212
1213template <class _Rp>
1214void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {
1215  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1216  if (__state_ == nullptr)
1217    __throw_future_error(future_errc::no_state);
1218  __state_->set_exception_at_thread_exit(__p);
1219}
1220
1221// promise<R&>
1222
1223template <class _Rp>
1224class _LIBCPP_TEMPLATE_VIS promise<_Rp&> {
1225  __assoc_state<_Rp&>* __state_;
1226
1227  _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1228
1229  template <class>
1230  friend class packaged_task;
1231
1232public:
1233  _LIBCPP_HIDE_FROM_ABI promise();
1234  template <class _Allocator>
1235  _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1236  _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1237  promise(const promise& __rhs) = delete;
1238  _LIBCPP_HIDE_FROM_ABI ~promise();
1239
1240  // assignment
1241  _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1242    promise(std::move(__rhs)).swap(*this);
1243    return *this;
1244  }
1245  promise& operator=(const promise& __rhs) = delete;
1246
1247  _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1248
1249  // retrieving the result
1250  _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1251
1252  // setting the result
1253  _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1254  _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1255
1256  // setting the result with deferred notification
1257  _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1258  _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1259};
1260
1261template <class _Rp>
1262promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}
1263
1264template <class _Rp>
1265template <class _Alloc>
1266promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {
1267  typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1268  typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1269  typedef __allocator_destructor<_A2> _D2;
1270  _A2 __a(__a0);
1271  unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1272  ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1273  __state_ = std::addressof(*__hold.release());
1274}
1275
1276template <class _Rp>
1277promise<_Rp&>::~promise() {
1278  if (__state_) {
1279    if (!__state_->__has_value() && __state_->use_count() > 1)
1280      __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1281    __state_->__release_shared();
1282  }
1283}
1284
1285template <class _Rp>
1286future<_Rp&> promise<_Rp&>::get_future() {
1287  if (__state_ == nullptr)
1288    __throw_future_error(future_errc::no_state);
1289  return future<_Rp&>(__state_);
1290}
1291
1292template <class _Rp>
1293void promise<_Rp&>::set_value(_Rp& __r) {
1294  if (__state_ == nullptr)
1295    __throw_future_error(future_errc::no_state);
1296  __state_->set_value(__r);
1297}
1298
1299template <class _Rp>
1300void promise<_Rp&>::set_exception(exception_ptr __p) {
1301  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1302  if (__state_ == nullptr)
1303    __throw_future_error(future_errc::no_state);
1304  __state_->set_exception(__p);
1305}
1306
1307template <class _Rp>
1308void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {
1309  if (__state_ == nullptr)
1310    __throw_future_error(future_errc::no_state);
1311  __state_->set_value_at_thread_exit(__r);
1312}
1313
1314template <class _Rp>
1315void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {
1316  _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1317  if (__state_ == nullptr)
1318    __throw_future_error(future_errc::no_state);
1319  __state_->set_exception_at_thread_exit(__p);
1320}
1321
1322// promise<void>
1323
1324template <>
1325class _LIBCPP_EXPORTED_FROM_ABI promise<void> {
1326  __assoc_sub_state* __state_;
1327
1328  _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1329
1330  template <class>
1331  friend class packaged_task;
1332
1333public:
1334  promise();
1335  template <class _Allocator>
1336  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS promise(allocator_arg_t, const _Allocator& __a);
1337  _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1338  promise(const promise& __rhs) = delete;
1339  ~promise();
1340
1341  // assignment
1342  _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1343    promise(std::move(__rhs)).swap(*this);
1344    return *this;
1345  }
1346  promise& operator=(const promise& __rhs) = delete;
1347
1348  _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1349
1350  // retrieving the result
1351  future<void> get_future();
1352
1353  // setting the result
1354  void set_value();
1355  void set_exception(exception_ptr __p);
1356
1357  // setting the result with deferred notification
1358  void set_value_at_thread_exit();
1359  void set_exception_at_thread_exit(exception_ptr __p);
1360};
1361
1362template <class _Alloc>
1363promise<void>::promise(allocator_arg_t, const _Alloc& __a0) {
1364  typedef __assoc_sub_state_alloc<_Alloc> _State;
1365  typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1366  typedef __allocator_destructor<_A2> _D2;
1367  _A2 __a(__a0);
1368  unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1369  ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1370  __state_ = std::addressof(*__hold.release());
1371}
1372
1373template <class _Rp>
1374inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {
1375  __x.swap(__y);
1376}
1377
1378template <class _Rp, class _Alloc>
1379struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc> : public true_type {};
1380
1381// packaged_task
1382
1383template <class _Fp>
1384class __packaged_task_base;
1385
1386template <class _Rp, class... _ArgTypes>
1387class __packaged_task_base<_Rp(_ArgTypes...)> {
1388  __packaged_task_base(const __packaged_task_base&);
1389  __packaged_task_base& operator=(const __packaged_task_base&);
1390
1391public:
1392  _LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}
1393  _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1394  virtual ~__packaged_task_base() {}
1395  virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1396  virtual void destroy()                                  = 0;
1397  virtual void destroy_deallocate()                       = 0;
1398  virtual _Rp operator()(_ArgTypes&&...)                  = 0;
1399};
1400
1401template <class _FD, class _Alloc, class _FB>
1402class __packaged_task_func;
1403
1404template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1405class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
1406  __compressed_pair<_Fp, _Alloc> __f_;
1407
1408public:
1409  _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
1410  _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __f_(std::move(__f), __default_init_tag()) {}
1411  _LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __f_(__f, __a) {}
1412  _LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __f_(std::move(__f), __a) {}
1413  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1414  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1415  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1416  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);
1417};
1418
1419template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1420void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1421    __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {
1422  ::new ((void*)__p) __packaged_task_func(std::move(__f_.first()), std::move(__f_.second()));
1423}
1424
1425template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1426void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
1427  __f_.~__compressed_pair<_Fp, _Alloc>();
1428}
1429
1430template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1431void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {
1432  typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1433  typedef allocator_traits<_Ap> _ATraits;
1434  typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1435  _Ap __a(__f_.second());
1436  __f_.~__compressed_pair<_Fp, _Alloc>();
1437  __a.deallocate(_PTraits::pointer_to(*this), 1);
1438}
1439
1440template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1441_Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
1442  return std::__invoke(__f_.first(), std::forward<_ArgTypes>(__arg)...);
1443}
1444
1445template <class _Callable>
1446class __packaged_task_function;
1447
1448template <class _Rp, class... _ArgTypes>
1449class __packaged_task_function<_Rp(_ArgTypes...)> {
1450  typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1451
1452  _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }
1453
1454  _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1455  typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1456  _LIBCPP_SUPPRESS_DEPRECATED_POP
1457  __base* __f_;
1458
1459public:
1460  typedef _Rp result_type;
1461
1462  // construct/copy/destroy:
1463  _LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1464  template <class _Fp>
1465  _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1466  template <class _Fp, class _Alloc>
1467  _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1468
1469  _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1470  _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1471
1472  __packaged_task_function(const __packaged_task_function&)            = delete;
1473  __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1474
1475  _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1476
1477  _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1478
1479  _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1480};
1481
1482template <class _Rp, class... _ArgTypes>
1483__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {
1484  if (__f.__f_ == nullptr)
1485    __f_ = nullptr;
1486  else if (__f.__f_ == __f.__get_buf()) {
1487    __f.__f_->__move_to(__get_buf());
1488    __f_ = (__base*)&__buf_;
1489  } else {
1490    __f_     = __f.__f_;
1491    __f.__f_ = nullptr;
1492  }
1493}
1494
1495template <class _Rp, class... _ArgTypes>
1496template <class _Fp>
1497__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {
1498  typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1499  typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1500  if (sizeof(_FF) <= sizeof(__buf_)) {
1501    ::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));
1502    __f_ = (__base*)&__buf_;
1503  } else {
1504    typedef allocator<_FF> _Ap;
1505    _Ap __a;
1506    typedef __allocator_destructor<_Ap> _Dp;
1507    unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1508    ::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));
1509    __f_ = __hold.release();
1510  }
1511}
1512
1513template <class _Rp, class... _ArgTypes>
1514template <class _Fp, class _Alloc>
1515__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1516    : __f_(nullptr) {
1517  typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1518  typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1519  if (sizeof(_FF) <= sizeof(__buf_)) {
1520    __f_ = (__base*)&__buf_;
1521    ::new ((void*)__f_) _FF(std::forward<_Fp>(__f));
1522  } else {
1523    typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1524    _Ap __a(__a0);
1525    typedef __allocator_destructor<_Ap> _Dp;
1526    unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1527    ::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));
1528    __f_ = std::addressof(*__hold.release());
1529  }
1530}
1531
1532template <class _Rp, class... _ArgTypes>
1533__packaged_task_function<_Rp(_ArgTypes...)>&
1534__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {
1535  if (__f_ == __get_buf())
1536    __f_->destroy();
1537  else if (__f_)
1538    __f_->destroy_deallocate();
1539  __f_ = nullptr;
1540  if (__f.__f_ == nullptr)
1541    __f_ = nullptr;
1542  else if (__f.__f_ == __f.__get_buf()) {
1543    __f.__f_->__move_to(__get_buf());
1544    __f_ = __get_buf();
1545  } else {
1546    __f_     = __f.__f_;
1547    __f.__f_ = nullptr;
1548  }
1549  return *this;
1550}
1551
1552template <class _Rp, class... _ArgTypes>
1553__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {
1554  if (__f_ == __get_buf())
1555    __f_->destroy();
1556  else if (__f_)
1557    __f_->destroy_deallocate();
1558}
1559
1560template <class _Rp, class... _ArgTypes>
1561_LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {
1562  if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {
1563    _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1564    typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1565    _LIBCPP_SUPPRESS_DEPRECATED_POP
1566    __base* __t = (__base*)&__tempbuf;
1567    __f_->__move_to(__t);
1568    __f_->destroy();
1569    __f_ = nullptr;
1570    __f.__f_->__move_to((__base*)&__buf_);
1571    __f.__f_->destroy();
1572    __f.__f_ = nullptr;
1573    __f_     = (__base*)&__buf_;
1574    __t->__move_to((__base*)&__f.__buf_);
1575    __t->destroy();
1576    __f.__f_ = (__base*)&__f.__buf_;
1577  } else if (__f_ == (__base*)&__buf_) {
1578    __f_->__move_to((__base*)&__f.__buf_);
1579    __f_->destroy();
1580    __f_     = __f.__f_;
1581    __f.__f_ = (__base*)&__f.__buf_;
1582  } else if (__f.__f_ == (__base*)&__f.__buf_) {
1583    __f.__f_->__move_to((__base*)&__buf_);
1584    __f.__f_->destroy();
1585    __f.__f_ = __f_;
1586    __f_     = (__base*)&__buf_;
1587  } else
1588    std::swap(__f_, __f.__f_);
1589}
1590
1591template <class _Rp, class... _ArgTypes>
1592inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
1593  return (*__f_)(std::forward<_ArgTypes>(__arg)...);
1594}
1595
1596template <class _Rp, class... _ArgTypes>
1597class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)> {
1598public:
1599  typedef _Rp result_type; // extension
1600
1601private:
1602  __packaged_task_function<result_type(_ArgTypes...)> __f_;
1603  promise<result_type> __p_;
1604
1605public:
1606  // construction and destruction
1607  _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1608
1609  template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1610  _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1611
1612  template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1613  _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1614      : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1615  // ~packaged_task() = default;
1616
1617  // no copy
1618  packaged_task(const packaged_task&)            = delete;
1619  packaged_task& operator=(const packaged_task&) = delete;
1620
1621  // move support
1622  _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1623      : __f_(std::move(__other.__f_)),
1624        __p_(std::move(__other.__p_)) {}
1625  _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1626    __f_ = std::move(__other.__f_);
1627    __p_ = std::move(__other.__p_);
1628    return *this;
1629  }
1630  _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1631    __f_.swap(__other.__f_);
1632    __p_.swap(__other.__p_);
1633  }
1634
1635  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1636
1637  // result retrieval
1638  _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1639
1640  // execution
1641  _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1642  _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1643
1644  _LIBCPP_HIDE_FROM_ABI void reset();
1645};
1646
1647template <class _Rp, class... _ArgTypes>
1648void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1649  if (__p_.__state_ == nullptr)
1650    __throw_future_error(future_errc::no_state);
1651  if (__p_.__state_->__has_value())
1652    __throw_future_error(future_errc::promise_already_satisfied);
1653#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1654  try {
1655#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1656    __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
1657#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1658  } catch (...) {
1659    __p_.set_exception(current_exception());
1660  }
1661#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1662}
1663
1664template <class _Rp, class... _ArgTypes>
1665void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1666  if (__p_.__state_ == nullptr)
1667    __throw_future_error(future_errc::no_state);
1668  if (__p_.__state_->__has_value())
1669    __throw_future_error(future_errc::promise_already_satisfied);
1670#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1671  try {
1672#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1673    __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
1674#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1675  } catch (...) {
1676    __p_.set_exception_at_thread_exit(current_exception());
1677  }
1678#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1679}
1680
1681template <class _Rp, class... _ArgTypes>
1682void packaged_task<_Rp(_ArgTypes...)>::reset() {
1683  if (!valid())
1684    __throw_future_error(future_errc::no_state);
1685  __p_ = promise<result_type>();
1686}
1687
1688template <class... _ArgTypes>
1689class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)> {
1690public:
1691  typedef void result_type; // extension
1692
1693private:
1694  __packaged_task_function<result_type(_ArgTypes...)> __f_;
1695  promise<result_type> __p_;
1696
1697public:
1698  // construction and destruction
1699  _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1700  template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1701  _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1702  template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1703  _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1704      : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1705  // ~packaged_task() = default;
1706
1707  // no copy
1708  packaged_task(const packaged_task&)            = delete;
1709  packaged_task& operator=(const packaged_task&) = delete;
1710
1711  // move support
1712  _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1713      : __f_(std::move(__other.__f_)),
1714        __p_(std::move(__other.__p_)) {}
1715  _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1716    __f_ = std::move(__other.__f_);
1717    __p_ = std::move(__other.__p_);
1718    return *this;
1719  }
1720  _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1721    __f_.swap(__other.__f_);
1722    __p_.swap(__other.__p_);
1723  }
1724
1725  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1726
1727  // result retrieval
1728  _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1729
1730  // execution
1731  _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1732  _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1733
1734  _LIBCPP_HIDE_FROM_ABI void reset();
1735};
1736
1737#if _LIBCPP_STD_VER >= 17
1738
1739template <class _Rp, class... _Args>
1740packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
1741
1742template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1743packaged_task(_Fp) -> packaged_task<_Stripped>;
1744
1745#endif
1746
1747template <class... _ArgTypes>
1748void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1749  if (__p_.__state_ == nullptr)
1750    __throw_future_error(future_errc::no_state);
1751  if (__p_.__state_->__has_value())
1752    __throw_future_error(future_errc::promise_already_satisfied);
1753#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1754  try {
1755#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1756    __f_(std::forward<_ArgTypes>(__args)...);
1757    __p_.set_value();
1758#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1759  } catch (...) {
1760    __p_.set_exception(current_exception());
1761  }
1762#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1763}
1764
1765template <class... _ArgTypes>
1766void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1767  if (__p_.__state_ == nullptr)
1768    __throw_future_error(future_errc::no_state);
1769  if (__p_.__state_->__has_value())
1770    __throw_future_error(future_errc::promise_already_satisfied);
1771#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1772  try {
1773#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1774    __f_(std::forward<_ArgTypes>(__args)...);
1775    __p_.set_value_at_thread_exit();
1776#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1777  } catch (...) {
1778    __p_.set_exception_at_thread_exit(current_exception());
1779  }
1780#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1781}
1782
1783template <class... _ArgTypes>
1784void packaged_task<void(_ArgTypes...)>::reset() {
1785  if (!valid())
1786    __throw_future_error(future_errc::no_state);
1787  __p_ = promise<result_type>();
1788}
1789
1790template <class _Rp, class... _ArgTypes>
1791inline _LIBCPP_HIDE_FROM_ABI void
1792swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1793  __x.swap(__y);
1794}
1795
1796template <class _Callable, class _Alloc>
1797struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
1798
1799template <class _Rp, class _Fp>
1800_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
1801  unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1802      new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1803  return future<_Rp>(__h.get());
1804}
1805
1806template <class _Rp, class _Fp>
1807_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
1808  unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1809      new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1810  std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
1811  return future<_Rp>(__h.get());
1812}
1813
1814#ifndef _LIBCPP_CXX03_LANG
1815
1816template <class _Fp, class... _Args>
1817class _LIBCPP_HIDDEN __async_func {
1818  tuple<_Fp, _Args...> __f_;
1819
1820public:
1821  typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
1822
1823  _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Fp&& __f, _Args&&... __args)
1824      : __f_(std::move(__f), std::move(__args)...) {}
1825
1826  _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
1827
1828  _LIBCPP_HIDE_FROM_ABI _Rp operator()() {
1829    typedef typename __make_tuple_indices<1 + sizeof...(_Args), 1>::type _Index;
1830    return __execute(_Index());
1831  }
1832
1833private:
1834  template <size_t... _Indices>
1835  _LIBCPP_HIDE_FROM_ABI _Rp __execute(__tuple_indices<_Indices...>) {
1836    return std::__invoke(std::move(std::get<0>(__f_)), std::move(std::get<_Indices>(__f_))...);
1837  }
1838};
1839
1840inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
1841  return (int(__policy) & int(__value)) != 0;
1842}
1843
1844template <class _Fp, class... _Args>
1845_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
1846    future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1847    async(launch __policy, _Fp&& __f, _Args&&... __args) {
1848  typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
1849  typedef typename _BF::_Rp _Rp;
1850
1851#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1852  try {
1853#  endif
1854    if (__does_policy_contain(__policy, launch::async))
1855      return std::__make_async_assoc_state<_Rp>(
1856          _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1857#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1858  } catch (...) {
1859    if (__policy == launch::async)
1860      throw;
1861  }
1862#  endif
1863
1864  if (__does_policy_contain(__policy, launch::deferred))
1865    return std::__make_deferred_assoc_state<_Rp>(
1866        _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1867  return future<_Rp>{};
1868}
1869
1870template <class _Fp, class... _Args>
1871_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
1872    future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1873    async(_Fp&& __f, _Args&&... __args) {
1874  return std::async(launch::any, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);
1875}
1876
1877#endif // C++03
1878
1879// shared_future
1880
1881template <class _Rp>
1882class _LIBCPP_TEMPLATE_VIS shared_future {
1883  __assoc_state<_Rp>* __state_;
1884
1885public:
1886  _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1887  _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1888    if (__state_)
1889      __state_->__add_shared();
1890  }
1891  _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1892  _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1893    __rhs.__state_ = nullptr;
1894  }
1895  _LIBCPP_HIDE_FROM_ABI ~shared_future();
1896  _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
1897  _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1898    shared_future(std::move(__rhs)).swap(*this);
1899    return *this;
1900  }
1901
1902  // retrieving the value
1903  _LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }
1904
1905  _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1906
1907  // functions to check state
1908  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1909
1910  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1911  template <class _Rep, class _Period>
1912  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1913    return __state_->wait_for(__rel_time);
1914  }
1915  template <class _Clock, class _Duration>
1916  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1917    return __state_->wait_until(__abs_time);
1918  }
1919};
1920
1921template <class _Rp>
1922shared_future<_Rp>::~shared_future() {
1923  if (__state_)
1924    __state_->__release_shared();
1925}
1926
1927template <class _Rp>
1928shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {
1929  if (__rhs.__state_)
1930    __rhs.__state_->__add_shared();
1931  if (__state_)
1932    __state_->__release_shared();
1933  __state_ = __rhs.__state_;
1934  return *this;
1935}
1936
1937template <class _Rp>
1938class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&> {
1939  __assoc_state<_Rp&>* __state_;
1940
1941public:
1942  _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1943  _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
1944    if (__state_)
1945      __state_->__add_shared();
1946  }
1947  _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1948  _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1949    __rhs.__state_ = nullptr;
1950  }
1951  _LIBCPP_HIDE_FROM_ABI ~shared_future();
1952  _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
1953  _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1954    shared_future(std::move(__rhs)).swap(*this);
1955    return *this;
1956  }
1957
1958  // retrieving the value
1959  _LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }
1960
1961  _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1962
1963  // functions to check state
1964  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1965
1966  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1967  template <class _Rep, class _Period>
1968  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1969    return __state_->wait_for(__rel_time);
1970  }
1971  template <class _Clock, class _Duration>
1972  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1973    return __state_->wait_until(__abs_time);
1974  }
1975};
1976
1977template <class _Rp>
1978shared_future<_Rp&>::~shared_future() {
1979  if (__state_)
1980    __state_->__release_shared();
1981}
1982
1983template <class _Rp>
1984shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {
1985  if (__rhs.__state_)
1986    __rhs.__state_->__add_shared();
1987  if (__state_)
1988    __state_->__release_shared();
1989  __state_ = __rhs.__state_;
1990  return *this;
1991}
1992
1993template <>
1994class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
1995  __assoc_sub_state* __state_;
1996
1997public:
1998  _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1999  _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
2000    if (__state_)
2001      __state_->__add_shared();
2002  }
2003  _LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
2004  _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
2005    __rhs.__state_ = nullptr;
2006  }
2007  ~shared_future();
2008  shared_future& operator=(const shared_future& __rhs);
2009  _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
2010    shared_future(std::move(__rhs)).swap(*this);
2011    return *this;
2012  }
2013
2014  // retrieving the value
2015  _LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }
2016
2017  _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
2018
2019  // functions to check state
2020  _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
2021
2022  _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
2023  template <class _Rep, class _Period>
2024  _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
2025    return __state_->wait_for(__rel_time);
2026  }
2027  template <class _Clock, class _Duration>
2028  _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
2029    return __state_->wait_until(__abs_time);
2030  }
2031};
2032
2033template <class _Rp>
2034inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
2035  __x.swap(__y);
2036}
2037
2038template <class _Rp>
2039inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
2040  return shared_future<_Rp>(std::move(*this));
2041}
2042
2043template <class _Rp>
2044inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
2045  return shared_future<_Rp&>(std::move(*this));
2046}
2047
2048inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
2049
2050_LIBCPP_END_NAMESPACE_STD
2051
2052_LIBCPP_POP_MACROS
2053
2054#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2055#  include <chrono>
2056#endif
2057
2058#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2059#  include <atomic>
2060#  include <cstdlib>
2061#  include <exception>
2062#  include <iosfwd>
2063#  include <system_error>
2064#endif
2065
2066#endif // _LIBCPP_FUTURE
2067