29 #ifndef _GLIBCXX_FUTURE
30 #define _GLIBCXX_FUTURE 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
47 #include <bits/shared_ptr.h>
48 #include <bits/std_function.h>
49 #include <bits/uses_allocator.h>
53 namespace std _GLIBCXX_VISIBILITY(default)
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
68 future_already_retrieved = 1,
69 promise_already_satisfied,
88 inline error_condition
107 what()
const noexcept;
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 result_of<
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) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
203 exception_ptr _M_error;
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)
376 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
377 return future_status::ready;
378 if (_M_is_deferred_future())
379 return future_status::deferred;
380 if (_M_status._M_load_when_equal_until(_Status::__ready,
381 memory_order_acquire, __abs))
388 return future_status::ready;
390 return future_status::timeout;
396 _M_set_result(
function<_Ptr_type()> __res,
bool __ignore_failure =
false)
398 bool __did_set =
false;
401 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
405 _M_status._M_store_notify_all(_Status::__ready,
406 memory_order_release);
407 else if (!__ignore_failure)
408 __throw_future_error(
int(future_errc::promise_already_satisfied));
415 _M_set_delayed_result(
function<_Ptr_type()> __res,
416 weak_ptr<_State_baseV2> __self)
418 bool __did_set =
false;
419 unique_ptr<_Make_ready> __mr{
new _Make_ready};
422 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
425 __throw_future_error(
int(future_errc::promise_already_satisfied));
426 __mr->_M_shared_state = std::move(__self);
433 _M_break_promise(_Ptr_type __res)
435 if (static_cast<bool>(__res))
443 _M_result.swap(__res);
445 _M_status._M_store_notify_all(_Status::__ready,
446 memory_order_release);
452 _M_set_retrieved_flag()
454 if (_M_retrieved.test_and_set())
455 __throw_future_error(
int(future_errc::future_already_retrieved));
458 template<
typename _Res,
typename _Arg>
462 template<
typename _Res,
typename _Arg>
463 struct _Setter<_Res, _Arg&>
467 static_assert(is_same<_Res, _Arg&>::value
468 || is_same<const _Res, _Arg>::value,
469 "Invalid specialisation");
472 typename promise<_Res>::_Ptr_type operator()()
const
474 _State_baseV2::_S_check(_M_promise->_M_future);
475 _M_promise->_M_storage->_M_set(*_M_arg);
476 return std::move(_M_promise->_M_storage);
478 promise<_Res>* _M_promise;
483 template<
typename _Res>
484 struct _Setter<_Res, _Res&&>
487 typename promise<_Res>::_Ptr_type operator()()
const
489 _State_baseV2::_S_check(_M_promise->_M_future);
490 _M_promise->_M_storage->_M_set(std::move(*_M_arg));
491 return std::move(_M_promise->_M_storage);
493 promise<_Res>* _M_promise;
497 struct __exception_ptr_tag { };
500 template<
typename _Res>
501 struct _Setter<_Res, __exception_ptr_tag>
504 typename promise<_Res>::_Ptr_type operator()()
const
506 _State_baseV2::_S_check(_M_promise->_M_future);
507 _M_promise->_M_storage->_M_error = *_M_ex;
508 return std::move(_M_promise->_M_storage);
511 promise<_Res>* _M_promise;
512 exception_ptr* _M_ex;
515 template<
typename _Res,
typename _Arg>
516 static _Setter<_Res, _Arg&&>
517 __setter(promise<_Res>* __prom, _Arg&& __arg)
522 template<
typename _Res>
523 static _Setter<_Res, __exception_ptr_tag>
524 __setter(exception_ptr& __ex, promise<_Res>* __prom)
526 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
529 template<
typename _Tp>
531 _S_check(
const shared_ptr<_Tp>& __p)
533 if (!static_cast<bool>(__p))
534 __throw_future_error((
int)future_errc::no_state);
540 _M_do_set(
function<_Ptr_type()>* __f,
bool* __did_set)
542 _Ptr_type __res = (*__f)();
547 _M_result.swap(__res);
551 virtual void _M_complete_async() { }
554 virtual bool _M_is_deferred_future()
const {
return false; }
556 struct _Make_ready final : __at_thread_exit_elt
558 weak_ptr<_State_baseV2> _M_shared_state;
559 static void _S_run(
void*);
564 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
566 class _Async_state_common;
568 using _State_base = _State_baseV2;
569 class _Async_state_commonV2;
572 template<
typename _BoundFn,
573 typename _Res = decltype(std::declval<_BoundFn&>()())>
574 class _Deferred_state;
576 template<
typename _BoundFn,
577 typename _Res = decltype(std::declval<_BoundFn&>()())>
578 class _Async_state_impl;
580 template<
typename _Signature>
581 class _Task_state_base;
583 template<
typename _Fn,
typename _Alloc,
typename _Signature>
586 template<
typename _BoundFn>
588 _S_make_deferred_state(_BoundFn&& __fn);
590 template<
typename _BoundFn>
592 _S_make_async_state(_BoundFn&& __fn);
594 template<
typename _Res_ptr,
typename _Fn,
595 typename _Res =
typename _Res_ptr::element_type::result_type>
598 template<
typename _Res_ptr,
typename _BoundFn>
599 static _Task_setter<_Res_ptr, _BoundFn>
600 _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
607 template<
typename _Res>
610 typedef _Res& result_type;
612 _Result() noexcept : _M_value_ptr() { }
615 _M_set(_Res& __res) noexcept
618 _Res& _M_get() noexcept {
return *_M_value_ptr; }
623 void _M_destroy() {
delete this; }
630 typedef void result_type;
633 void _M_destroy() {
delete this; }
636 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
639 template<
typename _Res,
typename _Arg>
645 template<
typename _Res_ptr,
typename _Fn,
typename _Res>
651 template<
typename _Res>
667 valid()
const noexcept {
return static_cast<bool>(_M_state); }
672 _State_base::_S_check(_M_state);
676 template<
typename _Rep,
typename _Period>
680 _State_base::_S_check(_M_state);
681 return _M_state->wait_for(__rel);
684 template<
typename _Clock,
typename _Duration>
688 _State_base::_S_check(_M_state);
689 return _M_state->wait_until(__abs);
697 _State_base::_S_check(_M_state);
699 if (!(__res._M_error == 0))
706 _M_state.swap(__that._M_state);
711 __basic_future(
const __state_type& __state) : _M_state(__state)
713 _State_base::_S_check(_M_state);
714 _M_state->_M_set_retrieved_flag();
719 __basic_future(
const shared_future<_Res>&) noexcept;
723 __basic_future(shared_future<_Res>&&) noexcept;
727 __basic_future(future<_Res>&&) noexcept;
729 constexpr __basic_future() noexcept : _M_state() { }
733 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
734 ~_Reset() { _M_fut._M_state.reset(); }
735 __basic_future& _M_fut;
741 template<
typename _Res>
742 class future :
public __basic_future<_Res>
744 friend class promise<_Res>;
745 template<
typename>
friend class packaged_task;
746 template<
typename _Fn,
typename... _Args>
747 friend future<__async_result_of<_Fn, _Args...>>
750 typedef __basic_future<_Res> _Base_type;
751 typedef typename _Base_type::__state_type __state_type;
754 future(
const __state_type& __state) : _Base_type(__state) { }
757 constexpr future() noexcept : _Base_type() { }
768 future(std::move(__fut))._M_swap(*
this);
776 typename _Base_type::_Reset __reset(*
this);
784 template<typename _Res>
788 template<
typename>
friend class packaged_task;
789 template<
typename _Fn,
typename... _Args>
790 friend future<__async_result_of<_Fn, _Args...>>
797 future(
const __state_type& __state) : _Base_type(__state) { }
800 constexpr
future() noexcept : _Base_type() { }
811 future(std::move(__fut))._M_swap(*
this);
819 typename _Base_type::_Reset __reset(*
this);
831 template<
typename>
friend class packaged_task;
832 template<
typename _Fn,
typename... _Args>
833 friend future<__async_result_of<_Fn, _Args...>>
840 future(
const __state_type& __state) : _Base_type(__state) { }
843 constexpr
future() noexcept : _Base_type() { }
854 future(std::move(__fut))._M_swap(*
this);
862 typename _Base_type::_Reset __reset(*
this);
871 template<typename _Res>
884 : _Base_type(std::move(__uf))
889 : _Base_type(std::move(__sf))
898 shared_future& operator=(shared_future&& __sf) noexcept
900 shared_future(std::move(__sf))._M_swap(*
this);
910 template<
typename _Res>
923 : _Base_type(std::move(__uf))
928 : _Base_type(std::move(__sf))
937 shared_future& operator=(shared_future&& __sf) noexcept
939 shared_future(std::move(__sf))._M_swap(*
this);
962 : _Base_type(std::move(__uf))
967 : _Base_type(std::move(__sf))
976 shared_future& operator=(shared_future&& __sf) noexcept
978 shared_future(std::move(__sf))._M_swap(*
this);
988 template<
typename _Res>
989 inline __basic_future<_Res>::
990 __basic_future(
const shared_future<_Res>& __sf) noexcept
991 : _M_state(__sf._M_state)
994 template<
typename _Res>
995 inline __basic_future<_Res>::
996 __basic_future(shared_future<_Res>&& __sf) noexcept
997 : _M_state(std::move(__sf._M_state))
1000 template<
typename _Res>
1001 inline __basic_future<_Res>::
1002 __basic_future(future<_Res>&& __uf) noexcept
1003 : _M_state(std::move(__uf._M_state))
1008 template<
typename _Res>
1009 inline shared_future<_Res>
1010 future<_Res>::share() noexcept
1011 {
return shared_future<_Res>(std::move(*
this)); }
1013 template<
typename _Res>
1014 inline shared_future<_Res&>
1015 future<_Res&>::share() noexcept
1016 {
return shared_future<_Res&>(std::move(*
this)); }
1018 inline shared_future<void>
1019 future<void>::share() noexcept
1020 {
return shared_future<void>(std::move(*
this)); }
1023 template<
typename _Res>
1026 typedef __future_base::_State_base _State;
1027 typedef __future_base::_Result<_Res> _Res_type;
1028 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1029 template<
typename,
typename>
friend class _State::_Setter;
1031 shared_ptr<_State> _M_future;
1032 _Ptr_type _M_storage;
1037 _M_storage(new _Res_type())
1040 promise(promise&& __rhs) noexcept
1041 : _M_future(std::move(__rhs._M_future)),
1042 _M_storage(std::move(__rhs._M_storage))
1045 template<
typename _Allocator>
1046 promise(allocator_arg_t,
const _Allocator& __a)
1048 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1051 template<
typename _Allocator>
1052 promise(allocator_arg_t,
const _Allocator&, promise&& __rhs)
1053 : _M_future(std::move(__rhs._M_future)),
1054 _M_storage(std::move(__rhs._M_storage))
1057 promise(
const promise&) =
delete;
1061 if (static_cast<bool>(_M_future) && !_M_future.unique())
1062 _M_future->_M_break_promise(std::move(_M_storage));
1067 operator=(promise&& __rhs) noexcept
1069 promise(std::move(__rhs)).swap(*
this);
1073 promise& operator=(
const promise&) =
delete;
1076 swap(promise& __rhs) noexcept
1078 _M_future.swap(__rhs._M_future);
1079 _M_storage.swap(__rhs._M_storage);
1085 {
return future<_Res>(_M_future); }
1089 set_value(
const _Res& __r)
1090 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1093 set_value(_Res&& __r)
1094 { _M_future->_M_set_result(_State::__setter(
this, std::move(__r))); }
1097 set_exception(exception_ptr __p)
1098 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1101 set_value_at_thread_exit(
const _Res& __r)
1103 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1108 set_value_at_thread_exit(_Res&& __r)
1110 _M_future->_M_set_delayed_result(
1111 _State::__setter(
this, std::move(__r)), _M_future);
1115 set_exception_at_thread_exit(exception_ptr __p)
1117 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1122 template<
typename _Res>
1124 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1127 template<
typename _Res,
typename _Alloc>
1128 struct uses_allocator<promise<_Res>, _Alloc>
1133 template<
typename _Res>
1136 typedef __future_base::_State_base _State;
1139 template<
typename,
typename>
friend class _State::_Setter;
1142 _Ptr_type _M_storage;
1146 : _M_future(std::make_shared<_State>()),
1147 _M_storage(
new _Res_type())
1151 : _M_future(std::move(__rhs._M_future)),
1152 _M_storage(std::move(__rhs._M_storage))
1155 template<
typename _Allocator>
1157 : _M_future(std::allocate_shared<_State>(__a)),
1158 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1161 template<
typename _Allocator>
1163 : _M_future(std::move(__rhs._M_future)),
1164 _M_storage(std::move(__rhs._M_storage))
1171 if (static_cast<bool>(_M_future) && !_M_future.unique())
1172 _M_future->_M_break_promise(std::move(_M_storage));
1177 operator=(
promise&& __rhs) noexcept
1179 promise(std::move(__rhs)).swap(*
this);
1188 _M_future.swap(__rhs._M_future);
1189 _M_storage.
swap(__rhs._M_storage);
1199 set_value(_Res& __r)
1200 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1203 set_exception(exception_ptr __p)
1204 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1207 set_value_at_thread_exit(_Res& __r)
1209 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1214 set_exception_at_thread_exit(exception_ptr __p)
1216 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1225 typedef __future_base::_State_base _State;
1228 template<
typename,
typename>
friend class _State::_Setter;
1235 : _M_future(std::make_shared<_State>()),
1240 : _M_future(std::move(__rhs._M_future)),
1241 _M_storage(std::move(__rhs._M_storage))
1244 template<
typename _Allocator>
1246 : _M_future(std::allocate_shared<_State>(__a)),
1247 _M_storage(__future_base::_S_allocate_result<void>(__a))
1252 template<
typename _Allocator>
1254 : _M_future(std::move(__rhs._M_future)),
1255 _M_storage(std::move(__rhs._M_storage))
1262 if (static_cast<bool>(_M_future) && !_M_future.unique())
1263 _M_future->_M_break_promise(std::move(_M_storage));
1268 operator=(
promise&& __rhs) noexcept
1270 promise(std::move(__rhs)).swap(*
this);
1279 _M_future.swap(__rhs._M_future);
1280 _M_storage.swap(__rhs._M_storage);
1292 set_exception(exception_ptr __p)
1293 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1296 set_value_at_thread_exit();
1299 set_exception_at_thread_exit(exception_ptr __p)
1301 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1308 struct __future_base::_State_base::_Setter<void, void>
1312 _State_base::_S_check(_M_promise->_M_future);
1313 return std::move(_M_promise->_M_storage);
1316 promise<void>* _M_promise;
1320 promise<void>::set_value()
1321 { _M_future->_M_set_result(_State::_Setter<void, void>{
this }); }
1324 promise<void>::set_value_at_thread_exit()
1326 _M_future->_M_set_delayed_result(_State::_Setter<void, void>{
this},
1330 template<
typename _Ptr_type,
typename _Fn,
typename _Res>
1331 struct __future_base::_Task_setter
1334 _Ptr_type operator()()
const
1338 (*_M_result)->_M_set((*_M_fn)());
1342 __throw_exception_again;
1348 return std::move(*_M_result);
1350 _Ptr_type* _M_result;
1354 template<
typename _Ptr_type,
typename _Fn>
1355 struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1357 _Ptr_type operator()()
const
1365 __throw_exception_again;
1371 return std::move(*_M_result);
1373 _Ptr_type* _M_result;
1378 template<
typename _Res,
typename... _Args>
1379 struct __future_base::_Task_state_base<_Res(_Args...)>
1380 : __future_base::_State_base
1382 typedef _Res _Res_type;
1384 template<
typename _Alloc>
1385 _Task_state_base(
const _Alloc& __a)
1386 : _M_result(_S_allocate_result<_Res>(__a))
1391 _M_run(_Args&&... __args) = 0;
1395 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1397 virtual shared_ptr<_Task_state_base>
1400 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1401 _Ptr_type _M_result;
1405 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1406 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1407 : __future_base::_Task_state_base<_Res(_Args...)>
1409 template<
typename _Fn2>
1410 _Task_state(_Fn2&& __fn,
const _Alloc& __a)
1411 : _Task_state_base<_Res(_Args...)>(__a),
1412 _M_impl(std::
forward<_Fn2>(__fn), __a)
1417 _M_run(_Args&&... __args)
1419 auto __boundfn = [&] () ->
typename result_of<_Fn&(_Args&&...)>::type {
1420 return std::__invoke(_M_impl._M_fn, std::forward<_Args>(__args)...);
1422 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1426 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1428 auto __boundfn = [&] () ->
typename result_of<_Fn&(_Args&&...)>::type {
1429 return std::__invoke(_M_impl._M_fn, std::forward<_Args>(__args)...);
1431 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1435 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1438 struct _Impl : _Alloc
1440 template<
typename _Fn2>
1441 _Impl(_Fn2&& __fn,
const _Alloc& __a)
1442 : _Alloc(__a), _M_fn(std::
forward<_Fn2>(__fn)) { }
1447 template<
typename _Signature,
typename _Fn,
typename _Alloc>
1448 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1449 __create_task_state(_Fn&& __fn,
const _Alloc& __a)
1451 typedef typename decay<_Fn>::type _Fn2;
1452 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1453 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1456 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1457 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1458 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1460 return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
1461 static_cast<_Alloc&
>(_M_impl));
1464 template<
typename _Task,
typename _Fn,
bool
1465 = is_same<_Task, typename decay<_Fn>::type>::value>
1466 struct __constrain_pkgdtask
1467 {
typedef void __type; };
1469 template<
typename _Task,
typename _Fn>
1470 struct __constrain_pkgdtask<_Task, _Fn, true>
1474 template<
typename _Res,
typename... _ArgTypes>
1475 class packaged_task<_Res(_ArgTypes...)>
1477 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1482 packaged_task() noexcept { }
1486 template<
typename _Allocator>
1490 template<
typename _Fn,
typename =
typename
1491 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1493 packaged_task(_Fn&& __fn)
1495 std::forward<_Fn>(__fn))
1501 template<
typename _Fn,
typename _Alloc,
typename =
typename
1502 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1504 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1505 std::forward<_Fn>(__fn), __a))
1510 if (static_cast<bool>(_M_state) && !_M_state.unique())
1511 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1515 packaged_task(
const packaged_task&) =
delete;
1516 packaged_task& operator=(
const packaged_task&) =
delete;
1518 template<
typename _Allocator>
1520 const packaged_task&) =
delete;
1523 packaged_task(packaged_task&& __other) noexcept
1524 { this->swap(__other); }
1526 template<
typename _Allocator>
1528 packaged_task&& __other) noexcept
1529 { this->swap(__other); }
1531 packaged_task& operator=(packaged_task&& __other) noexcept
1533 packaged_task(std::move(__other)).swap(*
this);
1538 swap(packaged_task& __other) noexcept
1539 { _M_state.swap(__other._M_state); }
1542 valid()
const noexcept
1543 {
return static_cast<bool>(_M_state); }
1552 operator()(_ArgTypes... __args)
1554 __future_base::_State_base::_S_check(_M_state);
1555 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1559 make_ready_at_thread_exit(_ArgTypes... __args)
1561 __future_base::_State_base::_S_check(_M_state);
1562 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1568 __future_base::_State_base::_S_check(_M_state);
1569 packaged_task __tmp;
1570 __tmp._M_state = _M_state;
1571 _M_state = _M_state->_M_reset();
1576 template<
typename _Res,
typename... _ArgTypes>
1578 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1579 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1582 template<
typename _Res,
typename _Alloc>
1583 struct uses_allocator<packaged_task<_Res>, _Alloc>
1589 template<
typename _BoundFn,
typename _Res>
1590 class __future_base::_Deferred_state final
1591 :
public __future_base::_State_base
1595 _Deferred_state(_BoundFn&& __fn)
1596 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1600 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1601 _Ptr_type _M_result;
1614 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1619 virtual bool _M_is_deferred_future()
const {
return true; }
1623 class __future_base::_Async_state_commonV2
1624 :
public __future_base::_State_base
1627 ~_Async_state_commonV2() =
default;
1644 virtual void _M_complete_async() { _M_join(); }
1646 void _M_join() {
std::call_once(_M_once, &thread::join, &_M_thread); }
1654 template<
typename _BoundFn,
typename _Res>
1655 class __future_base::_Async_state_impl final
1656 :
public __future_base::_Async_state_commonV2
1660 _Async_state_impl(_BoundFn&& __fn)
1661 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1666 _M_set_result(_S_task_setter(_M_result, _M_fn));
1671 if (static_cast<bool>(_M_result))
1672 this->_M_break_promise(std::move(_M_result));
1673 __throw_exception_again;
1681 ~_Async_state_impl() {
if (_M_thread.joinable()) _M_thread.join(); }
1684 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1685 _Ptr_type _M_result;
1689 template<
typename _BoundFn>
1691 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1693 typedef typename remove_reference<_BoundFn>::type __fn_type;
1694 typedef _Deferred_state<__fn_type> __state_type;
1695 return std::make_shared<__state_type>(std::move(__fn));
1698 template<
typename _BoundFn>
1700 __future_base::_S_make_async_state(_BoundFn&& __fn)
1702 typedef typename remove_reference<_BoundFn>::type __fn_type;
1703 typedef _Async_state_impl<__fn_type> __state_type;
1704 return std::make_shared<__state_type>(std::move(__fn));
1709 template<
typename _Fn,
typename... _Args>
1710 future<__async_result_of<_Fn, _Args...>>
1711 async(
launch __policy, _Fn&& __fn, _Args&&... __args)
1714 if ((__policy & launch::async) == launch::async)
1718 __state = __future_base::_S_make_async_state(
1719 std::thread::__make_invoker(std::forward<_Fn>(__fn),
1720 std::forward<_Args>(__args)...)
1723 #if __cpp_exceptions
1726 if (__e.code() != errc::resource_unavailable_try_again
1727 || (__policy & launch::deferred) != launch::deferred)
1734 __state = __future_base::_S_make_deferred_state(
1735 std::thread::__make_invoker(std::forward<_Fn>(__fn),
1736 std::forward<_Args>(__args)...));
1738 return future<__async_result_of<_Fn, _Args...>>(__state);
1742 template<
typename _Fn,
typename... _Args>
1743 inline future<__async_result_of<_Fn, _Args...>>
1744 async(_Fn&& __fn, _Args&&... __args)
1746 return std::async(launch::async|launch::deferred,
1747 std::forward<_Fn>(__fn),
1748 std::forward<_Args>(__args)...);
1751 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
1752 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1755 _GLIBCXX_END_NAMESPACE_VERSION
1760 #endif // _GLIBCXX_FUTURE
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
Explicit specialization for shared_future<void>
shared_future(const shared_future &__sf)
Copy constructor.
Explicit specialization for future<void>
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
call_once
A result object that has storage for an object of type _Res.
future(future &&__uf) noexcept
Move constructor.
A result object that uses an allocator.
future(future &&__uf) noexcept
Move constructor.
const error_category & future_category() noexcept
Points to a statically-allocated object derived from error_category.
future_errc
Error code for futures.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
shared_future(future< _Res > &&__uf) noexcept
Construct from a future rvalue.
shared_future(future< _Res & > &&__uf) noexcept
Construct from a future rvalue.
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.
Non-standard RAII type for managing pointers obtained from allocators.
The standard allocator, as per [20.4].
Base class and enclosing scope.
future_status
Status code for futures.
Partial specialization for shared_future<R&>
shared_ptr< _Tp > allocate_shared(const _Alloc &__a, _Args &&...__args)
Create an object that is owned by a shared_ptr.
future< __async_result_of< _Fn, _Args...> > async(launch __policy, _Fn &&__fn, _Args &&...__args)
async
shared_future(const shared_future &__sf)
Copy constructor.
_GLIBCXX17_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...
Partial specialization for future<R&>
__allocated_ptr< _Alloc > __allocate_guarded(_Alloc &__a)
Allocate space for a single object using __a.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
shared_future(const shared_future &__sf)
Copy constructor.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
Explicit specialization for void.
Thrown to indicate error code of underlying system.
exception_ptr make_exception_ptr(_Ex) noexcept
Obtain an exception_ptr pointing to a copy of the supplied object.
Primary template for future.
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
logic_error(const string &__arg) _GLIBCXX_TXN_SAFE
Exception type thrown by futures.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
Primary template for shared_future.
__result_type _M_get_result() const
Wait for the state to be ready and rethrow any stored exception.
Partial specialization for reference types.
20.7.1.2 unique_ptr for single objects.
One of two subclasses of exception.
launch
Launch code for futures.
shared_ptr< _Tp > make_shared(_Args &&...__args)
Create an object that is owned by a shared_ptr.
future< __async_result_of< _Fn, _Args...> > async(_Fn &&__fn, _Args &&...__args)
async, potential overload
shared_future(future< void > &&__uf) noexcept
Construct from a future rvalue.
future(future &&__uf) noexcept
Move constructor.
Common implementation for future and shared_future.
void rethrow_exception(exception_ptr) __attribute__((__noreturn__))
Throw the object pointed to by the exception_ptr.
virtual const char * what() const noexcept
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
exception_ptr current_exception() noexcept
Thrown as part of forced unwinding.A magic placeholder class that can be caught by reference to recog...
Primary template for promise.