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.
75 future_category() noexcept;
77 /// Overload for make_error_code.
79 make_error_code(future_errc __errc) noexcept
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) noexcept
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() noexcept;
103 what() const noexcept;
106 code() const noexcept { 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
132 constexpr launch operator&(launch __x, launch __y)
134 return static_cast<launch>(
135 static_cast<int>(__x) & static_cast<int>(__y));
138 constexpr launch operator|(launch __x, launch __y)
140 return static_cast<launch>(
141 static_cast<int>(__x) | static_cast<int>(__y));
144 constexpr launch operator^(launch __x, launch __y)
146 return static_cast<launch>(
147 static_cast<int>(__x) ^ static_cast<int>(__y));
150 constexpr launch operator~(launch __x)
151 { return static_cast<launch>(~static_cast<int>(__x)); }
153 inline launch& operator&=(launch& __x, launch __y)
154 { return __x = __x & __y; }
156 inline launch& operator|=(launch& __x, launch __y)
157 { return __x = __x | __y; }
159 inline launch& operator^=(launch& __x, launch __y)
160 { return __x = __x ^ __y; }
162 /// Status code for futures
163 enum class future_status
170 template<typename _Fn, typename... _Args>
171 future<typename result_of<_Fn(_Args...)>::type>
172 async(launch __policy, _Fn&& __fn, _Args&&... __args);
174 template<typename _FnCheck, typename _Fn, typename... _Args>
175 struct __async_sfinae_helper
177 typedef future<typename result_of<_Fn(_Args...)>::type> type;
180 template<typename _Fn, typename... _Args>
181 struct __async_sfinae_helper<launch, _Fn, _Args...>
184 template<typename _Fn, typename... _Args>
186 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
187 async(_Fn&& __fn, _Args&&... __args);
189 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
190 && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
192 /// Base class and enclosing scope.
195 /// Base class for results.
198 exception_ptr _M_error;
200 _Result_base(const _Result_base&) = delete;
201 _Result_base& operator=(const _Result_base&) = delete;
203 // _M_destroy() allows derived classes to control deallocation
204 virtual void _M_destroy() = 0;
208 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
213 virtual ~_Result_base();
217 template<typename _Res>
218 struct _Result : _Result_base
221 typedef alignment_of<_Res> __a_of;
222 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
223 typedef typename __align_storage::type __align_type;
225 __align_type _M_storage;
229 _Result() noexcept : _M_initialized() { }
237 // Return lvalue, future will add const or rvalue-reference
239 _M_value() noexcept { return *static_cast<_Res*>(_M_addr()); }
242 _M_set(const _Res& __res)
244 ::new (_M_addr()) _Res(__res);
245 _M_initialized = true;
251 ::new (_M_addr()) _Res(std::move(__res));
252 _M_initialized = true;
256 void _M_destroy() { delete this; }
258 void* _M_addr() noexcept { return static_cast<void*>(&_M_storage); }
261 // TODO: use template alias when available
263 template<typename _Res>
264 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
266 /// A unique_ptr based on the instantiating type.
267 template<typename _Res>
270 typedef unique_ptr<_Res, _Result_base::_Deleter> type;
274 template<typename _Res, typename _Alloc>
275 struct _Result_alloc : _Result<_Res>, _Alloc
277 typedef typename _Alloc::template rebind<_Result_alloc>::other
281 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
287 __allocator_type __a(*this);
289 __a.deallocate(this, 1);
293 template<typename _Res, typename _Allocator>
294 static typename _Ptr<_Result_alloc<_Res, _Allocator>>::type
295 _S_allocate_result(const _Allocator& __a)
297 typedef _Result_alloc<_Res, _Allocator> __result_type;
298 typename __result_type::__allocator_type __a2(__a);
299 __result_type* __p = __a2.allocate(1);
302 __a2.construct(__p, __a);
306 __a2.deallocate(__p, 1);
307 __throw_exception_again;
309 return typename _Ptr<__result_type>::type(__p);
313 /// Base class for state between a promise and one or more
314 /// associated futures.
317 typedef _Ptr<_Result_base>::type _Ptr_type;
321 condition_variable _M_cond;
322 atomic_flag _M_retrieved;
326 _State_base() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
327 _State_base(const _State_base&) = delete;
328 _State_base& operator=(const _State_base&) = delete;
329 virtual ~_State_base();
335 unique_lock<mutex> __lock(_M_mutex);
337 _M_cond.wait(__lock, std::bind<bool>(&_State_base::_M_ready, this));
341 template<typename _Rep, typename _Period>
343 wait_for(const chrono::duration<_Rep, _Period>& __rel)
345 unique_lock<mutex> __lock(_M_mutex);
346 auto __bound = std::bind<bool>(&_State_base::_M_ready, this);
347 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
350 template<typename _Clock, typename _Duration>
352 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
354 unique_lock<mutex> __lock(_M_mutex);
355 auto __bound = std::bind<bool>(&_State_base::_M_ready, this);
356 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
360 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
362 bool __set = __ignore_failure;
363 // all calls to this function are serialized,
364 // side-effects of invoking __res only happen once
365 call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
368 __throw_future_error(int(future_errc::promise_already_satisfied));
372 _M_break_promise(_Ptr_type __res)
374 if (static_cast<bool>(__res))
376 error_code __ec(make_error_code(future_errc::broken_promise));
377 __res->_M_error = copy_exception(future_error(__ec));
379 lock_guard<mutex> __lock(_M_mutex);
380 _M_result.swap(__res);
382 _M_cond.notify_all();
386 // Called when this object is passed to a future.
388 _M_set_retrieved_flag()
390 if (_M_retrieved.test_and_set())
391 __throw_future_error(int(future_errc::future_already_retrieved));
394 template<typename _Res, typename _Arg>
398 template<typename _Res, typename _Arg>
399 struct _Setter<_Res, _Arg&>
401 // check this is only used by promise<R>::set_value(const R&)
402 // or promise<R>::set_value(R&)
403 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
404 || is_same<const _Res, _Arg>::value, // promise<R>
405 "Invalid specialisation");
407 typename promise<_Res>::_Ptr_type operator()()
409 _State_base::_S_check(_M_promise->_M_future);
410 _M_promise->_M_storage->_M_set(_M_arg);
411 return std::move(_M_promise->_M_storage);
413 promise<_Res>* _M_promise;
418 template<typename _Res>
419 struct _Setter<_Res, _Res&&>
421 typename promise<_Res>::_Ptr_type operator()()
423 _State_base::_S_check(_M_promise->_M_future);
424 _M_promise->_M_storage->_M_set(std::move(_M_arg));
425 return std::move(_M_promise->_M_storage);
427 promise<_Res>* _M_promise;
431 struct __exception_ptr_tag { };
434 template<typename _Res>
435 struct _Setter<_Res, __exception_ptr_tag>
437 typename promise<_Res>::_Ptr_type operator()()
439 _State_base::_S_check(_M_promise->_M_future);
440 _M_promise->_M_storage->_M_error = _M_ex;
441 return std::move(_M_promise->_M_storage);
444 promise<_Res>* _M_promise;
445 exception_ptr& _M_ex;
448 template<typename _Res, typename _Arg>
449 static _Setter<_Res, _Arg&&>
450 __setter(promise<_Res>* __prom, _Arg&& __arg)
452 return _Setter<_Res, _Arg&&>{ __prom, __arg };
455 template<typename _Res>
456 static _Setter<_Res, __exception_ptr_tag>
457 __setter(exception_ptr& __ex, promise<_Res>* __prom)
459 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
462 static _Setter<void, void>
463 __setter(promise<void>* __prom);
465 template<typename _Tp>
467 _S_check(const shared_ptr<_Tp>& __p)
469 if (!static_cast<bool>(__p))
470 __throw_future_error((int)future_errc::no_state);
475 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
477 _Ptr_type __res = __f();
479 lock_guard<mutex> __lock(_M_mutex);
480 _M_result.swap(__res);
482 _M_cond.notify_all();
486 bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
488 virtual void _M_run_deferred() { }
491 template<typename _BoundFn, typename = typename _BoundFn::result_type>
492 class _Deferred_state;
494 template<typename _BoundFn, typename = typename _BoundFn::result_type>
497 template<typename _Signature>
500 template<typename _BoundFn>
501 static std::shared_ptr<_State_base>
502 _S_make_deferred_state(_BoundFn&& __fn);
504 template<typename _BoundFn>
505 static std::shared_ptr<_State_base>
506 _S_make_async_state(_BoundFn&& __fn);
508 template<typename _Res_ptr, typename _Res>
511 template<typename _Res_ptr, typename _BoundFn>
512 class _Task_setter_helper
514 typedef typename remove_reference<_BoundFn>::type::result_type __res;
516 typedef _Task_setter<_Res_ptr, __res> __type;
519 template<typename _Res_ptr, typename _BoundFn>
520 static typename _Task_setter_helper<_Res_ptr, _BoundFn>::__type
521 _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
523 typedef _Task_setter_helper<_Res_ptr, _BoundFn> __helper_type;
524 typedef typename __helper_type::__type _Setter;
525 return _Setter{ __ptr, std::ref(__call) };
529 /// Partial specialization for reference types.
530 template<typename _Res>
531 struct __future_base::_Result<_Res&> : __future_base::_Result_base
533 _Result() noexcept : _M_value_ptr() { }
535 void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
537 _Res& _M_get() noexcept { return *_M_value_ptr; }
542 void _M_destroy() { delete this; }
545 /// Explicit specialization for void.
547 struct __future_base::_Result<void> : __future_base::_Result_base
550 void _M_destroy() { delete this; }
554 /// Common implementation for future and shared_future.
555 template<typename _Res>
556 class __basic_future : public __future_base
559 typedef shared_ptr<_State_base> __state_type;
560 typedef __future_base::_Result<_Res>& __result_type;
563 __state_type _M_state;
567 __basic_future(const __basic_future&) = delete;
568 __basic_future& operator=(const __basic_future&) = delete;
571 valid() const noexcept { return static_cast<bool>(_M_state); }
576 _State_base::_S_check(_M_state);
580 template<typename _Rep, typename _Period>
582 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
584 _State_base::_S_check(_M_state);
585 return _M_state->wait_for(__rel);
588 template<typename _Clock, typename _Duration>
590 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
592 _State_base::_S_check(_M_state);
593 return _M_state->wait_until(__abs);
597 /// Wait for the state to be ready and rethrow any stored exception
601 _State_base::_S_check(_M_state);
602 _Result_base& __res = _M_state->wait();
603 if (!(__res._M_error == 0))
604 rethrow_exception(__res._M_error);
605 return static_cast<__result_type>(__res);
608 void _M_swap(__basic_future& __that) noexcept
610 _M_state.swap(__that._M_state);
613 // Construction of a future by promise::get_future()
615 __basic_future(const __state_type& __state) : _M_state(__state)
617 _State_base::_S_check(_M_state);
618 _M_state->_M_set_retrieved_flag();
621 // Copy construction from a shared_future
623 __basic_future(const shared_future<_Res>&) noexcept;
625 // Move construction from a shared_future
627 __basic_future(shared_future<_Res>&&) noexcept;
629 // Move construction from a future
631 __basic_future(future<_Res>&&) noexcept;
633 constexpr __basic_future() noexcept : _M_state() { }
637 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
638 ~_Reset() { _M_fut._M_state.reset(); }
639 __basic_future& _M_fut;
644 /// Primary template for future.
645 template<typename _Res>
646 class future : public __basic_future<_Res>
648 friend class promise<_Res>;
649 template<typename> friend class packaged_task;
650 template<typename _Fn, typename... _Args>
651 friend future<typename result_of<_Fn(_Args...)>::type>
652 async(launch, _Fn&&, _Args&&...);
654 typedef __basic_future<_Res> _Base_type;
655 typedef typename _Base_type::__state_type __state_type;
658 future(const __state_type& __state) : _Base_type(__state) { }
661 constexpr future() noexcept : _Base_type() { }
664 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
667 future(const future&) = delete;
668 future& operator=(const future&) = delete;
670 future& operator=(future&& __fut) noexcept
672 future(std::move(__fut))._M_swap(*this);
676 /// Retrieving the value
680 typename _Base_type::_Reset __reset(*this);
681 return std::move(this->_M_get_result()._M_value());
684 shared_future<_Res> share();
687 /// Partial specialization for future<R&>
688 template<typename _Res>
689 class future<_Res&> : public __basic_future<_Res&>
691 friend class promise<_Res&>;
692 template<typename> friend class packaged_task;
693 template<typename _Fn, typename... _Args>
694 friend future<typename result_of<_Fn(_Args...)>::type>
695 async(launch, _Fn&&, _Args&&...);
697 typedef __basic_future<_Res&> _Base_type;
698 typedef typename _Base_type::__state_type __state_type;
701 future(const __state_type& __state) : _Base_type(__state) { }
704 constexpr future() noexcept : _Base_type() { }
707 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
710 future(const future&) = delete;
711 future& operator=(const future&) = delete;
713 future& operator=(future&& __fut) noexcept
715 future(std::move(__fut))._M_swap(*this);
719 /// Retrieving the value
723 typename _Base_type::_Reset __reset(*this);
724 return this->_M_get_result()._M_get();
727 shared_future<_Res&> share();
730 /// Explicit specialization for future<void>
732 class future<void> : public __basic_future<void>
734 friend class promise<void>;
735 template<typename> friend class packaged_task;
736 template<typename _Fn, typename... _Args>
737 friend future<typename result_of<_Fn(_Args...)>::type>
738 async(launch, _Fn&&, _Args&&...);
740 typedef __basic_future<void> _Base_type;
741 typedef typename _Base_type::__state_type __state_type;
744 future(const __state_type& __state) : _Base_type(__state) { }
747 constexpr future() noexcept : _Base_type() { }
750 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
753 future(const future&) = delete;
754 future& operator=(const future&) = delete;
756 future& operator=(future&& __fut) noexcept
758 future(std::move(__fut))._M_swap(*this);
762 /// Retrieving the value
766 typename _Base_type::_Reset __reset(*this);
767 this->_M_get_result();
770 shared_future<void> share();
774 /// Primary template for shared_future.
775 template<typename _Res>
776 class shared_future : public __basic_future<_Res>
778 typedef __basic_future<_Res> _Base_type;
781 constexpr shared_future() noexcept : _Base_type() { }
784 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
786 /// Construct from a future rvalue
787 shared_future(future<_Res>&& __uf) noexcept
788 : _Base_type(std::move(__uf))
791 /// Construct from a shared_future rvalue
792 shared_future(shared_future&& __sf) noexcept
793 : _Base_type(std::move(__sf))
796 shared_future& operator=(const shared_future& __sf)
798 shared_future(__sf)._M_swap(*this);
802 shared_future& operator=(shared_future&& __sf) noexcept
804 shared_future(std::move(__sf))._M_swap(*this);
808 /// Retrieving the value
812 typename _Base_type::__result_type __r = this->_M_get_result();
813 _Res& __rs(__r._M_value());
818 /// Partial specialization for shared_future<R&>
819 template<typename _Res>
820 class shared_future<_Res&> : public __basic_future<_Res&>
822 typedef __basic_future<_Res&> _Base_type;
825 constexpr shared_future() noexcept : _Base_type() { }
828 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
830 /// Construct from a future rvalue
831 shared_future(future<_Res&>&& __uf) noexcept
832 : _Base_type(std::move(__uf))
835 /// Construct from a shared_future rvalue
836 shared_future(shared_future&& __sf) noexcept
837 : _Base_type(std::move(__sf))
840 shared_future& operator=(const shared_future& __sf)
842 shared_future(__sf)._M_swap(*this);
846 shared_future& operator=(shared_future&& __sf) noexcept
848 shared_future(std::move(__sf))._M_swap(*this);
852 /// Retrieving the value
854 get() { return this->_M_get_result()._M_get(); }
857 /// Explicit specialization for shared_future<void>
859 class shared_future<void> : public __basic_future<void>
861 typedef __basic_future<void> _Base_type;
864 constexpr shared_future() noexcept : _Base_type() { }
867 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
869 /// Construct from a future rvalue
870 shared_future(future<void>&& __uf) noexcept
871 : _Base_type(std::move(__uf))
874 /// Construct from a shared_future rvalue
875 shared_future(shared_future&& __sf) noexcept
876 : _Base_type(std::move(__sf))
879 shared_future& operator=(const shared_future& __sf)
881 shared_future(__sf)._M_swap(*this);
885 shared_future& operator=(shared_future&& __sf) noexcept
887 shared_future(std::move(__sf))._M_swap(*this);
891 // Retrieving the value
893 get() { this->_M_get_result(); }
896 // Now we can define the protected __basic_future constructors.
897 template<typename _Res>
898 inline __basic_future<_Res>::
899 __basic_future(const shared_future<_Res>& __sf) noexcept
900 : _M_state(__sf._M_state)
903 template<typename _Res>
904 inline __basic_future<_Res>::
905 __basic_future(shared_future<_Res>&& __sf) noexcept
906 : _M_state(std::move(__sf._M_state))
909 template<typename _Res>
910 inline __basic_future<_Res>::
911 __basic_future(future<_Res>&& __uf) noexcept
912 : _M_state(std::move(__uf._M_state))
915 template<typename _Res>
916 inline shared_future<_Res>
917 future<_Res>::share()
918 { return shared_future<_Res>(std::move(*this)); }
920 template<typename _Res>
921 inline shared_future<_Res&>
922 future<_Res&>::share()
923 { return shared_future<_Res&>(std::move(*this)); }
925 inline shared_future<void>
926 future<void>::share()
927 { return shared_future<void>(std::move(*this)); }
929 /// Primary template for promise
930 template<typename _Res>
933 typedef __future_base::_State_base _State;
934 typedef __future_base::_Result<_Res> _Res_type;
935 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
936 template<typename, typename> friend class _State::_Setter;
938 shared_ptr<_State> _M_future;
939 _Ptr_type _M_storage;
943 : _M_future(std::make_shared<_State>()),
944 _M_storage(new _Res_type())
947 promise(promise&& __rhs) noexcept
948 : _M_future(std::move(__rhs._M_future)),
949 _M_storage(std::move(__rhs._M_storage))
952 template<typename _Allocator>
953 promise(allocator_arg_t, const _Allocator& __a)
954 : _M_future(std::allocate_shared<_State>(__a)),
955 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
958 promise(const promise&) = delete;
962 if (static_cast<bool>(_M_future) && !_M_future.unique())
963 _M_future->_M_break_promise(std::move(_M_storage));
968 operator=(promise&& __rhs) noexcept
970 promise(std::move(__rhs)).swap(*this);
974 promise& operator=(const promise&) = delete;
977 swap(promise& __rhs) noexcept
979 _M_future.swap(__rhs._M_future);
980 _M_storage.swap(__rhs._M_storage);
983 // Retrieving the result
986 { return future<_Res>(_M_future); }
988 // Setting the result
990 set_value(const _Res& __r)
992 auto __setter = _State::__setter(this, __r);
993 _M_future->_M_set_result(std::move(__setter));
997 set_value(_Res&& __r)
999 auto __setter = _State::__setter(this, std::move(__r));
1000 _M_future->_M_set_result(std::move(__setter));
1004 set_exception(exception_ptr __p)
1006 auto __setter = _State::__setter(__p, this);
1007 _M_future->_M_set_result(std::move(__setter));
1011 template<typename _Res>
1013 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1016 template<typename _Res, typename _Alloc>
1017 struct uses_allocator<promise<_Res>, _Alloc>
1018 : public true_type { };
1021 /// Partial specialization for promise<R&>
1022 template<typename _Res>
1023 class promise<_Res&>
1025 typedef __future_base::_State_base _State;
1026 typedef __future_base::_Result<_Res&> _Res_type;
1027 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
1028 template<typename, typename> friend class _State::_Setter;
1030 shared_ptr<_State> _M_future;
1031 _Ptr_type _M_storage;
1035 : _M_future(std::make_shared<_State>()),
1036 _M_storage(new _Res_type())
1039 promise(promise&& __rhs) noexcept
1040 : _M_future(std::move(__rhs._M_future)),
1041 _M_storage(std::move(__rhs._M_storage))
1044 template<typename _Allocator>
1045 promise(allocator_arg_t, const _Allocator& __a)
1046 : _M_future(std::allocate_shared<_State>(__a)),
1047 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1050 promise(const promise&) = delete;
1054 if (static_cast<bool>(_M_future) && !_M_future.unique())
1055 _M_future->_M_break_promise(std::move(_M_storage));
1060 operator=(promise&& __rhs) noexcept
1062 promise(std::move(__rhs)).swap(*this);
1066 promise& operator=(const promise&) = delete;
1069 swap(promise& __rhs) noexcept
1071 _M_future.swap(__rhs._M_future);
1072 _M_storage.swap(__rhs._M_storage);
1075 // Retrieving the result
1078 { return future<_Res&>(_M_future); }
1080 // Setting the result
1082 set_value(_Res& __r)
1084 auto __setter = _State::__setter(this, __r);
1085 _M_future->_M_set_result(std::move(__setter));
1089 set_exception(exception_ptr __p)
1091 auto __setter = _State::__setter(__p, this);
1092 _M_future->_M_set_result(std::move(__setter));
1096 /// Explicit specialization for promise<void>
1100 typedef __future_base::_State_base _State;
1101 typedef __future_base::_Result<void> _Res_type;
1102 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
1103 template<typename, typename> friend class _State::_Setter;
1105 shared_ptr<_State> _M_future;
1106 _Ptr_type _M_storage;
1110 : _M_future(std::make_shared<_State>()),
1111 _M_storage(new _Res_type())
1114 promise(promise&& __rhs) noexcept
1115 : _M_future(std::move(__rhs._M_future)),
1116 _M_storage(std::move(__rhs._M_storage))
1119 template<typename _Allocator>
1120 promise(allocator_arg_t, const _Allocator& __a)
1121 : _M_future(std::allocate_shared<_State>(__a)),
1122 _M_storage(__future_base::_S_allocate_result<void>(__a))
1125 promise(const promise&) = delete;
1129 if (static_cast<bool>(_M_future) && !_M_future.unique())
1130 _M_future->_M_break_promise(std::move(_M_storage));
1135 operator=(promise&& __rhs) noexcept
1137 promise(std::move(__rhs)).swap(*this);
1141 promise& operator=(const promise&) = delete;
1144 swap(promise& __rhs) noexcept
1146 _M_future.swap(__rhs._M_future);
1147 _M_storage.swap(__rhs._M_storage);
1150 // Retrieving the result
1153 { return future<void>(_M_future); }
1155 // Setting the result
1159 set_exception(exception_ptr __p)
1161 auto __setter = _State::__setter(__p, this);
1162 _M_future->_M_set_result(std::move(__setter));
1168 struct __future_base::_State_base::_Setter<void, void>
1170 promise<void>::_Ptr_type operator()()
1172 _State_base::_S_check(_M_promise->_M_future);
1173 return std::move(_M_promise->_M_storage);
1176 promise<void>* _M_promise;
1179 inline __future_base::_State_base::_Setter<void, void>
1180 __future_base::_State_base::__setter(promise<void>* __prom)
1182 return _Setter<void, void>{ __prom };
1186 promise<void>::set_value()
1188 auto __setter = _State::__setter(this);
1189 _M_future->_M_set_result(std::move(__setter));
1193 template<typename _Ptr_type, typename _Res>
1194 struct __future_base::_Task_setter
1196 _Ptr_type operator()()
1200 _M_result->_M_set(_M_fn());
1204 _M_result->_M_error = current_exception();
1206 return std::move(_M_result);
1208 _Ptr_type& _M_result;
1209 std::function<_Res()> _M_fn;
1212 template<typename _Ptr_type>
1213 struct __future_base::_Task_setter<_Ptr_type, void>
1215 _Ptr_type operator()()
1223 _M_result->_M_error = current_exception();
1225 return std::move(_M_result);
1227 _Ptr_type& _M_result;
1228 std::function<void()> _M_fn;
1231 template<typename _Res, typename... _Args>
1232 struct __future_base::_Task_state<_Res(_Args...)>
1233 : __future_base::_State_base
1235 typedef _Res _Res_type;
1237 _Task_state(std::function<_Res(_Args...)> __task)
1238 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1241 template<typename _Func, typename _Alloc>
1242 _Task_state(_Func&& __task, const _Alloc& __a)
1243 : _M_result(_S_allocate_result<_Res>(__a)),
1244 _M_task(allocator_arg, __a, std::move(__task))
1248 _M_run(_Args... __args)
1250 // bound arguments decay so wrap lvalue references
1251 auto __boundfn = std::__bind_simple(std::ref(_M_task),
1252 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1253 auto __setter = _S_task_setter(_M_result, std::move(__boundfn));
1254 _M_set_result(std::move(__setter));
1257 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1258 _Ptr_type _M_result;
1259 std::function<_Res(_Args...)> _M_task;
1261 template<typename _Tp>
1262 static reference_wrapper<_Tp>
1263 _S_maybe_wrap_ref(_Tp& __t)
1264 { return std::ref(__t); }
1266 template<typename _Tp>
1267 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1269 _S_maybe_wrap_ref(_Tp&& __t)
1270 { return std::forward<_Tp>(__t); }
1274 template<typename _Res, typename... _ArgTypes>
1275 class packaged_task<_Res(_ArgTypes...)>
1277 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1278 shared_ptr<_State_type> _M_state;
1281 // Construction and destruction
1282 packaged_task() noexcept { }
1284 template<typename _Fn>
1286 packaged_task(_Fn&& __fn)
1287 : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn)))
1290 template<typename _Fn, typename _Allocator>
1292 packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn)
1293 : _M_state(std::allocate_shared<_State_type>(__a,
1294 std::forward<_Fn>(__fn)))
1299 if (static_cast<bool>(_M_state) && !_M_state.unique())
1300 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1304 packaged_task(packaged_task&) = delete;
1305 packaged_task& operator=(packaged_task&) = delete;
1308 packaged_task(packaged_task&& __other) noexcept
1309 { this->swap(__other); }
1311 packaged_task& operator=(packaged_task&& __other) noexcept
1313 packaged_task(std::move(__other)).swap(*this);
1318 swap(packaged_task& __other) noexcept
1319 { _M_state.swap(__other._M_state); }
1322 valid() const noexcept
1323 { return static_cast<bool>(_M_state); }
1328 { return future<_Res>(_M_state); }
1332 operator()(_ArgTypes... __args)
1334 __future_base::_State_base::_S_check(_M_state);
1335 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1341 __future_base::_State_base::_S_check(_M_state);
1342 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1347 template<typename _Res, typename... _ArgTypes>
1349 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1350 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1353 template<typename _Res, typename _Alloc>
1354 struct uses_allocator<packaged_task<_Res>, _Alloc>
1355 : public true_type { };
1358 template<typename _BoundFn, typename _Res>
1359 class __future_base::_Deferred_state : public __future_base::_State_base
1363 _Deferred_state(_BoundFn&& __fn)
1364 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1368 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1369 _Ptr_type _M_result;
1375 // safe to call multiple times so ignore failure
1376 _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1380 template<typename _BoundFn, typename _Res>
1381 class __future_base::_Async_state : public __future_base::_State_base
1385 _Async_state(_BoundFn&& __fn)
1386 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1387 _M_thread(mem_fn(&_Async_state::_M_do_run), this)
1390 ~_Async_state() { _M_thread.join(); }
1395 _M_set_result(_S_task_setter(_M_result, _M_fn));
1398 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1399 _Ptr_type _M_result;
1404 template<typename _BoundFn>
1405 inline std::shared_ptr<__future_base::_State_base>
1406 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1408 typedef typename remove_reference<_BoundFn>::type __fn_type;
1409 typedef _Deferred_state<__fn_type> __state_type;
1410 return std::make_shared<__state_type>(std::move(__fn));
1413 template<typename _BoundFn>
1414 inline std::shared_ptr<__future_base::_State_base>
1415 __future_base::_S_make_async_state(_BoundFn&& __fn)
1417 typedef typename remove_reference<_BoundFn>::type __fn_type;
1418 typedef _Async_state<__fn_type> __state_type;
1419 return std::make_shared<__state_type>(std::move(__fn));
1424 template<typename _Fn, typename... _Args>
1425 future<typename result_of<_Fn(_Args...)>::type>
1426 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1428 typedef typename result_of<_Fn(_Args...)>::type result_type;
1429 std::shared_ptr<__future_base::_State_base> __state;
1430 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1432 __state = __future_base::_S_make_async_state(std::__bind_simple(
1433 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1437 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1438 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1440 return future<result_type>(__state);
1443 /// async, potential overload
1444 template<typename _Fn, typename... _Args>
1446 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
1447 async(_Fn&& __fn, _Args&&... __args)
1449 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1450 std::forward<_Args>(__args)...);
1453 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1454 // && _GLIBCXX_ATOMIC_BUILTINS_4
1457 _GLIBCXX_END_NAMESPACE_VERSION
1460 #endif // __GXX_EXPERIMENTAL_CXX0X__
1462 #endif // _GLIBCXX_FUTURE