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;
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>;
364 #include <__assert> // all public C++ headers provide the assertion handler
365 #include <__availability>
366 #include <__chrono/duration.h>
367 #include <__chrono/time_point.h>
369 #include <__exception/exception_ptr.h>
370 #include <__memory/addressof.h>
371 #include <__memory/allocator.h>
372 #include <__memory/allocator_arg_t.h>
373 #include <__memory/allocator_destructor.h>
374 #include <__memory/allocator_traits.h>
375 #include <__memory/compressed_pair.h>
376 #include <__memory/pointer_traits.h>
377 #include <__memory/shared_ptr.h>
378 #include <__memory/unique_ptr.h>
379 #include <__memory/uses_allocator.h>
380 #include <__system_error/error_category.h>
381 #include <__system_error/error_code.h>
382 #include <__system_error/error_condition.h>
383 #include <__type_traits/aligned_storage.h>
384 #include <__type_traits/alignment_of.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 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
524 future_error(error_code __ec);
526 _LIBCPP_INLINE_VISIBILITY
527 const error_code& code() const _NOEXCEPT {return __ec_;}
529 _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
530 ~future_error() _NOEXCEPT override;
533 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
534 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
535 _LIBCPP_AVAILABILITY_FUTURE_ERROR
537 void __throw_future_error(future_errc __ev)
539 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
540 throw future_error(make_error_code(__ev));
543 _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
547 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
548 : public __shared_count
551 exception_ptr __exception_;
552 mutable mutex __mut_;
553 mutable condition_variable __cv_;
556 void __on_zero_shared() _NOEXCEPT override;
557 void __sub_wait(unique_lock<mutex>& __lk);
562 __future_attached = 2,
567 _LIBCPP_INLINE_VISIBILITY
568 __assoc_sub_state() : __state_(0) {}
570 _LIBCPP_INLINE_VISIBILITY
571 bool __has_value() const
572 {return (__state_ & __constructed) || (__exception_ != nullptr);}
574 _LIBCPP_INLINE_VISIBILITY
575 void __attach_future() {
576 lock_guard<mutex> __lk(__mut_);
577 bool __has_future_attached = (__state_ & __future_attached) != 0;
578 if (__has_future_attached)
579 __throw_future_error(future_errc::future_already_retrieved);
580 this->__add_shared();
581 __state_ |= __future_attached;
584 _LIBCPP_INLINE_VISIBILITY
585 void __set_deferred() {__state_ |= deferred;}
588 _LIBCPP_INLINE_VISIBILITY
589 bool __is_ready() const {return (__state_ & ready) != 0;}
592 void set_value_at_thread_exit();
594 void set_exception(exception_ptr __p);
595 void set_exception_at_thread_exit(exception_ptr __p);
600 template <class _Rep, class _Period>
602 _LIBCPP_INLINE_VISIBILITY
603 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
604 template <class _Clock, class _Duration>
605 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
607 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
609 virtual void __execute();
612 template <class _Clock, class _Duration>
614 __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
616 unique_lock<mutex> __lk(__mut_);
617 if (__state_ & deferred)
618 return future_status::deferred;
619 while (!(__state_ & ready) && _Clock::now() < __abs_time)
620 __cv_.wait_until(__lk, __abs_time);
621 if (__state_ & ready)
622 return future_status::ready;
623 return future_status::timeout;
626 template <class _Rep, class _Period>
629 __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
631 return wait_until(chrono::steady_clock::now() + __rel_time);
635 class _LIBCPP_AVAILABILITY_FUTURE _LIBCPP_HIDDEN __assoc_state
636 : public __assoc_sub_state
638 typedef __assoc_sub_state base;
639 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
640 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
641 _LIBCPP_SUPPRESS_DEPRECATED_POP
645 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
648 template <class _Arg>
649 _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
651 template <class _Arg>
652 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
654 _LIBCPP_HIDE_FROM_ABI _Rp move();
655 _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<_Rp> copy();
660 __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
662 if (this->__state_ & base::__constructed)
663 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
668 template <class _Arg>
669 _LIBCPP_AVAILABILITY_FUTURE
671 __assoc_state<_Rp>::set_value(_Arg&& __arg)
673 unique_lock<mutex> __lk(this->__mut_);
674 if (this->__has_value())
675 __throw_future_error(future_errc::promise_already_satisfied);
676 ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
677 this->__state_ |= base::__constructed | base::ready;
682 template <class _Arg>
684 __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
686 unique_lock<mutex> __lk(this->__mut_);
687 if (this->__has_value())
688 __throw_future_error(future_errc::promise_already_satisfied);
689 ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
690 this->__state_ |= base::__constructed;
691 __thread_local_data()->__make_ready_at_thread_exit(this);
696 __assoc_state<_Rp>::move()
698 unique_lock<mutex> __lk(this->__mut_);
699 this->__sub_wait(__lk);
700 if (this->__exception_ != nullptr)
701 std::rethrow_exception(this->__exception_);
702 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
706 __add_lvalue_reference_t<_Rp>
707 __assoc_state<_Rp>::copy()
709 unique_lock<mutex> __lk(this->__mut_);
710 this->__sub_wait(__lk);
711 if (this->__exception_ != nullptr)
712 std::rethrow_exception(this->__exception_);
713 return *reinterpret_cast<_Rp*>(&__value_);
717 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
718 : public __assoc_sub_state
720 typedef __assoc_sub_state base;
725 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
728 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
729 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
731 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
736 __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
743 __assoc_state<_Rp&>::set_value(_Rp& __arg)
745 unique_lock<mutex> __lk(this->__mut_);
746 if (this->__has_value())
747 __throw_future_error(future_errc::promise_already_satisfied);
748 __value_ = _VSTD::addressof(__arg);
749 this->__state_ |= base::__constructed | base::ready;
755 __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
757 unique_lock<mutex> __lk(this->__mut_);
758 if (this->__has_value())
759 __throw_future_error(future_errc::promise_already_satisfied);
760 __value_ = _VSTD::addressof(__arg);
761 this->__state_ |= base::__constructed;
762 __thread_local_data()->__make_ready_at_thread_exit(this);
767 __assoc_state<_Rp&>::copy()
769 unique_lock<mutex> __lk(this->__mut_);
770 this->__sub_wait(__lk);
771 if (this->__exception_ != nullptr)
772 std::rethrow_exception(this->__exception_);
776 template <class _Rp, class _Alloc>
777 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
778 : public __assoc_state<_Rp>
780 typedef __assoc_state<_Rp> base;
783 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
785 _LIBCPP_INLINE_VISIBILITY
786 explicit __assoc_state_alloc(const _Alloc& __a)
790 template <class _Rp, class _Alloc>
792 __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
794 if (this->__state_ & base::__constructed)
795 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
796 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
797 typedef allocator_traits<_Al> _ATraits;
798 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
800 this->~__assoc_state_alloc();
801 __a.deallocate(_PTraits::pointer_to(*this), 1);
804 template <class _Rp, class _Alloc>
805 class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
806 : public __assoc_state<_Rp&>
808 typedef __assoc_state<_Rp&> base;
811 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
813 _LIBCPP_INLINE_VISIBILITY
814 explicit __assoc_state_alloc(const _Alloc& __a)
818 template <class _Rp, class _Alloc>
820 __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
822 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
823 typedef allocator_traits<_Al> _ATraits;
824 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
826 this->~__assoc_state_alloc();
827 __a.deallocate(_PTraits::pointer_to(*this), 1);
830 template <class _Alloc>
831 class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
832 : public __assoc_sub_state
834 typedef __assoc_sub_state base;
837 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
839 _LIBCPP_INLINE_VISIBILITY
840 explicit __assoc_sub_state_alloc(const _Alloc& __a)
844 template <class _Alloc>
846 __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
848 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
849 typedef allocator_traits<_Al> _ATraits;
850 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
852 this->~__assoc_sub_state_alloc();
853 __a.deallocate(_PTraits::pointer_to(*this), 1);
856 template <class _Rp, class _Fp>
857 class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
858 : public __assoc_state<_Rp>
860 typedef __assoc_state<_Rp> base;
865 _LIBCPP_INLINE_VISIBILITY
866 explicit __deferred_assoc_state(_Fp&& __f);
868 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
871 template <class _Rp, class _Fp>
873 __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
874 : __func_(_VSTD::forward<_Fp>(__f))
876 this->__set_deferred();
879 template <class _Rp, class _Fp>
881 __deferred_assoc_state<_Rp, _Fp>::__execute()
883 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
886 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
887 this->set_value(__func_());
888 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
892 this->set_exception(current_exception());
894 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
898 class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
899 : public __assoc_sub_state
901 typedef __assoc_sub_state base;
906 _LIBCPP_INLINE_VISIBILITY
907 explicit __deferred_assoc_state(_Fp&& __f);
909 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
914 __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
915 : __func_(_VSTD::forward<_Fp>(__f))
917 this->__set_deferred();
922 __deferred_assoc_state<void, _Fp>::__execute()
924 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
927 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
930 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
934 this->set_exception(current_exception());
936 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
939 template <class _Rp, class _Fp>
940 class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
941 : public __assoc_state<_Rp>
943 typedef __assoc_state<_Rp> base;
947 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
949 _LIBCPP_INLINE_VISIBILITY
950 explicit __async_assoc_state(_Fp&& __f);
952 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
955 template <class _Rp, class _Fp>
957 __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
958 : __func_(_VSTD::forward<_Fp>(__f))
962 template <class _Rp, class _Fp>
964 __async_assoc_state<_Rp, _Fp>::__execute()
966 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
969 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
970 this->set_value(__func_());
971 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
975 this->set_exception(current_exception());
977 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
980 template <class _Rp, class _Fp>
982 __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
985 base::__on_zero_shared();
989 class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
990 : public __assoc_sub_state
992 typedef __assoc_sub_state base;
996 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
998 _LIBCPP_INLINE_VISIBILITY
999 explicit __async_assoc_state(_Fp&& __f);
1001 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
1004 template <class _Fp>
1006 __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1007 : __func_(_VSTD::forward<_Fp>(__f))
1011 template <class _Fp>
1013 __async_assoc_state<void, _Fp>::__execute()
1015 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1018 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1021 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1025 this->set_exception(current_exception());
1027 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1030 template <class _Fp>
1032 __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1035 base::__on_zero_shared();
1038 template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1039 template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
1043 template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
1045 template <class _Rp, class _Fp>
1046 _LIBCPP_INLINE_VISIBILITY future<_Rp>
1047 __make_deferred_assoc_state(_Fp&& __f);
1049 template <class _Rp, class _Fp>
1050 _LIBCPP_INLINE_VISIBILITY future<_Rp>
1051 __make_async_assoc_state(_Fp&& __f);
1053 template <class _Rp>
1054 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
1056 __assoc_state<_Rp>* __state_;
1058 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
1060 template <class> friend class promise;
1061 template <class> friend class shared_future;
1063 template <class _R1, class _Fp>
1064 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1065 template <class _R1, class _Fp>
1066 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1069 _LIBCPP_INLINE_VISIBILITY
1070 future() _NOEXCEPT : __state_(nullptr) {}
1071 _LIBCPP_INLINE_VISIBILITY
1072 future(future&& __rhs) _NOEXCEPT
1073 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1074 future(const future&) = delete;
1075 future& operator=(const future&) = delete;
1076 _LIBCPP_INLINE_VISIBILITY
1077 future& operator=(future&& __rhs) _NOEXCEPT
1079 future(_VSTD::move(__rhs)).swap(*this);
1083 _LIBCPP_HIDE_FROM_ABI ~future();
1084 _LIBCPP_INLINE_VISIBILITY
1085 shared_future<_Rp> share() _NOEXCEPT;
1087 // retrieving the value
1088 _LIBCPP_HIDE_FROM_ABI _Rp get();
1090 _LIBCPP_INLINE_VISIBILITY
1091 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1093 // functions to check state
1094 _LIBCPP_INLINE_VISIBILITY
1095 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1097 _LIBCPP_INLINE_VISIBILITY
1098 void wait() const {__state_->wait();}
1099 template <class _Rep, class _Period>
1100 _LIBCPP_INLINE_VISIBILITY
1102 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1103 {return __state_->wait_for(__rel_time);}
1104 template <class _Clock, class _Duration>
1105 _LIBCPP_INLINE_VISIBILITY
1107 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1108 {return __state_->wait_until(__abs_time);}
1111 template <class _Rp>
1112 future<_Rp>::future(__assoc_state<_Rp>* __state)
1115 __state_->__attach_future();
1118 struct __release_shared_count
1120 _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) {__p->__release_shared();}
1123 template <class _Rp>
1124 future<_Rp>::~future()
1127 __state_->__release_shared();
1130 template <class _Rp>
1134 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1135 __assoc_state<_Rp>* __s = __state_;
1140 template <class _Rp>
1141 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
1143 __assoc_state<_Rp&>* __state_;
1145 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
1147 template <class> friend class promise;
1148 template <class> friend class shared_future;
1150 template <class _R1, class _Fp>
1151 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1152 template <class _R1, class _Fp>
1153 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1156 _LIBCPP_INLINE_VISIBILITY
1157 future() _NOEXCEPT : __state_(nullptr) {}
1158 _LIBCPP_INLINE_VISIBILITY
1159 future(future&& __rhs) _NOEXCEPT
1160 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1161 future(const future&) = delete;
1162 future& operator=(const future&) = delete;
1163 _LIBCPP_INLINE_VISIBILITY
1164 future& operator=(future&& __rhs) _NOEXCEPT
1166 future(_VSTD::move(__rhs)).swap(*this);
1170 _LIBCPP_HIDE_FROM_ABI ~future();
1171 _LIBCPP_INLINE_VISIBILITY
1172 shared_future<_Rp&> share() _NOEXCEPT;
1174 // retrieving the value
1175 _LIBCPP_HIDE_FROM_ABI _Rp& get();
1177 _LIBCPP_INLINE_VISIBILITY
1178 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1180 // functions to check state
1181 _LIBCPP_INLINE_VISIBILITY
1182 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1184 _LIBCPP_INLINE_VISIBILITY
1185 void wait() const {__state_->wait();}
1186 template <class _Rep, class _Period>
1187 _LIBCPP_INLINE_VISIBILITY
1189 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1190 {return __state_->wait_for(__rel_time);}
1191 template <class _Clock, class _Duration>
1192 _LIBCPP_INLINE_VISIBILITY
1194 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1195 {return __state_->wait_until(__abs_time);}
1198 template <class _Rp>
1199 future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1202 __state_->__attach_future();
1205 template <class _Rp>
1206 future<_Rp&>::~future()
1209 __state_->__release_shared();
1212 template <class _Rp>
1216 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1217 __assoc_state<_Rp&>* __s = __state_;
1223 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE future<void>
1225 __assoc_sub_state* __state_;
1227 explicit future(__assoc_sub_state* __state);
1229 template <class> friend class promise;
1230 template <class> friend class shared_future;
1232 template <class _R1, class _Fp>
1233 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1234 template <class _R1, class _Fp>
1235 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1238 _LIBCPP_INLINE_VISIBILITY
1239 future() _NOEXCEPT : __state_(nullptr) {}
1240 _LIBCPP_INLINE_VISIBILITY
1241 future(future&& __rhs) _NOEXCEPT
1242 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1243 future(const future&) = delete;
1244 future& operator=(const future&) = delete;
1245 _LIBCPP_INLINE_VISIBILITY
1246 future& operator=(future&& __rhs) _NOEXCEPT
1248 future(_VSTD::move(__rhs)).swap(*this);
1253 _LIBCPP_INLINE_VISIBILITY
1254 shared_future<void> share() _NOEXCEPT;
1256 // retrieving the value
1259 _LIBCPP_INLINE_VISIBILITY
1260 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1262 // functions to check state
1263 _LIBCPP_INLINE_VISIBILITY
1264 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1266 _LIBCPP_INLINE_VISIBILITY
1267 void wait() const {__state_->wait();}
1268 template <class _Rep, class _Period>
1269 _LIBCPP_INLINE_VISIBILITY
1271 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1272 {return __state_->wait_for(__rel_time);}
1273 template <class _Clock, class _Duration>
1274 _LIBCPP_INLINE_VISIBILITY
1276 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1277 {return __state_->wait_until(__abs_time);}
1280 template <class _Rp>
1281 inline _LIBCPP_INLINE_VISIBILITY
1283 swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1290 template <class _Callable> class packaged_task;
1292 template <class _Rp>
1293 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
1295 __assoc_state<_Rp>* __state_;
1297 _LIBCPP_INLINE_VISIBILITY
1298 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1300 template <class> friend class packaged_task;
1302 _LIBCPP_HIDE_FROM_ABI promise();
1303 template <class _Alloc>
1304 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1305 _LIBCPP_INLINE_VISIBILITY
1306 promise(promise&& __rhs) _NOEXCEPT
1307 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1308 promise(const promise& __rhs) = delete;
1309 _LIBCPP_HIDE_FROM_ABI ~promise();
1312 _LIBCPP_INLINE_VISIBILITY
1313 promise& operator=(promise&& __rhs) _NOEXCEPT
1315 promise(_VSTD::move(__rhs)).swap(*this);
1318 promise& operator=(const promise& __rhs) = delete;
1320 _LIBCPP_INLINE_VISIBILITY
1321 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1323 // retrieving the result
1324 _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1326 // setting the result
1327 _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1328 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1329 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1331 // setting the result with deferred notification
1332 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1333 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1334 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1337 template <class _Rp>
1338 promise<_Rp>::promise()
1339 : __state_(new __assoc_state<_Rp>)
1343 template <class _Rp>
1344 template <class _Alloc>
1345 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1347 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1348 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1349 typedef __allocator_destructor<_A2> _D2;
1351 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1352 ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1353 __state_ = _VSTD::addressof(*__hold.release());
1356 template <class _Rp>
1357 promise<_Rp>::~promise()
1361 if (!__state_->__has_value() && __state_->use_count() > 1)
1362 __state_->set_exception(make_exception_ptr(
1363 future_error(make_error_code(future_errc::broken_promise))
1365 __state_->__release_shared();
1369 template <class _Rp>
1371 promise<_Rp>::get_future()
1373 if (__state_ == nullptr)
1374 __throw_future_error(future_errc::no_state);
1375 return future<_Rp>(__state_);
1378 template <class _Rp>
1380 promise<_Rp>::set_value(const _Rp& __r)
1382 if (__state_ == nullptr)
1383 __throw_future_error(future_errc::no_state);
1384 __state_->set_value(__r);
1387 template <class _Rp>
1389 promise<_Rp>::set_value(_Rp&& __r)
1391 if (__state_ == nullptr)
1392 __throw_future_error(future_errc::no_state);
1393 __state_->set_value(_VSTD::move(__r));
1396 template <class _Rp>
1398 promise<_Rp>::set_exception(exception_ptr __p)
1400 _LIBCPP_ASSERT_UNCATEGORIZED( __p != nullptr, "promise::set_exception: received nullptr" );
1401 if (__state_ == nullptr)
1402 __throw_future_error(future_errc::no_state);
1403 __state_->set_exception(__p);
1406 template <class _Rp>
1408 promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1410 if (__state_ == nullptr)
1411 __throw_future_error(future_errc::no_state);
1412 __state_->set_value_at_thread_exit(__r);
1415 template <class _Rp>
1417 promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1419 if (__state_ == nullptr)
1420 __throw_future_error(future_errc::no_state);
1421 __state_->set_value_at_thread_exit(_VSTD::move(__r));
1424 template <class _Rp>
1426 promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
1428 _LIBCPP_ASSERT_UNCATEGORIZED( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1429 if (__state_ == nullptr)
1430 __throw_future_error(future_errc::no_state);
1431 __state_->set_exception_at_thread_exit(__p);
1436 template <class _Rp>
1437 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
1439 __assoc_state<_Rp&>* __state_;
1441 _LIBCPP_INLINE_VISIBILITY
1442 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1444 template <class> friend class packaged_task;
1447 _LIBCPP_HIDE_FROM_ABI promise();
1448 template <class _Allocator>
1449 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1450 _LIBCPP_INLINE_VISIBILITY
1451 promise(promise&& __rhs) _NOEXCEPT
1452 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1453 promise(const promise& __rhs) = delete;
1454 _LIBCPP_HIDE_FROM_ABI ~promise();
1457 _LIBCPP_INLINE_VISIBILITY
1458 promise& operator=(promise&& __rhs) _NOEXCEPT
1460 promise(_VSTD::move(__rhs)).swap(*this);
1463 promise& operator=(const promise& __rhs) = delete;
1465 _LIBCPP_INLINE_VISIBILITY
1466 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1468 // retrieving the result
1469 _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1471 // setting the result
1472 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1473 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1475 // setting the result with deferred notification
1476 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1477 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1480 template <class _Rp>
1481 promise<_Rp&>::promise()
1482 : __state_(new __assoc_state<_Rp&>)
1486 template <class _Rp>
1487 template <class _Alloc>
1488 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1490 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1491 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1492 typedef __allocator_destructor<_A2> _D2;
1494 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1495 ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1496 __state_ = _VSTD::addressof(*__hold.release());
1499 template <class _Rp>
1500 promise<_Rp&>::~promise()
1504 if (!__state_->__has_value() && __state_->use_count() > 1)
1505 __state_->set_exception(make_exception_ptr(
1506 future_error(make_error_code(future_errc::broken_promise))
1508 __state_->__release_shared();
1512 template <class _Rp>
1514 promise<_Rp&>::get_future()
1516 if (__state_ == nullptr)
1517 __throw_future_error(future_errc::no_state);
1518 return future<_Rp&>(__state_);
1521 template <class _Rp>
1523 promise<_Rp&>::set_value(_Rp& __r)
1525 if (__state_ == nullptr)
1526 __throw_future_error(future_errc::no_state);
1527 __state_->set_value(__r);
1530 template <class _Rp>
1532 promise<_Rp&>::set_exception(exception_ptr __p)
1534 _LIBCPP_ASSERT_UNCATEGORIZED( __p != nullptr, "promise::set_exception: received nullptr" );
1535 if (__state_ == nullptr)
1536 __throw_future_error(future_errc::no_state);
1537 __state_->set_exception(__p);
1540 template <class _Rp>
1542 promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1544 if (__state_ == nullptr)
1545 __throw_future_error(future_errc::no_state);
1546 __state_->set_value_at_thread_exit(__r);
1549 template <class _Rp>
1551 promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
1553 _LIBCPP_ASSERT_UNCATEGORIZED( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1554 if (__state_ == nullptr)
1555 __throw_future_error(future_errc::no_state);
1556 __state_->set_exception_at_thread_exit(__p);
1562 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE promise<void>
1564 __assoc_sub_state* __state_;
1566 _LIBCPP_INLINE_VISIBILITY
1567 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1569 template <class> friend class packaged_task;
1573 template <class _Allocator>
1574 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1575 promise(allocator_arg_t, const _Allocator& __a);
1576 _LIBCPP_INLINE_VISIBILITY
1577 promise(promise&& __rhs) _NOEXCEPT
1578 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1579 promise(const promise& __rhs) = delete;
1583 _LIBCPP_INLINE_VISIBILITY
1584 promise& operator=(promise&& __rhs) _NOEXCEPT
1586 promise(_VSTD::move(__rhs)).swap(*this);
1589 promise& operator=(const promise& __rhs) = delete;
1591 _LIBCPP_INLINE_VISIBILITY
1592 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1594 // retrieving the result
1595 future<void> get_future();
1597 // setting the result
1599 void set_exception(exception_ptr __p);
1601 // setting the result with deferred notification
1602 void set_value_at_thread_exit();
1603 void set_exception_at_thread_exit(exception_ptr __p);
1606 template <class _Alloc>
1607 promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1609 typedef __assoc_sub_state_alloc<_Alloc> _State;
1610 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1611 typedef __allocator_destructor<_A2> _D2;
1613 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1614 ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1615 __state_ = _VSTD::addressof(*__hold.release());
1618 template <class _Rp>
1619 inline _LIBCPP_INLINE_VISIBILITY
1621 swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1626 template <class _Rp, class _Alloc>
1627 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
1628 : public true_type {};
1632 template<class _Fp> class __packaged_task_base;
1634 template<class _Rp, class ..._ArgTypes>
1635 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
1637 __packaged_task_base(const __packaged_task_base&);
1638 __packaged_task_base& operator=(const __packaged_task_base&);
1640 _LIBCPP_INLINE_VISIBILITY
1641 __packaged_task_base() {}
1642 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1643 virtual ~__packaged_task_base() {}
1644 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1645 virtual void destroy() = 0;
1646 virtual void destroy_deallocate() = 0;
1647 virtual _Rp operator()(_ArgTypes&& ...) = 0;
1650 template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1652 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1653 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1654 : public __packaged_task_base<_Rp(_ArgTypes...)>
1656 __compressed_pair<_Fp, _Alloc> __f_;
1658 _LIBCPP_INLINE_VISIBILITY
1659 explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
1660 _LIBCPP_INLINE_VISIBILITY
1661 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1662 _LIBCPP_INLINE_VISIBILITY
1663 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
1665 _LIBCPP_INLINE_VISIBILITY
1666 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
1667 : __f_(_VSTD::move(__f), __a) {}
1668 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1669 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1670 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1671 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&& ... __args);
1674 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1676 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1677 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1679 ::new ((void*)__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1682 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1684 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1686 __f_.~__compressed_pair<_Fp, _Alloc>();
1689 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1691 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
1693 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1694 typedef allocator_traits<_Ap> _ATraits;
1695 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1696 _Ap __a(__f_.second());
1697 __f_.~__compressed_pair<_Fp, _Alloc>();
1698 __a.deallocate(_PTraits::pointer_to(*this), 1);
1701 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1703 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1705 return _VSTD::__invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1708 template <class _Callable> class __packaged_task_function;
1710 template<class _Rp, class ..._ArgTypes>
1711 class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
1713 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1715 _LIBCPP_INLINE_VISIBILITY _LIBCPP_NO_CFI
1716 __base* __get_buf() { return (__base*)&__buf_; }
1718 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1719 typename aligned_storage<3*sizeof(void*)>::type __buf_;
1720 _LIBCPP_SUPPRESS_DEPRECATED_POP
1724 typedef _Rp result_type;
1726 // construct/copy/destroy:
1727 _LIBCPP_INLINE_VISIBILITY
1728 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1730 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1731 template<class _Fp, class _Alloc>
1732 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1734 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1735 _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1737 __packaged_task_function(const __packaged_task_function&) = delete;
1738 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1740 _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1742 _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1744 _LIBCPP_INLINE_VISIBILITY
1745 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1748 template<class _Rp, class ..._ArgTypes>
1749 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1751 if (__f.__f_ == nullptr)
1753 else if (__f.__f_ == __f.__get_buf())
1755 __f.__f_->__move_to(__get_buf());
1756 __f_ = (__base*)&__buf_;
1765 template<class _Rp, class ..._ArgTypes>
1766 template <class _Fp>
1767 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
1770 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1771 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1772 if (sizeof(_FF) <= sizeof(__buf_))
1774 ::new ((void*)&__buf_) _FF(_VSTD::forward<_Fp>(__f));
1775 __f_ = (__base*)&__buf_;
1779 typedef allocator<_FF> _Ap;
1781 typedef __allocator_destructor<_Ap> _Dp;
1782 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1783 ::new ((void*)__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
1784 __f_ = __hold.release();
1788 template<class _Rp, class ..._ArgTypes>
1789 template <class _Fp, class _Alloc>
1790 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1791 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1794 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1795 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1796 if (sizeof(_FF) <= sizeof(__buf_))
1798 __f_ = (__base*)&__buf_;
1799 ::new ((void*)__f_) _FF(_VSTD::forward<_Fp>(__f));
1803 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1805 typedef __allocator_destructor<_Ap> _Dp;
1806 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1807 ::new ((void*)_VSTD::addressof(*__hold.get()))
1808 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1809 __f_ = _VSTD::addressof(*__hold.release());
1813 template<class _Rp, class ..._ArgTypes>
1814 __packaged_task_function<_Rp(_ArgTypes...)>&
1815 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1817 if (__f_ == __get_buf())
1820 __f_->destroy_deallocate();
1822 if (__f.__f_ == nullptr)
1824 else if (__f.__f_ == __f.__get_buf())
1826 __f.__f_->__move_to(__get_buf());
1837 template<class _Rp, class ..._ArgTypes>
1838 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1840 if (__f_ == __get_buf())
1843 __f_->destroy_deallocate();
1846 template<class _Rp, class ..._ArgTypes>
1849 __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1851 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1853 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1854 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1855 _LIBCPP_SUPPRESS_DEPRECATED_POP
1856 __base* __t = (__base*)&__tempbuf;
1857 __f_->__move_to(__t);
1860 __f.__f_->__move_to((__base*)&__buf_);
1861 __f.__f_->destroy();
1863 __f_ = (__base*)&__buf_;
1864 __t->__move_to((__base*)&__f.__buf_);
1866 __f.__f_ = (__base*)&__f.__buf_;
1868 else if (__f_ == (__base*)&__buf_)
1870 __f_->__move_to((__base*)&__f.__buf_);
1873 __f.__f_ = (__base*)&__f.__buf_;
1875 else if (__f.__f_ == (__base*)&__f.__buf_)
1877 __f.__f_->__move_to((__base*)&__buf_);
1878 __f.__f_->destroy();
1880 __f_ = (__base*)&__buf_;
1883 _VSTD::swap(__f_, __f.__f_);
1886 template<class _Rp, class ..._ArgTypes>
1889 __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1891 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
1894 template<class _Rp, class ..._ArgTypes>
1895 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
1898 typedef _Rp result_type; // extension
1901 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1902 promise<result_type> __p_;
1905 // construction and destruction
1906 _LIBCPP_INLINE_VISIBILITY
1907 packaged_task() _NOEXCEPT : __p_(nullptr) {}
1908 template <class _Fp,
1909 class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1910 _LIBCPP_INLINE_VISIBILITY
1911 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
1912 template <class _Fp, class _Allocator,
1913 class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1914 _LIBCPP_INLINE_VISIBILITY
1915 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1916 : __f_(allocator_arg_t(), __a, _VSTD::forward<_Fp>(__f)),
1917 __p_(allocator_arg_t(), __a) {}
1918 // ~packaged_task() = default;
1921 packaged_task(const packaged_task&) = delete;
1922 packaged_task& operator=(const packaged_task&) = delete;
1925 _LIBCPP_INLINE_VISIBILITY
1926 packaged_task(packaged_task&& __other) _NOEXCEPT
1927 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
1928 _LIBCPP_INLINE_VISIBILITY
1929 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
1931 __f_ = _VSTD::move(__other.__f_);
1932 __p_ = _VSTD::move(__other.__p_);
1935 _LIBCPP_INLINE_VISIBILITY
1936 void swap(packaged_task& __other) _NOEXCEPT
1938 __f_.swap(__other.__f_);
1939 __p_.swap(__other.__p_);
1942 _LIBCPP_INLINE_VISIBILITY
1943 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
1946 _LIBCPP_INLINE_VISIBILITY
1947 future<result_type> get_future() {return __p_.get_future();}
1950 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1951 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1953 _LIBCPP_HIDE_FROM_ABI void reset();
1956 template<class _Rp, class ..._ArgTypes>
1958 packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
1960 if (__p_.__state_ == nullptr)
1961 __throw_future_error(future_errc::no_state);
1962 if (__p_.__state_->__has_value())
1963 __throw_future_error(future_errc::promise_already_satisfied);
1964 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1967 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1968 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
1969 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1973 __p_.set_exception(current_exception());
1975 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1978 template<class _Rp, class ..._ArgTypes>
1980 packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
1982 if (__p_.__state_ == nullptr)
1983 __throw_future_error(future_errc::no_state);
1984 if (__p_.__state_->__has_value())
1985 __throw_future_error(future_errc::promise_already_satisfied);
1986 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1989 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1990 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
1991 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1995 __p_.set_exception_at_thread_exit(current_exception());
1997 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2000 template<class _Rp, class ..._ArgTypes>
2002 packaged_task<_Rp(_ArgTypes...)>::reset()
2005 __throw_future_error(future_errc::no_state);
2006 __p_ = promise<result_type>();
2009 template<class ..._ArgTypes>
2010 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
2013 typedef void result_type; // extension
2016 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2017 promise<result_type> __p_;
2020 // construction and destruction
2021 _LIBCPP_INLINE_VISIBILITY
2022 packaged_task() _NOEXCEPT : __p_(nullptr) {}
2023 template <class _Fp,
2024 class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
2025 _LIBCPP_INLINE_VISIBILITY
2026 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2027 template <class _Fp, class _Allocator,
2028 class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
2029 _LIBCPP_INLINE_VISIBILITY
2030 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2031 : __f_(allocator_arg_t(), __a, _VSTD::forward<_Fp>(__f)),
2032 __p_(allocator_arg_t(), __a) {}
2033 // ~packaged_task() = default;
2036 packaged_task(const packaged_task&) = delete;
2037 packaged_task& operator=(const packaged_task&) = delete;
2040 _LIBCPP_INLINE_VISIBILITY
2041 packaged_task(packaged_task&& __other) _NOEXCEPT
2042 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2043 _LIBCPP_INLINE_VISIBILITY
2044 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2046 __f_ = _VSTD::move(__other.__f_);
2047 __p_ = _VSTD::move(__other.__p_);
2050 _LIBCPP_INLINE_VISIBILITY
2051 void swap(packaged_task& __other) _NOEXCEPT
2053 __f_.swap(__other.__f_);
2054 __p_.swap(__other.__p_);
2057 _LIBCPP_INLINE_VISIBILITY
2058 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2061 _LIBCPP_INLINE_VISIBILITY
2062 future<result_type> get_future() {return __p_.get_future();}
2065 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
2066 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
2068 _LIBCPP_HIDE_FROM_ABI void reset();
2071 #if _LIBCPP_STD_VER >= 17
2073 template <class _Rp, class... _Args>
2074 packaged_task(_Rp(*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
2076 template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2077 packaged_task(_Fp) -> packaged_task<_Stripped>;
2081 template<class ..._ArgTypes>
2083 packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2085 if (__p_.__state_ == nullptr)
2086 __throw_future_error(future_errc::no_state);
2087 if (__p_.__state_->__has_value())
2088 __throw_future_error(future_errc::promise_already_satisfied);
2089 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2092 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2093 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2095 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2099 __p_.set_exception(current_exception());
2101 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2104 template<class ..._ArgTypes>
2106 packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2108 if (__p_.__state_ == nullptr)
2109 __throw_future_error(future_errc::no_state);
2110 if (__p_.__state_->__has_value())
2111 __throw_future_error(future_errc::promise_already_satisfied);
2112 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2115 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2116 __f_(_VSTD::forward<_ArgTypes>(__args)...);
2117 __p_.set_value_at_thread_exit();
2118 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2122 __p_.set_exception_at_thread_exit(current_exception());
2124 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2127 template<class ..._ArgTypes>
2129 packaged_task<void(_ArgTypes...)>::reset()
2132 __throw_future_error(future_errc::no_state);
2133 __p_ = promise<result_type>();
2136 template <class _Rp, class... _ArgTypes>
2137 inline _LIBCPP_INLINE_VISIBILITY
2139 swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
2144 template <class _Callable, class _Alloc>
2145 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
2146 : public true_type {};
2148 template <class _Rp, class _Fp>
2149 _LIBCPP_INLINE_VISIBILITY future<_Rp>
2150 __make_deferred_assoc_state(_Fp&& __f)
2152 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2153 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2154 return future<_Rp>(__h.get());
2157 template <class _Rp, class _Fp>
2158 _LIBCPP_INLINE_VISIBILITY future<_Rp>
2159 __make_async_assoc_state(_Fp&& __f)
2161 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2162 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2163 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2164 return future<_Rp>(__h.get());
2167 #ifndef _LIBCPP_CXX03_LANG
2169 template <class _Fp, class... _Args>
2170 class _LIBCPP_HIDDEN __async_func
2172 tuple<_Fp, _Args...> __f_;
2175 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2177 _LIBCPP_INLINE_VISIBILITY
2178 explicit __async_func(_Fp&& __f, _Args&&... __args)
2179 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2181 _LIBCPP_INLINE_VISIBILITY
2182 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2184 _LIBCPP_HIDE_FROM_ABI _Rp operator()()
2186 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2187 return __execute(_Index());
2190 template <size_t ..._Indices>
2191 _LIBCPP_HIDE_FROM_ABI _Rp
2192 __execute(__tuple_indices<_Indices...>)
2194 return _VSTD::__invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2198 inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2199 { return (int(__policy) & int(__value)) != 0; }
2201 template <class _Fp, class... _Args>
2202 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
2203 future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
2204 async(launch __policy, _Fp&& __f, _Args&&... __args)
2206 typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
2207 typedef typename _BF::_Rp _Rp;
2209 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2213 if (__does_policy_contain(__policy, launch::async))
2214 return _VSTD::__make_async_assoc_state<_Rp>(_BF(_LIBCPP_AUTO_CAST(_VSTD::forward<_Fp>(__f)),
2215 _LIBCPP_AUTO_CAST(_VSTD::forward<_Args>(__args))...));
2216 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2218 catch ( ... ) { if (__policy == launch::async) throw ; }
2221 if (__does_policy_contain(__policy, launch::deferred))
2222 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(_LIBCPP_AUTO_CAST(_VSTD::forward<_Fp>(__f)),
2223 _LIBCPP_AUTO_CAST(_VSTD::forward<_Args>(__args))...));
2224 return future<_Rp>{};
2227 template <class _Fp, class... _Args>
2228 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
2229 future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
2230 async(_Fp&& __f, _Args&&... __args)
2232 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2233 _VSTD::forward<_Args>(__args)...);
2240 template <class _Rp>
2241 class _LIBCPP_TEMPLATE_VIS shared_future
2243 __assoc_state<_Rp>* __state_;
2246 _LIBCPP_INLINE_VISIBILITY
2247 shared_future() _NOEXCEPT : __state_(nullptr) {}
2248 _LIBCPP_INLINE_VISIBILITY
2249 shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2250 {if (__state_) __state_->__add_shared();}
2251 _LIBCPP_INLINE_VISIBILITY
2252 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
2253 {__f.__state_ = nullptr;}
2254 _LIBCPP_INLINE_VISIBILITY
2255 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2256 {__rhs.__state_ = nullptr;}
2257 _LIBCPP_HIDE_FROM_ABI ~shared_future();
2258 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
2259 _LIBCPP_INLINE_VISIBILITY
2260 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2262 shared_future(_VSTD::move(__rhs)).swap(*this);
2266 // retrieving the value
2267 _LIBCPP_INLINE_VISIBILITY
2268 const _Rp& get() const {return __state_->copy();}
2270 _LIBCPP_INLINE_VISIBILITY
2271 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2273 // functions to check state
2274 _LIBCPP_INLINE_VISIBILITY
2275 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2277 _LIBCPP_INLINE_VISIBILITY
2278 void wait() const {__state_->wait();}
2279 template <class _Rep, class _Period>
2280 _LIBCPP_INLINE_VISIBILITY
2282 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2283 {return __state_->wait_for(__rel_time);}
2284 template <class _Clock, class _Duration>
2285 _LIBCPP_INLINE_VISIBILITY
2287 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2288 {return __state_->wait_until(__abs_time);}
2291 template <class _Rp>
2292 shared_future<_Rp>::~shared_future()
2295 __state_->__release_shared();
2298 template <class _Rp>
2300 shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
2303 __rhs.__state_->__add_shared();
2305 __state_->__release_shared();
2306 __state_ = __rhs.__state_;
2310 template <class _Rp>
2311 class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
2313 __assoc_state<_Rp&>* __state_;
2316 _LIBCPP_INLINE_VISIBILITY
2317 shared_future() _NOEXCEPT : __state_(nullptr) {}
2318 _LIBCPP_INLINE_VISIBILITY
2319 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2320 {if (__state_) __state_->__add_shared();}
2321 _LIBCPP_INLINE_VISIBILITY
2322 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
2323 {__f.__state_ = nullptr;}
2324 _LIBCPP_INLINE_VISIBILITY
2325 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2326 {__rhs.__state_ = nullptr;}
2327 _LIBCPP_HIDE_FROM_ABI ~shared_future();
2328 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
2329 _LIBCPP_INLINE_VISIBILITY
2330 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2332 shared_future(_VSTD::move(__rhs)).swap(*this);
2336 // retrieving the value
2337 _LIBCPP_INLINE_VISIBILITY
2338 _Rp& get() const {return __state_->copy();}
2340 _LIBCPP_INLINE_VISIBILITY
2341 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2343 // functions to check state
2344 _LIBCPP_INLINE_VISIBILITY
2345 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2347 _LIBCPP_INLINE_VISIBILITY
2348 void wait() const {__state_->wait();}
2349 template <class _Rep, class _Period>
2350 _LIBCPP_INLINE_VISIBILITY
2352 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2353 {return __state_->wait_for(__rel_time);}
2354 template <class _Clock, class _Duration>
2355 _LIBCPP_INLINE_VISIBILITY
2357 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2358 {return __state_->wait_until(__abs_time);}
2361 template <class _Rp>
2362 shared_future<_Rp&>::~shared_future()
2365 __state_->__release_shared();
2368 template <class _Rp>
2369 shared_future<_Rp&>&
2370 shared_future<_Rp&>::operator=(const shared_future& __rhs)
2373 __rhs.__state_->__add_shared();
2375 __state_->__release_shared();
2376 __state_ = __rhs.__state_;
2381 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
2383 __assoc_sub_state* __state_;
2386 _LIBCPP_INLINE_VISIBILITY
2387 shared_future() _NOEXCEPT : __state_(nullptr) {}
2388 _LIBCPP_INLINE_VISIBILITY
2389 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2390 {if (__state_) __state_->__add_shared();}
2391 _LIBCPP_INLINE_VISIBILITY
2392 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
2393 {__f.__state_ = nullptr;}
2394 _LIBCPP_INLINE_VISIBILITY
2395 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2396 {__rhs.__state_ = nullptr;}
2398 shared_future& operator=(const shared_future& __rhs);
2399 _LIBCPP_INLINE_VISIBILITY
2400 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2402 shared_future(_VSTD::move(__rhs)).swap(*this);
2406 // retrieving the value
2407 _LIBCPP_INLINE_VISIBILITY
2408 void get() const {__state_->copy();}
2410 _LIBCPP_INLINE_VISIBILITY
2411 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2413 // functions to check state
2414 _LIBCPP_INLINE_VISIBILITY
2415 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2417 _LIBCPP_INLINE_VISIBILITY
2418 void wait() const {__state_->wait();}
2419 template <class _Rep, class _Period>
2420 _LIBCPP_INLINE_VISIBILITY
2422 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2423 {return __state_->wait_for(__rel_time);}
2424 template <class _Clock, class _Duration>
2425 _LIBCPP_INLINE_VISIBILITY
2427 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2428 {return __state_->wait_until(__abs_time);}
2431 template <class _Rp>
2432 inline _LIBCPP_INLINE_VISIBILITY
2434 swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2439 template <class _Rp>
2442 future<_Rp>::share() _NOEXCEPT
2444 return shared_future<_Rp>(_VSTD::move(*this));
2447 template <class _Rp>
2450 future<_Rp&>::share() _NOEXCEPT
2452 return shared_future<_Rp&>(_VSTD::move(*this));
2457 future<void>::share() _NOEXCEPT
2459 return shared_future<void>(_VSTD::move(*this));
2462 _LIBCPP_END_NAMESPACE_STD
2464 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2468 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2471 # include <exception>
2472 # include <system_error>
2475 #endif // _LIBCPP_FUTURE