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