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() = default;
164 _Result_base(const _Result_base&) = delete;
165 _Result_base& operator=(const _Result_base&) = delete;
167 // _M_destroy() allows derived classes to control deallocation
168 virtual void _M_destroy() = 0;
172 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
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 /// Shared state between a promise and one or more associated futures.
279 typedef _Ptr<_Result_base>::type _Ptr_type;
283 condition_variable _M_cond;
284 atomic_flag _M_retrieved;
288 _State() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
290 _State(const _State&) = delete;
291 _State& operator=(const _State&) = delete;
297 unique_lock<mutex> __lock(_M_mutex);
299 _M_cond.wait(__lock, std::bind<bool>(&_State::_M_ready, this));
303 template<typename _Rep, typename _Period>
305 wait_for(const chrono::duration<_Rep, _Period>& __rel)
307 unique_lock<mutex> __lock(_M_mutex);
308 auto __bound = std::bind<bool>(&_State::_M_ready, this);
309 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
312 template<typename _Clock, typename _Duration>
314 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
316 unique_lock<mutex> __lock(_M_mutex);
317 auto __bound = std::bind<bool>(&_State::_M_ready, this);
318 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
322 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
324 bool __set = __ignore_failure;
325 // all calls to this function are serialized,
326 // side-effects of invoking __res only happen once
327 call_once(_M_once, &_State::_M_do_set, this, ref(__res),
330 __throw_future_error(int(future_errc::promise_already_satisfied));
334 _M_break_promise(_Ptr_type __res)
336 if (static_cast<bool>(__res))
338 error_code __ec(make_error_code(future_errc::broken_promise));
339 __res->_M_error = copy_exception(future_error(__ec));
341 lock_guard<mutex> __lock(_M_mutex);
342 _M_result.swap(__res);
344 _M_cond.notify_all();
348 // Called when this object is passed to a future.
350 _M_set_retrieved_flag()
352 if (_M_retrieved.test_and_set())
353 __throw_future_error(int(future_errc::future_already_retrieved));
356 template<typename _Res, typename _Arg>
360 template<typename _Res, typename _Arg>
361 struct _Setter<_Res, _Arg&>
363 // check this is only used by promise<R>::set_value(const R&)
364 // or promise<R>::set_value(R&)
365 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
366 || is_same<const _Res, _Arg>::value, // promise<R>
367 "Invalid specialisation");
369 typename promise<_Res>::_Ptr_type operator()()
371 _State::_S_check(_M_promise->_M_future);
372 _M_promise->_M_storage->_M_set(_M_arg);
373 return std::move(_M_promise->_M_storage);
375 promise<_Res>* _M_promise;
380 template<typename _Res>
381 struct _Setter<_Res, _Res&&>
383 typename promise<_Res>::_Ptr_type operator()()
385 _State::_S_check(_M_promise->_M_future);
386 _M_promise->_M_storage->_M_set(std::move(_M_arg));
387 return std::move(_M_promise->_M_storage);
389 promise<_Res>* _M_promise;
393 struct __exception_ptr_tag { };
396 template<typename _Res>
397 struct _Setter<_Res, __exception_ptr_tag>
399 typename promise<_Res>::_Ptr_type operator()()
401 _State::_S_check(_M_promise->_M_future);
402 _M_promise->_M_storage->_M_error = _M_ex;
403 return std::move(_M_promise->_M_storage);
406 promise<_Res>* _M_promise;
407 exception_ptr& _M_ex;
410 template<typename _Res, typename _Arg>
411 static _Setter<_Res, _Arg&&>
412 __setter(promise<_Res>* __prom, _Arg&& __arg)
414 return _Setter<_Res, _Arg&&>{ __prom, __arg };
417 template<typename _Res>
418 static _Setter<_Res, __exception_ptr_tag>
419 __setter(exception_ptr& __ex, promise<_Res>* __prom)
421 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
424 static _Setter<void, void>
425 __setter(promise<void>* __prom);
427 template<typename _Tp>
429 _S_check(const shared_ptr<_Tp>& __p)
431 if (!static_cast<bool>(__p))
432 __throw_future_error((int)future_errc::no_state);
437 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
439 _Ptr_type __res = __f();
441 lock_guard<mutex> __lock(_M_mutex);
442 _M_result.swap(__res);
444 _M_cond.notify_all();
448 bool _M_ready() const { return static_cast<bool>(_M_result); }
450 virtual void _M_run_deferred() { }
453 template<typename _Res>
454 class _Deferred_state;
456 template<typename _Res>
459 template<typename _Signature>
462 template<typename _StateT, typename _Res = typename _StateT::_Res_type>
466 inline __future_base::_Result_base::~_Result_base() = default;
468 /// Partial specialization for reference types.
469 template<typename _Res>
470 struct __future_base::_Result<_Res&> : __future_base::_Result_base
472 _Result() : _M_value_ptr() { }
474 void _M_set(_Res& __res) { _M_value_ptr = &__res; }
476 _Res& _M_get() { return *_M_value_ptr; }
481 void _M_destroy() { delete this; }
484 /// Explicit specialization for void.
486 struct __future_base::_Result<void> : __future_base::_Result_base
489 void _M_destroy() { delete this; }
493 /// Common implementation for future and shared_future.
494 template<typename _Res>
495 class __basic_future : public __future_base
498 typedef shared_ptr<_State> __state_type;
499 typedef __future_base::_Result<_Res>& __result_type;
502 __state_type _M_state;
506 __basic_future(const __basic_future&) = delete;
507 __basic_future& operator=(const __basic_future&) = delete;
510 valid() const { return static_cast<bool>(_M_state); }
515 _State::_S_check(_M_state);
519 template<typename _Rep, typename _Period>
521 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
523 _State::_S_check(_M_state);
524 return _M_state->wait_for(__rel);
527 template<typename _Clock, typename _Duration>
529 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
531 _State::_S_check(_M_state);
532 return _M_state->wait_until(__abs);
536 /// Wait for the state to be ready and rethrow any stored exception
540 _State::_S_check(_M_state);
541 _Result_base& __res = _M_state->wait();
542 if (!(__res._M_error == 0))
543 rethrow_exception(__res._M_error);
544 return static_cast<__result_type>(__res);
547 void _M_swap(__basic_future& __that)
549 _M_state.swap(__that._M_state);
552 // Construction of a future by promise::get_future()
554 __basic_future(const __state_type& __state) : _M_state(__state)
556 _State::_S_check(_M_state);
557 _M_state->_M_set_retrieved_flag();
560 // Copy construction from a shared_future
562 __basic_future(const shared_future<_Res>&);
564 // Move construction from a shared_future
566 __basic_future(shared_future<_Res>&&);
568 // Move construction from a future
570 __basic_future(future<_Res>&&);
572 constexpr __basic_future() : _M_state() { }
576 explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { }
577 ~_Reset() { _M_fut._M_state.reset(); }
578 __basic_future& _M_fut;
583 /// Primary template for future.
584 template<typename _Res>
585 class future : public __basic_future<_Res>
587 friend class promise<_Res>;
588 template<typename> friend class packaged_task;
589 template<typename _Fn, typename... _Args>
590 friend future<typename result_of<_Fn(_Args...)>::type>
591 async(launch, _Fn&&, _Args&&...);
593 typedef __basic_future<_Res> _Base_type;
594 typedef typename _Base_type::__state_type __state_type;
597 future(const __state_type& __state) : _Base_type(__state) { }
600 constexpr future() : _Base_type() { }
603 future(future&& __uf) : _Base_type(std::move(__uf)) { }
606 future(const future&) = delete;
607 future& operator=(const future&) = delete;
609 future& operator=(future&& __fut)
611 future(std::move(__fut))._M_swap(*this);
615 /// Retrieving the value
619 typename _Base_type::_Reset __reset(*this);
620 return std::move(this->_M_get_result()._M_value());
624 /// Partial specialization for future<R&>
625 template<typename _Res>
626 class future<_Res&> : public __basic_future<_Res&>
628 friend class promise<_Res&>;
629 template<typename> friend class packaged_task;
630 template<typename _Fn, typename... _Args>
631 friend future<typename result_of<_Fn(_Args...)>::type>
632 async(launch, _Fn&&, _Args&&...);
634 typedef __basic_future<_Res&> _Base_type;
635 typedef typename _Base_type::__state_type __state_type;
638 future(const __state_type& __state) : _Base_type(__state) { }
641 constexpr future() : _Base_type() { }
644 future(future&& __uf) : _Base_type(std::move(__uf)) { }
647 future(const future&) = delete;
648 future& operator=(const future&) = delete;
650 future& operator=(future&& __fut)
652 future(std::move(__fut))._M_swap(*this);
656 /// Retrieving the value
660 typename _Base_type::_Reset __reset(*this);
661 return this->_M_get_result()._M_get();
665 /// Explicit specialization for future<void>
667 class future<void> : public __basic_future<void>
669 friend class promise<void>;
670 template<typename> friend class packaged_task;
671 template<typename _Fn, typename... _Args>
672 friend future<typename result_of<_Fn(_Args...)>::type>
673 async(launch, _Fn&&, _Args&&...);
675 typedef __basic_future<void> _Base_type;
676 typedef typename _Base_type::__state_type __state_type;
679 future(const __state_type& __state) : _Base_type(__state) { }
682 constexpr future() : _Base_type() { }
685 future(future&& __uf) : _Base_type(std::move(__uf)) { }
688 future(const future&) = delete;
689 future& operator=(const future&) = delete;
691 future& operator=(future&& __fut)
693 future(std::move(__fut))._M_swap(*this);
697 /// Retrieving the value
701 typename _Base_type::_Reset __reset(*this);
702 this->_M_get_result();
707 /// Primary template for shared_future.
708 template<typename _Res>
709 class shared_future : public __basic_future<_Res>
711 typedef __basic_future<_Res> _Base_type;
714 constexpr shared_future() : _Base_type() { }
717 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
719 /// Construct from a future rvalue
720 shared_future(future<_Res>&& __uf)
721 : _Base_type(std::move(__uf))
724 /// Construct from a shared_future rvalue
725 shared_future(shared_future&& __sf)
726 : _Base_type(std::move(__sf))
729 shared_future& operator=(const shared_future& __sf)
731 shared_future(__sf)._M_swap(*this);
735 shared_future& operator=(shared_future&& __sf)
737 shared_future(std::move(__sf))._M_swap(*this);
741 /// Retrieving the value
745 typename _Base_type::__result_type __r = this->_M_get_result();
746 _Res& __rs(__r._M_value());
751 /// Partial specialization for shared_future<R&>
752 template<typename _Res>
753 class shared_future<_Res&> : public __basic_future<_Res&>
755 typedef __basic_future<_Res&> _Base_type;
758 constexpr shared_future() : _Base_type() { }
761 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
763 /// Construct from a future rvalue
764 shared_future(future<_Res&>&& __uf)
765 : _Base_type(std::move(__uf))
768 /// Construct from a shared_future rvalue
769 shared_future(shared_future&& __sf)
770 : _Base_type(std::move(__sf))
773 shared_future& operator=(const shared_future& __sf)
775 shared_future(__sf)._M_swap(*this);
779 shared_future& operator=(shared_future&& __sf)
781 shared_future(std::move(__sf))._M_swap(*this);
785 /// Retrieving the value
787 get() { return this->_M_get_result()._M_get(); }
790 /// Explicit specialization for shared_future<void>
792 class shared_future<void> : public __basic_future<void>
794 typedef __basic_future<void> _Base_type;
797 constexpr shared_future() : _Base_type() { }
800 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
802 /// Construct from a future rvalue
803 shared_future(future<void>&& __uf)
804 : _Base_type(std::move(__uf))
807 /// Construct from a shared_future rvalue
808 shared_future(shared_future&& __sf)
809 : _Base_type(std::move(__sf))
812 shared_future& operator=(const shared_future& __sf)
814 shared_future(__sf)._M_swap(*this);
818 shared_future& operator=(shared_future&& __sf)
820 shared_future(std::move(__sf))._M_swap(*this);
824 // Retrieving the value
826 get() { this->_M_get_result(); }
829 // Now we can define the protected __basic_future constructors.
830 template<typename _Res>
831 inline __basic_future<_Res>::
832 __basic_future(const shared_future<_Res>& __sf)
833 : _M_state(__sf._M_state)
836 template<typename _Res>
837 inline __basic_future<_Res>::
838 __basic_future(shared_future<_Res>&& __sf)
839 : _M_state(std::move(__sf._M_state))
842 template<typename _Res>
843 inline __basic_future<_Res>::
844 __basic_future(future<_Res>&& __uf)
845 : _M_state(std::move(__uf._M_state))
849 /// Primary template for promise
850 template<typename _Res>
853 typedef __future_base::_State _State;
854 typedef __future_base::_Result<_Res> _Res_type;
855 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
856 template<typename, typename> friend class _State::_Setter;
858 shared_ptr<_State> _M_future;
859 _Ptr_type _M_storage;
863 : _M_future(std::make_shared<_State>()),
864 _M_storage(new _Res_type())
867 promise(promise&& __rhs)
868 : _M_future(std::move(__rhs._M_future)),
869 _M_storage(std::move(__rhs._M_storage))
872 template<typename _Allocator>
873 promise(allocator_arg_t, const _Allocator& __a)
874 : _M_future(std::allocate_shared<_State>(__a)),
875 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
878 promise(const promise&) = delete;
882 if (static_cast<bool>(_M_future) && !_M_future.unique())
883 _M_future->_M_break_promise(std::move(_M_storage));
888 operator=(promise&& __rhs)
890 promise(std::move(__rhs)).swap(*this);
894 promise& operator=(const promise&) = delete;
899 _M_future.swap(__rhs._M_future);
900 _M_storage.swap(__rhs._M_storage);
903 // Retrieving the result
906 { return future<_Res>(_M_future); }
908 // Setting the result
910 set_value(const _Res& __r)
912 auto __setter = _State::__setter(this, __r);
913 _M_future->_M_set_result(std::move(__setter));
917 set_value(_Res&& __r)
919 auto __setter = _State::__setter(this, std::move(__r));
920 _M_future->_M_set_result(std::move(__setter));
924 set_exception(exception_ptr __p)
926 auto __setter = _State::__setter(__p, this);
927 _M_future->_M_set_result(std::move(__setter));
931 template<typename _Res>
933 swap(promise<_Res>& __x, promise<_Res>& __y)
936 template<typename _Res, typename _Alloc>
937 struct uses_allocator<promise<_Res>, _Alloc>
938 : public true_type { };
941 /// Partial specialization for promise<R&>
942 template<typename _Res>
945 typedef __future_base::_State _State;
946 typedef __future_base::_Result<_Res&> _Res_type;
947 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
948 template<typename, typename> friend class _State::_Setter;
950 shared_ptr<_State> _M_future;
951 _Ptr_type _M_storage;
955 : _M_future(std::make_shared<_State>()),
956 _M_storage(new _Res_type())
959 promise(promise&& __rhs)
960 : _M_future(std::move(__rhs._M_future)),
961 _M_storage(std::move(__rhs._M_storage))
964 template<typename _Allocator>
965 promise(allocator_arg_t, const _Allocator& __a)
966 : _M_future(std::allocate_shared<_State>(__a)),
967 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
970 promise(const promise&) = delete;
974 if (static_cast<bool>(_M_future) && !_M_future.unique())
975 _M_future->_M_break_promise(std::move(_M_storage));
980 operator=(promise&& __rhs)
982 promise(std::move(__rhs)).swap(*this);
986 promise& operator=(const promise&) = delete;
991 _M_future.swap(__rhs._M_future);
992 _M_storage.swap(__rhs._M_storage);
995 // Retrieving the result
998 { return future<_Res&>(_M_future); }
1000 // Setting the result
1002 set_value(_Res& __r)
1004 auto __setter = _State::__setter(this, __r);
1005 _M_future->_M_set_result(std::move(__setter));
1009 set_exception(exception_ptr __p)
1011 auto __setter = _State::__setter(__p, this);
1012 _M_future->_M_set_result(std::move(__setter));
1016 /// Explicit specialization for promise<void>
1020 typedef __future_base::_State _State;
1021 typedef __future_base::_Result<void> _Res_type;
1022 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
1023 template<typename, typename> friend class _State::_Setter;
1025 shared_ptr<_State> _M_future;
1026 _Ptr_type _M_storage;
1030 : _M_future(std::make_shared<_State>()),
1031 _M_storage(new _Res_type())
1034 promise(promise&& __rhs)
1035 : _M_future(std::move(__rhs._M_future)),
1036 _M_storage(std::move(__rhs._M_storage))
1039 template<typename _Allocator>
1040 promise(allocator_arg_t, const _Allocator& __a)
1041 : _M_future(std::allocate_shared<_State>(__a)),
1042 _M_storage(__future_base::_S_allocate_result<void>(__a))
1045 promise(const promise&) = delete;
1049 if (static_cast<bool>(_M_future) && !_M_future.unique())
1050 _M_future->_M_break_promise(std::move(_M_storage));
1055 operator=(promise&& __rhs)
1057 promise(std::move(__rhs)).swap(*this);
1061 promise& operator=(const promise&) = delete;
1064 swap(promise& __rhs)
1066 _M_future.swap(__rhs._M_future);
1067 _M_storage.swap(__rhs._M_storage);
1070 // Retrieving the result
1073 { return future<void>(_M_future); }
1075 // Setting the result
1079 set_exception(exception_ptr __p)
1081 auto __setter = _State::__setter(__p, this);
1082 _M_future->_M_set_result(std::move(__setter));
1088 struct __future_base::_State::_Setter<void, void>
1090 promise<void>::_Ptr_type operator()()
1092 _State::_S_check(_M_promise->_M_future);
1093 return std::move(_M_promise->_M_storage);
1096 promise<void>* _M_promise;
1099 inline __future_base::_State::_Setter<void, void>
1100 __future_base::_State::__setter(promise<void>* __prom)
1102 return _Setter<void, void>{ __prom };
1106 promise<void>::set_value()
1108 auto __setter = _State::__setter(this);
1109 _M_future->_M_set_result(std::move(__setter));
1113 template<typename _StateT, typename _Res>
1114 struct __future_base::_Task_setter
1116 typename _StateT::_Ptr_type operator()()
1120 _M_state->_M_result->_M_set(_M_fn());
1124 _M_state->_M_result->_M_error = current_exception();
1126 return std::move(_M_state->_M_result);
1129 std::function<_Res()> _M_fn;
1132 template<typename _StateT>
1133 struct __future_base::_Task_setter<_StateT, void>
1135 typename _StateT::_Ptr_type operator()()
1143 _M_state->_M_result->_M_error = current_exception();
1145 return std::move(_M_state->_M_result);
1148 std::function<void()> _M_fn;
1151 template<typename _Res, typename... _Args>
1152 struct __future_base::_Task_state<_Res(_Args...)> : __future_base::_State
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::_S_check(_M_state);
1267 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1273 __future_base::_State::_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
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
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> __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