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());
623 /// Partial specialization for future<R&>
624 template<typename _Res>
625 class future<_Res&> : public __basic_future<_Res&>
627 friend class promise<_Res&>;
628 template<typename> friend class packaged_task;
629 template<typename _Fn, typename... _Args>
630 friend future<typename result_of<_Fn(_Args...)>::type>
631 async(launch, _Fn&&, _Args&&...);
633 typedef __basic_future<_Res&> _Base_type;
634 typedef typename _Base_type::__state_type __state_type;
637 future(const __state_type& __state) : _Base_type(__state) { }
640 constexpr future() : _Base_type() { }
643 future(future&& __uf) : _Base_type(std::move(__uf)) { }
646 future(const future&) = delete;
647 future& operator=(const future&) = delete;
649 future& operator=(future&& __fut)
651 future(std::move(__fut))._M_swap(*this);
655 /// Retrieving the value
659 typename _Base_type::_Reset __reset(*this);
660 return this->_M_get_result()._M_get();
664 /// Explicit specialization for future<void>
666 class future<void> : public __basic_future<void>
668 friend class promise<void>;
669 template<typename> friend class packaged_task;
670 template<typename _Fn, typename... _Args>
671 friend future<typename result_of<_Fn(_Args...)>::type>
672 async(launch, _Fn&&, _Args&&...);
674 typedef __basic_future<void> _Base_type;
675 typedef typename _Base_type::__state_type __state_type;
678 future(const __state_type& __state) : _Base_type(__state) { }
681 constexpr future() : _Base_type() { }
684 future(future&& __uf) : _Base_type(std::move(__uf)) { }
687 future(const future&) = delete;
688 future& operator=(const future&) = delete;
690 future& operator=(future&& __fut)
692 future(std::move(__fut))._M_swap(*this);
696 /// Retrieving the value
700 typename _Base_type::_Reset __reset(*this);
701 this->_M_get_result();
706 /// Primary template for shared_future.
707 template<typename _Res>
708 class shared_future : public __basic_future<_Res>
710 typedef __basic_future<_Res> _Base_type;
713 constexpr shared_future() : _Base_type() { }
716 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
718 /// Construct from a future rvalue
719 shared_future(future<_Res>&& __uf)
720 : _Base_type(std::move(__uf))
723 /// Construct from a shared_future rvalue
724 shared_future(shared_future&& __sf)
725 : _Base_type(std::move(__sf))
728 shared_future& operator=(const shared_future& __sf)
730 shared_future(__sf)._M_swap(*this);
734 shared_future& operator=(shared_future&& __sf)
736 shared_future(std::move(__sf))._M_swap(*this);
740 /// Retrieving the value
744 typename _Base_type::__result_type __r = this->_M_get_result();
745 _Res& __rs(__r._M_value());
750 /// Partial specialization for shared_future<R&>
751 template<typename _Res>
752 class shared_future<_Res&> : public __basic_future<_Res&>
754 typedef __basic_future<_Res&> _Base_type;
757 constexpr shared_future() : _Base_type() { }
760 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
762 /// Construct from a future rvalue
763 shared_future(future<_Res&>&& __uf)
764 : _Base_type(std::move(__uf))
767 /// Construct from a shared_future rvalue
768 shared_future(shared_future&& __sf)
769 : _Base_type(std::move(__sf))
772 shared_future& operator=(const shared_future& __sf)
774 shared_future(__sf)._M_swap(*this);
778 shared_future& operator=(shared_future&& __sf)
780 shared_future(std::move(__sf))._M_swap(*this);
784 /// Retrieving the value
786 get() { return this->_M_get_result()._M_get(); }
789 /// Explicit specialization for shared_future<void>
791 class shared_future<void> : public __basic_future<void>
793 typedef __basic_future<void> _Base_type;
796 constexpr shared_future() : _Base_type() { }
799 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
801 /// Construct from a future rvalue
802 shared_future(future<void>&& __uf)
803 : _Base_type(std::move(__uf))
806 /// Construct from a shared_future rvalue
807 shared_future(shared_future&& __sf)
808 : _Base_type(std::move(__sf))
811 shared_future& operator=(const shared_future& __sf)
813 shared_future(__sf)._M_swap(*this);
817 shared_future& operator=(shared_future&& __sf)
819 shared_future(std::move(__sf))._M_swap(*this);
823 // Retrieving the value
825 get() { this->_M_get_result(); }
828 // Now we can define the protected __basic_future constructors.
829 template<typename _Res>
830 inline __basic_future<_Res>::
831 __basic_future(const shared_future<_Res>& __sf)
832 : _M_state(__sf._M_state)
835 template<typename _Res>
836 inline __basic_future<_Res>::
837 __basic_future(shared_future<_Res>&& __sf)
838 : _M_state(std::move(__sf._M_state))
841 template<typename _Res>
842 inline __basic_future<_Res>::
843 __basic_future(future<_Res>&& __uf)
844 : _M_state(std::move(__uf._M_state))
848 /// Primary template for promise
849 template<typename _Res>
852 typedef __future_base::_State_base _State;
853 typedef __future_base::_Result<_Res> _Res_type;
854 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
855 template<typename, typename> friend class _State::_Setter;
857 shared_ptr<_State> _M_future;
858 _Ptr_type _M_storage;
862 : _M_future(std::make_shared<_State>()),
863 _M_storage(new _Res_type())
866 promise(promise&& __rhs)
867 : _M_future(std::move(__rhs._M_future)),
868 _M_storage(std::move(__rhs._M_storage))
871 template<typename _Allocator>
872 promise(allocator_arg_t, const _Allocator& __a)
873 : _M_future(std::allocate_shared<_State>(__a)),
874 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
877 promise(const promise&) = delete;
881 if (static_cast<bool>(_M_future) && !_M_future.unique())
882 _M_future->_M_break_promise(std::move(_M_storage));
887 operator=(promise&& __rhs)
889 promise(std::move(__rhs)).swap(*this);
893 promise& operator=(const promise&) = delete;
898 _M_future.swap(__rhs._M_future);
899 _M_storage.swap(__rhs._M_storage);
902 // Retrieving the result
905 { return future<_Res>(_M_future); }
907 // Setting the result
909 set_value(const _Res& __r)
911 auto __setter = _State::__setter(this, __r);
912 _M_future->_M_set_result(std::move(__setter));
916 set_value(_Res&& __r)
918 auto __setter = _State::__setter(this, std::move(__r));
919 _M_future->_M_set_result(std::move(__setter));
923 set_exception(exception_ptr __p)
925 auto __setter = _State::__setter(__p, this);
926 _M_future->_M_set_result(std::move(__setter));
930 template<typename _Res>
932 swap(promise<_Res>& __x, promise<_Res>& __y)
935 template<typename _Res, typename _Alloc>
936 struct uses_allocator<promise<_Res>, _Alloc>
937 : public true_type { };
940 /// Partial specialization for promise<R&>
941 template<typename _Res>
944 typedef __future_base::_State_base _State;
945 typedef __future_base::_Result<_Res&> _Res_type;
946 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
947 template<typename, typename> friend class _State::_Setter;
949 shared_ptr<_State> _M_future;
950 _Ptr_type _M_storage;
954 : _M_future(std::make_shared<_State>()),
955 _M_storage(new _Res_type())
958 promise(promise&& __rhs)
959 : _M_future(std::move(__rhs._M_future)),
960 _M_storage(std::move(__rhs._M_storage))
963 template<typename _Allocator>
964 promise(allocator_arg_t, const _Allocator& __a)
965 : _M_future(std::allocate_shared<_State>(__a)),
966 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
969 promise(const promise&) = delete;
973 if (static_cast<bool>(_M_future) && !_M_future.unique())
974 _M_future->_M_break_promise(std::move(_M_storage));
979 operator=(promise&& __rhs)
981 promise(std::move(__rhs)).swap(*this);
985 promise& operator=(const promise&) = delete;
990 _M_future.swap(__rhs._M_future);
991 _M_storage.swap(__rhs._M_storage);
994 // Retrieving the result
997 { return future<_Res&>(_M_future); }
999 // Setting the result
1001 set_value(_Res& __r)
1003 auto __setter = _State::__setter(this, __r);
1004 _M_future->_M_set_result(std::move(__setter));
1008 set_exception(exception_ptr __p)
1010 auto __setter = _State::__setter(__p, this);
1011 _M_future->_M_set_result(std::move(__setter));
1015 /// Explicit specialization for promise<void>
1019 typedef __future_base::_State_base _State;
1020 typedef __future_base::_Result<void> _Res_type;
1021 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
1022 template<typename, typename> friend class _State::_Setter;
1024 shared_ptr<_State> _M_future;
1025 _Ptr_type _M_storage;
1029 : _M_future(std::make_shared<_State>()),
1030 _M_storage(new _Res_type())
1033 promise(promise&& __rhs)
1034 : _M_future(std::move(__rhs._M_future)),
1035 _M_storage(std::move(__rhs._M_storage))
1038 template<typename _Allocator>
1039 promise(allocator_arg_t, const _Allocator& __a)
1040 : _M_future(std::allocate_shared<_State>(__a)),
1041 _M_storage(__future_base::_S_allocate_result<void>(__a))
1044 promise(const promise&) = delete;
1048 if (static_cast<bool>(_M_future) && !_M_future.unique())
1049 _M_future->_M_break_promise(std::move(_M_storage));
1054 operator=(promise&& __rhs)
1056 promise(std::move(__rhs)).swap(*this);
1060 promise& operator=(const promise&) = delete;
1063 swap(promise& __rhs)
1065 _M_future.swap(__rhs._M_future);
1066 _M_storage.swap(__rhs._M_storage);
1069 // Retrieving the result
1072 { return future<void>(_M_future); }
1074 // Setting the result
1078 set_exception(exception_ptr __p)
1080 auto __setter = _State::__setter(__p, this);
1081 _M_future->_M_set_result(std::move(__setter));
1087 struct __future_base::_State_base::_Setter<void, void>
1089 promise<void>::_Ptr_type operator()()
1091 _State_base::_S_check(_M_promise->_M_future);
1092 return std::move(_M_promise->_M_storage);
1095 promise<void>* _M_promise;
1098 inline __future_base::_State_base::_Setter<void, void>
1099 __future_base::_State_base::__setter(promise<void>* __prom)
1101 return _Setter<void, void>{ __prom };
1105 promise<void>::set_value()
1107 auto __setter = _State::__setter(this);
1108 _M_future->_M_set_result(std::move(__setter));
1112 template<typename _StateT, typename _Res>
1113 struct __future_base::_Task_setter
1115 typename _StateT::_Ptr_type operator()()
1119 _M_state->_M_result->_M_set(_M_fn());
1123 _M_state->_M_result->_M_error = current_exception();
1125 return std::move(_M_state->_M_result);
1128 std::function<_Res()> _M_fn;
1131 template<typename _StateT>
1132 struct __future_base::_Task_setter<_StateT, void>
1134 typename _StateT::_Ptr_type operator()()
1142 _M_state->_M_result->_M_error = current_exception();
1144 return std::move(_M_state->_M_result);
1147 std::function<void()> _M_fn;
1150 template<typename _Res, typename... _Args>
1151 struct __future_base::_Task_state<_Res(_Args...)>
1152 : __future_base::_State_base
1154 typedef _Res _Res_type;
1156 _Task_state(std::function<_Res(_Args...)> __task)
1157 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1160 template<typename _Func, typename _Alloc>
1161 _Task_state(_Func&& __task, const _Alloc& __a)
1162 : _M_result(_S_allocate_result<_Res>(__a)),
1163 _M_task(allocator_arg, __a, std::move(__task))
1167 _M_run(_Args... __args)
1169 // bound arguments decay so wrap lvalue references
1170 auto __bound = std::bind<_Res>(std::ref(_M_task),
1171 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1172 _Task_setter<_Task_state> __setter{ this, std::move(__bound) };
1173 _M_set_result(std::move(__setter));
1176 template<typename, typename> friend class _Task_setter;
1177 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1178 _Ptr_type _M_result;
1179 std::function<_Res(_Args...)> _M_task;
1181 template<typename _Tp>
1182 static reference_wrapper<_Tp>
1183 _S_maybe_wrap_ref(_Tp& __t)
1184 { return std::ref(__t); }
1186 template<typename _Tp>
1187 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1189 _S_maybe_wrap_ref(_Tp&& __t)
1190 { return std::forward<_Tp>(__t); }
1194 template<typename _Res, typename... _ArgTypes>
1195 class packaged_task<_Res(_ArgTypes...)>
1197 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1198 shared_ptr<_State_type> _M_state;
1201 typedef _Res result_type;
1203 // Construction and destruction
1206 template<typename _Fn>
1208 packaged_task(const _Fn& __fn)
1209 : _M_state(std::make_shared<_State_type>(__fn))
1212 template<typename _Fn>
1214 packaged_task(_Fn&& __fn)
1215 : _M_state(std::make_shared<_State_type>(std::move(__fn)))
1219 packaged_task(_Res(*__fn)(_ArgTypes...))
1220 : _M_state(std::make_shared<_State_type>(__fn))
1223 template<typename _Fn, typename _Allocator>
1225 packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn __fn)
1226 : _M_state(std::allocate_shared<_State_type>(__a, std::move(__fn)))
1231 if (static_cast<bool>(_M_state) && !_M_state.unique())
1232 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1236 packaged_task(packaged_task&) = delete;
1237 packaged_task& operator=(packaged_task&) = delete;
1240 packaged_task(packaged_task&& __other)
1241 { this->swap(__other); }
1243 packaged_task& operator=(packaged_task&& __other)
1245 packaged_task(std::move(__other)).swap(*this);
1250 swap(packaged_task& __other)
1251 { _M_state.swap(__other._M_state); }
1255 { return static_cast<bool>(_M_state); }
1260 { return future<_Res>(_M_state); }
1264 operator()(_ArgTypes... __args)
1266 __future_base::_State_base::_S_check(_M_state);
1267 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1273 __future_base::_State_base::_S_check(_M_state);
1274 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1279 template<typename _Res, typename... _ArgTypes>
1281 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1282 packaged_task<_Res(_ArgTypes...)>& __y)
1285 template<typename _Res, typename _Alloc>
1286 struct uses_allocator<packaged_task<_Res>, _Alloc>
1287 : public true_type { };
1290 template<typename _Res>
1291 class __future_base::_Deferred_state : public __future_base::_State_base
1294 typedef _Res _Res_type;
1297 _Deferred_state(std::function<_Res()>&& __fn)
1298 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1302 template<typename, typename> friend class _Task_setter;
1303 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1304 _Ptr_type _M_result;
1305 std::function<_Res()> _M_fn;
1310 _Task_setter<_Deferred_state> __setter{ this, _M_fn };
1311 // safe to call multiple times so ignore failure
1312 _M_set_result(std::move(__setter), true);
1316 template<typename _Res>
1317 class __future_base::_Async_state : public __future_base::_State_base
1320 typedef _Res _Res_type;
1323 _Async_state(std::function<_Res()>&& __fn)
1324 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1325 _M_thread(mem_fn(&_Async_state::_M_do_run), this)
1328 ~_Async_state() { _M_thread.join(); }
1333 _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) };
1334 _M_set_result(std::move(__setter));
1337 template<typename, typename> friend class _Task_setter;
1338 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1339 _Ptr_type _M_result;
1340 std::function<_Res()> _M_fn;
1345 template<typename _Fn, typename... _Args>
1346 future<typename result_of<_Fn(_Args...)>::type>
1347 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1349 typedef typename result_of<_Fn(_Args...)>::type result_type;
1350 std::shared_ptr<__future_base::_State_base> __state;
1351 if (__policy == launch::async)
1353 typedef typename __future_base::_Async_state<result_type> _State;
1354 __state = std::make_shared<_State>(std::bind<result_type>(
1355 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1359 typedef typename __future_base::_Deferred_state<result_type> _State;
1360 __state = std::make_shared<_State>(std::bind<result_type>(
1361 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1363 return future<result_type>(__state);
1366 /// async, potential overload
1367 template<typename _Fn, typename... _Args>
1369 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
1370 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
1372 async(_Fn&& __fn, _Args&&... __args)
1374 return async(launch::any, std::forward<_Fn>(__fn),
1375 std::forward<_Args>(__args)...);
1378 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1379 // && _GLIBCXX_ATOMIC_BUILTINS_4
1382 _GLIBCXX_END_NAMESPACE_VERSION
1385 #endif // __GXX_EXPERIMENTAL_CXX0X__
1387 #endif // _GLIBCXX_FUTURE