1 // <future> -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/future
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_FUTURE
30 #define _GLIBCXX_FUTURE 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
42 #include <condition_variable>
43 #include <system_error>
46 #include <bits/functexcept.h>
48 namespace std _GLIBCXX_VISIBILITY(default)
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 * @defgroup futures Futures
54 * @ingroup concurrency
56 * Classes for futures support.
60 /// Error code for futures
61 enum class future_errc
63 future_already_retrieved = 1,
64 promise_already_satisfied,
71 struct is_error_code_enum<future_errc> : public true_type { };
73 /// Points to a statically-allocated object derived from error_category.
75 future_category() noexcept;
77 /// Overload for make_error_code.
79 make_error_code(future_errc __errc) noexcept
80 { return error_code(static_cast<int>(__errc), future_category()); }
82 /// Overload for make_error_condition.
83 inline error_condition
84 make_error_condition(future_errc __errc) noexcept
85 { return error_condition(static_cast<int>(__errc), future_category()); }
88 * @brief Exception type thrown by futures.
91 class future_error : public logic_error
96 explicit future_error(error_code __ec)
97 : logic_error("std::future_error"), _M_code(__ec)
100 virtual ~future_error() noexcept;
103 what() const noexcept;
106 code() const noexcept { return _M_code; }
109 // Forward declarations.
110 template<typename _Res>
113 template<typename _Res>
116 template<typename _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 _FnCheck, typename _Fn, typename... _Args>
175 struct __async_sfinae_helper
177 typedef future<typename result_of<_Fn(_Args...)>::type> type;
180 template<typename _Fn, typename... _Args>
181 struct __async_sfinae_helper<launch, _Fn, _Args...>
184 template<typename _Fn, typename... _Args>
186 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
187 async(_Fn&& __fn, _Args&&... __args);
189 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
190 && (ATOMIC_INT_LOCK_FREE > 1)
192 /// Base class and enclosing scope.
195 /// Base class for results.
198 exception_ptr _M_error;
200 _Result_base(const _Result_base&) = delete;
201 _Result_base& operator=(const _Result_base&) = delete;
203 // _M_destroy() allows derived classes to control deallocation
204 virtual void _M_destroy() = 0;
208 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
213 virtual ~_Result_base();
217 template<typename _Res>
218 struct _Result : _Result_base
221 typedef alignment_of<_Res> __a_of;
222 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
223 typedef typename __align_storage::type __align_type;
225 __align_type _M_storage;
229 _Result() noexcept : _M_initialized() { }
237 // Return lvalue, future will add const or rvalue-reference
239 _M_value() noexcept { return *static_cast<_Res*>(_M_addr()); }
242 _M_set(const _Res& __res)
244 ::new (_M_addr()) _Res(__res);
245 _M_initialized = true;
251 ::new (_M_addr()) _Res(std::move(__res));
252 _M_initialized = true;
256 void _M_destroy() { delete this; }
258 void* _M_addr() noexcept { return static_cast<void*>(&_M_storage); }
261 /// A unique_ptr based on the instantiating type.
262 template<typename _Res>
263 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
266 template<typename _Res, typename _Alloc>
267 struct _Result_alloc final : _Result<_Res>, _Alloc
269 typedef typename allocator_traits<_Alloc>::template
270 rebind_alloc<_Result_alloc> __allocator_type;
273 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
279 typedef allocator_traits<__allocator_type> __traits;
280 __allocator_type __a(*this);
281 __traits::destroy(__a, this);
282 __traits::deallocate(__a, this, 1);
286 template<typename _Res, typename _Allocator>
287 static _Ptr<_Result_alloc<_Res, _Allocator>>
288 _S_allocate_result(const _Allocator& __a)
290 typedef _Result_alloc<_Res, _Allocator> __result_type;
291 typedef allocator_traits<typename __result_type::__allocator_type>
293 typename __traits::allocator_type __a2(__a);
294 __result_type* __p = __traits::allocate(__a2, 1);
297 __traits::construct(__a2, __p, __a);
301 __traits::deallocate(__a2, __p, 1);
302 __throw_exception_again;
304 return _Ptr<__result_type>(__p);
308 /// Base class for state between a promise and one or more
309 /// associated futures.
312 typedef _Ptr<_Result_base> _Ptr_type;
316 condition_variable _M_cond;
317 atomic_flag _M_retrieved;
321 _State_base() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
322 _State_base(const _State_base&) = delete;
323 _State_base& operator=(const _State_base&) = delete;
324 virtual ~_State_base();
330 unique_lock<mutex> __lock(_M_mutex);
331 _M_cond.wait(__lock, [&] { return _M_ready(); });
335 template<typename _Rep, typename _Period>
337 wait_for(const chrono::duration<_Rep, _Period>& __rel)
339 unique_lock<mutex> __lock(_M_mutex);
340 if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
341 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);
350 if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
351 return future_status::ready;
352 return future_status::timeout;
356 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
358 bool __set = __ignore_failure;
359 // all calls to this function are serialized,
360 // side-effects of invoking __res only happen once
361 call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
364 __throw_future_error(int(future_errc::promise_already_satisfied));
368 _M_break_promise(_Ptr_type __res)
370 if (static_cast<bool>(__res))
372 error_code __ec(make_error_code(future_errc::broken_promise));
373 __res->_M_error = copy_exception(future_error(__ec));
375 lock_guard<mutex> __lock(_M_mutex);
376 _M_result.swap(__res);
378 _M_cond.notify_all();
382 // Called when this object is passed to a future.
384 _M_set_retrieved_flag()
386 if (_M_retrieved.test_and_set())
387 __throw_future_error(int(future_errc::future_already_retrieved));
390 template<typename _Res, typename _Arg>
394 template<typename _Res, typename _Arg>
395 struct _Setter<_Res, _Arg&>
397 // check this is only used by promise<R>::set_value(const R&)
398 // or promise<R>::set_value(R&)
399 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
400 || is_same<const _Res, _Arg>::value, // promise<R>
401 "Invalid specialisation");
403 typename promise<_Res>::_Ptr_type operator()()
405 _State_base::_S_check(_M_promise->_M_future);
406 _M_promise->_M_storage->_M_set(_M_arg);
407 return std::move(_M_promise->_M_storage);
409 promise<_Res>* _M_promise;
414 template<typename _Res>
415 struct _Setter<_Res, _Res&&>
417 typename promise<_Res>::_Ptr_type operator()()
419 _State_base::_S_check(_M_promise->_M_future);
420 _M_promise->_M_storage->_M_set(std::move(_M_arg));
421 return std::move(_M_promise->_M_storage);
423 promise<_Res>* _M_promise;
427 struct __exception_ptr_tag { };
430 template<typename _Res>
431 struct _Setter<_Res, __exception_ptr_tag>
433 typename promise<_Res>::_Ptr_type operator()()
435 _State_base::_S_check(_M_promise->_M_future);
436 _M_promise->_M_storage->_M_error = _M_ex;
437 return std::move(_M_promise->_M_storage);
440 promise<_Res>* _M_promise;
441 exception_ptr& _M_ex;
444 template<typename _Res, typename _Arg>
445 static _Setter<_Res, _Arg&&>
446 __setter(promise<_Res>* __prom, _Arg&& __arg)
448 return _Setter<_Res, _Arg&&>{ __prom, __arg };
451 template<typename _Res>
452 static _Setter<_Res, __exception_ptr_tag>
453 __setter(exception_ptr& __ex, promise<_Res>* __prom)
455 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
458 static _Setter<void, void>
459 __setter(promise<void>* __prom);
461 template<typename _Tp>
463 _S_check(const shared_ptr<_Tp>& __p)
465 if (!static_cast<bool>(__p))
466 __throw_future_error((int)future_errc::no_state);
471 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
473 _Ptr_type __res = __f();
475 lock_guard<mutex> __lock(_M_mutex);
476 _M_result.swap(__res);
478 _M_cond.notify_all();
482 bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
484 // Misnamed: waits for completion of async function.
485 virtual void _M_run_deferred() { }
488 template<typename _BoundFn, typename = typename _BoundFn::result_type>
489 class _Deferred_state;
491 class _Async_state_common;
493 template<typename _BoundFn, typename = typename _BoundFn::result_type>
494 class _Async_state_impl;
496 template<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, typename _Res>
510 template<typename _Res_ptr, typename _BoundFn>
511 class _Task_setter_helper
513 typedef typename remove_reference<_BoundFn>::type::result_type __res;
515 typedef _Task_setter<_Res_ptr, __res> __type;
518 template<typename _Res_ptr, typename _BoundFn>
519 static typename _Task_setter_helper<_Res_ptr, _BoundFn>::__type
520 _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
522 typedef _Task_setter_helper<_Res_ptr, _BoundFn> __helper_type;
523 typedef typename __helper_type::__type _Setter;
524 return _Setter{ __ptr, std::ref(__call) };
528 /// Partial specialization for reference types.
529 template<typename _Res>
530 struct __future_base::_Result<_Res&> : __future_base::_Result_base
532 _Result() noexcept : _M_value_ptr() { }
534 void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
536 _Res& _M_get() noexcept { return *_M_value_ptr; }
541 void _M_destroy() { delete this; }
544 /// Explicit specialization for void.
546 struct __future_base::_Result<void> : __future_base::_Result_base
549 void _M_destroy() { delete this; }
553 /// Common implementation for future and shared_future.
554 template<typename _Res>
555 class __basic_future : public __future_base
558 typedef shared_ptr<_State_base> __state_type;
559 typedef __future_base::_Result<_Res>& __result_type;
562 __state_type _M_state;
566 __basic_future(const __basic_future&) = delete;
567 __basic_future& operator=(const __basic_future&) = delete;
570 valid() const noexcept { return static_cast<bool>(_M_state); }
575 _State_base::_S_check(_M_state);
579 template<typename _Rep, typename _Period>
581 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
583 _State_base::_S_check(_M_state);
584 return _M_state->wait_for(__rel);
587 template<typename _Clock, typename _Duration>
589 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
591 _State_base::_S_check(_M_state);
592 return _M_state->wait_until(__abs);
596 /// Wait for the state to be ready and rethrow any stored exception
600 _State_base::_S_check(_M_state);
601 _Result_base& __res = _M_state->wait();
602 if (!(__res._M_error == 0))
603 rethrow_exception(__res._M_error);
604 return static_cast<__result_type>(__res);
607 void _M_swap(__basic_future& __that) noexcept
609 _M_state.swap(__that._M_state);
612 // Construction of a future by promise::get_future()
614 __basic_future(const __state_type& __state) : _M_state(__state)
616 _State_base::_S_check(_M_state);
617 _M_state->_M_set_retrieved_flag();
620 // Copy construction from a shared_future
622 __basic_future(const shared_future<_Res>&) noexcept;
624 // Move construction from a shared_future
626 __basic_future(shared_future<_Res>&&) noexcept;
628 // Move construction from a future
630 __basic_future(future<_Res>&&) noexcept;
632 constexpr __basic_future() noexcept : _M_state() { }
636 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
637 ~_Reset() { _M_fut._M_state.reset(); }
638 __basic_future& _M_fut;
643 /// Primary template for future.
644 template<typename _Res>
645 class future : public __basic_future<_Res>
647 friend class promise<_Res>;
648 template<typename> friend class packaged_task;
649 template<typename _Fn, typename... _Args>
650 friend future<typename result_of<_Fn(_Args...)>::type>
651 async(launch, _Fn&&, _Args&&...);
653 typedef __basic_future<_Res> _Base_type;
654 typedef typename _Base_type::__state_type __state_type;
657 future(const __state_type& __state) : _Base_type(__state) { }
660 constexpr future() noexcept : _Base_type() { }
663 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
666 future(const future&) = delete;
667 future& operator=(const future&) = delete;
669 future& operator=(future&& __fut) noexcept
671 future(std::move(__fut))._M_swap(*this);
675 /// Retrieving the value
679 typename _Base_type::_Reset __reset(*this);
680 return std::move(this->_M_get_result()._M_value());
683 shared_future<_Res> share();
686 /// Partial specialization for future<R&>
687 template<typename _Res>
688 class future<_Res&> : public __basic_future<_Res&>
690 friend class promise<_Res&>;
691 template<typename> friend class packaged_task;
692 template<typename _Fn, typename... _Args>
693 friend future<typename result_of<_Fn(_Args...)>::type>
694 async(launch, _Fn&&, _Args&&...);
696 typedef __basic_future<_Res&> _Base_type;
697 typedef typename _Base_type::__state_type __state_type;
700 future(const __state_type& __state) : _Base_type(__state) { }
703 constexpr future() noexcept : _Base_type() { }
706 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
709 future(const future&) = delete;
710 future& operator=(const future&) = delete;
712 future& operator=(future&& __fut) noexcept
714 future(std::move(__fut))._M_swap(*this);
718 /// Retrieving the value
722 typename _Base_type::_Reset __reset(*this);
723 return this->_M_get_result()._M_get();
726 shared_future<_Res&> share();
729 /// Explicit specialization for future<void>
731 class future<void> : public __basic_future<void>
733 friend class promise<void>;
734 template<typename> friend class packaged_task;
735 template<typename _Fn, typename... _Args>
736 friend future<typename result_of<_Fn(_Args...)>::type>
737 async(launch, _Fn&&, _Args&&...);
739 typedef __basic_future<void> _Base_type;
740 typedef typename _Base_type::__state_type __state_type;
743 future(const __state_type& __state) : _Base_type(__state) { }
746 constexpr future() noexcept : _Base_type() { }
749 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
752 future(const future&) = delete;
753 future& operator=(const future&) = delete;
755 future& operator=(future&& __fut) noexcept
757 future(std::move(__fut))._M_swap(*this);
761 /// Retrieving the value
765 typename _Base_type::_Reset __reset(*this);
766 this->_M_get_result();
769 shared_future<void> share();
773 /// Primary template for shared_future.
774 template<typename _Res>
775 class shared_future : public __basic_future<_Res>
777 typedef __basic_future<_Res> _Base_type;
780 constexpr shared_future() noexcept : _Base_type() { }
783 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
785 /// Construct from a future rvalue
786 shared_future(future<_Res>&& __uf) noexcept
787 : _Base_type(std::move(__uf))
790 /// Construct from a shared_future rvalue
791 shared_future(shared_future&& __sf) noexcept
792 : _Base_type(std::move(__sf))
795 shared_future& operator=(const shared_future& __sf)
797 shared_future(__sf)._M_swap(*this);
801 shared_future& operator=(shared_future&& __sf) noexcept
803 shared_future(std::move(__sf))._M_swap(*this);
807 /// Retrieving the value
811 typename _Base_type::__result_type __r = this->_M_get_result();
812 _Res& __rs(__r._M_value());
817 /// Partial specialization for shared_future<R&>
818 template<typename _Res>
819 class shared_future<_Res&> : public __basic_future<_Res&>
821 typedef __basic_future<_Res&> _Base_type;
824 constexpr shared_future() noexcept : _Base_type() { }
827 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
829 /// Construct from a future rvalue
830 shared_future(future<_Res&>&& __uf) noexcept
831 : _Base_type(std::move(__uf))
834 /// Construct from a shared_future rvalue
835 shared_future(shared_future&& __sf) noexcept
836 : _Base_type(std::move(__sf))
839 shared_future& operator=(const shared_future& __sf)
841 shared_future(__sf)._M_swap(*this);
845 shared_future& operator=(shared_future&& __sf) noexcept
847 shared_future(std::move(__sf))._M_swap(*this);
851 /// Retrieving the value
853 get() { return this->_M_get_result()._M_get(); }
856 /// Explicit specialization for shared_future<void>
858 class shared_future<void> : public __basic_future<void>
860 typedef __basic_future<void> _Base_type;
863 constexpr shared_future() noexcept : _Base_type() { }
866 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
868 /// Construct from a future rvalue
869 shared_future(future<void>&& __uf) noexcept
870 : _Base_type(std::move(__uf))
873 /// Construct from a shared_future rvalue
874 shared_future(shared_future&& __sf) noexcept
875 : _Base_type(std::move(__sf))
878 shared_future& operator=(const shared_future& __sf)
880 shared_future(__sf)._M_swap(*this);
884 shared_future& operator=(shared_future&& __sf) noexcept
886 shared_future(std::move(__sf))._M_swap(*this);
890 // Retrieving the value
892 get() { this->_M_get_result(); }
895 // Now we can define the protected __basic_future constructors.
896 template<typename _Res>
897 inline __basic_future<_Res>::
898 __basic_future(const shared_future<_Res>& __sf) noexcept
899 : _M_state(__sf._M_state)
902 template<typename _Res>
903 inline __basic_future<_Res>::
904 __basic_future(shared_future<_Res>&& __sf) noexcept
905 : _M_state(std::move(__sf._M_state))
908 template<typename _Res>
909 inline __basic_future<_Res>::
910 __basic_future(future<_Res>&& __uf) noexcept
911 : _M_state(std::move(__uf._M_state))
914 template<typename _Res>
915 inline shared_future<_Res>
916 future<_Res>::share()
917 { return shared_future<_Res>(std::move(*this)); }
919 template<typename _Res>
920 inline shared_future<_Res&>
921 future<_Res&>::share()
922 { return shared_future<_Res&>(std::move(*this)); }
924 inline shared_future<void>
925 future<void>::share()
926 { return shared_future<void>(std::move(*this)); }
928 /// Primary template for promise
929 template<typename _Res>
932 typedef __future_base::_State_base _State;
933 typedef __future_base::_Result<_Res> _Res_type;
934 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
935 template<typename, typename> friend class _State::_Setter;
937 shared_ptr<_State> _M_future;
938 _Ptr_type _M_storage;
942 : _M_future(std::make_shared<_State>()),
943 _M_storage(new _Res_type())
946 promise(promise&& __rhs) noexcept
947 : _M_future(std::move(__rhs._M_future)),
948 _M_storage(std::move(__rhs._M_storage))
951 template<typename _Allocator>
952 promise(allocator_arg_t, const _Allocator& __a)
953 : _M_future(std::allocate_shared<_State>(__a)),
954 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
957 template<typename _Allocator>
958 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
959 : _M_future(std::move(__rhs._M_future)),
960 _M_storage(std::move(__rhs._M_storage))
963 promise(const promise&) = delete;
967 if (static_cast<bool>(_M_future) && !_M_future.unique())
968 _M_future->_M_break_promise(std::move(_M_storage));
973 operator=(promise&& __rhs) noexcept
975 promise(std::move(__rhs)).swap(*this);
979 promise& operator=(const promise&) = delete;
982 swap(promise& __rhs) noexcept
984 _M_future.swap(__rhs._M_future);
985 _M_storage.swap(__rhs._M_storage);
988 // Retrieving the result
991 { return future<_Res>(_M_future); }
993 // Setting the result
995 set_value(const _Res& __r)
997 auto __setter = _State::__setter(this, __r);
998 _M_future->_M_set_result(std::move(__setter));
1002 set_value(_Res&& __r)
1004 auto __setter = _State::__setter(this, std::move(__r));
1005 _M_future->_M_set_result(std::move(__setter));
1009 set_exception(exception_ptr __p)
1011 auto __setter = _State::__setter(__p, this);
1012 _M_future->_M_set_result(std::move(__setter));
1016 template<typename _Res>
1018 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1021 template<typename _Res, typename _Alloc>
1022 struct uses_allocator<promise<_Res>, _Alloc>
1023 : public true_type { };
1026 /// Partial specialization for promise<R&>
1027 template<typename _Res>
1028 class promise<_Res&>
1030 typedef __future_base::_State_base _State;
1031 typedef __future_base::_Result<_Res&> _Res_type;
1032 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1033 template<typename, typename> friend class _State::_Setter;
1035 shared_ptr<_State> _M_future;
1036 _Ptr_type _M_storage;
1040 : _M_future(std::make_shared<_State>()),
1041 _M_storage(new _Res_type())
1044 promise(promise&& __rhs) noexcept
1045 : _M_future(std::move(__rhs._M_future)),
1046 _M_storage(std::move(__rhs._M_storage))
1049 template<typename _Allocator>
1050 promise(allocator_arg_t, const _Allocator& __a)
1051 : _M_future(std::allocate_shared<_State>(__a)),
1052 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1055 template<typename _Allocator>
1056 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1057 : _M_future(std::move(__rhs._M_future)),
1058 _M_storage(std::move(__rhs._M_storage))
1061 promise(const promise&) = delete;
1065 if (static_cast<bool>(_M_future) && !_M_future.unique())
1066 _M_future->_M_break_promise(std::move(_M_storage));
1071 operator=(promise&& __rhs) noexcept
1073 promise(std::move(__rhs)).swap(*this);
1077 promise& operator=(const promise&) = delete;
1080 swap(promise& __rhs) noexcept
1082 _M_future.swap(__rhs._M_future);
1083 _M_storage.swap(__rhs._M_storage);
1086 // Retrieving the result
1089 { return future<_Res&>(_M_future); }
1091 // Setting the result
1093 set_value(_Res& __r)
1095 auto __setter = _State::__setter(this, __r);
1096 _M_future->_M_set_result(std::move(__setter));
1100 set_exception(exception_ptr __p)
1102 auto __setter = _State::__setter(__p, this);
1103 _M_future->_M_set_result(std::move(__setter));
1107 /// Explicit specialization for promise<void>
1111 typedef __future_base::_State_base _State;
1112 typedef __future_base::_Result<void> _Res_type;
1113 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1114 template<typename, typename> friend class _State::_Setter;
1116 shared_ptr<_State> _M_future;
1117 _Ptr_type _M_storage;
1121 : _M_future(std::make_shared<_State>()),
1122 _M_storage(new _Res_type())
1125 promise(promise&& __rhs) noexcept
1126 : _M_future(std::move(__rhs._M_future)),
1127 _M_storage(std::move(__rhs._M_storage))
1130 template<typename _Allocator>
1131 promise(allocator_arg_t, const _Allocator& __a)
1132 : _M_future(std::allocate_shared<_State>(__a)),
1133 _M_storage(__future_base::_S_allocate_result<void>(__a))
1136 template<typename _Allocator>
1137 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1138 : _M_future(std::move(__rhs._M_future)),
1139 _M_storage(std::move(__rhs._M_storage))
1142 promise(const promise&) = delete;
1146 if (static_cast<bool>(_M_future) && !_M_future.unique())
1147 _M_future->_M_break_promise(std::move(_M_storage));
1152 operator=(promise&& __rhs) noexcept
1154 promise(std::move(__rhs)).swap(*this);
1158 promise& operator=(const promise&) = delete;
1161 swap(promise& __rhs) noexcept
1163 _M_future.swap(__rhs._M_future);
1164 _M_storage.swap(__rhs._M_storage);
1167 // Retrieving the result
1170 { return future<void>(_M_future); }
1172 // Setting the result
1176 set_exception(exception_ptr __p)
1178 auto __setter = _State::__setter(__p, this);
1179 _M_future->_M_set_result(std::move(__setter));
1185 struct __future_base::_State_base::_Setter<void, void>
1187 promise<void>::_Ptr_type operator()()
1189 _State_base::_S_check(_M_promise->_M_future);
1190 return std::move(_M_promise->_M_storage);
1193 promise<void>* _M_promise;
1196 inline __future_base::_State_base::_Setter<void, void>
1197 __future_base::_State_base::__setter(promise<void>* __prom)
1199 return _Setter<void, void>{ __prom };
1203 promise<void>::set_value()
1205 auto __setter = _State::__setter(this);
1206 _M_future->_M_set_result(std::move(__setter));
1210 template<typename _Ptr_type, typename _Res>
1211 struct __future_base::_Task_setter
1213 _Ptr_type operator()()
1217 _M_result->_M_set(_M_fn());
1221 _M_result->_M_error = current_exception();
1223 return std::move(_M_result);
1225 _Ptr_type& _M_result;
1226 std::function<_Res()> _M_fn;
1229 template<typename _Ptr_type>
1230 struct __future_base::_Task_setter<_Ptr_type, void>
1232 _Ptr_type operator()()
1240 _M_result->_M_error = current_exception();
1242 return std::move(_M_result);
1244 _Ptr_type& _M_result;
1245 std::function<void()> _M_fn;
1248 template<typename _Res, typename... _Args>
1249 struct __future_base::_Task_state<_Res(_Args...)> final
1250 : __future_base::_State_base
1252 typedef _Res _Res_type;
1254 _Task_state(std::function<_Res(_Args...)> __task)
1255 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1258 template<typename _Func, typename _Alloc>
1259 _Task_state(_Func&& __task, const _Alloc& __a)
1260 : _M_result(_S_allocate_result<_Res>(__a)),
1261 _M_task(allocator_arg, __a, std::move(__task))
1265 _M_run(_Args... __args)
1267 // bound arguments decay so wrap lvalue references
1268 auto __boundfn = std::__bind_simple(std::ref(_M_task),
1269 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1270 auto __setter = _S_task_setter(_M_result, std::move(__boundfn));
1271 _M_set_result(std::move(__setter));
1274 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1275 _Ptr_type _M_result;
1276 std::function<_Res(_Args...)> _M_task;
1278 template<typename _Tp>
1279 static reference_wrapper<_Tp>
1280 _S_maybe_wrap_ref(_Tp& __t)
1281 { return std::ref(__t); }
1283 template<typename _Tp>
1284 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1286 _S_maybe_wrap_ref(_Tp&& __t)
1287 { return std::forward<_Tp>(__t); }
1290 template<typename _Task, typename _Fn, bool
1291 = is_same<_Task, typename decay<_Fn>::type>::value>
1292 struct __constrain_pkgdtask
1293 { typedef void __type; };
1295 template<typename _Task, typename _Fn>
1296 struct __constrain_pkgdtask<_Task, _Fn, true>
1300 template<typename _Res, typename... _ArgTypes>
1301 class packaged_task<_Res(_ArgTypes...)>
1303 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1304 shared_ptr<_State_type> _M_state;
1307 // Construction and destruction
1308 packaged_task() noexcept { }
1310 template<typename _Allocator>
1312 packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
1315 template<typename _Fn, typename = typename
1316 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1318 packaged_task(_Fn&& __fn)
1319 : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn)))
1322 template<typename _Fn, typename _Allocator, typename = typename
1323 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1325 packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn)
1326 : _M_state(std::allocate_shared<_State_type>(__a,
1327 std::forward<_Fn>(__fn)))
1332 if (static_cast<bool>(_M_state) && !_M_state.unique())
1333 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1337 packaged_task(const packaged_task&) = delete;
1338 packaged_task& operator=(const packaged_task&) = delete;
1340 template<typename _Allocator>
1342 packaged_task(allocator_arg_t, const _Allocator&,
1343 const packaged_task&) = delete;
1346 packaged_task(packaged_task&& __other) noexcept
1347 { this->swap(__other); }
1349 template<typename _Allocator>
1351 packaged_task(allocator_arg_t, const _Allocator&,
1352 packaged_task&& __other) noexcept
1353 { this->swap(__other); }
1355 packaged_task& operator=(packaged_task&& __other) noexcept
1357 packaged_task(std::move(__other)).swap(*this);
1362 swap(packaged_task& __other) noexcept
1363 { _M_state.swap(__other._M_state); }
1366 valid() const noexcept
1367 { return static_cast<bool>(_M_state); }
1372 { return future<_Res>(_M_state); }
1376 operator()(_ArgTypes... __args)
1378 __future_base::_State_base::_S_check(_M_state);
1379 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1385 __future_base::_State_base::_S_check(_M_state);
1386 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1391 template<typename _Res, typename... _ArgTypes>
1393 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1394 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1397 template<typename _Res, typename _Alloc>
1398 struct uses_allocator<packaged_task<_Res>, _Alloc>
1399 : public true_type { };
1402 template<typename _BoundFn, typename _Res>
1403 class __future_base::_Deferred_state final
1404 : public __future_base::_State_base
1408 _Deferred_state(_BoundFn&& __fn)
1409 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1413 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1414 _Ptr_type _M_result;
1420 // safe to call multiple times so ignore failure
1421 _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1425 class __future_base::_Async_state_common : public __future_base::_State_base
1428 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
1429 ~_Async_state_common();
1431 ~_Async_state_common() = default;
1434 // Allow non-timed waiting functions to block until the thread completes,
1436 virtual void _M_run_deferred() { _M_join(); }
1438 void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
1444 template<typename _BoundFn, typename _Res>
1445 class __future_base::_Async_state_impl final
1446 : public __future_base::_Async_state_common
1450 _Async_state_impl(_BoundFn&& __fn)
1451 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1453 _M_thread = std::thread{ [this] {
1454 _M_set_result(_S_task_setter(_M_result, _M_fn));
1458 ~_Async_state_impl() { _M_join(); }
1461 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1462 _Ptr_type _M_result;
1466 template<typename _BoundFn>
1467 inline std::shared_ptr<__future_base::_State_base>
1468 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1470 typedef typename remove_reference<_BoundFn>::type __fn_type;
1471 typedef _Deferred_state<__fn_type> __state_type;
1472 return std::make_shared<__state_type>(std::move(__fn));
1475 template<typename _BoundFn>
1476 inline std::shared_ptr<__future_base::_State_base>
1477 __future_base::_S_make_async_state(_BoundFn&& __fn)
1479 typedef typename remove_reference<_BoundFn>::type __fn_type;
1480 typedef _Async_state_impl<__fn_type> __state_type;
1481 return std::make_shared<__state_type>(std::move(__fn));
1486 template<typename _Fn, typename... _Args>
1487 future<typename result_of<_Fn(_Args...)>::type>
1488 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1490 typedef typename result_of<_Fn(_Args...)>::type result_type;
1491 std::shared_ptr<__future_base::_State_base> __state;
1492 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1494 __state = __future_base::_S_make_async_state(std::__bind_simple(
1495 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1499 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1500 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1502 return future<result_type>(__state);
1505 /// async, potential overload
1506 template<typename _Fn, typename... _Args>
1508 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
1509 async(_Fn&& __fn, _Args&&... __args)
1511 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1512 std::forward<_Args>(__args)...);
1515 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1516 // && ATOMIC_INT_LOCK_FREE
1519 _GLIBCXX_END_NAMESPACE_VERSION
1522 #endif // __GXX_EXPERIMENTAL_CXX0X__
1524 #endif // _GLIBCXX_FUTURE