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();
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; // 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 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 void __throw_future_error(future_errc _Ev)
520 #ifndef _LIBCPP_NO_EXCEPTIONS
521 throw future_error(make_error_code(_Ev));
528 class _LIBCPP_TYPE_VIS __assoc_sub_state
529 : public __shared_count
532 exception_ptr __exception_;
533 mutable mutex __mut_;
534 mutable condition_variable __cv_;
537 virtual void __on_zero_shared() _NOEXCEPT;
538 void __sub_wait(unique_lock<mutex>& __lk);
543 __future_attached = 2,
548 _LIBCPP_INLINE_VISIBILITY
549 __assoc_sub_state() : __state_(0) {}
551 _LIBCPP_INLINE_VISIBILITY
552 bool __has_value() const
553 {return (__state_ & __constructed) || (__exception_ != nullptr);}
555 _LIBCPP_INLINE_VISIBILITY
556 void __set_future_attached()
558 lock_guard<mutex> __lk(__mut_);
559 __state_ |= __future_attached;
561 _LIBCPP_INLINE_VISIBILITY
562 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
564 _LIBCPP_INLINE_VISIBILITY
565 void __set_deferred() {__state_ |= deferred;}
568 _LIBCPP_INLINE_VISIBILITY
569 bool __is_ready() const {return (__state_ & ready) != 0;}
572 void set_value_at_thread_exit();
574 void set_exception(exception_ptr __p);
575 void set_exception_at_thread_exit(exception_ptr __p);
580 template <class _Rep, class _Period>
582 _LIBCPP_INLINE_VISIBILITY
583 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
584 template <class _Clock, class _Duration>
586 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
588 virtual void __execute();
591 template <class _Clock, class _Duration>
593 __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
595 unique_lock<mutex> __lk(__mut_);
596 if (__state_ & deferred)
597 return future_status::deferred;
598 while (!(__state_ & ready) && _Clock::now() < __abs_time)
599 __cv_.wait_until(__lk, __abs_time);
600 if (__state_ & ready)
601 return future_status::ready;
602 return future_status::timeout;
605 template <class _Rep, class _Period>
608 __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
610 return wait_until(chrono::steady_clock::now() + __rel_time);
615 : public __assoc_sub_state
617 typedef __assoc_sub_state base;
618 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
622 virtual void __on_zero_shared() _NOEXCEPT;
625 template <class _Arg>
626 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
627 void set_value(_Arg&& __arg);
629 void set_value(_Arg& __arg);
632 template <class _Arg>
633 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
634 void set_value_at_thread_exit(_Arg&& __arg);
636 void set_value_at_thread_exit(_Arg& __arg);
640 typename add_lvalue_reference<_Rp>::type copy();
645 __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
647 if (this->__state_ & base::__constructed)
648 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
653 template <class _Arg>
655 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
656 __assoc_state<_Rp>::set_value(_Arg&& __arg)
658 __assoc_state<_Rp>::set_value(_Arg& __arg)
661 unique_lock<mutex> __lk(this->__mut_);
662 if (this->__has_value())
663 __throw_future_error(future_errc::promise_already_satisfied);
664 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
665 this->__state_ |= base::__constructed | base::ready;
670 template <class _Arg>
672 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
673 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
675 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
678 unique_lock<mutex> __lk(this->__mut_);
679 if (this->__has_value())
680 __throw_future_error(future_errc::promise_already_satisfied);
681 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
682 this->__state_ |= base::__constructed;
683 __thread_local_data()->__make_ready_at_thread_exit(this);
688 __assoc_state<_Rp>::move()
690 unique_lock<mutex> __lk(this->__mut_);
691 this->__sub_wait(__lk);
692 if (this->__exception_ != nullptr)
693 rethrow_exception(this->__exception_);
694 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
698 typename add_lvalue_reference<_Rp>::type
699 __assoc_state<_Rp>::copy()
701 unique_lock<mutex> __lk(this->__mut_);
702 this->__sub_wait(__lk);
703 if (this->__exception_ != nullptr)
704 rethrow_exception(this->__exception_);
705 return *reinterpret_cast<_Rp*>(&__value_);
709 class __assoc_state<_Rp&>
710 : public __assoc_sub_state
712 typedef __assoc_sub_state base;
717 virtual void __on_zero_shared() _NOEXCEPT;
720 void set_value(_Rp& __arg);
721 void set_value_at_thread_exit(_Rp& __arg);
728 __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
735 __assoc_state<_Rp&>::set_value(_Rp& __arg)
737 unique_lock<mutex> __lk(this->__mut_);
738 if (this->__has_value())
739 __throw_future_error(future_errc::promise_already_satisfied);
740 __value_ = _VSTD::addressof(__arg);
741 this->__state_ |= base::__constructed | base::ready;
747 __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
749 unique_lock<mutex> __lk(this->__mut_);
750 if (this->__has_value())
751 __throw_future_error(future_errc::promise_already_satisfied);
752 __value_ = _VSTD::addressof(__arg);
753 this->__state_ |= base::__constructed;
754 __thread_local_data()->__make_ready_at_thread_exit(this);
759 __assoc_state<_Rp&>::copy()
761 unique_lock<mutex> __lk(this->__mut_);
762 this->__sub_wait(__lk);
763 if (this->__exception_ != nullptr)
764 rethrow_exception(this->__exception_);
768 template <class _Rp, class _Alloc>
769 class __assoc_state_alloc
770 : public __assoc_state<_Rp>
772 typedef __assoc_state<_Rp> base;
775 virtual void __on_zero_shared() _NOEXCEPT;
777 _LIBCPP_INLINE_VISIBILITY
778 explicit __assoc_state_alloc(const _Alloc& __a)
782 template <class _Rp, class _Alloc>
784 __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
786 if (this->__state_ & base::__constructed)
787 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
788 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
789 typedef allocator_traits<_Al> _ATraits;
790 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
792 this->~__assoc_state_alloc();
793 __a.deallocate(_PTraits::pointer_to(*this), 1);
796 template <class _Rp, class _Alloc>
797 class __assoc_state_alloc<_Rp&, _Alloc>
798 : public __assoc_state<_Rp&>
800 typedef __assoc_state<_Rp&> base;
803 virtual void __on_zero_shared() _NOEXCEPT;
805 _LIBCPP_INLINE_VISIBILITY
806 explicit __assoc_state_alloc(const _Alloc& __a)
810 template <class _Rp, class _Alloc>
812 __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
814 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
815 typedef allocator_traits<_Al> _ATraits;
816 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
818 this->~__assoc_state_alloc();
819 __a.deallocate(_PTraits::pointer_to(*this), 1);
822 template <class _Alloc>
823 class __assoc_sub_state_alloc
824 : public __assoc_sub_state
826 typedef __assoc_sub_state base;
829 virtual void __on_zero_shared() _NOEXCEPT;
831 _LIBCPP_INLINE_VISIBILITY
832 explicit __assoc_sub_state_alloc(const _Alloc& __a)
836 template <class _Alloc>
838 __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
840 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
841 typedef allocator_traits<_Al> _ATraits;
842 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
844 this->~__assoc_sub_state_alloc();
845 __a.deallocate(_PTraits::pointer_to(*this), 1);
848 template <class _Rp, class _Fp>
849 class __deferred_assoc_state
850 : public __assoc_state<_Rp>
852 typedef __assoc_state<_Rp> base;
857 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
858 _LIBCPP_INLINE_VISIBILITY
859 explicit __deferred_assoc_state(_Fp&& __f);
862 virtual void __execute();
865 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
867 template <class _Rp, class _Fp>
869 __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
870 : __func_(_VSTD::forward<_Fp>(__f))
872 this->__set_deferred();
875 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
877 template <class _Rp, class _Fp>
879 __deferred_assoc_state<_Rp, _Fp>::__execute()
881 #ifndef _LIBCPP_NO_EXCEPTIONS
884 #endif // _LIBCPP_NO_EXCEPTIONS
885 this->set_value(__func_());
886 #ifndef _LIBCPP_NO_EXCEPTIONS
890 this->set_exception(current_exception());
892 #endif // _LIBCPP_NO_EXCEPTIONS
896 class __deferred_assoc_state<void, _Fp>
897 : public __assoc_sub_state
899 typedef __assoc_sub_state base;
904 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
905 _LIBCPP_INLINE_VISIBILITY
906 explicit __deferred_assoc_state(_Fp&& __f);
909 virtual void __execute();
912 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
916 __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
917 : __func_(_VSTD::forward<_Fp>(__f))
919 this->__set_deferred();
922 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
926 __deferred_assoc_state<void, _Fp>::__execute()
928 #ifndef _LIBCPP_NO_EXCEPTIONS
931 #endif // _LIBCPP_NO_EXCEPTIONS
934 #ifndef _LIBCPP_NO_EXCEPTIONS
938 this->set_exception(current_exception());
940 #endif // _LIBCPP_NO_EXCEPTIONS
943 template <class _Rp, class _Fp>
944 class __async_assoc_state
945 : public __assoc_state<_Rp>
947 typedef __assoc_state<_Rp> base;
951 virtual void __on_zero_shared() _NOEXCEPT;
953 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
954 _LIBCPP_INLINE_VISIBILITY
955 explicit __async_assoc_state(_Fp&& __f);
958 virtual void __execute();
961 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
963 template <class _Rp, class _Fp>
965 __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
966 : __func_(_VSTD::forward<_Fp>(__f))
970 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
972 template <class _Rp, class _Fp>
974 __async_assoc_state<_Rp, _Fp>::__execute()
976 #ifndef _LIBCPP_NO_EXCEPTIONS
979 #endif // _LIBCPP_NO_EXCEPTIONS
980 this->set_value(__func_());
981 #ifndef _LIBCPP_NO_EXCEPTIONS
985 this->set_exception(current_exception());
987 #endif // _LIBCPP_NO_EXCEPTIONS
990 template <class _Rp, class _Fp>
992 __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
995 base::__on_zero_shared();
999 class __async_assoc_state<void, _Fp>
1000 : public __assoc_sub_state
1002 typedef __assoc_sub_state base;
1006 virtual void __on_zero_shared() _NOEXCEPT;
1008 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1009 _LIBCPP_INLINE_VISIBILITY
1010 explicit __async_assoc_state(_Fp&& __f);
1013 virtual void __execute();
1016 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1018 template <class _Fp>
1020 __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1021 : __func_(_VSTD::forward<_Fp>(__f))
1025 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1027 template <class _Fp>
1029 __async_assoc_state<void, _Fp>::__execute()
1031 #ifndef _LIBCPP_NO_EXCEPTIONS
1034 #endif // _LIBCPP_NO_EXCEPTIONS
1037 #ifndef _LIBCPP_NO_EXCEPTIONS
1041 this->set_exception(current_exception());
1043 #endif // _LIBCPP_NO_EXCEPTIONS
1046 template <class _Fp>
1048 __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1051 base::__on_zero_shared();
1054 template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1055 template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
1059 template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
1061 template <class _Rp, class _Fp>
1063 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1064 __make_deferred_assoc_state(_Fp&& __f);
1066 __make_deferred_assoc_state(_Fp __f);
1069 template <class _Rp, class _Fp>
1071 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1072 __make_async_assoc_state(_Fp&& __f);
1074 __make_async_assoc_state(_Fp __f);
1077 template <class _Rp>
1078 class _LIBCPP_TEMPLATE_VIS future
1080 __assoc_state<_Rp>* __state_;
1082 explicit future(__assoc_state<_Rp>* __state);
1084 template <class> friend class promise;
1085 template <class> friend class shared_future;
1087 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1088 template <class _R1, class _Fp>
1089 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1090 template <class _R1, class _Fp>
1091 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
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);
1100 _LIBCPP_INLINE_VISIBILITY
1101 future() _NOEXCEPT : __state_(nullptr) {}
1102 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1103 _LIBCPP_INLINE_VISIBILITY
1104 future(future&& __rhs) _NOEXCEPT
1105 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1106 future(const future&) = delete;
1107 future& operator=(const future&) = delete;
1108 _LIBCPP_INLINE_VISIBILITY
1109 future& operator=(future&& __rhs) _NOEXCEPT
1111 future(std::move(__rhs)).swap(*this);
1114 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1116 future(const future&);
1117 future& operator=(const future&);
1119 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1121 _LIBCPP_INLINE_VISIBILITY
1122 shared_future<_Rp> share();
1124 // retrieving the value
1127 _LIBCPP_INLINE_VISIBILITY
1128 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1130 // functions to check state
1131 _LIBCPP_INLINE_VISIBILITY
1132 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1134 _LIBCPP_INLINE_VISIBILITY
1135 void wait() const {__state_->wait();}
1136 template <class _Rep, class _Period>
1137 _LIBCPP_INLINE_VISIBILITY
1139 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1140 {return __state_->wait_for(__rel_time);}
1141 template <class _Clock, class _Duration>
1142 _LIBCPP_INLINE_VISIBILITY
1144 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1145 {return __state_->wait_until(__abs_time);}
1148 template <class _Rp>
1149 future<_Rp>::future(__assoc_state<_Rp>* __state)
1152 if (__state_->__has_future_attached())
1153 __throw_future_error(future_errc::future_already_retrieved);
1154 __state_->__add_shared();
1155 __state_->__set_future_attached();
1158 struct __release_shared_count
1160 void operator()(__shared_count* p) {p->__release_shared();}
1163 template <class _Rp>
1164 future<_Rp>::~future()
1167 __state_->__release_shared();
1170 template <class _Rp>
1174 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1175 __assoc_state<_Rp>* __s = __state_;
1180 template <class _Rp>
1181 class _LIBCPP_TEMPLATE_VIS future<_Rp&>
1183 __assoc_state<_Rp&>* __state_;
1185 explicit future(__assoc_state<_Rp&>* __state);
1187 template <class> friend class promise;
1188 template <class> friend class shared_future;
1190 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
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);
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);
1203 _LIBCPP_INLINE_VISIBILITY
1204 future() _NOEXCEPT : __state_(nullptr) {}
1205 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1206 _LIBCPP_INLINE_VISIBILITY
1207 future(future&& __rhs) _NOEXCEPT
1208 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1209 future(const future&) = delete;
1210 future& operator=(const future&) = delete;
1211 _LIBCPP_INLINE_VISIBILITY
1212 future& operator=(future&& __rhs) _NOEXCEPT
1214 future(std::move(__rhs)).swap(*this);
1217 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1219 future(const future&);
1220 future& operator=(const future&);
1222 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1224 _LIBCPP_INLINE_VISIBILITY
1225 shared_future<_Rp&> share();
1227 // retrieving the value
1230 _LIBCPP_INLINE_VISIBILITY
1231 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1233 // functions to check state
1234 _LIBCPP_INLINE_VISIBILITY
1235 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1237 _LIBCPP_INLINE_VISIBILITY
1238 void wait() const {__state_->wait();}
1239 template <class _Rep, class _Period>
1240 _LIBCPP_INLINE_VISIBILITY
1242 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1243 {return __state_->wait_for(__rel_time);}
1244 template <class _Clock, class _Duration>
1245 _LIBCPP_INLINE_VISIBILITY
1247 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1248 {return __state_->wait_until(__abs_time);}
1251 template <class _Rp>
1252 future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1255 if (__state_->__has_future_attached())
1256 __throw_future_error(future_errc::future_already_retrieved);
1257 __state_->__add_shared();
1258 __state_->__set_future_attached();
1261 template <class _Rp>
1262 future<_Rp&>::~future()
1265 __state_->__release_shared();
1268 template <class _Rp>
1272 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1273 __assoc_state<_Rp&>* __s = __state_;
1279 class _LIBCPP_TYPE_VIS future<void>
1281 __assoc_sub_state* __state_;
1283 explicit future(__assoc_sub_state* __state);
1285 template <class> friend class promise;
1286 template <class> friend class shared_future;
1288 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1289 template <class _R1, class _Fp>
1290 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1291 template <class _R1, class _Fp>
1292 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
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);
1301 _LIBCPP_INLINE_VISIBILITY
1302 future() _NOEXCEPT : __state_(nullptr) {}
1303 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1304 _LIBCPP_INLINE_VISIBILITY
1305 future(future&& __rhs) _NOEXCEPT
1306 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1307 future(const future&) = delete;
1308 future& operator=(const future&) = delete;
1309 _LIBCPP_INLINE_VISIBILITY
1310 future& operator=(future&& __rhs) _NOEXCEPT
1312 future(std::move(__rhs)).swap(*this);
1315 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1317 future(const future&);
1318 future& operator=(const future&);
1320 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1322 _LIBCPP_INLINE_VISIBILITY
1323 shared_future<void> share();
1325 // retrieving the value
1328 _LIBCPP_INLINE_VISIBILITY
1329 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1331 // functions to check state
1332 _LIBCPP_INLINE_VISIBILITY
1333 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1335 _LIBCPP_INLINE_VISIBILITY
1336 void wait() const {__state_->wait();}
1337 template <class _Rep, class _Period>
1338 _LIBCPP_INLINE_VISIBILITY
1340 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1341 {return __state_->wait_for(__rel_time);}
1342 template <class _Clock, class _Duration>
1343 _LIBCPP_INLINE_VISIBILITY
1345 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1346 {return __state_->wait_until(__abs_time);}
1349 template <class _Rp>
1350 inline _LIBCPP_INLINE_VISIBILITY
1352 swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1359 template <class _Callable> class packaged_task;
1361 template <class _Rp>
1362 class _LIBCPP_TEMPLATE_VIS promise
1364 __assoc_state<_Rp>* __state_;
1366 _LIBCPP_INLINE_VISIBILITY
1367 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1369 template <class> friend class packaged_task;
1372 template <class _Alloc>
1373 promise(allocator_arg_t, const _Alloc& __a);
1374 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1375 _LIBCPP_INLINE_VISIBILITY
1376 promise(promise&& __rhs) _NOEXCEPT
1377 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1378 promise(const promise& __rhs) = delete;
1379 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1381 promise(const promise& __rhs);
1383 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1387 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1388 _LIBCPP_INLINE_VISIBILITY
1389 promise& operator=(promise&& __rhs) _NOEXCEPT
1391 promise(std::move(__rhs)).swap(*this);
1394 promise& operator=(const promise& __rhs) = delete;
1395 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1397 promise& operator=(const promise& __rhs);
1399 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1400 _LIBCPP_INLINE_VISIBILITY
1401 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1403 // retrieving the result
1404 future<_Rp> get_future();
1406 // setting the result
1407 void set_value(const _Rp& __r);
1408 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1409 void set_value(_Rp&& __r);
1411 void set_exception(exception_ptr __p);
1413 // setting the result with deferred notification
1414 void set_value_at_thread_exit(const _Rp& __r);
1415 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1416 void set_value_at_thread_exit(_Rp&& __r);
1418 void set_exception_at_thread_exit(exception_ptr __p);
1421 template <class _Rp>
1422 promise<_Rp>::promise()
1423 : __state_(new __assoc_state<_Rp>)
1427 template <class _Rp>
1428 template <class _Alloc>
1429 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1431 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1432 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1433 typedef __allocator_destructor<_A2> _D2;
1435 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1436 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1437 __state_ = _VSTD::addressof(*__hold.release());
1440 template <class _Rp>
1441 promise<_Rp>::~promise()
1445 if (!__state_->__has_value() && __state_->use_count() > 1)
1446 __state_->set_exception(make_exception_ptr(
1447 future_error(make_error_code(future_errc::broken_promise))
1449 __state_->__release_shared();
1453 template <class _Rp>
1455 promise<_Rp>::get_future()
1457 if (__state_ == nullptr)
1458 __throw_future_error(future_errc::no_state);
1459 return future<_Rp>(__state_);
1462 template <class _Rp>
1464 promise<_Rp>::set_value(const _Rp& __r)
1466 if (__state_ == nullptr)
1467 __throw_future_error(future_errc::no_state);
1468 __state_->set_value(__r);
1471 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1473 template <class _Rp>
1475 promise<_Rp>::set_value(_Rp&& __r)
1477 if (__state_ == nullptr)
1478 __throw_future_error(future_errc::no_state);
1479 __state_->set_value(_VSTD::move(__r));
1482 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1484 template <class _Rp>
1486 promise<_Rp>::set_exception(exception_ptr __p)
1488 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
1489 if (__state_ == nullptr)
1490 __throw_future_error(future_errc::no_state);
1491 __state_->set_exception(__p);
1494 template <class _Rp>
1496 promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1498 if (__state_ == nullptr)
1499 __throw_future_error(future_errc::no_state);
1500 __state_->set_value_at_thread_exit(__r);
1503 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1505 template <class _Rp>
1507 promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1509 if (__state_ == nullptr)
1510 __throw_future_error(future_errc::no_state);
1511 __state_->set_value_at_thread_exit(_VSTD::move(__r));
1514 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1516 template <class _Rp>
1518 promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
1520 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1521 if (__state_ == nullptr)
1522 __throw_future_error(future_errc::no_state);
1523 __state_->set_exception_at_thread_exit(__p);
1528 template <class _Rp>
1529 class _LIBCPP_TEMPLATE_VIS promise<_Rp&>
1531 __assoc_state<_Rp&>* __state_;
1533 _LIBCPP_INLINE_VISIBILITY
1534 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1536 template <class> friend class packaged_task;
1540 template <class _Allocator>
1541 promise(allocator_arg_t, const _Allocator& __a);
1542 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1543 _LIBCPP_INLINE_VISIBILITY
1544 promise(promise&& __rhs) _NOEXCEPT
1545 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1546 promise(const promise& __rhs) = delete;
1547 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1549 promise(const promise& __rhs);
1551 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1555 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1556 _LIBCPP_INLINE_VISIBILITY
1557 promise& operator=(promise&& __rhs) _NOEXCEPT
1559 promise(std::move(__rhs)).swap(*this);
1562 promise& operator=(const promise& __rhs) = delete;
1563 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1565 promise& operator=(const promise& __rhs);
1567 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1568 _LIBCPP_INLINE_VISIBILITY
1569 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1571 // retrieving the result
1572 future<_Rp&> get_future();
1574 // setting the result
1575 void set_value(_Rp& __r);
1576 void set_exception(exception_ptr __p);
1578 // setting the result with deferred notification
1579 void set_value_at_thread_exit(_Rp&);
1580 void set_exception_at_thread_exit(exception_ptr __p);
1583 template <class _Rp>
1584 promise<_Rp&>::promise()
1585 : __state_(new __assoc_state<_Rp&>)
1589 template <class _Rp>
1590 template <class _Alloc>
1591 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1593 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1594 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1595 typedef __allocator_destructor<_A2> _D2;
1597 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1598 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1599 __state_ = _VSTD::addressof(*__hold.release());
1602 template <class _Rp>
1603 promise<_Rp&>::~promise()
1607 if (!__state_->__has_value() && __state_->use_count() > 1)
1608 __state_->set_exception(make_exception_ptr(
1609 future_error(make_error_code(future_errc::broken_promise))
1611 __state_->__release_shared();
1615 template <class _Rp>
1617 promise<_Rp&>::get_future()
1619 if (__state_ == nullptr)
1620 __throw_future_error(future_errc::no_state);
1621 return future<_Rp&>(__state_);
1624 template <class _Rp>
1626 promise<_Rp&>::set_value(_Rp& __r)
1628 if (__state_ == nullptr)
1629 __throw_future_error(future_errc::no_state);
1630 __state_->set_value(__r);
1633 template <class _Rp>
1635 promise<_Rp&>::set_exception(exception_ptr __p)
1637 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
1638 if (__state_ == nullptr)
1639 __throw_future_error(future_errc::no_state);
1640 __state_->set_exception(__p);
1643 template <class _Rp>
1645 promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1647 if (__state_ == nullptr)
1648 __throw_future_error(future_errc::no_state);
1649 __state_->set_value_at_thread_exit(__r);
1652 template <class _Rp>
1654 promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
1656 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1657 if (__state_ == nullptr)
1658 __throw_future_error(future_errc::no_state);
1659 __state_->set_exception_at_thread_exit(__p);
1665 class _LIBCPP_TYPE_VIS promise<void>
1667 __assoc_sub_state* __state_;
1669 _LIBCPP_INLINE_VISIBILITY
1670 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1672 template <class> friend class packaged_task;
1676 template <class _Allocator>
1677 promise(allocator_arg_t, const _Allocator& __a);
1678 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1679 _LIBCPP_INLINE_VISIBILITY
1680 promise(promise&& __rhs) _NOEXCEPT
1681 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1682 promise(const promise& __rhs) = delete;
1683 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1685 promise(const promise& __rhs);
1687 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1691 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1692 _LIBCPP_INLINE_VISIBILITY
1693 promise& operator=(promise&& __rhs) _NOEXCEPT
1695 promise(std::move(__rhs)).swap(*this);
1698 promise& operator=(const promise& __rhs) = delete;
1699 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1701 promise& operator=(const promise& __rhs);
1703 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1704 _LIBCPP_INLINE_VISIBILITY
1705 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1707 // retrieving the result
1708 future<void> get_future();
1710 // setting the result
1712 void set_exception(exception_ptr __p);
1714 // setting the result with deferred notification
1715 void set_value_at_thread_exit();
1716 void set_exception_at_thread_exit(exception_ptr __p);
1719 template <class _Alloc>
1720 promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1722 typedef __assoc_sub_state_alloc<_Alloc> _State;
1723 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1724 typedef __allocator_destructor<_A2> _D2;
1726 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1727 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1728 __state_ = _VSTD::addressof(*__hold.release());
1731 template <class _Rp>
1732 inline _LIBCPP_INLINE_VISIBILITY
1734 swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1739 template <class _Rp, class _Alloc>
1740 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
1741 : public true_type {};
1743 #ifndef _LIBCPP_HAS_NO_VARIADICS
1747 template<class _Fp> class __packaged_task_base;
1749 template<class _Rp, class ..._ArgTypes>
1750 class __packaged_task_base<_Rp(_ArgTypes...)>
1752 __packaged_task_base(const __packaged_task_base&);
1753 __packaged_task_base& operator=(const __packaged_task_base&);
1755 _LIBCPP_INLINE_VISIBILITY
1756 __packaged_task_base() {}
1757 _LIBCPP_INLINE_VISIBILITY
1758 virtual ~__packaged_task_base() {}
1759 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1760 virtual void destroy() = 0;
1761 virtual void destroy_deallocate() = 0;
1762 virtual _Rp operator()(_ArgTypes&& ...) = 0;
1765 template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1767 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1768 class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1769 : public __packaged_task_base<_Rp(_ArgTypes...)>
1771 __compressed_pair<_Fp, _Alloc> __f_;
1773 _LIBCPP_INLINE_VISIBILITY
1774 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
1775 _LIBCPP_INLINE_VISIBILITY
1776 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
1777 _LIBCPP_INLINE_VISIBILITY
1778 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
1780 _LIBCPP_INLINE_VISIBILITY
1781 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
1782 : __f_(_VSTD::move(__f), __a) {}
1783 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1784 virtual void destroy();
1785 virtual void destroy_deallocate();
1786 virtual _Rp operator()(_ArgTypes&& ... __args);
1789 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1791 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1792 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1794 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1797 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1799 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1801 __f_.~__compressed_pair<_Fp, _Alloc>();
1804 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1806 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
1808 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1809 typedef allocator_traits<_Ap> _ATraits;
1810 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1811 _Ap __a(__f_.second());
1812 __f_.~__compressed_pair<_Fp, _Alloc>();
1813 __a.deallocate(_PTraits::pointer_to(*this), 1);
1816 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1818 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1820 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1823 template <class _Callable> class __packaged_task_function;
1825 template<class _Rp, class ..._ArgTypes>
1826 class __packaged_task_function<_Rp(_ArgTypes...)>
1828 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1829 typename aligned_storage<3*sizeof(void*)>::type __buf_;
1833 typedef _Rp result_type;
1835 // construct/copy/destroy:
1836 _LIBCPP_INLINE_VISIBILITY
1837 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1839 __packaged_task_function(_Fp&& __f);
1840 template<class _Fp, class _Alloc>
1841 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1843 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1844 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1846 __packaged_task_function(const __packaged_task_function&) = delete;
1847 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1849 ~__packaged_task_function();
1851 void swap(__packaged_task_function&) _NOEXCEPT;
1853 _LIBCPP_INLINE_VISIBILITY
1854 _Rp operator()(_ArgTypes...) const;
1857 template<class _Rp, class ..._ArgTypes>
1858 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1860 if (__f.__f_ == nullptr)
1862 else if (__f.__f_ == (__base*)&__f.__buf_)
1864 __f_ = (__base*)&__buf_;
1865 __f.__f_->__move_to(__f_);
1874 template<class _Rp, class ..._ArgTypes>
1875 template <class _Fp>
1876 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
1879 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
1880 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1881 if (sizeof(_FF) <= sizeof(__buf_))
1883 __f_ = (__base*)&__buf_;
1884 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1888 typedef allocator<_FF> _Ap;
1890 typedef __allocator_destructor<_Ap> _Dp;
1891 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1892 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
1893 __f_ = __hold.release();
1897 template<class _Rp, class ..._ArgTypes>
1898 template <class _Fp, class _Alloc>
1899 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1900 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1903 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
1904 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1905 if (sizeof(_FF) <= sizeof(__buf_))
1907 __f_ = (__base*)&__buf_;
1908 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1912 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1914 typedef __allocator_destructor<_Ap> _Dp;
1915 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1916 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1917 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1918 __f_ = _VSTD::addressof(*__hold.release());
1922 template<class _Rp, class ..._ArgTypes>
1923 __packaged_task_function<_Rp(_ArgTypes...)>&
1924 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1926 if (__f_ == (__base*)&__buf_)
1929 __f_->destroy_deallocate();
1931 if (__f.__f_ == nullptr)
1933 else if (__f.__f_ == (__base*)&__f.__buf_)
1935 __f_ = (__base*)&__buf_;
1936 __f.__f_->__move_to(__f_);
1946 template<class _Rp, class ..._ArgTypes>
1947 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1949 if (__f_ == (__base*)&__buf_)
1952 __f_->destroy_deallocate();
1955 template<class _Rp, class ..._ArgTypes>
1957 __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1959 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1961 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1962 __base* __t = (__base*)&__tempbuf;
1963 __f_->__move_to(__t);
1966 __f.__f_->__move_to((__base*)&__buf_);
1967 __f.__f_->destroy();
1969 __f_ = (__base*)&__buf_;
1970 __t->__move_to((__base*)&__f.__buf_);
1972 __f.__f_ = (__base*)&__f.__buf_;
1974 else if (__f_ == (__base*)&__buf_)
1976 __f_->__move_to((__base*)&__f.__buf_);
1979 __f.__f_ = (__base*)&__f.__buf_;
1981 else if (__f.__f_ == (__base*)&__f.__buf_)
1983 __f.__f_->__move_to((__base*)&__buf_);
1984 __f.__f_->destroy();
1986 __f_ = (__base*)&__buf_;
1989 _VSTD::swap(__f_, __f.__f_);
1992 template<class _Rp, class ..._ArgTypes>
1995 __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1997 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
2000 template<class _Rp, class ..._ArgTypes>
2001 class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)>
2004 typedef _Rp result_type; // extension
2007 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2008 promise<result_type> __p_;
2011 // construction and destruction
2012 _LIBCPP_INLINE_VISIBILITY
2013 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2014 template <class _Fp,
2015 class = typename enable_if
2018 typename decay<_Fp>::type,
2023 _LIBCPP_INLINE_VISIBILITY
2024 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2025 template <class _Fp, class _Allocator,
2026 class = typename enable_if
2029 typename decay<_Fp>::type,
2034 _LIBCPP_INLINE_VISIBILITY
2035 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2036 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2037 __p_(allocator_arg, __a) {}
2038 // ~packaged_task() = default;
2041 packaged_task(const packaged_task&) = delete;
2042 packaged_task& operator=(const packaged_task&) = delete;
2045 _LIBCPP_INLINE_VISIBILITY
2046 packaged_task(packaged_task&& __other) _NOEXCEPT
2047 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2048 _LIBCPP_INLINE_VISIBILITY
2049 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2051 __f_ = _VSTD::move(__other.__f_);
2052 __p_ = _VSTD::move(__other.__p_);
2055 _LIBCPP_INLINE_VISIBILITY
2056 void swap(packaged_task& __other) _NOEXCEPT
2058 __f_.swap(__other.__f_);
2059 __p_.swap(__other.__p_);
2062 _LIBCPP_INLINE_VISIBILITY
2063 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2066 _LIBCPP_INLINE_VISIBILITY
2067 future<result_type> get_future() {return __p_.get_future();}
2070 void operator()(_ArgTypes... __args);
2071 void make_ready_at_thread_exit(_ArgTypes... __args);
2076 template<class _Rp, class ..._ArgTypes>
2078 packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
2080 if (__p_.__state_ == nullptr)
2081 __throw_future_error(future_errc::no_state);
2082 if (__p_.__state_->__has_value())
2083 __throw_future_error(future_errc::promise_already_satisfied);
2084 #ifndef _LIBCPP_NO_EXCEPTIONS
2087 #endif // _LIBCPP_NO_EXCEPTIONS
2088 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2089 #ifndef _LIBCPP_NO_EXCEPTIONS
2093 __p_.set_exception(current_exception());
2095 #endif // _LIBCPP_NO_EXCEPTIONS
2098 template<class _Rp, class ..._ArgTypes>
2100 packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2102 if (__p_.__state_ == nullptr)
2103 __throw_future_error(future_errc::no_state);
2104 if (__p_.__state_->__has_value())
2105 __throw_future_error(future_errc::promise_already_satisfied);
2106 #ifndef _LIBCPP_NO_EXCEPTIONS
2109 #endif // _LIBCPP_NO_EXCEPTIONS
2110 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2111 #ifndef _LIBCPP_NO_EXCEPTIONS
2115 __p_.set_exception_at_thread_exit(current_exception());
2117 #endif // _LIBCPP_NO_EXCEPTIONS
2120 template<class _Rp, class ..._ArgTypes>
2122 packaged_task<_Rp(_ArgTypes...)>::reset()
2125 __throw_future_error(future_errc::no_state);
2126 __p_ = promise<result_type>();
2129 template<class ..._ArgTypes>
2130 class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)>
2133 typedef void result_type; // extension
2136 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2137 promise<result_type> __p_;
2140 // construction and destruction
2141 _LIBCPP_INLINE_VISIBILITY
2142 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2143 template <class _Fp,
2144 class = typename enable_if
2147 typename decay<_Fp>::type,
2152 _LIBCPP_INLINE_VISIBILITY
2153 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2154 template <class _Fp, class _Allocator,
2155 class = typename enable_if
2158 typename decay<_Fp>::type,
2163 _LIBCPP_INLINE_VISIBILITY
2164 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2165 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2166 __p_(allocator_arg, __a) {}
2167 // ~packaged_task() = default;
2170 packaged_task(const packaged_task&) = delete;
2171 packaged_task& operator=(const packaged_task&) = delete;
2174 _LIBCPP_INLINE_VISIBILITY
2175 packaged_task(packaged_task&& __other) _NOEXCEPT
2176 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2177 _LIBCPP_INLINE_VISIBILITY
2178 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2180 __f_ = _VSTD::move(__other.__f_);
2181 __p_ = _VSTD::move(__other.__p_);
2184 _LIBCPP_INLINE_VISIBILITY
2185 void swap(packaged_task& __other) _NOEXCEPT
2187 __f_.swap(__other.__f_);
2188 __p_.swap(__other.__p_);
2191 _LIBCPP_INLINE_VISIBILITY
2192 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2195 _LIBCPP_INLINE_VISIBILITY
2196 future<result_type> get_future() {return __p_.get_future();}
2199 void operator()(_ArgTypes... __args);
2200 void make_ready_at_thread_exit(_ArgTypes... __args);
2205 template<class ..._ArgTypes>
2207 packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2209 if (__p_.__state_ == nullptr)
2210 __throw_future_error(future_errc::no_state);
2211 if (__p_.__state_->__has_value())
2212 __throw_future_error(future_errc::promise_already_satisfied);
2213 #ifndef _LIBCPP_NO_EXCEPTIONS
2216 #endif // _LIBCPP_NO_EXCEPTIONS
2217 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2219 #ifndef _LIBCPP_NO_EXCEPTIONS
2223 __p_.set_exception(current_exception());
2225 #endif // _LIBCPP_NO_EXCEPTIONS
2228 template<class ..._ArgTypes>
2230 packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2232 if (__p_.__state_ == nullptr)
2233 __throw_future_error(future_errc::no_state);
2234 if (__p_.__state_->__has_value())
2235 __throw_future_error(future_errc::promise_already_satisfied);
2236 #ifndef _LIBCPP_NO_EXCEPTIONS
2239 #endif // _LIBCPP_NO_EXCEPTIONS
2240 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2241 __p_.set_value_at_thread_exit();
2242 #ifndef _LIBCPP_NO_EXCEPTIONS
2246 __p_.set_exception_at_thread_exit(current_exception());
2248 #endif // _LIBCPP_NO_EXCEPTIONS
2251 template<class ..._ArgTypes>
2253 packaged_task<void(_ArgTypes...)>::reset()
2256 __throw_future_error(future_errc::no_state);
2257 __p_ = promise<result_type>();
2260 template <class _Callable>
2261 inline _LIBCPP_INLINE_VISIBILITY
2263 swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
2268 template <class _Callable, class _Alloc>
2269 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
2270 : public true_type {};
2272 template <class _Rp, class _Fp>
2274 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2275 __make_deferred_assoc_state(_Fp&& __f)
2277 __make_deferred_assoc_state(_Fp __f)
2280 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2281 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2282 return future<_Rp>(__h.get());
2285 template <class _Rp, class _Fp>
2287 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2288 __make_async_assoc_state(_Fp&& __f)
2290 __make_async_assoc_state(_Fp __f)
2293 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2294 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2295 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2296 return future<_Rp>(__h.get());
2299 template <class _Fp, class... _Args>
2302 tuple<_Fp, _Args...> __f_;
2305 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2307 _LIBCPP_INLINE_VISIBILITY
2308 explicit __async_func(_Fp&& __f, _Args&&... __args)
2309 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2311 _LIBCPP_INLINE_VISIBILITY
2312 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2316 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2317 return __execute(_Index());
2320 template <size_t ..._Indices>
2322 __execute(__tuple_indices<_Indices...>)
2324 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2328 inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2329 { return (int(__policy) & int(__value)) != 0; }
2331 template <class _Fp, class... _Args>
2332 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2333 async(launch __policy, _Fp&& __f, _Args&&... __args)
2335 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2336 typedef typename _BF::_Rp _Rp;
2338 #ifndef _LIBCPP_NO_EXCEPTIONS
2342 if (__does_policy_contain(__policy, launch::async))
2343 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2344 __decay_copy(_VSTD::forward<_Args>(__args))...));
2345 #ifndef _LIBCPP_NO_EXCEPTIONS
2347 catch ( ... ) { if (__policy == launch::async) throw ; }
2350 if (__does_policy_contain(__policy, launch::deferred))
2351 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2352 __decay_copy(_VSTD::forward<_Args>(__args))...));
2353 return future<_Rp>{};
2356 template <class _Fp, class... _Args>
2357 inline _LIBCPP_INLINE_VISIBILITY
2358 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2359 async(_Fp&& __f, _Args&&... __args)
2361 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2362 _VSTD::forward<_Args>(__args)...);
2365 #endif // _LIBCPP_HAS_NO_VARIADICS
2369 template <class _Rp>
2370 class _LIBCPP_TEMPLATE_VIS shared_future
2372 __assoc_state<_Rp>* __state_;
2375 _LIBCPP_INLINE_VISIBILITY
2376 shared_future() _NOEXCEPT : __state_(nullptr) {}
2377 _LIBCPP_INLINE_VISIBILITY
2378 shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2379 {if (__state_) __state_->__add_shared();}
2380 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2381 _LIBCPP_INLINE_VISIBILITY
2382 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
2383 {__f.__state_ = nullptr;}
2384 _LIBCPP_INLINE_VISIBILITY
2385 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2386 {__rhs.__state_ = nullptr;}
2387 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2389 shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
2390 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2391 _LIBCPP_INLINE_VISIBILITY
2392 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2394 shared_future(std::move(__rhs)).swap(*this);
2397 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2399 // retrieving the value
2400 _LIBCPP_INLINE_VISIBILITY
2401 const _Rp& get() const {return __state_->copy();}
2403 _LIBCPP_INLINE_VISIBILITY
2404 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2406 // functions to check state
2407 _LIBCPP_INLINE_VISIBILITY
2408 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2410 _LIBCPP_INLINE_VISIBILITY
2411 void wait() const {__state_->wait();}
2412 template <class _Rep, class _Period>
2413 _LIBCPP_INLINE_VISIBILITY
2415 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2416 {return __state_->wait_for(__rel_time);}
2417 template <class _Clock, class _Duration>
2418 _LIBCPP_INLINE_VISIBILITY
2420 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2421 {return __state_->wait_until(__abs_time);}
2424 template <class _Rp>
2425 shared_future<_Rp>::~shared_future()
2428 __state_->__release_shared();
2431 template <class _Rp>
2433 shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
2436 __rhs.__state_->__add_shared();
2438 __state_->__release_shared();
2439 __state_ = __rhs.__state_;
2443 template <class _Rp>
2444 class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
2446 __assoc_state<_Rp&>* __state_;
2449 _LIBCPP_INLINE_VISIBILITY
2450 shared_future() _NOEXCEPT : __state_(nullptr) {}
2451 _LIBCPP_INLINE_VISIBILITY
2452 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2453 {if (__state_) __state_->__add_shared();}
2454 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2455 _LIBCPP_INLINE_VISIBILITY
2456 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
2457 {__f.__state_ = nullptr;}
2458 _LIBCPP_INLINE_VISIBILITY
2459 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2460 {__rhs.__state_ = nullptr;}
2461 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2463 shared_future& operator=(const shared_future& __rhs);
2464 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2465 _LIBCPP_INLINE_VISIBILITY
2466 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2468 shared_future(std::move(__rhs)).swap(*this);
2471 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2473 // retrieving the value
2474 _LIBCPP_INLINE_VISIBILITY
2475 _Rp& get() const {return __state_->copy();}
2477 _LIBCPP_INLINE_VISIBILITY
2478 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2480 // functions to check state
2481 _LIBCPP_INLINE_VISIBILITY
2482 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2484 _LIBCPP_INLINE_VISIBILITY
2485 void wait() const {__state_->wait();}
2486 template <class _Rep, class _Period>
2487 _LIBCPP_INLINE_VISIBILITY
2489 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2490 {return __state_->wait_for(__rel_time);}
2491 template <class _Clock, class _Duration>
2492 _LIBCPP_INLINE_VISIBILITY
2494 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2495 {return __state_->wait_until(__abs_time);}
2498 template <class _Rp>
2499 shared_future<_Rp&>::~shared_future()
2502 __state_->__release_shared();
2505 template <class _Rp>
2506 shared_future<_Rp&>&
2507 shared_future<_Rp&>::operator=(const shared_future& __rhs)
2510 __rhs.__state_->__add_shared();
2512 __state_->__release_shared();
2513 __state_ = __rhs.__state_;
2518 class _LIBCPP_TYPE_VIS shared_future<void>
2520 __assoc_sub_state* __state_;
2523 _LIBCPP_INLINE_VISIBILITY
2524 shared_future() _NOEXCEPT : __state_(nullptr) {}
2525 _LIBCPP_INLINE_VISIBILITY
2526 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2527 {if (__state_) __state_->__add_shared();}
2528 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2529 _LIBCPP_INLINE_VISIBILITY
2530 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
2531 {__f.__state_ = nullptr;}
2532 _LIBCPP_INLINE_VISIBILITY
2533 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2534 {__rhs.__state_ = nullptr;}
2535 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2537 shared_future& operator=(const shared_future& __rhs);
2538 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2539 _LIBCPP_INLINE_VISIBILITY
2540 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2542 shared_future(std::move(__rhs)).swap(*this);
2545 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2547 // retrieving the value
2548 _LIBCPP_INLINE_VISIBILITY
2549 void get() const {__state_->copy();}
2551 _LIBCPP_INLINE_VISIBILITY
2552 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2554 // functions to check state
2555 _LIBCPP_INLINE_VISIBILITY
2556 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2558 _LIBCPP_INLINE_VISIBILITY
2559 void wait() const {__state_->wait();}
2560 template <class _Rep, class _Period>
2561 _LIBCPP_INLINE_VISIBILITY
2563 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2564 {return __state_->wait_for(__rel_time);}
2565 template <class _Clock, class _Duration>
2566 _LIBCPP_INLINE_VISIBILITY
2568 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2569 {return __state_->wait_until(__abs_time);}
2572 template <class _Rp>
2573 inline _LIBCPP_INLINE_VISIBILITY
2575 swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2580 template <class _Rp>
2583 future<_Rp>::share()
2585 return shared_future<_Rp>(_VSTD::move(*this));
2588 template <class _Rp>
2591 future<_Rp&>::share()
2593 return shared_future<_Rp&>(_VSTD::move(*this));
2596 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2600 future<void>::share()
2602 return shared_future<void>(_VSTD::move(*this));
2605 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2607 _LIBCPP_END_NAMESPACE_STD
2609 #endif // !_LIBCPP_HAS_NO_THREADS
2611 #endif // _LIBCPP_FUTURE