29 #ifndef _GLIBCXX_FUTURE
30 #define _GLIBCXX_FUTURE 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
47 #include <bits/shared_ptr.h>
50 #include <bits/uses_allocator.h>
53 namespace std _GLIBCXX_VISIBILITY(default)
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
74 future_already_retrieved = 1,
75 promise_already_satisfied,
85 [[__nodiscard__, __gnu__::__const__]]
97 inline error_condition
120 code() const noexcept {
return _M_code; }
125 :
logic_error(
"std::future_error: " + __ec.message()), _M_code(__ec)
128 friend void __throw_future_error(
int);
134 template<
typename _Res>
137 template<
typename _Res>
140 template<
typename _Signature>
143 template<
typename _Res>
155 return static_cast<launch>(
156 static_cast<int>(__x) &
static_cast<int>(__y));
161 return static_cast<launch>(
162 static_cast<int>(__x) |
static_cast<int>(__y));
167 return static_cast<launch>(
168 static_cast<int>(__x) ^
static_cast<int>(__y));
172 {
return static_cast<launch>(~static_cast<int>(__x)); }
175 {
return __x = __x & __y; }
178 {
return __x = __x | __y; }
181 {
return __x = __x ^ __y; }
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)
217 exception_ptr _M_error;
219 _Result_base(
const _Result_base&) =
delete;
220 _Result_base& operator=(
const _Result_base&) =
delete;
223 virtual void _M_destroy() = 0;
227 void operator()(_Result_base* __fr)
const { __fr->_M_destroy(); }
232 virtual ~_Result_base();
236 template<
typename _Res>
237 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
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() { }
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; }
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();
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);
313 template<
typename _Res,
typename _Tp>
314 static _Ptr<_Result<_Res>>
317 return _Ptr<_Result<_Res>>(
new _Result<_Res>);
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;
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)
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;
368 if (__rel > __rel.zero()
369 && _M_status._M_load_when_equal_for(_Status::__ready,
370 memory_order_acquire,
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>);
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,
413 return future_status::ready;
415 return future_status::timeout;
421 _M_set_result(
function<_Ptr_type()> __res,
bool __ignore_failure =
false)
423 bool __did_set =
false;
426 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
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));
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};
447 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
450 __throw_future_error(
int(future_errc::promise_already_satisfied));
451 __mr->_M_shared_state =
std::move(__self);
458 _M_break_promise(_Ptr_type __res)
460 if (
static_cast<bool>(__res))
468 _M_result.swap(__res);
470 _M_status._M_store_notify_all(_Status::__ready,
471 memory_order_release);
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&>
492 static_assert(is_same<_Res, _Arg&>::value
493 || is_same<const _Res, _Arg>::value,
494 "Invalid specialisation");
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&&>
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>
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
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);
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);
585 _M_do_set(
function<_Ptr_type()>* __f,
bool* __did_set)
587 _Ptr_type __res = (*__f)();
592 _M_result.swap(__res);
596 virtual void _M_complete_async() { }
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)
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
655 _Res& _M_get() noexcept {
return *_M_value_ptr; }
660 void _M_destroy() {
delete this; }
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
679 template<
typename _Res,
typename _Arg>
680 struct __is_location_invariant
681 <__future_base::_State_base::_Setter<_Res, _Arg>>
685 template<
typename _Res_ptr,
typename _Fn,
typename _Res>
686 struct __is_location_invariant
687 <__future_base::_Task_setter<_Res_ptr, _Fn, _Res>>
692 template<
typename _Res>
697 typedef __future_base::_Result<_Res>& __result_type;
708 valid()
const noexcept {
return static_cast<bool>(_M_state); }
713 _State_base::_S_check(_M_state);
717 template<
typename _Rep,
typename _Period>
721 _State_base::_S_check(_M_state);
722 return _M_state->wait_for(__rel);
725 template<
typename _Clock,
typename _Duration>
729 _State_base::_S_check(_M_state);
730 return _M_state->wait_until(__abs);
738 _State_base::_S_check(_M_state);
739 _Result_base& __res = _M_state->wait();
740 if (!(__res._M_error ==
nullptr))
742 return static_cast<__result_type
>(__res);
747 _M_state.
swap(__that._M_state);
752 __basic_future(
const __state_type& __state) : _M_state(__state)
754 _State_base::_S_check(_M_state);
755 _M_state->_M_set_retrieved_flag();
760 __basic_future(
const shared_future<_Res>&) noexcept;
764 __basic_future(shared_future<_Res>&&) noexcept;
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;
782 template<
typename _Res>
787 static_assert(!
is_array<_Res>{},
"result type must not be an array");
790 "result type must be destructible");
793 template<
typename>
friend class packaged_task;
794 template<
typename _Fn,
typename... _Args>
795 friend future<__async_result_of<_Fn, _Args...>>
824 typename _Base_type::_Reset __reset(*
this);
825 return std::move(this->_M_get_result()._M_value());
832 template<typename _Res>
836 template<
typename>
friend class packaged_task;
837 template<
typename _Fn,
typename... _Args>
838 friend future<__async_result_of<_Fn, _Args...>>
867 typename _Base_type::_Reset __reset(*
this);
868 return this->_M_get_result()._M_get();
879 template<
typename>
friend class packaged_task;
880 template<
typename _Fn,
typename... _Args>
881 friend future<__async_result_of<_Fn, _Args...>>
910 typename _Base_type::_Reset __reset(*
this);
911 this->_M_get_result();
919 template<typename _Res>
924 static_assert(!
is_array<_Res>{},
"result type must not be an array");
927 "result type must be destructible");
953 shared_future& operator=(shared_future&& __sf) noexcept
955 shared_future(
std::move(__sf))._M_swap(*
this);
961 get()
const {
return this->_M_get_result()._M_value(); }
965 template<
typename _Res>
992 shared_future& operator=(shared_future&& __sf) noexcept
994 shared_future(
std::move(__sf))._M_swap(*
this);
1000 get()
const {
return this->_M_get_result()._M_get(); }
1031 shared_future& operator=(shared_future&& __sf) noexcept
1033 shared_future(
std::move(__sf))._M_swap(*
this);
1039 get()
const { this->_M_get_result(); }
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
1055 template<
typename _Res>
1056 inline __basic_future<_Res>::
1057 __basic_future(future<_Res>&& __uf) noexcept
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)); }
1078 template<
typename _Res>
1083 static_assert(!
is_array<_Res>{},
"result type must not be an array");
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;
1095 _Ptr_type _M_storage;
1099 : _M_future(std::make_shared<_State>()),
1100 _M_storage(
new _Res_type())
1104 : _M_future(
std::move(__rhs._M_future)),
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>
1116 : _M_future(
std::move(__rhs._M_future)),
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
1141 _M_future.
swap(__rhs._M_future);
1142 _M_storage.swap(__rhs._M_storage);
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))); }
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);
1180 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1188 __future_base::_State_base::_S_check(_M_future);
1193 template<
typename _Res>
1198 template<
typename _Res,
typename _Alloc>
1199 struct uses_allocator<promise<_Res>, _Alloc>
1204 template<
typename _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;
1214 _Ptr_type _M_storage;
1218 : _M_future(std::make_shared<_State>()),
1219 _M_storage(
new _Res_type())
1223 : _M_future(
std::move(__rhs._M_future)),
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>
1235 : _M_future(
std::move(__rhs._M_future)),
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
1260 _M_future.
swap(__rhs._M_future);
1261 _M_storage.swap(__rhs._M_storage);
1271 set_value(_Res& __r)
1272 { _M_state()._M_set_result(_State::__setter(
this, __r)); }
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),
1288 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1296 __future_base::_State_base::_S_check(_M_future);
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;
1312 _Ptr_type _M_storage;
1316 : _M_future(std::make_shared<_State>()),
1317 _M_storage(
new _Res_type())
1321 : _M_future(
std::move(__rhs._M_future)),
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))
1333 template<
typename _Allocator>
1335 : _M_future(
std::move(__rhs._M_future)),
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
1360 _M_future.
swap(__rhs._M_future);
1361 _M_storage.swap(__rhs._M_storage);
1372 { _M_state()._M_set_result(_State::__setter(
this)); }
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); }
1385 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1393 __future_base::_State_base::_S_check(_M_future);
1399 template<
typename _Ptr_type,
typename _Fn,
typename _Res>
1400 struct __future_base::_Task_setter
1403 _Ptr_type operator()()
const
1407 (*_M_result)->_M_set((*_M_fn)());
1411 __throw_exception_again;
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
1434 __throw_exception_again;
1442 _Ptr_type* _M_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))
1460 _M_run(_Args&&... __args) = 0;
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;
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),
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,
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;
1546 template<
typename _Fn,
typename _Fn2 = __remove_cvref_t<_Fn>>
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
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))
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); }
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();
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
1667 template<
typename _Res,
typename _Alloc>
1668 struct uses_allocator<packaged_task<_Res>, _Alloc>
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>()),
1689 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1690 _Ptr_type _M_result;
1703 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1708 virtual bool _M_is_deferred_future()
const {
return true; }
1712 class __future_base::_Async_state_commonV2
1713 :
public __future_base::_State_base
1716 ~_Async_state_commonV2() =
default;
1733 virtual void _M_complete_async() { _M_join(); }
1735 void _M_join() {
std::call_once(_M_once, &thread::join, &_M_thread); }
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>()),
1754 _M_thread =
std::thread{&_Async_state_impl::_M_run,
this};
1760 ~_Async_state_impl()
1762 if (_M_thread.joinable())
1772 _M_set_result(_S_task_setter(_M_result, _M_fn));
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...>>
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>;
1799 if ((__policy & launch::async) == launch::async)
1803 __state = std::make_shared<_As>(std::forward<_Fn>(__fn),
1804 std::forward<_Args>(__args)...);
1806 #if __cpp_exceptions
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)...);
1824 template<
typename _Fn,
typename... _Args>
1825 _GLIBCXX_NODISCARD
inline future<__async_result_of<_Fn, _Args...>>
1828 return std::async(launch::async|launch::deferred,
1829 std::forward<_Fn>(__fn),
1830 std::forward<_Args>(__args)...);
1837 _GLIBCXX_END_NAMESPACE_VERSION
error_condition make_error_condition(future_errc __errc) noexcept
Overload of make_error_condition for future_errc.
error_code make_error_code(future_errc __errc) noexcept
Overload of make_error_code for future_errc.
future_status
Status code for futures.
const error_category & future_category() noexcept
Points to a statically-allocated object derived from error_category.
future_errc
Error code for futures.
launch
Launch code for futures.
future< __async_result_of< _Fn, _Args... > > async(launch __policy, _Fn &&__fn, _Args &&... __args)
async
void swap(shared_ptr< _Tp > &__a, shared_ptr< _Tp > &__b) noexcept
Swap overload for shared_ptr.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
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...
exception_ptr current_exception() noexcept
exception_ptr make_exception_ptr(_Ex) noexcept
Obtain an exception_ptr pointing to a copy of the supplied object.
void rethrow_exception(exception_ptr)
Throw the object pointed to by the exception_ptr.
void call_once(once_flag &__once, _Callable &&__f, _Args &&... __args)
Invoke a callable and synchronize with other calls using the same flag.
ISO C++ entities toplevel namespace is std.
bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Exception type thrown by futures.
virtual const char * what() const noexcept
Primary template for future.
future(future &&__uf) noexcept
Move constructor.
_Res get()
Retrieving the value.
Primary template for shared_future.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
const _Res & get() const
Retrieving the value.
shared_future(const shared_future &__sf) noexcept
Copy constructor.
shared_future(future< _Res > &&__uf) noexcept
Construct from a future rvalue.
Primary template for promise.
Common implementation for future and shared_future.
__result_type _M_get_result() const
Wait for the state to be ready and rethrow any stored exception.
Partial specialization for future<R&>
_Res & get()
Retrieving the value.
future(future &&__uf) noexcept
Move constructor.
Explicit specialization for future<void>
void get()
Retrieving the value.
future(future &&__uf) noexcept
Move constructor.
Partial specialization for shared_future<R&>
_Res & get() const
Retrieving the value.
shared_future(future< _Res & > &&__uf) noexcept
Construct from a future rvalue.
shared_future(const shared_future &__sf)
Copy constructor.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
Explicit specialization for shared_future<void>
shared_future(future< void > &&__uf) noexcept
Construct from a future rvalue.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
shared_future(const shared_future &__sf)
Copy constructor.
One of two subclasses of exception.
An exception type that includes an error_code value.
Define a member typedef type only if a boolean constant is true.
The standard allocator, as per C++03 [20.4.1].
chrono::duration represents a distance between two points in time
chrono::time_point represents a point in time as measured by a clock
Thrown as part of forced unwinding.
An opaque pointer to an arbitrary exception.