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)
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 __gnu_cxx::__aligned_buffer<_Res> _M_storage;
213 typedef _Res result_type;
215 _Result() noexcept : _M_initialized() { }
223 // Return lvalue, future will add const or rvalue-reference
225 _M_value() noexcept { return *_M_storage._M_ptr(); }
228 _M_set(const _Res& __res)
230 ::new (_M_storage._M_addr()) _Res(__res);
231 _M_initialized = true;
237 ::new (_M_storage._M_addr()) _Res(std::move(__res));
238 _M_initialized = true;
242 void _M_destroy() { delete this; }
245 /// A unique_ptr based on the instantiating type.
246 template<typename _Res>
247 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
250 template<typename _Res, typename _Alloc>
251 struct _Result_alloc final : _Result<_Res>, _Alloc
253 typedef typename allocator_traits<_Alloc>::template
254 rebind_alloc<_Result_alloc> __allocator_type;
257 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
263 typedef allocator_traits<__allocator_type> __traits;
264 __allocator_type __a(*this);
265 __traits::destroy(__a, this);
266 __traits::deallocate(__a, this, 1);
270 template<typename _Res, typename _Allocator>
271 static _Ptr<_Result_alloc<_Res, _Allocator>>
272 _S_allocate_result(const _Allocator& __a)
274 typedef _Result_alloc<_Res, _Allocator> __result_type;
275 typedef allocator_traits<typename __result_type::__allocator_type>
277 typename __traits::allocator_type __a2(__a);
278 __result_type* __p = __traits::allocate(__a2, 1);
281 __traits::construct(__a2, __p, __a);
285 __traits::deallocate(__a2, __p, 1);
286 __throw_exception_again;
288 return _Ptr<__result_type>(__p);
291 template<typename _Res, typename _Tp>
292 static _Ptr<_Result<_Res>>
293 _S_allocate_result(const std::allocator<_Tp>& __a)
295 return _Ptr<_Result<_Res>>(new _Result<_Res>);
298 /// Base class for state between a promise and one or more
299 /// associated futures.
302 typedef _Ptr<_Result_base> _Ptr_type;
306 condition_variable _M_cond;
307 atomic_flag _M_retrieved;
311 _State_baseV2() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT)
313 _State_baseV2(const _State_baseV2&) = delete;
314 _State_baseV2& operator=(const _State_baseV2&) = delete;
315 virtual ~_State_baseV2() = default;
321 unique_lock<mutex> __lock(_M_mutex);
322 _M_cond.wait(__lock, [&] { return _M_ready(); });
326 template<typename _Rep, typename _Period>
328 wait_for(const chrono::duration<_Rep, _Period>& __rel)
330 unique_lock<mutex> __lock(_M_mutex);
332 return future_status::ready;
333 if (_M_has_deferred())
334 return future_status::deferred;
335 if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
337 // _GLIBCXX_RESOLVE_LIB_DEFECTS
338 // 2100. timed waiting functions must also join
340 return future_status::ready;
342 return future_status::timeout;
345 template<typename _Clock, typename _Duration>
347 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
349 unique_lock<mutex> __lock(_M_mutex);
351 return future_status::ready;
352 if (_M_has_deferred())
353 return future_status::deferred;
354 if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
356 // _GLIBCXX_RESOLVE_LIB_DEFECTS
357 // 2100. timed waiting functions must also join
359 return future_status::ready;
361 return future_status::timeout;
365 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
368 // all calls to this function are serialized,
369 // side-effects of invoking __res only happen once
370 call_once(_M_once, &_State_baseV2::_M_do_set, this, ref(__res),
373 _M_cond.notify_all();
374 else if (!__ignore_failure)
375 __throw_future_error(int(future_errc::promise_already_satisfied));
379 _M_break_promise(_Ptr_type __res)
381 if (static_cast<bool>(__res))
383 error_code __ec(make_error_code(future_errc::broken_promise));
384 __res->_M_error = make_exception_ptr(future_error(__ec));
386 lock_guard<mutex> __lock(_M_mutex);
387 _M_result.swap(__res);
389 _M_cond.notify_all();
393 // Called when this object is passed to a future.
395 _M_set_retrieved_flag()
397 if (_M_retrieved.test_and_set())
398 __throw_future_error(int(future_errc::future_already_retrieved));
401 template<typename _Res, typename _Arg>
405 template<typename _Res, typename _Arg>
406 struct _Setter<_Res, _Arg&>
408 // check this is only used by promise<R>::set_value(const R&)
409 // or promise<R>::set_value(R&)
410 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
411 || is_same<const _Res, _Arg>::value, // promise<R>
412 "Invalid specialisation");
414 typename promise<_Res>::_Ptr_type operator()()
416 _State_baseV2::_S_check(_M_promise->_M_future);
417 _M_promise->_M_storage->_M_set(_M_arg);
418 return std::move(_M_promise->_M_storage);
420 promise<_Res>* _M_promise;
425 template<typename _Res>
426 struct _Setter<_Res, _Res&&>
428 typename promise<_Res>::_Ptr_type operator()()
430 _State_baseV2::_S_check(_M_promise->_M_future);
431 _M_promise->_M_storage->_M_set(std::move(_M_arg));
432 return std::move(_M_promise->_M_storage);
434 promise<_Res>* _M_promise;
438 struct __exception_ptr_tag { };
441 template<typename _Res>
442 struct _Setter<_Res, __exception_ptr_tag>
444 typename promise<_Res>::_Ptr_type operator()()
446 _State_baseV2::_S_check(_M_promise->_M_future);
447 _M_promise->_M_storage->_M_error = _M_ex;
448 return std::move(_M_promise->_M_storage);
451 promise<_Res>* _M_promise;
452 exception_ptr& _M_ex;
455 template<typename _Res, typename _Arg>
456 static _Setter<_Res, _Arg&&>
457 __setter(promise<_Res>* __prom, _Arg&& __arg)
459 return _Setter<_Res, _Arg&&>{ __prom, __arg };
462 template<typename _Res>
463 static _Setter<_Res, __exception_ptr_tag>
464 __setter(exception_ptr& __ex, promise<_Res>* __prom)
466 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
469 static _Setter<void, void>
470 __setter(promise<void>* __prom);
472 template<typename _Tp>
474 _S_check(const shared_ptr<_Tp>& __p)
476 if (!static_cast<bool>(__p))
477 __throw_future_error((int)future_errc::no_state);
482 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
484 _Ptr_type __res = __f();
486 lock_guard<mutex> __lock(_M_mutex);
487 _M_result.swap(__res);
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 // Caller must own _M_mutex.
499 virtual bool _M_has_deferred() const { return false; }
502 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
504 class _Async_state_common;
506 using _State_base = _State_baseV2;
507 class _Async_state_commonV2;
510 template<typename _BoundFn, typename = typename _BoundFn::result_type>
511 class _Deferred_state;
513 template<typename _BoundFn, typename = typename _BoundFn::result_type>
514 class _Async_state_impl;
516 template<typename _Signature>
517 class _Task_state_base;
519 template<typename _Fn, typename _Alloc, typename _Signature>
522 template<typename _BoundFn>
523 static std::shared_ptr<_State_base>
524 _S_make_deferred_state(_BoundFn&& __fn);
526 template<typename _BoundFn>
527 static std::shared_ptr<_State_base>
528 _S_make_async_state(_BoundFn&& __fn);
530 template<typename _Res_ptr,
531 typename _Res = typename _Res_ptr::element_type::result_type>
534 template<typename _Res_ptr, typename _BoundFn>
535 static _Task_setter<_Res_ptr>
536 _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
538 return _Task_setter<_Res_ptr>{ __ptr, std::ref(__call) };
542 /// Partial specialization for reference types.
543 template<typename _Res>
544 struct __future_base::_Result<_Res&> : __future_base::_Result_base
546 typedef _Res& result_type;
548 _Result() noexcept : _M_value_ptr() { }
550 void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
552 _Res& _M_get() noexcept { return *_M_value_ptr; }
557 void _M_destroy() { delete this; }
560 /// Explicit specialization for void.
562 struct __future_base::_Result<void> : __future_base::_Result_base
564 typedef void result_type;
567 void _M_destroy() { delete this; }
570 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
572 /// Common implementation for future and shared_future.
573 template<typename _Res>
574 class __basic_future : public __future_base
577 typedef shared_ptr<_State_base> __state_type;
578 typedef __future_base::_Result<_Res>& __result_type;
581 __state_type _M_state;
585 __basic_future(const __basic_future&) = delete;
586 __basic_future& operator=(const __basic_future&) = delete;
589 valid() const noexcept { return static_cast<bool>(_M_state); }
594 _State_base::_S_check(_M_state);
598 template<typename _Rep, typename _Period>
600 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
602 _State_base::_S_check(_M_state);
603 return _M_state->wait_for(__rel);
606 template<typename _Clock, typename _Duration>
608 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
610 _State_base::_S_check(_M_state);
611 return _M_state->wait_until(__abs);
615 /// Wait for the state to be ready and rethrow any stored exception
617 _M_get_result() const
619 _State_base::_S_check(_M_state);
620 _Result_base& __res = _M_state->wait();
621 if (!(__res._M_error == 0))
622 rethrow_exception(__res._M_error);
623 return static_cast<__result_type>(__res);
626 void _M_swap(__basic_future& __that) noexcept
628 _M_state.swap(__that._M_state);
631 // Construction of a future by promise::get_future()
633 __basic_future(const __state_type& __state) : _M_state(__state)
635 _State_base::_S_check(_M_state);
636 _M_state->_M_set_retrieved_flag();
639 // Copy construction from a shared_future
641 __basic_future(const shared_future<_Res>&) noexcept;
643 // Move construction from a shared_future
645 __basic_future(shared_future<_Res>&&) noexcept;
647 // Move construction from a future
649 __basic_future(future<_Res>&&) noexcept;
651 constexpr __basic_future() noexcept : _M_state() { }
655 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
656 ~_Reset() { _M_fut._M_state.reset(); }
657 __basic_future& _M_fut;
662 /// Primary template for future.
663 template<typename _Res>
664 class future : 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 std::move(this->_M_get_result()._M_value());
702 shared_future<_Res> share();
705 /// Partial specialization for future<R&>
706 template<typename _Res>
707 class future<_Res&> : public __basic_future<_Res&>
709 friend class promise<_Res&>;
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<_Res&> _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 return this->_M_get_result()._M_get();
745 shared_future<_Res&> share();
748 /// Explicit specialization for future<void>
750 class future<void> : public __basic_future<void>
752 friend class promise<void>;
753 template<typename> friend class packaged_task;
754 template<typename _Fn, typename... _Args>
755 friend future<typename result_of<_Fn(_Args...)>::type>
756 async(launch, _Fn&&, _Args&&...);
758 typedef __basic_future<void> _Base_type;
759 typedef typename _Base_type::__state_type __state_type;
762 future(const __state_type& __state) : _Base_type(__state) { }
765 constexpr future() noexcept : _Base_type() { }
768 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
771 future(const future&) = delete;
772 future& operator=(const future&) = delete;
774 future& operator=(future&& __fut) noexcept
776 future(std::move(__fut))._M_swap(*this);
780 /// Retrieving the value
784 typename _Base_type::_Reset __reset(*this);
785 this->_M_get_result();
788 shared_future<void> share();
792 /// Primary template for shared_future.
793 template<typename _Res>
794 class shared_future : public __basic_future<_Res>
796 typedef __basic_future<_Res> _Base_type;
799 constexpr shared_future() noexcept : _Base_type() { }
802 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
804 /// Construct from a future rvalue
805 shared_future(future<_Res>&& __uf) noexcept
806 : _Base_type(std::move(__uf))
809 /// Construct from a shared_future rvalue
810 shared_future(shared_future&& __sf) noexcept
811 : _Base_type(std::move(__sf))
814 shared_future& operator=(const shared_future& __sf)
816 shared_future(__sf)._M_swap(*this);
820 shared_future& operator=(shared_future&& __sf) noexcept
822 shared_future(std::move(__sf))._M_swap(*this);
826 /// Retrieving the value
828 get() const { return this->_M_get_result()._M_value(); }
831 /// Partial specialization for shared_future<R&>
832 template<typename _Res>
833 class shared_future<_Res&> : public __basic_future<_Res&>
835 typedef __basic_future<_Res&> _Base_type;
838 constexpr shared_future() noexcept : _Base_type() { }
841 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
843 /// Construct from a future rvalue
844 shared_future(future<_Res&>&& __uf) noexcept
845 : _Base_type(std::move(__uf))
848 /// Construct from a shared_future rvalue
849 shared_future(shared_future&& __sf) noexcept
850 : _Base_type(std::move(__sf))
853 shared_future& operator=(const shared_future& __sf)
855 shared_future(__sf)._M_swap(*this);
859 shared_future& operator=(shared_future&& __sf) noexcept
861 shared_future(std::move(__sf))._M_swap(*this);
865 /// Retrieving the value
867 get() const { return this->_M_get_result()._M_get(); }
870 /// Explicit specialization for shared_future<void>
872 class shared_future<void> : public __basic_future<void>
874 typedef __basic_future<void> _Base_type;
877 constexpr shared_future() noexcept : _Base_type() { }
880 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
882 /// Construct from a future rvalue
883 shared_future(future<void>&& __uf) noexcept
884 : _Base_type(std::move(__uf))
887 /// Construct from a shared_future rvalue
888 shared_future(shared_future&& __sf) noexcept
889 : _Base_type(std::move(__sf))
892 shared_future& operator=(const shared_future& __sf)
894 shared_future(__sf)._M_swap(*this);
898 shared_future& operator=(shared_future&& __sf) noexcept
900 shared_future(std::move(__sf))._M_swap(*this);
904 // Retrieving the value
906 get() const { this->_M_get_result(); }
909 // Now we can define the protected __basic_future constructors.
910 template<typename _Res>
911 inline __basic_future<_Res>::
912 __basic_future(const shared_future<_Res>& __sf) noexcept
913 : _M_state(__sf._M_state)
916 template<typename _Res>
917 inline __basic_future<_Res>::
918 __basic_future(shared_future<_Res>&& __sf) noexcept
919 : _M_state(std::move(__sf._M_state))
922 template<typename _Res>
923 inline __basic_future<_Res>::
924 __basic_future(future<_Res>&& __uf) noexcept
925 : _M_state(std::move(__uf._M_state))
928 template<typename _Res>
929 inline shared_future<_Res>
930 future<_Res>::share()
931 { return shared_future<_Res>(std::move(*this)); }
933 template<typename _Res>
934 inline shared_future<_Res&>
935 future<_Res&>::share()
936 { return shared_future<_Res&>(std::move(*this)); }
938 inline shared_future<void>
939 future<void>::share()
940 { return shared_future<void>(std::move(*this)); }
942 /// Primary template for promise
943 template<typename _Res>
946 typedef __future_base::_State_base _State;
947 typedef __future_base::_Result<_Res> _Res_type;
948 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
949 template<typename, typename> friend class _State::_Setter;
951 shared_ptr<_State> _M_future;
952 _Ptr_type _M_storage;
956 : _M_future(std::make_shared<_State>()),
957 _M_storage(new _Res_type())
960 promise(promise&& __rhs) noexcept
961 : _M_future(std::move(__rhs._M_future)),
962 _M_storage(std::move(__rhs._M_storage))
965 template<typename _Allocator>
966 promise(allocator_arg_t, const _Allocator& __a)
967 : _M_future(std::allocate_shared<_State>(__a)),
968 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
971 template<typename _Allocator>
972 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
973 : _M_future(std::move(__rhs._M_future)),
974 _M_storage(std::move(__rhs._M_storage))
977 promise(const promise&) = delete;
981 if (static_cast<bool>(_M_future) && !_M_future.unique())
982 _M_future->_M_break_promise(std::move(_M_storage));
987 operator=(promise&& __rhs) noexcept
989 promise(std::move(__rhs)).swap(*this);
993 promise& operator=(const promise&) = delete;
996 swap(promise& __rhs) noexcept
998 _M_future.swap(__rhs._M_future);
999 _M_storage.swap(__rhs._M_storage);
1002 // Retrieving the result
1005 { return future<_Res>(_M_future); }
1007 // Setting the result
1009 set_value(const _Res& __r)
1011 auto __future = _M_future;
1012 auto __setter = _State::__setter(this, __r);
1013 __future->_M_set_result(std::move(__setter));
1017 set_value(_Res&& __r)
1019 auto __future = _M_future;
1020 auto __setter = _State::__setter(this, std::move(__r));
1021 __future->_M_set_result(std::move(__setter));
1025 set_exception(exception_ptr __p)
1027 auto __future = _M_future;
1028 auto __setter = _State::__setter(__p, this);
1029 __future->_M_set_result(std::move(__setter));
1033 template<typename _Res>
1035 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1038 template<typename _Res, typename _Alloc>
1039 struct uses_allocator<promise<_Res>, _Alloc>
1040 : public true_type { };
1043 /// Partial specialization for promise<R&>
1044 template<typename _Res>
1045 class promise<_Res&>
1047 typedef __future_base::_State_base _State;
1048 typedef __future_base::_Result<_Res&> _Res_type;
1049 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1050 template<typename, typename> friend class _State::_Setter;
1052 shared_ptr<_State> _M_future;
1053 _Ptr_type _M_storage;
1057 : _M_future(std::make_shared<_State>()),
1058 _M_storage(new _Res_type())
1061 promise(promise&& __rhs) noexcept
1062 : _M_future(std::move(__rhs._M_future)),
1063 _M_storage(std::move(__rhs._M_storage))
1066 template<typename _Allocator>
1067 promise(allocator_arg_t, const _Allocator& __a)
1068 : _M_future(std::allocate_shared<_State>(__a)),
1069 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1072 template<typename _Allocator>
1073 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1074 : _M_future(std::move(__rhs._M_future)),
1075 _M_storage(std::move(__rhs._M_storage))
1078 promise(const promise&) = delete;
1082 if (static_cast<bool>(_M_future) && !_M_future.unique())
1083 _M_future->_M_break_promise(std::move(_M_storage));
1088 operator=(promise&& __rhs) noexcept
1090 promise(std::move(__rhs)).swap(*this);
1094 promise& operator=(const promise&) = delete;
1097 swap(promise& __rhs) noexcept
1099 _M_future.swap(__rhs._M_future);
1100 _M_storage.swap(__rhs._M_storage);
1103 // Retrieving the result
1106 { return future<_Res&>(_M_future); }
1108 // Setting the result
1110 set_value(_Res& __r)
1112 auto __future = _M_future;
1113 auto __setter = _State::__setter(this, __r);
1114 __future->_M_set_result(std::move(__setter));
1118 set_exception(exception_ptr __p)
1120 auto __future = _M_future;
1121 auto __setter = _State::__setter(__p, this);
1122 __future->_M_set_result(std::move(__setter));
1126 /// Explicit specialization for promise<void>
1130 typedef __future_base::_State_base _State;
1131 typedef __future_base::_Result<void> _Res_type;
1132 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1133 template<typename, typename> friend class _State::_Setter;
1135 shared_ptr<_State> _M_future;
1136 _Ptr_type _M_storage;
1140 : _M_future(std::make_shared<_State>()),
1141 _M_storage(new _Res_type())
1144 promise(promise&& __rhs) noexcept
1145 : _M_future(std::move(__rhs._M_future)),
1146 _M_storage(std::move(__rhs._M_storage))
1149 template<typename _Allocator>
1150 promise(allocator_arg_t, const _Allocator& __a)
1151 : _M_future(std::allocate_shared<_State>(__a)),
1152 _M_storage(__future_base::_S_allocate_result<void>(__a))
1155 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1156 // 2095. missing constructors needed for uses-allocator construction
1157 template<typename _Allocator>
1158 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1159 : _M_future(std::move(__rhs._M_future)),
1160 _M_storage(std::move(__rhs._M_storage))
1163 promise(const promise&) = delete;
1167 if (static_cast<bool>(_M_future) && !_M_future.unique())
1168 _M_future->_M_break_promise(std::move(_M_storage));
1173 operator=(promise&& __rhs) noexcept
1175 promise(std::move(__rhs)).swap(*this);
1179 promise& operator=(const promise&) = delete;
1182 swap(promise& __rhs) noexcept
1184 _M_future.swap(__rhs._M_future);
1185 _M_storage.swap(__rhs._M_storage);
1188 // Retrieving the result
1191 { return future<void>(_M_future); }
1193 // Setting the result
1197 set_exception(exception_ptr __p)
1199 auto __future = _M_future;
1200 auto __setter = _State::__setter(__p, this);
1201 __future->_M_set_result(std::move(__setter));
1207 struct __future_base::_State_base::_Setter<void, void>
1209 promise<void>::_Ptr_type operator()()
1211 _State_base::_S_check(_M_promise->_M_future);
1212 return std::move(_M_promise->_M_storage);
1215 promise<void>* _M_promise;
1218 inline __future_base::_State_base::_Setter<void, void>
1219 __future_base::_State_base::__setter(promise<void>* __prom)
1221 return _Setter<void, void>{ __prom };
1225 promise<void>::set_value()
1227 auto __future = _M_future;
1228 auto __setter = _State::__setter(this);
1229 __future->_M_set_result(std::move(__setter));
1233 template<typename _Ptr_type, typename _Res>
1234 struct __future_base::_Task_setter
1236 _Ptr_type operator()()
1240 _M_result->_M_set(_M_fn());
1242 __catch(const __cxxabiv1::__forced_unwind&)
1244 __throw_exception_again; // will cause broken_promise
1248 _M_result->_M_error = current_exception();
1250 return std::move(_M_result);
1252 _Ptr_type& _M_result;
1253 std::function<_Res()> _M_fn;
1256 template<typename _Ptr_type>
1257 struct __future_base::_Task_setter<_Ptr_type, void>
1259 _Ptr_type operator()()
1265 __catch(const __cxxabiv1::__forced_unwind&)
1267 __throw_exception_again; // will cause broken_promise
1271 _M_result->_M_error = current_exception();
1273 return std::move(_M_result);
1275 _Ptr_type& _M_result;
1276 std::function<void()> _M_fn;
1279 template<typename _Res, typename... _Args>
1280 struct __future_base::_Task_state_base<_Res(_Args...)>
1281 : __future_base::_State_base
1283 typedef _Res _Res_type;
1285 template<typename _Alloc>
1286 _Task_state_base(const _Alloc& __a)
1287 : _M_result(_S_allocate_result<_Res>(__a))
1291 _M_run(_Args... __args) = 0;
1293 virtual shared_ptr<_Task_state_base>
1296 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1297 _Ptr_type _M_result;
1300 template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
1301 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1302 : __future_base::_Task_state_base<_Res(_Args...)>
1304 template<typename _Fn2>
1305 _Task_state(_Fn2&& __fn, const _Alloc& __a)
1306 : _Task_state_base<_Res(_Args...)>(__a),
1307 _M_impl(std::forward<_Fn2>(__fn), __a)
1312 _M_run(_Args... __args)
1314 // bound arguments decay so wrap lvalue references
1315 auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
1316 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1317 auto __setter = _S_task_setter(this->_M_result, std::move(__boundfn));
1318 this->_M_set_result(std::move(__setter));
1321 virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1324 template<typename _Tp>
1325 static reference_wrapper<_Tp>
1326 _S_maybe_wrap_ref(_Tp& __t)
1327 { return std::ref(__t); }
1329 template<typename _Tp>
1331 typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
1332 _S_maybe_wrap_ref(_Tp&& __t)
1333 { return std::forward<_Tp>(__t); }
1335 struct _Impl : _Alloc
1337 template<typename _Fn2>
1338 _Impl(_Fn2&& __fn, const _Alloc& __a)
1339 : _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { }
1344 template<typename _Signature, typename _Fn, typename _Alloc>
1345 static shared_ptr<__future_base::_Task_state_base<_Signature>>
1346 __create_task_state(_Fn&& __fn, const _Alloc& __a)
1348 typedef typename decay<_Fn>::type _Fn2;
1349 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1350 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1353 template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
1354 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1355 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1357 return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
1358 static_cast<_Alloc&>(_M_impl));
1361 template<typename _Task, typename _Fn, bool
1362 = is_same<_Task, typename decay<_Fn>::type>::value>
1363 struct __constrain_pkgdtask
1364 { typedef void __type; };
1366 template<typename _Task, typename _Fn>
1367 struct __constrain_pkgdtask<_Task, _Fn, true>
1371 template<typename _Res, typename... _ArgTypes>
1372 class packaged_task<_Res(_ArgTypes...)>
1374 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1375 shared_ptr<_State_type> _M_state;
1378 // Construction and destruction
1379 packaged_task() noexcept { }
1381 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1382 // 2095. missing constructors needed for uses-allocator construction
1383 template<typename _Allocator>
1384 packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
1387 template<typename _Fn, typename = typename
1388 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1390 packaged_task(_Fn&& __fn)
1391 : packaged_task(allocator_arg, std::allocator<int>(),
1392 std::forward<_Fn>(__fn))
1395 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1396 // 2097. packaged_task constructors should be constrained
1397 template<typename _Fn, typename _Alloc, typename = typename
1398 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1400 packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
1401 : _M_state(__create_task_state<_Res(_ArgTypes...)>(
1402 std::forward<_Fn>(__fn), __a))
1407 if (static_cast<bool>(_M_state) && !_M_state.unique())
1408 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1412 packaged_task(const packaged_task&) = delete;
1413 packaged_task& operator=(const packaged_task&) = delete;
1415 template<typename _Allocator>
1416 packaged_task(allocator_arg_t, const _Allocator&,
1417 const packaged_task&) = delete;
1420 packaged_task(packaged_task&& __other) noexcept
1421 { this->swap(__other); }
1423 template<typename _Allocator>
1424 packaged_task(allocator_arg_t, const _Allocator&,
1425 packaged_task&& __other) noexcept
1426 { this->swap(__other); }
1428 packaged_task& operator=(packaged_task&& __other) noexcept
1430 packaged_task(std::move(__other)).swap(*this);
1435 swap(packaged_task& __other) noexcept
1436 { _M_state.swap(__other._M_state); }
1439 valid() const noexcept
1440 { return static_cast<bool>(_M_state); }
1445 { return future<_Res>(_M_state); }
1449 operator()(_ArgTypes... __args)
1451 __future_base::_State_base::_S_check(_M_state);
1452 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1458 __future_base::_State_base::_S_check(_M_state);
1459 packaged_task __tmp;
1460 __tmp._M_state = _M_state;
1461 _M_state = _M_state->_M_reset();
1466 template<typename _Res, typename... _ArgTypes>
1468 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1469 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1472 template<typename _Res, typename _Alloc>
1473 struct uses_allocator<packaged_task<_Res>, _Alloc>
1474 : public true_type { };
1477 template<typename _BoundFn, typename _Res>
1478 class __future_base::_Deferred_state final
1479 : public __future_base::_State_base
1483 _Deferred_state(_BoundFn&& __fn)
1484 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1488 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1489 _Ptr_type _M_result;
1492 // Run the deferred function.
1496 // safe to call multiple times so ignore failure
1497 _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1501 _M_has_deferred() const { return static_cast<bool>(_M_result); }
1504 class __future_base::_Async_state_commonV2
1505 : public __future_base::_State_base
1508 ~_Async_state_commonV2() = default;
1510 // Make waiting functions block until the thread completes, as if joined.
1511 virtual void _M_complete_async() { _M_join(); }
1513 void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
1519 template<typename _BoundFn, typename _Res>
1520 class __future_base::_Async_state_impl final
1521 : public __future_base::_Async_state_commonV2
1525 _Async_state_impl(_BoundFn&& __fn)
1526 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1528 _M_thread = std::thread{ [this] {
1531 _M_set_result(_S_task_setter(_M_result, _M_fn));
1533 __catch (const __cxxabiv1::__forced_unwind&)
1535 // make the shared state ready on thread cancellation
1536 if (static_cast<bool>(_M_result))
1537 this->_M_break_promise(std::move(_M_result));
1538 __throw_exception_again;
1543 ~_Async_state_impl() { _M_join(); }
1546 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1547 _Ptr_type _M_result;
1551 template<typename _BoundFn>
1552 inline std::shared_ptr<__future_base::_State_base>
1553 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1555 typedef typename remove_reference<_BoundFn>::type __fn_type;
1556 typedef _Deferred_state<__fn_type> __state_type;
1557 return std::make_shared<__state_type>(std::move(__fn));
1560 template<typename _BoundFn>
1561 inline std::shared_ptr<__future_base::_State_base>
1562 __future_base::_S_make_async_state(_BoundFn&& __fn)
1564 typedef typename remove_reference<_BoundFn>::type __fn_type;
1565 typedef _Async_state_impl<__fn_type> __state_type;
1566 return std::make_shared<__state_type>(std::move(__fn));
1571 template<typename _Fn, typename... _Args>
1572 future<typename result_of<_Fn(_Args...)>::type>
1573 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1575 typedef typename result_of<_Fn(_Args...)>::type result_type;
1576 std::shared_ptr<__future_base::_State_base> __state;
1577 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1579 __state = __future_base::_S_make_async_state(std::__bind_simple(
1580 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1584 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1585 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1587 return future<result_type>(__state);
1590 /// async, potential overload
1591 template<typename _Fn, typename... _Args>
1592 inline future<typename result_of<_Fn(_Args...)>::type>
1593 async(_Fn&& __fn, _Args&&... __args)
1595 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1596 std::forward<_Args>(__args)...);
1599 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
1600 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1603 _GLIBCXX_END_NAMESPACE_VERSION
1608 #endif // _GLIBCXX_FUTURE