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
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)
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);
285 template<
typename _Res,
typename _Allocator>
287 _S_allocate_result(
const _Allocator& __a)
290 typename __result_type::__allocator_type __a2(__a);
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 static _Setter<_Res, _Arg&&>
536 __setter(promise<_Res>* __prom, _Arg&& __arg)
538 _S_check(__prom->_M_future);
542 template<
typename _Res>
543 static _Setter<_Res, __exception_ptr_tag>
544 __setter(exception_ptr& __ex, promise<_Res>* __prom)
546 _S_check(__prom->_M_future);
547 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
550 template<
typename _Res>
551 static _Setter<_Res, void>
552 __setter(promise<_Res>* __prom)
554 _S_check(__prom->_M_future);
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>
767 static_assert(!
is_array<_Res>{},
"result type must not be an array");
770 "result type must be destructible");
773 template<
typename>
friend class packaged_task;
774 template<
typename _Fn,
typename... _Args>
775 friend future<__async_result_of<_Fn, _Args...>>
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...>>
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...>>
890 typename _Base_type::_Reset __reset(*
this);
899 template<typename _Res>
904 static_assert(!
is_array<_Res>{},
"result type must not be an array");
907 "result type must be destructible");
933 shared_future&
operator=(shared_future&& __sf) noexcept
935 shared_future(
std::move(__sf))._M_swap(*
this);
945 template<
typename _Res>
972 shared_future&
operator=(shared_future&& __sf) noexcept
974 shared_future(
std::move(__sf))._M_swap(*
this);
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
1035 template<
typename _Res>
1036 inline __basic_future<_Res>::
1037 __basic_future(future<_Res>&& __uf) noexcept
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");
1066 "result type must be destructible");
1068 typedef __future_base::_State_base _State;
1071 template<
typename,
typename>
friend struct _State::_Setter;
1079 : _M_future(std::make_shared<_State>()),
1084 : _M_future(
std::move(__rhs._M_future)),
1088 template<
typename _Allocator>
1090 : _M_future(std::allocate_shared<_State>(__a)),
1091 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1094 template<
typename _Allocator>
1096 : _M_future(
std::move(__rhs._M_future)),
1104 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1105 _M_future->_M_break_promise(
std::move(_M_storage));
1121 _M_future.
swap(__rhs._M_future);
1122 _M_storage.
swap(__rhs._M_storage);
1132 set_value(
const _Res& __r)
1133 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1136 set_value(_Res&& __r)
1137 { _M_future->_M_set_result(_State::__setter(
this,
std::move(__r))); }
1141 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1144 set_value_at_thread_exit(
const _Res& __r)
1146 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1151 set_value_at_thread_exit(_Res&& __r)
1153 _M_future->_M_set_delayed_result(
1154 _State::__setter(
this,
std::move(__r)), _M_future);
1160 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1165 template<
typename _Res>
1170 template<
typename _Res,
typename _Alloc>
1171 struct uses_allocator<promise<_Res>, _Alloc>
1176 template<
typename _Res>
1179 typedef __future_base::_State_base _State;
1182 template<
typename,
typename>
friend struct _State::_Setter;
1190 : _M_future(std::make_shared<_State>()),
1195 : _M_future(
std::move(__rhs._M_future)),
1199 template<
typename _Allocator>
1201 : _M_future(std::allocate_shared<_State>(__a)),
1202 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1205 template<
typename _Allocator>
1207 : _M_future(
std::move(__rhs._M_future)),
1215 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1216 _M_future->_M_break_promise(
std::move(_M_storage));
1232 _M_future.
swap(__rhs._M_future);
1233 _M_storage.
swap(__rhs._M_storage);
1243 set_value(_Res& __r)
1244 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1248 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1251 set_value_at_thread_exit(_Res& __r)
1253 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1260 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1269 typedef __future_base::_State_base _State;
1272 template<
typename,
typename>
friend struct _State::_Setter;
1280 : _M_future(std::make_shared<_State>()),
1285 : _M_future(
std::move(__rhs._M_future)),
1289 template<
typename _Allocator>
1291 : _M_future(std::allocate_shared<_State>(__a)),
1292 _M_storage(__future_base::_S_allocate_result<void>(__a))
1297 template<
typename _Allocator>
1299 : _M_future(
std::move(__rhs._M_future)),
1307 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1308 _M_future->_M_break_promise(
std::move(_M_storage));
1324 _M_future.
swap(__rhs._M_future);
1325 _M_storage.
swap(__rhs._M_storage);
1336 { _M_future->_M_set_result(_State::__setter(
this)); }
1340 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1343 set_value_at_thread_exit()
1344 { _M_future->_M_set_delayed_result(_State::__setter(
this), _M_future); }
1349 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1354 template<
typename _Ptr_type,
typename _Fn,
typename _Res>
1355 struct __future_base::_Task_setter
1358 _Ptr_type operator()()
const
1362 (*_M_result)->_M_set((*_M_fn)());
1366 __throw_exception_again;
1374 _Ptr_type* _M_result;
1378 template<
typename _Ptr_type,
typename _Fn>
1379 struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1381 _Ptr_type operator()()
const
1389 __throw_exception_again;
1397 _Ptr_type* _M_result;
1402 template<
typename _Res,
typename... _Args>
1403 struct __future_base::_Task_state_base<_Res(_Args...)>
1404 : __future_base::_State_base
1406 typedef _Res _Res_type;
1408 template<
typename _Alloc>
1409 _Task_state_base(
const _Alloc& __a)
1410 : _M_result(_S_allocate_result<_Res>(__a))
1415 _M_run(_Args&&... __args) = 0;
1419 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1421 virtual shared_ptr<_Task_state_base>
1424 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1425 _Ptr_type _M_result;
1429 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1430 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1431 : __future_base::_Task_state_base<_Res(_Args...)>
1433 template<
typename _Fn2>
1434 _Task_state(_Fn2&& __fn,
const _Alloc& __a)
1435 : _Task_state_base<_Res(_Args...)>(__a),
1441 _M_run(_Args&&... __args)
1443 auto __boundfn = [&] () -> _Res {
1444 return std::__invoke_r<_Res>(_M_impl._M_fn,
1445 std::forward<_Args>(__args)...);
1447 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1451 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1453 auto __boundfn = [&] () -> _Res {
1454 return std::__invoke_r<_Res>(_M_impl._M_fn,
1455 std::forward<_Args>(__args)...);
1457 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1461 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1464 struct _Impl : _Alloc
1466 template<
typename _Fn2>
1467 _Impl(_Fn2&& __fn,
const _Alloc& __a)
1468 : _Alloc(__a), _M_fn(
std::
forward<_Fn2>(__fn)) { }
1473 template<
typename _Signature,
typename _Fn,
1475 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1476 __create_task_state(_Fn&& __fn,
const _Alloc& __a = _Alloc())
1478 typedef typename decay<_Fn>::type _Fn2;
1479 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1480 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1483 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1484 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1485 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1487 return __create_task_state<_Res(_Args...)>(
std::move(_M_impl._M_fn),
1488 static_cast<_Alloc&
>(_M_impl));
1492 template<
typename _Res,
typename... _ArgTypes>
1493 class packaged_task<_Res(_ArgTypes...)>
1495 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1500 template<
typename _Fn,
typename _Fn2 = __remove_cvref_t<_Fn>>
1506 packaged_task() noexcept { }
1508 template<
typename _Fn,
typename = __not_same<_Fn>>
1510 packaged_task(_Fn&& __fn)
1512 __create_task_state<_Res(_ArgTypes...)>(std::forward<_Fn>(__fn)))
1515 #if __cplusplus < 201703L
1520 template<
typename _Fn,
typename _Alloc,
typename = __not_same<_Fn>>
1522 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1523 std::forward<_Fn>(__fn), __a))
1528 template<
typename _Allocator>
1532 template<
typename _Allocator>
1534 const packaged_task&) =
delete;
1536 template<
typename _Allocator>
1538 packaged_task&& __other) noexcept
1539 { this->
swap(__other); }
1544 if (
static_cast<bool>(_M_state) && !_M_state.unique())
1545 _M_state->_M_break_promise(
std::move(_M_state->_M_result));
1549 packaged_task(
const packaged_task&) =
delete;
1550 packaged_task&
operator=(
const packaged_task&) =
delete;
1553 packaged_task(packaged_task&& __other) noexcept
1554 { this->
swap(__other); }
1556 packaged_task&
operator=(packaged_task&& __other) noexcept
1558 packaged_task(
std::move(__other)).swap(*
this);
1563 swap(packaged_task& __other) noexcept
1564 { _M_state.
swap(__other._M_state); }
1567 valid()
const noexcept
1568 {
return static_cast<bool>(_M_state); }
1577 operator()(_ArgTypes... __args)
1579 __future_base::_State_base::_S_check(_M_state);
1580 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1584 make_ready_at_thread_exit(_ArgTypes... __args)
1586 __future_base::_State_base::_S_check(_M_state);
1587 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1593 __future_base::_State_base::_S_check(_M_state);
1594 packaged_task __tmp;
1595 __tmp._M_state = _M_state;
1596 _M_state = _M_state->_M_reset();
1601 template<
typename _Res,
typename... _ArgTypes>
1603 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1604 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1607 #if __cplusplus < 201703L
1610 template<
typename _Res,
typename _Alloc>
1611 struct uses_allocator<packaged_task<_Res>, _Alloc>
1617 template<
typename _BoundFn,
typename _Res>
1618 class __future_base::_Deferred_state final
1619 :
public __future_base::_State_base
1622 template<
typename... _Args>
1624 _Deferred_state(_Args&&... __args)
1625 : _M_result(new _Result<_Res>()),
1626 _M_fn{{std::forward<_Args>(__args)...}}
1630 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1631 _Ptr_type _M_result;
1644 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1649 virtual bool _M_is_deferred_future()
const {
return true; }
1653 class __future_base::_Async_state_commonV2
1654 :
public __future_base::_State_base
1657 ~_Async_state_commonV2() =
default;
1674 virtual void _M_complete_async() { _M_join(); }
1676 void _M_join() {
std::call_once(_M_once, &thread::join, &_M_thread); }
1684 template<
typename _BoundFn,
typename _Res>
1685 class __future_base::_Async_state_impl final
1686 :
public __future_base::_Async_state_commonV2
1689 template<
typename... _Args>
1691 _Async_state_impl(_Args&&... __args)
1692 : _M_result(new _Result<_Res>()),
1693 _M_fn{{std::forward<_Args>(__args)...}}
1695 _M_thread =
std::thread{&_Async_state_impl::_M_run,
this};
1701 ~_Async_state_impl()
1703 if (_M_thread.joinable())
1713 _M_set_result(_S_task_setter(_M_result, _M_fn));
1718 if (
static_cast<bool>(_M_result))
1719 this->_M_break_promise(
std::move(_M_result));
1720 __throw_exception_again;
1724 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1725 _Ptr_type _M_result;
1731 template<
typename _Fn,
typename... _Args>
1732 _GLIBCXX_NODISCARD future<__async_result_of<_Fn, _Args...>>
1733 async(
launch __policy, _Fn&& __fn, _Args&&... __args)
1735 using _Wr = std::thread::_Call_wrapper<_Fn, _Args...>;
1736 using _As = __future_base::_Async_state_impl<_Wr>;
1737 using _Ds = __future_base::_Deferred_state<_Wr>;
1740 if ((__policy & launch::async) == launch::async)
1744 __state = std::make_shared<_As>(std::forward<_Fn>(__fn),
1745 std::forward<_Args>(__args)...);
1747 #if __cpp_exceptions
1750 if (__e.code() != errc::resource_unavailable_try_again
1751 || (__policy & launch::deferred) != launch::deferred)
1758 __state = std::make_shared<_Ds>(std::forward<_Fn>(__fn),
1759 std::forward<_Args>(__args)...);
1765 template<
typename _Fn,
typename... _Args>
1766 _GLIBCXX_NODISCARD
inline future<__async_result_of<_Fn, _Args...>>
1767 async(_Fn&& __fn, _Args&&... __args)
1769 return std::async(launch::async|launch::deferred,
1770 std::forward<_Fn>(__fn),
1771 std::forward<_Args>(__args)...);
1778 _GLIBCXX_END_NAMESPACE_VERSION
auto_ptr & operator=(auto_ptr &__a)
auto_ptr assignment operator.
void reset(element_type *__p=0)
Forcibly deletes the managed object.
error_condition make_error_condition(future_errc __errc) noexcept
Overload for make_error_condition.
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(_Fn &&__fn, _Args &&... __args)
async, potential overload
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.
__allocated_ptr< _Alloc > __allocate_guarded(_Alloc &__a)
Allocate space for a single object using __a.
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.
Base class and enclosing scope.
A result object that has storage for an object of type _Res.
A result object that uses an allocator.
Partial specialization for reference types.
Explicit specialization for void.
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.
Non-standard RAII type for managing pointers obtained from allocators.
The standard allocator, as per [20.4].
Thrown as part of forced unwinding.
An opaque pointer to an arbitrary exception.
20.7.1.2 unique_ptr for single objects.
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.