2 //===----------------------------------------------------------------------===//
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;
47 class future_error : public logic_error {
49 explicit future_error(future_errc e); // since C++17
51 const error_code& code() const noexcept;
52 const char* what() const noexcept;
55 error_code ec_; // exposition only
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
65 promise(promise&& rhs) noexcept;
66 promise(const promise& rhs) = delete;
70 promise& operator=(promise&& rhs) noexcept;
71 promise& operator=(const promise& rhs) = delete;
72 void swap(promise& other) noexcept;
74 // retrieving the result
75 future<R> get_future();
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
95 promise(promise&& rhs) noexcept;
96 promise(const promise& rhs) = delete;
100 promise& operator=(promise&& rhs) noexcept;
101 promise& operator=(const promise& rhs) = delete;
102 void swap(promise& other) noexcept;
104 // retrieving the result
105 future<R&> get_future();
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
123 promise(promise&& rhs) noexcept;
124 promise(const promise& rhs) = delete;
128 promise& operator=(promise&& rhs) noexcept;
129 promise& operator=(const promise& rhs) = delete;
130 void swap(promise& other) noexcept;
132 // retrieving the result
133 future<void> get_future();
135 // setting the result
137 void set_exception(exception_ptr p);
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
144 template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
146 template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
154 future(future&&) noexcept;
155 future(const future& rhs) = delete;
157 future& operator=(const future& rhs) = delete;
158 future& operator=(future&&) noexcept;
159 shared_future<R> share() noexcept;
161 // retrieving the value
164 // functions to check state
165 bool valid() const noexcept;
168 template <class Rep, class Period>
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
181 future(future&&) noexcept;
182 future(const future& rhs) = delete;
184 future& operator=(const future& rhs) = delete;
185 future& operator=(future&&) noexcept;
186 shared_future<R&> share() noexcept;
188 // retrieving the value
191 // functions to check state
192 bool valid() const noexcept;
195 template <class Rep, class Period>
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
208 future(future&&) noexcept;
209 future(const future& rhs) = delete;
211 future& operator=(const future& rhs) = delete;
212 future& operator=(future&&) noexcept;
213 shared_future<void> share() noexcept;
215 // retrieving the value
218 // functions to check state
219 bool valid() const noexcept;
222 template <class Rep, class Period>
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
234 shared_future() noexcept;
235 shared_future(const shared_future& rhs);
236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
239 shared_future& operator=(const shared_future& rhs);
240 shared_future& operator=(shared_future&& rhs) noexcept;
242 // retrieving the value
243 const R& get() const;
245 // functions to check state
246 bool valid() const noexcept;
249 template <class Rep, class Period>
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
258 class shared_future<R&>
261 shared_future() noexcept;
262 shared_future(const shared_future& rhs);
263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
266 shared_future& operator=(const shared_future& rhs);
267 shared_future& operator=(shared_future&& rhs) noexcept;
269 // retrieving the value
272 // functions to check state
273 bool valid() const noexcept;
276 template <class Rep, class Period>
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
285 class shared_future<void>
288 shared_future() noexcept;
289 shared_future(const shared_future& rhs);
290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
293 shared_future& operator=(const shared_future& rhs);
294 shared_future& operator=(shared_future&& rhs) noexcept;
296 // retrieving the value
299 // functions to check state
300 bool valid() const noexcept;
303 template <class Rep, class Period>
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
311 template <class F, class... Args>
312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
313 async(F&& f, Args&&... args);
315 template <class F, class... Args>
316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
317 async(launch policy, F&& f, Args&&... args);
319 template <class> class packaged_task; // undefined
321 template <class R, class... ArgTypes>
322 class packaged_task<R(ArgTypes...)>
325 typedef R result_type; // extension
327 // construction and destruction
328 packaged_task() noexcept;
330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
344 bool valid() const noexcept;
347 future<R> get_future();
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
359 template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
365 #include <__assert> // all public C++ headers provide the assertion handler
366 #include <__availability>
367 #include <__chrono/duration.h>
368 #include <__chrono/time_point.h>
370 #include <__exception/exception_ptr.h>
371 #include <__memory/addressof.h>
372 #include <__memory/allocator.h>
373 #include <__memory/allocator_arg_t.h>
374 #include <__memory/allocator_destructor.h>
375 #include <__memory/allocator_traits.h>
376 #include <__memory/compressed_pair.h>
377 #include <__memory/pointer_traits.h>
378 #include <__memory/shared_ptr.h>
379 #include <__memory/unique_ptr.h>
380 #include <__memory/uses_allocator.h>
381 #include <__system_error/error_category.h>
382 #include <__system_error/error_code.h>
383 #include <__system_error/error_condition.h>
384 #include <__type_traits/aligned_storage.h>
385 #include <__type_traits/strip_signature.h>
386 #include <__utility/auto_cast.h>
387 #include <__utility/forward.h>
388 #include <__utility/move.h>
395 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
396 # pragma GCC system_header
399 #ifdef _LIBCPP_HAS_NO_THREADS
400 # error "<future> is not supported since libc++ has been configured without support for threads."
403 _LIBCPP_BEGIN_NAMESPACE_STD
405 //enum class future_errc
406 _LIBCPP_DECLARE_STRONG_ENUM(future_errc)
408 future_already_retrieved = 1,
409 promise_already_satisfied,
413 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
416 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
418 #ifdef _LIBCPP_CXX03_LANG
420 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
424 _LIBCPP_DECLARE_STRONG_ENUM(launch)
428 any = async | deferred
430 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
432 #ifndef _LIBCPP_CXX03_LANG
434 typedef underlying_type<launch>::type __launch_underlying_type;
436 inline _LIBCPP_INLINE_VISIBILITY
439 operator&(launch __x, launch __y)
441 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
442 static_cast<__launch_underlying_type>(__y));
445 inline _LIBCPP_INLINE_VISIBILITY
448 operator|(launch __x, launch __y)
450 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
451 static_cast<__launch_underlying_type>(__y));
454 inline _LIBCPP_INLINE_VISIBILITY
457 operator^(launch __x, launch __y)
459 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
460 static_cast<__launch_underlying_type>(__y));
463 inline _LIBCPP_INLINE_VISIBILITY
466 operator~(launch __x)
468 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
471 inline _LIBCPP_INLINE_VISIBILITY
473 operator&=(launch& __x, launch __y)
475 __x = __x & __y; return __x;
478 inline _LIBCPP_INLINE_VISIBILITY
480 operator|=(launch& __x, launch __y)
482 __x = __x | __y; return __x;
485 inline _LIBCPP_INLINE_VISIBILITY
487 operator^=(launch& __x, launch __y)
489 __x = __x ^ __y; return __x;
492 #endif // !_LIBCPP_CXX03_LANG
494 //enum class future_status
495 _LIBCPP_DECLARE_STRONG_ENUM(future_status)
501 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
503 _LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
505 inline _LIBCPP_INLINE_VISIBILITY
507 make_error_code(future_errc __e) _NOEXCEPT
509 return error_code(static_cast<int>(__e), future_category());
512 inline _LIBCPP_INLINE_VISIBILITY
514 make_error_condition(future_errc __e) _NOEXCEPT
516 return error_condition(static_cast<int>(__e), future_category());
519 _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
520 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
521 _LIBCPP_AVAILABILITY_FUTURE_ERROR
523 void __throw_future_error(future_errc __ev);
525 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
530 future_error(error_code);
531 friend void __throw_future_error(future_errc);
532 template <class> friend class promise;
535 #if _LIBCPP_STD_VER >= 17
536 _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
539 _LIBCPP_INLINE_VISIBILITY
540 const error_code& code() const _NOEXCEPT {return __ec_;}
542 _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
543 ~future_error() _NOEXCEPT override;
546 // Declared above std::future_error
547 void __throw_future_error(future_errc __ev)
549 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
550 throw future_error(make_error_code(__ev));
553 _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
557 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
558 : public __shared_count
561 exception_ptr __exception_;
562 mutable mutex __mut_;
563 mutable condition_variable __cv_;
566 void __on_zero_shared() _NOEXCEPT override;
567 void __sub_wait(unique_lock<mutex>& __lk);
572 __future_attached = 2,
577 _LIBCPP_INLINE_VISIBILITY
578 __assoc_sub_state() : __state_(0) {}
580 _LIBCPP_INLINE_VISIBILITY
581 bool __has_value() const
582 {return (__state_ & __constructed) || (__exception_ != nullptr);}
584 _LIBCPP_INLINE_VISIBILITY
585 void __attach_future() {
586 lock_guard<mutex> __lk(__mut_);
587 bool __has_future_attached = (__state_ & __future_attached) != 0;
588 if (__has_future_attached)
589 __throw_future_error(future_errc::future_already_retrieved);
590 this->__add_shared();
591 __state_ |= __future_attached;
594 _LIBCPP_INLINE_VISIBILITY
595 void __set_deferred() {__state_ |= deferred;}
598 _LIBCPP_INLINE_VISIBILITY
599 bool __is_ready() const {return (__state_ & ready) != 0;}
602 void set_value_at_thread_exit();
604 void set_exception(exception_ptr __p);
605 void set_exception_at_thread_exit(exception_ptr __p);
610 template <class _Rep, class _Period>
612 _LIBCPP_INLINE_VISIBILITY
613 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
614 template <class _Clock, class _Duration>
615 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
617 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
619 virtual void __execute();
622 template <class _Clock, class _Duration>
624 __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
626 unique_lock<mutex> __lk(__mut_);
627 if (__state_ & deferred)
628 return future_status::deferred;
629 while (!(__state_ & ready) && _Clock::now() < __abs_time)
630 __cv_.wait_until(__lk, __abs_time);
631 if (__state_ & ready)
632 return future_status::ready;
633 return future_status::timeout;
636 template <class _Rep, class _Period>
639 __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
641 return wait_until(chrono::steady_clock::now() + __rel_time);
645 class _LIBCPP_AVAILABILITY_FUTURE _LIBCPP_HIDDEN __assoc_state
646 : public __assoc_sub_state
648 typedef __assoc_sub_state base;
649 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
650 typedef typename aligned_storage<sizeof(_Rp), _LIBCPP_ALIGNOF(_Rp)>::type _Up;
651 _LIBCPP_SUPPRESS_DEPRECATED_POP
655 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
658 template <class _Arg>
659 _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
661 template <class _Arg>
662 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
664 _LIBCPP_HIDE_FROM_ABI _Rp move();
665 _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<_Rp> copy();
670 __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
672 if (this->__state_ & base::__constructed)
673 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
678 template <class _Arg>
679 _LIBCPP_AVAILABILITY_FUTURE
681 __assoc_state<_Rp>::set_value(_Arg&& __arg)
683 unique_lock<mutex> __lk(this->__mut_);
684 if (this->__has_value())
685 __throw_future_error(future_errc::promise_already_satisfied);
686 ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
687 this->__state_ |= base::__constructed | base::ready;
692 template <class _Arg>
694 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
696 unique_lock<mutex> __lk(this->__mut_);
697 if (this->__has_value())
698 __throw_future_error(future_errc::promise_already_satisfied);
699 ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
700 this->__state_ |= base::__constructed;
701 __thread_local_data()->__make_ready_at_thread_exit(this);
706 __assoc_state<_Rp>::move()
708 unique_lock<mutex> __lk(this->__mut_);
709 this->__sub_wait(__lk);
710 if (this->__exception_ != nullptr)
711 std::rethrow_exception(this->__exception_);
712 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
716 __add_lvalue_reference_t<_Rp>
717 __assoc_state<_Rp>::copy()
719 unique_lock<mutex> __lk(this->__mut_);
720 this->__sub_wait(__lk);
721 if (this->__exception_ != nullptr)
722 std::rethrow_exception(this->__exception_);
723 return *reinterpret_cast<_Rp*>(&__value_);
727 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
728 : public __assoc_sub_state
730 typedef __assoc_sub_state base;
735 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
738 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
739 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
741 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
746 __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
753 __assoc_state<_Rp&>::set_value(_Rp& __arg)
755 unique_lock<mutex> __lk(this->__mut_);
756 if (this->__has_value())
757 __throw_future_error(future_errc::promise_already_satisfied);
758 __value_ = _VSTD::addressof(__arg);
759 this->__state_ |= base::__constructed | base::ready;
765 __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
767 unique_lock<mutex> __lk(this->__mut_);
768 if (this->__has_value())
769 __throw_future_error(future_errc::promise_already_satisfied);
770 __value_ = _VSTD::addressof(__arg);
771 this->__state_ |= base::__constructed;
772 __thread_local_data()->__make_ready_at_thread_exit(this);
777 __assoc_state<_Rp&>::copy()
779 unique_lock<mutex> __lk(this->__mut_);
780 this->__sub_wait(__lk);
781 if (this->__exception_ != nullptr)
782 std::rethrow_exception(this->__exception_);
786 template <class _Rp, class _Alloc>
787 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
788 : public __assoc_state<_Rp>
790 typedef __assoc_state<_Rp> base;
793 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
795 _LIBCPP_INLINE_VISIBILITY
796 explicit __assoc_state_alloc(const _Alloc& __a)
800 template <class _Rp, class _Alloc>
802 __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
804 if (this->__state_ & base::__constructed)
805 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
806 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
807 typedef allocator_traits<_Al> _ATraits;
808 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
810 this->~__assoc_state_alloc();
811 __a.deallocate(_PTraits::pointer_to(*this), 1);
814 template <class _Rp, class _Alloc>
815 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
816 : public __assoc_state<_Rp&>
818 typedef __assoc_state<_Rp&> base;
821 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
823 _LIBCPP_INLINE_VISIBILITY
824 explicit __assoc_state_alloc(const _Alloc& __a)
828 template <class _Rp, class _Alloc>
830 __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
832 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
833 typedef allocator_traits<_Al> _ATraits;
834 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
836 this->~__assoc_state_alloc();
837 __a.deallocate(_PTraits::pointer_to(*this), 1);
840 template <class _Alloc>
841 class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
842 : public __assoc_sub_state
844 typedef __assoc_sub_state base;
847 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
849 _LIBCPP_INLINE_VISIBILITY
850 explicit __assoc_sub_state_alloc(const _Alloc& __a)
854 template <class _Alloc>
856 __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
858 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
859 typedef allocator_traits<_Al> _ATraits;
860 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
862 this->~__assoc_sub_state_alloc();
863 __a.deallocate(_PTraits::pointer_to(*this), 1);
866 template <class _Rp, class _Fp>
867 class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
868 : public __assoc_state<_Rp>
870 typedef __assoc_state<_Rp> base;
875 _LIBCPP_INLINE_VISIBILITY
876 explicit __deferred_assoc_state(_Fp&& __f);
878 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
881 template <class _Rp, class _Fp>
883 __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
884 : __func_(_VSTD::forward<_Fp>(__f))
886 this->__set_deferred();
889 template <class _Rp, class _Fp>
891 __deferred_assoc_state<_Rp, _Fp>::__execute()
893 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
896 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
897 this->set_value(__func_());
898 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
902 this->set_exception(current_exception());
904 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
908 class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
909 : public __assoc_sub_state
911 typedef __assoc_sub_state base;
916 _LIBCPP_INLINE_VISIBILITY
917 explicit __deferred_assoc_state(_Fp&& __f);
919 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
924 __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
925 : __func_(_VSTD::forward<_Fp>(__f))
927 this->__set_deferred();
932 __deferred_assoc_state<void, _Fp>::__execute()
934 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
937 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
940 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
944 this->set_exception(current_exception());
946 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
949 template <class _Rp, class _Fp>
950 class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
951 : public __assoc_state<_Rp>
953 typedef __assoc_state<_Rp> base;
957 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
959 _LIBCPP_INLINE_VISIBILITY
960 explicit __async_assoc_state(_Fp&& __f);
962 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
965 template <class _Rp, class _Fp>
967 __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
968 : __func_(_VSTD::forward<_Fp>(__f))
972 template <class _Rp, class _Fp>
974 __async_assoc_state<_Rp, _Fp>::__execute()
976 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
979 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
980 this->set_value(__func_());
981 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
985 this->set_exception(current_exception());
987 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
990 template <class _Rp, class _Fp>
992 __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
995 base::__on_zero_shared();
999 class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
1000 : public __assoc_sub_state
1002 typedef __assoc_sub_state base;
1006 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
1008 _LIBCPP_INLINE_VISIBILITY
1009 explicit __async_assoc_state(_Fp&& __f);
1011 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
1014 template <class _Fp>
1016 __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1017 : __func_(_VSTD::forward<_Fp>(__f))
1021 template <class _Fp>
1023 __async_assoc_state<void, _Fp>::__execute()
1025 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1028 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1031 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1035 this->set_exception(current_exception());
1037 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1040 template <class _Fp>
1042 __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1045 base::__on_zero_shared();
1048 template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1049 template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
1053 template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
1055 template <class _Rp, class _Fp>
1056 _LIBCPP_INLINE_VISIBILITY future<_Rp>
1057 __make_deferred_assoc_state(_Fp&& __f);
1059 template <class _Rp, class _Fp>
1060 _LIBCPP_INLINE_VISIBILITY future<_Rp>
1061 __make_async_assoc_state(_Fp&& __f);
1063 template <class _Rp>
1064 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
1066 __assoc_state<_Rp>* __state_;
1068 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
1070 template <class> friend class promise;
1071 template <class> friend class shared_future;
1073 template <class _R1, class _Fp>
1074 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1075 template <class _R1, class _Fp>
1076 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1079 _LIBCPP_INLINE_VISIBILITY
1080 future() _NOEXCEPT : __state_(nullptr) {}
1081 _LIBCPP_INLINE_VISIBILITY
1082 future(future&& __rhs) _NOEXCEPT
1083 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1084 future(const future&) = delete;
1085 future& operator=(const future&) = delete;
1086 _LIBCPP_INLINE_VISIBILITY
1087 future& operator=(future&& __rhs) _NOEXCEPT
1089 future(_VSTD::move(__rhs)).swap(*this);
1093 _LIBCPP_HIDE_FROM_ABI ~future();
1094 _LIBCPP_INLINE_VISIBILITY
1095 shared_future<_Rp> share() _NOEXCEPT;
1097 // retrieving the value
1098 _LIBCPP_HIDE_FROM_ABI _Rp get();
1100 _LIBCPP_INLINE_VISIBILITY
1101 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1103 // functions to check state
1104 _LIBCPP_INLINE_VISIBILITY
1105 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1107 _LIBCPP_INLINE_VISIBILITY
1108 void wait() const {__state_->wait();}
1109 template <class _Rep, class _Period>
1110 _LIBCPP_INLINE_VISIBILITY
1112 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1113 {return __state_->wait_for(__rel_time);}
1114 template <class _Clock, class _Duration>
1115 _LIBCPP_INLINE_VISIBILITY
1117 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1118 {return __state_->wait_until(__abs_time);}
1121 template <class _Rp>
1122 future<_Rp>::future(__assoc_state<_Rp>* __state)
1125 __state_->__attach_future();
1128 struct __release_shared_count
1130 _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) {__p->__release_shared();}
1133 template <class _Rp>
1134 future<_Rp>::~future()
1137 __state_->__release_shared();
1140 template <class _Rp>
1144 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1145 __assoc_state<_Rp>* __s = __state_;
1150 template <class _Rp>
1151 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
1153 __assoc_state<_Rp&>* __state_;
1155 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
1157 template <class> friend class promise;
1158 template <class> friend class shared_future;
1160 template <class _R1, class _Fp>
1161 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1162 template <class _R1, class _Fp>
1163 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1166 _LIBCPP_INLINE_VISIBILITY
1167 future() _NOEXCEPT : __state_(nullptr) {}
1168 _LIBCPP_INLINE_VISIBILITY
1169 future(future&& __rhs) _NOEXCEPT
1170 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1171 future(const future&) = delete;
1172 future& operator=(const future&) = delete;
1173 _LIBCPP_INLINE_VISIBILITY
1174 future& operator=(future&& __rhs) _NOEXCEPT
1176 future(_VSTD::move(__rhs)).swap(*this);
1180 _LIBCPP_HIDE_FROM_ABI ~future();
1181 _LIBCPP_INLINE_VISIBILITY
1182 shared_future<_Rp&> share() _NOEXCEPT;
1184 // retrieving the value
1185 _LIBCPP_HIDE_FROM_ABI _Rp& get();
1187 _LIBCPP_INLINE_VISIBILITY
1188 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1190 // functions to check state
1191 _LIBCPP_INLINE_VISIBILITY
1192 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1194 _LIBCPP_INLINE_VISIBILITY
1195 void wait() const {__state_->wait();}
1196 template <class _Rep, class _Period>
1197 _LIBCPP_INLINE_VISIBILITY
1199 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1200 {return __state_->wait_for(__rel_time);}
1201 template <class _Clock, class _Duration>
1202 _LIBCPP_INLINE_VISIBILITY
1204 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1205 {return __state_->wait_until(__abs_time);}
1208 template <class _Rp>
1209 future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1212 __state_->__attach_future();
1215 template <class _Rp>
1216 future<_Rp&>::~future()
1219 __state_->__release_shared();
1222 template <class _Rp>
1226 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1227 __assoc_state<_Rp&>* __s = __state_;
1233 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE future<void>
1235 __assoc_sub_state* __state_;
1237 explicit future(__assoc_sub_state* __state);
1239 template <class> friend class promise;
1240 template <class> friend class shared_future;
1242 template <class _R1, class _Fp>
1243 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1244 template <class _R1, class _Fp>
1245 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1248 _LIBCPP_INLINE_VISIBILITY
1249 future() _NOEXCEPT : __state_(nullptr) {}
1250 _LIBCPP_INLINE_VISIBILITY
1251 future(future&& __rhs) _NOEXCEPT
1252 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1253 future(const future&) = delete;
1254 future& operator=(const future&) = delete;
1255 _LIBCPP_INLINE_VISIBILITY
1256 future& operator=(future&& __rhs) _NOEXCEPT
1258 future(_VSTD::move(__rhs)).swap(*this);
1263 _LIBCPP_INLINE_VISIBILITY
1264 shared_future<void> share() _NOEXCEPT;
1266 // retrieving the value
1269 _LIBCPP_INLINE_VISIBILITY
1270 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1272 // functions to check state
1273 _LIBCPP_INLINE_VISIBILITY
1274 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1276 _LIBCPP_INLINE_VISIBILITY
1277 void wait() const {__state_->wait();}
1278 template <class _Rep, class _Period>
1279 _LIBCPP_INLINE_VISIBILITY
1281 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1282 {return __state_->wait_for(__rel_time);}
1283 template <class _Clock, class _Duration>
1284 _LIBCPP_INLINE_VISIBILITY
1286 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1287 {return __state_->wait_until(__abs_time);}
1290 template <class _Rp>
1291 inline _LIBCPP_INLINE_VISIBILITY
1293 swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1300 template <class _Callable> class packaged_task;
1302 template <class _Rp>
1303 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
1305 __assoc_state<_Rp>* __state_;
1307 _LIBCPP_INLINE_VISIBILITY
1308 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1310 template <class> friend class packaged_task;
1312 _LIBCPP_HIDE_FROM_ABI promise();
1313 template <class _Alloc>
1314 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1315 _LIBCPP_INLINE_VISIBILITY
1316 promise(promise&& __rhs) _NOEXCEPT
1317 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1318 promise(const promise& __rhs) = delete;
1319 _LIBCPP_HIDE_FROM_ABI ~promise();
1322 _LIBCPP_INLINE_VISIBILITY
1323 promise& operator=(promise&& __rhs) _NOEXCEPT
1325 promise(_VSTD::move(__rhs)).swap(*this);
1328 promise& operator=(const promise& __rhs) = delete;
1330 _LIBCPP_INLINE_VISIBILITY
1331 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1333 // retrieving the result
1334 _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1336 // setting the result
1337 _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1338 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1339 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1341 // setting the result with deferred notification
1342 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1343 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1344 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1347 template <class _Rp>
1348 promise<_Rp>::promise()
1349 : __state_(new __assoc_state<_Rp>)
1353 template <class _Rp>
1354 template <class _Alloc>
1355 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1357 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1358 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1359 typedef __allocator_destructor<_A2> _D2;
1361 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1362 ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1363 __state_ = _VSTD::addressof(*__hold.release());
1366 template <class _Rp>
1367 promise<_Rp>::~promise()
1371 if (!__state_->__has_value() && __state_->use_count() > 1)
1372 __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1373 __state_->__release_shared();
1377 template <class _Rp>
1379 promise<_Rp>::get_future()
1381 if (__state_ == nullptr)
1382 __throw_future_error(future_errc::no_state);
1383 return future<_Rp>(__state_);
1386 template <class _Rp>
1388 promise<_Rp>::set_value(const _Rp& __r)
1390 if (__state_ == nullptr)
1391 __throw_future_error(future_errc::no_state);
1392 __state_->set_value(__r);
1395 template <class _Rp>
1397 promise<_Rp>::set_value(_Rp&& __r)
1399 if (__state_ == nullptr)
1400 __throw_future_error(future_errc::no_state);
1401 __state_->set_value(_VSTD::move(__r));
1404 template <class _Rp>
1406 promise<_Rp>::set_exception(exception_ptr __p)
1408 _LIBCPP_ASSERT_UNCATEGORIZED( __p != nullptr, "promise::set_exception: received nullptr" );
1409 if (__state_ == nullptr)
1410 __throw_future_error(future_errc::no_state);
1411 __state_->set_exception(__p);
1414 template <class _Rp>
1416 promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1418 if (__state_ == nullptr)
1419 __throw_future_error(future_errc::no_state);
1420 __state_->set_value_at_thread_exit(__r);
1423 template <class _Rp>
1425 promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1427 if (__state_ == nullptr)
1428 __throw_future_error(future_errc::no_state);
1429 __state_->set_value_at_thread_exit(_VSTD::move(__r));
1432 template <class _Rp>
1434 promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
1436 _LIBCPP_ASSERT_UNCATEGORIZED( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1437 if (__state_ == nullptr)
1438 __throw_future_error(future_errc::no_state);
1439 __state_->set_exception_at_thread_exit(__p);
1444 template <class _Rp>
1445 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
1447 __assoc_state<_Rp&>* __state_;
1449 _LIBCPP_INLINE_VISIBILITY
1450 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1452 template <class> friend class packaged_task;
1455 _LIBCPP_HIDE_FROM_ABI promise();
1456 template <class _Allocator>
1457 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1458 _LIBCPP_INLINE_VISIBILITY
1459 promise(promise&& __rhs) _NOEXCEPT
1460 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1461 promise(const promise& __rhs) = delete;
1462 _LIBCPP_HIDE_FROM_ABI ~promise();
1465 _LIBCPP_INLINE_VISIBILITY
1466 promise& operator=(promise&& __rhs) _NOEXCEPT
1468 promise(_VSTD::move(__rhs)).swap(*this);
1471 promise& operator=(const promise& __rhs) = delete;
1473 _LIBCPP_INLINE_VISIBILITY
1474 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1476 // retrieving the result
1477 _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1479 // setting the result
1480 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1481 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1483 // setting the result with deferred notification
1484 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1485 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1488 template <class _Rp>
1489 promise<_Rp&>::promise()
1490 : __state_(new __assoc_state<_Rp&>)
1494 template <class _Rp>
1495 template <class _Alloc>
1496 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1498 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1499 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1500 typedef __allocator_destructor<_A2> _D2;
1502 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1503 ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1504 __state_ = _VSTD::addressof(*__hold.release());
1507 template <class _Rp>
1508 promise<_Rp&>::~promise()
1512 if (!__state_->__has_value() && __state_->use_count() > 1)
1513 __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1514 __state_->__release_shared();
1518 template <class _Rp>
1520 promise<_Rp&>::get_future()
1522 if (__state_ == nullptr)
1523 __throw_future_error(future_errc::no_state);
1524 return future<_Rp&>(__state_);
1527 template <class _Rp>
1529 promise<_Rp&>::set_value(_Rp& __r)
1531 if (__state_ == nullptr)
1532 __throw_future_error(future_errc::no_state);
1533 __state_->set_value(__r);
1536 template <class _Rp>
1538 promise<_Rp&>::set_exception(exception_ptr __p)
1540 _LIBCPP_ASSERT_UNCATEGORIZED( __p != nullptr, "promise::set_exception: received nullptr" );
1541 if (__state_ == nullptr)
1542 __throw_future_error(future_errc::no_state);
1543 __state_->set_exception(__p);
1546 template <class _Rp>
1548 promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1550 if (__state_ == nullptr)
1551 __throw_future_error(future_errc::no_state);
1552 __state_->set_value_at_thread_exit(__r);
1555 template <class _Rp>
1557 promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
1559 _LIBCPP_ASSERT_UNCATEGORIZED( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1560 if (__state_ == nullptr)
1561 __throw_future_error(future_errc::no_state);
1562 __state_->set_exception_at_thread_exit(__p);
1568 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE promise<void>
1570 __assoc_sub_state* __state_;
1572 _LIBCPP_INLINE_VISIBILITY
1573 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1575 template <class> friend class packaged_task;
1579 template <class _Allocator>
1580 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1581 promise(allocator_arg_t, const _Allocator& __a);
1582 _LIBCPP_INLINE_VISIBILITY
1583 promise(promise&& __rhs) _NOEXCEPT
1584 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1585 promise(const promise& __rhs) = delete;
1589 _LIBCPP_INLINE_VISIBILITY
1590 promise& operator=(promise&& __rhs) _NOEXCEPT
1592 promise(_VSTD::move(__rhs)).swap(*this);
1595 promise& operator=(const promise& __rhs) = delete;
1597 _LIBCPP_INLINE_VISIBILITY
1598 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1600 // retrieving the result
1601 future<void> get_future();
1603 // setting the result
1605 void set_exception(exception_ptr __p);
1607 // setting the result with deferred notification
1608 void set_value_at_thread_exit();
1609 void set_exception_at_thread_exit(exception_ptr __p);
1612 template <class _Alloc>
1613 promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1615 typedef __assoc_sub_state_alloc<_Alloc> _State;
1616 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1617 typedef __allocator_destructor<_A2> _D2;
1619 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1620 ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1621 __state_ = _VSTD::addressof(*__hold.release());
1624 template <class _Rp>
1625 inline _LIBCPP_INLINE_VISIBILITY
1627 swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1632 template <class _Rp, class _Alloc>
1633 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
1634 : public true_type {};
1638 template<class _Fp> class __packaged_task_base;
1640 template<class _Rp, class ..._ArgTypes>
1641 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
1643 __packaged_task_base(const __packaged_task_base&);
1644 __packaged_task_base& operator=(const __packaged_task_base&);
1646 _LIBCPP_INLINE_VISIBILITY
1647 __packaged_task_base() {}
1648 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1649 virtual ~__packaged_task_base() {}
1650 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1651 virtual void destroy() = 0;
1652 virtual void destroy_deallocate() = 0;
1653 virtual _Rp operator()(_ArgTypes&& ...) = 0;
1656 template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1658 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1659 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1660 : public __packaged_task_base<_Rp(_ArgTypes...)>
1662 __compressed_pair<_Fp, _Alloc> __f_;
1664 _LIBCPP_INLINE_VISIBILITY
1665 explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
1666 _LIBCPP_INLINE_VISIBILITY
1667 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1668 _LIBCPP_INLINE_VISIBILITY
1669 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
1671 _LIBCPP_INLINE_VISIBILITY
1672 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
1673 : __f_(_VSTD::move(__f), __a) {}
1674 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1675 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1676 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1677 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&& ... __args);
1680 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1682 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1683 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1685 ::new ((void*)__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1688 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1690 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1692 __f_.~__compressed_pair<_Fp, _Alloc>();
1695 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1697 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
1699 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1700 typedef allocator_traits<_Ap> _ATraits;
1701 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1702 _Ap __a(__f_.second());
1703 __f_.~__compressed_pair<_Fp, _Alloc>();
1704 __a.deallocate(_PTraits::pointer_to(*this), 1);
1707 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1709 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1711 return _VSTD::__invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1714 template <class _Callable> class __packaged_task_function;
1716 template<class _Rp, class ..._ArgTypes>
1717 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
1719 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1721 _LIBCPP_INLINE_VISIBILITY _LIBCPP_NO_CFI
1722 __base* __get_buf() { return (__base*)&__buf_; }
1724 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1725 typename aligned_storage<3*sizeof(void*)>::type __buf_;
1726 _LIBCPP_SUPPRESS_DEPRECATED_POP
1730 typedef _Rp result_type;
1732 // construct/copy/destroy:
1733 _LIBCPP_INLINE_VISIBILITY
1734 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1736 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1737 template<class _Fp, class _Alloc>
1738 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1740 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1741 _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1743 __packaged_task_function(const __packaged_task_function&) = delete;
1744 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1746 _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1748 _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1750 _LIBCPP_INLINE_VISIBILITY
1751 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1754 template<class _Rp, class ..._ArgTypes>
1755 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1757 if (__f.__f_ == nullptr)
1759 else if (__f.__f_ == __f.__get_buf())
1761 __f.__f_->__move_to(__get_buf());
1762 __f_ = (__base*)&__buf_;
1771 template<class _Rp, class ..._ArgTypes>
1772 template <class _Fp>
1773 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
1776 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1777 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1778 if (sizeof(_FF) <= sizeof(__buf_))
1780 ::new ((void*)&__buf_) _FF(_VSTD::forward<_Fp>(__f));
1781 __f_ = (__base*)&__buf_;
1785 typedef allocator<_FF> _Ap;
1787 typedef __allocator_destructor<_Ap> _Dp;
1788 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1789 ::new ((void*)__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
1790 __f_ = __hold.release();
1794 template<class _Rp, class ..._ArgTypes>
1795 template <class _Fp, class _Alloc>
1796 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1797 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1800 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1801 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1802 if (sizeof(_FF) <= sizeof(__buf_))
1804 __f_ = (__base*)&__buf_;
1805 ::new ((void*)__f_) _FF(_VSTD::forward<_Fp>(__f));
1809 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1811 typedef __allocator_destructor<_Ap> _Dp;
1812 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1813 ::new ((void*)_VSTD::addressof(*__hold.get()))
1814 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1815 __f_ = _VSTD::addressof(*__hold.release());
1819 template<class _Rp, class ..._ArgTypes>
1820 __packaged_task_function<_Rp(_ArgTypes...)>&
1821 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1823 if (__f_ == __get_buf())
1826 __f_->destroy_deallocate();
1828 if (__f.__f_ == nullptr)
1830 else if (__f.__f_ == __f.__get_buf())
1832 __f.__f_->__move_to(__get_buf());
1843 template<class _Rp, class ..._ArgTypes>
1844 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1846 if (__f_ == __get_buf())
1849 __f_->destroy_deallocate();
1852 template<class _Rp, class ..._ArgTypes>
1855 __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1857 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1859 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1860 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1861 _LIBCPP_SUPPRESS_DEPRECATED_POP
1862 __base* __t = (__base*)&__tempbuf;
1863 __f_->__move_to(__t);
1866 __f.__f_->__move_to((__base*)&__buf_);
1867 __f.__f_->destroy();
1869 __f_ = (__base*)&__buf_;
1870 __t->__move_to((__base*)&__f.__buf_);
1872 __f.__f_ = (__base*)&__f.__buf_;
1874 else if (__f_ == (__base*)&__buf_)
1876 __f_->__move_to((__base*)&__f.__buf_);
1879 __f.__f_ = (__base*)&__f.__buf_;
1881 else if (__f.__f_ == (__base*)&__f.__buf_)
1883 __f.__f_->__move_to((__base*)&__buf_);
1884 __f.__f_->destroy();
1886 __f_ = (__base*)&__buf_;
1889 _VSTD::swap(__f_, __f.__f_);
1892 template<class _Rp, class ..._ArgTypes>
1895 __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1897 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
1900 template<class _Rp, class ..._ArgTypes>
1901 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
1904 typedef _Rp result_type; // extension
1907 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1908 promise<result_type> __p_;
1911 // construction and destruction
1912 _LIBCPP_INLINE_VISIBILITY
1913 packaged_task() _NOEXCEPT : __p_(nullptr) {}
1914 template <class _Fp,
1915 class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1916 _LIBCPP_INLINE_VISIBILITY
1917 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
1918 template <class _Fp, class _Allocator,
1919 class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1920 _LIBCPP_INLINE_VISIBILITY
1921 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1922 : __f_(allocator_arg_t(), __a, _VSTD::forward<_Fp>(__f)),
1923 __p_(allocator_arg_t(), __a) {}
1924 // ~packaged_task() = default;
1927 packaged_task(const packaged_task&) = delete;
1928 packaged_task& operator=(const packaged_task&) = delete;
1931 _LIBCPP_INLINE_VISIBILITY
1932 packaged_task(packaged_task&& __other) _NOEXCEPT
1933 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
1934 _LIBCPP_INLINE_VISIBILITY
1935 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
1937 __f_ = _VSTD::move(__other.__f_);
1938 __p_ = _VSTD::move(__other.__p_);
1941 _LIBCPP_INLINE_VISIBILITY
1942 void swap(packaged_task& __other) _NOEXCEPT
1944 __f_.swap(__other.__f_);
1945 __p_.swap(__other.__p_);
1948 _LIBCPP_INLINE_VISIBILITY
1949 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
1952 _LIBCPP_INLINE_VISIBILITY
1953 future<result_type> get_future() {return __p_.get_future();}
1956 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1957 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1959 _LIBCPP_HIDE_FROM_ABI void reset();
1962 template<class _Rp, class ..._ArgTypes>
1964 packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
1966 if (__p_.__state_ == nullptr)
1967 __throw_future_error(future_errc::no_state);
1968 if (__p_.__state_->__has_value())
1969 __throw_future_error(future_errc::promise_already_satisfied);
1970 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1973 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1974 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
1975 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1979 __p_.set_exception(current_exception());
1981 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1984 template<class _Rp, class ..._ArgTypes>
1986 packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
1988 if (__p_.__state_ == nullptr)
1989 __throw_future_error(future_errc::no_state);
1990 if (__p_.__state_->__has_value())
1991 __throw_future_error(future_errc::promise_already_satisfied);
1992 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1995 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1996 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
1997 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2001 __p_.set_exception_at_thread_exit(current_exception());
2003 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2006 template<class _Rp, class ..._ArgTypes>
2008 packaged_task<_Rp(_ArgTypes...)>::reset()
2011 __throw_future_error(future_errc::no_state);
2012 __p_ = promise<result_type>();
2015 template<class ..._ArgTypes>
2016 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
2019 typedef void result_type; // extension
2022 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2023 promise<result_type> __p_;
2026 // construction and destruction
2027 _LIBCPP_INLINE_VISIBILITY
2028 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2029 template <class _Fp,
2030 class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
2031 _LIBCPP_INLINE_VISIBILITY
2032 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2033 template <class _Fp, class _Allocator,
2034 class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
2035 _LIBCPP_INLINE_VISIBILITY
2036 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2037 : __f_(allocator_arg_t(), __a, _VSTD::forward<_Fp>(__f)),
2038 __p_(allocator_arg_t(), __a) {}
2039 // ~packaged_task() = default;
2042 packaged_task(const packaged_task&) = delete;
2043 packaged_task& operator=(const packaged_task&) = delete;
2046 _LIBCPP_INLINE_VISIBILITY
2047 packaged_task(packaged_task&& __other) _NOEXCEPT
2048 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2049 _LIBCPP_INLINE_VISIBILITY
2050 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2052 __f_ = _VSTD::move(__other.__f_);
2053 __p_ = _VSTD::move(__other.__p_);
2056 _LIBCPP_INLINE_VISIBILITY
2057 void swap(packaged_task& __other) _NOEXCEPT
2059 __f_.swap(__other.__f_);
2060 __p_.swap(__other.__p_);
2063 _LIBCPP_INLINE_VISIBILITY
2064 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2067 _LIBCPP_INLINE_VISIBILITY
2068 future<result_type> get_future() {return __p_.get_future();}
2071 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
2072 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
2074 _LIBCPP_HIDE_FROM_ABI void reset();
2077 #if _LIBCPP_STD_VER >= 17
2079 template <class _Rp, class... _Args>
2080 packaged_task(_Rp(*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
2082 template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2083 packaged_task(_Fp) -> packaged_task<_Stripped>;
2087 template<class ..._ArgTypes>
2089 packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2091 if (__p_.__state_ == nullptr)
2092 __throw_future_error(future_errc::no_state);
2093 if (__p_.__state_->__has_value())
2094 __throw_future_error(future_errc::promise_already_satisfied);
2095 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2098 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2099 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2101 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2105 __p_.set_exception(current_exception());
2107 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2110 template<class ..._ArgTypes>
2112 packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2114 if (__p_.__state_ == nullptr)
2115 __throw_future_error(future_errc::no_state);
2116 if (__p_.__state_->__has_value())
2117 __throw_future_error(future_errc::promise_already_satisfied);
2118 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2121 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2122 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2123 __p_.set_value_at_thread_exit();
2124 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2128 __p_.set_exception_at_thread_exit(current_exception());
2130 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2133 template<class ..._ArgTypes>
2135 packaged_task<void(_ArgTypes...)>::reset()
2138 __throw_future_error(future_errc::no_state);
2139 __p_ = promise<result_type>();
2142 template <class _Rp, class... _ArgTypes>
2143 inline _LIBCPP_INLINE_VISIBILITY
2145 swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
2150 template <class _Callable, class _Alloc>
2151 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
2152 : public true_type {};
2154 template <class _Rp, class _Fp>
2155 _LIBCPP_INLINE_VISIBILITY future<_Rp>
2156 __make_deferred_assoc_state(_Fp&& __f)
2158 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2159 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2160 return future<_Rp>(__h.get());
2163 template <class _Rp, class _Fp>
2164 _LIBCPP_INLINE_VISIBILITY future<_Rp>
2165 __make_async_assoc_state(_Fp&& __f)
2167 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2168 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2169 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2170 return future<_Rp>(__h.get());
2173 #ifndef _LIBCPP_CXX03_LANG
2175 template <class _Fp, class... _Args>
2176 class _LIBCPP_HIDDEN __async_func
2178 tuple<_Fp, _Args...> __f_;
2181 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2183 _LIBCPP_INLINE_VISIBILITY
2184 explicit __async_func(_Fp&& __f, _Args&&... __args)
2185 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2187 _LIBCPP_INLINE_VISIBILITY
2188 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2190 _LIBCPP_HIDE_FROM_ABI _Rp operator()()
2192 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2193 return __execute(_Index());
2196 template <size_t ..._Indices>
2197 _LIBCPP_HIDE_FROM_ABI _Rp
2198 __execute(__tuple_indices<_Indices...>)
2200 return _VSTD::__invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2204 inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2205 { return (int(__policy) & int(__value)) != 0; }
2207 template <class _Fp, class... _Args>
2208 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
2209 future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
2210 async(launch __policy, _Fp&& __f, _Args&&... __args)
2212 typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
2213 typedef typename _BF::_Rp _Rp;
2215 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2219 if (__does_policy_contain(__policy, launch::async))
2220 return _VSTD::__make_async_assoc_state<_Rp>(_BF(_LIBCPP_AUTO_CAST(_VSTD::forward<_Fp>(__f)),
2221 _LIBCPP_AUTO_CAST(_VSTD::forward<_Args>(__args))...));
2222 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2224 catch ( ... ) { if (__policy == launch::async) throw ; }
2227 if (__does_policy_contain(__policy, launch::deferred))
2228 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(_LIBCPP_AUTO_CAST(_VSTD::forward<_Fp>(__f)),
2229 _LIBCPP_AUTO_CAST(_VSTD::forward<_Args>(__args))...));
2230 return future<_Rp>{};
2233 template <class _Fp, class... _Args>
2234 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
2235 future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
2236 async(_Fp&& __f, _Args&&... __args)
2238 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2239 _VSTD::forward<_Args>(__args)...);
2246 template <class _Rp>
2247 class _LIBCPP_TEMPLATE_VIS shared_future
2249 __assoc_state<_Rp>* __state_;
2252 _LIBCPP_INLINE_VISIBILITY
2253 shared_future() _NOEXCEPT : __state_(nullptr) {}
2254 _LIBCPP_INLINE_VISIBILITY
2255 shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2256 {if (__state_) __state_->__add_shared();}
2257 _LIBCPP_INLINE_VISIBILITY
2258 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
2259 {__f.__state_ = nullptr;}
2260 _LIBCPP_INLINE_VISIBILITY
2261 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2262 {__rhs.__state_ = nullptr;}
2263 _LIBCPP_HIDE_FROM_ABI ~shared_future();
2264 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
2265 _LIBCPP_INLINE_VISIBILITY
2266 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2268 shared_future(_VSTD::move(__rhs)).swap(*this);
2272 // retrieving the value
2273 _LIBCPP_INLINE_VISIBILITY
2274 const _Rp& get() const {return __state_->copy();}
2276 _LIBCPP_INLINE_VISIBILITY
2277 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2279 // functions to check state
2280 _LIBCPP_INLINE_VISIBILITY
2281 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2283 _LIBCPP_INLINE_VISIBILITY
2284 void wait() const {__state_->wait();}
2285 template <class _Rep, class _Period>
2286 _LIBCPP_INLINE_VISIBILITY
2288 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2289 {return __state_->wait_for(__rel_time);}
2290 template <class _Clock, class _Duration>
2291 _LIBCPP_INLINE_VISIBILITY
2293 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2294 {return __state_->wait_until(__abs_time);}
2297 template <class _Rp>
2298 shared_future<_Rp>::~shared_future()
2301 __state_->__release_shared();
2304 template <class _Rp>
2306 shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
2309 __rhs.__state_->__add_shared();
2311 __state_->__release_shared();
2312 __state_ = __rhs.__state_;
2316 template <class _Rp>
2317 class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
2319 __assoc_state<_Rp&>* __state_;
2322 _LIBCPP_INLINE_VISIBILITY
2323 shared_future() _NOEXCEPT : __state_(nullptr) {}
2324 _LIBCPP_INLINE_VISIBILITY
2325 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2326 {if (__state_) __state_->__add_shared();}
2327 _LIBCPP_INLINE_VISIBILITY
2328 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
2329 {__f.__state_ = nullptr;}
2330 _LIBCPP_INLINE_VISIBILITY
2331 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2332 {__rhs.__state_ = nullptr;}
2333 _LIBCPP_HIDE_FROM_ABI ~shared_future();
2334 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
2335 _LIBCPP_INLINE_VISIBILITY
2336 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2338 shared_future(_VSTD::move(__rhs)).swap(*this);
2342 // retrieving the value
2343 _LIBCPP_INLINE_VISIBILITY
2344 _Rp& get() const {return __state_->copy();}
2346 _LIBCPP_INLINE_VISIBILITY
2347 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2349 // functions to check state
2350 _LIBCPP_INLINE_VISIBILITY
2351 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2353 _LIBCPP_INLINE_VISIBILITY
2354 void wait() const {__state_->wait();}
2355 template <class _Rep, class _Period>
2356 _LIBCPP_INLINE_VISIBILITY
2358 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2359 {return __state_->wait_for(__rel_time);}
2360 template <class _Clock, class _Duration>
2361 _LIBCPP_INLINE_VISIBILITY
2363 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2364 {return __state_->wait_until(__abs_time);}
2367 template <class _Rp>
2368 shared_future<_Rp&>::~shared_future()
2371 __state_->__release_shared();
2374 template <class _Rp>
2375 shared_future<_Rp&>&
2376 shared_future<_Rp&>::operator=(const shared_future& __rhs)
2379 __rhs.__state_->__add_shared();
2381 __state_->__release_shared();
2382 __state_ = __rhs.__state_;
2387 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
2389 __assoc_sub_state* __state_;
2392 _LIBCPP_INLINE_VISIBILITY
2393 shared_future() _NOEXCEPT : __state_(nullptr) {}
2394 _LIBCPP_INLINE_VISIBILITY
2395 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2396 {if (__state_) __state_->__add_shared();}
2397 _LIBCPP_INLINE_VISIBILITY
2398 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
2399 {__f.__state_ = nullptr;}
2400 _LIBCPP_INLINE_VISIBILITY
2401 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2402 {__rhs.__state_ = nullptr;}
2404 shared_future& operator=(const shared_future& __rhs);
2405 _LIBCPP_INLINE_VISIBILITY
2406 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2408 shared_future(_VSTD::move(__rhs)).swap(*this);
2412 // retrieving the value
2413 _LIBCPP_INLINE_VISIBILITY
2414 void get() const {__state_->copy();}
2416 _LIBCPP_INLINE_VISIBILITY
2417 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2419 // functions to check state
2420 _LIBCPP_INLINE_VISIBILITY
2421 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2423 _LIBCPP_INLINE_VISIBILITY
2424 void wait() const {__state_->wait();}
2425 template <class _Rep, class _Period>
2426 _LIBCPP_INLINE_VISIBILITY
2428 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2429 {return __state_->wait_for(__rel_time);}
2430 template <class _Clock, class _Duration>
2431 _LIBCPP_INLINE_VISIBILITY
2433 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2434 {return __state_->wait_until(__abs_time);}
2437 template <class _Rp>
2438 inline _LIBCPP_INLINE_VISIBILITY
2440 swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2445 template <class _Rp>
2448 future<_Rp>::share() _NOEXCEPT
2450 return shared_future<_Rp>(_VSTD::move(*this));
2453 template <class _Rp>
2456 future<_Rp&>::share() _NOEXCEPT
2458 return shared_future<_Rp&>(_VSTD::move(*this));
2463 future<void>::share() _NOEXCEPT
2465 return shared_future<void>(_VSTD::move(*this));
2468 _LIBCPP_END_NAMESPACE_STD
2470 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2474 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2477 # include <exception>
2479 # include <system_error>
2482 #endif // _LIBCPP_FUTURE