1 // <future> -*- C++ -*-
3 // Copyright (C) 2009, 2010 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 _GLIBCXX_BEGIN_NAMESPACE(std)
51 * @defgroup futures Futures
52 * @ingroup concurrency
54 * Classes for futures support.
58 /// Error code for futures
59 enum class future_errc
62 future_already_retrieved,
63 promise_already_satisfied,
69 struct is_error_code_enum<future_errc> : public true_type { };
71 /// Points to a statically-allocated object derived from error_category.
75 /// Overload for make_error_code.
77 make_error_code(future_errc __errc)
78 { return error_code(static_cast<int>(__errc), future_category()); }
80 /// Overload for make_error_condition.
81 inline error_condition
82 make_error_condition(future_errc __errc)
83 { return error_condition(static_cast<int>(__errc), future_category()); }
86 * @brief Exception type thrown by futures.
89 class future_error : public logic_error
94 explicit future_error(error_code __ec)
95 : logic_error("std::future_error"), _M_code(__ec)
98 virtual ~future_error() throw();
101 what() const throw();
104 code() const throw() { return _M_code; }
107 // Forward declarations.
108 template<typename _Res>
111 template<typename _Res>
114 template<typename _Res>
117 template<typename _Signature>
120 template<typename _Res>
123 /// Launch code for futures
131 /// Status code for futures
132 enum class future_status
139 template<typename _Fn, typename... _Args>
140 future<typename result_of<_Fn(_Args...)>::type>
141 async(launch __policy, _Fn&& __fn, _Args&&... __args);
143 template<typename _Fn, typename... _Args>
145 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
146 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
148 async(_Fn&& __fn, _Args&&... __args);
150 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
151 && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
153 /// Base class and enclosing scope.
156 /// Base class for results.
159 exception_ptr _M_error;
161 _Result_base() = default;
162 _Result_base(const _Result_base&) = delete;
163 _Result_base& operator=(const _Result_base&) = delete;
165 // _M_destroy() allows derived classes to control deallocation
166 virtual void _M_destroy() = 0;
170 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
178 template<typename _Res>
179 struct _Result : _Result_base
182 typedef alignment_of<_Res> __a_of;
183 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
184 typedef typename __align_storage::type __align_type;
186 __align_type _M_storage;
190 _Result() : _M_initialized() { }
198 // Return lvalue, future will add const or rvalue-reference
200 _M_value() { return *static_cast<_Res*>(_M_addr()); }
203 _M_set(const _Res& __res)
205 ::new (_M_addr()) _Res(__res);
206 _M_initialized = true;
212 ::new (_M_addr()) _Res(std::move(__res));
213 _M_initialized = true;
217 void _M_destroy() { delete this; }
219 void* _M_addr() { return static_cast<void*>(&_M_storage); }
222 // TODO: use template alias when available
224 template<typename _Res>
225 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
227 /// A unique_ptr based on the instantiating type.
228 template<typename _Res>
231 typedef unique_ptr<_Res, _Result_base::_Deleter> type;
235 template<typename _Res, typename _Alloc>
236 struct _Result_alloc : _Result<_Res>, _Alloc
238 typedef typename _Alloc::template rebind<_Result_alloc>::other
242 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
248 __allocator_type __a(*this);
250 __a.deallocate(this, 1);
254 template<typename _Res, typename _Allocator>
255 static typename _Ptr<_Result_alloc<_Res, _Allocator>>::type
256 _S_allocate_result(const _Allocator& __a)
258 typedef _Result_alloc<_Res, _Allocator> __result_type;
259 typename __result_type::__allocator_type __a2(__a);
260 __result_type* __p = __a2.allocate(1);
263 __a2.construct(__p, __a);
267 __a2.deallocate(__p, 1);
268 __throw_exception_again;
270 return typename _Ptr<__result_type>::type(__p);
274 /// Shared state between a promise and one or more associated futures.
277 typedef _Ptr<_Result_base>::type _Ptr_type;
281 condition_variable _M_cond;
282 atomic_flag _M_retrieved;
286 _State() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
288 _State(const _State&) = delete;
289 _State& operator=(const _State&) = delete;
295 unique_lock<mutex> __lock(_M_mutex);
297 _M_cond.wait(__lock, std::bind<bool>(&_State::_M_ready, this));
301 template<typename _Rep, typename _Period>
303 wait_for(const chrono::duration<_Rep, _Period>& __rel)
305 unique_lock<mutex> __lock(_M_mutex);
306 auto __bound = std::bind<bool>(&_State::_M_ready, this);
307 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
310 template<typename _Clock, typename _Duration>
312 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
314 unique_lock<mutex> __lock(_M_mutex);
315 auto __bound = std::bind<bool>(&_State::_M_ready, this);
316 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
320 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
322 bool __set = __ignore_failure;
323 // all calls to this function are serialized,
324 // side-effects of invoking __res only happen once
325 call_once(_M_once, &_State::_M_do_set, this, ref(__res),
328 __throw_future_error(int(future_errc::promise_already_satisfied));
332 _M_break_promise(_Ptr_type __res)
334 if (static_cast<bool>(__res))
336 error_code __ec(make_error_code(future_errc::broken_promise));
337 __res->_M_error = copy_exception(future_error(__ec));
339 lock_guard<mutex> __lock(_M_mutex);
340 _M_result.swap(__res);
342 _M_cond.notify_all();
346 // Called when this object is passed to a future.
348 _M_set_retrieved_flag()
350 if (_M_retrieved.test_and_set())
351 __throw_future_error(int(future_errc::future_already_retrieved));
354 template<typename _Res, typename _Arg>
358 template<typename _Res, typename _Arg>
359 struct _Setter<_Res, _Arg&>
361 // check this is only used by promise<R>::set_value(const R&)
362 // or promise<R>::set_value(R&)
363 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
364 || is_same<const _Res, _Arg>::value, // promise<R>
365 "Invalid specialisation");
367 typename promise<_Res>::_Ptr_type operator()()
369 _State::_S_check(_M_promise->_M_future);
370 _M_promise->_M_storage->_M_set(_M_arg);
371 return std::move(_M_promise->_M_storage);
373 promise<_Res>* _M_promise;
378 template<typename _Res>
379 struct _Setter<_Res, _Res&&>
381 typename promise<_Res>::_Ptr_type operator()()
383 _State::_S_check(_M_promise->_M_future);
384 _M_promise->_M_storage->_M_set(std::move(_M_arg));
385 return std::move(_M_promise->_M_storage);
387 promise<_Res>* _M_promise;
391 struct __exception_ptr_tag { };
394 template<typename _Res>
395 struct _Setter<_Res, __exception_ptr_tag>
397 typename promise<_Res>::_Ptr_type operator()()
399 _State::_S_check(_M_promise->_M_future);
400 _M_promise->_M_storage->_M_error = _M_ex;
401 return std::move(_M_promise->_M_storage);
404 promise<_Res>* _M_promise;
405 exception_ptr& _M_ex;
408 template<typename _Res, typename _Arg>
409 static _Setter<_Res, _Arg&&>
410 __setter(promise<_Res>* __prom, _Arg&& __arg)
412 return _Setter<_Res, _Arg&&>{ __prom, __arg };
415 template<typename _Res>
416 static _Setter<_Res, __exception_ptr_tag>
417 __setter(exception_ptr& __ex, promise<_Res>* __prom)
419 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
422 static _Setter<void, void>
423 __setter(promise<void>* __prom);
425 template<typename _Tp>
427 _S_check(const shared_ptr<_Tp>& __p)
429 if (!static_cast<bool>(__p))
430 __throw_future_error((int)future_errc::no_state);
435 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
437 _Ptr_type __res = __f();
439 lock_guard<mutex> __lock(_M_mutex);
440 _M_result.swap(__res);
442 _M_cond.notify_all();
446 bool _M_ready() const { return static_cast<bool>(_M_result); }
448 virtual void _M_run_deferred() { }
451 template<typename _Res>
452 class _Deferred_state;
454 template<typename _Res>
457 template<typename _Signature>
460 template<typename _StateT, typename _Res = typename _StateT::_Res_type>
464 inline __future_base::_Result_base::~_Result_base() = default;
466 /// Partial specialization for reference types.
467 template<typename _Res>
468 struct __future_base::_Result<_Res&> : __future_base::_Result_base
470 _Result() : _M_value_ptr() { }
472 void _M_set(_Res& __res) { _M_value_ptr = &__res; }
474 _Res& _M_get() { return *_M_value_ptr; }
479 void _M_destroy() { delete this; }
482 /// Explicit specialization for void.
484 struct __future_base::_Result<void> : __future_base::_Result_base
487 void _M_destroy() { delete this; }
491 /// Common implementation for future and shared_future.
492 template<typename _Res>
493 class __basic_future : public __future_base
496 typedef shared_ptr<_State> __state_type;
497 typedef __future_base::_Result<_Res>& __result_type;
500 __state_type _M_state;
504 __basic_future(const __basic_future&) = delete;
505 __basic_future& operator=(const __basic_future&) = delete;
508 valid() const { return static_cast<bool>(_M_state); }
513 _State::_S_check(_M_state);
517 template<typename _Rep, typename _Period>
519 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
521 _State::_S_check(_M_state);
522 return _M_state->wait_for(__rel);
525 template<typename _Clock, typename _Duration>
527 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
529 _State::_S_check(_M_state);
530 return _M_state->wait_until(__abs);
534 /// Wait for the state to be ready and rethrow any stored exception
538 _State::_S_check(_M_state);
539 _Result_base& __res = _M_state->wait();
540 if (!(__res._M_error == 0))
541 rethrow_exception(__res._M_error);
542 return static_cast<__result_type>(__res);
545 void _M_swap(__basic_future& __that)
547 _M_state.swap(__that._M_state);
550 // Construction of a future by promise::get_future()
552 __basic_future(const __state_type& __state) : _M_state(__state)
554 _State::_S_check(_M_state);
555 _M_state->_M_set_retrieved_flag();
558 // Copy construction from a shared_future
560 __basic_future(const shared_future<_Res>&);
562 // Move construction from a shared_future
564 __basic_future(shared_future<_Res>&&);
566 // Move construction from a future
568 __basic_future(future<_Res>&&);
570 constexpr __basic_future() : _M_state() { }
574 explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { }
575 ~_Reset() { _M_fut._M_state.reset(); }
576 __basic_future& _M_fut;
581 /// Primary template for future.
582 template<typename _Res>
583 class future : public __basic_future<_Res>
585 friend class promise<_Res>;
586 template<typename> friend class packaged_task;
587 template<typename _Fn, typename... _Args>
588 friend future<typename result_of<_Fn(_Args...)>::type>
589 async(launch, _Fn&&, _Args&&...);
591 typedef __basic_future<_Res> _Base_type;
592 typedef typename _Base_type::__state_type __state_type;
595 future(const __state_type& __state) : _Base_type(__state) { }
598 constexpr future() : _Base_type() { }
601 future(future&& __uf) : _Base_type(std::move(__uf)) { }
604 future(const future&) = delete;
605 future& operator=(const future&) = delete;
607 future& operator=(future&& __fut)
609 future(std::move(__fut))._M_swap(*this);
613 /// Retrieving the value
617 typename _Base_type::_Reset __reset(*this);
618 return std::move(this->_M_get_result()._M_value());
622 /// Partial specialization for future<R&>
623 template<typename _Res>
624 class future<_Res&> : public __basic_future<_Res&>
626 friend class promise<_Res&>;
627 template<typename> friend class packaged_task;
628 template<typename _Fn, typename... _Args>
629 friend future<typename result_of<_Fn(_Args...)>::type>
630 async(launch, _Fn&&, _Args&&...);
632 typedef __basic_future<_Res&> _Base_type;
633 typedef typename _Base_type::__state_type __state_type;
636 future(const __state_type& __state) : _Base_type(__state) { }
639 constexpr future() : _Base_type() { }
642 future(future&& __uf) : _Base_type(std::move(__uf)) { }
645 future(const future&) = delete;
646 future& operator=(const future&) = delete;
648 future& operator=(future&& __fut)
650 future(std::move(__fut))._M_swap(*this);
654 /// Retrieving the value
658 typename _Base_type::_Reset __reset(*this);
659 return this->_M_get_result()._M_get();
663 /// Explicit specialization for future<void>
665 class future<void> : public __basic_future<void>
667 friend class promise<void>;
668 template<typename> friend class packaged_task;
669 template<typename _Fn, typename... _Args>
670 friend future<typename result_of<_Fn(_Args...)>::type>
671 async(launch, _Fn&&, _Args&&...);
673 typedef __basic_future<void> _Base_type;
674 typedef typename _Base_type::__state_type __state_type;
677 future(const __state_type& __state) : _Base_type(__state) { }
680 constexpr future() : _Base_type() { }
683 future(future&& __uf) : _Base_type(std::move(__uf)) { }
686 future(const future&) = delete;
687 future& operator=(const future&) = delete;
689 future& operator=(future&& __fut)
691 future(std::move(__fut))._M_swap(*this);
695 /// Retrieving the value
699 typename _Base_type::_Reset __reset(*this);
700 this->_M_get_result();
705 /// Primary template for shared_future.
706 template<typename _Res>
707 class shared_future : public __basic_future<_Res>
709 typedef __basic_future<_Res> _Base_type;
712 constexpr shared_future() : _Base_type() { }
715 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
717 /// Construct from a future rvalue
718 shared_future(future<_Res>&& __uf)
719 : _Base_type(std::move(__uf))
722 /// Construct from a shared_future rvalue
723 shared_future(shared_future&& __sf)
724 : _Base_type(std::move(__sf))
727 shared_future& operator=(const shared_future& __sf)
729 shared_future(__sf)._M_swap(*this);
733 shared_future& operator=(shared_future&& __sf)
735 shared_future(std::move(__sf))._M_swap(*this);
739 /// Retrieving the value
743 typename _Base_type::__result_type __r = this->_M_get_result();
744 _Res& __rs(__r._M_value());
749 /// Partial specialization for shared_future<R&>
750 template<typename _Res>
751 class shared_future<_Res&> : public __basic_future<_Res&>
753 typedef __basic_future<_Res&> _Base_type;
756 constexpr shared_future() : _Base_type() { }
759 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
761 /// Construct from a future rvalue
762 shared_future(future<_Res&>&& __uf)
763 : _Base_type(std::move(__uf))
766 /// Construct from a shared_future rvalue
767 shared_future(shared_future&& __sf)
768 : _Base_type(std::move(__sf))
771 shared_future& operator=(const shared_future& __sf)
773 shared_future(__sf)._M_swap(*this);
777 shared_future& operator=(shared_future&& __sf)
779 shared_future(std::move(__sf))._M_swap(*this);
783 /// Retrieving the value
785 get() { return this->_M_get_result()._M_get(); }
788 /// Explicit specialization for shared_future<void>
790 class shared_future<void> : public __basic_future<void>
792 typedef __basic_future<void> _Base_type;
795 constexpr shared_future() : _Base_type() { }
798 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
800 /// Construct from a future rvalue
801 shared_future(future<void>&& __uf)
802 : _Base_type(std::move(__uf))
805 /// Construct from a shared_future rvalue
806 shared_future(shared_future&& __sf)
807 : _Base_type(std::move(__sf))
810 shared_future& operator=(const shared_future& __sf)
812 shared_future(__sf)._M_swap(*this);
816 shared_future& operator=(shared_future&& __sf)
818 shared_future(std::move(__sf))._M_swap(*this);
822 // Retrieving the value
824 get() { this->_M_get_result(); }
827 // Now we can define the protected __basic_future constructors.
828 template<typename _Res>
829 inline __basic_future<_Res>::
830 __basic_future(const shared_future<_Res>& __sf)
831 : _M_state(__sf._M_state)
834 template<typename _Res>
835 inline __basic_future<_Res>::
836 __basic_future(shared_future<_Res>&& __sf)
837 : _M_state(std::move(__sf._M_state))
840 template<typename _Res>
841 inline __basic_future<_Res>::
842 __basic_future(future<_Res>&& __uf)
843 : _M_state(std::move(__uf._M_state))
847 /// Primary template for promise
848 template<typename _Res>
851 typedef __future_base::_State _State;
852 typedef __future_base::_Result<_Res> _Res_type;
853 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
854 template<typename, typename> friend class _State::_Setter;
856 shared_ptr<_State> _M_future;
857 _Ptr_type _M_storage;
861 : _M_future(std::make_shared<_State>()),
862 _M_storage(new _Res_type())
865 promise(promise&& __rhs)
866 : _M_future(std::move(__rhs._M_future)),
867 _M_storage(std::move(__rhs._M_storage))
870 template<typename _Allocator>
871 promise(allocator_arg_t, const _Allocator& __a)
872 : _M_future(std::allocate_shared<_State>(__a)),
873 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
876 promise(const promise&) = delete;
880 if (static_cast<bool>(_M_future) && !_M_future.unique())
881 _M_future->_M_break_promise(std::move(_M_storage));
886 operator=(promise&& __rhs)
888 promise(std::move(__rhs)).swap(*this);
892 promise& operator=(const promise&) = delete;
897 _M_future.swap(__rhs._M_future);
898 _M_storage.swap(__rhs._M_storage);
901 // Retrieving the result
904 { return future<_Res>(_M_future); }
906 // Setting the result
908 set_value(const _Res& __r)
910 auto __setter = _State::__setter(this, __r);
911 _M_future->_M_set_result(std::move(__setter));
915 set_value(_Res&& __r)
917 auto __setter = _State::__setter(this, std::move(__r));
918 _M_future->_M_set_result(std::move(__setter));
922 set_exception(exception_ptr __p)
924 auto __setter = _State::__setter(__p, this);
925 _M_future->_M_set_result(std::move(__setter));
929 template<typename _Res>
931 swap(promise<_Res>& __x, promise<_Res>& __y)
934 template<typename _Res, typename _Alloc>
935 struct uses_allocator<promise<_Res>, _Alloc>
936 : public true_type { };
939 /// Partial specialization for promise<R&>
940 template<typename _Res>
943 typedef __future_base::_State _State;
944 typedef __future_base::_Result<_Res&> _Res_type;
945 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
946 template<typename, typename> friend class _State::_Setter;
948 shared_ptr<_State> _M_future;
949 _Ptr_type _M_storage;
953 : _M_future(std::make_shared<_State>()),
954 _M_storage(new _Res_type())
957 promise(promise&& __rhs)
958 : _M_future(std::move(__rhs._M_future)),
959 _M_storage(std::move(__rhs._M_storage))
962 template<typename _Allocator>
963 promise(allocator_arg_t, const _Allocator& __a)
964 : _M_future(std::allocate_shared<_State>(__a)),
965 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
968 promise(const promise&) = delete;
972 if (static_cast<bool>(_M_future) && !_M_future.unique())
973 _M_future->_M_break_promise(std::move(_M_storage));
978 operator=(promise&& __rhs)
980 promise(std::move(__rhs)).swap(*this);
984 promise& operator=(const promise&) = delete;
989 _M_future.swap(__rhs._M_future);
990 _M_storage.swap(__rhs._M_storage);
993 // Retrieving the result
996 { return future<_Res&>(_M_future); }
998 // Setting the result
1000 set_value(_Res& __r)
1002 auto __setter = _State::__setter(this, __r);
1003 _M_future->_M_set_result(std::move(__setter));
1007 set_exception(exception_ptr __p)
1009 auto __setter = _State::__setter(__p, this);
1010 _M_future->_M_set_result(std::move(__setter));
1014 /// Explicit specialization for promise<void>
1018 typedef __future_base::_State _State;
1019 typedef __future_base::_Result<void> _Res_type;
1020 typedef typename __future_base::_Ptr<_Res_type>::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)
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<void>(__a))
1043 promise(const promise&) = delete;
1047 if (static_cast<bool>(_M_future) && !_M_future.unique())
1048 _M_future->_M_break_promise(std::move(_M_storage));
1053 operator=(promise&& __rhs)
1055 promise(std::move(__rhs)).swap(*this);
1059 promise& operator=(const promise&) = delete;
1062 swap(promise& __rhs)
1064 _M_future.swap(__rhs._M_future);
1065 _M_storage.swap(__rhs._M_storage);
1068 // Retrieving the result
1071 { return future<void>(_M_future); }
1073 // Setting the result
1077 set_exception(exception_ptr __p)
1079 auto __setter = _State::__setter(__p, this);
1080 _M_future->_M_set_result(std::move(__setter));
1086 struct __future_base::_State::_Setter<void, void>
1088 promise<void>::_Ptr_type operator()()
1090 _State::_S_check(_M_promise->_M_future);
1091 return std::move(_M_promise->_M_storage);
1094 promise<void>* _M_promise;
1097 inline __future_base::_State::_Setter<void, void>
1098 __future_base::_State::__setter(promise<void>* __prom)
1100 return _Setter<void, void>{ __prom };
1104 promise<void>::set_value()
1106 auto __setter = _State::__setter(this);
1107 _M_future->_M_set_result(std::move(__setter));
1111 template<typename _StateT, typename _Res>
1112 struct __future_base::_Task_setter
1114 typename _StateT::_Ptr_type operator()()
1118 _M_state->_M_result->_M_set(_M_fn());
1122 _M_state->_M_result->_M_error = current_exception();
1124 return std::move(_M_state->_M_result);
1127 std::function<_Res()> _M_fn;
1130 template<typename _StateT>
1131 struct __future_base::_Task_setter<_StateT, void>
1133 typename _StateT::_Ptr_type operator()()
1141 _M_state->_M_result->_M_error = current_exception();
1143 return std::move(_M_state->_M_result);
1146 std::function<void()> _M_fn;
1149 template<typename _Res, typename... _Args>
1150 struct __future_base::_Task_state<_Res(_Args...)> : __future_base::_State
1152 typedef _Res _Res_type;
1154 _Task_state(std::function<_Res(_Args...)> __task)
1155 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1158 template<typename _Func, typename _Alloc>
1159 _Task_state(_Func&& __task, const _Alloc& __a)
1160 : _M_result(_S_allocate_result<_Res>(__a)),
1161 _M_task(allocator_arg, __a, std::move(__task))
1165 _M_run(_Args... __args)
1167 // bound arguments decay so wrap lvalue references
1168 auto __bound = std::bind<_Res>(std::ref(_M_task),
1169 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1170 _Task_setter<_Task_state> __setter{ this, std::move(__bound) };
1171 _M_set_result(std::move(__setter));
1174 template<typename, typename> friend class _Task_setter;
1175 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1176 _Ptr_type _M_result;
1177 std::function<_Res(_Args...)> _M_task;
1179 template<typename _Tp>
1180 static reference_wrapper<_Tp>
1181 _S_maybe_wrap_ref(_Tp& __t)
1182 { return std::ref(__t); }
1184 template<typename _Tp>
1185 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1187 _S_maybe_wrap_ref(_Tp&& __t)
1188 { return std::forward<_Tp>(__t); }
1192 template<typename _Res, typename... _ArgTypes>
1193 class packaged_task<_Res(_ArgTypes...)>
1195 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1196 shared_ptr<_State_type> _M_state;
1199 typedef _Res result_type;
1201 // Construction and destruction
1204 template<typename _Fn>
1206 packaged_task(const _Fn& __fn)
1207 : _M_state(std::make_shared<_State_type>(__fn))
1210 template<typename _Fn>
1212 packaged_task(_Fn&& __fn)
1213 : _M_state(std::make_shared<_State_type>(std::move(__fn)))
1217 packaged_task(_Res(*__fn)(_ArgTypes...))
1218 : _M_state(std::make_shared<_State_type>(__fn))
1221 template<typename _Fn, typename _Allocator>
1223 packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn __fn)
1224 : _M_state(std::allocate_shared<_State_type>(__a, std::move(__fn)))
1229 if (static_cast<bool>(_M_state) && !_M_state.unique())
1230 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1234 packaged_task(packaged_task&) = delete;
1235 packaged_task& operator=(packaged_task&) = delete;
1238 packaged_task(packaged_task&& __other)
1239 { this->swap(__other); }
1241 packaged_task& operator=(packaged_task&& __other)
1243 packaged_task(std::move(__other)).swap(*this);
1248 swap(packaged_task& __other)
1249 { _M_state.swap(__other._M_state); }
1251 explicit operator bool() const { return static_cast<bool>(_M_state); }
1256 { return future<_Res>(_M_state); }
1260 operator()(_ArgTypes... __args)
1262 __future_base::_State::_S_check(_M_state);
1263 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1269 __future_base::_State::_S_check(_M_state);
1270 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1275 template<typename _Res, typename... _ArgTypes>
1277 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1278 packaged_task<_Res(_ArgTypes...)>& __y)
1281 template<typename _Res, typename _Alloc>
1282 struct uses_allocator<packaged_task<_Res>, _Alloc>
1283 : public true_type { };
1286 template<typename _Res>
1287 class __future_base::_Deferred_state : public __future_base::_State
1290 typedef _Res _Res_type;
1293 _Deferred_state(std::function<_Res()>&& __fn)
1294 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1298 template<typename, typename> friend class _Task_setter;
1299 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1300 _Ptr_type _M_result;
1301 std::function<_Res()> _M_fn;
1306 _Task_setter<_Deferred_state> __setter{ this, _M_fn };
1307 // safe to call multiple times so ignore failure
1308 _M_set_result(std::move(__setter), true);
1312 template<typename _Res>
1313 class __future_base::_Async_state : public __future_base::_State
1316 typedef _Res _Res_type;
1319 _Async_state(std::function<_Res()>&& __fn)
1320 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1321 _M_thread(mem_fn(&_Async_state::_M_do_run), this)
1324 ~_Async_state() { _M_thread.join(); }
1329 _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) };
1330 _M_set_result(std::move(__setter));
1333 template<typename, typename> friend class _Task_setter;
1334 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1335 _Ptr_type _M_result;
1336 std::function<_Res()> _M_fn;
1341 template<typename _Fn, typename... _Args>
1342 future<typename result_of<_Fn(_Args...)>::type>
1343 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1345 typedef typename result_of<_Fn(_Args...)>::type result_type;
1346 std::shared_ptr<__future_base::_State> __state;
1347 if (__policy == launch::async)
1349 typedef typename __future_base::_Async_state<result_type> _State;
1350 __state = std::make_shared<_State>(std::bind<result_type>(
1351 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1355 typedef typename __future_base::_Deferred_state<result_type> _State;
1356 __state = std::make_shared<_State>(std::bind<result_type>(
1357 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1359 return future<result_type>(__state);
1362 /// async, potential overload
1363 template<typename _Fn, typename... _Args>
1365 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
1366 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
1368 async(_Fn&& __fn, _Args&&... __args)
1370 return async(launch::any, std::forward<_Fn>(__fn),
1371 std::forward<_Args>(__args)...);
1374 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1375 // && _GLIBCXX_ATOMIC_BUILTINS_4
1378 _GLIBCXX_END_NAMESPACE
1380 #endif // __GXX_EXPERIMENTAL_CXX0X__
1382 #endif // _GLIBCXX_FUTURE