29#ifndef _GLIBCXX_FUTURE
30#define _GLIBCXX_FUTURE 1
32#pragma GCC system_header
34#if __cplusplus < 201103L
47#include <bits/shared_ptr.h>
49#include <bits/uses_allocator.h>
53namespace std _GLIBCXX_VISIBILITY(default)
55_GLIBCXX_BEGIN_NAMESPACE_VERSION
68 future_already_retrieved = 1,
69 promise_already_satisfied,
88 inline error_condition
110 code() const noexcept {
return _M_code; }
115 :
logic_error(
"std::future_error: " + __ec.message()), _M_code(__ec)
118 friend void __throw_future_error(
int);
124 template<
typename _Res>
127 template<
typename _Res>
130 template<
typename _Signature>
133 template<
typename _Res>
145 return static_cast<launch>(
146 static_cast<int>(__x) &
static_cast<int>(__y));
151 return static_cast<launch>(
152 static_cast<int>(__x) |
static_cast<int>(__y));
157 return static_cast<launch>(
158 static_cast<int>(__x) ^
static_cast<int>(__y));
162 {
return static_cast<launch>(~static_cast<int>(__x)); }
165 {
return __x = __x & __y; }
168 {
return __x = __x | __y; }
171 {
return __x = __x ^ __y; }
183 template<
typename _Fn,
typename... _Args>
184 using __async_result_of =
typename __invoke_result<
185 typename decay<_Fn>::type,
typename decay<_Args>::type...>::type;
187 template<
typename _Fn,
typename... _Args>
188 future<__async_result_of<_Fn, _Args...>>
189 async(
launch __policy, _Fn&& __fn, _Args&&... __args);
191 template<
typename _Fn,
typename... _Args>
192 future<__async_result_of<_Fn, _Args...>>
193 async(_Fn&& __fn, _Args&&... __args);
195#if defined(_GLIBCXX_HAS_GTHREADS)
209 virtual void _M_destroy() = 0;
213 void operator()(
_Result_base* __fr)
const { __fr->_M_destroy(); }
222 template<
typename _Res>
226 template<
typename _Res>
230 __gnu_cxx::__aligned_buffer<_Res> _M_storage;
234 typedef _Res result_type;
236 _Result() noexcept : _M_initialized() { }
246 _M_value()
noexcept {
return *_M_storage._M_ptr(); }
249 _M_set(
const _Res& __res)
251 ::new (_M_storage._M_addr()) _Res(__res);
252 _M_initialized =
true;
258 ::new (_M_storage._M_addr()) _Res(
std::move(__res));
259 _M_initialized =
true;
263 void _M_destroy() {
delete this; }
267 template<
typename _Res,
typename _Alloc>
270 using __allocator_type = __alloc_rebind<_Alloc, _Result_alloc>;
279 __allocator_type __a(*
this);
286 template<
typename _Res,
typename _Allocator>
288 _S_allocate_result(
const _Allocator& __a)
291 typename __result_type::__allocator_type __a2(__a);
293 __result_type* __p = ::new((
void*)__guard.get()) __result_type{__a};
295 return _Ptr<__result_type>(__p);
299 template<
typename _Res,
typename _Tp>
300 static _Ptr<_Result<_Res>>
303 return _Ptr<_Result<_Res>>(
new _Result<_Res>);
311 typedef _Ptr<_Result_base> _Ptr_type;
313 enum _Status :
unsigned {
319 __atomic_futex_unsigned<> _M_status;
320 atomic_flag _M_retrieved = ATOMIC_FLAG_INIT;
324 _State_baseV2() noexcept : _M_result(), _M_status(_Status::__not_ready)
326 _State_baseV2(
const _State_baseV2&) =
delete;
327 _State_baseV2& operator=(
const _State_baseV2&) =
delete;
328 virtual ~_State_baseV2() =
default;
337 _M_status._M_load_when_equal(_Status::__ready, memory_order_acquire);
341 template<
typename _Rep,
typename _Period>
343 wait_for(
const chrono::duration<_Rep, _Period>& __rel)
347 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
348 return future_status::ready;
349 if (_M_is_deferred_future())
350 return future_status::deferred;
351 if (_M_status._M_load_when_equal_for(_Status::__ready,
352 memory_order_acquire, __rel))
365 return future_status::ready;
367 return future_status::timeout;
370 template<
typename _Clock,
typename _Duration>
372 wait_until(
const chrono::time_point<_Clock, _Duration>& __abs)
374#if __cplusplus > 201703L
375 static_assert(chrono::is_clock_v<_Clock>);
379 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
380 return future_status::ready;
381 if (_M_is_deferred_future())
382 return future_status::deferred;
383 if (_M_status._M_load_when_equal_until(_Status::__ready,
384 memory_order_acquire, __abs))
391 return future_status::ready;
393 return future_status::timeout;
399 _M_set_result(function<_Ptr_type()> __res,
bool __ignore_failure =
false)
401 bool __did_set =
false;
404 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
408 _M_status._M_store_notify_all(_Status::__ready,
409 memory_order_release);
410 else if (!__ignore_failure)
411 __throw_future_error(
int(future_errc::promise_already_satisfied));
418 _M_set_delayed_result(function<_Ptr_type()> __res,
419 weak_ptr<_State_baseV2> __self)
421 bool __did_set =
false;
422 unique_ptr<_Make_ready> __mr{
new _Make_ready};
425 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
428 __throw_future_error(
int(future_errc::promise_already_satisfied));
429 __mr->_M_shared_state =
std::move(__self);
436 _M_break_promise(_Ptr_type __res)
438 if (
static_cast<bool>(__res))
446 _M_result.swap(__res);
448 _M_status._M_store_notify_all(_Status::__ready,
449 memory_order_release);
455 _M_set_retrieved_flag()
457 if (_M_retrieved.test_and_set())
458 __throw_future_error(
int(future_errc::future_already_retrieved));
461 template<
typename _Res,
typename _Arg>
465 template<
typename _Res,
typename _Arg>
466 struct _Setter<_Res, _Arg&>
470 static_assert(is_same<_Res, _Arg&>::value
471 || is_same<const _Res, _Arg>::value,
472 "Invalid specialisation");
475 typename promise<_Res>::_Ptr_type operator()()
const
477 _M_promise->_M_storage->_M_set(*_M_arg);
478 return std::move(_M_promise->_M_storage);
480 promise<_Res>* _M_promise;
485 template<
typename _Res>
486 struct _Setter<_Res, _Res&&>
489 typename promise<_Res>::_Ptr_type operator()()
const
491 _M_promise->_M_storage->_M_set(
std::move(*_M_arg));
492 return std::move(_M_promise->_M_storage);
494 promise<_Res>* _M_promise;
499 template<
typename _Res>
500 struct _Setter<_Res, void>
502 static_assert(is_void<_Res>::value,
"Only used for promise<void>");
504 typename promise<_Res>::_Ptr_type operator()()
const
505 {
return std::move(_M_promise->_M_storage); }
507 promise<_Res>* _M_promise;
510 struct __exception_ptr_tag { };
513 template<
typename _Res>
514 struct _Setter<_Res, __exception_ptr_tag>
517 typename promise<_Res>::_Ptr_type operator()()
const
519 _M_promise->_M_storage->_M_error = *_M_ex;
520 return std::move(_M_promise->_M_storage);
523 promise<_Res>* _M_promise;
524 exception_ptr* _M_ex;
527 template<
typename _Res,
typename _Arg>
528 static _Setter<_Res, _Arg&&>
529 __setter(promise<_Res>* __prom, _Arg&& __arg)
531 _S_check(__prom->_M_future);
535 template<
typename _Res>
536 static _Setter<_Res, __exception_ptr_tag>
537 __setter(exception_ptr& __ex, promise<_Res>* __prom)
539 _S_check(__prom->_M_future);
540 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
543 template<
typename _Res>
544 static _Setter<_Res, void>
545 __setter(promise<_Res>* __prom)
547 _S_check(__prom->_M_future);
548 return _Setter<_Res, void>{ __prom };
551 template<
typename _Tp>
553 _S_check(
const shared_ptr<_Tp>& __p)
555 if (!
static_cast<bool>(__p))
556 __throw_future_error((
int)future_errc::no_state);
562 _M_do_set(function<_Ptr_type()>* __f,
bool* __did_set)
564 _Ptr_type __res = (*__f)();
569 _M_result.swap(__res);
573 virtual void _M_complete_async() { }
576 virtual bool _M_is_deferred_future()
const {
return false; }
578 struct _Make_ready final : __at_thread_exit_elt
580 weak_ptr<_State_baseV2> _M_shared_state;
581 static void _S_run(
void*);
586#ifdef _GLIBCXX_ASYNC_ABI_COMPAT
588 class _Async_state_common;
590 using _State_base = _State_baseV2;
591 class _Async_state_commonV2;
594 template<
typename _BoundFn,
595 typename _Res =
decltype(std::declval<_BoundFn&>()())>
596 class _Deferred_state;
598 template<
typename _BoundFn,
599 typename _Res =
decltype(std::declval<_BoundFn&>()())>
600 class _Async_state_impl;
602 template<
typename _Signature>
603 class _Task_state_base;
605 template<
typename _Fn,
typename _Alloc,
typename _Signature>
608 template<
typename _BoundFn>
610 _S_make_deferred_state(_BoundFn&& __fn);
612 template<
typename _BoundFn>
614 _S_make_async_state(_BoundFn&& __fn);
616 template<
typename _Res_ptr,
typename _Fn,
617 typename _Res =
typename _Res_ptr::element_type::result_type>
620 template<
typename _Res_ptr,
typename _BoundFn>
621 static _Task_setter<_Res_ptr, _BoundFn>
622 _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
629 template<
typename _Res>
632 typedef _Res& result_type;
634 _Result() noexcept : _M_value_ptr() { }
637 _M_set(_Res& __res)
noexcept
640 _Res& _M_get()
noexcept {
return *_M_value_ptr; }
645 void _M_destroy() {
delete this; }
652 typedef void result_type;
655 void _M_destroy() {
delete this; }
658#ifndef _GLIBCXX_ASYNC_ABI_COMPAT
661 template<
typename _Res,
typename _Arg>
667 template<
typename _Res_ptr,
typename _Fn,
typename _Res>
673 template<
typename _Res>
689 valid()
const noexcept {
return static_cast<bool>(_M_state); }
694 _State_base::_S_check(_M_state);
698 template<
typename _Rep,
typename _Period>
702 _State_base::_S_check(_M_state);
703 return _M_state->wait_for(__rel);
706 template<
typename _Clock,
typename _Duration>
710 _State_base::_S_check(_M_state);
711 return _M_state->wait_until(__abs);
719 _State_base::_S_check(_M_state);
721 if (!(__res._M_error == 0))
728 _M_state.
swap(__that._M_state);
733 __basic_future(
const __state_type& __state) : _M_state(__state)
735 _State_base::_S_check(_M_state);
736 _M_state->_M_set_retrieved_flag();
741 __basic_future(
const shared_future<_Res>&)
noexcept;
745 __basic_future(shared_future<_Res>&&) noexcept;
749 __basic_future(future<_Res>&&) noexcept;
751 constexpr __basic_future() noexcept : _M_state() { }
755 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
756 ~_Reset() { _M_fut._M_state.reset(); }
757 __basic_future& _M_fut;
763 template<
typename _Res>
767 template<
typename>
friend class packaged_task;
768 template<
typename _Fn,
typename... _Args>
769 friend future<__async_result_of<_Fn, _Args...>>
798 typename _Base_type::_Reset __reset(*
this);
806 template<typename _Res>
810 template<
typename>
friend class packaged_task;
811 template<
typename _Fn,
typename... _Args>
812 friend future<__async_result_of<_Fn, _Args...>>
841 typename _Base_type::_Reset __reset(*
this);
853 template<
typename>
friend class packaged_task;
854 template<
typename _Fn,
typename... _Args>
855 friend future<__async_result_of<_Fn, _Args...>>
884 typename _Base_type::_Reset __reset(*
this);
893 template<typename _Res>
920 shared_future& operator=(shared_future&& __sf)
noexcept
922 shared_future(
std::move(__sf))._M_swap(*
this);
932 template<
typename _Res>
959 shared_future& operator=(shared_future&& __sf)
noexcept
961 shared_future(
std::move(__sf))._M_swap(*
this);
998 shared_future& operator=(shared_future&& __sf)
noexcept
1000 shared_future(
std::move(__sf))._M_swap(*
this);
1010 template<
typename _Res>
1011 inline __basic_future<_Res>::
1012 __basic_future(
const shared_future<_Res>& __sf) noexcept
1013 : _M_state(__sf._M_state)
1016 template<
typename _Res>
1017 inline __basic_future<_Res>::
1018 __basic_future(shared_future<_Res>&& __sf) noexcept
1022 template<
typename _Res>
1023 inline __basic_future<_Res>::
1024 __basic_future(future<_Res>&& __uf) noexcept
1030 template<
typename _Res>
1031 inline shared_future<_Res>
1032 future<_Res>::share() noexcept
1033 {
return shared_future<_Res>(
std::move(*
this)); }
1035 template<
typename _Res>
1036 inline shared_future<_Res&>
1037 future<_Res&>::share() noexcept
1038 {
return shared_future<_Res&>(
std::move(*
this)); }
1040 inline shared_future<void>
1041 future<void>::share() noexcept
1042 {
return shared_future<void>(
std::move(*
this)); }
1045 template<
typename _Res>
1048 typedef __future_base::_State_base _State;
1051 template<
typename,
typename>
friend class _State::_Setter;
1059 : _M_future(std::make_shared<_State>()),
1064 : _M_future(
std::move(__rhs._M_future)),
1068 template<
typename _Allocator>
1070 : _M_future(std::allocate_shared<_State>(__a)),
1071 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1074 template<
typename _Allocator>
1076 : _M_future(
std::move(__rhs._M_future)),
1084 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1085 _M_future->_M_break_promise(
std::move(_M_storage));
1090 operator=(
promise&& __rhs)
noexcept
1101 _M_future.
swap(__rhs._M_future);
1102 _M_storage.
swap(__rhs._M_storage);
1112 set_value(
const _Res& __r)
1113 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1116 set_value(_Res&& __r)
1117 { _M_future->_M_set_result(_State::__setter(
this,
std::move(__r))); }
1121 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1124 set_value_at_thread_exit(
const _Res& __r)
1126 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1131 set_value_at_thread_exit(_Res&& __r)
1133 _M_future->_M_set_delayed_result(
1134 _State::__setter(
this,
std::move(__r)), _M_future);
1140 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1145 template<
typename _Res>
1150 template<
typename _Res,
typename _Alloc>
1151 struct uses_allocator<promise<_Res>, _Alloc>
1156 template<
typename _Res>
1159 typedef __future_base::_State_base _State;
1162 template<
typename,
typename>
friend class _State::_Setter;
1170 : _M_future(std::make_shared<_State>()),
1175 : _M_future(
std::move(__rhs._M_future)),
1179 template<
typename _Allocator>
1181 : _M_future(std::allocate_shared<_State>(__a)),
1182 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1185 template<
typename _Allocator>
1187 : _M_future(
std::move(__rhs._M_future)),
1195 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1196 _M_future->_M_break_promise(
std::move(_M_storage));
1201 operator=(
promise&& __rhs)
noexcept
1212 _M_future.
swap(__rhs._M_future);
1213 _M_storage.
swap(__rhs._M_storage);
1223 set_value(_Res& __r)
1224 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1228 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1231 set_value_at_thread_exit(_Res& __r)
1233 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1240 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1249 typedef __future_base::_State_base _State;
1252 template<
typename,
typename>
friend class _State::_Setter;
1260 : _M_future(std::make_shared<_State>()),
1265 : _M_future(
std::move(__rhs._M_future)),
1269 template<
typename _Allocator>
1271 : _M_future(std::allocate_shared<_State>(__a)),
1272 _M_storage(__future_base::_S_allocate_result<void>(__a))
1277 template<
typename _Allocator>
1279 : _M_future(
std::move(__rhs._M_future)),
1287 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1288 _M_future->_M_break_promise(
std::move(_M_storage));
1293 operator=(
promise&& __rhs)
noexcept
1304 _M_future.
swap(__rhs._M_future);
1305 _M_storage.
swap(__rhs._M_storage);
1316 { _M_future->_M_set_result(_State::__setter(
this)); }
1320 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1323 set_value_at_thread_exit()
1324 { _M_future->_M_set_delayed_result(_State::__setter(
this), _M_future); }
1329 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1334 template<
typename _Ptr_type,
typename _Fn,
typename _Res>
1335 struct __future_base::_Task_setter
1338 _Ptr_type operator()()
const
1342 (*_M_result)->_M_set((*_M_fn)());
1346 __throw_exception_again;
1354 _Ptr_type* _M_result;
1358 template<
typename _Ptr_type,
typename _Fn>
1359 struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1361 _Ptr_type operator()()
const
1369 __throw_exception_again;
1377 _Ptr_type* _M_result;
1382 template<
typename _Res,
typename... _Args>
1383 struct __future_base::_Task_state_base<_Res(_Args...)>
1384 : __future_base::_State_base
1386 typedef _Res _Res_type;
1388 template<
typename _Alloc>
1389 _Task_state_base(
const _Alloc& __a)
1390 : _M_result(_S_allocate_result<_Res>(__a))
1395 _M_run(_Args&&... __args) = 0;
1399 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1401 virtual shared_ptr<_Task_state_base>
1404 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1405 _Ptr_type _M_result;
1409 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1410 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1411 : __future_base::_Task_state_base<_Res(_Args...)>
1413 template<
typename _Fn2>
1414 _Task_state(_Fn2&& __fn,
const _Alloc& __a)
1415 : _Task_state_base<_Res(_Args...)>(__a),
1421 _M_run(_Args&&... __args)
1423 auto __boundfn = [&] () -> _Res {
1424 return std::__invoke_r<_Res>(_M_impl._M_fn,
1425 std::forward<_Args>(__args)...);
1427 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1431 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1433 auto __boundfn = [&] () -> _Res {
1434 return std::__invoke_r<_Res>(_M_impl._M_fn,
1435 std::forward<_Args>(__args)...);
1437 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1441 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1444 struct _Impl : _Alloc
1446 template<
typename _Fn2>
1447 _Impl(_Fn2&& __fn,
const _Alloc& __a)
1448 : _Alloc(__a), _M_fn(
std::
forward<_Fn2>(__fn)) { }
1453 template<
typename _Signature,
typename _Fn,
1455 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1456 __create_task_state(_Fn&& __fn,
const _Alloc& __a = _Alloc())
1458 typedef typename decay<_Fn>::type _Fn2;
1459 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1460 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1463 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1464 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1465 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1467 return __create_task_state<_Res(_Args...)>(
std::move(_M_impl._M_fn),
1468 static_cast<_Alloc&
>(_M_impl));
1472 template<
typename _Res,
typename... _ArgTypes>
1473 class packaged_task<_Res(_ArgTypes...)>
1475 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1480 template<
typename _Fn,
typename _Fn2 = __remove_cvref_t<_Fn>>
1486 packaged_task()
noexcept { }
1488 template<
typename _Fn,
typename = __not_same<_Fn>>
1490 packaged_task(_Fn&& __fn)
1492 __create_task_state<_Res(_ArgTypes...)>(std::forward<_Fn>(__fn)))
1495#if __cplusplus < 201703L
1500 template<
typename _Fn,
typename _Alloc,
typename = __not_same<_Fn>>
1502 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1503 std::forward<_Fn>(__fn), __a))
1508 template<
typename _Allocator>
1512 template<
typename _Allocator>
1514 const packaged_task&) =
delete;
1516 template<
typename _Allocator>
1518 packaged_task&& __other)
noexcept
1519 { this->swap(__other); }
1524 if (
static_cast<bool>(_M_state) && !_M_state.unique())
1525 _M_state->_M_break_promise(
std::move(_M_state->_M_result));
1529 packaged_task(
const packaged_task&) =
delete;
1530 packaged_task& operator=(
const packaged_task&) =
delete;
1533 packaged_task(packaged_task&& __other)
noexcept
1534 { this->swap(__other); }
1536 packaged_task& operator=(packaged_task&& __other)
noexcept
1538 packaged_task(
std::move(__other)).swap(*
this);
1543 swap(packaged_task& __other)
noexcept
1544 { _M_state.
swap(__other._M_state); }
1547 valid()
const noexcept
1548 {
return static_cast<bool>(_M_state); }
1557 operator()(_ArgTypes... __args)
1559 __future_base::_State_base::_S_check(_M_state);
1560 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1564 make_ready_at_thread_exit(_ArgTypes... __args)
1566 __future_base::_State_base::_S_check(_M_state);
1567 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1573 __future_base::_State_base::_S_check(_M_state);
1574 packaged_task __tmp;
1575 __tmp._M_state = _M_state;
1576 _M_state = _M_state->_M_reset();
1581 template<
typename _Res,
typename... _ArgTypes>
1583 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1584 packaged_task<_Res(_ArgTypes...)>& __y)
noexcept
1587#if __cplusplus < 201703L
1590 template<
typename _Res,
typename _Alloc>
1591 struct uses_allocator<packaged_task<_Res>, _Alloc>
1597 template<
typename _BoundFn,
typename _Res>
1598 class __future_base::_Deferred_state final
1599 :
public __future_base::_State_base
1603 _Deferred_state(_BoundFn&& __fn)
1604 : _M_result(new _Result<_Res>()), _M_fn(
std::
move(__fn))
1608 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1609 _Ptr_type _M_result;
1622 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1627 virtual bool _M_is_deferred_future()
const {
return true; }
1631 class __future_base::_Async_state_commonV2
1632 :
public __future_base::_State_base
1635 ~_Async_state_commonV2() =
default;
1652 virtual void _M_complete_async() { _M_join(); }
1654 void _M_join() {
std::call_once(_M_once, &thread::join, &_M_thread); }
1662 template<
typename _BoundFn,
typename _Res>
1663 class __future_base::_Async_state_impl final
1664 :
public __future_base::_Async_state_commonV2
1668 _Async_state_impl(_BoundFn&& __fn)
1669 : _M_result(new _Result<_Res>()), _M_fn(
std::
move(__fn))
1674 _M_set_result(_S_task_setter(_M_result, _M_fn));
1679 if (
static_cast<bool>(_M_result))
1680 this->_M_break_promise(
std::move(_M_result));
1681 __throw_exception_again;
1689 ~_Async_state_impl() {
if (_M_thread.joinable()) _M_thread.join(); }
1692 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1693 _Ptr_type _M_result;
1697 template<
typename _BoundFn>
1699 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1701 typedef typename remove_reference<_BoundFn>::type __fn_type;
1702 typedef _Deferred_state<__fn_type> __state_type;
1703 return std::make_shared<__state_type>(
std::move(__fn));
1706 template<
typename _BoundFn>
1708 __future_base::_S_make_async_state(_BoundFn&& __fn)
1710 typedef typename remove_reference<_BoundFn>::type __fn_type;
1711 typedef _Async_state_impl<__fn_type> __state_type;
1712 return std::make_shared<__state_type>(
std::move(__fn));
1717 template<
typename _Fn,
typename... _Args>
1718 _GLIBCXX_NODISCARD future<__async_result_of<_Fn, _Args...>>
1719 async(
launch __policy, _Fn&& __fn, _Args&&... __args)
1722 if ((__policy & launch::async) == launch::async)
1726 __state = __future_base::_S_make_async_state(
1727 std::thread::__make_invoker(std::forward<_Fn>(__fn),
1728 std::forward<_Args>(__args)...)
1734 if (__e.code() != errc::resource_unavailable_try_again
1735 || (__policy & launch::deferred) != launch::deferred)
1742 __state = __future_base::_S_make_deferred_state(
1743 std::thread::__make_invoker(std::forward<_Fn>(__fn),
1744 std::forward<_Args>(__args)...));
1746 return future<__async_result_of<_Fn, _Args...>>(__state);
1750 template<
typename _Fn,
typename... _Args>
1751 _GLIBCXX_NODISCARD
inline future<__async_result_of<_Fn, _Args...>>
1752 async(_Fn&& __fn, _Args&&... __args)
1754 return std::async(launch::async|launch::deferred,
1755 std::forward<_Fn>(__fn),
1756 std::forward<_Args>(__args)...);
1763_GLIBCXX_END_NAMESPACE_VERSION
const error_category & future_category() noexcept
Points to a statically-allocated object derived from error_category.
void get()
Retrieving the value.
void swap(packaged_task< _Res(_ArgTypes...)> &__x, packaged_task< _Res(_ArgTypes...)> &__y) noexcept
swap
future(future &&__uf) noexcept
Move constructor.
error_condition make_error_condition(future_errc __errc) noexcept
Overload for make_error_condition.
_Res & get()
Retrieving the value.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
future(future &&__uf) noexcept
Move constructor.
future_status
Status code for futures.
future_errc
Error code for futures.
shared_future(future< _Res & > &&__uf) noexcept
Construct from a future rvalue.
virtual const char * what() const noexcept
launch
Launch code for futures.
__result_type _M_get_result() const
Wait for the state to be ready and rethrow any stored exception.
shared_future(const shared_future &__sf) noexcept
Copy constructor.
future(future &&__uf) noexcept
Move constructor.
shared_future(future< void > &&__uf) noexcept
Construct from a future rvalue.
shared_future(future< _Res > &&__uf) noexcept
Construct from a future rvalue.
_Res & get() const
Retrieving the value.
const _Res & get() const
Retrieving the value.
shared_future(const shared_future &__sf)
Copy constructor.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
_Res get()
Retrieving the value.
future< __async_result_of< _Fn, _Args... > > async(_Fn &&__fn, _Args &&... __args)
async, potential overload
shared_future(const shared_future &__sf)
Copy constructor.
future< __async_result_of< _Fn, _Args... > > async(launch __policy, _Fn &&__fn, _Args &&... __args)
async
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
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
Returns the actual address of the object or function referenced by r, even in the presence of an over...
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
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.
Exception type thrown by futures.
Primary template for future.
Primary template for shared_future.
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.
Partial specialization for future<R&>
Explicit specialization for future<void>
Partial specialization for shared_future<R&>
Explicit specialization for shared_future<void>
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.