2 //===--------------------------- future -----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_FUTURE
12 #define _LIBCPP_FUTURE
20 enum class future_errc
22 future_already_retrieved = 1,
23 promise_already_satisfied,
32 any = async | deferred
35 enum class future_status
42 template <> struct is_error_code_enum<future_errc> : public true_type { };
43 error_code make_error_code(future_errc e) noexcept;
44 error_condition make_error_condition(future_errc e) noexcept;
46 const error_category& future_category() noexcept;
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;
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
65 promise(promise&& rhs) noexcept;
66 promise(const promise& rhs) = delete;
70 promise& operator=(promise&& rhs) noexcept;
71 promise& operator=(const promise& rhs) = delete;
72 void swap(promise& other) noexcept;
74 // retrieving the result
75 future<R> get_future();
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
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);
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
95 promise(promise&& rhs) noexcept;
96 promise(const promise& rhs) = delete;
100 promise& operator=(promise&& rhs) noexcept;
101 promise& operator=(const promise& rhs) = delete;
102 void swap(promise& other) noexcept;
104 // retrieving the result
105 future<R&> get_future();
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
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);
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
123 promise(promise&& rhs) noexcept;
124 promise(const promise& rhs) = delete;
128 promise& operator=(promise&& rhs) noexcept;
129 promise& operator=(const promise& rhs) = delete;
130 void swap(promise& other) noexcept;
132 // retrieving the result
133 future<void> get_future();
135 // setting the result
137 void set_exception(exception_ptr p);
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
144 template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
146 template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
154 future(future&&) noexcept;
155 future(const future& rhs) = delete;
157 future& operator=(const future& rhs) = delete;
158 future& operator=(future&&) noexcept;
159 shared_future<R> share() noexcept;
161 // retrieving the value
164 // functions to check state
165 bool valid() const noexcept;
168 template <class Rep, class Period>
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
181 future(future&&) noexcept;
182 future(const future& rhs) = delete;
184 future& operator=(const future& rhs) = delete;
185 future& operator=(future&&) noexcept;
186 shared_future<R&> share() noexcept;
188 // retrieving the value
191 // functions to check state
192 bool valid() const noexcept;
195 template <class Rep, class Period>
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
208 future(future&&) noexcept;
209 future(const future& rhs) = delete;
211 future& operator=(const future& rhs) = delete;
212 future& operator=(future&&) noexcept;
213 shared_future<void> share() noexcept;
215 // retrieving the value
218 // functions to check state
219 bool valid() const noexcept;
222 template <class Rep, class Period>
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
234 shared_future() noexcept;
235 shared_future(const shared_future& rhs);
236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
239 shared_future& operator=(const shared_future& rhs);
240 shared_future& operator=(shared_future&& rhs) noexcept;
242 // retrieving the value
243 const R& get() const;
245 // functions to check state
246 bool valid() const noexcept;
249 template <class Rep, class Period>
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
258 class shared_future<R&>
261 shared_future() noexcept;
262 shared_future(const shared_future& rhs);
263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
266 shared_future& operator=(const shared_future& rhs);
267 shared_future& operator=(shared_future&& rhs) noexcept;
269 // retrieving the value
272 // functions to check state
273 bool valid() const noexcept;
276 template <class Rep, class Period>
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
285 class shared_future<void>
288 shared_future() noexcept;
289 shared_future(const shared_future& rhs);
290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
293 shared_future& operator=(const shared_future& rhs);
294 shared_future& operator=(shared_future&& rhs) noexcept;
296 // retrieving the value
299 // functions to check state
300 bool valid() const noexcept;
303 template <class Rep, class Period>
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
311 template <class F, class... Args>
312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
313 async(F&& f, Args&&... args);
315 template <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);
319 template <class> class packaged_task; // undefined
321 template <class R, class... ArgTypes>
322 class packaged_task<R(ArgTypes...)>
325 typedef R result_type; // extension
327 // construction and destruction
328 packaged_task() noexcept;
330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
344 bool valid() const noexcept;
347 future<R> get_future();
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
359 template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
366 #include <system_error>
373 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
374 #pragma GCC system_header
377 #ifdef _LIBCPP_HAS_NO_THREADS
378 #error <future> is not supported on this single threaded system
379 #else // !_LIBCPP_HAS_NO_THREADS
381 _LIBCPP_BEGIN_NAMESPACE_STD
383 //enum class future_errc
384 _LIBCPP_DECLARE_STRONG_ENUM(future_errc)
386 future_already_retrieved = 1,
387 promise_already_satisfied,
391 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
394 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
396 #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
398 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
402 _LIBCPP_DECLARE_STRONG_ENUM(launch)
406 any = async | deferred
408 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
410 #ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
412 #ifdef _LIBCXX_UNDERLYING_TYPE
413 typedef underlying_type<launch>::type __launch_underlying_type;
415 typedef int __launch_underlying_type;
418 inline _LIBCPP_INLINE_VISIBILITY
421 operator&(launch __x, launch __y)
423 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
424 static_cast<__launch_underlying_type>(__y));
427 inline _LIBCPP_INLINE_VISIBILITY
430 operator|(launch __x, launch __y)
432 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
433 static_cast<__launch_underlying_type>(__y));
436 inline _LIBCPP_INLINE_VISIBILITY
439 operator^(launch __x, launch __y)
441 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
442 static_cast<__launch_underlying_type>(__y));
445 inline _LIBCPP_INLINE_VISIBILITY
448 operator~(launch __x)
450 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
453 inline _LIBCPP_INLINE_VISIBILITY
455 operator&=(launch& __x, launch __y)
457 __x = __x & __y; return __x;
460 inline _LIBCPP_INLINE_VISIBILITY
462 operator|=(launch& __x, launch __y)
464 __x = __x | __y; return __x;
467 inline _LIBCPP_INLINE_VISIBILITY
469 operator^=(launch& __x, launch __y)
471 __x = __x ^ __y; return __x;
474 #endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
476 //enum class future_status
477 _LIBCPP_DECLARE_STRONG_ENUM(future_status)
483 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
486 const error_category& future_category() _NOEXCEPT;
488 inline _LIBCPP_INLINE_VISIBILITY
490 make_error_code(future_errc __e) _NOEXCEPT
492 return error_code(static_cast<int>(__e), future_category());
495 inline _LIBCPP_INLINE_VISIBILITY
497 make_error_condition(future_errc __e) _NOEXCEPT
499 return error_condition(static_cast<int>(__e), future_category());
502 class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
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)) {}
511 _LIBCPP_INLINE_VISIBILITY
512 const error_code& code() const _NOEXCEPT {return __ec_;}
514 virtual ~future_error() _NOEXCEPT;
517 _LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
518 #ifndef _LIBCPP_NO_EXCEPTIONS
519 _LIBCPP_AVAILABILITY_FUTURE_ERROR
521 void __throw_future_error(future_errc _Ev)
523 #ifndef _LIBCPP_NO_EXCEPTIONS
524 throw future_error(make_error_code(_Ev));
531 class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
532 : public __shared_count
535 exception_ptr __exception_;
536 mutable mutex __mut_;
537 mutable condition_variable __cv_;
540 virtual void __on_zero_shared() _NOEXCEPT;
541 void __sub_wait(unique_lock<mutex>& __lk);
546 __future_attached = 2,
551 _LIBCPP_INLINE_VISIBILITY
552 __assoc_sub_state() : __state_(0) {}
554 _LIBCPP_INLINE_VISIBILITY
555 bool __has_value() const
556 {return (__state_ & __constructed) || (__exception_ != nullptr);}
558 _LIBCPP_INLINE_VISIBILITY
559 void __set_future_attached()
561 lock_guard<mutex> __lk(__mut_);
562 __state_ |= __future_attached;
564 _LIBCPP_INLINE_VISIBILITY
565 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
567 _LIBCPP_INLINE_VISIBILITY
568 void __set_deferred() {__state_ |= deferred;}
571 _LIBCPP_INLINE_VISIBILITY
572 bool __is_ready() const {return (__state_ & ready) != 0;}
575 void set_value_at_thread_exit();
577 void set_exception(exception_ptr __p);
578 void set_exception_at_thread_exit(exception_ptr __p);
583 template <class _Rep, class _Period>
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
590 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
592 virtual void __execute();
595 template <class _Clock, class _Duration>
597 __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
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;
609 template <class _Rep, class _Period>
612 __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
614 return wait_until(chrono::steady_clock::now() + __rel_time);
618 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state
619 : public __assoc_sub_state
621 typedef __assoc_sub_state base;
622 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
626 virtual void __on_zero_shared() _NOEXCEPT;
629 template <class _Arg>
630 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
631 void set_value(_Arg&& __arg);
633 void set_value(_Arg& __arg);
636 template <class _Arg>
637 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
638 void set_value_at_thread_exit(_Arg&& __arg);
640 void set_value_at_thread_exit(_Arg& __arg);
644 typename add_lvalue_reference<_Rp>::type copy();
649 __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
651 if (this->__state_ & base::__constructed)
652 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
657 template <class _Arg>
658 _LIBCPP_AVAILABILITY_FUTURE
660 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
661 __assoc_state<_Rp>::set_value(_Arg&& __arg)
663 __assoc_state<_Rp>::set_value(_Arg& __arg)
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;
675 template <class _Arg>
677 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
678 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
680 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
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);
693 __assoc_state<_Rp>::move()
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_));
703 typename add_lvalue_reference<_Rp>::type
704 __assoc_state<_Rp>::copy()
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_);
714 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
715 : public __assoc_sub_state
717 typedef __assoc_sub_state base;
722 virtual void __on_zero_shared() _NOEXCEPT;
725 void set_value(_Rp& __arg);
726 void set_value_at_thread_exit(_Rp& __arg);
733 __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
740 __assoc_state<_Rp&>::set_value(_Rp& __arg)
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;
752 __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
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);
764 __assoc_state<_Rp&>::copy()
766 unique_lock<mutex> __lk(this->__mut_);
767 this->__sub_wait(__lk);
768 if (this->__exception_ != nullptr)
769 rethrow_exception(this->__exception_);
773 template <class _Rp, class _Alloc>
774 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
775 : public __assoc_state<_Rp>
777 typedef __assoc_state<_Rp> base;
780 virtual void __on_zero_shared() _NOEXCEPT;
782 _LIBCPP_INLINE_VISIBILITY
783 explicit __assoc_state_alloc(const _Alloc& __a)
787 template <class _Rp, class _Alloc>
789 __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
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;
797 this->~__assoc_state_alloc();
798 __a.deallocate(_PTraits::pointer_to(*this), 1);
801 template <class _Rp, class _Alloc>
802 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
803 : public __assoc_state<_Rp&>
805 typedef __assoc_state<_Rp&> base;
808 virtual void __on_zero_shared() _NOEXCEPT;
810 _LIBCPP_INLINE_VISIBILITY
811 explicit __assoc_state_alloc(const _Alloc& __a)
815 template <class _Rp, class _Alloc>
817 __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
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;
823 this->~__assoc_state_alloc();
824 __a.deallocate(_PTraits::pointer_to(*this), 1);
827 template <class _Alloc>
828 class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
829 : public __assoc_sub_state
831 typedef __assoc_sub_state base;
834 virtual void __on_zero_shared() _NOEXCEPT;
836 _LIBCPP_INLINE_VISIBILITY
837 explicit __assoc_sub_state_alloc(const _Alloc& __a)
841 template <class _Alloc>
843 __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
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;
849 this->~__assoc_sub_state_alloc();
850 __a.deallocate(_PTraits::pointer_to(*this), 1);
853 template <class _Rp, class _Fp>
854 class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
855 : public __assoc_state<_Rp>
857 typedef __assoc_state<_Rp> base;
862 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
863 _LIBCPP_INLINE_VISIBILITY
864 explicit __deferred_assoc_state(_Fp&& __f);
867 virtual void __execute();
870 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
872 template <class _Rp, class _Fp>
874 __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
875 : __func_(_VSTD::forward<_Fp>(__f))
877 this->__set_deferred();
880 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
882 template <class _Rp, class _Fp>
884 __deferred_assoc_state<_Rp, _Fp>::__execute()
886 #ifndef _LIBCPP_NO_EXCEPTIONS
889 #endif // _LIBCPP_NO_EXCEPTIONS
890 this->set_value(__func_());
891 #ifndef _LIBCPP_NO_EXCEPTIONS
895 this->set_exception(current_exception());
897 #endif // _LIBCPP_NO_EXCEPTIONS
901 class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
902 : public __assoc_sub_state
904 typedef __assoc_sub_state base;
909 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
910 _LIBCPP_INLINE_VISIBILITY
911 explicit __deferred_assoc_state(_Fp&& __f);
914 virtual void __execute();
917 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
921 __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
922 : __func_(_VSTD::forward<_Fp>(__f))
924 this->__set_deferred();
927 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
931 __deferred_assoc_state<void, _Fp>::__execute()
933 #ifndef _LIBCPP_NO_EXCEPTIONS
936 #endif // _LIBCPP_NO_EXCEPTIONS
939 #ifndef _LIBCPP_NO_EXCEPTIONS
943 this->set_exception(current_exception());
945 #endif // _LIBCPP_NO_EXCEPTIONS
948 template <class _Rp, class _Fp>
949 class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
950 : public __assoc_state<_Rp>
952 typedef __assoc_state<_Rp> base;
956 virtual void __on_zero_shared() _NOEXCEPT;
958 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
959 _LIBCPP_INLINE_VISIBILITY
960 explicit __async_assoc_state(_Fp&& __f);
963 virtual void __execute();
966 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
968 template <class _Rp, class _Fp>
970 __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
971 : __func_(_VSTD::forward<_Fp>(__f))
975 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
977 template <class _Rp, class _Fp>
979 __async_assoc_state<_Rp, _Fp>::__execute()
981 #ifndef _LIBCPP_NO_EXCEPTIONS
984 #endif // _LIBCPP_NO_EXCEPTIONS
985 this->set_value(__func_());
986 #ifndef _LIBCPP_NO_EXCEPTIONS
990 this->set_exception(current_exception());
992 #endif // _LIBCPP_NO_EXCEPTIONS
995 template <class _Rp, class _Fp>
997 __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
1000 base::__on_zero_shared();
1003 template <class _Fp>
1004 class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
1005 : public __assoc_sub_state
1007 typedef __assoc_sub_state base;
1011 virtual void __on_zero_shared() _NOEXCEPT;
1013 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1014 _LIBCPP_INLINE_VISIBILITY
1015 explicit __async_assoc_state(_Fp&& __f);
1018 virtual void __execute();
1021 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1023 template <class _Fp>
1025 __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1026 : __func_(_VSTD::forward<_Fp>(__f))
1030 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1032 template <class _Fp>
1034 __async_assoc_state<void, _Fp>::__execute()
1036 #ifndef _LIBCPP_NO_EXCEPTIONS
1039 #endif // _LIBCPP_NO_EXCEPTIONS
1042 #ifndef _LIBCPP_NO_EXCEPTIONS
1046 this->set_exception(current_exception());
1048 #endif // _LIBCPP_NO_EXCEPTIONS
1051 template <class _Fp>
1053 __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1056 base::__on_zero_shared();
1059 template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1060 template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
1064 template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
1066 template <class _Rp, class _Fp>
1068 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1069 __make_deferred_assoc_state(_Fp&& __f);
1071 __make_deferred_assoc_state(_Fp __f);
1074 template <class _Rp, class _Fp>
1076 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1077 __make_async_assoc_state(_Fp&& __f);
1079 __make_async_assoc_state(_Fp __f);
1082 template <class _Rp>
1083 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
1085 __assoc_state<_Rp>* __state_;
1087 explicit future(__assoc_state<_Rp>* __state);
1089 template <class> friend class promise;
1090 template <class> friend class shared_future;
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);
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);
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
1116 future(std::move(__rhs)).swap(*this);
1119 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1121 future(const future&);
1122 future& operator=(const future&);
1124 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1126 _LIBCPP_INLINE_VISIBILITY
1127 shared_future<_Rp> share() _NOEXCEPT;
1129 // retrieving the value
1132 _LIBCPP_INLINE_VISIBILITY
1133 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1135 // functions to check state
1136 _LIBCPP_INLINE_VISIBILITY
1137 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1139 _LIBCPP_INLINE_VISIBILITY
1140 void wait() const {__state_->wait();}
1141 template <class _Rep, class _Period>
1142 _LIBCPP_INLINE_VISIBILITY
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
1149 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1150 {return __state_->wait_until(__abs_time);}
1153 template <class _Rp>
1154 future<_Rp>::future(__assoc_state<_Rp>* __state)
1157 if (__state_->__has_future_attached())
1158 __throw_future_error(future_errc::future_already_retrieved);
1159 __state_->__add_shared();
1160 __state_->__set_future_attached();
1163 struct __release_shared_count
1165 void operator()(__shared_count* p) {p->__release_shared();}
1168 template <class _Rp>
1169 future<_Rp>::~future()
1172 __state_->__release_shared();
1175 template <class _Rp>
1179 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1180 __assoc_state<_Rp>* __s = __state_;
1185 template <class _Rp>
1186 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
1188 __assoc_state<_Rp&>* __state_;
1190 explicit future(__assoc_state<_Rp&>* __state);
1192 template <class> friend class promise;
1193 template <class> friend class shared_future;
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);
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);
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
1219 future(std::move(__rhs)).swap(*this);
1222 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1224 future(const future&);
1225 future& operator=(const future&);
1227 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1229 _LIBCPP_INLINE_VISIBILITY
1230 shared_future<_Rp&> share() _NOEXCEPT;
1232 // retrieving the value
1235 _LIBCPP_INLINE_VISIBILITY
1236 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1238 // functions to check state
1239 _LIBCPP_INLINE_VISIBILITY
1240 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1242 _LIBCPP_INLINE_VISIBILITY
1243 void wait() const {__state_->wait();}
1244 template <class _Rep, class _Period>
1245 _LIBCPP_INLINE_VISIBILITY
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
1252 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1253 {return __state_->wait_until(__abs_time);}
1256 template <class _Rp>
1257 future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1260 if (__state_->__has_future_attached())
1261 __throw_future_error(future_errc::future_already_retrieved);
1262 __state_->__add_shared();
1263 __state_->__set_future_attached();
1266 template <class _Rp>
1267 future<_Rp&>::~future()
1270 __state_->__release_shared();
1273 template <class _Rp>
1277 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1278 __assoc_state<_Rp&>* __s = __state_;
1284 class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE future<void>
1286 __assoc_sub_state* __state_;
1288 explicit future(__assoc_sub_state* __state);
1290 template <class> friend class promise;
1291 template <class> friend class shared_future;
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);
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);
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
1317 future(std::move(__rhs)).swap(*this);
1320 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1322 future(const future&);
1323 future& operator=(const future&);
1325 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1327 _LIBCPP_INLINE_VISIBILITY
1328 shared_future<void> share() _NOEXCEPT;
1330 // retrieving the value
1333 _LIBCPP_INLINE_VISIBILITY
1334 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1336 // functions to check state
1337 _LIBCPP_INLINE_VISIBILITY
1338 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1340 _LIBCPP_INLINE_VISIBILITY
1341 void wait() const {__state_->wait();}
1342 template <class _Rep, class _Period>
1343 _LIBCPP_INLINE_VISIBILITY
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
1350 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1351 {return __state_->wait_until(__abs_time);}
1354 template <class _Rp>
1355 inline _LIBCPP_INLINE_VISIBILITY
1357 swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1364 template <class _Callable> class packaged_task;
1366 template <class _Rp>
1367 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
1369 __assoc_state<_Rp>* __state_;
1371 _LIBCPP_INLINE_VISIBILITY
1372 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1374 template <class> friend class packaged_task;
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
1386 promise(const promise& __rhs);
1388 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1392 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1393 _LIBCPP_INLINE_VISIBILITY
1394 promise& operator=(promise&& __rhs) _NOEXCEPT
1396 promise(std::move(__rhs)).swap(*this);
1399 promise& operator=(const promise& __rhs) = delete;
1400 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1402 promise& operator=(const promise& __rhs);
1404 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1405 _LIBCPP_INLINE_VISIBILITY
1406 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1408 // retrieving the result
1409 future<_Rp> get_future();
1411 // setting the result
1412 void set_value(const _Rp& __r);
1413 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1414 void set_value(_Rp&& __r);
1416 void set_exception(exception_ptr __p);
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);
1423 void set_exception_at_thread_exit(exception_ptr __p);
1426 template <class _Rp>
1427 promise<_Rp>::promise()
1428 : __state_(new __assoc_state<_Rp>)
1432 template <class _Rp>
1433 template <class _Alloc>
1434 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1436 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1437 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1438 typedef __allocator_destructor<_A2> _D2;
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());
1445 template <class _Rp>
1446 promise<_Rp>::~promise()
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))
1454 __state_->__release_shared();
1458 template <class _Rp>
1460 promise<_Rp>::get_future()
1462 if (__state_ == nullptr)
1463 __throw_future_error(future_errc::no_state);
1464 return future<_Rp>(__state_);
1467 template <class _Rp>
1469 promise<_Rp>::set_value(const _Rp& __r)
1471 if (__state_ == nullptr)
1472 __throw_future_error(future_errc::no_state);
1473 __state_->set_value(__r);
1476 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1478 template <class _Rp>
1480 promise<_Rp>::set_value(_Rp&& __r)
1482 if (__state_ == nullptr)
1483 __throw_future_error(future_errc::no_state);
1484 __state_->set_value(_VSTD::move(__r));
1487 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1489 template <class _Rp>
1491 promise<_Rp>::set_exception(exception_ptr __p)
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);
1499 template <class _Rp>
1501 promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1503 if (__state_ == nullptr)
1504 __throw_future_error(future_errc::no_state);
1505 __state_->set_value_at_thread_exit(__r);
1508 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1510 template <class _Rp>
1512 promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1514 if (__state_ == nullptr)
1515 __throw_future_error(future_errc::no_state);
1516 __state_->set_value_at_thread_exit(_VSTD::move(__r));
1519 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1521 template <class _Rp>
1523 promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
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);
1533 template <class _Rp>
1534 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
1536 __assoc_state<_Rp&>* __state_;
1538 _LIBCPP_INLINE_VISIBILITY
1539 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1541 template <class> friend class packaged_task;
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
1554 promise(const promise& __rhs);
1556 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1560 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1561 _LIBCPP_INLINE_VISIBILITY
1562 promise& operator=(promise&& __rhs) _NOEXCEPT
1564 promise(std::move(__rhs)).swap(*this);
1567 promise& operator=(const promise& __rhs) = delete;
1568 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1570 promise& operator=(const promise& __rhs);
1572 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1573 _LIBCPP_INLINE_VISIBILITY
1574 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1576 // retrieving the result
1577 future<_Rp&> get_future();
1579 // setting the result
1580 void set_value(_Rp& __r);
1581 void set_exception(exception_ptr __p);
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);
1588 template <class _Rp>
1589 promise<_Rp&>::promise()
1590 : __state_(new __assoc_state<_Rp&>)
1594 template <class _Rp>
1595 template <class _Alloc>
1596 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1598 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1599 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1600 typedef __allocator_destructor<_A2> _D2;
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());
1607 template <class _Rp>
1608 promise<_Rp&>::~promise()
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))
1616 __state_->__release_shared();
1620 template <class _Rp>
1622 promise<_Rp&>::get_future()
1624 if (__state_ == nullptr)
1625 __throw_future_error(future_errc::no_state);
1626 return future<_Rp&>(__state_);
1629 template <class _Rp>
1631 promise<_Rp&>::set_value(_Rp& __r)
1633 if (__state_ == nullptr)
1634 __throw_future_error(future_errc::no_state);
1635 __state_->set_value(__r);
1638 template <class _Rp>
1640 promise<_Rp&>::set_exception(exception_ptr __p)
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);
1648 template <class _Rp>
1650 promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1652 if (__state_ == nullptr)
1653 __throw_future_error(future_errc::no_state);
1654 __state_->set_value_at_thread_exit(__r);
1657 template <class _Rp>
1659 promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
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);
1670 class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<void>
1672 __assoc_sub_state* __state_;
1674 _LIBCPP_INLINE_VISIBILITY
1675 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1677 template <class> friend class packaged_task;
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
1691 promise(const promise& __rhs);
1693 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1697 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1698 _LIBCPP_INLINE_VISIBILITY
1699 promise& operator=(promise&& __rhs) _NOEXCEPT
1701 promise(std::move(__rhs)).swap(*this);
1704 promise& operator=(const promise& __rhs) = delete;
1705 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1707 promise& operator=(const promise& __rhs);
1709 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1710 _LIBCPP_INLINE_VISIBILITY
1711 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1713 // retrieving the result
1714 future<void> get_future();
1716 // setting the result
1718 void set_exception(exception_ptr __p);
1720 // setting the result with deferred notification
1721 void set_value_at_thread_exit();
1722 void set_exception_at_thread_exit(exception_ptr __p);
1725 template <class _Alloc>
1726 promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1728 typedef __assoc_sub_state_alloc<_Alloc> _State;
1729 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1730 typedef __allocator_destructor<_A2> _D2;
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());
1737 template <class _Rp>
1738 inline _LIBCPP_INLINE_VISIBILITY
1740 swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1745 template <class _Rp, class _Alloc>
1746 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
1747 : public true_type {};
1749 #ifndef _LIBCPP_HAS_NO_VARIADICS
1753 template<class _Fp> class __packaged_task_base;
1755 template<class _Rp, class ..._ArgTypes>
1756 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
1758 __packaged_task_base(const __packaged_task_base&);
1759 __packaged_task_base& operator=(const __packaged_task_base&);
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;
1771 template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1773 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1774 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1775 : public __packaged_task_base<_Rp(_ArgTypes...)>
1777 __compressed_pair<_Fp, _Alloc> __f_;
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)
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);
1795 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1797 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1798 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1800 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1803 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1805 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1807 __f_.~__compressed_pair<_Fp, _Alloc>();
1810 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1812 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
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);
1822 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1824 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1826 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1829 template <class _Callable> class __packaged_task_function;
1831 template<class _Rp, class ..._ArgTypes>
1832 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
1834 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1835 typename aligned_storage<3*sizeof(void*)>::type __buf_;
1839 typedef _Rp result_type;
1841 // construct/copy/destroy:
1842 _LIBCPP_INLINE_VISIBILITY
1843 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1845 __packaged_task_function(_Fp&& __f);
1846 template<class _Fp, class _Alloc>
1847 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1849 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1850 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1852 __packaged_task_function(const __packaged_task_function&) = delete;
1853 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1855 ~__packaged_task_function();
1857 void swap(__packaged_task_function&) _NOEXCEPT;
1859 _LIBCPP_INLINE_VISIBILITY
1860 _Rp operator()(_ArgTypes...) const;
1863 template<class _Rp, class ..._ArgTypes>
1864 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1866 if (__f.__f_ == nullptr)
1868 else if (__f.__f_ == (__base*)&__f.__buf_)
1870 __f_ = (__base*)&__buf_;
1871 __f.__f_->__move_to(__f_);
1880 template<class _Rp, class ..._ArgTypes>
1881 template <class _Fp>
1882 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
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_))
1889 __f_ = (__base*)&__buf_;
1890 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1894 typedef allocator<_FF> _Ap;
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();
1903 template<class _Rp, class ..._ArgTypes>
1904 template <class _Fp, class _Alloc>
1905 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1906 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
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_))
1913 __f_ = (__base*)&__buf_;
1914 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1918 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
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());
1928 template<class _Rp, class ..._ArgTypes>
1929 __packaged_task_function<_Rp(_ArgTypes...)>&
1930 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1932 if (__f_ == (__base*)&__buf_)
1935 __f_->destroy_deallocate();
1937 if (__f.__f_ == nullptr)
1939 else if (__f.__f_ == (__base*)&__f.__buf_)
1941 __f_ = (__base*)&__buf_;
1942 __f.__f_->__move_to(__f_);
1952 template<class _Rp, class ..._ArgTypes>
1953 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1955 if (__f_ == (__base*)&__buf_)
1958 __f_->destroy_deallocate();
1961 template<class _Rp, class ..._ArgTypes>
1963 __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1965 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1967 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1968 __base* __t = (__base*)&__tempbuf;
1969 __f_->__move_to(__t);
1972 __f.__f_->__move_to((__base*)&__buf_);
1973 __f.__f_->destroy();
1975 __f_ = (__base*)&__buf_;
1976 __t->__move_to((__base*)&__f.__buf_);
1978 __f.__f_ = (__base*)&__f.__buf_;
1980 else if (__f_ == (__base*)&__buf_)
1982 __f_->__move_to((__base*)&__f.__buf_);
1985 __f.__f_ = (__base*)&__f.__buf_;
1987 else if (__f.__f_ == (__base*)&__f.__buf_)
1989 __f.__f_->__move_to((__base*)&__buf_);
1990 __f.__f_->destroy();
1992 __f_ = (__base*)&__buf_;
1995 _VSTD::swap(__f_, __f.__f_);
1998 template<class _Rp, class ..._ArgTypes>
2001 __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
2003 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
2006 template<class _Rp, class ..._ArgTypes>
2007 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
2010 typedef _Rp result_type; // extension
2013 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2014 promise<result_type> __p_;
2017 // construction and destruction
2018 _LIBCPP_INLINE_VISIBILITY
2019 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2020 template <class _Fp,
2021 class = typename enable_if
2024 typename decay<_Fp>::type,
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
2035 typename decay<_Fp>::type,
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;
2047 packaged_task(const packaged_task&) = delete;
2048 packaged_task& operator=(const packaged_task&) = delete;
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
2057 __f_ = _VSTD::move(__other.__f_);
2058 __p_ = _VSTD::move(__other.__p_);
2061 _LIBCPP_INLINE_VISIBILITY
2062 void swap(packaged_task& __other) _NOEXCEPT
2064 __f_.swap(__other.__f_);
2065 __p_.swap(__other.__p_);
2068 _LIBCPP_INLINE_VISIBILITY
2069 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2072 _LIBCPP_INLINE_VISIBILITY
2073 future<result_type> get_future() {return __p_.get_future();}
2076 void operator()(_ArgTypes... __args);
2077 void make_ready_at_thread_exit(_ArgTypes... __args);
2082 template<class _Rp, class ..._ArgTypes>
2084 packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
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
2093 #endif // _LIBCPP_NO_EXCEPTIONS
2094 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2095 #ifndef _LIBCPP_NO_EXCEPTIONS
2099 __p_.set_exception(current_exception());
2101 #endif // _LIBCPP_NO_EXCEPTIONS
2104 template<class _Rp, class ..._ArgTypes>
2106 packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
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
2115 #endif // _LIBCPP_NO_EXCEPTIONS
2116 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2117 #ifndef _LIBCPP_NO_EXCEPTIONS
2121 __p_.set_exception_at_thread_exit(current_exception());
2123 #endif // _LIBCPP_NO_EXCEPTIONS
2126 template<class _Rp, class ..._ArgTypes>
2128 packaged_task<_Rp(_ArgTypes...)>::reset()
2131 __throw_future_error(future_errc::no_state);
2132 __p_ = promise<result_type>();
2135 template<class ..._ArgTypes>
2136 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
2139 typedef void result_type; // extension
2142 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2143 promise<result_type> __p_;
2146 // construction and destruction
2147 _LIBCPP_INLINE_VISIBILITY
2148 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2149 template <class _Fp,
2150 class = typename enable_if
2153 typename decay<_Fp>::type,
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
2164 typename decay<_Fp>::type,
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;
2176 packaged_task(const packaged_task&) = delete;
2177 packaged_task& operator=(const packaged_task&) = delete;
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
2186 __f_ = _VSTD::move(__other.__f_);
2187 __p_ = _VSTD::move(__other.__p_);
2190 _LIBCPP_INLINE_VISIBILITY
2191 void swap(packaged_task& __other) _NOEXCEPT
2193 __f_.swap(__other.__f_);
2194 __p_.swap(__other.__p_);
2197 _LIBCPP_INLINE_VISIBILITY
2198 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2201 _LIBCPP_INLINE_VISIBILITY
2202 future<result_type> get_future() {return __p_.get_future();}
2205 void operator()(_ArgTypes... __args);
2206 void make_ready_at_thread_exit(_ArgTypes... __args);
2211 template<class ..._ArgTypes>
2213 packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
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
2222 #endif // _LIBCPP_NO_EXCEPTIONS
2223 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2225 #ifndef _LIBCPP_NO_EXCEPTIONS
2229 __p_.set_exception(current_exception());
2231 #endif // _LIBCPP_NO_EXCEPTIONS
2234 template<class ..._ArgTypes>
2236 packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
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
2245 #endif // _LIBCPP_NO_EXCEPTIONS
2246 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2247 __p_.set_value_at_thread_exit();
2248 #ifndef _LIBCPP_NO_EXCEPTIONS
2252 __p_.set_exception_at_thread_exit(current_exception());
2254 #endif // _LIBCPP_NO_EXCEPTIONS
2257 template<class ..._ArgTypes>
2259 packaged_task<void(_ArgTypes...)>::reset()
2262 __throw_future_error(future_errc::no_state);
2263 __p_ = promise<result_type>();
2266 template <class _Callable>
2267 inline _LIBCPP_INLINE_VISIBILITY
2269 swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
2274 template <class _Callable, class _Alloc>
2275 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
2276 : public true_type {};
2278 template <class _Rp, class _Fp>
2280 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2281 __make_deferred_assoc_state(_Fp&& __f)
2283 __make_deferred_assoc_state(_Fp __f)
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());
2291 template <class _Rp, class _Fp>
2293 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2294 __make_async_assoc_state(_Fp&& __f)
2296 __make_async_assoc_state(_Fp __f)
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());
2305 template <class _Fp, class... _Args>
2308 tuple<_Fp, _Args...> __f_;
2311 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2313 _LIBCPP_INLINE_VISIBILITY
2314 explicit __async_func(_Fp&& __f, _Args&&... __args)
2315 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2317 _LIBCPP_INLINE_VISIBILITY
2318 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2322 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2323 return __execute(_Index());
2326 template <size_t ..._Indices>
2328 __execute(__tuple_indices<_Indices...>)
2330 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2334 inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2335 { return (int(__policy) & int(__value)) != 0; }
2337 template <class _Fp, class... _Args>
2338 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2339 async(launch __policy, _Fp&& __f, _Args&&... __args)
2341 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2342 typedef typename _BF::_Rp _Rp;
2344 #ifndef _LIBCPP_NO_EXCEPTIONS
2348 if (__does_policy_contain(__policy, launch::async))
2349 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2350 __decay_copy(_VSTD::forward<_Args>(__args))...));
2351 #ifndef _LIBCPP_NO_EXCEPTIONS
2353 catch ( ... ) { if (__policy == launch::async) throw ; }
2356 if (__does_policy_contain(__policy, launch::deferred))
2357 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2358 __decay_copy(_VSTD::forward<_Args>(__args))...));
2359 return future<_Rp>{};
2362 template <class _Fp, class... _Args>
2363 inline _LIBCPP_INLINE_VISIBILITY
2364 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2365 async(_Fp&& __f, _Args&&... __args)
2367 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2368 _VSTD::forward<_Args>(__args)...);
2371 #endif // _LIBCPP_HAS_NO_VARIADICS
2375 template <class _Rp>
2376 class _LIBCPP_TEMPLATE_VIS shared_future
2378 __assoc_state<_Rp>* __state_;
2381 _LIBCPP_INLINE_VISIBILITY
2382 shared_future() _NOEXCEPT : __state_(nullptr) {}
2383 _LIBCPP_INLINE_VISIBILITY
2384 shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2385 {if (__state_) __state_->__add_shared();}
2386 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2387 _LIBCPP_INLINE_VISIBILITY
2388 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
2389 {__f.__state_ = nullptr;}
2390 _LIBCPP_INLINE_VISIBILITY
2391 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2392 {__rhs.__state_ = nullptr;}
2393 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2395 shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
2396 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2397 _LIBCPP_INLINE_VISIBILITY
2398 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2400 shared_future(std::move(__rhs)).swap(*this);
2403 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2405 // retrieving the value
2406 _LIBCPP_INLINE_VISIBILITY
2407 const _Rp& get() const {return __state_->copy();}
2409 _LIBCPP_INLINE_VISIBILITY
2410 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2412 // functions to check state
2413 _LIBCPP_INLINE_VISIBILITY
2414 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2416 _LIBCPP_INLINE_VISIBILITY
2417 void wait() const {__state_->wait();}
2418 template <class _Rep, class _Period>
2419 _LIBCPP_INLINE_VISIBILITY
2421 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2422 {return __state_->wait_for(__rel_time);}
2423 template <class _Clock, class _Duration>
2424 _LIBCPP_INLINE_VISIBILITY
2426 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2427 {return __state_->wait_until(__abs_time);}
2430 template <class _Rp>
2431 shared_future<_Rp>::~shared_future()
2434 __state_->__release_shared();
2437 template <class _Rp>
2439 shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
2442 __rhs.__state_->__add_shared();
2444 __state_->__release_shared();
2445 __state_ = __rhs.__state_;
2449 template <class _Rp>
2450 class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
2452 __assoc_state<_Rp&>* __state_;
2455 _LIBCPP_INLINE_VISIBILITY
2456 shared_future() _NOEXCEPT : __state_(nullptr) {}
2457 _LIBCPP_INLINE_VISIBILITY
2458 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2459 {if (__state_) __state_->__add_shared();}
2460 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2461 _LIBCPP_INLINE_VISIBILITY
2462 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
2463 {__f.__state_ = nullptr;}
2464 _LIBCPP_INLINE_VISIBILITY
2465 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2466 {__rhs.__state_ = nullptr;}
2467 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2469 shared_future& operator=(const shared_future& __rhs);
2470 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2471 _LIBCPP_INLINE_VISIBILITY
2472 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2474 shared_future(std::move(__rhs)).swap(*this);
2477 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2479 // retrieving the value
2480 _LIBCPP_INLINE_VISIBILITY
2481 _Rp& get() const {return __state_->copy();}
2483 _LIBCPP_INLINE_VISIBILITY
2484 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2486 // functions to check state
2487 _LIBCPP_INLINE_VISIBILITY
2488 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2490 _LIBCPP_INLINE_VISIBILITY
2491 void wait() const {__state_->wait();}
2492 template <class _Rep, class _Period>
2493 _LIBCPP_INLINE_VISIBILITY
2495 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2496 {return __state_->wait_for(__rel_time);}
2497 template <class _Clock, class _Duration>
2498 _LIBCPP_INLINE_VISIBILITY
2500 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2501 {return __state_->wait_until(__abs_time);}
2504 template <class _Rp>
2505 shared_future<_Rp&>::~shared_future()
2508 __state_->__release_shared();
2511 template <class _Rp>
2512 shared_future<_Rp&>&
2513 shared_future<_Rp&>::operator=(const shared_future& __rhs)
2516 __rhs.__state_->__add_shared();
2518 __state_->__release_shared();
2519 __state_ = __rhs.__state_;
2524 class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
2526 __assoc_sub_state* __state_;
2529 _LIBCPP_INLINE_VISIBILITY
2530 shared_future() _NOEXCEPT : __state_(nullptr) {}
2531 _LIBCPP_INLINE_VISIBILITY
2532 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2533 {if (__state_) __state_->__add_shared();}
2534 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2535 _LIBCPP_INLINE_VISIBILITY
2536 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
2537 {__f.__state_ = nullptr;}
2538 _LIBCPP_INLINE_VISIBILITY
2539 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2540 {__rhs.__state_ = nullptr;}
2541 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2543 shared_future& operator=(const shared_future& __rhs);
2544 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2545 _LIBCPP_INLINE_VISIBILITY
2546 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2548 shared_future(std::move(__rhs)).swap(*this);
2551 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2553 // retrieving the value
2554 _LIBCPP_INLINE_VISIBILITY
2555 void get() const {__state_->copy();}
2557 _LIBCPP_INLINE_VISIBILITY
2558 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2560 // functions to check state
2561 _LIBCPP_INLINE_VISIBILITY
2562 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2564 _LIBCPP_INLINE_VISIBILITY
2565 void wait() const {__state_->wait();}
2566 template <class _Rep, class _Period>
2567 _LIBCPP_INLINE_VISIBILITY
2569 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2570 {return __state_->wait_for(__rel_time);}
2571 template <class _Clock, class _Duration>
2572 _LIBCPP_INLINE_VISIBILITY
2574 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2575 {return __state_->wait_until(__abs_time);}
2578 template <class _Rp>
2579 inline _LIBCPP_INLINE_VISIBILITY
2581 swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2586 template <class _Rp>
2589 future<_Rp>::share() _NOEXCEPT
2591 return shared_future<_Rp>(_VSTD::move(*this));
2594 template <class _Rp>
2597 future<_Rp&>::share() _NOEXCEPT
2599 return shared_future<_Rp&>(_VSTD::move(*this));
2602 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2606 future<void>::share() _NOEXCEPT
2608 return shared_future<void>(_VSTD::move(*this));
2611 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2613 _LIBCPP_END_NAMESPACE_STD
2615 #endif // !_LIBCPP_HAS_NO_THREADS
2617 #endif // _LIBCPP_FUTURE