29 #ifndef _GLIBCXX_FUTURE
30 #define _GLIBCXX_FUTURE 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
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; }
171 template<
typename _Fn,
typename... _Args>
172 future<
typename result_of<_Fn(_Args...)>::type>
173 async(
launch __policy, _Fn&& __fn, _Args&&... __args);
175 template<
typename _Fn,
typename... _Args>
176 future<
typename result_of<_Fn(_Args...)>::type>
177 async(_Fn&& __fn, _Args&&... __args);
179 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
180 && (ATOMIC_INT_LOCK_FREE > 1)
188 exception_ptr _M_error;
190 _Result_base(
const _Result_base&) =
delete;
191 _Result_base& operator=(
const _Result_base&) =
delete;
194 virtual void _M_destroy() = 0;
198 void operator()(_Result_base* __fr)
const { __fr->_M_destroy(); }
203 virtual ~_Result_base();
207 template<
typename _Res>
208 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
211 template<
typename _Res>
212 struct _Result : _Result_base
215 __gnu_cxx::__aligned_buffer<_Res> _M_storage;
219 typedef _Res result_type;
221 _Result() noexcept : _M_initialized() { }
231 _M_value() noexcept {
return *_M_storage._M_ptr(); }
234 _M_set(
const _Res& __res)
236 ::new (_M_storage._M_addr()) _Res(__res);
237 _M_initialized =
true;
243 ::new (_M_storage._M_addr()) _Res(std::move(__res));
244 _M_initialized =
true;
248 void _M_destroy() {
delete this; }
252 template<
typename _Res,
typename _Alloc>
253 struct _Result_alloc final : _Result<_Res>, _Alloc
255 using __allocator_type = __alloc_rebind<_Alloc, _Result_alloc>;
258 _Result_alloc(
const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
264 __allocator_type __a(*
this);
265 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
266 this->~_Result_alloc();
271 template<
typename _Res,
typename _Allocator>
272 static _Ptr<_Result_alloc<_Res, _Allocator>>
273 _S_allocate_result(
const _Allocator& __a)
275 using __result_type = _Result_alloc<_Res, _Allocator>;
276 typename __result_type::__allocator_type __a2(__a);
278 __result_type* __p = ::new((
void*)__guard.get()) __result_type{__a};
280 return _Ptr<__result_type>(__p);
284 template<
typename _Res,
typename _Tp>
285 static _Ptr<_Result<_Res>>
288 return _Ptr<_Result<_Res>>(
new _Result<_Res>);
296 typedef _Ptr<_Result_base> _Ptr_type;
298 enum _Status :
unsigned {
304 __atomic_futex_unsigned<> _M_status;
305 atomic_flag _M_retrieved = ATOMIC_FLAG_INIT;
309 _State_baseV2() noexcept : _M_result(), _M_status(_Status::__not_ready)
311 _State_baseV2(
const _State_baseV2&) =
delete;
312 _State_baseV2& operator=(
const _State_baseV2&) =
delete;
313 virtual ~_State_baseV2() =
default;
322 _M_status._M_load_when_equal(_Status::__ready, memory_order_acquire);
326 template<
typename _Rep,
typename _Period>
328 wait_for(
const chrono::duration<_Rep, _Period>& __rel)
332 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
333 return future_status::ready;
334 if (_M_is_deferred_future())
335 return future_status::deferred;
336 if (_M_status._M_load_when_equal_for(_Status::__ready,
337 memory_order_acquire, __rel))
350 return future_status::ready;
352 return future_status::timeout;
355 template<
typename _Clock,
typename _Duration>
357 wait_until(
const chrono::time_point<_Clock, _Duration>& __abs)
361 if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
362 return future_status::ready;
363 if (_M_is_deferred_future())
364 return future_status::deferred;
365 if (_M_status._M_load_when_equal_until(_Status::__ready,
366 memory_order_acquire, __abs))
373 return future_status::ready;
375 return future_status::timeout;
381 _M_set_result(
function<_Ptr_type()> __res,
bool __ignore_failure =
false)
383 bool __did_set =
false;
386 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
390 _M_status._M_store_notify_all(_Status::__ready,
391 memory_order_release);
392 else if (!__ignore_failure)
393 __throw_future_error(
int(future_errc::promise_already_satisfied));
400 _M_set_delayed_result(
function<_Ptr_type()> __res,
401 weak_ptr<_State_baseV2> __self)
403 bool __did_set =
false;
404 unique_ptr<_Make_ready> __mr{
new _Make_ready};
407 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
410 __throw_future_error(
int(future_errc::promise_already_satisfied));
411 __mr->_M_shared_state = std::move(__self);
418 _M_break_promise(_Ptr_type __res)
420 if (static_cast<bool>(__res))
422 error_code __ec(make_error_code(future_errc::broken_promise));
428 _M_result.swap(__res);
430 _M_status._M_store_notify_all(_Status::__ready,
431 memory_order_release);
437 _M_set_retrieved_flag()
439 if (_M_retrieved.test_and_set())
440 __throw_future_error(
int(future_errc::future_already_retrieved));
443 template<
typename _Res,
typename _Arg>
447 template<
typename _Res,
typename _Arg>
448 struct _Setter<_Res, _Arg&>
452 static_assert(is_same<_Res, _Arg&>::value
453 || is_same<const _Res, _Arg>::value,
454 "Invalid specialisation");
457 typename promise<_Res>::_Ptr_type operator()()
const
459 _State_baseV2::_S_check(_M_promise->_M_future);
460 _M_promise->_M_storage->_M_set(*_M_arg);
461 return std::move(_M_promise->_M_storage);
463 promise<_Res>* _M_promise;
468 template<
typename _Res>
469 struct _Setter<_Res, _Res&&>
472 typename promise<_Res>::_Ptr_type operator()()
const
474 _State_baseV2::_S_check(_M_promise->_M_future);
475 _M_promise->_M_storage->_M_set(std::move(*_M_arg));
476 return std::move(_M_promise->_M_storage);
478 promise<_Res>* _M_promise;
482 struct __exception_ptr_tag { };
485 template<
typename _Res>
486 struct _Setter<_Res, __exception_ptr_tag>
489 typename promise<_Res>::_Ptr_type operator()()
const
491 _State_baseV2::_S_check(_M_promise->_M_future);
492 _M_promise->_M_storage->_M_error = *_M_ex;
493 return std::move(_M_promise->_M_storage);
496 promise<_Res>* _M_promise;
497 exception_ptr* _M_ex;
500 template<
typename _Res,
typename _Arg>
501 static _Setter<_Res, _Arg&&>
502 __setter(promise<_Res>* __prom, _Arg&& __arg)
504 return _Setter<_Res, _Arg&&>{ __prom, &__arg };
507 template<
typename _Res>
508 static _Setter<_Res, __exception_ptr_tag>
509 __setter(exception_ptr& __ex, promise<_Res>* __prom)
511 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
514 template<
typename _Tp>
516 _S_check(
const shared_ptr<_Tp>& __p)
518 if (!static_cast<bool>(__p))
519 __throw_future_error((
int)future_errc::no_state);
525 _M_do_set(
function<_Ptr_type()>* __f,
bool* __did_set)
527 _Ptr_type __res = (*__f)();
532 _M_result.swap(__res);
536 virtual void _M_complete_async() { }
539 virtual bool _M_is_deferred_future()
const {
return false; }
541 struct _Make_ready final : __at_thread_exit_elt
543 weak_ptr<_State_baseV2> _M_shared_state;
544 static void _S_run(
void*);
549 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
551 class _Async_state_common;
553 using _State_base = _State_baseV2;
554 class _Async_state_commonV2;
557 template<
typename _BoundFn,
typename =
typename _BoundFn::result_type>
558 class _Deferred_state;
560 template<
typename _BoundFn,
typename =
typename _BoundFn::result_type>
561 class _Async_state_impl;
563 template<
typename _Signature>
564 class _Task_state_base;
566 template<
typename _Fn,
typename _Alloc,
typename _Signature>
569 template<
typename _BoundFn>
571 _S_make_deferred_state(_BoundFn&& __fn);
573 template<
typename _BoundFn>
575 _S_make_async_state(_BoundFn&& __fn);
577 template<
typename _Res_ptr,
typename _Fn,
578 typename _Res =
typename _Res_ptr::element_type::result_type>
581 template<
typename _Res_ptr,
typename _BoundFn>
582 static _Task_setter<_Res_ptr, _BoundFn>
583 _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
590 template<
typename _Res>
591 struct __future_base::_Result<_Res&> : __future_base::_Result_base
593 typedef _Res& result_type;
595 _Result() noexcept : _M_value_ptr() { }
598 _M_set(_Res& __res) noexcept
601 _Res& _M_get() noexcept {
return *_M_value_ptr; }
606 void _M_destroy() {
delete this; }
611 struct __future_base::_Result<void> : __future_base::_Result_base
613 typedef void result_type;
616 void _M_destroy() {
delete this; }
619 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
622 template<
typename _Res,
typename _Arg>
623 struct __is_location_invariant
624 <__future_base::_State_base::_Setter<_Res, _Arg>>
628 template<
typename _Res_ptr,
typename _Fn,
typename _Res>
629 struct __is_location_invariant
630 <__future_base::_Task_setter<_Res_ptr, _Fn, _Res>>
634 template<
typename _Res>
635 class __basic_future :
public __future_base
638 typedef shared_ptr<_State_base> __state_type;
639 typedef __future_base::_Result<_Res>& __result_type;
642 __state_type _M_state;
646 __basic_future(
const __basic_future&) =
delete;
647 __basic_future& operator=(
const __basic_future&) =
delete;
650 valid() const noexcept {
return static_cast<bool>(_M_state); }
655 _State_base::_S_check(_M_state);
659 template<
typename _Rep,
typename _Period>
661 wait_for(
const chrono::duration<_Rep, _Period>& __rel)
const
663 _State_base::_S_check(_M_state);
664 return _M_state->wait_for(__rel);
667 template<
typename _Clock,
typename _Duration>
669 wait_until(
const chrono::time_point<_Clock, _Duration>& __abs)
const
671 _State_base::_S_check(_M_state);
672 return _M_state->wait_until(__abs);
678 _M_get_result()
const
680 _State_base::_S_check(_M_state);
681 _Result_base& __res = _M_state->wait();
682 if (!(__res._M_error == 0))
684 return static_cast<__result_type
>(__res);
687 void _M_swap(__basic_future& __that) noexcept
689 _M_state.swap(__that._M_state);
694 __basic_future(
const __state_type& __state) : _M_state(__state)
696 _State_base::_S_check(_M_state);
697 _M_state->_M_set_retrieved_flag();
702 __basic_future(
const shared_future<_Res>&) noexcept;
706 __basic_future(shared_future<_Res>&&) noexcept;
710 __basic_future(future<_Res>&&) noexcept;
712 constexpr __basic_future() noexcept : _M_state() { }
716 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
717 ~_Reset() { _M_fut._M_state.reset(); }
718 __basic_future& _M_fut;
724 template<
typename _Res>
725 class future :
public __basic_future<_Res>
727 friend class promise<_Res>;
728 template<
typename>
friend class packaged_task;
729 template<
typename _Fn,
typename... _Args>
730 friend future<
typename result_of<_Fn(_Args...)>::type>
731 async(
launch, _Fn&&, _Args&&...);
733 typedef __basic_future<_Res> _Base_type;
734 typedef typename _Base_type::__state_type __state_type;
737 future(
const __state_type& __state) : _Base_type(__state) { }
740 constexpr future() noexcept : _Base_type() { }
743 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
746 future(
const future&) =
delete;
747 future& operator=(
const future&) =
delete;
749 future& operator=(future&& __fut) noexcept
751 future(std::move(__fut))._M_swap(*
this);
759 typename _Base_type::_Reset __reset(*
this);
760 return std::move(this->_M_get_result()._M_value());
763 shared_future<_Res> share();
767 template<
typename _Res>
768 class future<_Res&> :
public __basic_future<_Res&>
770 friend class promise<_Res&>;
771 template<
typename>
friend class packaged_task;
772 template<
typename _Fn,
typename... _Args>
773 friend future<
typename result_of<_Fn(_Args...)>::type>
774 async(
launch, _Fn&&, _Args&&...);
776 typedef __basic_future<_Res&> _Base_type;
777 typedef typename _Base_type::__state_type __state_type;
780 future(
const __state_type& __state) : _Base_type(__state) { }
783 constexpr future() noexcept : _Base_type() { }
786 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
789 future(
const future&) =
delete;
790 future& operator=(
const future&) =
delete;
792 future& operator=(future&& __fut) noexcept
794 future(std::move(__fut))._M_swap(*
this);
802 typename _Base_type::_Reset __reset(*
this);
803 return this->_M_get_result()._M_get();
806 shared_future<_Res&> share();
811 class future<void> :
public __basic_future<void>
813 friend class promise<void>;
814 template<
typename>
friend class packaged_task;
815 template<
typename _Fn,
typename... _Args>
816 friend future<
typename result_of<_Fn(_Args...)>::type>
817 async(
launch, _Fn&&, _Args&&...);
819 typedef __basic_future<void> _Base_type;
820 typedef typename _Base_type::__state_type __state_type;
823 future(
const __state_type& __state) : _Base_type(__state) { }
826 constexpr future() noexcept : _Base_type() { }
829 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
832 future(
const future&) =
delete;
833 future& operator=(
const future&) =
delete;
835 future& operator=(future&& __fut) noexcept
837 future(std::move(__fut))._M_swap(*
this);
845 typename _Base_type::_Reset __reset(*
this);
846 this->_M_get_result();
849 shared_future<void> share();
854 template<
typename _Res>
855 class shared_future :
public __basic_future<_Res>
857 typedef __basic_future<_Res> _Base_type;
860 constexpr shared_future() noexcept : _Base_type() { }
863 shared_future(
const shared_future& __sf) : _Base_type(__sf) { }
866 shared_future(future<_Res>&& __uf) noexcept
867 : _Base_type(std::move(__uf))
871 shared_future(shared_future&& __sf) noexcept
872 : _Base_type(std::move(__sf))
875 shared_future& operator=(
const shared_future& __sf)
877 shared_future(__sf)._M_swap(*
this);
881 shared_future& operator=(shared_future&& __sf) noexcept
883 shared_future(std::move(__sf))._M_swap(*
this);
889 get()
const {
return this->_M_get_result()._M_value(); }
893 template<
typename _Res>
894 class shared_future<_Res&> :
public __basic_future<_Res&>
896 typedef __basic_future<_Res&> _Base_type;
899 constexpr shared_future() noexcept : _Base_type() { }
902 shared_future(
const shared_future& __sf) : _Base_type(__sf) { }
905 shared_future(future<_Res&>&& __uf) noexcept
906 : _Base_type(std::move(__uf))
910 shared_future(shared_future&& __sf) noexcept
911 : _Base_type(std::move(__sf))
914 shared_future& operator=(
const shared_future& __sf)
916 shared_future(__sf)._M_swap(*
this);
920 shared_future& operator=(shared_future&& __sf) noexcept
922 shared_future(std::move(__sf))._M_swap(*
this);
928 get()
const {
return this->_M_get_result()._M_get(); }
933 class shared_future<void> :
public __basic_future<void>
935 typedef __basic_future<void> _Base_type;
938 constexpr shared_future() noexcept : _Base_type() { }
941 shared_future(
const shared_future& __sf) : _Base_type(__sf) { }
944 shared_future(future<void>&& __uf) noexcept
945 : _Base_type(std::move(__uf))
949 shared_future(shared_future&& __sf) noexcept
950 : _Base_type(std::move(__sf))
953 shared_future& operator=(
const shared_future& __sf)
955 shared_future(__sf)._M_swap(*
this);
959 shared_future& operator=(shared_future&& __sf) noexcept
961 shared_future(std::move(__sf))._M_swap(*
this);
967 get()
const { this->_M_get_result(); }
971 template<
typename _Res>
972 inline __basic_future<_Res>::
973 __basic_future(
const shared_future<_Res>& __sf) noexcept
974 : _M_state(__sf._M_state)
977 template<
typename _Res>
978 inline __basic_future<_Res>::
979 __basic_future(shared_future<_Res>&& __sf) noexcept
980 : _M_state(std::move(__sf._M_state))
983 template<
typename _Res>
984 inline __basic_future<_Res>::
985 __basic_future(future<_Res>&& __uf) noexcept
986 : _M_state(std::move(__uf._M_state))
989 template<
typename _Res>
990 inline shared_future<_Res>
991 future<_Res>::share()
992 {
return shared_future<_Res>(std::move(*
this)); }
994 template<
typename _Res>
995 inline shared_future<_Res&>
996 future<_Res&>::share()
997 {
return shared_future<_Res&>(std::move(*
this)); }
999 inline shared_future<void>
1000 future<void>::share()
1001 {
return shared_future<void>(std::move(*
this)); }
1004 template<
typename _Res>
1007 typedef __future_base::_State_base _State;
1008 typedef __future_base::_Result<_Res> _Res_type;
1009 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1010 template<
typename,
typename>
friend class _State::_Setter;
1012 shared_ptr<_State> _M_future;
1013 _Ptr_type _M_storage;
1018 _M_storage(new _Res_type())
1021 promise(promise&& __rhs) noexcept
1022 : _M_future(std::move(__rhs._M_future)),
1023 _M_storage(std::move(__rhs._M_storage))
1026 template<
typename _Allocator>
1027 promise(allocator_arg_t,
const _Allocator& __a)
1029 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1032 template<
typename _Allocator>
1033 promise(allocator_arg_t,
const _Allocator&, promise&& __rhs)
1034 : _M_future(std::move(__rhs._M_future)),
1035 _M_storage(std::move(__rhs._M_storage))
1038 promise(
const promise&) =
delete;
1042 if (static_cast<bool>(_M_future) && !_M_future.unique())
1043 _M_future->_M_break_promise(std::move(_M_storage));
1048 operator=(promise&& __rhs) noexcept
1050 promise(std::move(__rhs)).swap(*
this);
1054 promise& operator=(
const promise&) =
delete;
1057 swap(promise& __rhs) noexcept
1059 _M_future.swap(__rhs._M_future);
1060 _M_storage.swap(__rhs._M_storage);
1066 {
return future<_Res>(_M_future); }
1070 set_value(
const _Res& __r)
1071 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1074 set_value(_Res&& __r)
1075 { _M_future->_M_set_result(_State::__setter(
this, std::move(__r))); }
1078 set_exception(exception_ptr __p)
1079 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1082 set_value_at_thread_exit(
const _Res& __r)
1084 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1089 set_value_at_thread_exit(_Res&& __r)
1091 _M_future->_M_set_delayed_result(
1092 _State::__setter(
this, std::move(__r)), _M_future);
1096 set_exception_at_thread_exit(exception_ptr __p)
1098 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1103 template<
typename _Res>
1105 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1108 template<
typename _Res,
typename _Alloc>
1109 struct uses_allocator<promise<_Res>, _Alloc>
1114 template<
typename _Res>
1115 class promise<_Res&>
1117 typedef __future_base::_State_base _State;
1118 typedef __future_base::_Result<_Res&> _Res_type;
1119 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1120 template<
typename,
typename>
friend class _State::_Setter;
1122 shared_ptr<_State> _M_future;
1123 _Ptr_type _M_storage;
1128 _M_storage(new _Res_type())
1131 promise(promise&& __rhs) noexcept
1132 : _M_future(std::move(__rhs._M_future)),
1133 _M_storage(std::move(__rhs._M_storage))
1136 template<
typename _Allocator>
1137 promise(allocator_arg_t,
const _Allocator& __a)
1139 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1142 template<
typename _Allocator>
1143 promise(allocator_arg_t,
const _Allocator&, promise&& __rhs)
1144 : _M_future(std::move(__rhs._M_future)),
1145 _M_storage(std::move(__rhs._M_storage))
1148 promise(
const promise&) =
delete;
1152 if (static_cast<bool>(_M_future) && !_M_future.unique())
1153 _M_future->_M_break_promise(std::move(_M_storage));
1158 operator=(promise&& __rhs) noexcept
1160 promise(std::move(__rhs)).swap(*
this);
1164 promise& operator=(
const promise&) =
delete;
1167 swap(promise& __rhs) noexcept
1169 _M_future.swap(__rhs._M_future);
1170 _M_storage.swap(__rhs._M_storage);
1176 {
return future<_Res&>(_M_future); }
1180 set_value(_Res& __r)
1181 { _M_future->_M_set_result(_State::__setter(
this, __r)); }
1184 set_exception(exception_ptr __p)
1185 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1188 set_value_at_thread_exit(_Res& __r)
1190 _M_future->_M_set_delayed_result(_State::__setter(
this, __r),
1195 set_exception_at_thread_exit(exception_ptr __p)
1197 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1206 typedef __future_base::_State_base _State;
1207 typedef __future_base::_Result<void> _Res_type;
1208 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1209 template<
typename,
typename>
friend class _State::_Setter;
1211 shared_ptr<_State> _M_future;
1212 _Ptr_type _M_storage;
1217 _M_storage(new _Res_type())
1220 promise(promise&& __rhs) noexcept
1221 : _M_future(std::move(__rhs._M_future)),
1222 _M_storage(std::move(__rhs._M_storage))
1225 template<
typename _Allocator>
1226 promise(allocator_arg_t,
const _Allocator& __a)
1228 _M_storage(__future_base::_S_allocate_result<void>(__a))
1233 template<
typename _Allocator>
1234 promise(allocator_arg_t,
const _Allocator&, promise&& __rhs)
1235 : _M_future(std::move(__rhs._M_future)),
1236 _M_storage(std::move(__rhs._M_storage))
1239 promise(
const promise&) =
delete;
1243 if (static_cast<bool>(_M_future) && !_M_future.unique())
1244 _M_future->_M_break_promise(std::move(_M_storage));
1249 operator=(promise&& __rhs) noexcept
1251 promise(std::move(__rhs)).swap(*
this);
1255 promise& operator=(
const promise&) =
delete;
1258 swap(promise& __rhs) noexcept
1260 _M_future.swap(__rhs._M_future);
1261 _M_storage.swap(__rhs._M_storage);
1267 {
return future<void>(_M_future); }
1273 set_exception(exception_ptr __p)
1274 { _M_future->_M_set_result(_State::__setter(__p,
this)); }
1277 set_value_at_thread_exit();
1280 set_exception_at_thread_exit(exception_ptr __p)
1282 _M_future->_M_set_delayed_result(_State::__setter(__p,
this),
1289 struct __future_base::_State_base::_Setter<void, void>
1291 promise<void>::_Ptr_type operator()()
const
1293 _State_base::_S_check(_M_promise->_M_future);
1294 return std::move(_M_promise->_M_storage);
1297 promise<void>* _M_promise;
1301 promise<void>::set_value()
1302 { _M_future->_M_set_result(_State::_Setter<void, void>{
this }); }
1305 promise<void>::set_value_at_thread_exit()
1307 _M_future->_M_set_delayed_result(_State::_Setter<void, void>{
this},
1311 template<
typename _Ptr_type,
typename _Fn,
typename _Res>
1312 struct __future_base::_Task_setter
1315 _Ptr_type operator()()
const
1319 (*_M_result)->_M_set((*_M_fn)());
1323 __throw_exception_again;
1329 return std::move(*_M_result);
1331 _Ptr_type* _M_result;
1335 template<
typename _Ptr_type,
typename _Fn>
1336 struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1338 _Ptr_type operator()()
const
1346 __throw_exception_again;
1352 return std::move(*_M_result);
1354 _Ptr_type* _M_result;
1359 template<
typename _Res,
typename... _Args>
1360 struct __future_base::_Task_state_base<_Res(_Args...)>
1361 : __future_base::_State_base
1363 typedef _Res _Res_type;
1365 template<
typename _Alloc>
1366 _Task_state_base(
const _Alloc& __a)
1367 : _M_result(_S_allocate_result<_Res>(__a))
1372 _M_run(_Args&&... __args) = 0;
1376 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1378 virtual shared_ptr<_Task_state_base>
1381 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1382 _Ptr_type _M_result;
1386 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1387 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1388 : __future_base::_Task_state_base<_Res(_Args...)>
1390 template<
typename _Fn2>
1391 _Task_state(_Fn2&& __fn,
const _Alloc& __a)
1392 : _Task_state_base<_Res(_Args...)>(__a),
1393 _M_impl(std::
forward<_Fn2>(__fn), __a)
1398 _M_run(_Args&&... __args)
1401 auto __boundfn = std::__bind_simple(
std::ref(_M_impl._M_fn),
1402 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1403 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1407 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1410 auto __boundfn = std::__bind_simple(
std::ref(_M_impl._M_fn),
1411 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1412 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1416 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1419 template<
typename _Tp>
1420 static reference_wrapper<_Tp>
1421 _S_maybe_wrap_ref(_Tp& __t)
1424 template<
typename _Tp>
1426 typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
1427 _S_maybe_wrap_ref(_Tp&& __t)
1428 {
return std::forward<_Tp>(__t); }
1430 struct _Impl : _Alloc
1432 template<
typename _Fn2>
1433 _Impl(_Fn2&& __fn,
const _Alloc& __a)
1434 : _Alloc(__a), _M_fn(std::
forward<_Fn2>(__fn)) { }
1439 template<
typename _Signature,
typename _Fn,
typename _Alloc>
1440 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1441 __create_task_state(_Fn&& __fn,
const _Alloc& __a)
1443 typedef typename decay<_Fn>::type _Fn2;
1444 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1445 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1448 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1449 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1450 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1452 return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
1453 static_cast<_Alloc&
>(_M_impl));
1456 template<
typename _Task,
typename _Fn,
bool
1457 = is_same<_Task, typename decay<_Fn>::type>::value>
1458 struct __constrain_pkgdtask
1459 {
typedef void __type; };
1461 template<
typename _Task,
typename _Fn>
1462 struct __constrain_pkgdtask<_Task, _Fn, true>
1466 template<
typename _Res,
typename... _ArgTypes>
1467 class packaged_task<_Res(_ArgTypes...)>
1469 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1470 shared_ptr<_State_type> _M_state;
1474 packaged_task() noexcept { }
1478 template<
typename _Allocator>
1479 packaged_task(allocator_arg_t,
const _Allocator& __a) noexcept
1482 template<
typename _Fn,
typename =
typename
1483 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1485 packaged_task(_Fn&& __fn)
1486 : packaged_task(allocator_arg, std::allocator<int>(),
1492 template<
typename _Fn,
typename _Alloc,
typename =
typename
1493 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1495 packaged_task(allocator_arg_t,
const _Alloc& __a, _Fn&& __fn)
1496 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1497 std::
forward<_Fn>(__fn), __a))
1502 if (static_cast<bool>(_M_state) && !_M_state.unique())
1503 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1507 packaged_task(
const packaged_task&) =
delete;
1508 packaged_task& operator=(
const packaged_task&) =
delete;
1510 template<
typename _Allocator>
1511 packaged_task(allocator_arg_t,
const _Allocator&,
1512 const packaged_task&) =
delete;
1515 packaged_task(packaged_task&& __other) noexcept
1516 { this->
swap(__other); }
1518 template<
typename _Allocator>
1519 packaged_task(allocator_arg_t,
const _Allocator&,
1520 packaged_task&& __other) noexcept
1521 { this->
swap(__other); }
1523 packaged_task& operator=(packaged_task&& __other) noexcept
1525 packaged_task(std::move(__other)).swap(*
this);
1530 swap(packaged_task& __other) noexcept
1531 { _M_state.swap(__other._M_state); }
1534 valid() const noexcept
1535 {
return static_cast<bool>(_M_state); }
1540 {
return future<_Res>(_M_state); }
1544 operator()(_ArgTypes... __args)
1546 __future_base::_State_base::_S_check(_M_state);
1547 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1551 make_ready_at_thread_exit(_ArgTypes... __args)
1553 __future_base::_State_base::_S_check(_M_state);
1554 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1560 __future_base::_State_base::_S_check(_M_state);
1561 packaged_task __tmp;
1562 __tmp._M_state = _M_state;
1563 _M_state = _M_state->_M_reset();
1568 template<
typename _Res,
typename... _ArgTypes>
1570 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1571 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1574 template<
typename _Res,
typename _Alloc>
1575 struct uses_allocator<packaged_task<_Res>, _Alloc>
1581 template<
typename _BoundFn,
typename _Res>
1582 class __future_base::_Deferred_state final
1583 :
public __future_base::_State_base
1587 _Deferred_state(_BoundFn&& __fn)
1588 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1592 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1593 _Ptr_type _M_result;
1606 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1611 virtual bool _M_is_deferred_future()
const {
return true; }
1615 class __future_base::_Async_state_commonV2
1616 :
public __future_base::_State_base
1619 ~_Async_state_commonV2() =
default;
1636 virtual void _M_complete_async() { _M_join(); }
1646 template<
typename _BoundFn,
typename _Res>
1647 class __future_base::_Async_state_impl final
1648 :
public __future_base::_Async_state_commonV2
1652 _Async_state_impl(_BoundFn&& __fn)
1653 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1658 _M_set_result(_S_task_setter(_M_result, _M_fn));
1663 if (static_cast<bool>(_M_result))
1664 this->_M_break_promise(std::move(_M_result));
1665 __throw_exception_again;
1673 ~_Async_state_impl() {
if (_M_thread.joinable()) _M_thread.join(); }
1676 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1677 _Ptr_type _M_result;
1681 template<
typename _BoundFn>
1683 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1685 typedef typename remove_reference<_BoundFn>::type __fn_type;
1686 typedef _Deferred_state<__fn_type> __state_type;
1687 return std::make_shared<__state_type>(std::move(__fn));
1690 template<
typename _BoundFn>
1692 __future_base::_S_make_async_state(_BoundFn&& __fn)
1694 typedef typename remove_reference<_BoundFn>::type __fn_type;
1695 typedef _Async_state_impl<__fn_type> __state_type;
1696 return std::make_shared<__state_type>(std::move(__fn));
1701 template<
typename _Fn,
typename... _Args>
1702 future<
typename result_of<_Fn(_Args...)>::type>
1703 async(
launch __policy, _Fn&& __fn, _Args&&... __args)
1705 typedef typename result_of<_Fn(_Args...)>::type result_type;
1707 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1709 __state = __future_base::_S_make_async_state(std::__bind_simple(
1710 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1714 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1715 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1717 return future<result_type>(__state);
1721 template<
typename _Fn,
typename... _Args>
1722 inline future<
typename result_of<_Fn(_Args...)>::type>
1723 async(_Fn&& __fn, _Args&&... __args)
1725 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1726 std::forward<_Args>(__args)...);
1729 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
1730 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1734 _GLIBCXX_END_NAMESPACE_VERSION
1739 #endif // _GLIBCXX_FUTURE
Exception type thrown by futures.
void swap(basic_filebuf< _CharT, _Traits > &__x, basic_filebuf< _CharT, _Traits > &__y)
Swap specialization for filebufs.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
exception_ptr make_exception_ptr(_Ex __ex) noexcept
Obtain an exception_ptr pointing to a copy of the supplied object.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
const error_category & future_category() noexcept
Points to a statically-allocated object derived from error_category.
shared_ptr< _Tp > make_shared(_Args &&...__args)
Create an object that is owned by a shared_ptr.
The standard allocator, as per [20.4].
logic_error(const string &__arg)
bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
A smart pointer with reference-counted copy semantics.
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
__allocated_ptr< _Alloc > __allocate_guarded(_Alloc &__a)
Allocate space for a single object using __a.
reference_wrapper< _Tp > ref(_Tp &__t) noexcept
Denotes a reference should be taken to a variable.
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
virtual const char * what() const noexcept
Thrown as part of forced unwinding.A magic placeholder class that can be caught by reference to recog...
bitset< _Nb > & reset() noexcept
Sets every bit to false.
bitset< _Nb > operator~() const noexcept
See the no-argument flip().
One of two subclasses of exception.
bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
void rethrow_exception(exception_ptr) __attribute__((__noreturn__))
Throw the object pointed to by the exception_ptr.
launch
Launch code for futures.
future_errc
Error code for futures.
_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_status
Status code for futures.
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
call_once
exception_ptr current_exception() noexcept
shared_ptr< _Tp > allocate_shared(const _Alloc &__a, _Args &&...__args)
Create an object that is owned by a shared_ptr.