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>;
367 #ifdef _LIBCPP_HAS_NO_THREADS
368 # error "<future> is not supported since libc++ has been configured without support for threads."
371 #include <__assert> // all public C++ headers provide the assertion handler
372 #include <__availability>
373 #include <__chrono/duration.h>
374 #include <__chrono/time_point.h>
375 #include <__exception/exception_ptr.h>
376 #include <__memory/addressof.h>
377 #include <__memory/allocator.h>
378 #include <__memory/allocator_arg_t.h>
379 #include <__memory/allocator_destructor.h>
380 #include <__memory/allocator_traits.h>
381 #include <__memory/compressed_pair.h>
382 #include <__memory/pointer_traits.h>
383 #include <__memory/shared_ptr.h>
384 #include <__memory/unique_ptr.h>
385 #include <__memory/uses_allocator.h>
386 #include <__system_error/error_category.h>
387 #include <__system_error/error_code.h>
388 #include <__system_error/error_condition.h>
389 #include <__type_traits/aligned_storage.h>
390 #include <__type_traits/strip_signature.h>
391 #include <__utility/auto_cast.h>
392 #include <__utility/forward.h>
393 #include <__utility/move.h>
400 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
401 # pragma GCC system_header
405 #include <__undef_macros>
407 _LIBCPP_BEGIN_NAMESPACE_STD
409 // enum class future_errc
410 _LIBCPP_DECLARE_STRONG_ENUM(future_errc){
411 future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};
412 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
415 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
417 #ifdef _LIBCPP_CXX03_LANG
419 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type {};
423 _LIBCPP_DECLARE_STRONG_ENUM(launch){async = 1, deferred = 2, any = async | deferred};
424 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
426 #ifndef _LIBCPP_CXX03_LANG
428 typedef underlying_type<launch>::type __launch_underlying_type;
430 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator&(launch __x, launch __y) {
431 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));
434 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator|(launch __x, launch __y) {
435 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | static_cast<__launch_underlying_type>(__y));
438 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator^(launch __x, launch __y) {
439 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));
442 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator~(launch __x) {
443 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
446 inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {
451 inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {
456 inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {
461 #endif // !_LIBCPP_CXX03_LANG
463 // enum class future_status
464 _LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};
465 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
467 _LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
469 inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {
470 return error_code(static_cast<int>(__e), future_category());
473 inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {
474 return error_condition(static_cast<int>(__e), future_category());
477 _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);
479 class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {
482 future_error(error_code);
483 friend void __throw_future_error(future_errc);
485 friend class promise;
488 #if _LIBCPP_STD_VER >= 17
489 _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
492 _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
494 _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
495 ~future_error() _NOEXCEPT override;
498 // Declared above std::future_error
499 void __throw_future_error(future_errc __ev) {
500 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
501 throw future_error(make_error_code(__ev));
504 _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
508 class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {
510 exception_ptr __exception_;
511 mutable mutex __mut_;
512 mutable condition_variable __cv_;
515 void __on_zero_shared() _NOEXCEPT override;
516 void __sub_wait(unique_lock<mutex>& __lk);
519 enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };
521 _LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}
523 _LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }
525 _LIBCPP_HIDE_FROM_ABI void __attach_future() {
526 lock_guard<mutex> __lk(__mut_);
527 bool __has_future_attached = (__state_ & __future_attached) != 0;
528 if (__has_future_attached)
529 __throw_future_error(future_errc::future_already_retrieved);
530 this->__add_shared();
531 __state_ |= __future_attached;
534 _LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }
537 _LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }
540 void set_value_at_thread_exit();
542 void set_exception(exception_ptr __p);
543 void set_exception_at_thread_exit(exception_ptr __p);
548 template <class _Rep, class _Period>
549 future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
550 template <class _Clock, class _Duration>
551 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS future_status
552 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
554 virtual void __execute();
557 template <class _Clock, class _Duration>
558 future_status __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
559 unique_lock<mutex> __lk(__mut_);
560 if (__state_ & deferred)
561 return future_status::deferred;
562 while (!(__state_ & ready) && _Clock::now() < __abs_time)
563 __cv_.wait_until(__lk, __abs_time);
564 if (__state_ & ready)
565 return future_status::ready;
566 return future_status::timeout;
569 template <class _Rep, class _Period>
570 inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
571 return wait_until(chrono::steady_clock::now() + __rel_time);
575 class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {
576 typedef __assoc_sub_state base;
577 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
578 typedef typename aligned_storage<sizeof(_Rp), _LIBCPP_ALIGNOF(_Rp)>::type _Up;
579 _LIBCPP_SUPPRESS_DEPRECATED_POP
584 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
587 template <class _Arg>
588 _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
590 template <class _Arg>
591 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
593 _LIBCPP_HIDE_FROM_ABI _Rp move();
594 _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<_Rp> copy();
598 void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {
599 if (this->__state_ & base::__constructed)
600 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
605 template <class _Arg>
606 void __assoc_state<_Rp>::set_value(_Arg&& __arg) {
607 unique_lock<mutex> __lk(this->__mut_);
608 if (this->__has_value())
609 __throw_future_error(future_errc::promise_already_satisfied);
610 ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
611 this->__state_ |= base::__constructed | base::ready;
616 template <class _Arg>
617 void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {
618 unique_lock<mutex> __lk(this->__mut_);
619 if (this->__has_value())
620 __throw_future_error(future_errc::promise_already_satisfied);
621 ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
622 this->__state_ |= base::__constructed;
623 __thread_local_data()->__make_ready_at_thread_exit(this);
627 _Rp __assoc_state<_Rp>::move() {
628 unique_lock<mutex> __lk(this->__mut_);
629 this->__sub_wait(__lk);
630 if (this->__exception_ != nullptr)
631 std::rethrow_exception(this->__exception_);
632 return std::move(*reinterpret_cast<_Rp*>(&__value_));
636 __add_lvalue_reference_t<_Rp> __assoc_state<_Rp>::copy() {
637 unique_lock<mutex> __lk(this->__mut_);
638 this->__sub_wait(__lk);
639 if (this->__exception_ != nullptr)
640 std::rethrow_exception(this->__exception_);
641 return *reinterpret_cast<_Rp*>(&__value_);
645 class __assoc_state<_Rp&> : public __assoc_sub_state {
646 typedef __assoc_sub_state base;
652 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
655 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
656 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
658 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
662 void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {
667 void __assoc_state<_Rp&>::set_value(_Rp& __arg) {
668 unique_lock<mutex> __lk(this->__mut_);
669 if (this->__has_value())
670 __throw_future_error(future_errc::promise_already_satisfied);
671 __value_ = std::addressof(__arg);
672 this->__state_ |= base::__constructed | base::ready;
677 void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {
678 unique_lock<mutex> __lk(this->__mut_);
679 if (this->__has_value())
680 __throw_future_error(future_errc::promise_already_satisfied);
681 __value_ = std::addressof(__arg);
682 this->__state_ |= base::__constructed;
683 __thread_local_data()->__make_ready_at_thread_exit(this);
687 _Rp& __assoc_state<_Rp&>::copy() {
688 unique_lock<mutex> __lk(this->__mut_);
689 this->__sub_wait(__lk);
690 if (this->__exception_ != nullptr)
691 std::rethrow_exception(this->__exception_);
695 template <class _Rp, class _Alloc>
696 class __assoc_state_alloc : public __assoc_state<_Rp> {
697 typedef __assoc_state<_Rp> base;
700 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
703 _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
706 template <class _Rp, class _Alloc>
707 void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {
708 if (this->__state_ & base::__constructed)
709 reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();
710 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
711 typedef allocator_traits<_Al> _ATraits;
712 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
714 this->~__assoc_state_alloc();
715 __a.deallocate(_PTraits::pointer_to(*this), 1);
718 template <class _Rp, class _Alloc>
719 class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {
720 typedef __assoc_state<_Rp&> base;
723 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
726 _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
729 template <class _Rp, class _Alloc>
730 void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {
731 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
732 typedef allocator_traits<_Al> _ATraits;
733 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
735 this->~__assoc_state_alloc();
736 __a.deallocate(_PTraits::pointer_to(*this), 1);
739 template <class _Alloc>
740 class __assoc_sub_state_alloc : public __assoc_sub_state {
741 typedef __assoc_sub_state base;
744 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
747 _LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
750 template <class _Alloc>
751 void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {
752 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
753 typedef allocator_traits<_Al> _ATraits;
754 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
756 this->~__assoc_sub_state_alloc();
757 __a.deallocate(_PTraits::pointer_to(*this), 1);
760 template <class _Rp, class _Fp>
761 class __deferred_assoc_state : public __assoc_state<_Rp> {
762 typedef __assoc_state<_Rp> base;
767 _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
769 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
772 template <class _Rp, class _Fp>
773 inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
774 this->__set_deferred();
777 template <class _Rp, class _Fp>
778 void __deferred_assoc_state<_Rp, _Fp>::__execute() {
779 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
781 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
782 this->set_value(__func_());
783 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
785 this->set_exception(current_exception());
787 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
791 class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {
792 typedef __assoc_sub_state base;
797 _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
799 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
803 inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
804 this->__set_deferred();
808 void __deferred_assoc_state<void, _Fp>::__execute() {
809 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
811 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
814 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
816 this->set_exception(current_exception());
818 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
821 template <class _Rp, class _Fp>
822 class __async_assoc_state : public __assoc_state<_Rp> {
823 typedef __assoc_state<_Rp> base;
827 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
830 _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
832 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
835 template <class _Rp, class _Fp>
836 inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
838 template <class _Rp, class _Fp>
839 void __async_assoc_state<_Rp, _Fp>::__execute() {
840 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
842 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
843 this->set_value(__func_());
844 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
846 this->set_exception(current_exception());
848 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
851 template <class _Rp, class _Fp>
852 void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {
854 base::__on_zero_shared();
858 class __async_assoc_state<void, _Fp> : public __assoc_sub_state {
859 typedef __assoc_sub_state base;
863 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
866 _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
868 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
872 inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
875 void __async_assoc_state<void, _Fp>::__execute() {
876 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
878 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
881 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
883 this->set_exception(current_exception());
885 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
889 void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {
891 base::__on_zero_shared();
895 class _LIBCPP_TEMPLATE_VIS promise;
897 class _LIBCPP_TEMPLATE_VIS shared_future;
902 class _LIBCPP_TEMPLATE_VIS future;
904 template <class _Rp, class _Fp>
905 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);
907 template <class _Rp, class _Fp>
908 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);
911 class _LIBCPP_TEMPLATE_VIS future {
912 __assoc_state<_Rp>* __state_;
914 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
917 friend class promise;
919 friend class shared_future;
921 template <class _R1, class _Fp>
922 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
923 template <class _R1, class _Fp>
924 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
927 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
928 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
929 future(const future&) = delete;
930 future& operator=(const future&) = delete;
931 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
932 future(std::move(__rhs)).swap(*this);
936 _LIBCPP_HIDE_FROM_ABI ~future();
937 _LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;
939 // retrieving the value
940 _LIBCPP_HIDE_FROM_ABI _Rp get();
942 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
944 // functions to check state
945 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
947 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
948 template <class _Rep, class _Period>
949 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
950 return __state_->wait_for(__rel_time);
952 template <class _Clock, class _Duration>
953 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
954 return __state_->wait_until(__abs_time);
959 future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {
960 __state_->__attach_future();
963 struct __release_shared_count {
964 _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }
968 future<_Rp>::~future() {
970 __state_->__release_shared();
974 _Rp future<_Rp>::get() {
975 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
976 __assoc_state<_Rp>* __s = __state_;
982 class _LIBCPP_TEMPLATE_VIS future<_Rp&> {
983 __assoc_state<_Rp&>* __state_;
985 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
988 friend class promise;
990 friend class shared_future;
992 template <class _R1, class _Fp>
993 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
994 template <class _R1, class _Fp>
995 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
998 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
999 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1000 future(const future&) = delete;
1001 future& operator=(const future&) = delete;
1002 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1003 future(std::move(__rhs)).swap(*this);
1007 _LIBCPP_HIDE_FROM_ABI ~future();
1008 _LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;
1010 // retrieving the value
1011 _LIBCPP_HIDE_FROM_ABI _Rp& get();
1013 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1015 // functions to check state
1016 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1018 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1019 template <class _Rep, class _Period>
1020 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1021 return __state_->wait_for(__rel_time);
1023 template <class _Clock, class _Duration>
1024 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1025 return __state_->wait_until(__abs_time);
1029 template <class _Rp>
1030 future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {
1031 __state_->__attach_future();
1034 template <class _Rp>
1035 future<_Rp&>::~future() {
1037 __state_->__release_shared();
1040 template <class _Rp>
1041 _Rp& future<_Rp&>::get() {
1042 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1043 __assoc_state<_Rp&>* __s = __state_;
1049 class _LIBCPP_EXPORTED_FROM_ABI future<void> {
1050 __assoc_sub_state* __state_;
1052 explicit future(__assoc_sub_state* __state);
1055 friend class promise;
1057 friend class shared_future;
1059 template <class _R1, class _Fp>
1060 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1061 template <class _R1, class _Fp>
1062 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1065 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1066 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1067 future(const future&) = delete;
1068 future& operator=(const future&) = delete;
1069 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1070 future(std::move(__rhs)).swap(*this);
1075 _LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;
1077 // retrieving the value
1080 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1082 // functions to check state
1083 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1085 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1086 template <class _Rep, class _Period>
1087 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1088 return __state_->wait_for(__rel_time);
1090 template <class _Clock, class _Duration>
1091 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1092 return __state_->wait_until(__abs_time);
1096 template <class _Rp>
1097 inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {
1103 template <class _Callable>
1104 class packaged_task;
1106 template <class _Rp>
1107 class _LIBCPP_TEMPLATE_VIS promise {
1108 __assoc_state<_Rp>* __state_;
1110 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1113 friend class packaged_task;
1116 _LIBCPP_HIDE_FROM_ABI promise();
1117 template <class _Alloc>
1118 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1119 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1120 promise(const promise& __rhs) = delete;
1121 _LIBCPP_HIDE_FROM_ABI ~promise();
1124 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1125 promise(std::move(__rhs)).swap(*this);
1128 promise& operator=(const promise& __rhs) = delete;
1130 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1132 // retrieving the result
1133 _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1135 // setting the result
1136 _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1137 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1138 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1140 // setting the result with deferred notification
1141 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1142 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1143 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1146 template <class _Rp>
1147 promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}
1149 template <class _Rp>
1150 template <class _Alloc>
1151 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {
1152 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1153 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1154 typedef __allocator_destructor<_A2> _D2;
1156 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1157 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1158 __state_ = std::addressof(*__hold.release());
1161 template <class _Rp>
1162 promise<_Rp>::~promise() {
1164 if (!__state_->__has_value() && __state_->use_count() > 1)
1165 __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1166 __state_->__release_shared();
1170 template <class _Rp>
1171 future<_Rp> promise<_Rp>::get_future() {
1172 if (__state_ == nullptr)
1173 __throw_future_error(future_errc::no_state);
1174 return future<_Rp>(__state_);
1177 template <class _Rp>
1178 void promise<_Rp>::set_value(const _Rp& __r) {
1179 if (__state_ == nullptr)
1180 __throw_future_error(future_errc::no_state);
1181 __state_->set_value(__r);
1184 template <class _Rp>
1185 void promise<_Rp>::set_value(_Rp&& __r) {
1186 if (__state_ == nullptr)
1187 __throw_future_error(future_errc::no_state);
1188 __state_->set_value(std::move(__r));
1191 template <class _Rp>
1192 void promise<_Rp>::set_exception(exception_ptr __p) {
1193 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1194 if (__state_ == nullptr)
1195 __throw_future_error(future_errc::no_state);
1196 __state_->set_exception(__p);
1199 template <class _Rp>
1200 void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {
1201 if (__state_ == nullptr)
1202 __throw_future_error(future_errc::no_state);
1203 __state_->set_value_at_thread_exit(__r);
1206 template <class _Rp>
1207 void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {
1208 if (__state_ == nullptr)
1209 __throw_future_error(future_errc::no_state);
1210 __state_->set_value_at_thread_exit(std::move(__r));
1213 template <class _Rp>
1214 void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {
1215 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1216 if (__state_ == nullptr)
1217 __throw_future_error(future_errc::no_state);
1218 __state_->set_exception_at_thread_exit(__p);
1223 template <class _Rp>
1224 class _LIBCPP_TEMPLATE_VIS promise<_Rp&> {
1225 __assoc_state<_Rp&>* __state_;
1227 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1230 friend class packaged_task;
1233 _LIBCPP_HIDE_FROM_ABI promise();
1234 template <class _Allocator>
1235 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1236 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1237 promise(const promise& __rhs) = delete;
1238 _LIBCPP_HIDE_FROM_ABI ~promise();
1241 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1242 promise(std::move(__rhs)).swap(*this);
1245 promise& operator=(const promise& __rhs) = delete;
1247 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1249 // retrieving the result
1250 _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1252 // setting the result
1253 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1254 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1256 // setting the result with deferred notification
1257 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1258 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1261 template <class _Rp>
1262 promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}
1264 template <class _Rp>
1265 template <class _Alloc>
1266 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {
1267 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1268 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1269 typedef __allocator_destructor<_A2> _D2;
1271 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1272 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1273 __state_ = std::addressof(*__hold.release());
1276 template <class _Rp>
1277 promise<_Rp&>::~promise() {
1279 if (!__state_->__has_value() && __state_->use_count() > 1)
1280 __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1281 __state_->__release_shared();
1285 template <class _Rp>
1286 future<_Rp&> promise<_Rp&>::get_future() {
1287 if (__state_ == nullptr)
1288 __throw_future_error(future_errc::no_state);
1289 return future<_Rp&>(__state_);
1292 template <class _Rp>
1293 void promise<_Rp&>::set_value(_Rp& __r) {
1294 if (__state_ == nullptr)
1295 __throw_future_error(future_errc::no_state);
1296 __state_->set_value(__r);
1299 template <class _Rp>
1300 void promise<_Rp&>::set_exception(exception_ptr __p) {
1301 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1302 if (__state_ == nullptr)
1303 __throw_future_error(future_errc::no_state);
1304 __state_->set_exception(__p);
1307 template <class _Rp>
1308 void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {
1309 if (__state_ == nullptr)
1310 __throw_future_error(future_errc::no_state);
1311 __state_->set_value_at_thread_exit(__r);
1314 template <class _Rp>
1315 void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {
1316 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1317 if (__state_ == nullptr)
1318 __throw_future_error(future_errc::no_state);
1319 __state_->set_exception_at_thread_exit(__p);
1325 class _LIBCPP_EXPORTED_FROM_ABI promise<void> {
1326 __assoc_sub_state* __state_;
1328 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1331 friend class packaged_task;
1335 template <class _Allocator>
1336 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS promise(allocator_arg_t, const _Allocator& __a);
1337 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1338 promise(const promise& __rhs) = delete;
1342 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1343 promise(std::move(__rhs)).swap(*this);
1346 promise& operator=(const promise& __rhs) = delete;
1348 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1350 // retrieving the result
1351 future<void> get_future();
1353 // setting the result
1355 void set_exception(exception_ptr __p);
1357 // setting the result with deferred notification
1358 void set_value_at_thread_exit();
1359 void set_exception_at_thread_exit(exception_ptr __p);
1362 template <class _Alloc>
1363 promise<void>::promise(allocator_arg_t, const _Alloc& __a0) {
1364 typedef __assoc_sub_state_alloc<_Alloc> _State;
1365 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1366 typedef __allocator_destructor<_A2> _D2;
1368 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1369 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1370 __state_ = std::addressof(*__hold.release());
1373 template <class _Rp>
1374 inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {
1378 template <class _Rp, class _Alloc>
1379 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc> : public true_type {};
1383 template <class _Fp>
1384 class __packaged_task_base;
1386 template <class _Rp, class... _ArgTypes>
1387 class __packaged_task_base<_Rp(_ArgTypes...)> {
1388 __packaged_task_base(const __packaged_task_base&);
1389 __packaged_task_base& operator=(const __packaged_task_base&);
1392 _LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}
1393 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1394 virtual ~__packaged_task_base() {}
1395 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1396 virtual void destroy() = 0;
1397 virtual void destroy_deallocate() = 0;
1398 virtual _Rp operator()(_ArgTypes&&...) = 0;
1401 template <class _FD, class _Alloc, class _FB>
1402 class __packaged_task_func;
1404 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1405 class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
1406 __compressed_pair<_Fp, _Alloc> __f_;
1409 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
1410 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __f_(std::move(__f), __default_init_tag()) {}
1411 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __f_(__f, __a) {}
1412 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __f_(std::move(__f), __a) {}
1413 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1414 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1415 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1416 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);
1419 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1420 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1421 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {
1422 ::new ((void*)__p) __packaged_task_func(std::move(__f_.first()), std::move(__f_.second()));
1425 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1426 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
1427 __f_.~__compressed_pair<_Fp, _Alloc>();
1430 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1431 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {
1432 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1433 typedef allocator_traits<_Ap> _ATraits;
1434 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1435 _Ap __a(__f_.second());
1436 __f_.~__compressed_pair<_Fp, _Alloc>();
1437 __a.deallocate(_PTraits::pointer_to(*this), 1);
1440 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1441 _Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
1442 return std::__invoke(__f_.first(), std::forward<_ArgTypes>(__arg)...);
1445 template <class _Callable>
1446 class __packaged_task_function;
1448 template <class _Rp, class... _ArgTypes>
1449 class __packaged_task_function<_Rp(_ArgTypes...)> {
1450 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1452 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }
1454 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1455 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1456 _LIBCPP_SUPPRESS_DEPRECATED_POP
1460 typedef _Rp result_type;
1462 // construct/copy/destroy:
1463 _LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1464 template <class _Fp>
1465 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1466 template <class _Fp, class _Alloc>
1467 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1469 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1470 _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1472 __packaged_task_function(const __packaged_task_function&) = delete;
1473 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1475 _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1477 _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1479 _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1482 template <class _Rp, class... _ArgTypes>
1483 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {
1484 if (__f.__f_ == nullptr)
1486 else if (__f.__f_ == __f.__get_buf()) {
1487 __f.__f_->__move_to(__get_buf());
1488 __f_ = (__base*)&__buf_;
1495 template <class _Rp, class... _ArgTypes>
1496 template <class _Fp>
1497 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {
1498 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1499 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1500 if (sizeof(_FF) <= sizeof(__buf_)) {
1501 ::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));
1502 __f_ = (__base*)&__buf_;
1504 typedef allocator<_FF> _Ap;
1506 typedef __allocator_destructor<_Ap> _Dp;
1507 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1508 ::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));
1509 __f_ = __hold.release();
1513 template <class _Rp, class... _ArgTypes>
1514 template <class _Fp, class _Alloc>
1515 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1517 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1518 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1519 if (sizeof(_FF) <= sizeof(__buf_)) {
1520 __f_ = (__base*)&__buf_;
1521 ::new ((void*)__f_) _FF(std::forward<_Fp>(__f));
1523 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1525 typedef __allocator_destructor<_Ap> _Dp;
1526 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1527 ::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));
1528 __f_ = std::addressof(*__hold.release());
1532 template <class _Rp, class... _ArgTypes>
1533 __packaged_task_function<_Rp(_ArgTypes...)>&
1534 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {
1535 if (__f_ == __get_buf())
1538 __f_->destroy_deallocate();
1540 if (__f.__f_ == nullptr)
1542 else if (__f.__f_ == __f.__get_buf()) {
1543 __f.__f_->__move_to(__get_buf());
1552 template <class _Rp, class... _ArgTypes>
1553 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {
1554 if (__f_ == __get_buf())
1557 __f_->destroy_deallocate();
1560 template <class _Rp, class... _ArgTypes>
1561 _LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {
1562 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {
1563 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1564 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1565 _LIBCPP_SUPPRESS_DEPRECATED_POP
1566 __base* __t = (__base*)&__tempbuf;
1567 __f_->__move_to(__t);
1570 __f.__f_->__move_to((__base*)&__buf_);
1571 __f.__f_->destroy();
1573 __f_ = (__base*)&__buf_;
1574 __t->__move_to((__base*)&__f.__buf_);
1576 __f.__f_ = (__base*)&__f.__buf_;
1577 } else if (__f_ == (__base*)&__buf_) {
1578 __f_->__move_to((__base*)&__f.__buf_);
1581 __f.__f_ = (__base*)&__f.__buf_;
1582 } else if (__f.__f_ == (__base*)&__f.__buf_) {
1583 __f.__f_->__move_to((__base*)&__buf_);
1584 __f.__f_->destroy();
1586 __f_ = (__base*)&__buf_;
1588 std::swap(__f_, __f.__f_);
1591 template <class _Rp, class... _ArgTypes>
1592 inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
1593 return (*__f_)(std::forward<_ArgTypes>(__arg)...);
1596 template <class _Rp, class... _ArgTypes>
1597 class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)> {
1599 typedef _Rp result_type; // extension
1602 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1603 promise<result_type> __p_;
1606 // construction and destruction
1607 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1608 template <class _Fp, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1609 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1610 template <class _Fp, class _Allocator, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1611 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1612 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1613 // ~packaged_task() = default;
1616 packaged_task(const packaged_task&) = delete;
1617 packaged_task& operator=(const packaged_task&) = delete;
1620 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1621 : __f_(std::move(__other.__f_)),
1622 __p_(std::move(__other.__p_)) {}
1623 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1624 __f_ = std::move(__other.__f_);
1625 __p_ = std::move(__other.__p_);
1628 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1629 __f_.swap(__other.__f_);
1630 __p_.swap(__other.__p_);
1633 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1636 _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1639 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1640 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1642 _LIBCPP_HIDE_FROM_ABI void reset();
1645 template <class _Rp, class... _ArgTypes>
1646 void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1647 if (__p_.__state_ == nullptr)
1648 __throw_future_error(future_errc::no_state);
1649 if (__p_.__state_->__has_value())
1650 __throw_future_error(future_errc::promise_already_satisfied);
1651 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1653 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1654 __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
1655 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1657 __p_.set_exception(current_exception());
1659 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1662 template <class _Rp, class... _ArgTypes>
1663 void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1664 if (__p_.__state_ == nullptr)
1665 __throw_future_error(future_errc::no_state);
1666 if (__p_.__state_->__has_value())
1667 __throw_future_error(future_errc::promise_already_satisfied);
1668 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1670 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1671 __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
1672 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1674 __p_.set_exception_at_thread_exit(current_exception());
1676 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1679 template <class _Rp, class... _ArgTypes>
1680 void packaged_task<_Rp(_ArgTypes...)>::reset() {
1682 __throw_future_error(future_errc::no_state);
1683 __p_ = promise<result_type>();
1686 template <class... _ArgTypes>
1687 class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)> {
1689 typedef void result_type; // extension
1692 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1693 promise<result_type> __p_;
1696 // construction and destruction
1697 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1698 template <class _Fp, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1699 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1700 template <class _Fp, class _Allocator, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1701 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1702 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1703 // ~packaged_task() = default;
1706 packaged_task(const packaged_task&) = delete;
1707 packaged_task& operator=(const packaged_task&) = delete;
1710 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1711 : __f_(std::move(__other.__f_)),
1712 __p_(std::move(__other.__p_)) {}
1713 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1714 __f_ = std::move(__other.__f_);
1715 __p_ = std::move(__other.__p_);
1718 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1719 __f_.swap(__other.__f_);
1720 __p_.swap(__other.__p_);
1723 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1726 _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1729 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1730 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1732 _LIBCPP_HIDE_FROM_ABI void reset();
1735 #if _LIBCPP_STD_VER >= 17
1737 template <class _Rp, class... _Args>
1738 packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
1740 template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1741 packaged_task(_Fp) -> packaged_task<_Stripped>;
1745 template <class... _ArgTypes>
1746 void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1747 if (__p_.__state_ == nullptr)
1748 __throw_future_error(future_errc::no_state);
1749 if (__p_.__state_->__has_value())
1750 __throw_future_error(future_errc::promise_already_satisfied);
1751 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1753 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1754 __f_(std::forward<_ArgTypes>(__args)...);
1756 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1758 __p_.set_exception(current_exception());
1760 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1763 template <class... _ArgTypes>
1764 void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1765 if (__p_.__state_ == nullptr)
1766 __throw_future_error(future_errc::no_state);
1767 if (__p_.__state_->__has_value())
1768 __throw_future_error(future_errc::promise_already_satisfied);
1769 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1771 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1772 __f_(std::forward<_ArgTypes>(__args)...);
1773 __p_.set_value_at_thread_exit();
1774 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1776 __p_.set_exception_at_thread_exit(current_exception());
1778 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1781 template <class... _ArgTypes>
1782 void packaged_task<void(_ArgTypes...)>::reset() {
1784 __throw_future_error(future_errc::no_state);
1785 __p_ = promise<result_type>();
1788 template <class _Rp, class... _ArgTypes>
1789 inline _LIBCPP_HIDE_FROM_ABI void
1790 swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1794 template <class _Callable, class _Alloc>
1795 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
1797 template <class _Rp, class _Fp>
1798 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
1799 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1800 new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1801 return future<_Rp>(__h.get());
1804 template <class _Rp, class _Fp>
1805 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
1806 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1807 new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1808 std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
1809 return future<_Rp>(__h.get());
1812 #ifndef _LIBCPP_CXX03_LANG
1814 template <class _Fp, class... _Args>
1815 class _LIBCPP_HIDDEN __async_func {
1816 tuple<_Fp, _Args...> __f_;
1819 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
1821 _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Fp&& __f, _Args&&... __args)
1822 : __f_(std::move(__f), std::move(__args)...) {}
1824 _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
1826 _LIBCPP_HIDE_FROM_ABI _Rp operator()() {
1827 typedef typename __make_tuple_indices<1 + sizeof...(_Args), 1>::type _Index;
1828 return __execute(_Index());
1832 template <size_t... _Indices>
1833 _LIBCPP_HIDE_FROM_ABI _Rp __execute(__tuple_indices<_Indices...>) {
1834 return std::__invoke(std::move(std::get<0>(__f_)), std::move(std::get<_Indices>(__f_))...);
1838 inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
1839 return (int(__policy) & int(__value)) != 0;
1842 template <class _Fp, class... _Args>
1843 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
1844 future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1845 async(launch __policy, _Fp&& __f, _Args&&... __args) {
1846 typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
1847 typedef typename _BF::_Rp _Rp;
1849 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1852 if (__does_policy_contain(__policy, launch::async))
1853 return std::__make_async_assoc_state<_Rp>(
1854 _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1855 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1857 if (__policy == launch::async)
1862 if (__does_policy_contain(__policy, launch::deferred))
1863 return std::__make_deferred_assoc_state<_Rp>(
1864 _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1865 return future<_Rp>{};
1868 template <class _Fp, class... _Args>
1869 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
1870 future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1871 async(_Fp&& __f, _Args&&... __args) {
1872 return std::async(launch::any, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);
1879 template <class _Rp>
1880 class _LIBCPP_TEMPLATE_VIS shared_future {
1881 __assoc_state<_Rp>* __state_;
1884 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1885 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1887 __state_->__add_shared();
1889 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1890 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1891 __rhs.__state_ = nullptr;
1893 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1894 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
1895 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1896 shared_future(std::move(__rhs)).swap(*this);
1900 // retrieving the value
1901 _LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }
1903 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1905 // functions to check state
1906 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1908 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1909 template <class _Rep, class _Period>
1910 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1911 return __state_->wait_for(__rel_time);
1913 template <class _Clock, class _Duration>
1914 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1915 return __state_->wait_until(__abs_time);
1919 template <class _Rp>
1920 shared_future<_Rp>::~shared_future() {
1922 __state_->__release_shared();
1925 template <class _Rp>
1926 shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {
1928 __rhs.__state_->__add_shared();
1930 __state_->__release_shared();
1931 __state_ = __rhs.__state_;
1935 template <class _Rp>
1936 class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&> {
1937 __assoc_state<_Rp&>* __state_;
1940 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1941 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
1943 __state_->__add_shared();
1945 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1946 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1947 __rhs.__state_ = nullptr;
1949 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1950 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
1951 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1952 shared_future(std::move(__rhs)).swap(*this);
1956 // retrieving the value
1957 _LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }
1959 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1961 // functions to check state
1962 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1964 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1965 template <class _Rep, class _Period>
1966 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1967 return __state_->wait_for(__rel_time);
1969 template <class _Clock, class _Duration>
1970 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1971 return __state_->wait_until(__abs_time);
1975 template <class _Rp>
1976 shared_future<_Rp&>::~shared_future() {
1978 __state_->__release_shared();
1981 template <class _Rp>
1982 shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {
1984 __rhs.__state_->__add_shared();
1986 __state_->__release_shared();
1987 __state_ = __rhs.__state_;
1992 class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
1993 __assoc_sub_state* __state_;
1996 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1997 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
1999 __state_->__add_shared();
2001 _LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
2002 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
2003 __rhs.__state_ = nullptr;
2006 shared_future& operator=(const shared_future& __rhs);
2007 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
2008 shared_future(std::move(__rhs)).swap(*this);
2012 // retrieving the value
2013 _LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }
2015 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
2017 // functions to check state
2018 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
2020 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
2021 template <class _Rep, class _Period>
2022 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
2023 return __state_->wait_for(__rel_time);
2025 template <class _Clock, class _Duration>
2026 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
2027 return __state_->wait_until(__abs_time);
2031 template <class _Rp>
2032 inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
2036 template <class _Rp>
2037 inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
2038 return shared_future<_Rp>(std::move(*this));
2041 template <class _Rp>
2042 inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
2043 return shared_future<_Rp&>(std::move(*this));
2046 inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
2048 _LIBCPP_END_NAMESPACE_STD
2052 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2056 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2059 # include <exception>
2061 # include <system_error>
2064 #endif // _LIBCPP_FUTURE