1 // <future> -*- C++ -*-
3 // Copyright (C) 2009-2013 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 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
41 #include <condition_variable>
42 #include <system_error>
44 #include <bits/functexcept.h>
45 #include <bits/unique_ptr.h>
46 #include <bits/shared_ptr.h>
47 #include <bits/uses_allocator.h>
48 #include <bits/alloc_traits.h>
50 namespace std _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 * @defgroup futures Futures
56 * @ingroup concurrency
58 * Classes for futures support.
62 /// Error code for futures
63 enum class future_errc
65 future_already_retrieved = 1,
66 promise_already_satisfied,
73 struct is_error_code_enum<future_errc> : public true_type { };
75 /// Points to a statically-allocated object derived from error_category.
77 future_category() noexcept;
79 /// Overload for make_error_code.
81 make_error_code(future_errc __errc) noexcept
82 { return error_code(static_cast<int>(__errc), future_category()); }
84 /// Overload for make_error_condition.
85 inline error_condition
86 make_error_condition(future_errc __errc) noexcept
87 { return error_condition(static_cast<int>(__errc), future_category()); }
90 * @brief Exception type thrown by futures.
93 class future_error : public logic_error
98 explicit future_error(error_code __ec)
99 : logic_error("std::future_error"), _M_code(__ec)
102 virtual ~future_error() noexcept;
105 what() const noexcept;
108 code() const noexcept { return _M_code; }
111 // Forward declarations.
112 template<typename _Res>
115 template<typename _Res>
118 template<typename _Signature>
121 template<typename _Res>
124 /// Launch code for futures
131 constexpr launch operator&(launch __x, launch __y)
133 return static_cast<launch>(
134 static_cast<int>(__x) & static_cast<int>(__y));
137 constexpr launch operator|(launch __x, launch __y)
139 return static_cast<launch>(
140 static_cast<int>(__x) | static_cast<int>(__y));
143 constexpr launch operator^(launch __x, launch __y)
145 return static_cast<launch>(
146 static_cast<int>(__x) ^ static_cast<int>(__y));
149 constexpr launch operator~(launch __x)
150 { return static_cast<launch>(~static_cast<int>(__x)); }
152 inline launch& operator&=(launch& __x, launch __y)
153 { return __x = __x & __y; }
155 inline launch& operator|=(launch& __x, launch __y)
156 { return __x = __x | __y; }
158 inline launch& operator^=(launch& __x, launch __y)
159 { return __x = __x ^ __y; }
161 /// Status code for futures
162 enum class future_status
169 template<typename _Fn, typename... _Args>
170 future<typename result_of<_Fn(_Args...)>::type>
171 async(launch __policy, _Fn&& __fn, _Args&&... __args);
173 template<typename _Fn, typename... _Args>
174 future<typename result_of<_Fn(_Args...)>::type>
175 async(_Fn&& __fn, _Args&&... __args);
177 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
178 && (ATOMIC_INT_LOCK_FREE > 1)
180 /// Base class and enclosing scope.
183 /// Base class for results.
186 exception_ptr _M_error;
188 _Result_base(const _Result_base&) = delete;
189 _Result_base& operator=(const _Result_base&) = delete;
191 // _M_destroy() allows derived classes to control deallocation
192 virtual void _M_destroy() = 0;
196 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
201 virtual ~_Result_base();
205 template<typename _Res>
206 struct _Result : _Result_base
209 typedef alignment_of<_Res> __a_of;
210 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
211 typedef typename __align_storage::type __align_type;
213 __align_type _M_storage;
217 typedef _Res result_type;
219 _Result() noexcept : _M_initialized() { }
227 // Return lvalue, future will add const or rvalue-reference
229 _M_value() noexcept { return *static_cast<_Res*>(_M_addr()); }
232 _M_set(const _Res& __res)
234 ::new (_M_addr()) _Res(__res);
235 _M_initialized = true;
241 ::new (_M_addr()) _Res(std::move(__res));
242 _M_initialized = true;
246 void _M_destroy() { delete this; }
248 void* _M_addr() noexcept { return static_cast<void*>(&_M_storage); }
251 /// A unique_ptr based on the instantiating type.
252 template<typename _Res>
253 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
256 template<typename _Res, typename _Alloc>
257 struct _Result_alloc final : _Result<_Res>, _Alloc
259 typedef typename allocator_traits<_Alloc>::template
260 rebind_alloc<_Result_alloc> __allocator_type;
263 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
269 typedef allocator_traits<__allocator_type> __traits;
270 __allocator_type __a(*this);
271 __traits::destroy(__a, this);
272 __traits::deallocate(__a, this, 1);
276 template<typename _Res, typename _Allocator>
277 static _Ptr<_Result_alloc<_Res, _Allocator>>
278 _S_allocate_result(const _Allocator& __a)
280 typedef _Result_alloc<_Res, _Allocator> __result_type;
281 typedef allocator_traits<typename __result_type::__allocator_type>
283 typename __traits::allocator_type __a2(__a);
284 __result_type* __p = __traits::allocate(__a2, 1);
287 __traits::construct(__a2, __p, __a);
291 __traits::deallocate(__a2, __p, 1);
292 __throw_exception_again;
294 return _Ptr<__result_type>(__p);
297 template<typename _Res, typename _Tp>
298 static _Ptr<_Result<_Res>>
299 _S_allocate_result(const std::allocator<_Tp>& __a)
301 return _Ptr<_Result<_Res>>(new _Result<_Res>);
304 /// Base class for state between a promise and one or more
305 /// associated futures.
308 typedef _Ptr<_Result_base> _Ptr_type;
312 condition_variable _M_cond;
313 atomic_flag _M_retrieved;
317 _State_base() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
318 _State_base(const _State_base&) = delete;
319 _State_base& operator=(const _State_base&) = delete;
320 virtual ~_State_base();
326 unique_lock<mutex> __lock(_M_mutex);
327 _M_cond.wait(__lock, [&] { return _M_ready(); });
331 template<typename _Rep, typename _Period>
333 wait_for(const chrono::duration<_Rep, _Period>& __rel)
335 unique_lock<mutex> __lock(_M_mutex);
336 if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
337 return future_status::ready;
338 return future_status::timeout;
341 template<typename _Clock, typename _Duration>
343 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
345 unique_lock<mutex> __lock(_M_mutex);
346 if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
347 return future_status::ready;
348 return future_status::timeout;
352 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
355 // all calls to this function are serialized,
356 // side-effects of invoking __res only happen once
357 call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
360 _M_cond.notify_all();
361 else if (!__ignore_failure)
362 __throw_future_error(int(future_errc::promise_already_satisfied));
366 _M_break_promise(_Ptr_type __res)
368 if (static_cast<bool>(__res))
370 error_code __ec(make_error_code(future_errc::broken_promise));
371 __res->_M_error = copy_exception(future_error(__ec));
373 lock_guard<mutex> __lock(_M_mutex);
374 _M_result.swap(__res);
376 _M_cond.notify_all();
380 // Called when this object is passed to a future.
382 _M_set_retrieved_flag()
384 if (_M_retrieved.test_and_set())
385 __throw_future_error(int(future_errc::future_already_retrieved));
388 template<typename _Res, typename _Arg>
392 template<typename _Res, typename _Arg>
393 struct _Setter<_Res, _Arg&>
395 // check this is only used by promise<R>::set_value(const R&)
396 // or promise<R>::set_value(R&)
397 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
398 || is_same<const _Res, _Arg>::value, // promise<R>
399 "Invalid specialisation");
401 typename promise<_Res>::_Ptr_type operator()()
403 _State_base::_S_check(_M_promise->_M_future);
404 _M_promise->_M_storage->_M_set(_M_arg);
405 return std::move(_M_promise->_M_storage);
407 promise<_Res>* _M_promise;
412 template<typename _Res>
413 struct _Setter<_Res, _Res&&>
415 typename promise<_Res>::_Ptr_type operator()()
417 _State_base::_S_check(_M_promise->_M_future);
418 _M_promise->_M_storage->_M_set(std::move(_M_arg));
419 return std::move(_M_promise->_M_storage);
421 promise<_Res>* _M_promise;
425 struct __exception_ptr_tag { };
428 template<typename _Res>
429 struct _Setter<_Res, __exception_ptr_tag>
431 typename promise<_Res>::_Ptr_type operator()()
433 _State_base::_S_check(_M_promise->_M_future);
434 _M_promise->_M_storage->_M_error = _M_ex;
435 return std::move(_M_promise->_M_storage);
438 promise<_Res>* _M_promise;
439 exception_ptr& _M_ex;
442 template<typename _Res, typename _Arg>
443 static _Setter<_Res, _Arg&&>
444 __setter(promise<_Res>* __prom, _Arg&& __arg)
446 return _Setter<_Res, _Arg&&>{ __prom, __arg };
449 template<typename _Res>
450 static _Setter<_Res, __exception_ptr_tag>
451 __setter(exception_ptr& __ex, promise<_Res>* __prom)
453 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
456 static _Setter<void, void>
457 __setter(promise<void>* __prom);
459 template<typename _Tp>
461 _S_check(const shared_ptr<_Tp>& __p)
463 if (!static_cast<bool>(__p))
464 __throw_future_error((int)future_errc::no_state);
469 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
471 _Ptr_type __res = __f();
473 lock_guard<mutex> __lock(_M_mutex);
474 _M_result.swap(__res);
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>
494 class _Task_state_base;
496 template<typename _Fn, typename _Alloc, typename _Signature>
499 template<typename _BoundFn>
500 static std::shared_ptr<_State_base>
501 _S_make_deferred_state(_BoundFn&& __fn);
503 template<typename _BoundFn>
504 static std::shared_ptr<_State_base>
505 _S_make_async_state(_BoundFn&& __fn);
507 template<typename _Res_ptr,
508 typename _Res = typename _Res_ptr::element_type::result_type>
511 template<typename _Res_ptr, typename _BoundFn>
512 static _Task_setter<_Res_ptr>
513 _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
515 return _Task_setter<_Res_ptr>{ __ptr, std::ref(__call) };
519 /// Partial specialization for reference types.
520 template<typename _Res>
521 struct __future_base::_Result<_Res&> : __future_base::_Result_base
523 typedef _Res& result_type;
525 _Result() noexcept : _M_value_ptr() { }
527 void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
529 _Res& _M_get() noexcept { return *_M_value_ptr; }
534 void _M_destroy() { delete this; }
537 /// Explicit specialization for void.
539 struct __future_base::_Result<void> : __future_base::_Result_base
541 typedef void result_type;
544 void _M_destroy() { delete this; }
548 /// Common implementation for future and shared_future.
549 template<typename _Res>
550 class __basic_future : public __future_base
553 typedef shared_ptr<_State_base> __state_type;
554 typedef __future_base::_Result<_Res>& __result_type;
557 __state_type _M_state;
561 __basic_future(const __basic_future&) = delete;
562 __basic_future& operator=(const __basic_future&) = delete;
565 valid() const noexcept { return static_cast<bool>(_M_state); }
570 _State_base::_S_check(_M_state);
574 template<typename _Rep, typename _Period>
576 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
578 _State_base::_S_check(_M_state);
579 return _M_state->wait_for(__rel);
582 template<typename _Clock, typename _Duration>
584 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
586 _State_base::_S_check(_M_state);
587 return _M_state->wait_until(__abs);
591 /// Wait for the state to be ready and rethrow any stored exception
593 _M_get_result() const
595 _State_base::_S_check(_M_state);
596 _Result_base& __res = _M_state->wait();
597 if (!(__res._M_error == 0))
598 rethrow_exception(__res._M_error);
599 return static_cast<__result_type>(__res);
602 void _M_swap(__basic_future& __that) noexcept
604 _M_state.swap(__that._M_state);
607 // Construction of a future by promise::get_future()
609 __basic_future(const __state_type& __state) : _M_state(__state)
611 _State_base::_S_check(_M_state);
612 _M_state->_M_set_retrieved_flag();
615 // Copy construction from a shared_future
617 __basic_future(const shared_future<_Res>&) noexcept;
619 // Move construction from a shared_future
621 __basic_future(shared_future<_Res>&&) noexcept;
623 // Move construction from a future
625 __basic_future(future<_Res>&&) noexcept;
627 constexpr __basic_future() noexcept : _M_state() { }
631 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
632 ~_Reset() { _M_fut._M_state.reset(); }
633 __basic_future& _M_fut;
638 /// Primary template for future.
639 template<typename _Res>
640 class future : public __basic_future<_Res>
642 friend class promise<_Res>;
643 template<typename> friend class packaged_task;
644 template<typename _Fn, typename... _Args>
645 friend future<typename result_of<_Fn(_Args...)>::type>
646 async(launch, _Fn&&, _Args&&...);
648 typedef __basic_future<_Res> _Base_type;
649 typedef typename _Base_type::__state_type __state_type;
652 future(const __state_type& __state) : _Base_type(__state) { }
655 constexpr future() noexcept : _Base_type() { }
658 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
661 future(const future&) = delete;
662 future& operator=(const future&) = delete;
664 future& operator=(future&& __fut) noexcept
666 future(std::move(__fut))._M_swap(*this);
670 /// Retrieving the value
674 typename _Base_type::_Reset __reset(*this);
675 return std::move(this->_M_get_result()._M_value());
678 shared_future<_Res> share();
681 /// Partial specialization for future<R&>
682 template<typename _Res>
683 class future<_Res&> : public __basic_future<_Res&>
685 friend class promise<_Res&>;
686 template<typename> friend class packaged_task;
687 template<typename _Fn, typename... _Args>
688 friend future<typename result_of<_Fn(_Args...)>::type>
689 async(launch, _Fn&&, _Args&&...);
691 typedef __basic_future<_Res&> _Base_type;
692 typedef typename _Base_type::__state_type __state_type;
695 future(const __state_type& __state) : _Base_type(__state) { }
698 constexpr future() noexcept : _Base_type() { }
701 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
704 future(const future&) = delete;
705 future& operator=(const future&) = delete;
707 future& operator=(future&& __fut) noexcept
709 future(std::move(__fut))._M_swap(*this);
713 /// Retrieving the value
717 typename _Base_type::_Reset __reset(*this);
718 return this->_M_get_result()._M_get();
721 shared_future<_Res&> share();
724 /// Explicit specialization for future<void>
726 class future<void> : public __basic_future<void>
728 friend class promise<void>;
729 template<typename> friend class packaged_task;
730 template<typename _Fn, typename... _Args>
731 friend future<typename result_of<_Fn(_Args...)>::type>
732 async(launch, _Fn&&, _Args&&...);
734 typedef __basic_future<void> _Base_type;
735 typedef typename _Base_type::__state_type __state_type;
738 future(const __state_type& __state) : _Base_type(__state) { }
741 constexpr future() noexcept : _Base_type() { }
744 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
747 future(const future&) = delete;
748 future& operator=(const future&) = delete;
750 future& operator=(future&& __fut) noexcept
752 future(std::move(__fut))._M_swap(*this);
756 /// Retrieving the value
760 typename _Base_type::_Reset __reset(*this);
761 this->_M_get_result();
764 shared_future<void> share();
768 /// Primary template for shared_future.
769 template<typename _Res>
770 class shared_future : public __basic_future<_Res>
772 typedef __basic_future<_Res> _Base_type;
775 constexpr shared_future() noexcept : _Base_type() { }
778 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
780 /// Construct from a future rvalue
781 shared_future(future<_Res>&& __uf) noexcept
782 : _Base_type(std::move(__uf))
785 /// Construct from a shared_future rvalue
786 shared_future(shared_future&& __sf) noexcept
787 : _Base_type(std::move(__sf))
790 shared_future& operator=(const shared_future& __sf)
792 shared_future(__sf)._M_swap(*this);
796 shared_future& operator=(shared_future&& __sf) noexcept
798 shared_future(std::move(__sf))._M_swap(*this);
802 /// Retrieving the value
804 get() const { return this->_M_get_result()._M_value(); }
807 /// Partial specialization for shared_future<R&>
808 template<typename _Res>
809 class shared_future<_Res&> : public __basic_future<_Res&>
811 typedef __basic_future<_Res&> _Base_type;
814 constexpr shared_future() noexcept : _Base_type() { }
817 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
819 /// Construct from a future rvalue
820 shared_future(future<_Res&>&& __uf) noexcept
821 : _Base_type(std::move(__uf))
824 /// Construct from a shared_future rvalue
825 shared_future(shared_future&& __sf) noexcept
826 : _Base_type(std::move(__sf))
829 shared_future& operator=(const shared_future& __sf)
831 shared_future(__sf)._M_swap(*this);
835 shared_future& operator=(shared_future&& __sf) noexcept
837 shared_future(std::move(__sf))._M_swap(*this);
841 /// Retrieving the value
843 get() const { return this->_M_get_result()._M_get(); }
846 /// Explicit specialization for shared_future<void>
848 class shared_future<void> : public __basic_future<void>
850 typedef __basic_future<void> _Base_type;
853 constexpr shared_future() noexcept : _Base_type() { }
856 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
858 /// Construct from a future rvalue
859 shared_future(future<void>&& __uf) noexcept
860 : _Base_type(std::move(__uf))
863 /// Construct from a shared_future rvalue
864 shared_future(shared_future&& __sf) noexcept
865 : _Base_type(std::move(__sf))
868 shared_future& operator=(const shared_future& __sf)
870 shared_future(__sf)._M_swap(*this);
874 shared_future& operator=(shared_future&& __sf) noexcept
876 shared_future(std::move(__sf))._M_swap(*this);
880 // Retrieving the value
882 get() const { this->_M_get_result(); }
885 // Now we can define the protected __basic_future constructors.
886 template<typename _Res>
887 inline __basic_future<_Res>::
888 __basic_future(const shared_future<_Res>& __sf) noexcept
889 : _M_state(__sf._M_state)
892 template<typename _Res>
893 inline __basic_future<_Res>::
894 __basic_future(shared_future<_Res>&& __sf) noexcept
895 : _M_state(std::move(__sf._M_state))
898 template<typename _Res>
899 inline __basic_future<_Res>::
900 __basic_future(future<_Res>&& __uf) noexcept
901 : _M_state(std::move(__uf._M_state))
904 template<typename _Res>
905 inline shared_future<_Res>
906 future<_Res>::share()
907 { return shared_future<_Res>(std::move(*this)); }
909 template<typename _Res>
910 inline shared_future<_Res&>
911 future<_Res&>::share()
912 { return shared_future<_Res&>(std::move(*this)); }
914 inline shared_future<void>
915 future<void>::share()
916 { return shared_future<void>(std::move(*this)); }
918 /// Primary template for promise
919 template<typename _Res>
922 typedef __future_base::_State_base _State;
923 typedef __future_base::_Result<_Res> _Res_type;
924 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
925 template<typename, typename> friend class _State::_Setter;
927 shared_ptr<_State> _M_future;
928 _Ptr_type _M_storage;
932 : _M_future(std::make_shared<_State>()),
933 _M_storage(new _Res_type())
936 promise(promise&& __rhs) noexcept
937 : _M_future(std::move(__rhs._M_future)),
938 _M_storage(std::move(__rhs._M_storage))
941 template<typename _Allocator>
942 promise(allocator_arg_t, const _Allocator& __a)
943 : _M_future(std::allocate_shared<_State>(__a)),
944 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
947 template<typename _Allocator>
948 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
949 : _M_future(std::move(__rhs._M_future)),
950 _M_storage(std::move(__rhs._M_storage))
953 promise(const promise&) = delete;
957 if (static_cast<bool>(_M_future) && !_M_future.unique())
958 _M_future->_M_break_promise(std::move(_M_storage));
963 operator=(promise&& __rhs) noexcept
965 promise(std::move(__rhs)).swap(*this);
969 promise& operator=(const promise&) = delete;
972 swap(promise& __rhs) noexcept
974 _M_future.swap(__rhs._M_future);
975 _M_storage.swap(__rhs._M_storage);
978 // Retrieving the result
981 { return future<_Res>(_M_future); }
983 // Setting the result
985 set_value(const _Res& __r)
987 auto __future = _M_future;
988 auto __setter = _State::__setter(this, __r);
989 __future->_M_set_result(std::move(__setter));
993 set_value(_Res&& __r)
995 auto __future = _M_future;
996 auto __setter = _State::__setter(this, std::move(__r));
997 __future->_M_set_result(std::move(__setter));
1001 set_exception(exception_ptr __p)
1003 auto __future = _M_future;
1004 auto __setter = _State::__setter(__p, this);
1005 __future->_M_set_result(std::move(__setter));
1009 template<typename _Res>
1011 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1014 template<typename _Res, typename _Alloc>
1015 struct uses_allocator<promise<_Res>, _Alloc>
1016 : public true_type { };
1019 /// Partial specialization for promise<R&>
1020 template<typename _Res>
1021 class promise<_Res&>
1023 typedef __future_base::_State_base _State;
1024 typedef __future_base::_Result<_Res&> _Res_type;
1025 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1026 template<typename, typename> friend class _State::_Setter;
1028 shared_ptr<_State> _M_future;
1029 _Ptr_type _M_storage;
1033 : _M_future(std::make_shared<_State>()),
1034 _M_storage(new _Res_type())
1037 promise(promise&& __rhs) noexcept
1038 : _M_future(std::move(__rhs._M_future)),
1039 _M_storage(std::move(__rhs._M_storage))
1042 template<typename _Allocator>
1043 promise(allocator_arg_t, const _Allocator& __a)
1044 : _M_future(std::allocate_shared<_State>(__a)),
1045 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1048 template<typename _Allocator>
1049 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1050 : _M_future(std::move(__rhs._M_future)),
1051 _M_storage(std::move(__rhs._M_storage))
1054 promise(const promise&) = delete;
1058 if (static_cast<bool>(_M_future) && !_M_future.unique())
1059 _M_future->_M_break_promise(std::move(_M_storage));
1064 operator=(promise&& __rhs) noexcept
1066 promise(std::move(__rhs)).swap(*this);
1070 promise& operator=(const promise&) = delete;
1073 swap(promise& __rhs) noexcept
1075 _M_future.swap(__rhs._M_future);
1076 _M_storage.swap(__rhs._M_storage);
1079 // Retrieving the result
1082 { return future<_Res&>(_M_future); }
1084 // Setting the result
1086 set_value(_Res& __r)
1088 auto __future = _M_future;
1089 auto __setter = _State::__setter(this, __r);
1090 __future->_M_set_result(std::move(__setter));
1094 set_exception(exception_ptr __p)
1096 auto __future = _M_future;
1097 auto __setter = _State::__setter(__p, this);
1098 __future->_M_set_result(std::move(__setter));
1102 /// Explicit specialization for promise<void>
1106 typedef __future_base::_State_base _State;
1107 typedef __future_base::_Result<void> _Res_type;
1108 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1109 template<typename, typename> friend class _State::_Setter;
1111 shared_ptr<_State> _M_future;
1112 _Ptr_type _M_storage;
1116 : _M_future(std::make_shared<_State>()),
1117 _M_storage(new _Res_type())
1120 promise(promise&& __rhs) noexcept
1121 : _M_future(std::move(__rhs._M_future)),
1122 _M_storage(std::move(__rhs._M_storage))
1125 template<typename _Allocator>
1126 promise(allocator_arg_t, const _Allocator& __a)
1127 : _M_future(std::allocate_shared<_State>(__a)),
1128 _M_storage(__future_base::_S_allocate_result<void>(__a))
1131 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1132 // 2095. missing constructors needed for uses-allocator construction
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 __future = _M_future;
1176 auto __setter = _State::__setter(__p, this);
1177 __future->_M_set_result(std::move(__setter));
1183 struct __future_base::_State_base::_Setter<void, void>
1185 promise<void>::_Ptr_type operator()()
1187 _State_base::_S_check(_M_promise->_M_future);
1188 return std::move(_M_promise->_M_storage);
1191 promise<void>* _M_promise;
1194 inline __future_base::_State_base::_Setter<void, void>
1195 __future_base::_State_base::__setter(promise<void>* __prom)
1197 return _Setter<void, void>{ __prom };
1201 promise<void>::set_value()
1203 auto __future = _M_future;
1204 auto __setter = _State::__setter(this);
1205 __future->_M_set_result(std::move(__setter));
1209 template<typename _Ptr_type, typename _Res>
1210 struct __future_base::_Task_setter
1212 _Ptr_type operator()()
1216 _M_result->_M_set(_M_fn());
1220 _M_result->_M_error = current_exception();
1222 return std::move(_M_result);
1224 _Ptr_type& _M_result;
1225 std::function<_Res()> _M_fn;
1228 template<typename _Ptr_type>
1229 struct __future_base::_Task_setter<_Ptr_type, void>
1231 _Ptr_type operator()()
1239 _M_result->_M_error = current_exception();
1241 return std::move(_M_result);
1243 _Ptr_type& _M_result;
1244 std::function<void()> _M_fn;
1247 template<typename _Res, typename... _Args>
1248 struct __future_base::_Task_state_base<_Res(_Args...)>
1249 : __future_base::_State_base
1251 typedef _Res _Res_type;
1253 template<typename _Alloc>
1254 _Task_state_base(const _Alloc& __a)
1255 : _M_result(_S_allocate_result<_Res>(__a))
1259 _M_run(_Args... __args) = 0;
1261 virtual shared_ptr<_Task_state_base>
1264 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1265 _Ptr_type _M_result;
1268 template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
1269 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1270 : __future_base::_Task_state_base<_Res(_Args...)>
1272 template<typename _Fn2>
1273 _Task_state(_Fn2&& __fn, const _Alloc& __a)
1274 : _Task_state_base<_Res(_Args...)>(__a),
1275 _M_impl(std::forward<_Fn2>(__fn), __a)
1280 _M_run(_Args... __args)
1282 // bound arguments decay so wrap lvalue references
1283 auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
1284 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1285 auto __setter = _S_task_setter(this->_M_result, std::move(__boundfn));
1286 this->_M_set_result(std::move(__setter));
1289 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1292 template<typename _Tp>
1293 static reference_wrapper<_Tp>
1294 _S_maybe_wrap_ref(_Tp& __t)
1295 { return std::ref(__t); }
1297 template<typename _Tp>
1299 typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
1300 _S_maybe_wrap_ref(_Tp&& __t)
1301 { return std::forward<_Tp>(__t); }
1303 struct _Impl : _Alloc
1305 template<typename _Fn2>
1306 _Impl(_Fn2&& __fn, const _Alloc& __a)
1307 : _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { }
1312 template<typename _Signature, typename _Fn, typename _Alloc>
1313 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1314 __create_task_state(_Fn&& __fn, const _Alloc& __a)
1316 typedef typename decay<_Fn>::type _Fn2;
1317 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1318 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1321 template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
1322 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1323 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1325 return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
1326 static_cast<_Alloc&>(_M_impl));
1329 template<typename _Task, typename _Fn, bool
1330 = is_same<_Task, typename decay<_Fn>::type>::value>
1331 struct __constrain_pkgdtask
1332 { typedef void __type; };
1334 template<typename _Task, typename _Fn>
1335 struct __constrain_pkgdtask<_Task, _Fn, true>
1339 template<typename _Res, typename... _ArgTypes>
1340 class packaged_task<_Res(_ArgTypes...)>
1342 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1343 shared_ptr<_State_type> _M_state;
1346 // Construction and destruction
1347 packaged_task() noexcept { }
1349 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1350 // 2095. missing constructors needed for uses-allocator construction
1351 template<typename _Allocator>
1352 packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
1355 template<typename _Fn, typename = typename
1356 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1358 packaged_task(_Fn&& __fn)
1359 : packaged_task(allocator_arg, std::allocator<int>(),
1360 std::forward<_Fn>(__fn))
1363 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1364 // 2097. packaged_task constructors should be constrained
1365 template<typename _Fn, typename _Alloc, typename = typename
1366 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1368 packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
1369 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1370 std::forward<_Fn>(__fn), __a))
1375 if (static_cast<bool>(_M_state) && !_M_state.unique())
1376 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1380 packaged_task(const packaged_task&) = delete;
1381 packaged_task& operator=(const packaged_task&) = delete;
1383 template<typename _Allocator>
1384 packaged_task(allocator_arg_t, const _Allocator&,
1385 const packaged_task&) = delete;
1388 packaged_task(packaged_task&& __other) noexcept
1389 { this->swap(__other); }
1391 template<typename _Allocator>
1392 packaged_task(allocator_arg_t, const _Allocator&,
1393 packaged_task&& __other) noexcept
1394 { this->swap(__other); }
1396 packaged_task& operator=(packaged_task&& __other) noexcept
1398 packaged_task(std::move(__other)).swap(*this);
1403 swap(packaged_task& __other) noexcept
1404 { _M_state.swap(__other._M_state); }
1407 valid() const noexcept
1408 { return static_cast<bool>(_M_state); }
1413 { return future<_Res>(_M_state); }
1417 operator()(_ArgTypes... __args)
1419 __future_base::_State_base::_S_check(_M_state);
1420 auto __state = _M_state;
1421 __state->_M_run(std::forward<_ArgTypes>(__args)...);
1427 __future_base::_State_base::_S_check(_M_state);
1428 packaged_task __tmp;
1429 __tmp._M_state = _M_state;
1430 _M_state = _M_state->_M_reset();
1435 template<typename _Res, typename... _ArgTypes>
1437 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1438 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1441 template<typename _Res, typename _Alloc>
1442 struct uses_allocator<packaged_task<_Res>, _Alloc>
1443 : public true_type { };
1446 template<typename _BoundFn, typename _Res>
1447 class __future_base::_Deferred_state final
1448 : public __future_base::_State_base
1452 _Deferred_state(_BoundFn&& __fn)
1453 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1457 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1458 _Ptr_type _M_result;
1464 // safe to call multiple times so ignore failure
1465 _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1469 class __future_base::_Async_state_common : public __future_base::_State_base
1472 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
1473 ~_Async_state_common();
1475 ~_Async_state_common() = default;
1478 // Allow non-timed waiting functions to block until the thread completes,
1480 virtual void _M_run_deferred() { _M_join(); }
1482 void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
1488 template<typename _BoundFn, typename _Res>
1489 class __future_base::_Async_state_impl final
1490 : public __future_base::_Async_state_common
1494 _Async_state_impl(_BoundFn&& __fn)
1495 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1497 _M_thread = std::thread{ [this] {
1498 _M_set_result(_S_task_setter(_M_result, _M_fn));
1502 ~_Async_state_impl() { _M_join(); }
1505 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1506 _Ptr_type _M_result;
1510 template<typename _BoundFn>
1511 inline std::shared_ptr<__future_base::_State_base>
1512 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1514 typedef typename remove_reference<_BoundFn>::type __fn_type;
1515 typedef _Deferred_state<__fn_type> __state_type;
1516 return std::make_shared<__state_type>(std::move(__fn));
1519 template<typename _BoundFn>
1520 inline std::shared_ptr<__future_base::_State_base>
1521 __future_base::_S_make_async_state(_BoundFn&& __fn)
1523 typedef typename remove_reference<_BoundFn>::type __fn_type;
1524 typedef _Async_state_impl<__fn_type> __state_type;
1525 return std::make_shared<__state_type>(std::move(__fn));
1530 template<typename _Fn, typename... _Args>
1531 future<typename result_of<_Fn(_Args...)>::type>
1532 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1534 typedef typename result_of<_Fn(_Args...)>::type result_type;
1535 std::shared_ptr<__future_base::_State_base> __state;
1536 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1538 __state = __future_base::_S_make_async_state(std::__bind_simple(
1539 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1543 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1544 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1546 return future<result_type>(__state);
1549 /// async, potential overload
1550 template<typename _Fn, typename... _Args>
1551 inline future<typename result_of<_Fn(_Args...)>::type>
1552 async(_Fn&& __fn, _Args&&... __args)
1554 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1555 std::forward<_Args>(__args)...);
1558 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1559 // && ATOMIC_INT_LOCK_FREE
1562 _GLIBCXX_END_NAMESPACE_VERSION
1567 #endif // _GLIBCXX_FUTURE