3// Copyright (C) 2009-2022 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file include/future
26 * This is a Standard C++ Library header.
29#ifndef _GLIBCXX_FUTURE
30#define _GLIBCXX_FUTURE 1
32#pragma GCC system_header
34#if __cplusplus < 201103L
35# include <bits/c++0x_warning.h>
38#include <mutex> // call_once
39#include <condition_variable> // __at_thread_exit_elt
40#include <system_error>
41#include <bits/atomic_base.h> // atomic_flag
42#include <bits/allocated_ptr.h>
43#include <bits/atomic_futex.h>
44#include <bits/exception_defines.h>
45#include <bits/invoke.h>
46#include <bits/unique_ptr.h>
47#include <bits/shared_ptr.h>
48#include <bits/std_function.h>
49#include <bits/std_thread.h>
50#include <bits/uses_allocator.h>
51#include <ext/aligned_buffer.h>
53namespace std _GLIBCXX_VISIBILITY(default)
55_GLIBCXX_BEGIN_NAMESPACE_VERSION
58 * @defgroup futures Futures
59 * @ingroup concurrency
61 * Futures and promises provide support for retrieving the result from
62 * an asynchronous function, e.g. one that is running in another thread.
63 * A `std::future` represents an asynchronous result that will become
64 * ready at some later time. A consumer can wait on a future until the
65 * result is ready to be accessed.
71 /// Error code for futures
72 enum class future_errc
74 future_already_retrieved = 1,
75 promise_already_satisfied,
80 /// Specialization that allows `future_errc` to convert to `error_code`.
82 struct is_error_code_enum<future_errc> : public true_type { };
84 /// Points to a statically-allocated object derived from error_category.
85 [[__nodiscard__, __gnu__::__const__]]
87 future_category() noexcept;
89 /// Overload of make_error_code for `future_errc`.
92 make_error_code(future_errc __errc) noexcept
93 { return error_code(static_cast<int>(__errc), future_category()); }
95 /// Overload of make_error_condition for `future_errc`.
97 inline error_condition
98 make_error_condition(future_errc __errc) noexcept
99 { return error_condition(static_cast<int>(__errc), future_category()); }
102 * @brief Exception type thrown by futures.
103 * @ingroup exceptions
106 class future_error : public logic_error
110 future_error(future_errc __errc)
111 : future_error(std::make_error_code(__errc))
114 virtual ~future_error() noexcept;
117 what() const noexcept;
120 code() const noexcept { return _M_code; }
124 future_error(error_code __ec)
125 : logic_error("std::future_error: " + __ec.message()), _M_code(__ec)
128 friend void __throw_future_error(int);
133 // Forward declarations.
134 template<typename _Res>
137 template<typename _Res>
140 template<typename _Signature>
143 template<typename _Res>
146 /// Launch code for futures
153 constexpr launch operator&(launch __x, launch __y) noexcept
155 return static_cast<launch>(
156 static_cast<int>(__x) & static_cast<int>(__y));
159 constexpr launch operator|(launch __x, launch __y) noexcept
161 return static_cast<launch>(
162 static_cast<int>(__x) | static_cast<int>(__y));
165 constexpr launch operator^(launch __x, launch __y) noexcept
167 return static_cast<launch>(
168 static_cast<int>(__x) ^ static_cast<int>(__y));
171 constexpr launch operator~(launch __x) noexcept
172 { return static_cast<launch>(~static_cast<int>(__x)); }
174 inline launch& operator&=(launch& __x, launch __y) noexcept
175 { return __x = __x & __y; }
177 inline launch& operator|=(launch& __x, launch __y) noexcept
178 { return __x = __x | __y; }
180 inline launch& operator^=(launch& __x, launch __y) noexcept
181 { return __x = __x ^ __y; }
183 /// Status code for futures
184 enum class future_status
191 /// @cond undocumented
192 // _GLIBCXX_RESOLVE_LIB_DEFECTS
193 // 2021. Further incorrect usages of result_of
194 template<typename _Fn, typename... _Args>
195 using __async_result_of = typename __invoke_result<
196 typename decay<_Fn>::type, typename decay<_Args>::type...>::type;
199 template<typename _Fn, typename... _Args>
200 future<__async_result_of<_Fn, _Args...>>
201 async(launch __policy, _Fn&& __fn, _Args&&... __args);
203 template<typename _Fn, typename... _Args>
204 future<__async_result_of<_Fn, _Args...>>
205 async(_Fn&& __fn, _Args&&... __args);
207#if defined(_GLIBCXX_HAS_GTHREADS)
209 /// @cond undocumented
211 /// Base class and enclosing scope.
214 /// Base class for results.
217 exception_ptr _M_error;
219 _Result_base(const _Result_base&) = delete;
220 _Result_base& operator=(const _Result_base&) = delete;
222 // _M_destroy() allows derived classes to control deallocation
223 virtual void _M_destroy() = 0;
227 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
232 virtual ~_Result_base();
235 /// A unique_ptr for result objects.
236 template<typename _Res>
237 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
239 /// A result object that has storage for an object of type _Res.
240 template<typename _Res>
241 struct _Result : _Result_base
244 __gnu_cxx::__aligned_buffer<_Res> _M_storage;
248 typedef _Res result_type;
250 _Result() noexcept : _M_initialized() { }
258 // Return lvalue, future will add const or rvalue-reference
260 _M_value() noexcept { return *_M_storage._M_ptr(); }
263 _M_set(const _Res& __res)
265 ::new (_M_storage._M_addr()) _Res(__res);
266 _M_initialized = true;
272 ::new (_M_storage._M_addr()) _Res(std::move(__res));
273 _M_initialized = true;
277 void _M_destroy() { delete this; }
280 /// A result object that uses an allocator.
281 template<typename _Res, typename _Alloc>
282 struct _Result_alloc final : _Result<_Res>, _Alloc
284 using __allocator_type = __alloc_rebind<_Alloc, _Result_alloc>;
287 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
293 __allocator_type __a(*this);
294 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
295 this->~_Result_alloc();
299 // Create a result object that uses an allocator.
300 template<typename _Res, typename _Allocator>
301 static _Ptr<_Result_alloc<_Res, _Allocator>>
302 _S_allocate_result(const _Allocator& __a)
304 using __result_type = _Result_alloc<_Res, _Allocator>;
305 typename __result_type::__allocator_type __a2(__a);
306 auto __guard = std::__allocate_guarded(__a2);
307 __result_type* __p = ::new((void*)__guard.get()) __result_type{__a};
309 return _Ptr<__result_type>(__p);
312 // Keep it simple for std::allocator.
313 template<typename _Res, typename _Tp>
314 static _Ptr<_Result<_Res>>
315 _S_allocate_result(const std::allocator<_Tp>& __a)
317 return _Ptr<_Result<_Res>>(new _Result<_Res>);
320 // Base class for various types of shared state created by an
321 // asynchronous provider (such as a std::promise) and shared with one
322 // or more associated futures.
325 typedef _Ptr<_Result_base> _Ptr_type;
327 enum _Status : unsigned {
333 __atomic_futex_unsigned<> _M_status;
334 atomic_flag _M_retrieved = ATOMIC_FLAG_INIT;
338 _State_baseV2() noexcept : _M_result(), _M_status(_Status::__not_ready)
340 _State_baseV2(const _State_baseV2&) = delete;
341 _State_baseV2& operator=(const _State_baseV2&) = delete;
342 virtual ~_State_baseV2() = default;
347 // Run any deferred function or join any asynchronous thread:
349 // Acquire MO makes sure this synchronizes with the thread that made
351 _M_status._M_load_when_equal(_Status::__ready, memory_order_acquire);
355 template<typename _Rep, typename _Period>
357 wait_for(const chrono::duration<_Rep, _Period>& __rel)
359 // First, check if the future has been made ready. Use acquire MO
360 // to synchronize with the thread that made it ready.
361 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
362 return future_status::ready;
364 if (_M_is_deferred_future())
365 return future_status::deferred;
367 // Don't wait unless the relative time is greater than zero.
368 if (__rel > __rel.zero()
369 && _M_status._M_load_when_equal_for(_Status::__ready,
370 memory_order_acquire,
373 // _GLIBCXX_RESOLVE_LIB_DEFECTS
374 // 2100. timed waiting functions must also join
375 // This call is a no-op by default except on an async future,
376 // in which case the async thread is joined. It's also not a
377 // no-op for a deferred future, but such a future will never
378 // reach this point because it returns future_status::deferred
379 // instead of waiting for the future to become ready (see
380 // above). Async futures synchronize in this call, so we need
381 // no further synchronization here.
384 return future_status::ready;
386 return future_status::timeout;
389 template<typename _Clock, typename _Duration>
391 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
393#if __cplusplus > 201703L
394 static_assert(chrono::is_clock_v<_Clock>);
396 // First, check if the future has been made ready. Use acquire MO
397 // to synchronize with the thread that made it ready.
398 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
399 return future_status::ready;
401 if (_M_is_deferred_future())
402 return future_status::deferred;
404 if (_M_status._M_load_when_equal_until(_Status::__ready,
405 memory_order_acquire,
408 // _GLIBCXX_RESOLVE_LIB_DEFECTS
409 // 2100. timed waiting functions must also join
410 // See wait_for(...) above.
413 return future_status::ready;
415 return future_status::timeout;
418 // Provide a result to the shared state and make it ready.
419 // Calls at most once: _M_result = __res();
421 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
423 bool __did_set = false;
424 // all calls to this function are serialized,
425 // side-effects of invoking __res only happen once
426 call_once(_M_once, &_State_baseV2::_M_do_set, this,
427 std::__addressof(__res), std::__addressof(__did_set));
429 // Use release MO to synchronize with observers of the ready state.
430 _M_status._M_store_notify_all(_Status::__ready,
431 memory_order_release);
432 else if (!__ignore_failure)
433 __throw_future_error(int(future_errc::promise_already_satisfied));
436 // Provide a result to the shared state but delay making it ready
437 // until the calling thread exits.
438 // Calls at most once: _M_result = __res();
440 _M_set_delayed_result(function<_Ptr_type()> __res,
441 weak_ptr<_State_baseV2> __self)
443 bool __did_set = false;
444 unique_ptr<_Make_ready> __mr{new _Make_ready};
445 // all calls to this function are serialized,
446 // side-effects of invoking __res only happen once
447 call_once(_M_once, &_State_baseV2::_M_do_set, this,
448 std::__addressof(__res), std::__addressof(__did_set));
450 __throw_future_error(int(future_errc::promise_already_satisfied));
451 __mr->_M_shared_state = std::move(__self);
456 // Abandon this shared state.
458 _M_break_promise(_Ptr_type __res)
460 if (static_cast<bool>(__res))
463 make_exception_ptr(future_error(future_errc::broken_promise));
464 // This function is only called when the last asynchronous result
465 // provider is abandoning this shared state, so noone can be
466 // trying to make the shared state ready at the same time, and
467 // we can access _M_result directly instead of through call_once.
468 _M_result.swap(__res);
469 // Use release MO to synchronize with observers of the ready state.
470 _M_status._M_store_notify_all(_Status::__ready,
471 memory_order_release);
475 // Called when this object is first passed to a future.
477 _M_set_retrieved_flag()
479 if (_M_retrieved.test_and_set())
480 __throw_future_error(int(future_errc::future_already_retrieved));
483 template<typename _Res, typename _Arg>
487 template<typename _Res, typename _Arg>
488 struct _Setter<_Res, _Arg&>
490 // check this is only used by promise<R>::set_value(const R&)
491 // or promise<R&>::set_value(R&)
492 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
493 || is_same<const _Res, _Arg>::value, // promise<R>
494 "Invalid specialisation");
496 // Used by std::promise to copy construct the result.
497 typename promise<_Res>::_Ptr_type operator()() const
499 _M_promise->_M_storage->_M_set(*_M_arg);
500 return std::move(_M_promise->_M_storage);
502 promise<_Res>* _M_promise;
507 template<typename _Res>
508 struct _Setter<_Res, _Res&&>
510 // Used by std::promise to move construct the result.
511 typename promise<_Res>::_Ptr_type operator()() const
513 _M_promise->_M_storage->_M_set(std::move(*_M_arg));
514 return std::move(_M_promise->_M_storage);
516 promise<_Res>* _M_promise;
521 template<typename _Res>
522 struct _Setter<_Res, void>
524 static_assert(is_void<_Res>::value, "Only used for promise<void>");
526 typename promise<_Res>::_Ptr_type operator()() const
527 { return std::move(_M_promise->_M_storage); }
529 promise<_Res>* _M_promise;
532 struct __exception_ptr_tag { };
535 template<typename _Res>
536 struct _Setter<_Res, __exception_ptr_tag>
538 // Used by std::promise to store an exception as the result.
539 typename promise<_Res>::_Ptr_type operator()() const
541 _M_promise->_M_storage->_M_error = *_M_ex;
542 return std::move(_M_promise->_M_storage);
545 promise<_Res>* _M_promise;
546 exception_ptr* _M_ex;
549 template<typename _Res, typename _Arg>
550 __attribute__((__always_inline__))
551 static _Setter<_Res, _Arg&&>
552 __setter(promise<_Res>* __prom, _Arg&& __arg) noexcept
554 return _Setter<_Res, _Arg&&>{ __prom, std::__addressof(__arg) };
557 template<typename _Res>
558 __attribute__((__always_inline__))
559 static _Setter<_Res, __exception_ptr_tag>
560 __setter(exception_ptr& __ex, promise<_Res>* __prom) noexcept
562 __glibcxx_assert(__ex != nullptr); // LWG 2276
563 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
566 template<typename _Res>
567 __attribute__((__always_inline__))
568 static _Setter<_Res, void>
569 __setter(promise<_Res>* __prom) noexcept
571 return _Setter<_Res, void>{ __prom };
574 template<typename _Tp>
576 _S_check(const shared_ptr<_Tp>& __p)
578 if (!static_cast<bool>(__p))
579 __throw_future_error((int)future_errc::no_state);
583 // The function invoked with std::call_once(_M_once, ...).
585 _M_do_set(function<_Ptr_type()>* __f, bool* __did_set)
587 _Ptr_type __res = (*__f)();
588 // Notify the caller that we did try to set; if we do not throw an
589 // exception, the caller will be aware that it did set (e.g., see
592 _M_result.swap(__res); // nothrow
595 // Wait for completion of async function.
596 virtual void _M_complete_async() { }
598 // Return true if state corresponds to a deferred function.
599 virtual bool _M_is_deferred_future() const { return false; }
601 struct _Make_ready final : __at_thread_exit_elt
603 weak_ptr<_State_baseV2> _M_shared_state;
604 static void _S_run(void*);
609#ifdef _GLIBCXX_ASYNC_ABI_COMPAT
611 class _Async_state_common;
613 using _State_base = _State_baseV2;
614 class _Async_state_commonV2;
617 template<typename _BoundFn,
618 typename _Res = decltype(std::declval<_BoundFn&>()())>
619 class _Deferred_state;
621 template<typename _BoundFn,
622 typename _Res = decltype(std::declval<_BoundFn&>()())>
623 class _Async_state_impl;
625 template<typename _Signature>
626 class _Task_state_base;
628 template<typename _Fn, typename _Alloc, typename _Signature>
631 template<typename _Res_ptr, typename _Fn,
632 typename _Res = typename _Res_ptr::element_type::result_type>
635 template<typename _Res_ptr, typename _BoundFn>
636 static _Task_setter<_Res_ptr, _BoundFn>
637 _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
639 return { std::__addressof(__ptr), std::__addressof(__call) };
643 /// Partial specialization for reference types.
644 template<typename _Res>
645 struct __future_base::_Result<_Res&> : __future_base::_Result_base
647 typedef _Res& result_type;
649 _Result() noexcept : _M_value_ptr() { }
652 _M_set(_Res& __res) noexcept
653 { _M_value_ptr = std::addressof(__res); }
655 _Res& _M_get() noexcept { return *_M_value_ptr; }
660 void _M_destroy() { delete this; }
663 /// Explicit specialization for void.
665 struct __future_base::_Result<void> : __future_base::_Result_base
667 typedef void result_type;
670 void _M_destroy() { delete this; }
675#ifndef _GLIBCXX_ASYNC_ABI_COMPAT
677 /// @cond undocumented
678 // Allow _Setter objects to be stored locally in std::function
679 template<typename _Res, typename _Arg>
680 struct __is_location_invariant
681 <__future_base::_State_base::_Setter<_Res, _Arg>>
684 // Allow _Task_setter objects to be stored locally in std::function
685 template<typename _Res_ptr, typename _Fn, typename _Res>
686 struct __is_location_invariant
687 <__future_base::_Task_setter<_Res_ptr, _Fn, _Res>>
691 /// Common implementation for future and shared_future.
692 template<typename _Res>
693 class __basic_future : public __future_base
696 typedef shared_ptr<_State_base> __state_type;
697 typedef __future_base::_Result<_Res>& __result_type;
700 __state_type _M_state;
704 __basic_future(const __basic_future&) = delete;
705 __basic_future& operator=(const __basic_future&) = delete;
708 valid() const noexcept { return static_cast<bool>(_M_state); }
713 _State_base::_S_check(_M_state);
717 template<typename _Rep, typename _Period>
719 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
721 _State_base::_S_check(_M_state);
722 return _M_state->wait_for(__rel);
725 template<typename _Clock, typename _Duration>
727 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
729 _State_base::_S_check(_M_state);
730 return _M_state->wait_until(__abs);
734 /// Wait for the state to be ready and rethrow any stored exception
736 _M_get_result() const
738 _State_base::_S_check(_M_state);
739 _Result_base& __res = _M_state->wait();
740 if (!(__res._M_error == nullptr))
741 rethrow_exception(__res._M_error);
742 return static_cast<__result_type>(__res);
745 void _M_swap(__basic_future& __that) noexcept
747 _M_state.swap(__that._M_state);
750 // Construction of a future by promise::get_future()
752 __basic_future(const __state_type& __state) : _M_state(__state)
754 _State_base::_S_check(_M_state);
755 _M_state->_M_set_retrieved_flag();
758 // Copy construction from a shared_future
760 __basic_future(const shared_future<_Res>&) noexcept;
762 // Move construction from a shared_future
764 __basic_future(shared_future<_Res>&&) noexcept;
766 // Move construction from a future
768 __basic_future(future<_Res>&&) noexcept;
770 constexpr __basic_future() noexcept : _M_state() { }
774 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
775 ~_Reset() { _M_fut._M_state.reset(); }
776 __basic_future& _M_fut;
781 /// Primary template for future.
782 template<typename _Res>
783 class future : public __basic_future<_Res>
785 // _GLIBCXX_RESOLVE_LIB_DEFECTS
786 // 3458. Is shared_future intended to work with arrays or function types?
787 static_assert(!is_array<_Res>{}, "result type must not be an array");
788 static_assert(!is_function<_Res>{}, "result type must not be a function");
789 static_assert(is_destructible<_Res>{},
790 "result type must be destructible");
792 friend class promise<_Res>;
793 template<typename> friend class packaged_task;
794 template<typename _Fn, typename... _Args>
795 friend future<__async_result_of<_Fn, _Args...>>
796 async(launch, _Fn&&, _Args&&...);
798 typedef __basic_future<_Res> _Base_type;
799 typedef typename _Base_type::__state_type __state_type;
802 future(const __state_type& __state) : _Base_type(__state) { }
805 constexpr future() noexcept : _Base_type() { }
808 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
811 future(const future&) = delete;
812 future& operator=(const future&) = delete;
814 future& operator=(future&& __fut) noexcept
816 future(std::move(__fut))._M_swap(*this);
820 /// Retrieving the value
824 typename _Base_type::_Reset __reset(*this);
825 return std::move(this->_M_get_result()._M_value());
828 shared_future<_Res> share() noexcept;
831 /// Partial specialization for future<R&>
832 template<typename _Res>
833 class future<_Res&> : public __basic_future<_Res&>
835 friend class promise<_Res&>;
836 template<typename> friend class packaged_task;
837 template<typename _Fn, typename... _Args>
838 friend future<__async_result_of<_Fn, _Args...>>
839 async(launch, _Fn&&, _Args&&...);
841 typedef __basic_future<_Res&> _Base_type;
842 typedef typename _Base_type::__state_type __state_type;
845 future(const __state_type& __state) : _Base_type(__state) { }
848 constexpr future() noexcept : _Base_type() { }
851 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
854 future(const future&) = delete;
855 future& operator=(const future&) = delete;
857 future& operator=(future&& __fut) noexcept
859 future(std::move(__fut))._M_swap(*this);
863 /// Retrieving the value
867 typename _Base_type::_Reset __reset(*this);
868 return this->_M_get_result()._M_get();
871 shared_future<_Res&> share() noexcept;
874 /// Explicit specialization for future<void>
876 class future<void> : public __basic_future<void>
878 friend class promise<void>;
879 template<typename> friend class packaged_task;
880 template<typename _Fn, typename... _Args>
881 friend future<__async_result_of<_Fn, _Args...>>
882 async(launch, _Fn&&, _Args&&...);
884 typedef __basic_future<void> _Base_type;
885 typedef typename _Base_type::__state_type __state_type;
888 future(const __state_type& __state) : _Base_type(__state) { }
891 constexpr future() noexcept : _Base_type() { }
894 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
897 future(const future&) = delete;
898 future& operator=(const future&) = delete;
900 future& operator=(future&& __fut) noexcept
902 future(std::move(__fut))._M_swap(*this);
906 /// Retrieving the value
910 typename _Base_type::_Reset __reset(*this);
911 this->_M_get_result();
914 shared_future<void> share() noexcept;
918 /// Primary template for shared_future.
919 template<typename _Res>
920 class shared_future : public __basic_future<_Res>
922 // _GLIBCXX_RESOLVE_LIB_DEFECTS
923 // 3458. Is shared_future intended to work with arrays or function types?
924 static_assert(!is_array<_Res>{}, "result type must not be an array");
925 static_assert(!is_function<_Res>{}, "result type must not be a function");
926 static_assert(is_destructible<_Res>{},
927 "result type must be destructible");
929 typedef __basic_future<_Res> _Base_type;
932 constexpr shared_future() noexcept : _Base_type() { }
935 shared_future(const shared_future& __sf) noexcept : _Base_type(__sf) { }
937 /// Construct from a future rvalue
938 shared_future(future<_Res>&& __uf) noexcept
939 : _Base_type(std::move(__uf))
942 /// Construct from a shared_future rvalue
943 shared_future(shared_future&& __sf) noexcept
944 : _Base_type(std::move(__sf))
947 shared_future& operator=(const shared_future& __sf) noexcept
949 shared_future(__sf)._M_swap(*this);
953 shared_future& operator=(shared_future&& __sf) noexcept
955 shared_future(std::move(__sf))._M_swap(*this);
959 /// Retrieving the value
961 get() const { return this->_M_get_result()._M_value(); }
964 /// Partial specialization for shared_future<R&>
965 template<typename _Res>
966 class shared_future<_Res&> : public __basic_future<_Res&>
968 typedef __basic_future<_Res&> _Base_type;
971 constexpr shared_future() noexcept : _Base_type() { }
974 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
976 /// Construct from a future rvalue
977 shared_future(future<_Res&>&& __uf) noexcept
978 : _Base_type(std::move(__uf))
981 /// Construct from a shared_future rvalue
982 shared_future(shared_future&& __sf) noexcept
983 : _Base_type(std::move(__sf))
986 shared_future& operator=(const shared_future& __sf)
988 shared_future(__sf)._M_swap(*this);
992 shared_future& operator=(shared_future&& __sf) noexcept
994 shared_future(std::move(__sf))._M_swap(*this);
998 /// Retrieving the value
1000 get() const { return this->_M_get_result()._M_get(); }
1003 /// Explicit specialization for shared_future<void>
1005 class shared_future<void> : public __basic_future<void>
1007 typedef __basic_future<void> _Base_type;
1010 constexpr shared_future() noexcept : _Base_type() { }
1012 /// Copy constructor
1013 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
1015 /// Construct from a future rvalue
1016 shared_future(future<void>&& __uf) noexcept
1017 : _Base_type(std::move(__uf))
1020 /// Construct from a shared_future rvalue
1021 shared_future(shared_future&& __sf) noexcept
1022 : _Base_type(std::move(__sf))
1025 shared_future& operator=(const shared_future& __sf)
1027 shared_future(__sf)._M_swap(*this);
1031 shared_future& operator=(shared_future&& __sf) noexcept
1033 shared_future(std::move(__sf))._M_swap(*this);
1037 // Retrieving the value
1039 get() const { this->_M_get_result(); }
1042 // Now we can define the protected __basic_future constructors.
1043 template<typename _Res>
1044 inline __basic_future<_Res>::
1045 __basic_future(const shared_future<_Res>& __sf) noexcept
1046 : _M_state(__sf._M_state)
1049 template<typename _Res>
1050 inline __basic_future<_Res>::
1051 __basic_future(shared_future<_Res>&& __sf) noexcept
1052 : _M_state(std::move(__sf._M_state))
1055 template<typename _Res>
1056 inline __basic_future<_Res>::
1057 __basic_future(future<_Res>&& __uf) noexcept
1058 : _M_state(std::move(__uf._M_state))
1061 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1062 // 2556. Wide contract for future::share()
1063 template<typename _Res>
1064 inline shared_future<_Res>
1065 future<_Res>::share() noexcept
1066 { return shared_future<_Res>(std::move(*this)); }
1068 template<typename _Res>
1069 inline shared_future<_Res&>
1070 future<_Res&>::share() noexcept
1071 { return shared_future<_Res&>(std::move(*this)); }
1073 inline shared_future<void>
1074 future<void>::share() noexcept
1075 { return shared_future<void>(std::move(*this)); }
1077 /// Primary template for promise
1078 template<typename _Res>
1081 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1082 // 3466: Specify the requirements for promise/future/[...] consistently
1083 static_assert(!is_array<_Res>{}, "result type must not be an array");
1084 static_assert(!is_function<_Res>{}, "result type must not be a function");
1085 static_assert(is_destructible<_Res>{},
1086 "result type must be destructible");
1088 typedef __future_base::_State_base _State;
1089 typedef __future_base::_Result<_Res> _Res_type;
1090 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1091 template<typename, typename> friend struct _State::_Setter;
1094 shared_ptr<_State> _M_future;
1095 _Ptr_type _M_storage;
1099 : _M_future(std::make_shared<_State>()),
1100 _M_storage(new _Res_type())
1103 promise(promise&& __rhs) noexcept
1104 : _M_future(std::move(__rhs._M_future)),
1105 _M_storage(std::move(__rhs._M_storage))
1108 template<typename _Allocator>
1109 promise(allocator_arg_t, const _Allocator& __a)
1110 : _M_future(std::allocate_shared<_State>(__a)),
1111 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1114 template<typename _Allocator>
1115 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1116 : _M_future(std::move(__rhs._M_future)),
1117 _M_storage(std::move(__rhs._M_storage))
1120 promise(const promise&) = delete;
1124 if (static_cast<bool>(_M_future) && !_M_future.unique())
1125 _M_future->_M_break_promise(std::move(_M_storage));
1130 operator=(promise&& __rhs) noexcept
1132 promise(std::move(__rhs)).swap(*this);
1136 promise& operator=(const promise&) = delete;
1139 swap(promise& __rhs) noexcept
1141 _M_future.swap(__rhs._M_future);
1142 _M_storage.swap(__rhs._M_storage);
1145 // Retrieving the result
1148 { return future<_Res>(_M_future); }
1150 // Setting the result
1152 set_value(const _Res& __r)
1153 { _M_state()._M_set_result(_State::__setter(this, __r)); }
1156 set_value(_Res&& __r)
1157 { _M_state()._M_set_result(_State::__setter(this, std::move(__r))); }
1160 set_exception(exception_ptr __p)
1161 { _M_state()._M_set_result(_State::__setter(__p, this)); }
1164 set_value_at_thread_exit(const _Res& __r)
1166 _M_state()._M_set_delayed_result(_State::__setter(this, __r),
1171 set_value_at_thread_exit(_Res&& __r)
1173 _M_state()._M_set_delayed_result(
1174 _State::__setter(this, std::move(__r)), _M_future);
1178 set_exception_at_thread_exit(exception_ptr __p)
1180 _M_state()._M_set_delayed_result(_State::__setter(__p, this),
1188 __future_base::_State_base::_S_check(_M_future);
1193 template<typename _Res>
1195 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1198 template<typename _Res, typename _Alloc>
1199 struct uses_allocator<promise<_Res>, _Alloc>
1200 : public true_type { };
1203 /// Partial specialization for promise<R&>
1204 template<typename _Res>
1205 class promise<_Res&>
1207 typedef __future_base::_State_base _State;
1208 typedef __future_base::_Result<_Res&> _Res_type;
1209 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1210 template<typename, typename> friend struct _State::_Setter;
1213 shared_ptr<_State> _M_future;
1214 _Ptr_type _M_storage;
1218 : _M_future(std::make_shared<_State>()),
1219 _M_storage(new _Res_type())
1222 promise(promise&& __rhs) noexcept
1223 : _M_future(std::move(__rhs._M_future)),
1224 _M_storage(std::move(__rhs._M_storage))
1227 template<typename _Allocator>
1228 promise(allocator_arg_t, const _Allocator& __a)
1229 : _M_future(std::allocate_shared<_State>(__a)),
1230 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1233 template<typename _Allocator>
1234 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1235 : _M_future(std::move(__rhs._M_future)),
1236 _M_storage(std::move(__rhs._M_storage))
1239 promise(const promise&) = delete;
1243 if (static_cast<bool>(_M_future) && !_M_future.unique())
1244 _M_future->_M_break_promise(std::move(_M_storage));
1249 operator=(promise&& __rhs) noexcept
1251 promise(std::move(__rhs)).swap(*this);
1255 promise& operator=(const promise&) = delete;
1258 swap(promise& __rhs) noexcept
1260 _M_future.swap(__rhs._M_future);
1261 _M_storage.swap(__rhs._M_storage);
1264 // Retrieving the result
1267 { return future<_Res&>(_M_future); }
1269 // Setting the result
1271 set_value(_Res& __r)
1272 { _M_state()._M_set_result(_State::__setter(this, __r)); }
1275 set_exception(exception_ptr __p)
1276 { _M_state()._M_set_result(_State::__setter(__p, this)); }
1279 set_value_at_thread_exit(_Res& __r)
1281 _M_state()._M_set_delayed_result(_State::__setter(this, __r),
1286 set_exception_at_thread_exit(exception_ptr __p)
1288 _M_state()._M_set_delayed_result(_State::__setter(__p, this),
1296 __future_base::_State_base::_S_check(_M_future);
1301 /// Explicit specialization for promise<void>
1305 typedef __future_base::_State_base _State;
1306 typedef __future_base::_Result<void> _Res_type;
1307 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1308 template<typename, typename> friend struct _State::_Setter;
1311 shared_ptr<_State> _M_future;
1312 _Ptr_type _M_storage;
1316 : _M_future(std::make_shared<_State>()),
1317 _M_storage(new _Res_type())
1320 promise(promise&& __rhs) noexcept
1321 : _M_future(std::move(__rhs._M_future)),
1322 _M_storage(std::move(__rhs._M_storage))
1325 template<typename _Allocator>
1326 promise(allocator_arg_t, const _Allocator& __a)
1327 : _M_future(std::allocate_shared<_State>(__a)),
1328 _M_storage(__future_base::_S_allocate_result<void>(__a))
1331 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1332 // 2095. missing constructors needed for uses-allocator construction
1333 template<typename _Allocator>
1334 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1335 : _M_future(std::move(__rhs._M_future)),
1336 _M_storage(std::move(__rhs._M_storage))
1339 promise(const promise&) = delete;
1343 if (static_cast<bool>(_M_future) && !_M_future.unique())
1344 _M_future->_M_break_promise(std::move(_M_storage));
1349 operator=(promise&& __rhs) noexcept
1351 promise(std::move(__rhs)).swap(*this);
1355 promise& operator=(const promise&) = delete;
1358 swap(promise& __rhs) noexcept
1360 _M_future.swap(__rhs._M_future);
1361 _M_storage.swap(__rhs._M_storage);
1364 // Retrieving the result
1367 { return future<void>(_M_future); }
1369 // Setting the result
1372 { _M_state()._M_set_result(_State::__setter(this)); }
1375 set_exception(exception_ptr __p)
1376 { _M_state()._M_set_result(_State::__setter(__p, this)); }
1379 set_value_at_thread_exit()
1380 { _M_state()._M_set_delayed_result(_State::__setter(this), _M_future); }
1383 set_exception_at_thread_exit(exception_ptr __p)
1385 _M_state()._M_set_delayed_result(_State::__setter(__p, this),
1393 __future_base::_State_base::_S_check(_M_future);
1398 /// @cond undocumented
1399 template<typename _Ptr_type, typename _Fn, typename _Res>
1400 struct __future_base::_Task_setter
1402 // Invoke the function and provide the result to the caller.
1403 _Ptr_type operator()() const
1407 (*_M_result)->_M_set((*_M_fn)());
1409 __catch(const __cxxabiv1::__forced_unwind&)
1411 __throw_exception_again; // will cause broken_promise
1415 (*_M_result)->_M_error = current_exception();
1417 return std::move(*_M_result);
1419 _Ptr_type* _M_result;
1423 template<typename _Ptr_type, typename _Fn>
1424 struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1426 _Ptr_type operator()() const
1432 __catch(const __cxxabiv1::__forced_unwind&)
1434 __throw_exception_again; // will cause broken_promise
1438 (*_M_result)->_M_error = current_exception();
1440 return std::move(*_M_result);
1442 _Ptr_type* _M_result;
1446 // Holds storage for a packaged_task's result.
1447 template<typename _Res, typename... _Args>
1448 struct __future_base::_Task_state_base<_Res(_Args...)>
1449 : __future_base::_State_base
1451 typedef _Res _Res_type;
1453 template<typename _Alloc>
1454 _Task_state_base(const _Alloc& __a)
1455 : _M_result(_S_allocate_result<_Res>(__a))
1458 // Invoke the stored task and make the state ready.
1460 _M_run(_Args&&... __args) = 0;
1462 // Invoke the stored task and make the state ready at thread exit.
1464 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1466 virtual shared_ptr<_Task_state_base>
1469 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1470 _Ptr_type _M_result;
1473 // Holds a packaged_task's stored task.
1474 template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
1475 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1476 : __future_base::_Task_state_base<_Res(_Args...)>
1478 template<typename _Fn2>
1479 _Task_state(_Fn2&& __fn, const _Alloc& __a)
1480 : _Task_state_base<_Res(_Args...)>(__a),
1481 _M_impl(std::forward<_Fn2>(__fn), __a)
1486 _M_run(_Args&&... __args)
1488 auto __boundfn = [&] () -> _Res {
1489 return std::__invoke_r<_Res>(_M_impl._M_fn,
1490 std::forward<_Args>(__args)...);
1492 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1496 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1498 auto __boundfn = [&] () -> _Res {
1499 return std::__invoke_r<_Res>(_M_impl._M_fn,
1500 std::forward<_Args>(__args)...);
1502 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1506 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1509 struct _Impl : _Alloc
1511 template<typename _Fn2>
1512 _Impl(_Fn2&& __fn, const _Alloc& __a)
1513 : _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { }
1518 template<typename _Signature, typename _Fn,
1519 typename _Alloc = std::allocator<int>>
1520 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1521 __create_task_state(_Fn&& __fn, const _Alloc& __a = _Alloc())
1523 typedef typename decay<_Fn>::type _Fn2;
1524 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1525 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1528 template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
1529 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1530 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1532 return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
1533 static_cast<_Alloc&>(_M_impl));
1538 template<typename _Res, typename... _ArgTypes>
1539 class packaged_task<_Res(_ArgTypes...)>
1541 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1542 shared_ptr<_State_type> _M_state;
1544 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1545 // 3039. Unnecessary decay in thread and packaged_task
1546 template<typename _Fn, typename _Fn2 = __remove_cvref_t<_Fn>>
1548 = typename enable_if<!is_same<packaged_task, _Fn2>::value>::type;
1551 // Construction and destruction
1552 packaged_task() noexcept { }
1554 template<typename _Fn, typename = __not_same<_Fn>>
1556 packaged_task(_Fn&& __fn)
1558 __create_task_state<_Res(_ArgTypes...)>(std::forward<_Fn>(__fn)))
1561#if __cplusplus < 201703L
1562 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1563 // 2097. packaged_task constructors should be constrained
1564 // 2407. [this constructor should not be] explicit
1565 // 2921. packaged_task and type-erased allocators
1566 template<typename _Fn, typename _Alloc, typename = __not_same<_Fn>>
1567 packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
1568 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1569 std::forward<_Fn>(__fn), __a))
1572 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1573 // 2095. missing constructors needed for uses-allocator construction
1574 template<typename _Allocator>
1575 packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
1578 template<typename _Allocator>
1579 packaged_task(allocator_arg_t, const _Allocator&,
1580 const packaged_task&) = delete;
1582 template<typename _Allocator>
1583 packaged_task(allocator_arg_t, const _Allocator&,
1584 packaged_task&& __other) noexcept
1585 { this->swap(__other); }
1590 if (static_cast<bool>(_M_state) && !_M_state.unique())
1591 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1595 packaged_task(const packaged_task&) = delete;
1596 packaged_task& operator=(const packaged_task&) = delete;
1599 packaged_task(packaged_task&& __other) noexcept
1600 { this->swap(__other); }
1602 packaged_task& operator=(packaged_task&& __other) noexcept
1604 packaged_task(std::move(__other)).swap(*this);
1609 swap(packaged_task& __other) noexcept
1610 { _M_state.swap(__other._M_state); }
1613 valid() const noexcept
1614 { return static_cast<bool>(_M_state); }
1619 { return future<_Res>(_M_state); }
1623 operator()(_ArgTypes... __args)
1625 __future_base::_State_base::_S_check(_M_state);
1626 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1630 make_ready_at_thread_exit(_ArgTypes... __args)
1632 __future_base::_State_base::_S_check(_M_state);
1633 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1639 __future_base::_State_base::_S_check(_M_state);
1640 packaged_task __tmp;
1641 __tmp._M_state = _M_state;
1642 _M_state = _M_state->_M_reset();
1646 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1647 // 3117. Missing packaged_task deduction guides
1648#if __cpp_deduction_guides >= 201606
1649 template<typename _Res, typename... _ArgTypes>
1650 packaged_task(_Res(*)(_ArgTypes...)) -> packaged_task<_Res(_ArgTypes...)>;
1652 template<typename _Fun, typename _Signature = typename
1653 __function_guide_helper<decltype(&_Fun::operator())>::type>
1654 packaged_task(_Fun) -> packaged_task<_Signature>;
1658 template<typename _Res, typename... _ArgTypes>
1660 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1661 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1664#if __cplusplus < 201703L
1665 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1666 // 2976. Dangling uses_allocator specialization for packaged_task
1667 template<typename _Res, typename _Alloc>
1668 struct uses_allocator<packaged_task<_Res>, _Alloc>
1669 : public true_type { };
1672 /// @cond undocumented
1674 // Shared state created by std::async().
1675 // Holds a deferred function and storage for its result.
1676 template<typename _BoundFn, typename _Res>
1677 class __future_base::_Deferred_state final
1678 : public __future_base::_State_base
1681 template<typename... _Args>
1683 _Deferred_state(_Args&&... __args)
1684 : _M_result(new _Result<_Res>()),
1685 _M_fn(std::forward<_Args>(__args)...)
1689 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1690 _Ptr_type _M_result;
1693 // Run the deferred function.
1697 // Multiple threads can call a waiting function on the future and
1698 // reach this point at the same time. The call_once in _M_set_result
1699 // ensures only the first one run the deferred function, stores the
1700 // result in _M_result, swaps that with the base _M_result and makes
1701 // the state ready. Tell _M_set_result to ignore failure so all later
1702 // calls do nothing.
1703 _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1706 // Caller should check whether the state is ready first, because this
1707 // function will return true even after the deferred function has run.
1708 virtual bool _M_is_deferred_future() const { return true; }
1711 // Common functionality hoisted out of the _Async_state_impl template.
1712 class __future_base::_Async_state_commonV2
1713 : public __future_base::_State_base
1716 ~_Async_state_commonV2() = default;
1718 // Make waiting functions block until the thread completes, as if joined.
1720 // This function is used by wait() to satisfy the first requirement below
1721 // and by wait_for() / wait_until() to satisfy the second.
1725 // - a call to a waiting function on an asynchronous return object that
1726 // shares the shared state created by this async call shall block until
1727 // the associated thread has completed, as if joined, or else time out.
1729 // - the associated thread completion synchronizes with the return from
1730 // the first function that successfully detects the ready status of the
1731 // shared state or with the return from the last function that releases
1732 // the shared state, whichever happens first.
1733 virtual void _M_complete_async() { _M_join(); }
1735 void _M_join() { std::call_once(_M_once, &thread::join, &_M_thread); }
1741 // Shared state created by std::async().
1742 // Starts a new thread that runs a function and makes the shared state ready.
1743 template<typename _BoundFn, typename _Res>
1744 class __future_base::_Async_state_impl final
1745 : public __future_base::_Async_state_commonV2
1748 template<typename... _Args>
1750 _Async_state_impl(_Args&&... __args)
1751 : _M_result(new _Result<_Res>()),
1752 _M_fn(std::forward<_Args>(__args)...)
1754 _M_thread = std::thread{&_Async_state_impl::_M_run, this};
1757 // Must not destroy _M_result and _M_fn until the thread finishes.
1758 // Call join() directly rather than through _M_join() because no other
1759 // thread can be referring to this state if it is being destroyed.
1760 ~_Async_state_impl()
1762 if (_M_thread.joinable())
1772 _M_set_result(_S_task_setter(_M_result, _M_fn));
1774 __catch (const __cxxabiv1::__forced_unwind&)
1776 // make the shared state ready on thread cancellation
1777 if (static_cast<bool>(_M_result))
1778 this->_M_break_promise(std::move(_M_result));
1779 __throw_exception_again;
1783 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1784 _Ptr_type _M_result;
1790 template<typename _Fn, typename... _Args>
1791 _GLIBCXX_NODISCARD future<__async_result_of<_Fn, _Args...>>
1792 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1794 using _Wr = std::thread::_Call_wrapper<_Fn, _Args...>;
1795 using _As = __future_base::_Async_state_impl<_Wr>;
1796 using _Ds = __future_base::_Deferred_state<_Wr>;
1798 std::shared_ptr<__future_base::_State_base> __state;
1799 if ((__policy & launch::async) == launch::async)
1803 __state = std::make_shared<_As>(std::forward<_Fn>(__fn),
1804 std::forward<_Args>(__args)...);
1807 catch(const system_error& __e)
1809 if (__e.code() != errc::resource_unavailable_try_again
1810 || (__policy & launch::deferred) != launch::deferred)
1817 __state = std::make_shared<_Ds>(std::forward<_Fn>(__fn),
1818 std::forward<_Args>(__args)...);
1820 return future<__async_result_of<_Fn, _Args...>>(std::move(__state));
1823 /// async, potential overload
1824 template<typename _Fn, typename... _Args>
1825 _GLIBCXX_NODISCARD inline future<__async_result_of<_Fn, _Args...>>
1826 async(_Fn&& __fn, _Args&&... __args)
1828 return std::async(launch::async|launch::deferred,
1829 std::forward<_Fn>(__fn),
1830 std::forward<_Args>(__args)...);
1833#endif // _GLIBCXX_ASYNC_ABI_COMPAT
1834#endif // _GLIBCXX_HAS_GTHREADS
1836 /// @} group futures
1837_GLIBCXX_END_NAMESPACE_VERSION
1842#endif // _GLIBCXX_FUTURE