• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
48    : public logic_error
49{
50public:
51    future_error(error_code ec);  // exposition only
52    explicit future_error(future_errc); // C++17
53    const error_code& code() const noexcept;
54    const char*       what() const noexcept;
55};
56
57template <class R>
58class promise
59{
60public:
61    promise();
62    template <class Allocator>
63        promise(allocator_arg_t, const Allocator& a);
64    promise(promise&& rhs) noexcept;
65    promise(const promise& rhs) = delete;
66    ~promise();
67
68    // assignment
69    promise& operator=(promise&& rhs) noexcept;
70    promise& operator=(const promise& rhs) = delete;
71    void swap(promise& other) noexcept;
72
73    // retrieving the result
74    future<R> get_future();
75
76    // setting the result
77    void set_value(const R& r);
78    void set_value(R&& r);
79    void set_exception(exception_ptr p);
80
81    // setting the result with deferred notification
82    void set_value_at_thread_exit(const R& r);
83    void set_value_at_thread_exit(R&& r);
84    void set_exception_at_thread_exit(exception_ptr p);
85};
86
87template <class R>
88class promise<R&>
89{
90public:
91    promise();
92    template <class Allocator>
93        promise(allocator_arg_t, const Allocator& a);
94    promise(promise&& rhs) noexcept;
95    promise(const promise& rhs) = delete;
96    ~promise();
97
98    // assignment
99    promise& operator=(promise&& rhs) noexcept;
100    promise& operator=(const promise& rhs) = delete;
101    void swap(promise& other) noexcept;
102
103    // retrieving the result
104    future<R&> get_future();
105
106    // setting the result
107    void set_value(R& r);
108    void set_exception(exception_ptr p);
109
110    // setting the result with deferred notification
111    void set_value_at_thread_exit(R&);
112    void set_exception_at_thread_exit(exception_ptr p);
113};
114
115template <>
116class promise<void>
117{
118public:
119    promise();
120    template <class Allocator>
121        promise(allocator_arg_t, const Allocator& a);
122    promise(promise&& rhs) noexcept;
123    promise(const promise& rhs) = delete;
124    ~promise();
125
126    // assignment
127    promise& operator=(promise&& rhs) noexcept;
128    promise& operator=(const promise& rhs) = delete;
129    void swap(promise& other) noexcept;
130
131    // retrieving the result
132    future<void> get_future();
133
134    // setting the result
135    void set_value();
136    void set_exception(exception_ptr p);
137
138    // setting the result with deferred notification
139    void set_value_at_thread_exit();
140    void set_exception_at_thread_exit(exception_ptr p);
141};
142
143template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
144
145template <class R, class Alloc>
146    struct uses_allocator<promise<R>, Alloc> : public true_type {};
147
148template <class R>
149class future
150{
151public:
152    future() noexcept;
153    future(future&&) noexcept;
154    future(const future& rhs) = delete;
155    ~future();
156    future& operator=(const future& rhs) = delete;
157    future& operator=(future&&) noexcept;
158    shared_future<R> share() noexcept;
159
160    // retrieving the value
161    R get();
162
163    // functions to check state
164    bool valid() const noexcept;
165
166    void wait() const;
167    template <class Rep, class Period>
168        future_status
169        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
170    template <class Clock, class Duration>
171        future_status
172        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
173};
174
175template <class R>
176class future<R&>
177{
178public:
179    future() noexcept;
180    future(future&&) noexcept;
181    future(const future& rhs) = delete;
182    ~future();
183    future& operator=(const future& rhs) = delete;
184    future& operator=(future&&) noexcept;
185    shared_future<R&> share() noexcept;
186
187    // retrieving the value
188    R& get();
189
190    // functions to check state
191    bool valid() const noexcept;
192
193    void wait() const;
194    template <class Rep, class Period>
195        future_status
196        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
197    template <class Clock, class Duration>
198        future_status
199        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
200};
201
202template <>
203class future<void>
204{
205public:
206    future() noexcept;
207    future(future&&) noexcept;
208    future(const future& rhs) = delete;
209    ~future();
210    future& operator=(const future& rhs) = delete;
211    future& operator=(future&&) noexcept;
212    shared_future<void> share() noexcept;
213
214    // retrieving the value
215    void get();
216
217    // functions to check state
218    bool valid() const noexcept;
219
220    void wait() const;
221    template <class Rep, class Period>
222        future_status
223        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
224    template <class Clock, class Duration>
225        future_status
226        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
227};
228
229template <class R>
230class shared_future
231{
232public:
233    shared_future() noexcept;
234    shared_future(const shared_future& rhs);
235    shared_future(future<R>&&) noexcept;
236    shared_future(shared_future&& rhs) noexcept;
237    ~shared_future();
238    shared_future& operator=(const shared_future& rhs);
239    shared_future& operator=(shared_future&& rhs) noexcept;
240
241    // retrieving the value
242    const R& get() const;
243
244    // functions to check state
245    bool valid() const noexcept;
246
247    void wait() const;
248    template <class Rep, class Period>
249        future_status
250        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
251    template <class Clock, class Duration>
252        future_status
253        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
254};
255
256template <class R>
257class shared_future<R&>
258{
259public:
260    shared_future() noexcept;
261    shared_future(const shared_future& rhs);
262    shared_future(future<R&>&&) noexcept;
263    shared_future(shared_future&& rhs) noexcept;
264    ~shared_future();
265    shared_future& operator=(const shared_future& rhs);
266    shared_future& operator=(shared_future&& rhs) noexcept;
267
268    // retrieving the value
269    R& get() const;
270
271    // functions to check state
272    bool valid() const noexcept;
273
274    void wait() const;
275    template <class Rep, class Period>
276        future_status
277        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
278    template <class Clock, class Duration>
279        future_status
280        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
281};
282
283template <>
284class shared_future<void>
285{
286public:
287    shared_future() noexcept;
288    shared_future(const shared_future& rhs);
289    shared_future(future<void>&&) noexcept;
290    shared_future(shared_future&& rhs) noexcept;
291    ~shared_future();
292    shared_future& operator=(const shared_future& rhs);
293    shared_future& operator=(shared_future&& rhs) noexcept;
294
295    // retrieving the value
296    void get() const;
297
298    // functions to check state
299    bool valid() const noexcept;
300
301    void wait() const;
302    template <class Rep, class Period>
303        future_status
304        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
305    template <class Clock, class Duration>
306        future_status
307        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
308};
309
310template <class F, class... Args>
311  future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
312  async(F&& f, Args&&... args);
313
314template <class F, class... Args>
315  future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
316  async(launch policy, F&& f, Args&&... args);
317
318template <class> class packaged_task; // undefined
319
320template <class R, class... ArgTypes>
321class packaged_task<R(ArgTypes...)>
322{
323public:
324    typedef R result_type; // extension
325
326    // construction and destruction
327    packaged_task() noexcept;
328    template <class F>
329        explicit packaged_task(F&& f);
330    template <class F, class Allocator>
331        packaged_task(allocator_arg_t, const Allocator& a, F&& f);
332    ~packaged_task();
333
334    // no copy
335    packaged_task(const packaged_task&) = delete;
336    packaged_task& operator=(const packaged_task&) = delete;
337
338    // move support
339    packaged_task(packaged_task&& other) noexcept;
340    packaged_task& operator=(packaged_task&& other) noexcept;
341    void swap(packaged_task& other) noexcept;
342
343    bool valid() const noexcept;
344
345    // result retrieval
346    future<R> get_future();
347
348    // execution
349    void operator()(ArgTypes... );
350    void make_ready_at_thread_exit(ArgTypes...);
351
352    void reset();
353};
354
355template <class R>
356  void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
357
358template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
359
360}  // std
361
362*/
363
364#include <__assert> // all public C++ headers provide the assertion handler
365#include <__availability>
366#include <__chrono/duration.h>
367#include <__chrono/time_point.h>
368#include <__config>
369#include <__exception/exception_ptr.h>
370#include <__memory/allocator.h>
371#include <__memory/allocator_arg_t.h>
372#include <__memory/allocator_destructor.h>
373#include <__memory/allocator_traits.h>
374#include <__memory/compressed_pair.h>
375#include <__memory/pointer_traits.h>
376#include <__memory/shared_ptr.h>
377#include <__memory/unique_ptr.h>
378#include <__memory/uses_allocator.h>
379#include <__system_error/error_category.h>
380#include <__system_error/error_code.h>
381#include <__system_error/error_condition.h>
382#include <__type_traits/aligned_storage.h>
383#include <__type_traits/alignment_of.h>
384#include <__type_traits/strip_signature.h>
385#include <__utility/auto_cast.h>
386#include <__utility/forward.h>
387#include <__utility/move.h>
388#include <mutex>
389#include <new>
390#include <stdexcept>
391#include <thread>
392#include <version>
393
394#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
395#  pragma GCC system_header
396#endif
397
398#ifdef _LIBCPP_HAS_NO_THREADS
399# error "<future> is not supported since libc++ has been configured without support for threads."
400#endif
401
402_LIBCPP_BEGIN_NAMESPACE_STD
403
404//enum class future_errc
405_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
406{
407    future_already_retrieved = 1,
408    promise_already_satisfied,
409    no_state,
410    broken_promise
411};
412_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
413
414template <>
415struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
416
417#ifdef _LIBCPP_CXX03_LANG
418template <>
419struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
420#endif
421
422//enum class launch
423_LIBCPP_DECLARE_STRONG_ENUM(launch)
424{
425    async = 1,
426    deferred = 2,
427    any = async | deferred
428};
429_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
430
431#ifndef _LIBCPP_CXX03_LANG
432
433typedef underlying_type<launch>::type __launch_underlying_type;
434
435inline _LIBCPP_INLINE_VISIBILITY
436_LIBCPP_CONSTEXPR
437launch
438operator&(launch __x, launch __y)
439{
440    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
441                               static_cast<__launch_underlying_type>(__y));
442}
443
444inline _LIBCPP_INLINE_VISIBILITY
445_LIBCPP_CONSTEXPR
446launch
447operator|(launch __x, launch __y)
448{
449    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
450                               static_cast<__launch_underlying_type>(__y));
451}
452
453inline _LIBCPP_INLINE_VISIBILITY
454_LIBCPP_CONSTEXPR
455launch
456operator^(launch __x, launch __y)
457{
458    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
459                               static_cast<__launch_underlying_type>(__y));
460}
461
462inline _LIBCPP_INLINE_VISIBILITY
463_LIBCPP_CONSTEXPR
464launch
465operator~(launch __x)
466{
467    return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
468}
469
470inline _LIBCPP_INLINE_VISIBILITY
471launch&
472operator&=(launch& __x, launch __y)
473{
474    __x = __x & __y; return __x;
475}
476
477inline _LIBCPP_INLINE_VISIBILITY
478launch&
479operator|=(launch& __x, launch __y)
480{
481    __x = __x | __y; return __x;
482}
483
484inline _LIBCPP_INLINE_VISIBILITY
485launch&
486operator^=(launch& __x, launch __y)
487{
488    __x = __x ^ __y; return __x;
489}
490
491#endif // !_LIBCPP_CXX03_LANG
492
493//enum class future_status
494_LIBCPP_DECLARE_STRONG_ENUM(future_status)
495{
496    ready,
497    timeout,
498    deferred
499};
500_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
501
502_LIBCPP_FUNC_VIS
503const error_category& future_category() _NOEXCEPT;
504
505inline _LIBCPP_INLINE_VISIBILITY
506error_code
507make_error_code(future_errc __e) _NOEXCEPT
508{
509    return error_code(static_cast<int>(__e), future_category());
510}
511
512inline _LIBCPP_INLINE_VISIBILITY
513error_condition
514make_error_condition(future_errc __e) _NOEXCEPT
515{
516    return error_condition(static_cast<int>(__e), future_category());
517}
518
519class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
520    : public logic_error
521{
522    error_code __ec_;
523public:
524    future_error(error_code __ec);
525
526    _LIBCPP_INLINE_VISIBILITY
527    const error_code& code() const _NOEXCEPT {return __ec_;}
528
529    _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
530    ~future_error() _NOEXCEPT override;
531};
532
533_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
534#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
535_LIBCPP_AVAILABILITY_FUTURE_ERROR
536#endif
537void __throw_future_error(future_errc __ev)
538{
539#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
540    throw future_error(make_error_code(__ev));
541#else
542    (void)__ev;
543    _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
544#endif
545}
546
547class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
548    : public __shared_count
549{
550protected:
551    exception_ptr __exception_;
552    mutable mutex __mut_;
553    mutable condition_variable __cv_;
554    unsigned __state_;
555
556    void __on_zero_shared() _NOEXCEPT override;
557    void __sub_wait(unique_lock<mutex>& __lk);
558public:
559    enum
560    {
561        __constructed = 1,
562        __future_attached = 2,
563        ready = 4,
564        deferred = 8
565    };
566
567    _LIBCPP_INLINE_VISIBILITY
568    __assoc_sub_state() : __state_(0) {}
569
570    _LIBCPP_INLINE_VISIBILITY
571    bool __has_value() const
572        {return (__state_ & __constructed) || (__exception_ != nullptr);}
573
574    _LIBCPP_INLINE_VISIBILITY
575    void __attach_future() {
576        lock_guard<mutex> __lk(__mut_);
577        bool __has_future_attached = (__state_ & __future_attached) != 0;
578        if (__has_future_attached)
579            __throw_future_error(future_errc::future_already_retrieved);
580        this->__add_shared();
581        __state_ |= __future_attached;
582    }
583
584    _LIBCPP_INLINE_VISIBILITY
585    void __set_deferred() {__state_ |= deferred;}
586
587    void __make_ready();
588    _LIBCPP_INLINE_VISIBILITY
589    bool __is_ready() const {return (__state_ & ready) != 0;}
590
591    void set_value();
592    void set_value_at_thread_exit();
593
594    void set_exception(exception_ptr __p);
595    void set_exception_at_thread_exit(exception_ptr __p);
596
597    void copy();
598
599    void wait();
600    template <class _Rep, class _Period>
601        future_status
602        _LIBCPP_INLINE_VISIBILITY
603        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
604    template <class _Clock, class _Duration>
605        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
606        future_status
607        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
608
609    virtual void __execute();
610};
611
612template <class _Clock, class _Duration>
613future_status
614__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
615{
616    unique_lock<mutex> __lk(__mut_);
617    if (__state_ & deferred)
618        return future_status::deferred;
619    while (!(__state_ & ready) && _Clock::now() < __abs_time)
620        __cv_.wait_until(__lk, __abs_time);
621    if (__state_ & ready)
622        return future_status::ready;
623    return future_status::timeout;
624}
625
626template <class _Rep, class _Period>
627inline
628future_status
629__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
630{
631    return wait_until(chrono::steady_clock::now() + __rel_time);
632}
633
634template <class _Rp>
635class _LIBCPP_AVAILABILITY_FUTURE _LIBCPP_HIDDEN __assoc_state
636    : public __assoc_sub_state
637{
638    typedef __assoc_sub_state base;
639_LIBCPP_SUPPRESS_DEPRECATED_PUSH
640    typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
641_LIBCPP_SUPPRESS_DEPRECATED_POP
642protected:
643    _Up __value_;
644
645    _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
646public:
647
648    template <class _Arg>
649    _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
650
651    template <class _Arg>
652    _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
653
654    _LIBCPP_HIDE_FROM_ABI _Rp move();
655    _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<_Rp> copy();
656};
657
658template <class _Rp>
659void
660__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
661{
662    if (this->__state_ & base::__constructed)
663        reinterpret_cast<_Rp*>(&__value_)->~_Rp();
664    delete this;
665}
666
667template <class _Rp>
668template <class _Arg>
669_LIBCPP_AVAILABILITY_FUTURE
670void
671__assoc_state<_Rp>::set_value(_Arg&& __arg)
672{
673    unique_lock<mutex> __lk(this->__mut_);
674    if (this->__has_value())
675        __throw_future_error(future_errc::promise_already_satisfied);
676    ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
677    this->__state_ |= base::__constructed | base::ready;
678    __cv_.notify_all();
679}
680
681template <class _Rp>
682template <class _Arg>
683void
684__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
685{
686    unique_lock<mutex> __lk(this->__mut_);
687    if (this->__has_value())
688        __throw_future_error(future_errc::promise_already_satisfied);
689    ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
690    this->__state_ |= base::__constructed;
691    __thread_local_data()->__make_ready_at_thread_exit(this);
692}
693
694template <class _Rp>
695_Rp
696__assoc_state<_Rp>::move()
697{
698    unique_lock<mutex> __lk(this->__mut_);
699    this->__sub_wait(__lk);
700    if (this->__exception_ != nullptr)
701        std::rethrow_exception(this->__exception_);
702    return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
703}
704
705template <class _Rp>
706__add_lvalue_reference_t<_Rp>
707__assoc_state<_Rp>::copy()
708{
709    unique_lock<mutex> __lk(this->__mut_);
710    this->__sub_wait(__lk);
711    if (this->__exception_ != nullptr)
712        std::rethrow_exception(this->__exception_);
713    return *reinterpret_cast<_Rp*>(&__value_);
714}
715
716template <class _Rp>
717class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
718    : public __assoc_sub_state
719{
720    typedef __assoc_sub_state base;
721    typedef _Rp* _Up;
722protected:
723    _Up __value_;
724
725    _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
726public:
727
728    _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
729    _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
730
731    _LIBCPP_HIDE_FROM_ABI _Rp& copy();
732};
733
734template <class _Rp>
735void
736__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
737{
738    delete this;
739}
740
741template <class _Rp>
742void
743__assoc_state<_Rp&>::set_value(_Rp& __arg)
744{
745    unique_lock<mutex> __lk(this->__mut_);
746    if (this->__has_value())
747        __throw_future_error(future_errc::promise_already_satisfied);
748    __value_ = _VSTD::addressof(__arg);
749    this->__state_ |= base::__constructed | base::ready;
750    __cv_.notify_all();
751}
752
753template <class _Rp>
754void
755__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
756{
757    unique_lock<mutex> __lk(this->__mut_);
758    if (this->__has_value())
759        __throw_future_error(future_errc::promise_already_satisfied);
760    __value_ = _VSTD::addressof(__arg);
761    this->__state_ |= base::__constructed;
762    __thread_local_data()->__make_ready_at_thread_exit(this);
763}
764
765template <class _Rp>
766_Rp&
767__assoc_state<_Rp&>::copy()
768{
769    unique_lock<mutex> __lk(this->__mut_);
770    this->__sub_wait(__lk);
771    if (this->__exception_ != nullptr)
772        std::rethrow_exception(this->__exception_);
773    return *__value_;
774}
775
776template <class _Rp, class _Alloc>
777class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
778    : public __assoc_state<_Rp>
779{
780    typedef __assoc_state<_Rp> base;
781    _Alloc __alloc_;
782
783    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
784public:
785    _LIBCPP_INLINE_VISIBILITY
786    explicit __assoc_state_alloc(const _Alloc& __a)
787        : __alloc_(__a) {}
788};
789
790template <class _Rp, class _Alloc>
791void
792__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
793{
794    if (this->__state_ & base::__constructed)
795        reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
796    typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
797    typedef allocator_traits<_Al> _ATraits;
798    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
799    _Al __a(__alloc_);
800    this->~__assoc_state_alloc();
801    __a.deallocate(_PTraits::pointer_to(*this), 1);
802}
803
804template <class _Rp, class _Alloc>
805class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
806    : public __assoc_state<_Rp&>
807{
808    typedef __assoc_state<_Rp&> base;
809    _Alloc __alloc_;
810
811    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
812public:
813    _LIBCPP_INLINE_VISIBILITY
814    explicit __assoc_state_alloc(const _Alloc& __a)
815        : __alloc_(__a) {}
816};
817
818template <class _Rp, class _Alloc>
819void
820__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
821{
822    typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
823    typedef allocator_traits<_Al> _ATraits;
824    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
825    _Al __a(__alloc_);
826    this->~__assoc_state_alloc();
827    __a.deallocate(_PTraits::pointer_to(*this), 1);
828}
829
830template <class _Alloc>
831class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
832    : public __assoc_sub_state
833{
834    typedef __assoc_sub_state base;
835    _Alloc __alloc_;
836
837    _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
838public:
839    _LIBCPP_INLINE_VISIBILITY
840    explicit __assoc_sub_state_alloc(const _Alloc& __a)
841        : __alloc_(__a) {}
842};
843
844template <class _Alloc>
845void
846__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
847{
848    typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
849    typedef allocator_traits<_Al> _ATraits;
850    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
851    _Al __a(__alloc_);
852    this->~__assoc_sub_state_alloc();
853    __a.deallocate(_PTraits::pointer_to(*this), 1);
854}
855
856template <class _Rp, class _Fp>
857class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
858    : public __assoc_state<_Rp>
859{
860    typedef __assoc_state<_Rp> base;
861
862    _Fp __func_;
863
864public:
865    _LIBCPP_INLINE_VISIBILITY
866    explicit __deferred_assoc_state(_Fp&& __f);
867
868    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
869};
870
871template <class _Rp, class _Fp>
872inline
873__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
874    : __func_(_VSTD::forward<_Fp>(__f))
875{
876    this->__set_deferred();
877}
878
879template <class _Rp, class _Fp>
880void
881__deferred_assoc_state<_Rp, _Fp>::__execute()
882{
883#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
884    try
885    {
886#endif // _LIBCPP_HAS_NO_EXCEPTIONS
887        this->set_value(__func_());
888#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
889    }
890    catch (...)
891    {
892        this->set_exception(current_exception());
893    }
894#endif // _LIBCPP_HAS_NO_EXCEPTIONS
895}
896
897template <class _Fp>
898class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
899    : public __assoc_sub_state
900{
901    typedef __assoc_sub_state base;
902
903    _Fp __func_;
904
905public:
906    _LIBCPP_INLINE_VISIBILITY
907    explicit __deferred_assoc_state(_Fp&& __f);
908
909    _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
910};
911
912template <class _Fp>
913inline
914__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
915    : __func_(_VSTD::forward<_Fp>(__f))
916{
917    this->__set_deferred();
918}
919
920template <class _Fp>
921void
922__deferred_assoc_state<void, _Fp>::__execute()
923{
924#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
925    try
926    {
927#endif // _LIBCPP_HAS_NO_EXCEPTIONS
928        __func_();
929        this->set_value();
930#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
931    }
932    catch (...)
933    {
934        this->set_exception(current_exception());
935    }
936#endif // _LIBCPP_HAS_NO_EXCEPTIONS
937}
938
939template <class _Rp, class _Fp>
940class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
941    : public __assoc_state<_Rp>
942{
943    typedef __assoc_state<_Rp> base;
944
945    _Fp __func_;
946
947    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
948public:
949    _LIBCPP_INLINE_VISIBILITY
950    explicit __async_assoc_state(_Fp&& __f);
951
952    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
953};
954
955template <class _Rp, class _Fp>
956inline
957__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
958    : __func_(_VSTD::forward<_Fp>(__f))
959{
960}
961
962template <class _Rp, class _Fp>
963void
964__async_assoc_state<_Rp, _Fp>::__execute()
965{
966#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
967    try
968    {
969#endif // _LIBCPP_HAS_NO_EXCEPTIONS
970        this->set_value(__func_());
971#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
972    }
973    catch (...)
974    {
975        this->set_exception(current_exception());
976    }
977#endif // _LIBCPP_HAS_NO_EXCEPTIONS
978}
979
980template <class _Rp, class _Fp>
981void
982__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
983{
984    this->wait();
985    base::__on_zero_shared();
986}
987
988template <class _Fp>
989class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
990    : public __assoc_sub_state
991{
992    typedef __assoc_sub_state base;
993
994    _Fp __func_;
995
996    _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
997public:
998    _LIBCPP_INLINE_VISIBILITY
999    explicit __async_assoc_state(_Fp&& __f);
1000
1001    _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
1002};
1003
1004template <class _Fp>
1005inline
1006__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1007    : __func_(_VSTD::forward<_Fp>(__f))
1008{
1009}
1010
1011template <class _Fp>
1012void
1013__async_assoc_state<void, _Fp>::__execute()
1014{
1015#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1016    try
1017    {
1018#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1019        __func_();
1020        this->set_value();
1021#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1022    }
1023    catch (...)
1024    {
1025        this->set_exception(current_exception());
1026    }
1027#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1028}
1029
1030template <class _Fp>
1031void
1032__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1033{
1034    this->wait();
1035    base::__on_zero_shared();
1036}
1037
1038template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1039template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
1040
1041// future
1042
1043template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
1044
1045template <class _Rp, class _Fp>
1046_LIBCPP_INLINE_VISIBILITY future<_Rp>
1047__make_deferred_assoc_state(_Fp&& __f);
1048
1049template <class _Rp, class _Fp>
1050_LIBCPP_INLINE_VISIBILITY future<_Rp>
1051__make_async_assoc_state(_Fp&& __f);
1052
1053template <class _Rp>
1054class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
1055{
1056    __assoc_state<_Rp>* __state_;
1057
1058    explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
1059
1060    template <class> friend class promise;
1061    template <class> friend class shared_future;
1062
1063    template <class _R1, class _Fp>
1064        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1065    template <class _R1, class _Fp>
1066        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1067
1068public:
1069    _LIBCPP_INLINE_VISIBILITY
1070    future() _NOEXCEPT : __state_(nullptr) {}
1071    _LIBCPP_INLINE_VISIBILITY
1072    future(future&& __rhs) _NOEXCEPT
1073        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1074    future(const future&) = delete;
1075    future& operator=(const future&) = delete;
1076    _LIBCPP_INLINE_VISIBILITY
1077    future& operator=(future&& __rhs) _NOEXCEPT
1078        {
1079            future(_VSTD::move(__rhs)).swap(*this);
1080            return *this;
1081        }
1082
1083    _LIBCPP_HIDE_FROM_ABI ~future();
1084    _LIBCPP_INLINE_VISIBILITY
1085    shared_future<_Rp> share() _NOEXCEPT;
1086
1087    // retrieving the value
1088    _LIBCPP_HIDE_FROM_ABI _Rp get();
1089
1090    _LIBCPP_INLINE_VISIBILITY
1091    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1092
1093    // functions to check state
1094    _LIBCPP_INLINE_VISIBILITY
1095    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1096
1097    _LIBCPP_INLINE_VISIBILITY
1098    void wait() const {__state_->wait();}
1099    template <class _Rep, class _Period>
1100        _LIBCPP_INLINE_VISIBILITY
1101        future_status
1102        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1103            {return __state_->wait_for(__rel_time);}
1104    template <class _Clock, class _Duration>
1105        _LIBCPP_INLINE_VISIBILITY
1106        future_status
1107        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1108            {return __state_->wait_until(__abs_time);}
1109};
1110
1111template <class _Rp>
1112future<_Rp>::future(__assoc_state<_Rp>* __state)
1113    : __state_(__state)
1114{
1115    __state_->__attach_future();
1116}
1117
1118struct __release_shared_count
1119{
1120    _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) {__p->__release_shared();}
1121};
1122
1123template <class _Rp>
1124future<_Rp>::~future()
1125{
1126    if (__state_)
1127        __state_->__release_shared();
1128}
1129
1130template <class _Rp>
1131_Rp
1132future<_Rp>::get()
1133{
1134    unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1135    __assoc_state<_Rp>* __s = __state_;
1136    __state_ = nullptr;
1137    return __s->move();
1138}
1139
1140template <class _Rp>
1141class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
1142{
1143    __assoc_state<_Rp&>* __state_;
1144
1145    explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
1146
1147    template <class> friend class promise;
1148    template <class> friend class shared_future;
1149
1150    template <class _R1, class _Fp>
1151        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1152    template <class _R1, class _Fp>
1153        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1154
1155public:
1156    _LIBCPP_INLINE_VISIBILITY
1157    future() _NOEXCEPT : __state_(nullptr) {}
1158    _LIBCPP_INLINE_VISIBILITY
1159    future(future&& __rhs) _NOEXCEPT
1160        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1161    future(const future&) = delete;
1162    future& operator=(const future&) = delete;
1163    _LIBCPP_INLINE_VISIBILITY
1164    future& operator=(future&& __rhs) _NOEXCEPT
1165        {
1166            future(_VSTD::move(__rhs)).swap(*this);
1167            return *this;
1168        }
1169
1170    _LIBCPP_HIDE_FROM_ABI ~future();
1171    _LIBCPP_INLINE_VISIBILITY
1172    shared_future<_Rp&> share() _NOEXCEPT;
1173
1174    // retrieving the value
1175    _LIBCPP_HIDE_FROM_ABI _Rp& get();
1176
1177    _LIBCPP_INLINE_VISIBILITY
1178    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1179
1180    // functions to check state
1181    _LIBCPP_INLINE_VISIBILITY
1182    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1183
1184    _LIBCPP_INLINE_VISIBILITY
1185    void wait() const {__state_->wait();}
1186    template <class _Rep, class _Period>
1187        _LIBCPP_INLINE_VISIBILITY
1188        future_status
1189        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1190            {return __state_->wait_for(__rel_time);}
1191    template <class _Clock, class _Duration>
1192        _LIBCPP_INLINE_VISIBILITY
1193        future_status
1194        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1195            {return __state_->wait_until(__abs_time);}
1196};
1197
1198template <class _Rp>
1199future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1200    : __state_(__state)
1201{
1202    __state_->__attach_future();
1203}
1204
1205template <class _Rp>
1206future<_Rp&>::~future()
1207{
1208    if (__state_)
1209        __state_->__release_shared();
1210}
1211
1212template <class _Rp>
1213_Rp&
1214future<_Rp&>::get()
1215{
1216    unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1217    __assoc_state<_Rp&>* __s = __state_;
1218    __state_ = nullptr;
1219    return __s->copy();
1220}
1221
1222template <>
1223class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE future<void>
1224{
1225    __assoc_sub_state* __state_;
1226
1227    explicit future(__assoc_sub_state* __state);
1228
1229    template <class> friend class promise;
1230    template <class> friend class shared_future;
1231
1232    template <class _R1, class _Fp>
1233        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1234    template <class _R1, class _Fp>
1235        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1236
1237public:
1238    _LIBCPP_INLINE_VISIBILITY
1239    future() _NOEXCEPT : __state_(nullptr) {}
1240    _LIBCPP_INLINE_VISIBILITY
1241    future(future&& __rhs) _NOEXCEPT
1242        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1243    future(const future&) = delete;
1244    future& operator=(const future&) = delete;
1245    _LIBCPP_INLINE_VISIBILITY
1246    future& operator=(future&& __rhs) _NOEXCEPT
1247        {
1248            future(_VSTD::move(__rhs)).swap(*this);
1249            return *this;
1250        }
1251
1252    ~future();
1253    _LIBCPP_INLINE_VISIBILITY
1254    shared_future<void> share() _NOEXCEPT;
1255
1256    // retrieving the value
1257    void get();
1258
1259    _LIBCPP_INLINE_VISIBILITY
1260    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1261
1262    // functions to check state
1263    _LIBCPP_INLINE_VISIBILITY
1264    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1265
1266    _LIBCPP_INLINE_VISIBILITY
1267    void wait() const {__state_->wait();}
1268    template <class _Rep, class _Period>
1269        _LIBCPP_INLINE_VISIBILITY
1270        future_status
1271        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1272            {return __state_->wait_for(__rel_time);}
1273    template <class _Clock, class _Duration>
1274        _LIBCPP_INLINE_VISIBILITY
1275        future_status
1276        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1277            {return __state_->wait_until(__abs_time);}
1278};
1279
1280template <class _Rp>
1281inline _LIBCPP_INLINE_VISIBILITY
1282void
1283swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1284{
1285    __x.swap(__y);
1286}
1287
1288// promise<R>
1289
1290template <class _Callable> class packaged_task;
1291
1292template <class _Rp>
1293class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
1294{
1295    __assoc_state<_Rp>* __state_;
1296
1297    _LIBCPP_INLINE_VISIBILITY
1298    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1299
1300    template <class> friend class packaged_task;
1301public:
1302    _LIBCPP_HIDE_FROM_ABI promise();
1303    template <class _Alloc>
1304    _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1305    _LIBCPP_INLINE_VISIBILITY
1306    promise(promise&& __rhs) _NOEXCEPT
1307        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1308    promise(const promise& __rhs) = delete;
1309    _LIBCPP_HIDE_FROM_ABI ~promise();
1310
1311    // assignment
1312    _LIBCPP_INLINE_VISIBILITY
1313    promise& operator=(promise&& __rhs) _NOEXCEPT
1314        {
1315            promise(_VSTD::move(__rhs)).swap(*this);
1316            return *this;
1317        }
1318    promise& operator=(const promise& __rhs) = delete;
1319
1320    _LIBCPP_INLINE_VISIBILITY
1321    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1322
1323    // retrieving the result
1324    _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1325
1326    // setting the result
1327    _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1328    _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1329    _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1330
1331    // setting the result with deferred notification
1332    _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1333    _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1334    _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1335};
1336
1337template <class _Rp>
1338promise<_Rp>::promise()
1339    : __state_(new __assoc_state<_Rp>)
1340{
1341}
1342
1343template <class _Rp>
1344template <class _Alloc>
1345promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1346{
1347    typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1348    typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1349    typedef __allocator_destructor<_A2> _D2;
1350    _A2 __a(__a0);
1351    unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1352    ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1353    __state_ = _VSTD::addressof(*__hold.release());
1354}
1355
1356template <class _Rp>
1357promise<_Rp>::~promise()
1358{
1359    if (__state_)
1360    {
1361        if (!__state_->__has_value() && __state_->use_count() > 1)
1362            __state_->set_exception(make_exception_ptr(
1363                      future_error(make_error_code(future_errc::broken_promise))
1364                                                      ));
1365        __state_->__release_shared();
1366    }
1367}
1368
1369template <class _Rp>
1370future<_Rp>
1371promise<_Rp>::get_future()
1372{
1373    if (__state_ == nullptr)
1374        __throw_future_error(future_errc::no_state);
1375    return future<_Rp>(__state_);
1376}
1377
1378template <class _Rp>
1379void
1380promise<_Rp>::set_value(const _Rp& __r)
1381{
1382    if (__state_ == nullptr)
1383        __throw_future_error(future_errc::no_state);
1384    __state_->set_value(__r);
1385}
1386
1387template <class _Rp>
1388void
1389promise<_Rp>::set_value(_Rp&& __r)
1390{
1391    if (__state_ == nullptr)
1392        __throw_future_error(future_errc::no_state);
1393    __state_->set_value(_VSTD::move(__r));
1394}
1395
1396template <class _Rp>
1397void
1398promise<_Rp>::set_exception(exception_ptr __p)
1399{
1400    _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
1401    if (__state_ == nullptr)
1402        __throw_future_error(future_errc::no_state);
1403    __state_->set_exception(__p);
1404}
1405
1406template <class _Rp>
1407void
1408promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1409{
1410    if (__state_ == nullptr)
1411        __throw_future_error(future_errc::no_state);
1412    __state_->set_value_at_thread_exit(__r);
1413}
1414
1415template <class _Rp>
1416void
1417promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1418{
1419    if (__state_ == nullptr)
1420        __throw_future_error(future_errc::no_state);
1421    __state_->set_value_at_thread_exit(_VSTD::move(__r));
1422}
1423
1424template <class _Rp>
1425void
1426promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
1427{
1428    _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1429    if (__state_ == nullptr)
1430        __throw_future_error(future_errc::no_state);
1431    __state_->set_exception_at_thread_exit(__p);
1432}
1433
1434// promise<R&>
1435
1436template <class _Rp>
1437class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
1438{
1439    __assoc_state<_Rp&>* __state_;
1440
1441    _LIBCPP_INLINE_VISIBILITY
1442    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1443
1444    template <class> friend class packaged_task;
1445
1446public:
1447    _LIBCPP_HIDE_FROM_ABI promise();
1448    template <class _Allocator>
1449    _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1450    _LIBCPP_INLINE_VISIBILITY
1451    promise(promise&& __rhs) _NOEXCEPT
1452        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1453    promise(const promise& __rhs) = delete;
1454    _LIBCPP_HIDE_FROM_ABI ~promise();
1455
1456    // assignment
1457    _LIBCPP_INLINE_VISIBILITY
1458    promise& operator=(promise&& __rhs) _NOEXCEPT
1459        {
1460            promise(_VSTD::move(__rhs)).swap(*this);
1461            return *this;
1462        }
1463    promise& operator=(const promise& __rhs) = delete;
1464
1465    _LIBCPP_INLINE_VISIBILITY
1466    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1467
1468    // retrieving the result
1469    _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1470
1471    // setting the result
1472    _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1473    _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1474
1475    // setting the result with deferred notification
1476    _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1477    _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1478};
1479
1480template <class _Rp>
1481promise<_Rp&>::promise()
1482    : __state_(new __assoc_state<_Rp&>)
1483{
1484}
1485
1486template <class _Rp>
1487template <class _Alloc>
1488promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1489{
1490    typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1491    typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1492    typedef __allocator_destructor<_A2> _D2;
1493    _A2 __a(__a0);
1494    unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1495    ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1496    __state_ = _VSTD::addressof(*__hold.release());
1497}
1498
1499template <class _Rp>
1500promise<_Rp&>::~promise()
1501{
1502    if (__state_)
1503    {
1504        if (!__state_->__has_value() && __state_->use_count() > 1)
1505            __state_->set_exception(make_exception_ptr(
1506                      future_error(make_error_code(future_errc::broken_promise))
1507                                                      ));
1508        __state_->__release_shared();
1509    }
1510}
1511
1512template <class _Rp>
1513future<_Rp&>
1514promise<_Rp&>::get_future()
1515{
1516    if (__state_ == nullptr)
1517        __throw_future_error(future_errc::no_state);
1518    return future<_Rp&>(__state_);
1519}
1520
1521template <class _Rp>
1522void
1523promise<_Rp&>::set_value(_Rp& __r)
1524{
1525    if (__state_ == nullptr)
1526        __throw_future_error(future_errc::no_state);
1527    __state_->set_value(__r);
1528}
1529
1530template <class _Rp>
1531void
1532promise<_Rp&>::set_exception(exception_ptr __p)
1533{
1534    _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
1535    if (__state_ == nullptr)
1536        __throw_future_error(future_errc::no_state);
1537    __state_->set_exception(__p);
1538}
1539
1540template <class _Rp>
1541void
1542promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1543{
1544    if (__state_ == nullptr)
1545        __throw_future_error(future_errc::no_state);
1546    __state_->set_value_at_thread_exit(__r);
1547}
1548
1549template <class _Rp>
1550void
1551promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
1552{
1553    _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1554    if (__state_ == nullptr)
1555        __throw_future_error(future_errc::no_state);
1556    __state_->set_exception_at_thread_exit(__p);
1557}
1558
1559// promise<void>
1560
1561template <>
1562class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<void>
1563{
1564    __assoc_sub_state* __state_;
1565
1566    _LIBCPP_INLINE_VISIBILITY
1567    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1568
1569    template <class> friend class packaged_task;
1570
1571public:
1572    promise();
1573    template <class _Allocator>
1574        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1575        promise(allocator_arg_t, const _Allocator& __a);
1576    _LIBCPP_INLINE_VISIBILITY
1577    promise(promise&& __rhs) _NOEXCEPT
1578        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1579    promise(const promise& __rhs) = delete;
1580    ~promise();
1581
1582    // assignment
1583    _LIBCPP_INLINE_VISIBILITY
1584    promise& operator=(promise&& __rhs) _NOEXCEPT
1585        {
1586            promise(_VSTD::move(__rhs)).swap(*this);
1587            return *this;
1588        }
1589    promise& operator=(const promise& __rhs) = delete;
1590
1591    _LIBCPP_INLINE_VISIBILITY
1592    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1593
1594    // retrieving the result
1595    future<void> get_future();
1596
1597    // setting the result
1598    void set_value();
1599    void set_exception(exception_ptr __p);
1600
1601    // setting the result with deferred notification
1602    void set_value_at_thread_exit();
1603    void set_exception_at_thread_exit(exception_ptr __p);
1604};
1605
1606template <class _Alloc>
1607promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1608{
1609    typedef __assoc_sub_state_alloc<_Alloc> _State;
1610    typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1611    typedef __allocator_destructor<_A2> _D2;
1612    _A2 __a(__a0);
1613    unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1614    ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1615    __state_ = _VSTD::addressof(*__hold.release());
1616}
1617
1618template <class _Rp>
1619inline _LIBCPP_INLINE_VISIBILITY
1620void
1621swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1622{
1623    __x.swap(__y);
1624}
1625
1626template <class _Rp, class _Alloc>
1627    struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
1628        : public true_type {};
1629
1630// packaged_task
1631
1632template<class _Fp> class __packaged_task_base;
1633
1634template<class _Rp, class ..._ArgTypes>
1635class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
1636{
1637    __packaged_task_base(const __packaged_task_base&);
1638    __packaged_task_base& operator=(const __packaged_task_base&);
1639public:
1640    _LIBCPP_INLINE_VISIBILITY
1641    __packaged_task_base() {}
1642    _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1643    virtual ~__packaged_task_base() {}
1644    virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1645    virtual void destroy() = 0;
1646    virtual void destroy_deallocate() = 0;
1647    virtual _Rp operator()(_ArgTypes&& ...) = 0;
1648};
1649
1650template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1651
1652template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1653class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1654    : public  __packaged_task_base<_Rp(_ArgTypes...)>
1655{
1656    __compressed_pair<_Fp, _Alloc> __f_;
1657public:
1658    _LIBCPP_INLINE_VISIBILITY
1659    explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
1660    _LIBCPP_INLINE_VISIBILITY
1661    explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1662    _LIBCPP_INLINE_VISIBILITY
1663    __packaged_task_func(const _Fp& __f, const _Alloc& __a)
1664        : __f_(__f, __a) {}
1665    _LIBCPP_INLINE_VISIBILITY
1666    __packaged_task_func(_Fp&& __f, const _Alloc& __a)
1667        : __f_(_VSTD::move(__f), __a) {}
1668    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1669    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1670    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1671    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&& ... __args);
1672};
1673
1674template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1675void
1676__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1677                              __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1678{
1679    ::new ((void*)__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1680}
1681
1682template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1683void
1684__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1685{
1686    __f_.~__compressed_pair<_Fp, _Alloc>();
1687}
1688
1689template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1690void
1691__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
1692{
1693    typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1694    typedef allocator_traits<_Ap> _ATraits;
1695    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1696    _Ap __a(__f_.second());
1697    __f_.~__compressed_pair<_Fp, _Alloc>();
1698    __a.deallocate(_PTraits::pointer_to(*this), 1);
1699}
1700
1701template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1702_Rp
1703__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1704{
1705    return _VSTD::__invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1706}
1707
1708template <class _Callable> class __packaged_task_function;
1709
1710template<class _Rp, class ..._ArgTypes>
1711class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
1712{
1713    typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1714
1715    _LIBCPP_INLINE_VISIBILITY _LIBCPP_NO_CFI
1716    __base* __get_buf() { return (__base*)&__buf_; }
1717
1718    _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1719    typename aligned_storage<3*sizeof(void*)>::type __buf_;
1720    _LIBCPP_SUPPRESS_DEPRECATED_POP
1721    __base* __f_;
1722
1723public:
1724    typedef _Rp result_type;
1725
1726    // construct/copy/destroy:
1727    _LIBCPP_INLINE_VISIBILITY
1728    __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1729    template<class _Fp>
1730    _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1731    template<class _Fp, class _Alloc>
1732    _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1733
1734    _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1735    _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1736
1737    __packaged_task_function(const __packaged_task_function&) =  delete;
1738    __packaged_task_function& operator=(const __packaged_task_function&) =  delete;
1739
1740    _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1741
1742    _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1743
1744    _LIBCPP_INLINE_VISIBILITY
1745    _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1746};
1747
1748template<class _Rp, class ..._ArgTypes>
1749__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1750{
1751    if (__f.__f_ == nullptr)
1752        __f_ = nullptr;
1753    else if (__f.__f_ == __f.__get_buf())
1754    {
1755        __f.__f_->__move_to(__get_buf());
1756        __f_ = (__base*)&__buf_;
1757    }
1758    else
1759    {
1760        __f_ = __f.__f_;
1761        __f.__f_ = nullptr;
1762    }
1763}
1764
1765template<class _Rp, class ..._ArgTypes>
1766template <class _Fp>
1767__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
1768    : __f_(nullptr)
1769{
1770    typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1771    typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1772    if (sizeof(_FF) <= sizeof(__buf_))
1773    {
1774        ::new ((void*)&__buf_) _FF(_VSTD::forward<_Fp>(__f));
1775        __f_ = (__base*)&__buf_;
1776    }
1777    else
1778    {
1779        typedef allocator<_FF> _Ap;
1780        _Ap __a;
1781        typedef __allocator_destructor<_Ap> _Dp;
1782        unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1783        ::new ((void*)__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
1784        __f_ = __hold.release();
1785    }
1786}
1787
1788template<class _Rp, class ..._ArgTypes>
1789template <class _Fp, class _Alloc>
1790__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1791                                  allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1792    : __f_(nullptr)
1793{
1794    typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1795    typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1796    if (sizeof(_FF) <= sizeof(__buf_))
1797    {
1798        __f_ = (__base*)&__buf_;
1799        ::new ((void*)__f_) _FF(_VSTD::forward<_Fp>(__f));
1800    }
1801    else
1802    {
1803        typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1804        _Ap __a(__a0);
1805        typedef __allocator_destructor<_Ap> _Dp;
1806        unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1807        ::new ((void*)_VSTD::addressof(*__hold.get()))
1808            _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1809        __f_ = _VSTD::addressof(*__hold.release());
1810    }
1811}
1812
1813template<class _Rp, class ..._ArgTypes>
1814__packaged_task_function<_Rp(_ArgTypes...)>&
1815__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1816{
1817    if (__f_ == __get_buf())
1818        __f_->destroy();
1819    else if (__f_)
1820        __f_->destroy_deallocate();
1821    __f_ = nullptr;
1822    if (__f.__f_ == nullptr)
1823        __f_ = nullptr;
1824    else if (__f.__f_ == __f.__get_buf())
1825    {
1826        __f.__f_->__move_to(__get_buf());
1827        __f_ = __get_buf();
1828    }
1829    else
1830    {
1831        __f_ = __f.__f_;
1832        __f.__f_ = nullptr;
1833    }
1834    return *this;
1835}
1836
1837template<class _Rp, class ..._ArgTypes>
1838__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1839{
1840    if (__f_ == __get_buf())
1841        __f_->destroy();
1842    else if (__f_)
1843        __f_->destroy_deallocate();
1844}
1845
1846template<class _Rp, class ..._ArgTypes>
1847_LIBCPP_NO_CFI
1848void
1849__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1850{
1851    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1852    {
1853        _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1854        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1855        _LIBCPP_SUPPRESS_DEPRECATED_POP
1856        __base* __t = (__base*)&__tempbuf;
1857        __f_->__move_to(__t);
1858        __f_->destroy();
1859        __f_ = nullptr;
1860        __f.__f_->__move_to((__base*)&__buf_);
1861        __f.__f_->destroy();
1862        __f.__f_ = nullptr;
1863        __f_ = (__base*)&__buf_;
1864        __t->__move_to((__base*)&__f.__buf_);
1865        __t->destroy();
1866        __f.__f_ = (__base*)&__f.__buf_;
1867    }
1868    else if (__f_ == (__base*)&__buf_)
1869    {
1870        __f_->__move_to((__base*)&__f.__buf_);
1871        __f_->destroy();
1872        __f_ = __f.__f_;
1873        __f.__f_ = (__base*)&__f.__buf_;
1874    }
1875    else if (__f.__f_ == (__base*)&__f.__buf_)
1876    {
1877        __f.__f_->__move_to((__base*)&__buf_);
1878        __f.__f_->destroy();
1879        __f.__f_ = __f_;
1880        __f_ = (__base*)&__buf_;
1881    }
1882    else
1883        _VSTD::swap(__f_, __f.__f_);
1884}
1885
1886template<class _Rp, class ..._ArgTypes>
1887inline
1888_Rp
1889__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1890{
1891    return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
1892}
1893
1894template<class _Rp, class ..._ArgTypes>
1895class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
1896{
1897public:
1898    typedef _Rp result_type; // extension
1899
1900private:
1901    __packaged_task_function<result_type(_ArgTypes...)> __f_;
1902    promise<result_type>                                __p_;
1903
1904public:
1905    // construction and destruction
1906    _LIBCPP_INLINE_VISIBILITY
1907    packaged_task() _NOEXCEPT : __p_(nullptr) {}
1908    template <class _Fp,
1909              class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1910        _LIBCPP_INLINE_VISIBILITY
1911        explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
1912    template <class _Fp, class _Allocator,
1913              class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1914        _LIBCPP_INLINE_VISIBILITY
1915        packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1916             : __f_(allocator_arg_t(), __a, _VSTD::forward<_Fp>(__f)),
1917               __p_(allocator_arg_t(), __a) {}
1918    // ~packaged_task() = default;
1919
1920    // no copy
1921    packaged_task(const packaged_task&) = delete;
1922    packaged_task& operator=(const packaged_task&) = delete;
1923
1924    // move support
1925    _LIBCPP_INLINE_VISIBILITY
1926    packaged_task(packaged_task&& __other) _NOEXCEPT
1927        : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
1928    _LIBCPP_INLINE_VISIBILITY
1929    packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
1930    {
1931        __f_ = _VSTD::move(__other.__f_);
1932        __p_ = _VSTD::move(__other.__p_);
1933        return *this;
1934    }
1935    _LIBCPP_INLINE_VISIBILITY
1936    void swap(packaged_task& __other) _NOEXCEPT
1937    {
1938        __f_.swap(__other.__f_);
1939        __p_.swap(__other.__p_);
1940    }
1941
1942    _LIBCPP_INLINE_VISIBILITY
1943    bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
1944
1945    // result retrieval
1946    _LIBCPP_INLINE_VISIBILITY
1947    future<result_type> get_future() {return __p_.get_future();}
1948
1949    // execution
1950    _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1951    _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1952
1953    _LIBCPP_HIDE_FROM_ABI void reset();
1954};
1955
1956template<class _Rp, class ..._ArgTypes>
1957void
1958packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
1959{
1960    if (__p_.__state_ == nullptr)
1961        __throw_future_error(future_errc::no_state);
1962    if (__p_.__state_->__has_value())
1963        __throw_future_error(future_errc::promise_already_satisfied);
1964#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1965    try
1966    {
1967#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1968        __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
1969#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1970    }
1971    catch (...)
1972    {
1973        __p_.set_exception(current_exception());
1974    }
1975#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1976}
1977
1978template<class _Rp, class ..._ArgTypes>
1979void
1980packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
1981{
1982    if (__p_.__state_ == nullptr)
1983        __throw_future_error(future_errc::no_state);
1984    if (__p_.__state_->__has_value())
1985        __throw_future_error(future_errc::promise_already_satisfied);
1986#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1987    try
1988    {
1989#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1990        __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
1991#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1992    }
1993    catch (...)
1994    {
1995        __p_.set_exception_at_thread_exit(current_exception());
1996    }
1997#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1998}
1999
2000template<class _Rp, class ..._ArgTypes>
2001void
2002packaged_task<_Rp(_ArgTypes...)>::reset()
2003{
2004    if (!valid())
2005        __throw_future_error(future_errc::no_state);
2006    __p_ = promise<result_type>();
2007}
2008
2009template<class ..._ArgTypes>
2010class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
2011{
2012public:
2013    typedef void result_type; // extension
2014
2015private:
2016    __packaged_task_function<result_type(_ArgTypes...)> __f_;
2017    promise<result_type>                                __p_;
2018
2019public:
2020    // construction and destruction
2021    _LIBCPP_INLINE_VISIBILITY
2022    packaged_task() _NOEXCEPT : __p_(nullptr) {}
2023    template <class _Fp,
2024              class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
2025        _LIBCPP_INLINE_VISIBILITY
2026        explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2027    template <class _Fp, class _Allocator,
2028              class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
2029        _LIBCPP_INLINE_VISIBILITY
2030        packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2031             : __f_(allocator_arg_t(), __a, _VSTD::forward<_Fp>(__f)),
2032               __p_(allocator_arg_t(), __a) {}
2033    // ~packaged_task() = default;
2034
2035    // no copy
2036    packaged_task(const packaged_task&) = delete;
2037    packaged_task& operator=(const packaged_task&) = delete;
2038
2039    // move support
2040    _LIBCPP_INLINE_VISIBILITY
2041    packaged_task(packaged_task&& __other) _NOEXCEPT
2042        : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2043    _LIBCPP_INLINE_VISIBILITY
2044    packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2045    {
2046        __f_ = _VSTD::move(__other.__f_);
2047        __p_ = _VSTD::move(__other.__p_);
2048        return *this;
2049    }
2050    _LIBCPP_INLINE_VISIBILITY
2051    void swap(packaged_task& __other) _NOEXCEPT
2052    {
2053        __f_.swap(__other.__f_);
2054        __p_.swap(__other.__p_);
2055    }
2056
2057    _LIBCPP_INLINE_VISIBILITY
2058    bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2059
2060    // result retrieval
2061    _LIBCPP_INLINE_VISIBILITY
2062    future<result_type> get_future() {return __p_.get_future();}
2063
2064    // execution
2065    _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
2066    _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
2067
2068    _LIBCPP_HIDE_FROM_ABI void reset();
2069};
2070
2071#if _LIBCPP_STD_VER >= 17
2072
2073template <class _Rp, class... _Args>
2074packaged_task(_Rp(*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
2075
2076template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2077packaged_task(_Fp) -> packaged_task<_Stripped>;
2078
2079#endif
2080
2081template<class ..._ArgTypes>
2082void
2083packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2084{
2085    if (__p_.__state_ == nullptr)
2086        __throw_future_error(future_errc::no_state);
2087    if (__p_.__state_->__has_value())
2088        __throw_future_error(future_errc::promise_already_satisfied);
2089#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2090    try
2091    {
2092#endif // _LIBCPP_HAS_NO_EXCEPTIONS
2093        __f_(_VSTD::forward<_ArgTypes>(__args)...);
2094        __p_.set_value();
2095#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2096    }
2097    catch (...)
2098    {
2099        __p_.set_exception(current_exception());
2100    }
2101#endif // _LIBCPP_HAS_NO_EXCEPTIONS
2102}
2103
2104template<class ..._ArgTypes>
2105void
2106packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2107{
2108    if (__p_.__state_ == nullptr)
2109        __throw_future_error(future_errc::no_state);
2110    if (__p_.__state_->__has_value())
2111        __throw_future_error(future_errc::promise_already_satisfied);
2112#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2113    try
2114    {
2115#endif // _LIBCPP_HAS_NO_EXCEPTIONS
2116        __f_(_VSTD::forward<_ArgTypes>(__args)...);
2117        __p_.set_value_at_thread_exit();
2118#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2119    }
2120    catch (...)
2121    {
2122        __p_.set_exception_at_thread_exit(current_exception());
2123    }
2124#endif // _LIBCPP_HAS_NO_EXCEPTIONS
2125}
2126
2127template<class ..._ArgTypes>
2128void
2129packaged_task<void(_ArgTypes...)>::reset()
2130{
2131    if (!valid())
2132        __throw_future_error(future_errc::no_state);
2133    __p_ = promise<result_type>();
2134}
2135
2136template <class _Rp, class... _ArgTypes>
2137inline _LIBCPP_INLINE_VISIBILITY
2138void
2139swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
2140{
2141    __x.swap(__y);
2142}
2143
2144template <class _Callable, class _Alloc>
2145struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
2146    : public true_type {};
2147
2148template <class _Rp, class _Fp>
2149_LIBCPP_INLINE_VISIBILITY future<_Rp>
2150__make_deferred_assoc_state(_Fp&& __f)
2151{
2152    unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2153        __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2154    return future<_Rp>(__h.get());
2155}
2156
2157template <class _Rp, class _Fp>
2158_LIBCPP_INLINE_VISIBILITY future<_Rp>
2159__make_async_assoc_state(_Fp&& __f)
2160{
2161    unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2162        __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2163    _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2164    return future<_Rp>(__h.get());
2165}
2166
2167#ifndef _LIBCPP_CXX03_LANG
2168
2169template <class _Fp, class... _Args>
2170class _LIBCPP_HIDDEN __async_func
2171{
2172    tuple<_Fp, _Args...> __f_;
2173
2174public:
2175    typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2176
2177    _LIBCPP_INLINE_VISIBILITY
2178    explicit __async_func(_Fp&& __f, _Args&&... __args)
2179        : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2180
2181    _LIBCPP_INLINE_VISIBILITY
2182    __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2183
2184    _LIBCPP_HIDE_FROM_ABI _Rp operator()()
2185    {
2186        typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2187        return __execute(_Index());
2188    }
2189private:
2190    template <size_t ..._Indices>
2191    _LIBCPP_HIDE_FROM_ABI _Rp
2192    __execute(__tuple_indices<_Indices...>)
2193    {
2194        return _VSTD::__invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2195    }
2196};
2197
2198inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2199{ return (int(__policy) & int(__value)) != 0; }
2200
2201template <class _Fp, class... _Args>
2202_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
2203future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
2204async(launch __policy, _Fp&& __f, _Args&&... __args)
2205{
2206    typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
2207    typedef typename _BF::_Rp _Rp;
2208
2209#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2210    try
2211    {
2212#endif
2213        if (__does_policy_contain(__policy, launch::async))
2214        return _VSTD::__make_async_assoc_state<_Rp>(_BF(_LIBCPP_AUTO_CAST(_VSTD::forward<_Fp>(__f)),
2215                                                        _LIBCPP_AUTO_CAST(_VSTD::forward<_Args>(__args))...));
2216#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2217    }
2218    catch ( ... ) { if (__policy == launch::async) throw ; }
2219#endif
2220
2221    if (__does_policy_contain(__policy, launch::deferred))
2222        return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(_LIBCPP_AUTO_CAST(_VSTD::forward<_Fp>(__f)),
2223                                                           _LIBCPP_AUTO_CAST(_VSTD::forward<_Args>(__args))...));
2224    return future<_Rp>{};
2225}
2226
2227template <class _Fp, class... _Args>
2228_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
2229future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
2230async(_Fp&& __f, _Args&&... __args)
2231{
2232    return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2233                                    _VSTD::forward<_Args>(__args)...);
2234}
2235
2236#endif // C++03
2237
2238// shared_future
2239
2240template <class _Rp>
2241class _LIBCPP_TEMPLATE_VIS shared_future
2242{
2243    __assoc_state<_Rp>* __state_;
2244
2245public:
2246    _LIBCPP_INLINE_VISIBILITY
2247    shared_future() _NOEXCEPT : __state_(nullptr) {}
2248    _LIBCPP_INLINE_VISIBILITY
2249    shared_future(const shared_future& __rhs)  _NOEXCEPT : __state_(__rhs.__state_)
2250        {if (__state_) __state_->__add_shared();}
2251    _LIBCPP_INLINE_VISIBILITY
2252    shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
2253        {__f.__state_ = nullptr;}
2254    _LIBCPP_INLINE_VISIBILITY
2255    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2256        {__rhs.__state_ = nullptr;}
2257    _LIBCPP_HIDE_FROM_ABI ~shared_future();
2258    _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
2259    _LIBCPP_INLINE_VISIBILITY
2260    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2261        {
2262            shared_future(_VSTD::move(__rhs)).swap(*this);
2263            return *this;
2264        }
2265
2266    // retrieving the value
2267    _LIBCPP_INLINE_VISIBILITY
2268    const _Rp& get() const {return __state_->copy();}
2269
2270    _LIBCPP_INLINE_VISIBILITY
2271    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2272
2273    // functions to check state
2274    _LIBCPP_INLINE_VISIBILITY
2275    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2276
2277    _LIBCPP_INLINE_VISIBILITY
2278    void wait() const {__state_->wait();}
2279    template <class _Rep, class _Period>
2280        _LIBCPP_INLINE_VISIBILITY
2281        future_status
2282        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2283            {return __state_->wait_for(__rel_time);}
2284    template <class _Clock, class _Duration>
2285        _LIBCPP_INLINE_VISIBILITY
2286        future_status
2287        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2288            {return __state_->wait_until(__abs_time);}
2289};
2290
2291template <class _Rp>
2292shared_future<_Rp>::~shared_future()
2293{
2294    if (__state_)
2295        __state_->__release_shared();
2296}
2297
2298template <class _Rp>
2299shared_future<_Rp>&
2300shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
2301{
2302    if (__rhs.__state_)
2303        __rhs.__state_->__add_shared();
2304    if (__state_)
2305        __state_->__release_shared();
2306    __state_ = __rhs.__state_;
2307    return *this;
2308}
2309
2310template <class _Rp>
2311class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
2312{
2313    __assoc_state<_Rp&>* __state_;
2314
2315public:
2316    _LIBCPP_INLINE_VISIBILITY
2317    shared_future() _NOEXCEPT : __state_(nullptr) {}
2318    _LIBCPP_INLINE_VISIBILITY
2319    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2320        {if (__state_) __state_->__add_shared();}
2321    _LIBCPP_INLINE_VISIBILITY
2322    shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
2323        {__f.__state_ = nullptr;}
2324    _LIBCPP_INLINE_VISIBILITY
2325    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2326        {__rhs.__state_ = nullptr;}
2327    _LIBCPP_HIDE_FROM_ABI ~shared_future();
2328    _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
2329    _LIBCPP_INLINE_VISIBILITY
2330    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2331        {
2332            shared_future(_VSTD::move(__rhs)).swap(*this);
2333            return *this;
2334        }
2335
2336    // retrieving the value
2337    _LIBCPP_INLINE_VISIBILITY
2338    _Rp& get() const {return __state_->copy();}
2339
2340    _LIBCPP_INLINE_VISIBILITY
2341    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2342
2343    // functions to check state
2344    _LIBCPP_INLINE_VISIBILITY
2345    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2346
2347    _LIBCPP_INLINE_VISIBILITY
2348    void wait() const {__state_->wait();}
2349    template <class _Rep, class _Period>
2350        _LIBCPP_INLINE_VISIBILITY
2351        future_status
2352        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2353            {return __state_->wait_for(__rel_time);}
2354    template <class _Clock, class _Duration>
2355        _LIBCPP_INLINE_VISIBILITY
2356        future_status
2357        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2358            {return __state_->wait_until(__abs_time);}
2359};
2360
2361template <class _Rp>
2362shared_future<_Rp&>::~shared_future()
2363{
2364    if (__state_)
2365        __state_->__release_shared();
2366}
2367
2368template <class _Rp>
2369shared_future<_Rp&>&
2370shared_future<_Rp&>::operator=(const shared_future& __rhs)
2371{
2372    if (__rhs.__state_)
2373        __rhs.__state_->__add_shared();
2374    if (__state_)
2375        __state_->__release_shared();
2376    __state_ = __rhs.__state_;
2377    return *this;
2378}
2379
2380template <>
2381class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
2382{
2383    __assoc_sub_state* __state_;
2384
2385public:
2386    _LIBCPP_INLINE_VISIBILITY
2387    shared_future() _NOEXCEPT : __state_(nullptr) {}
2388    _LIBCPP_INLINE_VISIBILITY
2389    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2390        {if (__state_) __state_->__add_shared();}
2391    _LIBCPP_INLINE_VISIBILITY
2392    shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
2393        {__f.__state_ = nullptr;}
2394    _LIBCPP_INLINE_VISIBILITY
2395    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2396        {__rhs.__state_ = nullptr;}
2397    ~shared_future();
2398    shared_future& operator=(const shared_future& __rhs);
2399    _LIBCPP_INLINE_VISIBILITY
2400    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2401        {
2402            shared_future(_VSTD::move(__rhs)).swap(*this);
2403            return *this;
2404        }
2405
2406    // retrieving the value
2407    _LIBCPP_INLINE_VISIBILITY
2408    void get() const {__state_->copy();}
2409
2410    _LIBCPP_INLINE_VISIBILITY
2411    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2412
2413    // functions to check state
2414    _LIBCPP_INLINE_VISIBILITY
2415    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2416
2417    _LIBCPP_INLINE_VISIBILITY
2418    void wait() const {__state_->wait();}
2419    template <class _Rep, class _Period>
2420        _LIBCPP_INLINE_VISIBILITY
2421        future_status
2422        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2423            {return __state_->wait_for(__rel_time);}
2424    template <class _Clock, class _Duration>
2425        _LIBCPP_INLINE_VISIBILITY
2426        future_status
2427        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2428            {return __state_->wait_until(__abs_time);}
2429};
2430
2431template <class _Rp>
2432inline _LIBCPP_INLINE_VISIBILITY
2433void
2434swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2435{
2436    __x.swap(__y);
2437}
2438
2439template <class _Rp>
2440inline
2441shared_future<_Rp>
2442future<_Rp>::share() _NOEXCEPT
2443{
2444    return shared_future<_Rp>(_VSTD::move(*this));
2445}
2446
2447template <class _Rp>
2448inline
2449shared_future<_Rp&>
2450future<_Rp&>::share() _NOEXCEPT
2451{
2452    return shared_future<_Rp&>(_VSTD::move(*this));
2453}
2454
2455inline
2456shared_future<void>
2457future<void>::share() _NOEXCEPT
2458{
2459    return shared_future<void>(_VSTD::move(*this));
2460}
2461
2462_LIBCPP_END_NAMESPACE_STD
2463
2464#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2465#  include <chrono>
2466#endif
2467
2468#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2469#  include <atomic>
2470#  include <cstdlib>
2471#  include <exception>
2472#  include <system_error>
2473#endif
2474
2475#endif // _LIBCPP_FUTURE
2476