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
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();
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();
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();
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;
327 // construction and destruction
328 packaged_task() noexcept;
330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
332 explicit 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_TYPE_VIS_ONLY is_error_code_enum<future_errc> : public true_type {};
396 #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
398 struct _LIBCPP_TYPE_VIS_ONLY 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 future_error
507 future_error(error_code __ec);
509 _LIBCPP_INLINE_VISIBILITY
510 const error_code& code() const _NOEXCEPT {return __ec_;}
512 virtual ~future_error() _NOEXCEPT;
515 class _LIBCPP_TYPE_VIS __assoc_sub_state
516 : public __shared_count
519 exception_ptr __exception_;
520 mutable mutex __mut_;
521 mutable condition_variable __cv_;
524 virtual void __on_zero_shared() _NOEXCEPT;
525 void __sub_wait(unique_lock<mutex>& __lk);
530 __future_attached = 2,
535 _LIBCPP_INLINE_VISIBILITY
536 __assoc_sub_state() : __state_(0) {}
538 _LIBCPP_INLINE_VISIBILITY
539 bool __has_value() const
540 {return (__state_ & __constructed) || (__exception_ != nullptr);}
542 _LIBCPP_INLINE_VISIBILITY
543 void __set_future_attached()
545 lock_guard<mutex> __lk(__mut_);
546 __state_ |= __future_attached;
548 _LIBCPP_INLINE_VISIBILITY
549 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
551 _LIBCPP_INLINE_VISIBILITY
552 void __set_deferred() {__state_ |= deferred;}
555 _LIBCPP_INLINE_VISIBILITY
556 bool __is_ready() const {return (__state_ & ready) != 0;}
559 void set_value_at_thread_exit();
561 void set_exception(exception_ptr __p);
562 void set_exception_at_thread_exit(exception_ptr __p);
567 template <class _Rep, class _Period>
569 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
570 template <class _Clock, class _Duration>
572 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
574 virtual void __execute();
577 template <class _Clock, class _Duration>
579 __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
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;
591 template <class _Rep, class _Period>
592 inline _LIBCPP_INLINE_VISIBILITY
594 __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
596 return wait_until(chrono::steady_clock::now() + __rel_time);
601 : public __assoc_sub_state
603 typedef __assoc_sub_state base;
604 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
608 virtual void __on_zero_shared() _NOEXCEPT;
611 template <class _Arg>
612 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
613 void set_value(_Arg&& __arg);
615 void set_value(_Arg& __arg);
618 template <class _Arg>
619 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
620 void set_value_at_thread_exit(_Arg&& __arg);
622 void set_value_at_thread_exit(_Arg& __arg);
626 typename add_lvalue_reference<_Rp>::type copy();
631 __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
633 if (this->__state_ & base::__constructed)
634 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
639 template <class _Arg>
641 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
642 __assoc_state<_Rp>::set_value(_Arg&& __arg)
644 __assoc_state<_Rp>::set_value(_Arg& __arg)
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));
652 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
653 this->__state_ |= base::__constructed | base::ready;
659 template <class _Arg>
661 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
662 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
664 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
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));
672 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
673 this->__state_ |= base::__constructed;
674 __thread_local_data()->__make_ready_at_thread_exit(this);
680 __assoc_state<_Rp>::move()
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_));
690 typename add_lvalue_reference<_Rp>::type
691 __assoc_state<_Rp>::copy()
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_);
701 class __assoc_state<_Rp&>
702 : public __assoc_sub_state
704 typedef __assoc_sub_state base;
709 virtual void __on_zero_shared() _NOEXCEPT;
712 void set_value(_Rp& __arg);
713 void set_value_at_thread_exit(_Rp& __arg);
720 __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
727 __assoc_state<_Rp&>::set_value(_Rp& __arg)
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));
734 __value_ = _VSTD::addressof(__arg);
735 this->__state_ |= base::__constructed | base::ready;
742 __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
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));
749 __value_ = _VSTD::addressof(__arg);
750 this->__state_ |= base::__constructed;
751 __thread_local_data()->__make_ready_at_thread_exit(this);
757 __assoc_state<_Rp&>::copy()
759 unique_lock<mutex> __lk(this->__mut_);
760 this->__sub_wait(__lk);
761 if (this->__exception_ != nullptr)
762 rethrow_exception(this->__exception_);
766 template <class _Rp, class _Alloc>
767 class __assoc_state_alloc
768 : public __assoc_state<_Rp>
770 typedef __assoc_state<_Rp> base;
773 virtual void __on_zero_shared() _NOEXCEPT;
775 _LIBCPP_INLINE_VISIBILITY
776 explicit __assoc_state_alloc(const _Alloc& __a)
780 template <class _Rp, class _Alloc>
782 __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
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 _A;
787 typedef allocator_traits<_A> _ATraits;
788 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
790 this->~__assoc_state_alloc();
791 __a.deallocate(_PTraits::pointer_to(*this), 1);
794 template <class _Rp, class _Alloc>
795 class __assoc_state_alloc<_Rp&, _Alloc>
796 : public __assoc_state<_Rp&>
798 typedef __assoc_state<_Rp&> base;
801 virtual void __on_zero_shared() _NOEXCEPT;
803 _LIBCPP_INLINE_VISIBILITY
804 explicit __assoc_state_alloc(const _Alloc& __a)
808 template <class _Rp, class _Alloc>
810 __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
812 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _A;
813 typedef allocator_traits<_A> _ATraits;
814 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
816 this->~__assoc_state_alloc();
817 __a.deallocate(_PTraits::pointer_to(*this), 1);
820 template <class _Alloc>
821 class __assoc_sub_state_alloc
822 : public __assoc_sub_state
824 typedef __assoc_sub_state base;
827 virtual void __on_zero_shared() _NOEXCEPT;
829 _LIBCPP_INLINE_VISIBILITY
830 explicit __assoc_sub_state_alloc(const _Alloc& __a)
834 template <class _Alloc>
836 __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
838 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _A;
839 typedef allocator_traits<_A> _ATraits;
840 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
842 this->~__assoc_sub_state_alloc();
843 __a.deallocate(_PTraits::pointer_to(*this), 1);
846 template <class _Rp, class _Fp>
847 class __deferred_assoc_state
848 : public __assoc_state<_Rp>
850 typedef __assoc_state<_Rp> base;
855 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
856 explicit __deferred_assoc_state(_Fp&& __f);
859 virtual void __execute();
862 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
864 template <class _Rp, class _Fp>
865 inline _LIBCPP_INLINE_VISIBILITY
866 __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
867 : __func_(_VSTD::forward<_Fp>(__f))
869 this->__set_deferred();
872 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
874 template <class _Rp, class _Fp>
876 __deferred_assoc_state<_Rp, _Fp>::__execute()
878 #ifndef _LIBCPP_NO_EXCEPTIONS
881 #endif // _LIBCPP_NO_EXCEPTIONS
882 this->set_value(__func_());
883 #ifndef _LIBCPP_NO_EXCEPTIONS
887 this->set_exception(current_exception());
889 #endif // _LIBCPP_NO_EXCEPTIONS
893 class __deferred_assoc_state<void, _Fp>
894 : public __assoc_sub_state
896 typedef __assoc_sub_state base;
901 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
902 explicit __deferred_assoc_state(_Fp&& __f);
905 virtual void __execute();
908 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
911 inline _LIBCPP_INLINE_VISIBILITY
912 __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
913 : __func_(_VSTD::forward<_Fp>(__f))
915 this->__set_deferred();
918 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
922 __deferred_assoc_state<void, _Fp>::__execute()
924 #ifndef _LIBCPP_NO_EXCEPTIONS
927 #endif // _LIBCPP_NO_EXCEPTIONS
930 #ifndef _LIBCPP_NO_EXCEPTIONS
934 this->set_exception(current_exception());
936 #endif // _LIBCPP_NO_EXCEPTIONS
939 template <class _Rp, class _Fp>
940 class __async_assoc_state
941 : public __assoc_state<_Rp>
943 typedef __assoc_state<_Rp> base;
947 virtual void __on_zero_shared() _NOEXCEPT;
949 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
950 explicit __async_assoc_state(_Fp&& __f);
953 virtual void __execute();
956 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
958 template <class _Rp, class _Fp>
959 inline _LIBCPP_INLINE_VISIBILITY
960 __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
961 : __func_(_VSTD::forward<_Fp>(__f))
965 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
967 template <class _Rp, class _Fp>
969 __async_assoc_state<_Rp, _Fp>::__execute()
971 #ifndef _LIBCPP_NO_EXCEPTIONS
974 #endif // _LIBCPP_NO_EXCEPTIONS
975 this->set_value(__func_());
976 #ifndef _LIBCPP_NO_EXCEPTIONS
980 this->set_exception(current_exception());
982 #endif // _LIBCPP_NO_EXCEPTIONS
985 template <class _Rp, class _Fp>
987 __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
990 base::__on_zero_shared();
994 class __async_assoc_state<void, _Fp>
995 : public __assoc_sub_state
997 typedef __assoc_sub_state base;
1001 virtual void __on_zero_shared() _NOEXCEPT;
1003 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1004 explicit __async_assoc_state(_Fp&& __f);
1007 virtual void __execute();
1010 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1012 template <class _Fp>
1013 inline _LIBCPP_INLINE_VISIBILITY
1014 __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1015 : __func_(_VSTD::forward<_Fp>(__f))
1019 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1021 template <class _Fp>
1023 __async_assoc_state<void, _Fp>::__execute()
1025 #ifndef _LIBCPP_NO_EXCEPTIONS
1028 #endif // _LIBCPP_NO_EXCEPTIONS
1031 #ifndef _LIBCPP_NO_EXCEPTIONS
1035 this->set_exception(current_exception());
1037 #endif // _LIBCPP_NO_EXCEPTIONS
1040 template <class _Fp>
1042 __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1045 base::__on_zero_shared();
1048 template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY promise;
1049 template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY shared_future;
1053 template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY future;
1055 template <class _Rp, class _Fp>
1057 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1058 __make_deferred_assoc_state(_Fp&& __f);
1060 __make_deferred_assoc_state(_Fp __f);
1063 template <class _Rp, class _Fp>
1065 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1066 __make_async_assoc_state(_Fp&& __f);
1068 __make_async_assoc_state(_Fp __f);
1071 template <class _Rp>
1072 class _LIBCPP_TYPE_VIS_ONLY future
1074 __assoc_state<_Rp>* __state_;
1076 explicit future(__assoc_state<_Rp>* __state);
1078 template <class> friend class promise;
1079 template <class> friend class shared_future;
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);
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);
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
1105 future(std::move(__rhs)).swap(*this);
1108 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1110 future(const future&);
1111 future& operator=(const future&);
1113 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1115 shared_future<_Rp> share();
1117 // retrieving the value
1120 _LIBCPP_INLINE_VISIBILITY
1121 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1123 // functions to check state
1124 _LIBCPP_INLINE_VISIBILITY
1125 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1127 _LIBCPP_INLINE_VISIBILITY
1128 void wait() const {__state_->wait();}
1129 template <class _Rep, class _Period>
1130 _LIBCPP_INLINE_VISIBILITY
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
1137 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1138 {return __state_->wait_until(__abs_time);}
1141 template <class _Rp>
1142 future<_Rp>::future(__assoc_state<_Rp>* __state)
1145 #ifndef _LIBCPP_NO_EXCEPTIONS
1146 if (__state_->__has_future_attached())
1147 throw future_error(make_error_code(future_errc::future_already_retrieved));
1149 __state_->__add_shared();
1150 __state_->__set_future_attached();
1153 struct __release_shared_count
1155 void operator()(__shared_count* p) {p->__release_shared();}
1158 template <class _Rp>
1159 future<_Rp>::~future()
1162 __state_->__release_shared();
1165 template <class _Rp>
1169 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1170 __assoc_state<_Rp>* __s = __state_;
1175 template <class _Rp>
1176 class _LIBCPP_TYPE_VIS_ONLY future<_Rp&>
1178 __assoc_state<_Rp&>* __state_;
1180 explicit future(__assoc_state<_Rp&>* __state);
1182 template <class> friend class promise;
1183 template <class> friend class shared_future;
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);
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);
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
1209 future(std::move(__rhs)).swap(*this);
1212 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1214 future(const future&);
1215 future& operator=(const future&);
1217 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1219 shared_future<_Rp&> share();
1221 // retrieving the value
1224 _LIBCPP_INLINE_VISIBILITY
1225 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1227 // functions to check state
1228 _LIBCPP_INLINE_VISIBILITY
1229 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1231 _LIBCPP_INLINE_VISIBILITY
1232 void wait() const {__state_->wait();}
1233 template <class _Rep, class _Period>
1234 _LIBCPP_INLINE_VISIBILITY
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
1241 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1242 {return __state_->wait_until(__abs_time);}
1245 template <class _Rp>
1246 future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1249 #ifndef _LIBCPP_NO_EXCEPTIONS
1250 if (__state_->__has_future_attached())
1251 throw future_error(make_error_code(future_errc::future_already_retrieved));
1253 __state_->__add_shared();
1254 __state_->__set_future_attached();
1257 template <class _Rp>
1258 future<_Rp&>::~future()
1261 __state_->__release_shared();
1264 template <class _Rp>
1268 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1269 __assoc_state<_Rp&>* __s = __state_;
1275 class _LIBCPP_TYPE_VIS future<void>
1277 __assoc_sub_state* __state_;
1279 explicit future(__assoc_sub_state* __state);
1281 template <class> friend class promise;
1282 template <class> friend class shared_future;
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);
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);
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
1308 future(std::move(__rhs)).swap(*this);
1311 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1313 future(const future&);
1314 future& operator=(const future&);
1316 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1318 shared_future<void> share();
1320 // retrieving the value
1323 _LIBCPP_INLINE_VISIBILITY
1324 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1326 // functions to check state
1327 _LIBCPP_INLINE_VISIBILITY
1328 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1330 _LIBCPP_INLINE_VISIBILITY
1331 void wait() const {__state_->wait();}
1332 template <class _Rep, class _Period>
1333 _LIBCPP_INLINE_VISIBILITY
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
1340 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1341 {return __state_->wait_until(__abs_time);}
1344 template <class _Rp>
1345 inline _LIBCPP_INLINE_VISIBILITY
1347 swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1354 template <class _Callable> class packaged_task;
1356 template <class _Rp>
1357 class _LIBCPP_TYPE_VIS_ONLY promise
1359 __assoc_state<_Rp>* __state_;
1361 _LIBCPP_INLINE_VISIBILITY
1362 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1364 template <class> friend class packaged_task;
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
1376 promise(const promise& __rhs);
1378 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1382 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1383 _LIBCPP_INLINE_VISIBILITY
1384 promise& operator=(promise&& __rhs) _NOEXCEPT
1386 promise(std::move(__rhs)).swap(*this);
1389 promise& operator=(const promise& __rhs) = delete;
1390 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1392 promise& operator=(const promise& __rhs);
1394 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1395 _LIBCPP_INLINE_VISIBILITY
1396 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1398 // retrieving the result
1399 future<_Rp> get_future();
1401 // setting the result
1402 void set_value(const _Rp& __r);
1403 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1404 void set_value(_Rp&& __r);
1406 void set_exception(exception_ptr __p);
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);
1413 void set_exception_at_thread_exit(exception_ptr __p);
1416 template <class _Rp>
1417 promise<_Rp>::promise()
1418 : __state_(new __assoc_state<_Rp>)
1422 template <class _Rp>
1423 template <class _Alloc>
1424 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1426 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1427 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1428 typedef __allocator_destructor<_A2> _D2;
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());
1435 template <class _Rp>
1436 promise<_Rp>::~promise()
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))
1444 __state_->__release_shared();
1448 template <class _Rp>
1450 promise<_Rp>::get_future()
1452 #ifndef _LIBCPP_NO_EXCEPTIONS
1453 if (__state_ == nullptr)
1454 throw future_error(make_error_code(future_errc::no_state));
1456 return future<_Rp>(__state_);
1459 template <class _Rp>
1461 promise<_Rp>::set_value(const _Rp& __r)
1463 #ifndef _LIBCPP_NO_EXCEPTIONS
1464 if (__state_ == nullptr)
1465 throw future_error(make_error_code(future_errc::no_state));
1467 __state_->set_value(__r);
1470 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1472 template <class _Rp>
1474 promise<_Rp>::set_value(_Rp&& __r)
1476 #ifndef _LIBCPP_NO_EXCEPTIONS
1477 if (__state_ == nullptr)
1478 throw future_error(make_error_code(future_errc::no_state));
1480 __state_->set_value(_VSTD::move(__r));
1483 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1485 template <class _Rp>
1487 promise<_Rp>::set_exception(exception_ptr __p)
1489 #ifndef _LIBCPP_NO_EXCEPTIONS
1490 if (__state_ == nullptr)
1491 throw future_error(make_error_code(future_errc::no_state));
1493 __state_->set_exception(__p);
1496 template <class _Rp>
1498 promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1500 #ifndef _LIBCPP_NO_EXCEPTIONS
1501 if (__state_ == nullptr)
1502 throw future_error(make_error_code(future_errc::no_state));
1504 __state_->set_value_at_thread_exit(__r);
1507 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1509 template <class _Rp>
1511 promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1513 #ifndef _LIBCPP_NO_EXCEPTIONS
1514 if (__state_ == nullptr)
1515 throw future_error(make_error_code(future_errc::no_state));
1517 __state_->set_value_at_thread_exit(_VSTD::move(__r));
1520 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1522 template <class _Rp>
1524 promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
1526 #ifndef _LIBCPP_NO_EXCEPTIONS
1527 if (__state_ == nullptr)
1528 throw future_error(make_error_code(future_errc::no_state));
1530 __state_->set_exception_at_thread_exit(__p);
1535 template <class _Rp>
1536 class _LIBCPP_TYPE_VIS_ONLY promise<_Rp&>
1538 __assoc_state<_Rp&>* __state_;
1540 _LIBCPP_INLINE_VISIBILITY
1541 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1543 template <class> friend class packaged_task;
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
1556 promise(const promise& __rhs);
1558 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1562 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1563 _LIBCPP_INLINE_VISIBILITY
1564 promise& operator=(promise&& __rhs) _NOEXCEPT
1566 promise(std::move(__rhs)).swap(*this);
1569 promise& operator=(const promise& __rhs) = delete;
1570 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1572 promise& operator=(const promise& __rhs);
1574 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1575 _LIBCPP_INLINE_VISIBILITY
1576 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1578 // retrieving the result
1579 future<_Rp&> get_future();
1581 // setting the result
1582 void set_value(_Rp& __r);
1583 void set_exception(exception_ptr __p);
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);
1590 template <class _Rp>
1591 promise<_Rp&>::promise()
1592 : __state_(new __assoc_state<_Rp&>)
1596 template <class _Rp>
1597 template <class _Alloc>
1598 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1600 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1601 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1602 typedef __allocator_destructor<_A2> _D2;
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());
1609 template <class _Rp>
1610 promise<_Rp&>::~promise()
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))
1618 __state_->__release_shared();
1622 template <class _Rp>
1624 promise<_Rp&>::get_future()
1626 #ifndef _LIBCPP_NO_EXCEPTIONS
1627 if (__state_ == nullptr)
1628 throw future_error(make_error_code(future_errc::no_state));
1630 return future<_Rp&>(__state_);
1633 template <class _Rp>
1635 promise<_Rp&>::set_value(_Rp& __r)
1637 #ifndef _LIBCPP_NO_EXCEPTIONS
1638 if (__state_ == nullptr)
1639 throw future_error(make_error_code(future_errc::no_state));
1641 __state_->set_value(__r);
1644 template <class _Rp>
1646 promise<_Rp&>::set_exception(exception_ptr __p)
1648 #ifndef _LIBCPP_NO_EXCEPTIONS
1649 if (__state_ == nullptr)
1650 throw future_error(make_error_code(future_errc::no_state));
1652 __state_->set_exception(__p);
1655 template <class _Rp>
1657 promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1659 #ifndef _LIBCPP_NO_EXCEPTIONS
1660 if (__state_ == nullptr)
1661 throw future_error(make_error_code(future_errc::no_state));
1663 __state_->set_value_at_thread_exit(__r);
1666 template <class _Rp>
1668 promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
1670 #ifndef _LIBCPP_NO_EXCEPTIONS
1671 if (__state_ == nullptr)
1672 throw future_error(make_error_code(future_errc::no_state));
1674 __state_->set_exception_at_thread_exit(__p);
1680 class _LIBCPP_TYPE_VIS promise<void>
1682 __assoc_sub_state* __state_;
1684 _LIBCPP_INLINE_VISIBILITY
1685 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1687 template <class> friend class packaged_task;
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
1700 promise(const promise& __rhs);
1702 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1706 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1707 _LIBCPP_INLINE_VISIBILITY
1708 promise& operator=(promise&& __rhs) _NOEXCEPT
1710 promise(std::move(__rhs)).swap(*this);
1713 promise& operator=(const promise& __rhs) = delete;
1714 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1716 promise& operator=(const promise& __rhs);
1718 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1719 _LIBCPP_INLINE_VISIBILITY
1720 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1722 // retrieving the result
1723 future<void> get_future();
1725 // setting the result
1727 void set_exception(exception_ptr __p);
1729 // setting the result with deferred notification
1730 void set_value_at_thread_exit();
1731 void set_exception_at_thread_exit(exception_ptr __p);
1734 template <class _Alloc>
1735 promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1737 typedef __assoc_sub_state_alloc<_Alloc> _State;
1738 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1739 typedef __allocator_destructor<_A2> _D2;
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());
1746 template <class _Rp>
1747 inline _LIBCPP_INLINE_VISIBILITY
1749 swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1754 template <class _Rp, class _Alloc>
1755 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<promise<_Rp>, _Alloc>
1756 : public true_type {};
1758 #ifndef _LIBCPP_HAS_NO_VARIADICS
1762 template<class _Fp> class __packaged_task_base;
1764 template<class _Rp, class ..._ArgTypes>
1765 class __packaged_task_base<_Rp(_ArgTypes...)>
1767 __packaged_task_base(const __packaged_task_base&);
1768 __packaged_task_base& operator=(const __packaged_task_base&);
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;
1780 template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1782 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1783 class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1784 : public __packaged_task_base<_Rp(_ArgTypes...)>
1786 __compressed_pair<_Fp, _Alloc> __f_;
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)
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);
1804 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1806 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1807 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1809 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1812 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1814 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1816 __f_.~__compressed_pair<_Fp, _Alloc>();
1819 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1821 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
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);
1831 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1833 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1835 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1838 template <class _Callable> class __packaged_task_function;
1840 template<class _Rp, class ..._ArgTypes>
1841 class __packaged_task_function<_Rp(_ArgTypes...)>
1843 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1844 typename aligned_storage<3*sizeof(void*)>::type __buf_;
1848 typedef _Rp result_type;
1850 // construct/copy/destroy:
1851 _LIBCPP_INLINE_VISIBILITY
1852 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1854 __packaged_task_function(_Fp&& __f);
1855 template<class _Fp, class _Alloc>
1856 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1858 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1859 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1861 __packaged_task_function(const __packaged_task_function&) = delete;
1862 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1864 ~__packaged_task_function();
1866 void swap(__packaged_task_function&) _NOEXCEPT;
1868 _Rp operator()(_ArgTypes...) const;
1871 template<class _Rp, class ..._ArgTypes>
1872 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1874 if (__f.__f_ == nullptr)
1876 else if (__f.__f_ == (__base*)&__f.__buf_)
1878 __f_ = (__base*)&__buf_;
1879 __f.__f_->__move_to(__f_);
1888 template<class _Rp, class ..._ArgTypes>
1889 template <class _Fp>
1890 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
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_))
1897 __f_ = (__base*)&__buf_;
1898 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1902 typedef allocator<_FF> _Ap;
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();
1911 template<class _Rp, class ..._ArgTypes>
1912 template <class _Fp, class _Alloc>
1913 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1914 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
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_))
1921 __f_ = (__base*)&__buf_;
1922 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1926 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
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());
1936 template<class _Rp, class ..._ArgTypes>
1937 __packaged_task_function<_Rp(_ArgTypes...)>&
1938 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1940 if (__f_ == (__base*)&__buf_)
1943 __f_->destroy_deallocate();
1945 if (__f.__f_ == nullptr)
1947 else if (__f.__f_ == (__base*)&__f.__buf_)
1949 __f_ = (__base*)&__buf_;
1950 __f.__f_->__move_to(__f_);
1960 template<class _Rp, class ..._ArgTypes>
1961 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1963 if (__f_ == (__base*)&__buf_)
1966 __f_->destroy_deallocate();
1969 template<class _Rp, class ..._ArgTypes>
1971 __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1973 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1975 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1976 __base* __t = (__base*)&__tempbuf;
1977 __f_->__move_to(__t);
1980 __f.__f_->__move_to((__base*)&__buf_);
1981 __f.__f_->destroy();
1983 __f_ = (__base*)&__buf_;
1984 __t->__move_to((__base*)&__f.__buf_);
1986 __f.__f_ = (__base*)&__f.__buf_;
1988 else if (__f_ == (__base*)&__buf_)
1990 __f_->__move_to((__base*)&__f.__buf_);
1993 __f.__f_ = (__base*)&__f.__buf_;
1995 else if (__f.__f_ == (__base*)&__f.__buf_)
1997 __f.__f_->__move_to((__base*)&__buf_);
1998 __f.__f_->destroy();
2000 __f_ = (__base*)&__buf_;
2003 _VSTD::swap(__f_, __f.__f_);
2006 template<class _Rp, class ..._ArgTypes>
2007 inline _LIBCPP_INLINE_VISIBILITY
2009 __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
2011 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
2014 template<class _Rp, class ..._ArgTypes>
2015 class _LIBCPP_TYPE_VIS_ONLY packaged_task<_Rp(_ArgTypes...)>
2018 typedef _Rp result_type;
2021 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2022 promise<result_type> __p_;
2025 // construction and destruction
2026 _LIBCPP_INLINE_VISIBILITY
2027 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2028 template <class _Fp,
2029 class = typename enable_if
2032 typename decay<_Fp>::type,
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
2043 typename decay<_Fp>::type,
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;
2055 packaged_task(const packaged_task&) = delete;
2056 packaged_task& operator=(const packaged_task&) = delete;
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
2065 __f_ = _VSTD::move(__other.__f_);
2066 __p_ = _VSTD::move(__other.__p_);
2069 _LIBCPP_INLINE_VISIBILITY
2070 void swap(packaged_task& __other) _NOEXCEPT
2072 __f_.swap(__other.__f_);
2073 __p_.swap(__other.__p_);
2076 _LIBCPP_INLINE_VISIBILITY
2077 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2080 _LIBCPP_INLINE_VISIBILITY
2081 future<result_type> get_future() {return __p_.get_future();}
2084 void operator()(_ArgTypes... __args);
2085 void make_ready_at_thread_exit(_ArgTypes... __args);
2090 template<class _Rp, class ..._ArgTypes>
2092 packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
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));
2101 #endif // _LIBCPP_NO_EXCEPTIONS
2102 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2103 #ifndef _LIBCPP_NO_EXCEPTIONS
2107 __p_.set_exception(current_exception());
2109 #endif // _LIBCPP_NO_EXCEPTIONS
2112 template<class _Rp, class ..._ArgTypes>
2114 packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
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));
2123 #endif // _LIBCPP_NO_EXCEPTIONS
2124 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2125 #ifndef _LIBCPP_NO_EXCEPTIONS
2129 __p_.set_exception_at_thread_exit(current_exception());
2131 #endif // _LIBCPP_NO_EXCEPTIONS
2134 template<class _Rp, class ..._ArgTypes>
2136 packaged_task<_Rp(_ArgTypes...)>::reset()
2138 #ifndef _LIBCPP_NO_EXCEPTIONS
2140 throw future_error(make_error_code(future_errc::no_state));
2141 #endif // _LIBCPP_NO_EXCEPTIONS
2142 __p_ = promise<result_type>();
2145 template<class ..._ArgTypes>
2146 class _LIBCPP_TYPE_VIS_ONLY packaged_task<void(_ArgTypes...)>
2149 typedef void result_type;
2152 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2153 promise<result_type> __p_;
2156 // construction and destruction
2157 _LIBCPP_INLINE_VISIBILITY
2158 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2159 template <class _Fp,
2160 class = typename enable_if
2163 typename decay<_Fp>::type,
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
2174 typename decay<_Fp>::type,
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;
2186 packaged_task(const packaged_task&) = delete;
2187 packaged_task& operator=(const packaged_task&) = delete;
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
2196 __f_ = _VSTD::move(__other.__f_);
2197 __p_ = _VSTD::move(__other.__p_);
2200 _LIBCPP_INLINE_VISIBILITY
2201 void swap(packaged_task& __other) _NOEXCEPT
2203 __f_.swap(__other.__f_);
2204 __p_.swap(__other.__p_);
2207 _LIBCPP_INLINE_VISIBILITY
2208 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2211 _LIBCPP_INLINE_VISIBILITY
2212 future<result_type> get_future() {return __p_.get_future();}
2215 void operator()(_ArgTypes... __args);
2216 void make_ready_at_thread_exit(_ArgTypes... __args);
2221 template<class ..._ArgTypes>
2223 packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
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));
2232 #endif // _LIBCPP_NO_EXCEPTIONS
2233 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2235 #ifndef _LIBCPP_NO_EXCEPTIONS
2239 __p_.set_exception(current_exception());
2241 #endif // _LIBCPP_NO_EXCEPTIONS
2244 template<class ..._ArgTypes>
2246 packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
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));
2255 #endif // _LIBCPP_NO_EXCEPTIONS
2256 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2257 __p_.set_value_at_thread_exit();
2258 #ifndef _LIBCPP_NO_EXCEPTIONS
2262 __p_.set_exception_at_thread_exit(current_exception());
2264 #endif // _LIBCPP_NO_EXCEPTIONS
2267 template<class ..._ArgTypes>
2269 packaged_task<void(_ArgTypes...)>::reset()
2271 #ifndef _LIBCPP_NO_EXCEPTIONS
2273 throw future_error(make_error_code(future_errc::no_state));
2274 #endif // _LIBCPP_NO_EXCEPTIONS
2275 __p_ = promise<result_type>();
2278 template <class _Callable>
2279 inline _LIBCPP_INLINE_VISIBILITY
2281 swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
2286 template <class _Callable, class _Alloc>
2287 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<packaged_task<_Callable>, _Alloc>
2288 : public true_type {};
2290 template <class _Rp, class _Fp>
2292 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2293 __make_deferred_assoc_state(_Fp&& __f)
2295 __make_deferred_assoc_state(_Fp __f)
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());
2303 template <class _Rp, class _Fp>
2305 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2306 __make_async_assoc_state(_Fp&& __f)
2308 __make_async_assoc_state(_Fp __f)
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());
2317 template <class _Fp, class... _Args>
2320 tuple<_Fp, _Args...> __f_;
2323 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2325 _LIBCPP_INLINE_VISIBILITY
2326 explicit __async_func(_Fp&& __f, _Args&&... __args)
2327 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2329 _LIBCPP_INLINE_VISIBILITY
2330 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2334 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2335 return __execute(_Index());
2338 template <size_t ..._Indices>
2340 __execute(__tuple_indices<_Indices...>)
2342 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2346 inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2347 { return (int(__policy) & int(__value)) != 0; }
2349 template <class _Fp, class... _Args>
2350 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2351 async(launch __policy, _Fp&& __f, _Args&&... __args)
2353 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2354 typedef typename _BF::_Rp _Rp;
2356 #ifndef _LIBCPP_NO_EXCEPTIONS
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
2365 catch ( ... ) { if (__policy == launch::async) throw ; }
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>{};
2374 template <class _Fp, class... _Args>
2375 inline _LIBCPP_INLINE_VISIBILITY
2376 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2377 async(_Fp&& __f, _Args&&... __args)
2379 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2380 _VSTD::forward<_Args>(__args)...);
2383 #endif // _LIBCPP_HAS_NO_VARIADICS
2387 template <class _Rp>
2388 class _LIBCPP_TYPE_VIS_ONLY shared_future
2390 __assoc_state<_Rp>* __state_;
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
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
2412 shared_future(std::move(__rhs)).swap(*this);
2415 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2417 // retrieving the value
2418 _LIBCPP_INLINE_VISIBILITY
2419 const _Rp& get() const {return __state_->copy();}
2421 _LIBCPP_INLINE_VISIBILITY
2422 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2424 // functions to check state
2425 _LIBCPP_INLINE_VISIBILITY
2426 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2428 _LIBCPP_INLINE_VISIBILITY
2429 void wait() const {__state_->wait();}
2430 template <class _Rep, class _Period>
2431 _LIBCPP_INLINE_VISIBILITY
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
2438 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2439 {return __state_->wait_until(__abs_time);}
2442 template <class _Rp>
2443 shared_future<_Rp>::~shared_future()
2446 __state_->__release_shared();
2449 template <class _Rp>
2451 shared_future<_Rp>::operator=(const shared_future& __rhs)
2454 __rhs.__state_->__add_shared();
2456 __state_->__release_shared();
2457 __state_ = __rhs.__state_;
2461 template <class _Rp>
2462 class _LIBCPP_TYPE_VIS_ONLY shared_future<_Rp&>
2464 __assoc_state<_Rp&>* __state_;
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
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
2486 shared_future(std::move(__rhs)).swap(*this);
2489 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2491 // retrieving the value
2492 _LIBCPP_INLINE_VISIBILITY
2493 _Rp& get() const {return __state_->copy();}
2495 _LIBCPP_INLINE_VISIBILITY
2496 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2498 // functions to check state
2499 _LIBCPP_INLINE_VISIBILITY
2500 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2502 _LIBCPP_INLINE_VISIBILITY
2503 void wait() const {__state_->wait();}
2504 template <class _Rep, class _Period>
2505 _LIBCPP_INLINE_VISIBILITY
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
2512 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2513 {return __state_->wait_until(__abs_time);}
2516 template <class _Rp>
2517 shared_future<_Rp&>::~shared_future()
2520 __state_->__release_shared();
2523 template <class _Rp>
2524 shared_future<_Rp&>&
2525 shared_future<_Rp&>::operator=(const shared_future& __rhs)
2528 __rhs.__state_->__add_shared();
2530 __state_->__release_shared();
2531 __state_ = __rhs.__state_;
2536 class _LIBCPP_TYPE_VIS shared_future<void>
2538 __assoc_sub_state* __state_;
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
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
2560 shared_future(std::move(__rhs)).swap(*this);
2563 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2565 // retrieving the value
2566 _LIBCPP_INLINE_VISIBILITY
2567 void get() const {__state_->copy();}
2569 _LIBCPP_INLINE_VISIBILITY
2570 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2572 // functions to check state
2573 _LIBCPP_INLINE_VISIBILITY
2574 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2576 _LIBCPP_INLINE_VISIBILITY
2577 void wait() const {__state_->wait();}
2578 template <class _Rep, class _Period>
2579 _LIBCPP_INLINE_VISIBILITY
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
2586 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2587 {return __state_->wait_until(__abs_time);}
2590 template <class _Rp>
2591 inline _LIBCPP_INLINE_VISIBILITY
2593 swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2598 template <class _Rp>
2599 inline _LIBCPP_INLINE_VISIBILITY
2601 future<_Rp>::share()
2603 return shared_future<_Rp>(_VSTD::move(*this));
2606 template <class _Rp>
2607 inline _LIBCPP_INLINE_VISIBILITY
2609 future<_Rp&>::share()
2611 return shared_future<_Rp&>(_VSTD::move(*this));
2614 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2616 inline _LIBCPP_INLINE_VISIBILITY
2618 future<void>::share()
2620 return shared_future<void>(_VSTD::move(*this));
2623 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2625 _LIBCPP_END_NAMESPACE_STD
2627 #endif // !_LIBCPP_HAS_NO_THREADS
2629 #endif // _LIBCPP_FUTURE