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 _M_promise->_M_storage->_M_set(*_M_arg);
466 return std::move(_M_promise->_M_storage);
468 promise<_Res>* _M_promise;
473 template<
typename _Res>
474 struct _Setter<_Res, _Res&&>
477 typename promise<_Res>::_Ptr_type operator()()
const
479 _M_promise->_M_storage->_M_set(std::move(*_M_arg));
480 return std::move(_M_promise->_M_storage);
482 promise<_Res>* _M_promise;
487 template<
typename _Res>
488 struct _Setter<_Res, void>
490 static_assert(is_void<_Res>::value,
"Only used for promise<void>");
492 typename promise<_Res>::_Ptr_type operator()()
const
493 {
return std::move(_M_promise->_M_storage); }
495 promise<_Res>* _M_promise;
498 struct __exception_ptr_tag { };
501 template<
typename _Res>
502 struct _Setter<_Res, __exception_ptr_tag>
505 typename promise<_Res>::_Ptr_type operator()()
const
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)
519 _S_check(__prom->_M_future);
523 template<
typename _Res>
524 static _Setter<_Res, __exception_ptr_tag>
525 __setter(exception_ptr& __ex, promise<_Res>* __prom)
527 _S_check(__prom->_M_future);
528 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
531 template<
typename _Res>
532 static _Setter<_Res, void>
533 __setter(promise<_Res>* __prom)
535 _S_check(__prom->_M_future);
536 return _Setter<_Res, void>{ __prom };
539 template<
typename _Tp>
541 _S_check(
const shared_ptr<_Tp>& __p)
543 if (!static_cast<bool>(__p))
544 __throw_future_error((
int)future_errc::no_state);
550 _M_do_set(
function<_Ptr_type()>* __f,
bool* __did_set)
552 _Ptr_type __res = (*__f)();
557 _M_result.swap(__res);
561 virtual void _M_complete_async() { }
564 virtual bool _M_is_deferred_future()
const {
return false; }
566 struct _Make_ready final : __at_thread_exit_elt
568 weak_ptr<_State_baseV2> _M_shared_state;
569 static void _S_run(
void*);
574 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
576 class _Async_state_common;
578 using _State_base = _State_baseV2;
579 class _Async_state_commonV2;
582 template<
typename _BoundFn,
typename =
typename _BoundFn::result_type>
583 class _Deferred_state;
585 template<
typename _BoundFn,
typename =
typename _BoundFn::result_type>
586 class _Async_state_impl;
588 template<
typename _Signature>
589 class _Task_state_base;
591 template<
typename _Fn,
typename _Alloc,
typename _Signature>
594 template<
typename _BoundFn>
596 _S_make_deferred_state(_BoundFn&& __fn);
598 template<
typename _BoundFn>
600 _S_make_async_state(_BoundFn&& __fn);
602 template<
typename _Res_ptr,
typename _Fn,
603 typename _Res =
typename _Res_ptr::element_type::result_type>
606 template<
typename _Res_ptr,
typename _BoundFn>
607 static _Task_setter<_Res_ptr, _BoundFn>
608 _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
615 template<
typename _Res>
618 typedef _Res& result_type;
620 _Result() noexcept : _M_value_ptr() { }
623 _M_set(_Res& __res) noexcept
626 _Res& _M_get() noexcept {
return *_M_value_ptr; }
631 void _M_destroy() {
delete this; }
638 typedef void result_type;
641 void _M_destroy() {
delete this; }
644 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
647 template<
typename _Res,
typename _Arg>
653 template<
typename _Res_ptr,
typename _Fn,
typename _Res>
659 template<
typename _Res>
675 valid()
const noexcept {
return static_cast<bool>(_M_state); }
680 _State_base::_S_check(_M_state);
684 template<
typename _Rep,
typename _Period>
688 _State_base::_S_check(_M_state);
689 return _M_state->wait_for(__rel);
692 template<
typename _Clock,
typename _Duration>
696 _State_base::_S_check(_M_state);
697 return _M_state->wait_until(__abs);
705 _State_base::_S_check(_M_state);
707 if (!(__res._M_error == 0))
714 _M_state.swap(__that._M_state);
719 __basic_future(
const __state_type& __state) : _M_state(__state)
721 _State_base::_S_check(_M_state);
722 _M_state->_M_set_retrieved_flag();
727 __basic_future(
const shared_future<_Res>&) noexcept;
731 __basic_future(shared_future<_Res>&&) noexcept;
735 __basic_future(future<_Res>&&) noexcept;
737 constexpr __basic_future() noexcept : _M_state() { }
741 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
742 ~_Reset() { _M_fut._M_state.reset(); }
743 __basic_future& _M_fut;
749 template<
typename _Res>
750 class future :
public __basic_future<_Res>
752 friend class promise<_Res>;
753 template<
typename>
friend class packaged_task;
754 template<
typename _Fn,
typename... _Args>
755 friend future<__async_result_of<_Fn, _Args...>>
758 typedef __basic_future<_Res> _Base_type;
759 typedef typename _Base_type::__state_type __state_type;
762 future(
const __state_type& __state) : _Base_type(__state) { }
765 constexpr future() noexcept : _Base_type() { }
776 future(std::move(__fut))._M_swap(*
this);
784 typename _Base_type::_Reset __reset(*
this);
792 template<
typename _Res>
796 template<
typename>
friend class packaged_task;
797 template<
typename _Fn,
typename... _Args>
798 friend future<__async_result_of<_Fn, _Args...>>
805 future(
const __state_type& __state) : _Base_type(__state) { }
808 constexpr
future() noexcept : _Base_type() { }
819 future(std::move(__fut))._M_swap(*
this);
827 typename _Base_type::_Reset __reset(*
this);
828 return this->_M_get_result()._M_get();
839 template<
typename>
friend class packaged_task;
840 template<
typename _Fn,
typename... _Args>
841 friend future<__async_result_of<_Fn, _Args...>>
848 future(
const __state_type& __state) : _Base_type(__state) { }
851 constexpr
future() noexcept : _Base_type() { }
862 future(std::move(__fut))._M_swap(*
this);
870 typename _Base_type::_Reset __reset(*
this);
879 template<
typename _Res>
880 class shared_future :
public __basic_future<_Res>
882 typedef __basic_future<_Res> _Base_type;
885 constexpr shared_future() noexcept : _Base_type() { }
892 : _Base_type(std::move(__uf))
897 : _Base_type(std::move(__sf))
906 shared_future& operator=(shared_future&& __sf) noexcept
908 shared_future(std::move(__sf))._M_swap(*
this);
918 template<
typename _Res>
931 : _Base_type(std::move(__uf))
936 : _Base_type(std::move(__sf))
945 shared_future& operator=(shared_future&& __sf) noexcept
947 shared_future(std::move(__sf))._M_swap(*
this);
953 get()
const {
return this->_M_get_result()._M_get(); }
970 : _Base_type(std::move(__uf))
975 : _Base_type(std::move(__sf))
984 shared_future& operator=(shared_future&& __sf) noexcept
986 shared_future(std::move(__sf))._M_swap(*
this);
996 template<
typename _Res>
997 inline __basic_future<_Res>::
998 __basic_future(
const shared_future<_Res>& __sf) noexcept
999 : _M_state(__sf._M_state)
1002 template<
typename _Res>
1003 inline __basic_future<_Res>::
1004 __basic_future(shared_future<_Res>&& __sf) noexcept
1005 : _M_state(std::move(__sf._M_state))
1008 template<
typename _Res>
1009 inline __basic_future<_Res>::
1010 __basic_future(future<_Res>&& __uf) noexcept
1011 : _M_state(std::move(__uf._M_state))
1014 template<
typename _Res>
1015 inline shared_future<_Res>
1016 future<_Res>::share()
1017 {
return shared_future<_Res>(std::move(*
this)); }
1019 template<
typename _Res>
1020 inline shared_future<_Res&>
1021 future<_Res&>::share()
1022 {
return shared_future<_Res&>(std::move(*
this)); }
1024 inline shared_future<void>
1025 future<void>::share()
1026 {
return shared_future<void>(std::move(*
this)); }
1029 template<
typename _Res>
1032 typedef __future_base::_State_base _State;
1033 typedef __future_base::_Result<_Res> _Res_type;
1034 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1035 template<
typename,
typename>
friend class _State::_Setter;
1038 shared_ptr<_State> _M_future;
1039 _Ptr_type _M_storage;
1044 _M_storage(new _Res_type())
1047 promise(promise&& __rhs) noexcept
1048 : _M_future(std::move(__rhs._M_future)),
1049 _M_storage(std::move(__rhs._M_storage))
1052 template<
typename _Allocator>
1053 promise(allocator_arg_t,
const _Allocator& __a)
1055 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1058 template<
typename _Allocator>
1059 promise(allocator_arg_t,
const _Allocator&, promise&& __rhs)
1060 : _M_future(std::move(__rhs._M_future)),
1061 _M_storage(std::move(__rhs._M_storage))
1064 promise(
const promise&) =
delete;
1068 if (static_cast<bool>(_M_future) && !_M_future.unique())
1069 _M_future->_M_break_promise(std::move(_M_storage));
1074 operator=(promise&& __rhs) noexcept
1076 promise(std::move(__rhs)).swap(*
this);
1080 promise& operator=(
const promise&) =
delete;
1083 swap(promise& __rhs) noexcept
1085 _M_future.swap(__rhs._M_future);
1086 _M_storage.
swap(__rhs._M_storage);
1092 {
return future<_Res>(_M_future); }
1096 set_value(
const _Res& __r)
1097 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1100 set_value(_Res&& __r)
1101 { _M_future->_M_set_result(_State::__setter(
this, std::move(__r))); }
1104 set_exception(exception_ptr __p)
1105 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1108 set_value_at_thread_exit(
const _Res& __r)
1110 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1115 set_value_at_thread_exit(_Res&& __r)
1117 _M_future->_M_set_delayed_result(
1118 _State::__setter(
this, std::move(__r)), _M_future);
1122 set_exception_at_thread_exit(exception_ptr __p)
1124 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1129 template<
typename _Res>
1131 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1134 template<
typename _Res,
typename _Alloc>
1135 struct uses_allocator<promise<_Res>, _Alloc>
1140 template<
typename _Res>
1143 typedef __future_base::_State_base _State;
1146 template<
typename,
typename>
friend class _State::_Setter;
1150 _Ptr_type _M_storage;
1154 : _M_future(std::make_shared<_State>()),
1155 _M_storage(
new _Res_type())
1159 : _M_future(std::move(__rhs._M_future)),
1160 _M_storage(std::move(__rhs._M_storage))
1163 template<
typename _Allocator>
1165 : _M_future(std::allocate_shared<_State>(__a)),
1166 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1169 template<
typename _Allocator>
1171 : _M_future(std::move(__rhs._M_future)),
1172 _M_storage(std::move(__rhs._M_storage))
1179 if (static_cast<bool>(_M_future) && !_M_future.unique())
1180 _M_future->_M_break_promise(std::move(_M_storage));
1185 operator=(
promise&& __rhs) noexcept
1187 promise(std::move(__rhs)).swap(*
this);
1196 _M_future.swap(__rhs._M_future);
1197 _M_storage.
swap(__rhs._M_storage);
1207 set_value(_Res& __r)
1208 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1211 set_exception(exception_ptr __p)
1212 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1215 set_value_at_thread_exit(_Res& __r)
1217 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1222 set_exception_at_thread_exit(exception_ptr __p)
1224 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1233 typedef __future_base::_State_base _State;
1236 template<
typename,
typename>
friend class _State::_Setter;
1244 : _M_future(std::make_shared<_State>()),
1249 : _M_future(std::move(__rhs._M_future)),
1250 _M_storage(std::move(__rhs._M_storage))
1253 template<
typename _Allocator>
1255 : _M_future(std::allocate_shared<_State>(__a)),
1256 _M_storage(__future_base::_S_allocate_result<void>(__a))
1261 template<
typename _Allocator>
1263 : _M_future(std::move(__rhs._M_future)),
1264 _M_storage(std::move(__rhs._M_storage))
1271 if (static_cast<bool>(_M_future) && !_M_future.unique())
1272 _M_future->_M_break_promise(std::move(_M_storage));
1277 operator=(
promise&& __rhs) noexcept
1279 promise(std::move(__rhs)).swap(*
this);
1288 _M_future.swap(__rhs._M_future);
1289 _M_storage.
swap(__rhs._M_storage);
1300 { _M_future->_M_set_result(_State::__setter(
this)); }
1303 set_exception(exception_ptr __p)
1304 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1307 set_value_at_thread_exit()
1308 { _M_future->_M_set_delayed_result(_State::__setter(
this), _M_future); }
1311 set_exception_at_thread_exit(exception_ptr __p)
1313 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1318 template<
typename _Ptr_type,
typename _Fn,
typename _Res>
1319 struct __future_base::_Task_setter
1322 _Ptr_type operator()()
const
1326 (*_M_result)->_M_set((*_M_fn)());
1330 __throw_exception_again;
1336 return std::move(*_M_result);
1338 _Ptr_type* _M_result;
1342 template<
typename _Ptr_type,
typename _Fn>
1343 struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1345 _Ptr_type operator()()
const
1353 __throw_exception_again;
1359 return std::move(*_M_result);
1361 _Ptr_type* _M_result;
1366 template<
typename _Res,
typename... _Args>
1367 struct __future_base::_Task_state_base<_Res(_Args...)>
1368 : __future_base::_State_base
1370 typedef _Res _Res_type;
1372 template<
typename _Alloc>
1373 _Task_state_base(
const _Alloc& __a)
1374 : _M_result(_S_allocate_result<_Res>(__a))
1379 _M_run(_Args&&... __args) = 0;
1383 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1385 virtual shared_ptr<_Task_state_base>
1388 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1389 _Ptr_type _M_result;
1393 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1394 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1395 : __future_base::_Task_state_base<_Res(_Args...)>
1397 template<
typename _Fn2>
1398 _Task_state(_Fn2&& __fn,
const _Alloc& __a)
1399 : _Task_state_base<_Res(_Args...)>(__a),
1400 _M_impl(std::
forward<_Fn2>(__fn), __a)
1405 _M_run(_Args&&... __args)
1408 auto __boundfn = std::__bind_simple(
std::ref(_M_impl._M_fn),
1409 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1410 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1414 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1417 auto __boundfn = std::__bind_simple(
std::ref(_M_impl._M_fn),
1418 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1419 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1423 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1426 template<
typename _Tp>
1427 static reference_wrapper<_Tp>
1428 _S_maybe_wrap_ref(_Tp& __t)
1431 template<
typename _Tp>
1433 typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
1434 _S_maybe_wrap_ref(_Tp&& __t)
1435 {
return std::forward<_Tp>(__t); }
1437 struct _Impl : _Alloc
1439 template<
typename _Fn2>
1440 _Impl(_Fn2&& __fn,
const _Alloc& __a)
1441 : _Alloc(__a), _M_fn(std::
forward<_Fn2>(__fn)) { }
1446 template<
typename _Signature,
typename _Fn,
typename _Alloc>
1447 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1448 __create_task_state(_Fn&& __fn,
const _Alloc& __a)
1450 typedef typename decay<_Fn>::type _Fn2;
1451 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1452 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1455 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1456 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1457 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1459 return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
1460 static_cast<_Alloc&
>(_M_impl));
1463 template<
typename _Task,
typename _Fn,
bool
1464 = is_same<_Task, typename decay<_Fn>::type>::value>
1465 struct __constrain_pkgdtask
1466 {
typedef void __type; };
1468 template<
typename _Task,
typename _Fn>
1469 struct __constrain_pkgdtask<_Task, _Fn, true>
1473 template<
typename _Res,
typename... _ArgTypes>
1474 class packaged_task<_Res(_ArgTypes...)>
1476 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1481 packaged_task() noexcept { }
1485 template<
typename _Allocator>
1489 template<
typename _Fn,
typename =
typename
1490 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1492 packaged_task(_Fn&& __fn)
1494 std::forward<_Fn>(__fn))
1500 template<
typename _Fn,
typename _Alloc,
typename =
typename
1501 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1503 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1504 std::forward<_Fn>(__fn), __a))
1509 if (static_cast<bool>(_M_state) && !_M_state.unique())
1510 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1514 packaged_task(
const packaged_task&) =
delete;
1515 packaged_task& operator=(
const packaged_task&) =
delete;
1517 template<
typename _Allocator>
1519 const packaged_task&) =
delete;
1522 packaged_task(packaged_task&& __other) noexcept
1523 { this->swap(__other); }
1525 template<
typename _Allocator>
1527 packaged_task&& __other) noexcept
1528 { this->swap(__other); }
1530 packaged_task& operator=(packaged_task&& __other) noexcept
1532 packaged_task(std::move(__other)).swap(*
this);
1537 swap(packaged_task& __other) noexcept
1538 { _M_state.swap(__other._M_state); }
1541 valid()
const noexcept
1542 {
return static_cast<bool>(_M_state); }
1551 operator()(_ArgTypes... __args)
1553 __future_base::_State_base::_S_check(_M_state);
1554 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1558 make_ready_at_thread_exit(_ArgTypes... __args)
1560 __future_base::_State_base::_S_check(_M_state);
1561 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1567 __future_base::_State_base::_S_check(_M_state);
1568 packaged_task __tmp;
1569 __tmp._M_state = _M_state;
1570 _M_state = _M_state->_M_reset();
1575 template<
typename _Res,
typename... _ArgTypes>
1577 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1578 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1581 template<
typename _Res,
typename _Alloc>
1582 struct uses_allocator<packaged_task<_Res>, _Alloc>
1588 template<
typename _BoundFn,
typename _Res>
1589 class __future_base::_Deferred_state final
1590 :
public __future_base::_State_base
1594 _Deferred_state(_BoundFn&& __fn)
1595 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1599 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1600 _Ptr_type _M_result;
1613 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1618 virtual bool _M_is_deferred_future()
const {
return true; }
1622 class __future_base::_Async_state_commonV2
1623 :
public __future_base::_State_base
1626 ~_Async_state_commonV2() =
default;
1643 virtual void _M_complete_async() { _M_join(); }
1645 void _M_join() {
std::call_once(_M_once, &thread::join, &_M_thread); }
1653 template<
typename _BoundFn,
typename _Res>
1654 class __future_base::_Async_state_impl final
1655 :
public __future_base::_Async_state_commonV2
1659 _Async_state_impl(_BoundFn&& __fn)
1660 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1665 _M_set_result(_S_task_setter(_M_result, _M_fn));
1670 if (static_cast<bool>(_M_result))
1671 this->_M_break_promise(std::move(_M_result));
1672 __throw_exception_again;
1680 ~_Async_state_impl() {
if (_M_thread.joinable()) _M_thread.join(); }
1683 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1684 _Ptr_type _M_result;
1688 template<
typename _BoundFn>
1690 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1692 typedef typename remove_reference<_BoundFn>::type __fn_type;
1693 typedef _Deferred_state<__fn_type> __state_type;
1694 return std::make_shared<__state_type>(std::move(__fn));
1697 template<
typename _BoundFn>
1699 __future_base::_S_make_async_state(_BoundFn&& __fn)
1701 typedef typename remove_reference<_BoundFn>::type __fn_type;
1702 typedef _Async_state_impl<__fn_type> __state_type;
1703 return std::make_shared<__state_type>(std::move(__fn));
1708 template<
typename _Fn,
typename... _Args>
1709 future<__async_result_of<_Fn, _Args...>>
1713 if ((__policy & launch::async) == launch::async)
1717 __state = __future_base::_S_make_async_state(std::__bind_simple(
1718 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1720 #if __cpp_exceptions
1723 if (__e.code() != errc::resource_unavailable_try_again
1724 || (__policy & launch::deferred) != launch::deferred)
1731 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1732 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1734 return future<__async_result_of<_Fn, _Args...>>(__state);
1738 template<
typename _Fn,
typename... _Args>
1739 inline future<__async_result_of<_Fn, _Args...>>
1742 return std::async(launch::async|launch::deferred,
1743 std::forward<_Fn>(__fn),
1744 std::forward<_Args>(__args)...);
1747 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
1748 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1752 _GLIBCXX_END_NAMESPACE_VERSION
1757 #endif // _GLIBCXX_FUTURE
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
exception_ptr make_exception_ptr(_Ex __ex) noexcept
Obtain an exception_ptr pointing to a copy of the supplied object.
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
launch
Launch code for futures.
Exception type thrown by futures.
Primary template for shared_future.
_Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
future(future &&__uf) noexcept
Move constructor.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
A result object that uses an allocator.
Common implementation for future and shared_future.
const error_category & future_category() noexcept
Points to a statically-allocated object derived from error_category.
Partial specialization for reference types.
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
call_once
One of two subclasses of exception.
void swap(packaged_task< _Res(_ArgTypes...)> &__x, packaged_task< _Res(_ArgTypes...)> &__y) noexcept
swap
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.
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.
Non-standard RAII type for managing pointers obtained from allocators.
shared_future(future< _Res > &&__uf) noexcept
Construct from a future rvalue.
Explicit specialization for void.
Thrown as part of forced unwinding.A magic placeholder class that can be caught by reference to recog...
future_status
Status code for futures.
20.7.1.2 unique_ptr for single objects.
A result object that has storage for an object of type _Res.
Explicit specialization for shared_future<void>
future(future &&__uf) noexcept
Move constructor.
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
Explicit specialization for future<void>
shared_future(shared_future &&__sf) noexcept
Construct from a shared_future rvalue.
shared_ptr< _Tp > make_shared(_Args &&...__args)
Create an object that is owned by a shared_ptr.
__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.
Primary template for promise.
__result_type _M_get_result() const
Wait for the state to be ready and rethrow any stored exception.
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
shared_ptr< _Tp > allocate_shared(const _Alloc &__a, _Args &&...__args)
Create an object that is owned by a shared_ptr.
Partial specialization for future<R&>
The standard allocator, as per [20.4].
Base class and enclosing scope.
shared_future(const shared_future &__sf) noexcept
Copy constructor.
Partial specialization for shared_future<R&>
future< __async_result_of< _Fn, _Args...> > async(_Fn &&__fn, _Args &&...__args)
async, potential overload
future< __async_result_of< _Fn, _Args...> > async(launch __policy, _Fn &&__fn, _Args &&...__args)
async
exception_ptr current_exception() noexcept
logic_error(const string &__arg) _GLIBCXX_TXN_SAFE
shared_future(const shared_future &__sf)
Copy constructor.
shared_future(future< void > &&__uf) noexcept
Construct from a future rvalue.
future(future &&__uf) noexcept
Move constructor.
void rethrow_exception(exception_ptr) __attribute__((__noreturn__))
Throw the object pointed to by the exception_ptr.
virtual const char * what() const noexcept
future_errc
Error code for futures.
shared_future(future< _Res & > &&__uf) noexcept
Construct from a future rvalue.
Thrown to indicate error code of underlying system.
shared_future(const shared_future &__sf)
Copy constructor.
Primary template for future.