1 // <future> -*- C++ -*-
3 // Copyright (C) 2009-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 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
41 #include <condition_variable>
42 #include <system_error>
44 #include <bits/functexcept.h>
45 #include <bits/unique_ptr.h>
46 #include <bits/shared_ptr.h>
47 #include <bits/uses_allocator.h>
48 #include <bits/alloc_traits.h>
50 namespace std _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 * @defgroup futures Futures
56 * @ingroup concurrency
58 * Classes for futures support.
62 /// Error code for futures
63 enum class future_errc
65 future_already_retrieved = 1,
66 promise_already_satisfied,
73 struct is_error_code_enum<future_errc> : public true_type { };
75 /// Points to a statically-allocated object derived from error_category.
77 future_category() noexcept;
79 /// Overload for make_error_code.
81 make_error_code(future_errc __errc) noexcept
82 { return error_code(static_cast<int>(__errc), future_category()); }
84 /// Overload for make_error_condition.
85 inline error_condition
86 make_error_condition(future_errc __errc) noexcept
87 { return error_condition(static_cast<int>(__errc), future_category()); }
90 * @brief Exception type thrown by futures.
93 class future_error : public logic_error
98 explicit future_error(error_code __ec)
99 : logic_error("std::future_error"), _M_code(__ec)
102 virtual ~future_error() noexcept;
105 what() const noexcept;
108 code() const noexcept { return _M_code; }
111 // Forward declarations.
112 template<typename _Res>
115 template<typename _Res>
118 template<typename _Signature>
121 template<typename _Res>
124 /// Launch code for futures
131 constexpr launch operator&(launch __x, launch __y)
133 return static_cast<launch>(
134 static_cast<int>(__x) & static_cast<int>(__y));
137 constexpr launch operator|(launch __x, launch __y)
139 return static_cast<launch>(
140 static_cast<int>(__x) | static_cast<int>(__y));
143 constexpr launch operator^(launch __x, launch __y)
145 return static_cast<launch>(
146 static_cast<int>(__x) ^ static_cast<int>(__y));
149 constexpr launch operator~(launch __x)
150 { return static_cast<launch>(~static_cast<int>(__x)); }
152 inline launch& operator&=(launch& __x, launch __y)
153 { return __x = __x & __y; }
155 inline launch& operator|=(launch& __x, launch __y)
156 { return __x = __x | __y; }
158 inline launch& operator^=(launch& __x, launch __y)
159 { return __x = __x ^ __y; }
161 /// Status code for futures
162 enum class future_status
169 template<typename _Fn, typename... _Args>
170 future<typename result_of<_Fn(_Args...)>::type>
171 async(launch __policy, _Fn&& __fn, _Args&&... __args);
173 template<typename _Fn, typename... _Args>
174 future<typename result_of<_Fn(_Args...)>::type>
175 async(_Fn&& __fn, _Args&&... __args);
177 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
178 && (ATOMIC_INT_LOCK_FREE > 1)
180 /// Base class and enclosing scope.
183 /// Base class for results.
186 exception_ptr _M_error;
188 _Result_base(const _Result_base&) = delete;
189 _Result_base& operator=(const _Result_base&) = delete;
191 // _M_destroy() allows derived classes to control deallocation
192 virtual void _M_destroy() = 0;
196 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
201 virtual ~_Result_base();
205 template<typename _Res>
206 struct _Result : _Result_base
209 typedef alignment_of<_Res> __a_of;
210 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
211 typedef typename __align_storage::type __align_type;
213 __align_type _M_storage;
217 _Result() noexcept : _M_initialized() { }
225 // Return lvalue, future will add const or rvalue-reference
227 _M_value() noexcept { return *static_cast<_Res*>(_M_addr()); }
230 _M_set(const _Res& __res)
232 ::new (_M_addr()) _Res(__res);
233 _M_initialized = true;
239 ::new (_M_addr()) _Res(std::move(__res));
240 _M_initialized = true;
244 void _M_destroy() { delete this; }
246 void* _M_addr() noexcept { return static_cast<void*>(&_M_storage); }
249 /// A unique_ptr based on the instantiating type.
250 template<typename _Res>
251 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
254 template<typename _Res, typename _Alloc>
255 struct _Result_alloc final : _Result<_Res>, _Alloc
257 typedef typename allocator_traits<_Alloc>::template
258 rebind_alloc<_Result_alloc> __allocator_type;
261 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
267 typedef allocator_traits<__allocator_type> __traits;
268 __allocator_type __a(*this);
269 __traits::destroy(__a, this);
270 __traits::deallocate(__a, this, 1);
274 template<typename _Res, typename _Allocator>
275 static _Ptr<_Result_alloc<_Res, _Allocator>>
276 _S_allocate_result(const _Allocator& __a)
278 typedef _Result_alloc<_Res, _Allocator> __result_type;
279 typedef allocator_traits<typename __result_type::__allocator_type>
281 typename __traits::allocator_type __a2(__a);
282 __result_type* __p = __traits::allocate(__a2, 1);
285 __traits::construct(__a2, __p, __a);
289 __traits::deallocate(__a2, __p, 1);
290 __throw_exception_again;
292 return _Ptr<__result_type>(__p);
296 /// Base class for state between a promise and one or more
297 /// associated futures.
300 typedef _Ptr<_Result_base> _Ptr_type;
304 condition_variable _M_cond;
305 atomic_flag _M_retrieved;
309 _State_base() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
310 _State_base(const _State_base&) = delete;
311 _State_base& operator=(const _State_base&) = delete;
312 virtual ~_State_base();
318 unique_lock<mutex> __lock(_M_mutex);
319 _M_cond.wait(__lock, [&] { return _M_ready(); });
323 template<typename _Rep, typename _Period>
325 wait_for(const chrono::duration<_Rep, _Period>& __rel)
327 unique_lock<mutex> __lock(_M_mutex);
328 if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
329 return future_status::ready;
330 return future_status::timeout;
333 template<typename _Clock, typename _Duration>
335 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
337 unique_lock<mutex> __lock(_M_mutex);
338 if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
339 return future_status::ready;
340 return future_status::timeout;
344 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
346 bool __set = __ignore_failure;
347 // all calls to this function are serialized,
348 // side-effects of invoking __res only happen once
349 call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
352 __throw_future_error(int(future_errc::promise_already_satisfied));
356 _M_break_promise(_Ptr_type __res)
358 if (static_cast<bool>(__res))
360 error_code __ec(make_error_code(future_errc::broken_promise));
361 __res->_M_error = copy_exception(future_error(__ec));
363 lock_guard<mutex> __lock(_M_mutex);
364 _M_result.swap(__res);
366 _M_cond.notify_all();
370 // Called when this object is passed to a future.
372 _M_set_retrieved_flag()
374 if (_M_retrieved.test_and_set())
375 __throw_future_error(int(future_errc::future_already_retrieved));
378 template<typename _Res, typename _Arg>
382 template<typename _Res, typename _Arg>
383 struct _Setter<_Res, _Arg&>
385 // check this is only used by promise<R>::set_value(const R&)
386 // or promise<R>::set_value(R&)
387 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
388 || is_same<const _Res, _Arg>::value, // promise<R>
389 "Invalid specialisation");
391 typename promise<_Res>::_Ptr_type operator()()
393 _State_base::_S_check(_M_promise->_M_future);
394 _M_promise->_M_storage->_M_set(_M_arg);
395 return std::move(_M_promise->_M_storage);
397 promise<_Res>* _M_promise;
402 template<typename _Res>
403 struct _Setter<_Res, _Res&&>
405 typename promise<_Res>::_Ptr_type operator()()
407 _State_base::_S_check(_M_promise->_M_future);
408 _M_promise->_M_storage->_M_set(std::move(_M_arg));
409 return std::move(_M_promise->_M_storage);
411 promise<_Res>* _M_promise;
415 struct __exception_ptr_tag { };
418 template<typename _Res>
419 struct _Setter<_Res, __exception_ptr_tag>
421 typename promise<_Res>::_Ptr_type operator()()
423 _State_base::_S_check(_M_promise->_M_future);
424 _M_promise->_M_storage->_M_error = _M_ex;
425 return std::move(_M_promise->_M_storage);
428 promise<_Res>* _M_promise;
429 exception_ptr& _M_ex;
432 template<typename _Res, typename _Arg>
433 static _Setter<_Res, _Arg&&>
434 __setter(promise<_Res>* __prom, _Arg&& __arg)
436 return _Setter<_Res, _Arg&&>{ __prom, __arg };
439 template<typename _Res>
440 static _Setter<_Res, __exception_ptr_tag>
441 __setter(exception_ptr& __ex, promise<_Res>* __prom)
443 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
446 static _Setter<void, void>
447 __setter(promise<void>* __prom);
449 template<typename _Tp>
451 _S_check(const shared_ptr<_Tp>& __p)
453 if (!static_cast<bool>(__p))
454 __throw_future_error((int)future_errc::no_state);
459 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
461 _Ptr_type __res = __f();
463 lock_guard<mutex> __lock(_M_mutex);
464 _M_result.swap(__res);
466 _M_cond.notify_all();
470 bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
472 // Misnamed: waits for completion of async function.
473 virtual void _M_run_deferred() { }
476 template<typename _BoundFn, typename = typename _BoundFn::result_type>
477 class _Deferred_state;
479 class _Async_state_common;
481 template<typename _BoundFn, typename = typename _BoundFn::result_type>
482 class _Async_state_impl;
484 template<typename _Signature>
487 template<typename _BoundFn>
488 static std::shared_ptr<_State_base>
489 _S_make_deferred_state(_BoundFn&& __fn);
491 template<typename _BoundFn>
492 static std::shared_ptr<_State_base>
493 _S_make_async_state(_BoundFn&& __fn);
495 template<typename _Res_ptr, typename _Res>
498 template<typename _Res_ptr, typename _BoundFn>
499 class _Task_setter_helper
501 typedef typename remove_reference<_BoundFn>::type::result_type __res;
503 typedef _Task_setter<_Res_ptr, __res> __type;
506 template<typename _Res_ptr, typename _BoundFn>
507 static typename _Task_setter_helper<_Res_ptr, _BoundFn>::__type
508 _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
510 typedef _Task_setter_helper<_Res_ptr, _BoundFn> __helper_type;
511 typedef typename __helper_type::__type _Setter;
512 return _Setter{ __ptr, std::ref(__call) };
516 /// Partial specialization for reference types.
517 template<typename _Res>
518 struct __future_base::_Result<_Res&> : __future_base::_Result_base
520 _Result() noexcept : _M_value_ptr() { }
522 void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
524 _Res& _M_get() noexcept { return *_M_value_ptr; }
529 void _M_destroy() { delete this; }
532 /// Explicit specialization for void.
534 struct __future_base::_Result<void> : __future_base::_Result_base
537 void _M_destroy() { delete this; }
541 /// Common implementation for future and shared_future.
542 template<typename _Res>
543 class __basic_future : public __future_base
546 typedef shared_ptr<_State_base> __state_type;
547 typedef __future_base::_Result<_Res>& __result_type;
550 __state_type _M_state;
554 __basic_future(const __basic_future&) = delete;
555 __basic_future& operator=(const __basic_future&) = delete;
558 valid() const noexcept { return static_cast<bool>(_M_state); }
563 _State_base::_S_check(_M_state);
567 template<typename _Rep, typename _Period>
569 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
571 _State_base::_S_check(_M_state);
572 return _M_state->wait_for(__rel);
575 template<typename _Clock, typename _Duration>
577 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
579 _State_base::_S_check(_M_state);
580 return _M_state->wait_until(__abs);
584 /// Wait for the state to be ready and rethrow any stored exception
588 _State_base::_S_check(_M_state);
589 _Result_base& __res = _M_state->wait();
590 if (!(__res._M_error == 0))
591 rethrow_exception(__res._M_error);
592 return static_cast<__result_type>(__res);
595 void _M_swap(__basic_future& __that) noexcept
597 _M_state.swap(__that._M_state);
600 // Construction of a future by promise::get_future()
602 __basic_future(const __state_type& __state) : _M_state(__state)
604 _State_base::_S_check(_M_state);
605 _M_state->_M_set_retrieved_flag();
608 // Copy construction from a shared_future
610 __basic_future(const shared_future<_Res>&) noexcept;
612 // Move construction from a shared_future
614 __basic_future(shared_future<_Res>&&) noexcept;
616 // Move construction from a future
618 __basic_future(future<_Res>&&) noexcept;
620 constexpr __basic_future() noexcept : _M_state() { }
624 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
625 ~_Reset() { _M_fut._M_state.reset(); }
626 __basic_future& _M_fut;
631 /// Primary template for future.
632 template<typename _Res>
633 class future : public __basic_future<_Res>
635 friend class promise<_Res>;
636 template<typename> friend class packaged_task;
637 template<typename _Fn, typename... _Args>
638 friend future<typename result_of<_Fn(_Args...)>::type>
639 async(launch, _Fn&&, _Args&&...);
641 typedef __basic_future<_Res> _Base_type;
642 typedef typename _Base_type::__state_type __state_type;
645 future(const __state_type& __state) : _Base_type(__state) { }
648 constexpr future() noexcept : _Base_type() { }
651 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
654 future(const future&) = delete;
655 future& operator=(const future&) = delete;
657 future& operator=(future&& __fut) noexcept
659 future(std::move(__fut))._M_swap(*this);
663 /// Retrieving the value
667 typename _Base_type::_Reset __reset(*this);
668 return std::move(this->_M_get_result()._M_value());
671 shared_future<_Res> share();
674 /// Partial specialization for future<R&>
675 template<typename _Res>
676 class future<_Res&> : public __basic_future<_Res&>
678 friend class promise<_Res&>;
679 template<typename> friend class packaged_task;
680 template<typename _Fn, typename... _Args>
681 friend future<typename result_of<_Fn(_Args...)>::type>
682 async(launch, _Fn&&, _Args&&...);
684 typedef __basic_future<_Res&> _Base_type;
685 typedef typename _Base_type::__state_type __state_type;
688 future(const __state_type& __state) : _Base_type(__state) { }
691 constexpr future() noexcept : _Base_type() { }
694 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
697 future(const future&) = delete;
698 future& operator=(const future&) = delete;
700 future& operator=(future&& __fut) noexcept
702 future(std::move(__fut))._M_swap(*this);
706 /// Retrieving the value
710 typename _Base_type::_Reset __reset(*this);
711 return this->_M_get_result()._M_get();
714 shared_future<_Res&> share();
717 /// Explicit specialization for future<void>
719 class future<void> : public __basic_future<void>
721 friend class promise<void>;
722 template<typename> friend class packaged_task;
723 template<typename _Fn, typename... _Args>
724 friend future<typename result_of<_Fn(_Args...)>::type>
725 async(launch, _Fn&&, _Args&&...);
727 typedef __basic_future<void> _Base_type;
728 typedef typename _Base_type::__state_type __state_type;
731 future(const __state_type& __state) : _Base_type(__state) { }
734 constexpr future() noexcept : _Base_type() { }
737 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
740 future(const future&) = delete;
741 future& operator=(const future&) = delete;
743 future& operator=(future&& __fut) noexcept
745 future(std::move(__fut))._M_swap(*this);
749 /// Retrieving the value
753 typename _Base_type::_Reset __reset(*this);
754 this->_M_get_result();
757 shared_future<void> share();
761 /// Primary template for shared_future.
762 template<typename _Res>
763 class shared_future : public __basic_future<_Res>
765 typedef __basic_future<_Res> _Base_type;
768 constexpr shared_future() noexcept : _Base_type() { }
771 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
773 /// Construct from a future rvalue
774 shared_future(future<_Res>&& __uf) noexcept
775 : _Base_type(std::move(__uf))
778 /// Construct from a shared_future rvalue
779 shared_future(shared_future&& __sf) noexcept
780 : _Base_type(std::move(__sf))
783 shared_future& operator=(const shared_future& __sf)
785 shared_future(__sf)._M_swap(*this);
789 shared_future& operator=(shared_future&& __sf) noexcept
791 shared_future(std::move(__sf))._M_swap(*this);
795 /// Retrieving the value
799 typename _Base_type::__result_type __r = this->_M_get_result();
800 _Res& __rs(__r._M_value());
805 /// Partial specialization for shared_future<R&>
806 template<typename _Res>
807 class shared_future<_Res&> : public __basic_future<_Res&>
809 typedef __basic_future<_Res&> _Base_type;
812 constexpr shared_future() noexcept : _Base_type() { }
815 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
817 /// Construct from a future rvalue
818 shared_future(future<_Res&>&& __uf) noexcept
819 : _Base_type(std::move(__uf))
822 /// Construct from a shared_future rvalue
823 shared_future(shared_future&& __sf) noexcept
824 : _Base_type(std::move(__sf))
827 shared_future& operator=(const shared_future& __sf)
829 shared_future(__sf)._M_swap(*this);
833 shared_future& operator=(shared_future&& __sf) noexcept
835 shared_future(std::move(__sf))._M_swap(*this);
839 /// Retrieving the value
841 get() { return this->_M_get_result()._M_get(); }
844 /// Explicit specialization for shared_future<void>
846 class shared_future<void> : public __basic_future<void>
848 typedef __basic_future<void> _Base_type;
851 constexpr shared_future() noexcept : _Base_type() { }
854 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
856 /// Construct from a future rvalue
857 shared_future(future<void>&& __uf) noexcept
858 : _Base_type(std::move(__uf))
861 /// Construct from a shared_future rvalue
862 shared_future(shared_future&& __sf) noexcept
863 : _Base_type(std::move(__sf))
866 shared_future& operator=(const shared_future& __sf)
868 shared_future(__sf)._M_swap(*this);
872 shared_future& operator=(shared_future&& __sf) noexcept
874 shared_future(std::move(__sf))._M_swap(*this);
878 // Retrieving the value
880 get() { this->_M_get_result(); }
883 // Now we can define the protected __basic_future constructors.
884 template<typename _Res>
885 inline __basic_future<_Res>::
886 __basic_future(const shared_future<_Res>& __sf) noexcept
887 : _M_state(__sf._M_state)
890 template<typename _Res>
891 inline __basic_future<_Res>::
892 __basic_future(shared_future<_Res>&& __sf) noexcept
893 : _M_state(std::move(__sf._M_state))
896 template<typename _Res>
897 inline __basic_future<_Res>::
898 __basic_future(future<_Res>&& __uf) noexcept
899 : _M_state(std::move(__uf._M_state))
902 template<typename _Res>
903 inline shared_future<_Res>
904 future<_Res>::share()
905 { return shared_future<_Res>(std::move(*this)); }
907 template<typename _Res>
908 inline shared_future<_Res&>
909 future<_Res&>::share()
910 { return shared_future<_Res&>(std::move(*this)); }
912 inline shared_future<void>
913 future<void>::share()
914 { return shared_future<void>(std::move(*this)); }
916 /// Primary template for promise
917 template<typename _Res>
920 typedef __future_base::_State_base _State;
921 typedef __future_base::_Result<_Res> _Res_type;
922 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
923 template<typename, typename> friend class _State::_Setter;
925 shared_ptr<_State> _M_future;
926 _Ptr_type _M_storage;
930 : _M_future(std::make_shared<_State>()),
931 _M_storage(new _Res_type())
934 promise(promise&& __rhs) noexcept
935 : _M_future(std::move(__rhs._M_future)),
936 _M_storage(std::move(__rhs._M_storage))
939 template<typename _Allocator>
940 promise(allocator_arg_t, const _Allocator& __a)
941 : _M_future(std::allocate_shared<_State>(__a)),
942 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
945 template<typename _Allocator>
946 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
947 : _M_future(std::move(__rhs._M_future)),
948 _M_storage(std::move(__rhs._M_storage))
951 promise(const promise&) = delete;
955 if (static_cast<bool>(_M_future) && !_M_future.unique())
956 _M_future->_M_break_promise(std::move(_M_storage));
961 operator=(promise&& __rhs) noexcept
963 promise(std::move(__rhs)).swap(*this);
967 promise& operator=(const promise&) = delete;
970 swap(promise& __rhs) noexcept
972 _M_future.swap(__rhs._M_future);
973 _M_storage.swap(__rhs._M_storage);
976 // Retrieving the result
979 { return future<_Res>(_M_future); }
981 // Setting the result
983 set_value(const _Res& __r)
985 auto __setter = _State::__setter(this, __r);
986 _M_future->_M_set_result(std::move(__setter));
990 set_value(_Res&& __r)
992 auto __setter = _State::__setter(this, std::move(__r));
993 _M_future->_M_set_result(std::move(__setter));
997 set_exception(exception_ptr __p)
999 auto __setter = _State::__setter(__p, this);
1000 _M_future->_M_set_result(std::move(__setter));
1004 template<typename _Res>
1006 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1009 template<typename _Res, typename _Alloc>
1010 struct uses_allocator<promise<_Res>, _Alloc>
1011 : public true_type { };
1014 /// Partial specialization for promise<R&>
1015 template<typename _Res>
1016 class promise<_Res&>
1018 typedef __future_base::_State_base _State;
1019 typedef __future_base::_Result<_Res&> _Res_type;
1020 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1021 template<typename, typename> friend class _State::_Setter;
1023 shared_ptr<_State> _M_future;
1024 _Ptr_type _M_storage;
1028 : _M_future(std::make_shared<_State>()),
1029 _M_storage(new _Res_type())
1032 promise(promise&& __rhs) noexcept
1033 : _M_future(std::move(__rhs._M_future)),
1034 _M_storage(std::move(__rhs._M_storage))
1037 template<typename _Allocator>
1038 promise(allocator_arg_t, const _Allocator& __a)
1039 : _M_future(std::allocate_shared<_State>(__a)),
1040 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1043 template<typename _Allocator>
1044 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1045 : _M_future(std::move(__rhs._M_future)),
1046 _M_storage(std::move(__rhs._M_storage))
1049 promise(const promise&) = delete;
1053 if (static_cast<bool>(_M_future) && !_M_future.unique())
1054 _M_future->_M_break_promise(std::move(_M_storage));
1059 operator=(promise&& __rhs) noexcept
1061 promise(std::move(__rhs)).swap(*this);
1065 promise& operator=(const promise&) = delete;
1068 swap(promise& __rhs) noexcept
1070 _M_future.swap(__rhs._M_future);
1071 _M_storage.swap(__rhs._M_storage);
1074 // Retrieving the result
1077 { return future<_Res&>(_M_future); }
1079 // Setting the result
1081 set_value(_Res& __r)
1083 auto __setter = _State::__setter(this, __r);
1084 _M_future->_M_set_result(std::move(__setter));
1088 set_exception(exception_ptr __p)
1090 auto __setter = _State::__setter(__p, this);
1091 _M_future->_M_set_result(std::move(__setter));
1095 /// Explicit specialization for promise<void>
1099 typedef __future_base::_State_base _State;
1100 typedef __future_base::_Result<void> _Res_type;
1101 typedef __future_base::_Ptr<_Res_type> _Ptr_type;
1102 template<typename, typename> friend class _State::_Setter;
1104 shared_ptr<_State> _M_future;
1105 _Ptr_type _M_storage;
1109 : _M_future(std::make_shared<_State>()),
1110 _M_storage(new _Res_type())
1113 promise(promise&& __rhs) noexcept
1114 : _M_future(std::move(__rhs._M_future)),
1115 _M_storage(std::move(__rhs._M_storage))
1118 template<typename _Allocator>
1119 promise(allocator_arg_t, const _Allocator& __a)
1120 : _M_future(std::allocate_shared<_State>(__a)),
1121 _M_storage(__future_base::_S_allocate_result<void>(__a))
1124 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1125 // 2095. missing constructors needed for uses-allocator construction
1126 template<typename _Allocator>
1127 promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1128 : _M_future(std::move(__rhs._M_future)),
1129 _M_storage(std::move(__rhs._M_storage))
1132 promise(const promise&) = delete;
1136 if (static_cast<bool>(_M_future) && !_M_future.unique())
1137 _M_future->_M_break_promise(std::move(_M_storage));
1142 operator=(promise&& __rhs) noexcept
1144 promise(std::move(__rhs)).swap(*this);
1148 promise& operator=(const promise&) = delete;
1151 swap(promise& __rhs) noexcept
1153 _M_future.swap(__rhs._M_future);
1154 _M_storage.swap(__rhs._M_storage);
1157 // Retrieving the result
1160 { return future<void>(_M_future); }
1162 // Setting the result
1166 set_exception(exception_ptr __p)
1168 auto __setter = _State::__setter(__p, this);
1169 _M_future->_M_set_result(std::move(__setter));
1175 struct __future_base::_State_base::_Setter<void, void>
1177 promise<void>::_Ptr_type operator()()
1179 _State_base::_S_check(_M_promise->_M_future);
1180 return std::move(_M_promise->_M_storage);
1183 promise<void>* _M_promise;
1186 inline __future_base::_State_base::_Setter<void, void>
1187 __future_base::_State_base::__setter(promise<void>* __prom)
1189 return _Setter<void, void>{ __prom };
1193 promise<void>::set_value()
1195 auto __setter = _State::__setter(this);
1196 _M_future->_M_set_result(std::move(__setter));
1200 template<typename _Ptr_type, typename _Res>
1201 struct __future_base::_Task_setter
1203 _Ptr_type operator()()
1207 _M_result->_M_set(_M_fn());
1211 _M_result->_M_error = current_exception();
1213 return std::move(_M_result);
1215 _Ptr_type& _M_result;
1216 std::function<_Res()> _M_fn;
1219 template<typename _Ptr_type>
1220 struct __future_base::_Task_setter<_Ptr_type, void>
1222 _Ptr_type operator()()
1230 _M_result->_M_error = current_exception();
1232 return std::move(_M_result);
1234 _Ptr_type& _M_result;
1235 std::function<void()> _M_fn;
1238 template<typename _Res, typename... _Args>
1239 struct __future_base::_Task_state<_Res(_Args...)> final
1240 : __future_base::_State_base
1242 typedef _Res _Res_type;
1244 _Task_state(std::function<_Res(_Args...)> __task)
1245 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1248 template<typename _Func, typename _Alloc>
1249 _Task_state(_Func&& __task, const _Alloc& __a)
1250 : _M_result(_S_allocate_result<_Res>(__a)),
1251 _M_task(allocator_arg, __a, std::move(__task))
1255 _M_run(_Args... __args)
1257 // bound arguments decay so wrap lvalue references
1258 auto __boundfn = std::__bind_simple(std::ref(_M_task),
1259 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1260 auto __setter = _S_task_setter(_M_result, std::move(__boundfn));
1261 _M_set_result(std::move(__setter));
1264 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1265 _Ptr_type _M_result;
1266 std::function<_Res(_Args...)> _M_task;
1268 template<typename _Tp>
1269 static reference_wrapper<_Tp>
1270 _S_maybe_wrap_ref(_Tp& __t)
1271 { return std::ref(__t); }
1273 template<typename _Tp>
1274 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1276 _S_maybe_wrap_ref(_Tp&& __t)
1277 { return std::forward<_Tp>(__t); }
1280 template<typename _Task, typename _Fn, bool
1281 = is_same<_Task, typename decay<_Fn>::type>::value>
1282 struct __constrain_pkgdtask
1283 { typedef void __type; };
1285 template<typename _Task, typename _Fn>
1286 struct __constrain_pkgdtask<_Task, _Fn, true>
1290 template<typename _Res, typename... _ArgTypes>
1291 class packaged_task<_Res(_ArgTypes...)>
1293 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1294 shared_ptr<_State_type> _M_state;
1297 // Construction and destruction
1298 packaged_task() noexcept { }
1300 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1301 // 2095. missing constructors needed for uses-allocator construction
1302 template<typename _Allocator>
1304 packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
1307 template<typename _Fn, typename = typename
1308 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1310 packaged_task(_Fn&& __fn)
1311 : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn)))
1314 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1315 // 2097. packaged_task constructors should be constrained
1316 template<typename _Fn, typename _Allocator, typename = typename
1317 __constrain_pkgdtask<packaged_task, _Fn>::__type>
1319 packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn)
1320 : _M_state(std::allocate_shared<_State_type>(__a,
1321 std::forward<_Fn>(__fn)))
1326 if (static_cast<bool>(_M_state) && !_M_state.unique())
1327 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1331 packaged_task(const packaged_task&) = delete;
1332 packaged_task& operator=(const packaged_task&) = delete;
1334 template<typename _Allocator>
1336 packaged_task(allocator_arg_t, const _Allocator&,
1337 const packaged_task&) = delete;
1340 packaged_task(packaged_task&& __other) noexcept
1341 { this->swap(__other); }
1343 template<typename _Allocator>
1345 packaged_task(allocator_arg_t, const _Allocator&,
1346 packaged_task&& __other) noexcept
1347 { this->swap(__other); }
1349 packaged_task& operator=(packaged_task&& __other) noexcept
1351 packaged_task(std::move(__other)).swap(*this);
1356 swap(packaged_task& __other) noexcept
1357 { _M_state.swap(__other._M_state); }
1360 valid() const noexcept
1361 { return static_cast<bool>(_M_state); }
1366 { return future<_Res>(_M_state); }
1370 operator()(_ArgTypes... __args)
1372 __future_base::_State_base::_S_check(_M_state);
1373 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1379 __future_base::_State_base::_S_check(_M_state);
1380 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1385 template<typename _Res, typename... _ArgTypes>
1387 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1388 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1391 template<typename _Res, typename _Alloc>
1392 struct uses_allocator<packaged_task<_Res>, _Alloc>
1393 : public true_type { };
1396 template<typename _BoundFn, typename _Res>
1397 class __future_base::_Deferred_state final
1398 : public __future_base::_State_base
1402 _Deferred_state(_BoundFn&& __fn)
1403 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1407 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1408 _Ptr_type _M_result;
1414 // safe to call multiple times so ignore failure
1415 _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1419 class __future_base::_Async_state_common : public __future_base::_State_base
1422 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
1423 ~_Async_state_common();
1425 ~_Async_state_common() = default;
1428 // Allow non-timed waiting functions to block until the thread completes,
1430 virtual void _M_run_deferred() { _M_join(); }
1432 void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
1438 template<typename _BoundFn, typename _Res>
1439 class __future_base::_Async_state_impl final
1440 : public __future_base::_Async_state_common
1444 _Async_state_impl(_BoundFn&& __fn)
1445 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1447 _M_thread = std::thread{ [this] {
1448 _M_set_result(_S_task_setter(_M_result, _M_fn));
1452 ~_Async_state_impl() { _M_join(); }
1455 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1456 _Ptr_type _M_result;
1460 template<typename _BoundFn>
1461 inline std::shared_ptr<__future_base::_State_base>
1462 __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1464 typedef typename remove_reference<_BoundFn>::type __fn_type;
1465 typedef _Deferred_state<__fn_type> __state_type;
1466 return std::make_shared<__state_type>(std::move(__fn));
1469 template<typename _BoundFn>
1470 inline std::shared_ptr<__future_base::_State_base>
1471 __future_base::_S_make_async_state(_BoundFn&& __fn)
1473 typedef typename remove_reference<_BoundFn>::type __fn_type;
1474 typedef _Async_state_impl<__fn_type> __state_type;
1475 return std::make_shared<__state_type>(std::move(__fn));
1480 template<typename _Fn, typename... _Args>
1481 future<typename result_of<_Fn(_Args...)>::type>
1482 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1484 typedef typename result_of<_Fn(_Args...)>::type result_type;
1485 std::shared_ptr<__future_base::_State_base> __state;
1486 if ((__policy & (launch::async|launch::deferred)) == launch::async)
1488 __state = __future_base::_S_make_async_state(std::__bind_simple(
1489 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1493 __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1494 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1496 return future<result_type>(__state);
1499 /// async, potential overload
1500 template<typename _Fn, typename... _Args>
1501 inline future<typename result_of<_Fn(_Args...)>::type>
1502 async(_Fn&& __fn, _Args&&... __args)
1504 return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1505 std::forward<_Args>(__args)...);
1508 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1509 // && ATOMIC_INT_LOCK_FREE
1512 _GLIBCXX_END_NAMESPACE_VERSION
1517 #endif // _GLIBCXX_FUTURE