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); // removed in C++17
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>; // removed in C++17
367 #if _LIBCPP_HAS_THREADS
370 # include <__chrono/duration.h>
371 # include <__chrono/time_point.h>
372 # include <__condition_variable/condition_variable.h>
373 # include <__exception/exception_ptr.h>
374 # include <__memory/addressof.h>
375 # include <__memory/allocator.h>
376 # include <__memory/allocator_arg_t.h>
377 # include <__memory/allocator_destructor.h>
378 # include <__memory/allocator_traits.h>
379 # include <__memory/compressed_pair.h>
380 # include <__memory/pointer_traits.h>
381 # include <__memory/shared_count.h>
382 # include <__memory/unique_ptr.h>
383 # include <__memory/uses_allocator.h>
384 # include <__system_error/error_category.h>
385 # include <__system_error/error_code.h>
386 # include <__system_error/error_condition.h>
387 # include <__thread/thread.h>
388 # include <__type_traits/add_lvalue_reference.h>
389 # include <__type_traits/aligned_storage.h>
390 # include <__type_traits/conditional.h>
391 # include <__type_traits/decay.h>
392 # include <__type_traits/enable_if.h>
393 # include <__type_traits/strip_signature.h>
394 # include <__type_traits/underlying_type.h>
395 # include <__utility/auto_cast.h>
396 # include <__utility/forward.h>
397 # include <__utility/move.h>
400 # include <stdexcept>
403 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
404 # pragma GCC system_header
408 # include <__undef_macros>
410 _LIBCPP_BEGIN_NAMESPACE_STD
412 // enum class future_errc
413 _LIBCPP_DECLARE_STRONG_ENUM(future_errc){
414 future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};
415 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
418 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
420 # ifdef _LIBCPP_CXX03_LANG
422 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type {};
426 _LIBCPP_DECLARE_STRONG_ENUM(launch){async = 1, deferred = 2, any = async | deferred};
427 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
429 # ifndef _LIBCPP_CXX03_LANG
431 typedef underlying_type<launch>::type __launch_underlying_type;
433 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator&(launch __x, launch __y) {
434 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));
437 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator|(launch __x, launch __y) {
438 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | static_cast<__launch_underlying_type>(__y));
441 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator^(launch __x, launch __y) {
442 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));
445 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator~(launch __x) {
446 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
449 inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {
454 inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {
459 inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {
464 # endif // !_LIBCPP_CXX03_LANG
466 // enum class future_status
467 _LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};
468 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
470 _LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
472 inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {
473 return error_code(static_cast<int>(__e), future_category());
476 inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {
477 return error_condition(static_cast<int>(__e), future_category());
480 [[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);
482 class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {
485 future_error(error_code);
486 friend void __throw_future_error(future_errc);
488 friend class promise;
491 # if _LIBCPP_STD_VER >= 17
492 _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
495 _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
497 _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
498 ~future_error() _NOEXCEPT override;
501 // Declared above std::future_error
502 void __throw_future_error(future_errc __ev) {
503 # if _LIBCPP_HAS_EXCEPTIONS
504 throw future_error(make_error_code(__ev));
507 _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
511 class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {
513 exception_ptr __exception_;
514 mutable mutex __mut_;
515 mutable condition_variable __cv_;
518 void __on_zero_shared() _NOEXCEPT override;
519 void __sub_wait(unique_lock<mutex>& __lk);
522 enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };
524 _LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}
526 _LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }
528 _LIBCPP_HIDE_FROM_ABI void __attach_future() {
529 lock_guard<mutex> __lk(__mut_);
530 bool __has_future_attached = (__state_ & __future_attached) != 0;
531 if (__has_future_attached)
532 __throw_future_error(future_errc::future_already_retrieved);
533 this->__add_shared();
534 __state_ |= __future_attached;
537 _LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }
540 _LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }
543 void set_value_at_thread_exit();
545 void set_exception(exception_ptr __p);
546 void set_exception_at_thread_exit(exception_ptr __p);
551 template <class _Rep, class _Period>
552 future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
553 template <class _Clock, class _Duration>
554 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS future_status
555 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
557 virtual void __execute();
560 template <class _Clock, class _Duration>
561 future_status __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
562 unique_lock<mutex> __lk(__mut_);
563 if (__state_ & deferred)
564 return future_status::deferred;
565 while (!(__state_ & ready) && _Clock::now() < __abs_time)
566 __cv_.wait_until(__lk, __abs_time);
567 if (__state_ & ready)
568 return future_status::ready;
569 return future_status::timeout;
572 template <class _Rep, class _Period>
573 inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
574 return wait_until(chrono::steady_clock::now() + __rel_time);
578 class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {
579 typedef __assoc_sub_state base;
580 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
581 typedef typename aligned_storage<sizeof(_Rp), _LIBCPP_ALIGNOF(_Rp)>::type _Up;
582 _LIBCPP_SUPPRESS_DEPRECATED_POP
587 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
590 template <class _Arg>
591 _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
593 template <class _Arg>
594 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
596 _LIBCPP_HIDE_FROM_ABI _Rp move();
597 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
601 void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {
602 if (this->__state_ & base::__constructed)
603 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
608 template <class _Arg>
609 void __assoc_state<_Rp>::set_value(_Arg&& __arg) {
610 unique_lock<mutex> __lk(this->__mut_);
611 if (this->__has_value())
612 __throw_future_error(future_errc::promise_already_satisfied);
613 ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
614 this->__state_ |= base::__constructed | base::ready;
619 template <class _Arg>
620 void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {
621 unique_lock<mutex> __lk(this->__mut_);
622 if (this->__has_value())
623 __throw_future_error(future_errc::promise_already_satisfied);
624 ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
625 this->__state_ |= base::__constructed;
626 __thread_local_data()->__make_ready_at_thread_exit(this);
630 _Rp __assoc_state<_Rp>::move() {
631 unique_lock<mutex> __lk(this->__mut_);
632 this->__sub_wait(__lk);
633 if (this->__exception_ != nullptr)
634 std::rethrow_exception(this->__exception_);
635 return std::move(*reinterpret_cast<_Rp*>(&__value_));
639 _Rp& __assoc_state<_Rp>::copy() {
640 unique_lock<mutex> __lk(this->__mut_);
641 this->__sub_wait(__lk);
642 if (this->__exception_ != nullptr)
643 std::rethrow_exception(this->__exception_);
644 return *reinterpret_cast<_Rp*>(&__value_);
648 class __assoc_state<_Rp&> : public __assoc_sub_state {
649 typedef __assoc_sub_state base;
655 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
658 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
659 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
661 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
665 void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {
670 void __assoc_state<_Rp&>::set_value(_Rp& __arg) {
671 unique_lock<mutex> __lk(this->__mut_);
672 if (this->__has_value())
673 __throw_future_error(future_errc::promise_already_satisfied);
674 __value_ = std::addressof(__arg);
675 this->__state_ |= base::__constructed | base::ready;
680 void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {
681 unique_lock<mutex> __lk(this->__mut_);
682 if (this->__has_value())
683 __throw_future_error(future_errc::promise_already_satisfied);
684 __value_ = std::addressof(__arg);
685 this->__state_ |= base::__constructed;
686 __thread_local_data()->__make_ready_at_thread_exit(this);
690 _Rp& __assoc_state<_Rp&>::copy() {
691 unique_lock<mutex> __lk(this->__mut_);
692 this->__sub_wait(__lk);
693 if (this->__exception_ != nullptr)
694 std::rethrow_exception(this->__exception_);
698 template <class _Rp, class _Alloc>
699 class __assoc_state_alloc : public __assoc_state<_Rp> {
700 typedef __assoc_state<_Rp> base;
703 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
706 _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
709 template <class _Rp, class _Alloc>
710 void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {
711 if (this->__state_ & base::__constructed)
712 reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();
713 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
714 typedef allocator_traits<_Al> _ATraits;
715 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
717 this->~__assoc_state_alloc();
718 __a.deallocate(_PTraits::pointer_to(*this), 1);
721 template <class _Rp, class _Alloc>
722 class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {
723 typedef __assoc_state<_Rp&> base;
726 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
729 _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
732 template <class _Rp, class _Alloc>
733 void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {
734 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
735 typedef allocator_traits<_Al> _ATraits;
736 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
738 this->~__assoc_state_alloc();
739 __a.deallocate(_PTraits::pointer_to(*this), 1);
742 template <class _Alloc>
743 class __assoc_sub_state_alloc : public __assoc_sub_state {
744 typedef __assoc_sub_state base;
747 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
750 _LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
753 template <class _Alloc>
754 void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {
755 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
756 typedef allocator_traits<_Al> _ATraits;
757 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
759 this->~__assoc_sub_state_alloc();
760 __a.deallocate(_PTraits::pointer_to(*this), 1);
763 template <class _Rp, class _Fp>
764 class __deferred_assoc_state : public __assoc_state<_Rp> {
765 typedef __assoc_state<_Rp> base;
770 _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
772 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
775 template <class _Rp, class _Fp>
776 inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
777 this->__set_deferred();
780 template <class _Rp, class _Fp>
781 void __deferred_assoc_state<_Rp, _Fp>::__execute() {
782 # if _LIBCPP_HAS_EXCEPTIONS
784 # endif // _LIBCPP_HAS_EXCEPTIONS
785 this->set_value(__func_());
786 # if _LIBCPP_HAS_EXCEPTIONS
788 this->set_exception(current_exception());
790 # endif // _LIBCPP_HAS_EXCEPTIONS
794 class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {
795 typedef __assoc_sub_state base;
800 _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
802 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
806 inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
807 this->__set_deferred();
811 void __deferred_assoc_state<void, _Fp>::__execute() {
812 # if _LIBCPP_HAS_EXCEPTIONS
814 # endif // _LIBCPP_HAS_EXCEPTIONS
817 # if _LIBCPP_HAS_EXCEPTIONS
819 this->set_exception(current_exception());
821 # endif // _LIBCPP_HAS_EXCEPTIONS
824 template <class _Rp, class _Fp>
825 class __async_assoc_state : public __assoc_state<_Rp> {
826 typedef __assoc_state<_Rp> base;
830 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
833 _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
835 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
838 template <class _Rp, class _Fp>
839 inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
841 template <class _Rp, class _Fp>
842 void __async_assoc_state<_Rp, _Fp>::__execute() {
843 # if _LIBCPP_HAS_EXCEPTIONS
845 # endif // _LIBCPP_HAS_EXCEPTIONS
846 this->set_value(__func_());
847 # if _LIBCPP_HAS_EXCEPTIONS
849 this->set_exception(current_exception());
851 # endif // _LIBCPP_HAS_EXCEPTIONS
854 template <class _Rp, class _Fp>
855 void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {
857 base::__on_zero_shared();
861 class __async_assoc_state<void, _Fp> : public __assoc_sub_state {
862 typedef __assoc_sub_state base;
866 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
869 _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
871 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
875 inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
878 void __async_assoc_state<void, _Fp>::__execute() {
879 # if _LIBCPP_HAS_EXCEPTIONS
881 # endif // _LIBCPP_HAS_EXCEPTIONS
884 # if _LIBCPP_HAS_EXCEPTIONS
886 this->set_exception(current_exception());
888 # endif // _LIBCPP_HAS_EXCEPTIONS
892 void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {
894 base::__on_zero_shared();
898 class _LIBCPP_TEMPLATE_VIS promise;
900 class _LIBCPP_TEMPLATE_VIS shared_future;
905 class _LIBCPP_TEMPLATE_VIS future;
907 template <class _Rp, class _Fp>
908 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);
910 template <class _Rp, class _Fp>
911 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);
914 class _LIBCPP_TEMPLATE_VIS future {
915 __assoc_state<_Rp>* __state_;
917 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
920 friend class promise;
922 friend class shared_future;
924 template <class _R1, class _Fp>
925 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
926 template <class _R1, class _Fp>
927 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
930 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
931 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
932 future(const future&) = delete;
933 future& operator=(const future&) = delete;
934 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
935 future(std::move(__rhs)).swap(*this);
939 _LIBCPP_HIDE_FROM_ABI ~future();
940 _LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;
942 // retrieving the value
943 _LIBCPP_HIDE_FROM_ABI _Rp get();
945 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
947 // functions to check state
948 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
950 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
951 template <class _Rep, class _Period>
952 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
953 return __state_->wait_for(__rel_time);
955 template <class _Clock, class _Duration>
956 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
957 return __state_->wait_until(__abs_time);
962 future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {
963 __state_->__attach_future();
966 struct __release_shared_count {
967 _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }
971 future<_Rp>::~future() {
973 __state_->__release_shared();
977 _Rp future<_Rp>::get() {
978 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
979 __assoc_state<_Rp>* __s = __state_;
985 class _LIBCPP_TEMPLATE_VIS future<_Rp&> {
986 __assoc_state<_Rp&>* __state_;
988 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
991 friend class promise;
993 friend class shared_future;
995 template <class _R1, class _Fp>
996 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
997 template <class _R1, class _Fp>
998 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1001 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1002 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1003 future(const future&) = delete;
1004 future& operator=(const future&) = delete;
1005 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1006 future(std::move(__rhs)).swap(*this);
1010 _LIBCPP_HIDE_FROM_ABI ~future();
1011 _LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;
1013 // retrieving the value
1014 _LIBCPP_HIDE_FROM_ABI _Rp& get();
1016 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1018 // functions to check state
1019 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1021 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1022 template <class _Rep, class _Period>
1023 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1024 return __state_->wait_for(__rel_time);
1026 template <class _Clock, class _Duration>
1027 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1028 return __state_->wait_until(__abs_time);
1032 template <class _Rp>
1033 future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {
1034 __state_->__attach_future();
1037 template <class _Rp>
1038 future<_Rp&>::~future() {
1040 __state_->__release_shared();
1043 template <class _Rp>
1044 _Rp& future<_Rp&>::get() {
1045 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1046 __assoc_state<_Rp&>* __s = __state_;
1052 class _LIBCPP_EXPORTED_FROM_ABI future<void> {
1053 __assoc_sub_state* __state_;
1055 explicit future(__assoc_sub_state* __state);
1058 friend class promise;
1060 friend class shared_future;
1062 template <class _R1, class _Fp>
1063 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1064 template <class _R1, class _Fp>
1065 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1068 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1069 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1070 future(const future&) = delete;
1071 future& operator=(const future&) = delete;
1072 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1073 future(std::move(__rhs)).swap(*this);
1078 _LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;
1080 // retrieving the value
1083 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1085 // functions to check state
1086 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1088 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1089 template <class _Rep, class _Period>
1090 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1091 return __state_->wait_for(__rel_time);
1093 template <class _Clock, class _Duration>
1094 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1095 return __state_->wait_until(__abs_time);
1099 template <class _Rp>
1100 inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {
1106 template <class _Callable>
1107 class packaged_task;
1109 template <class _Rp>
1110 class _LIBCPP_TEMPLATE_VIS promise {
1111 __assoc_state<_Rp>* __state_;
1113 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1116 friend class packaged_task;
1119 _LIBCPP_HIDE_FROM_ABI promise();
1120 template <class _Alloc>
1121 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1122 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1123 promise(const promise& __rhs) = delete;
1124 _LIBCPP_HIDE_FROM_ABI ~promise();
1127 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1128 promise(std::move(__rhs)).swap(*this);
1131 promise& operator=(const promise& __rhs) = delete;
1133 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1135 // retrieving the result
1136 _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1138 // setting the result
1139 _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1140 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1141 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1143 // setting the result with deferred notification
1144 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1145 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1146 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1149 template <class _Rp>
1150 promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}
1152 template <class _Rp>
1153 template <class _Alloc>
1154 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {
1155 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1156 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1157 typedef __allocator_destructor<_A2> _D2;
1159 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1160 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1161 __state_ = std::addressof(*__hold.release());
1164 template <class _Rp>
1165 promise<_Rp>::~promise() {
1167 if (!__state_->__has_value() && __state_->use_count() > 1)
1168 __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1169 __state_->__release_shared();
1173 template <class _Rp>
1174 future<_Rp> promise<_Rp>::get_future() {
1175 if (__state_ == nullptr)
1176 __throw_future_error(future_errc::no_state);
1177 return future<_Rp>(__state_);
1180 template <class _Rp>
1181 void promise<_Rp>::set_value(const _Rp& __r) {
1182 if (__state_ == nullptr)
1183 __throw_future_error(future_errc::no_state);
1184 __state_->set_value(__r);
1187 template <class _Rp>
1188 void promise<_Rp>::set_value(_Rp&& __r) {
1189 if (__state_ == nullptr)
1190 __throw_future_error(future_errc::no_state);
1191 __state_->set_value(std::move(__r));
1194 template <class _Rp>
1195 void promise<_Rp>::set_exception(exception_ptr __p) {
1196 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1197 if (__state_ == nullptr)
1198 __throw_future_error(future_errc::no_state);
1199 __state_->set_exception(__p);
1202 template <class _Rp>
1203 void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {
1204 if (__state_ == nullptr)
1205 __throw_future_error(future_errc::no_state);
1206 __state_->set_value_at_thread_exit(__r);
1209 template <class _Rp>
1210 void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {
1211 if (__state_ == nullptr)
1212 __throw_future_error(future_errc::no_state);
1213 __state_->set_value_at_thread_exit(std::move(__r));
1216 template <class _Rp>
1217 void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {
1218 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1219 if (__state_ == nullptr)
1220 __throw_future_error(future_errc::no_state);
1221 __state_->set_exception_at_thread_exit(__p);
1226 template <class _Rp>
1227 class _LIBCPP_TEMPLATE_VIS promise<_Rp&> {
1228 __assoc_state<_Rp&>* __state_;
1230 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1233 friend class packaged_task;
1236 _LIBCPP_HIDE_FROM_ABI promise();
1237 template <class _Allocator>
1238 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1239 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1240 promise(const promise& __rhs) = delete;
1241 _LIBCPP_HIDE_FROM_ABI ~promise();
1244 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1245 promise(std::move(__rhs)).swap(*this);
1248 promise& operator=(const promise& __rhs) = delete;
1250 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1252 // retrieving the result
1253 _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1255 // setting the result
1256 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1257 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1259 // setting the result with deferred notification
1260 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1261 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1264 template <class _Rp>
1265 promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}
1267 template <class _Rp>
1268 template <class _Alloc>
1269 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {
1270 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1271 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1272 typedef __allocator_destructor<_A2> _D2;
1274 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1275 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1276 __state_ = std::addressof(*__hold.release());
1279 template <class _Rp>
1280 promise<_Rp&>::~promise() {
1282 if (!__state_->__has_value() && __state_->use_count() > 1)
1283 __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1284 __state_->__release_shared();
1288 template <class _Rp>
1289 future<_Rp&> promise<_Rp&>::get_future() {
1290 if (__state_ == nullptr)
1291 __throw_future_error(future_errc::no_state);
1292 return future<_Rp&>(__state_);
1295 template <class _Rp>
1296 void promise<_Rp&>::set_value(_Rp& __r) {
1297 if (__state_ == nullptr)
1298 __throw_future_error(future_errc::no_state);
1299 __state_->set_value(__r);
1302 template <class _Rp>
1303 void promise<_Rp&>::set_exception(exception_ptr __p) {
1304 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1305 if (__state_ == nullptr)
1306 __throw_future_error(future_errc::no_state);
1307 __state_->set_exception(__p);
1310 template <class _Rp>
1311 void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {
1312 if (__state_ == nullptr)
1313 __throw_future_error(future_errc::no_state);
1314 __state_->set_value_at_thread_exit(__r);
1317 template <class _Rp>
1318 void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {
1319 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1320 if (__state_ == nullptr)
1321 __throw_future_error(future_errc::no_state);
1322 __state_->set_exception_at_thread_exit(__p);
1328 class _LIBCPP_EXPORTED_FROM_ABI promise<void> {
1329 __assoc_sub_state* __state_;
1331 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1334 friend class packaged_task;
1338 template <class _Allocator>
1339 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS promise(allocator_arg_t, const _Allocator& __a);
1340 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1341 promise(const promise& __rhs) = delete;
1345 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1346 promise(std::move(__rhs)).swap(*this);
1349 promise& operator=(const promise& __rhs) = delete;
1351 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1353 // retrieving the result
1354 future<void> get_future();
1356 // setting the result
1358 void set_exception(exception_ptr __p);
1360 // setting the result with deferred notification
1361 void set_value_at_thread_exit();
1362 void set_exception_at_thread_exit(exception_ptr __p);
1365 template <class _Alloc>
1366 promise<void>::promise(allocator_arg_t, const _Alloc& __a0) {
1367 typedef __assoc_sub_state_alloc<_Alloc> _State;
1368 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1369 typedef __allocator_destructor<_A2> _D2;
1371 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1372 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1373 __state_ = std::addressof(*__hold.release());
1376 template <class _Rp>
1377 inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {
1381 template <class _Rp, class _Alloc>
1382 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc> : public true_type {};
1386 template <class _Fp>
1387 class __packaged_task_base;
1389 template <class _Rp, class... _ArgTypes>
1390 class __packaged_task_base<_Rp(_ArgTypes...)> {
1392 _LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}
1393 __packaged_task_base(const __packaged_task_base&) = delete;
1394 __packaged_task_base& operator=(const __packaged_task_base&) = delete;
1395 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1396 virtual ~__packaged_task_base() {}
1397 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1398 virtual void destroy() = 0;
1399 virtual void destroy_deallocate() = 0;
1400 virtual _Rp operator()(_ArgTypes&&...) = 0;
1403 template <class _FD, class _Alloc, class _FB>
1404 class __packaged_task_func;
1406 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1407 class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
1408 _LIBCPP_COMPRESSED_PAIR(_Fp, __func_, _Alloc, __alloc_);
1411 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __func_(__f) {}
1412 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __func_(std::move(__f)) {}
1413 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __func_(__f), __alloc_(__a) {}
1414 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __func_(std::move(__f)), __alloc_(__a) {}
1415 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1416 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1417 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1418 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);
1421 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1422 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1423 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {
1424 ::new ((void*)__p) __packaged_task_func(std::move(__func_), std::move(__alloc_));
1427 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1428 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
1433 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1434 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {
1435 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1436 typedef allocator_traits<_Ap> _ATraits;
1437 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1441 __a.deallocate(_PTraits::pointer_to(*this), 1);
1444 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1445 _Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
1446 return std::__invoke(__func_, std::forward<_ArgTypes>(__arg)...);
1449 template <class _Callable>
1450 class __packaged_task_function;
1452 template <class _Rp, class... _ArgTypes>
1453 class __packaged_task_function<_Rp(_ArgTypes...)> {
1454 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1456 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }
1458 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1459 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1460 _LIBCPP_SUPPRESS_DEPRECATED_POP
1464 typedef _Rp result_type;
1466 // construct/copy/destroy:
1467 _LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1468 template <class _Fp>
1469 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1470 template <class _Fp, class _Alloc>
1471 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1473 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1474 _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1476 __packaged_task_function(const __packaged_task_function&) = delete;
1477 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1479 _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1481 _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1483 _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1486 template <class _Rp, class... _ArgTypes>
1487 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {
1488 if (__f.__f_ == nullptr)
1490 else if (__f.__f_ == __f.__get_buf()) {
1491 __f.__f_->__move_to(__get_buf());
1492 __f_ = (__base*)&__buf_;
1499 template <class _Rp, class... _ArgTypes>
1500 template <class _Fp>
1501 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {
1502 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1503 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1504 if (sizeof(_FF) <= sizeof(__buf_)) {
1505 ::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));
1506 __f_ = (__base*)&__buf_;
1508 typedef allocator<_FF> _Ap;
1510 typedef __allocator_destructor<_Ap> _Dp;
1511 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1512 ::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));
1513 __f_ = __hold.release();
1517 template <class _Rp, class... _ArgTypes>
1518 template <class _Fp, class _Alloc>
1519 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1521 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1522 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1523 if (sizeof(_FF) <= sizeof(__buf_)) {
1524 __f_ = (__base*)&__buf_;
1525 ::new ((void*)__f_) _FF(std::forward<_Fp>(__f));
1527 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1529 typedef __allocator_destructor<_Ap> _Dp;
1530 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1531 ::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));
1532 __f_ = std::addressof(*__hold.release());
1536 template <class _Rp, class... _ArgTypes>
1537 __packaged_task_function<_Rp(_ArgTypes...)>&
1538 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {
1539 if (__f_ == __get_buf())
1542 __f_->destroy_deallocate();
1544 if (__f.__f_ == nullptr)
1546 else if (__f.__f_ == __f.__get_buf()) {
1547 __f.__f_->__move_to(__get_buf());
1556 template <class _Rp, class... _ArgTypes>
1557 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {
1558 if (__f_ == __get_buf())
1561 __f_->destroy_deallocate();
1564 template <class _Rp, class... _ArgTypes>
1565 _LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {
1566 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {
1567 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1568 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1569 _LIBCPP_SUPPRESS_DEPRECATED_POP
1570 __base* __t = (__base*)&__tempbuf;
1571 __f_->__move_to(__t);
1574 __f.__f_->__move_to((__base*)&__buf_);
1575 __f.__f_->destroy();
1577 __f_ = (__base*)&__buf_;
1578 __t->__move_to((__base*)&__f.__buf_);
1580 __f.__f_ = (__base*)&__f.__buf_;
1581 } else if (__f_ == (__base*)&__buf_) {
1582 __f_->__move_to((__base*)&__f.__buf_);
1585 __f.__f_ = (__base*)&__f.__buf_;
1586 } else if (__f.__f_ == (__base*)&__f.__buf_) {
1587 __f.__f_->__move_to((__base*)&__buf_);
1588 __f.__f_->destroy();
1590 __f_ = (__base*)&__buf_;
1592 std::swap(__f_, __f.__f_);
1595 template <class _Rp, class... _ArgTypes>
1596 inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
1597 return (*__f_)(std::forward<_ArgTypes>(__arg)...);
1600 template <class _Rp, class... _ArgTypes>
1601 class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)> {
1603 typedef _Rp result_type; // extension
1606 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1607 promise<result_type> __p_;
1610 // construction and destruction
1611 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1613 template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1614 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1616 # if _LIBCPP_STD_VER <= 14
1617 template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1618 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1619 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1621 // ~packaged_task() = default;
1624 packaged_task(const packaged_task&) = delete;
1625 packaged_task& operator=(const packaged_task&) = delete;
1628 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1629 : __f_(std::move(__other.__f_)),
1630 __p_(std::move(__other.__p_)) {}
1631 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1632 __f_ = std::move(__other.__f_);
1633 __p_ = std::move(__other.__p_);
1636 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1637 __f_.swap(__other.__f_);
1638 __p_.swap(__other.__p_);
1641 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1644 _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1647 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1648 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1650 _LIBCPP_HIDE_FROM_ABI void reset();
1653 template <class _Rp, class... _ArgTypes>
1654 void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1655 if (__p_.__state_ == nullptr)
1656 __throw_future_error(future_errc::no_state);
1657 if (__p_.__state_->__has_value())
1658 __throw_future_error(future_errc::promise_already_satisfied);
1659 # if _LIBCPP_HAS_EXCEPTIONS
1661 # endif // _LIBCPP_HAS_EXCEPTIONS
1662 __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
1663 # if _LIBCPP_HAS_EXCEPTIONS
1665 __p_.set_exception(current_exception());
1667 # endif // _LIBCPP_HAS_EXCEPTIONS
1670 template <class _Rp, class... _ArgTypes>
1671 void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1672 if (__p_.__state_ == nullptr)
1673 __throw_future_error(future_errc::no_state);
1674 if (__p_.__state_->__has_value())
1675 __throw_future_error(future_errc::promise_already_satisfied);
1676 # if _LIBCPP_HAS_EXCEPTIONS
1678 # endif // _LIBCPP_HAS_EXCEPTIONS
1679 __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
1680 # if _LIBCPP_HAS_EXCEPTIONS
1682 __p_.set_exception_at_thread_exit(current_exception());
1684 # endif // _LIBCPP_HAS_EXCEPTIONS
1687 template <class _Rp, class... _ArgTypes>
1688 void packaged_task<_Rp(_ArgTypes...)>::reset() {
1690 __throw_future_error(future_errc::no_state);
1691 __p_ = promise<result_type>();
1694 template <class... _ArgTypes>
1695 class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)> {
1697 typedef void result_type; // extension
1700 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1701 promise<result_type> __p_;
1704 // construction and destruction
1705 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1706 template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1707 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1708 # if _LIBCPP_STD_VER <= 14
1709 template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1710 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1711 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1713 // ~packaged_task() = default;
1716 packaged_task(const packaged_task&) = delete;
1717 packaged_task& operator=(const packaged_task&) = delete;
1720 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1721 : __f_(std::move(__other.__f_)),
1722 __p_(std::move(__other.__p_)) {}
1723 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1724 __f_ = std::move(__other.__f_);
1725 __p_ = std::move(__other.__p_);
1728 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1729 __f_.swap(__other.__f_);
1730 __p_.swap(__other.__p_);
1733 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1736 _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1739 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1740 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1742 _LIBCPP_HIDE_FROM_ABI void reset();
1745 # if _LIBCPP_STD_VER >= 17
1747 template <class _Rp, class... _Args>
1748 packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
1750 template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1751 packaged_task(_Fp) -> packaged_task<_Stripped>;
1755 template <class... _ArgTypes>
1756 void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1757 if (__p_.__state_ == nullptr)
1758 __throw_future_error(future_errc::no_state);
1759 if (__p_.__state_->__has_value())
1760 __throw_future_error(future_errc::promise_already_satisfied);
1761 # if _LIBCPP_HAS_EXCEPTIONS
1763 # endif // _LIBCPP_HAS_EXCEPTIONS
1764 __f_(std::forward<_ArgTypes>(__args)...);
1766 # if _LIBCPP_HAS_EXCEPTIONS
1768 __p_.set_exception(current_exception());
1770 # endif // _LIBCPP_HAS_EXCEPTIONS
1773 template <class... _ArgTypes>
1774 void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1775 if (__p_.__state_ == nullptr)
1776 __throw_future_error(future_errc::no_state);
1777 if (__p_.__state_->__has_value())
1778 __throw_future_error(future_errc::promise_already_satisfied);
1779 # if _LIBCPP_HAS_EXCEPTIONS
1781 # endif // _LIBCPP_HAS_EXCEPTIONS
1782 __f_(std::forward<_ArgTypes>(__args)...);
1783 __p_.set_value_at_thread_exit();
1784 # if _LIBCPP_HAS_EXCEPTIONS
1786 __p_.set_exception_at_thread_exit(current_exception());
1788 # endif // _LIBCPP_HAS_EXCEPTIONS
1791 template <class... _ArgTypes>
1792 void packaged_task<void(_ArgTypes...)>::reset() {
1794 __throw_future_error(future_errc::no_state);
1795 __p_ = promise<result_type>();
1798 template <class _Rp, class... _ArgTypes>
1799 inline _LIBCPP_HIDE_FROM_ABI void
1800 swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1804 # if _LIBCPP_STD_VER <= 14
1805 template <class _Callable, class _Alloc>
1806 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
1809 template <class _Rp, class _Fp>
1810 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
1811 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1812 new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1813 return future<_Rp>(__h.get());
1816 template <class _Rp, class _Fp>
1817 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
1818 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1819 new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1820 std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
1821 return future<_Rp>(__h.get());
1824 # ifndef _LIBCPP_CXX03_LANG
1826 template <class _Fp, class... _Args>
1827 class _LIBCPP_HIDDEN __async_func {
1828 tuple<_Fp, _Args...> __f_;
1831 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
1833 _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Fp&& __f, _Args&&... __args)
1834 : __f_(std::move(__f), std::move(__args)...) {}
1836 _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
1838 _LIBCPP_HIDE_FROM_ABI _Rp operator()() {
1839 typedef typename __make_tuple_indices<1 + sizeof...(_Args), 1>::type _Index;
1840 return __execute(_Index());
1844 template <size_t... _Indices>
1845 _LIBCPP_HIDE_FROM_ABI _Rp __execute(__tuple_indices<_Indices...>) {
1846 return std::__invoke(std::move(std::get<0>(__f_)), std::move(std::get<_Indices>(__f_))...);
1850 inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
1851 return (int(__policy) & int(__value)) != 0;
1854 template <class _Fp, class... _Args>
1855 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1856 async(launch __policy, _Fp&& __f, _Args&&... __args) {
1857 typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
1858 typedef typename _BF::_Rp _Rp;
1860 # if _LIBCPP_HAS_EXCEPTIONS
1863 if (__does_policy_contain(__policy, launch::async))
1864 return std::__make_async_assoc_state<_Rp>(
1865 _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1866 # if _LIBCPP_HAS_EXCEPTIONS
1868 if (__policy == launch::async)
1873 if (__does_policy_contain(__policy, launch::deferred))
1874 return std::__make_deferred_assoc_state<_Rp>(
1875 _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1876 return future<_Rp>{};
1879 template <class _Fp, class... _Args>
1880 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1881 async(_Fp&& __f, _Args&&... __args) {
1882 return std::async(launch::any, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);
1889 template <class _Rp>
1890 class _LIBCPP_TEMPLATE_VIS shared_future {
1891 __assoc_state<_Rp>* __state_;
1894 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1895 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1897 __state_->__add_shared();
1899 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1900 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1901 __rhs.__state_ = nullptr;
1903 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1904 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
1905 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1906 shared_future(std::move(__rhs)).swap(*this);
1910 // retrieving the value
1911 _LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }
1913 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1915 // functions to check state
1916 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1918 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1919 template <class _Rep, class _Period>
1920 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1921 return __state_->wait_for(__rel_time);
1923 template <class _Clock, class _Duration>
1924 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1925 return __state_->wait_until(__abs_time);
1929 template <class _Rp>
1930 shared_future<_Rp>::~shared_future() {
1932 __state_->__release_shared();
1935 template <class _Rp>
1936 shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {
1938 __rhs.__state_->__add_shared();
1940 __state_->__release_shared();
1941 __state_ = __rhs.__state_;
1945 template <class _Rp>
1946 class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&> {
1947 __assoc_state<_Rp&>* __state_;
1950 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1951 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
1953 __state_->__add_shared();
1955 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1956 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1957 __rhs.__state_ = nullptr;
1959 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1960 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
1961 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1962 shared_future(std::move(__rhs)).swap(*this);
1966 // retrieving the value
1967 _LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }
1969 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1971 // functions to check state
1972 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1974 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1975 template <class _Rep, class _Period>
1976 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1977 return __state_->wait_for(__rel_time);
1979 template <class _Clock, class _Duration>
1980 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1981 return __state_->wait_until(__abs_time);
1985 template <class _Rp>
1986 shared_future<_Rp&>::~shared_future() {
1988 __state_->__release_shared();
1991 template <class _Rp>
1992 shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {
1994 __rhs.__state_->__add_shared();
1996 __state_->__release_shared();
1997 __state_ = __rhs.__state_;
2002 class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
2003 __assoc_sub_state* __state_;
2006 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
2007 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
2009 __state_->__add_shared();
2011 _LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
2012 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
2013 __rhs.__state_ = nullptr;
2016 shared_future& operator=(const shared_future& __rhs);
2017 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
2018 shared_future(std::move(__rhs)).swap(*this);
2022 // retrieving the value
2023 _LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }
2025 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
2027 // functions to check state
2028 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
2030 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
2031 template <class _Rep, class _Period>
2032 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
2033 return __state_->wait_for(__rel_time);
2035 template <class _Clock, class _Duration>
2036 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
2037 return __state_->wait_until(__abs_time);
2041 template <class _Rp>
2042 inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
2046 template <class _Rp>
2047 inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
2048 return shared_future<_Rp>(std::move(*this));
2051 template <class _Rp>
2052 inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
2053 return shared_future<_Rp&>(std::move(*this));
2056 inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
2058 _LIBCPP_END_NAMESPACE_STD
2062 #endif // _LIBCPP_HAS_THREADS
2064 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2068 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2071 # include <exception>
2073 # include <system_error>
2077 #endif // _LIBCPP_FUTURE