29 #ifndef _GLIBCXX_FUTURE
30 #define _GLIBCXX_FUTURE 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
46 #include <bits/shared_ptr.h>
49 #include <bits/uses_allocator.h>
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 future_already_retrieved = 1,
68 promise_already_satisfied,
87 inline error_condition
106 what()
const noexcept;
109 code()
const noexcept {
return _M_code; }
114 :
logic_error(
"std::future_error: " + __ec.message()), _M_code(__ec)
117 friend void __throw_future_error(
int);
123 template<
typename _Res>
126 template<
typename _Res>
129 template<
typename _Signature>
132 template<
typename _Res>
144 return static_cast<launch>(
145 static_cast<int>(__x) & static_cast<int>(__y));
150 return static_cast<launch>(
151 static_cast<int>(__x) | static_cast<int>(__y));
156 return static_cast<launch>(
157 static_cast<int>(__x) ^ static_cast<int>(__y));
161 {
return static_cast<launch>(~static_cast<
int>(__x)); }
164 {
return __x = __x & __y; }
167 {
return __x = __x | __y; }
170 {
return __x = __x ^ __y; }
182 template<
typename _Fn,
typename... _Args>
183 using __async_result_of =
typename __invoke_result<
184 typename decay<_Fn>::type,
typename decay<_Args>::type...>::type;
186 template<
typename _Fn,
typename... _Args>
187 future<__async_result_of<_Fn, _Args...>>
188 async(
launch __policy, _Fn&& __fn, _Args&&... __args);
190 template<
typename _Fn,
typename... _Args>
191 future<__async_result_of<_Fn, _Args...>>
192 async(_Fn&& __fn, _Args&&... __args);
194 #if defined(_GLIBCXX_HAS_GTHREADS)
202 exception_ptr _M_error;
208 virtual void _M_destroy() = 0;
212 void operator()(
_Result_base* __fr)
const { __fr->_M_destroy(); }
221 template<
typename _Res>
225 template<
typename _Res>
229 __gnu_cxx::__aligned_buffer<_Res> _M_storage;
233 typedef _Res result_type;
235 _Result() noexcept : _M_initialized() { }
245 _M_value() noexcept {
return *_M_storage._M_ptr(); }
248 _M_set(
const _Res& __res)
250 ::new (_M_storage._M_addr()) _Res(__res);
251 _M_initialized =
true;
257 ::new (_M_storage._M_addr()) _Res(
std::move(__res));
258 _M_initialized =
true;
262 void _M_destroy() {
delete this; }
266 template<
typename _Res,
typename _Alloc>
269 using __allocator_type = __alloc_rebind<_Alloc, _Result_alloc>;
278 __allocator_type __a(*
this);
279 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
285 template<
typename _Res,
typename _Allocator>
287 _S_allocate_result(
const _Allocator& __a)
290 typename __result_type::__allocator_type __a2(__a);
291 auto __guard = std::__allocate_guarded(__a2);
292 __result_type* __p = ::new((
void*)__guard.get()) __result_type{__a};
294 return _Ptr<__result_type>(__p);
298 template<
typename _Res,
typename _Tp>
299 static _Ptr<_Result<_Res>>
302 return _Ptr<_Result<_Res>>(
new _Result<_Res>);
310 typedef _Ptr<_Result_base> _Ptr_type;
312 enum _Status :
unsigned {
318 __atomic_futex_unsigned<> _M_status;
319 atomic_flag _M_retrieved = ATOMIC_FLAG_INIT;
323 _State_baseV2() noexcept : _M_result(), _M_status(_Status::__not_ready)
325 _State_baseV2(
const _State_baseV2&) =
delete;
326 _State_baseV2& operator=(
const _State_baseV2&) =
delete;
327 virtual ~_State_baseV2() =
default;
336 _M_status._M_load_when_equal(_Status::__ready, memory_order_acquire);
340 template<
typename _Rep,
typename _Period>
342 wait_for(
const chrono::duration<_Rep, _Period>& __rel)
346 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
347 return future_status::ready;
349 if (_M_is_deferred_future())
350 return future_status::deferred;
353 if (__rel > __rel.zero()
354 && _M_status._M_load_when_equal_for(_Status::__ready,
355 memory_order_acquire,
369 return future_status::ready;
371 return future_status::timeout;
374 template<
typename _Clock,
typename _Duration>
376 wait_until(
const chrono::time_point<_Clock, _Duration>& __abs)
378 #if __cplusplus > 201703L
379 static_assert(chrono::is_clock_v<_Clock>);
383 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
384 return future_status::ready;
386 if (_M_is_deferred_future())
387 return future_status::deferred;
389 if (_M_status._M_load_when_equal_until(_Status::__ready,
390 memory_order_acquire,
398 return future_status::ready;
400 return future_status::timeout;
406 _M_set_result(
function<_Ptr_type()> __res,
bool __ignore_failure =
false)
408 bool __did_set =
false;
411 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
415 _M_status._M_store_notify_all(_Status::__ready,
416 memory_order_release);
417 else if (!__ignore_failure)
418 __throw_future_error(
int(future_errc::promise_already_satisfied));
425 _M_set_delayed_result(
function<_Ptr_type()> __res,
426 weak_ptr<_State_baseV2> __self)
428 bool __did_set =
false;
429 unique_ptr<_Make_ready> __mr{
new _Make_ready};
432 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
435 __throw_future_error(
int(future_errc::promise_already_satisfied));
436 __mr->_M_shared_state =
std::move(__self);
443 _M_break_promise(_Ptr_type __res)
445 if (static_cast<bool>(__res))
453 _M_result.swap(__res);
455 _M_status._M_store_notify_all(_Status::__ready,
456 memory_order_release);
462 _M_set_retrieved_flag()
464 if (_M_retrieved.test_and_set())
465 __throw_future_error(
int(future_errc::future_already_retrieved));
468 template<
typename _Res,
typename _Arg>
472 template<
typename _Res,
typename _Arg>
473 struct _Setter<_Res, _Arg&>
477 static_assert(is_same<_Res, _Arg&>::value
478 || is_same<const _Res, _Arg>::value,
479 "Invalid specialisation");
482 typename promise<_Res>::_Ptr_type operator()()
const
484 _M_promise->_M_storage->_M_set(*_M_arg);
485 return std::move(_M_promise->_M_storage);
487 promise<_Res>* _M_promise;
492 template<
typename _Res>
493 struct _Setter<_Res, _Res&&>
496 typename promise<_Res>::_Ptr_type operator()()
const
498 _M_promise->_M_storage->_M_set(
std::move(*_M_arg));
499 return std::move(_M_promise->_M_storage);
501 promise<_Res>* _M_promise;
506 template<
typename _Res>
507 struct _Setter<_Res, void>
509 static_assert(is_void<_Res>::value,
"Only used for promise<void>");
511 typename promise<_Res>::_Ptr_type operator()()
const
512 {
return std::move(_M_promise->_M_storage); }
514 promise<_Res>* _M_promise;
517 struct __exception_ptr_tag { };
520 template<
typename _Res>
521 struct _Setter<_Res, __exception_ptr_tag>
524 typename promise<_Res>::_Ptr_type operator()()
const
526 _M_promise->_M_storage->_M_error = *_M_ex;
527 return std::move(_M_promise->_M_storage);
530 promise<_Res>* _M_promise;
531 exception_ptr* _M_ex;
534 template<
typename _Res,
typename _Arg>
535 __attribute__((__always_inline__))
536 static _Setter<_Res, _Arg&&>
537 __setter(promise<_Res>* __prom, _Arg&& __arg) noexcept
542 template<
typename _Res>
543 __attribute__((__always_inline__))
544 static _Setter<_Res, __exception_ptr_tag>
545 __setter(exception_ptr& __ex, promise<_Res>* __prom) noexcept
547 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
550 template<
typename _Res>
551 __attribute__((__always_inline__))
552 static _Setter<_Res,
void>
553 __setter(promise<_Res>* __prom) noexcept
555 return _Setter<_Res, void>{ __prom };
558 template<
typename _Tp>
560 _S_check(
const shared_ptr<_Tp>& __p)
562 if (!static_cast<bool>(__p))
563 __throw_future_error((
int)future_errc::no_state);
569 _M_do_set(
function<_Ptr_type()>* __f,
bool* __did_set)
571 _Ptr_type __res = (*__f)();
576 _M_result.swap(__res);
580 virtual void _M_complete_async() { }
583 virtual bool _M_is_deferred_future()
const {
return false; }
585 struct _Make_ready final : __at_thread_exit_elt
587 weak_ptr<_State_baseV2> _M_shared_state;
588 static void _S_run(
void*);
593 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
595 class _Async_state_common;
597 using _State_base = _State_baseV2;
598 class _Async_state_commonV2;
601 template<
typename _BoundFn,
602 typename _Res = decltype(std::declval<_BoundFn&>()())>
603 class _Deferred_state;
605 template<
typename _BoundFn,
606 typename _Res = decltype(std::declval<_BoundFn&>()())>
607 class _Async_state_impl;
609 template<
typename _Signature>
610 class _Task_state_base;
612 template<
typename _Fn,
typename _Alloc,
typename _Signature>
615 template<
typename _Res_ptr,
typename _Fn,
616 typename _Res =
typename _Res_ptr::element_type::result_type>
619 template<
typename _Res_ptr,
typename _BoundFn>
620 static _Task_setter<_Res_ptr, _BoundFn>
621 _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
628 template<
typename _Res>
631 typedef _Res& result_type;
633 _Result() noexcept : _M_value_ptr() { }
636 _M_set(_Res& __res) noexcept
639 _Res& _M_get() noexcept {
return *_M_value_ptr; }
644 void _M_destroy() {
delete this; }
651 typedef void result_type;
654 void _M_destroy() {
delete this; }
657 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
660 template<
typename _Res,
typename _Arg>
666 template<
typename _Res_ptr,
typename _Fn,
typename _Res>
672 template<
typename _Res>
688 valid()
const noexcept {
return static_cast<bool>(_M_state); }
693 _State_base::_S_check(_M_state);
697 template<
typename _Rep,
typename _Period>
701 _State_base::_S_check(_M_state);
702 return _M_state->wait_for(__rel);
705 template<
typename _Clock,
typename _Duration>
709 _State_base::_S_check(_M_state);
710 return _M_state->wait_until(__abs);
718 _State_base::_S_check(_M_state);
720 if (!(__res._M_error ==
nullptr))
727 _M_state.swap(__that._M_state);
732 __basic_future(
const __state_type& __state) : _M_state(__state)
734 _State_base::_S_check(_M_state);
735 _M_state->_M_set_retrieved_flag();
740 __basic_future(
const shared_future<_Res>&) noexcept;
744 __basic_future(shared_future<_Res>&&) noexcept;
748 __basic_future(future<_Res>&&) noexcept;
750 constexpr __basic_future() noexcept : _M_state() { }
754 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
755 ~_Reset() { _M_fut._M_state.reset(); }
756 __basic_future& _M_fut;
762 template<
typename _Res>
763 class future :
public __basic_future<_Res>
767 static_assert(!is_array<_Res>{},
"result type must not be an array");
768 static_assert(!is_function<_Res>{},
"result type must not be a function");
769 static_assert(is_destructible<_Res>{},
770 "result type must be destructible");
772 friend class promise<_Res>;
773 template<
typename>
friend class packaged_task;
774 template<
typename _Fn,
typename... _Args>
775 friend future<__async_result_of<_Fn, _Args...>>
778 typedef __basic_future<_Res> _Base_type;
779 typedef typename _Base_type::__state_type __state_type;
782 future(
const __state_type& __state) : _Base_type(__state) { }
785 constexpr future() noexcept : _Base_type() { }
804 typename _Base_type::_Reset __reset(*
this);
812 template<typename _Res>
816 template<
typename>
friend class packaged_task;
817 template<
typename _Fn,
typename... _Args>
818 friend future<__async_result_of<_Fn, _Args...>>
825 future(
const __state_type& __state) : _Base_type(__state) { }
828 constexpr
future() noexcept : _Base_type() { }
847 typename _Base_type::_Reset __reset(*
this);
859 template<
typename>
friend class packaged_task;
860 template<
typename _Fn,
typename... _Args>
861 friend future<__async_result_of<_Fn, _Args...>>
868 future(
const __state_type& __state) : _Base_type(__state) { }
871 constexpr
future() noexcept : _Base_type() { }
890 typename _Base_type::_Reset __reset(*
this);
899 template<typename _Res>
904 static_assert(!
is_array<_Res>{},
"result type must not be an array");
905 static_assert(!is_function<_Res>{},
"result type must not be a function");
906 static_assert(is_destructible<_Res>{},
907 "result type must be destructible");
909 typedef __basic_future<_Res> _Base_type;
912 constexpr shared_future() noexcept : _Base_type() { }
919 : _Base_type(std::
move(__uf))
924 : _Base_type(std::
move(__sf))
933 shared_future& operator=(shared_future&& __sf) noexcept
935 shared_future(
std::move(__sf))._M_swap(*
this);
945 template<
typename _Res>
958 : _Base_type(std::
move(__uf))
963 : _Base_type(std::
move(__sf))
972 shared_future& operator=(shared_future&& __sf) noexcept
974 shared_future(
std::move(__sf))._M_swap(*
this);
997 : _Base_type(std::
move(__uf))
1002 : _Base_type(std::
move(__sf))
1011 shared_future& operator=(shared_future&& __sf) noexcept
1013 shared_future(
std::move(__sf))._M_swap(*
this);
1023 template<
typename _Res>
1024 inline __basic_future<_Res>::
1025 __basic_future(
const shared_future<_Res>& __sf) noexcept
1026 : _M_state(__sf._M_state)
1029 template<
typename _Res>
1030 inline __basic_future<_Res>::
1031 __basic_future(shared_future<_Res>&& __sf) noexcept
1032 : _M_state(std::
move(__sf._M_state))
1035 template<
typename _Res>
1036 inline __basic_future<_Res>::
1037 __basic_future(future<_Res>&& __uf) noexcept
1038 : _M_state(std::
move(__uf._M_state))
1043 template<
typename _Res>
1044 inline shared_future<_Res>
1045 future<_Res>::share() noexcept
1046 {
return shared_future<_Res>(
std::move(*
this)); }
1048 template<
typename _Res>
1049 inline shared_future<_Res&>
1050 future<_Res&>::share() noexcept
1051 {
return shared_future<_Res&>(
std::move(*
this)); }
1053 inline shared_future<void>
1054 future<void>::share() noexcept
1055 {
return shared_future<void>(
std::move(*
this)); }
1058 template<
typename _Res>
1063 static_assert(!is_array<_Res>{},
"result type must not be an array");
1064 static_assert(!is_function<_Res>{},
"result type must not be a function");
1065 static_assert(is_destructible<_Res>{},
1066 "result type must be destructible");
1068 typedef __future_base::_State_base _State;
1069 typedef __future_base::_Result<_Res> _Res_type;
1070 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1071 template<
typename,
typename>
friend struct _State::_Setter;
1074 shared_ptr<_State> _M_future;
1075 _Ptr_type _M_storage;
1079 : _M_future(std::make_shared<_State>()),
1080 _M_storage(new _Res_type())
1083 promise(promise&& __rhs) noexcept
1084 : _M_future(std::
move(__rhs._M_future)),
1085 _M_storage(std::
move(__rhs._M_storage))
1088 template<
typename _Allocator>
1089 promise(allocator_arg_t,
const _Allocator& __a)
1090 : _M_future(std::allocate_shared<_State>(__a)),
1091 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1094 template<
typename _Allocator>
1095 promise(allocator_arg_t,
const _Allocator&, promise&& __rhs)
1096 : _M_future(std::
move(__rhs._M_future)),
1097 _M_storage(std::
move(__rhs._M_storage))
1100 promise(
const promise&) =
delete;
1104 if (static_cast<bool>(_M_future) && !_M_future.unique())
1105 _M_future->_M_break_promise(
std::move(_M_storage));
1110 operator=(promise&& __rhs) noexcept
1116 promise& operator=(
const promise&) =
delete;
1119 swap(promise& __rhs) noexcept
1121 _M_future.swap(__rhs._M_future);
1122 _M_storage.swap(__rhs._M_storage);
1128 {
return future<_Res>(_M_future); }
1132 set_value(
const _Res& __r)
1133 { _M_state()._M_set_result(_State::__setter(
this, __r)); }
1136 set_value(_Res&& __r)
1137 { _M_state()._M_set_result(_State::__setter(
this,
std::move(__r))); }
1140 set_exception(exception_ptr __p)
1141 { _M_state()._M_set_result(_State::__setter(__p,
this)); }
1144 set_value_at_thread_exit(
const _Res& __r)
1146 _M_state()._M_set_delayed_result(_State::__setter(
this, __r),
1151 set_value_at_thread_exit(_Res&& __r)
1153 _M_state()._M_set_delayed_result(
1154 _State::__setter(
this,
std::move(__r)), _M_future);
1158 set_exception_at_thread_exit(exception_ptr __p)
1160 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1168 __future_base::_State_base::_S_check(_M_future);
1173 template<
typename _Res>
1175 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1178 template<
typename _Res,
typename _Alloc>
1179 struct uses_allocator<promise<_Res>, _Alloc>
1184 template<
typename _Res>
1187 typedef __future_base::_State_base _State;
1190 template<
typename,
typename>
friend struct _State::_Setter;
1194 _Ptr_type _M_storage;
1198 : _M_future(std::make_shared<_State>()),
1199 _M_storage(
new _Res_type())
1203 : _M_future(
std::move(__rhs._M_future)),
1207 template<
typename _Allocator>
1208 promise(allocator_arg_t,
const _Allocator& __a)
1209 : _M_future(std::allocate_shared<_State>(__a)),
1210 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1213 template<
typename _Allocator>
1215 : _M_future(
std::move(__rhs._M_future)),
1223 if (static_cast<bool>(_M_future) && !_M_future.unique())
1224 _M_future->_M_break_promise(
std::move(_M_storage));
1229 operator=(
promise&& __rhs) noexcept
1240 _M_future.swap(__rhs._M_future);
1241 _M_storage.
swap(__rhs._M_storage);
1251 set_value(_Res& __r)
1252 { _M_state()._M_set_result(_State::__setter(
this, __r)); }
1255 set_exception(exception_ptr __p)
1256 { _M_state()._M_set_result(_State::__setter(__p,
this)); }
1259 set_value_at_thread_exit(_Res& __r)
1261 _M_state()._M_set_delayed_result(_State::__setter(
this, __r),
1266 set_exception_at_thread_exit(exception_ptr __p)
1268 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1276 __future_base::_State_base::_S_check(_M_future);
1285 typedef __future_base::_State_base _State;
1288 template<
typename,
typename>
friend struct _State::_Setter;
1296 : _M_future(std::make_shared<_State>()),
1301 : _M_future(
std::move(__rhs._M_future)),
1305 template<
typename _Allocator>
1306 promise(allocator_arg_t,
const _Allocator& __a)
1307 : _M_future(std::allocate_shared<_State>(__a)),
1308 _M_storage(__future_base::_S_allocate_result<void>(__a))
1313 template<
typename _Allocator>
1315 : _M_future(
std::move(__rhs._M_future)),
1323 if (static_cast<bool>(_M_future) && !_M_future.unique())
1324 _M_future->_M_break_promise(
std::move(_M_storage));
1329 operator=(
promise&& __rhs) noexcept
1340 _M_future.swap(__rhs._M_future);
1341 _M_storage.swap(__rhs._M_storage);
1352 { _M_state()._M_set_result(_State::__setter(
this)); }
1355 set_exception(exception_ptr __p)
1356 { _M_state()._M_set_result(_State::__setter(__p,
this)); }
1359 set_value_at_thread_exit()
1360 { _M_state()._M_set_delayed_result(_State::__setter(
this), _M_future); }
1363 set_exception_at_thread_exit(exception_ptr __p)
1365 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1373 __future_base::_State_base::_S_check(_M_future);
1378 template<
typename _Ptr_type,
typename _Fn,
typename _Res>
1379 struct __future_base::_Task_setter
1382 _Ptr_type operator()()
const
1386 (*_M_result)->_M_set((*_M_fn)());
1390 __throw_exception_again;
1398 _Ptr_type* _M_result;
1402 template<
typename _Ptr_type,
typename _Fn>
1403 struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1405 _Ptr_type operator()()
const
1413 __throw_exception_again;
1421 _Ptr_type* _M_result;
1426 template<
typename _Res,
typename... _Args>
1427 struct __future_base::_Task_state_base<_Res(_Args...)>
1428 : __future_base::_State_base
1430 typedef _Res _Res_type;
1432 template<
typename _Alloc>
1433 _Task_state_base(
const _Alloc& __a)
1434 : _M_result(_S_allocate_result<_Res>(__a))
1439 _M_run(_Args&&... __args) = 0;
1443 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1445 virtual shared_ptr<_Task_state_base>
1448 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1449 _Ptr_type _M_result;
1453 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1454 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1455 : __future_base::_Task_state_base<_Res(_Args...)>
1457 template<
typename _Fn2>
1458 _Task_state(_Fn2&& __fn,
const _Alloc& __a)
1459 : _Task_state_base<_Res(_Args...)>(__a),
1460 _M_impl(std::
forward<_Fn2>(__fn), __a)
1465 _M_run(_Args&&... __args)
1467 auto __boundfn = [&] () -> _Res {
1468 return std::__invoke_r<_Res>(_M_impl._M_fn,
1469 std::forward<_Args>(__args)...);
1471 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1475 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1477 auto __boundfn = [&] () -> _Res {
1478 return std::__invoke_r<_Res>(_M_impl._M_fn,
1479 std::forward<_Args>(__args)...);
1481 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1485 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1488 struct _Impl : _Alloc
1490 template<
typename _Fn2>
1491 _Impl(_Fn2&& __fn,
const _Alloc& __a)
1492 : _Alloc(__a), _M_fn(std::
forward<_Fn2>(__fn)) { }
1497 template<
typename _Signature,
typename _Fn,
1499 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1500 __create_task_state(_Fn&& __fn,
const _Alloc& __a = _Alloc())
1502 typedef typename decay<_Fn>::type _Fn2;
1503 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1504 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1507 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1508 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1509 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1511 return __create_task_state<_Res(_Args...)>(
std::move(_M_impl._M_fn),
1512 static_cast<_Alloc&
>(_M_impl));
1516 template<
typename _Res,
typename... _ArgTypes>
1517 class packaged_task<_Res(_ArgTypes...)>
1519 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1524 template<
typename _Fn,
typename _Fn2 = __remove_cvref_t<_Fn>>
1530 packaged_task() noexcept { }
1532 template<
typename _Fn,
typename = __not_same<_Fn>>
1534 packaged_task(_Fn&& __fn)
1536 __create_task_state<_Res(_ArgTypes...)>(std::forward<_Fn>(__fn)))
1539 #if __cplusplus < 201703L
1544 template<
typename _Fn,
typename _Alloc,
typename = __not_same<_Fn>>
1545 packaged_task(allocator_arg_t,
const _Alloc& __a, _Fn&& __fn)
1546 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1547 std::forward<_Fn>(__fn), __a))
1552 template<
typename _Allocator>
1553 packaged_task(allocator_arg_t,
const _Allocator& __a) noexcept
1556 template<
typename _Allocator>
1557 packaged_task(allocator_arg_t,
const _Allocator&,
1558 const packaged_task&) =
delete;
1560 template<
typename _Allocator>
1561 packaged_task(allocator_arg_t,
const _Allocator&,
1562 packaged_task&& __other) noexcept
1563 { this->swap(__other); }
1568 if (static_cast<bool>(_M_state) && !_M_state.unique())
1569 _M_state->_M_break_promise(
std::move(_M_state->_M_result));
1573 packaged_task(
const packaged_task&) =
delete;
1574 packaged_task& operator=(
const packaged_task&) =
delete;
1577 packaged_task(packaged_task&& __other) noexcept
1578 { this->swap(__other); }
1580 packaged_task& operator=(packaged_task&& __other) noexcept
1582 packaged_task(
std::move(__other)).swap(*
this);
1587 swap(packaged_task& __other) noexcept
1588 { _M_state.swap(__other._M_state); }
1591 valid()
const noexcept
1592 {
return static_cast<bool>(_M_state); }
1601 operator()(_ArgTypes... __args)
1603 __future_base::_State_base::_S_check(_M_state);
1604 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1608 make_ready_at_thread_exit(_ArgTypes... __args)
1610 __future_base::_State_base::_S_check(_M_state);
1611 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1617 __future_base::_State_base::_S_check(_M_state);
1618 packaged_task __tmp;
1619 __tmp._M_state = _M_state;
1620 _M_state = _M_state->_M_reset();
1625 template<
typename _Res,
typename... _ArgTypes>
1627 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1628 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1631 #if __cplusplus < 201703L
1634 template<
typename _Res,
typename _Alloc>
1635 struct uses_allocator<packaged_task<_Res>, _Alloc>
1641 template<
typename _BoundFn,
typename _Res>
1642 class __future_base::_Deferred_state final
1643 :
public __future_base::_State_base
1646 template<
typename... _Args>
1648 _Deferred_state(_Args&&... __args)
1649 : _M_result(new _Result<_Res>()),
1650 _M_fn{{std::forward<_Args>(__args)...}}
1654 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1655 _Ptr_type _M_result;
1668 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1673 virtual bool _M_is_deferred_future()
const {
return true; }
1677 class __future_base::_Async_state_commonV2
1678 :
public __future_base::_State_base
1681 ~_Async_state_commonV2() =
default;
1698 virtual void _M_complete_async() { _M_join(); }
1700 void _M_join() {
std::call_once(_M_once, &thread::join, &_M_thread); }
1708 template<
typename _BoundFn,
typename _Res>
1709 class __future_base::_Async_state_impl final
1710 :
public __future_base::_Async_state_commonV2
1713 template<
typename... _Args>
1715 _Async_state_impl(_Args&&... __args)
1716 : _M_result(new _Result<_Res>()),
1717 _M_fn{{std::forward<_Args>(__args)...}}
1719 _M_thread =
std::thread{&_Async_state_impl::_M_run,
this};
1725 ~_Async_state_impl()
1727 if (_M_thread.joinable())
1737 _M_set_result(_S_task_setter(_M_result, _M_fn));
1742 if (static_cast<bool>(_M_result))
1743 this->_M_break_promise(
std::move(_M_result));
1744 __throw_exception_again;
1748 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1749 _Ptr_type _M_result;
1755 template<
typename _Fn,
typename... _Args>
1756 _GLIBCXX_NODISCARD future<__async_result_of<_Fn, _Args...>>
1759 using _Wr = std::thread::_Call_wrapper<_Fn, _Args...>;
1760 using _As = __future_base::_Async_state_impl<_Wr>;
1761 using _Ds = __future_base::_Deferred_state<_Wr>;
1764 if ((__policy & launch::async) == launch::async)
1768 __state = std::make_shared<_As>(std::forward<_Fn>(__fn),
1769 std::forward<_Args>(__args)...);
1771 #if __cpp_exceptions
1774 if (__e.code() != errc::resource_unavailable_try_again
1775 || (__policy & launch::deferred) != launch::deferred)
1782 __state = std::make_shared<_Ds>(std::forward<_Fn>(__fn),
1783 std::forward<_Args>(__args)...);
1789 template<
typename _Fn,
typename... _Args>
1790 _GLIBCXX_NODISCARD
inline future<__async_result_of<_Fn, _Args...>>
1793 return std::async(launch::async|launch::deferred,
1794 std::forward<_Fn>(__fn),
1795 std::forward<_Args>(__args)...);
1798 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
1799 #endif // _GLIBCXX_HAS_GTHREADS
1802 _GLIBCXX_END_NAMESPACE_VERSION
1807 #endif // _GLIBCXX_FUTURE
Base class and enclosing scope.
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
Define a member typedef type only if a boolean constant is true.
future_errc
Error code for futures.
future(future &&__uf) noexcept
Move constructor.
shared_future(future< _Res > &&__uf) noexcept
Construct from a future rvalue.
exception_ptr current_exception() noexcept
20.7.1.2 unique_ptr for single objects.
An exception type that includes an error_code value.
__result_type _M_get_result() const
Wait for the state to be ready and rethrow any stored exception.
const error_category & future_category() noexcept
Points to a statically-allocated object derived from error_category.
launch
Launch code for futures.
Primary template for future.
Thrown as part of forced unwinding.A magic placeholder class that can be caught by reference to recog...
Explicit specialization for void.
chrono::duration represents a distance between two points in time
exception_ptr make_exception_ptr(_Ex) noexcept
Obtain an exception_ptr pointing to a copy of the supplied object.
Exception type thrown by futures.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
shared_future(const shared_future &__sf)
Copy constructor.
Common implementation for future and shared_future.
A result object that uses an allocator.
logic_error(const string &__arg) _GLIBCXX_TXN_SAFE
future(future &&__uf) noexcept
Move constructor.
Partial specialization for reference types.
Explicit specialization for shared_future<void>
Primary template for shared_future.
future(future &&__uf) noexcept
Move constructor.
shared_future(future< void > &&__uf) noexcept
Construct from a future rvalue.
chrono::time_point represents a point in time as measured by a clock
future< __async_result_of< _Fn, _Args...> > async(launch __policy, _Fn &&__fn, _Args &&...__args)
async
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Explicit specialization for future<void>
bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Primary template for promise.
error_condition make_error_condition(future_errc __errc) noexcept
Overload for make_error_condition.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Partial specialization for shared_future<R&>
future_status
Status code for futures.
bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
shared_future(const shared_future &__sf)
Copy constructor.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
future< __async_result_of< _Fn, _Args...> > async(_Fn &&__fn, _Args &&...__args)
async, potential overload
A result object that has storage for an object of type _Res.
Partial specialization for future<R&>
void rethrow_exception(exception_ptr)
Throw the object pointed to by the exception_ptr.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
One of two subclasses of exception.
shared_future(const shared_future &__sf) noexcept
Copy constructor.
virtual const char * what() const noexcept
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
Invoke a callable and synchronize with other calls using the same flag.
The standard allocator, as per C++03 [20.4.1].
shared_future(future< _Res & > &&__uf) noexcept
Construct from a future rvalue.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.