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 && (ATOMIC_INT_LOCK_FREE > 1)
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 /// A unique_ptr based on the instantiating type.
262 template<typename _Res>
263 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
266 template<typename _Res, typename _Alloc>
267 struct _Result_alloc final : _Result<_Res>, _Alloc
269 typedef typename allocator_traits<_Alloc>::template
270 rebind_alloc<_Result_alloc> __allocator_type;
273 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
279 typedef allocator_traits<__allocator_type> __traits;
280 __allocator_type __a(*this);
281 __traits::destroy(__a, this);
282 __traits::deallocate(__a, this, 1);
286 template<typename _Res, typename _Allocator>
287 static _Ptr<_Result_alloc<_Res, _Allocator>>
288 _S_allocate_result(const _Allocator& __a)
290 typedef _Result_alloc<_Res, _Allocator> __result_type;
291 typedef allocator_traits<typename __result_type::__allocator_type>
293 typename __traits::allocator_type __a2(__a);
294 __result_type* __p = __traits::allocate(__a2, 1);
297 __traits::construct(__a2, __p, __a);
301 __traits::deallocate(__a2, __p, 1);
302 __throw_exception_again;
304 return _Ptr<__result_type>(__p);
308 /// Base class for state between a promise and one or more
309 /// associated futures.
312 typedef _Ptr<_Result_base> _Ptr_type;
316 condition_variable _M_cond;
317 atomic_flag _M_retrieved;
321 _State_base() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
322 _State_base(const _State_base&) = delete;
323 _State_base& operator=(const _State_base&) = delete;
324 virtual ~_State_base();
330 unique_lock<mutex> __lock(_M_mutex);
332 _M_cond.wait(__lock, std::bind<bool>(&_State_base::_M_ready, this));
336 template<typename _Rep, typename _Period>
338 wait_for(const chrono::duration<_Rep, _Period>& __rel)
340 unique_lock<mutex> __lock(_M_mutex);
341 auto __bound = std::bind<bool>(&_State_base::_M_ready, this);
342 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
345 template<typename _Clock, typename _Duration>
347 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
349 unique_lock<mutex> __lock(_M_mutex);
350 auto __bound = std::bind<bool>(&_State_base::_M_ready, this);
351 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
355 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
357 bool __set = __ignore_failure;
358 // all calls to this function are serialized,
359 // side-effects of invoking __res only happen once
360 call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
363 __throw_future_error(int(future_errc::promise_already_satisfied));
367 _M_break_promise(_Ptr_type __res)
369 if (static_cast<bool>(__res))
371 error_code __ec(make_error_code(future_errc::broken_promise));
372 __res->_M_error = copy_exception(future_error(__ec));
374 lock_guard<mutex> __lock(_M_mutex);
375 _M_result.swap(__res);
377 _M_cond.notify_all();
381 // Called when this object is passed to a future.
383 _M_set_retrieved_flag()
385 if (_M_retrieved.test_and_set())
386 __throw_future_error(int(future_errc::future_already_retrieved));
389 template<typename _Res, typename _Arg>
393 template<typename _Res, typename _Arg>
394 struct _Setter<_Res, _Arg&>
396 // check this is only used by promise<R>::set_value(const R&)
397 // or promise<R>::set_value(R&)
398 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
399 || is_same<const _Res, _Arg>::value, // promise<R>
400 "Invalid specialisation");
402 typename promise<_Res>::_Ptr_type operator()()
404 _State_base::_S_check(_M_promise->_M_future);
405 _M_promise->_M_storage->_M_set(_M_arg);
406 return std::move(_M_promise->_M_storage);
408 promise<_Res>* _M_promise;
413 template<typename _Res>
414 struct _Setter<_Res, _Res&&>
416 typename promise<_Res>::_Ptr_type operator()()
418 _State_base::_S_check(_M_promise->_M_future);
419 _M_promise->_M_storage->_M_set(std::move(_M_arg));
420 return std::move(_M_promise->_M_storage);
422 promise<_Res>* _M_promise;
426 struct __exception_ptr_tag { };
429 template<typename _Res>
430 struct _Setter<_Res, __exception_ptr_tag>
432 typename promise<_Res>::_Ptr_type operator()()
434 _State_base::_S_check(_M_promise->_M_future);
435 _M_promise->_M_storage->_M_error = _M_ex;
436 return std::move(_M_promise->_M_storage);
439 promise<_Res>* _M_promise;
440 exception_ptr& _M_ex;
443 template<typename _Res, typename _Arg>
444 static _Setter<_Res, _Arg&&>
445 __setter(promise<_Res>* __prom, _Arg&& __arg)
447 return _Setter<_Res, _Arg&&>{ __prom, __arg };
450 template<typename _Res>
451 static _Setter<_Res, __exception_ptr_tag>
452 __setter(exception_ptr& __ex, promise<_Res>* __prom)
454 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
457 static _Setter<void, void>
458 __setter(promise<void>* __prom);
460 template<typename _Tp>
462 _S_check(const shared_ptr<_Tp>& __p)
464 if (!static_cast<bool>(__p))
465 __throw_future_error((int)future_errc::no_state);
470 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
472 _Ptr_type __res = __f();
474 lock_guard<mutex> __lock(_M_mutex);
475 _M_result.swap(__res);
477 _M_cond.notify_all();
481 bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
483 virtual void _M_run_deferred() { }
486 template<typename _BoundFn, typename = typename _BoundFn::result_type>
487 class _Deferred_state;
489 template<typename _BoundFn, typename = typename _BoundFn::result_type>
492 template<typename _Signature>
495 template<typename _BoundFn>
496 static std::shared_ptr<_State_base>
497 _S_make_deferred_state(_BoundFn&& __fn);
499 template<typename _BoundFn>
500 static std::shared_ptr<_State_base>
501 _S_make_async_state(_BoundFn&& __fn);
503 template<typename _Res_ptr, typename _Res>
506 template<typename _Res_ptr, typename _BoundFn>
507 class _Task_setter_helper
509 typedef typename remove_reference<_BoundFn>::type::result_type __res;
511 typedef _Task_setter<_Res_ptr, __res> __type;
514 template<typename _Res_ptr, typename _BoundFn>
515 static typename _Task_setter_helper<_Res_ptr, _BoundFn>::__type
516 _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
518 typedef _Task_setter_helper<_Res_ptr, _BoundFn> __helper_type;
519 typedef typename __helper_type::__type _Setter;
520 return _Setter{ __ptr, std::ref(__call) };
524 /// Partial specialization for reference types.
525 template<typename _Res>
526 struct __future_base::_Result<_Res&> : __future_base::_Result_base
528 _Result() noexcept : _M_value_ptr() { }
530 void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
532 _Res& _M_get() noexcept { return *_M_value_ptr; }
537 void _M_destroy() { delete this; }
540 /// Explicit specialization for void.
542 struct __future_base::_Result<void> : __future_base::_Result_base
545 void _M_destroy() { delete this; }
549 /// Common implementation for future and shared_future.
550 template<typename _Res>
551 class __basic_future : public __future_base
554 typedef shared_ptr<_State_base> __state_type;
555 typedef __future_base::_Result<_Res>& __result_type;
558 __state_type _M_state;
562 __basic_future(const __basic_future&) = delete;
563 __basic_future& operator=(const __basic_future&) = delete;
566 valid() const noexcept { return static_cast<bool>(_M_state); }
571 _State_base::_S_check(_M_state);
575 template<typename _Rep, typename _Period>
577 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
579 _State_base::_S_check(_M_state);
580 return _M_state->wait_for(__rel);
583 template<typename _Clock, typename _Duration>
585 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
587 _State_base::_S_check(_M_state);
588 return _M_state->wait_until(__abs);
592 /// Wait for the state to be ready and rethrow any stored exception
596 _State_base::_S_check(_M_state);
597 _Result_base& __res = _M_state->wait();
598 if (!(__res._M_error == 0))
599 rethrow_exception(__res._M_error);
600 return static_cast<__result_type>(__res);
603 void _M_swap(__basic_future& __that) noexcept
605 _M_state.swap(__that._M_state);
608 // Construction of a future by promise::get_future()
610 __basic_future(const __state_type& __state) : _M_state(__state)
612 _State_base::_S_check(_M_state);
613 _M_state->_M_set_retrieved_flag();
616 // Copy construction from a shared_future
618 __basic_future(const shared_future<_Res>&) noexcept;
620 // Move construction from a shared_future
622 __basic_future(shared_future<_Res>&&) noexcept;
624 // Move construction from a future
626 __basic_future(future<_Res>&&) noexcept;
628 constexpr __basic_future() noexcept : _M_state() { }
632 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
633 ~_Reset() { _M_fut._M_state.reset(); }
634 __basic_future& _M_fut;
639 /// Primary template for future.
640 template<typename _Res>
641 class future : public __basic_future<_Res>
643 friend class promise<_Res>;
644 template<typename> friend class packaged_task;
645 template<typename _Fn, typename... _Args>
646 friend future<typename result_of<_Fn(_Args...)>::type>
647 async(launch, _Fn&&, _Args&&...);
649 typedef __basic_future<_Res> _Base_type;
650 typedef typename _Base_type::__state_type __state_type;
653 future(const __state_type& __state) : _Base_type(__state) { }
656 constexpr future() noexcept : _Base_type() { }
659 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
662 future(const future&) = delete;
663 future& operator=(const future&) = delete;
665 future& operator=(future&& __fut) noexcept
667 future(std::move(__fut))._M_swap(*this);
671 /// Retrieving the value
675 typename _Base_type::_Reset __reset(*this);
676 return std::move(this->_M_get_result()._M_value());
679 shared_future<_Res> share();
682 /// Partial specialization for future<R&>
683 template<typename _Res>
684 class future<_Res&> : public __basic_future<_Res&>
686 friend class promise<_Res&>;
687 template<typename> friend class packaged_task;
688 template<typename _Fn, typename... _Args>
689 friend future<typename result_of<_Fn(_Args...)>::type>
690 async(launch, _Fn&&, _Args&&...);
692 typedef __basic_future<_Res&> _Base_type;
693 typedef typename _Base_type::__state_type __state_type;
696 future(const __state_type& __state) : _Base_type(__state) { }
699 constexpr future() noexcept : _Base_type() { }
702 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
705 future(const future&) = delete;
706 future& operator=(const future&) = delete;
708 future& operator=(future&& __fut) noexcept
710 future(std::move(__fut))._M_swap(*this);
714 /// Retrieving the value
718 typename _Base_type::_Reset __reset(*this);
719 return this->_M_get_result()._M_get();
722 shared_future<_Res&> share();
725 /// Explicit specialization for future<void>
727 class future<void> : public __basic_future<void>
729 friend class promise<void>;
730 template<typename> friend class packaged_task;
731 template<typename _Fn, typename... _Args>
732 friend future<typename result_of<_Fn(_Args...)>::type>
733 async(launch, _Fn&&, _Args&&...);
735 typedef __basic_future<void> _Base_type;
736 typedef typename _Base_type::__state_type __state_type;
739 future(const __state_type& __state) : _Base_type(__state) { }
742 constexpr future() noexcept : _Base_type() { }
745 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
748 future(const future&) = delete;
749 future& operator=(const future&) = delete;
751 future& operator=(future&& __fut) noexcept
753 future(std::move(__fut))._M_swap(*this);
757 /// Retrieving the value
761 typename _Base_type::_Reset __reset(*this);
762 this->_M_get_result();
765 shared_future<void> share();
769 /// Primary template for shared_future.
770 template<typename _Res>
771 class shared_future : public __basic_future<_Res>
773 typedef __basic_future<_Res> _Base_type;
776 constexpr shared_future() noexcept : _Base_type() { }
779 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
781 /// Construct from a future rvalue
782 shared_future(future<_Res>&& __uf) noexcept
783 : _Base_type(std::move(__uf))
786 /// Construct from a shared_future rvalue
787 shared_future(shared_future&& __sf) noexcept
788 : _Base_type(std::move(__sf))
791 shared_future& operator=(const shared_future& __sf)
793 shared_future(__sf)._M_swap(*this);
797 shared_future& operator=(shared_future&& __sf) noexcept
799 shared_future(std::move(__sf))._M_swap(*this);
803 /// Retrieving the value
807 typename _Base_type::__result_type __r = this->_M_get_result();
808 _Res& __rs(__r._M_value());
813 /// Partial specialization for shared_future<R&>
814 template<typename _Res>
815 class shared_future<_Res&> : public __basic_future<_Res&>
817 typedef __basic_future<_Res&> _Base_type;
820 constexpr shared_future() noexcept : _Base_type() { }
823 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
825 /// Construct from a future rvalue
826 shared_future(future<_Res&>&& __uf) noexcept
827 : _Base_type(std::move(__uf))
830 /// Construct from a shared_future rvalue
831 shared_future(shared_future&& __sf) noexcept
832 : _Base_type(std::move(__sf))
835 shared_future& operator=(const shared_future& __sf)
837 shared_future(__sf)._M_swap(*this);
841 shared_future& operator=(shared_future&& __sf) noexcept
843 shared_future(std::move(__sf))._M_swap(*this);
847 /// Retrieving the value
849 get() { return this->_M_get_result()._M_get(); }
852 /// Explicit specialization for shared_future<void>
854 class shared_future<void> : public __basic_future<void>
856 typedef __basic_future<void> _Base_type;
859 constexpr shared_future() noexcept : _Base_type() { }
862 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
864 /// Construct from a future rvalue
865 shared_future(future<void>&& __uf) noexcept
866 : _Base_type(std::move(__uf))
869 /// Construct from a shared_future rvalue
870 shared_future(shared_future&& __sf) noexcept
871 : _Base_type(std::move(__sf))
874 shared_future& operator=(const shared_future& __sf)
876 shared_future(__sf)._M_swap(*this);
880 shared_future& operator=(shared_future&& __sf) noexcept
882 shared_future(std::move(__sf))._M_swap(*this);
886 // Retrieving the value
888 get() { this->_M_get_result(); }
891 // Now we can define the protected __basic_future constructors.
892 template<typename _Res>
893 inline __basic_future<_Res>::
894 __basic_future(const shared_future<_Res>& __sf) noexcept
895 : _M_state(__sf._M_state)
898 template<typename _Res>
899 inline __basic_future<_Res>::
900 __basic_future(shared_future<_Res>&& __sf) noexcept
901 : _M_state(std::move(__sf._M_state))
904 template<typename _Res>
905 inline __basic_future<_Res>::
906 __basic_future(future<_Res>&& __uf) noexcept
907 : _M_state(std::move(__uf._M_state))
910 template<typename _Res>
911 inline shared_future<_Res>
912 future<_Res>::share()
913 { return shared_future<_Res>(std::move(*this)); }
915 template<typename _Res>
916 inline shared_future<_Res&>
917 future<_Res&>::share()
918 { return shared_future<_Res&>(std::move(*this)); }
920 inline shared_future<void>
921 future<void>::share()
922 { return shared_future<void>(std::move(*this)); }
924 /// Primary template for promise
925 template<typename _Res>
928 typedef __future_base::_State_base _State;
929 typedef __future_base::_Result<_Res> _Res_type;
930 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
931 template<typename, typename> friend class _State::_Setter;
933 shared_ptr<_State> _M_future;
934 _Ptr_type _M_storage;
938 : _M_future(std::make_shared<_State>()),
939 _M_storage(new _Res_type())
942 promise(promise&& __rhs) noexcept
943 : _M_future(std::move(__rhs._M_future)),
944 _M_storage(std::move(__rhs._M_storage))
947 template<typename _Allocator>
948 promise(allocator_arg_t, const _Allocator& __a)
949 : _M_future(std::allocate_shared<_State>(__a)),
950 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
953 template<typename _Allocator>
954 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
955 : _M_future(std::move(__rhs._M_future)),
956 _M_storage(std::move(__rhs._M_storage))
959 promise(const promise&) = delete;
963 if (static_cast<bool>(_M_future) && !_M_future.unique())
964 _M_future->_M_break_promise(std::move(_M_storage));
969 operator=(promise&& __rhs) noexcept
971 promise(std::move(__rhs)).swap(*this);
975 promise& operator=(const promise&) = delete;
978 swap(promise& __rhs) noexcept
980 _M_future.swap(__rhs._M_future);
981 _M_storage.swap(__rhs._M_storage);
984 // Retrieving the result
987 { return future<_Res>(_M_future); }
989 // Setting the result
991 set_value(const _Res& __r)
993 auto __setter = _State::__setter(this, __r);
994 _M_future->_M_set_result(std::move(__setter));
998 set_value(_Res&& __r)
1000 auto __setter = _State::__setter(this, std::move(__r));
1001 _M_future->_M_set_result(std::move(__setter));
1005 set_exception(exception_ptr __p)
1007 auto __setter = _State::__setter(__p, this);
1008 _M_future->_M_set_result(std::move(__setter));
1012 template<typename _Res>
1014 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1017 template<typename _Res, typename _Alloc>
1018 struct uses_allocator<promise<_Res>, _Alloc>
1019 : public true_type { };
1022 /// Partial specialization for promise<R&>
1023 template<typename _Res>
1024 class promise<_Res&>
1026 typedef __future_base::_State_base _State;
1027 typedef __future_base::_Result<_Res&> _Res_type;
1028 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1029 template<typename, typename> friend class _State::_Setter;
1031 shared_ptr<_State> _M_future;
1032 _Ptr_type _M_storage;
1036 : _M_future(std::make_shared<_State>()),
1037 _M_storage(new _Res_type())
1040 promise(promise&& __rhs) noexcept
1041 : _M_future(std::move(__rhs._M_future)),
1042 _M_storage(std::move(__rhs._M_storage))
1045 template<typename _Allocator>
1046 promise(allocator_arg_t, const _Allocator& __a)
1047 : _M_future(std::allocate_shared<_State>(__a)),
1048 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1051 template<typename _Allocator>
1052 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1053 : _M_future(std::move(__rhs._M_future)),
1054 _M_storage(std::move(__rhs._M_storage))
1057 promise(const promise&) = delete;
1061 if (static_cast<bool>(_M_future) && !_M_future.unique())
1062 _M_future->_M_break_promise(std::move(_M_storage));
1067 operator=(promise&& __rhs) noexcept
1069 promise(std::move(__rhs)).swap(*this);
1073 promise& operator=(const promise&) = delete;
1076 swap(promise& __rhs) noexcept
1078 _M_future.swap(__rhs._M_future);
1079 _M_storage.swap(__rhs._M_storage);
1082 // Retrieving the result
1085 { return future<_Res&>(_M_future); }
1087 // Setting the result
1089 set_value(_Res& __r)
1091 auto __setter = _State::__setter(this, __r);
1092 _M_future->_M_set_result(std::move(__setter));
1096 set_exception(exception_ptr __p)
1098 auto __setter = _State::__setter(__p, this);
1099 _M_future->_M_set_result(std::move(__setter));
1103 /// Explicit specialization for promise<void>
1107 typedef __future_base::_State_base _State;
1108 typedef __future_base::_Result<void> _Res_type;
1109 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1110 template<typename, typename> friend class _State::_Setter;
1112 shared_ptr<_State> _M_future;
1113 _Ptr_type _M_storage;
1117 : _M_future(std::make_shared<_State>()),
1118 _M_storage(new _Res_type())
1121 promise(promise&& __rhs) noexcept
1122 : _M_future(std::move(__rhs._M_future)),
1123 _M_storage(std::move(__rhs._M_storage))
1126 template<typename _Allocator>
1127 promise(allocator_arg_t, const _Allocator& __a)
1128 : _M_future(std::allocate_shared<_State>(__a)),
1129 _M_storage(__future_base::_S_allocate_result<void>(__a))
1132 template<typename _Allocator>
1133 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1134 : _M_future(std::move(__rhs._M_future)),
1135 _M_storage(std::move(__rhs._M_storage))
1138 promise(const promise&) = delete;
1142 if (static_cast<bool>(_M_future) && !_M_future.unique())
1143 _M_future->_M_break_promise(std::move(_M_storage));
1148 operator=(promise&& __rhs) noexcept
1150 promise(std::move(__rhs)).swap(*this);
1154 promise& operator=(const promise&) = delete;
1157 swap(promise& __rhs) noexcept
1159 _M_future.swap(__rhs._M_future);
1160 _M_storage.swap(__rhs._M_storage);
1163 // Retrieving the result
1166 { return future<void>(_M_future); }
1168 // Setting the result
1172 set_exception(exception_ptr __p)
1174 auto __setter = _State::__setter(__p, this);
1175 _M_future->_M_set_result(std::move(__setter));
1181 struct __future_base::_State_base::_Setter<void, void>
1183 promise<void>::_Ptr_type operator()()
1185 _State_base::_S_check(_M_promise->_M_future);
1186 return std::move(_M_promise->_M_storage);
1189 promise<void>* _M_promise;
1192 inline __future_base::_State_base::_Setter<void, void>
1193 __future_base::_State_base::__setter(promise<void>* __prom)
1195 return _Setter<void, void>{ __prom };
1199 promise<void>::set_value()
1201 auto __setter = _State::__setter(this);
1202 _M_future->_M_set_result(std::move(__setter));
1206 template<typename _Ptr_type, typename _Res>
1207 struct __future_base::_Task_setter
1209 _Ptr_type operator()()
1213 _M_result->_M_set(_M_fn());
1217 _M_result->_M_error = current_exception();
1219 return std::move(_M_result);
1221 _Ptr_type& _M_result;
1222 std::function<_Res()> _M_fn;
1225 template<typename _Ptr_type>
1226 struct __future_base::_Task_setter<_Ptr_type, void>
1228 _Ptr_type operator()()
1236 _M_result->_M_error = current_exception();
1238 return std::move(_M_result);
1240 _Ptr_type& _M_result;
1241 std::function<void()> _M_fn;
1244 template<typename _Res, typename... _Args>
1245 struct __future_base::_Task_state<_Res(_Args...)> final
1246 : __future_base::_State_base
1248 typedef _Res _Res_type;
1250 _Task_state(std::function<_Res(_Args...)> __task)
1251 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1254 template<typename _Func, typename _Alloc>
1255 _Task_state(_Func&& __task, const _Alloc& __a)
1256 : _M_result(_S_allocate_result<_Res>(__a)),
1257 _M_task(allocator_arg, __a, std::move(__task))
1261 _M_run(_Args... __args)
1263 // bound arguments decay so wrap lvalue references
1264 auto __boundfn = std::__bind_simple(std::ref(_M_task),
1265 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1266 auto __setter = _S_task_setter(_M_result, std::move(__boundfn));
1267 _M_set_result(std::move(__setter));
1270 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1271 _Ptr_type _M_result;
1272 std::function<_Res(_Args...)> _M_task;
1274 template<typename _Tp>
1275 static reference_wrapper<_Tp>
1276 _S_maybe_wrap_ref(_Tp& __t)
1277 { return std::ref(__t); }
1279 template<typename _Tp>
1280 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1282 _S_maybe_wrap_ref(_Tp&& __t)
1283 { return std::forward<_Tp>(__t); }
1286 template<typename _Task, typename _Fn, bool
1287 = is_same<_Task, typename decay<_Fn>::type>::value>
1288 struct __constrain_pkgdtask
1289 { typedef void __type; };
1291 template<typename _Task, typename _Fn>
1292 struct __constrain_pkgdtask<_Task, _Fn, true>
1296 template<typename _Res, typename... _ArgTypes>
1297 class packaged_task<_Res(_ArgTypes...)>
1299 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1300 shared_ptr<_State_type> _M_state;
1303 // Construction and destruction
1304 packaged_task() noexcept { }
1306 template<typename _Allocator>
1308 packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
1311 template<typename _Fn, typename = typename
1312 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1314 packaged_task(_Fn&& __fn)
1315 : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn)))
1318 template<typename _Fn, typename _Allocator, typename = typename
1319 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1321 packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn)
1322 : _M_state(std::allocate_shared<_State_type>(__a,
1323 std::forward<_Fn>(__fn)))
1328 if (static_cast<bool>(_M_state) && !_M_state.unique())
1329 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1333 packaged_task(const packaged_task&) = delete;
1334 packaged_task& operator=(const packaged_task&) = delete;
1336 template<typename _Allocator>
1338 packaged_task(allocator_arg_t, const _Allocator&,
1339 const packaged_task&) = delete;
1342 packaged_task(packaged_task&& __other) noexcept
1343 { this->swap(__other); }
1345 template<typename _Allocator>
1347 packaged_task(allocator_arg_t, const _Allocator&,
1348 packaged_task&& __other) noexcept
1349 { this->swap(__other); }
1351 packaged_task& operator=(packaged_task&& __other) noexcept
1353 packaged_task(std::move(__other)).swap(*this);
1358 swap(packaged_task& __other) noexcept
1359 { _M_state.swap(__other._M_state); }
1362 valid() const noexcept
1363 { return static_cast<bool>(_M_state); }
1368 { return future<_Res>(_M_state); }
1372 operator()(_ArgTypes... __args)
1374 __future_base::_State_base::_S_check(_M_state);
1375 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1381 __future_base::_State_base::_S_check(_M_state);
1382 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1387 template<typename _Res, typename... _ArgTypes>
1389 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1390 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1393 template<typename _Res, typename _Alloc>
1394 struct uses_allocator<packaged_task<_Res>, _Alloc>
1395 : public true_type { };
1398 template<typename _BoundFn, typename _Res>
1399 class __future_base::_Deferred_state final
1400 : public __future_base::_State_base
1404 _Deferred_state(_BoundFn&& __fn)
1405 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1409 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1410 _Ptr_type _M_result;
1416 // safe to call multiple times so ignore failure
1417 _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1421 template<typename _BoundFn, typename _Res>
1422 class __future_base::_Async_state final
1423 : public __future_base::_State_base
1427 _Async_state(_BoundFn&& __fn)
1428 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1429 _M_thread(mem_fn(&_Async_state::_M_do_run), this)
1432 ~_Async_state() { _M_thread.join(); }
1437 _M_set_result(_S_task_setter(_M_result, _M_fn));
1440 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1441 _Ptr_type _M_result;
1446 template<typename _BoundFn>
1447 inline std::shared_ptr<__future_base::_State_base>
1448 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1450 typedef typename remove_reference<_BoundFn>::type __fn_type;
1451 typedef _Deferred_state<__fn_type> __state_type;
1452 return std::make_shared<__state_type>(std::move(__fn));
1455 template<typename _BoundFn>
1456 inline std::shared_ptr<__future_base::_State_base>
1457 __future_base::_S_make_async_state(_BoundFn&& __fn)
1459 typedef typename remove_reference<_BoundFn>::type __fn_type;
1460 typedef _Async_state<__fn_type> __state_type;
1461 return std::make_shared<__state_type>(std::move(__fn));
1466 template<typename _Fn, typename... _Args>
1467 future<typename result_of<_Fn(_Args...)>::type>
1468 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1470 typedef typename result_of<_Fn(_Args...)>::type result_type;
1471 std::shared_ptr<__future_base::_State_base> __state;
1472 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1474 __state = __future_base::_S_make_async_state(std::__bind_simple(
1475 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1479 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1480 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1482 return future<result_type>(__state);
1485 /// async, potential overload
1486 template<typename _Fn, typename... _Args>
1488 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
1489 async(_Fn&& __fn, _Args&&... __args)
1491 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1492 std::forward<_Args>(__args)...);
1495 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1496 // && ATOMIC_INT_LOCK_FREE
1499 _GLIBCXX_END_NAMESPACE_VERSION
1502 #endif // __GXX_EXPERIMENTAL_CXX0X__
1504 #endif // _GLIBCXX_FUTURE