1 // <future> -*- C++ -*-
3 // Copyright (C) 2009, 2010 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/>.
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 _GLIBCXX_BEGIN_NAMESPACE(std)
51 * @defgroup futures Futures
52 * @ingroup concurrency
54 * Classes for futures support.
58 /// Error code for futures
59 enum class future_errc
62 future_already_retrieved,
63 promise_already_satisfied,
69 struct is_error_code_enum<future_errc> : public true_type { };
71 /// Points to a statically-allocated object derived from error_category.
75 /// Overload for make_error_code.
77 make_error_code(future_errc __errc)
78 { return error_code(static_cast<int>(__errc), future_category()); }
80 /// Overload for make_error_condition.
81 inline error_condition
82 make_error_condition(future_errc __errc)
83 { return error_condition(static_cast<int>(__errc), future_category()); }
86 * @brief Exception type thrown by futures.
89 class future_error : public logic_error
94 explicit future_error(error_code __ec)
95 : logic_error("std::future_error"), _M_code(__ec)
98 virtual ~future_error() throw();
101 what() const throw();
104 code() const throw() { return _M_code; }
107 // Forward declarations.
108 template<typename _Res>
111 template<typename _Res>
114 template<typename _Res>
117 template<typename _Signature>
120 template<typename _Res>
123 /// Launch code for futures
131 /// Status code for futures
132 enum class future_status
139 template<typename _Fn, typename... _Args>
140 future<typename result_of<_Fn(_Args...)>::type>
141 async(launch __policy, _Fn&& __fn, _Args&&... __args);
143 template<typename _Fn, typename... _Args>
145 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
146 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
148 async(_Fn&& __fn, _Args&&... __args);
150 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
151 && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
153 /// Base class and enclosing scope.
156 /// Base class for results.
159 exception_ptr _M_error;
161 _Result_base() = default;
162 _Result_base(const _Result_base&) = delete;
163 _Result_base& operator=(const _Result_base&) = delete;
165 // _M_destroy() allows derived classes to control deallocation
166 virtual void _M_destroy() = 0;
170 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
178 template<typename _Res>
179 struct _Result : _Result_base
182 typedef alignment_of<_Res> __a_of;
183 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
184 typedef typename __align_storage::type __align_type;
186 __align_type _M_storage;
190 _Result() : _M_initialized() { }
198 // Return lvalue, future will add const or rvalue-reference
200 _M_value() { return *static_cast<_Res*>(_M_addr()); }
203 _M_set(const _Res& __res)
205 ::new (_M_addr()) _Res(__res);
206 _M_initialized = true;
212 ::new (_M_addr()) _Res(std::move(__res));
213 _M_initialized = true;
217 void _M_destroy() { delete this; }
219 void* _M_addr() { return static_cast<void*>(&_M_storage); }
222 // TODO: use template alias when available
224 template<typename _Res>
225 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
227 /// A unique_ptr based on the instantiating type.
228 template<typename _Res>
231 typedef unique_ptr<_Res, _Result_base::_Deleter> type;
235 template<typename _Res, typename _Alloc>
236 struct _Result_alloc : _Result<_Res>, _Alloc
238 typedef typename _Alloc::template rebind<_Result_alloc>::other
242 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
248 __allocator_type __a(*this);
250 __a.deallocate(this, 1);
254 template<typename _Res, typename _Allocator>
255 static typename _Ptr<_Result_alloc<_Res, _Allocator>>::type
256 _S_allocate_result(const _Allocator& __a)
258 typedef _Result_alloc<_Res, _Allocator> __result_type;
259 typename __result_type::__allocator_type __a2(__a);
260 __result_type* __p = __a2.allocate(1);
263 __a2.construct(__p, __a);
267 __a2.deallocate(__p, 1);
268 __throw_exception_again;
270 return typename _Ptr<__result_type>::type(__p);
274 /// Shared state between a promise and one or more associated futures.
277 typedef _Ptr<_Result_base>::type _Ptr_type;
281 condition_variable _M_cond;
282 atomic_flag _M_retrieved;
286 _State() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
288 _State(const _State&) = delete;
289 _State& operator=(const _State&) = delete;
295 unique_lock<mutex> __lock(_M_mutex);
297 _M_cond.wait(__lock, std::bind<bool>(&_State::_M_ready, this));
301 template<typename _Rep, typename _Period>
303 wait_for(const chrono::duration<_Rep, _Period>& __rel)
305 unique_lock<mutex> __lock(_M_mutex);
306 auto __bound = std::bind<bool>(&_State::_M_ready, this);
307 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
310 template<typename _Clock, typename _Duration>
312 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
314 unique_lock<mutex> __lock(_M_mutex);
315 auto __bound = std::bind<bool>(&_State::_M_ready, this);
316 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
320 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
322 bool __set = __ignore_failure;
323 // all calls to this function are serialized,
324 // side-effects of invoking __res only happen once
325 call_once(_M_once, &_State::_M_do_set, this, ref(__res),
328 __throw_future_error(int(future_errc::promise_already_satisfied));
332 _M_break_promise(_Ptr_type __res)
334 if (static_cast<bool>(__res))
336 error_code __ec(make_error_code(future_errc::broken_promise));
337 __res->_M_error = copy_exception(future_error(__ec));
339 lock_guard<mutex> __lock(_M_mutex);
340 _M_result.swap(__res);
342 _M_cond.notify_all();
346 // Called when this object is passed to a future.
348 _M_set_retrieved_flag()
350 if (_M_retrieved.test_and_set())
351 __throw_future_error(int(future_errc::future_already_retrieved));
354 template<typename _Res, typename _Arg>
358 template<typename _Res, typename _Arg>
359 struct _Setter<_Res, _Arg&>
361 // check this is only used by promise<R>::set_value(const R&)
362 // or promise<R>::set_value(R&)
363 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
364 || is_same<const _Res, _Arg>::value, // promise<R>
365 "Invalid specialisation");
367 typename promise<_Res>::_Ptr_type operator()()
369 _State::_S_check(_M_promise->_M_future);
370 _M_promise->_M_storage->_M_set(_M_arg);
371 return std::move(_M_promise->_M_storage);
373 promise<_Res>* _M_promise;
378 template<typename _Res>
379 struct _Setter<_Res, _Res&&>
381 typename promise<_Res>::_Ptr_type operator()()
383 _State::_S_check(_M_promise->_M_future);
384 _M_promise->_M_storage->_M_set(std::move(_M_arg));
385 return std::move(_M_promise->_M_storage);
387 promise<_Res>* _M_promise;
391 struct __exception_ptr_tag { };
394 template<typename _Res>
395 struct _Setter<_Res, __exception_ptr_tag>
397 typename promise<_Res>::_Ptr_type operator()()
399 _State::_S_check(_M_promise->_M_future);
400 _M_promise->_M_storage->_M_error = _M_ex;
401 return std::move(_M_promise->_M_storage);
404 promise<_Res>* _M_promise;
405 exception_ptr& _M_ex;
408 template<typename _Res, typename _Arg>
409 static _Setter<_Res, _Arg&&>
410 __setter(promise<_Res>* __prom, _Arg&& __arg)
412 return _Setter<_Res, _Arg&&>{ __prom, __arg };
415 template<typename _Res>
416 static _Setter<_Res, __exception_ptr_tag>
417 __setter(exception_ptr& __ex, promise<_Res>* __prom)
419 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
422 static _Setter<void, void>
423 __setter(promise<void>* __prom);
425 template<typename _Tp>
427 _S_check(const shared_ptr<_Tp>& __p)
429 if (!static_cast<bool>(__p))
430 __throw_future_error((int)future_errc::no_state);
435 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
437 _Ptr_type __res = __f();
439 lock_guard<mutex> __lock(_M_mutex);
440 _M_result.swap(__res);
442 _M_cond.notify_all();
446 bool _M_ready() const { return static_cast<bool>(_M_result); }
448 virtual void _M_run_deferred() { }
451 template<typename _Res>
452 class _Deferred_state;
454 template<typename _Res>
457 template<typename _Signature>
460 template<typename _StateT, typename _Res = typename _StateT::_Res_type>
464 inline __future_base::_Result_base::~_Result_base() = default;
466 /// Partial specialization for reference types.
467 template<typename _Res>
468 struct __future_base::_Result<_Res&> : __future_base::_Result_base
470 _Result() : _M_value_ptr() { }
472 void _M_set(_Res& __res) { _M_value_ptr = &__res; }
474 _Res& _M_get() { return *_M_value_ptr; }
479 void _M_destroy() { delete this; }
482 /// Explicit specialization for void.
484 struct __future_base::_Result<void> : __future_base::_Result_base
487 void _M_destroy() { delete this; }
491 /// Common implementation for future and shared_future.
492 template<typename _Res>
493 class __basic_future : public __future_base
496 typedef shared_ptr<_State> __state_type;
497 typedef __future_base::_Result<_Res>& __result_type;
500 __state_type _M_state;
504 __basic_future(const __basic_future&) = delete;
505 __basic_future& operator=(const __basic_future&) = delete;
508 valid() const { return static_cast<bool>(_M_state); }
511 wait() const { _M_state->wait(); }
513 template<typename _Rep, typename _Period>
515 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
516 { return _M_state->wait_for(__rel); }
518 template<typename _Clock, typename _Duration>
520 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
521 { return _M_state->wait_until(__abs); }
524 /// Wait for the state to be ready and rethrow any stored exception
528 _Result_base& __res = _M_state->wait();
529 if (!(__res._M_error == 0))
530 rethrow_exception(__res._M_error);
531 return static_cast<__result_type>(__res);
534 void _M_swap(__basic_future& __that)
536 _M_state.swap(__that._M_state);
539 // Construction of a future by promise::get_future()
541 __basic_future(const __state_type& __state) : _M_state(__state)
543 _State::_S_check(_M_state);
544 _M_state->_M_set_retrieved_flag();
547 // Copy construction from a shared_future
549 __basic_future(const shared_future<_Res>&);
551 // Move construction from a shared_future
553 __basic_future(shared_future<_Res>&&);
555 // Move construction from a future
557 __basic_future(future<_Res>&&);
559 constexpr __basic_future() : _M_state() { }
563 explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { }
564 ~_Reset() { _M_fut._M_state.reset(); }
565 __basic_future& _M_fut;
570 /// Primary template for future.
571 template<typename _Res>
572 class future : public __basic_future<_Res>
574 friend class promise<_Res>;
575 template<typename> friend class packaged_task;
576 template<typename _Fn, typename... _Args>
577 friend future<typename result_of<_Fn(_Args...)>::type>
578 async(launch, _Fn&&, _Args&&...);
580 typedef __basic_future<_Res> _Base_type;
581 typedef typename _Base_type::__state_type __state_type;
584 future(const __state_type& __state) : _Base_type(__state) { }
587 constexpr future() : _Base_type() { }
590 future(future&& __uf) : _Base_type(std::move(__uf)) { }
593 future(const future&) = delete;
594 future& operator=(const future&) = delete;
596 future& operator=(future&& __fut)
598 future(std::move(__fut))._M_swap(*this);
602 /// Retrieving the value
606 typename _Base_type::_Reset __reset(*this);
607 return std::move(this->_M_get_result()._M_value());
611 /// Partial specialization for future<R&>
612 template<typename _Res>
613 class future<_Res&> : public __basic_future<_Res&>
615 friend class promise<_Res&>;
616 template<typename> friend class packaged_task;
617 template<typename _Fn, typename... _Args>
618 friend future<typename result_of<_Fn(_Args...)>::type>
619 async(launch, _Fn&&, _Args&&...);
621 typedef __basic_future<_Res&> _Base_type;
622 typedef typename _Base_type::__state_type __state_type;
625 future(const __state_type& __state) : _Base_type(__state) { }
628 constexpr future() : _Base_type() { }
631 future(future&& __uf) : _Base_type(std::move(__uf)) { }
634 future(const future&) = delete;
635 future& operator=(const future&) = delete;
637 future& operator=(future&& __fut)
639 future(std::move(__fut))._M_swap(*this);
643 /// Retrieving the value
647 typename _Base_type::_Reset __reset(*this);
648 return this->_M_get_result()._M_get();
652 /// Explicit specialization for future<void>
654 class future<void> : public __basic_future<void>
656 friend class promise<void>;
657 template<typename> friend class packaged_task;
658 template<typename _Fn, typename... _Args>
659 friend future<typename result_of<_Fn(_Args...)>::type>
660 async(launch, _Fn&&, _Args&&...);
662 typedef __basic_future<void> _Base_type;
663 typedef typename _Base_type::__state_type __state_type;
666 future(const __state_type& __state) : _Base_type(__state) { }
669 constexpr future() : _Base_type() { }
672 future(future&& __uf) : _Base_type(std::move(__uf)) { }
675 future(const future&) = delete;
676 future& operator=(const future&) = delete;
678 future& operator=(future&& __fut)
680 future(std::move(__fut))._M_swap(*this);
684 /// Retrieving the value
688 typename _Base_type::_Reset __reset(*this);
689 this->_M_get_result();
694 /// Primary template for shared_future.
695 template<typename _Res>
696 class shared_future : public __basic_future<_Res>
698 typedef __basic_future<_Res> _Base_type;
701 constexpr shared_future() : _Base_type() { }
704 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
706 /// Construct from a future rvalue
707 shared_future(future<_Res>&& __uf)
708 : _Base_type(std::move(__uf))
711 /// Construct from a shared_future rvalue
712 shared_future(shared_future&& __sf)
713 : _Base_type(std::move(__sf))
716 shared_future& operator=(const shared_future& __sf)
718 shared_future(__sf)._M_swap(*this);
722 shared_future& operator=(shared_future&& __sf)
724 shared_future(std::move(__sf))._M_swap(*this);
728 /// Retrieving the value
732 typename _Base_type::__result_type __r = this->_M_get_result();
733 _Res& __rs(__r._M_value());
738 /// Partial specialization for shared_future<R&>
739 template<typename _Res>
740 class shared_future<_Res&> : public __basic_future<_Res&>
742 typedef __basic_future<_Res&> _Base_type;
745 constexpr shared_future() : _Base_type() { }
748 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
750 /// Construct from a future rvalue
751 shared_future(future<_Res&>&& __uf)
752 : _Base_type(std::move(__uf))
755 /// Construct from a shared_future rvalue
756 shared_future(shared_future&& __sf)
757 : _Base_type(std::move(__sf))
760 shared_future& operator=(const shared_future& __sf)
762 shared_future(__sf)._M_swap(*this);
766 shared_future& operator=(shared_future&& __sf)
768 shared_future(std::move(__sf))._M_swap(*this);
772 /// Retrieving the value
774 get() { return this->_M_get_result()._M_get(); }
777 /// Explicit specialization for shared_future<void>
779 class shared_future<void> : public __basic_future<void>
781 typedef __basic_future<void> _Base_type;
784 constexpr shared_future() : _Base_type() { }
787 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
789 /// Construct from a future rvalue
790 shared_future(future<void>&& __uf)
791 : _Base_type(std::move(__uf))
794 /// Construct from a shared_future rvalue
795 shared_future(shared_future&& __sf)
796 : _Base_type(std::move(__sf))
799 shared_future& operator=(const shared_future& __sf)
801 shared_future(__sf)._M_swap(*this);
805 shared_future& operator=(shared_future&& __sf)
807 shared_future(std::move(__sf))._M_swap(*this);
811 // Retrieving the value
813 get() { this->_M_get_result(); }
816 // Now we can define the protected __basic_future constructors.
817 template<typename _Res>
818 inline __basic_future<_Res>::
819 __basic_future(const shared_future<_Res>& __sf)
820 : _M_state(__sf._M_state)
823 template<typename _Res>
824 inline __basic_future<_Res>::
825 __basic_future(shared_future<_Res>&& __sf)
826 : _M_state(std::move(__sf._M_state))
829 template<typename _Res>
830 inline __basic_future<_Res>::
831 __basic_future(future<_Res>&& __uf)
832 : _M_state(std::move(__uf._M_state))
836 /// Primary template for promise
837 template<typename _Res>
840 typedef __future_base::_State _State;
841 typedef __future_base::_Result<_Res> _Res_type;
842 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
843 template<typename, typename> friend class _State::_Setter;
845 shared_ptr<_State> _M_future;
846 _Ptr_type _M_storage;
850 : _M_future(std::make_shared<_State>()),
851 _M_storage(new _Res_type())
854 promise(promise&& __rhs)
855 : _M_future(std::move(__rhs._M_future)),
856 _M_storage(std::move(__rhs._M_storage))
859 template<typename _Allocator>
860 promise(allocator_arg_t, const _Allocator& __a)
861 : _M_future(std::allocate_shared<_State>(__a)),
862 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
865 promise(const promise&) = delete;
869 if (static_cast<bool>(_M_future) && !_M_future.unique())
870 _M_future->_M_break_promise(std::move(_M_storage));
875 operator=(promise&& __rhs)
877 promise(std::move(__rhs)).swap(*this);
881 promise& operator=(const promise&) = delete;
886 _M_future.swap(__rhs._M_future);
887 _M_storage.swap(__rhs._M_storage);
890 // Retrieving the result
893 { return future<_Res>(_M_future); }
895 // Setting the result
897 set_value(const _Res& __r)
899 auto __setter = _State::__setter(this, __r);
900 _M_future->_M_set_result(std::move(__setter));
904 set_value(_Res&& __r)
906 auto __setter = _State::__setter(this, std::move(__r));
907 _M_future->_M_set_result(std::move(__setter));
911 set_exception(exception_ptr __p)
913 auto __setter = _State::__setter(__p, this);
914 _M_future->_M_set_result(std::move(__setter));
918 template<typename _Res>
920 swap(promise<_Res>& __x, promise<_Res>& __y)
923 template<typename _Res, typename _Alloc>
924 struct uses_allocator<promise<_Res>, _Alloc>
925 : public true_type { };
928 /// Partial specialization for promise<R&>
929 template<typename _Res>
932 typedef __future_base::_State _State;
933 typedef __future_base::_Result<_Res&> _Res_type;
934 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
935 template<typename, typename> friend class _State::_Setter;
937 shared_ptr<_State> _M_future;
938 _Ptr_type _M_storage;
942 : _M_future(std::make_shared<_State>()),
943 _M_storage(new _Res_type())
946 promise(promise&& __rhs)
947 : _M_future(std::move(__rhs._M_future)),
948 _M_storage(std::move(__rhs._M_storage))
951 template<typename _Allocator>
952 promise(allocator_arg_t, const _Allocator& __a)
953 : _M_future(std::allocate_shared<_State>(__a)),
954 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
957 promise(const promise&) = delete;
961 if (static_cast<bool>(_M_future) && !_M_future.unique())
962 _M_future->_M_break_promise(std::move(_M_storage));
967 operator=(promise&& __rhs)
969 promise(std::move(__rhs)).swap(*this);
973 promise& operator=(const promise&) = delete;
978 _M_future.swap(__rhs._M_future);
979 _M_storage.swap(__rhs._M_storage);
982 // Retrieving the result
985 { return future<_Res&>(_M_future); }
987 // Setting the result
991 auto __setter = _State::__setter(this, __r);
992 _M_future->_M_set_result(std::move(__setter));
996 set_exception(exception_ptr __p)
998 auto __setter = _State::__setter(__p, this);
999 _M_future->_M_set_result(std::move(__setter));
1003 /// Explicit specialization for promise<void>
1007 typedef __future_base::_State _State;
1008 typedef __future_base::_Result<void> _Res_type;
1009 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
1010 template<typename, typename> friend class _State::_Setter;
1012 shared_ptr<_State> _M_future;
1013 _Ptr_type _M_storage;
1017 : _M_future(std::make_shared<_State>()),
1018 _M_storage(new _Res_type())
1021 promise(promise&& __rhs)
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)
1028 : _M_future(std::allocate_shared<_State>(__a)),
1029 _M_storage(__future_base::_S_allocate_result<void>(__a))
1032 promise(const promise&) = delete;
1036 if (static_cast<bool>(_M_future) && !_M_future.unique())
1037 _M_future->_M_break_promise(std::move(_M_storage));
1042 operator=(promise&& __rhs)
1044 promise(std::move(__rhs)).swap(*this);
1048 promise& operator=(const promise&) = delete;
1051 swap(promise& __rhs)
1053 _M_future.swap(__rhs._M_future);
1054 _M_storage.swap(__rhs._M_storage);
1057 // Retrieving the result
1060 { return future<void>(_M_future); }
1062 // Setting the result
1066 set_exception(exception_ptr __p)
1068 auto __setter = _State::__setter(__p, this);
1069 _M_future->_M_set_result(std::move(__setter));
1075 struct __future_base::_State::_Setter<void, void>
1077 promise<void>::_Ptr_type operator()()
1079 _State::_S_check(_M_promise->_M_future);
1080 return std::move(_M_promise->_M_storage);
1083 promise<void>* _M_promise;
1086 inline __future_base::_State::_Setter<void, void>
1087 __future_base::_State::__setter(promise<void>* __prom)
1089 return _Setter<void, void>{ __prom };
1093 promise<void>::set_value()
1095 auto __setter = _State::__setter(this);
1096 _M_future->_M_set_result(std::move(__setter));
1100 template<typename _StateT, typename _Res>
1101 struct __future_base::_Task_setter
1103 typename _StateT::_Ptr_type operator()()
1107 _M_state->_M_result->_M_set(_M_fn());
1111 _M_state->_M_result->_M_error = current_exception();
1113 return std::move(_M_state->_M_result);
1116 std::function<_Res()> _M_fn;
1119 template<typename _StateT>
1120 struct __future_base::_Task_setter<_StateT, void>
1122 typename _StateT::_Ptr_type operator()()
1130 _M_state->_M_result->_M_error = current_exception();
1132 return std::move(_M_state->_M_result);
1135 std::function<void()> _M_fn;
1138 template<typename _Res, typename... _Args>
1139 struct __future_base::_Task_state<_Res(_Args...)> : __future_base::_State
1141 typedef _Res _Res_type;
1143 _Task_state(std::function<_Res(_Args...)> __task)
1144 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1147 template<typename _Func, typename _Alloc>
1148 _Task_state(_Func&& __task, const _Alloc& __a)
1149 : _M_result(_S_allocate_result<_Res>(__a)),
1150 _M_task(allocator_arg, __a, std::move(__task))
1154 _M_run(_Args... __args)
1156 // bound arguments decay so wrap lvalue references
1157 auto __bound = std::bind<_Res>(std::ref(_M_task),
1158 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1159 _Task_setter<_Task_state> __setter{ this, std::move(__bound) };
1160 _M_set_result(std::move(__setter));
1163 template<typename, typename> friend class _Task_setter;
1164 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1165 _Ptr_type _M_result;
1166 std::function<_Res(_Args...)> _M_task;
1168 template<typename _Tp>
1169 static reference_wrapper<_Tp>
1170 _S_maybe_wrap_ref(_Tp& __t)
1171 { return std::ref(__t); }
1173 template<typename _Tp>
1174 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1176 _S_maybe_wrap_ref(_Tp&& __t)
1177 { return std::forward<_Tp>(__t); }
1181 template<typename _Res, typename... _ArgTypes>
1182 class packaged_task<_Res(_ArgTypes...)>
1184 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1185 shared_ptr<_State_type> _M_state;
1188 typedef _Res result_type;
1190 // Construction and destruction
1193 template<typename _Fn>
1195 packaged_task(const _Fn& __fn)
1196 : _M_state(std::make_shared<_State_type>(__fn))
1199 template<typename _Fn>
1201 packaged_task(_Fn&& __fn)
1202 : _M_state(std::make_shared<_State_type>(std::move(__fn)))
1206 packaged_task(_Res(*__fn)(_ArgTypes...))
1207 : _M_state(std::make_shared<_State_type>(__fn))
1210 template<typename _Fn, typename _Allocator>
1212 packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn __fn)
1213 : _M_state(std::allocate_shared<_State_type>(__a, std::move(__fn)))
1218 if (static_cast<bool>(_M_state) && !_M_state.unique())
1219 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1223 packaged_task(packaged_task&) = delete;
1224 packaged_task& operator=(packaged_task&) = delete;
1227 packaged_task(packaged_task&& __other)
1228 { this->swap(__other); }
1230 packaged_task& operator=(packaged_task&& __other)
1232 packaged_task(std::move(__other)).swap(*this);
1237 swap(packaged_task& __other)
1238 { _M_state.swap(__other._M_state); }
1240 explicit operator bool() const { return static_cast<bool>(_M_state); }
1245 { return future<_Res>(_M_state); }
1249 operator()(_ArgTypes... __args)
1251 __future_base::_State::_S_check(_M_state);
1252 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1258 __future_base::_State::_S_check(_M_state);
1259 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1264 template<typename _Res, typename... _ArgTypes>
1266 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1267 packaged_task<_Res(_ArgTypes...)>& __y)
1270 template<typename _Res, typename _Alloc>
1271 struct uses_allocator<packaged_task<_Res>, _Alloc>
1272 : public true_type { };
1275 template<typename _Res>
1276 class __future_base::_Deferred_state : public __future_base::_State
1279 typedef _Res _Res_type;
1282 _Deferred_state(std::function<_Res()>&& __fn)
1283 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1287 template<typename, typename> friend class _Task_setter;
1288 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1289 _Ptr_type _M_result;
1290 std::function<_Res()> _M_fn;
1295 _Task_setter<_Deferred_state> __setter{ this, _M_fn };
1296 // safe to call multiple times so ignore failure
1297 _M_set_result(std::move(__setter), true);
1301 template<typename _Res>
1302 class __future_base::_Async_state : public __future_base::_State
1305 typedef _Res _Res_type;
1308 _Async_state(std::function<_Res()>&& __fn)
1309 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1310 _M_thread(mem_fn(&_Async_state::_M_do_run), this)
1313 ~_Async_state() { _M_thread.join(); }
1318 _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) };
1319 _M_set_result(std::move(__setter));
1322 template<typename, typename> friend class _Task_setter;
1323 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1324 _Ptr_type _M_result;
1325 std::function<_Res()> _M_fn;
1330 template<typename _Fn, typename... _Args>
1331 future<typename result_of<_Fn(_Args...)>::type>
1332 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1334 typedef typename result_of<_Fn(_Args...)>::type result_type;
1335 std::shared_ptr<__future_base::_State> __state;
1336 if (__policy == launch::async)
1338 typedef typename __future_base::_Async_state<result_type> _State;
1339 __state = std::make_shared<_State>(std::bind<result_type>(
1340 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1344 typedef typename __future_base::_Deferred_state<result_type> _State;
1345 __state = std::make_shared<_State>(std::bind<result_type>(
1346 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1348 return future<result_type>(__state);
1351 /// async, potential overload
1352 template<typename _Fn, typename... _Args>
1354 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
1355 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
1357 async(_Fn&& __fn, _Args&&... __args)
1359 return async(launch::any, std::forward<_Fn>(__fn),
1360 std::forward<_Args>(__args)...);
1363 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1364 // && _GLIBCXX_ATOMIC_BUILTINS_4
1367 _GLIBCXX_END_NAMESPACE
1369 #endif // __GXX_EXPERIMENTAL_CXX0X__
1371 #endif // _GLIBCXX_FUTURE