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