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