2 //===--------------------------- future -----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_FUTURE
12 #define _LIBCPP_FUTURE
20 enum class future_errc
22 future_already_retrieved = 1,
23 promise_already_satisfied,
32 any = async | deferred
35 enum class future_status
42 template <> struct is_error_code_enum<future_errc> : public true_type { };
43 error_code make_error_code(future_errc e) noexcept;
44 error_condition make_error_condition(future_errc e) noexcept;
46 const error_category& future_category() noexcept;
52 future_error(error_code ec); // exposition only
54 const error_code& code() const noexcept;
55 const char* what() const noexcept;
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
65 promise(promise&& rhs) noexcept;
66 promise(const promise& rhs) = delete;
70 promise& operator=(promise&& rhs) noexcept;
71 promise& operator=(const promise& rhs) = delete;
72 void swap(promise& other) noexcept;
74 // retrieving the result
75 future<R> get_future();
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
95 promise(promise&& rhs) noexcept;
96 promise(const promise& rhs) = delete;
100 promise& operator=(promise&& rhs) noexcept;
101 promise& operator=(const promise& rhs) = delete;
102 void swap(promise& other) noexcept;
104 // retrieving the result
105 future<R&> get_future();
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
123 promise(promise&& rhs) noexcept;
124 promise(const promise& rhs) = delete;
128 promise& operator=(promise&& rhs) noexcept;
129 promise& operator=(const promise& rhs) = delete;
130 void swap(promise& other) noexcept;
132 // retrieving the result
133 future<void> get_future();
135 // setting the result
137 void set_exception(exception_ptr p);
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
144 template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
146 template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
154 future(future&&) noexcept;
155 future(const future& rhs) = delete;
157 future& operator=(const future& rhs) = delete;
158 future& operator=(future&&) noexcept;
159 shared_future<R> share();
161 // retrieving the value
164 // functions to check state
165 bool valid() const noexcept;
168 template <class Rep, class Period>
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
181 future(future&&) noexcept;
182 future(const future& rhs) = delete;
184 future& operator=(const future& rhs) = delete;
185 future& operator=(future&&) noexcept;
186 shared_future<R&> share();
188 // retrieving the value
191 // functions to check state
192 bool valid() const noexcept;
195 template <class Rep, class Period>
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
208 future(future&&) noexcept;
209 future(const future& rhs) = delete;
211 future& operator=(const future& rhs) = delete;
212 future& operator=(future&&) noexcept;
213 shared_future<void> share();
215 // retrieving the value
218 // functions to check state
219 bool valid() const noexcept;
222 template <class Rep, class Period>
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
234 shared_future() noexcept;
235 shared_future(const shared_future& rhs);
236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
239 shared_future& operator=(const shared_future& rhs);
240 shared_future& operator=(shared_future&& rhs) noexcept;
242 // retrieving the value
243 const R& get() const;
245 // functions to check state
246 bool valid() const noexcept;
249 template <class Rep, class Period>
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
258 class shared_future<R&>
261 shared_future() noexcept;
262 shared_future(const shared_future& rhs);
263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
266 shared_future& operator=(const shared_future& rhs);
267 shared_future& operator=(shared_future&& rhs) noexcept;
269 // retrieving the value
272 // functions to check state
273 bool valid() const noexcept;
276 template <class Rep, class Period>
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
285 class shared_future<void>
288 shared_future() noexcept;
289 shared_future(const shared_future& rhs);
290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
293 shared_future& operator=(const shared_future& rhs);
294 shared_future& operator=(shared_future&& rhs) noexcept;
296 // retrieving the value
299 // functions to check state
300 bool valid() const noexcept;
303 template <class Rep, class Period>
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
311 template <class F, class... Args>
312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
313 async(F&& f, Args&&... args);
315 template <class F, class... Args>
316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
317 async(launch policy, F&& f, Args&&... args);
319 template <class> class packaged_task; // undefined
321 template <class R, class... ArgTypes>
322 class packaged_task<R(ArgTypes...)>
325 typedef R result_type;
327 // construction and destruction
328 packaged_task() noexcept;
330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
332 explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
344 bool valid() const noexcept;
347 future<R> get_future();
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
359 template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
366 #include <system_error>
373 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
374 #pragma GCC system_header
377 _LIBCPP_BEGIN_NAMESPACE_STD
379 //enum class future_errc
380 _LIBCPP_DECLARE_STRONG_ENUM(future_errc)
382 future_already_retrieved = 1,
383 promise_already_satisfied,
387 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
390 struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc> : public true_type {};
392 #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
394 struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc::__lx> : public true_type { };
398 _LIBCPP_DECLARE_STRONG_ENUM(launch)
402 any = async | deferred
404 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
406 #ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
408 #ifdef _LIBCXX_UNDERLYING_TYPE
409 typedef underlying_type<launch>::type __launch_underlying_type;
411 typedef int __launch_underlying_type;
414 inline _LIBCPP_INLINE_VISIBILITY
417 operator&(launch __x, launch __y)
419 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
420 static_cast<__launch_underlying_type>(__y));
423 inline _LIBCPP_INLINE_VISIBILITY
426 operator|(launch __x, launch __y)
428 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
429 static_cast<__launch_underlying_type>(__y));
432 inline _LIBCPP_INLINE_VISIBILITY
435 operator^(launch __x, launch __y)
437 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
438 static_cast<__launch_underlying_type>(__y));
441 inline _LIBCPP_INLINE_VISIBILITY
444 operator~(launch __x)
446 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
449 inline _LIBCPP_INLINE_VISIBILITY
451 operator&=(launch& __x, launch __y)
453 __x = __x & __y; return __x;
456 inline _LIBCPP_INLINE_VISIBILITY
458 operator|=(launch& __x, launch __y)
460 __x = __x | __y; return __x;
463 inline _LIBCPP_INLINE_VISIBILITY
465 operator^=(launch& __x, launch __y)
467 __x = __x ^ __y; return __x;
470 #endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
472 //enum class future_status
473 _LIBCPP_DECLARE_STRONG_ENUM(future_status)
479 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
482 const error_category& future_category() _NOEXCEPT;
484 inline _LIBCPP_INLINE_VISIBILITY
486 make_error_code(future_errc __e) _NOEXCEPT
488 return error_code(static_cast<int>(__e), future_category());
491 inline _LIBCPP_INLINE_VISIBILITY
493 make_error_condition(future_errc __e) _NOEXCEPT
495 return error_condition(static_cast<int>(__e), future_category());
498 class _LIBCPP_EXCEPTION_ABI future_error
503 future_error(error_code __ec);
505 _LIBCPP_INLINE_VISIBILITY
506 const error_code& code() const _NOEXCEPT {return __ec_;}
508 virtual ~future_error() _NOEXCEPT;
511 class _LIBCPP_TYPE_VIS __assoc_sub_state
512 : public __shared_count
515 exception_ptr __exception_;
516 mutable mutex __mut_;
517 mutable condition_variable __cv_;
520 virtual void __on_zero_shared() _NOEXCEPT;
521 void __sub_wait(unique_lock<mutex>& __lk);
526 __future_attached = 2,
531 _LIBCPP_INLINE_VISIBILITY
532 __assoc_sub_state() : __state_(0) {}
534 _LIBCPP_INLINE_VISIBILITY
535 bool __has_value() const
536 {return (__state_ & __constructed) || (__exception_ != nullptr);}
538 _LIBCPP_INLINE_VISIBILITY
539 void __set_future_attached()
541 lock_guard<mutex> __lk(__mut_);
542 __state_ |= __future_attached;
544 _LIBCPP_INLINE_VISIBILITY
545 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
547 _LIBCPP_INLINE_VISIBILITY
548 void __set_deferred() {__state_ |= deferred;}
551 _LIBCPP_INLINE_VISIBILITY
552 bool __is_ready() const {return (__state_ & ready) != 0;}
555 void set_value_at_thread_exit();
557 void set_exception(exception_ptr __p);
558 void set_exception_at_thread_exit(exception_ptr __p);
563 template <class _Rep, class _Period>
565 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
566 template <class _Clock, class _Duration>
568 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
570 virtual void __execute();
573 template <class _Clock, class _Duration>
575 __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
577 unique_lock<mutex> __lk(__mut_);
578 if (__state_ & deferred)
579 return future_status::deferred;
580 while (!(__state_ & ready) && _Clock::now() < __abs_time)
581 __cv_.wait_until(__lk, __abs_time);
582 if (__state_ & ready)
583 return future_status::ready;
584 return future_status::timeout;
587 template <class _Rep, class _Period>
588 inline _LIBCPP_INLINE_VISIBILITY
590 __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
592 return wait_until(chrono::steady_clock::now() + __rel_time);
597 : public __assoc_sub_state
599 typedef __assoc_sub_state base;
600 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
604 virtual void __on_zero_shared() _NOEXCEPT;
607 template <class _Arg>
608 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
609 void set_value(_Arg&& __arg);
611 void set_value(_Arg& __arg);
614 template <class _Arg>
615 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
616 void set_value_at_thread_exit(_Arg&& __arg);
618 void set_value_at_thread_exit(_Arg& __arg);
622 typename add_lvalue_reference<_Rp>::type copy();
627 __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
629 if (this->__state_ & base::__constructed)
630 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
635 template <class _Arg>
637 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
638 __assoc_state<_Rp>::set_value(_Arg&& __arg)
640 __assoc_state<_Rp>::set_value(_Arg& __arg)
643 unique_lock<mutex> __lk(this->__mut_);
644 #ifndef _LIBCPP_NO_EXCEPTIONS
645 if (this->__has_value())
646 throw future_error(make_error_code(future_errc::promise_already_satisfied));
648 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
649 this->__state_ |= base::__constructed | base::ready;
655 template <class _Arg>
657 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
658 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
660 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
663 unique_lock<mutex> __lk(this->__mut_);
664 #ifndef _LIBCPP_NO_EXCEPTIONS
665 if (this->__has_value())
666 throw future_error(make_error_code(future_errc::promise_already_satisfied));
668 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
669 this->__state_ |= base::__constructed;
670 __thread_local_data()->__make_ready_at_thread_exit(this);
676 __assoc_state<_Rp>::move()
678 unique_lock<mutex> __lk(this->__mut_);
679 this->__sub_wait(__lk);
680 if (this->__exception_ != nullptr)
681 rethrow_exception(this->__exception_);
682 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
686 typename add_lvalue_reference<_Rp>::type
687 __assoc_state<_Rp>::copy()
689 unique_lock<mutex> __lk(this->__mut_);
690 this->__sub_wait(__lk);
691 if (this->__exception_ != nullptr)
692 rethrow_exception(this->__exception_);
693 return *reinterpret_cast<_Rp*>(&__value_);
697 class __assoc_state<_Rp&>
698 : public __assoc_sub_state
700 typedef __assoc_sub_state base;
705 virtual void __on_zero_shared() _NOEXCEPT;
708 void set_value(_Rp& __arg);
709 void set_value_at_thread_exit(_Rp& __arg);
716 __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
723 __assoc_state<_Rp&>::set_value(_Rp& __arg)
725 unique_lock<mutex> __lk(this->__mut_);
726 #ifndef _LIBCPP_NO_EXCEPTIONS
727 if (this->__has_value())
728 throw future_error(make_error_code(future_errc::promise_already_satisfied));
730 __value_ = _VSTD::addressof(__arg);
731 this->__state_ |= base::__constructed | base::ready;
738 __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
740 unique_lock<mutex> __lk(this->__mut_);
741 #ifndef _LIBCPP_NO_EXCEPTIONS
742 if (this->__has_value())
743 throw future_error(make_error_code(future_errc::promise_already_satisfied));
745 __value_ = _VSTD::addressof(__arg);
746 this->__state_ |= base::__constructed;
747 __thread_local_data()->__make_ready_at_thread_exit(this);
753 __assoc_state<_Rp&>::copy()
755 unique_lock<mutex> __lk(this->__mut_);
756 this->__sub_wait(__lk);
757 if (this->__exception_ != nullptr)
758 rethrow_exception(this->__exception_);
762 template <class _Rp, class _Alloc>
763 class __assoc_state_alloc
764 : public __assoc_state<_Rp>
766 typedef __assoc_state<_Rp> base;
769 virtual void __on_zero_shared() _NOEXCEPT;
771 _LIBCPP_INLINE_VISIBILITY
772 explicit __assoc_state_alloc(const _Alloc& __a)
776 template <class _Rp, class _Alloc>
778 __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
780 if (this->__state_ & base::__constructed)
781 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
782 typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
783 this->~__assoc_state_alloc();
784 __a.deallocate(this, 1);
787 template <class _Rp, class _Alloc>
788 class __assoc_state_alloc<_Rp&, _Alloc>
789 : public __assoc_state<_Rp&>
791 typedef __assoc_state<_Rp&> base;
794 virtual void __on_zero_shared() _NOEXCEPT;
796 _LIBCPP_INLINE_VISIBILITY
797 explicit __assoc_state_alloc(const _Alloc& __a)
801 template <class _Rp, class _Alloc>
803 __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
805 typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
806 this->~__assoc_state_alloc();
807 __a.deallocate(this, 1);
810 template <class _Alloc>
811 class __assoc_sub_state_alloc
812 : public __assoc_sub_state
814 typedef __assoc_sub_state base;
817 virtual void __on_zero_shared() _NOEXCEPT;
819 _LIBCPP_INLINE_VISIBILITY
820 explicit __assoc_sub_state_alloc(const _Alloc& __a)
824 template <class _Alloc>
826 __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
828 typename _Alloc::template rebind<__assoc_sub_state_alloc>::other __a(__alloc_);
829 this->~__assoc_sub_state_alloc();
830 __a.deallocate(this, 1);
833 template <class _Rp, class _Fp>
834 class __deferred_assoc_state
835 : public __assoc_state<_Rp>
837 typedef __assoc_state<_Rp> base;
842 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
843 explicit __deferred_assoc_state(_Fp&& __f);
846 virtual void __execute();
849 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
851 template <class _Rp, class _Fp>
852 inline _LIBCPP_INLINE_VISIBILITY
853 __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
854 : __func_(_VSTD::forward<_Fp>(__f))
856 this->__set_deferred();
859 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
861 template <class _Rp, class _Fp>
863 __deferred_assoc_state<_Rp, _Fp>::__execute()
865 #ifndef _LIBCPP_NO_EXCEPTIONS
868 #endif // _LIBCPP_NO_EXCEPTIONS
869 this->set_value(__func_());
870 #ifndef _LIBCPP_NO_EXCEPTIONS
874 this->set_exception(current_exception());
876 #endif // _LIBCPP_NO_EXCEPTIONS
880 class __deferred_assoc_state<void, _Fp>
881 : public __assoc_sub_state
883 typedef __assoc_sub_state base;
888 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
889 explicit __deferred_assoc_state(_Fp&& __f);
892 virtual void __execute();
895 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
898 inline _LIBCPP_INLINE_VISIBILITY
899 __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
900 : __func_(_VSTD::forward<_Fp>(__f))
902 this->__set_deferred();
905 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
909 __deferred_assoc_state<void, _Fp>::__execute()
911 #ifndef _LIBCPP_NO_EXCEPTIONS
914 #endif // _LIBCPP_NO_EXCEPTIONS
917 #ifndef _LIBCPP_NO_EXCEPTIONS
921 this->set_exception(current_exception());
923 #endif // _LIBCPP_NO_EXCEPTIONS
926 template <class _Rp, class _Fp>
927 class __async_assoc_state
928 : public __assoc_state<_Rp>
930 typedef __assoc_state<_Rp> base;
934 virtual void __on_zero_shared() _NOEXCEPT;
936 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
937 explicit __async_assoc_state(_Fp&& __f);
940 virtual void __execute();
943 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
945 template <class _Rp, class _Fp>
946 inline _LIBCPP_INLINE_VISIBILITY
947 __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
948 : __func_(_VSTD::forward<_Fp>(__f))
952 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
954 template <class _Rp, class _Fp>
956 __async_assoc_state<_Rp, _Fp>::__execute()
958 #ifndef _LIBCPP_NO_EXCEPTIONS
961 #endif // _LIBCPP_NO_EXCEPTIONS
962 this->set_value(__func_());
963 #ifndef _LIBCPP_NO_EXCEPTIONS
967 this->set_exception(current_exception());
969 #endif // _LIBCPP_NO_EXCEPTIONS
972 template <class _Rp, class _Fp>
974 __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
977 base::__on_zero_shared();
981 class __async_assoc_state<void, _Fp>
982 : public __assoc_sub_state
984 typedef __assoc_sub_state base;
988 virtual void __on_zero_shared() _NOEXCEPT;
990 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
991 explicit __async_assoc_state(_Fp&& __f);
994 virtual void __execute();
997 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1000 inline _LIBCPP_INLINE_VISIBILITY
1001 __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1002 : __func_(_VSTD::forward<_Fp>(__f))
1006 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1008 template <class _Fp>
1010 __async_assoc_state<void, _Fp>::__execute()
1012 #ifndef _LIBCPP_NO_EXCEPTIONS
1015 #endif // _LIBCPP_NO_EXCEPTIONS
1018 #ifndef _LIBCPP_NO_EXCEPTIONS
1022 this->set_exception(current_exception());
1024 #endif // _LIBCPP_NO_EXCEPTIONS
1027 template <class _Fp>
1029 __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1032 base::__on_zero_shared();
1035 template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY promise;
1036 template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY shared_future;
1040 template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY future;
1042 template <class _Rp, class _Fp>
1044 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1045 __make_deferred_assoc_state(_Fp&& __f);
1047 __make_deferred_assoc_state(_Fp __f);
1050 template <class _Rp, class _Fp>
1052 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1053 __make_async_assoc_state(_Fp&& __f);
1055 __make_async_assoc_state(_Fp __f);
1058 template <class _Rp>
1059 class _LIBCPP_TYPE_VIS_ONLY future
1061 __assoc_state<_Rp>* __state_;
1063 explicit future(__assoc_state<_Rp>* __state);
1065 template <class> friend class promise;
1066 template <class> friend class shared_future;
1068 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1069 template <class _R1, class _Fp>
1070 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1071 template <class _R1, class _Fp>
1072 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1074 template <class _R1, class _Fp>
1075 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1076 template <class _R1, class _Fp>
1077 friend future<_R1> __make_async_assoc_state(_Fp __f);
1081 _LIBCPP_INLINE_VISIBILITY
1082 future() _NOEXCEPT : __state_(nullptr) {}
1083 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1084 _LIBCPP_INLINE_VISIBILITY
1085 future(future&& __rhs) _NOEXCEPT
1086 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1087 future(const future&) = delete;
1088 future& operator=(const future&) = delete;
1089 _LIBCPP_INLINE_VISIBILITY
1090 future& operator=(future&& __rhs) _NOEXCEPT
1092 future(std::move(__rhs)).swap(*this);
1095 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1097 future(const future&);
1098 future& operator=(const future&);
1100 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1102 shared_future<_Rp> share();
1104 // retrieving the value
1107 _LIBCPP_INLINE_VISIBILITY
1108 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1110 // functions to check state
1111 _LIBCPP_INLINE_VISIBILITY
1112 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1114 _LIBCPP_INLINE_VISIBILITY
1115 void wait() const {__state_->wait();}
1116 template <class _Rep, class _Period>
1117 _LIBCPP_INLINE_VISIBILITY
1119 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1120 {return __state_->wait_for(__rel_time);}
1121 template <class _Clock, class _Duration>
1122 _LIBCPP_INLINE_VISIBILITY
1124 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1125 {return __state_->wait_until(__abs_time);}
1128 template <class _Rp>
1129 future<_Rp>::future(__assoc_state<_Rp>* __state)
1132 #ifndef _LIBCPP_NO_EXCEPTIONS
1133 if (__state_->__has_future_attached())
1134 throw future_error(make_error_code(future_errc::future_already_retrieved));
1136 __state_->__add_shared();
1137 __state_->__set_future_attached();
1140 struct __release_shared_count
1142 void operator()(__shared_count* p) {p->__release_shared();}
1145 template <class _Rp>
1146 future<_Rp>::~future()
1149 __state_->__release_shared();
1152 template <class _Rp>
1156 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1157 __assoc_state<_Rp>* __s = __state_;
1162 template <class _Rp>
1163 class _LIBCPP_TYPE_VIS_ONLY future<_Rp&>
1165 __assoc_state<_Rp&>* __state_;
1167 explicit future(__assoc_state<_Rp&>* __state);
1169 template <class> friend class promise;
1170 template <class> friend class shared_future;
1172 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1173 template <class _R1, class _Fp>
1174 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1175 template <class _R1, class _Fp>
1176 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1178 template <class _R1, class _Fp>
1179 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1180 template <class _R1, class _Fp>
1181 friend future<_R1> __make_async_assoc_state(_Fp __f);
1185 _LIBCPP_INLINE_VISIBILITY
1186 future() _NOEXCEPT : __state_(nullptr) {}
1187 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1188 _LIBCPP_INLINE_VISIBILITY
1189 future(future&& __rhs) _NOEXCEPT
1190 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1191 future(const future&) = delete;
1192 future& operator=(const future&) = delete;
1193 _LIBCPP_INLINE_VISIBILITY
1194 future& operator=(future&& __rhs) _NOEXCEPT
1196 future(std::move(__rhs)).swap(*this);
1199 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1201 future(const future&);
1202 future& operator=(const future&);
1204 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1206 shared_future<_Rp&> share();
1208 // retrieving the value
1211 _LIBCPP_INLINE_VISIBILITY
1212 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1214 // functions to check state
1215 _LIBCPP_INLINE_VISIBILITY
1216 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1218 _LIBCPP_INLINE_VISIBILITY
1219 void wait() const {__state_->wait();}
1220 template <class _Rep, class _Period>
1221 _LIBCPP_INLINE_VISIBILITY
1223 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1224 {return __state_->wait_for(__rel_time);}
1225 template <class _Clock, class _Duration>
1226 _LIBCPP_INLINE_VISIBILITY
1228 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1229 {return __state_->wait_until(__abs_time);}
1232 template <class _Rp>
1233 future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1236 #ifndef _LIBCPP_NO_EXCEPTIONS
1237 if (__state_->__has_future_attached())
1238 throw future_error(make_error_code(future_errc::future_already_retrieved));
1240 __state_->__add_shared();
1241 __state_->__set_future_attached();
1244 template <class _Rp>
1245 future<_Rp&>::~future()
1248 __state_->__release_shared();
1251 template <class _Rp>
1255 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1256 __assoc_state<_Rp&>* __s = __state_;
1262 class _LIBCPP_TYPE_VIS future<void>
1264 __assoc_sub_state* __state_;
1266 explicit future(__assoc_sub_state* __state);
1268 template <class> friend class promise;
1269 template <class> friend class shared_future;
1271 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1272 template <class _R1, class _Fp>
1273 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1274 template <class _R1, class _Fp>
1275 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1277 template <class _R1, class _Fp>
1278 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1279 template <class _R1, class _Fp>
1280 friend future<_R1> __make_async_assoc_state(_Fp __f);
1284 _LIBCPP_INLINE_VISIBILITY
1285 future() _NOEXCEPT : __state_(nullptr) {}
1286 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1287 _LIBCPP_INLINE_VISIBILITY
1288 future(future&& __rhs) _NOEXCEPT
1289 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1290 future(const future&) = delete;
1291 future& operator=(const future&) = delete;
1292 _LIBCPP_INLINE_VISIBILITY
1293 future& operator=(future&& __rhs) _NOEXCEPT
1295 future(std::move(__rhs)).swap(*this);
1298 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1300 future(const future&);
1301 future& operator=(const future&);
1303 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1305 shared_future<void> share();
1307 // retrieving the value
1310 _LIBCPP_INLINE_VISIBILITY
1311 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1313 // functions to check state
1314 _LIBCPP_INLINE_VISIBILITY
1315 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1317 _LIBCPP_INLINE_VISIBILITY
1318 void wait() const {__state_->wait();}
1319 template <class _Rep, class _Period>
1320 _LIBCPP_INLINE_VISIBILITY
1322 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1323 {return __state_->wait_for(__rel_time);}
1324 template <class _Clock, class _Duration>
1325 _LIBCPP_INLINE_VISIBILITY
1327 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1328 {return __state_->wait_until(__abs_time);}
1331 template <class _Rp>
1332 inline _LIBCPP_INLINE_VISIBILITY
1334 swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1341 template <class _Callable> class packaged_task;
1343 template <class _Rp>
1344 class _LIBCPP_TYPE_VIS_ONLY promise
1346 __assoc_state<_Rp>* __state_;
1348 _LIBCPP_INLINE_VISIBILITY
1349 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1351 template <class> friend class packaged_task;
1354 template <class _Alloc>
1355 promise(allocator_arg_t, const _Alloc& __a);
1356 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1357 _LIBCPP_INLINE_VISIBILITY
1358 promise(promise&& __rhs) _NOEXCEPT
1359 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1360 promise(const promise& __rhs) = delete;
1361 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1363 promise(const promise& __rhs);
1365 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1369 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1370 _LIBCPP_INLINE_VISIBILITY
1371 promise& operator=(promise&& __rhs) _NOEXCEPT
1373 promise(std::move(__rhs)).swap(*this);
1376 promise& operator=(const promise& __rhs) = delete;
1377 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1379 promise& operator=(const promise& __rhs);
1381 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1382 _LIBCPP_INLINE_VISIBILITY
1383 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1385 // retrieving the result
1386 future<_Rp> get_future();
1388 // setting the result
1389 void set_value(const _Rp& __r);
1390 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1391 void set_value(_Rp&& __r);
1393 void set_exception(exception_ptr __p);
1395 // setting the result with deferred notification
1396 void set_value_at_thread_exit(const _Rp& __r);
1397 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1398 void set_value_at_thread_exit(_Rp&& __r);
1400 void set_exception_at_thread_exit(exception_ptr __p);
1403 template <class _Rp>
1404 promise<_Rp>::promise()
1405 : __state_(new __assoc_state<_Rp>)
1409 template <class _Rp>
1410 template <class _Alloc>
1411 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1413 typedef typename _Alloc::template rebind<__assoc_state_alloc<_Rp, _Alloc> >::other _A2;
1414 typedef __allocator_destructor<_A2> _D2;
1416 unique_ptr<__assoc_state_alloc<_Rp, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1417 ::new(__hold.get()) __assoc_state_alloc<_Rp, _Alloc>(__a0);
1418 __state_ = __hold.release();
1421 template <class _Rp>
1422 promise<_Rp>::~promise()
1426 if (!__state_->__has_value() && __state_->use_count() > 1)
1427 __state_->set_exception(make_exception_ptr(
1428 future_error(make_error_code(future_errc::broken_promise))
1430 __state_->__release_shared();
1434 template <class _Rp>
1436 promise<_Rp>::get_future()
1438 #ifndef _LIBCPP_NO_EXCEPTIONS
1439 if (__state_ == nullptr)
1440 throw future_error(make_error_code(future_errc::no_state));
1442 return future<_Rp>(__state_);
1445 template <class _Rp>
1447 promise<_Rp>::set_value(const _Rp& __r)
1449 #ifndef _LIBCPP_NO_EXCEPTIONS
1450 if (__state_ == nullptr)
1451 throw future_error(make_error_code(future_errc::no_state));
1453 __state_->set_value(__r);
1456 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1458 template <class _Rp>
1460 promise<_Rp>::set_value(_Rp&& __r)
1462 #ifndef _LIBCPP_NO_EXCEPTIONS
1463 if (__state_ == nullptr)
1464 throw future_error(make_error_code(future_errc::no_state));
1466 __state_->set_value(_VSTD::move(__r));
1469 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1471 template <class _Rp>
1473 promise<_Rp>::set_exception(exception_ptr __p)
1475 #ifndef _LIBCPP_NO_EXCEPTIONS
1476 if (__state_ == nullptr)
1477 throw future_error(make_error_code(future_errc::no_state));
1479 __state_->set_exception(__p);
1482 template <class _Rp>
1484 promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1486 #ifndef _LIBCPP_NO_EXCEPTIONS
1487 if (__state_ == nullptr)
1488 throw future_error(make_error_code(future_errc::no_state));
1490 __state_->set_value_at_thread_exit(__r);
1493 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1495 template <class _Rp>
1497 promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1499 #ifndef _LIBCPP_NO_EXCEPTIONS
1500 if (__state_ == nullptr)
1501 throw future_error(make_error_code(future_errc::no_state));
1503 __state_->set_value_at_thread_exit(_VSTD::move(__r));
1506 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1508 template <class _Rp>
1510 promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
1512 #ifndef _LIBCPP_NO_EXCEPTIONS
1513 if (__state_ == nullptr)
1514 throw future_error(make_error_code(future_errc::no_state));
1516 __state_->set_exception_at_thread_exit(__p);
1521 template <class _Rp>
1522 class _LIBCPP_TYPE_VIS_ONLY promise<_Rp&>
1524 __assoc_state<_Rp&>* __state_;
1526 _LIBCPP_INLINE_VISIBILITY
1527 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1529 template <class> friend class packaged_task;
1533 template <class _Allocator>
1534 promise(allocator_arg_t, const _Allocator& __a);
1535 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1536 _LIBCPP_INLINE_VISIBILITY
1537 promise(promise&& __rhs) _NOEXCEPT
1538 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1539 promise(const promise& __rhs) = delete;
1540 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1542 promise(const promise& __rhs);
1544 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1548 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1549 _LIBCPP_INLINE_VISIBILITY
1550 promise& operator=(promise&& __rhs) _NOEXCEPT
1552 promise(std::move(__rhs)).swap(*this);
1555 promise& operator=(const promise& __rhs) = delete;
1556 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1558 promise& operator=(const promise& __rhs);
1560 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1561 _LIBCPP_INLINE_VISIBILITY
1562 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1564 // retrieving the result
1565 future<_Rp&> get_future();
1567 // setting the result
1568 void set_value(_Rp& __r);
1569 void set_exception(exception_ptr __p);
1571 // setting the result with deferred notification
1572 void set_value_at_thread_exit(_Rp&);
1573 void set_exception_at_thread_exit(exception_ptr __p);
1576 template <class _Rp>
1577 promise<_Rp&>::promise()
1578 : __state_(new __assoc_state<_Rp&>)
1582 template <class _Rp>
1583 template <class _Alloc>
1584 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1586 typedef typename _Alloc::template rebind<__assoc_state_alloc<_Rp&, _Alloc> >::other _A2;
1587 typedef __allocator_destructor<_A2> _D2;
1589 unique_ptr<__assoc_state_alloc<_Rp&, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1590 ::new(__hold.get()) __assoc_state_alloc<_Rp&, _Alloc>(__a0);
1591 __state_ = __hold.release();
1594 template <class _Rp>
1595 promise<_Rp&>::~promise()
1599 if (!__state_->__has_value() && __state_->use_count() > 1)
1600 __state_->set_exception(make_exception_ptr(
1601 future_error(make_error_code(future_errc::broken_promise))
1603 __state_->__release_shared();
1607 template <class _Rp>
1609 promise<_Rp&>::get_future()
1611 #ifndef _LIBCPP_NO_EXCEPTIONS
1612 if (__state_ == nullptr)
1613 throw future_error(make_error_code(future_errc::no_state));
1615 return future<_Rp&>(__state_);
1618 template <class _Rp>
1620 promise<_Rp&>::set_value(_Rp& __r)
1622 #ifndef _LIBCPP_NO_EXCEPTIONS
1623 if (__state_ == nullptr)
1624 throw future_error(make_error_code(future_errc::no_state));
1626 __state_->set_value(__r);
1629 template <class _Rp>
1631 promise<_Rp&>::set_exception(exception_ptr __p)
1633 #ifndef _LIBCPP_NO_EXCEPTIONS
1634 if (__state_ == nullptr)
1635 throw future_error(make_error_code(future_errc::no_state));
1637 __state_->set_exception(__p);
1640 template <class _Rp>
1642 promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1644 #ifndef _LIBCPP_NO_EXCEPTIONS
1645 if (__state_ == nullptr)
1646 throw future_error(make_error_code(future_errc::no_state));
1648 __state_->set_value_at_thread_exit(__r);
1651 template <class _Rp>
1653 promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
1655 #ifndef _LIBCPP_NO_EXCEPTIONS
1656 if (__state_ == nullptr)
1657 throw future_error(make_error_code(future_errc::no_state));
1659 __state_->set_exception_at_thread_exit(__p);
1665 class _LIBCPP_TYPE_VIS promise<void>
1667 __assoc_sub_state* __state_;
1669 _LIBCPP_INLINE_VISIBILITY
1670 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1672 template <class> friend class packaged_task;
1676 template <class _Allocator>
1677 promise(allocator_arg_t, const _Allocator& __a);
1678 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1679 _LIBCPP_INLINE_VISIBILITY
1680 promise(promise&& __rhs) _NOEXCEPT
1681 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1682 promise(const promise& __rhs) = delete;
1683 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1685 promise(const promise& __rhs);
1687 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1691 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1692 _LIBCPP_INLINE_VISIBILITY
1693 promise& operator=(promise&& __rhs) _NOEXCEPT
1695 promise(std::move(__rhs)).swap(*this);
1698 promise& operator=(const promise& __rhs) = delete;
1699 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1701 promise& operator=(const promise& __rhs);
1703 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1704 _LIBCPP_INLINE_VISIBILITY
1705 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1707 // retrieving the result
1708 future<void> get_future();
1710 // setting the result
1712 void set_exception(exception_ptr __p);
1714 // setting the result with deferred notification
1715 void set_value_at_thread_exit();
1716 void set_exception_at_thread_exit(exception_ptr __p);
1719 template <class _Alloc>
1720 promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1722 typedef typename _Alloc::template rebind<__assoc_sub_state_alloc<_Alloc> >::other _A2;
1723 typedef __allocator_destructor<_A2> _D2;
1725 unique_ptr<__assoc_sub_state_alloc<_Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1726 ::new(__hold.get()) __assoc_sub_state_alloc<_Alloc>(__a0);
1727 __state_ = __hold.release();
1730 template <class _Rp>
1731 inline _LIBCPP_INLINE_VISIBILITY
1733 swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1738 template <class _Rp, class _Alloc>
1739 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<promise<_Rp>, _Alloc>
1740 : public true_type {};
1742 #ifndef _LIBCPP_HAS_NO_VARIADICS
1746 template<class _Fp> class __packaged_task_base;
1748 template<class _Rp, class ..._ArgTypes>
1749 class __packaged_task_base<_Rp(_ArgTypes...)>
1751 __packaged_task_base(const __packaged_task_base&);
1752 __packaged_task_base& operator=(const __packaged_task_base&);
1754 _LIBCPP_INLINE_VISIBILITY
1755 __packaged_task_base() {}
1756 _LIBCPP_INLINE_VISIBILITY
1757 virtual ~__packaged_task_base() {}
1758 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1759 virtual void destroy() = 0;
1760 virtual void destroy_deallocate() = 0;
1761 virtual _Rp operator()(_ArgTypes&& ...) = 0;
1764 template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1766 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1767 class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1768 : public __packaged_task_base<_Rp(_ArgTypes...)>
1770 __compressed_pair<_Fp, _Alloc> __f_;
1772 _LIBCPP_INLINE_VISIBILITY
1773 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
1774 _LIBCPP_INLINE_VISIBILITY
1775 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
1776 _LIBCPP_INLINE_VISIBILITY
1777 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
1779 _LIBCPP_INLINE_VISIBILITY
1780 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
1781 : __f_(_VSTD::move(__f), __a) {}
1782 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1783 virtual void destroy();
1784 virtual void destroy_deallocate();
1785 virtual _Rp operator()(_ArgTypes&& ... __args);
1788 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1790 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1791 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1793 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1796 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1798 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1800 __f_.~__compressed_pair<_Fp, _Alloc>();
1803 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1805 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
1807 typedef typename _Alloc::template rebind<__packaged_task_func>::other _Ap;
1808 _Ap __a(__f_.second());
1809 __f_.~__compressed_pair<_Fp, _Alloc>();
1810 __a.deallocate(this, 1);
1813 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1815 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1817 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1820 template <class _Callable> class __packaged_task_function;
1822 template<class _Rp, class ..._ArgTypes>
1823 class __packaged_task_function<_Rp(_ArgTypes...)>
1825 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1826 typename aligned_storage<3*sizeof(void*)>::type __buf_;
1830 typedef _Rp result_type;
1832 // construct/copy/destroy:
1833 _LIBCPP_INLINE_VISIBILITY
1834 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1836 __packaged_task_function(_Fp&& __f);
1837 template<class _Fp, class _Alloc>
1838 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1840 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1841 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1843 __packaged_task_function(const __packaged_task_function&) = delete;
1844 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1846 ~__packaged_task_function();
1848 void swap(__packaged_task_function&) _NOEXCEPT;
1850 _Rp operator()(_ArgTypes...) const;
1853 template<class _Rp, class ..._ArgTypes>
1854 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1856 if (__f.__f_ == nullptr)
1858 else if (__f.__f_ == (__base*)&__f.__buf_)
1860 __f_ = (__base*)&__buf_;
1861 __f.__f_->__move_to(__f_);
1870 template<class _Rp, class ..._ArgTypes>
1871 template <class _Fp>
1872 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
1875 typedef typename remove_reference<_Fp>::type _FR;
1876 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1877 if (sizeof(_FF) <= sizeof(__buf_))
1879 __f_ = (__base*)&__buf_;
1880 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1884 typedef allocator<_FF> _Ap;
1886 typedef __allocator_destructor<_Ap> _Dp;
1887 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1888 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
1889 __f_ = __hold.release();
1893 template<class _Rp, class ..._ArgTypes>
1894 template <class _Fp, class _Alloc>
1895 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1896 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1899 typedef allocator_traits<_Alloc> __alloc_traits;
1900 typedef typename remove_reference<_Fp>::type _FR;
1901 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1902 if (sizeof(_FF) <= sizeof(__buf_))
1904 __f_ = (__base*)&__buf_;
1905 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1909 typedef typename __alloc_traits::template
1910 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1913 rebind_alloc<_FF>::other
1917 typedef __allocator_destructor<_Ap> _Dp;
1918 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1919 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1920 __f_ = __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>
1995 inline _LIBCPP_INLINE_VISIBILITY
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_TYPE_VIS_ONLY packaged_task<_Rp(_ArgTypes...)>
2006 typedef _Rp result_type;
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 explicit 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 #ifndef _LIBCPP_NO_EXCEPTIONS
2083 if (__p_.__state_ == nullptr)
2084 throw future_error(make_error_code(future_errc::no_state));
2085 if (__p_.__state_->__has_value())
2086 throw future_error(make_error_code(future_errc::promise_already_satisfied));
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 #ifndef _LIBCPP_NO_EXCEPTIONS
2105 if (__p_.__state_ == nullptr)
2106 throw future_error(make_error_code(future_errc::no_state));
2107 if (__p_.__state_->__has_value())
2108 throw future_error(make_error_code(future_errc::promise_already_satisfied));
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()
2126 #ifndef _LIBCPP_NO_EXCEPTIONS
2128 throw future_error(make_error_code(future_errc::no_state));
2129 #endif // _LIBCPP_NO_EXCEPTIONS
2130 __p_ = promise<result_type>();
2133 template<class ..._ArgTypes>
2134 class _LIBCPP_TYPE_VIS_ONLY packaged_task<void(_ArgTypes...)>
2137 typedef void result_type;
2140 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2141 promise<result_type> __p_;
2144 // construction and destruction
2145 _LIBCPP_INLINE_VISIBILITY
2146 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2147 template <class _Fp,
2148 class = typename enable_if
2151 typename decay<_Fp>::type,
2156 _LIBCPP_INLINE_VISIBILITY
2157 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2158 template <class _Fp, class _Allocator,
2159 class = typename enable_if
2162 typename decay<_Fp>::type,
2167 _LIBCPP_INLINE_VISIBILITY
2168 explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2169 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2170 __p_(allocator_arg, __a) {}
2171 // ~packaged_task() = default;
2174 packaged_task(const packaged_task&) = delete;
2175 packaged_task& operator=(const packaged_task&) = delete;
2178 _LIBCPP_INLINE_VISIBILITY
2179 packaged_task(packaged_task&& __other) _NOEXCEPT
2180 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2181 _LIBCPP_INLINE_VISIBILITY
2182 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2184 __f_ = _VSTD::move(__other.__f_);
2185 __p_ = _VSTD::move(__other.__p_);
2188 _LIBCPP_INLINE_VISIBILITY
2189 void swap(packaged_task& __other) _NOEXCEPT
2191 __f_.swap(__other.__f_);
2192 __p_.swap(__other.__p_);
2195 _LIBCPP_INLINE_VISIBILITY
2196 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2199 _LIBCPP_INLINE_VISIBILITY
2200 future<result_type> get_future() {return __p_.get_future();}
2203 void operator()(_ArgTypes... __args);
2204 void make_ready_at_thread_exit(_ArgTypes... __args);
2209 template<class ..._ArgTypes>
2211 packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2213 #ifndef _LIBCPP_NO_EXCEPTIONS
2214 if (__p_.__state_ == nullptr)
2215 throw future_error(make_error_code(future_errc::no_state));
2216 if (__p_.__state_->__has_value())
2217 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2220 #endif // _LIBCPP_NO_EXCEPTIONS
2221 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2223 #ifndef _LIBCPP_NO_EXCEPTIONS
2227 __p_.set_exception(current_exception());
2229 #endif // _LIBCPP_NO_EXCEPTIONS
2232 template<class ..._ArgTypes>
2234 packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2236 #ifndef _LIBCPP_NO_EXCEPTIONS
2237 if (__p_.__state_ == nullptr)
2238 throw future_error(make_error_code(future_errc::no_state));
2239 if (__p_.__state_->__has_value())
2240 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2243 #endif // _LIBCPP_NO_EXCEPTIONS
2244 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2245 __p_.set_value_at_thread_exit();
2246 #ifndef _LIBCPP_NO_EXCEPTIONS
2250 __p_.set_exception_at_thread_exit(current_exception());
2252 #endif // _LIBCPP_NO_EXCEPTIONS
2255 template<class ..._ArgTypes>
2257 packaged_task<void(_ArgTypes...)>::reset()
2259 #ifndef _LIBCPP_NO_EXCEPTIONS
2261 throw future_error(make_error_code(future_errc::no_state));
2262 #endif // _LIBCPP_NO_EXCEPTIONS
2263 __p_ = promise<result_type>();
2266 template <class _Callable>
2267 inline _LIBCPP_INLINE_VISIBILITY
2269 swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
2274 template <class _Callable, class _Alloc>
2275 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<packaged_task<_Callable>, _Alloc>
2276 : public true_type {};
2278 template <class _Rp, class _Fp>
2280 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2281 __make_deferred_assoc_state(_Fp&& __f)
2283 __make_deferred_assoc_state(_Fp __f)
2286 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2287 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2288 return future<_Rp>(__h.get());
2291 template <class _Rp, class _Fp>
2293 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2294 __make_async_assoc_state(_Fp&& __f)
2296 __make_async_assoc_state(_Fp __f)
2299 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2300 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2301 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2302 return future<_Rp>(__h.get());
2305 template <class _Fp, class... _Args>
2308 tuple<_Fp, _Args...> __f_;
2311 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2313 _LIBCPP_INLINE_VISIBILITY
2314 explicit __async_func(_Fp&& __f, _Args&&... __args)
2315 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2317 _LIBCPP_INLINE_VISIBILITY
2318 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2322 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2323 return __execute(_Index());
2326 template <size_t ..._Indices>
2328 __execute(__tuple_indices<_Indices...>)
2330 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2334 inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2335 { return (int(__policy) & int(__value)) != 0; }
2337 template <class _Fp, class... _Args>
2338 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2339 async(launch __policy, _Fp&& __f, _Args&&... __args)
2341 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2342 typedef typename _BF::_Rp _Rp;
2344 #ifndef _LIBCPP_NO_EXCEPTIONS
2348 if (__does_policy_contain(__policy, launch::async))
2349 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2350 __decay_copy(_VSTD::forward<_Args>(__args))...));
2351 #ifndef _LIBCPP_NO_EXCEPTIONS
2353 catch ( ... ) { if (__policy == launch::async) throw ; }
2356 if (__does_policy_contain(__policy, launch::deferred))
2357 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2358 __decay_copy(_VSTD::forward<_Args>(__args))...));
2359 return future<_Rp>{};
2362 template <class _Fp, class... _Args>
2363 inline _LIBCPP_INLINE_VISIBILITY
2364 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2365 async(_Fp&& __f, _Args&&... __args)
2367 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2368 _VSTD::forward<_Args>(__args)...);
2371 #endif // _LIBCPP_HAS_NO_VARIADICS
2375 template <class _Rp>
2376 class _LIBCPP_TYPE_VIS_ONLY shared_future
2378 __assoc_state<_Rp>* __state_;
2381 _LIBCPP_INLINE_VISIBILITY
2382 shared_future() _NOEXCEPT : __state_(nullptr) {}
2383 _LIBCPP_INLINE_VISIBILITY
2384 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2385 {if (__state_) __state_->__add_shared();}
2386 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2387 _LIBCPP_INLINE_VISIBILITY
2388 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
2389 {__f.__state_ = nullptr;}
2390 _LIBCPP_INLINE_VISIBILITY
2391 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2392 {__rhs.__state_ = nullptr;}
2393 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2395 shared_future& operator=(const shared_future& __rhs);
2396 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2397 _LIBCPP_INLINE_VISIBILITY
2398 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2400 shared_future(std::move(__rhs)).swap(*this);
2403 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2405 // retrieving the value
2406 _LIBCPP_INLINE_VISIBILITY
2407 const _Rp& get() const {return __state_->copy();}
2409 _LIBCPP_INLINE_VISIBILITY
2410 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2412 // functions to check state
2413 _LIBCPP_INLINE_VISIBILITY
2414 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2416 _LIBCPP_INLINE_VISIBILITY
2417 void wait() const {__state_->wait();}
2418 template <class _Rep, class _Period>
2419 _LIBCPP_INLINE_VISIBILITY
2421 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2422 {return __state_->wait_for(__rel_time);}
2423 template <class _Clock, class _Duration>
2424 _LIBCPP_INLINE_VISIBILITY
2426 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2427 {return __state_->wait_until(__abs_time);}
2430 template <class _Rp>
2431 shared_future<_Rp>::~shared_future()
2434 __state_->__release_shared();
2437 template <class _Rp>
2439 shared_future<_Rp>::operator=(const shared_future& __rhs)
2442 __rhs.__state_->__add_shared();
2444 __state_->__release_shared();
2445 __state_ = __rhs.__state_;
2449 template <class _Rp>
2450 class _LIBCPP_TYPE_VIS_ONLY shared_future<_Rp&>
2452 __assoc_state<_Rp&>* __state_;
2455 _LIBCPP_INLINE_VISIBILITY
2456 shared_future() _NOEXCEPT : __state_(nullptr) {}
2457 _LIBCPP_INLINE_VISIBILITY
2458 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2459 {if (__state_) __state_->__add_shared();}
2460 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2461 _LIBCPP_INLINE_VISIBILITY
2462 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
2463 {__f.__state_ = nullptr;}
2464 _LIBCPP_INLINE_VISIBILITY
2465 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2466 {__rhs.__state_ = nullptr;}
2467 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2469 shared_future& operator=(const shared_future& __rhs);
2470 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2471 _LIBCPP_INLINE_VISIBILITY
2472 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2474 shared_future(std::move(__rhs)).swap(*this);
2477 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2479 // retrieving the value
2480 _LIBCPP_INLINE_VISIBILITY
2481 _Rp& get() const {return __state_->copy();}
2483 _LIBCPP_INLINE_VISIBILITY
2484 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2486 // functions to check state
2487 _LIBCPP_INLINE_VISIBILITY
2488 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2490 _LIBCPP_INLINE_VISIBILITY
2491 void wait() const {__state_->wait();}
2492 template <class _Rep, class _Period>
2493 _LIBCPP_INLINE_VISIBILITY
2495 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2496 {return __state_->wait_for(__rel_time);}
2497 template <class _Clock, class _Duration>
2498 _LIBCPP_INLINE_VISIBILITY
2500 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2501 {return __state_->wait_until(__abs_time);}
2504 template <class _Rp>
2505 shared_future<_Rp&>::~shared_future()
2508 __state_->__release_shared();
2511 template <class _Rp>
2512 shared_future<_Rp&>&
2513 shared_future<_Rp&>::operator=(const shared_future& __rhs)
2516 __rhs.__state_->__add_shared();
2518 __state_->__release_shared();
2519 __state_ = __rhs.__state_;
2524 class _LIBCPP_TYPE_VIS shared_future<void>
2526 __assoc_sub_state* __state_;
2529 _LIBCPP_INLINE_VISIBILITY
2530 shared_future() _NOEXCEPT : __state_(nullptr) {}
2531 _LIBCPP_INLINE_VISIBILITY
2532 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2533 {if (__state_) __state_->__add_shared();}
2534 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2535 _LIBCPP_INLINE_VISIBILITY
2536 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
2537 {__f.__state_ = nullptr;}
2538 _LIBCPP_INLINE_VISIBILITY
2539 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2540 {__rhs.__state_ = nullptr;}
2541 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2543 shared_future& operator=(const shared_future& __rhs);
2544 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2545 _LIBCPP_INLINE_VISIBILITY
2546 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2548 shared_future(std::move(__rhs)).swap(*this);
2551 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2553 // retrieving the value
2554 _LIBCPP_INLINE_VISIBILITY
2555 void get() const {__state_->copy();}
2557 _LIBCPP_INLINE_VISIBILITY
2558 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2560 // functions to check state
2561 _LIBCPP_INLINE_VISIBILITY
2562 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2564 _LIBCPP_INLINE_VISIBILITY
2565 void wait() const {__state_->wait();}
2566 template <class _Rep, class _Period>
2567 _LIBCPP_INLINE_VISIBILITY
2569 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2570 {return __state_->wait_for(__rel_time);}
2571 template <class _Clock, class _Duration>
2572 _LIBCPP_INLINE_VISIBILITY
2574 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2575 {return __state_->wait_until(__abs_time);}
2578 template <class _Rp>
2579 inline _LIBCPP_INLINE_VISIBILITY
2581 swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2586 template <class _Rp>
2587 inline _LIBCPP_INLINE_VISIBILITY
2589 future<_Rp>::share()
2591 return shared_future<_Rp>(_VSTD::move(*this));
2594 template <class _Rp>
2595 inline _LIBCPP_INLINE_VISIBILITY
2597 future<_Rp&>::share()
2599 return shared_future<_Rp&>(_VSTD::move(*this));
2602 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2604 inline _LIBCPP_INLINE_VISIBILITY
2606 future<void>::share()
2608 return shared_future<void>(_VSTD::move(*this));
2611 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2613 _LIBCPP_END_NAMESPACE_STD
2615 #endif // _LIBCPP_FUTURE