29 #ifndef _GLIBCXX_FUTURE
30 #define _GLIBCXX_FUTURE 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
47 #include <bits/uses_allocator.h>
51 namespace std _GLIBCXX_VISIBILITY(default)
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
66 future_already_retrieved = 1,
67 promise_already_satisfied,
86 inline error_condition
106 what()
const noexcept;
109 code()
const noexcept {
return _M_code; }
113 template<
typename _Res>
116 template<
typename _Res>
119 template<
typename _Signature>
122 template<
typename _Res>
134 return static_cast<launch>(
135 static_cast<int>(__x) & static_cast<int>(__y));
140 return static_cast<launch>(
141 static_cast<int>(__x) | static_cast<int>(__y));
146 return static_cast<launch>(
147 static_cast<int>(__x) ^ static_cast<int>(__y));
151 {
return static_cast<launch>(~static_cast<
int>(__x)); }
154 {
return __x = __x & __y; }
157 {
return __x = __x | __y; }
160 {
return __x = __x ^ __y; }
170 template<
typename _Fn,
typename... _Args>
171 future<
typename result_of<_Fn(_Args...)>::type>
172 async(
launch __policy, _Fn&& __fn, _Args&&... __args);
174 template<
typename _Fn,
typename... _Args>
175 future<
typename result_of<_Fn(_Args...)>::type>
176 async(_Fn&& __fn, _Args&&... __args);
178 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
179 && (ATOMIC_INT_LOCK_FREE > 1)
187 exception_ptr _M_error;
189 _Result_base(
const _Result_base&) =
delete;
190 _Result_base& operator=(
const _Result_base&) =
delete;
193 virtual void _M_destroy() = 0;
197 void operator()(_Result_base* __fr)
const { __fr->_M_destroy(); }
202 virtual ~_Result_base();
206 template<
typename _Res>
207 struct _Result : _Result_base
210 __gnu_cxx::__aligned_buffer<_Res> _M_storage;
214 typedef _Res result_type;
216 _Result() noexcept : _M_initialized() { }
226 _M_value() noexcept {
return *_M_storage._M_ptr(); }
229 _M_set(
const _Res& __res)
231 ::new (_M_storage._M_addr()) _Res(__res);
232 _M_initialized =
true;
238 ::new (_M_storage._M_addr()) _Res(std::move(__res));
239 _M_initialized =
true;
243 void _M_destroy() {
delete this; }
247 template<
typename _Res>
248 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
251 template<
typename _Res,
typename _Alloc>
252 struct _Result_alloc final : _Result<_Res>, _Alloc
254 typedef typename allocator_traits<_Alloc>::template
255 rebind_alloc<_Result_alloc> __allocator_type;
258 _Result_alloc(
const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
264 typedef allocator_traits<__allocator_type> __traits;
265 __allocator_type __a(*
this);
266 __traits::destroy(__a,
this);
267 __traits::deallocate(__a,
this, 1);
271 template<
typename _Res,
typename _Allocator>
272 static _Ptr<_Result_alloc<_Res, _Allocator>>
273 _S_allocate_result(
const _Allocator& __a)
275 typedef _Result_alloc<_Res, _Allocator> __result_type;
276 typedef allocator_traits<typename __result_type::__allocator_type>
278 typename __traits::allocator_type __a2(__a);
279 __result_type* __p = __traits::allocate(__a2, 1);
282 __traits::construct(__a2, __p, __a);
286 __traits::deallocate(__a2, __p, 1);
287 __throw_exception_again;
289 return _Ptr<__result_type>(__p);
292 template<
typename _Res,
typename _Tp>
293 static _Ptr<_Result<_Res>>
296 return _Ptr<_Result<_Res>>(
new _Result<_Res>);
303 typedef _Ptr<_Result_base> _Ptr_type;
307 condition_variable _M_cond;
308 atomic_flag _M_retrieved;
312 _State_baseV2() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT)
314 _State_baseV2(
const _State_baseV2&) =
delete;
315 _State_baseV2& operator=(
const _State_baseV2&) =
delete;
316 virtual ~_State_baseV2() =
default;
322 unique_lock<mutex> __lock(_M_mutex);
323 _M_cond.wait(__lock, [&] {
return _M_ready(); });
327 template<
typename _Rep,
typename _Period>
329 wait_for(
const chrono::duration<_Rep, _Period>& __rel)
331 unique_lock<mutex> __lock(_M_mutex);
333 return future_status::ready;
334 if (_M_has_deferred())
335 return future_status::deferred;
336 if (_M_cond.wait_for(__lock, __rel, [&] {
return _M_ready(); }))
341 return future_status::ready;
343 return future_status::timeout;
346 template<
typename _Clock,
typename _Duration>
348 wait_until(
const chrono::time_point<_Clock, _Duration>& __abs)
350 unique_lock<mutex> __lock(_M_mutex);
352 return future_status::ready;
353 if (_M_has_deferred())
354 return future_status::deferred;
355 if (_M_cond.wait_until(__lock, __abs, [&] {
return _M_ready(); }))
360 return future_status::ready;
362 return future_status::timeout;
366 _M_set_result(
function<_Ptr_type()> __res,
bool __ignore_failure =
false)
368 bool __set = __ignore_failure;
371 call_once(_M_once, &_State_baseV2::_M_do_set,
this,
ref(__res),
374 __throw_future_error(
int(future_errc::promise_already_satisfied));
378 _M_break_promise(_Ptr_type __res)
380 if (static_cast<bool>(__res))
382 error_code __ec(make_error_code(future_errc::broken_promise));
385 lock_guard<mutex> __lock(_M_mutex);
386 _M_result.swap(__res);
388 _M_cond.notify_all();
394 _M_set_retrieved_flag()
396 if (_M_retrieved.test_and_set())
397 __throw_future_error(
int(future_errc::future_already_retrieved));
400 template<
typename _Res,
typename _Arg>
404 template<
typename _Res,
typename _Arg>
405 struct _Setter<_Res, _Arg&>
409 static_assert(is_same<_Res, _Arg&>::value
410 || is_same<const _Res, _Arg>::value,
411 "Invalid specialisation");
413 typename promise<_Res>::_Ptr_type operator()()
415 _State_baseV2::_S_check(_M_promise->_M_future);
416 _M_promise->_M_storage->_M_set(_M_arg);
417 return std::move(_M_promise->_M_storage);
419 promise<_Res>* _M_promise;
424 template<
typename _Res>
425 struct _Setter<_Res, _Res&&>
427 typename promise<_Res>::_Ptr_type operator()()
429 _State_baseV2::_S_check(_M_promise->_M_future);
430 _M_promise->_M_storage->_M_set(std::move(_M_arg));
431 return std::move(_M_promise->_M_storage);
433 promise<_Res>* _M_promise;
437 struct __exception_ptr_tag { };
440 template<
typename _Res>
441 struct _Setter<_Res, __exception_ptr_tag>
443 typename promise<_Res>::_Ptr_type operator()()
445 _State_baseV2::_S_check(_M_promise->_M_future);
446 _M_promise->_M_storage->_M_error = _M_ex;
447 return std::move(_M_promise->_M_storage);
450 promise<_Res>* _M_promise;
451 exception_ptr& _M_ex;
454 template<
typename _Res,
typename _Arg>
455 static _Setter<_Res, _Arg&&>
456 __setter(promise<_Res>* __prom, _Arg&& __arg)
458 return _Setter<_Res, _Arg&&>{ __prom, __arg };
461 template<
typename _Res>
462 static _Setter<_Res, __exception_ptr_tag>
463 __setter(exception_ptr& __ex, promise<_Res>* __prom)
465 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
468 static _Setter<void, void>
469 __setter(promise<void>* __prom);
471 template<
typename _Tp>
473 _S_check(
const shared_ptr<_Tp>& __p)
475 if (!static_cast<bool>(__p))
476 __throw_future_error((
int)future_errc::no_state);
481 _M_do_set(
function<_Ptr_type()>& __f,
bool& __set)
483 _Ptr_type __res = __f();
485 lock_guard<mutex> __lock(_M_mutex);
486 _M_result.swap(__res);
488 _M_cond.notify_all();
492 bool _M_ready() const noexcept {
return static_cast<bool>(_M_result); }
495 virtual void _M_complete_async() { }
498 virtual bool _M_has_deferred()
const {
return false; }
501 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
503 class _Async_state_common;
505 using _State_base = _State_baseV2;
506 class _Async_state_commonV2;
509 template<
typename _BoundFn,
typename =
typename _BoundFn::result_type>
510 class _Deferred_state;
512 template<
typename _BoundFn,
typename =
typename _BoundFn::result_type>
513 class _Async_state_impl;
515 template<
typename _Signature>
516 class _Task_state_base;
518 template<
typename _Fn,
typename _Alloc,
typename _Signature>
521 template<
typename _BoundFn>
523 _S_make_deferred_state(_BoundFn&& __fn);
525 template<
typename _BoundFn>
527 _S_make_async_state(_BoundFn&& __fn);
529 template<
typename _Res_ptr,
530 typename _Res =
typename _Res_ptr::element_type::result_type>
533 template<
typename _Res_ptr,
typename _BoundFn>
534 static _Task_setter<_Res_ptr>
535 _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
537 return _Task_setter<_Res_ptr>{ __ptr,
std::ref(__call) };
542 template<
typename _Res>
543 struct __future_base::_Result<_Res&> : __future_base::_Result_base
545 typedef _Res& result_type;
547 _Result() noexcept : _M_value_ptr() { }
549 void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
551 _Res& _M_get() noexcept {
return *_M_value_ptr; }
556 void _M_destroy() {
delete this; }
561 struct __future_base::_Result<void> : __future_base::_Result_base
563 typedef void result_type;
566 void _M_destroy() {
delete this; }
569 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
572 template<
typename _Res>
573 class __basic_future :
public __future_base
576 typedef shared_ptr<_State_base> __state_type;
577 typedef __future_base::_Result<_Res>& __result_type;
580 __state_type _M_state;
584 __basic_future(
const __basic_future&) =
delete;
585 __basic_future& operator=(
const __basic_future&) =
delete;
588 valid() const noexcept {
return static_cast<bool>(_M_state); }
593 _State_base::_S_check(_M_state);
597 template<
typename _Rep,
typename _Period>
599 wait_for(
const chrono::duration<_Rep, _Period>& __rel)
const
601 _State_base::_S_check(_M_state);
602 return _M_state->wait_for(__rel);
605 template<
typename _Clock,
typename _Duration>
607 wait_until(
const chrono::time_point<_Clock, _Duration>& __abs)
const
609 _State_base::_S_check(_M_state);
610 return _M_state->wait_until(__abs);
616 _M_get_result()
const
618 _State_base::_S_check(_M_state);
619 _Result_base& __res = _M_state->wait();
620 if (!(__res._M_error == 0))
622 return static_cast<__result_type
>(__res);
625 void _M_swap(__basic_future& __that) noexcept
627 _M_state.swap(__that._M_state);
632 __basic_future(
const __state_type& __state) : _M_state(__state)
634 _State_base::_S_check(_M_state);
635 _M_state->_M_set_retrieved_flag();
640 __basic_future(
const shared_future<_Res>&) noexcept;
644 __basic_future(shared_future<_Res>&&) noexcept;
648 __basic_future(future<_Res>&&) noexcept;
650 constexpr __basic_future() noexcept : _M_state() { }
654 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
655 ~_Reset() { _M_fut._M_state.reset(); }
656 __basic_future& _M_fut;
662 template<
typename _Res>
663 class future :
public __basic_future<_Res>
665 friend class promise<_Res>;
666 template<
typename>
friend class packaged_task;
667 template<
typename _Fn,
typename... _Args>
668 friend future<
typename result_of<_Fn(_Args...)>::type>
669 async(
launch, _Fn&&, _Args&&...);
671 typedef __basic_future<_Res> _Base_type;
672 typedef typename _Base_type::__state_type __state_type;
675 future(
const __state_type& __state) : _Base_type(__state) { }
678 constexpr future() noexcept : _Base_type() { }
681 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
684 future(
const future&) =
delete;
685 future& operator=(
const future&) =
delete;
687 future& operator=(future&& __fut) noexcept
689 future(std::move(__fut))._M_swap(*
this);
697 typename _Base_type::_Reset __reset(*
this);
698 return std::move(this->_M_get_result()._M_value());
701 shared_future<_Res> share();
705 template<
typename _Res>
706 class future<_Res&> :
public __basic_future<_Res&>
708 friend class promise<_Res&>;
709 template<
typename>
friend class packaged_task;
710 template<
typename _Fn,
typename... _Args>
711 friend future<
typename result_of<_Fn(_Args...)>::type>
712 async(
launch, _Fn&&, _Args&&...);
714 typedef __basic_future<_Res&> _Base_type;
715 typedef typename _Base_type::__state_type __state_type;
718 future(
const __state_type& __state) : _Base_type(__state) { }
721 constexpr future() noexcept : _Base_type() { }
724 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
727 future(
const future&) =
delete;
728 future& operator=(
const future&) =
delete;
730 future& operator=(future&& __fut) noexcept
732 future(std::move(__fut))._M_swap(*
this);
740 typename _Base_type::_Reset __reset(*
this);
741 return this->_M_get_result()._M_get();
744 shared_future<_Res&> share();
749 class future<void> :
public __basic_future<void>
751 friend class promise<void>;
752 template<
typename>
friend class packaged_task;
753 template<
typename _Fn,
typename... _Args>
754 friend future<
typename result_of<_Fn(_Args...)>::type>
755 async(
launch, _Fn&&, _Args&&...);
757 typedef __basic_future<void> _Base_type;
758 typedef typename _Base_type::__state_type __state_type;
761 future(
const __state_type& __state) : _Base_type(__state) { }
764 constexpr future() noexcept : _Base_type() { }
767 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
770 future(
const future&) =
delete;
771 future& operator=(
const future&) =
delete;
773 future& operator=(future&& __fut) noexcept
775 future(std::move(__fut))._M_swap(*
this);
783 typename _Base_type::_Reset __reset(*
this);
784 this->_M_get_result();
787 shared_future<void> share();
792 template<
typename _Res>
793 class shared_future :
public __basic_future<_Res>
795 typedef __basic_future<_Res> _Base_type;
798 constexpr shared_future() noexcept : _Base_type() { }
801 shared_future(
const shared_future& __sf) : _Base_type(__sf) { }
804 shared_future(future<_Res>&& __uf) noexcept
805 : _Base_type(std::move(__uf))
809 shared_future(shared_future&& __sf) noexcept
810 : _Base_type(std::move(__sf))
813 shared_future& operator=(
const shared_future& __sf)
815 shared_future(__sf)._M_swap(*
this);
819 shared_future& operator=(shared_future&& __sf) noexcept
821 shared_future(std::move(__sf))._M_swap(*
this);
827 get()
const {
return this->_M_get_result()._M_value(); }
831 template<
typename _Res>
832 class shared_future<_Res&> :
public __basic_future<_Res&>
834 typedef __basic_future<_Res&> _Base_type;
837 constexpr shared_future() noexcept : _Base_type() { }
840 shared_future(
const shared_future& __sf) : _Base_type(__sf) { }
843 shared_future(future<_Res&>&& __uf) noexcept
844 : _Base_type(std::move(__uf))
848 shared_future(shared_future&& __sf) noexcept
849 : _Base_type(std::move(__sf))
852 shared_future& operator=(
const shared_future& __sf)
854 shared_future(__sf)._M_swap(*
this);
858 shared_future& operator=(shared_future&& __sf) noexcept
860 shared_future(std::move(__sf))._M_swap(*
this);
866 get()
const {
return this->_M_get_result()._M_get(); }
871 class shared_future<void> :
public __basic_future<void>
873 typedef __basic_future<void> _Base_type;
876 constexpr shared_future() noexcept : _Base_type() { }
879 shared_future(
const shared_future& __sf) : _Base_type(__sf) { }
882 shared_future(future<void>&& __uf) noexcept
883 : _Base_type(std::move(__uf))
887 shared_future(shared_future&& __sf) noexcept
888 : _Base_type(std::move(__sf))
891 shared_future& operator=(
const shared_future& __sf)
893 shared_future(__sf)._M_swap(*
this);
897 shared_future& operator=(shared_future&& __sf) noexcept
899 shared_future(std::move(__sf))._M_swap(*
this);
905 get()
const { this->_M_get_result(); }
909 template<
typename _Res>
910 inline __basic_future<_Res>::
911 __basic_future(
const shared_future<_Res>& __sf) noexcept
912 : _M_state(__sf._M_state)
915 template<
typename _Res>
916 inline __basic_future<_Res>::
917 __basic_future(shared_future<_Res>&& __sf) noexcept
918 : _M_state(std::move(__sf._M_state))
921 template<
typename _Res>
922 inline __basic_future<_Res>::
923 __basic_future(future<_Res>&& __uf) noexcept
924 : _M_state(std::move(__uf._M_state))
927 template<
typename _Res>
928 inline shared_future<_Res>
929 future<_Res>::share()
930 {
return shared_future<_Res>(std::move(*
this)); }
932 template<
typename _Res>
933 inline shared_future<_Res&>
934 future<_Res&>::share()
935 {
return shared_future<_Res&>(std::move(*
this)); }
937 inline shared_future<void>
938 future<void>::share()
939 {
return shared_future<void>(std::move(*
this)); }
942 template<
typename _Res>
945 typedef __future_base::_State_base _State;
946 typedef __future_base::_Result<_Res> _Res_type;
947 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
948 template<
typename,
typename>
friend class _State::_Setter;
950 shared_ptr<_State> _M_future;
951 _Ptr_type _M_storage;
956 _M_storage(new _Res_type())
959 promise(promise&& __rhs) noexcept
960 : _M_future(std::move(__rhs._M_future)),
961 _M_storage(std::move(__rhs._M_storage))
964 template<
typename _Allocator>
965 promise(allocator_arg_t,
const _Allocator& __a)
967 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
970 template<
typename _Allocator>
971 promise(allocator_arg_t,
const _Allocator&, promise&& __rhs)
972 : _M_future(std::move(__rhs._M_future)),
973 _M_storage(std::move(__rhs._M_storage))
976 promise(
const promise&) =
delete;
980 if (static_cast<bool>(_M_future) && !_M_future.unique())
981 _M_future->_M_break_promise(std::move(_M_storage));
986 operator=(promise&& __rhs) noexcept
988 promise(std::move(__rhs)).swap(*
this);
992 promise& operator=(
const promise&) =
delete;
995 swap(promise& __rhs) noexcept
997 _M_future.swap(__rhs._M_future);
998 _M_storage.swap(__rhs._M_storage);
1004 {
return future<_Res>(_M_future); }
1008 set_value(
const _Res& __r)
1010 auto __setter = _State::__setter(
this, __r);
1011 _M_future->_M_set_result(std::move(__setter));
1015 set_value(_Res&& __r)
1017 auto __setter = _State::__setter(
this, std::move(__r));
1018 _M_future->_M_set_result(std::move(__setter));
1022 set_exception(exception_ptr __p)
1024 auto __setter = _State::__setter(__p,
this);
1025 _M_future->_M_set_result(std::move(__setter));
1029 template<
typename _Res>
1031 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1034 template<
typename _Res,
typename _Alloc>
1035 struct uses_allocator<promise<_Res>, _Alloc>
1040 template<
typename _Res>
1041 class promise<_Res&>
1043 typedef __future_base::_State_base _State;
1044 typedef __future_base::_Result<_Res&> _Res_type;
1045 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1046 template<
typename,
typename>
friend class _State::_Setter;
1048 shared_ptr<_State> _M_future;
1049 _Ptr_type _M_storage;
1054 _M_storage(new _Res_type())
1057 promise(promise&& __rhs) noexcept
1058 : _M_future(std::move(__rhs._M_future)),
1059 _M_storage(std::move(__rhs._M_storage))
1062 template<
typename _Allocator>
1063 promise(allocator_arg_t,
const _Allocator& __a)
1065 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1068 template<
typename _Allocator>
1069 promise(allocator_arg_t,
const _Allocator&, promise&& __rhs)
1070 : _M_future(std::move(__rhs._M_future)),
1071 _M_storage(std::move(__rhs._M_storage))
1074 promise(
const promise&) =
delete;
1078 if (static_cast<bool>(_M_future) && !_M_future.unique())
1079 _M_future->_M_break_promise(std::move(_M_storage));
1084 operator=(promise&& __rhs) noexcept
1086 promise(std::move(__rhs)).swap(*
this);
1090 promise& operator=(
const promise&) =
delete;
1093 swap(promise& __rhs) noexcept
1095 _M_future.swap(__rhs._M_future);
1096 _M_storage.swap(__rhs._M_storage);
1102 {
return future<_Res&>(_M_future); }
1106 set_value(_Res& __r)
1108 auto __setter = _State::__setter(
this, __r);
1109 _M_future->_M_set_result(std::move(__setter));
1113 set_exception(exception_ptr __p)
1115 auto __setter = _State::__setter(__p,
this);
1116 _M_future->_M_set_result(std::move(__setter));
1124 typedef __future_base::_State_base _State;
1125 typedef __future_base::_Result<void> _Res_type;
1126 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1127 template<
typename,
typename>
friend class _State::_Setter;
1129 shared_ptr<_State> _M_future;
1130 _Ptr_type _M_storage;
1135 _M_storage(new _Res_type())
1138 promise(promise&& __rhs) noexcept
1139 : _M_future(std::move(__rhs._M_future)),
1140 _M_storage(std::move(__rhs._M_storage))
1143 template<
typename _Allocator>
1144 promise(allocator_arg_t,
const _Allocator& __a)
1146 _M_storage(__future_base::_S_allocate_result<void>(__a))
1151 template<
typename _Allocator>
1152 promise(allocator_arg_t,
const _Allocator&, promise&& __rhs)
1153 : _M_future(std::move(__rhs._M_future)),
1154 _M_storage(std::move(__rhs._M_storage))
1157 promise(
const promise&) =
delete;
1161 if (static_cast<bool>(_M_future) && !_M_future.unique())
1162 _M_future->_M_break_promise(std::move(_M_storage));
1167 operator=(promise&& __rhs) noexcept
1169 promise(std::move(__rhs)).swap(*
this);
1173 promise& operator=(
const promise&) =
delete;
1176 swap(promise& __rhs) noexcept
1178 _M_future.swap(__rhs._M_future);
1179 _M_storage.swap(__rhs._M_storage);
1185 {
return future<void>(_M_future); }
1191 set_exception(exception_ptr __p)
1193 auto __setter = _State::__setter(__p,
this);
1194 _M_future->_M_set_result(std::move(__setter));
1200 struct __future_base::_State_base::_Setter<void, void>
1202 promise<void>::_Ptr_type operator()()
1204 _State_base::_S_check(_M_promise->_M_future);
1205 return std::move(_M_promise->_M_storage);
1208 promise<void>* _M_promise;
1211 inline __future_base::_State_base::_Setter<void, void>
1212 __future_base::_State_base::__setter(promise<void>* __prom)
1214 return _Setter<void, void>{ __prom };
1218 promise<void>::set_value()
1220 auto __setter = _State::__setter(
this);
1221 _M_future->_M_set_result(std::move(__setter));
1225 template<
typename _Ptr_type,
typename _Res>
1226 struct __future_base::_Task_setter
1228 _Ptr_type operator()()
1232 _M_result->_M_set(_M_fn());
1238 return std::move(_M_result);
1240 _Ptr_type& _M_result;
1241 std::function<_Res()> _M_fn;
1244 template<
typename _Ptr_type>
1245 struct __future_base::_Task_setter<_Ptr_type, void>
1247 _Ptr_type operator()()
1257 return std::move(_M_result);
1259 _Ptr_type& _M_result;
1260 std::function<void()> _M_fn;
1263 template<
typename _Res,
typename... _Args>
1264 struct __future_base::_Task_state_base<_Res(_Args...)>
1265 : __future_base::_State_base
1267 typedef _Res _Res_type;
1269 template<
typename _Alloc>
1270 _Task_state_base(
const _Alloc& __a)
1271 : _M_result(_S_allocate_result<_Res>(__a))
1275 _M_run(_Args... __args) = 0;
1277 virtual shared_ptr<_Task_state_base>
1280 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1281 _Ptr_type _M_result;
1284 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1285 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1286 : __future_base::_Task_state_base<_Res(_Args...)>
1288 _Task_state(_Fn&& __fn,
const _Alloc& __a)
1289 : _Task_state_base<_Res(_Args...)>(__a), _M_impl(std::move(__fn), __a)
1294 _M_run(_Args... __args)
1297 auto __boundfn = std::__bind_simple(
std::ref(_M_impl._M_fn),
1298 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1299 auto __setter = _S_task_setter(this->_M_result, std::move(__boundfn));
1300 this->_M_set_result(std::move(__setter));
1303 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1306 template<
typename _Tp>
1307 static reference_wrapper<_Tp>
1308 _S_maybe_wrap_ref(_Tp& __t)
1311 template<
typename _Tp>
1313 typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
1314 _S_maybe_wrap_ref(_Tp&& __t)
1315 {
return std::forward<_Tp>(__t); }
1317 struct _Impl : _Alloc
1319 _Impl(_Fn&& __fn,
const _Alloc& __a)
1320 : _Alloc(__a), _M_fn(std::move(__fn)) { }
1325 template<
typename _Signature,
typename _Fn,
typename _Alloc>
1326 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1327 __create_task_state(_Fn&& __fn,
const _Alloc& __a)
1329 typedef __future_base::_Task_state<_Fn, _Alloc, _Signature> _State;
1330 return std::allocate_shared<_State>(__a, std::move(__fn), __a);
1333 template<
typename _Fn,
typename _Alloc,
typename _Res,
typename... _Args>
1334 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1335 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1337 return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
1338 static_cast<_Alloc&
>(_M_impl));
1341 template<
typename _Task,
typename _Fn,
bool
1342 = is_same<_Task, typename decay<_Fn>::type>::value>
1343 struct __constrain_pkgdtask
1344 {
typedef void __type; };
1346 template<
typename _Task,
typename _Fn>
1347 struct __constrain_pkgdtask<_Task, _Fn, true>
1351 template<
typename _Res,
typename... _ArgTypes>
1352 class packaged_task<_Res(_ArgTypes...)>
1354 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1355 shared_ptr<_State_type> _M_state;
1359 packaged_task() noexcept { }
1363 template<
typename _Allocator>
1364 packaged_task(allocator_arg_t,
const _Allocator& __a) noexcept
1367 template<
typename _Fn,
typename =
typename
1368 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1370 packaged_task(_Fn&& __fn)
1371 : packaged_task(allocator_arg, std::allocator<int>(), std::move(__fn))
1376 template<
typename _Fn,
typename _Alloc,
typename =
typename
1377 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1379 packaged_task(allocator_arg_t,
const _Alloc& __a, _Fn&& __fn)
1380 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1381 std::
forward<_Fn>(__fn), __a))
1386 if (static_cast<bool>(_M_state) && !_M_state.unique())
1387 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1391 packaged_task(
const packaged_task&) =
delete;
1392 packaged_task& operator=(
const packaged_task&) =
delete;
1394 template<
typename _Allocator>
1395 packaged_task(allocator_arg_t,
const _Allocator&,
1396 const packaged_task&) =
delete;
1399 packaged_task(packaged_task&& __other) noexcept
1400 { this->
swap(__other); }
1402 template<
typename _Allocator>
1403 packaged_task(allocator_arg_t,
const _Allocator&,
1404 packaged_task&& __other) noexcept
1405 { this->
swap(__other); }
1407 packaged_task& operator=(packaged_task&& __other) noexcept
1409 packaged_task(std::move(__other)).swap(*
this);
1414 swap(packaged_task& __other) noexcept
1415 { _M_state.swap(__other._M_state); }
1418 valid() const noexcept
1419 {
return static_cast<bool>(_M_state); }
1424 {
return future<_Res>(_M_state); }
1428 operator()(_ArgTypes... __args)
1430 __future_base::_State_base::_S_check(_M_state);
1431 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1437 __future_base::_State_base::_S_check(_M_state);
1438 packaged_task __tmp;
1439 __tmp._M_state = _M_state;
1440 _M_state = _M_state->_M_reset();
1445 template<
typename _Res,
typename... _ArgTypes>
1447 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1448 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1451 template<
typename _Res,
typename _Alloc>
1452 struct uses_allocator<packaged_task<_Res>, _Alloc>
1456 template<
typename _BoundFn,
typename _Res>
1457 class __future_base::_Deferred_state final
1458 :
public __future_base::_State_base
1462 _Deferred_state(_BoundFn&& __fn)
1463 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1467 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1468 _Ptr_type _M_result;
1476 _M_set_result(_S_task_setter(_M_result, _M_fn),
true);
1480 _M_has_deferred()
const {
return static_cast<bool>(_M_result); }
1483 class __future_base::_Async_state_commonV2
1484 :
public __future_base::_State_base
1487 ~_Async_state_commonV2() =
default;
1490 virtual void _M_complete_async() { _M_join(); }
1498 template<
typename _BoundFn,
typename _Res>
1499 class __future_base::_Async_state_impl final
1500 :
public __future_base::_Async_state_commonV2
1504 _Async_state_impl(_BoundFn&& __fn)
1505 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1508 _M_set_result(_S_task_setter(_M_result, _M_fn));
1512 ~_Async_state_impl() { _M_join(); }
1515 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1516 _Ptr_type _M_result;
1520 template<
typename _BoundFn>
1522 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1524 typedef typename remove_reference<_BoundFn>::type __fn_type;
1525 typedef _Deferred_state<__fn_type> __state_type;
1526 return std::make_shared<__state_type>(std::move(__fn));
1529 template<
typename _BoundFn>
1531 __future_base::_S_make_async_state(_BoundFn&& __fn)
1533 typedef typename remove_reference<_BoundFn>::type __fn_type;
1534 typedef _Async_state_impl<__fn_type> __state_type;
1535 return std::make_shared<__state_type>(std::move(__fn));
1540 template<
typename _Fn,
typename... _Args>
1541 future<
typename result_of<_Fn(_Args...)>::type>
1542 async(
launch __policy, _Fn&& __fn, _Args&&... __args)
1544 typedef typename result_of<_Fn(_Args...)>::type result_type;
1546 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1548 __state = __future_base::_S_make_async_state(std::__bind_simple(
1549 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1553 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1554 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1556 return future<result_type>(__state);
1560 template<
typename _Fn,
typename... _Args>
1561 inline future<
typename result_of<_Fn(_Args...)>::type>
1562 async(_Fn&& __fn, _Args&&... __args)
1564 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1565 std::forward<_Args>(__args)...);
1568 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
1569 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1573 _GLIBCXX_END_NAMESPACE_VERSION
1578 #endif // _GLIBCXX_FUTURE
reference_wrapper< _Tp > ref(_Tp &__t) noexcept
Denotes a reference should be taken to a variable.
logic_error(const string &__arg)
bitset< _Nb > operator~() const noexcept
See the no-argument flip().
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
future_status
Status code for futures.
bitset< _Nb > & reset() noexcept
Sets every bit to false.
exception_ptr current_exception() noexcept
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_ptr< _Tp > allocate_shared(const _Alloc &__a, _Args &&...__args)
Create an object that is owned by a shared_ptr.
virtual const char * what() const noexcept
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
call_once
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
future_errc
Error code for futures.
The standard allocator, as per [20.4].
A smart pointer with reference-counted copy semantics.
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.
void rethrow_exception(exception_ptr) __attribute__((__noreturn__))
Throw the object pointed to by the exception_ptr.
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y)
Swap the targets of two polymorphic function object wrappers.
Exception type thrown by futures.
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.
launch
Launch code for futures.