29 #ifndef _GLIBCXX_FUTURE
30 #define _GLIBCXX_FUTURE 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
48 namespace std _GLIBCXX_VISIBILITY(default)
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 future_already_retrieved,
65 promise_already_satisfied,
83 inline error_condition
103 what()
const throw();
106 code()
const throw() {
return _M_code; }
110 template<
typename _Res>
113 template<
typename _Res>
116 template<
typename _Res>
119 template<
typename _Signature>
122 template<
typename _Res>
141 template<
typename _Fn,
typename... _Args>
142 future<
typename result_of<_Fn(_Args...)>::type>
143 async(
launch __policy, _Fn&& __fn, _Args&&... __args);
145 template<
typename _FnCheck,
typename _Fn,
typename... _Args>
146 struct __async_sfinae_helper
148 typedef future<
typename result_of<_Fn(_Args...)>::type> type;
151 template<
typename _Fn,
typename... _Args>
152 struct __async_sfinae_helper<
launch, _Fn, _Args...>
155 template<
typename _Fn,
typename... _Args>
157 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
158 async(_Fn&& __fn, _Args&&... __args);
160 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
161 && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
169 exception_ptr _M_error;
175 virtual void _M_destroy() = 0;
179 void operator()(
_Result_base* __fr)
const { __fr->_M_destroy(); }
188 template<
typename _Res>
194 typedef typename __align_storage::type __align_type;
196 __align_type _M_storage;
200 _Result() : _M_initialized() { }
210 _M_value() {
return *
static_cast<_Res*
>(_M_addr()); }
213 _M_set(
const _Res& __res)
215 ::new (_M_addr()) _Res(__res);
216 _M_initialized =
true;
222 ::new (_M_addr()) _Res(std::move(__res));
223 _M_initialized =
true;
227 void _M_destroy() {
delete this; }
229 void* _M_addr() {
return static_cast<void*
>(&_M_storage); }
238 template<
typename _Res>
245 template<
typename _Res,
typename _Alloc>
248 typedef typename _Alloc::template rebind<_Result_alloc>::other
258 __allocator_type __a(*
this);
260 __a.deallocate(
this, 1);
264 template<
typename _Res,
typename _Allocator>
266 _S_allocate_result(
const _Allocator& __a)
269 typename __result_type::__allocator_type __a2(__a);
270 __result_type* __p = __a2.allocate(1);
273 __a2.construct(__p, __a);
277 __a2.deallocate(__p, 1);
278 __throw_exception_again;
280 return typename _Ptr<__result_type>::type(__p);
293 atomic_flag _M_retrieved;
297 _State_base() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
308 _M_cond.wait(__lock, std::bind<bool>(&_State_base::_M_ready,
this));
312 template<
typename _Rep,
typename _Period>
317 auto __bound = std::bind<bool>(&_State_base::_M_ready,
this);
318 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
321 template<
typename _Clock,
typename _Duration>
326 auto __bound = std::bind<bool>(&_State_base::_M_ready,
this);
327 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
331 _M_set_result(
function<_Ptr_type()> __res,
bool __ignore_failure =
false)
333 bool __set = __ignore_failure;
336 call_once(_M_once, &_State_base::_M_do_set,
this,
ref(__res),
339 __throw_future_error(
int(future_errc::promise_already_satisfied));
343 _M_break_promise(_Ptr_type __res)
345 if (static_cast<bool>(__res))
347 error_code __ec(make_error_code(future_errc::broken_promise));
351 _M_result.swap(__res);
353 _M_cond.notify_all();
359 _M_set_retrieved_flag()
361 if (_M_retrieved.test_and_set())
362 __throw_future_error(
int(future_errc::future_already_retrieved));
365 template<
typename _Res,
typename _Arg>
369 template<
typename _Res,
typename _Arg>
370 struct _Setter<_Res, _Arg&>
376 "Invalid specialisation");
380 _State_base::_S_check(_M_promise->_M_future);
381 _M_promise->_M_storage->_M_set(_M_arg);
382 return std::move(_M_promise->_M_storage);
389 template<
typename _Res>
390 struct _Setter<_Res, _Res&&>
394 _State_base::_S_check(_M_promise->_M_future);
395 _M_promise->_M_storage->_M_set(std::move(_M_arg));
396 return std::move(_M_promise->_M_storage);
402 struct __exception_ptr_tag { };
405 template<
typename _Res>
406 struct _Setter<_Res, __exception_ptr_tag>
410 _State_base::_S_check(_M_promise->_M_future);
411 _M_promise->_M_storage->_M_error = _M_ex;
412 return std::move(_M_promise->_M_storage);
416 exception_ptr& _M_ex;
419 template<
typename _Res,
typename _Arg>
420 static _Setter<_Res, _Arg&&>
423 return _Setter<_Res, _Arg&&>{ __prom, __arg };
426 template<
typename _Res>
427 static _Setter<_Res, __exception_ptr_tag>
430 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
433 static _Setter<void, void>
436 template<
typename _Tp>
440 if (!static_cast<bool>(__p))
441 __throw_future_error((
int)future_errc::no_state);
446 _M_do_set(
function<_Ptr_type()>& __f,
bool& __set)
448 _Ptr_type __res = __f();
451 _M_result.swap(__res);
453 _M_cond.notify_all();
457 bool _M_ready()
const {
return static_cast<bool>(_M_result); }
459 virtual void _M_run_deferred() { }
462 template<
typename _Res>
463 class _Deferred_state;
465 template<
typename _Res>
468 template<
typename _Signature>
471 template<
typename _StateT,
typename _Res =
typename _StateT::_Res_type>
476 template<
typename _Res>
481 void _M_set(_Res& __res) { _M_value_ptr = &__res; }
483 _Res& _M_get() {
return *_M_value_ptr; }
488 void _M_destroy() {
delete this; }
496 void _M_destroy() {
delete this; }
501 template<
typename _Res>
517 valid()
const {
return static_cast<bool>(_M_state); }
522 _State_base::_S_check(_M_state);
526 template<
typename _Rep,
typename _Period>
530 _State_base::_S_check(_M_state);
531 return _M_state->wait_for(__rel);
534 template<
typename _Clock,
typename _Duration>
538 _State_base::_S_check(_M_state);
539 return _M_state->wait_until(__abs);
547 _State_base::_S_check(_M_state);
549 if (!(__res._M_error == 0))
551 return static_cast<__result_type
>(__res);
556 _M_state.swap(__that._M_state);
561 __basic_future(
const __state_type& __state) : _M_state(__state)
563 _State_base::_S_check(_M_state);
564 _M_state->_M_set_retrieved_flag();
569 __basic_future(
const shared_future<_Res>&);
573 __basic_future(shared_future<_Res>&&);
577 __basic_future(future<_Res>&&);
579 constexpr __basic_future() : _M_state() { }
583 explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { }
584 ~_Reset() { _M_fut._M_state.reset(); }
585 __basic_future& _M_fut;
591 template<
typename _Res>
592 class future :
public __basic_future<_Res>
594 friend class promise<_Res>;
595 template<
typename>
friend class packaged_task;
596 template<
typename _Fn,
typename... _Args>
597 friend future<
typename result_of<_Fn(_Args...)>::type>
600 typedef __basic_future<_Res> _Base_type;
601 typedef typename _Base_type::__state_type __state_type;
604 future(
const __state_type& __state) : _Base_type(__state) { }
607 constexpr future() : _Base_type() { }
618 future(std::move(__fut))._M_swap(*
this);
626 typename _Base_type::_Reset __reset(*
this);
632 template<
typename _Res>
636 template<
typename>
friend class packaged_task;
637 template<
typename _Fn,
typename... _Args>
645 future(
const __state_type& __state) : _Base_type(__state) { }
648 constexpr
future() : _Base_type() { }
651 future(future&& __uf) : _Base_type(std::move(__uf)) { }
654 future(
const future&) =
delete;
655 future& operator=(
const future&) =
delete;
657 future& operator=(future&& __fut)
659 future(std::move(__fut))._M_swap(*
this);
667 typename _Base_type::_Reset __reset(*
this);
668 return this->_M_get_result()._M_get();
677 template<
typename>
friend class packaged_task;
678 template<
typename _Fn,
typename... _Args>
679 friend future<
typename result_of<_Fn(_Args...)>::type>
686 future(
const __state_type& __state) : _Base_type(__state) { }
689 constexpr future() : _Base_type() { }
692 future(future&& __uf) : _Base_type(std::move(__uf)) { }
695 future(
const future&) =
delete;
696 future& operator=(
const future&) =
delete;
698 future& operator=(future&& __fut)
700 future(std::move(__fut))._M_swap(*
this);
708 typename _Base_type::_Reset __reset(*
this);
715 template<
typename _Res>
716 class shared_future :
public __basic_future<_Res>
718 typedef __basic_future<_Res> _Base_type;
721 constexpr shared_future() : _Base_type() { }
728 : _Base_type(std::move(__uf))
733 : _Base_type(std::move(__sf))
742 shared_future& operator=(shared_future&& __sf)
744 shared_future(std::move(__sf))._M_swap(*
this);
753 _Res& __rs(__r._M_value());
759 template<
typename _Res>
772 : _Base_type(std::move(__uf))
776 shared_future(shared_future&& __sf)
777 : _Base_type(std::move(__sf))
780 shared_future& operator=(
const shared_future& __sf)
782 shared_future(__sf)._M_swap(*
this);
786 shared_future& operator=(shared_future&& __sf)
788 shared_future(std::move(__sf))._M_swap(*
this);
794 get() {
return this->_M_get_result()._M_get(); }
804 constexpr shared_future() : _Base_type() { }
811 : _Base_type(std::move(__uf))
815 shared_future(shared_future&& __sf)
816 : _Base_type(std::move(__sf))
819 shared_future& operator=(
const shared_future& __sf)
821 shared_future(__sf)._M_swap(*
this);
825 shared_future& operator=(shared_future&& __sf)
827 shared_future(std::move(__sf))._M_swap(*
this);
837 template<
typename _Res>
838 inline __basic_future<_Res>::
839 __basic_future(
const shared_future<_Res>& __sf)
840 : _M_state(__sf._M_state)
843 template<
typename _Res>
844 inline __basic_future<_Res>::
845 __basic_future(shared_future<_Res>&& __sf)
846 : _M_state(std::move(__sf._M_state))
849 template<
typename _Res>
850 inline __basic_future<_Res>::
851 __basic_future(future<_Res>&& __uf)
852 : _M_state(std::move(__uf._M_state))
857 template<
typename _Res>
860 typedef __future_base::_State_base _State;
861 typedef __future_base::_Result<_Res> _Res_type;
862 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
863 template<
typename,
typename>
friend class _State::_Setter;
865 shared_ptr<_State> _M_future;
866 _Ptr_type _M_storage;
871 _M_storage(new _Res_type())
874 promise(promise&& __rhs)
875 : _M_future(std::move(__rhs._M_future)),
876 _M_storage(std::move(__rhs._M_storage))
879 template<
typename _Allocator>
880 promise(allocator_arg_t,
const _Allocator& __a)
882 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
885 promise(
const promise&) =
delete;
889 if (static_cast<bool>(_M_future) && !_M_future.unique())
890 _M_future->_M_break_promise(std::move(_M_storage));
895 operator=(promise&& __rhs)
897 promise(std::move(__rhs)).swap(*
this);
901 promise& operator=(
const promise&) =
delete;
906 _M_future.swap(__rhs._M_future);
907 _M_storage.swap(__rhs._M_storage);
913 {
return future<_Res>(_M_future); }
917 set_value(
const _Res& __r)
919 auto __setter = _State::__setter(
this, __r);
920 _M_future->_M_set_result(std::move(__setter));
924 set_value(_Res&& __r)
926 auto __setter = _State::__setter(
this, std::move(__r));
927 _M_future->_M_set_result(std::move(__setter));
931 set_exception(exception_ptr __p)
933 auto __setter = _State::__setter(__p,
this);
934 _M_future->_M_set_result(std::move(__setter));
938 template<
typename _Res>
940 swap(promise<_Res>& __x, promise<_Res>& __y)
943 template<
typename _Res,
typename _Alloc>
944 struct uses_allocator<promise<_Res>, _Alloc>
949 template<
typename _Res>
955 template<
typename,
typename>
friend class _State::_Setter;
958 _Ptr_type _M_storage;
962 : _M_future(std::make_shared<_State>()),
963 _M_storage(
new _Res_type())
967 : _M_future(std::move(__rhs._M_future)),
968 _M_storage(std::move(__rhs._M_storage))
971 template<
typename _Allocator>
973 : _M_future(std::allocate_shared<_State>(__a)),
974 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
981 if (static_cast<bool>(_M_future) && !_M_future.unique())
982 _M_future->_M_break_promise(std::move(_M_storage));
989 promise(std::move(__rhs)).swap(*
this);
998 _M_future.swap(__rhs._M_future);
999 _M_storage.swap(__rhs._M_storage);
1009 set_value(_Res& __r)
1011 auto __setter = _State::__setter(
this, __r);
1012 _M_future->_M_set_result(std::move(__setter));
1016 set_exception(exception_ptr __p)
1018 auto __setter = _State::__setter(__p,
this);
1019 _M_future->_M_set_result(std::move(__setter));
1030 template<
typename,
typename>
friend class _State::_Setter;
1037 : _M_future(std::make_shared<_State>()),
1042 : _M_future(std::move(__rhs._M_future)),
1043 _M_storage(std::move(__rhs._M_storage))
1046 template<
typename _Allocator>
1048 : _M_future(std::allocate_shared<_State>(__a)),
1049 _M_storage(__future_base::_S_allocate_result<void>(__a))
1056 if (static_cast<bool>(_M_future) && !_M_future.unique())
1057 _M_future->_M_break_promise(std::move(_M_storage));
1064 promise(std::move(__rhs)).swap(*
this);
1073 _M_future.swap(__rhs._M_future);
1074 _M_storage.swap(__rhs._M_storage);
1086 set_exception(exception_ptr __p)
1088 auto __setter = _State::__setter(__p,
this);
1089 _M_future->_M_set_result(std::move(__setter));
1095 struct __future_base::_State_base::_Setter<void, void>
1099 _State_base::_S_check(_M_promise->_M_future);
1100 return std::move(_M_promise->_M_storage);
1103 promise<void>* _M_promise;
1106 inline __future_base::_State_base::_Setter<void, void>
1107 __future_base::_State_base::__setter(promise<void>* __prom)
1109 return _Setter<void, void>{ __prom };
1113 promise<void>::set_value()
1115 auto __setter = _State::__setter(
this);
1116 _M_future->_M_set_result(std::move(__setter));
1120 template<
typename _StateT,
typename _Res>
1121 struct __future_base::_Task_setter
1123 typename _StateT::_Ptr_type operator()()
1127 _M_state->_M_result->_M_set(_M_fn());
1133 return std::move(_M_state->_M_result);
1136 std::function<_Res()> _M_fn;
1139 template<
typename _StateT>
1140 struct __future_base::_Task_setter<_StateT, void>
1142 typename _StateT::_Ptr_type operator()()
1152 return std::move(_M_state->_M_result);
1155 std::function<void()> _M_fn;
1158 template<
typename _Res,
typename... _Args>
1159 struct __future_base::_Task_state<_Res(_Args...)>
1160 : __future_base::_State_base
1162 typedef _Res _Res_type;
1164 _Task_state(std::function<_Res(_Args...)> __task)
1165 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1168 template<
typename _Func,
typename _Alloc>
1169 _Task_state(_Func&& __task,
const _Alloc& __a)
1170 : _M_result(_S_allocate_result<_Res>(__a)),
1171 _M_task(allocator_arg, __a, std::move(__task))
1175 _M_run(_Args... __args)
1178 auto __bound = std::bind<_Res>(
std::ref(_M_task),
1179 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1180 _Task_setter<_Task_state> __setter{
this, std::move(__bound) };
1181 _M_set_result(std::move(__setter));
1184 template<
typename,
typename>
friend class _Task_setter;
1185 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1186 _Ptr_type _M_result;
1187 std::function<_Res(_Args...)> _M_task;
1189 template<
typename _Tp>
1190 static reference_wrapper<_Tp>
1191 _S_maybe_wrap_ref(_Tp& __t)
1194 template<
typename _Tp>
1195 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1197 _S_maybe_wrap_ref(_Tp&& __t)
1198 {
return std::forward<_Tp>(__t); }
1202 template<
typename _Res,
typename... _ArgTypes>
1203 class packaged_task<_Res(_ArgTypes...)>
1205 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1209 typedef _Res result_type;
1214 template<
typename _Fn>
1216 packaged_task(
const _Fn& __fn)
1217 : _M_state(std::make_shared<_State_type>(__fn))
1220 template<
typename _Fn>
1222 packaged_task(_Fn&& __fn)
1223 : _M_state(std::make_shared<_State_type>(std::move(__fn)))
1227 packaged_task(_Res(*__fn)(_ArgTypes...))
1228 : _M_state(std::make_shared<_State_type>(__fn))
1231 template<
typename _Fn,
typename _Allocator>
1234 : _M_state(std::allocate_shared<_State_type>(__a, std::move(__fn)))
1239 if (static_cast<bool>(_M_state) && !_M_state.unique())
1240 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1244 packaged_task(packaged_task&) =
delete;
1245 packaged_task& operator=(packaged_task&) =
delete;
1248 packaged_task(packaged_task&& __other)
1249 { this->swap(__other); }
1251 packaged_task& operator=(packaged_task&& __other)
1253 packaged_task(std::move(__other)).swap(*
this);
1258 swap(packaged_task& __other)
1259 { _M_state.swap(__other._M_state); }
1263 {
return static_cast<bool>(_M_state); }
1272 operator()(_ArgTypes... __args)
1274 __future_base::_State_base::_S_check(_M_state);
1275 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1281 __future_base::_State_base::_S_check(_M_state);
1282 packaged_task(std::move(_M_state->_M_task)).swap(*
this);
1287 template<
typename _Res,
typename... _ArgTypes>
1289 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1290 packaged_task<_Res(_ArgTypes...)>& __y)
1293 template<
typename _Res,
typename _Alloc>
1294 struct uses_allocator<packaged_task<_Res>, _Alloc>
1298 template<
typename _Res>
1299 class __future_base::_Deferred_state :
public __future_base::_State_base
1302 typedef _Res _Res_type;
1305 _Deferred_state(std::function<_Res()>&& __fn)
1306 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1310 template<
typename,
typename>
friend class _Task_setter;
1311 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1312 _Ptr_type _M_result;
1313 std::function<_Res()> _M_fn;
1318 _Task_setter<_Deferred_state> __setter{
this, _M_fn };
1320 _M_set_result(std::move(__setter),
true);
1324 template<
typename _Res>
1325 class __future_base::_Async_state :
public __future_base::_State_base
1328 typedef _Res _Res_type;
1331 _Async_state(std::function<_Res()>&& __fn)
1332 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1333 _M_thread(
mem_fn(&_Async_state::_M_do_run), this)
1336 ~_Async_state() { _M_thread.join(); }
1341 _Task_setter<_Async_state> __setter{
this, std::move(_M_fn) };
1342 _M_set_result(std::move(__setter));
1345 template<
typename,
typename>
friend class _Task_setter;
1346 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1347 _Ptr_type _M_result;
1348 std::function<_Res()> _M_fn;
1353 template<
typename _Fn,
typename... _Args>
1354 future<
typename result_of<_Fn(_Args...)>::type>
1357 typedef typename result_of<_Fn(_Args...)>::type result_type;
1361 typedef typename __future_base::_Async_state<result_type> _State;
1362 __state = std::make_shared<_State>(std::bind<result_type>(
1363 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1367 typedef typename __future_base::_Deferred_state<result_type> _State;
1368 __state = std::make_shared<_State>(std::bind<result_type>(
1369 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1375 template<
typename _Fn,
typename... _Args>
1377 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
1381 std::forward<_Args>(__args)...);
1384 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1388 _GLIBCXX_END_NAMESPACE_VERSION
1391 #endif // __GXX_EXPERIMENTAL_CXX0X__
1393 #endif // _GLIBCXX_FUTURE