1 // <future> -*- C++ -*-
3 // Copyright (C) 2009-2014 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>
49 #include <ext/aligned_buffer.h>
51 namespace std _GLIBCXX_VISIBILITY(default)
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 * @defgroup futures Futures
57 * @ingroup concurrency
59 * Classes for futures support.
63 /// Error code for futures
64 enum class future_errc
66 future_already_retrieved = 1,
67 promise_already_satisfied,
74 struct is_error_code_enum<future_errc> : public true_type { };
76 /// Points to a statically-allocated object derived from error_category.
78 future_category() noexcept;
80 /// Overload for make_error_code.
82 make_error_code(future_errc __errc) noexcept
83 { return error_code(static_cast<int>(__errc), future_category()); }
85 /// Overload for make_error_condition.
86 inline error_condition
87 make_error_condition(future_errc __errc) noexcept
88 { return error_condition(static_cast<int>(__errc), future_category()); }
91 * @brief Exception type thrown by futures.
94 class future_error : public logic_error
99 explicit future_error(error_code __ec)
100 : logic_error("std::future_error"), _M_code(__ec)
103 virtual ~future_error() noexcept;
106 what() const noexcept;
109 code() const noexcept { return _M_code; }
112 // Forward declarations.
113 template<typename _Res>
116 template<typename _Res>
119 template<typename _Signature>
122 template<typename _Res>
125 /// Launch code for futures
132 constexpr launch operator&(launch __x, launch __y)
134 return static_cast<launch>(
135 static_cast<int>(__x) & static_cast<int>(__y));
138 constexpr launch operator|(launch __x, launch __y)
140 return static_cast<launch>(
141 static_cast<int>(__x) | static_cast<int>(__y));
144 constexpr launch operator^(launch __x, launch __y)
146 return static_cast<launch>(
147 static_cast<int>(__x) ^ static_cast<int>(__y));
150 constexpr launch operator~(launch __x)
151 { return static_cast<launch>(~static_cast<int>(__x)); }
153 inline launch& operator&=(launch& __x, launch __y)
154 { return __x = __x & __y; }
156 inline launch& operator|=(launch& __x, launch __y)
157 { return __x = __x | __y; }
159 inline launch& operator^=(launch& __x, launch __y)
160 { return __x = __x ^ __y; }
162 /// Status code for futures
163 enum class future_status
170 template<typename _Fn, typename... _Args>
171 future<typename result_of<_Fn(_Args...)>::type>
172 async(launch __policy, _Fn&& __fn, _Args&&... __args);
174 template<typename _Fn, typename... _Args>
175 future<typename result_of<_Fn(_Args...)>::type>
176 async(_Fn&& __fn, _Args&&... __args);
178 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
179 && (ATOMIC_INT_LOCK_FREE > 1)
181 /// Base class and enclosing scope.
184 /// Base class for results.
187 exception_ptr _M_error;
189 _Result_base(const _Result_base&) = delete;
190 _Result_base& operator=(const _Result_base&) = delete;
192 // _M_destroy() allows derived classes to control deallocation
193 virtual void _M_destroy() = 0;
197 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
202 virtual ~_Result_base();
206 template<typename _Res>
207 struct _Result : _Result_base
210 __gnu_cxx::__aligned_buffer<_Res> _M_storage;
214 typedef _Res result_type;
216 _Result() noexcept : _M_initialized() { }
224 // Return lvalue, future will add const or rvalue-reference
226 _M_value() noexcept { return *_M_storage._M_ptr(); }
229 _M_set(const _Res& __res)
231 ::new (_M_storage._M_addr()) _Res(__res);
232 _M_initialized = true;
238 ::new (_M_storage._M_addr()) _Res(std::move(__res));
239 _M_initialized = true;
243 void _M_destroy() { delete this; }
246 /// A unique_ptr based on the instantiating type.
247 template<typename _Res>
248 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
251 template<typename _Res, typename _Alloc>
252 struct _Result_alloc final : _Result<_Res>, _Alloc
254 typedef typename allocator_traits<_Alloc>::template
255 rebind_alloc<_Result_alloc> __allocator_type;
258 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
264 typedef allocator_traits<__allocator_type> __traits;
265 __allocator_type __a(*this);
266 __traits::destroy(__a, this);
267 __traits::deallocate(__a, this, 1);
271 template<typename _Res, typename _Allocator>
272 static _Ptr<_Result_alloc<_Res, _Allocator>>
273 _S_allocate_result(const _Allocator& __a)
275 typedef _Result_alloc<_Res, _Allocator> __result_type;
276 typedef allocator_traits<typename __result_type::__allocator_type>
278 typename __traits::allocator_type __a2(__a);
279 __result_type* __p = __traits::allocate(__a2, 1);
282 __traits::construct(__a2, __p, __a);
286 __traits::deallocate(__a2, __p, 1);
287 __throw_exception_again;
289 return _Ptr<__result_type>(__p);
292 template<typename _Res, typename _Tp>
293 static _Ptr<_Result<_Res>>
294 _S_allocate_result(const std::allocator<_Tp>& __a)
296 return _Ptr<_Result<_Res>>(new _Result<_Res>);
299 /// Base class for state between a promise and one or more
300 /// associated futures.
303 typedef _Ptr<_Result_base> _Ptr_type;
307 condition_variable _M_cond;
308 atomic_flag _M_retrieved;
312 _State_baseV2() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT)
314 _State_baseV2(const _State_baseV2&) = delete;
315 _State_baseV2& operator=(const _State_baseV2&) = delete;
316 virtual ~_State_baseV2() = default;
322 unique_lock<mutex> __lock(_M_mutex);
323 _M_cond.wait(__lock, [&] { return _M_ready(); });
327 template<typename _Rep, typename _Period>
329 wait_for(const chrono::duration<_Rep, _Period>& __rel)
331 unique_lock<mutex> __lock(_M_mutex);
333 return future_status::ready;
334 if (_M_has_deferred())
335 return future_status::deferred;
336 if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
338 // _GLIBCXX_RESOLVE_LIB_DEFECTS
339 // 2100. timed waiting functions must also join
341 return future_status::ready;
343 return future_status::timeout;
346 template<typename _Clock, typename _Duration>
348 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
350 unique_lock<mutex> __lock(_M_mutex);
352 return future_status::ready;
353 if (_M_has_deferred())
354 return future_status::deferred;
355 if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
357 // _GLIBCXX_RESOLVE_LIB_DEFECTS
358 // 2100. timed waiting functions must also join
360 return future_status::ready;
362 return future_status::timeout;
366 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
368 bool __set = __ignore_failure;
369 // all calls to this function are serialized,
370 // side-effects of invoking __res only happen once
371 call_once(_M_once, &_State_baseV2::_M_do_set, this, ref(__res),
374 __throw_future_error(int(future_errc::promise_already_satisfied));
378 _M_break_promise(_Ptr_type __res)
380 if (static_cast<bool>(__res))
382 error_code __ec(make_error_code(future_errc::broken_promise));
383 __res->_M_error = make_exception_ptr(future_error(__ec));
385 lock_guard<mutex> __lock(_M_mutex);
386 _M_result.swap(__res);
388 _M_cond.notify_all();
392 // Called when this object is passed to a future.
394 _M_set_retrieved_flag()
396 if (_M_retrieved.test_and_set())
397 __throw_future_error(int(future_errc::future_already_retrieved));
400 template<typename _Res, typename _Arg>
404 template<typename _Res, typename _Arg>
405 struct _Setter<_Res, _Arg&>
407 // check this is only used by promise<R>::set_value(const R&)
408 // or promise<R>::set_value(R&)
409 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
410 || is_same<const _Res, _Arg>::value, // promise<R>
411 "Invalid specialisation");
413 typename promise<_Res>::_Ptr_type operator()()
415 _State_baseV2::_S_check(_M_promise->_M_future);
416 _M_promise->_M_storage->_M_set(_M_arg);
417 return std::move(_M_promise->_M_storage);
419 promise<_Res>* _M_promise;
424 template<typename _Res>
425 struct _Setter<_Res, _Res&&>
427 typename promise<_Res>::_Ptr_type operator()()
429 _State_baseV2::_S_check(_M_promise->_M_future);
430 _M_promise->_M_storage->_M_set(std::move(_M_arg));
431 return std::move(_M_promise->_M_storage);
433 promise<_Res>* _M_promise;
437 struct __exception_ptr_tag { };
440 template<typename _Res>
441 struct _Setter<_Res, __exception_ptr_tag>
443 typename promise<_Res>::_Ptr_type operator()()
445 _State_baseV2::_S_check(_M_promise->_M_future);
446 _M_promise->_M_storage->_M_error = _M_ex;
447 return std::move(_M_promise->_M_storage);
450 promise<_Res>* _M_promise;
451 exception_ptr& _M_ex;
454 template<typename _Res, typename _Arg>
455 static _Setter<_Res, _Arg&&>
456 __setter(promise<_Res>* __prom, _Arg&& __arg)
458 return _Setter<_Res, _Arg&&>{ __prom, __arg };
461 template<typename _Res>
462 static _Setter<_Res, __exception_ptr_tag>
463 __setter(exception_ptr& __ex, promise<_Res>* __prom)
465 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
468 static _Setter<void, void>
469 __setter(promise<void>* __prom);
471 template<typename _Tp>
473 _S_check(const shared_ptr<_Tp>& __p)
475 if (!static_cast<bool>(__p))
476 __throw_future_error((int)future_errc::no_state);
481 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
483 _Ptr_type __res = __f();
485 lock_guard<mutex> __lock(_M_mutex);
486 _M_result.swap(__res);
488 _M_cond.notify_all();
492 bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
494 // Wait for completion of async function.
495 virtual void _M_complete_async() { }
497 // Return true if state contains a deferred function.
498 virtual bool _M_has_deferred() const { return false; }
501 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
503 class _Async_state_common;
505 using _State_base = _State_baseV2;
506 class _Async_state_commonV2;
509 template<typename _BoundFn, typename = typename _BoundFn::result_type>
510 class _Deferred_state;
512 template<typename _BoundFn, typename = typename _BoundFn::result_type>
513 class _Async_state_impl;
515 template<typename _Signature>
516 class _Task_state_base;
518 template<typename _Fn, typename _Alloc, typename _Signature>
521 template<typename _BoundFn>
522 static std::shared_ptr<_State_base>
523 _S_make_deferred_state(_BoundFn&& __fn);
525 template<typename _BoundFn>
526 static std::shared_ptr<_State_base>
527 _S_make_async_state(_BoundFn&& __fn);
529 template<typename _Res_ptr,
530 typename _Res = typename _Res_ptr::element_type::result_type>
533 template<typename _Res_ptr, typename _BoundFn>
534 static _Task_setter<_Res_ptr>
535 _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
537 return _Task_setter<_Res_ptr>{ __ptr, std::ref(__call) };
541 /// Partial specialization for reference types.
542 template<typename _Res>
543 struct __future_base::_Result<_Res&> : __future_base::_Result_base
545 typedef _Res& result_type;
547 _Result() noexcept : _M_value_ptr() { }
549 void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
551 _Res& _M_get() noexcept { return *_M_value_ptr; }
556 void _M_destroy() { delete this; }
559 /// Explicit specialization for void.
561 struct __future_base::_Result<void> : __future_base::_Result_base
563 typedef void result_type;
566 void _M_destroy() { delete this; }
569 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
571 /// Common implementation for future and shared_future.
572 template<typename _Res>
573 class __basic_future : public __future_base
576 typedef shared_ptr<_State_base> __state_type;
577 typedef __future_base::_Result<_Res>& __result_type;
580 __state_type _M_state;
584 __basic_future(const __basic_future&) = delete;
585 __basic_future& operator=(const __basic_future&) = delete;
588 valid() const noexcept { return static_cast<bool>(_M_state); }
593 _State_base::_S_check(_M_state);
597 template<typename _Rep, typename _Period>
599 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
601 _State_base::_S_check(_M_state);
602 return _M_state->wait_for(__rel);
605 template<typename _Clock, typename _Duration>
607 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
609 _State_base::_S_check(_M_state);
610 return _M_state->wait_until(__abs);
614 /// Wait for the state to be ready and rethrow any stored exception
616 _M_get_result() const
618 _State_base::_S_check(_M_state);
619 _Result_base& __res = _M_state->wait();
620 if (!(__res._M_error == 0))
621 rethrow_exception(__res._M_error);
622 return static_cast<__result_type>(__res);
625 void _M_swap(__basic_future& __that) noexcept
627 _M_state.swap(__that._M_state);
630 // Construction of a future by promise::get_future()
632 __basic_future(const __state_type& __state) : _M_state(__state)
634 _State_base::_S_check(_M_state);
635 _M_state->_M_set_retrieved_flag();
638 // Copy construction from a shared_future
640 __basic_future(const shared_future<_Res>&) noexcept;
642 // Move construction from a shared_future
644 __basic_future(shared_future<_Res>&&) noexcept;
646 // Move construction from a future
648 __basic_future(future<_Res>&&) noexcept;
650 constexpr __basic_future() noexcept : _M_state() { }
654 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
655 ~_Reset() { _M_fut._M_state.reset(); }
656 __basic_future& _M_fut;
661 /// Primary template for future.
662 template<typename _Res>
663 class future : public __basic_future<_Res>
665 friend class promise<_Res>;
666 template<typename> friend class packaged_task;
667 template<typename _Fn, typename... _Args>
668 friend future<typename result_of<_Fn(_Args...)>::type>
669 async(launch, _Fn&&, _Args&&...);
671 typedef __basic_future<_Res> _Base_type;
672 typedef typename _Base_type::__state_type __state_type;
675 future(const __state_type& __state) : _Base_type(__state) { }
678 constexpr future() noexcept : _Base_type() { }
681 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
684 future(const future&) = delete;
685 future& operator=(const future&) = delete;
687 future& operator=(future&& __fut) noexcept
689 future(std::move(__fut))._M_swap(*this);
693 /// Retrieving the value
697 typename _Base_type::_Reset __reset(*this);
698 return std::move(this->_M_get_result()._M_value());
701 shared_future<_Res> share();
704 /// Partial specialization for future<R&>
705 template<typename _Res>
706 class future<_Res&> : public __basic_future<_Res&>
708 friend class promise<_Res&>;
709 template<typename> friend class packaged_task;
710 template<typename _Fn, typename... _Args>
711 friend future<typename result_of<_Fn(_Args...)>::type>
712 async(launch, _Fn&&, _Args&&...);
714 typedef __basic_future<_Res&> _Base_type;
715 typedef typename _Base_type::__state_type __state_type;
718 future(const __state_type& __state) : _Base_type(__state) { }
721 constexpr future() noexcept : _Base_type() { }
724 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
727 future(const future&) = delete;
728 future& operator=(const future&) = delete;
730 future& operator=(future&& __fut) noexcept
732 future(std::move(__fut))._M_swap(*this);
736 /// Retrieving the value
740 typename _Base_type::_Reset __reset(*this);
741 return this->_M_get_result()._M_get();
744 shared_future<_Res&> share();
747 /// Explicit specialization for future<void>
749 class future<void> : public __basic_future<void>
751 friend class promise<void>;
752 template<typename> friend class packaged_task;
753 template<typename _Fn, typename... _Args>
754 friend future<typename result_of<_Fn(_Args...)>::type>
755 async(launch, _Fn&&, _Args&&...);
757 typedef __basic_future<void> _Base_type;
758 typedef typename _Base_type::__state_type __state_type;
761 future(const __state_type& __state) : _Base_type(__state) { }
764 constexpr future() noexcept : _Base_type() { }
767 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
770 future(const future&) = delete;
771 future& operator=(const future&) = delete;
773 future& operator=(future&& __fut) noexcept
775 future(std::move(__fut))._M_swap(*this);
779 /// Retrieving the value
783 typename _Base_type::_Reset __reset(*this);
784 this->_M_get_result();
787 shared_future<void> share();
791 /// Primary template for shared_future.
792 template<typename _Res>
793 class shared_future : public __basic_future<_Res>
795 typedef __basic_future<_Res> _Base_type;
798 constexpr shared_future() noexcept : _Base_type() { }
801 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
803 /// Construct from a future rvalue
804 shared_future(future<_Res>&& __uf) noexcept
805 : _Base_type(std::move(__uf))
808 /// Construct from a shared_future rvalue
809 shared_future(shared_future&& __sf) noexcept
810 : _Base_type(std::move(__sf))
813 shared_future& operator=(const shared_future& __sf)
815 shared_future(__sf)._M_swap(*this);
819 shared_future& operator=(shared_future&& __sf) noexcept
821 shared_future(std::move(__sf))._M_swap(*this);
825 /// Retrieving the value
827 get() const { return this->_M_get_result()._M_value(); }
830 /// Partial specialization for shared_future<R&>
831 template<typename _Res>
832 class shared_future<_Res&> : public __basic_future<_Res&>
834 typedef __basic_future<_Res&> _Base_type;
837 constexpr shared_future() noexcept : _Base_type() { }
840 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
842 /// Construct from a future rvalue
843 shared_future(future<_Res&>&& __uf) noexcept
844 : _Base_type(std::move(__uf))
847 /// Construct from a shared_future rvalue
848 shared_future(shared_future&& __sf) noexcept
849 : _Base_type(std::move(__sf))
852 shared_future& operator=(const shared_future& __sf)
854 shared_future(__sf)._M_swap(*this);
858 shared_future& operator=(shared_future&& __sf) noexcept
860 shared_future(std::move(__sf))._M_swap(*this);
864 /// Retrieving the value
866 get() const { return this->_M_get_result()._M_get(); }
869 /// Explicit specialization for shared_future<void>
871 class shared_future<void> : public __basic_future<void>
873 typedef __basic_future<void> _Base_type;
876 constexpr shared_future() noexcept : _Base_type() { }
879 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
881 /// Construct from a future rvalue
882 shared_future(future<void>&& __uf) noexcept
883 : _Base_type(std::move(__uf))
886 /// Construct from a shared_future rvalue
887 shared_future(shared_future&& __sf) noexcept
888 : _Base_type(std::move(__sf))
891 shared_future& operator=(const shared_future& __sf)
893 shared_future(__sf)._M_swap(*this);
897 shared_future& operator=(shared_future&& __sf) noexcept
899 shared_future(std::move(__sf))._M_swap(*this);
903 // Retrieving the value
905 get() const { this->_M_get_result(); }
908 // Now we can define the protected __basic_future constructors.
909 template<typename _Res>
910 inline __basic_future<_Res>::
911 __basic_future(const shared_future<_Res>& __sf) noexcept
912 : _M_state(__sf._M_state)
915 template<typename _Res>
916 inline __basic_future<_Res>::
917 __basic_future(shared_future<_Res>&& __sf) noexcept
918 : _M_state(std::move(__sf._M_state))
921 template<typename _Res>
922 inline __basic_future<_Res>::
923 __basic_future(future<_Res>&& __uf) noexcept
924 : _M_state(std::move(__uf._M_state))
927 template<typename _Res>
928 inline shared_future<_Res>
929 future<_Res>::share()
930 { return shared_future<_Res>(std::move(*this)); }
932 template<typename _Res>
933 inline shared_future<_Res&>
934 future<_Res&>::share()
935 { return shared_future<_Res&>(std::move(*this)); }
937 inline shared_future<void>
938 future<void>::share()
939 { return shared_future<void>(std::move(*this)); }
941 /// Primary template for promise
942 template<typename _Res>
945 typedef __future_base::_State_base _State;
946 typedef __future_base::_Result<_Res> _Res_type;
947 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
948 template<typename, typename> friend class _State::_Setter;
950 shared_ptr<_State> _M_future;
951 _Ptr_type _M_storage;
955 : _M_future(std::make_shared<_State>()),
956 _M_storage(new _Res_type())
959 promise(promise&& __rhs) noexcept
960 : _M_future(std::move(__rhs._M_future)),
961 _M_storage(std::move(__rhs._M_storage))
964 template<typename _Allocator>
965 promise(allocator_arg_t, const _Allocator& __a)
966 : _M_future(std::allocate_shared<_State>(__a)),
967 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
970 template<typename _Allocator>
971 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
972 : _M_future(std::move(__rhs._M_future)),
973 _M_storage(std::move(__rhs._M_storage))
976 promise(const promise&) = delete;
980 if (static_cast<bool>(_M_future) && !_M_future.unique())
981 _M_future->_M_break_promise(std::move(_M_storage));
986 operator=(promise&& __rhs) noexcept
988 promise(std::move(__rhs)).swap(*this);
992 promise& operator=(const promise&) = delete;
995 swap(promise& __rhs) noexcept
997 _M_future.swap(__rhs._M_future);
998 _M_storage.swap(__rhs._M_storage);
1001 // Retrieving the result
1004 { return future<_Res>(_M_future); }
1006 // Setting the result
1008 set_value(const _Res& __r)
1010 auto __setter = _State::__setter(this, __r);
1011 _M_future->_M_set_result(std::move(__setter));
1015 set_value(_Res&& __r)
1017 auto __setter = _State::__setter(this, std::move(__r));
1018 _M_future->_M_set_result(std::move(__setter));
1022 set_exception(exception_ptr __p)
1024 auto __setter = _State::__setter(__p, this);
1025 _M_future->_M_set_result(std::move(__setter));
1029 template<typename _Res>
1031 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1034 template<typename _Res, typename _Alloc>
1035 struct uses_allocator<promise<_Res>, _Alloc>
1036 : public true_type { };
1039 /// Partial specialization for promise<R&>
1040 template<typename _Res>
1041 class promise<_Res&>
1043 typedef __future_base::_State_base _State;
1044 typedef __future_base::_Result<_Res&> _Res_type;
1045 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1046 template<typename, typename> friend class _State::_Setter;
1048 shared_ptr<_State> _M_future;
1049 _Ptr_type _M_storage;
1053 : _M_future(std::make_shared<_State>()),
1054 _M_storage(new _Res_type())
1057 promise(promise&& __rhs) noexcept
1058 : _M_future(std::move(__rhs._M_future)),
1059 _M_storage(std::move(__rhs._M_storage))
1062 template<typename _Allocator>
1063 promise(allocator_arg_t, const _Allocator& __a)
1064 : _M_future(std::allocate_shared<_State>(__a)),
1065 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1068 template<typename _Allocator>
1069 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1070 : _M_future(std::move(__rhs._M_future)),
1071 _M_storage(std::move(__rhs._M_storage))
1074 promise(const promise&) = delete;
1078 if (static_cast<bool>(_M_future) && !_M_future.unique())
1079 _M_future->_M_break_promise(std::move(_M_storage));
1084 operator=(promise&& __rhs) noexcept
1086 promise(std::move(__rhs)).swap(*this);
1090 promise& operator=(const promise&) = delete;
1093 swap(promise& __rhs) noexcept
1095 _M_future.swap(__rhs._M_future);
1096 _M_storage.swap(__rhs._M_storage);
1099 // Retrieving the result
1102 { return future<_Res&>(_M_future); }
1104 // Setting the result
1106 set_value(_Res& __r)
1108 auto __setter = _State::__setter(this, __r);
1109 _M_future->_M_set_result(std::move(__setter));
1113 set_exception(exception_ptr __p)
1115 auto __setter = _State::__setter(__p, this);
1116 _M_future->_M_set_result(std::move(__setter));
1120 /// Explicit specialization for promise<void>
1124 typedef __future_base::_State_base _State;
1125 typedef __future_base::_Result<void> _Res_type;
1126 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1127 template<typename, typename> friend class _State::_Setter;
1129 shared_ptr<_State> _M_future;
1130 _Ptr_type _M_storage;
1134 : _M_future(std::make_shared<_State>()),
1135 _M_storage(new _Res_type())
1138 promise(promise&& __rhs) noexcept
1139 : _M_future(std::move(__rhs._M_future)),
1140 _M_storage(std::move(__rhs._M_storage))
1143 template<typename _Allocator>
1144 promise(allocator_arg_t, const _Allocator& __a)
1145 : _M_future(std::allocate_shared<_State>(__a)),
1146 _M_storage(__future_base::_S_allocate_result<void>(__a))
1149 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1150 // 2095. missing constructors needed for uses-allocator construction
1151 template<typename _Allocator>
1152 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1153 : _M_future(std::move(__rhs._M_future)),
1154 _M_storage(std::move(__rhs._M_storage))
1157 promise(const promise&) = delete;
1161 if (static_cast<bool>(_M_future) && !_M_future.unique())
1162 _M_future->_M_break_promise(std::move(_M_storage));
1167 operator=(promise&& __rhs) noexcept
1169 promise(std::move(__rhs)).swap(*this);
1173 promise& operator=(const promise&) = delete;
1176 swap(promise& __rhs) noexcept
1178 _M_future.swap(__rhs._M_future);
1179 _M_storage.swap(__rhs._M_storage);
1182 // Retrieving the result
1185 { return future<void>(_M_future); }
1187 // Setting the result
1191 set_exception(exception_ptr __p)
1193 auto __setter = _State::__setter(__p, this);
1194 _M_future->_M_set_result(std::move(__setter));
1200 struct __future_base::_State_base::_Setter<void, void>
1202 promise<void>::_Ptr_type operator()()
1204 _State_base::_S_check(_M_promise->_M_future);
1205 return std::move(_M_promise->_M_storage);
1208 promise<void>* _M_promise;
1211 inline __future_base::_State_base::_Setter<void, void>
1212 __future_base::_State_base::__setter(promise<void>* __prom)
1214 return _Setter<void, void>{ __prom };
1218 promise<void>::set_value()
1220 auto __setter = _State::__setter(this);
1221 _M_future->_M_set_result(std::move(__setter));
1225 template<typename _Ptr_type, typename _Res>
1226 struct __future_base::_Task_setter
1228 _Ptr_type operator()()
1232 _M_result->_M_set(_M_fn());
1236 _M_result->_M_error = current_exception();
1238 return std::move(_M_result);
1240 _Ptr_type& _M_result;
1241 std::function<_Res()> _M_fn;
1244 template<typename _Ptr_type>
1245 struct __future_base::_Task_setter<_Ptr_type, void>
1247 _Ptr_type operator()()
1255 _M_result->_M_error = current_exception();
1257 return std::move(_M_result);
1259 _Ptr_type& _M_result;
1260 std::function<void()> _M_fn;
1263 template<typename _Res, typename... _Args>
1264 struct __future_base::_Task_state_base<_Res(_Args...)>
1265 : __future_base::_State_base
1267 typedef _Res _Res_type;
1269 template<typename _Alloc>
1270 _Task_state_base(const _Alloc& __a)
1271 : _M_result(_S_allocate_result<_Res>(__a))
1275 _M_run(_Args... __args) = 0;
1277 virtual shared_ptr<_Task_state_base>
1280 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1281 _Ptr_type _M_result;
1284 template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
1285 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1286 : __future_base::_Task_state_base<_Res(_Args...)>
1288 template<typename _Fn2>
1289 _Task_state(_Fn2&& __fn, const _Alloc& __a)
1290 : _Task_state_base<_Res(_Args...)>(__a),
1291 _M_impl(std::forward<_Fn2>(__fn), __a)
1296 _M_run(_Args... __args)
1298 // bound arguments decay so wrap lvalue references
1299 auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
1300 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1301 auto __setter = _S_task_setter(this->_M_result, std::move(__boundfn));
1302 this->_M_set_result(std::move(__setter));
1305 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1308 template<typename _Tp>
1309 static reference_wrapper<_Tp>
1310 _S_maybe_wrap_ref(_Tp& __t)
1311 { return std::ref(__t); }
1313 template<typename _Tp>
1315 typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
1316 _S_maybe_wrap_ref(_Tp&& __t)
1317 { return std::forward<_Tp>(__t); }
1319 struct _Impl : _Alloc
1321 template<typename _Fn2>
1322 _Impl(_Fn2&& __fn, const _Alloc& __a)
1323 : _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { }
1328 template<typename _Signature, typename _Fn, typename _Alloc>
1329 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1330 __create_task_state(_Fn&& __fn, const _Alloc& __a)
1332 typedef typename decay<_Fn>::type _Fn2;
1333 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1334 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1337 template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
1338 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1339 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1341 return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
1342 static_cast<_Alloc&>(_M_impl));
1345 template<typename _Task, typename _Fn, bool
1346 = is_same<_Task, typename decay<_Fn>::type>::value>
1347 struct __constrain_pkgdtask
1348 { typedef void __type; };
1350 template<typename _Task, typename _Fn>
1351 struct __constrain_pkgdtask<_Task, _Fn, true>
1355 template<typename _Res, typename... _ArgTypes>
1356 class packaged_task<_Res(_ArgTypes...)>
1358 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1359 shared_ptr<_State_type> _M_state;
1362 // Construction and destruction
1363 packaged_task() noexcept { }
1365 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1366 // 2095. missing constructors needed for uses-allocator construction
1367 template<typename _Allocator>
1368 packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
1371 template<typename _Fn, typename = typename
1372 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1374 packaged_task(_Fn&& __fn)
1375 : packaged_task(allocator_arg, std::allocator<int>(),
1376 std::forward<_Fn>(__fn))
1379 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1380 // 2097. packaged_task constructors should be constrained
1381 template<typename _Fn, typename _Alloc, typename = typename
1382 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1384 packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
1385 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1386 std::forward<_Fn>(__fn), __a))
1391 if (static_cast<bool>(_M_state) && !_M_state.unique())
1392 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1396 packaged_task(const packaged_task&) = delete;
1397 packaged_task& operator=(const packaged_task&) = delete;
1399 template<typename _Allocator>
1400 packaged_task(allocator_arg_t, const _Allocator&,
1401 const packaged_task&) = delete;
1404 packaged_task(packaged_task&& __other) noexcept
1405 { this->swap(__other); }
1407 template<typename _Allocator>
1408 packaged_task(allocator_arg_t, const _Allocator&,
1409 packaged_task&& __other) noexcept
1410 { this->swap(__other); }
1412 packaged_task& operator=(packaged_task&& __other) noexcept
1414 packaged_task(std::move(__other)).swap(*this);
1419 swap(packaged_task& __other) noexcept
1420 { _M_state.swap(__other._M_state); }
1423 valid() const noexcept
1424 { return static_cast<bool>(_M_state); }
1429 { return future<_Res>(_M_state); }
1433 operator()(_ArgTypes... __args)
1435 __future_base::_State_base::_S_check(_M_state);
1436 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1442 __future_base::_State_base::_S_check(_M_state);
1443 packaged_task __tmp;
1444 __tmp._M_state = _M_state;
1445 _M_state = _M_state->_M_reset();
1450 template<typename _Res, typename... _ArgTypes>
1452 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1453 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1456 template<typename _Res, typename _Alloc>
1457 struct uses_allocator<packaged_task<_Res>, _Alloc>
1458 : public true_type { };
1461 template<typename _BoundFn, typename _Res>
1462 class __future_base::_Deferred_state final
1463 : public __future_base::_State_base
1467 _Deferred_state(_BoundFn&& __fn)
1468 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1472 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1473 _Ptr_type _M_result;
1476 // Run the deferred function.
1480 // safe to call multiple times so ignore failure
1481 _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1485 _M_has_deferred() const { return static_cast<bool>(_M_result); }
1488 class __future_base::_Async_state_commonV2
1489 : public __future_base::_State_base
1492 ~_Async_state_commonV2() = default;
1494 // Make waiting functions block until the thread completes, as if joined.
1495 virtual void _M_complete_async() { _M_join(); }
1497 void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
1503 template<typename _BoundFn, typename _Res>
1504 class __future_base::_Async_state_impl final
1505 : public __future_base::_Async_state_commonV2
1509 _Async_state_impl(_BoundFn&& __fn)
1510 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1512 _M_thread = std::thread{ [this] {
1513 _M_set_result(_S_task_setter(_M_result, _M_fn));
1517 ~_Async_state_impl() { _M_join(); }
1520 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1521 _Ptr_type _M_result;
1525 template<typename _BoundFn>
1526 inline std::shared_ptr<__future_base::_State_base>
1527 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1529 typedef typename remove_reference<_BoundFn>::type __fn_type;
1530 typedef _Deferred_state<__fn_type> __state_type;
1531 return std::make_shared<__state_type>(std::move(__fn));
1534 template<typename _BoundFn>
1535 inline std::shared_ptr<__future_base::_State_base>
1536 __future_base::_S_make_async_state(_BoundFn&& __fn)
1538 typedef typename remove_reference<_BoundFn>::type __fn_type;
1539 typedef _Async_state_impl<__fn_type> __state_type;
1540 return std::make_shared<__state_type>(std::move(__fn));
1545 template<typename _Fn, typename... _Args>
1546 future<typename result_of<_Fn(_Args...)>::type>
1547 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1549 typedef typename result_of<_Fn(_Args...)>::type result_type;
1550 std::shared_ptr<__future_base::_State_base> __state;
1551 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1553 __state = __future_base::_S_make_async_state(std::__bind_simple(
1554 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1558 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1559 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1561 return future<result_type>(__state);
1564 /// async, potential overload
1565 template<typename _Fn, typename... _Args>
1566 inline future<typename result_of<_Fn(_Args...)>::type>
1567 async(_Fn&& __fn, _Args&&... __args)
1569 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1570 std::forward<_Args>(__args)...);
1573 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
1574 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1575 // && ATOMIC_INT_LOCK_FREE
1578 _GLIBCXX_END_NAMESPACE_VERSION
1583 #endif // _GLIBCXX_FUTURE