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>
158 return static_cast<launch>(
159 static_cast<int>(__x) &
static_cast<int>(__y));
165 return static_cast<launch>(
166 static_cast<int>(__x) |
static_cast<int>(__y));
172 return static_cast<launch>(
173 static_cast<int>(__x) ^
static_cast<int>(__y));
178 {
return static_cast<launch>(~static_cast<int>(__x)); }
182 {
return __x = __x & __y; }
186 {
return __x = __x | __y; }
190 {
return __x = __x ^ __y; }
203 template<
typename _Fn,
typename... _Args>
204 using __async_result_of =
typename __invoke_result<
205 typename decay<_Fn>::type,
typename decay<_Args>::type...>::type;
208 template<
typename _Fn,
typename... _Args>
209 future<__async_result_of<_Fn, _Args...>>
210 async(
launch __policy, _Fn&& __fn, _Args&&... __args);
212 template<
typename _Fn,
typename... _Args>
213 future<__async_result_of<_Fn, _Args...>>
214 async(_Fn&& __fn, _Args&&... __args);
216#if defined(_GLIBCXX_HAS_GTHREADS)
226 exception_ptr _M_error;
228 _Result_base(
const _Result_base&) =
delete;
229 _Result_base& operator=(
const _Result_base&) =
delete;
232 virtual void _M_destroy() = 0;
236 void operator()(_Result_base* __fr)
const { __fr->_M_destroy(); }
241 virtual ~_Result_base();
245 template<
typename _Res>
246 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
249 template<
typename _Res>
250 struct _Result : _Result_base
253 __gnu_cxx::__aligned_buffer<_Res> _M_storage;
257 typedef _Res result_type;
259 _Result() noexcept : _M_initialized() { }
269 _M_value() noexcept {
return *_M_storage._M_ptr(); }
272 _M_set(
const _Res& __res)
274 ::new (_M_storage._M_addr()) _Res(__res);
275 _M_initialized = true;
281 ::new (_M_storage._M_addr()) _Res(
std::move(__res));
282 _M_initialized = true;
286 void _M_destroy() {
delete this; }
290 template<
typename _Res,
typename _Alloc>
291 struct _Result_alloc final : _Result<_Res>, _Alloc
293 using __allocator_type = __alloc_rebind<_Alloc, _Result_alloc>;
296 _Result_alloc(
const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
302 __allocator_type __a(*
this);
303 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
304 this->~_Result_alloc();
309 template<
typename _Res,
typename _Allocator>
310 static _Ptr<_Result_alloc<_Res, _Allocator>>
311 _S_allocate_result(
const _Allocator& __a)
313 using __result_type = _Result_alloc<_Res, _Allocator>;
314 typename __result_type::__allocator_type __a2(__a);
315 auto __guard = std::__allocate_guarded(__a2);
316 __result_type* __p = ::new((
void*)__guard.get()) __result_type{__a};
318 return _Ptr<__result_type>(__p);
322 template<
typename _Res,
typename _Tp>
323 static _Ptr<_Result<_Res>>
326 return _Ptr<_Result<_Res>>(
new _Result<_Res>);
334 typedef _Ptr<_Result_base> _Ptr_type;
336 enum _Status :
unsigned {
342 __atomic_futex_unsigned<> _M_status;
343 atomic_flag _M_retrieved = ATOMIC_FLAG_INIT;
347 _State_baseV2() noexcept : _M_result(), _M_status(_Status::__not_ready)
349 _State_baseV2(
const _State_baseV2&) =
delete;
350 _State_baseV2& operator=(
const _State_baseV2&) =
delete;
351 virtual ~_State_baseV2() =
default;
360 _M_status._M_load_when_equal(_Status::__ready, memory_order_acquire);
364 template<
typename _Rep,
typename _Period>
366 wait_for(
const chrono::duration<_Rep, _Period>& __rel)
370 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
371 return future_status::ready;
373 if (_M_is_deferred_future())
374 return future_status::deferred;
377 if (__rel > __rel.zero()
378 && _M_status._M_load_when_equal_for(_Status::__ready,
379 memory_order_acquire,
393 return future_status::ready;
395 return future_status::timeout;
398 template<
typename _Clock,
typename _Duration>
400 wait_until(
const chrono::time_point<_Clock, _Duration>& __abs)
402#if __cplusplus > 201703L
403 static_assert(chrono::is_clock_v<_Clock>);
407 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
408 return future_status::ready;
410 if (_M_is_deferred_future())
411 return future_status::deferred;
413 if (_M_status._M_load_when_equal_until(_Status::__ready,
414 memory_order_acquire,
422 return future_status::ready;
424 return future_status::timeout;
430 _M_set_result(function<_Ptr_type()> __res,
bool __ignore_failure =
false)
432 bool __did_set =
false;
435 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
439 _M_status._M_store_notify_all(_Status::__ready,
440 memory_order_release);
441 else if (!__ignore_failure)
442 __throw_future_error(
int(future_errc::promise_already_satisfied));
449 _M_set_delayed_result(function<_Ptr_type()> __res,
450 weak_ptr<_State_baseV2> __self)
452 bool __did_set =
false;
453 unique_ptr<_Make_ready> __mr{
new _Make_ready};
456 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
459 __throw_future_error(
int(future_errc::promise_already_satisfied));
460 __mr->_M_shared_state =
std::move(__self);
467 _M_break_promise(_Ptr_type __res)
469 if (
static_cast<bool>(__res))
477 _M_result.swap(__res);
479 _M_status._M_store_notify_all(_Status::__ready,
480 memory_order_release);
486 _M_set_retrieved_flag()
488 if (_M_retrieved.test_and_set())
489 __throw_future_error(
int(future_errc::future_already_retrieved));
492 template<
typename _Res,
typename _Arg>
496 template<
typename _Res,
typename _Arg>
497 struct _Setter<_Res, _Arg&>
501 static_assert(is_same<_Res, _Arg&>::value
502 || is_same<const _Res, _Arg>::value,
503 "Invalid specialisation");
506 typename promise<_Res>::_Ptr_type operator()()
const
508 _M_promise->_M_storage->_M_set(*_M_arg);
509 return std::move(_M_promise->_M_storage);
511 promise<_Res>* _M_promise;
516 template<
typename _Res>
517 struct _Setter<_Res, _Res&&>
520 typename promise<_Res>::_Ptr_type operator()()
const
522 _M_promise->_M_storage->_M_set(
std::move(*_M_arg));
523 return std::move(_M_promise->_M_storage);
525 promise<_Res>* _M_promise;
530 template<
typename _Res>
531 struct _Setter<_Res, void>
533 static_assert(is_void<_Res>::value,
"Only used for promise<void>");
535 typename promise<_Res>::_Ptr_type operator()()
const
536 {
return std::move(_M_promise->_M_storage); }
538 promise<_Res>* _M_promise;
541 struct __exception_ptr_tag { };
544 template<
typename _Res>
545 struct _Setter<_Res, __exception_ptr_tag>
548 typename promise<_Res>::_Ptr_type operator()()
const
550 _M_promise->_M_storage->_M_error = *_M_ex;
551 return std::move(_M_promise->_M_storage);
554 promise<_Res>* _M_promise;
555 exception_ptr* _M_ex;
558 template<
typename _Res,
typename _Arg>
559 __attribute__((__always_inline__))
560 static _Setter<_Res, _Arg&&>
561 __setter(promise<_Res>* __prom, _Arg&& __arg)
noexcept
566 template<
typename _Res>
567 __attribute__((__always_inline__))
568 static _Setter<_Res, __exception_ptr_tag>
569 __setter(exception_ptr& __ex, promise<_Res>* __prom)
noexcept
571 __glibcxx_assert(__ex !=
nullptr);
572 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
575 template<
typename _Res>
576 __attribute__((__always_inline__))
577 static _Setter<_Res, void>
578 __setter(promise<_Res>* __prom)
noexcept
580 return _Setter<_Res, void>{ __prom };
583 template<
typename _Tp>
585 _S_check(
const shared_ptr<_Tp>& __p)
587 if (!
static_cast<bool>(__p))
588 __throw_future_error((
int)future_errc::no_state);
594 _M_do_set(function<_Ptr_type()>* __f,
bool* __did_set)
596 _Ptr_type __res = (*__f)();
601 _M_result.swap(__res);
605 virtual void _M_complete_async() { }
608 virtual bool _M_is_deferred_future()
const {
return false; }
610 struct _Make_ready final : __at_thread_exit_elt
612 weak_ptr<_State_baseV2> _M_shared_state;
613 static void _S_run(
void*);
618#ifdef _GLIBCXX_ASYNC_ABI_COMPAT
620 class _Async_state_common;
622 using _State_base = _State_baseV2;
623 class _Async_state_commonV2;
626 template<
typename _BoundFn,
627 typename _Res =
decltype(std::declval<_BoundFn&>()())>
628 class _Deferred_state;
630 template<
typename _BoundFn,
631 typename _Res =
decltype(std::declval<_BoundFn&>()())>
632 class _Async_state_impl;
634 template<
typename _Signature>
635 struct _Task_state_base;
637 template<
typename _Fn,
typename _Alloc,
typename _Signature>
640 template<
typename _Res_ptr,
typename _Fn,
641 typename _Res =
typename _Res_ptr::element_type::result_type>
644 template<
typename _Res_ptr,
typename _BoundFn>
645 static _Task_setter<_Res_ptr, _BoundFn>
646 _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
653 template<
typename _Res>
654 struct __future_base::_Result<_Res&> : __future_base::_Result_base
656 typedef _Res& result_type;
658 _Result() noexcept : _M_value_ptr() { }
661 _M_set(_Res& __res)
noexcept
664 _Res& _M_get() noexcept {
return *_M_value_ptr; }
669 void _M_destroy() {
delete this; }
674 struct __future_base::_Result<void> : __future_base::_Result_base
676 typedef void result_type;
679 void _M_destroy() {
delete this; }
684#ifndef _GLIBCXX_ASYNC_ABI_COMPAT
688 template<
typename _Res,
typename _Arg>
689 struct __is_location_invariant
690 <__future_base::_State_base::_Setter<_Res, _Arg>>
694 template<
typename _Res_ptr,
typename _Fn,
typename _Res>
695 struct __is_location_invariant
696 <__future_base::_Task_setter<_Res_ptr, _Fn, _Res>>
701 template<
typename _Res>
706 typedef __future_base::_Result<_Res>& __result_type;
717 valid()
const noexcept {
return static_cast<bool>(_M_state); }
722 _State_base::_S_check(_M_state);
726 template<
typename _Rep,
typename _Period>
730 _State_base::_S_check(_M_state);
731 return _M_state->wait_for(__rel);
734 template<
typename _Clock,
typename _Duration>
738 _State_base::_S_check(_M_state);
739 return _M_state->wait_until(__abs);
747 _State_base::_S_check(_M_state);
748 _Result_base& __res = _M_state->wait();
749 if (!(__res._M_error ==
nullptr))
751 return static_cast<__result_type
>(__res);
756 _M_state.
swap(__that._M_state);
761 __basic_future(
const __state_type& __state) : _M_state(__state)
763 _State_base::_S_check(_M_state);
764 _M_state->_M_set_retrieved_flag();
769 __basic_future(
const shared_future<_Res>&)
noexcept;
773 __basic_future(shared_future<_Res>&&) noexcept;
777 __basic_future(future<_Res>&&) noexcept;
779 constexpr __basic_future() noexcept : _M_state() { }
783 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
784 ~_Reset() { _M_fut._M_state.reset(); }
785 __basic_future& _M_fut;
791 template<
typename _Res>
796 static_assert(!
is_array<_Res>{},
"result type must not be an array");
799 "result type must be destructible");
802 template<
typename>
friend class packaged_task;
803 template<
typename _Fn,
typename... _Args>
804 friend future<__async_result_of<_Fn, _Args...>>
808 typedef typename _Base_type::__state_type __state_type;
811 future(
const __state_type& __state) : _Base_type(__state) { }
814 constexpr future() noexcept : _Base_type() { }
833 typename _Base_type::_Reset __reset(*
this);
834 return std::move(this->_M_get_result()._M_value());
841 template<typename _Res>
845 template<
typename>
friend class packaged_task;
846 template<
typename _Fn,
typename... _Args>
847 friend future<__async_result_of<_Fn, _Args...>>
876 typename _Base_type::_Reset __reset(*
this);
877 return this->_M_get_result()._M_get();
888 template<
typename>
friend class packaged_task;
889 template<
typename _Fn,
typename... _Args>
890 friend future<__async_result_of<_Fn, _Args...>>
919 typename _Base_type::_Reset __reset(*
this);
920 this->_M_get_result();
928 template<typename _Res>
933 static_assert(!
is_array<_Res>{},
"result type must not be an array");
936 "result type must be destructible");
962 shared_future& operator=(shared_future&& __sf)
noexcept
964 shared_future(
std::move(__sf))._M_swap(*
this);
970 get()
const {
return this->_M_get_result()._M_value(); }
974 template<
typename _Res>
1001 shared_future& operator=(shared_future&& __sf)
noexcept
1003 shared_future(
std::move(__sf))._M_swap(*
this);
1009 get()
const {
return this->_M_get_result()._M_get(); }
1040 shared_future& operator=(shared_future&& __sf)
noexcept
1042 shared_future(
std::move(__sf))._M_swap(*
this);
1048 get()
const { this->_M_get_result(); }
1052 template<
typename _Res>
1053 inline __basic_future<_Res>::
1054 __basic_future(
const shared_future<_Res>& __sf) noexcept
1055 : _M_state(__sf._M_state)
1058 template<
typename _Res>
1059 inline __basic_future<_Res>::
1060 __basic_future(shared_future<_Res>&& __sf) noexcept
1064 template<
typename _Res>
1065 inline __basic_future<_Res>::
1066 __basic_future(future<_Res>&& __uf) noexcept
1072 template<
typename _Res>
1073 inline shared_future<_Res>
1074 future<_Res>::share() noexcept
1075 {
return shared_future<_Res>(
std::move(*
this)); }
1077 template<
typename _Res>
1078 inline shared_future<_Res&>
1079 future<_Res&>::share() noexcept
1080 {
return shared_future<_Res&>(
std::move(*
this)); }
1082 inline shared_future<void>
1083 future<void>::share() noexcept
1084 {
return shared_future<void>(
std::move(*
this)); }
1087 template<
typename _Res>
1092 static_assert(!
is_array<_Res>{},
"result type must not be an array");
1095 "result type must be destructible");
1097 typedef __future_base::_State_base _State;
1098 typedef __future_base::_Result<_Res> _Res_type;
1099 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1100 template<
typename,
typename>
friend struct _State::_Setter;
1104 _Ptr_type _M_storage;
1108 : _M_future(std::make_shared<_State>()),
1109 _M_storage(
new _Res_type())
1113 : _M_future(
std::move(__rhs._M_future)),
1117 template<
typename _Allocator>
1118 promise(allocator_arg_t,
const _Allocator& __a)
1119 : _M_future(std::allocate_shared<_State>(__a)),
1120 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1123 template<
typename _Allocator>
1125 : _M_future(
std::move(__rhs._M_future)),
1133 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1134 _M_future->_M_break_promise(
std::move(_M_storage));
1139 operator=(
promise&& __rhs)
noexcept
1150 _M_future.
swap(__rhs._M_future);
1151 _M_storage.swap(__rhs._M_storage);
1161 set_value(
const _Res& __r)
1162 { _M_state()._M_set_result(_State::__setter(
this, __r)); }
1165 set_value(_Res&& __r)
1166 { _M_state()._M_set_result(_State::__setter(
this,
std::move(__r))); }
1170 { _M_state()._M_set_result(_State::__setter(__p,
this)); }
1173 set_value_at_thread_exit(
const _Res& __r)
1175 _M_state()._M_set_delayed_result(_State::__setter(
this, __r),
1180 set_value_at_thread_exit(_Res&& __r)
1182 _M_state()._M_set_delayed_result(
1183 _State::__setter(
this,
std::move(__r)), _M_future);
1189 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1197 __future_base::_State_base::_S_check(_M_future);
1202 template<
typename _Res>
1207 template<
typename _Res,
typename _Alloc>
1208 struct uses_allocator<promise<_Res>, _Alloc>
1209 :
public true_type { };
1213 template<
typename _Res>
1216 typedef __future_base::_State_base _State;
1217 typedef __future_base::_Result<_Res&> _Res_type;
1218 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1219 template<
typename,
typename>
friend struct _State::_Setter;
1223 _Ptr_type _M_storage;
1227 : _M_future(std::make_shared<_State>()),
1228 _M_storage(
new _Res_type())
1232 : _M_future(
std::move(__rhs._M_future)),
1236 template<
typename _Allocator>
1237 promise(allocator_arg_t,
const _Allocator& __a)
1238 : _M_future(std::allocate_shared<_State>(__a)),
1239 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1242 template<
typename _Allocator>
1244 : _M_future(
std::move(__rhs._M_future)),
1252 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1253 _M_future->_M_break_promise(
std::move(_M_storage));
1258 operator=(
promise&& __rhs)
noexcept
1269 _M_future.
swap(__rhs._M_future);
1270 _M_storage.swap(__rhs._M_storage);
1280 set_value(_Res& __r)
1281 { _M_state()._M_set_result(_State::__setter(
this, __r)); }
1285 { _M_state()._M_set_result(_State::__setter(__p,
this)); }
1288 set_value_at_thread_exit(_Res& __r)
1290 _M_state()._M_set_delayed_result(_State::__setter(
this, __r),
1297 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1305 __future_base::_State_base::_S_check(_M_future);
1314 typedef __future_base::_State_base _State;
1315 typedef __future_base::_Result<void> _Res_type;
1316 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1317 template<
typename,
typename>
friend struct _State::_Setter;
1321 _Ptr_type _M_storage;
1325 : _M_future(std::make_shared<_State>()),
1326 _M_storage(
new _Res_type())
1330 : _M_future(
std::move(__rhs._M_future)),
1334 template<
typename _Allocator>
1335 promise(allocator_arg_t,
const _Allocator& __a)
1336 : _M_future(std::allocate_shared<_State>(__a)),
1337 _M_storage(__future_base::_S_allocate_result<void>(__a))
1342 template<
typename _Allocator>
1344 : _M_future(
std::move(__rhs._M_future)),
1352 if (
static_cast<bool>(_M_future) && !_M_future.unique())
1353 _M_future->_M_break_promise(
std::move(_M_storage));
1358 operator=(
promise&& __rhs)
noexcept
1369 _M_future.
swap(__rhs._M_future);
1370 _M_storage.swap(__rhs._M_storage);
1381 { _M_state()._M_set_result(_State::__setter(
this)); }
1385 { _M_state()._M_set_result(_State::__setter(__p,
this)); }
1388 set_value_at_thread_exit()
1389 { _M_state()._M_set_delayed_result(_State::__setter(
this), _M_future); }
1394 _M_state()._M_set_delayed_result(_State::__setter(__p,
this),
1402 __future_base::_State_base::_S_check(_M_future);
1408 template<
typename _Ptr_type,
typename _Fn,
typename _Res>
1409 struct __future_base::_Task_setter
1412 _Ptr_type operator()()
const
1416 (*_M_result)->_M_set((*_M_fn)());
1420 __throw_exception_again;
1428 _Ptr_type* _M_result;
1432 template<
typename _Ptr_type,
typename _Fn>
1433 struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1435 _Ptr_type operator()()
const
1443 __throw_exception_again;
1451 _Ptr_type* _M_result;
1456 template<
typename _Res,
typename... _Args>
1457 struct __future_base::_Task_state_base<_Res(_Args...)>
1458 : __future_base::_State_base
1460 typedef _Res _Res_type;
1462 template<
typename _Alloc>
1463 _Task_state_base(
const _Alloc& __a)
1464 : _M_result(_S_allocate_result<_Res>(__a))
1469 _M_run(_Args&&... __args) = 0;
1473 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1475 virtual shared_ptr<_Task_state_base>
1478 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1479 _Ptr_type _M_result;
1483 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1484 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1485 : __future_base::_Task_state_base<_Res(_Args...)>
1487 template<
typename _Fn2>
1488 _Task_state(_Fn2&& __fn,
const _Alloc& __a)
1489 : _Task_state_base<_Res(_Args...)>(__a),
1495 _M_run(_Args&&... __args)
1497 auto __boundfn = [&] () -> _Res {
1498 return std::__invoke_r<_Res>(_M_impl._M_fn,
1499 std::forward<_Args>(__args)...);
1501 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1505 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1507 auto __boundfn = [&] () -> _Res {
1508 return std::__invoke_r<_Res>(_M_impl._M_fn,
1509 std::forward<_Args>(__args)...);
1511 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1515 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1518 struct _Impl : _Alloc
1520 template<
typename _Fn2>
1521 _Impl(_Fn2&& __fn,
const _Alloc& __a)
1522 : _Alloc(__a), _M_fn(
std::
forward<_Fn2>(__fn)) { }
1527 template<
typename _Signature,
typename _Fn,
1529 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1530 __create_task_state(_Fn&& __fn,
const _Alloc& __a = _Alloc())
1532 typedef typename decay<_Fn>::type _Fn2;
1533 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1534 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1537 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1538 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1539 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1541 return __create_task_state<_Res(_Args...)>(
std::move(_M_impl._M_fn),
1542 static_cast<_Alloc&
>(_M_impl));
1547 template<
typename _Res,
typename... _ArgTypes>
1548 class packaged_task<_Res(_ArgTypes...)>
1550 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1555 template<
typename _Fn,
typename _Fn2 = __remove_cvref_t<_Fn>>
1561 packaged_task()
noexcept { }
1563 template<
typename _Fn,
typename = __not_same<_Fn>>
1565 packaged_task(_Fn&& __fn)
1567 __create_task_state<_Res(_ArgTypes...)>(std::forward<_Fn>(__fn)))
1570#if __cplusplus < 201703L
1575 template<
typename _Fn,
typename _Alloc,
typename = __not_same<_Fn>>
1576 packaged_task(allocator_arg_t,
const _Alloc& __a, _Fn&& __fn)
1577 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1578 std::forward<_Fn>(__fn), __a))
1583 template<
typename _Allocator>
1584 packaged_task(allocator_arg_t,
const _Allocator&)
noexcept
1587 template<
typename _Allocator>
1588 packaged_task(allocator_arg_t,
const _Allocator&,
1589 const packaged_task&) =
delete;
1591 template<
typename _Allocator>
1592 packaged_task(allocator_arg_t,
const _Allocator&,
1593 packaged_task&& __other)
noexcept
1594 { this->swap(__other); }
1599 if (
static_cast<bool>(_M_state) && !_M_state.unique())
1600 _M_state->_M_break_promise(
std::move(_M_state->_M_result));
1604 packaged_task(
const packaged_task&) =
delete;
1605 packaged_task& operator=(
const packaged_task&) =
delete;
1608 packaged_task(packaged_task&& __other)
noexcept
1609 { this->swap(__other); }
1611 packaged_task& operator=(packaged_task&& __other)
noexcept
1613 packaged_task(
std::move(__other)).swap(*
this);
1618 swap(packaged_task& __other)
noexcept
1619 { _M_state.
swap(__other._M_state); }
1622 valid()
const noexcept
1623 {
return static_cast<bool>(_M_state); }
1632 operator()(_ArgTypes... __args)
1634 __future_base::_State_base::_S_check(_M_state);
1635 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1639 make_ready_at_thread_exit(_ArgTypes... __args)
1641 __future_base::_State_base::_S_check(_M_state);
1642 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1648 __future_base::_State_base::_S_check(_M_state);
1649 packaged_task __tmp;
1650 __tmp._M_state = _M_state;
1651 _M_state = _M_state->_M_reset();
1657#if __cpp_deduction_guides >= 201606
1658 template<
typename _Res,
typename... _ArgTypes>
1659 packaged_task(_Res(*)(_ArgTypes...)) -> packaged_task<_Res(_ArgTypes...)>;
1661 template<
typename _Fun,
typename _Signature
1662 = __function_guide_t<_Fun,
decltype(&_Fun::operator())>>
1663 packaged_task(_Fun) -> packaged_task<_Signature>;
1667 template<
typename _Res,
typename... _ArgTypes>
1669 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1670 packaged_task<_Res(_ArgTypes...)>& __y)
noexcept
1673#if __cplusplus < 201703L
1676 template<
typename _Res,
typename _Alloc>
1677 struct uses_allocator<packaged_task<_Res>, _Alloc>
1678 :
public true_type { };
1685 template<
typename _BoundFn,
typename _Res>
1686 class __future_base::_Deferred_state final
1687 :
public __future_base::_State_base
1690 template<
typename... _Args>
1692 _Deferred_state(_Args&&... __args)
1693 : _M_result(new _Result<_Res>()),
1698 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1699 _Ptr_type _M_result;
1712 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1717 virtual bool _M_is_deferred_future()
const {
return true; }
1721 class __future_base::_Async_state_commonV2
1722 :
public __future_base::_State_base
1725 ~_Async_state_commonV2() =
default;
1742 virtual void _M_complete_async() { _M_join(); }
1744 void _M_join() {
std::call_once(_M_once, &thread::join, &_M_thread); }
1752 template<
typename _BoundFn,
typename _Res>
1753 class __future_base::_Async_state_impl final
1754 :
public __future_base::_Async_state_commonV2
1757 template<
typename... _Args>
1759 _Async_state_impl(_Args&&... __args)
1760 : _M_result(new _Result<_Res>()),
1763 _M_thread =
std::thread{&_Async_state_impl::_M_run,
this};
1769 ~_Async_state_impl()
1771 if (_M_thread.joinable())
1781 _M_set_result(_S_task_setter(_M_result, _M_fn));
1786 if (
static_cast<bool>(_M_result))
1787 this->_M_break_promise(
std::move(_M_result));
1788 __throw_exception_again;
1792 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1793 _Ptr_type _M_result;
1799 template<
typename _Fn,
typename... _Args>
1800 _GLIBCXX_NODISCARD future<__async_result_of<_Fn, _Args...>>
1803 using _Wr = std::thread::_Call_wrapper<_Fn, _Args...>;
1804 using _As = __future_base::_Async_state_impl<_Wr>;
1805 using _Ds = __future_base::_Deferred_state<_Wr>;
1808 if ((__policy & launch::async) == launch::async)
1812 __state = std::make_shared<_As>(std::forward<_Fn>(__fn),
1813 std::forward<_Args>(__args)...);
1818 if (__e.code() != errc::resource_unavailable_try_again
1819 || (__policy & launch::deferred) != launch::deferred)
1826 __state = std::make_shared<_Ds>(std::forward<_Fn>(__fn),
1827 std::forward<_Args>(__args)...);
1833 template<
typename _Fn,
typename... _Args>
1834 _GLIBCXX_NODISCARD
inline future<__async_result_of<_Fn, _Args...>>
1837 return std::async(launch::async|launch::deferred,
1838 std::forward<_Fn>(__fn),
1839 std::forward<_Args>(__args)...);
1846_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.