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 inline constexpr launch operator&(launch __x, launch __y)
134 return static_cast<launch>(
135 static_cast<int>(__x) & static_cast<int>(__y));
138 inline constexpr launch operator|(launch __x, launch __y)
140 return static_cast<launch>(
141 static_cast<int>(__x) | static_cast<int>(__y));
144 inline constexpr launch operator^(launch __x, launch __y)
146 return static_cast<launch>(
147 static_cast<int>(__x) ^ static_cast<int>(__y));
150 inline 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 _Res>
492 class _Deferred_state;
494 template<typename _Res>
497 template<typename _Signature>
500 template<typename _StateT, typename _Res = typename _StateT::_Res_type>
504 /// Partial specialization for reference types.
505 template<typename _Res>
506 struct __future_base::_Result<_Res&> : __future_base::_Result_base
508 _Result() noexcept : _M_value_ptr() { }
510 void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
512 _Res& _M_get() noexcept { return *_M_value_ptr; }
517 void _M_destroy() { delete this; }
520 /// Explicit specialization for void.
522 struct __future_base::_Result<void> : __future_base::_Result_base
525 void _M_destroy() { delete this; }
529 /// Common implementation for future and shared_future.
530 template<typename _Res>
531 class __basic_future : public __future_base
534 typedef shared_ptr<_State_base> __state_type;
535 typedef __future_base::_Result<_Res>& __result_type;
538 __state_type _M_state;
542 __basic_future(const __basic_future&) = delete;
543 __basic_future& operator=(const __basic_future&) = delete;
546 valid() const noexcept { return static_cast<bool>(_M_state); }
551 _State_base::_S_check(_M_state);
555 template<typename _Rep, typename _Period>
557 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
559 _State_base::_S_check(_M_state);
560 return _M_state->wait_for(__rel);
563 template<typename _Clock, typename _Duration>
565 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
567 _State_base::_S_check(_M_state);
568 return _M_state->wait_until(__abs);
572 /// Wait for the state to be ready and rethrow any stored exception
576 _State_base::_S_check(_M_state);
577 _Result_base& __res = _M_state->wait();
578 if (!(__res._M_error == 0))
579 rethrow_exception(__res._M_error);
580 return static_cast<__result_type>(__res);
583 void _M_swap(__basic_future& __that) noexcept
585 _M_state.swap(__that._M_state);
588 // Construction of a future by promise::get_future()
590 __basic_future(const __state_type& __state) : _M_state(__state)
592 _State_base::_S_check(_M_state);
593 _M_state->_M_set_retrieved_flag();
596 // Copy construction from a shared_future
598 __basic_future(const shared_future<_Res>&) noexcept;
600 // Move construction from a shared_future
602 __basic_future(shared_future<_Res>&&) noexcept;
604 // Move construction from a future
606 __basic_future(future<_Res>&&) noexcept;
608 constexpr __basic_future() noexcept : _M_state() { }
612 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
613 ~_Reset() { _M_fut._M_state.reset(); }
614 __basic_future& _M_fut;
619 /// Primary template for future.
620 template<typename _Res>
621 class future : public __basic_future<_Res>
623 friend class promise<_Res>;
624 template<typename> friend class packaged_task;
625 template<typename _Fn, typename... _Args>
626 friend future<typename result_of<_Fn(_Args...)>::type>
627 async(launch, _Fn&&, _Args&&...);
629 typedef __basic_future<_Res> _Base_type;
630 typedef typename _Base_type::__state_type __state_type;
633 future(const __state_type& __state) : _Base_type(__state) { }
636 constexpr future() noexcept : _Base_type() { }
639 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
642 future(const future&) = delete;
643 future& operator=(const future&) = delete;
645 future& operator=(future&& __fut) noexcept
647 future(std::move(__fut))._M_swap(*this);
651 /// Retrieving the value
655 typename _Base_type::_Reset __reset(*this);
656 return std::move(this->_M_get_result()._M_value());
659 shared_future<_Res> share();
662 /// Partial specialization for future<R&>
663 template<typename _Res>
664 class future<_Res&> : public __basic_future<_Res&>
666 friend class promise<_Res&>;
667 template<typename> friend class packaged_task;
668 template<typename _Fn, typename... _Args>
669 friend future<typename result_of<_Fn(_Args...)>::type>
670 async(launch, _Fn&&, _Args&&...);
672 typedef __basic_future<_Res&> _Base_type;
673 typedef typename _Base_type::__state_type __state_type;
676 future(const __state_type& __state) : _Base_type(__state) { }
679 constexpr future() noexcept : _Base_type() { }
682 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
685 future(const future&) = delete;
686 future& operator=(const future&) = delete;
688 future& operator=(future&& __fut) noexcept
690 future(std::move(__fut))._M_swap(*this);
694 /// Retrieving the value
698 typename _Base_type::_Reset __reset(*this);
699 return this->_M_get_result()._M_get();
702 shared_future<_Res&> share();
705 /// Explicit specialization for future<void>
707 class future<void> : public __basic_future<void>
709 friend class promise<void>;
710 template<typename> friend class packaged_task;
711 template<typename _Fn, typename... _Args>
712 friend future<typename result_of<_Fn(_Args...)>::type>
713 async(launch, _Fn&&, _Args&&...);
715 typedef __basic_future<void> _Base_type;
716 typedef typename _Base_type::__state_type __state_type;
719 future(const __state_type& __state) : _Base_type(__state) { }
722 constexpr future() noexcept : _Base_type() { }
725 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
728 future(const future&) = delete;
729 future& operator=(const future&) = delete;
731 future& operator=(future&& __fut) noexcept
733 future(std::move(__fut))._M_swap(*this);
737 /// Retrieving the value
741 typename _Base_type::_Reset __reset(*this);
742 this->_M_get_result();
745 shared_future<void> share();
749 /// Primary template for shared_future.
750 template<typename _Res>
751 class shared_future : public __basic_future<_Res>
753 typedef __basic_future<_Res> _Base_type;
756 constexpr shared_future() noexcept : _Base_type() { }
759 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
761 /// Construct from a future rvalue
762 shared_future(future<_Res>&& __uf) noexcept
763 : _Base_type(std::move(__uf))
766 /// Construct from a shared_future rvalue
767 shared_future(shared_future&& __sf) noexcept
768 : _Base_type(std::move(__sf))
771 shared_future& operator=(const shared_future& __sf)
773 shared_future(__sf)._M_swap(*this);
777 shared_future& operator=(shared_future&& __sf) noexcept
779 shared_future(std::move(__sf))._M_swap(*this);
783 /// Retrieving the value
787 typename _Base_type::__result_type __r = this->_M_get_result();
788 _Res& __rs(__r._M_value());
793 /// Partial specialization for shared_future<R&>
794 template<typename _Res>
795 class shared_future<_Res&> : public __basic_future<_Res&>
797 typedef __basic_future<_Res&> _Base_type;
800 constexpr shared_future() noexcept : _Base_type() { }
803 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
805 /// Construct from a future rvalue
806 shared_future(future<_Res&>&& __uf) noexcept
807 : _Base_type(std::move(__uf))
810 /// Construct from a shared_future rvalue
811 shared_future(shared_future&& __sf) noexcept
812 : _Base_type(std::move(__sf))
815 shared_future& operator=(const shared_future& __sf)
817 shared_future(__sf)._M_swap(*this);
821 shared_future& operator=(shared_future&& __sf) noexcept
823 shared_future(std::move(__sf))._M_swap(*this);
827 /// Retrieving the value
829 get() { return this->_M_get_result()._M_get(); }
832 /// Explicit specialization for shared_future<void>
834 class shared_future<void> : public __basic_future<void>
836 typedef __basic_future<void> _Base_type;
839 constexpr shared_future() noexcept : _Base_type() { }
842 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
844 /// Construct from a future rvalue
845 shared_future(future<void>&& __uf) noexcept
846 : _Base_type(std::move(__uf))
849 /// Construct from a shared_future rvalue
850 shared_future(shared_future&& __sf) noexcept
851 : _Base_type(std::move(__sf))
854 shared_future& operator=(const shared_future& __sf)
856 shared_future(__sf)._M_swap(*this);
860 shared_future& operator=(shared_future&& __sf) noexcept
862 shared_future(std::move(__sf))._M_swap(*this);
866 // Retrieving the value
868 get() { this->_M_get_result(); }
871 // Now we can define the protected __basic_future constructors.
872 template<typename _Res>
873 inline __basic_future<_Res>::
874 __basic_future(const shared_future<_Res>& __sf) noexcept
875 : _M_state(__sf._M_state)
878 template<typename _Res>
879 inline __basic_future<_Res>::
880 __basic_future(shared_future<_Res>&& __sf) noexcept
881 : _M_state(std::move(__sf._M_state))
884 template<typename _Res>
885 inline __basic_future<_Res>::
886 __basic_future(future<_Res>&& __uf) noexcept
887 : _M_state(std::move(__uf._M_state))
890 template<typename _Res>
891 inline shared_future<_Res>
892 future<_Res>::share()
893 { return shared_future<_Res>(std::move(*this)); }
895 template<typename _Res>
896 inline shared_future<_Res&>
897 future<_Res&>::share()
898 { return shared_future<_Res&>(std::move(*this)); }
900 inline shared_future<void>
901 future<void>::share()
902 { return shared_future<void>(std::move(*this)); }
904 /// Primary template for promise
905 template<typename _Res>
908 typedef __future_base::_State_base _State;
909 typedef __future_base::_Result<_Res> _Res_type;
910 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
911 template<typename, typename> friend class _State::_Setter;
913 shared_ptr<_State> _M_future;
914 _Ptr_type _M_storage;
918 : _M_future(std::make_shared<_State>()),
919 _M_storage(new _Res_type())
922 promise(promise&& __rhs) noexcept
923 : _M_future(std::move(__rhs._M_future)),
924 _M_storage(std::move(__rhs._M_storage))
927 template<typename _Allocator>
928 promise(allocator_arg_t, const _Allocator& __a)
929 : _M_future(std::allocate_shared<_State>(__a)),
930 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
933 promise(const promise&) = delete;
937 if (static_cast<bool>(_M_future) && !_M_future.unique())
938 _M_future->_M_break_promise(std::move(_M_storage));
943 operator=(promise&& __rhs) noexcept
945 promise(std::move(__rhs)).swap(*this);
949 promise& operator=(const promise&) = delete;
952 swap(promise& __rhs) noexcept
954 _M_future.swap(__rhs._M_future);
955 _M_storage.swap(__rhs._M_storage);
958 // Retrieving the result
961 { return future<_Res>(_M_future); }
963 // Setting the result
965 set_value(const _Res& __r)
967 auto __setter = _State::__setter(this, __r);
968 _M_future->_M_set_result(std::move(__setter));
972 set_value(_Res&& __r)
974 auto __setter = _State::__setter(this, std::move(__r));
975 _M_future->_M_set_result(std::move(__setter));
979 set_exception(exception_ptr __p)
981 auto __setter = _State::__setter(__p, this);
982 _M_future->_M_set_result(std::move(__setter));
986 template<typename _Res>
988 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
991 template<typename _Res, typename _Alloc>
992 struct uses_allocator<promise<_Res>, _Alloc>
993 : public true_type { };
996 /// Partial specialization for promise<R&>
997 template<typename _Res>
1000 typedef __future_base::_State_base _State;
1001 typedef __future_base::_Result<_Res&> _Res_type;
1002 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
1003 template<typename, typename> friend class _State::_Setter;
1005 shared_ptr<_State> _M_future;
1006 _Ptr_type _M_storage;
1010 : _M_future(std::make_shared<_State>()),
1011 _M_storage(new _Res_type())
1014 promise(promise&& __rhs) noexcept
1015 : _M_future(std::move(__rhs._M_future)),
1016 _M_storage(std::move(__rhs._M_storage))
1019 template<typename _Allocator>
1020 promise(allocator_arg_t, const _Allocator& __a)
1021 : _M_future(std::allocate_shared<_State>(__a)),
1022 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1025 promise(const promise&) = delete;
1029 if (static_cast<bool>(_M_future) && !_M_future.unique())
1030 _M_future->_M_break_promise(std::move(_M_storage));
1035 operator=(promise&& __rhs) noexcept
1037 promise(std::move(__rhs)).swap(*this);
1041 promise& operator=(const promise&) = delete;
1044 swap(promise& __rhs) noexcept
1046 _M_future.swap(__rhs._M_future);
1047 _M_storage.swap(__rhs._M_storage);
1050 // Retrieving the result
1053 { return future<_Res&>(_M_future); }
1055 // Setting the result
1057 set_value(_Res& __r)
1059 auto __setter = _State::__setter(this, __r);
1060 _M_future->_M_set_result(std::move(__setter));
1064 set_exception(exception_ptr __p)
1066 auto __setter = _State::__setter(__p, this);
1067 _M_future->_M_set_result(std::move(__setter));
1071 /// Explicit specialization for promise<void>
1075 typedef __future_base::_State_base _State;
1076 typedef __future_base::_Result<void> _Res_type;
1077 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
1078 template<typename, typename> friend class _State::_Setter;
1080 shared_ptr<_State> _M_future;
1081 _Ptr_type _M_storage;
1085 : _M_future(std::make_shared<_State>()),
1086 _M_storage(new _Res_type())
1089 promise(promise&& __rhs) noexcept
1090 : _M_future(std::move(__rhs._M_future)),
1091 _M_storage(std::move(__rhs._M_storage))
1094 template<typename _Allocator>
1095 promise(allocator_arg_t, const _Allocator& __a)
1096 : _M_future(std::allocate_shared<_State>(__a)),
1097 _M_storage(__future_base::_S_allocate_result<void>(__a))
1100 promise(const promise&) = delete;
1104 if (static_cast<bool>(_M_future) && !_M_future.unique())
1105 _M_future->_M_break_promise(std::move(_M_storage));
1110 operator=(promise&& __rhs) noexcept
1112 promise(std::move(__rhs)).swap(*this);
1116 promise& operator=(const promise&) = delete;
1119 swap(promise& __rhs) noexcept
1121 _M_future.swap(__rhs._M_future);
1122 _M_storage.swap(__rhs._M_storage);
1125 // Retrieving the result
1128 { return future<void>(_M_future); }
1130 // Setting the result
1134 set_exception(exception_ptr __p)
1136 auto __setter = _State::__setter(__p, this);
1137 _M_future->_M_set_result(std::move(__setter));
1143 struct __future_base::_State_base::_Setter<void, void>
1145 promise<void>::_Ptr_type operator()()
1147 _State_base::_S_check(_M_promise->_M_future);
1148 return std::move(_M_promise->_M_storage);
1151 promise<void>* _M_promise;
1154 inline __future_base::_State_base::_Setter<void, void>
1155 __future_base::_State_base::__setter(promise<void>* __prom)
1157 return _Setter<void, void>{ __prom };
1161 promise<void>::set_value()
1163 auto __setter = _State::__setter(this);
1164 _M_future->_M_set_result(std::move(__setter));
1168 template<typename _StateT, typename _Res>
1169 struct __future_base::_Task_setter
1171 typename _StateT::_Ptr_type operator()()
1175 _M_state->_M_result->_M_set(_M_fn());
1179 _M_state->_M_result->_M_error = current_exception();
1181 return std::move(_M_state->_M_result);
1184 std::function<_Res()> _M_fn;
1187 template<typename _StateT>
1188 struct __future_base::_Task_setter<_StateT, void>
1190 typename _StateT::_Ptr_type operator()()
1198 _M_state->_M_result->_M_error = current_exception();
1200 return std::move(_M_state->_M_result);
1203 std::function<void()> _M_fn;
1206 template<typename _Res, typename... _Args>
1207 struct __future_base::_Task_state<_Res(_Args...)>
1208 : __future_base::_State_base
1210 typedef _Res _Res_type;
1212 _Task_state(std::function<_Res(_Args...)> __task)
1213 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1216 template<typename _Func, typename _Alloc>
1217 _Task_state(_Func&& __task, const _Alloc& __a)
1218 : _M_result(_S_allocate_result<_Res>(__a)),
1219 _M_task(allocator_arg, __a, std::move(__task))
1223 _M_run(_Args... __args)
1225 // bound arguments decay so wrap lvalue references
1226 auto __bound = std::bind<_Res>(std::ref(_M_task),
1227 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1228 _Task_setter<_Task_state> __setter{ this, std::move(__bound) };
1229 _M_set_result(std::move(__setter));
1232 template<typename, typename> friend class _Task_setter;
1233 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1234 _Ptr_type _M_result;
1235 std::function<_Res(_Args...)> _M_task;
1237 template<typename _Tp>
1238 static reference_wrapper<_Tp>
1239 _S_maybe_wrap_ref(_Tp& __t)
1240 { return std::ref(__t); }
1242 template<typename _Tp>
1243 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1245 _S_maybe_wrap_ref(_Tp&& __t)
1246 { return std::forward<_Tp>(__t); }
1250 template<typename _Res, typename... _ArgTypes>
1251 class packaged_task<_Res(_ArgTypes...)>
1253 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1254 shared_ptr<_State_type> _M_state;
1257 // Construction and destruction
1258 packaged_task() noexcept { }
1260 template<typename _Fn>
1262 packaged_task(_Fn&& __fn)
1263 : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn)))
1266 template<typename _Fn, typename _Allocator>
1268 packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn)
1269 : _M_state(std::allocate_shared<_State_type>(__a,
1270 std::forward<_Fn>(__fn)))
1275 if (static_cast<bool>(_M_state) && !_M_state.unique())
1276 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1280 packaged_task(packaged_task&) = delete;
1281 packaged_task& operator=(packaged_task&) = delete;
1284 packaged_task(packaged_task&& __other) noexcept
1285 { this->swap(__other); }
1287 packaged_task& operator=(packaged_task&& __other) noexcept
1289 packaged_task(std::move(__other)).swap(*this);
1294 swap(packaged_task& __other) noexcept
1295 { _M_state.swap(__other._M_state); }
1298 valid() const noexcept
1299 { return static_cast<bool>(_M_state); }
1304 { return future<_Res>(_M_state); }
1308 operator()(_ArgTypes... __args)
1310 __future_base::_State_base::_S_check(_M_state);
1311 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1317 __future_base::_State_base::_S_check(_M_state);
1318 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1323 template<typename _Res, typename... _ArgTypes>
1325 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1326 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1329 template<typename _Res, typename _Alloc>
1330 struct uses_allocator<packaged_task<_Res>, _Alloc>
1331 : public true_type { };
1334 template<typename _Res>
1335 class __future_base::_Deferred_state : public __future_base::_State_base
1338 typedef _Res _Res_type;
1341 _Deferred_state(std::function<_Res()>&& __fn)
1342 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1346 template<typename, typename> friend class _Task_setter;
1347 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1348 _Ptr_type _M_result;
1349 std::function<_Res()> _M_fn;
1354 _Task_setter<_Deferred_state> __setter{ this, _M_fn };
1355 // safe to call multiple times so ignore failure
1356 _M_set_result(std::move(__setter), true);
1360 template<typename _Res>
1361 class __future_base::_Async_state : public __future_base::_State_base
1364 typedef _Res _Res_type;
1367 _Async_state(std::function<_Res()>&& __fn)
1368 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1369 _M_thread(mem_fn(&_Async_state::_M_do_run), this)
1372 ~_Async_state() { _M_thread.join(); }
1377 _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) };
1378 _M_set_result(std::move(__setter));
1381 template<typename, typename> friend class _Task_setter;
1382 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1383 _Ptr_type _M_result;
1384 std::function<_Res()> _M_fn;
1389 template<typename _Fn, typename... _Args>
1390 future<typename result_of<_Fn(_Args...)>::type>
1391 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1393 typedef typename result_of<_Fn(_Args...)>::type result_type;
1394 std::shared_ptr<__future_base::_State_base> __state;
1395 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1397 typedef typename __future_base::_Async_state<result_type> _State;
1398 __state = std::make_shared<_State>(std::bind<result_type>(
1399 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1403 typedef typename __future_base::_Deferred_state<result_type> _State;
1404 __state = std::make_shared<_State>(std::bind<result_type>(
1405 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1407 return future<result_type>(__state);
1410 /// async, potential overload
1411 template<typename _Fn, typename... _Args>
1413 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
1414 async(_Fn&& __fn, _Args&&... __args)
1416 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1417 std::forward<_Args>(__args)...);
1420 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1421 // && _GLIBCXX_ATOMIC_BUILTINS_4
1424 _GLIBCXX_END_NAMESPACE_VERSION
1427 #endif // __GXX_EXPERIMENTAL_CXX0X__
1429 #endif // _GLIBCXX_FUTURE