2 //===--------------------------- future -----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_FUTURE
12 #define _LIBCPP_FUTURE
20 enum class future_errc
22 future_already_retrieved = 1,
23 promise_already_satisfied,
32 any = async | deferred
35 enum class future_status
42 template <> struct is_error_code_enum<future_errc> : public true_type { };
43 error_code make_error_code(future_errc e) noexcept;
44 error_condition make_error_condition(future_errc e) noexcept;
46 const error_category& future_category() noexcept;
52 future_error(error_code ec); // exposition only
53 explicit future_error(future_errc); // C++17
54 const error_code& code() const noexcept;
55 const char* what() const noexcept;
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
65 promise(promise&& rhs) noexcept;
66 promise(const promise& rhs) = delete;
70 promise& operator=(promise&& rhs) noexcept;
71 promise& operator=(const promise& rhs) = delete;
72 void swap(promise& other) noexcept;
74 // retrieving the result
75 future<R> get_future();
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
95 promise(promise&& rhs) noexcept;
96 promise(const promise& rhs) = delete;
100 promise& operator=(promise&& rhs) noexcept;
101 promise& operator=(const promise& rhs) = delete;
102 void swap(promise& other) noexcept;
104 // retrieving the result
105 future<R&> get_future();
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
123 promise(promise&& rhs) noexcept;
124 promise(const promise& rhs) = delete;
128 promise& operator=(promise&& rhs) noexcept;
129 promise& operator=(const promise& rhs) = delete;
130 void swap(promise& other) noexcept;
132 // retrieving the result
133 future<void> get_future();
135 // setting the result
137 void set_exception(exception_ptr p);
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
144 template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
146 template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
154 future(future&&) noexcept;
155 future(const future& rhs) = delete;
157 future& operator=(const future& rhs) = delete;
158 future& operator=(future&&) noexcept;
159 shared_future<R> share() noexcept;
161 // retrieving the value
164 // functions to check state
165 bool valid() const noexcept;
168 template <class Rep, class Period>
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
181 future(future&&) noexcept;
182 future(const future& rhs) = delete;
184 future& operator=(const future& rhs) = delete;
185 future& operator=(future&&) noexcept;
186 shared_future<R&> share() noexcept;
188 // retrieving the value
191 // functions to check state
192 bool valid() const noexcept;
195 template <class Rep, class Period>
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
208 future(future&&) noexcept;
209 future(const future& rhs) = delete;
211 future& operator=(const future& rhs) = delete;
212 future& operator=(future&&) noexcept;
213 shared_future<void> share() noexcept;
215 // retrieving the value
218 // functions to check state
219 bool valid() const noexcept;
222 template <class Rep, class Period>
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
234 shared_future() noexcept;
235 shared_future(const shared_future& rhs);
236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
239 shared_future& operator=(const shared_future& rhs);
240 shared_future& operator=(shared_future&& rhs) noexcept;
242 // retrieving the value
243 const R& get() const;
245 // functions to check state
246 bool valid() const noexcept;
249 template <class Rep, class Period>
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
258 class shared_future<R&>
261 shared_future() noexcept;
262 shared_future(const shared_future& rhs);
263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
266 shared_future& operator=(const shared_future& rhs);
267 shared_future& operator=(shared_future&& rhs) noexcept;
269 // retrieving the value
272 // functions to check state
273 bool valid() const noexcept;
276 template <class Rep, class Period>
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
285 class shared_future<void>
288 shared_future() noexcept;
289 shared_future(const shared_future& rhs);
290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
293 shared_future& operator=(const shared_future& rhs);
294 shared_future& operator=(shared_future&& rhs) noexcept;
296 // retrieving the value
299 // functions to check state
300 bool valid() const noexcept;
303 template <class Rep, class Period>
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
311 template <class F, class... Args>
312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
313 async(F&& f, Args&&... args);
315 template <class F, class... Args>
316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
317 async(launch policy, F&& f, Args&&... args);
319 template <class> class packaged_task; // undefined
321 template <class R, class... ArgTypes>
322 class packaged_task<R(ArgTypes...)>
325 typedef R result_type; // extension
327 // construction and destruction
328 packaged_task() noexcept;
330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
344 bool valid() const noexcept;
347 future<R> get_future();
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
359 template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
366 #include <system_error>
373 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
374 #pragma GCC system_header
377 #ifdef _LIBCPP_HAS_NO_THREADS
378 #error <future> is not supported on this single threaded system
379 #else // !_LIBCPP_HAS_NO_THREADS
381 _LIBCPP_BEGIN_NAMESPACE_STD
383 //enum class future_errc
384 _LIBCPP_DECLARE_STRONG_ENUM(future_errc)
386 future_already_retrieved = 1,
387 promise_already_satisfied,
391 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
394 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
396 #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
398 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
402 _LIBCPP_DECLARE_STRONG_ENUM(launch)
406 any = async | deferred
408 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
410 #ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
412 #ifdef _LIBCPP_UNDERLYING_TYPE
413 typedef underlying_type<launch>::type __launch_underlying_type;
415 typedef int __launch_underlying_type;
418 inline _LIBCPP_INLINE_VISIBILITY
421 operator&(launch __x, launch __y)
423 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
424 static_cast<__launch_underlying_type>(__y));
427 inline _LIBCPP_INLINE_VISIBILITY
430 operator|(launch __x, launch __y)
432 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
433 static_cast<__launch_underlying_type>(__y));
436 inline _LIBCPP_INLINE_VISIBILITY
439 operator^(launch __x, launch __y)
441 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
442 static_cast<__launch_underlying_type>(__y));
445 inline _LIBCPP_INLINE_VISIBILITY
448 operator~(launch __x)
450 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
453 inline _LIBCPP_INLINE_VISIBILITY
455 operator&=(launch& __x, launch __y)
457 __x = __x & __y; return __x;
460 inline _LIBCPP_INLINE_VISIBILITY
462 operator|=(launch& __x, launch __y)
464 __x = __x | __y; return __x;
467 inline _LIBCPP_INLINE_VISIBILITY
469 operator^=(launch& __x, launch __y)
471 __x = __x ^ __y; return __x;
474 #endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
476 //enum class future_status
477 _LIBCPP_DECLARE_STRONG_ENUM(future_status)
483 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
486 const error_category& future_category() _NOEXCEPT;
488 inline _LIBCPP_INLINE_VISIBILITY
490 make_error_code(future_errc __e) _NOEXCEPT
492 return error_code(static_cast<int>(__e), future_category());
495 inline _LIBCPP_INLINE_VISIBILITY
497 make_error_condition(future_errc __e) _NOEXCEPT
499 return error_condition(static_cast<int>(__e), future_category());
502 class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
507 future_error(error_code __ec);
508 #if _LIBCPP_STD_VERS > 14
509 explicit future_error(future_errc _Ev) : logic_error(), __ec_(make_error_code(_Ev)) {}
511 _LIBCPP_INLINE_VISIBILITY
512 const error_code& code() const _NOEXCEPT {return __ec_;}
514 virtual ~future_error() _NOEXCEPT;
517 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
518 #ifndef _LIBCPP_NO_EXCEPTIONS
519 _LIBCPP_AVAILABILITY_FUTURE_ERROR
521 void __throw_future_error(future_errc _Ev)
523 #ifndef _LIBCPP_NO_EXCEPTIONS
524 throw future_error(make_error_code(_Ev));
531 class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
532 : public __shared_count
535 exception_ptr __exception_;
536 mutable mutex __mut_;
537 mutable condition_variable __cv_;
540 virtual void __on_zero_shared() _NOEXCEPT;
541 void __sub_wait(unique_lock<mutex>& __lk);
546 __future_attached = 2,
551 _LIBCPP_INLINE_VISIBILITY
552 __assoc_sub_state() : __state_(0) {}
554 _LIBCPP_INLINE_VISIBILITY
555 bool __has_value() const
556 {return (__state_ & __constructed) || (__exception_ != nullptr);}
558 _LIBCPP_INLINE_VISIBILITY
559 void __attach_future() {
560 lock_guard<mutex> __lk(__mut_);
561 bool __has_future_attached = (__state_ & __future_attached) != 0;
562 if (__has_future_attached)
563 __throw_future_error(future_errc::future_already_retrieved);
564 this->__add_shared();
565 __state_ |= __future_attached;
568 _LIBCPP_INLINE_VISIBILITY
569 void __set_deferred() {__state_ |= deferred;}
572 _LIBCPP_INLINE_VISIBILITY
573 bool __is_ready() const {return (__state_ & ready) != 0;}
576 void set_value_at_thread_exit();
578 void set_exception(exception_ptr __p);
579 void set_exception_at_thread_exit(exception_ptr __p);
584 template <class _Rep, class _Period>
586 _LIBCPP_INLINE_VISIBILITY
587 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
588 template <class _Clock, class _Duration>
589 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
591 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
593 virtual void __execute();
596 template <class _Clock, class _Duration>
598 __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
600 unique_lock<mutex> __lk(__mut_);
601 if (__state_ & deferred)
602 return future_status::deferred;
603 while (!(__state_ & ready) && _Clock::now() < __abs_time)
604 __cv_.wait_until(__lk, __abs_time);
605 if (__state_ & ready)
606 return future_status::ready;
607 return future_status::timeout;
610 template <class _Rep, class _Period>
613 __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
615 return wait_until(chrono::steady_clock::now() + __rel_time);
619 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state
620 : public __assoc_sub_state
622 typedef __assoc_sub_state base;
623 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
627 virtual void __on_zero_shared() _NOEXCEPT;
630 template <class _Arg>
631 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
632 void set_value(_Arg&& __arg);
634 void set_value(_Arg& __arg);
637 template <class _Arg>
638 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
639 void set_value_at_thread_exit(_Arg&& __arg);
641 void set_value_at_thread_exit(_Arg& __arg);
645 typename add_lvalue_reference<_Rp>::type copy();
650 __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
652 if (this->__state_ & base::__constructed)
653 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
658 template <class _Arg>
659 _LIBCPP_AVAILABILITY_FUTURE
661 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
662 __assoc_state<_Rp>::set_value(_Arg&& __arg)
664 __assoc_state<_Rp>::set_value(_Arg& __arg)
667 unique_lock<mutex> __lk(this->__mut_);
668 if (this->__has_value())
669 __throw_future_error(future_errc::promise_already_satisfied);
670 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
671 this->__state_ |= base::__constructed | base::ready;
676 template <class _Arg>
678 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
679 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
681 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
684 unique_lock<mutex> __lk(this->__mut_);
685 if (this->__has_value())
686 __throw_future_error(future_errc::promise_already_satisfied);
687 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
688 this->__state_ |= base::__constructed;
689 __thread_local_data()->__make_ready_at_thread_exit(this);
694 __assoc_state<_Rp>::move()
696 unique_lock<mutex> __lk(this->__mut_);
697 this->__sub_wait(__lk);
698 if (this->__exception_ != nullptr)
699 rethrow_exception(this->__exception_);
700 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
704 typename add_lvalue_reference<_Rp>::type
705 __assoc_state<_Rp>::copy()
707 unique_lock<mutex> __lk(this->__mut_);
708 this->__sub_wait(__lk);
709 if (this->__exception_ != nullptr)
710 rethrow_exception(this->__exception_);
711 return *reinterpret_cast<_Rp*>(&__value_);
715 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
716 : public __assoc_sub_state
718 typedef __assoc_sub_state base;
723 virtual void __on_zero_shared() _NOEXCEPT;
726 void set_value(_Rp& __arg);
727 void set_value_at_thread_exit(_Rp& __arg);
734 __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
741 __assoc_state<_Rp&>::set_value(_Rp& __arg)
743 unique_lock<mutex> __lk(this->__mut_);
744 if (this->__has_value())
745 __throw_future_error(future_errc::promise_already_satisfied);
746 __value_ = _VSTD::addressof(__arg);
747 this->__state_ |= base::__constructed | base::ready;
753 __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
755 unique_lock<mutex> __lk(this->__mut_);
756 if (this->__has_value())
757 __throw_future_error(future_errc::promise_already_satisfied);
758 __value_ = _VSTD::addressof(__arg);
759 this->__state_ |= base::__constructed;
760 __thread_local_data()->__make_ready_at_thread_exit(this);
765 __assoc_state<_Rp&>::copy()
767 unique_lock<mutex> __lk(this->__mut_);
768 this->__sub_wait(__lk);
769 if (this->__exception_ != nullptr)
770 rethrow_exception(this->__exception_);
774 template <class _Rp, class _Alloc>
775 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
776 : public __assoc_state<_Rp>
778 typedef __assoc_state<_Rp> base;
781 virtual void __on_zero_shared() _NOEXCEPT;
783 _LIBCPP_INLINE_VISIBILITY
784 explicit __assoc_state_alloc(const _Alloc& __a)
788 template <class _Rp, class _Alloc>
790 __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
792 if (this->__state_ & base::__constructed)
793 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
794 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
795 typedef allocator_traits<_Al> _ATraits;
796 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
798 this->~__assoc_state_alloc();
799 __a.deallocate(_PTraits::pointer_to(*this), 1);
802 template <class _Rp, class _Alloc>
803 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
804 : public __assoc_state<_Rp&>
806 typedef __assoc_state<_Rp&> base;
809 virtual void __on_zero_shared() _NOEXCEPT;
811 _LIBCPP_INLINE_VISIBILITY
812 explicit __assoc_state_alloc(const _Alloc& __a)
816 template <class _Rp, class _Alloc>
818 __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
820 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
821 typedef allocator_traits<_Al> _ATraits;
822 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
824 this->~__assoc_state_alloc();
825 __a.deallocate(_PTraits::pointer_to(*this), 1);
828 template <class _Alloc>
829 class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
830 : public __assoc_sub_state
832 typedef __assoc_sub_state base;
835 virtual void __on_zero_shared() _NOEXCEPT;
837 _LIBCPP_INLINE_VISIBILITY
838 explicit __assoc_sub_state_alloc(const _Alloc& __a)
842 template <class _Alloc>
844 __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
846 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
847 typedef allocator_traits<_Al> _ATraits;
848 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
850 this->~__assoc_sub_state_alloc();
851 __a.deallocate(_PTraits::pointer_to(*this), 1);
854 template <class _Rp, class _Fp>
855 class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
856 : public __assoc_state<_Rp>
858 typedef __assoc_state<_Rp> base;
863 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
864 _LIBCPP_INLINE_VISIBILITY
865 explicit __deferred_assoc_state(_Fp&& __f);
868 virtual void __execute();
871 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
873 template <class _Rp, class _Fp>
875 __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
876 : __func_(_VSTD::forward<_Fp>(__f))
878 this->__set_deferred();
881 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
883 template <class _Rp, class _Fp>
885 __deferred_assoc_state<_Rp, _Fp>::__execute()
887 #ifndef _LIBCPP_NO_EXCEPTIONS
890 #endif // _LIBCPP_NO_EXCEPTIONS
891 this->set_value(__func_());
892 #ifndef _LIBCPP_NO_EXCEPTIONS
896 this->set_exception(current_exception());
898 #endif // _LIBCPP_NO_EXCEPTIONS
902 class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
903 : public __assoc_sub_state
905 typedef __assoc_sub_state base;
910 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
911 _LIBCPP_INLINE_VISIBILITY
912 explicit __deferred_assoc_state(_Fp&& __f);
915 virtual void __execute();
918 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
922 __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
923 : __func_(_VSTD::forward<_Fp>(__f))
925 this->__set_deferred();
928 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
932 __deferred_assoc_state<void, _Fp>::__execute()
934 #ifndef _LIBCPP_NO_EXCEPTIONS
937 #endif // _LIBCPP_NO_EXCEPTIONS
940 #ifndef _LIBCPP_NO_EXCEPTIONS
944 this->set_exception(current_exception());
946 #endif // _LIBCPP_NO_EXCEPTIONS
949 template <class _Rp, class _Fp>
950 class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
951 : public __assoc_state<_Rp>
953 typedef __assoc_state<_Rp> base;
957 virtual void __on_zero_shared() _NOEXCEPT;
959 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
960 _LIBCPP_INLINE_VISIBILITY
961 explicit __async_assoc_state(_Fp&& __f);
964 virtual void __execute();
967 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
969 template <class _Rp, class _Fp>
971 __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
972 : __func_(_VSTD::forward<_Fp>(__f))
976 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
978 template <class _Rp, class _Fp>
980 __async_assoc_state<_Rp, _Fp>::__execute()
982 #ifndef _LIBCPP_NO_EXCEPTIONS
985 #endif // _LIBCPP_NO_EXCEPTIONS
986 this->set_value(__func_());
987 #ifndef _LIBCPP_NO_EXCEPTIONS
991 this->set_exception(current_exception());
993 #endif // _LIBCPP_NO_EXCEPTIONS
996 template <class _Rp, class _Fp>
998 __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
1001 base::__on_zero_shared();
1004 template <class _Fp>
1005 class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
1006 : public __assoc_sub_state
1008 typedef __assoc_sub_state base;
1012 virtual void __on_zero_shared() _NOEXCEPT;
1014 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1015 _LIBCPP_INLINE_VISIBILITY
1016 explicit __async_assoc_state(_Fp&& __f);
1019 virtual void __execute();
1022 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1024 template <class _Fp>
1026 __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1027 : __func_(_VSTD::forward<_Fp>(__f))
1031 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1033 template <class _Fp>
1035 __async_assoc_state<void, _Fp>::__execute()
1037 #ifndef _LIBCPP_NO_EXCEPTIONS
1040 #endif // _LIBCPP_NO_EXCEPTIONS
1043 #ifndef _LIBCPP_NO_EXCEPTIONS
1047 this->set_exception(current_exception());
1049 #endif // _LIBCPP_NO_EXCEPTIONS
1052 template <class _Fp>
1054 __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1057 base::__on_zero_shared();
1060 template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1061 template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
1065 template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
1067 template <class _Rp, class _Fp>
1069 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1070 __make_deferred_assoc_state(_Fp&& __f);
1072 __make_deferred_assoc_state(_Fp __f);
1075 template <class _Rp, class _Fp>
1077 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1078 __make_async_assoc_state(_Fp&& __f);
1080 __make_async_assoc_state(_Fp __f);
1083 template <class _Rp>
1084 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
1086 __assoc_state<_Rp>* __state_;
1088 explicit future(__assoc_state<_Rp>* __state);
1090 template <class> friend class promise;
1091 template <class> friend class shared_future;
1093 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1094 template <class _R1, class _Fp>
1095 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1096 template <class _R1, class _Fp>
1097 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1099 template <class _R1, class _Fp>
1100 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1101 template <class _R1, class _Fp>
1102 friend future<_R1> __make_async_assoc_state(_Fp __f);
1106 _LIBCPP_INLINE_VISIBILITY
1107 future() _NOEXCEPT : __state_(nullptr) {}
1108 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1109 _LIBCPP_INLINE_VISIBILITY
1110 future(future&& __rhs) _NOEXCEPT
1111 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1112 future(const future&) = delete;
1113 future& operator=(const future&) = delete;
1114 _LIBCPP_INLINE_VISIBILITY
1115 future& operator=(future&& __rhs) _NOEXCEPT
1117 future(std::move(__rhs)).swap(*this);
1120 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1122 future(const future&);
1123 future& operator=(const future&);
1125 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1127 _LIBCPP_INLINE_VISIBILITY
1128 shared_future<_Rp> share() _NOEXCEPT;
1130 // retrieving the value
1133 _LIBCPP_INLINE_VISIBILITY
1134 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1136 // functions to check state
1137 _LIBCPP_INLINE_VISIBILITY
1138 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1140 _LIBCPP_INLINE_VISIBILITY
1141 void wait() const {__state_->wait();}
1142 template <class _Rep, class _Period>
1143 _LIBCPP_INLINE_VISIBILITY
1145 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1146 {return __state_->wait_for(__rel_time);}
1147 template <class _Clock, class _Duration>
1148 _LIBCPP_INLINE_VISIBILITY
1150 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1151 {return __state_->wait_until(__abs_time);}
1154 template <class _Rp>
1155 future<_Rp>::future(__assoc_state<_Rp>* __state)
1158 __state_->__attach_future();
1161 struct __release_shared_count
1163 void operator()(__shared_count* p) {p->__release_shared();}
1166 template <class _Rp>
1167 future<_Rp>::~future()
1170 __state_->__release_shared();
1173 template <class _Rp>
1177 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1178 __assoc_state<_Rp>* __s = __state_;
1183 template <class _Rp>
1184 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
1186 __assoc_state<_Rp&>* __state_;
1188 explicit future(__assoc_state<_Rp&>* __state);
1190 template <class> friend class promise;
1191 template <class> friend class shared_future;
1193 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1194 template <class _R1, class _Fp>
1195 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1196 template <class _R1, class _Fp>
1197 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1199 template <class _R1, class _Fp>
1200 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1201 template <class _R1, class _Fp>
1202 friend future<_R1> __make_async_assoc_state(_Fp __f);
1206 _LIBCPP_INLINE_VISIBILITY
1207 future() _NOEXCEPT : __state_(nullptr) {}
1208 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1209 _LIBCPP_INLINE_VISIBILITY
1210 future(future&& __rhs) _NOEXCEPT
1211 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1212 future(const future&) = delete;
1213 future& operator=(const future&) = delete;
1214 _LIBCPP_INLINE_VISIBILITY
1215 future& operator=(future&& __rhs) _NOEXCEPT
1217 future(std::move(__rhs)).swap(*this);
1220 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1222 future(const future&);
1223 future& operator=(const future&);
1225 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1227 _LIBCPP_INLINE_VISIBILITY
1228 shared_future<_Rp&> share() _NOEXCEPT;
1230 // retrieving the value
1233 _LIBCPP_INLINE_VISIBILITY
1234 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1236 // functions to check state
1237 _LIBCPP_INLINE_VISIBILITY
1238 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1240 _LIBCPP_INLINE_VISIBILITY
1241 void wait() const {__state_->wait();}
1242 template <class _Rep, class _Period>
1243 _LIBCPP_INLINE_VISIBILITY
1245 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1246 {return __state_->wait_for(__rel_time);}
1247 template <class _Clock, class _Duration>
1248 _LIBCPP_INLINE_VISIBILITY
1250 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1251 {return __state_->wait_until(__abs_time);}
1254 template <class _Rp>
1255 future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1258 __state_->__attach_future();
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 _LIBCPP_AVAILABILITY_FUTURE 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() _NOEXCEPT;
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 _LIBCPP_AVAILABILITY_FUTURE 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 _LIBCPP_AVAILABILITY_FUTURE 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 _LIBCPP_AVAILABILITY_FUTURE 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 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1678 promise(allocator_arg_t, const _Allocator& __a);
1679 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1680 _LIBCPP_INLINE_VISIBILITY
1681 promise(promise&& __rhs) _NOEXCEPT
1682 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1683 promise(const promise& __rhs) = delete;
1684 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1686 promise(const promise& __rhs);
1688 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1692 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1693 _LIBCPP_INLINE_VISIBILITY
1694 promise& operator=(promise&& __rhs) _NOEXCEPT
1696 promise(std::move(__rhs)).swap(*this);
1699 promise& operator=(const promise& __rhs) = delete;
1700 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1702 promise& operator=(const promise& __rhs);
1704 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1705 _LIBCPP_INLINE_VISIBILITY
1706 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1708 // retrieving the result
1709 future<void> get_future();
1711 // setting the result
1713 void set_exception(exception_ptr __p);
1715 // setting the result with deferred notification
1716 void set_value_at_thread_exit();
1717 void set_exception_at_thread_exit(exception_ptr __p);
1720 template <class _Alloc>
1721 promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1723 typedef __assoc_sub_state_alloc<_Alloc> _State;
1724 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1725 typedef __allocator_destructor<_A2> _D2;
1727 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1728 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1729 __state_ = _VSTD::addressof(*__hold.release());
1732 template <class _Rp>
1733 inline _LIBCPP_INLINE_VISIBILITY
1735 swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1740 template <class _Rp, class _Alloc>
1741 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
1742 : public true_type {};
1744 #ifndef _LIBCPP_HAS_NO_VARIADICS
1748 template<class _Fp> class __packaged_task_base;
1750 template<class _Rp, class ..._ArgTypes>
1751 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
1753 __packaged_task_base(const __packaged_task_base&);
1754 __packaged_task_base& operator=(const __packaged_task_base&);
1756 _LIBCPP_INLINE_VISIBILITY
1757 __packaged_task_base() {}
1758 _LIBCPP_INLINE_VISIBILITY
1759 virtual ~__packaged_task_base() {}
1760 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1761 virtual void destroy() = 0;
1762 virtual void destroy_deallocate() = 0;
1763 virtual _Rp operator()(_ArgTypes&& ...) = 0;
1766 template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1768 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1769 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1770 : public __packaged_task_base<_Rp(_ArgTypes...)>
1772 __compressed_pair<_Fp, _Alloc> __f_;
1774 _LIBCPP_INLINE_VISIBILITY
1775 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
1776 _LIBCPP_INLINE_VISIBILITY
1777 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
1778 _LIBCPP_INLINE_VISIBILITY
1779 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
1781 _LIBCPP_INLINE_VISIBILITY
1782 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
1783 : __f_(_VSTD::move(__f), __a) {}
1784 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1785 virtual void destroy();
1786 virtual void destroy_deallocate();
1787 virtual _Rp operator()(_ArgTypes&& ... __args);
1790 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1792 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1793 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1795 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1798 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1800 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1802 __f_.~__compressed_pair<_Fp, _Alloc>();
1805 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1807 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
1809 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1810 typedef allocator_traits<_Ap> _ATraits;
1811 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1812 _Ap __a(__f_.second());
1813 __f_.~__compressed_pair<_Fp, _Alloc>();
1814 __a.deallocate(_PTraits::pointer_to(*this), 1);
1817 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1819 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1821 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1824 template <class _Callable> class __packaged_task_function;
1826 template<class _Rp, class ..._ArgTypes>
1827 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
1829 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1830 typename aligned_storage<3*sizeof(void*)>::type __buf_;
1834 typedef _Rp result_type;
1836 // construct/copy/destroy:
1837 _LIBCPP_INLINE_VISIBILITY
1838 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1840 __packaged_task_function(_Fp&& __f);
1841 template<class _Fp, class _Alloc>
1842 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1844 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1845 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1847 __packaged_task_function(const __packaged_task_function&) = delete;
1848 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1850 ~__packaged_task_function();
1852 void swap(__packaged_task_function&) _NOEXCEPT;
1854 _LIBCPP_INLINE_VISIBILITY
1855 _Rp operator()(_ArgTypes...) const;
1858 template<class _Rp, class ..._ArgTypes>
1859 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1861 if (__f.__f_ == nullptr)
1863 else if (__f.__f_ == (__base*)&__f.__buf_)
1865 __f_ = (__base*)&__buf_;
1866 __f.__f_->__move_to(__f_);
1875 template<class _Rp, class ..._ArgTypes>
1876 template <class _Fp>
1877 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
1880 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
1881 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1882 if (sizeof(_FF) <= sizeof(__buf_))
1884 __f_ = (__base*)&__buf_;
1885 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1889 typedef allocator<_FF> _Ap;
1891 typedef __allocator_destructor<_Ap> _Dp;
1892 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1893 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
1894 __f_ = __hold.release();
1898 template<class _Rp, class ..._ArgTypes>
1899 template <class _Fp, class _Alloc>
1900 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1901 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1904 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
1905 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1906 if (sizeof(_FF) <= sizeof(__buf_))
1908 __f_ = (__base*)&__buf_;
1909 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1913 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1915 typedef __allocator_destructor<_Ap> _Dp;
1916 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1917 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1918 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1919 __f_ = _VSTD::addressof(*__hold.release());
1923 template<class _Rp, class ..._ArgTypes>
1924 __packaged_task_function<_Rp(_ArgTypes...)>&
1925 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1927 if (__f_ == (__base*)&__buf_)
1930 __f_->destroy_deallocate();
1932 if (__f.__f_ == nullptr)
1934 else if (__f.__f_ == (__base*)&__f.__buf_)
1936 __f_ = (__base*)&__buf_;
1937 __f.__f_->__move_to(__f_);
1947 template<class _Rp, class ..._ArgTypes>
1948 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1950 if (__f_ == (__base*)&__buf_)
1953 __f_->destroy_deallocate();
1956 template<class _Rp, class ..._ArgTypes>
1958 __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1960 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1962 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1963 __base* __t = (__base*)&__tempbuf;
1964 __f_->__move_to(__t);
1967 __f.__f_->__move_to((__base*)&__buf_);
1968 __f.__f_->destroy();
1970 __f_ = (__base*)&__buf_;
1971 __t->__move_to((__base*)&__f.__buf_);
1973 __f.__f_ = (__base*)&__f.__buf_;
1975 else if (__f_ == (__base*)&__buf_)
1977 __f_->__move_to((__base*)&__f.__buf_);
1980 __f.__f_ = (__base*)&__f.__buf_;
1982 else if (__f.__f_ == (__base*)&__f.__buf_)
1984 __f.__f_->__move_to((__base*)&__buf_);
1985 __f.__f_->destroy();
1987 __f_ = (__base*)&__buf_;
1990 _VSTD::swap(__f_, __f.__f_);
1993 template<class _Rp, class ..._ArgTypes>
1996 __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1998 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
2001 template<class _Rp, class ..._ArgTypes>
2002 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
2005 typedef _Rp result_type; // extension
2008 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2009 promise<result_type> __p_;
2012 // construction and destruction
2013 _LIBCPP_INLINE_VISIBILITY
2014 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2015 template <class _Fp,
2016 class = typename enable_if
2019 typename __uncvref<_Fp>::type,
2024 _LIBCPP_INLINE_VISIBILITY
2025 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2026 template <class _Fp, class _Allocator,
2027 class = typename enable_if
2030 typename __uncvref<_Fp>::type,
2035 _LIBCPP_INLINE_VISIBILITY
2036 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2037 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2038 __p_(allocator_arg, __a) {}
2039 // ~packaged_task() = default;
2042 packaged_task(const packaged_task&) = delete;
2043 packaged_task& operator=(const packaged_task&) = delete;
2046 _LIBCPP_INLINE_VISIBILITY
2047 packaged_task(packaged_task&& __other) _NOEXCEPT
2048 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2049 _LIBCPP_INLINE_VISIBILITY
2050 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2052 __f_ = _VSTD::move(__other.__f_);
2053 __p_ = _VSTD::move(__other.__p_);
2056 _LIBCPP_INLINE_VISIBILITY
2057 void swap(packaged_task& __other) _NOEXCEPT
2059 __f_.swap(__other.__f_);
2060 __p_.swap(__other.__p_);
2063 _LIBCPP_INLINE_VISIBILITY
2064 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2067 _LIBCPP_INLINE_VISIBILITY
2068 future<result_type> get_future() {return __p_.get_future();}
2071 void operator()(_ArgTypes... __args);
2072 void make_ready_at_thread_exit(_ArgTypes... __args);
2077 template<class _Rp, class ..._ArgTypes>
2079 packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
2081 if (__p_.__state_ == nullptr)
2082 __throw_future_error(future_errc::no_state);
2083 if (__p_.__state_->__has_value())
2084 __throw_future_error(future_errc::promise_already_satisfied);
2085 #ifndef _LIBCPP_NO_EXCEPTIONS
2088 #endif // _LIBCPP_NO_EXCEPTIONS
2089 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2090 #ifndef _LIBCPP_NO_EXCEPTIONS
2094 __p_.set_exception(current_exception());
2096 #endif // _LIBCPP_NO_EXCEPTIONS
2099 template<class _Rp, class ..._ArgTypes>
2101 packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2103 if (__p_.__state_ == nullptr)
2104 __throw_future_error(future_errc::no_state);
2105 if (__p_.__state_->__has_value())
2106 __throw_future_error(future_errc::promise_already_satisfied);
2107 #ifndef _LIBCPP_NO_EXCEPTIONS
2110 #endif // _LIBCPP_NO_EXCEPTIONS
2111 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2112 #ifndef _LIBCPP_NO_EXCEPTIONS
2116 __p_.set_exception_at_thread_exit(current_exception());
2118 #endif // _LIBCPP_NO_EXCEPTIONS
2121 template<class _Rp, class ..._ArgTypes>
2123 packaged_task<_Rp(_ArgTypes...)>::reset()
2126 __throw_future_error(future_errc::no_state);
2127 __p_ = promise<result_type>();
2130 template<class ..._ArgTypes>
2131 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
2134 typedef void result_type; // extension
2137 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2138 promise<result_type> __p_;
2141 // construction and destruction
2142 _LIBCPP_INLINE_VISIBILITY
2143 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2144 template <class _Fp,
2145 class = typename enable_if
2148 typename __uncvref<_Fp>::type,
2153 _LIBCPP_INLINE_VISIBILITY
2154 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2155 template <class _Fp, class _Allocator,
2156 class = typename enable_if
2159 typename __uncvref<_Fp>::type,
2164 _LIBCPP_INLINE_VISIBILITY
2165 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2166 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2167 __p_(allocator_arg, __a) {}
2168 // ~packaged_task() = default;
2171 packaged_task(const packaged_task&) = delete;
2172 packaged_task& operator=(const packaged_task&) = delete;
2175 _LIBCPP_INLINE_VISIBILITY
2176 packaged_task(packaged_task&& __other) _NOEXCEPT
2177 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2178 _LIBCPP_INLINE_VISIBILITY
2179 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2181 __f_ = _VSTD::move(__other.__f_);
2182 __p_ = _VSTD::move(__other.__p_);
2185 _LIBCPP_INLINE_VISIBILITY
2186 void swap(packaged_task& __other) _NOEXCEPT
2188 __f_.swap(__other.__f_);
2189 __p_.swap(__other.__p_);
2192 _LIBCPP_INLINE_VISIBILITY
2193 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2196 _LIBCPP_INLINE_VISIBILITY
2197 future<result_type> get_future() {return __p_.get_future();}
2200 void operator()(_ArgTypes... __args);
2201 void make_ready_at_thread_exit(_ArgTypes... __args);
2206 template<class ..._ArgTypes>
2208 packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2210 if (__p_.__state_ == nullptr)
2211 __throw_future_error(future_errc::no_state);
2212 if (__p_.__state_->__has_value())
2213 __throw_future_error(future_errc::promise_already_satisfied);
2214 #ifndef _LIBCPP_NO_EXCEPTIONS
2217 #endif // _LIBCPP_NO_EXCEPTIONS
2218 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2220 #ifndef _LIBCPP_NO_EXCEPTIONS
2224 __p_.set_exception(current_exception());
2226 #endif // _LIBCPP_NO_EXCEPTIONS
2229 template<class ..._ArgTypes>
2231 packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2233 if (__p_.__state_ == nullptr)
2234 __throw_future_error(future_errc::no_state);
2235 if (__p_.__state_->__has_value())
2236 __throw_future_error(future_errc::promise_already_satisfied);
2237 #ifndef _LIBCPP_NO_EXCEPTIONS
2240 #endif // _LIBCPP_NO_EXCEPTIONS
2241 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2242 __p_.set_value_at_thread_exit();
2243 #ifndef _LIBCPP_NO_EXCEPTIONS
2247 __p_.set_exception_at_thread_exit(current_exception());
2249 #endif // _LIBCPP_NO_EXCEPTIONS
2252 template<class ..._ArgTypes>
2254 packaged_task<void(_ArgTypes...)>::reset()
2257 __throw_future_error(future_errc::no_state);
2258 __p_ = promise<result_type>();
2261 template <class _Callable>
2262 inline _LIBCPP_INLINE_VISIBILITY
2264 swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
2269 template <class _Callable, class _Alloc>
2270 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
2271 : public true_type {};
2273 template <class _Rp, class _Fp>
2275 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2276 __make_deferred_assoc_state(_Fp&& __f)
2278 __make_deferred_assoc_state(_Fp __f)
2281 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2282 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2283 return future<_Rp>(__h.get());
2286 template <class _Rp, class _Fp>
2288 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2289 __make_async_assoc_state(_Fp&& __f)
2291 __make_async_assoc_state(_Fp __f)
2294 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2295 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2296 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2297 return future<_Rp>(__h.get());
2300 template <class _Fp, class... _Args>
2303 tuple<_Fp, _Args...> __f_;
2306 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2308 _LIBCPP_INLINE_VISIBILITY
2309 explicit __async_func(_Fp&& __f, _Args&&... __args)
2310 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2312 _LIBCPP_INLINE_VISIBILITY
2313 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2317 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2318 return __execute(_Index());
2321 template <size_t ..._Indices>
2323 __execute(__tuple_indices<_Indices...>)
2325 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2329 inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2330 { return (int(__policy) & int(__value)) != 0; }
2332 template <class _Fp, class... _Args>
2333 _LIBCPP_NODISCARD_AFTER_CXX17
2334 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2335 async(launch __policy, _Fp&& __f, _Args&&... __args)
2337 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2338 typedef typename _BF::_Rp _Rp;
2340 #ifndef _LIBCPP_NO_EXCEPTIONS
2344 if (__does_policy_contain(__policy, launch::async))
2345 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2346 __decay_copy(_VSTD::forward<_Args>(__args))...));
2347 #ifndef _LIBCPP_NO_EXCEPTIONS
2349 catch ( ... ) { if (__policy == launch::async) throw ; }
2352 if (__does_policy_contain(__policy, launch::deferred))
2353 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2354 __decay_copy(_VSTD::forward<_Args>(__args))...));
2355 return future<_Rp>{};
2358 template <class _Fp, class... _Args>
2359 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
2360 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2361 async(_Fp&& __f, _Args&&... __args)
2363 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2364 _VSTD::forward<_Args>(__args)...);
2367 #endif // _LIBCPP_HAS_NO_VARIADICS
2371 template <class _Rp>
2372 class _LIBCPP_TEMPLATE_VIS shared_future
2374 __assoc_state<_Rp>* __state_;
2377 _LIBCPP_INLINE_VISIBILITY
2378 shared_future() _NOEXCEPT : __state_(nullptr) {}
2379 _LIBCPP_INLINE_VISIBILITY
2380 shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2381 {if (__state_) __state_->__add_shared();}
2382 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2383 _LIBCPP_INLINE_VISIBILITY
2384 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
2385 {__f.__state_ = nullptr;}
2386 _LIBCPP_INLINE_VISIBILITY
2387 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2388 {__rhs.__state_ = nullptr;}
2389 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2391 shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
2392 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2393 _LIBCPP_INLINE_VISIBILITY
2394 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2396 shared_future(std::move(__rhs)).swap(*this);
2399 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2401 // retrieving the value
2402 _LIBCPP_INLINE_VISIBILITY
2403 const _Rp& get() const {return __state_->copy();}
2405 _LIBCPP_INLINE_VISIBILITY
2406 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2408 // functions to check state
2409 _LIBCPP_INLINE_VISIBILITY
2410 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2412 _LIBCPP_INLINE_VISIBILITY
2413 void wait() const {__state_->wait();}
2414 template <class _Rep, class _Period>
2415 _LIBCPP_INLINE_VISIBILITY
2417 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2418 {return __state_->wait_for(__rel_time);}
2419 template <class _Clock, class _Duration>
2420 _LIBCPP_INLINE_VISIBILITY
2422 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2423 {return __state_->wait_until(__abs_time);}
2426 template <class _Rp>
2427 shared_future<_Rp>::~shared_future()
2430 __state_->__release_shared();
2433 template <class _Rp>
2435 shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
2438 __rhs.__state_->__add_shared();
2440 __state_->__release_shared();
2441 __state_ = __rhs.__state_;
2445 template <class _Rp>
2446 class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
2448 __assoc_state<_Rp&>* __state_;
2451 _LIBCPP_INLINE_VISIBILITY
2452 shared_future() _NOEXCEPT : __state_(nullptr) {}
2453 _LIBCPP_INLINE_VISIBILITY
2454 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2455 {if (__state_) __state_->__add_shared();}
2456 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2457 _LIBCPP_INLINE_VISIBILITY
2458 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
2459 {__f.__state_ = nullptr;}
2460 _LIBCPP_INLINE_VISIBILITY
2461 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2462 {__rhs.__state_ = nullptr;}
2463 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2465 shared_future& operator=(const shared_future& __rhs);
2466 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2467 _LIBCPP_INLINE_VISIBILITY
2468 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2470 shared_future(std::move(__rhs)).swap(*this);
2473 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2475 // retrieving the value
2476 _LIBCPP_INLINE_VISIBILITY
2477 _Rp& get() const {return __state_->copy();}
2479 _LIBCPP_INLINE_VISIBILITY
2480 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2482 // functions to check state
2483 _LIBCPP_INLINE_VISIBILITY
2484 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2486 _LIBCPP_INLINE_VISIBILITY
2487 void wait() const {__state_->wait();}
2488 template <class _Rep, class _Period>
2489 _LIBCPP_INLINE_VISIBILITY
2491 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2492 {return __state_->wait_for(__rel_time);}
2493 template <class _Clock, class _Duration>
2494 _LIBCPP_INLINE_VISIBILITY
2496 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2497 {return __state_->wait_until(__abs_time);}
2500 template <class _Rp>
2501 shared_future<_Rp&>::~shared_future()
2504 __state_->__release_shared();
2507 template <class _Rp>
2508 shared_future<_Rp&>&
2509 shared_future<_Rp&>::operator=(const shared_future& __rhs)
2512 __rhs.__state_->__add_shared();
2514 __state_->__release_shared();
2515 __state_ = __rhs.__state_;
2520 class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
2522 __assoc_sub_state* __state_;
2525 _LIBCPP_INLINE_VISIBILITY
2526 shared_future() _NOEXCEPT : __state_(nullptr) {}
2527 _LIBCPP_INLINE_VISIBILITY
2528 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2529 {if (__state_) __state_->__add_shared();}
2530 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2531 _LIBCPP_INLINE_VISIBILITY
2532 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
2533 {__f.__state_ = nullptr;}
2534 _LIBCPP_INLINE_VISIBILITY
2535 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2536 {__rhs.__state_ = nullptr;}
2537 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2539 shared_future& operator=(const shared_future& __rhs);
2540 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2541 _LIBCPP_INLINE_VISIBILITY
2542 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2544 shared_future(std::move(__rhs)).swap(*this);
2547 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2549 // retrieving the value
2550 _LIBCPP_INLINE_VISIBILITY
2551 void get() const {__state_->copy();}
2553 _LIBCPP_INLINE_VISIBILITY
2554 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2556 // functions to check state
2557 _LIBCPP_INLINE_VISIBILITY
2558 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2560 _LIBCPP_INLINE_VISIBILITY
2561 void wait() const {__state_->wait();}
2562 template <class _Rep, class _Period>
2563 _LIBCPP_INLINE_VISIBILITY
2565 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2566 {return __state_->wait_for(__rel_time);}
2567 template <class _Clock, class _Duration>
2568 _LIBCPP_INLINE_VISIBILITY
2570 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2571 {return __state_->wait_until(__abs_time);}
2574 template <class _Rp>
2575 inline _LIBCPP_INLINE_VISIBILITY
2577 swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2582 template <class _Rp>
2585 future<_Rp>::share() _NOEXCEPT
2587 return shared_future<_Rp>(_VSTD::move(*this));
2590 template <class _Rp>
2593 future<_Rp&>::share() _NOEXCEPT
2595 return shared_future<_Rp&>(_VSTD::move(*this));
2598 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2602 future<void>::share() _NOEXCEPT
2604 return shared_future<void>(_VSTD::move(*this));
2607 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2609 _LIBCPP_END_NAMESPACE_STD
2611 #endif // !_LIBCPP_HAS_NO_THREADS
2613 #endif // _LIBCPP_FUTURE