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
365 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
366 # include <__cxx03/future>
370 # if _LIBCPP_HAS_THREADS
373 # include <__chrono/duration.h>
374 # include <__chrono/steady_clock.h>
375 # include <__chrono/time_point.h>
376 # include <__condition_variable/condition_variable.h>
377 # include <__cstddef/nullptr_t.h>
378 # include <__exception/exception_ptr.h>
379 # include <__memory/addressof.h>
380 # include <__memory/allocator.h>
381 # include <__memory/allocator_arg_t.h>
382 # include <__memory/allocator_destructor.h>
383 # include <__memory/allocator_traits.h>
384 # include <__memory/compressed_pair.h>
385 # include <__memory/pointer_traits.h>
386 # include <__memory/shared_count.h>
387 # include <__memory/unique_ptr.h>
388 # include <__memory/uses_allocator.h>
389 # include <__mutex/lock_guard.h>
390 # include <__mutex/mutex.h>
391 # include <__mutex/unique_lock.h>
392 # include <__system_error/error_category.h>
393 # include <__system_error/error_code.h>
394 # include <__system_error/error_condition.h>
395 # include <__thread/thread.h>
396 # include <__type_traits/add_lvalue_reference.h>
397 # include <__type_traits/aligned_storage.h>
398 # include <__type_traits/conditional.h>
399 # include <__type_traits/decay.h>
400 # include <__type_traits/enable_if.h>
401 # include <__type_traits/invoke.h>
402 # include <__type_traits/is_same.h>
403 # include <__type_traits/remove_cvref.h>
404 # include <__type_traits/remove_reference.h>
405 # include <__type_traits/strip_signature.h>
406 # include <__type_traits/underlying_type.h>
407 # include <__utility/auto_cast.h>
408 # include <__utility/forward.h>
409 # include <__utility/move.h>
410 # include <__utility/swap.h>
411 # include <stdexcept>
415 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
416 # pragma GCC system_header
420 # include <__undef_macros>
422 _LIBCPP_BEGIN_NAMESPACE_STD
424 // enum class future_errc
425 _LIBCPP_DECLARE_STRONG_ENUM(future_errc){
426 future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};
427 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
430 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
432 # ifdef _LIBCPP_CXX03_LANG
434 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type {};
438 _LIBCPP_DECLARE_STRONG_ENUM(launch){async = 1, deferred = 2, any = async | deferred};
439 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
441 # ifndef _LIBCPP_CXX03_LANG
443 typedef underlying_type<launch>::type __launch_underlying_type;
445 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator&(launch __x, launch __y) {
446 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));
449 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator|(launch __x, launch __y) {
450 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | static_cast<__launch_underlying_type>(__y));
453 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator^(launch __x, launch __y) {
454 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));
457 inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator~(launch __x) {
458 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
461 inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {
466 inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {
471 inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {
476 # endif // !_LIBCPP_CXX03_LANG
478 // enum class future_status
479 _LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};
480 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
482 _LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
484 inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {
485 return error_code(static_cast<int>(__e), future_category());
488 inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {
489 return error_condition(static_cast<int>(__e), future_category());
492 [[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);
494 class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {
497 future_error(error_code);
498 friend void __throw_future_error(future_errc);
500 friend class promise;
503 # if _LIBCPP_STD_VER >= 17
504 _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
507 _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
509 _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
510 ~future_error() _NOEXCEPT override;
513 // Declared above std::future_error
514 void __throw_future_error(future_errc __ev) {
515 # if _LIBCPP_HAS_EXCEPTIONS
516 throw future_error(make_error_code(__ev));
519 _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
523 class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {
525 exception_ptr __exception_;
526 mutable mutex __mut_;
527 mutable condition_variable __cv_;
530 void __on_zero_shared() _NOEXCEPT override;
531 void __sub_wait(unique_lock<mutex>& __lk);
534 enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };
536 _LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}
538 _LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }
540 _LIBCPP_HIDE_FROM_ABI void __attach_future() {
541 lock_guard<mutex> __lk(__mut_);
542 bool __has_future_attached = (__state_ & __future_attached) != 0;
543 if (__has_future_attached)
544 __throw_future_error(future_errc::future_already_retrieved);
545 this->__add_shared();
546 __state_ |= __future_attached;
549 _LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }
552 _LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }
555 void set_value_at_thread_exit();
557 void set_exception(exception_ptr __p);
558 void set_exception_at_thread_exit(exception_ptr __p);
563 template <class _Rep, class _Period>
564 future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
565 template <class _Clock, class _Duration>
566 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS future_status
567 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
569 virtual void __execute();
572 template <class _Clock, class _Duration>
573 future_status __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
574 unique_lock<mutex> __lk(__mut_);
575 if (__state_ & deferred)
576 return future_status::deferred;
577 while (!(__state_ & ready) && _Clock::now() < __abs_time)
578 __cv_.wait_until(__lk, __abs_time);
579 if (__state_ & ready)
580 return future_status::ready;
581 return future_status::timeout;
584 template <class _Rep, class _Period>
585 inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
586 return wait_until(chrono::steady_clock::now() + __rel_time);
590 class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {
591 typedef __assoc_sub_state base;
592 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
593 typedef typename aligned_storage<sizeof(_Rp), _LIBCPP_ALIGNOF(_Rp)>::type _Up;
594 _LIBCPP_SUPPRESS_DEPRECATED_POP
599 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
602 template <class _Arg>
603 _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
605 template <class _Arg>
606 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
608 _LIBCPP_HIDE_FROM_ABI _Rp move();
609 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
613 void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {
614 if (this->__state_ & base::__constructed)
615 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
620 template <class _Arg>
621 void __assoc_state<_Rp>::set_value(_Arg&& __arg) {
622 unique_lock<mutex> __lk(this->__mut_);
623 if (this->__has_value())
624 __throw_future_error(future_errc::promise_already_satisfied);
625 ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
626 this->__state_ |= base::__constructed | base::ready;
631 template <class _Arg>
632 void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {
633 unique_lock<mutex> __lk(this->__mut_);
634 if (this->__has_value())
635 __throw_future_error(future_errc::promise_already_satisfied);
636 ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
637 this->__state_ |= base::__constructed;
638 __thread_local_data()->__make_ready_at_thread_exit(this);
642 _Rp __assoc_state<_Rp>::move() {
643 unique_lock<mutex> __lk(this->__mut_);
644 this->__sub_wait(__lk);
645 if (this->__exception_ != nullptr)
646 std::rethrow_exception(this->__exception_);
647 return std::move(*reinterpret_cast<_Rp*>(&__value_));
651 _Rp& __assoc_state<_Rp>::copy() {
652 unique_lock<mutex> __lk(this->__mut_);
653 this->__sub_wait(__lk);
654 if (this->__exception_ != nullptr)
655 std::rethrow_exception(this->__exception_);
656 return *reinterpret_cast<_Rp*>(&__value_);
660 class __assoc_state<_Rp&> : public __assoc_sub_state {
661 typedef __assoc_sub_state base;
667 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
670 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
671 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
673 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
677 void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {
682 void __assoc_state<_Rp&>::set_value(_Rp& __arg) {
683 unique_lock<mutex> __lk(this->__mut_);
684 if (this->__has_value())
685 __throw_future_error(future_errc::promise_already_satisfied);
686 __value_ = std::addressof(__arg);
687 this->__state_ |= base::__constructed | base::ready;
692 void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {
693 unique_lock<mutex> __lk(this->__mut_);
694 if (this->__has_value())
695 __throw_future_error(future_errc::promise_already_satisfied);
696 __value_ = std::addressof(__arg);
697 this->__state_ |= base::__constructed;
698 __thread_local_data()->__make_ready_at_thread_exit(this);
702 _Rp& __assoc_state<_Rp&>::copy() {
703 unique_lock<mutex> __lk(this->__mut_);
704 this->__sub_wait(__lk);
705 if (this->__exception_ != nullptr)
706 std::rethrow_exception(this->__exception_);
710 template <class _Rp, class _Alloc>
711 class __assoc_state_alloc : public __assoc_state<_Rp> {
712 typedef __assoc_state<_Rp> base;
715 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
718 _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
721 template <class _Rp, class _Alloc>
722 void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {
723 if (this->__state_ & base::__constructed)
724 reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();
725 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
726 typedef allocator_traits<_Al> _ATraits;
727 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
729 this->~__assoc_state_alloc();
730 __a.deallocate(_PTraits::pointer_to(*this), 1);
733 template <class _Rp, class _Alloc>
734 class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {
735 typedef __assoc_state<_Rp&> base;
738 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
741 _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
744 template <class _Rp, class _Alloc>
745 void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {
746 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
747 typedef allocator_traits<_Al> _ATraits;
748 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
750 this->~__assoc_state_alloc();
751 __a.deallocate(_PTraits::pointer_to(*this), 1);
754 template <class _Alloc>
755 class __assoc_sub_state_alloc : public __assoc_sub_state {
756 typedef __assoc_sub_state base;
759 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
762 _LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
765 template <class _Alloc>
766 void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {
767 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
768 typedef allocator_traits<_Al> _ATraits;
769 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
771 this->~__assoc_sub_state_alloc();
772 __a.deallocate(_PTraits::pointer_to(*this), 1);
775 template <class _Rp, class _Fp>
776 class __deferred_assoc_state : public __assoc_state<_Rp> {
777 typedef __assoc_state<_Rp> base;
782 _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
784 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
787 template <class _Rp, class _Fp>
788 inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
789 this->__set_deferred();
792 template <class _Rp, class _Fp>
793 void __deferred_assoc_state<_Rp, _Fp>::__execute() {
794 # if _LIBCPP_HAS_EXCEPTIONS
796 # endif // _LIBCPP_HAS_EXCEPTIONS
797 this->set_value(__func_());
798 # if _LIBCPP_HAS_EXCEPTIONS
800 this->set_exception(current_exception());
802 # endif // _LIBCPP_HAS_EXCEPTIONS
806 class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {
807 typedef __assoc_sub_state base;
812 _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
814 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
818 inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
819 this->__set_deferred();
823 void __deferred_assoc_state<void, _Fp>::__execute() {
824 # if _LIBCPP_HAS_EXCEPTIONS
826 # endif // _LIBCPP_HAS_EXCEPTIONS
829 # if _LIBCPP_HAS_EXCEPTIONS
831 this->set_exception(current_exception());
833 # endif // _LIBCPP_HAS_EXCEPTIONS
836 template <class _Rp, class _Fp>
837 class __async_assoc_state : public __assoc_state<_Rp> {
838 typedef __assoc_state<_Rp> base;
842 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
845 _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
847 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
850 template <class _Rp, class _Fp>
851 inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
853 template <class _Rp, class _Fp>
854 void __async_assoc_state<_Rp, _Fp>::__execute() {
855 # if _LIBCPP_HAS_EXCEPTIONS
857 # endif // _LIBCPP_HAS_EXCEPTIONS
858 this->set_value(__func_());
859 # if _LIBCPP_HAS_EXCEPTIONS
861 this->set_exception(current_exception());
863 # endif // _LIBCPP_HAS_EXCEPTIONS
866 template <class _Rp, class _Fp>
867 void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {
869 base::__on_zero_shared();
873 class __async_assoc_state<void, _Fp> : public __assoc_sub_state {
874 typedef __assoc_sub_state base;
878 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
881 _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
883 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
887 inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
890 void __async_assoc_state<void, _Fp>::__execute() {
891 # if _LIBCPP_HAS_EXCEPTIONS
893 # endif // _LIBCPP_HAS_EXCEPTIONS
896 # if _LIBCPP_HAS_EXCEPTIONS
898 this->set_exception(current_exception());
900 # endif // _LIBCPP_HAS_EXCEPTIONS
904 void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {
906 base::__on_zero_shared();
910 class _LIBCPP_TEMPLATE_VIS promise;
912 class _LIBCPP_TEMPLATE_VIS shared_future;
917 class _LIBCPP_TEMPLATE_VIS future;
919 template <class _Rp, class _Fp>
920 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);
922 template <class _Rp, class _Fp>
923 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);
926 class _LIBCPP_TEMPLATE_VIS future {
927 __assoc_state<_Rp>* __state_;
929 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
932 friend class promise;
934 friend class shared_future;
936 template <class _R1, class _Fp>
937 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
938 template <class _R1, class _Fp>
939 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
942 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
943 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
944 future(const future&) = delete;
945 future& operator=(const future&) = delete;
946 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
947 future(std::move(__rhs)).swap(*this);
951 _LIBCPP_HIDE_FROM_ABI ~future();
952 _LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;
954 // retrieving the value
955 _LIBCPP_HIDE_FROM_ABI _Rp get();
957 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
959 // functions to check state
960 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
962 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
963 template <class _Rep, class _Period>
964 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
965 return __state_->wait_for(__rel_time);
967 template <class _Clock, class _Duration>
968 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
969 return __state_->wait_until(__abs_time);
974 future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {
975 __state_->__attach_future();
978 struct __release_shared_count {
979 _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }
983 future<_Rp>::~future() {
985 __state_->__release_shared();
989 _Rp future<_Rp>::get() {
990 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
991 __assoc_state<_Rp>* __s = __state_;
997 class _LIBCPP_TEMPLATE_VIS future<_Rp&> {
998 __assoc_state<_Rp&>* __state_;
1000 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
1003 friend class promise;
1005 friend class shared_future;
1007 template <class _R1, class _Fp>
1008 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1009 template <class _R1, class _Fp>
1010 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1013 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1014 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1015 future(const future&) = delete;
1016 future& operator=(const future&) = delete;
1017 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1018 future(std::move(__rhs)).swap(*this);
1022 _LIBCPP_HIDE_FROM_ABI ~future();
1023 _LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;
1025 // retrieving the value
1026 _LIBCPP_HIDE_FROM_ABI _Rp& get();
1028 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1030 // functions to check state
1031 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1033 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1034 template <class _Rep, class _Period>
1035 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1036 return __state_->wait_for(__rel_time);
1038 template <class _Clock, class _Duration>
1039 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1040 return __state_->wait_until(__abs_time);
1044 template <class _Rp>
1045 future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {
1046 __state_->__attach_future();
1049 template <class _Rp>
1050 future<_Rp&>::~future() {
1052 __state_->__release_shared();
1055 template <class _Rp>
1056 _Rp& future<_Rp&>::get() {
1057 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1058 __assoc_state<_Rp&>* __s = __state_;
1064 class _LIBCPP_EXPORTED_FROM_ABI future<void> {
1065 __assoc_sub_state* __state_;
1067 explicit future(__assoc_sub_state* __state);
1070 friend class promise;
1072 friend class shared_future;
1074 template <class _R1, class _Fp>
1075 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1076 template <class _R1, class _Fp>
1077 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1080 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1081 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1082 future(const future&) = delete;
1083 future& operator=(const future&) = delete;
1084 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1085 future(std::move(__rhs)).swap(*this);
1090 _LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;
1092 // retrieving the value
1095 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1097 // functions to check state
1098 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1100 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1101 template <class _Rep, class _Period>
1102 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1103 return __state_->wait_for(__rel_time);
1105 template <class _Clock, class _Duration>
1106 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1107 return __state_->wait_until(__abs_time);
1111 template <class _Rp>
1112 inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {
1118 template <class _Callable>
1119 class packaged_task;
1121 template <class _Rp>
1122 class _LIBCPP_TEMPLATE_VIS promise {
1123 __assoc_state<_Rp>* __state_;
1125 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1128 friend class packaged_task;
1131 _LIBCPP_HIDE_FROM_ABI promise();
1132 template <class _Alloc>
1133 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1134 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1135 promise(const promise& __rhs) = delete;
1136 _LIBCPP_HIDE_FROM_ABI ~promise();
1139 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1140 promise(std::move(__rhs)).swap(*this);
1143 promise& operator=(const promise& __rhs) = delete;
1145 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1147 // retrieving the result
1148 _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1150 // setting the result
1151 _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1152 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1153 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1155 // setting the result with deferred notification
1156 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1157 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1158 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1161 template <class _Rp>
1162 promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}
1164 template <class _Rp>
1165 template <class _Alloc>
1166 promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {
1167 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1168 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1169 typedef __allocator_destructor<_A2> _D2;
1171 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1172 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1173 __state_ = std::addressof(*__hold.release());
1176 template <class _Rp>
1177 promise<_Rp>::~promise() {
1179 if (!__state_->__has_value() && __state_->use_count() > 1)
1180 __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1181 __state_->__release_shared();
1185 template <class _Rp>
1186 future<_Rp> promise<_Rp>::get_future() {
1187 if (__state_ == nullptr)
1188 __throw_future_error(future_errc::no_state);
1189 return future<_Rp>(__state_);
1192 template <class _Rp>
1193 void promise<_Rp>::set_value(const _Rp& __r) {
1194 if (__state_ == nullptr)
1195 __throw_future_error(future_errc::no_state);
1196 __state_->set_value(__r);
1199 template <class _Rp>
1200 void promise<_Rp>::set_value(_Rp&& __r) {
1201 if (__state_ == nullptr)
1202 __throw_future_error(future_errc::no_state);
1203 __state_->set_value(std::move(__r));
1206 template <class _Rp>
1207 void promise<_Rp>::set_exception(exception_ptr __p) {
1208 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1209 if (__state_ == nullptr)
1210 __throw_future_error(future_errc::no_state);
1211 __state_->set_exception(__p);
1214 template <class _Rp>
1215 void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {
1216 if (__state_ == nullptr)
1217 __throw_future_error(future_errc::no_state);
1218 __state_->set_value_at_thread_exit(__r);
1221 template <class _Rp>
1222 void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {
1223 if (__state_ == nullptr)
1224 __throw_future_error(future_errc::no_state);
1225 __state_->set_value_at_thread_exit(std::move(__r));
1228 template <class _Rp>
1229 void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {
1230 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1231 if (__state_ == nullptr)
1232 __throw_future_error(future_errc::no_state);
1233 __state_->set_exception_at_thread_exit(__p);
1238 template <class _Rp>
1239 class _LIBCPP_TEMPLATE_VIS promise<_Rp&> {
1240 __assoc_state<_Rp&>* __state_;
1242 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1245 friend class packaged_task;
1248 _LIBCPP_HIDE_FROM_ABI promise();
1249 template <class _Allocator>
1250 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1251 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1252 promise(const promise& __rhs) = delete;
1253 _LIBCPP_HIDE_FROM_ABI ~promise();
1256 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1257 promise(std::move(__rhs)).swap(*this);
1260 promise& operator=(const promise& __rhs) = delete;
1262 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1264 // retrieving the result
1265 _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1267 // setting the result
1268 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1269 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1271 // setting the result with deferred notification
1272 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1273 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1276 template <class _Rp>
1277 promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}
1279 template <class _Rp>
1280 template <class _Alloc>
1281 promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {
1282 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1283 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1284 typedef __allocator_destructor<_A2> _D2;
1286 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1287 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1288 __state_ = std::addressof(*__hold.release());
1291 template <class _Rp>
1292 promise<_Rp&>::~promise() {
1294 if (!__state_->__has_value() && __state_->use_count() > 1)
1295 __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
1296 __state_->__release_shared();
1300 template <class _Rp>
1301 future<_Rp&> promise<_Rp&>::get_future() {
1302 if (__state_ == nullptr)
1303 __throw_future_error(future_errc::no_state);
1304 return future<_Rp&>(__state_);
1307 template <class _Rp>
1308 void promise<_Rp&>::set_value(_Rp& __r) {
1309 if (__state_ == nullptr)
1310 __throw_future_error(future_errc::no_state);
1311 __state_->set_value(__r);
1314 template <class _Rp>
1315 void promise<_Rp&>::set_exception(exception_ptr __p) {
1316 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1317 if (__state_ == nullptr)
1318 __throw_future_error(future_errc::no_state);
1319 __state_->set_exception(__p);
1322 template <class _Rp>
1323 void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {
1324 if (__state_ == nullptr)
1325 __throw_future_error(future_errc::no_state);
1326 __state_->set_value_at_thread_exit(__r);
1329 template <class _Rp>
1330 void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {
1331 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1332 if (__state_ == nullptr)
1333 __throw_future_error(future_errc::no_state);
1334 __state_->set_exception_at_thread_exit(__p);
1340 class _LIBCPP_EXPORTED_FROM_ABI promise<void> {
1341 __assoc_sub_state* __state_;
1343 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1346 friend class packaged_task;
1350 template <class _Allocator>
1351 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS promise(allocator_arg_t, const _Allocator& __a);
1352 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1353 promise(const promise& __rhs) = delete;
1357 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1358 promise(std::move(__rhs)).swap(*this);
1361 promise& operator=(const promise& __rhs) = delete;
1363 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1365 // retrieving the result
1366 future<void> get_future();
1368 // setting the result
1370 void set_exception(exception_ptr __p);
1372 // setting the result with deferred notification
1373 void set_value_at_thread_exit();
1374 void set_exception_at_thread_exit(exception_ptr __p);
1377 template <class _Alloc>
1378 promise<void>::promise(allocator_arg_t, const _Alloc& __a0) {
1379 typedef __assoc_sub_state_alloc<_Alloc> _State;
1380 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1381 typedef __allocator_destructor<_A2> _D2;
1383 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1384 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1385 __state_ = std::addressof(*__hold.release());
1388 template <class _Rp>
1389 inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {
1393 template <class _Rp, class _Alloc>
1394 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc> : public true_type {};
1398 template <class _Fp>
1399 class __packaged_task_base;
1401 template <class _Rp, class... _ArgTypes>
1402 class __packaged_task_base<_Rp(_ArgTypes...)> {
1404 _LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}
1405 __packaged_task_base(const __packaged_task_base&) = delete;
1406 __packaged_task_base& operator=(const __packaged_task_base&) = delete;
1407 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1408 virtual ~__packaged_task_base() {}
1409 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1410 virtual void destroy() = 0;
1411 virtual void destroy_deallocate() = 0;
1412 virtual _Rp operator()(_ArgTypes&&...) = 0;
1415 template <class _FD, class _Alloc, class _FB>
1416 class __packaged_task_func;
1418 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1419 class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
1420 _LIBCPP_COMPRESSED_PAIR(_Fp, __func_, _Alloc, __alloc_);
1423 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __func_(__f) {}
1424 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __func_(std::move(__f)) {}
1425 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __func_(__f), __alloc_(__a) {}
1426 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __func_(std::move(__f)), __alloc_(__a) {}
1427 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1428 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1429 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1430 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);
1433 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1434 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1435 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {
1436 ::new ((void*)__p) __packaged_task_func(std::move(__func_), std::move(__alloc_));
1439 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1440 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
1445 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1446 void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {
1447 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1448 typedef allocator_traits<_Ap> _ATraits;
1449 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1453 __a.deallocate(_PTraits::pointer_to(*this), 1);
1456 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1457 _Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
1458 return std::__invoke(__func_, std::forward<_ArgTypes>(__arg)...);
1461 template <class _Callable>
1462 class __packaged_task_function;
1464 template <class _Rp, class... _ArgTypes>
1465 class __packaged_task_function<_Rp(_ArgTypes...)> {
1466 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1468 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }
1470 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1471 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1472 _LIBCPP_SUPPRESS_DEPRECATED_POP
1476 typedef _Rp result_type;
1478 // construct/copy/destroy:
1479 _LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1480 template <class _Fp>
1481 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1482 template <class _Fp, class _Alloc>
1483 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1485 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1486 _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1488 __packaged_task_function(const __packaged_task_function&) = delete;
1489 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1491 _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1493 _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1495 _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1498 template <class _Rp, class... _ArgTypes>
1499 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {
1500 if (__f.__f_ == nullptr)
1502 else if (__f.__f_ == __f.__get_buf()) {
1503 __f.__f_->__move_to(__get_buf());
1504 __f_ = (__base*)&__buf_;
1511 template <class _Rp, class... _ArgTypes>
1512 template <class _Fp>
1513 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {
1514 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1515 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1516 if (sizeof(_FF) <= sizeof(__buf_)) {
1517 ::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));
1518 __f_ = (__base*)&__buf_;
1520 typedef allocator<_FF> _Ap;
1522 typedef __allocator_destructor<_Ap> _Dp;
1523 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1524 ::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));
1525 __f_ = __hold.release();
1529 template <class _Rp, class... _ArgTypes>
1530 template <class _Fp, class _Alloc>
1531 __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1533 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1534 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1535 if (sizeof(_FF) <= sizeof(__buf_)) {
1536 __f_ = (__base*)&__buf_;
1537 ::new ((void*)__f_) _FF(std::forward<_Fp>(__f));
1539 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1541 typedef __allocator_destructor<_Ap> _Dp;
1542 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1543 ::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));
1544 __f_ = std::addressof(*__hold.release());
1548 template <class _Rp, class... _ArgTypes>
1549 __packaged_task_function<_Rp(_ArgTypes...)>&
1550 __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {
1551 if (__f_ == __get_buf())
1554 __f_->destroy_deallocate();
1556 if (__f.__f_ == nullptr)
1558 else if (__f.__f_ == __f.__get_buf()) {
1559 __f.__f_->__move_to(__get_buf());
1568 template <class _Rp, class... _ArgTypes>
1569 __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {
1570 if (__f_ == __get_buf())
1573 __f_->destroy_deallocate();
1576 template <class _Rp, class... _ArgTypes>
1577 _LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {
1578 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {
1579 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1580 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1581 _LIBCPP_SUPPRESS_DEPRECATED_POP
1582 __base* __t = (__base*)&__tempbuf;
1583 __f_->__move_to(__t);
1586 __f.__f_->__move_to((__base*)&__buf_);
1587 __f.__f_->destroy();
1589 __f_ = (__base*)&__buf_;
1590 __t->__move_to((__base*)&__f.__buf_);
1592 __f.__f_ = (__base*)&__f.__buf_;
1593 } else if (__f_ == (__base*)&__buf_) {
1594 __f_->__move_to((__base*)&__f.__buf_);
1597 __f.__f_ = (__base*)&__f.__buf_;
1598 } else if (__f.__f_ == (__base*)&__f.__buf_) {
1599 __f.__f_->__move_to((__base*)&__buf_);
1600 __f.__f_->destroy();
1602 __f_ = (__base*)&__buf_;
1604 std::swap(__f_, __f.__f_);
1607 template <class _Rp, class... _ArgTypes>
1608 inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
1609 return (*__f_)(std::forward<_ArgTypes>(__arg)...);
1612 template <class _Rp, class... _ArgTypes>
1613 class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)> {
1615 typedef _Rp result_type; // extension
1618 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1619 promise<result_type> __p_;
1622 // construction and destruction
1623 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1625 template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1626 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1628 # if _LIBCPP_STD_VER <= 14
1629 template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1630 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1631 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1633 // ~packaged_task() = default;
1636 packaged_task(const packaged_task&) = delete;
1637 packaged_task& operator=(const packaged_task&) = delete;
1640 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1641 : __f_(std::move(__other.__f_)),
1642 __p_(std::move(__other.__p_)) {}
1643 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1644 __f_ = std::move(__other.__f_);
1645 __p_ = std::move(__other.__p_);
1648 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1649 __f_.swap(__other.__f_);
1650 __p_.swap(__other.__p_);
1653 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1656 _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1659 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1660 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1662 _LIBCPP_HIDE_FROM_ABI void reset();
1665 template <class _Rp, class... _ArgTypes>
1666 void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1667 if (__p_.__state_ == nullptr)
1668 __throw_future_error(future_errc::no_state);
1669 if (__p_.__state_->__has_value())
1670 __throw_future_error(future_errc::promise_already_satisfied);
1671 # if _LIBCPP_HAS_EXCEPTIONS
1673 # endif // _LIBCPP_HAS_EXCEPTIONS
1674 __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
1675 # if _LIBCPP_HAS_EXCEPTIONS
1677 __p_.set_exception(current_exception());
1679 # endif // _LIBCPP_HAS_EXCEPTIONS
1682 template <class _Rp, class... _ArgTypes>
1683 void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1684 if (__p_.__state_ == nullptr)
1685 __throw_future_error(future_errc::no_state);
1686 if (__p_.__state_->__has_value())
1687 __throw_future_error(future_errc::promise_already_satisfied);
1688 # if _LIBCPP_HAS_EXCEPTIONS
1690 # endif // _LIBCPP_HAS_EXCEPTIONS
1691 __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
1692 # if _LIBCPP_HAS_EXCEPTIONS
1694 __p_.set_exception_at_thread_exit(current_exception());
1696 # endif // _LIBCPP_HAS_EXCEPTIONS
1699 template <class _Rp, class... _ArgTypes>
1700 void packaged_task<_Rp(_ArgTypes...)>::reset() {
1702 __throw_future_error(future_errc::no_state);
1703 __p_ = promise<result_type>();
1706 template <class... _ArgTypes>
1707 class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)> {
1709 typedef void result_type; // extension
1712 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1713 promise<result_type> __p_;
1716 // construction and destruction
1717 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1718 template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1719 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1720 # if _LIBCPP_STD_VER <= 14
1721 template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1722 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1723 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1725 // ~packaged_task() = default;
1728 packaged_task(const packaged_task&) = delete;
1729 packaged_task& operator=(const packaged_task&) = delete;
1732 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1733 : __f_(std::move(__other.__f_)),
1734 __p_(std::move(__other.__p_)) {}
1735 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1736 __f_ = std::move(__other.__f_);
1737 __p_ = std::move(__other.__p_);
1740 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1741 __f_.swap(__other.__f_);
1742 __p_.swap(__other.__p_);
1745 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1748 _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
1751 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1752 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1754 _LIBCPP_HIDE_FROM_ABI void reset();
1757 # if _LIBCPP_STD_VER >= 17
1759 template <class _Rp, class... _Args>
1760 packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
1762 template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1763 packaged_task(_Fp) -> packaged_task<_Stripped>;
1767 template <class... _ArgTypes>
1768 void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1769 if (__p_.__state_ == nullptr)
1770 __throw_future_error(future_errc::no_state);
1771 if (__p_.__state_->__has_value())
1772 __throw_future_error(future_errc::promise_already_satisfied);
1773 # if _LIBCPP_HAS_EXCEPTIONS
1775 # endif // _LIBCPP_HAS_EXCEPTIONS
1776 __f_(std::forward<_ArgTypes>(__args)...);
1778 # if _LIBCPP_HAS_EXCEPTIONS
1780 __p_.set_exception(current_exception());
1782 # endif // _LIBCPP_HAS_EXCEPTIONS
1785 template <class... _ArgTypes>
1786 void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1787 if (__p_.__state_ == nullptr)
1788 __throw_future_error(future_errc::no_state);
1789 if (__p_.__state_->__has_value())
1790 __throw_future_error(future_errc::promise_already_satisfied);
1791 # if _LIBCPP_HAS_EXCEPTIONS
1793 # endif // _LIBCPP_HAS_EXCEPTIONS
1794 __f_(std::forward<_ArgTypes>(__args)...);
1795 __p_.set_value_at_thread_exit();
1796 # if _LIBCPP_HAS_EXCEPTIONS
1798 __p_.set_exception_at_thread_exit(current_exception());
1800 # endif // _LIBCPP_HAS_EXCEPTIONS
1803 template <class... _ArgTypes>
1804 void packaged_task<void(_ArgTypes...)>::reset() {
1806 __throw_future_error(future_errc::no_state);
1807 __p_ = promise<result_type>();
1810 template <class _Rp, class... _ArgTypes>
1811 inline _LIBCPP_HIDE_FROM_ABI void
1812 swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1816 # if _LIBCPP_STD_VER <= 14
1817 template <class _Callable, class _Alloc>
1818 struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
1821 template <class _Rp, class _Fp>
1822 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
1823 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1824 new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1825 return future<_Rp>(__h.get());
1828 template <class _Rp, class _Fp>
1829 _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
1830 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1831 new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1832 std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
1833 return future<_Rp>(__h.get());
1836 # ifndef _LIBCPP_CXX03_LANG
1838 template <class _Fp, class... _Args>
1839 class _LIBCPP_HIDDEN __async_func {
1840 tuple<_Fp, _Args...> __f_;
1843 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
1845 _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Fp&& __f, _Args&&... __args)
1846 : __f_(std::move(__f), std::move(__args)...) {}
1848 _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
1850 _LIBCPP_HIDE_FROM_ABI _Rp operator()() {
1851 typedef typename __make_tuple_indices<1 + sizeof...(_Args), 1>::type _Index;
1852 return __execute(_Index());
1856 template <size_t... _Indices>
1857 _LIBCPP_HIDE_FROM_ABI _Rp __execute(__tuple_indices<_Indices...>) {
1858 return std::__invoke(std::move(std::get<0>(__f_)), std::move(std::get<_Indices>(__f_))...);
1862 inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
1863 return (int(__policy) & int(__value)) != 0;
1866 template <class _Fp, class... _Args>
1867 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1868 async(launch __policy, _Fp&& __f, _Args&&... __args) {
1869 typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
1870 typedef typename _BF::_Rp _Rp;
1872 # if _LIBCPP_HAS_EXCEPTIONS
1875 if (__does_policy_contain(__policy, launch::async))
1876 return std::__make_async_assoc_state<_Rp>(
1877 _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1878 # if _LIBCPP_HAS_EXCEPTIONS
1880 if (__policy == launch::async)
1885 if (__does_policy_contain(__policy, launch::deferred))
1886 return std::__make_deferred_assoc_state<_Rp>(
1887 _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
1888 return future<_Rp>{};
1891 template <class _Fp, class... _Args>
1892 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
1893 async(_Fp&& __f, _Args&&... __args) {
1894 return std::async(launch::any, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);
1901 template <class _Rp>
1902 class _LIBCPP_TEMPLATE_VIS shared_future {
1903 __assoc_state<_Rp>* __state_;
1906 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1907 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1909 __state_->__add_shared();
1911 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1912 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1913 __rhs.__state_ = nullptr;
1915 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1916 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
1917 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1918 shared_future(std::move(__rhs)).swap(*this);
1922 // retrieving the value
1923 _LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }
1925 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1927 // functions to check state
1928 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1930 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1931 template <class _Rep, class _Period>
1932 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1933 return __state_->wait_for(__rel_time);
1935 template <class _Clock, class _Duration>
1936 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1937 return __state_->wait_until(__abs_time);
1941 template <class _Rp>
1942 shared_future<_Rp>::~shared_future() {
1944 __state_->__release_shared();
1947 template <class _Rp>
1948 shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {
1950 __rhs.__state_->__add_shared();
1952 __state_->__release_shared();
1953 __state_ = __rhs.__state_;
1957 template <class _Rp>
1958 class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&> {
1959 __assoc_state<_Rp&>* __state_;
1962 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1963 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
1965 __state_->__add_shared();
1967 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1968 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1969 __rhs.__state_ = nullptr;
1971 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1972 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
1973 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1974 shared_future(std::move(__rhs)).swap(*this);
1978 // retrieving the value
1979 _LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }
1981 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1983 // functions to check state
1984 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1986 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1987 template <class _Rep, class _Period>
1988 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1989 return __state_->wait_for(__rel_time);
1991 template <class _Clock, class _Duration>
1992 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1993 return __state_->wait_until(__abs_time);
1997 template <class _Rp>
1998 shared_future<_Rp&>::~shared_future() {
2000 __state_->__release_shared();
2003 template <class _Rp>
2004 shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {
2006 __rhs.__state_->__add_shared();
2008 __state_->__release_shared();
2009 __state_ = __rhs.__state_;
2014 class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
2015 __assoc_sub_state* __state_;
2018 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
2019 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
2021 __state_->__add_shared();
2023 _LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
2024 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
2025 __rhs.__state_ = nullptr;
2028 shared_future& operator=(const shared_future& __rhs);
2029 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
2030 shared_future(std::move(__rhs)).swap(*this);
2034 // retrieving the value
2035 _LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }
2037 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
2039 // functions to check state
2040 _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
2042 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
2043 template <class _Rep, class _Period>
2044 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
2045 return __state_->wait_for(__rel_time);
2047 template <class _Clock, class _Duration>
2048 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
2049 return __state_->wait_until(__abs_time);
2053 template <class _Rp>
2054 inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
2058 template <class _Rp>
2059 inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
2060 return shared_future<_Rp>(std::move(*this));
2063 template <class _Rp>
2064 inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
2065 return shared_future<_Rp&>(std::move(*this));
2068 inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
2070 _LIBCPP_END_NAMESPACE_STD
2074 # endif // _LIBCPP_HAS_THREADS
2076 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2080 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2083 # include <exception>
2085 # include <system_error>
2088 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
2090 #endif // _LIBCPP_FUTURE