29#ifndef _GLIBCXX_FUTURE
30#define _GLIBCXX_FUTURE 1
32#pragma GCC system_header
36#if __cplusplus < 201103L
49#include <bits/shared_ptr.h>
52#include <bits/uses_allocator.h>
55namespace std _GLIBCXX_VISIBILITY(default)
57_GLIBCXX_BEGIN_NAMESPACE_VERSION
76 future_already_retrieved = 1,
77 promise_already_satisfied,
87 [[__nodiscard__, __gnu__::__const__]]
99 inline error_condition
122 code() const noexcept {
return _M_code; }
127 :
logic_error(
"std::future_error: " + __ec.message()), _M_code(__ec)
130 friend void __throw_future_error(
int);
136 template<
typename _Res>
139 template<
typename _Res>
142 template<
typename _Signature>
145 template<
typename _Res>
157 return static_cast<launch>(
158 static_cast<int>(__x) &
static_cast<int>(__y));
163 return static_cast<launch>(
164 static_cast<int>(__x) |
static_cast<int>(__y));
169 return static_cast<launch>(
170 static_cast<int>(__x) ^
static_cast<int>(__y));
174 {
return static_cast<launch>(~static_cast<int>(__x)); }
177 {
return __x = __x & __y; }
180 {
return __x = __x | __y; }
183 {
return __x = __x ^ __y; }
196 template<
typename _Fn,
typename... _Args>
197 using __async_result_of =
typename __invoke_result<
198 typename decay<_Fn>::type,
typename decay<_Args>::type...>::type;
201 template<
typename _Fn,
typename... _Args>
202 future<__async_result_of<_Fn, _Args...>>
203 async(
launch __policy, _Fn&& __fn, _Args&&... __args);
205 template<
typename _Fn,
typename... _Args>
206 future<__async_result_of<_Fn, _Args...>>
207 async(_Fn&& __fn, _Args&&... __args);
209#if defined(_GLIBCXX_HAS_GTHREADS)
219 exception_ptr _M_error;
221 _Result_base(
const _Result_base&) =
delete;
222 _Result_base& operator=(
const _Result_base&) =
delete;
225 virtual void _M_destroy() = 0;
229 void operator()(_Result_base* __fr)
const { __fr->_M_destroy(); }
234 virtual ~_Result_base();
238 template<
typename _Res>
239 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
242 template<
typename _Res>
243 struct _Result : _Result_base
246 __gnu_cxx::__aligned_buffer<_Res> _M_storage;
250 typedef _Res result_type;
252 _Result() noexcept : _M_initialized() { }
262 _M_value() noexcept {
return *_M_storage._M_ptr(); }
265 _M_set(
const _Res& __res)
267 ::new (_M_storage._M_addr()) _Res(__res);
268 _M_initialized = true;
274 ::new (_M_storage._M_addr()) _Res(
std::move(__res));
275 _M_initialized = true;
279 void _M_destroy() {
delete this; }
283 template<
typename _Res,
typename _Alloc>
284 struct _Result_alloc final : _Result<_Res>, _Alloc
286 using __allocator_type = __alloc_rebind<_Alloc, _Result_alloc>;
289 _Result_alloc(
const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
295 __allocator_type __a(*
this);
296 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
297 this->~_Result_alloc();
302 template<
typename _Res,
typename _Allocator>
303 static _Ptr<_Result_alloc<_Res, _Allocator>>
304 _S_allocate_result(
const _Allocator& __a)
306 using __result_type = _Result_alloc<_Res, _Allocator>;
307 typename __result_type::__allocator_type __a2(__a);
308 auto __guard = std::__allocate_guarded(__a2);
309 __result_type* __p = ::new((
void*)__guard.get()) __result_type{__a};
311 return _Ptr<__result_type>(__p);
315 template<
typename _Res,
typename _Tp>
316 static _Ptr<_Result<_Res>>
319 return _Ptr<_Result<_Res>>(
new _Result<_Res>);
327 typedef _Ptr<_Result_base> _Ptr_type;
329 enum _Status :
unsigned {
335 __atomic_futex_unsigned<> _M_status;
336 atomic_flag _M_retrieved = ATOMIC_FLAG_INIT;
340 _State_baseV2() noexcept : _M_result(), _M_status(_Status::__not_ready)
342 _State_baseV2(
const _State_baseV2&) =
delete;
343 _State_baseV2& operator=(
const _State_baseV2&) =
delete;
344 virtual ~_State_baseV2() =
default;
353 _M_status._M_load_when_equal(_Status::__ready, memory_order_acquire);
357 template<
typename _Rep,
typename _Period>
359 wait_for(
const chrono::duration<_Rep, _Period>& __rel)
363 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
364 return future_status::ready;
366 if (_M_is_deferred_future())
367 return future_status::deferred;
370 if (__rel > __rel.zero()
371 && _M_status._M_load_when_equal_for(_Status::__ready,
372 memory_order_acquire,
386 return future_status::ready;
388 return future_status::timeout;
391 template<
typename _Clock,
typename _Duration>
393 wait_until(
const chrono::time_point<_Clock, _Duration>& __abs)
395#if __cplusplus > 201703L
396 static_assert(chrono::is_clock_v<_Clock>);
400 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
401 return future_status::ready;
403 if (_M_is_deferred_future())
404 return future_status::deferred;
406 if (_M_status._M_load_when_equal_until(_Status::__ready,
407 memory_order_acquire,
415 return future_status::ready;
417 return future_status::timeout;
423 _M_set_result(function<_Ptr_type()> __res,
bool __ignore_failure =
false)
425 bool __did_set =
false;
428 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
432 _M_status._M_store_notify_all(_Status::__ready,
433 memory_order_release);
434 else if (!__ignore_failure)
435 __throw_future_error(
int(future_errc::promise_already_satisfied));
442 _M_set_delayed_result(function<_Ptr_type()> __res,
443 weak_ptr<_State_baseV2> __self)
445 bool __did_set =
false;
446 unique_ptr<_Make_ready> __mr{
new _Make_ready};
449 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
452 __throw_future_error(
int(future_errc::promise_already_satisfied));
453 __mr->_M_shared_state =
std::move(__self);
460 _M_break_promise(_Ptr_type __res)
462 if (
static_cast<bool>(__res))
470 _M_result.swap(__res);
472 _M_status._M_store_notify_all(_Status::__ready,
473 memory_order_release);
479 _M_set_retrieved_flag()
481 if (_M_retrieved.test_and_set())
482 __throw_future_error(
int(future_errc::future_already_retrieved));
485 template<
typename _Res,
typename _Arg>
489 template<
typename _Res,
typename _Arg>
490 struct _Setter<_Res, _Arg&>
494 static_assert(is_same<_Res, _Arg&>::value
495 || is_same<const _Res, _Arg>::value,
496 "Invalid specialisation");
499 typename promise<_Res>::_Ptr_type operator()()
const
501 _M_promise->_M_storage->_M_set(*_M_arg);
502 return std::move(_M_promise->_M_storage);
504 promise<_Res>* _M_promise;
509 template<
typename _Res>
510 struct _Setter<_Res, _Res&&>
513 typename promise<_Res>::_Ptr_type operator()()
const
515 _M_promise->_M_storage->_M_set(
std::move(*_M_arg));
516 return std::move(_M_promise->_M_storage);
518 promise<_Res>* _M_promise;
523 template<
typename _Res>
524 struct _Setter<_Res, void>
526 static_assert(is_void<_Res>::value,
"Only used for promise<void>");
528 typename promise<_Res>::_Ptr_type operator()()
const
529 {
return std::move(_M_promise->_M_storage); }
531 promise<_Res>* _M_promise;
534 struct __exception_ptr_tag { };
537 template<
typename _Res>
538 struct _Setter<_Res, __exception_ptr_tag>
541 typename promise<_Res>::_Ptr_type operator()()
const
543 _M_promise->_M_storage->_M_error = *_M_ex;
544 return std::move(_M_promise->_M_storage);
547 promise<_Res>* _M_promise;
548 exception_ptr* _M_ex;
551 template<
typename _Res,
typename _Arg>
552 __attribute__((__always_inline__))
553 static _Setter<_Res, _Arg&&>
554 __setter(promise<_Res>* __prom, _Arg&& __arg)
noexcept
559 template<
typename _Res>
560 __attribute__((__always_inline__))
561 static _Setter<_Res, __exception_ptr_tag>
562 __setter(exception_ptr& __ex, promise<_Res>* __prom)
noexcept
564 __glibcxx_assert(__ex !=
nullptr);
565 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
568 template<
typename _Res>
569 __attribute__((__always_inline__))
570 static _Setter<_Res, void>
571 __setter(promise<_Res>* __prom)
noexcept
573 return _Setter<_Res, void>{ __prom };
576 template<
typename _Tp>
578 _S_check(
const shared_ptr<_Tp>& __p)
580 if (!
static_cast<bool>(__p))
581 __throw_future_error((
int)future_errc::no_state);
587 _M_do_set(function<_Ptr_type()>* __f,
bool* __did_set)
589 _Ptr_type __res = (*__f)();
594 _M_result.swap(__res);
598 virtual void _M_complete_async() { }
601 virtual bool _M_is_deferred_future()
const {
return false; }
603 struct _Make_ready final : __at_thread_exit_elt
605 weak_ptr<_State_baseV2> _M_shared_state;
606 static void _S_run(
void*);
611#ifdef _GLIBCXX_ASYNC_ABI_COMPAT
613 class _Async_state_common;
615 using _State_base = _State_baseV2;
616 class _Async_state_commonV2;
619 template<
typename _BoundFn,
620 typename _Res =
decltype(std::declval<_BoundFn&>()())>
621 class _Deferred_state;
623 template<
typename _BoundFn,
624 typename _Res =
decltype(std::declval<_BoundFn&>()())>
625 class _Async_state_impl;
627 template<
typename _Signature>
628 struct _Task_state_base;
630 template<
typename _Fn,
typename _Alloc,
typename _Signature>
633 template<
typename _Res_ptr,
typename _Fn,
634 typename _Res =
typename _Res_ptr::element_type::result_type>
637 template<
typename _Res_ptr,
typename _BoundFn>
638 static _Task_setter<_Res_ptr, _BoundFn>
639 _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
646 template<
typename _Res>
647 struct __future_base::_Result<_Res&> : __future_base::_Result_base
649 typedef _Res& result_type;
651 _Result() noexcept : _M_value_ptr() { }
654 _M_set(_Res& __res)
noexcept
657 _Res& _M_get() noexcept {
return *_M_value_ptr; }
662 void _M_destroy() {
delete this; }
667 struct __future_base::_Result<void> : __future_base::_Result_base
669 typedef void result_type;
672 void _M_destroy() {
delete this; }
677#ifndef _GLIBCXX_ASYNC_ABI_COMPAT
681 template<
typename _Res,
typename _Arg>
682 struct __is_location_invariant
683 <__future_base::_State_base::_Setter<_Res, _Arg>>
687 template<
typename _Res_ptr,
typename _Fn,
typename _Res>
688 struct __is_location_invariant
689 <__future_base::_Task_setter<_Res_ptr, _Fn, _Res>>
694 template<
typename _Res>
699 typedef __future_base::_Result<_Res>& __result_type;
710 valid()
const noexcept {
return static_cast<bool>(_M_state); }
715 _State_base::_S_check(_M_state);
719 template<
typename _Rep,
typename _Period>
723 _State_base::_S_check(_M_state);
724 return _M_state->wait_for(__rel);
727 template<
typename _Clock,
typename _Duration>
731 _State_base::_S_check(_M_state);
732 return _M_state->wait_until(__abs);
740 _State_base::_S_check(_M_state);
741 _Result_base& __res = _M_state->wait();
742 if (!(__res._M_error ==
nullptr))
744 return static_cast<__result_type
>(__res);
749 _M_state.
swap(__that._M_state);
754 __basic_future(
const __state_type& __state) : _M_state(__state)
756 _State_base::_S_check(_M_state);
757 _M_state->_M_set_retrieved_flag();
762 __basic_future(
const shared_future<_Res>&)
noexcept;
766 __basic_future(shared_future<_Res>&&) noexcept;
770 __basic_future(future<_Res>&&) noexcept;
772 constexpr __basic_future() noexcept : _M_state() { }
776 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
777 ~_Reset() { _M_fut._M_state.reset(); }
778 __basic_future& _M_fut;
784 template<
typename _Res>
789 static_assert(!
is_array<_Res>{},
"result type must not be an array");
792 "result type must be destructible");
795 template<
typename>
friend class packaged_task;
796 template<
typename _Fn,
typename... _Args>
797 friend future<__async_result_of<_Fn, _Args...>>
801 typedef typename _Base_type::__state_type __state_type;
804 future(
const __state_type& __state) : _Base_type(__state) { }
807 constexpr future() noexcept : _Base_type() { }
826 typename _Base_type::_Reset __reset(*
this);
827 return std::move(this->_M_get_result()._M_value());
834 template<typename _Res>
838 template<
typename>
friend class packaged_task;
839 template<
typename _Fn,
typename... _Args>
840 friend future<__async_result_of<_Fn, _Args...>>
869 typename _Base_type::_Reset __reset(*
this);
870 return this->_M_get_result()._M_get();
881 template<
typename>
friend class packaged_task;
882 template<
typename _Fn,
typename... _Args>
883 friend future<__async_result_of<_Fn, _Args...>>
912 typename _Base_type::_Reset __reset(*
this);
913 this->_M_get_result();
921 template<typename _Res>
926 static_assert(!
is_array<_Res>{},
"result type must not be an array");
929 "result type must be destructible");
955 shared_future& operator=(shared_future&& __sf)
noexcept
957 shared_future(
std::move(__sf))._M_swap(*
this);
963 get()
const {
return this->_M_get_result()._M_value(); }
967 template<
typename _Res>
994 shared_future& operator=(shared_future&& __sf)
noexcept
996 shared_future(
std::move(__sf))._M_swap(*
this);
1002 get()
const {
return this->_M_get_result()._M_get(); }
1033 shared_future& operator=(shared_future&& __sf)
noexcept
1035 shared_future(
std::move(__sf))._M_swap(*
this);
1041 get()
const { this->_M_get_result(); }
1045 template<
typename _Res>
1046 inline __basic_future<_Res>::
1047 __basic_future(
const shared_future<_Res>& __sf) noexcept
1048 : _M_state(__sf._M_state)
1051 template<
typename _Res>
1052 inline __basic_future<_Res>::
1053 __basic_future(shared_future<_Res>&& __sf) noexcept
1057 template<
typename _Res>
1058 inline __basic_future<_Res>::
1059 __basic_future(future<_Res>&& __uf) noexcept
1065 template<
typename _Res>
1066 inline shared_future<_Res>
1067 future<_Res>::share() noexcept
1068 {
return shared_future<_Res>(
std::move(*
this)); }
1070 template<
typename _Res>
1071 inline shared_future<_Res&>
1072 future<_Res&>::share() noexcept
1073 {
return shared_future<_Res&>(
std::move(*
this)); }
1075 inline shared_future<void>
1076 future<void>::share() noexcept
1077 {
return shared_future<void>(
std::move(*
this)); }
1080 template<
typename _Res>
1085 static_assert(!
is_array<_Res>{},
"result type must not be an array");
1088 "result type must be destructible");
1090 typedef __future_base::_State_base _State;
1091 typedef __future_base::_Result<_Res> _Res_type;
1092 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1093 template<
typename,
typename>
friend struct _State::_Setter;
1097 _Ptr_type _M_storage;
1101 : _M_future(std::make_shared<_State>()),
1102 _M_storage(
new _Res_type())
1106 : _M_future(
std::move(__rhs._M_future)),
1110 template<
typename _Allocator>
1111 promise(allocator_arg_t,
const _Allocator& __a)
1112 : _M_future(std::allocate_shared<_State>(__a)),
1113 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1116 template<
typename _Allocator>
1118 : _M_future(
std::move(__rhs._M_future)),
1126 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1127 _M_future->_M_break_promise(
std::move(_M_storage));
1132 operator=(
promise&& __rhs)
noexcept
1143 _M_future.
swap(__rhs._M_future);
1144 _M_storage.swap(__rhs._M_storage);
1154 set_value(
const _Res& __r)
1155 { _M_state()._M_set_result(_State::__setter(
this, __r)); }
1158 set_value(_Res&& __r)
1159 { _M_state()._M_set_result(_State::__setter(
this,
std::move(__r))); }
1163 { _M_state()._M_set_result(_State::__setter(__p,
this)); }
1166 set_value_at_thread_exit(
const _Res& __r)
1168 _M_state()._M_set_delayed_result(_State::__setter(
this, __r),
1173 set_value_at_thread_exit(_Res&& __r)
1175 _M_state()._M_set_delayed_result(
1176 _State::__setter(
this,
std::move(__r)), _M_future);
1182 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1190 __future_base::_State_base::_S_check(_M_future);
1195 template<
typename _Res>
1200 template<
typename _Res,
typename _Alloc>
1201 struct uses_allocator<promise<_Res>, _Alloc>
1202 :
public true_type { };
1206 template<
typename _Res>
1209 typedef __future_base::_State_base _State;
1210 typedef __future_base::_Result<_Res&> _Res_type;
1211 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1212 template<
typename,
typename>
friend struct _State::_Setter;
1216 _Ptr_type _M_storage;
1220 : _M_future(std::make_shared<_State>()),
1221 _M_storage(
new _Res_type())
1225 : _M_future(
std::move(__rhs._M_future)),
1229 template<
typename _Allocator>
1230 promise(allocator_arg_t,
const _Allocator& __a)
1231 : _M_future(std::allocate_shared<_State>(__a)),
1232 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1235 template<
typename _Allocator>
1237 : _M_future(
std::move(__rhs._M_future)),
1245 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1246 _M_future->_M_break_promise(
std::move(_M_storage));
1251 operator=(
promise&& __rhs)
noexcept
1262 _M_future.
swap(__rhs._M_future);
1263 _M_storage.swap(__rhs._M_storage);
1273 set_value(_Res& __r)
1274 { _M_state()._M_set_result(_State::__setter(
this, __r)); }
1278 { _M_state()._M_set_result(_State::__setter(__p,
this)); }
1281 set_value_at_thread_exit(_Res& __r)
1283 _M_state()._M_set_delayed_result(_State::__setter(
this, __r),
1290 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1298 __future_base::_State_base::_S_check(_M_future);
1307 typedef __future_base::_State_base _State;
1308 typedef __future_base::_Result<void> _Res_type;
1309 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1310 template<
typename,
typename>
friend struct _State::_Setter;
1314 _Ptr_type _M_storage;
1318 : _M_future(std::make_shared<_State>()),
1319 _M_storage(
new _Res_type())
1323 : _M_future(
std::move(__rhs._M_future)),
1327 template<
typename _Allocator>
1328 promise(allocator_arg_t,
const _Allocator& __a)
1329 : _M_future(std::allocate_shared<_State>(__a)),
1330 _M_storage(__future_base::_S_allocate_result<void>(__a))
1335 template<
typename _Allocator>
1337 : _M_future(
std::move(__rhs._M_future)),
1345 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1346 _M_future->_M_break_promise(
std::move(_M_storage));
1351 operator=(
promise&& __rhs)
noexcept
1362 _M_future.
swap(__rhs._M_future);
1363 _M_storage.swap(__rhs._M_storage);
1374 { _M_state()._M_set_result(_State::__setter(
this)); }
1378 { _M_state()._M_set_result(_State::__setter(__p,
this)); }
1381 set_value_at_thread_exit()
1382 { _M_state()._M_set_delayed_result(_State::__setter(
this), _M_future); }
1387 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1395 __future_base::_State_base::_S_check(_M_future);
1401 template<
typename _Ptr_type,
typename _Fn,
typename _Res>
1402 struct __future_base::_Task_setter
1405 _Ptr_type operator()()
const
1409 (*_M_result)->_M_set((*_M_fn)());
1413 __throw_exception_again;
1421 _Ptr_type* _M_result;
1425 template<
typename _Ptr_type,
typename _Fn>
1426 struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1428 _Ptr_type operator()()
const
1436 __throw_exception_again;
1444 _Ptr_type* _M_result;
1449 template<
typename _Res,
typename... _Args>
1450 struct __future_base::_Task_state_base<_Res(_Args...)>
1451 : __future_base::_State_base
1453 typedef _Res _Res_type;
1455 template<
typename _Alloc>
1456 _Task_state_base(
const _Alloc& __a)
1457 : _M_result(_S_allocate_result<_Res>(__a))
1462 _M_run(_Args&&... __args) = 0;
1466 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1468 virtual shared_ptr<_Task_state_base>
1471 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1472 _Ptr_type _M_result;
1476 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1477 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1478 : __future_base::_Task_state_base<_Res(_Args...)>
1480 template<
typename _Fn2>
1481 _Task_state(_Fn2&& __fn,
const _Alloc& __a)
1482 : _Task_state_base<_Res(_Args...)>(__a),
1488 _M_run(_Args&&... __args)
1490 auto __boundfn = [&] () -> _Res {
1491 return std::__invoke_r<_Res>(_M_impl._M_fn,
1492 std::forward<_Args>(__args)...);
1494 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1498 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1500 auto __boundfn = [&] () -> _Res {
1501 return std::__invoke_r<_Res>(_M_impl._M_fn,
1502 std::forward<_Args>(__args)...);
1504 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1508 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1511 struct _Impl : _Alloc
1513 template<
typename _Fn2>
1514 _Impl(_Fn2&& __fn,
const _Alloc& __a)
1515 : _Alloc(__a), _M_fn(
std::
forward<_Fn2>(__fn)) { }
1520 template<
typename _Signature,
typename _Fn,
1522 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1523 __create_task_state(_Fn&& __fn,
const _Alloc& __a = _Alloc())
1525 typedef typename decay<_Fn>::type _Fn2;
1526 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1527 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1530 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1531 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1532 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1534 return __create_task_state<_Res(_Args...)>(
std::move(_M_impl._M_fn),
1535 static_cast<_Alloc&
>(_M_impl));
1540 template<
typename _Res,
typename... _ArgTypes>
1541 class packaged_task<_Res(_ArgTypes...)>
1543 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1548 template<
typename _Fn,
typename _Fn2 = __remove_cvref_t<_Fn>>
1554 packaged_task()
noexcept { }
1556 template<
typename _Fn,
typename = __not_same<_Fn>>
1558 packaged_task(_Fn&& __fn)
1560 __create_task_state<_Res(_ArgTypes...)>(std::forward<_Fn>(__fn)))
1563#if __cplusplus < 201703L
1568 template<
typename _Fn,
typename _Alloc,
typename = __not_same<_Fn>>
1569 packaged_task(allocator_arg_t,
const _Alloc& __a, _Fn&& __fn)
1570 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1571 std::forward<_Fn>(__fn), __a))
1576 template<
typename _Allocator>
1577 packaged_task(allocator_arg_t,
const _Allocator&)
noexcept
1580 template<
typename _Allocator>
1581 packaged_task(allocator_arg_t,
const _Allocator&,
1582 const packaged_task&) =
delete;
1584 template<
typename _Allocator>
1585 packaged_task(allocator_arg_t,
const _Allocator&,
1586 packaged_task&& __other)
noexcept
1587 { this->swap(__other); }
1592 if (
static_cast<bool>(_M_state) && !_M_state.unique())
1593 _M_state->_M_break_promise(
std::move(_M_state->_M_result));
1597 packaged_task(
const packaged_task&) =
delete;
1598 packaged_task& operator=(
const packaged_task&) =
delete;
1601 packaged_task(packaged_task&& __other)
noexcept
1602 { this->swap(__other); }
1604 packaged_task& operator=(packaged_task&& __other)
noexcept
1606 packaged_task(
std::move(__other)).swap(*
this);
1611 swap(packaged_task& __other)
noexcept
1612 { _M_state.
swap(__other._M_state); }
1615 valid()
const noexcept
1616 {
return static_cast<bool>(_M_state); }
1625 operator()(_ArgTypes... __args)
1627 __future_base::_State_base::_S_check(_M_state);
1628 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1632 make_ready_at_thread_exit(_ArgTypes... __args)
1634 __future_base::_State_base::_S_check(_M_state);
1635 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1641 __future_base::_State_base::_S_check(_M_state);
1642 packaged_task __tmp;
1643 __tmp._M_state = _M_state;
1644 _M_state = _M_state->_M_reset();
1650#if __cpp_deduction_guides >= 201606
1651 template<
typename _Res,
typename... _ArgTypes>
1652 packaged_task(_Res(*)(_ArgTypes...)) -> packaged_task<_Res(_ArgTypes...)>;
1654 template<
typename _Fun,
typename _Signature
1655 = __function_guide_t<_Fun,
decltype(&_Fun::operator())>>
1656 packaged_task(_Fun) -> packaged_task<_Signature>;
1660 template<
typename _Res,
typename... _ArgTypes>
1662 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1663 packaged_task<_Res(_ArgTypes...)>& __y)
noexcept
1666#if __cplusplus < 201703L
1669 template<
typename _Res,
typename _Alloc>
1670 struct uses_allocator<packaged_task<_Res>, _Alloc>
1671 :
public true_type { };
1678 template<
typename _BoundFn,
typename _Res>
1679 class __future_base::_Deferred_state final
1680 :
public __future_base::_State_base
1683 template<
typename... _Args>
1685 _Deferred_state(_Args&&... __args)
1686 : _M_result(new _Result<_Res>()),
1691 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1692 _Ptr_type _M_result;
1705 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1710 virtual bool _M_is_deferred_future()
const {
return true; }
1714 class __future_base::_Async_state_commonV2
1715 :
public __future_base::_State_base
1718 ~_Async_state_commonV2() =
default;
1735 virtual void _M_complete_async() { _M_join(); }
1737 void _M_join() {
std::call_once(_M_once, &thread::join, &_M_thread); }
1745 template<
typename _BoundFn,
typename _Res>
1746 class __future_base::_Async_state_impl final
1747 :
public __future_base::_Async_state_commonV2
1750 template<
typename... _Args>
1752 _Async_state_impl(_Args&&... __args)
1753 : _M_result(new _Result<_Res>()),
1756 _M_thread =
std::thread{&_Async_state_impl::_M_run,
this};
1762 ~_Async_state_impl()
1764 if (_M_thread.joinable())
1774 _M_set_result(_S_task_setter(_M_result, _M_fn));
1779 if (
static_cast<bool>(_M_result))
1780 this->_M_break_promise(
std::move(_M_result));
1781 __throw_exception_again;
1785 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1786 _Ptr_type _M_result;
1792 template<
typename _Fn,
typename... _Args>
1793 _GLIBCXX_NODISCARD future<__async_result_of<_Fn, _Args...>>
1796 using _Wr = std::thread::_Call_wrapper<_Fn, _Args...>;
1797 using _As = __future_base::_Async_state_impl<_Wr>;
1798 using _Ds = __future_base::_Deferred_state<_Wr>;
1801 if ((__policy & launch::async) == launch::async)
1805 __state = std::make_shared<_As>(std::forward<_Fn>(__fn),
1806 std::forward<_Args>(__args)...);
1811 if (__e.code() != errc::resource_unavailable_try_again
1812 || (__policy & launch::deferred) != launch::deferred)
1819 __state = std::make_shared<_Ds>(std::forward<_Fn>(__fn),
1820 std::forward<_Args>(__args)...);
1826 template<
typename _Fn,
typename... _Args>
1827 _GLIBCXX_NODISCARD
inline future<__async_result_of<_Fn, _Args...>>
1830 return std::async(launch::async|launch::deferred,
1831 std::forward<_Fn>(__fn),
1832 std::forward<_Args>(__args)...);
1839_GLIBCXX_END_NAMESPACE_VERSION
const error_category & future_category() noexcept
Points to a statically-allocated object derived from error_category.
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.
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.
__bool_constant< 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.
constexpr bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
constexpr 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.
shared_future(const shared_future &__sf) noexcept
Copy constructor.
shared_future(future< _Res > &&__uf) noexcept
Construct from a future rvalue.
const _Res & get() const
Retrieving the value.
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.
future(future &&__uf) noexcept
Move constructor.
_Res & get()
Retrieving the value.
void get()
Retrieving the value.
future(future &&__uf) noexcept
Move constructor.
shared_future(future< _Res & > &&__uf) noexcept
Construct from a future rvalue.
_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.
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.
A smart pointer with reference-counted copy semantics.