1 // <future> -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011, 2012 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
63 future_already_retrieved = 1,
64 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 _Signature>
119 template<typename _Res>
122 /// Launch code for futures
129 constexpr launch operator&(launch __x, launch __y)
131 return static_cast<launch>(
132 static_cast<int>(__x) & static_cast<int>(__y));
135 constexpr launch operator|(launch __x, launch __y)
137 return static_cast<launch>(
138 static_cast<int>(__x) | static_cast<int>(__y));
141 constexpr launch operator^(launch __x, launch __y)
143 return static_cast<launch>(
144 static_cast<int>(__x) ^ static_cast<int>(__y));
147 constexpr launch operator~(launch __x)
148 { return static_cast<launch>(~static_cast<int>(__x)); }
150 inline launch& operator&=(launch& __x, launch __y)
151 { return __x = __x & __y; }
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 /// Status code for futures
160 enum class future_status
167 template<typename _Fn, typename... _Args>
168 future<typename result_of<_Fn(_Args...)>::type>
169 async(launch __policy, _Fn&& __fn, _Args&&... __args);
171 template<typename _FnCheck, typename _Fn, typename... _Args>
172 struct __async_sfinae_helper
174 typedef future<typename result_of<_Fn(_Args...)>::type> type;
177 template<typename _Fn, typename... _Args>
178 struct __async_sfinae_helper<launch, _Fn, _Args...>
181 template<typename _Fn, typename... _Args>
183 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
184 async(_Fn&& __fn, _Args&&... __args);
186 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
187 && (ATOMIC_INT_LOCK_FREE > 1)
189 /// Base class and enclosing scope.
192 /// Base class for results.
195 exception_ptr _M_error;
197 _Result_base(const _Result_base&) = delete;
198 _Result_base& operator=(const _Result_base&) = delete;
200 // _M_destroy() allows derived classes to control deallocation
201 virtual void _M_destroy() = 0;
205 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
210 virtual ~_Result_base();
214 template<typename _Res>
215 struct _Result : _Result_base
218 typedef alignment_of<_Res> __a_of;
219 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
220 typedef typename __align_storage::type __align_type;
222 __align_type _M_storage;
226 _Result() noexcept : _M_initialized() { }
234 // Return lvalue, future will add const or rvalue-reference
236 _M_value() noexcept { return *static_cast<_Res*>(_M_addr()); }
239 _M_set(const _Res& __res)
241 ::new (_M_addr()) _Res(__res);
242 _M_initialized = true;
248 ::new (_M_addr()) _Res(std::move(__res));
249 _M_initialized = true;
253 void _M_destroy() { delete this; }
255 void* _M_addr() noexcept { return static_cast<void*>(&_M_storage); }
258 /// A unique_ptr based on the instantiating type.
259 template<typename _Res>
260 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
263 template<typename _Res, typename _Alloc>
264 struct _Result_alloc final : _Result<_Res>, _Alloc
266 typedef typename allocator_traits<_Alloc>::template
267 rebind_alloc<_Result_alloc> __allocator_type;
270 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
276 typedef allocator_traits<__allocator_type> __traits;
277 __allocator_type __a(*this);
278 __traits::destroy(__a, this);
279 __traits::deallocate(__a, this, 1);
283 template<typename _Res, typename _Allocator>
284 static _Ptr<_Result_alloc<_Res, _Allocator>>
285 _S_allocate_result(const _Allocator& __a)
287 typedef _Result_alloc<_Res, _Allocator> __result_type;
288 typedef allocator_traits<typename __result_type::__allocator_type>
290 typename __traits::allocator_type __a2(__a);
291 __result_type* __p = __traits::allocate(__a2, 1);
294 __traits::construct(__a2, __p, __a);
298 __traits::deallocate(__a2, __p, 1);
299 __throw_exception_again;
301 return _Ptr<__result_type>(__p);
305 /// Base class for state between a promise and one or more
306 /// associated futures.
309 typedef _Ptr<_Result_base> _Ptr_type;
313 condition_variable _M_cond;
314 atomic_flag _M_retrieved;
318 _State_base() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
319 _State_base(const _State_base&) = delete;
320 _State_base& operator=(const _State_base&) = delete;
321 virtual ~_State_base();
327 unique_lock<mutex> __lock(_M_mutex);
328 _M_cond.wait(__lock, [&] { return _M_ready(); });
332 template<typename _Rep, typename _Period>
334 wait_for(const chrono::duration<_Rep, _Period>& __rel)
336 unique_lock<mutex> __lock(_M_mutex);
337 if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
338 return future_status::ready;
339 return future_status::timeout;
342 template<typename _Clock, typename _Duration>
344 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
346 unique_lock<mutex> __lock(_M_mutex);
347 if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
348 return future_status::ready;
349 return future_status::timeout;
353 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
355 bool __set = __ignore_failure;
356 // all calls to this function are serialized,
357 // side-effects of invoking __res only happen once
358 call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
361 __throw_future_error(int(future_errc::promise_already_satisfied));
365 _M_break_promise(_Ptr_type __res)
367 if (static_cast<bool>(__res))
369 error_code __ec(make_error_code(future_errc::broken_promise));
370 __res->_M_error = copy_exception(future_error(__ec));
372 lock_guard<mutex> __lock(_M_mutex);
373 _M_result.swap(__res);
375 _M_cond.notify_all();
379 // Called when this object is passed to a future.
381 _M_set_retrieved_flag()
383 if (_M_retrieved.test_and_set())
384 __throw_future_error(int(future_errc::future_already_retrieved));
387 template<typename _Res, typename _Arg>
391 template<typename _Res, typename _Arg>
392 struct _Setter<_Res, _Arg&>
394 // check this is only used by promise<R>::set_value(const R&)
395 // or promise<R>::set_value(R&)
396 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
397 || is_same<const _Res, _Arg>::value, // promise<R>
398 "Invalid specialisation");
400 typename promise<_Res>::_Ptr_type operator()()
402 _State_base::_S_check(_M_promise->_M_future);
403 _M_promise->_M_storage->_M_set(_M_arg);
404 return std::move(_M_promise->_M_storage);
406 promise<_Res>* _M_promise;
411 template<typename _Res>
412 struct _Setter<_Res, _Res&&>
414 typename promise<_Res>::_Ptr_type operator()()
416 _State_base::_S_check(_M_promise->_M_future);
417 _M_promise->_M_storage->_M_set(std::move(_M_arg));
418 return std::move(_M_promise->_M_storage);
420 promise<_Res>* _M_promise;
424 struct __exception_ptr_tag { };
427 template<typename _Res>
428 struct _Setter<_Res, __exception_ptr_tag>
430 typename promise<_Res>::_Ptr_type operator()()
432 _State_base::_S_check(_M_promise->_M_future);
433 _M_promise->_M_storage->_M_error = _M_ex;
434 return std::move(_M_promise->_M_storage);
437 promise<_Res>* _M_promise;
438 exception_ptr& _M_ex;
441 template<typename _Res, typename _Arg>
442 static _Setter<_Res, _Arg&&>
443 __setter(promise<_Res>* __prom, _Arg&& __arg)
445 return _Setter<_Res, _Arg&&>{ __prom, __arg };
448 template<typename _Res>
449 static _Setter<_Res, __exception_ptr_tag>
450 __setter(exception_ptr& __ex, promise<_Res>* __prom)
452 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
455 static _Setter<void, void>
456 __setter(promise<void>* __prom);
458 template<typename _Tp>
460 _S_check(const shared_ptr<_Tp>& __p)
462 if (!static_cast<bool>(__p))
463 __throw_future_error((int)future_errc::no_state);
468 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
470 _Ptr_type __res = __f();
472 lock_guard<mutex> __lock(_M_mutex);
473 _M_result.swap(__res);
475 _M_cond.notify_all();
479 bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
481 // Misnamed: waits for completion of async function.
482 virtual void _M_run_deferred() { }
485 template<typename _BoundFn, typename = typename _BoundFn::result_type>
486 class _Deferred_state;
488 class _Async_state_common;
490 template<typename _BoundFn, typename = typename _BoundFn::result_type>
491 class _Async_state_impl;
493 template<typename _Signature>
496 template<typename _BoundFn>
497 static std::shared_ptr<_State_base>
498 _S_make_deferred_state(_BoundFn&& __fn);
500 template<typename _BoundFn>
501 static std::shared_ptr<_State_base>
502 _S_make_async_state(_BoundFn&& __fn);
504 template<typename _Res_ptr, typename _Res>
507 template<typename _Res_ptr, typename _BoundFn>
508 class _Task_setter_helper
510 typedef typename remove_reference<_BoundFn>::type::result_type __res;
512 typedef _Task_setter<_Res_ptr, __res> __type;
515 template<typename _Res_ptr, typename _BoundFn>
516 static typename _Task_setter_helper<_Res_ptr, _BoundFn>::__type
517 _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
519 typedef _Task_setter_helper<_Res_ptr, _BoundFn> __helper_type;
520 typedef typename __helper_type::__type _Setter;
521 return _Setter{ __ptr, std::ref(__call) };
525 /// Partial specialization for reference types.
526 template<typename _Res>
527 struct __future_base::_Result<_Res&> : __future_base::_Result_base
529 _Result() noexcept : _M_value_ptr() { }
531 void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
533 _Res& _M_get() noexcept { return *_M_value_ptr; }
538 void _M_destroy() { delete this; }
541 /// Explicit specialization for void.
543 struct __future_base::_Result<void> : __future_base::_Result_base
546 void _M_destroy() { delete this; }
550 /// Common implementation for future and shared_future.
551 template<typename _Res>
552 class __basic_future : public __future_base
555 typedef shared_ptr<_State_base> __state_type;
556 typedef __future_base::_Result<_Res>& __result_type;
559 __state_type _M_state;
563 __basic_future(const __basic_future&) = delete;
564 __basic_future& operator=(const __basic_future&) = delete;
567 valid() const noexcept { return static_cast<bool>(_M_state); }
572 _State_base::_S_check(_M_state);
576 template<typename _Rep, typename _Period>
578 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
580 _State_base::_S_check(_M_state);
581 return _M_state->wait_for(__rel);
584 template<typename _Clock, typename _Duration>
586 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
588 _State_base::_S_check(_M_state);
589 return _M_state->wait_until(__abs);
593 /// Wait for the state to be ready and rethrow any stored exception
597 _State_base::_S_check(_M_state);
598 _Result_base& __res = _M_state->wait();
599 if (!(__res._M_error == 0))
600 rethrow_exception(__res._M_error);
601 return static_cast<__result_type>(__res);
604 void _M_swap(__basic_future& __that) noexcept
606 _M_state.swap(__that._M_state);
609 // Construction of a future by promise::get_future()
611 __basic_future(const __state_type& __state) : _M_state(__state)
613 _State_base::_S_check(_M_state);
614 _M_state->_M_set_retrieved_flag();
617 // Copy construction from a shared_future
619 __basic_future(const shared_future<_Res>&) noexcept;
621 // Move construction from a shared_future
623 __basic_future(shared_future<_Res>&&) noexcept;
625 // Move construction from a future
627 __basic_future(future<_Res>&&) noexcept;
629 constexpr __basic_future() noexcept : _M_state() { }
633 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
634 ~_Reset() { _M_fut._M_state.reset(); }
635 __basic_future& _M_fut;
640 /// Primary template for future.
641 template<typename _Res>
642 class future : public __basic_future<_Res>
644 friend class promise<_Res>;
645 template<typename> friend class packaged_task;
646 template<typename _Fn, typename... _Args>
647 friend future<typename result_of<_Fn(_Args...)>::type>
648 async(launch, _Fn&&, _Args&&...);
650 typedef __basic_future<_Res> _Base_type;
651 typedef typename _Base_type::__state_type __state_type;
654 future(const __state_type& __state) : _Base_type(__state) { }
657 constexpr future() noexcept : _Base_type() { }
660 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
663 future(const future&) = delete;
664 future& operator=(const future&) = delete;
666 future& operator=(future&& __fut) noexcept
668 future(std::move(__fut))._M_swap(*this);
672 /// Retrieving the value
676 typename _Base_type::_Reset __reset(*this);
677 return std::move(this->_M_get_result()._M_value());
680 shared_future<_Res> share();
683 /// Partial specialization for future<R&>
684 template<typename _Res>
685 class future<_Res&> : public __basic_future<_Res&>
687 friend class promise<_Res&>;
688 template<typename> friend class packaged_task;
689 template<typename _Fn, typename... _Args>
690 friend future<typename result_of<_Fn(_Args...)>::type>
691 async(launch, _Fn&&, _Args&&...);
693 typedef __basic_future<_Res&> _Base_type;
694 typedef typename _Base_type::__state_type __state_type;
697 future(const __state_type& __state) : _Base_type(__state) { }
700 constexpr future() noexcept : _Base_type() { }
703 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
706 future(const future&) = delete;
707 future& operator=(const future&) = delete;
709 future& operator=(future&& __fut) noexcept
711 future(std::move(__fut))._M_swap(*this);
715 /// Retrieving the value
719 typename _Base_type::_Reset __reset(*this);
720 return this->_M_get_result()._M_get();
723 shared_future<_Res&> share();
726 /// Explicit specialization for future<void>
728 class future<void> : public __basic_future<void>
730 friend class promise<void>;
731 template<typename> friend class packaged_task;
732 template<typename _Fn, typename... _Args>
733 friend future<typename result_of<_Fn(_Args...)>::type>
734 async(launch, _Fn&&, _Args&&...);
736 typedef __basic_future<void> _Base_type;
737 typedef typename _Base_type::__state_type __state_type;
740 future(const __state_type& __state) : _Base_type(__state) { }
743 constexpr future() noexcept : _Base_type() { }
746 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
749 future(const future&) = delete;
750 future& operator=(const future&) = delete;
752 future& operator=(future&& __fut) noexcept
754 future(std::move(__fut))._M_swap(*this);
758 /// Retrieving the value
762 typename _Base_type::_Reset __reset(*this);
763 this->_M_get_result();
766 shared_future<void> share();
770 /// Primary template for shared_future.
771 template<typename _Res>
772 class shared_future : public __basic_future<_Res>
774 typedef __basic_future<_Res> _Base_type;
777 constexpr shared_future() noexcept : _Base_type() { }
780 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
782 /// Construct from a future rvalue
783 shared_future(future<_Res>&& __uf) noexcept
784 : _Base_type(std::move(__uf))
787 /// Construct from a shared_future rvalue
788 shared_future(shared_future&& __sf) noexcept
789 : _Base_type(std::move(__sf))
792 shared_future& operator=(const shared_future& __sf)
794 shared_future(__sf)._M_swap(*this);
798 shared_future& operator=(shared_future&& __sf) noexcept
800 shared_future(std::move(__sf))._M_swap(*this);
804 /// Retrieving the value
808 typename _Base_type::__result_type __r = this->_M_get_result();
809 _Res& __rs(__r._M_value());
814 /// Partial specialization for shared_future<R&>
815 template<typename _Res>
816 class shared_future<_Res&> : public __basic_future<_Res&>
818 typedef __basic_future<_Res&> _Base_type;
821 constexpr shared_future() noexcept : _Base_type() { }
824 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
826 /// Construct from a future rvalue
827 shared_future(future<_Res&>&& __uf) noexcept
828 : _Base_type(std::move(__uf))
831 /// Construct from a shared_future rvalue
832 shared_future(shared_future&& __sf) noexcept
833 : _Base_type(std::move(__sf))
836 shared_future& operator=(const shared_future& __sf)
838 shared_future(__sf)._M_swap(*this);
842 shared_future& operator=(shared_future&& __sf) noexcept
844 shared_future(std::move(__sf))._M_swap(*this);
848 /// Retrieving the value
850 get() { return this->_M_get_result()._M_get(); }
853 /// Explicit specialization for shared_future<void>
855 class shared_future<void> : public __basic_future<void>
857 typedef __basic_future<void> _Base_type;
860 constexpr shared_future() noexcept : _Base_type() { }
863 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
865 /// Construct from a future rvalue
866 shared_future(future<void>&& __uf) noexcept
867 : _Base_type(std::move(__uf))
870 /// Construct from a shared_future rvalue
871 shared_future(shared_future&& __sf) noexcept
872 : _Base_type(std::move(__sf))
875 shared_future& operator=(const shared_future& __sf)
877 shared_future(__sf)._M_swap(*this);
881 shared_future& operator=(shared_future&& __sf) noexcept
883 shared_future(std::move(__sf))._M_swap(*this);
887 // Retrieving the value
889 get() { this->_M_get_result(); }
892 // Now we can define the protected __basic_future constructors.
893 template<typename _Res>
894 inline __basic_future<_Res>::
895 __basic_future(const shared_future<_Res>& __sf) noexcept
896 : _M_state(__sf._M_state)
899 template<typename _Res>
900 inline __basic_future<_Res>::
901 __basic_future(shared_future<_Res>&& __sf) noexcept
902 : _M_state(std::move(__sf._M_state))
905 template<typename _Res>
906 inline __basic_future<_Res>::
907 __basic_future(future<_Res>&& __uf) noexcept
908 : _M_state(std::move(__uf._M_state))
911 template<typename _Res>
912 inline shared_future<_Res>
913 future<_Res>::share()
914 { return shared_future<_Res>(std::move(*this)); }
916 template<typename _Res>
917 inline shared_future<_Res&>
918 future<_Res&>::share()
919 { return shared_future<_Res&>(std::move(*this)); }
921 inline shared_future<void>
922 future<void>::share()
923 { return shared_future<void>(std::move(*this)); }
925 /// Primary template for promise
926 template<typename _Res>
929 typedef __future_base::_State_base _State;
930 typedef __future_base::_Result<_Res> _Res_type;
931 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
932 template<typename, typename> friend class _State::_Setter;
934 shared_ptr<_State> _M_future;
935 _Ptr_type _M_storage;
939 : _M_future(std::make_shared<_State>()),
940 _M_storage(new _Res_type())
943 promise(promise&& __rhs) noexcept
944 : _M_future(std::move(__rhs._M_future)),
945 _M_storage(std::move(__rhs._M_storage))
948 template<typename _Allocator>
949 promise(allocator_arg_t, const _Allocator& __a)
950 : _M_future(std::allocate_shared<_State>(__a)),
951 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
954 template<typename _Allocator>
955 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
956 : _M_future(std::move(__rhs._M_future)),
957 _M_storage(std::move(__rhs._M_storage))
960 promise(const promise&) = delete;
964 if (static_cast<bool>(_M_future) && !_M_future.unique())
965 _M_future->_M_break_promise(std::move(_M_storage));
970 operator=(promise&& __rhs) noexcept
972 promise(std::move(__rhs)).swap(*this);
976 promise& operator=(const promise&) = delete;
979 swap(promise& __rhs) noexcept
981 _M_future.swap(__rhs._M_future);
982 _M_storage.swap(__rhs._M_storage);
985 // Retrieving the result
988 { return future<_Res>(_M_future); }
990 // Setting the result
992 set_value(const _Res& __r)
994 auto __setter = _State::__setter(this, __r);
995 _M_future->_M_set_result(std::move(__setter));
999 set_value(_Res&& __r)
1001 auto __setter = _State::__setter(this, std::move(__r));
1002 _M_future->_M_set_result(std::move(__setter));
1006 set_exception(exception_ptr __p)
1008 auto __setter = _State::__setter(__p, this);
1009 _M_future->_M_set_result(std::move(__setter));
1013 template<typename _Res>
1015 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1018 template<typename _Res, typename _Alloc>
1019 struct uses_allocator<promise<_Res>, _Alloc>
1020 : public true_type { };
1023 /// Partial specialization for promise<R&>
1024 template<typename _Res>
1025 class promise<_Res&>
1027 typedef __future_base::_State_base _State;
1028 typedef __future_base::_Result<_Res&> _Res_type;
1029 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1030 template<typename, typename> friend class _State::_Setter;
1032 shared_ptr<_State> _M_future;
1033 _Ptr_type _M_storage;
1037 : _M_future(std::make_shared<_State>()),
1038 _M_storage(new _Res_type())
1041 promise(promise&& __rhs) noexcept
1042 : _M_future(std::move(__rhs._M_future)),
1043 _M_storage(std::move(__rhs._M_storage))
1046 template<typename _Allocator>
1047 promise(allocator_arg_t, const _Allocator& __a)
1048 : _M_future(std::allocate_shared<_State>(__a)),
1049 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1052 template<typename _Allocator>
1053 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1054 : _M_future(std::move(__rhs._M_future)),
1055 _M_storage(std::move(__rhs._M_storage))
1058 promise(const promise&) = delete;
1062 if (static_cast<bool>(_M_future) && !_M_future.unique())
1063 _M_future->_M_break_promise(std::move(_M_storage));
1068 operator=(promise&& __rhs) noexcept
1070 promise(std::move(__rhs)).swap(*this);
1074 promise& operator=(const promise&) = delete;
1077 swap(promise& __rhs) noexcept
1079 _M_future.swap(__rhs._M_future);
1080 _M_storage.swap(__rhs._M_storage);
1083 // Retrieving the result
1086 { return future<_Res&>(_M_future); }
1088 // Setting the result
1090 set_value(_Res& __r)
1092 auto __setter = _State::__setter(this, __r);
1093 _M_future->_M_set_result(std::move(__setter));
1097 set_exception(exception_ptr __p)
1099 auto __setter = _State::__setter(__p, this);
1100 _M_future->_M_set_result(std::move(__setter));
1104 /// Explicit specialization for promise<void>
1108 typedef __future_base::_State_base _State;
1109 typedef __future_base::_Result<void> _Res_type;
1110 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1111 template<typename, typename> friend class _State::_Setter;
1113 shared_ptr<_State> _M_future;
1114 _Ptr_type _M_storage;
1118 : _M_future(std::make_shared<_State>()),
1119 _M_storage(new _Res_type())
1122 promise(promise&& __rhs) noexcept
1123 : _M_future(std::move(__rhs._M_future)),
1124 _M_storage(std::move(__rhs._M_storage))
1127 template<typename _Allocator>
1128 promise(allocator_arg_t, const _Allocator& __a)
1129 : _M_future(std::allocate_shared<_State>(__a)),
1130 _M_storage(__future_base::_S_allocate_result<void>(__a))
1133 template<typename _Allocator>
1134 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1135 : _M_future(std::move(__rhs._M_future)),
1136 _M_storage(std::move(__rhs._M_storage))
1139 promise(const promise&) = delete;
1143 if (static_cast<bool>(_M_future) && !_M_future.unique())
1144 _M_future->_M_break_promise(std::move(_M_storage));
1149 operator=(promise&& __rhs) noexcept
1151 promise(std::move(__rhs)).swap(*this);
1155 promise& operator=(const promise&) = delete;
1158 swap(promise& __rhs) noexcept
1160 _M_future.swap(__rhs._M_future);
1161 _M_storage.swap(__rhs._M_storage);
1164 // Retrieving the result
1167 { return future<void>(_M_future); }
1169 // Setting the result
1173 set_exception(exception_ptr __p)
1175 auto __setter = _State::__setter(__p, this);
1176 _M_future->_M_set_result(std::move(__setter));
1182 struct __future_base::_State_base::_Setter<void, void>
1184 promise<void>::_Ptr_type operator()()
1186 _State_base::_S_check(_M_promise->_M_future);
1187 return std::move(_M_promise->_M_storage);
1190 promise<void>* _M_promise;
1193 inline __future_base::_State_base::_Setter<void, void>
1194 __future_base::_State_base::__setter(promise<void>* __prom)
1196 return _Setter<void, void>{ __prom };
1200 promise<void>::set_value()
1202 auto __setter = _State::__setter(this);
1203 _M_future->_M_set_result(std::move(__setter));
1207 template<typename _Ptr_type, typename _Res>
1208 struct __future_base::_Task_setter
1210 _Ptr_type operator()()
1214 _M_result->_M_set(_M_fn());
1218 _M_result->_M_error = current_exception();
1220 return std::move(_M_result);
1222 _Ptr_type& _M_result;
1223 std::function<_Res()> _M_fn;
1226 template<typename _Ptr_type>
1227 struct __future_base::_Task_setter<_Ptr_type, void>
1229 _Ptr_type operator()()
1237 _M_result->_M_error = current_exception();
1239 return std::move(_M_result);
1241 _Ptr_type& _M_result;
1242 std::function<void()> _M_fn;
1245 template<typename _Res, typename... _Args>
1246 struct __future_base::_Task_state<_Res(_Args...)> final
1247 : __future_base::_State_base
1249 typedef _Res _Res_type;
1251 _Task_state(std::function<_Res(_Args...)> __task)
1252 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1255 template<typename _Func, typename _Alloc>
1256 _Task_state(_Func&& __task, const _Alloc& __a)
1257 : _M_result(_S_allocate_result<_Res>(__a)),
1258 _M_task(allocator_arg, __a, std::move(__task))
1262 _M_run(_Args... __args)
1264 // bound arguments decay so wrap lvalue references
1265 auto __boundfn = std::__bind_simple(std::ref(_M_task),
1266 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1267 auto __setter = _S_task_setter(_M_result, std::move(__boundfn));
1268 _M_set_result(std::move(__setter));
1271 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1272 _Ptr_type _M_result;
1273 std::function<_Res(_Args...)> _M_task;
1275 template<typename _Tp>
1276 static reference_wrapper<_Tp>
1277 _S_maybe_wrap_ref(_Tp& __t)
1278 { return std::ref(__t); }
1280 template<typename _Tp>
1281 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1283 _S_maybe_wrap_ref(_Tp&& __t)
1284 { return std::forward<_Tp>(__t); }
1287 template<typename _Task, typename _Fn, bool
1288 = is_same<_Task, typename decay<_Fn>::type>::value>
1289 struct __constrain_pkgdtask
1290 { typedef void __type; };
1292 template<typename _Task, typename _Fn>
1293 struct __constrain_pkgdtask<_Task, _Fn, true>
1297 template<typename _Res, typename... _ArgTypes>
1298 class packaged_task<_Res(_ArgTypes...)>
1300 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1301 shared_ptr<_State_type> _M_state;
1304 // Construction and destruction
1305 packaged_task() noexcept { }
1307 template<typename _Allocator>
1309 packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
1312 template<typename _Fn, typename = typename
1313 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1315 packaged_task(_Fn&& __fn)
1316 : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn)))
1319 template<typename _Fn, typename _Allocator, typename = typename
1320 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1322 packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn)
1323 : _M_state(std::allocate_shared<_State_type>(__a,
1324 std::forward<_Fn>(__fn)))
1329 if (static_cast<bool>(_M_state) && !_M_state.unique())
1330 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1334 packaged_task(const packaged_task&) = delete;
1335 packaged_task& operator=(const packaged_task&) = delete;
1337 template<typename _Allocator>
1339 packaged_task(allocator_arg_t, const _Allocator&,
1340 const packaged_task&) = delete;
1343 packaged_task(packaged_task&& __other) noexcept
1344 { this->swap(__other); }
1346 template<typename _Allocator>
1348 packaged_task(allocator_arg_t, const _Allocator&,
1349 packaged_task&& __other) noexcept
1350 { this->swap(__other); }
1352 packaged_task& operator=(packaged_task&& __other) noexcept
1354 packaged_task(std::move(__other)).swap(*this);
1359 swap(packaged_task& __other) noexcept
1360 { _M_state.swap(__other._M_state); }
1363 valid() const noexcept
1364 { return static_cast<bool>(_M_state); }
1369 { return future<_Res>(_M_state); }
1373 operator()(_ArgTypes... __args)
1375 __future_base::_State_base::_S_check(_M_state);
1376 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1382 __future_base::_State_base::_S_check(_M_state);
1383 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1388 template<typename _Res, typename... _ArgTypes>
1390 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1391 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1394 template<typename _Res, typename _Alloc>
1395 struct uses_allocator<packaged_task<_Res>, _Alloc>
1396 : public true_type { };
1399 template<typename _BoundFn, typename _Res>
1400 class __future_base::_Deferred_state final
1401 : public __future_base::_State_base
1405 _Deferred_state(_BoundFn&& __fn)
1406 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1410 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1411 _Ptr_type _M_result;
1417 // safe to call multiple times so ignore failure
1418 _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1422 class __future_base::_Async_state_common : public __future_base::_State_base
1425 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
1426 ~_Async_state_common();
1428 ~_Async_state_common() = default;
1431 // Allow non-timed waiting functions to block until the thread completes,
1433 virtual void _M_run_deferred() { _M_join(); }
1435 void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
1441 template<typename _BoundFn, typename _Res>
1442 class __future_base::_Async_state_impl final
1443 : public __future_base::_Async_state_common
1447 _Async_state_impl(_BoundFn&& __fn)
1448 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1450 _M_thread = std::thread{ [this] {
1451 _M_set_result(_S_task_setter(_M_result, _M_fn));
1455 ~_Async_state_impl() { _M_join(); }
1458 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1459 _Ptr_type _M_result;
1463 template<typename _BoundFn>
1464 inline std::shared_ptr<__future_base::_State_base>
1465 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1467 typedef typename remove_reference<_BoundFn>::type __fn_type;
1468 typedef _Deferred_state<__fn_type> __state_type;
1469 return std::make_shared<__state_type>(std::move(__fn));
1472 template<typename _BoundFn>
1473 inline std::shared_ptr<__future_base::_State_base>
1474 __future_base::_S_make_async_state(_BoundFn&& __fn)
1476 typedef typename remove_reference<_BoundFn>::type __fn_type;
1477 typedef _Async_state_impl<__fn_type> __state_type;
1478 return std::make_shared<__state_type>(std::move(__fn));
1483 template<typename _Fn, typename... _Args>
1484 future<typename result_of<_Fn(_Args...)>::type>
1485 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1487 typedef typename result_of<_Fn(_Args...)>::type result_type;
1488 std::shared_ptr<__future_base::_State_base> __state;
1489 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1491 __state = __future_base::_S_make_async_state(std::__bind_simple(
1492 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1496 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1497 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1499 return future<result_type>(__state);
1502 /// async, potential overload
1503 template<typename _Fn, typename... _Args>
1505 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
1506 async(_Fn&& __fn, _Args&&... __args)
1508 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1509 std::forward<_Args>(__args)...);
1512 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1513 // && ATOMIC_INT_LOCK_FREE
1516 _GLIBCXX_END_NAMESPACE_VERSION
1519 #endif // __GXX_EXPERIMENTAL_CXX0X__
1521 #endif // _GLIBCXX_FUTURE