1 // <future> -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/future
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_FUTURE
30 #define _GLIBCXX_FUTURE 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
42 #include <condition_variable>
43 #include <system_error>
46 #include <bits/functexcept.h>
48 namespace std _GLIBCXX_VISIBILITY(default)
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 * @defgroup futures Futures
54 * @ingroup concurrency
56 * Classes for futures support.
60 /// Error code for futures
61 enum class future_errc
64 future_already_retrieved,
65 promise_already_satisfied,
71 struct is_error_code_enum<future_errc> : public true_type { };
73 /// Points to a statically-allocated object derived from error_category.
77 /// Overload for make_error_code.
79 make_error_code(future_errc __errc)
80 { return error_code(static_cast<int>(__errc), future_category()); }
82 /// Overload for make_error_condition.
83 inline error_condition
84 make_error_condition(future_errc __errc)
85 { return error_condition(static_cast<int>(__errc), future_category()); }
88 * @brief Exception type thrown by futures.
91 class future_error : public logic_error
96 explicit future_error(error_code __ec)
97 : logic_error("std::future_error"), _M_code(__ec)
100 virtual ~future_error() throw();
103 what() const throw();
106 code() const throw() { return _M_code; }
109 // Forward declarations.
110 template<typename _Res>
113 template<typename _Res>
116 template<typename _Res>
119 template<typename _Signature>
122 template<typename _Res>
125 /// Launch code for futures
133 /// Status code for futures
134 enum class future_status
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 _Fn, typename... _Args>
147 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
148 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
150 async(_Fn&& __fn, _Args&&... __args);
152 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
153 && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
155 /// Base class and enclosing scope.
158 /// Base class for results.
161 exception_ptr _M_error;
163 _Result_base(const _Result_base&) = delete;
164 _Result_base& operator=(const _Result_base&) = delete;
166 // _M_destroy() allows derived classes to control deallocation
167 virtual void _M_destroy() = 0;
171 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
176 virtual ~_Result_base();
180 template<typename _Res>
181 struct _Result : _Result_base
184 typedef alignment_of<_Res> __a_of;
185 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
186 typedef typename __align_storage::type __align_type;
188 __align_type _M_storage;
192 _Result() : _M_initialized() { }
200 // Return lvalue, future will add const or rvalue-reference
202 _M_value() { return *static_cast<_Res*>(_M_addr()); }
205 _M_set(const _Res& __res)
207 ::new (_M_addr()) _Res(__res);
208 _M_initialized = true;
214 ::new (_M_addr()) _Res(std::move(__res));
215 _M_initialized = true;
219 void _M_destroy() { delete this; }
221 void* _M_addr() { return static_cast<void*>(&_M_storage); }
224 // TODO: use template alias when available
226 template<typename _Res>
227 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
229 /// A unique_ptr based on the instantiating type.
230 template<typename _Res>
233 typedef unique_ptr<_Res, _Result_base::_Deleter> type;
237 template<typename _Res, typename _Alloc>
238 struct _Result_alloc : _Result<_Res>, _Alloc
240 typedef typename _Alloc::template rebind<_Result_alloc>::other
244 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
250 __allocator_type __a(*this);
252 __a.deallocate(this, 1);
256 template<typename _Res, typename _Allocator>
257 static typename _Ptr<_Result_alloc<_Res, _Allocator>>::type
258 _S_allocate_result(const _Allocator& __a)
260 typedef _Result_alloc<_Res, _Allocator> __result_type;
261 typename __result_type::__allocator_type __a2(__a);
262 __result_type* __p = __a2.allocate(1);
265 __a2.construct(__p, __a);
269 __a2.deallocate(__p, 1);
270 __throw_exception_again;
272 return typename _Ptr<__result_type>::type(__p);
276 /// Base class for state between a promise and one or more
277 /// associated futures.
280 typedef _Ptr<_Result_base>::type _Ptr_type;
284 condition_variable _M_cond;
285 atomic_flag _M_retrieved;
289 _State_base() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
290 _State_base(const _State_base&) = delete;
291 _State_base& operator=(const _State_base&) = delete;
292 virtual ~_State_base();
298 unique_lock<mutex> __lock(_M_mutex);
300 _M_cond.wait(__lock, std::bind<bool>(&_State_base::_M_ready, this));
304 template<typename _Rep, typename _Period>
306 wait_for(const chrono::duration<_Rep, _Period>& __rel)
308 unique_lock<mutex> __lock(_M_mutex);
309 auto __bound = std::bind<bool>(&_State_base::_M_ready, this);
310 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
313 template<typename _Clock, typename _Duration>
315 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
317 unique_lock<mutex> __lock(_M_mutex);
318 auto __bound = std::bind<bool>(&_State_base::_M_ready, this);
319 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
323 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
325 bool __set = __ignore_failure;
326 // all calls to this function are serialized,
327 // side-effects of invoking __res only happen once
328 call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
331 __throw_future_error(int(future_errc::promise_already_satisfied));
335 _M_break_promise(_Ptr_type __res)
337 if (static_cast<bool>(__res))
339 error_code __ec(make_error_code(future_errc::broken_promise));
340 __res->_M_error = copy_exception(future_error(__ec));
342 lock_guard<mutex> __lock(_M_mutex);
343 _M_result.swap(__res);
345 _M_cond.notify_all();
349 // Called when this object is passed to a future.
351 _M_set_retrieved_flag()
353 if (_M_retrieved.test_and_set())
354 __throw_future_error(int(future_errc::future_already_retrieved));
357 template<typename _Res, typename _Arg>
361 template<typename _Res, typename _Arg>
362 struct _Setter<_Res, _Arg&>
364 // check this is only used by promise<R>::set_value(const R&)
365 // or promise<R>::set_value(R&)
366 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
367 || is_same<const _Res, _Arg>::value, // promise<R>
368 "Invalid specialisation");
370 typename promise<_Res>::_Ptr_type operator()()
372 _State_base::_S_check(_M_promise->_M_future);
373 _M_promise->_M_storage->_M_set(_M_arg);
374 return std::move(_M_promise->_M_storage);
376 promise<_Res>* _M_promise;
381 template<typename _Res>
382 struct _Setter<_Res, _Res&&>
384 typename promise<_Res>::_Ptr_type operator()()
386 _State_base::_S_check(_M_promise->_M_future);
387 _M_promise->_M_storage->_M_set(std::move(_M_arg));
388 return std::move(_M_promise->_M_storage);
390 promise<_Res>* _M_promise;
394 struct __exception_ptr_tag { };
397 template<typename _Res>
398 struct _Setter<_Res, __exception_ptr_tag>
400 typename promise<_Res>::_Ptr_type operator()()
402 _State_base::_S_check(_M_promise->_M_future);
403 _M_promise->_M_storage->_M_error = _M_ex;
404 return std::move(_M_promise->_M_storage);
407 promise<_Res>* _M_promise;
408 exception_ptr& _M_ex;
411 template<typename _Res, typename _Arg>
412 static _Setter<_Res, _Arg&&>
413 __setter(promise<_Res>* __prom, _Arg&& __arg)
415 return _Setter<_Res, _Arg&&>{ __prom, __arg };
418 template<typename _Res>
419 static _Setter<_Res, __exception_ptr_tag>
420 __setter(exception_ptr& __ex, promise<_Res>* __prom)
422 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
425 static _Setter<void, void>
426 __setter(promise<void>* __prom);
428 template<typename _Tp>
430 _S_check(const shared_ptr<_Tp>& __p)
432 if (!static_cast<bool>(__p))
433 __throw_future_error((int)future_errc::no_state);
438 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
440 _Ptr_type __res = __f();
442 lock_guard<mutex> __lock(_M_mutex);
443 _M_result.swap(__res);
445 _M_cond.notify_all();
449 bool _M_ready() const { return static_cast<bool>(_M_result); }
451 virtual void _M_run_deferred() { }
454 template<typename _Res>
455 class _Deferred_state;
457 template<typename _Res>
460 template<typename _Signature>
463 template<typename _StateT, typename _Res = typename _StateT::_Res_type>
467 /// Partial specialization for reference types.
468 template<typename _Res>
469 struct __future_base::_Result<_Res&> : __future_base::_Result_base
471 _Result() : _M_value_ptr() { }
473 void _M_set(_Res& __res) { _M_value_ptr = &__res; }
475 _Res& _M_get() { return *_M_value_ptr; }
480 void _M_destroy() { delete this; }
483 /// Explicit specialization for void.
485 struct __future_base::_Result<void> : __future_base::_Result_base
488 void _M_destroy() { delete this; }
492 /// Common implementation for future and shared_future.
493 template<typename _Res>
494 class __basic_future : public __future_base
497 typedef shared_ptr<_State_base> __state_type;
498 typedef __future_base::_Result<_Res>& __result_type;
501 __state_type _M_state;
505 __basic_future(const __basic_future&) = delete;
506 __basic_future& operator=(const __basic_future&) = delete;
509 valid() const { return static_cast<bool>(_M_state); }
514 _State_base::_S_check(_M_state);
518 template<typename _Rep, typename _Period>
520 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
522 _State_base::_S_check(_M_state);
523 return _M_state->wait_for(__rel);
526 template<typename _Clock, typename _Duration>
528 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
530 _State_base::_S_check(_M_state);
531 return _M_state->wait_until(__abs);
535 /// Wait for the state to be ready and rethrow any stored exception
539 _State_base::_S_check(_M_state);
540 _Result_base& __res = _M_state->wait();
541 if (!(__res._M_error == 0))
542 rethrow_exception(__res._M_error);
543 return static_cast<__result_type>(__res);
546 void _M_swap(__basic_future& __that)
548 _M_state.swap(__that._M_state);
551 // Construction of a future by promise::get_future()
553 __basic_future(const __state_type& __state) : _M_state(__state)
555 _State_base::_S_check(_M_state);
556 _M_state->_M_set_retrieved_flag();
559 // Copy construction from a shared_future
561 __basic_future(const shared_future<_Res>&);
563 // Move construction from a shared_future
565 __basic_future(shared_future<_Res>&&);
567 // Move construction from a future
569 __basic_future(future<_Res>&&);
571 constexpr __basic_future() : _M_state() { }
575 explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { }
576 ~_Reset() { _M_fut._M_state.reset(); }
577 __basic_future& _M_fut;
582 /// Primary template for future.
583 template<typename _Res>
584 class future : public __basic_future<_Res>
586 friend class promise<_Res>;
587 template<typename> friend class packaged_task;
588 template<typename _Fn, typename... _Args>
589 friend future<typename result_of<_Fn(_Args...)>::type>
590 async(launch, _Fn&&, _Args&&...);
592 typedef __basic_future<_Res> _Base_type;
593 typedef typename _Base_type::__state_type __state_type;
596 future(const __state_type& __state) : _Base_type(__state) { }
599 constexpr future() : _Base_type() { }
602 future(future&& __uf) : _Base_type(std::move(__uf)) { }
605 future(const future&) = delete;
606 future& operator=(const future&) = delete;
608 future& operator=(future&& __fut)
610 future(std::move(__fut))._M_swap(*this);
614 /// Retrieving the value
618 typename _Base_type::_Reset __reset(*this);
619 return std::move(this->_M_get_result()._M_value());
622 shared_future<_Res> share();
625 /// Partial specialization for future<R&>
626 template<typename _Res>
627 class future<_Res&> : public __basic_future<_Res&>
629 friend class promise<_Res&>;
630 template<typename> friend class packaged_task;
631 template<typename _Fn, typename... _Args>
632 friend future<typename result_of<_Fn(_Args...)>::type>
633 async(launch, _Fn&&, _Args&&...);
635 typedef __basic_future<_Res&> _Base_type;
636 typedef typename _Base_type::__state_type __state_type;
639 future(const __state_type& __state) : _Base_type(__state) { }
642 constexpr future() : _Base_type() { }
645 future(future&& __uf) : _Base_type(std::move(__uf)) { }
648 future(const future&) = delete;
649 future& operator=(const future&) = delete;
651 future& operator=(future&& __fut)
653 future(std::move(__fut))._M_swap(*this);
657 /// Retrieving the value
661 typename _Base_type::_Reset __reset(*this);
662 return this->_M_get_result()._M_get();
665 shared_future<_Res&> share();
668 /// Explicit specialization for future<void>
670 class future<void> : public __basic_future<void>
672 friend class promise<void>;
673 template<typename> friend class packaged_task;
674 template<typename _Fn, typename... _Args>
675 friend future<typename result_of<_Fn(_Args...)>::type>
676 async(launch, _Fn&&, _Args&&...);
678 typedef __basic_future<void> _Base_type;
679 typedef typename _Base_type::__state_type __state_type;
682 future(const __state_type& __state) : _Base_type(__state) { }
685 constexpr future() : _Base_type() { }
688 future(future&& __uf) : _Base_type(std::move(__uf)) { }
691 future(const future&) = delete;
692 future& operator=(const future&) = delete;
694 future& operator=(future&& __fut)
696 future(std::move(__fut))._M_swap(*this);
700 /// Retrieving the value
704 typename _Base_type::_Reset __reset(*this);
705 this->_M_get_result();
708 shared_future<void> share();
712 /// Primary template for shared_future.
713 template<typename _Res>
714 class shared_future : public __basic_future<_Res>
716 typedef __basic_future<_Res> _Base_type;
719 constexpr shared_future() : _Base_type() { }
722 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
724 /// Construct from a future rvalue
725 shared_future(future<_Res>&& __uf)
726 : _Base_type(std::move(__uf))
729 /// Construct from a shared_future rvalue
730 shared_future(shared_future&& __sf)
731 : _Base_type(std::move(__sf))
734 shared_future& operator=(const shared_future& __sf)
736 shared_future(__sf)._M_swap(*this);
740 shared_future& operator=(shared_future&& __sf)
742 shared_future(std::move(__sf))._M_swap(*this);
746 /// Retrieving the value
750 typename _Base_type::__result_type __r = this->_M_get_result();
751 _Res& __rs(__r._M_value());
756 /// Partial specialization for shared_future<R&>
757 template<typename _Res>
758 class shared_future<_Res&> : public __basic_future<_Res&>
760 typedef __basic_future<_Res&> _Base_type;
763 constexpr shared_future() : _Base_type() { }
766 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
768 /// Construct from a future rvalue
769 shared_future(future<_Res&>&& __uf)
770 : _Base_type(std::move(__uf))
773 /// Construct from a shared_future rvalue
774 shared_future(shared_future&& __sf)
775 : _Base_type(std::move(__sf))
778 shared_future& operator=(const shared_future& __sf)
780 shared_future(__sf)._M_swap(*this);
784 shared_future& operator=(shared_future&& __sf)
786 shared_future(std::move(__sf))._M_swap(*this);
790 /// Retrieving the value
792 get() { return this->_M_get_result()._M_get(); }
795 /// Explicit specialization for shared_future<void>
797 class shared_future<void> : public __basic_future<void>
799 typedef __basic_future<void> _Base_type;
802 constexpr shared_future() : _Base_type() { }
805 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
807 /// Construct from a future rvalue
808 shared_future(future<void>&& __uf)
809 : _Base_type(std::move(__uf))
812 /// Construct from a shared_future rvalue
813 shared_future(shared_future&& __sf)
814 : _Base_type(std::move(__sf))
817 shared_future& operator=(const shared_future& __sf)
819 shared_future(__sf)._M_swap(*this);
823 shared_future& operator=(shared_future&& __sf)
825 shared_future(std::move(__sf))._M_swap(*this);
829 // Retrieving the value
831 get() { this->_M_get_result(); }
834 // Now we can define the protected __basic_future constructors.
835 template<typename _Res>
836 inline __basic_future<_Res>::
837 __basic_future(const shared_future<_Res>& __sf)
838 : _M_state(__sf._M_state)
841 template<typename _Res>
842 inline __basic_future<_Res>::
843 __basic_future(shared_future<_Res>&& __sf)
844 : _M_state(std::move(__sf._M_state))
847 template<typename _Res>
848 inline __basic_future<_Res>::
849 __basic_future(future<_Res>&& __uf)
850 : _M_state(std::move(__uf._M_state))
853 template<typename _Res>
854 inline shared_future<_Res>
855 future<_Res>::share()
856 { return shared_future<_Res>(std::move(*this)); }
858 template<typename _Res>
859 inline shared_future<_Res&>
860 future<_Res&>::share()
861 { return shared_future<_Res&>(std::move(*this)); }
863 inline shared_future<void>
864 future<void>::share()
865 { return shared_future<void>(std::move(*this)); }
867 /// Primary template for promise
868 template<typename _Res>
871 typedef __future_base::_State_base _State;
872 typedef __future_base::_Result<_Res> _Res_type;
873 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
874 template<typename, typename> friend class _State::_Setter;
876 shared_ptr<_State> _M_future;
877 _Ptr_type _M_storage;
881 : _M_future(std::make_shared<_State>()),
882 _M_storage(new _Res_type())
885 promise(promise&& __rhs)
886 : _M_future(std::move(__rhs._M_future)),
887 _M_storage(std::move(__rhs._M_storage))
890 template<typename _Allocator>
891 promise(allocator_arg_t, const _Allocator& __a)
892 : _M_future(std::allocate_shared<_State>(__a)),
893 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
896 promise(const promise&) = delete;
900 if (static_cast<bool>(_M_future) && !_M_future.unique())
901 _M_future->_M_break_promise(std::move(_M_storage));
906 operator=(promise&& __rhs)
908 promise(std::move(__rhs)).swap(*this);
912 promise& operator=(const promise&) = delete;
917 _M_future.swap(__rhs._M_future);
918 _M_storage.swap(__rhs._M_storage);
921 // Retrieving the result
924 { return future<_Res>(_M_future); }
926 // Setting the result
928 set_value(const _Res& __r)
930 auto __setter = _State::__setter(this, __r);
931 _M_future->_M_set_result(std::move(__setter));
935 set_value(_Res&& __r)
937 auto __setter = _State::__setter(this, std::move(__r));
938 _M_future->_M_set_result(std::move(__setter));
942 set_exception(exception_ptr __p)
944 auto __setter = _State::__setter(__p, this);
945 _M_future->_M_set_result(std::move(__setter));
949 template<typename _Res>
951 swap(promise<_Res>& __x, promise<_Res>& __y)
954 template<typename _Res, typename _Alloc>
955 struct uses_allocator<promise<_Res>, _Alloc>
956 : public true_type { };
959 /// Partial specialization for promise<R&>
960 template<typename _Res>
963 typedef __future_base::_State_base _State;
964 typedef __future_base::_Result<_Res&> _Res_type;
965 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
966 template<typename, typename> friend class _State::_Setter;
968 shared_ptr<_State> _M_future;
969 _Ptr_type _M_storage;
973 : _M_future(std::make_shared<_State>()),
974 _M_storage(new _Res_type())
977 promise(promise&& __rhs)
978 : _M_future(std::move(__rhs._M_future)),
979 _M_storage(std::move(__rhs._M_storage))
982 template<typename _Allocator>
983 promise(allocator_arg_t, const _Allocator& __a)
984 : _M_future(std::allocate_shared<_State>(__a)),
985 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
988 promise(const promise&) = delete;
992 if (static_cast<bool>(_M_future) && !_M_future.unique())
993 _M_future->_M_break_promise(std::move(_M_storage));
998 operator=(promise&& __rhs)
1000 promise(std::move(__rhs)).swap(*this);
1004 promise& operator=(const promise&) = delete;
1007 swap(promise& __rhs)
1009 _M_future.swap(__rhs._M_future);
1010 _M_storage.swap(__rhs._M_storage);
1013 // Retrieving the result
1016 { return future<_Res&>(_M_future); }
1018 // Setting the result
1020 set_value(_Res& __r)
1022 auto __setter = _State::__setter(this, __r);
1023 _M_future->_M_set_result(std::move(__setter));
1027 set_exception(exception_ptr __p)
1029 auto __setter = _State::__setter(__p, this);
1030 _M_future->_M_set_result(std::move(__setter));
1034 /// Explicit specialization for promise<void>
1038 typedef __future_base::_State_base _State;
1039 typedef __future_base::_Result<void> _Res_type;
1040 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
1041 template<typename, typename> friend class _State::_Setter;
1043 shared_ptr<_State> _M_future;
1044 _Ptr_type _M_storage;
1048 : _M_future(std::make_shared<_State>()),
1049 _M_storage(new _Res_type())
1052 promise(promise&& __rhs)
1053 : _M_future(std::move(__rhs._M_future)),
1054 _M_storage(std::move(__rhs._M_storage))
1057 template<typename _Allocator>
1058 promise(allocator_arg_t, const _Allocator& __a)
1059 : _M_future(std::allocate_shared<_State>(__a)),
1060 _M_storage(__future_base::_S_allocate_result<void>(__a))
1063 promise(const promise&) = delete;
1067 if (static_cast<bool>(_M_future) && !_M_future.unique())
1068 _M_future->_M_break_promise(std::move(_M_storage));
1073 operator=(promise&& __rhs)
1075 promise(std::move(__rhs)).swap(*this);
1079 promise& operator=(const promise&) = delete;
1082 swap(promise& __rhs)
1084 _M_future.swap(__rhs._M_future);
1085 _M_storage.swap(__rhs._M_storage);
1088 // Retrieving the result
1091 { return future<void>(_M_future); }
1093 // Setting the result
1097 set_exception(exception_ptr __p)
1099 auto __setter = _State::__setter(__p, this);
1100 _M_future->_M_set_result(std::move(__setter));
1106 struct __future_base::_State_base::_Setter<void, void>
1108 promise<void>::_Ptr_type operator()()
1110 _State_base::_S_check(_M_promise->_M_future);
1111 return std::move(_M_promise->_M_storage);
1114 promise<void>* _M_promise;
1117 inline __future_base::_State_base::_Setter<void, void>
1118 __future_base::_State_base::__setter(promise<void>* __prom)
1120 return _Setter<void, void>{ __prom };
1124 promise<void>::set_value()
1126 auto __setter = _State::__setter(this);
1127 _M_future->_M_set_result(std::move(__setter));
1131 template<typename _StateT, typename _Res>
1132 struct __future_base::_Task_setter
1134 typename _StateT::_Ptr_type operator()()
1138 _M_state->_M_result->_M_set(_M_fn());
1142 _M_state->_M_result->_M_error = current_exception();
1144 return std::move(_M_state->_M_result);
1147 std::function<_Res()> _M_fn;
1150 template<typename _StateT>
1151 struct __future_base::_Task_setter<_StateT, void>
1153 typename _StateT::_Ptr_type operator()()
1161 _M_state->_M_result->_M_error = current_exception();
1163 return std::move(_M_state->_M_result);
1166 std::function<void()> _M_fn;
1169 template<typename _Res, typename... _Args>
1170 struct __future_base::_Task_state<_Res(_Args...)>
1171 : __future_base::_State_base
1173 typedef _Res _Res_type;
1175 _Task_state(std::function<_Res(_Args...)> __task)
1176 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1179 template<typename _Func, typename _Alloc>
1180 _Task_state(_Func&& __task, const _Alloc& __a)
1181 : _M_result(_S_allocate_result<_Res>(__a)),
1182 _M_task(allocator_arg, __a, std::move(__task))
1186 _M_run(_Args... __args)
1188 // bound arguments decay so wrap lvalue references
1189 auto __bound = std::bind<_Res>(std::ref(_M_task),
1190 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1191 _Task_setter<_Task_state> __setter{ this, std::move(__bound) };
1192 _M_set_result(std::move(__setter));
1195 template<typename, typename> friend class _Task_setter;
1196 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1197 _Ptr_type _M_result;
1198 std::function<_Res(_Args...)> _M_task;
1200 template<typename _Tp>
1201 static reference_wrapper<_Tp>
1202 _S_maybe_wrap_ref(_Tp& __t)
1203 { return std::ref(__t); }
1205 template<typename _Tp>
1206 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1208 _S_maybe_wrap_ref(_Tp&& __t)
1209 { return std::forward<_Tp>(__t); }
1213 template<typename _Res, typename... _ArgTypes>
1214 class packaged_task<_Res(_ArgTypes...)>
1216 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1217 shared_ptr<_State_type> _M_state;
1220 // Construction and destruction
1223 template<typename _Fn>
1225 packaged_task(_Fn&& __fn)
1226 : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn)))
1229 template<typename _Fn, typename _Allocator>
1231 packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn)
1232 : _M_state(std::allocate_shared<_State_type>(__a,
1233 std::forward<_Fn>(__fn)))
1238 if (static_cast<bool>(_M_state) && !_M_state.unique())
1239 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1243 packaged_task(packaged_task&) = delete;
1244 packaged_task& operator=(packaged_task&) = delete;
1247 packaged_task(packaged_task&& __other)
1248 { this->swap(__other); }
1250 packaged_task& operator=(packaged_task&& __other)
1252 packaged_task(std::move(__other)).swap(*this);
1257 swap(packaged_task& __other)
1258 { _M_state.swap(__other._M_state); }
1262 { return static_cast<bool>(_M_state); }
1267 { return future<_Res>(_M_state); }
1271 operator()(_ArgTypes... __args)
1273 __future_base::_State_base::_S_check(_M_state);
1274 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1280 __future_base::_State_base::_S_check(_M_state);
1281 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1286 template<typename _Res, typename... _ArgTypes>
1288 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1289 packaged_task<_Res(_ArgTypes...)>& __y)
1292 template<typename _Res, typename _Alloc>
1293 struct uses_allocator<packaged_task<_Res>, _Alloc>
1294 : public true_type { };
1297 template<typename _Res>
1298 class __future_base::_Deferred_state : public __future_base::_State_base
1301 typedef _Res _Res_type;
1304 _Deferred_state(std::function<_Res()>&& __fn)
1305 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1309 template<typename, typename> friend class _Task_setter;
1310 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1311 _Ptr_type _M_result;
1312 std::function<_Res()> _M_fn;
1317 _Task_setter<_Deferred_state> __setter{ this, _M_fn };
1318 // safe to call multiple times so ignore failure
1319 _M_set_result(std::move(__setter), true);
1323 template<typename _Res>
1324 class __future_base::_Async_state : public __future_base::_State_base
1327 typedef _Res _Res_type;
1330 _Async_state(std::function<_Res()>&& __fn)
1331 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1332 _M_thread(mem_fn(&_Async_state::_M_do_run), this)
1335 ~_Async_state() { _M_thread.join(); }
1340 _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) };
1341 _M_set_result(std::move(__setter));
1344 template<typename, typename> friend class _Task_setter;
1345 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1346 _Ptr_type _M_result;
1347 std::function<_Res()> _M_fn;
1352 template<typename _Fn, typename... _Args>
1353 future<typename result_of<_Fn(_Args...)>::type>
1354 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1356 typedef typename result_of<_Fn(_Args...)>::type result_type;
1357 std::shared_ptr<__future_base::_State_base> __state;
1358 if (__policy == launch::async)
1360 typedef typename __future_base::_Async_state<result_type> _State;
1361 __state = std::make_shared<_State>(std::bind<result_type>(
1362 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1366 typedef typename __future_base::_Deferred_state<result_type> _State;
1367 __state = std::make_shared<_State>(std::bind<result_type>(
1368 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1370 return future<result_type>(__state);
1373 /// async, potential overload
1374 template<typename _Fn, typename... _Args>
1376 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
1377 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
1379 async(_Fn&& __fn, _Args&&... __args)
1381 return async(launch::any, std::forward<_Fn>(__fn),
1382 std::forward<_Args>(__args)...);
1385 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1386 // && _GLIBCXX_ATOMIC_BUILTINS_4
1389 _GLIBCXX_END_NAMESPACE_VERSION
1392 #endif // __GXX_EXPERIMENTAL_CXX0X__
1394 #endif // _GLIBCXX_FUTURE