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>
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,
68 struct is_error_code_enum<future_errc> : public true_type { };
70 /// Points to a statically-allocated object derived from error_category.
71 extern const error_category* const future_category;
73 // TODO: requires constexpr
74 inline error_code make_error_code(future_errc __errc)
75 { return error_code(static_cast<int>(__errc), *future_category); }
77 // TODO: requires constexpr
78 inline error_condition make_error_condition(future_errc __errc)
79 { return error_condition(static_cast<int>(__errc), *future_category); }
82 * @brief Exception type thrown by futures.
85 class future_error : public logic_error
90 explicit future_error(error_code __ec)
91 : logic_error("std::future_error"), _M_code(__ec)
94 virtual ~future_error() throw();
100 code() const throw() { return _M_code; }
103 // Forward declarations.
104 template<typename _Res>
107 template<typename _Res>
110 template<typename _Res>
113 template<typename _Signature>
116 template<typename _Res>
119 enum class launch { any, async, sync };
121 template<typename _Fn, typename... _Args>
122 future<typename result_of<_Fn(_Args...)>::type>
123 async(launch __policy, _Fn&& __fn, _Args&&... __args);
125 template<typename _Fn, typename... _Args>
127 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
128 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
130 async(_Fn&& __fn, _Args&&... __args);
132 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
133 && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
135 /// Base class and enclosing scope.
138 /// Base class for results.
141 exception_ptr _M_error;
143 _Result_base() = default;
144 _Result_base(const _Result_base&) = delete;
145 _Result_base& operator=(const _Result_base&) = delete;
147 // _M_destroy() allows derived classes to control deallocation
148 virtual void _M_destroy() = 0;
152 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
160 template<typename _Res>
161 struct _Result : _Result_base
164 typedef alignment_of<_Res> __a_of;
165 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
166 typedef typename __align_storage::type __align_type;
168 __align_type _M_storage;
172 _Result() : _M_initialized() { }
180 // Return lvalue, future will add const or rvalue-reference
182 _M_value() { return *static_cast<_Res*>(_M_addr()); }
185 _M_set(const _Res& __res)
187 ::new (_M_addr()) _Res(__res);
188 _M_initialized = true;
194 ::new (_M_addr()) _Res(std::move(__res));
195 _M_initialized = true;
199 void _M_destroy() { delete this; }
201 void* _M_addr() { return static_cast<void*>(&_M_storage); }
204 // TODO: use template alias when available
206 template<typename _Res>
207 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
209 /// A unique_ptr based on the instantiating type.
210 template<typename _Res>
213 typedef unique_ptr<_Res, _Result_base::_Deleter> type;
217 template<typename _Res, typename _Alloc>
218 struct _Result_alloc : _Result<_Res>
220 typedef typename _Alloc::template rebind<_Result_alloc>::other
224 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _M_alloc(__a)
230 __allocator_type __a(_M_alloc);
232 __a.deallocate(this, 1);
235 __allocator_type _M_alloc;
238 template<typename _Res, typename _Allocator>
239 static typename _Ptr<_Result_alloc<_Res, _Allocator>>::type
240 _S_allocate_result(const _Allocator& __a)
242 typedef _Result_alloc<_Res, _Allocator> __result_type;
243 typename __result_type::__allocator_type __a2(__a);
244 __result_type* __p = __a2.allocate(1);
247 __a2.construct(__p, __a);
251 __a2.deallocate(__p, 1);
252 __throw_exception_again;
254 return typename _Ptr<__result_type>::type(__p);
258 /// Shared state between a promise and one or more associated futures.
261 typedef _Ptr<_Result_base>::type _Ptr_type;
265 condition_variable _M_cond;
266 atomic_flag _M_retrieved;
270 _State() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
272 _State(const _State&) = delete;
273 _State& operator=(const _State&) = delete;
279 unique_lock<mutex> __lock(_M_mutex);
281 _M_cond.wait(__lock, std::bind<bool>(&_State::_M_ready, this));
285 template<typename _Rep, typename _Period>
287 wait_for(const chrono::duration<_Rep, _Period>& __rel)
289 unique_lock<mutex> __lock(_M_mutex);
290 auto __bound = std::bind<bool>(&_State::_M_ready, this);
291 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
294 template<typename _Clock, typename _Duration>
296 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
298 unique_lock<mutex> __lock(_M_mutex);
299 auto __bound = std::bind<bool>(&_State::_M_ready, this);
300 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
304 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
306 bool __set = __ignore_failure;
307 // all calls to this function are serialized,
308 // side-effects of invoking __res only happen once
309 call_once(_M_once, &_State::_M_do_set, this, ref(__res),
312 __throw_future_error(int(future_errc::promise_already_satisfied));
316 _M_break_promise(_Ptr_type __res)
318 if (static_cast<bool>(__res))
320 error_code __ec(make_error_code(future_errc::broken_promise));
321 __res->_M_error = copy_exception(future_error(__ec));
323 lock_guard<mutex> __lock(_M_mutex);
324 _M_result.swap(__res);
326 _M_cond.notify_all();
330 // Called when this object is passed to a future.
332 _M_set_retrieved_flag()
334 if (_M_retrieved.test_and_set())
335 __throw_future_error(int(future_errc::future_already_retrieved));
338 template<typename _Res, typename _Arg>
342 template<typename _Res, typename _Arg>
343 struct _Setter<_Res, _Arg&>
345 // check this is only used by promise<R>::set_value(const R&)
346 // or promise<R>::set_value(R&)
347 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
348 || is_same<const _Res, _Arg>::value, // promise<R>
349 "Invalid specialisation");
351 typename promise<_Res>::_Ptr_type operator()()
353 _State::_S_check(_M_promise->_M_future);
354 _M_promise->_M_storage->_M_set(_M_arg);
355 return std::move(_M_promise->_M_storage);
357 promise<_Res>* _M_promise;
362 template<typename _Res>
363 struct _Setter<_Res, _Res&&>
365 typename promise<_Res>::_Ptr_type operator()()
367 _State::_S_check(_M_promise->_M_future);
368 _M_promise->_M_storage->_M_set(std::move(_M_arg));
369 return std::move(_M_promise->_M_storage);
371 promise<_Res>* _M_promise;
375 struct __exception_ptr_tag { };
378 template<typename _Res>
379 struct _Setter<_Res, __exception_ptr_tag>
381 typename promise<_Res>::_Ptr_type operator()()
383 _State::_S_check(_M_promise->_M_future);
384 _M_promise->_M_storage->_M_error = _M_ex;
385 return std::move(_M_promise->_M_storage);
388 promise<_Res>* _M_promise;
389 exception_ptr& _M_ex;
392 template<typename _Res, typename _Arg>
393 static _Setter<_Res, _Arg&&>
394 __setter(promise<_Res>* __prom, _Arg&& __arg)
396 return _Setter<_Res, _Arg&&>{ __prom, __arg };
399 template<typename _Res>
400 static _Setter<_Res, __exception_ptr_tag>
401 __setter(exception_ptr& __ex, promise<_Res>* __prom)
403 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
406 static _Setter<void, void>
407 __setter(promise<void>* __prom);
409 template<typename _Tp>
411 _S_check(const shared_ptr<_Tp>& __p)
413 if (!static_cast<bool>(__p))
414 __throw_future_error((int)future_errc::no_state);
419 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
421 _Ptr_type __res = __f();
423 lock_guard<mutex> __lock(_M_mutex);
424 _M_result.swap(__res);
426 _M_cond.notify_all();
430 bool _M_ready() const { return static_cast<bool>(_M_result); }
432 virtual void _M_run_deferred() { }
435 template<typename _Res>
436 class _Deferred_state;
438 template<typename _Res>
441 template<typename _Signature>
444 template<typename _StateT, typename _Res = typename _StateT::_Res_type>
448 inline __future_base::_Result_base::~_Result_base() = default;
450 /// Partial specialization for reference types.
451 template<typename _Res>
452 struct __future_base::_Result<_Res&> : __future_base::_Result_base
454 _Result() : _M_value_ptr() { }
456 void _M_set(_Res& __res) { _M_value_ptr = &__res; }
458 _Res& _M_get() { return *_M_value_ptr; }
463 void _M_destroy() { delete this; }
466 /// Explicit specialization for void.
468 struct __future_base::_Result<void> : __future_base::_Result_base
471 void _M_destroy() { delete this; }
475 /// Common implementation for future and shared_future.
476 template<typename _Res>
477 class __basic_future : public __future_base
480 typedef shared_ptr<_State> __state_type;
481 typedef __future_base::_Result<_Res>& __result_type;
484 __state_type _M_state;
488 __basic_future(const __basic_future&) = delete;
489 __basic_future& operator=(const __basic_future&) = delete;
492 valid() const { return static_cast<bool>(_M_state); }
495 wait() const { _M_state->wait(); }
497 template<typename _Rep, typename _Period>
499 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
500 { return _M_state->wait_for(__rel); }
502 template<typename _Clock, typename _Duration>
504 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
505 { return _M_state->wait_until(__abs); }
508 /// Wait for the state to be ready and rethrow any stored exception
512 _Result_base& __res = _M_state->wait();
513 if (!(__res._M_error == 0))
514 rethrow_exception(__res._M_error);
515 return static_cast<__result_type>(__res);
518 void _M_swap(__basic_future& __that)
520 _M_state.swap(__that._M_state);
523 // Construction of a future by promise::get_future()
525 __basic_future(const __state_type& __state) : _M_state(__state)
527 _State::_S_check(_M_state);
528 _M_state->_M_set_retrieved_flag();
531 // Copy construction from a shared_future
533 __basic_future(const shared_future<_Res>&);
535 // Move construction from a shared_future
537 __basic_future(shared_future<_Res>&&);
539 // Move construction from a future
541 __basic_future(future<_Res>&&);
547 explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { }
548 ~_Reset() { _M_fut._M_state.reset(); }
549 __basic_future& _M_fut;
554 /// Primary template for future.
555 template<typename _Res>
556 class future : public __basic_future<_Res>
558 friend class promise<_Res>;
559 template<typename> friend class packaged_task;
560 template<typename _Fn, typename... _Args>
561 friend future<typename result_of<_Fn(_Args...)>::type>
562 async(launch, _Fn&&, _Args&&...);
564 typedef __basic_future<_Res> _Base_type;
565 typedef typename _Base_type::__state_type __state_type;
568 future(const __state_type& __state) : _Base_type(__state) { }
571 future() : _Base_type() { }
574 future(future&& __uf) : _Base_type(std::move(__uf)) { }
577 future(const future&) = delete;
578 future& operator=(const future&) = delete;
580 future& operator=(future&& __fut)
582 future(std::move(__fut))._M_swap(*this);
586 /// Retrieving the value
590 typename _Base_type::_Reset __reset(*this);
591 return std::move(this->_M_get_result()._M_value());
595 /// Partial specialization for future<R&>
596 template<typename _Res>
597 class future<_Res&> : public __basic_future<_Res&>
599 friend class promise<_Res&>;
600 template<typename> friend class packaged_task;
601 template<typename _Fn, typename... _Args>
602 friend future<typename result_of<_Fn(_Args...)>::type>
603 async(launch, _Fn&&, _Args&&...);
605 typedef __basic_future<_Res&> _Base_type;
606 typedef typename _Base_type::__state_type __state_type;
609 future(const __state_type& __state) : _Base_type(__state) { }
612 future() : _Base_type() { }
615 future(future&& __uf) : _Base_type(std::move(__uf)) { }
618 future(const future&) = delete;
619 future& operator=(const future&) = delete;
621 future& operator=(future&& __fut)
623 future(std::move(__fut))._M_swap(*this);
627 /// Retrieving the value
631 typename _Base_type::_Reset __reset(*this);
632 return this->_M_get_result()._M_get();
636 /// Explicit specialization for future<void>
638 class future<void> : public __basic_future<void>
640 friend class promise<void>;
641 template<typename> friend class packaged_task;
642 template<typename _Fn, typename... _Args>
643 friend future<typename result_of<_Fn(_Args...)>::type>
644 async(launch, _Fn&&, _Args&&...);
646 typedef __basic_future<void> _Base_type;
647 typedef typename _Base_type::__state_type __state_type;
650 future(const __state_type& __state) : _Base_type(__state) { }
653 future() : _Base_type() { }
656 future(future&& __uf) : _Base_type(std::move(__uf)) { }
659 future(const future&) = delete;
660 future& operator=(const future&) = delete;
662 future& operator=(future&& __fut)
664 future(std::move(__fut))._M_swap(*this);
668 /// Retrieving the value
672 typename _Base_type::_Reset __reset(*this);
673 this->_M_get_result();
678 /// Primary template for shared_future.
679 template<typename _Res>
680 class shared_future : public __basic_future<_Res>
682 typedef __basic_future<_Res> _Base_type;
685 shared_future() : _Base_type() { }
688 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
690 /// Construct from a future rvalue
691 shared_future(future<_Res>&& __uf)
692 : _Base_type(std::move(__uf))
695 /// Construct from a shared_future rvalue
696 shared_future(shared_future&& __sf)
697 : _Base_type(std::move(__sf))
700 shared_future& operator=(const shared_future& __sf)
702 shared_future(__sf)._M_swap(*this);
706 shared_future& operator=(shared_future&& __sf)
708 shared_future(std::move(__sf))._M_swap(*this);
712 /// Retrieving the value
716 typename _Base_type::__result_type __r = this->_M_get_result();
717 _Res& __rs(__r._M_value());
722 /// Partial specialization for shared_future<R&>
723 template<typename _Res>
724 class shared_future<_Res&> : public __basic_future<_Res&>
726 typedef __basic_future<_Res&> _Base_type;
729 shared_future() : _Base_type() { }
732 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
734 /// Construct from a future rvalue
735 shared_future(future<_Res&>&& __uf)
736 : _Base_type(std::move(__uf))
739 /// Construct from a shared_future rvalue
740 shared_future(shared_future&& __sf)
741 : _Base_type(std::move(__sf))
744 shared_future& operator=(const shared_future& __sf)
746 shared_future(__sf)._M_swap(*this);
750 shared_future& operator=(shared_future&& __sf)
752 shared_future(std::move(__sf))._M_swap(*this);
756 /// Retrieving the value
758 get() { return this->_M_get_result()._M_get(); }
761 /// Explicit specialization for shared_future<void>
763 class shared_future<void> : public __basic_future<void>
765 typedef __basic_future<void> _Base_type;
768 shared_future() : _Base_type() { }
771 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
773 /// Construct from a future rvalue
774 shared_future(future<void>&& __uf)
775 : _Base_type(std::move(__uf))
778 /// Construct from a shared_future rvalue
779 shared_future(shared_future&& __sf)
780 : _Base_type(std::move(__sf))
783 shared_future& operator=(const shared_future& __sf)
785 shared_future(__sf)._M_swap(*this);
789 shared_future& operator=(shared_future&& __sf)
791 shared_future(std::move(__sf))._M_swap(*this);
795 // Retrieving the value
797 get() { this->_M_get_result(); }
800 // Now we can define the protected __basic_future constructors.
801 template<typename _Res>
802 inline __basic_future<_Res>::
803 __basic_future(const shared_future<_Res>& __sf)
804 : _M_state(__sf._M_state)
807 template<typename _Res>
808 inline __basic_future<_Res>::
809 __basic_future(shared_future<_Res>&& __sf)
810 : _M_state(std::move(__sf._M_state))
813 template<typename _Res>
814 inline __basic_future<_Res>::
815 __basic_future(future<_Res>&& __uf)
816 : _M_state(std::move(__uf._M_state))
820 /// Primary template for promise
821 template<typename _Res>
824 typedef __future_base::_State _State;
825 typedef __future_base::_Result<_Res> _Res_type;
826 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
827 template<typename, typename> friend class _State::_Setter;
829 shared_ptr<_State> _M_future;
830 _Ptr_type _M_storage;
834 : _M_future(std::make_shared<_State>()),
835 _M_storage(new _Res_type())
838 promise(promise&& __rhs)
839 : _M_future(std::move(__rhs._M_future)),
840 _M_storage(std::move(__rhs._M_storage))
843 template<typename _Allocator>
844 promise(allocator_arg_t, const _Allocator& __a)
845 : _M_future(std::allocate_shared<_State>(__a)),
846 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
849 promise(const promise&) = delete;
853 if (static_cast<bool>(_M_future) && !_M_future.unique())
854 _M_future->_M_break_promise(std::move(_M_storage));
859 operator=(promise&& __rhs)
861 promise(std::move(__rhs)).swap(*this);
865 promise& operator=(const promise&) = delete;
870 _M_future.swap(__rhs._M_future);
871 _M_storage.swap(__rhs._M_storage);
874 // Retrieving the result
877 { return future<_Res>(_M_future); }
879 // Setting the result
881 set_value(const _Res& __r)
883 auto __setter = _State::__setter(this, __r);
884 _M_future->_M_set_result(std::move(__setter));
888 set_value(_Res&& __r)
890 auto __setter = _State::__setter(this, std::move(__r));
891 _M_future->_M_set_result(std::move(__setter));
895 set_exception(exception_ptr __p)
897 auto __setter = _State::__setter(__p, this);
898 _M_future->_M_set_result(std::move(__setter));
902 template<typename _Res>
904 swap(promise<_Res>& __x, promise<_Res>& __y)
907 /// Partial specialization for promise<R&>
908 template<typename _Res>
911 typedef __future_base::_State _State;
912 typedef __future_base::_Result<_Res&> _Res_type;
913 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
914 template<typename, typename> friend class _State::_Setter;
916 shared_ptr<_State> _M_future;
917 _Ptr_type _M_storage;
921 : _M_future(std::make_shared<_State>()),
922 _M_storage(new _Res_type())
925 promise(promise&& __rhs)
926 : _M_future(std::move(__rhs._M_future)),
927 _M_storage(std::move(__rhs._M_storage))
930 template<typename _Allocator>
931 promise(allocator_arg_t, const _Allocator& __a)
932 : _M_future(std::allocate_shared<_State>(__a)),
933 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
936 promise(const promise&) = delete;
940 if (static_cast<bool>(_M_future) && !_M_future.unique())
941 _M_future->_M_break_promise(std::move(_M_storage));
946 operator=(promise&& __rhs)
948 promise(std::move(__rhs)).swap(*this);
952 promise& operator=(const promise&) = delete;
957 _M_future.swap(__rhs._M_future);
958 _M_storage.swap(__rhs._M_storage);
961 // Retrieving the result
964 { return future<_Res&>(_M_future); }
966 // Setting the result
970 auto __setter = _State::__setter(this, __r);
971 _M_future->_M_set_result(std::move(__setter));
975 set_exception(exception_ptr __p)
977 auto __setter = _State::__setter(__p, this);
978 _M_future->_M_set_result(std::move(__setter));
982 /// Explicit specialization for promise<void>
986 typedef __future_base::_State _State;
987 typedef __future_base::_Result<void> _Res_type;
988 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
989 template<typename, typename> friend class _State::_Setter;
991 shared_ptr<_State> _M_future;
992 _Ptr_type _M_storage;
996 : _M_future(std::make_shared<_State>()),
997 _M_storage(new _Res_type())
1000 promise(promise&& __rhs)
1001 : _M_future(std::move(__rhs._M_future)),
1002 _M_storage(std::move(__rhs._M_storage))
1006 template<typename _Allocator>
1007 promise(allocator_arg_t, const _Allocator& __a)
1008 : _M_future(std::allocate_shared<_State>(__a)),
1009 _M_storage(__future_base::_S_allocate_result<void>(__a))
1012 promise(const promise&) = delete;
1016 if (static_cast<bool>(_M_future) && !_M_future.unique())
1017 _M_future->_M_break_promise(std::move(_M_storage));
1022 operator=(promise&& __rhs)
1024 promise(std::move(__rhs)).swap(*this);
1028 promise& operator=(const promise&) = delete;
1031 swap(promise& __rhs)
1033 _M_future.swap(__rhs._M_future);
1034 _M_storage.swap(__rhs._M_storage);
1037 // Retrieving the result
1040 { return future<void>(_M_future); }
1042 // Setting the result
1046 set_exception(exception_ptr __p)
1048 auto __setter = _State::__setter(__p, this);
1049 _M_future->_M_set_result(std::move(__setter));
1055 struct __future_base::_State::_Setter<void, void>
1057 promise<void>::_Ptr_type operator()()
1059 _State::_S_check(_M_promise->_M_future);
1060 return std::move(_M_promise->_M_storage);
1063 promise<void>* _M_promise;
1066 inline __future_base::_State::_Setter<void, void>
1067 __future_base::_State::__setter(promise<void>* __prom)
1069 return _Setter<void, void>{ __prom };
1073 promise<void>::set_value()
1075 auto __setter = _State::__setter(this);
1076 _M_future->_M_set_result(std::move(__setter));
1079 template<typename _Res, class Alloc>
1080 struct uses_allocator<promise<_Res>, Alloc> : true_type { };
1083 template<typename _StateT, typename _Res>
1084 struct __future_base::_Task_setter
1086 typename _StateT::_Ptr_type operator()()
1090 _M_state->_M_result->_M_set(_M_fn());
1094 _M_state->_M_result->_M_error = current_exception();
1096 return std::move(_M_state->_M_result);
1099 std::function<_Res()> _M_fn;
1102 template<typename _StateT>
1103 struct __future_base::_Task_setter<_StateT, void>
1105 typename _StateT::_Ptr_type operator()()
1113 _M_state->_M_result->_M_error = current_exception();
1115 return std::move(_M_state->_M_result);
1118 std::function<void()> _M_fn;
1121 template<typename _Res, typename... _Args>
1122 struct __future_base::_Task_state<_Res(_Args...)> : __future_base::_State
1124 typedef _Res _Res_type;
1126 _Task_state(std::function<_Res(_Args...)> __task)
1127 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1130 template<typename _Func, typename _Alloc>
1131 _Task_state(_Func&& __task, const _Alloc& __a)
1132 : _M_result(_S_allocate_result<_Res>(__a))
1133 , _M_task(allocator_arg, __a, std::move(__task))
1137 _M_run(_Args... __args)
1139 // bound arguments decay so wrap lvalue references
1140 auto __bound = std::bind<_Res>(std::ref(_M_task),
1141 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1142 _Task_setter<_Task_state> __setter{ this, std::move(__bound) };
1143 _M_set_result(std::move(__setter));
1146 template<typename, typename> friend class _Task_setter;
1147 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1148 _Ptr_type _M_result;
1149 std::function<_Res(_Args...)> _M_task;
1151 template<typename _Tp>
1152 static reference_wrapper<_Tp>
1153 _S_maybe_wrap_ref(_Tp& __t)
1154 { return std::ref(__t); }
1156 template<typename _Tp>
1157 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1159 _S_maybe_wrap_ref(_Tp&& __t)
1160 { return std::forward<_Tp>(__t); }
1164 template<typename _Res, typename... _ArgTypes>
1165 class packaged_task<_Res(_ArgTypes...)>
1167 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1168 shared_ptr<_State_type> _M_state;
1171 typedef _Res result_type;
1173 // Construction and destruction
1176 template<typename _Fn>
1178 packaged_task(const _Fn& __fn)
1179 : _M_state(std::make_shared<_State_type>(__fn))
1182 template<typename _Fn>
1184 packaged_task(_Fn&& __fn)
1185 : _M_state(std::make_shared<_State_type>(std::move(__fn)))
1189 packaged_task(_Res(*__fn)(_ArgTypes...))
1190 : _M_state(std::make_shared<_State_type>(__fn))
1193 template<typename _Fn, typename _Allocator>
1195 packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn __fn)
1196 : _M_state(std::allocate_shared<_State_type>(__a, std::move(__fn)))
1201 if (static_cast<bool>(_M_state) && !_M_state.unique())
1202 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1206 packaged_task(packaged_task&) = delete;
1207 packaged_task& operator=(packaged_task&) = delete;
1210 packaged_task(packaged_task&& __other)
1211 { this->swap(__other); }
1213 packaged_task& operator=(packaged_task&& __other)
1215 packaged_task(std::move(__other)).swap(*this);
1220 swap(packaged_task& __other)
1221 { _M_state.swap(__other._M_state); }
1223 explicit operator bool() const { return static_cast<bool>(_M_state); }
1228 { return future<_Res>(_M_state); }
1232 operator()(_ArgTypes... __args)
1234 __future_base::_State::_S_check(_M_state);
1235 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1241 __future_base::_State::_S_check(_M_state);
1242 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1246 template<typename _Res, typename... _ArgTypes>
1248 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1249 packaged_task<_Res(_ArgTypes...)>& __y)
1252 template<typename _Res>
1253 class __future_base::_Deferred_state : public __future_base::_State
1256 typedef _Res _Res_type;
1259 _Deferred_state(std::function<_Res()>&& __fn)
1260 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1264 template<typename, typename> friend class _Task_setter;
1265 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1266 _Ptr_type _M_result;
1267 std::function<_Res()> _M_fn;
1272 _Task_setter<_Deferred_state> __setter{ this, _M_fn };
1273 // safe to call multiple times so ignore failure
1274 _M_set_result(std::move(__setter), true);
1278 template<typename _Res>
1279 class __future_base::_Async_state : public __future_base::_State
1282 typedef _Res _Res_type;
1285 _Async_state(std::function<_Res()>&& __fn)
1286 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1287 _M_thread(mem_fn(&_Async_state::_M_do_run), this)
1290 ~_Async_state() { _M_thread.join(); }
1295 _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) };
1296 _M_set_result(std::move(__setter));
1299 template<typename, typename> friend class _Task_setter;
1300 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1301 _Ptr_type _M_result;
1302 std::function<_Res()> _M_fn;
1306 template<typename _Fn, typename... _Args>
1307 future<typename result_of<_Fn(_Args...)>::type>
1308 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1310 typedef typename result_of<_Fn(_Args...)>::type result_type;
1311 std::shared_ptr<__future_base::_State> __state;
1312 if (__policy == launch::async)
1314 typedef typename __future_base::_Async_state<result_type> _State;
1315 __state = std::make_shared<_State>(std::bind<result_type>(
1316 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1320 typedef typename __future_base::_Deferred_state<result_type> _State;
1321 __state = std::make_shared<_State>(std::bind<result_type>(
1322 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1324 return future<result_type>(__state);
1327 template<typename _Fn, typename... _Args>
1329 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
1330 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
1332 async(_Fn&& __fn, _Args&&... __args)
1334 return async(launch::any, std::forward<_Fn>(__fn),
1335 std::forward<_Args>(__args)...);
1338 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1339 // && _GLIBCXX_ATOMIC_BUILTINS_4
1344 #endif // __GXX_EXPERIMENTAL_CXX0X__
1346 #endif // _GLIBCXX_FUTURE