• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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();
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();
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();
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;
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        explicit 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_TYPE_VIS_ONLY is_error_code_enum<future_errc> : public true_type {};
395
396#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
397template <>
398struct _LIBCPP_TYPE_VIS_ONLY 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 future_error
503    : public logic_error
504{
505    error_code __ec_;
506public:
507    future_error(error_code __ec);
508
509    _LIBCPP_INLINE_VISIBILITY
510    const error_code& code() const _NOEXCEPT {return __ec_;}
511
512    virtual ~future_error() _NOEXCEPT;
513};
514
515class _LIBCPP_TYPE_VIS __assoc_sub_state
516    : public __shared_count
517{
518protected:
519    exception_ptr __exception_;
520    mutable mutex __mut_;
521    mutable condition_variable __cv_;
522    unsigned __state_;
523
524    virtual void __on_zero_shared() _NOEXCEPT;
525    void __sub_wait(unique_lock<mutex>& __lk);
526public:
527    enum
528    {
529        __constructed = 1,
530        __future_attached = 2,
531        ready = 4,
532        deferred = 8
533    };
534
535    _LIBCPP_INLINE_VISIBILITY
536    __assoc_sub_state() : __state_(0) {}
537
538    _LIBCPP_INLINE_VISIBILITY
539    bool __has_value() const
540        {return (__state_ & __constructed) || (__exception_ != nullptr);}
541
542    _LIBCPP_INLINE_VISIBILITY
543    void __set_future_attached()
544    {
545        lock_guard<mutex> __lk(__mut_);
546        __state_ |= __future_attached;
547    }
548    _LIBCPP_INLINE_VISIBILITY
549    bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
550
551    _LIBCPP_INLINE_VISIBILITY
552    void __set_deferred() {__state_ |= deferred;}
553
554    void __make_ready();
555    _LIBCPP_INLINE_VISIBILITY
556    bool __is_ready() const {return (__state_ & ready) != 0;}
557
558    void set_value();
559    void set_value_at_thread_exit();
560
561    void set_exception(exception_ptr __p);
562    void set_exception_at_thread_exit(exception_ptr __p);
563
564    void copy();
565
566    void wait();
567    template <class _Rep, class _Period>
568        future_status
569        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
570    template <class _Clock, class _Duration>
571        future_status
572        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
573
574    virtual void __execute();
575};
576
577template <class _Clock, class _Duration>
578future_status
579__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
580{
581    unique_lock<mutex> __lk(__mut_);
582    if (__state_ & deferred)
583        return future_status::deferred;
584    while (!(__state_ & ready) && _Clock::now() < __abs_time)
585        __cv_.wait_until(__lk, __abs_time);
586    if (__state_ & ready)
587        return future_status::ready;
588    return future_status::timeout;
589}
590
591template <class _Rep, class _Period>
592inline _LIBCPP_INLINE_VISIBILITY
593future_status
594__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
595{
596    return wait_until(chrono::steady_clock::now() + __rel_time);
597}
598
599template <class _Rp>
600class __assoc_state
601    : public __assoc_sub_state
602{
603    typedef __assoc_sub_state base;
604    typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
605protected:
606    _Up __value_;
607
608    virtual void __on_zero_shared() _NOEXCEPT;
609public:
610
611    template <class _Arg>
612#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
613        void set_value(_Arg&& __arg);
614#else
615        void set_value(_Arg& __arg);
616#endif
617
618    template <class _Arg>
619#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
620        void set_value_at_thread_exit(_Arg&& __arg);
621#else
622        void set_value_at_thread_exit(_Arg& __arg);
623#endif
624
625    _Rp move();
626    typename add_lvalue_reference<_Rp>::type copy();
627};
628
629template <class _Rp>
630void
631__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
632{
633    if (this->__state_ & base::__constructed)
634        reinterpret_cast<_Rp*>(&__value_)->~_Rp();
635    delete this;
636}
637
638template <class _Rp>
639template <class _Arg>
640void
641#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
642__assoc_state<_Rp>::set_value(_Arg&& __arg)
643#else
644__assoc_state<_Rp>::set_value(_Arg& __arg)
645#endif
646{
647    unique_lock<mutex> __lk(this->__mut_);
648#ifndef _LIBCPP_NO_EXCEPTIONS
649    if (this->__has_value())
650        throw future_error(make_error_code(future_errc::promise_already_satisfied));
651#endif
652    ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
653    this->__state_ |= base::__constructed | base::ready;
654    __lk.unlock();
655    __cv_.notify_all();
656}
657
658template <class _Rp>
659template <class _Arg>
660void
661#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
662__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
663#else
664__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
665#endif
666{
667    unique_lock<mutex> __lk(this->__mut_);
668#ifndef _LIBCPP_NO_EXCEPTIONS
669    if (this->__has_value())
670        throw future_error(make_error_code(future_errc::promise_already_satisfied));
671#endif
672    ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
673    this->__state_ |= base::__constructed;
674    __thread_local_data()->__make_ready_at_thread_exit(this);
675    __lk.unlock();
676}
677
678template <class _Rp>
679_Rp
680__assoc_state<_Rp>::move()
681{
682    unique_lock<mutex> __lk(this->__mut_);
683    this->__sub_wait(__lk);
684    if (this->__exception_ != nullptr)
685        rethrow_exception(this->__exception_);
686    return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
687}
688
689template <class _Rp>
690typename add_lvalue_reference<_Rp>::type
691__assoc_state<_Rp>::copy()
692{
693    unique_lock<mutex> __lk(this->__mut_);
694    this->__sub_wait(__lk);
695    if (this->__exception_ != nullptr)
696        rethrow_exception(this->__exception_);
697    return *reinterpret_cast<_Rp*>(&__value_);
698}
699
700template <class _Rp>
701class __assoc_state<_Rp&>
702    : public __assoc_sub_state
703{
704    typedef __assoc_sub_state base;
705    typedef _Rp* _Up;
706protected:
707    _Up __value_;
708
709    virtual void __on_zero_shared() _NOEXCEPT;
710public:
711
712    void set_value(_Rp& __arg);
713    void set_value_at_thread_exit(_Rp& __arg);
714
715    _Rp& copy();
716};
717
718template <class _Rp>
719void
720__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
721{
722    delete this;
723}
724
725template <class _Rp>
726void
727__assoc_state<_Rp&>::set_value(_Rp& __arg)
728{
729    unique_lock<mutex> __lk(this->__mut_);
730#ifndef _LIBCPP_NO_EXCEPTIONS
731    if (this->__has_value())
732        throw future_error(make_error_code(future_errc::promise_already_satisfied));
733#endif
734    __value_ = _VSTD::addressof(__arg);
735    this->__state_ |= base::__constructed | base::ready;
736    __lk.unlock();
737    __cv_.notify_all();
738}
739
740template <class _Rp>
741void
742__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
743{
744    unique_lock<mutex> __lk(this->__mut_);
745#ifndef _LIBCPP_NO_EXCEPTIONS
746    if (this->__has_value())
747        throw future_error(make_error_code(future_errc::promise_already_satisfied));
748#endif
749    __value_ = _VSTD::addressof(__arg);
750    this->__state_ |= base::__constructed;
751    __thread_local_data()->__make_ready_at_thread_exit(this);
752    __lk.unlock();
753}
754
755template <class _Rp>
756_Rp&
757__assoc_state<_Rp&>::copy()
758{
759    unique_lock<mutex> __lk(this->__mut_);
760    this->__sub_wait(__lk);
761    if (this->__exception_ != nullptr)
762        rethrow_exception(this->__exception_);
763    return *__value_;
764}
765
766template <class _Rp, class _Alloc>
767class __assoc_state_alloc
768    : public __assoc_state<_Rp>
769{
770    typedef __assoc_state<_Rp> base;
771    _Alloc __alloc_;
772
773    virtual void __on_zero_shared() _NOEXCEPT;
774public:
775    _LIBCPP_INLINE_VISIBILITY
776    explicit __assoc_state_alloc(const _Alloc& __a)
777        : __alloc_(__a) {}
778};
779
780template <class _Rp, class _Alloc>
781void
782__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
783{
784    if (this->__state_ & base::__constructed)
785        reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
786    typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
787    typedef allocator_traits<_Al> _ATraits;
788    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
789    _Al __a(__alloc_);
790    this->~__assoc_state_alloc();
791    __a.deallocate(_PTraits::pointer_to(*this), 1);
792}
793
794template <class _Rp, class _Alloc>
795class __assoc_state_alloc<_Rp&, _Alloc>
796    : public __assoc_state<_Rp&>
797{
798    typedef __assoc_state<_Rp&> base;
799    _Alloc __alloc_;
800
801    virtual void __on_zero_shared() _NOEXCEPT;
802public:
803    _LIBCPP_INLINE_VISIBILITY
804    explicit __assoc_state_alloc(const _Alloc& __a)
805        : __alloc_(__a) {}
806};
807
808template <class _Rp, class _Alloc>
809void
810__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
811{
812    typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
813    typedef allocator_traits<_Al> _ATraits;
814    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
815    _Al __a(__alloc_);
816    this->~__assoc_state_alloc();
817    __a.deallocate(_PTraits::pointer_to(*this), 1);
818}
819
820template <class _Alloc>
821class __assoc_sub_state_alloc
822    : public __assoc_sub_state
823{
824    typedef __assoc_sub_state base;
825    _Alloc __alloc_;
826
827    virtual void __on_zero_shared() _NOEXCEPT;
828public:
829    _LIBCPP_INLINE_VISIBILITY
830    explicit __assoc_sub_state_alloc(const _Alloc& __a)
831        : __alloc_(__a) {}
832};
833
834template <class _Alloc>
835void
836__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
837{
838    typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
839    typedef allocator_traits<_Al> _ATraits;
840    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
841    _Al __a(__alloc_);
842    this->~__assoc_sub_state_alloc();
843    __a.deallocate(_PTraits::pointer_to(*this), 1);
844}
845
846template <class _Rp, class _Fp>
847class __deferred_assoc_state
848    : public __assoc_state<_Rp>
849{
850    typedef __assoc_state<_Rp> base;
851
852    _Fp __func_;
853
854public:
855#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
856    explicit __deferred_assoc_state(_Fp&& __f);
857#endif
858
859    virtual void __execute();
860};
861
862#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
863
864template <class _Rp, class _Fp>
865inline _LIBCPP_INLINE_VISIBILITY
866__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
867    : __func_(_VSTD::forward<_Fp>(__f))
868{
869    this->__set_deferred();
870}
871
872#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
873
874template <class _Rp, class _Fp>
875void
876__deferred_assoc_state<_Rp, _Fp>::__execute()
877{
878#ifndef _LIBCPP_NO_EXCEPTIONS
879    try
880    {
881#endif  // _LIBCPP_NO_EXCEPTIONS
882        this->set_value(__func_());
883#ifndef _LIBCPP_NO_EXCEPTIONS
884    }
885    catch (...)
886    {
887        this->set_exception(current_exception());
888    }
889#endif  // _LIBCPP_NO_EXCEPTIONS
890}
891
892template <class _Fp>
893class __deferred_assoc_state<void, _Fp>
894    : public __assoc_sub_state
895{
896    typedef __assoc_sub_state base;
897
898    _Fp __func_;
899
900public:
901#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
902    explicit __deferred_assoc_state(_Fp&& __f);
903#endif
904
905    virtual void __execute();
906};
907
908#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
909
910template <class _Fp>
911inline _LIBCPP_INLINE_VISIBILITY
912__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
913    : __func_(_VSTD::forward<_Fp>(__f))
914{
915    this->__set_deferred();
916}
917
918#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
919
920template <class _Fp>
921void
922__deferred_assoc_state<void, _Fp>::__execute()
923{
924#ifndef _LIBCPP_NO_EXCEPTIONS
925    try
926    {
927#endif  // _LIBCPP_NO_EXCEPTIONS
928        __func_();
929        this->set_value();
930#ifndef _LIBCPP_NO_EXCEPTIONS
931    }
932    catch (...)
933    {
934        this->set_exception(current_exception());
935    }
936#endif  // _LIBCPP_NO_EXCEPTIONS
937}
938
939template <class _Rp, class _Fp>
940class __async_assoc_state
941    : public __assoc_state<_Rp>
942{
943    typedef __assoc_state<_Rp> base;
944
945    _Fp __func_;
946
947    virtual void __on_zero_shared() _NOEXCEPT;
948public:
949#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
950    explicit __async_assoc_state(_Fp&& __f);
951#endif
952
953    virtual void __execute();
954};
955
956#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
957
958template <class _Rp, class _Fp>
959inline _LIBCPP_INLINE_VISIBILITY
960__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
961    : __func_(_VSTD::forward<_Fp>(__f))
962{
963}
964
965#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
966
967template <class _Rp, class _Fp>
968void
969__async_assoc_state<_Rp, _Fp>::__execute()
970{
971#ifndef _LIBCPP_NO_EXCEPTIONS
972    try
973    {
974#endif  // _LIBCPP_NO_EXCEPTIONS
975        this->set_value(__func_());
976#ifndef _LIBCPP_NO_EXCEPTIONS
977    }
978    catch (...)
979    {
980        this->set_exception(current_exception());
981    }
982#endif  // _LIBCPP_NO_EXCEPTIONS
983}
984
985template <class _Rp, class _Fp>
986void
987__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
988{
989    this->wait();
990    base::__on_zero_shared();
991}
992
993template <class _Fp>
994class __async_assoc_state<void, _Fp>
995    : public __assoc_sub_state
996{
997    typedef __assoc_sub_state base;
998
999    _Fp __func_;
1000
1001    virtual void __on_zero_shared() _NOEXCEPT;
1002public:
1003#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1004    explicit __async_assoc_state(_Fp&& __f);
1005#endif
1006
1007    virtual void __execute();
1008};
1009
1010#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1011
1012template <class _Fp>
1013inline _LIBCPP_INLINE_VISIBILITY
1014__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1015    : __func_(_VSTD::forward<_Fp>(__f))
1016{
1017}
1018
1019#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1020
1021template <class _Fp>
1022void
1023__async_assoc_state<void, _Fp>::__execute()
1024{
1025#ifndef _LIBCPP_NO_EXCEPTIONS
1026    try
1027    {
1028#endif  // _LIBCPP_NO_EXCEPTIONS
1029        __func_();
1030        this->set_value();
1031#ifndef _LIBCPP_NO_EXCEPTIONS
1032    }
1033    catch (...)
1034    {
1035        this->set_exception(current_exception());
1036    }
1037#endif  // _LIBCPP_NO_EXCEPTIONS
1038}
1039
1040template <class _Fp>
1041void
1042__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1043{
1044    this->wait();
1045    base::__on_zero_shared();
1046}
1047
1048template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY promise;
1049template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY shared_future;
1050
1051// future
1052
1053template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY future;
1054
1055template <class _Rp, class _Fp>
1056future<_Rp>
1057#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1058__make_deferred_assoc_state(_Fp&& __f);
1059#else
1060__make_deferred_assoc_state(_Fp __f);
1061#endif
1062
1063template <class _Rp, class _Fp>
1064future<_Rp>
1065#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1066__make_async_assoc_state(_Fp&& __f);
1067#else
1068__make_async_assoc_state(_Fp __f);
1069#endif
1070
1071template <class _Rp>
1072class _LIBCPP_TYPE_VIS_ONLY future
1073{
1074    __assoc_state<_Rp>* __state_;
1075
1076    explicit future(__assoc_state<_Rp>* __state);
1077
1078    template <class> friend class promise;
1079    template <class> friend class shared_future;
1080
1081#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1082    template <class _R1, class _Fp>
1083        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1084    template <class _R1, class _Fp>
1085        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1086#else
1087    template <class _R1, class _Fp>
1088        friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1089    template <class _R1, class _Fp>
1090        friend future<_R1> __make_async_assoc_state(_Fp __f);
1091#endif
1092
1093public:
1094    _LIBCPP_INLINE_VISIBILITY
1095    future() _NOEXCEPT : __state_(nullptr) {}
1096#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1097    _LIBCPP_INLINE_VISIBILITY
1098    future(future&& __rhs) _NOEXCEPT
1099        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1100    future(const future&) = delete;
1101    future& operator=(const future&) = delete;
1102    _LIBCPP_INLINE_VISIBILITY
1103    future& operator=(future&& __rhs) _NOEXCEPT
1104        {
1105            future(std::move(__rhs)).swap(*this);
1106            return *this;
1107        }
1108#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1109private:
1110    future(const future&);
1111    future& operator=(const future&);
1112public:
1113#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1114    ~future();
1115    shared_future<_Rp> share();
1116
1117    // retrieving the value
1118    _Rp get();
1119
1120    _LIBCPP_INLINE_VISIBILITY
1121    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1122
1123    // functions to check state
1124    _LIBCPP_INLINE_VISIBILITY
1125    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1126
1127    _LIBCPP_INLINE_VISIBILITY
1128    void wait() const {__state_->wait();}
1129    template <class _Rep, class _Period>
1130        _LIBCPP_INLINE_VISIBILITY
1131        future_status
1132        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1133            {return __state_->wait_for(__rel_time);}
1134    template <class _Clock, class _Duration>
1135        _LIBCPP_INLINE_VISIBILITY
1136        future_status
1137        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1138            {return __state_->wait_until(__abs_time);}
1139};
1140
1141template <class _Rp>
1142future<_Rp>::future(__assoc_state<_Rp>* __state)
1143    : __state_(__state)
1144{
1145#ifndef _LIBCPP_NO_EXCEPTIONS
1146    if (__state_->__has_future_attached())
1147        throw future_error(make_error_code(future_errc::future_already_retrieved));
1148#endif
1149    __state_->__add_shared();
1150    __state_->__set_future_attached();
1151}
1152
1153struct __release_shared_count
1154{
1155    void operator()(__shared_count* p) {p->__release_shared();}
1156};
1157
1158template <class _Rp>
1159future<_Rp>::~future()
1160{
1161    if (__state_)
1162        __state_->__release_shared();
1163}
1164
1165template <class _Rp>
1166_Rp
1167future<_Rp>::get()
1168{
1169    unique_ptr<__shared_count, __release_shared_count> __(__state_);
1170    __assoc_state<_Rp>* __s = __state_;
1171    __state_ = nullptr;
1172    return __s->move();
1173}
1174
1175template <class _Rp>
1176class _LIBCPP_TYPE_VIS_ONLY future<_Rp&>
1177{
1178    __assoc_state<_Rp&>* __state_;
1179
1180    explicit future(__assoc_state<_Rp&>* __state);
1181
1182    template <class> friend class promise;
1183    template <class> friend class shared_future;
1184
1185#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1186    template <class _R1, class _Fp>
1187        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1188    template <class _R1, class _Fp>
1189        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1190#else
1191    template <class _R1, class _Fp>
1192        friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1193    template <class _R1, class _Fp>
1194        friend future<_R1> __make_async_assoc_state(_Fp __f);
1195#endif
1196
1197public:
1198    _LIBCPP_INLINE_VISIBILITY
1199    future() _NOEXCEPT : __state_(nullptr) {}
1200#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1201    _LIBCPP_INLINE_VISIBILITY
1202    future(future&& __rhs) _NOEXCEPT
1203        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1204    future(const future&) = delete;
1205    future& operator=(const future&) = delete;
1206    _LIBCPP_INLINE_VISIBILITY
1207    future& operator=(future&& __rhs) _NOEXCEPT
1208        {
1209            future(std::move(__rhs)).swap(*this);
1210            return *this;
1211        }
1212#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1213private:
1214    future(const future&);
1215    future& operator=(const future&);
1216public:
1217#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1218    ~future();
1219    shared_future<_Rp&> share();
1220
1221    // retrieving the value
1222    _Rp& get();
1223
1224    _LIBCPP_INLINE_VISIBILITY
1225    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1226
1227    // functions to check state
1228    _LIBCPP_INLINE_VISIBILITY
1229    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1230
1231    _LIBCPP_INLINE_VISIBILITY
1232    void wait() const {__state_->wait();}
1233    template <class _Rep, class _Period>
1234        _LIBCPP_INLINE_VISIBILITY
1235        future_status
1236        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1237            {return __state_->wait_for(__rel_time);}
1238    template <class _Clock, class _Duration>
1239        _LIBCPP_INLINE_VISIBILITY
1240        future_status
1241        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1242            {return __state_->wait_until(__abs_time);}
1243};
1244
1245template <class _Rp>
1246future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1247    : __state_(__state)
1248{
1249#ifndef _LIBCPP_NO_EXCEPTIONS
1250    if (__state_->__has_future_attached())
1251        throw future_error(make_error_code(future_errc::future_already_retrieved));
1252#endif
1253    __state_->__add_shared();
1254    __state_->__set_future_attached();
1255}
1256
1257template <class _Rp>
1258future<_Rp&>::~future()
1259{
1260    if (__state_)
1261        __state_->__release_shared();
1262}
1263
1264template <class _Rp>
1265_Rp&
1266future<_Rp&>::get()
1267{
1268    unique_ptr<__shared_count, __release_shared_count> __(__state_);
1269    __assoc_state<_Rp&>* __s = __state_;
1270    __state_ = nullptr;
1271    return __s->copy();
1272}
1273
1274template <>
1275class _LIBCPP_TYPE_VIS future<void>
1276{
1277    __assoc_sub_state* __state_;
1278
1279    explicit future(__assoc_sub_state* __state);
1280
1281    template <class> friend class promise;
1282    template <class> friend class shared_future;
1283
1284#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1285    template <class _R1, class _Fp>
1286        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1287    template <class _R1, class _Fp>
1288        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1289#else
1290    template <class _R1, class _Fp>
1291        friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1292    template <class _R1, class _Fp>
1293        friend future<_R1> __make_async_assoc_state(_Fp __f);
1294#endif
1295
1296public:
1297    _LIBCPP_INLINE_VISIBILITY
1298    future() _NOEXCEPT : __state_(nullptr) {}
1299#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1300    _LIBCPP_INLINE_VISIBILITY
1301    future(future&& __rhs) _NOEXCEPT
1302        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1303    future(const future&) = delete;
1304    future& operator=(const future&) = delete;
1305    _LIBCPP_INLINE_VISIBILITY
1306    future& operator=(future&& __rhs) _NOEXCEPT
1307        {
1308            future(std::move(__rhs)).swap(*this);
1309            return *this;
1310        }
1311#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1312private:
1313    future(const future&);
1314    future& operator=(const future&);
1315public:
1316#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1317    ~future();
1318    shared_future<void> share();
1319
1320    // retrieving the value
1321    void get();
1322
1323    _LIBCPP_INLINE_VISIBILITY
1324    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1325
1326    // functions to check state
1327    _LIBCPP_INLINE_VISIBILITY
1328    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1329
1330    _LIBCPP_INLINE_VISIBILITY
1331    void wait() const {__state_->wait();}
1332    template <class _Rep, class _Period>
1333        _LIBCPP_INLINE_VISIBILITY
1334        future_status
1335        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1336            {return __state_->wait_for(__rel_time);}
1337    template <class _Clock, class _Duration>
1338        _LIBCPP_INLINE_VISIBILITY
1339        future_status
1340        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1341            {return __state_->wait_until(__abs_time);}
1342};
1343
1344template <class _Rp>
1345inline _LIBCPP_INLINE_VISIBILITY
1346void
1347swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1348{
1349    __x.swap(__y);
1350}
1351
1352// promise<R>
1353
1354template <class _Callable> class packaged_task;
1355
1356template <class _Rp>
1357class _LIBCPP_TYPE_VIS_ONLY promise
1358{
1359    __assoc_state<_Rp>* __state_;
1360
1361    _LIBCPP_INLINE_VISIBILITY
1362    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1363
1364    template <class> friend class packaged_task;
1365public:
1366    promise();
1367    template <class _Alloc>
1368        promise(allocator_arg_t, const _Alloc& __a);
1369#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1370    _LIBCPP_INLINE_VISIBILITY
1371    promise(promise&& __rhs) _NOEXCEPT
1372        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1373    promise(const promise& __rhs) = delete;
1374#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1375private:
1376    promise(const promise& __rhs);
1377public:
1378#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1379    ~promise();
1380
1381    // assignment
1382#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1383    _LIBCPP_INLINE_VISIBILITY
1384    promise& operator=(promise&& __rhs) _NOEXCEPT
1385        {
1386            promise(std::move(__rhs)).swap(*this);
1387            return *this;
1388        }
1389    promise& operator=(const promise& __rhs) = delete;
1390#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1391private:
1392    promise& operator=(const promise& __rhs);
1393public:
1394#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1395    _LIBCPP_INLINE_VISIBILITY
1396    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1397
1398    // retrieving the result
1399    future<_Rp> get_future();
1400
1401    // setting the result
1402    void set_value(const _Rp& __r);
1403#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1404    void set_value(_Rp&& __r);
1405#endif
1406    void set_exception(exception_ptr __p);
1407
1408    // setting the result with deferred notification
1409    void set_value_at_thread_exit(const _Rp& __r);
1410#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1411    void set_value_at_thread_exit(_Rp&& __r);
1412#endif
1413    void set_exception_at_thread_exit(exception_ptr __p);
1414};
1415
1416template <class _Rp>
1417promise<_Rp>::promise()
1418    : __state_(new __assoc_state<_Rp>)
1419{
1420}
1421
1422template <class _Rp>
1423template <class _Alloc>
1424promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1425{
1426    typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1427    typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1428    typedef __allocator_destructor<_A2> _D2;
1429    _A2 __a(__a0);
1430    unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1431    ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1432    __state_ = _VSTD::addressof(*__hold.release());
1433}
1434
1435template <class _Rp>
1436promise<_Rp>::~promise()
1437{
1438    if (__state_)
1439    {
1440        if (!__state_->__has_value() && __state_->use_count() > 1)
1441            __state_->set_exception(make_exception_ptr(
1442                      future_error(make_error_code(future_errc::broken_promise))
1443                                                      ));
1444        __state_->__release_shared();
1445    }
1446}
1447
1448template <class _Rp>
1449future<_Rp>
1450promise<_Rp>::get_future()
1451{
1452#ifndef _LIBCPP_NO_EXCEPTIONS
1453    if (__state_ == nullptr)
1454        throw future_error(make_error_code(future_errc::no_state));
1455#endif
1456    return future<_Rp>(__state_);
1457}
1458
1459template <class _Rp>
1460void
1461promise<_Rp>::set_value(const _Rp& __r)
1462{
1463#ifndef _LIBCPP_NO_EXCEPTIONS
1464    if (__state_ == nullptr)
1465        throw future_error(make_error_code(future_errc::no_state));
1466#endif
1467    __state_->set_value(__r);
1468}
1469
1470#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1471
1472template <class _Rp>
1473void
1474promise<_Rp>::set_value(_Rp&& __r)
1475{
1476#ifndef _LIBCPP_NO_EXCEPTIONS
1477    if (__state_ == nullptr)
1478        throw future_error(make_error_code(future_errc::no_state));
1479#endif
1480    __state_->set_value(_VSTD::move(__r));
1481}
1482
1483#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1484
1485template <class _Rp>
1486void
1487promise<_Rp>::set_exception(exception_ptr __p)
1488{
1489#ifndef _LIBCPP_NO_EXCEPTIONS
1490    if (__state_ == nullptr)
1491        throw future_error(make_error_code(future_errc::no_state));
1492#endif
1493    __state_->set_exception(__p);
1494}
1495
1496template <class _Rp>
1497void
1498promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1499{
1500#ifndef _LIBCPP_NO_EXCEPTIONS
1501    if (__state_ == nullptr)
1502        throw future_error(make_error_code(future_errc::no_state));
1503#endif
1504    __state_->set_value_at_thread_exit(__r);
1505}
1506
1507#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1508
1509template <class _Rp>
1510void
1511promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1512{
1513#ifndef _LIBCPP_NO_EXCEPTIONS
1514    if (__state_ == nullptr)
1515        throw future_error(make_error_code(future_errc::no_state));
1516#endif
1517    __state_->set_value_at_thread_exit(_VSTD::move(__r));
1518}
1519
1520#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1521
1522template <class _Rp>
1523void
1524promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
1525{
1526#ifndef _LIBCPP_NO_EXCEPTIONS
1527    if (__state_ == nullptr)
1528        throw future_error(make_error_code(future_errc::no_state));
1529#endif
1530    __state_->set_exception_at_thread_exit(__p);
1531}
1532
1533// promise<R&>
1534
1535template <class _Rp>
1536class _LIBCPP_TYPE_VIS_ONLY promise<_Rp&>
1537{
1538    __assoc_state<_Rp&>* __state_;
1539
1540    _LIBCPP_INLINE_VISIBILITY
1541    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1542
1543    template <class> friend class packaged_task;
1544
1545public:
1546    promise();
1547    template <class _Allocator>
1548        promise(allocator_arg_t, const _Allocator& __a);
1549#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1550    _LIBCPP_INLINE_VISIBILITY
1551    promise(promise&& __rhs) _NOEXCEPT
1552        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1553    promise(const promise& __rhs) = delete;
1554#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1555private:
1556    promise(const promise& __rhs);
1557public:
1558#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1559    ~promise();
1560
1561    // assignment
1562#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1563    _LIBCPP_INLINE_VISIBILITY
1564    promise& operator=(promise&& __rhs) _NOEXCEPT
1565        {
1566            promise(std::move(__rhs)).swap(*this);
1567            return *this;
1568        }
1569    promise& operator=(const promise& __rhs) = delete;
1570#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1571private:
1572    promise& operator=(const promise& __rhs);
1573public:
1574#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1575    _LIBCPP_INLINE_VISIBILITY
1576    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1577
1578    // retrieving the result
1579    future<_Rp&> get_future();
1580
1581    // setting the result
1582    void set_value(_Rp& __r);
1583    void set_exception(exception_ptr __p);
1584
1585    // setting the result with deferred notification
1586    void set_value_at_thread_exit(_Rp&);
1587    void set_exception_at_thread_exit(exception_ptr __p);
1588};
1589
1590template <class _Rp>
1591promise<_Rp&>::promise()
1592    : __state_(new __assoc_state<_Rp&>)
1593{
1594}
1595
1596template <class _Rp>
1597template <class _Alloc>
1598promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1599{
1600    typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1601    typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1602    typedef __allocator_destructor<_A2> _D2;
1603    _A2 __a(__a0);
1604    unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1605    ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1606    __state_ = _VSTD::addressof(*__hold.release());
1607}
1608
1609template <class _Rp>
1610promise<_Rp&>::~promise()
1611{
1612    if (__state_)
1613    {
1614        if (!__state_->__has_value() && __state_->use_count() > 1)
1615            __state_->set_exception(make_exception_ptr(
1616                      future_error(make_error_code(future_errc::broken_promise))
1617                                                      ));
1618        __state_->__release_shared();
1619    }
1620}
1621
1622template <class _Rp>
1623future<_Rp&>
1624promise<_Rp&>::get_future()
1625{
1626#ifndef _LIBCPP_NO_EXCEPTIONS
1627    if (__state_ == nullptr)
1628        throw future_error(make_error_code(future_errc::no_state));
1629#endif
1630    return future<_Rp&>(__state_);
1631}
1632
1633template <class _Rp>
1634void
1635promise<_Rp&>::set_value(_Rp& __r)
1636{
1637#ifndef _LIBCPP_NO_EXCEPTIONS
1638    if (__state_ == nullptr)
1639        throw future_error(make_error_code(future_errc::no_state));
1640#endif
1641    __state_->set_value(__r);
1642}
1643
1644template <class _Rp>
1645void
1646promise<_Rp&>::set_exception(exception_ptr __p)
1647{
1648#ifndef _LIBCPP_NO_EXCEPTIONS
1649    if (__state_ == nullptr)
1650        throw future_error(make_error_code(future_errc::no_state));
1651#endif
1652    __state_->set_exception(__p);
1653}
1654
1655template <class _Rp>
1656void
1657promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1658{
1659#ifndef _LIBCPP_NO_EXCEPTIONS
1660    if (__state_ == nullptr)
1661        throw future_error(make_error_code(future_errc::no_state));
1662#endif
1663    __state_->set_value_at_thread_exit(__r);
1664}
1665
1666template <class _Rp>
1667void
1668promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
1669{
1670#ifndef _LIBCPP_NO_EXCEPTIONS
1671    if (__state_ == nullptr)
1672        throw future_error(make_error_code(future_errc::no_state));
1673#endif
1674    __state_->set_exception_at_thread_exit(__p);
1675}
1676
1677// promise<void>
1678
1679template <>
1680class _LIBCPP_TYPE_VIS promise<void>
1681{
1682    __assoc_sub_state* __state_;
1683
1684    _LIBCPP_INLINE_VISIBILITY
1685    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1686
1687    template <class> friend class packaged_task;
1688
1689public:
1690    promise();
1691    template <class _Allocator>
1692        promise(allocator_arg_t, const _Allocator& __a);
1693#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1694    _LIBCPP_INLINE_VISIBILITY
1695    promise(promise&& __rhs) _NOEXCEPT
1696        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1697    promise(const promise& __rhs) = delete;
1698#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1699private:
1700    promise(const promise& __rhs);
1701public:
1702#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1703    ~promise();
1704
1705    // assignment
1706#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1707    _LIBCPP_INLINE_VISIBILITY
1708    promise& operator=(promise&& __rhs) _NOEXCEPT
1709        {
1710            promise(std::move(__rhs)).swap(*this);
1711            return *this;
1712        }
1713    promise& operator=(const promise& __rhs) = delete;
1714#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1715private:
1716    promise& operator=(const promise& __rhs);
1717public:
1718#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1719    _LIBCPP_INLINE_VISIBILITY
1720    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1721
1722    // retrieving the result
1723    future<void> get_future();
1724
1725    // setting the result
1726    void set_value();
1727    void set_exception(exception_ptr __p);
1728
1729    // setting the result with deferred notification
1730    void set_value_at_thread_exit();
1731    void set_exception_at_thread_exit(exception_ptr __p);
1732};
1733
1734template <class _Alloc>
1735promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1736{
1737    typedef __assoc_sub_state_alloc<_Alloc> _State;
1738    typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1739    typedef __allocator_destructor<_A2> _D2;
1740    _A2 __a(__a0);
1741    unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1742    ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1743    __state_ = _VSTD::addressof(*__hold.release());
1744}
1745
1746template <class _Rp>
1747inline _LIBCPP_INLINE_VISIBILITY
1748void
1749swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1750{
1751    __x.swap(__y);
1752}
1753
1754template <class _Rp, class _Alloc>
1755    struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<promise<_Rp>, _Alloc>
1756        : public true_type {};
1757
1758#ifndef _LIBCPP_HAS_NO_VARIADICS
1759
1760// packaged_task
1761
1762template<class _Fp> class __packaged_task_base;
1763
1764template<class _Rp, class ..._ArgTypes>
1765class __packaged_task_base<_Rp(_ArgTypes...)>
1766{
1767    __packaged_task_base(const __packaged_task_base&);
1768    __packaged_task_base& operator=(const __packaged_task_base&);
1769public:
1770    _LIBCPP_INLINE_VISIBILITY
1771    __packaged_task_base() {}
1772    _LIBCPP_INLINE_VISIBILITY
1773    virtual ~__packaged_task_base() {}
1774    virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1775    virtual void destroy() = 0;
1776    virtual void destroy_deallocate() = 0;
1777    virtual _Rp operator()(_ArgTypes&& ...) = 0;
1778};
1779
1780template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1781
1782template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1783class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1784    : public  __packaged_task_base<_Rp(_ArgTypes...)>
1785{
1786    __compressed_pair<_Fp, _Alloc> __f_;
1787public:
1788    _LIBCPP_INLINE_VISIBILITY
1789    explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
1790    _LIBCPP_INLINE_VISIBILITY
1791    explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
1792    _LIBCPP_INLINE_VISIBILITY
1793    __packaged_task_func(const _Fp& __f, const _Alloc& __a)
1794        : __f_(__f, __a) {}
1795    _LIBCPP_INLINE_VISIBILITY
1796    __packaged_task_func(_Fp&& __f, const _Alloc& __a)
1797        : __f_(_VSTD::move(__f), __a) {}
1798    virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1799    virtual void destroy();
1800    virtual void destroy_deallocate();
1801    virtual _Rp operator()(_ArgTypes&& ... __args);
1802};
1803
1804template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1805void
1806__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1807                              __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1808{
1809    ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1810}
1811
1812template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1813void
1814__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1815{
1816    __f_.~__compressed_pair<_Fp, _Alloc>();
1817}
1818
1819template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1820void
1821__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
1822{
1823    typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1824    typedef allocator_traits<_Ap> _ATraits;
1825    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1826    _Ap __a(__f_.second());
1827    __f_.~__compressed_pair<_Fp, _Alloc>();
1828    __a.deallocate(_PTraits::pointer_to(*this), 1);
1829}
1830
1831template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1832_Rp
1833__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1834{
1835    return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1836}
1837
1838template <class _Callable> class __packaged_task_function;
1839
1840template<class _Rp, class ..._ArgTypes>
1841class __packaged_task_function<_Rp(_ArgTypes...)>
1842{
1843    typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1844    typename aligned_storage<3*sizeof(void*)>::type __buf_;
1845    __base* __f_;
1846
1847public:
1848    typedef _Rp result_type;
1849
1850    // construct/copy/destroy:
1851    _LIBCPP_INLINE_VISIBILITY
1852    __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1853    template<class _Fp>
1854      __packaged_task_function(_Fp&& __f);
1855    template<class _Fp, class _Alloc>
1856      __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1857
1858    __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1859    __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1860
1861    __packaged_task_function(const __packaged_task_function&) =  delete;
1862    __packaged_task_function& operator=(const __packaged_task_function&) =  delete;
1863
1864    ~__packaged_task_function();
1865
1866    void swap(__packaged_task_function&) _NOEXCEPT;
1867
1868    _Rp operator()(_ArgTypes...) const;
1869};
1870
1871template<class _Rp, class ..._ArgTypes>
1872__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1873{
1874    if (__f.__f_ == nullptr)
1875        __f_ = nullptr;
1876    else if (__f.__f_ == (__base*)&__f.__buf_)
1877    {
1878        __f_ = (__base*)&__buf_;
1879        __f.__f_->__move_to(__f_);
1880    }
1881    else
1882    {
1883        __f_ = __f.__f_;
1884        __f.__f_ = nullptr;
1885    }
1886}
1887
1888template<class _Rp, class ..._ArgTypes>
1889template <class _Fp>
1890__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
1891    : __f_(nullptr)
1892{
1893    typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
1894    typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1895    if (sizeof(_FF) <= sizeof(__buf_))
1896    {
1897        __f_ = (__base*)&__buf_;
1898        ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1899    }
1900    else
1901    {
1902        typedef allocator<_FF> _Ap;
1903        _Ap __a;
1904        typedef __allocator_destructor<_Ap> _Dp;
1905        unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1906        ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
1907        __f_ = __hold.release();
1908    }
1909}
1910
1911template<class _Rp, class ..._ArgTypes>
1912template <class _Fp, class _Alloc>
1913__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1914                                  allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1915    : __f_(nullptr)
1916{
1917    typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
1918    typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1919    if (sizeof(_FF) <= sizeof(__buf_))
1920    {
1921        __f_ = (__base*)&__buf_;
1922        ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1923    }
1924    else
1925    {
1926        typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1927        _Ap __a(__a0);
1928        typedef __allocator_destructor<_Ap> _Dp;
1929        unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1930        ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1931            _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1932        __f_ = _VSTD::addressof(*__hold.release());
1933    }
1934}
1935
1936template<class _Rp, class ..._ArgTypes>
1937__packaged_task_function<_Rp(_ArgTypes...)>&
1938__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1939{
1940    if (__f_ == (__base*)&__buf_)
1941        __f_->destroy();
1942    else if (__f_)
1943        __f_->destroy_deallocate();
1944    __f_ = nullptr;
1945    if (__f.__f_ == nullptr)
1946        __f_ = nullptr;
1947    else if (__f.__f_ == (__base*)&__f.__buf_)
1948    {
1949        __f_ = (__base*)&__buf_;
1950        __f.__f_->__move_to(__f_);
1951    }
1952    else
1953    {
1954        __f_ = __f.__f_;
1955        __f.__f_ = nullptr;
1956    }
1957    return *this;
1958}
1959
1960template<class _Rp, class ..._ArgTypes>
1961__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1962{
1963    if (__f_ == (__base*)&__buf_)
1964        __f_->destroy();
1965    else if (__f_)
1966        __f_->destroy_deallocate();
1967}
1968
1969template<class _Rp, class ..._ArgTypes>
1970void
1971__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1972{
1973    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1974    {
1975        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1976        __base* __t = (__base*)&__tempbuf;
1977        __f_->__move_to(__t);
1978        __f_->destroy();
1979        __f_ = nullptr;
1980        __f.__f_->__move_to((__base*)&__buf_);
1981        __f.__f_->destroy();
1982        __f.__f_ = nullptr;
1983        __f_ = (__base*)&__buf_;
1984        __t->__move_to((__base*)&__f.__buf_);
1985        __t->destroy();
1986        __f.__f_ = (__base*)&__f.__buf_;
1987    }
1988    else if (__f_ == (__base*)&__buf_)
1989    {
1990        __f_->__move_to((__base*)&__f.__buf_);
1991        __f_->destroy();
1992        __f_ = __f.__f_;
1993        __f.__f_ = (__base*)&__f.__buf_;
1994    }
1995    else if (__f.__f_ == (__base*)&__f.__buf_)
1996    {
1997        __f.__f_->__move_to((__base*)&__buf_);
1998        __f.__f_->destroy();
1999        __f.__f_ = __f_;
2000        __f_ = (__base*)&__buf_;
2001    }
2002    else
2003        _VSTD::swap(__f_, __f.__f_);
2004}
2005
2006template<class _Rp, class ..._ArgTypes>
2007inline _LIBCPP_INLINE_VISIBILITY
2008_Rp
2009__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
2010{
2011    return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
2012}
2013
2014template<class _Rp, class ..._ArgTypes>
2015class _LIBCPP_TYPE_VIS_ONLY packaged_task<_Rp(_ArgTypes...)>
2016{
2017public:
2018    typedef _Rp result_type;
2019
2020private:
2021    __packaged_task_function<result_type(_ArgTypes...)> __f_;
2022    promise<result_type>                                __p_;
2023
2024public:
2025    // construction and destruction
2026    _LIBCPP_INLINE_VISIBILITY
2027    packaged_task() _NOEXCEPT : __p_(nullptr) {}
2028    template <class _Fp,
2029              class = typename enable_if
2030              <
2031                  !is_same<
2032                      typename decay<_Fp>::type,
2033                      packaged_task
2034                      >::value
2035                  >::type
2036             >
2037        _LIBCPP_INLINE_VISIBILITY
2038        explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2039    template <class _Fp, class _Allocator,
2040              class = typename enable_if
2041              <
2042                  !is_same<
2043                      typename decay<_Fp>::type,
2044                      packaged_task
2045                      >::value
2046                  >::type
2047              >
2048        _LIBCPP_INLINE_VISIBILITY
2049        explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2050             : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2051               __p_(allocator_arg, __a) {}
2052    // ~packaged_task() = default;
2053
2054    // no copy
2055    packaged_task(const packaged_task&) = delete;
2056    packaged_task& operator=(const packaged_task&) = delete;
2057
2058    // move support
2059    _LIBCPP_INLINE_VISIBILITY
2060    packaged_task(packaged_task&& __other) _NOEXCEPT
2061        : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2062    _LIBCPP_INLINE_VISIBILITY
2063    packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2064    {
2065        __f_ = _VSTD::move(__other.__f_);
2066        __p_ = _VSTD::move(__other.__p_);
2067        return *this;
2068    }
2069    _LIBCPP_INLINE_VISIBILITY
2070    void swap(packaged_task& __other) _NOEXCEPT
2071    {
2072        __f_.swap(__other.__f_);
2073        __p_.swap(__other.__p_);
2074    }
2075
2076    _LIBCPP_INLINE_VISIBILITY
2077    bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2078
2079    // result retrieval
2080    _LIBCPP_INLINE_VISIBILITY
2081    future<result_type> get_future() {return __p_.get_future();}
2082
2083    // execution
2084    void operator()(_ArgTypes... __args);
2085    void make_ready_at_thread_exit(_ArgTypes... __args);
2086
2087    void reset();
2088};
2089
2090template<class _Rp, class ..._ArgTypes>
2091void
2092packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
2093{
2094#ifndef _LIBCPP_NO_EXCEPTIONS
2095    if (__p_.__state_ == nullptr)
2096        throw future_error(make_error_code(future_errc::no_state));
2097    if (__p_.__state_->__has_value())
2098        throw future_error(make_error_code(future_errc::promise_already_satisfied));
2099    try
2100    {
2101#endif  // _LIBCPP_NO_EXCEPTIONS
2102        __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2103#ifndef _LIBCPP_NO_EXCEPTIONS
2104    }
2105    catch (...)
2106    {
2107        __p_.set_exception(current_exception());
2108    }
2109#endif  // _LIBCPP_NO_EXCEPTIONS
2110}
2111
2112template<class _Rp, class ..._ArgTypes>
2113void
2114packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2115{
2116#ifndef _LIBCPP_NO_EXCEPTIONS
2117    if (__p_.__state_ == nullptr)
2118        throw future_error(make_error_code(future_errc::no_state));
2119    if (__p_.__state_->__has_value())
2120        throw future_error(make_error_code(future_errc::promise_already_satisfied));
2121    try
2122    {
2123#endif  // _LIBCPP_NO_EXCEPTIONS
2124        __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2125#ifndef _LIBCPP_NO_EXCEPTIONS
2126    }
2127    catch (...)
2128    {
2129        __p_.set_exception_at_thread_exit(current_exception());
2130    }
2131#endif  // _LIBCPP_NO_EXCEPTIONS
2132}
2133
2134template<class _Rp, class ..._ArgTypes>
2135void
2136packaged_task<_Rp(_ArgTypes...)>::reset()
2137{
2138#ifndef _LIBCPP_NO_EXCEPTIONS
2139    if (!valid())
2140        throw future_error(make_error_code(future_errc::no_state));
2141#endif  // _LIBCPP_NO_EXCEPTIONS
2142    __p_ = promise<result_type>();
2143}
2144
2145template<class ..._ArgTypes>
2146class _LIBCPP_TYPE_VIS_ONLY packaged_task<void(_ArgTypes...)>
2147{
2148public:
2149    typedef void result_type;
2150
2151private:
2152    __packaged_task_function<result_type(_ArgTypes...)> __f_;
2153    promise<result_type>                                __p_;
2154
2155public:
2156    // construction and destruction
2157    _LIBCPP_INLINE_VISIBILITY
2158    packaged_task() _NOEXCEPT : __p_(nullptr) {}
2159    template <class _Fp,
2160              class = typename enable_if
2161              <
2162                  !is_same<
2163                      typename decay<_Fp>::type,
2164                      packaged_task
2165                      >::value
2166                  >::type
2167              >
2168        _LIBCPP_INLINE_VISIBILITY
2169        explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2170    template <class _Fp, class _Allocator,
2171              class = typename enable_if
2172              <
2173                  !is_same<
2174                      typename decay<_Fp>::type,
2175                      packaged_task
2176                      >::value
2177                  >::type
2178              >
2179        _LIBCPP_INLINE_VISIBILITY
2180        explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2181             : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2182               __p_(allocator_arg, __a) {}
2183    // ~packaged_task() = default;
2184
2185    // no copy
2186    packaged_task(const packaged_task&) = delete;
2187    packaged_task& operator=(const packaged_task&) = delete;
2188
2189    // move support
2190    _LIBCPP_INLINE_VISIBILITY
2191    packaged_task(packaged_task&& __other) _NOEXCEPT
2192        : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2193    _LIBCPP_INLINE_VISIBILITY
2194    packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2195    {
2196        __f_ = _VSTD::move(__other.__f_);
2197        __p_ = _VSTD::move(__other.__p_);
2198        return *this;
2199    }
2200    _LIBCPP_INLINE_VISIBILITY
2201    void swap(packaged_task& __other) _NOEXCEPT
2202    {
2203        __f_.swap(__other.__f_);
2204        __p_.swap(__other.__p_);
2205    }
2206
2207    _LIBCPP_INLINE_VISIBILITY
2208    bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2209
2210    // result retrieval
2211    _LIBCPP_INLINE_VISIBILITY
2212    future<result_type> get_future() {return __p_.get_future();}
2213
2214    // execution
2215    void operator()(_ArgTypes... __args);
2216    void make_ready_at_thread_exit(_ArgTypes... __args);
2217
2218    void reset();
2219};
2220
2221template<class ..._ArgTypes>
2222void
2223packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2224{
2225#ifndef _LIBCPP_NO_EXCEPTIONS
2226    if (__p_.__state_ == nullptr)
2227        throw future_error(make_error_code(future_errc::no_state));
2228    if (__p_.__state_->__has_value())
2229        throw future_error(make_error_code(future_errc::promise_already_satisfied));
2230    try
2231    {
2232#endif  // _LIBCPP_NO_EXCEPTIONS
2233        __f_(_VSTD::forward<_ArgTypes>(__args)...);
2234        __p_.set_value();
2235#ifndef _LIBCPP_NO_EXCEPTIONS
2236    }
2237    catch (...)
2238    {
2239        __p_.set_exception(current_exception());
2240    }
2241#endif  // _LIBCPP_NO_EXCEPTIONS
2242}
2243
2244template<class ..._ArgTypes>
2245void
2246packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2247{
2248#ifndef _LIBCPP_NO_EXCEPTIONS
2249    if (__p_.__state_ == nullptr)
2250        throw future_error(make_error_code(future_errc::no_state));
2251    if (__p_.__state_->__has_value())
2252        throw future_error(make_error_code(future_errc::promise_already_satisfied));
2253    try
2254    {
2255#endif  // _LIBCPP_NO_EXCEPTIONS
2256        __f_(_VSTD::forward<_ArgTypes>(__args)...);
2257        __p_.set_value_at_thread_exit();
2258#ifndef _LIBCPP_NO_EXCEPTIONS
2259    }
2260    catch (...)
2261    {
2262        __p_.set_exception_at_thread_exit(current_exception());
2263    }
2264#endif  // _LIBCPP_NO_EXCEPTIONS
2265}
2266
2267template<class ..._ArgTypes>
2268void
2269packaged_task<void(_ArgTypes...)>::reset()
2270{
2271#ifndef _LIBCPP_NO_EXCEPTIONS
2272    if (!valid())
2273        throw future_error(make_error_code(future_errc::no_state));
2274#endif  // _LIBCPP_NO_EXCEPTIONS
2275    __p_ = promise<result_type>();
2276}
2277
2278template <class _Callable>
2279inline _LIBCPP_INLINE_VISIBILITY
2280void
2281swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
2282{
2283    __x.swap(__y);
2284}
2285
2286template <class _Callable, class _Alloc>
2287struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<packaged_task<_Callable>, _Alloc>
2288    : public true_type {};
2289
2290template <class _Rp, class _Fp>
2291future<_Rp>
2292#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2293__make_deferred_assoc_state(_Fp&& __f)
2294#else
2295__make_deferred_assoc_state(_Fp __f)
2296#endif
2297{
2298    unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2299        __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2300    return future<_Rp>(__h.get());
2301}
2302
2303template <class _Rp, class _Fp>
2304future<_Rp>
2305#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2306__make_async_assoc_state(_Fp&& __f)
2307#else
2308__make_async_assoc_state(_Fp __f)
2309#endif
2310{
2311    unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2312        __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2313    _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2314    return future<_Rp>(__h.get());
2315}
2316
2317template <class _Fp, class... _Args>
2318class __async_func
2319{
2320    tuple<_Fp, _Args...> __f_;
2321
2322public:
2323    typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2324
2325    _LIBCPP_INLINE_VISIBILITY
2326    explicit __async_func(_Fp&& __f, _Args&&... __args)
2327        : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2328
2329    _LIBCPP_INLINE_VISIBILITY
2330    __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2331
2332    _Rp operator()()
2333    {
2334        typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2335        return __execute(_Index());
2336    }
2337private:
2338    template <size_t ..._Indices>
2339    _Rp
2340    __execute(__tuple_indices<_Indices...>)
2341    {
2342        return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2343    }
2344};
2345
2346inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2347{ return (int(__policy) & int(__value)) != 0; }
2348
2349template <class _Fp, class... _Args>
2350future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2351async(launch __policy, _Fp&& __f, _Args&&... __args)
2352{
2353    typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2354    typedef typename _BF::_Rp _Rp;
2355
2356#ifndef _LIBCPP_NO_EXCEPTIONS
2357    try
2358    {
2359#endif
2360        if (__does_policy_contain(__policy, launch::async))
2361        return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2362                                                     __decay_copy(_VSTD::forward<_Args>(__args))...));
2363#ifndef _LIBCPP_NO_EXCEPTIONS
2364    }
2365    catch ( ... ) { if (__policy == launch::async) throw ; }
2366#endif
2367
2368    if (__does_policy_contain(__policy, launch::deferred))
2369        return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2370                                                        __decay_copy(_VSTD::forward<_Args>(__args))...));
2371    return future<_Rp>{};
2372}
2373
2374template <class _Fp, class... _Args>
2375inline _LIBCPP_INLINE_VISIBILITY
2376future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2377async(_Fp&& __f, _Args&&... __args)
2378{
2379    return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2380                                    _VSTD::forward<_Args>(__args)...);
2381}
2382
2383#endif  // _LIBCPP_HAS_NO_VARIADICS
2384
2385// shared_future
2386
2387template <class _Rp>
2388class _LIBCPP_TYPE_VIS_ONLY shared_future
2389{
2390    __assoc_state<_Rp>* __state_;
2391
2392public:
2393    _LIBCPP_INLINE_VISIBILITY
2394    shared_future() _NOEXCEPT : __state_(nullptr) {}
2395    _LIBCPP_INLINE_VISIBILITY
2396    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2397        {if (__state_) __state_->__add_shared();}
2398#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2399    _LIBCPP_INLINE_VISIBILITY
2400    shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
2401        {__f.__state_ = nullptr;}
2402    _LIBCPP_INLINE_VISIBILITY
2403    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2404        {__rhs.__state_ = nullptr;}
2405#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2406    ~shared_future();
2407    shared_future& operator=(const shared_future& __rhs);
2408#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2409    _LIBCPP_INLINE_VISIBILITY
2410    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2411        {
2412            shared_future(std::move(__rhs)).swap(*this);
2413            return *this;
2414        }
2415#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2416
2417    // retrieving the value
2418    _LIBCPP_INLINE_VISIBILITY
2419    const _Rp& get() const {return __state_->copy();}
2420
2421    _LIBCPP_INLINE_VISIBILITY
2422    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2423
2424    // functions to check state
2425    _LIBCPP_INLINE_VISIBILITY
2426    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2427
2428    _LIBCPP_INLINE_VISIBILITY
2429    void wait() const {__state_->wait();}
2430    template <class _Rep, class _Period>
2431        _LIBCPP_INLINE_VISIBILITY
2432        future_status
2433        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2434            {return __state_->wait_for(__rel_time);}
2435    template <class _Clock, class _Duration>
2436        _LIBCPP_INLINE_VISIBILITY
2437        future_status
2438        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2439            {return __state_->wait_until(__abs_time);}
2440};
2441
2442template <class _Rp>
2443shared_future<_Rp>::~shared_future()
2444{
2445    if (__state_)
2446        __state_->__release_shared();
2447}
2448
2449template <class _Rp>
2450shared_future<_Rp>&
2451shared_future<_Rp>::operator=(const shared_future& __rhs)
2452{
2453    if (__rhs.__state_)
2454        __rhs.__state_->__add_shared();
2455    if (__state_)
2456        __state_->__release_shared();
2457    __state_ = __rhs.__state_;
2458    return *this;
2459}
2460
2461template <class _Rp>
2462class _LIBCPP_TYPE_VIS_ONLY shared_future<_Rp&>
2463{
2464    __assoc_state<_Rp&>* __state_;
2465
2466public:
2467    _LIBCPP_INLINE_VISIBILITY
2468    shared_future() _NOEXCEPT : __state_(nullptr) {}
2469    _LIBCPP_INLINE_VISIBILITY
2470    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2471        {if (__state_) __state_->__add_shared();}
2472#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2473    _LIBCPP_INLINE_VISIBILITY
2474    shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
2475        {__f.__state_ = nullptr;}
2476    _LIBCPP_INLINE_VISIBILITY
2477    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2478        {__rhs.__state_ = nullptr;}
2479#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2480    ~shared_future();
2481    shared_future& operator=(const shared_future& __rhs);
2482#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2483    _LIBCPP_INLINE_VISIBILITY
2484    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2485        {
2486            shared_future(std::move(__rhs)).swap(*this);
2487            return *this;
2488        }
2489#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2490
2491    // retrieving the value
2492    _LIBCPP_INLINE_VISIBILITY
2493    _Rp& get() const {return __state_->copy();}
2494
2495    _LIBCPP_INLINE_VISIBILITY
2496    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2497
2498    // functions to check state
2499    _LIBCPP_INLINE_VISIBILITY
2500    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2501
2502    _LIBCPP_INLINE_VISIBILITY
2503    void wait() const {__state_->wait();}
2504    template <class _Rep, class _Period>
2505        _LIBCPP_INLINE_VISIBILITY
2506        future_status
2507        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2508            {return __state_->wait_for(__rel_time);}
2509    template <class _Clock, class _Duration>
2510        _LIBCPP_INLINE_VISIBILITY
2511        future_status
2512        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2513            {return __state_->wait_until(__abs_time);}
2514};
2515
2516template <class _Rp>
2517shared_future<_Rp&>::~shared_future()
2518{
2519    if (__state_)
2520        __state_->__release_shared();
2521}
2522
2523template <class _Rp>
2524shared_future<_Rp&>&
2525shared_future<_Rp&>::operator=(const shared_future& __rhs)
2526{
2527    if (__rhs.__state_)
2528        __rhs.__state_->__add_shared();
2529    if (__state_)
2530        __state_->__release_shared();
2531    __state_ = __rhs.__state_;
2532    return *this;
2533}
2534
2535template <>
2536class _LIBCPP_TYPE_VIS shared_future<void>
2537{
2538    __assoc_sub_state* __state_;
2539
2540public:
2541    _LIBCPP_INLINE_VISIBILITY
2542    shared_future() _NOEXCEPT : __state_(nullptr) {}
2543    _LIBCPP_INLINE_VISIBILITY
2544    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2545        {if (__state_) __state_->__add_shared();}
2546#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2547    _LIBCPP_INLINE_VISIBILITY
2548    shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
2549        {__f.__state_ = nullptr;}
2550    _LIBCPP_INLINE_VISIBILITY
2551    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2552        {__rhs.__state_ = nullptr;}
2553#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2554    ~shared_future();
2555    shared_future& operator=(const shared_future& __rhs);
2556#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2557    _LIBCPP_INLINE_VISIBILITY
2558    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2559        {
2560            shared_future(std::move(__rhs)).swap(*this);
2561            return *this;
2562        }
2563#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2564
2565    // retrieving the value
2566    _LIBCPP_INLINE_VISIBILITY
2567    void get() const {__state_->copy();}
2568
2569    _LIBCPP_INLINE_VISIBILITY
2570    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2571
2572    // functions to check state
2573    _LIBCPP_INLINE_VISIBILITY
2574    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2575
2576    _LIBCPP_INLINE_VISIBILITY
2577    void wait() const {__state_->wait();}
2578    template <class _Rep, class _Period>
2579        _LIBCPP_INLINE_VISIBILITY
2580        future_status
2581        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2582            {return __state_->wait_for(__rel_time);}
2583    template <class _Clock, class _Duration>
2584        _LIBCPP_INLINE_VISIBILITY
2585        future_status
2586        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2587            {return __state_->wait_until(__abs_time);}
2588};
2589
2590template <class _Rp>
2591inline _LIBCPP_INLINE_VISIBILITY
2592void
2593swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2594{
2595    __x.swap(__y);
2596}
2597
2598template <class _Rp>
2599inline _LIBCPP_INLINE_VISIBILITY
2600shared_future<_Rp>
2601future<_Rp>::share()
2602{
2603    return shared_future<_Rp>(_VSTD::move(*this));
2604}
2605
2606template <class _Rp>
2607inline _LIBCPP_INLINE_VISIBILITY
2608shared_future<_Rp&>
2609future<_Rp&>::share()
2610{
2611    return shared_future<_Rp&>(_VSTD::move(*this));
2612}
2613
2614#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2615
2616inline _LIBCPP_INLINE_VISIBILITY
2617shared_future<void>
2618future<void>::share()
2619{
2620    return shared_future<void>(_VSTD::move(*this));
2621}
2622
2623#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2624
2625_LIBCPP_END_NAMESPACE_STD
2626
2627#endif // !_LIBCPP_HAS_NO_THREADS
2628
2629#endif  // _LIBCPP_FUTURE
2630