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/uses_allocator.h>
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 future_already_retrieved = 1,
68 promise_already_satisfied,
87 inline error_condition
101 :
logic_error(
"std::future_error: " + __ec.message()), _M_code(__ec)
107 what()
const noexcept;
110 code()
const noexcept {
return _M_code; }
114 template<
typename _Res>
117 template<
typename _Res>
120 template<
typename _Signature>
123 template<
typename _Res>
135 return static_cast<launch>(
136 static_cast<int>(__x) & static_cast<int>(__y));
141 return static_cast<launch>(
142 static_cast<int>(__x) | static_cast<int>(__y));
147 return static_cast<launch>(
148 static_cast<int>(__x) ^ static_cast<int>(__y));
152 {
return static_cast<launch>(~static_cast<
int>(__x)); }
155 {
return __x = __x & __y; }
158 {
return __x = __x | __y; }
161 {
return __x = __x ^ __y; }
173 template<
typename _Fn,
typename... _Args>
174 using __async_result_of =
typename result_of<
175 typename decay<_Fn>::type(
typename decay<_Args>::type...)>::type;
177 template<
typename _Fn,
typename... _Args>
178 future<__async_result_of<_Fn, _Args...>>
179 async(
launch __policy, _Fn&& __fn, _Args&&... __args);
181 template<
typename _Fn,
typename... _Args>
182 future<__async_result_of<_Fn, _Args...>>
183 async(_Fn&& __fn, _Args&&... __args);
185 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
186 && (ATOMIC_INT_LOCK_FREE > 1)
194 exception_ptr _M_error;
200 virtual void _M_destroy() = 0;
204 void operator()(
_Result_base* __fr)
const { __fr->_M_destroy(); }
213 template<
typename _Res>
217 template<
typename _Res>
221 __gnu_cxx::__aligned_buffer<_Res> _M_storage;
225 typedef _Res result_type;
227 _Result() noexcept : _M_initialized() { }
237 _M_value() noexcept {
return *_M_storage._M_ptr(); }
240 _M_set(
const _Res& __res)
242 ::new (_M_storage._M_addr()) _Res(__res);
243 _M_initialized =
true;
249 ::new (_M_storage._M_addr()) _Res(std::move(__res));
250 _M_initialized =
true;
254 void _M_destroy() {
delete this; }
258 template<
typename _Res,
typename _Alloc>
261 using __allocator_type = __alloc_rebind<_Alloc, _Result_alloc>;
270 __allocator_type __a(*
this);
277 template<
typename _Res,
typename _Allocator>
279 _S_allocate_result(
const _Allocator& __a)
282 typename __result_type::__allocator_type __a2(__a);
284 __result_type* __p = ::new((
void*)__guard.get()) __result_type{__a};
286 return _Ptr<__result_type>(__p);
290 template<
typename _Res,
typename _Tp>
291 static _Ptr<_Result<_Res>>
294 return _Ptr<_Result<_Res>>(
new _Result<_Res>);
302 typedef _Ptr<_Result_base> _Ptr_type;
304 enum _Status :
unsigned {
310 __atomic_futex_unsigned<> _M_status;
311 atomic_flag _M_retrieved = ATOMIC_FLAG_INIT;
315 _State_baseV2() noexcept : _M_result(), _M_status(_Status::__not_ready)
317 _State_baseV2(
const _State_baseV2&) =
delete;
318 _State_baseV2& operator=(
const _State_baseV2&) =
delete;
319 virtual ~_State_baseV2() =
default;
328 _M_status._M_load_when_equal(_Status::__ready, memory_order_acquire);
332 template<
typename _Rep,
typename _Period>
334 wait_for(
const chrono::duration<_Rep, _Period>& __rel)
338 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
339 return future_status::ready;
340 if (_M_is_deferred_future())
341 return future_status::deferred;
342 if (_M_status._M_load_when_equal_for(_Status::__ready,
343 memory_order_acquire, __rel))
356 return future_status::ready;
358 return future_status::timeout;
361 template<
typename _Clock,
typename _Duration>
363 wait_until(
const chrono::time_point<_Clock, _Duration>& __abs)
367 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
368 return future_status::ready;
369 if (_M_is_deferred_future())
370 return future_status::deferred;
371 if (_M_status._M_load_when_equal_until(_Status::__ready,
372 memory_order_acquire, __abs))
379 return future_status::ready;
381 return future_status::timeout;
387 _M_set_result(
function<_Ptr_type()> __res,
bool __ignore_failure =
false)
389 bool __did_set =
false;
392 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
396 _M_status._M_store_notify_all(_Status::__ready,
397 memory_order_release);
398 else if (!__ignore_failure)
399 __throw_future_error(
int(future_errc::promise_already_satisfied));
406 _M_set_delayed_result(
function<_Ptr_type()> __res,
407 weak_ptr<_State_baseV2> __self)
409 bool __did_set =
false;
410 unique_ptr<_Make_ready> __mr{
new _Make_ready};
413 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
416 __throw_future_error(
int(future_errc::promise_already_satisfied));
417 __mr->_M_shared_state = std::move(__self);
424 _M_break_promise(_Ptr_type __res)
426 if (static_cast<bool>(__res))
428 error_code __ec(make_error_code(future_errc::broken_promise));
434 _M_result.swap(__res);
436 _M_status._M_store_notify_all(_Status::__ready,
437 memory_order_release);
443 _M_set_retrieved_flag()
445 if (_M_retrieved.test_and_set())
446 __throw_future_error(
int(future_errc::future_already_retrieved));
449 template<
typename _Res,
typename _Arg>
453 template<
typename _Res,
typename _Arg>
454 struct _Setter<_Res, _Arg&>
458 static_assert(is_same<_Res, _Arg&>::value
459 || is_same<const _Res, _Arg>::value,
460 "Invalid specialisation");
463 typename promise<_Res>::_Ptr_type operator()()
const
465 _State_baseV2::_S_check(_M_promise->_M_future);
466 _M_promise->_M_storage->_M_set(*_M_arg);
467 return std::move(_M_promise->_M_storage);
469 promise<_Res>* _M_promise;
474 template<
typename _Res>
475 struct _Setter<_Res, _Res&&>
478 typename promise<_Res>::_Ptr_type operator()()
const
480 _State_baseV2::_S_check(_M_promise->_M_future);
481 _M_promise->_M_storage->_M_set(std::move(*_M_arg));
482 return std::move(_M_promise->_M_storage);
484 promise<_Res>* _M_promise;
488 struct __exception_ptr_tag { };
491 template<
typename _Res>
492 struct _Setter<_Res, __exception_ptr_tag>
495 typename promise<_Res>::_Ptr_type operator()()
const
497 _State_baseV2::_S_check(_M_promise->_M_future);
498 _M_promise->_M_storage->_M_error = *_M_ex;
499 return std::move(_M_promise->_M_storage);
502 promise<_Res>* _M_promise;
503 exception_ptr* _M_ex;
506 template<
typename _Res,
typename _Arg>
507 static _Setter<_Res, _Arg&&>
508 __setter(promise<_Res>* __prom, _Arg&& __arg)
513 template<
typename _Res>
514 static _Setter<_Res, __exception_ptr_tag>
515 __setter(exception_ptr& __ex, promise<_Res>* __prom)
517 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
520 template<
typename _Tp>
522 _S_check(
const shared_ptr<_Tp>& __p)
524 if (!static_cast<bool>(__p))
525 __throw_future_error((
int)future_errc::no_state);
531 _M_do_set(
function<_Ptr_type()>* __f,
bool* __did_set)
533 _Ptr_type __res = (*__f)();
538 _M_result.swap(__res);
542 virtual void _M_complete_async() { }
545 virtual bool _M_is_deferred_future()
const {
return false; }
547 struct _Make_ready final : __at_thread_exit_elt
549 weak_ptr<_State_baseV2> _M_shared_state;
550 static void _S_run(
void*);
555 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
557 class _Async_state_common;
559 using _State_base = _State_baseV2;
560 class _Async_state_commonV2;
563 template<
typename _BoundFn,
typename =
typename _BoundFn::result_type>
564 class _Deferred_state;
566 template<
typename _BoundFn,
typename =
typename _BoundFn::result_type>
567 class _Async_state_impl;
569 template<
typename _Signature>
570 class _Task_state_base;
572 template<
typename _Fn,
typename _Alloc,
typename _Signature>
575 template<
typename _BoundFn>
577 _S_make_deferred_state(_BoundFn&& __fn);
579 template<
typename _BoundFn>
581 _S_make_async_state(_BoundFn&& __fn);
583 template<
typename _Res_ptr,
typename _Fn,
584 typename _Res =
typename _Res_ptr::element_type::result_type>
587 template<
typename _Res_ptr,
typename _BoundFn>
588 static _Task_setter<_Res_ptr, _BoundFn>
589 _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
596 template<
typename _Res>
599 typedef _Res& result_type;
601 _Result() noexcept : _M_value_ptr() { }
604 _M_set(_Res& __res) noexcept
607 _Res& _M_get() noexcept {
return *_M_value_ptr; }
612 void _M_destroy() {
delete this; }
619 typedef void result_type;
622 void _M_destroy() {
delete this; }
625 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
628 template<
typename _Res,
typename _Arg>
634 template<
typename _Res_ptr,
typename _Fn,
typename _Res>
640 template<
typename _Res>
656 valid()
const noexcept {
return static_cast<bool>(_M_state); }
661 _State_base::_S_check(_M_state);
665 template<
typename _Rep,
typename _Period>
669 _State_base::_S_check(_M_state);
670 return _M_state->wait_for(__rel);
673 template<
typename _Clock,
typename _Duration>
677 _State_base::_S_check(_M_state);
678 return _M_state->wait_until(__abs);
686 _State_base::_S_check(_M_state);
688 if (!(__res._M_error == 0))
695 _M_state.swap(__that._M_state);
700 __basic_future(
const __state_type& __state) : _M_state(__state)
702 _State_base::_S_check(_M_state);
703 _M_state->_M_set_retrieved_flag();
708 __basic_future(
const shared_future<_Res>&) noexcept;
712 __basic_future(shared_future<_Res>&&) noexcept;
716 __basic_future(future<_Res>&&) noexcept;
718 constexpr __basic_future() noexcept : _M_state() { }
722 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
723 ~_Reset() { _M_fut._M_state.reset(); }
724 __basic_future& _M_fut;
730 template<
typename _Res>
731 class future :
public __basic_future<_Res>
733 friend class promise<_Res>;
734 template<
typename>
friend class packaged_task;
735 template<
typename _Fn,
typename... _Args>
736 friend future<__async_result_of<_Fn, _Args...>>
739 typedef __basic_future<_Res> _Base_type;
740 typedef typename _Base_type::__state_type __state_type;
743 future(
const __state_type& __state) : _Base_type(__state) { }
746 constexpr future() noexcept : _Base_type() { }
757 future(std::move(__fut))._M_swap(*
this);
765 typename _Base_type::_Reset __reset(*
this);
773 template<
typename _Res>
777 template<
typename>
friend class packaged_task;
778 template<
typename _Fn,
typename... _Args>
779 friend future<__async_result_of<_Fn, _Args...>>
786 future(
const __state_type& __state) : _Base_type(__state) { }
789 constexpr
future() noexcept : _Base_type() { }
800 future(std::move(__fut))._M_swap(*
this);
808 typename _Base_type::_Reset __reset(*
this);
809 return this->_M_get_result()._M_get();
820 template<
typename>
friend class packaged_task;
821 template<
typename _Fn,
typename... _Args>
822 friend future<__async_result_of<_Fn, _Args...>>
829 future(
const __state_type& __state) : _Base_type(__state) { }
832 constexpr
future() noexcept : _Base_type() { }
843 future(std::move(__fut))._M_swap(*
this);
851 typename _Base_type::_Reset __reset(*
this);
860 template<
typename _Res>
861 class shared_future :
public __basic_future<_Res>
863 typedef __basic_future<_Res> _Base_type;
866 constexpr shared_future() noexcept : _Base_type() { }
873 : _Base_type(std::move(__uf))
878 : _Base_type(std::move(__sf))
887 shared_future& operator=(shared_future&& __sf) noexcept
889 shared_future(std::move(__sf))._M_swap(*
this);
899 template<
typename _Res>
912 : _Base_type(std::move(__uf))
917 : _Base_type(std::move(__sf))
926 shared_future& operator=(shared_future&& __sf) noexcept
928 shared_future(std::move(__sf))._M_swap(*
this);
934 get()
const {
return this->_M_get_result()._M_get(); }
951 : _Base_type(std::move(__uf))
956 : _Base_type(std::move(__sf))
965 shared_future& operator=(shared_future&& __sf) noexcept
967 shared_future(std::move(__sf))._M_swap(*
this);
977 template<
typename _Res>
978 inline __basic_future<_Res>::
979 __basic_future(
const shared_future<_Res>& __sf) noexcept
980 : _M_state(__sf._M_state)
983 template<
typename _Res>
984 inline __basic_future<_Res>::
985 __basic_future(shared_future<_Res>&& __sf) noexcept
986 : _M_state(std::move(__sf._M_state))
989 template<
typename _Res>
990 inline __basic_future<_Res>::
991 __basic_future(future<_Res>&& __uf) noexcept
992 : _M_state(std::move(__uf._M_state))
995 template<
typename _Res>
996 inline shared_future<_Res>
997 future<_Res>::share()
998 {
return shared_future<_Res>(std::move(*
this)); }
1000 template<
typename _Res>
1001 inline shared_future<_Res&>
1002 future<_Res&>::share()
1003 {
return shared_future<_Res&>(std::move(*
this)); }
1005 inline shared_future<void>
1006 future<void>::share()
1007 {
return shared_future<void>(std::move(*
this)); }
1010 template<
typename _Res>
1013 typedef __future_base::_State_base _State;
1014 typedef __future_base::_Result<_Res> _Res_type;
1015 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1016 template<
typename,
typename>
friend class _State::_Setter;
1018 shared_ptr<_State> _M_future;
1019 _Ptr_type _M_storage;
1024 _M_storage(new _Res_type())
1027 promise(promise&& __rhs) noexcept
1028 : _M_future(std::move(__rhs._M_future)),
1029 _M_storage(std::move(__rhs._M_storage))
1032 template<
typename _Allocator>
1033 promise(allocator_arg_t,
const _Allocator& __a)
1035 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1038 template<
typename _Allocator>
1039 promise(allocator_arg_t,
const _Allocator&, promise&& __rhs)
1040 : _M_future(std::move(__rhs._M_future)),
1041 _M_storage(std::move(__rhs._M_storage))
1044 promise(
const promise&) =
delete;
1048 if (static_cast<bool>(_M_future) && !_M_future.unique())
1049 _M_future->_M_break_promise(std::move(_M_storage));
1054 operator=(promise&& __rhs) noexcept
1056 promise(std::move(__rhs)).swap(*
this);
1060 promise& operator=(
const promise&) =
delete;
1063 swap(promise& __rhs) noexcept
1065 _M_future.swap(__rhs._M_future);
1066 _M_storage.
swap(__rhs._M_storage);
1072 {
return future<_Res>(_M_future); }
1076 set_value(
const _Res& __r)
1077 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1080 set_value(_Res&& __r)
1081 { _M_future->_M_set_result(_State::__setter(
this, std::move(__r))); }
1084 set_exception(exception_ptr __p)
1085 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1088 set_value_at_thread_exit(
const _Res& __r)
1090 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1095 set_value_at_thread_exit(_Res&& __r)
1097 _M_future->_M_set_delayed_result(
1098 _State::__setter(
this, std::move(__r)), _M_future);
1102 set_exception_at_thread_exit(exception_ptr __p)
1104 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1109 template<
typename _Res>
1111 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1114 template<
typename _Res,
typename _Alloc>
1115 struct uses_allocator<promise<_Res>, _Alloc>
1120 template<
typename _Res>
1123 typedef __future_base::_State_base _State;
1126 template<
typename,
typename>
friend class _State::_Setter;
1129 _Ptr_type _M_storage;
1133 : _M_future(std::make_shared<_State>()),
1134 _M_storage(
new _Res_type())
1138 : _M_future(std::move(__rhs._M_future)),
1139 _M_storage(std::move(__rhs._M_storage))
1142 template<
typename _Allocator>
1144 : _M_future(std::allocate_shared<_State>(__a)),
1145 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1148 template<
typename _Allocator>
1150 : _M_future(std::move(__rhs._M_future)),
1151 _M_storage(std::move(__rhs._M_storage))
1158 if (static_cast<bool>(_M_future) && !_M_future.unique())
1159 _M_future->_M_break_promise(std::move(_M_storage));
1164 operator=(
promise&& __rhs) noexcept
1166 promise(std::move(__rhs)).swap(*
this);
1175 _M_future.swap(__rhs._M_future);
1176 _M_storage.
swap(__rhs._M_storage);
1186 set_value(_Res& __r)
1187 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1190 set_exception(exception_ptr __p)
1191 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1194 set_value_at_thread_exit(_Res& __r)
1196 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1201 set_exception_at_thread_exit(exception_ptr __p)
1203 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1212 typedef __future_base::_State_base _State;
1215 template<
typename,
typename>
friend class _State::_Setter;
1222 : _M_future(std::make_shared<_State>()),
1227 : _M_future(std::move(__rhs._M_future)),
1228 _M_storage(std::move(__rhs._M_storage))
1231 template<
typename _Allocator>
1233 : _M_future(std::allocate_shared<_State>(__a)),
1234 _M_storage(__future_base::_S_allocate_result<void>(__a))
1239 template<
typename _Allocator>
1241 : _M_future(std::move(__rhs._M_future)),
1242 _M_storage(std::move(__rhs._M_storage))
1249 if (static_cast<bool>(_M_future) && !_M_future.unique())
1250 _M_future->_M_break_promise(std::move(_M_storage));
1255 operator=(
promise&& __rhs) noexcept
1257 promise(std::move(__rhs)).swap(*
this);
1266 _M_future.swap(__rhs._M_future);
1267 _M_storage.
swap(__rhs._M_storage);
1279 set_exception(exception_ptr __p)
1280 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1283 set_value_at_thread_exit();
1286 set_exception_at_thread_exit(exception_ptr __p)
1288 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1295 struct __future_base::_State_base::_Setter<void, void>
1299 _State_base::_S_check(_M_promise->_M_future);
1300 return std::move(_M_promise->_M_storage);
1303 promise<void>* _M_promise;
1307 promise<void>::set_value()
1308 { _M_future->_M_set_result(_State::_Setter<void, void>{
this }); }
1311 promise<void>::set_value_at_thread_exit()
1313 _M_future->_M_set_delayed_result(_State::_Setter<void, void>{
this},
1317 template<
typename _Ptr_type,
typename _Fn,
typename _Res>
1318 struct __future_base::_Task_setter
1321 _Ptr_type operator()()
const
1325 (*_M_result)->_M_set((*_M_fn)());
1329 __throw_exception_again;
1335 return std::move(*_M_result);
1337 _Ptr_type* _M_result;
1341 template<
typename _Ptr_type,
typename _Fn>
1342 struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1344 _Ptr_type operator()()
const
1352 __throw_exception_again;
1358 return std::move(*_M_result);
1360 _Ptr_type* _M_result;
1365 template<
typename _Res,
typename... _Args>
1366 struct __future_base::_Task_state_base<_Res(_Args...)>
1367 : __future_base::_State_base
1369 typedef _Res _Res_type;
1371 template<
typename _Alloc>
1372 _Task_state_base(
const _Alloc& __a)
1373 : _M_result(_S_allocate_result<_Res>(__a))
1378 _M_run(_Args&&... __args) = 0;
1382 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1384 virtual shared_ptr<_Task_state_base>
1387 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1388 _Ptr_type _M_result;
1392 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1393 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1394 : __future_base::_Task_state_base<_Res(_Args...)>
1396 template<
typename _Fn2>
1397 _Task_state(_Fn2&& __fn,
const _Alloc& __a)
1398 : _Task_state_base<_Res(_Args...)>(__a),
1399 _M_impl(std::
forward<_Fn2>(__fn), __a)
1404 _M_run(_Args&&... __args)
1407 auto __boundfn = std::__bind_simple(
std::ref(_M_impl._M_fn),
1408 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1409 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1413 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1416 auto __boundfn = std::__bind_simple(
std::ref(_M_impl._M_fn),
1417 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1418 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1422 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1425 template<
typename _Tp>
1426 static reference_wrapper<_Tp>
1427 _S_maybe_wrap_ref(_Tp& __t)
1430 template<
typename _Tp>
1432 typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
1433 _S_maybe_wrap_ref(_Tp&& __t)
1434 {
return std::forward<_Tp>(__t); }
1436 struct _Impl : _Alloc
1438 template<
typename _Fn2>
1439 _Impl(_Fn2&& __fn,
const _Alloc& __a)
1440 : _Alloc(__a), _M_fn(std::
forward<_Fn2>(__fn)) { }
1445 template<
typename _Signature,
typename _Fn,
typename _Alloc>
1446 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1447 __create_task_state(_Fn&& __fn,
const _Alloc& __a)
1449 typedef typename decay<_Fn>::type _Fn2;
1450 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1451 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1454 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1455 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1456 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1458 return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
1459 static_cast<_Alloc&
>(_M_impl));
1462 template<
typename _Task,
typename _Fn,
bool
1463 = is_same<_Task, typename decay<_Fn>::type>::value>
1464 struct __constrain_pkgdtask
1465 {
typedef void __type; };
1467 template<
typename _Task,
typename _Fn>
1468 struct __constrain_pkgdtask<_Task, _Fn, true>
1472 template<
typename _Res,
typename... _ArgTypes>
1473 class packaged_task<_Res(_ArgTypes...)>
1475 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1480 packaged_task() noexcept { }
1484 template<
typename _Allocator>
1488 template<
typename _Fn,
typename =
typename
1489 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1491 packaged_task(_Fn&& __fn)
1493 std::forward<_Fn>(__fn))
1499 template<
typename _Fn,
typename _Alloc,
typename =
typename
1500 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1502 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1503 std::forward<_Fn>(__fn), __a))
1508 if (static_cast<bool>(_M_state) && !_M_state.unique())
1509 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1513 packaged_task(
const packaged_task&) =
delete;
1514 packaged_task& operator=(
const packaged_task&) =
delete;
1516 template<
typename _Allocator>
1518 const packaged_task&) =
delete;
1521 packaged_task(packaged_task&& __other) noexcept
1522 { this->
swap(__other); }
1524 template<
typename _Allocator>
1526 packaged_task&& __other) noexcept
1527 { this->
swap(__other); }
1529 packaged_task& operator=(packaged_task&& __other) noexcept
1531 packaged_task(std::move(__other)).swap(*
this);
1536 swap(packaged_task& __other) noexcept
1537 { _M_state.swap(__other._M_state); }
1540 valid()
const noexcept
1541 {
return static_cast<bool>(_M_state); }
1550 operator()(_ArgTypes... __args)
1552 __future_base::_State_base::_S_check(_M_state);
1553 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1557 make_ready_at_thread_exit(_ArgTypes... __args)
1559 __future_base::_State_base::_S_check(_M_state);
1560 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1566 __future_base::_State_base::_S_check(_M_state);
1567 packaged_task __tmp;
1568 __tmp._M_state = _M_state;
1569 _M_state = _M_state->_M_reset();
1574 template<
typename _Res,
typename... _ArgTypes>
1576 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1577 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1580 template<
typename _Res,
typename _Alloc>
1581 struct uses_allocator<packaged_task<_Res>, _Alloc>
1587 template<
typename _BoundFn,
typename _Res>
1588 class __future_base::_Deferred_state final
1589 :
public __future_base::_State_base
1593 _Deferred_state(_BoundFn&& __fn)
1594 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1598 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1599 _Ptr_type _M_result;
1612 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1617 virtual bool _M_is_deferred_future()
const {
return true; }
1621 class __future_base::_Async_state_commonV2
1622 :
public __future_base::_State_base
1625 ~_Async_state_commonV2() =
default;
1642 virtual void _M_complete_async() { _M_join(); }
1644 void _M_join() {
std::call_once(_M_once, &thread::join, &_M_thread); }
1652 template<
typename _BoundFn,
typename _Res>
1653 class __future_base::_Async_state_impl final
1654 :
public __future_base::_Async_state_commonV2
1658 _Async_state_impl(_BoundFn&& __fn)
1659 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1664 _M_set_result(_S_task_setter(_M_result, _M_fn));
1669 if (static_cast<bool>(_M_result))
1670 this->_M_break_promise(std::move(_M_result));
1671 __throw_exception_again;
1679 ~_Async_state_impl() {
if (_M_thread.joinable()) _M_thread.join(); }
1682 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1683 _Ptr_type _M_result;
1687 template<
typename _BoundFn>
1689 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1691 typedef typename remove_reference<_BoundFn>::type __fn_type;
1692 typedef _Deferred_state<__fn_type> __state_type;
1693 return std::make_shared<__state_type>(std::move(__fn));
1696 template<
typename _BoundFn>
1698 __future_base::_S_make_async_state(_BoundFn&& __fn)
1700 typedef typename remove_reference<_BoundFn>::type __fn_type;
1701 typedef _Async_state_impl<__fn_type> __state_type;
1702 return std::make_shared<__state_type>(std::move(__fn));
1707 template<
typename _Fn,
typename... _Args>
1708 future<__async_result_of<_Fn, _Args...>>
1712 if ((__policy & launch::async) == launch::async)
1716 __state = __future_base::_S_make_async_state(std::__bind_simple(
1717 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1719 #if __cpp_exceptions
1722 if (__e.code() != errc::resource_unavailable_try_again
1723 || (__policy & launch::deferred) != launch::deferred)
1730 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1731 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1733 return future<__async_result_of<_Fn, _Args...>>(__state);
1737 template<
typename _Fn,
typename... _Args>
1738 inline future<__async_result_of<_Fn, _Args...>>
1741 return std::async(launch::async|launch::deferred,
1742 std::forward<_Fn>(__fn),
1743 std::forward<_Args>(__args)...);
1746 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
1747 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1751 _GLIBCXX_END_NAMESPACE_VERSION
1756 #endif // _GLIBCXX_FUTURE
Explicit specialization for shared_future<void>
shared_future(const shared_future &__sf)
Copy constructor.
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Explicit specialization for future<void>
Explicit specialization for void.
exception_ptr current_exception() noexcept
20.7.1.2 unique_ptr for single objects.
A result object that has storage for an object of type _Res.
future(future &&__uf) noexcept
Move constructor.
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
call_once
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Primary template for promise.
future(future &&__uf) noexcept
Move constructor.
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.
reference_wrapper< _Tp > ref(_Tp &__t) noexcept
Denotes a reference should be taken to a variable.
shared_future(future< _Res & > &&__uf) noexcept
Construct from a future rvalue.
The standard allocator, as per [20.4].
future< __async_result_of< _Fn, _Args...> > async(launch __policy, _Fn &&__fn, _Args &&...__args)
async
Partial specialization for reference types.
Base class and enclosing scope.
Partial specialization for shared_future<R&>
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
_Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
void rethrow_exception(exception_ptr) __attribute__((__noreturn__))
Throw the object pointed to by the exception_ptr.
shared_future(const shared_future &__sf)
Copy constructor.
Partial specialization for future<R&>
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
shared_future(const shared_future &__sf)
Copy constructor.
__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.
Non-standard RAII type for managing pointers obtained from allocators.
future_errc
Error code for futures.
Thrown to indicate error code of underlying system.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
shared_ptr< _Tp > allocate_shared(const _Alloc &__a, _Args &&...__args)
Create an object that is owned by a shared_ptr.
Primary template for future.
logic_error(const string &__arg) _GLIBCXX_TXN_SAFE
const error_category & future_category() noexcept
Points to a statically-allocated object derived from error_category.
void swap(basic_filebuf< _CharT, _Traits > &__x, basic_filebuf< _CharT, _Traits > &__y)
Swap specialization for filebufs.
Exception type thrown by futures.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
launch
Launch code for futures.
Primary template for shared_future.
__result_type _M_get_result() const
Wait for the state to be ready and rethrow any stored exception.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
A result object that uses an allocator.
One of two subclasses of exception.
exception_ptr make_exception_ptr(_Ex __ex) noexcept
Obtain an exception_ptr pointing to a copy of the supplied object.
shared_future(future< void > &&__uf) noexcept
Construct from a future rvalue.
future(future &&__uf) noexcept
Move constructor.
Common implementation for future and shared_future.
virtual const char * what() const noexcept
shared_ptr< _Tp > make_shared(_Args &&...__args)
Create an object that is owned by a shared_ptr.
Thrown as part of forced unwinding.A magic placeholder class that can be caught by reference to recog...
future_status
Status code for futures.