2 //===--------------------------- future -----------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_FUTURE
11 #define _LIBCPP_FUTURE
19 enum class future_errc
21 future_already_retrieved = 1,
22 promise_already_satisfied,
31 any = async | deferred
34 enum class future_status
41 template <> struct is_error_code_enum<future_errc> : public true_type { };
42 error_code make_error_code(future_errc e) noexcept;
43 error_condition make_error_condition(future_errc e) noexcept;
45 const error_category& future_category() noexcept;
51 future_error(error_code ec); // exposition only
52 explicit future_error(future_errc); // C++17
53 const error_code& code() const noexcept;
54 const char* what() const noexcept;
62 template <class Allocator>
63 promise(allocator_arg_t, const Allocator& a);
64 promise(promise&& rhs) noexcept;
65 promise(const promise& rhs) = delete;
69 promise& operator=(promise&& rhs) noexcept;
70 promise& operator=(const promise& rhs) = delete;
71 void swap(promise& other) noexcept;
73 // retrieving the result
74 future<R> get_future();
77 void set_value(const R& r);
78 void set_value(R&& r);
79 void set_exception(exception_ptr p);
81 // setting the result with deferred notification
82 void set_value_at_thread_exit(const R& r);
83 void set_value_at_thread_exit(R&& r);
84 void set_exception_at_thread_exit(exception_ptr p);
92 template <class Allocator>
93 promise(allocator_arg_t, const Allocator& a);
94 promise(promise&& rhs) noexcept;
95 promise(const promise& rhs) = delete;
99 promise& operator=(promise&& rhs) noexcept;
100 promise& operator=(const promise& rhs) = delete;
101 void swap(promise& other) noexcept;
103 // retrieving the result
104 future<R&> get_future();
106 // setting the result
107 void set_value(R& r);
108 void set_exception(exception_ptr p);
110 // setting the result with deferred notification
111 void set_value_at_thread_exit(R&);
112 void set_exception_at_thread_exit(exception_ptr p);
120 template <class Allocator>
121 promise(allocator_arg_t, const Allocator& a);
122 promise(promise&& rhs) noexcept;
123 promise(const promise& rhs) = delete;
127 promise& operator=(promise&& rhs) noexcept;
128 promise& operator=(const promise& rhs) = delete;
129 void swap(promise& other) noexcept;
131 // retrieving the result
132 future<void> get_future();
134 // setting the result
136 void set_exception(exception_ptr p);
138 // setting the result with deferred notification
139 void set_value_at_thread_exit();
140 void set_exception_at_thread_exit(exception_ptr p);
143 template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
145 template <class R, class Alloc>
146 struct uses_allocator<promise<R>, Alloc> : public true_type {};
153 future(future&&) noexcept;
154 future(const future& rhs) = delete;
156 future& operator=(const future& rhs) = delete;
157 future& operator=(future&&) noexcept;
158 shared_future<R> share() noexcept;
160 // retrieving the value
163 // functions to check state
164 bool valid() const noexcept;
167 template <class Rep, class Period>
169 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
170 template <class Clock, class Duration>
172 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
180 future(future&&) noexcept;
181 future(const future& rhs) = delete;
183 future& operator=(const future& rhs) = delete;
184 future& operator=(future&&) noexcept;
185 shared_future<R&> share() noexcept;
187 // retrieving the value
190 // functions to check state
191 bool valid() const noexcept;
194 template <class Rep, class Period>
196 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
197 template <class Clock, class Duration>
199 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
207 future(future&&) noexcept;
208 future(const future& rhs) = delete;
210 future& operator=(const future& rhs) = delete;
211 future& operator=(future&&) noexcept;
212 shared_future<void> share() noexcept;
214 // retrieving the value
217 // functions to check state
218 bool valid() const noexcept;
221 template <class Rep, class Period>
223 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
224 template <class Clock, class Duration>
226 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
233 shared_future() noexcept;
234 shared_future(const shared_future& rhs);
235 shared_future(future<R>&&) noexcept;
236 shared_future(shared_future&& rhs) noexcept;
238 shared_future& operator=(const shared_future& rhs);
239 shared_future& operator=(shared_future&& rhs) noexcept;
241 // retrieving the value
242 const R& get() const;
244 // functions to check state
245 bool valid() const noexcept;
248 template <class Rep, class Period>
250 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
251 template <class Clock, class Duration>
253 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
257 class shared_future<R&>
260 shared_future() noexcept;
261 shared_future(const shared_future& rhs);
262 shared_future(future<R&>&&) noexcept;
263 shared_future(shared_future&& rhs) noexcept;
265 shared_future& operator=(const shared_future& rhs);
266 shared_future& operator=(shared_future&& rhs) noexcept;
268 // retrieving the value
271 // functions to check state
272 bool valid() const noexcept;
275 template <class Rep, class Period>
277 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
278 template <class Clock, class Duration>
280 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
284 class shared_future<void>
287 shared_future() noexcept;
288 shared_future(const shared_future& rhs);
289 shared_future(future<void>&&) noexcept;
290 shared_future(shared_future&& rhs) noexcept;
292 shared_future& operator=(const shared_future& rhs);
293 shared_future& operator=(shared_future&& rhs) noexcept;
295 // retrieving the value
298 // functions to check state
299 bool valid() const noexcept;
302 template <class Rep, class Period>
304 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
305 template <class Clock, class Duration>
307 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
310 template <class F, class... Args>
311 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
312 async(F&& f, Args&&... args);
314 template <class F, class... Args>
315 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
316 async(launch policy, F&& f, Args&&... args);
318 template <class> class packaged_task; // undefined
320 template <class R, class... ArgTypes>
321 class packaged_task<R(ArgTypes...)>
324 typedef R result_type; // extension
326 // construction and destruction
327 packaged_task() noexcept;
329 explicit packaged_task(F&& f);
330 template <class F, class Allocator>
331 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
335 packaged_task(const packaged_task&) = delete;
336 packaged_task& operator=(const packaged_task&) = delete;
339 packaged_task(packaged_task&& other) noexcept;
340 packaged_task& operator=(packaged_task&& other) noexcept;
341 void swap(packaged_task& other) noexcept;
343 bool valid() const noexcept;
346 future<R> get_future();
349 void operator()(ArgTypes... );
350 void make_ready_at_thread_exit(ArgTypes...);
356 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
358 template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
365 #include <system_error>
372 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
373 #pragma GCC system_header
376 #ifdef _LIBCPP_HAS_NO_THREADS
377 #error <future> is not supported on this single threaded system
378 #else // !_LIBCPP_HAS_NO_THREADS
380 _LIBCPP_BEGIN_NAMESPACE_STD
382 //enum class future_errc
383 _LIBCPP_DECLARE_STRONG_ENUM(future_errc)
385 future_already_retrieved = 1,
386 promise_already_satisfied,
390 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
393 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
395 #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
397 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
401 _LIBCPP_DECLARE_STRONG_ENUM(launch)
405 any = async | deferred
407 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
409 #ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
411 typedef underlying_type<launch>::type __launch_underlying_type;
413 inline _LIBCPP_INLINE_VISIBILITY
416 operator&(launch __x, launch __y)
418 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
419 static_cast<__launch_underlying_type>(__y));
422 inline _LIBCPP_INLINE_VISIBILITY
425 operator|(launch __x, launch __y)
427 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
428 static_cast<__launch_underlying_type>(__y));
431 inline _LIBCPP_INLINE_VISIBILITY
434 operator^(launch __x, launch __y)
436 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
437 static_cast<__launch_underlying_type>(__y));
440 inline _LIBCPP_INLINE_VISIBILITY
443 operator~(launch __x)
445 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
448 inline _LIBCPP_INLINE_VISIBILITY
450 operator&=(launch& __x, launch __y)
452 __x = __x & __y; return __x;
455 inline _LIBCPP_INLINE_VISIBILITY
457 operator|=(launch& __x, launch __y)
459 __x = __x | __y; return __x;
462 inline _LIBCPP_INLINE_VISIBILITY
464 operator^=(launch& __x, launch __y)
466 __x = __x ^ __y; return __x;
469 #endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
471 //enum class future_status
472 _LIBCPP_DECLARE_STRONG_ENUM(future_status)
478 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
481 const error_category& future_category() _NOEXCEPT;
483 inline _LIBCPP_INLINE_VISIBILITY
485 make_error_code(future_errc __e) _NOEXCEPT
487 return error_code(static_cast<int>(__e), future_category());
490 inline _LIBCPP_INLINE_VISIBILITY
492 make_error_condition(future_errc __e) _NOEXCEPT
494 return error_condition(static_cast<int>(__e), future_category());
497 class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
502 future_error(error_code __ec);
503 #if _LIBCPP_STD_VERS > 14
504 explicit future_error(future_errc _Ev) : logic_error(), __ec_(make_error_code(_Ev)) {}
506 _LIBCPP_INLINE_VISIBILITY
507 const error_code& code() const _NOEXCEPT {return __ec_;}
509 virtual ~future_error() _NOEXCEPT;
512 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
513 #ifndef _LIBCPP_NO_EXCEPTIONS
514 _LIBCPP_AVAILABILITY_FUTURE_ERROR
516 void __throw_future_error(future_errc _Ev)
518 #ifndef _LIBCPP_NO_EXCEPTIONS
519 throw future_error(make_error_code(_Ev));
526 class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
527 : public __shared_count
530 exception_ptr __exception_;
531 mutable mutex __mut_;
532 mutable condition_variable __cv_;
535 virtual void __on_zero_shared() _NOEXCEPT;
536 void __sub_wait(unique_lock<mutex>& __lk);
541 __future_attached = 2,
546 _LIBCPP_INLINE_VISIBILITY
547 __assoc_sub_state() : __state_(0) {}
549 _LIBCPP_INLINE_VISIBILITY
550 bool __has_value() const
551 {return (__state_ & __constructed) || (__exception_ != nullptr);}
553 _LIBCPP_INLINE_VISIBILITY
554 void __attach_future() {
555 lock_guard<mutex> __lk(__mut_);
556 bool __has_future_attached = (__state_ & __future_attached) != 0;
557 if (__has_future_attached)
558 __throw_future_error(future_errc::future_already_retrieved);
559 this->__add_shared();
560 __state_ |= __future_attached;
563 _LIBCPP_INLINE_VISIBILITY
564 void __set_deferred() {__state_ |= deferred;}
567 _LIBCPP_INLINE_VISIBILITY
568 bool __is_ready() const {return (__state_ & ready) != 0;}
571 void set_value_at_thread_exit();
573 void set_exception(exception_ptr __p);
574 void set_exception_at_thread_exit(exception_ptr __p);
579 template <class _Rep, class _Period>
581 _LIBCPP_INLINE_VISIBILITY
582 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
583 template <class _Clock, class _Duration>
584 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
586 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
588 virtual void __execute();
591 template <class _Clock, class _Duration>
593 __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
595 unique_lock<mutex> __lk(__mut_);
596 if (__state_ & deferred)
597 return future_status::deferred;
598 while (!(__state_ & ready) && _Clock::now() < __abs_time)
599 __cv_.wait_until(__lk, __abs_time);
600 if (__state_ & ready)
601 return future_status::ready;
602 return future_status::timeout;
605 template <class _Rep, class _Period>
608 __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
610 return wait_until(chrono::steady_clock::now() + __rel_time);
614 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state
615 : public __assoc_sub_state
617 typedef __assoc_sub_state base;
618 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
622 virtual void __on_zero_shared() _NOEXCEPT;
625 template <class _Arg>
626 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
627 void set_value(_Arg&& __arg);
629 void set_value(_Arg& __arg);
632 template <class _Arg>
633 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
634 void set_value_at_thread_exit(_Arg&& __arg);
636 void set_value_at_thread_exit(_Arg& __arg);
640 typename add_lvalue_reference<_Rp>::type copy();
645 __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
647 if (this->__state_ & base::__constructed)
648 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
653 template <class _Arg>
654 _LIBCPP_AVAILABILITY_FUTURE
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 _LIBCPP_AVAILABILITY_FUTURE __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 _LIBCPP_AVAILABILITY_FUTURE __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 _LIBCPP_AVAILABILITY_FUTURE __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 _LIBCPP_AVAILABILITY_FUTURE __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 _LIBCPP_AVAILABILITY_FUTURE __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 _LIBCPP_AVAILABILITY_FUTURE __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 _LIBCPP_AVAILABILITY_FUTURE __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 _LIBCPP_AVAILABILITY_FUTURE __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 _LIBCPP_AVAILABILITY_FUTURE 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 __state_->__attach_future();
1156 struct __release_shared_count
1158 void operator()(__shared_count* p) {p->__release_shared();}
1161 template <class _Rp>
1162 future<_Rp>::~future()
1165 __state_->__release_shared();
1168 template <class _Rp>
1172 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1173 __assoc_state<_Rp>* __s = __state_;
1178 template <class _Rp>
1179 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
1181 __assoc_state<_Rp&>* __state_;
1183 explicit future(__assoc_state<_Rp&>* __state);
1185 template <class> friend class promise;
1186 template <class> friend class shared_future;
1188 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1189 template <class _R1, class _Fp>
1190 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1191 template <class _R1, class _Fp>
1192 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1194 template <class _R1, class _Fp>
1195 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1196 template <class _R1, class _Fp>
1197 friend future<_R1> __make_async_assoc_state(_Fp __f);
1201 _LIBCPP_INLINE_VISIBILITY
1202 future() _NOEXCEPT : __state_(nullptr) {}
1203 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1204 _LIBCPP_INLINE_VISIBILITY
1205 future(future&& __rhs) _NOEXCEPT
1206 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1207 future(const future&) = delete;
1208 future& operator=(const future&) = delete;
1209 _LIBCPP_INLINE_VISIBILITY
1210 future& operator=(future&& __rhs) _NOEXCEPT
1212 future(std::move(__rhs)).swap(*this);
1215 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1217 future(const future&);
1218 future& operator=(const future&);
1220 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1222 _LIBCPP_INLINE_VISIBILITY
1223 shared_future<_Rp&> share() _NOEXCEPT;
1225 // retrieving the value
1228 _LIBCPP_INLINE_VISIBILITY
1229 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1231 // functions to check state
1232 _LIBCPP_INLINE_VISIBILITY
1233 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1235 _LIBCPP_INLINE_VISIBILITY
1236 void wait() const {__state_->wait();}
1237 template <class _Rep, class _Period>
1238 _LIBCPP_INLINE_VISIBILITY
1240 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1241 {return __state_->wait_for(__rel_time);}
1242 template <class _Clock, class _Duration>
1243 _LIBCPP_INLINE_VISIBILITY
1245 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1246 {return __state_->wait_until(__abs_time);}
1249 template <class _Rp>
1250 future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1253 __state_->__attach_future();
1256 template <class _Rp>
1257 future<_Rp&>::~future()
1260 __state_->__release_shared();
1263 template <class _Rp>
1267 unique_ptr<__shared_count, __release_shared_count> __(__state_);
1268 __assoc_state<_Rp&>* __s = __state_;
1274 class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE future<void>
1276 __assoc_sub_state* __state_;
1278 explicit future(__assoc_sub_state* __state);
1280 template <class> friend class promise;
1281 template <class> friend class shared_future;
1283 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1284 template <class _R1, class _Fp>
1285 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1286 template <class _R1, class _Fp>
1287 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1289 template <class _R1, class _Fp>
1290 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1291 template <class _R1, class _Fp>
1292 friend future<_R1> __make_async_assoc_state(_Fp __f);
1296 _LIBCPP_INLINE_VISIBILITY
1297 future() _NOEXCEPT : __state_(nullptr) {}
1298 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1299 _LIBCPP_INLINE_VISIBILITY
1300 future(future&& __rhs) _NOEXCEPT
1301 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1302 future(const future&) = delete;
1303 future& operator=(const future&) = delete;
1304 _LIBCPP_INLINE_VISIBILITY
1305 future& operator=(future&& __rhs) _NOEXCEPT
1307 future(std::move(__rhs)).swap(*this);
1310 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1312 future(const future&);
1313 future& operator=(const future&);
1315 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1317 _LIBCPP_INLINE_VISIBILITY
1318 shared_future<void> share() _NOEXCEPT;
1320 // retrieving the value
1323 _LIBCPP_INLINE_VISIBILITY
1324 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1326 // functions to check state
1327 _LIBCPP_INLINE_VISIBILITY
1328 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1330 _LIBCPP_INLINE_VISIBILITY
1331 void wait() const {__state_->wait();}
1332 template <class _Rep, class _Period>
1333 _LIBCPP_INLINE_VISIBILITY
1335 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1336 {return __state_->wait_for(__rel_time);}
1337 template <class _Clock, class _Duration>
1338 _LIBCPP_INLINE_VISIBILITY
1340 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1341 {return __state_->wait_until(__abs_time);}
1344 template <class _Rp>
1345 inline _LIBCPP_INLINE_VISIBILITY
1347 swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1354 template <class _Callable> class packaged_task;
1356 template <class _Rp>
1357 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
1359 __assoc_state<_Rp>* __state_;
1361 _LIBCPP_INLINE_VISIBILITY
1362 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1364 template <class> friend class packaged_task;
1367 template <class _Alloc>
1368 promise(allocator_arg_t, const _Alloc& __a);
1369 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1370 _LIBCPP_INLINE_VISIBILITY
1371 promise(promise&& __rhs) _NOEXCEPT
1372 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1373 promise(const promise& __rhs) = delete;
1374 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1376 promise(const promise& __rhs);
1378 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1382 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1383 _LIBCPP_INLINE_VISIBILITY
1384 promise& operator=(promise&& __rhs) _NOEXCEPT
1386 promise(std::move(__rhs)).swap(*this);
1389 promise& operator=(const promise& __rhs) = delete;
1390 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1392 promise& operator=(const promise& __rhs);
1394 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1395 _LIBCPP_INLINE_VISIBILITY
1396 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1398 // retrieving the result
1399 future<_Rp> get_future();
1401 // setting the result
1402 void set_value(const _Rp& __r);
1403 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1404 void set_value(_Rp&& __r);
1406 void set_exception(exception_ptr __p);
1408 // setting the result with deferred notification
1409 void set_value_at_thread_exit(const _Rp& __r);
1410 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1411 void set_value_at_thread_exit(_Rp&& __r);
1413 void set_exception_at_thread_exit(exception_ptr __p);
1416 template <class _Rp>
1417 promise<_Rp>::promise()
1418 : __state_(new __assoc_state<_Rp>)
1422 template <class _Rp>
1423 template <class _Alloc>
1424 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1426 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1427 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1428 typedef __allocator_destructor<_A2> _D2;
1430 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1431 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1432 __state_ = _VSTD::addressof(*__hold.release());
1435 template <class _Rp>
1436 promise<_Rp>::~promise()
1440 if (!__state_->__has_value() && __state_->use_count() > 1)
1441 __state_->set_exception(make_exception_ptr(
1442 future_error(make_error_code(future_errc::broken_promise))
1444 __state_->__release_shared();
1448 template <class _Rp>
1450 promise<_Rp>::get_future()
1452 if (__state_ == nullptr)
1453 __throw_future_error(future_errc::no_state);
1454 return future<_Rp>(__state_);
1457 template <class _Rp>
1459 promise<_Rp>::set_value(const _Rp& __r)
1461 if (__state_ == nullptr)
1462 __throw_future_error(future_errc::no_state);
1463 __state_->set_value(__r);
1466 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1468 template <class _Rp>
1470 promise<_Rp>::set_value(_Rp&& __r)
1472 if (__state_ == nullptr)
1473 __throw_future_error(future_errc::no_state);
1474 __state_->set_value(_VSTD::move(__r));
1477 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1479 template <class _Rp>
1481 promise<_Rp>::set_exception(exception_ptr __p)
1483 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
1484 if (__state_ == nullptr)
1485 __throw_future_error(future_errc::no_state);
1486 __state_->set_exception(__p);
1489 template <class _Rp>
1491 promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1493 if (__state_ == nullptr)
1494 __throw_future_error(future_errc::no_state);
1495 __state_->set_value_at_thread_exit(__r);
1498 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1500 template <class _Rp>
1502 promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1504 if (__state_ == nullptr)
1505 __throw_future_error(future_errc::no_state);
1506 __state_->set_value_at_thread_exit(_VSTD::move(__r));
1509 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1511 template <class _Rp>
1513 promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
1515 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1516 if (__state_ == nullptr)
1517 __throw_future_error(future_errc::no_state);
1518 __state_->set_exception_at_thread_exit(__p);
1523 template <class _Rp>
1524 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
1526 __assoc_state<_Rp&>* __state_;
1528 _LIBCPP_INLINE_VISIBILITY
1529 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1531 template <class> friend class packaged_task;
1535 template <class _Allocator>
1536 promise(allocator_arg_t, const _Allocator& __a);
1537 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1538 _LIBCPP_INLINE_VISIBILITY
1539 promise(promise&& __rhs) _NOEXCEPT
1540 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1541 promise(const promise& __rhs) = delete;
1542 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1544 promise(const promise& __rhs);
1546 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1550 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1551 _LIBCPP_INLINE_VISIBILITY
1552 promise& operator=(promise&& __rhs) _NOEXCEPT
1554 promise(std::move(__rhs)).swap(*this);
1557 promise& operator=(const promise& __rhs) = delete;
1558 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1560 promise& operator=(const promise& __rhs);
1562 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1563 _LIBCPP_INLINE_VISIBILITY
1564 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1566 // retrieving the result
1567 future<_Rp&> get_future();
1569 // setting the result
1570 void set_value(_Rp& __r);
1571 void set_exception(exception_ptr __p);
1573 // setting the result with deferred notification
1574 void set_value_at_thread_exit(_Rp&);
1575 void set_exception_at_thread_exit(exception_ptr __p);
1578 template <class _Rp>
1579 promise<_Rp&>::promise()
1580 : __state_(new __assoc_state<_Rp&>)
1584 template <class _Rp>
1585 template <class _Alloc>
1586 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1588 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1589 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1590 typedef __allocator_destructor<_A2> _D2;
1592 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1593 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1594 __state_ = _VSTD::addressof(*__hold.release());
1597 template <class _Rp>
1598 promise<_Rp&>::~promise()
1602 if (!__state_->__has_value() && __state_->use_count() > 1)
1603 __state_->set_exception(make_exception_ptr(
1604 future_error(make_error_code(future_errc::broken_promise))
1606 __state_->__release_shared();
1610 template <class _Rp>
1612 promise<_Rp&>::get_future()
1614 if (__state_ == nullptr)
1615 __throw_future_error(future_errc::no_state);
1616 return future<_Rp&>(__state_);
1619 template <class _Rp>
1621 promise<_Rp&>::set_value(_Rp& __r)
1623 if (__state_ == nullptr)
1624 __throw_future_error(future_errc::no_state);
1625 __state_->set_value(__r);
1628 template <class _Rp>
1630 promise<_Rp&>::set_exception(exception_ptr __p)
1632 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
1633 if (__state_ == nullptr)
1634 __throw_future_error(future_errc::no_state);
1635 __state_->set_exception(__p);
1638 template <class _Rp>
1640 promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1642 if (__state_ == nullptr)
1643 __throw_future_error(future_errc::no_state);
1644 __state_->set_value_at_thread_exit(__r);
1647 template <class _Rp>
1649 promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
1651 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1652 if (__state_ == nullptr)
1653 __throw_future_error(future_errc::no_state);
1654 __state_->set_exception_at_thread_exit(__p);
1660 class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<void>
1662 __assoc_sub_state* __state_;
1664 _LIBCPP_INLINE_VISIBILITY
1665 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1667 template <class> friend class packaged_task;
1671 template <class _Allocator>
1672 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1673 promise(allocator_arg_t, const _Allocator& __a);
1674 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1675 _LIBCPP_INLINE_VISIBILITY
1676 promise(promise&& __rhs) _NOEXCEPT
1677 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1678 promise(const promise& __rhs) = delete;
1679 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1681 promise(const promise& __rhs);
1683 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1687 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1688 _LIBCPP_INLINE_VISIBILITY
1689 promise& operator=(promise&& __rhs) _NOEXCEPT
1691 promise(std::move(__rhs)).swap(*this);
1694 promise& operator=(const promise& __rhs) = delete;
1695 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1697 promise& operator=(const promise& __rhs);
1699 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1700 _LIBCPP_INLINE_VISIBILITY
1701 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1703 // retrieving the result
1704 future<void> get_future();
1706 // setting the result
1708 void set_exception(exception_ptr __p);
1710 // setting the result with deferred notification
1711 void set_value_at_thread_exit();
1712 void set_exception_at_thread_exit(exception_ptr __p);
1715 template <class _Alloc>
1716 promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1718 typedef __assoc_sub_state_alloc<_Alloc> _State;
1719 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1720 typedef __allocator_destructor<_A2> _D2;
1722 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1723 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1724 __state_ = _VSTD::addressof(*__hold.release());
1727 template <class _Rp>
1728 inline _LIBCPP_INLINE_VISIBILITY
1730 swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1735 template <class _Rp, class _Alloc>
1736 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
1737 : public true_type {};
1739 #ifndef _LIBCPP_HAS_NO_VARIADICS
1743 template<class _Fp> class __packaged_task_base;
1745 template<class _Rp, class ..._ArgTypes>
1746 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
1748 __packaged_task_base(const __packaged_task_base&);
1749 __packaged_task_base& operator=(const __packaged_task_base&);
1751 _LIBCPP_INLINE_VISIBILITY
1752 __packaged_task_base() {}
1753 _LIBCPP_INLINE_VISIBILITY
1754 virtual ~__packaged_task_base() {}
1755 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1756 virtual void destroy() = 0;
1757 virtual void destroy_deallocate() = 0;
1758 virtual _Rp operator()(_ArgTypes&& ...) = 0;
1761 template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1763 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1764 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1765 : public __packaged_task_base<_Rp(_ArgTypes...)>
1767 __compressed_pair<_Fp, _Alloc> __f_;
1769 _LIBCPP_INLINE_VISIBILITY
1770 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
1771 _LIBCPP_INLINE_VISIBILITY
1772 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
1773 _LIBCPP_INLINE_VISIBILITY
1774 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
1776 _LIBCPP_INLINE_VISIBILITY
1777 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
1778 : __f_(_VSTD::move(__f), __a) {}
1779 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1780 virtual void destroy();
1781 virtual void destroy_deallocate();
1782 virtual _Rp operator()(_ArgTypes&& ... __args);
1785 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1787 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1788 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1790 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1793 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1795 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1797 __f_.~__compressed_pair<_Fp, _Alloc>();
1800 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1802 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
1804 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1805 typedef allocator_traits<_Ap> _ATraits;
1806 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1807 _Ap __a(__f_.second());
1808 __f_.~__compressed_pair<_Fp, _Alloc>();
1809 __a.deallocate(_PTraits::pointer_to(*this), 1);
1812 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1814 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1816 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1819 template <class _Callable> class __packaged_task_function;
1821 template<class _Rp, class ..._ArgTypes>
1822 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
1824 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1825 typename aligned_storage<3*sizeof(void*)>::type __buf_;
1829 typedef _Rp result_type;
1831 // construct/copy/destroy:
1832 _LIBCPP_INLINE_VISIBILITY
1833 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1835 __packaged_task_function(_Fp&& __f);
1836 template<class _Fp, class _Alloc>
1837 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1839 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1840 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1842 __packaged_task_function(const __packaged_task_function&) = delete;
1843 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1845 ~__packaged_task_function();
1847 void swap(__packaged_task_function&) _NOEXCEPT;
1849 _LIBCPP_INLINE_VISIBILITY
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<typename decay<_Fp>::type>::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 typename remove_reference<typename decay<_Fp>::type>::type _FR;
1900 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1901 if (sizeof(_FF) <= sizeof(__buf_))
1903 __f_ = (__base*)&__buf_;
1904 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
1908 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1910 typedef __allocator_destructor<_Ap> _Dp;
1911 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1912 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1913 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1914 __f_ = _VSTD::addressof(*__hold.release());
1918 template<class _Rp, class ..._ArgTypes>
1919 __packaged_task_function<_Rp(_ArgTypes...)>&
1920 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1922 if (__f_ == (__base*)&__buf_)
1925 __f_->destroy_deallocate();
1927 if (__f.__f_ == nullptr)
1929 else if (__f.__f_ == (__base*)&__f.__buf_)
1931 __f_ = (__base*)&__buf_;
1932 __f.__f_->__move_to(__f_);
1942 template<class _Rp, class ..._ArgTypes>
1943 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1945 if (__f_ == (__base*)&__buf_)
1948 __f_->destroy_deallocate();
1951 template<class _Rp, class ..._ArgTypes>
1953 __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1955 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1957 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1958 __base* __t = (__base*)&__tempbuf;
1959 __f_->__move_to(__t);
1962 __f.__f_->__move_to((__base*)&__buf_);
1963 __f.__f_->destroy();
1965 __f_ = (__base*)&__buf_;
1966 __t->__move_to((__base*)&__f.__buf_);
1968 __f.__f_ = (__base*)&__f.__buf_;
1970 else if (__f_ == (__base*)&__buf_)
1972 __f_->__move_to((__base*)&__f.__buf_);
1975 __f.__f_ = (__base*)&__f.__buf_;
1977 else if (__f.__f_ == (__base*)&__f.__buf_)
1979 __f.__f_->__move_to((__base*)&__buf_);
1980 __f.__f_->destroy();
1982 __f_ = (__base*)&__buf_;
1985 _VSTD::swap(__f_, __f.__f_);
1988 template<class _Rp, class ..._ArgTypes>
1991 __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1993 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
1996 template<class _Rp, class ..._ArgTypes>
1997 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
2000 typedef _Rp result_type; // extension
2003 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2004 promise<result_type> __p_;
2007 // construction and destruction
2008 _LIBCPP_INLINE_VISIBILITY
2009 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2010 template <class _Fp,
2011 class = typename enable_if
2014 typename __uncvref<_Fp>::type,
2019 _LIBCPP_INLINE_VISIBILITY
2020 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2021 template <class _Fp, class _Allocator,
2022 class = typename enable_if
2025 typename __uncvref<_Fp>::type,
2030 _LIBCPP_INLINE_VISIBILITY
2031 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2032 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2033 __p_(allocator_arg, __a) {}
2034 // ~packaged_task() = default;
2037 packaged_task(const packaged_task&) = delete;
2038 packaged_task& operator=(const packaged_task&) = delete;
2041 _LIBCPP_INLINE_VISIBILITY
2042 packaged_task(packaged_task&& __other) _NOEXCEPT
2043 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2044 _LIBCPP_INLINE_VISIBILITY
2045 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2047 __f_ = _VSTD::move(__other.__f_);
2048 __p_ = _VSTD::move(__other.__p_);
2051 _LIBCPP_INLINE_VISIBILITY
2052 void swap(packaged_task& __other) _NOEXCEPT
2054 __f_.swap(__other.__f_);
2055 __p_.swap(__other.__p_);
2058 _LIBCPP_INLINE_VISIBILITY
2059 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2062 _LIBCPP_INLINE_VISIBILITY
2063 future<result_type> get_future() {return __p_.get_future();}
2066 void operator()(_ArgTypes... __args);
2067 void make_ready_at_thread_exit(_ArgTypes... __args);
2072 template<class _Rp, class ..._ArgTypes>
2074 packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
2076 if (__p_.__state_ == nullptr)
2077 __throw_future_error(future_errc::no_state);
2078 if (__p_.__state_->__has_value())
2079 __throw_future_error(future_errc::promise_already_satisfied);
2080 #ifndef _LIBCPP_NO_EXCEPTIONS
2083 #endif // _LIBCPP_NO_EXCEPTIONS
2084 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2085 #ifndef _LIBCPP_NO_EXCEPTIONS
2089 __p_.set_exception(current_exception());
2091 #endif // _LIBCPP_NO_EXCEPTIONS
2094 template<class _Rp, class ..._ArgTypes>
2096 packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2098 if (__p_.__state_ == nullptr)
2099 __throw_future_error(future_errc::no_state);
2100 if (__p_.__state_->__has_value())
2101 __throw_future_error(future_errc::promise_already_satisfied);
2102 #ifndef _LIBCPP_NO_EXCEPTIONS
2105 #endif // _LIBCPP_NO_EXCEPTIONS
2106 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
2107 #ifndef _LIBCPP_NO_EXCEPTIONS
2111 __p_.set_exception_at_thread_exit(current_exception());
2113 #endif // _LIBCPP_NO_EXCEPTIONS
2116 template<class _Rp, class ..._ArgTypes>
2118 packaged_task<_Rp(_ArgTypes...)>::reset()
2121 __throw_future_error(future_errc::no_state);
2122 __p_ = promise<result_type>();
2125 template<class ..._ArgTypes>
2126 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
2129 typedef void result_type; // extension
2132 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2133 promise<result_type> __p_;
2136 // construction and destruction
2137 _LIBCPP_INLINE_VISIBILITY
2138 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2139 template <class _Fp,
2140 class = typename enable_if
2143 typename __uncvref<_Fp>::type,
2148 _LIBCPP_INLINE_VISIBILITY
2149 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2150 template <class _Fp, class _Allocator,
2151 class = typename enable_if
2154 typename __uncvref<_Fp>::type,
2159 _LIBCPP_INLINE_VISIBILITY
2160 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2161 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2162 __p_(allocator_arg, __a) {}
2163 // ~packaged_task() = default;
2166 packaged_task(const packaged_task&) = delete;
2167 packaged_task& operator=(const packaged_task&) = delete;
2170 _LIBCPP_INLINE_VISIBILITY
2171 packaged_task(packaged_task&& __other) _NOEXCEPT
2172 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2173 _LIBCPP_INLINE_VISIBILITY
2174 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2176 __f_ = _VSTD::move(__other.__f_);
2177 __p_ = _VSTD::move(__other.__p_);
2180 _LIBCPP_INLINE_VISIBILITY
2181 void swap(packaged_task& __other) _NOEXCEPT
2183 __f_.swap(__other.__f_);
2184 __p_.swap(__other.__p_);
2187 _LIBCPP_INLINE_VISIBILITY
2188 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2191 _LIBCPP_INLINE_VISIBILITY
2192 future<result_type> get_future() {return __p_.get_future();}
2195 void operator()(_ArgTypes... __args);
2196 void make_ready_at_thread_exit(_ArgTypes... __args);
2201 template<class ..._ArgTypes>
2203 packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2205 if (__p_.__state_ == nullptr)
2206 __throw_future_error(future_errc::no_state);
2207 if (__p_.__state_->__has_value())
2208 __throw_future_error(future_errc::promise_already_satisfied);
2209 #ifndef _LIBCPP_NO_EXCEPTIONS
2212 #endif // _LIBCPP_NO_EXCEPTIONS
2213 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2215 #ifndef _LIBCPP_NO_EXCEPTIONS
2219 __p_.set_exception(current_exception());
2221 #endif // _LIBCPP_NO_EXCEPTIONS
2224 template<class ..._ArgTypes>
2226 packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2228 if (__p_.__state_ == nullptr)
2229 __throw_future_error(future_errc::no_state);
2230 if (__p_.__state_->__has_value())
2231 __throw_future_error(future_errc::promise_already_satisfied);
2232 #ifndef _LIBCPP_NO_EXCEPTIONS
2235 #endif // _LIBCPP_NO_EXCEPTIONS
2236 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2237 __p_.set_value_at_thread_exit();
2238 #ifndef _LIBCPP_NO_EXCEPTIONS
2242 __p_.set_exception_at_thread_exit(current_exception());
2244 #endif // _LIBCPP_NO_EXCEPTIONS
2247 template<class ..._ArgTypes>
2249 packaged_task<void(_ArgTypes...)>::reset()
2252 __throw_future_error(future_errc::no_state);
2253 __p_ = promise<result_type>();
2256 template <class _Callable>
2257 inline _LIBCPP_INLINE_VISIBILITY
2259 swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
2264 template <class _Callable, class _Alloc>
2265 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
2266 : public true_type {};
2268 template <class _Rp, class _Fp>
2270 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2271 __make_deferred_assoc_state(_Fp&& __f)
2273 __make_deferred_assoc_state(_Fp __f)
2276 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2277 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2278 return future<_Rp>(__h.get());
2281 template <class _Rp, class _Fp>
2283 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2284 __make_async_assoc_state(_Fp&& __f)
2286 __make_async_assoc_state(_Fp __f)
2289 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2290 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2291 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2292 return future<_Rp>(__h.get());
2295 template <class _Fp, class... _Args>
2298 tuple<_Fp, _Args...> __f_;
2301 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2303 _LIBCPP_INLINE_VISIBILITY
2304 explicit __async_func(_Fp&& __f, _Args&&... __args)
2305 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2307 _LIBCPP_INLINE_VISIBILITY
2308 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2312 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2313 return __execute(_Index());
2316 template <size_t ..._Indices>
2318 __execute(__tuple_indices<_Indices...>)
2320 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2324 inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2325 { return (int(__policy) & int(__value)) != 0; }
2327 template <class _Fp, class... _Args>
2328 _LIBCPP_NODISCARD_AFTER_CXX17
2329 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2330 async(launch __policy, _Fp&& __f, _Args&&... __args)
2332 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2333 typedef typename _BF::_Rp _Rp;
2335 #ifndef _LIBCPP_NO_EXCEPTIONS
2339 if (__does_policy_contain(__policy, launch::async))
2340 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2341 __decay_copy(_VSTD::forward<_Args>(__args))...));
2342 #ifndef _LIBCPP_NO_EXCEPTIONS
2344 catch ( ... ) { if (__policy == launch::async) throw ; }
2347 if (__does_policy_contain(__policy, launch::deferred))
2348 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
2349 __decay_copy(_VSTD::forward<_Args>(__args))...));
2350 return future<_Rp>{};
2353 template <class _Fp, class... _Args>
2354 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
2355 future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2356 async(_Fp&& __f, _Args&&... __args)
2358 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2359 _VSTD::forward<_Args>(__args)...);
2362 #endif // _LIBCPP_HAS_NO_VARIADICS
2366 template <class _Rp>
2367 class _LIBCPP_TEMPLATE_VIS shared_future
2369 __assoc_state<_Rp>* __state_;
2372 _LIBCPP_INLINE_VISIBILITY
2373 shared_future() _NOEXCEPT : __state_(nullptr) {}
2374 _LIBCPP_INLINE_VISIBILITY
2375 shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2376 {if (__state_) __state_->__add_shared();}
2377 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2378 _LIBCPP_INLINE_VISIBILITY
2379 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
2380 {__f.__state_ = nullptr;}
2381 _LIBCPP_INLINE_VISIBILITY
2382 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2383 {__rhs.__state_ = nullptr;}
2384 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2386 shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
2387 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2388 _LIBCPP_INLINE_VISIBILITY
2389 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2391 shared_future(std::move(__rhs)).swap(*this);
2394 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2396 // retrieving the value
2397 _LIBCPP_INLINE_VISIBILITY
2398 const _Rp& get() const {return __state_->copy();}
2400 _LIBCPP_INLINE_VISIBILITY
2401 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2403 // functions to check state
2404 _LIBCPP_INLINE_VISIBILITY
2405 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2407 _LIBCPP_INLINE_VISIBILITY
2408 void wait() const {__state_->wait();}
2409 template <class _Rep, class _Period>
2410 _LIBCPP_INLINE_VISIBILITY
2412 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2413 {return __state_->wait_for(__rel_time);}
2414 template <class _Clock, class _Duration>
2415 _LIBCPP_INLINE_VISIBILITY
2417 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2418 {return __state_->wait_until(__abs_time);}
2421 template <class _Rp>
2422 shared_future<_Rp>::~shared_future()
2425 __state_->__release_shared();
2428 template <class _Rp>
2430 shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
2433 __rhs.__state_->__add_shared();
2435 __state_->__release_shared();
2436 __state_ = __rhs.__state_;
2440 template <class _Rp>
2441 class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
2443 __assoc_state<_Rp&>* __state_;
2446 _LIBCPP_INLINE_VISIBILITY
2447 shared_future() _NOEXCEPT : __state_(nullptr) {}
2448 _LIBCPP_INLINE_VISIBILITY
2449 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2450 {if (__state_) __state_->__add_shared();}
2451 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2452 _LIBCPP_INLINE_VISIBILITY
2453 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
2454 {__f.__state_ = nullptr;}
2455 _LIBCPP_INLINE_VISIBILITY
2456 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2457 {__rhs.__state_ = nullptr;}
2458 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2460 shared_future& operator=(const shared_future& __rhs);
2461 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2462 _LIBCPP_INLINE_VISIBILITY
2463 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2465 shared_future(std::move(__rhs)).swap(*this);
2468 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2470 // retrieving the value
2471 _LIBCPP_INLINE_VISIBILITY
2472 _Rp& get() const {return __state_->copy();}
2474 _LIBCPP_INLINE_VISIBILITY
2475 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2477 // functions to check state
2478 _LIBCPP_INLINE_VISIBILITY
2479 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2481 _LIBCPP_INLINE_VISIBILITY
2482 void wait() const {__state_->wait();}
2483 template <class _Rep, class _Period>
2484 _LIBCPP_INLINE_VISIBILITY
2486 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2487 {return __state_->wait_for(__rel_time);}
2488 template <class _Clock, class _Duration>
2489 _LIBCPP_INLINE_VISIBILITY
2491 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2492 {return __state_->wait_until(__abs_time);}
2495 template <class _Rp>
2496 shared_future<_Rp&>::~shared_future()
2499 __state_->__release_shared();
2502 template <class _Rp>
2503 shared_future<_Rp&>&
2504 shared_future<_Rp&>::operator=(const shared_future& __rhs)
2507 __rhs.__state_->__add_shared();
2509 __state_->__release_shared();
2510 __state_ = __rhs.__state_;
2515 class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
2517 __assoc_sub_state* __state_;
2520 _LIBCPP_INLINE_VISIBILITY
2521 shared_future() _NOEXCEPT : __state_(nullptr) {}
2522 _LIBCPP_INLINE_VISIBILITY
2523 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2524 {if (__state_) __state_->__add_shared();}
2525 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2526 _LIBCPP_INLINE_VISIBILITY
2527 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
2528 {__f.__state_ = nullptr;}
2529 _LIBCPP_INLINE_VISIBILITY
2530 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2531 {__rhs.__state_ = nullptr;}
2532 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2534 shared_future& operator=(const shared_future& __rhs);
2535 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2536 _LIBCPP_INLINE_VISIBILITY
2537 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2539 shared_future(std::move(__rhs)).swap(*this);
2542 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2544 // retrieving the value
2545 _LIBCPP_INLINE_VISIBILITY
2546 void get() const {__state_->copy();}
2548 _LIBCPP_INLINE_VISIBILITY
2549 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2551 // functions to check state
2552 _LIBCPP_INLINE_VISIBILITY
2553 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2555 _LIBCPP_INLINE_VISIBILITY
2556 void wait() const {__state_->wait();}
2557 template <class _Rep, class _Period>
2558 _LIBCPP_INLINE_VISIBILITY
2560 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2561 {return __state_->wait_for(__rel_time);}
2562 template <class _Clock, class _Duration>
2563 _LIBCPP_INLINE_VISIBILITY
2565 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2566 {return __state_->wait_until(__abs_time);}
2569 template <class _Rp>
2570 inline _LIBCPP_INLINE_VISIBILITY
2572 swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2577 template <class _Rp>
2580 future<_Rp>::share() _NOEXCEPT
2582 return shared_future<_Rp>(_VSTD::move(*this));
2585 template <class _Rp>
2588 future<_Rp&>::share() _NOEXCEPT
2590 return shared_future<_Rp&>(_VSTD::move(*this));
2593 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2597 future<void>::share() _NOEXCEPT
2599 return shared_future<void>(_VSTD::move(*this));
2602 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2604 _LIBCPP_END_NAMESPACE_STD
2606 #endif // !_LIBCPP_HAS_NO_THREADS
2608 #endif // _LIBCPP_FUTURE