2 //===--------------------------- future -----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_FUTURE
12 #define _LIBCPP_FUTURE
20 enum class future_errc
22 future_already_retrieved = 1,
23 promise_already_satisfied,
32 any = async | deferred
35 enum class future_status
42 template <> struct is_error_code_enum<future_errc> : public true_type { };
43 error_code make_error_code(future_errc e) noexcept;
44 error_condition make_error_condition(future_errc e) noexcept;
46 const error_category& future_category() noexcept;
52 future_error(error_code ec); // exposition only
53 explicit future_error(future_errc); // C++17
54 const error_code& code() const noexcept;
55 const char* what() const noexcept;
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
65 promise(promise&& rhs) noexcept;
66 promise(const promise& rhs) = delete;
70 promise& operator=(promise&& rhs) noexcept;
71 promise& operator=(const promise& rhs) = delete;
72 void swap(promise& other) noexcept;
74 // retrieving the result
75 future<R> get_future();
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
95 promise(promise&& rhs) noexcept;
96 promise(const promise& rhs) = delete;
100 promise& operator=(promise&& rhs) noexcept;
101 promise& operator=(const promise& rhs) = delete;
102 void swap(promise& other) noexcept;
104 // retrieving the result
105 future<R&> get_future();
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
123 promise(promise&& rhs) noexcept;
124 promise(const promise& rhs) = delete;
128 promise& operator=(promise&& rhs) noexcept;
129 promise& operator=(const promise& rhs) = delete;
130 void swap(promise& other) noexcept;
132 // retrieving the result
133 future<void> get_future();
135 // setting the result
137 void set_exception(exception_ptr p);
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
144 template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
146 template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
154 future(future&&) noexcept;
155 future(const future& rhs) = delete;
157 future& operator=(const future& rhs) = delete;
158 future& operator=(future&&) noexcept;
159 shared_future<R> share() noexcept;
161 // retrieving the value
164 // functions to check state
165 bool valid() const noexcept;
168 template <class Rep, class Period>
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
181 future(future&&) noexcept;
182 future(const future& rhs) = delete;
184 future& operator=(const future& rhs) = delete;
185 future& operator=(future&&) noexcept;
186 shared_future<R&> share() noexcept;
188 // retrieving the value
191 // functions to check state
192 bool valid() const noexcept;
195 template <class Rep, class Period>
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
208 future(future&&) noexcept;
209 future(const future& rhs) = delete;
211 future& operator=(const future& rhs) = delete;
212 future& operator=(future&&) noexcept;
213 shared_future<void> share() noexcept;
215 // retrieving the value
218 // functions to check state
219 bool valid() const noexcept;
222 template <class Rep, class Period>
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
234 shared_future() noexcept;
235 shared_future(const shared_future& rhs);
236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
239 shared_future& operator=(const shared_future& rhs);
240 shared_future& operator=(shared_future&& rhs) noexcept;
242 // retrieving the value
243 const R& get() const;
245 // functions to check state
246 bool valid() const noexcept;
249 template <class Rep, class Period>
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
258 class shared_future<R&>
261 shared_future() noexcept;
262 shared_future(const shared_future& rhs);
263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
266 shared_future& operator=(const shared_future& rhs);
267 shared_future& operator=(shared_future&& rhs) noexcept;
269 // retrieving the value
272 // functions to check state
273 bool valid() const noexcept;
276 template <class Rep, class Period>
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
285 class shared_future<void>
288 shared_future() noexcept;
289 shared_future(const shared_future& rhs);
290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
293 shared_future& operator=(const shared_future& rhs);
294 shared_future& operator=(shared_future&& rhs) noexcept;
296 // retrieving the value
299 // functions to check state
300 bool valid() const noexcept;
303 template <class Rep, class Period>
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
311 template <class F, class... Args>
312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
313 async(F&& f, Args&&... args);
315 template <class F, class... Args>
316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
317 async(launch policy, F&& f, Args&&... args);
319 template <class> class packaged_task; // undefined
321 template <class R, class... ArgTypes>
322 class packaged_task<R(ArgTypes...)>
325 typedef R result_type; // extension
327 // construction and destruction
328 packaged_task() noexcept;
330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
344 bool valid() const noexcept;
347 future<R> get_future();
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
359 template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
366 #include <system_error>
373 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
374 #pragma GCC system_header
377 #ifdef _LIBCPP_HAS_NO_THREADS
378 #error <future> is not supported on this single threaded system
379 #else // !_LIBCPP_HAS_NO_THREADS
381 _LIBCPP_BEGIN_NAMESPACE_STD
383 //enum class future_errc
384 _LIBCPP_DECLARE_STRONG_ENUM(future_errc)
386 future_already_retrieved = 1,
387 promise_already_satisfied,
391 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
394 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
396 #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
398 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
402 _LIBCPP_DECLARE_STRONG_ENUM(launch)
406 any = async | deferred
408 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
410 #ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
412 #ifdef _LIBCXX_UNDERLYING_TYPE
413 typedef underlying_type<launch>::type __launch_underlying_type;
415 typedef int __launch_underlying_type;
418 inline _LIBCPP_INLINE_VISIBILITY
421 operator&(launch __x, launch __y)
423 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
424 static_cast<__launch_underlying_type>(__y));
427 inline _LIBCPP_INLINE_VISIBILITY
430 operator|(launch __x, launch __y)
432 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
433 static_cast<__launch_underlying_type>(__y));
436 inline _LIBCPP_INLINE_VISIBILITY
439 operator^(launch __x, launch __y)
441 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
442 static_cast<__launch_underlying_type>(__y));
445 inline _LIBCPP_INLINE_VISIBILITY
448 operator~(launch __x)
450 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
453 inline _LIBCPP_INLINE_VISIBILITY
455 operator&=(launch& __x, launch __y)
457 __x = __x & __y; return __x;
460 inline _LIBCPP_INLINE_VISIBILITY
462 operator|=(launch& __x, launch __y)
464 __x = __x | __y; return __x;
467 inline _LIBCPP_INLINE_VISIBILITY
469 operator^=(launch& __x, launch __y)
471 __x = __x ^ __y; return __x;
474 #endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
476 //enum class future_status
477 _LIBCPP_DECLARE_STRONG_ENUM(future_status)
483 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
486 const error_category& future_category() _NOEXCEPT;
488 inline _LIBCPP_INLINE_VISIBILITY
490 make_error_code(future_errc __e) _NOEXCEPT
492 return error_code(static_cast<int>(__e), future_category());
495 inline _LIBCPP_INLINE_VISIBILITY
497 make_error_condition(future_errc __e) _NOEXCEPT
499 return error_condition(static_cast<int>(__e), future_category());
502 class _LIBCPP_EXCEPTION_ABI 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>
585 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
587 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
589 virtual void __execute();
592 template <class _Clock, class _Duration>
594 __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
596 unique_lock<mutex> __lk(__mut_);
597 if (__state_ & deferred)
598 return future_status::deferred;
599 while (!(__state_ & ready) && _Clock::now() < __abs_time)
600 __cv_.wait_until(__lk, __abs_time);
601 if (__state_ & ready)
602 return future_status::ready;
603 return future_status::timeout;
606 template <class _Rep, class _Period>
609 __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
611 return wait_until(chrono::steady_clock::now() + __rel_time);
616 : public __assoc_sub_state
618 typedef __assoc_sub_state base;
619 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
623 virtual void __on_zero_shared() _NOEXCEPT;
626 template <class _Arg>
627 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
628 void set_value(_Arg&& __arg);
630 void set_value(_Arg& __arg);
633 template <class _Arg>
634 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
635 void set_value_at_thread_exit(_Arg&& __arg);
637 void set_value_at_thread_exit(_Arg& __arg);
641 typename add_lvalue_reference<_Rp>::type copy();
646 __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
648 if (this->__state_ & base::__constructed)
649 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
654 template <class _Arg>
656 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
657 __assoc_state<_Rp>::set_value(_Arg&& __arg)
659 __assoc_state<_Rp>::set_value(_Arg& __arg)
662 unique_lock<mutex> __lk(this->__mut_);
663 if (this->__has_value())
664 __throw_future_error(future_errc::promise_already_satisfied);
665 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
666 this->__state_ |= base::__constructed | base::ready;
671 template <class _Arg>
673 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
674 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
676 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
679 unique_lock<mutex> __lk(this->__mut_);
680 if (this->__has_value())
681 __throw_future_error(future_errc::promise_already_satisfied);
682 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
683 this->__state_ |= base::__constructed;
684 __thread_local_data()->__make_ready_at_thread_exit(this);
689 __assoc_state<_Rp>::move()
691 unique_lock<mutex> __lk(this->__mut_);
692 this->__sub_wait(__lk);
693 if (this->__exception_ != nullptr)
694 rethrow_exception(this->__exception_);
695 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
699 typename add_lvalue_reference<_Rp>::type
700 __assoc_state<_Rp>::copy()
702 unique_lock<mutex> __lk(this->__mut_);
703 this->__sub_wait(__lk);
704 if (this->__exception_ != nullptr)
705 rethrow_exception(this->__exception_);
706 return *reinterpret_cast<_Rp*>(&__value_);
710 class __assoc_state<_Rp&>
711 : public __assoc_sub_state
713 typedef __assoc_sub_state base;
718 virtual void __on_zero_shared() _NOEXCEPT;
721 void set_value(_Rp& __arg);
722 void set_value_at_thread_exit(_Rp& __arg);
729 __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
736 __assoc_state<_Rp&>::set_value(_Rp& __arg)
738 unique_lock<mutex> __lk(this->__mut_);
739 if (this->__has_value())
740 __throw_future_error(future_errc::promise_already_satisfied);
741 __value_ = _VSTD::addressof(__arg);
742 this->__state_ |= base::__constructed | base::ready;
748 __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
750 unique_lock<mutex> __lk(this->__mut_);
751 if (this->__has_value())
752 __throw_future_error(future_errc::promise_already_satisfied);
753 __value_ = _VSTD::addressof(__arg);
754 this->__state_ |= base::__constructed;
755 __thread_local_data()->__make_ready_at_thread_exit(this);
760 __assoc_state<_Rp&>::copy()
762 unique_lock<mutex> __lk(this->__mut_);
763 this->__sub_wait(__lk);
764 if (this->__exception_ != nullptr)
765 rethrow_exception(this->__exception_);
769 template <class _Rp, class _Alloc>
770 class __assoc_state_alloc
771 : public __assoc_state<_Rp>
773 typedef __assoc_state<_Rp> base;
776 virtual void __on_zero_shared() _NOEXCEPT;
778 _LIBCPP_INLINE_VISIBILITY
779 explicit __assoc_state_alloc(const _Alloc& __a)
783 template <class _Rp, class _Alloc>
785 __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
787 if (this->__state_ & base::__constructed)
788 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
789 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
790 typedef allocator_traits<_Al> _ATraits;
791 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
793 this->~__assoc_state_alloc();
794 __a.deallocate(_PTraits::pointer_to(*this), 1);
797 template <class _Rp, class _Alloc>
798 class __assoc_state_alloc<_Rp&, _Alloc>
799 : public __assoc_state<_Rp&>
801 typedef __assoc_state<_Rp&> base;
804 virtual void __on_zero_shared() _NOEXCEPT;
806 _LIBCPP_INLINE_VISIBILITY
807 explicit __assoc_state_alloc(const _Alloc& __a)
811 template <class _Rp, class _Alloc>
813 __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
815 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
816 typedef allocator_traits<_Al> _ATraits;
817 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
819 this->~__assoc_state_alloc();
820 __a.deallocate(_PTraits::pointer_to(*this), 1);
823 template <class _Alloc>
824 class __assoc_sub_state_alloc
825 : public __assoc_sub_state
827 typedef __assoc_sub_state base;
830 virtual void __on_zero_shared() _NOEXCEPT;
832 _LIBCPP_INLINE_VISIBILITY
833 explicit __assoc_sub_state_alloc(const _Alloc& __a)
837 template <class _Alloc>
839 __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
841 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
842 typedef allocator_traits<_Al> _ATraits;
843 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
845 this->~__assoc_sub_state_alloc();
846 __a.deallocate(_PTraits::pointer_to(*this), 1);
849 template <class _Rp, class _Fp>
850 class __deferred_assoc_state
851 : public __assoc_state<_Rp>
853 typedef __assoc_state<_Rp> base;
858 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
859 _LIBCPP_INLINE_VISIBILITY
860 explicit __deferred_assoc_state(_Fp&& __f);
863 virtual void __execute();
866 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
868 template <class _Rp, class _Fp>
870 __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
871 : __func_(_VSTD::forward<_Fp>(__f))
873 this->__set_deferred();
876 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
878 template <class _Rp, class _Fp>
880 __deferred_assoc_state<_Rp, _Fp>::__execute()
882 #ifndef _LIBCPP_NO_EXCEPTIONS
885 #endif // _LIBCPP_NO_EXCEPTIONS
886 this->set_value(__func_());
887 #ifndef _LIBCPP_NO_EXCEPTIONS
891 this->set_exception(current_exception());
893 #endif // _LIBCPP_NO_EXCEPTIONS
897 class __deferred_assoc_state<void, _Fp>
898 : public __assoc_sub_state
900 typedef __assoc_sub_state base;
905 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
906 _LIBCPP_INLINE_VISIBILITY
907 explicit __deferred_assoc_state(_Fp&& __f);
910 virtual void __execute();
913 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
917 __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
918 : __func_(_VSTD::forward<_Fp>(__f))
920 this->__set_deferred();
923 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
927 __deferred_assoc_state<void, _Fp>::__execute()
929 #ifndef _LIBCPP_NO_EXCEPTIONS
932 #endif // _LIBCPP_NO_EXCEPTIONS
935 #ifndef _LIBCPP_NO_EXCEPTIONS
939 this->set_exception(current_exception());
941 #endif // _LIBCPP_NO_EXCEPTIONS
944 template <class _Rp, class _Fp>
945 class __async_assoc_state
946 : public __assoc_state<_Rp>
948 typedef __assoc_state<_Rp> base;
952 virtual void __on_zero_shared() _NOEXCEPT;
954 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
955 _LIBCPP_INLINE_VISIBILITY
956 explicit __async_assoc_state(_Fp&& __f);
959 virtual void __execute();
962 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
964 template <class _Rp, class _Fp>
966 __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
967 : __func_(_VSTD::forward<_Fp>(__f))
971 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
973 template <class _Rp, class _Fp>
975 __async_assoc_state<_Rp, _Fp>::__execute()
977 #ifndef _LIBCPP_NO_EXCEPTIONS
980 #endif // _LIBCPP_NO_EXCEPTIONS
981 this->set_value(__func_());
982 #ifndef _LIBCPP_NO_EXCEPTIONS
986 this->set_exception(current_exception());
988 #endif // _LIBCPP_NO_EXCEPTIONS
991 template <class _Rp, class _Fp>
993 __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
996 base::__on_zero_shared();
1000 class __async_assoc_state<void, _Fp>
1001 : public __assoc_sub_state
1003 typedef __assoc_sub_state base;
1007 virtual void __on_zero_shared() _NOEXCEPT;
1009 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1010 _LIBCPP_INLINE_VISIBILITY
1011 explicit __async_assoc_state(_Fp&& __f);
1014 virtual void __execute();
1017 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1019 template <class _Fp>
1021 __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1022 : __func_(_VSTD::forward<_Fp>(__f))
1026 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1028 template <class _Fp>
1030 __async_assoc_state<void, _Fp>::__execute()
1032 #ifndef _LIBCPP_NO_EXCEPTIONS
1035 #endif // _LIBCPP_NO_EXCEPTIONS
1038 #ifndef _LIBCPP_NO_EXCEPTIONS
1042 this->set_exception(current_exception());
1044 #endif // _LIBCPP_NO_EXCEPTIONS
1047 template <class _Fp>
1049 __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1052 base::__on_zero_shared();
1055 template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1056 template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
1060 template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
1062 template <class _Rp, class _Fp>
1064 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1065 __make_deferred_assoc_state(_Fp&& __f);
1067 __make_deferred_assoc_state(_Fp __f);
1070 template <class _Rp, class _Fp>
1072 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1073 __make_async_assoc_state(_Fp&& __f);
1075 __make_async_assoc_state(_Fp __f);
1078 template <class _Rp>
1079 class _LIBCPP_TEMPLATE_VIS future
1081 __assoc_state<_Rp>* __state_;
1083 explicit future(__assoc_state<_Rp>* __state);
1085 template <class> friend class promise;
1086 template <class> friend class shared_future;
1088 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1089 template <class _R1, class _Fp>
1090 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1091 template <class _R1, class _Fp>
1092 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
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);
1101 _LIBCPP_INLINE_VISIBILITY
1102 future() _NOEXCEPT : __state_(nullptr) {}
1103 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1104 _LIBCPP_INLINE_VISIBILITY
1105 future(future&& __rhs) _NOEXCEPT
1106 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1107 future(const future&) = delete;
1108 future& operator=(const future&) = delete;
1109 _LIBCPP_INLINE_VISIBILITY
1110 future& operator=(future&& __rhs) _NOEXCEPT
1112 future(std::move(__rhs)).swap(*this);
1115 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1117 future(const future&);
1118 future& operator=(const future&);
1120 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1122 _LIBCPP_INLINE_VISIBILITY
1123 shared_future<_Rp> share() _NOEXCEPT;
1125 // retrieving the value
1128 _LIBCPP_INLINE_VISIBILITY
1129 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1131 // functions to check state
1132 _LIBCPP_INLINE_VISIBILITY
1133 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1135 _LIBCPP_INLINE_VISIBILITY
1136 void wait() const {__state_->wait();}
1137 template <class _Rep, class _Period>
1138 _LIBCPP_INLINE_VISIBILITY
1140 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1141 {return __state_->wait_for(__rel_time);}
1142 template <class _Clock, class _Duration>
1143 _LIBCPP_INLINE_VISIBILITY
1145 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1146 {return __state_->wait_until(__abs_time);}
1149 template <class _Rp>
1150 future<_Rp>::future(__assoc_state<_Rp>* __state)
1153 if (__state_->__has_future_attached())
1154 __throw_future_error(future_errc::future_already_retrieved);
1155 __state_->__add_shared();
1156 __state_->__set_future_attached();
1159 struct __release_shared_count
1161 void operator()(__shared_count* p) {p->__release_shared();}
1164 template <class _Rp>
1165 future<_Rp>::~future()
1168 __state_->__release_shared();
1171 template <class _Rp>
1175 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1176 __assoc_state<_Rp>* __s = __state_;
1181 template <class _Rp>
1182 class _LIBCPP_TEMPLATE_VIS future<_Rp&>
1184 __assoc_state<_Rp&>* __state_;
1186 explicit future(__assoc_state<_Rp&>* __state);
1188 template <class> friend class promise;
1189 template <class> friend class shared_future;
1191 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1192 template <class _R1, class _Fp>
1193 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1194 template <class _R1, class _Fp>
1195 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1197 template <class _R1, class _Fp>
1198 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1199 template <class _R1, class _Fp>
1200 friend future<_R1> __make_async_assoc_state(_Fp __f);
1204 _LIBCPP_INLINE_VISIBILITY
1205 future() _NOEXCEPT : __state_(nullptr) {}
1206 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1207 _LIBCPP_INLINE_VISIBILITY
1208 future(future&& __rhs) _NOEXCEPT
1209 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1210 future(const future&) = delete;
1211 future& operator=(const future&) = delete;
1212 _LIBCPP_INLINE_VISIBILITY
1213 future& operator=(future&& __rhs) _NOEXCEPT
1215 future(std::move(__rhs)).swap(*this);
1218 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1220 future(const future&);
1221 future& operator=(const future&);
1223 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1225 _LIBCPP_INLINE_VISIBILITY
1226 shared_future<_Rp&> share() _NOEXCEPT;
1228 // retrieving the value
1231 _LIBCPP_INLINE_VISIBILITY
1232 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1234 // functions to check state
1235 _LIBCPP_INLINE_VISIBILITY
1236 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1238 _LIBCPP_INLINE_VISIBILITY
1239 void wait() const {__state_->wait();}
1240 template <class _Rep, class _Period>
1241 _LIBCPP_INLINE_VISIBILITY
1243 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1244 {return __state_->wait_for(__rel_time);}
1245 template <class _Clock, class _Duration>
1246 _LIBCPP_INLINE_VISIBILITY
1248 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1249 {return __state_->wait_until(__abs_time);}
1252 template <class _Rp>
1253 future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1256 if (__state_->__has_future_attached())
1257 __throw_future_error(future_errc::future_already_retrieved);
1258 __state_->__add_shared();
1259 __state_->__set_future_attached();
1262 template <class _Rp>
1263 future<_Rp&>::~future()
1266 __state_->__release_shared();
1269 template <class _Rp>
1273 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1274 __assoc_state<_Rp&>* __s = __state_;
1280 class _LIBCPP_TYPE_VIS future<void>
1282 __assoc_sub_state* __state_;
1284 explicit future(__assoc_sub_state* __state);
1286 template <class> friend class promise;
1287 template <class> friend class shared_future;
1289 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1290 template <class _R1, class _Fp>
1291 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1292 template <class _R1, class _Fp>
1293 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1295 template <class _R1, class _Fp>
1296 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1297 template <class _R1, class _Fp>
1298 friend future<_R1> __make_async_assoc_state(_Fp __f);
1302 _LIBCPP_INLINE_VISIBILITY
1303 future() _NOEXCEPT : __state_(nullptr) {}
1304 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1305 _LIBCPP_INLINE_VISIBILITY
1306 future(future&& __rhs) _NOEXCEPT
1307 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1308 future(const future&) = delete;
1309 future& operator=(const future&) = delete;
1310 _LIBCPP_INLINE_VISIBILITY
1311 future& operator=(future&& __rhs) _NOEXCEPT
1313 future(std::move(__rhs)).swap(*this);
1316 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1318 future(const future&);
1319 future& operator=(const future&);
1321 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1323 _LIBCPP_INLINE_VISIBILITY
1324 shared_future<void> share() _NOEXCEPT;
1326 // retrieving the value
1329 _LIBCPP_INLINE_VISIBILITY
1330 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1332 // functions to check state
1333 _LIBCPP_INLINE_VISIBILITY
1334 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1336 _LIBCPP_INLINE_VISIBILITY
1337 void wait() const {__state_->wait();}
1338 template <class _Rep, class _Period>
1339 _LIBCPP_INLINE_VISIBILITY
1341 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1342 {return __state_->wait_for(__rel_time);}
1343 template <class _Clock, class _Duration>
1344 _LIBCPP_INLINE_VISIBILITY
1346 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1347 {return __state_->wait_until(__abs_time);}
1350 template <class _Rp>
1351 inline _LIBCPP_INLINE_VISIBILITY
1353 swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1360 template <class _Callable> class packaged_task;
1362 template <class _Rp>
1363 class _LIBCPP_TEMPLATE_VIS promise
1365 __assoc_state<_Rp>* __state_;
1367 _LIBCPP_INLINE_VISIBILITY
1368 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1370 template <class> friend class packaged_task;
1373 template <class _Alloc>
1374 promise(allocator_arg_t, const _Alloc& __a);
1375 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1376 _LIBCPP_INLINE_VISIBILITY
1377 promise(promise&& __rhs) _NOEXCEPT
1378 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1379 promise(const promise& __rhs) = delete;
1380 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1382 promise(const promise& __rhs);
1384 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1388 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1389 _LIBCPP_INLINE_VISIBILITY
1390 promise& operator=(promise&& __rhs) _NOEXCEPT
1392 promise(std::move(__rhs)).swap(*this);
1395 promise& operator=(const promise& __rhs) = delete;
1396 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1398 promise& operator=(const promise& __rhs);
1400 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1401 _LIBCPP_INLINE_VISIBILITY
1402 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1404 // retrieving the result
1405 future<_Rp> get_future();
1407 // setting the result
1408 void set_value(const _Rp& __r);
1409 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1410 void set_value(_Rp&& __r);
1412 void set_exception(exception_ptr __p);
1414 // setting the result with deferred notification
1415 void set_value_at_thread_exit(const _Rp& __r);
1416 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1417 void set_value_at_thread_exit(_Rp&& __r);
1419 void set_exception_at_thread_exit(exception_ptr __p);
1422 template <class _Rp>
1423 promise<_Rp>::promise()
1424 : __state_(new __assoc_state<_Rp>)
1428 template <class _Rp>
1429 template <class _Alloc>
1430 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1432 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1433 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1434 typedef __allocator_destructor<_A2> _D2;
1436 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1437 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1438 __state_ = _VSTD::addressof(*__hold.release());
1441 template <class _Rp>
1442 promise<_Rp>::~promise()
1446 if (!__state_->__has_value() && __state_->use_count() > 1)
1447 __state_->set_exception(make_exception_ptr(
1448 future_error(make_error_code(future_errc::broken_promise))
1450 __state_->__release_shared();
1454 template <class _Rp>
1456 promise<_Rp>::get_future()
1458 if (__state_ == nullptr)
1459 __throw_future_error(future_errc::no_state);
1460 return future<_Rp>(__state_);
1463 template <class _Rp>
1465 promise<_Rp>::set_value(const _Rp& __r)
1467 if (__state_ == nullptr)
1468 __throw_future_error(future_errc::no_state);
1469 __state_->set_value(__r);
1472 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1474 template <class _Rp>
1476 promise<_Rp>::set_value(_Rp&& __r)
1478 if (__state_ == nullptr)
1479 __throw_future_error(future_errc::no_state);
1480 __state_->set_value(_VSTD::move(__r));
1483 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1485 template <class _Rp>
1487 promise<_Rp>::set_exception(exception_ptr __p)
1489 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
1490 if (__state_ == nullptr)
1491 __throw_future_error(future_errc::no_state);
1492 __state_->set_exception(__p);
1495 template <class _Rp>
1497 promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1499 if (__state_ == nullptr)
1500 __throw_future_error(future_errc::no_state);
1501 __state_->set_value_at_thread_exit(__r);
1504 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1506 template <class _Rp>
1508 promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1510 if (__state_ == nullptr)
1511 __throw_future_error(future_errc::no_state);
1512 __state_->set_value_at_thread_exit(_VSTD::move(__r));
1515 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1517 template <class _Rp>
1519 promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
1521 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1522 if (__state_ == nullptr)
1523 __throw_future_error(future_errc::no_state);
1524 __state_->set_exception_at_thread_exit(__p);
1529 template <class _Rp>
1530 class _LIBCPP_TEMPLATE_VIS promise<_Rp&>
1532 __assoc_state<_Rp&>* __state_;
1534 _LIBCPP_INLINE_VISIBILITY
1535 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1537 template <class> friend class packaged_task;
1541 template <class _Allocator>
1542 promise(allocator_arg_t, const _Allocator& __a);
1543 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1544 _LIBCPP_INLINE_VISIBILITY
1545 promise(promise&& __rhs) _NOEXCEPT
1546 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1547 promise(const promise& __rhs) = delete;
1548 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1550 promise(const promise& __rhs);
1552 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1556 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1557 _LIBCPP_INLINE_VISIBILITY
1558 promise& operator=(promise&& __rhs) _NOEXCEPT
1560 promise(std::move(__rhs)).swap(*this);
1563 promise& operator=(const promise& __rhs) = delete;
1564 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1566 promise& operator=(const promise& __rhs);
1568 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1569 _LIBCPP_INLINE_VISIBILITY
1570 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1572 // retrieving the result
1573 future<_Rp&> get_future();
1575 // setting the result
1576 void set_value(_Rp& __r);
1577 void set_exception(exception_ptr __p);
1579 // setting the result with deferred notification
1580 void set_value_at_thread_exit(_Rp&);
1581 void set_exception_at_thread_exit(exception_ptr __p);
1584 template <class _Rp>
1585 promise<_Rp&>::promise()
1586 : __state_(new __assoc_state<_Rp&>)
1590 template <class _Rp>
1591 template <class _Alloc>
1592 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1594 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1595 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1596 typedef __allocator_destructor<_A2> _D2;
1598 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1599 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1600 __state_ = _VSTD::addressof(*__hold.release());
1603 template <class _Rp>
1604 promise<_Rp&>::~promise()
1608 if (!__state_->__has_value() && __state_->use_count() > 1)
1609 __state_->set_exception(make_exception_ptr(
1610 future_error(make_error_code(future_errc::broken_promise))
1612 __state_->__release_shared();
1616 template <class _Rp>
1618 promise<_Rp&>::get_future()
1620 if (__state_ == nullptr)
1621 __throw_future_error(future_errc::no_state);
1622 return future<_Rp&>(__state_);
1625 template <class _Rp>
1627 promise<_Rp&>::set_value(_Rp& __r)
1629 if (__state_ == nullptr)
1630 __throw_future_error(future_errc::no_state);
1631 __state_->set_value(__r);
1634 template <class _Rp>
1636 promise<_Rp&>::set_exception(exception_ptr __p)
1638 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
1639 if (__state_ == nullptr)
1640 __throw_future_error(future_errc::no_state);
1641 __state_->set_exception(__p);
1644 template <class _Rp>
1646 promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1648 if (__state_ == nullptr)
1649 __throw_future_error(future_errc::no_state);
1650 __state_->set_value_at_thread_exit(__r);
1653 template <class _Rp>
1655 promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
1657 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1658 if (__state_ == nullptr)
1659 __throw_future_error(future_errc::no_state);
1660 __state_->set_exception_at_thread_exit(__p);
1666 class _LIBCPP_TYPE_VIS promise<void>
1668 __assoc_sub_state* __state_;
1670 _LIBCPP_INLINE_VISIBILITY
1671 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1673 template <class> friend class packaged_task;
1677 template <class _Allocator>
1678 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1679 promise(allocator_arg_t, const _Allocator& __a);
1680 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1681 _LIBCPP_INLINE_VISIBILITY
1682 promise(promise&& __rhs) _NOEXCEPT
1683 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1684 promise(const promise& __rhs) = delete;
1685 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1687 promise(const promise& __rhs);
1689 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1693 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1694 _LIBCPP_INLINE_VISIBILITY
1695 promise& operator=(promise&& __rhs) _NOEXCEPT
1697 promise(std::move(__rhs)).swap(*this);
1700 promise& operator=(const promise& __rhs) = delete;
1701 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1703 promise& operator=(const promise& __rhs);
1705 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1706 _LIBCPP_INLINE_VISIBILITY
1707 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1709 // retrieving the result
1710 future<void> get_future();
1712 // setting the result
1714 void set_exception(exception_ptr __p);
1716 // setting the result with deferred notification
1717 void set_value_at_thread_exit();
1718 void set_exception_at_thread_exit(exception_ptr __p);
1721 template <class _Alloc>
1722 promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1724 typedef __assoc_sub_state_alloc<_Alloc> _State;
1725 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1726 typedef __allocator_destructor<_A2> _D2;
1728 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1729 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1730 __state_ = _VSTD::addressof(*__hold.release());
1733 template <class _Rp>
1734 inline _LIBCPP_INLINE_VISIBILITY
1736 swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1741 template <class _Rp, class _Alloc>
1742 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
1743 : public true_type {};
1745 #ifndef _LIBCPP_HAS_NO_VARIADICS
1749 template<class _Fp> class __packaged_task_base;
1751 template<class _Rp, class ..._ArgTypes>
1752 class __packaged_task_base<_Rp(_ArgTypes...)>
1754 __packaged_task_base(const __packaged_task_base&);
1755 __packaged_task_base& operator=(const __packaged_task_base&);
1757 _LIBCPP_INLINE_VISIBILITY
1758 __packaged_task_base() {}
1759 _LIBCPP_INLINE_VISIBILITY
1760 virtual ~__packaged_task_base() {}
1761 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1762 virtual void destroy() = 0;
1763 virtual void destroy_deallocate() = 0;
1764 virtual _Rp operator()(_ArgTypes&& ...) = 0;
1767 template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1769 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1770 class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1771 : public __packaged_task_base<_Rp(_ArgTypes...)>
1773 __compressed_pair<_Fp, _Alloc> __f_;
1775 _LIBCPP_INLINE_VISIBILITY
1776 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
1777 _LIBCPP_INLINE_VISIBILITY
1778 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
1779 _LIBCPP_INLINE_VISIBILITY
1780 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
1782 _LIBCPP_INLINE_VISIBILITY
1783 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
1784 : __f_(_VSTD::move(__f), __a) {}
1785 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1786 virtual void destroy();
1787 virtual void destroy_deallocate();
1788 virtual _Rp operator()(_ArgTypes&& ... __args);
1791 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1793 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1794 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1796 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1799 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1801 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1803 __f_.~__compressed_pair<_Fp, _Alloc>();
1806 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1808 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
1810 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1811 typedef allocator_traits<_Ap> _ATraits;
1812 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1813 _Ap __a(__f_.second());
1814 __f_.~__compressed_pair<_Fp, _Alloc>();
1815 __a.deallocate(_PTraits::pointer_to(*this), 1);
1818 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1820 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1822 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1825 template <class _Callable> class __packaged_task_function;
1827 template<class _Rp, class ..._ArgTypes>
1828 class __packaged_task_function<_Rp(_ArgTypes...)>
1830 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1831 typename aligned_storage<3*sizeof(void*)>::type __buf_;
1835 typedef _Rp result_type;
1837 // construct/copy/destroy:
1838 _LIBCPP_INLINE_VISIBILITY
1839 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1841 __packaged_task_function(_Fp&& __f);
1842 template<class _Fp, class _Alloc>
1843 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1845 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1846 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1848 __packaged_task_function(const __packaged_task_function&) = delete;
1849 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1851 ~__packaged_task_function();
1853 void swap(__packaged_task_function&) _NOEXCEPT;
1855 _LIBCPP_INLINE_VISIBILITY
1856 _Rp operator()(_ArgTypes...) const;
1859 template<class _Rp, class ..._ArgTypes>
1860 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1862 if (__f.__f_ == nullptr)
1864 else if (__f.__f_ == (__base*)&__f.__buf_)
1866 __f_ = (__base*)&__buf_;
1867 __f.__f_->__move_to(__f_);
1876 template<class _Rp, class ..._ArgTypes>
1877 template <class _Fp>
1878 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
1881 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
1882 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1883 if (sizeof(_FF) <= sizeof(__buf_))
1885 __f_ = (__base*)&__buf_;
1886 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1890 typedef allocator<_FF> _Ap;
1892 typedef __allocator_destructor<_Ap> _Dp;
1893 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1894 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
1895 __f_ = __hold.release();
1899 template<class _Rp, class ..._ArgTypes>
1900 template <class _Fp, class _Alloc>
1901 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1902 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1905 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
1906 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1907 if (sizeof(_FF) <= sizeof(__buf_))
1909 __f_ = (__base*)&__buf_;
1910 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1914 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1916 typedef __allocator_destructor<_Ap> _Dp;
1917 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1918 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1919 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1920 __f_ = _VSTD::addressof(*__hold.release());
1924 template<class _Rp, class ..._ArgTypes>
1925 __packaged_task_function<_Rp(_ArgTypes...)>&
1926 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1928 if (__f_ == (__base*)&__buf_)
1931 __f_->destroy_deallocate();
1933 if (__f.__f_ == nullptr)
1935 else if (__f.__f_ == (__base*)&__f.__buf_)
1937 __f_ = (__base*)&__buf_;
1938 __f.__f_->__move_to(__f_);
1948 template<class _Rp, class ..._ArgTypes>
1949 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1951 if (__f_ == (__base*)&__buf_)
1954 __f_->destroy_deallocate();
1957 template<class _Rp, class ..._ArgTypes>
1959 __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1961 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1963 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1964 __base* __t = (__base*)&__tempbuf;
1965 __f_->__move_to(__t);
1968 __f.__f_->__move_to((__base*)&__buf_);
1969 __f.__f_->destroy();
1971 __f_ = (__base*)&__buf_;
1972 __t->__move_to((__base*)&__f.__buf_);
1974 __f.__f_ = (__base*)&__f.__buf_;
1976 else if (__f_ == (__base*)&__buf_)
1978 __f_->__move_to((__base*)&__f.__buf_);
1981 __f.__f_ = (__base*)&__f.__buf_;
1983 else if (__f.__f_ == (__base*)&__f.__buf_)
1985 __f.__f_->__move_to((__base*)&__buf_);
1986 __f.__f_->destroy();
1988 __f_ = (__base*)&__buf_;
1991 _VSTD::swap(__f_, __f.__f_);
1994 template<class _Rp, class ..._ArgTypes>
1997 __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1999 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
2002 template<class _Rp, class ..._ArgTypes>
2003 class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)>
2006 typedef _Rp result_type; // extension
2009 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2010 promise<result_type> __p_;
2013 // construction and destruction
2014 _LIBCPP_INLINE_VISIBILITY
2015 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2016 template <class _Fp,
2017 class = typename enable_if
2020 typename decay<_Fp>::type,
2025 _LIBCPP_INLINE_VISIBILITY
2026 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2027 template <class _Fp, class _Allocator,
2028 class = typename enable_if
2031 typename decay<_Fp>::type,
2036 _LIBCPP_INLINE_VISIBILITY
2037 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2038 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2039 __p_(allocator_arg, __a) {}
2040 // ~packaged_task() = default;
2043 packaged_task(const packaged_task&) = delete;
2044 packaged_task& operator=(const packaged_task&) = delete;
2047 _LIBCPP_INLINE_VISIBILITY
2048 packaged_task(packaged_task&& __other) _NOEXCEPT
2049 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2050 _LIBCPP_INLINE_VISIBILITY
2051 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2053 __f_ = _VSTD::move(__other.__f_);
2054 __p_ = _VSTD::move(__other.__p_);
2057 _LIBCPP_INLINE_VISIBILITY
2058 void swap(packaged_task& __other) _NOEXCEPT
2060 __f_.swap(__other.__f_);
2061 __p_.swap(__other.__p_);
2064 _LIBCPP_INLINE_VISIBILITY
2065 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2068 _LIBCPP_INLINE_VISIBILITY
2069 future<result_type> get_future() {return __p_.get_future();}
2072 void operator()(_ArgTypes... __args);
2073 void make_ready_at_thread_exit(_ArgTypes... __args);
2078 template<class _Rp, class ..._ArgTypes>
2080 packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
2082 if (__p_.__state_ == nullptr)
2083 __throw_future_error(future_errc::no_state);
2084 if (__p_.__state_->__has_value())
2085 __throw_future_error(future_errc::promise_already_satisfied);
2086 #ifndef _LIBCPP_NO_EXCEPTIONS
2089 #endif // _LIBCPP_NO_EXCEPTIONS
2090 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2091 #ifndef _LIBCPP_NO_EXCEPTIONS
2095 __p_.set_exception(current_exception());
2097 #endif // _LIBCPP_NO_EXCEPTIONS
2100 template<class _Rp, class ..._ArgTypes>
2102 packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2104 if (__p_.__state_ == nullptr)
2105 __throw_future_error(future_errc::no_state);
2106 if (__p_.__state_->__has_value())
2107 __throw_future_error(future_errc::promise_already_satisfied);
2108 #ifndef _LIBCPP_NO_EXCEPTIONS
2111 #endif // _LIBCPP_NO_EXCEPTIONS
2112 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2113 #ifndef _LIBCPP_NO_EXCEPTIONS
2117 __p_.set_exception_at_thread_exit(current_exception());
2119 #endif // _LIBCPP_NO_EXCEPTIONS
2122 template<class _Rp, class ..._ArgTypes>
2124 packaged_task<_Rp(_ArgTypes...)>::reset()
2127 __throw_future_error(future_errc::no_state);
2128 __p_ = promise<result_type>();
2131 template<class ..._ArgTypes>
2132 class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)>
2135 typedef void result_type; // extension
2138 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2139 promise<result_type> __p_;
2142 // construction and destruction
2143 _LIBCPP_INLINE_VISIBILITY
2144 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2145 template <class _Fp,
2146 class = typename enable_if
2149 typename decay<_Fp>::type,
2154 _LIBCPP_INLINE_VISIBILITY
2155 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2156 template <class _Fp, class _Allocator,
2157 class = typename enable_if
2160 typename decay<_Fp>::type,
2165 _LIBCPP_INLINE_VISIBILITY
2166 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2167 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2168 __p_(allocator_arg, __a) {}
2169 // ~packaged_task() = default;
2172 packaged_task(const packaged_task&) = delete;
2173 packaged_task& operator=(const packaged_task&) = delete;
2176 _LIBCPP_INLINE_VISIBILITY
2177 packaged_task(packaged_task&& __other) _NOEXCEPT
2178 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2179 _LIBCPP_INLINE_VISIBILITY
2180 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2182 __f_ = _VSTD::move(__other.__f_);
2183 __p_ = _VSTD::move(__other.__p_);
2186 _LIBCPP_INLINE_VISIBILITY
2187 void swap(packaged_task& __other) _NOEXCEPT
2189 __f_.swap(__other.__f_);
2190 __p_.swap(__other.__p_);
2193 _LIBCPP_INLINE_VISIBILITY
2194 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2197 _LIBCPP_INLINE_VISIBILITY
2198 future<result_type> get_future() {return __p_.get_future();}
2201 void operator()(_ArgTypes... __args);
2202 void make_ready_at_thread_exit(_ArgTypes... __args);
2207 template<class ..._ArgTypes>
2209 packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2211 if (__p_.__state_ == nullptr)
2212 __throw_future_error(future_errc::no_state);
2213 if (__p_.__state_->__has_value())
2214 __throw_future_error(future_errc::promise_already_satisfied);
2215 #ifndef _LIBCPP_NO_EXCEPTIONS
2218 #endif // _LIBCPP_NO_EXCEPTIONS
2219 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2221 #ifndef _LIBCPP_NO_EXCEPTIONS
2225 __p_.set_exception(current_exception());
2227 #endif // _LIBCPP_NO_EXCEPTIONS
2230 template<class ..._ArgTypes>
2232 packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2234 if (__p_.__state_ == nullptr)
2235 __throw_future_error(future_errc::no_state);
2236 if (__p_.__state_->__has_value())
2237 __throw_future_error(future_errc::promise_already_satisfied);
2238 #ifndef _LIBCPP_NO_EXCEPTIONS
2241 #endif // _LIBCPP_NO_EXCEPTIONS
2242 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2243 __p_.set_value_at_thread_exit();
2244 #ifndef _LIBCPP_NO_EXCEPTIONS
2248 __p_.set_exception_at_thread_exit(current_exception());
2250 #endif // _LIBCPP_NO_EXCEPTIONS
2253 template<class ..._ArgTypes>
2255 packaged_task<void(_ArgTypes...)>::reset()
2258 __throw_future_error(future_errc::no_state);
2259 __p_ = promise<result_type>();
2262 template <class _Callable>
2263 inline _LIBCPP_INLINE_VISIBILITY
2265 swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
2270 template <class _Callable, class _Alloc>
2271 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
2272 : public true_type {};
2274 template <class _Rp, class _Fp>
2276 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2277 __make_deferred_assoc_state(_Fp&& __f)
2279 __make_deferred_assoc_state(_Fp __f)
2282 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2283 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2284 return future<_Rp>(__h.get());
2287 template <class _Rp, class _Fp>
2289 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2290 __make_async_assoc_state(_Fp&& __f)
2292 __make_async_assoc_state(_Fp __f)
2295 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2296 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2297 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2298 return future<_Rp>(__h.get());
2301 template <class _Fp, class... _Args>
2304 tuple<_Fp, _Args...> __f_;
2307 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2309 _LIBCPP_INLINE_VISIBILITY
2310 explicit __async_func(_Fp&& __f, _Args&&... __args)
2311 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2313 _LIBCPP_INLINE_VISIBILITY
2314 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2318 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2319 return __execute(_Index());
2322 template <size_t ..._Indices>
2324 __execute(__tuple_indices<_Indices...>)
2326 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2330 inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2331 { return (int(__policy) & int(__value)) != 0; }
2333 template <class _Fp, class... _Args>
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 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 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