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 _FnCheck, typename _Fn, typename... _Args>
146 struct __async_sfinae_helper
148 typedef future<typename result_of<_Fn(_Args...)>::type> type;
151 template<typename _Fn, typename... _Args>
152 struct __async_sfinae_helper<launch, _Fn, _Args...>
155 template<typename _Fn, typename... _Args>
157 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
158 async(_Fn&& __fn, _Args&&... __args);
160 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
161 && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
163 /// Base class and enclosing scope.
166 /// Base class for results.
169 exception_ptr _M_error;
171 _Result_base(const _Result_base&) = delete;
172 _Result_base& operator=(const _Result_base&) = delete;
174 // _M_destroy() allows derived classes to control deallocation
175 virtual void _M_destroy() = 0;
179 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
184 virtual ~_Result_base();
188 template<typename _Res>
189 struct _Result : _Result_base
192 typedef alignment_of<_Res> __a_of;
193 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
194 typedef typename __align_storage::type __align_type;
196 __align_type _M_storage;
200 _Result() : _M_initialized() { }
208 // Return lvalue, future will add const or rvalue-reference
210 _M_value() { return *static_cast<_Res*>(_M_addr()); }
213 _M_set(const _Res& __res)
215 ::new (_M_addr()) _Res(__res);
216 _M_initialized = true;
222 ::new (_M_addr()) _Res(std::move(__res));
223 _M_initialized = true;
227 void _M_destroy() { delete this; }
229 void* _M_addr() { return static_cast<void*>(&_M_storage); }
232 // TODO: use template alias when available
234 template<typename _Res>
235 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
237 /// A unique_ptr based on the instantiating type.
238 template<typename _Res>
241 typedef unique_ptr<_Res, _Result_base::_Deleter> type;
245 template<typename _Res, typename _Alloc>
246 struct _Result_alloc : _Result<_Res>, _Alloc
248 typedef typename _Alloc::template rebind<_Result_alloc>::other
252 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
258 __allocator_type __a(*this);
260 __a.deallocate(this, 1);
264 template<typename _Res, typename _Allocator>
265 static typename _Ptr<_Result_alloc<_Res, _Allocator>>::type
266 _S_allocate_result(const _Allocator& __a)
268 typedef _Result_alloc<_Res, _Allocator> __result_type;
269 typename __result_type::__allocator_type __a2(__a);
270 __result_type* __p = __a2.allocate(1);
273 __a2.construct(__p, __a);
277 __a2.deallocate(__p, 1);
278 __throw_exception_again;
280 return typename _Ptr<__result_type>::type(__p);
284 /// Base class for state between a promise and one or more
285 /// associated futures.
288 typedef _Ptr<_Result_base>::type _Ptr_type;
292 condition_variable _M_cond;
293 atomic_flag _M_retrieved;
297 _State_base() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
298 _State_base(const _State_base&) = delete;
299 _State_base& operator=(const _State_base&) = delete;
300 virtual ~_State_base();
306 unique_lock<mutex> __lock(_M_mutex);
308 _M_cond.wait(__lock, std::bind<bool>(&_State_base::_M_ready, this));
312 template<typename _Rep, typename _Period>
314 wait_for(const chrono::duration<_Rep, _Period>& __rel)
316 unique_lock<mutex> __lock(_M_mutex);
317 auto __bound = std::bind<bool>(&_State_base::_M_ready, this);
318 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
321 template<typename _Clock, typename _Duration>
323 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
325 unique_lock<mutex> __lock(_M_mutex);
326 auto __bound = std::bind<bool>(&_State_base::_M_ready, this);
327 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
331 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
333 bool __set = __ignore_failure;
334 // all calls to this function are serialized,
335 // side-effects of invoking __res only happen once
336 call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
339 __throw_future_error(int(future_errc::promise_already_satisfied));
343 _M_break_promise(_Ptr_type __res)
345 if (static_cast<bool>(__res))
347 error_code __ec(make_error_code(future_errc::broken_promise));
348 __res->_M_error = copy_exception(future_error(__ec));
350 lock_guard<mutex> __lock(_M_mutex);
351 _M_result.swap(__res);
353 _M_cond.notify_all();
357 // Called when this object is passed to a future.
359 _M_set_retrieved_flag()
361 if (_M_retrieved.test_and_set())
362 __throw_future_error(int(future_errc::future_already_retrieved));
365 template<typename _Res, typename _Arg>
369 template<typename _Res, typename _Arg>
370 struct _Setter<_Res, _Arg&>
372 // check this is only used by promise<R>::set_value(const R&)
373 // or promise<R>::set_value(R&)
374 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
375 || is_same<const _Res, _Arg>::value, // promise<R>
376 "Invalid specialisation");
378 typename promise<_Res>::_Ptr_type operator()()
380 _State_base::_S_check(_M_promise->_M_future);
381 _M_promise->_M_storage->_M_set(_M_arg);
382 return std::move(_M_promise->_M_storage);
384 promise<_Res>* _M_promise;
389 template<typename _Res>
390 struct _Setter<_Res, _Res&&>
392 typename promise<_Res>::_Ptr_type operator()()
394 _State_base::_S_check(_M_promise->_M_future);
395 _M_promise->_M_storage->_M_set(std::move(_M_arg));
396 return std::move(_M_promise->_M_storage);
398 promise<_Res>* _M_promise;
402 struct __exception_ptr_tag { };
405 template<typename _Res>
406 struct _Setter<_Res, __exception_ptr_tag>
408 typename promise<_Res>::_Ptr_type operator()()
410 _State_base::_S_check(_M_promise->_M_future);
411 _M_promise->_M_storage->_M_error = _M_ex;
412 return std::move(_M_promise->_M_storage);
415 promise<_Res>* _M_promise;
416 exception_ptr& _M_ex;
419 template<typename _Res, typename _Arg>
420 static _Setter<_Res, _Arg&&>
421 __setter(promise<_Res>* __prom, _Arg&& __arg)
423 return _Setter<_Res, _Arg&&>{ __prom, __arg };
426 template<typename _Res>
427 static _Setter<_Res, __exception_ptr_tag>
428 __setter(exception_ptr& __ex, promise<_Res>* __prom)
430 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
433 static _Setter<void, void>
434 __setter(promise<void>* __prom);
436 template<typename _Tp>
438 _S_check(const shared_ptr<_Tp>& __p)
440 if (!static_cast<bool>(__p))
441 __throw_future_error((int)future_errc::no_state);
446 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
448 _Ptr_type __res = __f();
450 lock_guard<mutex> __lock(_M_mutex);
451 _M_result.swap(__res);
453 _M_cond.notify_all();
457 bool _M_ready() const { return static_cast<bool>(_M_result); }
459 virtual void _M_run_deferred() { }
462 template<typename _Res>
463 class _Deferred_state;
465 template<typename _Res>
468 template<typename _Signature>
471 template<typename _StateT, typename _Res = typename _StateT::_Res_type>
475 /// Partial specialization for reference types.
476 template<typename _Res>
477 struct __future_base::_Result<_Res&> : __future_base::_Result_base
479 _Result() : _M_value_ptr() { }
481 void _M_set(_Res& __res) { _M_value_ptr = &__res; }
483 _Res& _M_get() { return *_M_value_ptr; }
488 void _M_destroy() { delete this; }
491 /// Explicit specialization for void.
493 struct __future_base::_Result<void> : __future_base::_Result_base
496 void _M_destroy() { delete this; }
500 /// Common implementation for future and shared_future.
501 template<typename _Res>
502 class __basic_future : public __future_base
505 typedef shared_ptr<_State_base> __state_type;
506 typedef __future_base::_Result<_Res>& __result_type;
509 __state_type _M_state;
513 __basic_future(const __basic_future&) = delete;
514 __basic_future& operator=(const __basic_future&) = delete;
517 valid() const { return static_cast<bool>(_M_state); }
522 _State_base::_S_check(_M_state);
526 template<typename _Rep, typename _Period>
528 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
530 _State_base::_S_check(_M_state);
531 return _M_state->wait_for(__rel);
534 template<typename _Clock, typename _Duration>
536 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
538 _State_base::_S_check(_M_state);
539 return _M_state->wait_until(__abs);
543 /// Wait for the state to be ready and rethrow any stored exception
547 _State_base::_S_check(_M_state);
548 _Result_base& __res = _M_state->wait();
549 if (!(__res._M_error == 0))
550 rethrow_exception(__res._M_error);
551 return static_cast<__result_type>(__res);
554 void _M_swap(__basic_future& __that)
556 _M_state.swap(__that._M_state);
559 // Construction of a future by promise::get_future()
561 __basic_future(const __state_type& __state) : _M_state(__state)
563 _State_base::_S_check(_M_state);
564 _M_state->_M_set_retrieved_flag();
567 // Copy construction from a shared_future
569 __basic_future(const shared_future<_Res>&);
571 // Move construction from a shared_future
573 __basic_future(shared_future<_Res>&&);
575 // Move construction from a future
577 __basic_future(future<_Res>&&);
579 constexpr __basic_future() : _M_state() { }
583 explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { }
584 ~_Reset() { _M_fut._M_state.reset(); }
585 __basic_future& _M_fut;
590 /// Primary template for future.
591 template<typename _Res>
592 class future : public __basic_future<_Res>
594 friend class promise<_Res>;
595 template<typename> friend class packaged_task;
596 template<typename _Fn, typename... _Args>
597 friend future<typename result_of<_Fn(_Args...)>::type>
598 async(launch, _Fn&&, _Args&&...);
600 typedef __basic_future<_Res> _Base_type;
601 typedef typename _Base_type::__state_type __state_type;
604 future(const __state_type& __state) : _Base_type(__state) { }
607 constexpr future() : _Base_type() { }
610 future(future&& __uf) : _Base_type(std::move(__uf)) { }
613 future(const future&) = delete;
614 future& operator=(const future&) = delete;
616 future& operator=(future&& __fut)
618 future(std::move(__fut))._M_swap(*this);
622 /// Retrieving the value
626 typename _Base_type::_Reset __reset(*this);
627 return std::move(this->_M_get_result()._M_value());
630 shared_future<_Res> share();
633 /// Partial specialization for future<R&>
634 template<typename _Res>
635 class future<_Res&> : public __basic_future<_Res&>
637 friend class promise<_Res&>;
638 template<typename> friend class packaged_task;
639 template<typename _Fn, typename... _Args>
640 friend future<typename result_of<_Fn(_Args...)>::type>
641 async(launch, _Fn&&, _Args&&...);
643 typedef __basic_future<_Res&> _Base_type;
644 typedef typename _Base_type::__state_type __state_type;
647 future(const __state_type& __state) : _Base_type(__state) { }
650 constexpr future() : _Base_type() { }
653 future(future&& __uf) : _Base_type(std::move(__uf)) { }
656 future(const future&) = delete;
657 future& operator=(const future&) = delete;
659 future& operator=(future&& __fut)
661 future(std::move(__fut))._M_swap(*this);
665 /// Retrieving the value
669 typename _Base_type::_Reset __reset(*this);
670 return this->_M_get_result()._M_get();
673 shared_future<_Res&> share();
676 /// Explicit specialization for future<void>
678 class future<void> : public __basic_future<void>
680 friend class promise<void>;
681 template<typename> friend class packaged_task;
682 template<typename _Fn, typename... _Args>
683 friend future<typename result_of<_Fn(_Args...)>::type>
684 async(launch, _Fn&&, _Args&&...);
686 typedef __basic_future<void> _Base_type;
687 typedef typename _Base_type::__state_type __state_type;
690 future(const __state_type& __state) : _Base_type(__state) { }
693 constexpr future() : _Base_type() { }
696 future(future&& __uf) : _Base_type(std::move(__uf)) { }
699 future(const future&) = delete;
700 future& operator=(const future&) = delete;
702 future& operator=(future&& __fut)
704 future(std::move(__fut))._M_swap(*this);
708 /// Retrieving the value
712 typename _Base_type::_Reset __reset(*this);
713 this->_M_get_result();
716 shared_future<void> share();
720 /// Primary template for shared_future.
721 template<typename _Res>
722 class shared_future : public __basic_future<_Res>
724 typedef __basic_future<_Res> _Base_type;
727 constexpr shared_future() : _Base_type() { }
730 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
732 /// Construct from a future rvalue
733 shared_future(future<_Res>&& __uf)
734 : _Base_type(std::move(__uf))
737 /// Construct from a shared_future rvalue
738 shared_future(shared_future&& __sf)
739 : _Base_type(std::move(__sf))
742 shared_future& operator=(const shared_future& __sf)
744 shared_future(__sf)._M_swap(*this);
748 shared_future& operator=(shared_future&& __sf)
750 shared_future(std::move(__sf))._M_swap(*this);
754 /// Retrieving the value
758 typename _Base_type::__result_type __r = this->_M_get_result();
759 _Res& __rs(__r._M_value());
764 /// Partial specialization for shared_future<R&>
765 template<typename _Res>
766 class shared_future<_Res&> : public __basic_future<_Res&>
768 typedef __basic_future<_Res&> _Base_type;
771 constexpr shared_future() : _Base_type() { }
774 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
776 /// Construct from a future rvalue
777 shared_future(future<_Res&>&& __uf)
778 : _Base_type(std::move(__uf))
781 /// Construct from a shared_future rvalue
782 shared_future(shared_future&& __sf)
783 : _Base_type(std::move(__sf))
786 shared_future& operator=(const shared_future& __sf)
788 shared_future(__sf)._M_swap(*this);
792 shared_future& operator=(shared_future&& __sf)
794 shared_future(std::move(__sf))._M_swap(*this);
798 /// Retrieving the value
800 get() { return this->_M_get_result()._M_get(); }
803 /// Explicit specialization for shared_future<void>
805 class shared_future<void> : public __basic_future<void>
807 typedef __basic_future<void> _Base_type;
810 constexpr shared_future() : _Base_type() { }
813 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
815 /// Construct from a future rvalue
816 shared_future(future<void>&& __uf)
817 : _Base_type(std::move(__uf))
820 /// Construct from a shared_future rvalue
821 shared_future(shared_future&& __sf)
822 : _Base_type(std::move(__sf))
825 shared_future& operator=(const shared_future& __sf)
827 shared_future(__sf)._M_swap(*this);
831 shared_future& operator=(shared_future&& __sf)
833 shared_future(std::move(__sf))._M_swap(*this);
837 // Retrieving the value
839 get() { this->_M_get_result(); }
842 // Now we can define the protected __basic_future constructors.
843 template<typename _Res>
844 inline __basic_future<_Res>::
845 __basic_future(const shared_future<_Res>& __sf)
846 : _M_state(__sf._M_state)
849 template<typename _Res>
850 inline __basic_future<_Res>::
851 __basic_future(shared_future<_Res>&& __sf)
852 : _M_state(std::move(__sf._M_state))
855 template<typename _Res>
856 inline __basic_future<_Res>::
857 __basic_future(future<_Res>&& __uf)
858 : _M_state(std::move(__uf._M_state))
861 template<typename _Res>
862 inline shared_future<_Res>
863 future<_Res>::share()
864 { return shared_future<_Res>(std::move(*this)); }
866 template<typename _Res>
867 inline shared_future<_Res&>
868 future<_Res&>::share()
869 { return shared_future<_Res&>(std::move(*this)); }
871 inline shared_future<void>
872 future<void>::share()
873 { return shared_future<void>(std::move(*this)); }
875 /// Primary template for promise
876 template<typename _Res>
879 typedef __future_base::_State_base _State;
880 typedef __future_base::_Result<_Res> _Res_type;
881 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
882 template<typename, typename> friend class _State::_Setter;
884 shared_ptr<_State> _M_future;
885 _Ptr_type _M_storage;
889 : _M_future(std::make_shared<_State>()),
890 _M_storage(new _Res_type())
893 promise(promise&& __rhs)
894 : _M_future(std::move(__rhs._M_future)),
895 _M_storage(std::move(__rhs._M_storage))
898 template<typename _Allocator>
899 promise(allocator_arg_t, const _Allocator& __a)
900 : _M_future(std::allocate_shared<_State>(__a)),
901 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
904 promise(const promise&) = delete;
908 if (static_cast<bool>(_M_future) && !_M_future.unique())
909 _M_future->_M_break_promise(std::move(_M_storage));
914 operator=(promise&& __rhs)
916 promise(std::move(__rhs)).swap(*this);
920 promise& operator=(const promise&) = delete;
925 _M_future.swap(__rhs._M_future);
926 _M_storage.swap(__rhs._M_storage);
929 // Retrieving the result
932 { return future<_Res>(_M_future); }
934 // Setting the result
936 set_value(const _Res& __r)
938 auto __setter = _State::__setter(this, __r);
939 _M_future->_M_set_result(std::move(__setter));
943 set_value(_Res&& __r)
945 auto __setter = _State::__setter(this, std::move(__r));
946 _M_future->_M_set_result(std::move(__setter));
950 set_exception(exception_ptr __p)
952 auto __setter = _State::__setter(__p, this);
953 _M_future->_M_set_result(std::move(__setter));
957 template<typename _Res>
959 swap(promise<_Res>& __x, promise<_Res>& __y)
962 template<typename _Res, typename _Alloc>
963 struct uses_allocator<promise<_Res>, _Alloc>
964 : public true_type { };
967 /// Partial specialization for promise<R&>
968 template<typename _Res>
971 typedef __future_base::_State_base _State;
972 typedef __future_base::_Result<_Res&> _Res_type;
973 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
974 template<typename, typename> friend class _State::_Setter;
976 shared_ptr<_State> _M_future;
977 _Ptr_type _M_storage;
981 : _M_future(std::make_shared<_State>()),
982 _M_storage(new _Res_type())
985 promise(promise&& __rhs)
986 : _M_future(std::move(__rhs._M_future)),
987 _M_storage(std::move(__rhs._M_storage))
990 template<typename _Allocator>
991 promise(allocator_arg_t, const _Allocator& __a)
992 : _M_future(std::allocate_shared<_State>(__a)),
993 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
996 promise(const promise&) = delete;
1000 if (static_cast<bool>(_M_future) && !_M_future.unique())
1001 _M_future->_M_break_promise(std::move(_M_storage));
1006 operator=(promise&& __rhs)
1008 promise(std::move(__rhs)).swap(*this);
1012 promise& operator=(const promise&) = delete;
1015 swap(promise& __rhs)
1017 _M_future.swap(__rhs._M_future);
1018 _M_storage.swap(__rhs._M_storage);
1021 // Retrieving the result
1024 { return future<_Res&>(_M_future); }
1026 // Setting the result
1028 set_value(_Res& __r)
1030 auto __setter = _State::__setter(this, __r);
1031 _M_future->_M_set_result(std::move(__setter));
1035 set_exception(exception_ptr __p)
1037 auto __setter = _State::__setter(__p, this);
1038 _M_future->_M_set_result(std::move(__setter));
1042 /// Explicit specialization for promise<void>
1046 typedef __future_base::_State_base _State;
1047 typedef __future_base::_Result<void> _Res_type;
1048 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
1049 template<typename, typename> friend class _State::_Setter;
1051 shared_ptr<_State> _M_future;
1052 _Ptr_type _M_storage;
1056 : _M_future(std::make_shared<_State>()),
1057 _M_storage(new _Res_type())
1060 promise(promise&& __rhs)
1061 : _M_future(std::move(__rhs._M_future)),
1062 _M_storage(std::move(__rhs._M_storage))
1065 template<typename _Allocator>
1066 promise(allocator_arg_t, const _Allocator& __a)
1067 : _M_future(std::allocate_shared<_State>(__a)),
1068 _M_storage(__future_base::_S_allocate_result<void>(__a))
1071 promise(const promise&) = delete;
1075 if (static_cast<bool>(_M_future) && !_M_future.unique())
1076 _M_future->_M_break_promise(std::move(_M_storage));
1081 operator=(promise&& __rhs)
1083 promise(std::move(__rhs)).swap(*this);
1087 promise& operator=(const promise&) = delete;
1090 swap(promise& __rhs)
1092 _M_future.swap(__rhs._M_future);
1093 _M_storage.swap(__rhs._M_storage);
1096 // Retrieving the result
1099 { return future<void>(_M_future); }
1101 // Setting the result
1105 set_exception(exception_ptr __p)
1107 auto __setter = _State::__setter(__p, this);
1108 _M_future->_M_set_result(std::move(__setter));
1114 struct __future_base::_State_base::_Setter<void, void>
1116 promise<void>::_Ptr_type operator()()
1118 _State_base::_S_check(_M_promise->_M_future);
1119 return std::move(_M_promise->_M_storage);
1122 promise<void>* _M_promise;
1125 inline __future_base::_State_base::_Setter<void, void>
1126 __future_base::_State_base::__setter(promise<void>* __prom)
1128 return _Setter<void, void>{ __prom };
1132 promise<void>::set_value()
1134 auto __setter = _State::__setter(this);
1135 _M_future->_M_set_result(std::move(__setter));
1139 template<typename _StateT, typename _Res>
1140 struct __future_base::_Task_setter
1142 typename _StateT::_Ptr_type operator()()
1146 _M_state->_M_result->_M_set(_M_fn());
1150 _M_state->_M_result->_M_error = current_exception();
1152 return std::move(_M_state->_M_result);
1155 std::function<_Res()> _M_fn;
1158 template<typename _StateT>
1159 struct __future_base::_Task_setter<_StateT, void>
1161 typename _StateT::_Ptr_type operator()()
1169 _M_state->_M_result->_M_error = current_exception();
1171 return std::move(_M_state->_M_result);
1174 std::function<void()> _M_fn;
1177 template<typename _Res, typename... _Args>
1178 struct __future_base::_Task_state<_Res(_Args...)>
1179 : __future_base::_State_base
1181 typedef _Res _Res_type;
1183 _Task_state(std::function<_Res(_Args...)> __task)
1184 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1187 template<typename _Func, typename _Alloc>
1188 _Task_state(_Func&& __task, const _Alloc& __a)
1189 : _M_result(_S_allocate_result<_Res>(__a)),
1190 _M_task(allocator_arg, __a, std::move(__task))
1194 _M_run(_Args... __args)
1196 // bound arguments decay so wrap lvalue references
1197 auto __bound = std::bind<_Res>(std::ref(_M_task),
1198 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1199 _Task_setter<_Task_state> __setter{ this, std::move(__bound) };
1200 _M_set_result(std::move(__setter));
1203 template<typename, typename> friend class _Task_setter;
1204 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1205 _Ptr_type _M_result;
1206 std::function<_Res(_Args...)> _M_task;
1208 template<typename _Tp>
1209 static reference_wrapper<_Tp>
1210 _S_maybe_wrap_ref(_Tp& __t)
1211 { return std::ref(__t); }
1213 template<typename _Tp>
1214 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1216 _S_maybe_wrap_ref(_Tp&& __t)
1217 { return std::forward<_Tp>(__t); }
1221 template<typename _Res, typename... _ArgTypes>
1222 class packaged_task<_Res(_ArgTypes...)>
1224 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1225 shared_ptr<_State_type> _M_state;
1228 // Construction and destruction
1231 template<typename _Fn>
1233 packaged_task(_Fn&& __fn)
1234 : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn)))
1237 template<typename _Fn, typename _Allocator>
1239 packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn)
1240 : _M_state(std::allocate_shared<_State_type>(__a,
1241 std::forward<_Fn>(__fn)))
1246 if (static_cast<bool>(_M_state) && !_M_state.unique())
1247 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1251 packaged_task(packaged_task&) = delete;
1252 packaged_task& operator=(packaged_task&) = delete;
1255 packaged_task(packaged_task&& __other)
1256 { this->swap(__other); }
1258 packaged_task& operator=(packaged_task&& __other)
1260 packaged_task(std::move(__other)).swap(*this);
1265 swap(packaged_task& __other)
1266 { _M_state.swap(__other._M_state); }
1270 { return static_cast<bool>(_M_state); }
1275 { return future<_Res>(_M_state); }
1279 operator()(_ArgTypes... __args)
1281 __future_base::_State_base::_S_check(_M_state);
1282 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1288 __future_base::_State_base::_S_check(_M_state);
1289 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1294 template<typename _Res, typename... _ArgTypes>
1296 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1297 packaged_task<_Res(_ArgTypes...)>& __y)
1300 template<typename _Res, typename _Alloc>
1301 struct uses_allocator<packaged_task<_Res>, _Alloc>
1302 : public true_type { };
1305 template<typename _Res>
1306 class __future_base::_Deferred_state : public __future_base::_State_base
1309 typedef _Res _Res_type;
1312 _Deferred_state(std::function<_Res()>&& __fn)
1313 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1317 template<typename, typename> friend class _Task_setter;
1318 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1319 _Ptr_type _M_result;
1320 std::function<_Res()> _M_fn;
1325 _Task_setter<_Deferred_state> __setter{ this, _M_fn };
1326 // safe to call multiple times so ignore failure
1327 _M_set_result(std::move(__setter), true);
1331 template<typename _Res>
1332 class __future_base::_Async_state : public __future_base::_State_base
1335 typedef _Res _Res_type;
1338 _Async_state(std::function<_Res()>&& __fn)
1339 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1340 _M_thread(mem_fn(&_Async_state::_M_do_run), this)
1343 ~_Async_state() { _M_thread.join(); }
1348 _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) };
1349 _M_set_result(std::move(__setter));
1352 template<typename, typename> friend class _Task_setter;
1353 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1354 _Ptr_type _M_result;
1355 std::function<_Res()> _M_fn;
1360 template<typename _Fn, typename... _Args>
1361 future<typename result_of<_Fn(_Args...)>::type>
1362 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1364 typedef typename result_of<_Fn(_Args...)>::type result_type;
1365 std::shared_ptr<__future_base::_State_base> __state;
1366 if (__policy == launch::async)
1368 typedef typename __future_base::_Async_state<result_type> _State;
1369 __state = std::make_shared<_State>(std::bind<result_type>(
1370 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1374 typedef typename __future_base::_Deferred_state<result_type> _State;
1375 __state = std::make_shared<_State>(std::bind<result_type>(
1376 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1378 return future<result_type>(__state);
1381 /// async, potential overload
1382 template<typename _Fn, typename... _Args>
1384 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
1385 async(_Fn&& __fn, _Args&&... __args)
1387 return async(launch::any, std::forward<_Fn>(__fn),
1388 std::forward<_Args>(__args)...);
1391 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1392 // && _GLIBCXX_ATOMIC_BUILTINS_4
1395 _GLIBCXX_END_NAMESPACE_VERSION
1398 #endif // __GXX_EXPERIMENTAL_CXX0X__
1400 #endif // _GLIBCXX_FUTURE