libstdc++
future
Go to the documentation of this file.
00001 // <future> -*- C++ -*-
00002 
00003 // Copyright (C) 2009-2013 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/future
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 #ifndef _GLIBCXX_FUTURE
00030 #define _GLIBCXX_FUTURE 1
00031 
00032 #pragma GCC system_header
00033 
00034 #if __cplusplus < 201103L
00035 # include <bits/c++0x_warning.h>
00036 #else
00037 
00038 #include <functional>
00039 #include <mutex>
00040 #include <thread>
00041 #include <condition_variable>
00042 #include <system_error>
00043 #include <atomic>
00044 #include <bits/functexcept.h>
00045 #include <bits/unique_ptr.h>
00046 #include <bits/shared_ptr.h>
00047 #include <bits/uses_allocator.h>
00048 #include <bits/alloc_traits.h>
00049 
00050 namespace std _GLIBCXX_VISIBILITY(default)
00051 {
00052 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00053 
00054   /**
00055    * @defgroup futures Futures
00056    * @ingroup concurrency
00057    *
00058    * Classes for futures support.
00059    * @{
00060    */
00061 
00062   /// Error code for futures
00063   enum class future_errc
00064   {
00065     future_already_retrieved = 1,
00066     promise_already_satisfied,
00067     no_state,
00068     broken_promise
00069   };
00070 
00071   /// Specialization.
00072   template<>
00073     struct is_error_code_enum<future_errc> : public true_type { };
00074 
00075   /// Points to a statically-allocated object derived from error_category.
00076   const error_category&
00077   future_category() noexcept;
00078 
00079   /// Overload for make_error_code.
00080   inline error_code
00081   make_error_code(future_errc __errc) noexcept
00082   { return error_code(static_cast<int>(__errc), future_category()); }
00083 
00084   /// Overload for make_error_condition.
00085   inline error_condition
00086   make_error_condition(future_errc __errc) noexcept
00087   { return error_condition(static_cast<int>(__errc), future_category()); }
00088 
00089   /**
00090    *  @brief Exception type thrown by futures.
00091    *  @ingroup exceptions
00092    */
00093   class future_error : public logic_error
00094   {
00095     error_code          _M_code;
00096 
00097   public:
00098     explicit future_error(error_code __ec)
00099     : logic_error("std::future_error"), _M_code(__ec)
00100     { }
00101 
00102     virtual ~future_error() noexcept;
00103 
00104     virtual const char*
00105     what() const noexcept;
00106 
00107     const error_code&
00108     code() const noexcept { return _M_code; }
00109   };
00110 
00111   // Forward declarations.
00112   template<typename _Res>
00113     class future;
00114 
00115   template<typename _Res>
00116     class shared_future;
00117 
00118   template<typename _Signature>
00119     class packaged_task;
00120 
00121   template<typename _Res>
00122     class promise;
00123 
00124   /// Launch code for futures
00125   enum class launch
00126   {
00127     async = 1,
00128     deferred = 2
00129   };
00130 
00131   constexpr launch operator&(launch __x, launch __y)
00132   {
00133     return static_cast<launch>(
00134     static_cast<int>(__x) & static_cast<int>(__y));
00135   }
00136 
00137   constexpr launch operator|(launch __x, launch __y)
00138   {
00139     return static_cast<launch>(
00140     static_cast<int>(__x) | static_cast<int>(__y));
00141   }
00142 
00143   constexpr launch operator^(launch __x, launch __y)
00144   {
00145     return static_cast<launch>(
00146     static_cast<int>(__x) ^ static_cast<int>(__y));
00147   }
00148 
00149   constexpr launch operator~(launch __x)
00150   { return static_cast<launch>(~static_cast<int>(__x)); }
00151 
00152   inline launch& operator&=(launch& __x, launch __y)
00153   { return __x = __x & __y; }
00154 
00155   inline launch& operator|=(launch& __x, launch __y)
00156   { return __x = __x | __y; }
00157 
00158   inline launch& operator^=(launch& __x, launch __y)
00159   { return __x = __x ^ __y; }
00160 
00161   /// Status code for futures
00162   enum class future_status
00163   {
00164     ready,
00165     timeout,
00166     deferred
00167   };
00168 
00169   template<typename _Fn, typename... _Args>
00170     future<typename result_of<_Fn(_Args...)>::type>
00171     async(launch __policy, _Fn&& __fn, _Args&&... __args);
00172 
00173   template<typename _Fn, typename... _Args>
00174     future<typename result_of<_Fn(_Args...)>::type>
00175     async(_Fn&& __fn, _Args&&... __args);
00176 
00177 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
00178   && (ATOMIC_INT_LOCK_FREE > 1)
00179 
00180   /// Base class and enclosing scope.
00181   struct __future_base
00182   {
00183     /// Base class for results.
00184     struct _Result_base
00185     {
00186       exception_ptr     _M_error;
00187 
00188       _Result_base(const _Result_base&) = delete;
00189       _Result_base& operator=(const _Result_base&) = delete;
00190 
00191       // _M_destroy() allows derived classes to control deallocation
00192       virtual void _M_destroy() = 0;
00193 
00194       struct _Deleter
00195       {
00196     void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
00197       };
00198 
00199     protected:
00200       _Result_base();
00201       virtual ~_Result_base();
00202     };
00203 
00204     /// Result.
00205     template<typename _Res>
00206       struct _Result : _Result_base
00207       {
00208       private:
00209     typedef alignment_of<_Res>              __a_of;
00210     typedef aligned_storage<sizeof(_Res), __a_of::value>    __align_storage;
00211     typedef typename __align_storage::type          __align_type;
00212 
00213     __align_type        _M_storage;
00214     bool            _M_initialized;
00215 
00216       public:
00217     typedef _Res result_type;
00218 
00219     _Result() noexcept : _M_initialized() { }
00220     
00221     ~_Result()
00222     {
00223       if (_M_initialized)
00224         _M_value().~_Res();
00225     }
00226 
00227     // Return lvalue, future will add const or rvalue-reference
00228     _Res&
00229     _M_value() noexcept { return *static_cast<_Res*>(_M_addr()); }
00230 
00231     void
00232     _M_set(const _Res& __res)
00233     {
00234       ::new (_M_addr()) _Res(__res);
00235       _M_initialized = true;
00236     }
00237 
00238     void
00239     _M_set(_Res&& __res)
00240     {
00241       ::new (_M_addr()) _Res(std::move(__res));
00242       _M_initialized = true;
00243     }
00244 
00245       private:
00246     void _M_destroy() { delete this; }
00247 
00248     void* _M_addr() noexcept { return static_cast<void*>(&_M_storage); }
00249     };
00250 
00251     /// A unique_ptr based on the instantiating type.
00252     template<typename _Res>
00253       using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
00254 
00255     /// Result_alloc.
00256     template<typename _Res, typename _Alloc>
00257       struct _Result_alloc final : _Result<_Res>, _Alloc
00258       {
00259         typedef typename allocator_traits<_Alloc>::template
00260           rebind_alloc<_Result_alloc> __allocator_type;
00261 
00262         explicit
00263     _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
00264         { }
00265     
00266       private:
00267     void _M_destroy()
00268         {
00269       typedef allocator_traits<__allocator_type> __traits;
00270           __allocator_type __a(*this);
00271       __traits::destroy(__a, this);
00272       __traits::deallocate(__a, this, 1);
00273         }
00274       };
00275 
00276     template<typename _Res, typename _Allocator>
00277       static _Ptr<_Result_alloc<_Res, _Allocator>>
00278       _S_allocate_result(const _Allocator& __a)
00279       {
00280         typedef _Result_alloc<_Res, _Allocator> __result_type;
00281     typedef allocator_traits<typename __result_type::__allocator_type>
00282       __traits;
00283         typename __traits::allocator_type __a2(__a);
00284         __result_type* __p = __traits::allocate(__a2, 1);
00285         __try
00286       {
00287         __traits::construct(__a2, __p, __a);
00288       }
00289         __catch(...)
00290       {
00291         __traits::deallocate(__a2, __p, 1);
00292         __throw_exception_again;
00293       }
00294         return _Ptr<__result_type>(__p);
00295       }
00296 
00297     template<typename _Res, typename _Tp>
00298       static _Ptr<_Result<_Res>>
00299       _S_allocate_result(const std::allocator<_Tp>& __a)
00300       {
00301     return _Ptr<_Result<_Res>>(new _Result<_Res>);
00302       }
00303 
00304     /// Base class for state between a promise and one or more
00305     /// associated futures.
00306     class _State_base
00307     {
00308       typedef _Ptr<_Result_base> _Ptr_type;
00309 
00310       _Ptr_type         _M_result;
00311       mutex                 _M_mutex;
00312       condition_variable    _M_cond;
00313       atomic_flag           _M_retrieved;
00314       once_flag         _M_once;
00315 
00316     public:
00317       _State_base() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
00318       _State_base(const _State_base&) = delete;
00319       _State_base& operator=(const _State_base&) = delete;
00320       virtual ~_State_base();
00321 
00322       _Result_base&
00323       wait()
00324       {
00325     _M_run_deferred();
00326     unique_lock<mutex> __lock(_M_mutex);
00327     _M_cond.wait(__lock, [&] { return _M_ready(); });
00328     return *_M_result;
00329       }
00330 
00331       template<typename _Rep, typename _Period>
00332         future_status
00333         wait_for(const chrono::duration<_Rep, _Period>& __rel)
00334         {
00335       unique_lock<mutex> __lock(_M_mutex);
00336       if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
00337         return future_status::ready;
00338       return future_status::timeout;
00339     }
00340 
00341       template<typename _Clock, typename _Duration>
00342         future_status
00343         wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
00344         {
00345       unique_lock<mutex> __lock(_M_mutex);
00346       if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
00347         return future_status::ready;
00348       return future_status::timeout;
00349     }
00350 
00351       void
00352       _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
00353       {
00354         bool __set = __ignore_failure;
00355         // all calls to this function are serialized,
00356         // side-effects of invoking __res only happen once
00357         call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
00358             ref(__set));
00359         if (!__set)
00360           __throw_future_error(int(future_errc::promise_already_satisfied));
00361       }
00362 
00363       void
00364       _M_break_promise(_Ptr_type __res)
00365       {
00366     if (static_cast<bool>(__res))
00367       {
00368         error_code __ec(make_error_code(future_errc::broken_promise));
00369         __res->_M_error = copy_exception(future_error(__ec));
00370         {
00371           lock_guard<mutex> __lock(_M_mutex);
00372           _M_result.swap(__res);
00373         }
00374         _M_cond.notify_all();
00375       }
00376       }
00377 
00378       // Called when this object is passed to a future.
00379       void
00380       _M_set_retrieved_flag()
00381       {
00382     if (_M_retrieved.test_and_set())
00383       __throw_future_error(int(future_errc::future_already_retrieved));
00384       }
00385 
00386       template<typename _Res, typename _Arg>
00387         struct _Setter;
00388 
00389       // set lvalues
00390       template<typename _Res, typename _Arg>
00391         struct _Setter<_Res, _Arg&>
00392         {
00393           // check this is only used by promise<R>::set_value(const R&)
00394           // or promise<R>::set_value(R&)
00395           static_assert(is_same<_Res, _Arg&>::value  // promise<R&>
00396               || is_same<const _Res, _Arg>::value,  // promise<R>
00397               "Invalid specialisation");
00398 
00399           typename promise<_Res>::_Ptr_type operator()()
00400           {
00401             _State_base::_S_check(_M_promise->_M_future);
00402             _M_promise->_M_storage->_M_set(_M_arg);
00403             return std::move(_M_promise->_M_storage);
00404           }
00405           promise<_Res>*    _M_promise;
00406           _Arg&             _M_arg;
00407         };
00408 
00409       // set rvalues
00410       template<typename _Res>
00411         struct _Setter<_Res, _Res&&>
00412         {
00413           typename promise<_Res>::_Ptr_type operator()()
00414           {
00415             _State_base::_S_check(_M_promise->_M_future);
00416             _M_promise->_M_storage->_M_set(std::move(_M_arg));
00417             return std::move(_M_promise->_M_storage);
00418           }
00419           promise<_Res>*    _M_promise;
00420           _Res&             _M_arg;
00421         };
00422 
00423       struct __exception_ptr_tag { };
00424 
00425       // set exceptions
00426       template<typename _Res>
00427         struct _Setter<_Res, __exception_ptr_tag>
00428         {
00429           typename promise<_Res>::_Ptr_type operator()()
00430           {
00431             _State_base::_S_check(_M_promise->_M_future);
00432             _M_promise->_M_storage->_M_error = _M_ex;
00433             return std::move(_M_promise->_M_storage);
00434           }
00435 
00436           promise<_Res>*   _M_promise;
00437           exception_ptr&    _M_ex;
00438         };
00439 
00440       template<typename _Res, typename _Arg>
00441         static _Setter<_Res, _Arg&&>
00442         __setter(promise<_Res>* __prom, _Arg&& __arg)
00443         {
00444           return _Setter<_Res, _Arg&&>{ __prom, __arg };
00445         }
00446 
00447       template<typename _Res>
00448         static _Setter<_Res, __exception_ptr_tag>
00449         __setter(exception_ptr& __ex, promise<_Res>* __prom)
00450         {
00451           return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
00452         }
00453 
00454       static _Setter<void, void>
00455       __setter(promise<void>* __prom);
00456 
00457       template<typename _Tp>
00458         static void
00459         _S_check(const shared_ptr<_Tp>& __p)
00460         {
00461           if (!static_cast<bool>(__p))
00462             __throw_future_error((int)future_errc::no_state);
00463         }
00464 
00465     private:
00466       void
00467       _M_do_set(function<_Ptr_type()>& __f, bool& __set)
00468       {
00469         _Ptr_type __res = __f();
00470         {
00471           lock_guard<mutex> __lock(_M_mutex);
00472           _M_result.swap(__res);
00473         }
00474         _M_cond.notify_all();
00475         __set = true;
00476       }
00477 
00478       bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
00479 
00480       // Misnamed: waits for completion of async function.
00481       virtual void _M_run_deferred() { }
00482     };
00483 
00484     template<typename _BoundFn, typename = typename _BoundFn::result_type>
00485       class _Deferred_state;
00486 
00487     class _Async_state_common;
00488 
00489     template<typename _BoundFn, typename = typename _BoundFn::result_type>
00490       class _Async_state_impl;
00491 
00492     template<typename _Signature>
00493       class _Task_state_base;
00494 
00495     template<typename _Fn, typename _Alloc, typename _Signature>
00496       class _Task_state;
00497 
00498     template<typename _BoundFn>
00499       static std::shared_ptr<_State_base>
00500       _S_make_deferred_state(_BoundFn&& __fn);
00501 
00502     template<typename _BoundFn>
00503       static std::shared_ptr<_State_base>
00504       _S_make_async_state(_BoundFn&& __fn);
00505 
00506     template<typename _Res_ptr,
00507          typename _Res = typename _Res_ptr::element_type::result_type>
00508       struct _Task_setter;
00509 
00510     template<typename _Res_ptr, typename _BoundFn>
00511       static _Task_setter<_Res_ptr>
00512       _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
00513       {
00514     return _Task_setter<_Res_ptr>{ __ptr, std::ref(__call) };
00515       }
00516   };
00517 
00518   /// Partial specialization for reference types.
00519   template<typename _Res>
00520     struct __future_base::_Result<_Res&> : __future_base::_Result_base
00521     {
00522       typedef _Res& result_type;
00523 
00524       _Result() noexcept : _M_value_ptr() { }
00525 
00526       void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
00527 
00528       _Res& _M_get() noexcept { return *_M_value_ptr; }
00529 
00530     private:
00531       _Res*             _M_value_ptr;
00532 
00533       void _M_destroy() { delete this; }
00534     };
00535 
00536   /// Explicit specialization for void.
00537   template<>
00538     struct __future_base::_Result<void> : __future_base::_Result_base
00539     {
00540       typedef void result_type;
00541 
00542     private:
00543       void _M_destroy() { delete this; }
00544     };
00545 
00546 
00547   /// Common implementation for future and shared_future.
00548   template<typename _Res>
00549     class __basic_future : public __future_base
00550     {
00551     protected:
00552       typedef shared_ptr<_State_base>       __state_type;
00553       typedef __future_base::_Result<_Res>& __result_type;
00554 
00555     private:
00556       __state_type      _M_state;
00557 
00558     public:
00559       // Disable copying.
00560       __basic_future(const __basic_future&) = delete;
00561       __basic_future& operator=(const __basic_future&) = delete;
00562 
00563       bool
00564       valid() const noexcept { return static_cast<bool>(_M_state); }
00565 
00566       void
00567       wait() const
00568       {
00569         _State_base::_S_check(_M_state);
00570         _M_state->wait();
00571       }
00572 
00573       template<typename _Rep, typename _Period>
00574         future_status
00575         wait_for(const chrono::duration<_Rep, _Period>& __rel) const
00576         {
00577           _State_base::_S_check(_M_state);
00578           return _M_state->wait_for(__rel);
00579         }
00580 
00581       template<typename _Clock, typename _Duration>
00582         future_status
00583         wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
00584         {
00585           _State_base::_S_check(_M_state);
00586           return _M_state->wait_until(__abs);
00587         }
00588 
00589     protected:
00590       /// Wait for the state to be ready and rethrow any stored exception
00591       __result_type
00592       _M_get_result() const
00593       {
00594         _State_base::_S_check(_M_state);
00595         _Result_base& __res = _M_state->wait();
00596         if (!(__res._M_error == 0))
00597           rethrow_exception(__res._M_error);
00598         return static_cast<__result_type>(__res);
00599       }
00600 
00601       void _M_swap(__basic_future& __that) noexcept
00602       {
00603         _M_state.swap(__that._M_state);
00604       }
00605 
00606       // Construction of a future by promise::get_future()
00607       explicit
00608       __basic_future(const __state_type& __state) : _M_state(__state)
00609       {
00610         _State_base::_S_check(_M_state);
00611         _M_state->_M_set_retrieved_flag();
00612       }
00613 
00614       // Copy construction from a shared_future
00615       explicit
00616       __basic_future(const shared_future<_Res>&) noexcept;
00617 
00618       // Move construction from a shared_future
00619       explicit
00620       __basic_future(shared_future<_Res>&&) noexcept;
00621 
00622       // Move construction from a future
00623       explicit
00624       __basic_future(future<_Res>&&) noexcept;
00625 
00626       constexpr __basic_future() noexcept : _M_state() { }
00627 
00628       struct _Reset
00629       {
00630         explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
00631         ~_Reset() { _M_fut._M_state.reset(); }
00632         __basic_future& _M_fut;
00633       };
00634     };
00635 
00636 
00637   /// Primary template for future.
00638   template<typename _Res>
00639     class future : public __basic_future<_Res>
00640     {
00641       friend class promise<_Res>;
00642       template<typename> friend class packaged_task;
00643       template<typename _Fn, typename... _Args>
00644         friend future<typename result_of<_Fn(_Args...)>::type>
00645         async(launch, _Fn&&, _Args&&...);
00646 
00647       typedef __basic_future<_Res> _Base_type;
00648       typedef typename _Base_type::__state_type __state_type;
00649 
00650       explicit
00651       future(const __state_type& __state) : _Base_type(__state) { }
00652 
00653     public:
00654       constexpr future() noexcept : _Base_type() { }
00655 
00656       /// Move constructor
00657       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
00658 
00659       // Disable copying
00660       future(const future&) = delete;
00661       future& operator=(const future&) = delete;
00662 
00663       future& operator=(future&& __fut) noexcept
00664       {
00665         future(std::move(__fut))._M_swap(*this);
00666         return *this;
00667       }
00668 
00669       /// Retrieving the value
00670       _Res
00671       get()
00672       {
00673         typename _Base_type::_Reset __reset(*this);
00674         return std::move(this->_M_get_result()._M_value());
00675       }
00676 
00677       shared_future<_Res> share();
00678     };
00679 
00680   /// Partial specialization for future<R&>
00681   template<typename _Res>
00682     class future<_Res&> : public __basic_future<_Res&>
00683     {
00684       friend class promise<_Res&>;
00685       template<typename> friend class packaged_task;
00686       template<typename _Fn, typename... _Args>
00687         friend future<typename result_of<_Fn(_Args...)>::type>
00688         async(launch, _Fn&&, _Args&&...);
00689 
00690       typedef __basic_future<_Res&> _Base_type;
00691       typedef typename _Base_type::__state_type __state_type;
00692 
00693       explicit
00694       future(const __state_type& __state) : _Base_type(__state) { }
00695 
00696     public:
00697       constexpr future() noexcept : _Base_type() { }
00698 
00699       /// Move constructor
00700       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
00701 
00702       // Disable copying
00703       future(const future&) = delete;
00704       future& operator=(const future&) = delete;
00705 
00706       future& operator=(future&& __fut) noexcept
00707       {
00708         future(std::move(__fut))._M_swap(*this);
00709         return *this;
00710       }
00711 
00712       /// Retrieving the value
00713       _Res&
00714       get()
00715       {
00716         typename _Base_type::_Reset __reset(*this);
00717         return this->_M_get_result()._M_get();
00718       }
00719 
00720       shared_future<_Res&> share();
00721     };
00722 
00723   /// Explicit specialization for future<void>
00724   template<>
00725     class future<void> : public __basic_future<void>
00726     {
00727       friend class promise<void>;
00728       template<typename> friend class packaged_task;
00729       template<typename _Fn, typename... _Args>
00730         friend future<typename result_of<_Fn(_Args...)>::type>
00731         async(launch, _Fn&&, _Args&&...);
00732 
00733       typedef __basic_future<void> _Base_type;
00734       typedef typename _Base_type::__state_type __state_type;
00735 
00736       explicit
00737       future(const __state_type& __state) : _Base_type(__state) { }
00738 
00739     public:
00740       constexpr future() noexcept : _Base_type() { }
00741 
00742       /// Move constructor
00743       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
00744 
00745       // Disable copying
00746       future(const future&) = delete;
00747       future& operator=(const future&) = delete;
00748 
00749       future& operator=(future&& __fut) noexcept
00750       {
00751         future(std::move(__fut))._M_swap(*this);
00752         return *this;
00753       }
00754 
00755       /// Retrieving the value
00756       void
00757       get()
00758       {
00759         typename _Base_type::_Reset __reset(*this);
00760         this->_M_get_result();
00761       }
00762 
00763       shared_future<void> share();
00764     };
00765 
00766 
00767   /// Primary template for shared_future.
00768   template<typename _Res>
00769     class shared_future : public __basic_future<_Res>
00770     {
00771       typedef __basic_future<_Res> _Base_type;
00772 
00773     public:
00774       constexpr shared_future() noexcept : _Base_type() { }
00775 
00776       /// Copy constructor
00777       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
00778 
00779       /// Construct from a future rvalue
00780       shared_future(future<_Res>&& __uf) noexcept
00781       : _Base_type(std::move(__uf))
00782       { }
00783 
00784       /// Construct from a shared_future rvalue
00785       shared_future(shared_future&& __sf) noexcept
00786       : _Base_type(std::move(__sf))
00787       { }
00788 
00789       shared_future& operator=(const shared_future& __sf)
00790       {
00791         shared_future(__sf)._M_swap(*this);
00792         return *this;
00793       }
00794 
00795       shared_future& operator=(shared_future&& __sf) noexcept
00796       {
00797         shared_future(std::move(__sf))._M_swap(*this);
00798         return *this;
00799       }
00800 
00801       /// Retrieving the value
00802       const _Res&
00803       get() const { return this->_M_get_result()._M_value(); }
00804     };
00805 
00806   /// Partial specialization for shared_future<R&>
00807   template<typename _Res>
00808     class shared_future<_Res&> : public __basic_future<_Res&>
00809     {
00810       typedef __basic_future<_Res&>           _Base_type;
00811 
00812     public:
00813       constexpr shared_future() noexcept : _Base_type() { }
00814 
00815       /// Copy constructor
00816       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
00817 
00818       /// Construct from a future rvalue
00819       shared_future(future<_Res&>&& __uf) noexcept
00820       : _Base_type(std::move(__uf))
00821       { }
00822 
00823       /// Construct from a shared_future rvalue
00824       shared_future(shared_future&& __sf) noexcept
00825       : _Base_type(std::move(__sf))
00826       { }
00827 
00828       shared_future& operator=(const shared_future& __sf)
00829       {
00830         shared_future(__sf)._M_swap(*this);
00831         return *this;
00832       }
00833 
00834       shared_future& operator=(shared_future&& __sf) noexcept
00835       {
00836         shared_future(std::move(__sf))._M_swap(*this);
00837         return *this;
00838       }
00839 
00840       /// Retrieving the value
00841       _Res&
00842       get() const { return this->_M_get_result()._M_get(); }
00843     };
00844 
00845   /// Explicit specialization for shared_future<void>
00846   template<>
00847     class shared_future<void> : public __basic_future<void>
00848     {
00849       typedef __basic_future<void> _Base_type;
00850 
00851     public:
00852       constexpr shared_future() noexcept : _Base_type() { }
00853 
00854       /// Copy constructor
00855       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
00856 
00857       /// Construct from a future rvalue
00858       shared_future(future<void>&& __uf) noexcept
00859       : _Base_type(std::move(__uf))
00860       { }
00861 
00862       /// Construct from a shared_future rvalue
00863       shared_future(shared_future&& __sf) noexcept
00864       : _Base_type(std::move(__sf))
00865       { }
00866 
00867       shared_future& operator=(const shared_future& __sf)
00868       {
00869         shared_future(__sf)._M_swap(*this);
00870         return *this;
00871       }
00872 
00873       shared_future& operator=(shared_future&& __sf) noexcept
00874       {
00875         shared_future(std::move(__sf))._M_swap(*this);
00876         return *this;
00877       }
00878 
00879       // Retrieving the value
00880       void
00881       get() const { this->_M_get_result(); }
00882     };
00883 
00884   // Now we can define the protected __basic_future constructors.
00885   template<typename _Res>
00886     inline __basic_future<_Res>::
00887     __basic_future(const shared_future<_Res>& __sf) noexcept
00888     : _M_state(__sf._M_state)
00889     { }
00890 
00891   template<typename _Res>
00892     inline __basic_future<_Res>::
00893     __basic_future(shared_future<_Res>&& __sf) noexcept
00894     : _M_state(std::move(__sf._M_state))
00895     { }
00896 
00897   template<typename _Res>
00898     inline __basic_future<_Res>::
00899     __basic_future(future<_Res>&& __uf) noexcept
00900     : _M_state(std::move(__uf._M_state))
00901     { }
00902 
00903   template<typename _Res>
00904     inline shared_future<_Res>
00905     future<_Res>::share()
00906     { return shared_future<_Res>(std::move(*this)); }
00907 
00908   template<typename _Res>
00909     inline shared_future<_Res&>
00910     future<_Res&>::share()
00911     { return shared_future<_Res&>(std::move(*this)); }
00912 
00913   inline shared_future<void>
00914   future<void>::share()
00915   { return shared_future<void>(std::move(*this)); }
00916 
00917   /// Primary template for promise
00918   template<typename _Res>
00919     class promise
00920     {
00921       typedef __future_base::_State_base    _State;
00922       typedef __future_base::_Result<_Res>  _Res_type;
00923       typedef __future_base::_Ptr<_Res_type>    _Ptr_type;
00924       template<typename, typename> friend class _State::_Setter;
00925 
00926       shared_ptr<_State>                        _M_future;
00927       _Ptr_type                                 _M_storage;
00928 
00929     public:
00930       promise()
00931       : _M_future(std::make_shared<_State>()),
00932     _M_storage(new _Res_type())
00933       { }
00934 
00935       promise(promise&& __rhs) noexcept
00936       : _M_future(std::move(__rhs._M_future)),
00937     _M_storage(std::move(__rhs._M_storage))
00938       { }
00939 
00940       template<typename _Allocator>
00941         promise(allocator_arg_t, const _Allocator& __a)
00942         : _M_future(std::allocate_shared<_State>(__a)),
00943       _M_storage(__future_base::_S_allocate_result<_Res>(__a))
00944         { }
00945 
00946       template<typename _Allocator>
00947         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
00948         : _M_future(std::move(__rhs._M_future)),
00949       _M_storage(std::move(__rhs._M_storage))
00950         { }
00951 
00952       promise(const promise&) = delete;
00953 
00954       ~promise()
00955       {
00956         if (static_cast<bool>(_M_future) && !_M_future.unique())
00957           _M_future->_M_break_promise(std::move(_M_storage));
00958       }
00959 
00960       // Assignment
00961       promise&
00962       operator=(promise&& __rhs) noexcept
00963       {
00964         promise(std::move(__rhs)).swap(*this);
00965         return *this;
00966       }
00967 
00968       promise& operator=(const promise&) = delete;
00969 
00970       void
00971       swap(promise& __rhs) noexcept
00972       {
00973         _M_future.swap(__rhs._M_future);
00974         _M_storage.swap(__rhs._M_storage);
00975       }
00976 
00977       // Retrieving the result
00978       future<_Res>
00979       get_future()
00980       { return future<_Res>(_M_future); }
00981 
00982       // Setting the result
00983       void
00984       set_value(const _Res& __r)
00985       {
00986         auto __setter = _State::__setter(this, __r);
00987         _M_future->_M_set_result(std::move(__setter));
00988       }
00989 
00990       void
00991       set_value(_Res&& __r)
00992       {
00993         auto __setter = _State::__setter(this, std::move(__r));
00994         _M_future->_M_set_result(std::move(__setter));
00995       }
00996 
00997       void
00998       set_exception(exception_ptr __p)
00999       {
01000         auto __setter = _State::__setter(__p, this);
01001         _M_future->_M_set_result(std::move(__setter));
01002       }
01003     };
01004 
01005   template<typename _Res>
01006     inline void
01007     swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
01008     { __x.swap(__y); }
01009 
01010   template<typename _Res, typename _Alloc>
01011     struct uses_allocator<promise<_Res>, _Alloc>
01012     : public true_type { };
01013 
01014 
01015   /// Partial specialization for promise<R&>
01016   template<typename _Res>
01017     class promise<_Res&>
01018     {
01019       typedef __future_base::_State_base    _State;
01020       typedef __future_base::_Result<_Res&> _Res_type;
01021       typedef __future_base::_Ptr<_Res_type>    _Ptr_type;
01022       template<typename, typename> friend class _State::_Setter;
01023 
01024       shared_ptr<_State>                        _M_future;
01025       _Ptr_type                                 _M_storage;
01026 
01027     public:
01028       promise()
01029       : _M_future(std::make_shared<_State>()),
01030     _M_storage(new _Res_type())
01031       { }
01032 
01033       promise(promise&& __rhs) noexcept
01034       : _M_future(std::move(__rhs._M_future)),
01035     _M_storage(std::move(__rhs._M_storage))
01036       { }
01037 
01038       template<typename _Allocator>
01039         promise(allocator_arg_t, const _Allocator& __a)
01040         : _M_future(std::allocate_shared<_State>(__a)),
01041       _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
01042         { }
01043 
01044       template<typename _Allocator>
01045         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
01046         : _M_future(std::move(__rhs._M_future)),
01047       _M_storage(std::move(__rhs._M_storage))
01048         { }
01049 
01050       promise(const promise&) = delete;
01051 
01052       ~promise()
01053       {
01054         if (static_cast<bool>(_M_future) && !_M_future.unique())
01055           _M_future->_M_break_promise(std::move(_M_storage));
01056       }
01057 
01058       // Assignment
01059       promise&
01060       operator=(promise&& __rhs) noexcept
01061       {
01062         promise(std::move(__rhs)).swap(*this);
01063         return *this;
01064       }
01065 
01066       promise& operator=(const promise&) = delete;
01067 
01068       void
01069       swap(promise& __rhs) noexcept
01070       {
01071         _M_future.swap(__rhs._M_future);
01072         _M_storage.swap(__rhs._M_storage);
01073       }
01074 
01075       // Retrieving the result
01076       future<_Res&>
01077       get_future()
01078       { return future<_Res&>(_M_future); }
01079 
01080       // Setting the result
01081       void
01082       set_value(_Res& __r)
01083       {
01084         auto __setter = _State::__setter(this, __r);
01085         _M_future->_M_set_result(std::move(__setter));
01086       }
01087 
01088       void
01089       set_exception(exception_ptr __p)
01090       {
01091         auto __setter = _State::__setter(__p, this);
01092         _M_future->_M_set_result(std::move(__setter));
01093       }
01094     };
01095 
01096   /// Explicit specialization for promise<void>
01097   template<>
01098     class promise<void>
01099     {
01100       typedef __future_base::_State_base    _State;
01101       typedef __future_base::_Result<void>  _Res_type;
01102       typedef __future_base::_Ptr<_Res_type>    _Ptr_type;
01103       template<typename, typename> friend class _State::_Setter;
01104 
01105       shared_ptr<_State>                        _M_future;
01106       _Ptr_type                                 _M_storage;
01107 
01108     public:
01109       promise()
01110       : _M_future(std::make_shared<_State>()),
01111     _M_storage(new _Res_type())
01112       { }
01113 
01114       promise(promise&& __rhs) noexcept
01115       : _M_future(std::move(__rhs._M_future)),
01116     _M_storage(std::move(__rhs._M_storage))
01117       { }
01118 
01119       template<typename _Allocator>
01120         promise(allocator_arg_t, const _Allocator& __a)
01121         : _M_future(std::allocate_shared<_State>(__a)),
01122       _M_storage(__future_base::_S_allocate_result<void>(__a))
01123         { }
01124 
01125       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01126       // 2095.  missing constructors needed for uses-allocator construction
01127       template<typename _Allocator>
01128         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
01129         : _M_future(std::move(__rhs._M_future)),
01130       _M_storage(std::move(__rhs._M_storage))
01131         { }
01132 
01133       promise(const promise&) = delete;
01134 
01135       ~promise()
01136       {
01137         if (static_cast<bool>(_M_future) && !_M_future.unique())
01138           _M_future->_M_break_promise(std::move(_M_storage));
01139       }
01140 
01141       // Assignment
01142       promise&
01143       operator=(promise&& __rhs) noexcept
01144       {
01145         promise(std::move(__rhs)).swap(*this);
01146         return *this;
01147       }
01148 
01149       promise& operator=(const promise&) = delete;
01150 
01151       void
01152       swap(promise& __rhs) noexcept
01153       {
01154         _M_future.swap(__rhs._M_future);
01155         _M_storage.swap(__rhs._M_storage);
01156       }
01157 
01158       // Retrieving the result
01159       future<void>
01160       get_future()
01161       { return future<void>(_M_future); }
01162 
01163       // Setting the result
01164       void set_value();
01165 
01166       void
01167       set_exception(exception_ptr __p)
01168       {
01169         auto __setter = _State::__setter(__p, this);
01170         _M_future->_M_set_result(std::move(__setter));
01171       }
01172     };
01173 
01174   // set void
01175   template<>
01176     struct __future_base::_State_base::_Setter<void, void>
01177     {
01178       promise<void>::_Ptr_type operator()()
01179       {
01180         _State_base::_S_check(_M_promise->_M_future);
01181         return std::move(_M_promise->_M_storage);
01182       }
01183 
01184       promise<void>*    _M_promise;
01185     };
01186 
01187   inline __future_base::_State_base::_Setter<void, void>
01188   __future_base::_State_base::__setter(promise<void>* __prom)
01189   {
01190     return _Setter<void, void>{ __prom };
01191   }
01192 
01193   inline void
01194   promise<void>::set_value()
01195   {
01196     auto __setter = _State::__setter(this);
01197     _M_future->_M_set_result(std::move(__setter));
01198   }
01199 
01200 
01201   template<typename _Ptr_type, typename _Res>
01202     struct __future_base::_Task_setter
01203     {
01204       _Ptr_type operator()()
01205       {
01206     __try
01207       {
01208         _M_result->_M_set(_M_fn());
01209       }
01210     __catch(...)
01211       {
01212         _M_result->_M_error = current_exception();
01213       }
01214     return std::move(_M_result);
01215       }
01216       _Ptr_type&                _M_result;
01217       std::function<_Res()>     _M_fn;
01218     };
01219 
01220   template<typename _Ptr_type>
01221     struct __future_base::_Task_setter<_Ptr_type, void>
01222     {
01223       _Ptr_type operator()()
01224       {
01225     __try
01226       {
01227         _M_fn();
01228       }
01229     __catch(...)
01230       {
01231         _M_result->_M_error = current_exception();
01232       }
01233     return std::move(_M_result);
01234       }
01235       _Ptr_type&                _M_result;
01236       std::function<void()>     _M_fn;
01237     };
01238 
01239   template<typename _Res, typename... _Args>
01240     struct __future_base::_Task_state_base<_Res(_Args...)>
01241     : __future_base::_State_base
01242     {
01243       typedef _Res _Res_type;
01244 
01245       template<typename _Alloc>
01246     _Task_state_base(const _Alloc& __a)
01247     : _M_result(_S_allocate_result<_Res>(__a))
01248     { }
01249 
01250       virtual void
01251       _M_run(_Args... __args) = 0;
01252 
01253       virtual shared_ptr<_Task_state_base>
01254       _M_reset() = 0;
01255 
01256       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
01257       _Ptr_type _M_result;
01258     };
01259 
01260   template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
01261     struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
01262     : __future_base::_Task_state_base<_Res(_Args...)>
01263     {
01264       _Task_state(_Fn&& __fn, const _Alloc& __a)
01265       : _Task_state_base<_Res(_Args...)>(__a), _M_impl(std::move(__fn), __a)
01266       { }
01267 
01268     private:
01269       virtual void
01270       _M_run(_Args... __args)
01271       {
01272     // bound arguments decay so wrap lvalue references
01273     auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
01274         _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
01275     auto __setter = _S_task_setter(this->_M_result, std::move(__boundfn));
01276     this->_M_set_result(std::move(__setter));
01277       }
01278 
01279       virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
01280       _M_reset();
01281 
01282       template<typename _Tp>
01283     static reference_wrapper<_Tp>
01284     _S_maybe_wrap_ref(_Tp& __t)
01285     { return std::ref(__t); }
01286 
01287       template<typename _Tp>
01288     static
01289     typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
01290     _S_maybe_wrap_ref(_Tp&& __t)
01291     { return std::forward<_Tp>(__t); }
01292 
01293       struct _Impl : _Alloc
01294       {
01295     _Impl(_Fn&& __fn, const _Alloc& __a)
01296       : _Alloc(__a), _M_fn(std::move(__fn)) { }
01297     _Fn _M_fn;
01298       } _M_impl;
01299     };
01300 
01301     template<typename _Signature, typename _Fn, typename _Alloc>
01302       static shared_ptr<__future_base::_Task_state_base<_Signature>>
01303       __create_task_state(_Fn&& __fn, const _Alloc& __a)
01304       {
01305     typedef __future_base::_Task_state<_Fn, _Alloc, _Signature> _State;
01306     return std::allocate_shared<_State>(__a, std::move(__fn), __a);
01307       }
01308 
01309   template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
01310     shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
01311     __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
01312     {
01313       return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
01314                          static_cast<_Alloc&>(_M_impl));
01315     }
01316 
01317   template<typename _Task, typename _Fn, bool
01318        = is_same<_Task, typename decay<_Fn>::type>::value>
01319     struct __constrain_pkgdtask
01320     { typedef void __type; };
01321 
01322   template<typename _Task, typename _Fn>
01323     struct __constrain_pkgdtask<_Task, _Fn, true>
01324     { };
01325 
01326   /// packaged_task
01327   template<typename _Res, typename... _ArgTypes>
01328     class packaged_task<_Res(_ArgTypes...)>
01329     {
01330       typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
01331       shared_ptr<_State_type>                   _M_state;
01332 
01333     public:
01334       // Construction and destruction
01335       packaged_task() noexcept { }
01336 
01337       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01338       // 2095.  missing constructors needed for uses-allocator construction
01339       template<typename _Allocator>
01340     packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
01341     { }
01342 
01343       template<typename _Fn, typename = typename
01344            __constrain_pkgdtask<packaged_task, _Fn>::__type>
01345     explicit
01346     packaged_task(_Fn&& __fn)
01347     : packaged_task(allocator_arg, std::allocator<int>(), std::move(__fn))
01348     { }
01349 
01350       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01351       // 2097.  packaged_task constructors should be constrained
01352       template<typename _Fn, typename _Alloc, typename = typename
01353            __constrain_pkgdtask<packaged_task, _Fn>::__type>
01354     explicit
01355     packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
01356     : _M_state(__create_task_state<_Res(_ArgTypes...)>(
01357             std::forward<_Fn>(__fn), __a))
01358     { }
01359 
01360       ~packaged_task()
01361       {
01362         if (static_cast<bool>(_M_state) && !_M_state.unique())
01363       _M_state->_M_break_promise(std::move(_M_state->_M_result));
01364       }
01365 
01366       // No copy
01367       packaged_task(const packaged_task&) = delete;
01368       packaged_task& operator=(const packaged_task&) = delete;
01369 
01370       template<typename _Allocator>
01371     packaged_task(allocator_arg_t, const _Allocator&,
01372               const packaged_task&) = delete;
01373 
01374       // Move support
01375       packaged_task(packaged_task&& __other) noexcept
01376       { this->swap(__other); }
01377 
01378       template<typename _Allocator>
01379     packaged_task(allocator_arg_t, const _Allocator&,
01380               packaged_task&& __other) noexcept
01381     { this->swap(__other); }
01382 
01383       packaged_task& operator=(packaged_task&& __other) noexcept
01384       {
01385     packaged_task(std::move(__other)).swap(*this);
01386     return *this;
01387       }
01388 
01389       void
01390       swap(packaged_task& __other) noexcept
01391       { _M_state.swap(__other._M_state); }
01392 
01393       bool
01394       valid() const noexcept
01395       { return static_cast<bool>(_M_state); }
01396 
01397       // Result retrieval
01398       future<_Res>
01399       get_future()
01400       { return future<_Res>(_M_state); }
01401 
01402       // Execution
01403       void
01404       operator()(_ArgTypes... __args)
01405       {
01406     __future_base::_State_base::_S_check(_M_state);
01407     _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
01408       }
01409 
01410       void
01411       reset()
01412       {
01413     __future_base::_State_base::_S_check(_M_state);
01414     packaged_task __tmp;
01415     __tmp._M_state = _M_state;
01416     _M_state = _M_state->_M_reset();
01417       }
01418     };
01419 
01420   /// swap
01421   template<typename _Res, typename... _ArgTypes>
01422     inline void
01423     swap(packaged_task<_Res(_ArgTypes...)>& __x,
01424      packaged_task<_Res(_ArgTypes...)>& __y) noexcept
01425     { __x.swap(__y); }
01426 
01427   template<typename _Res, typename _Alloc>
01428     struct uses_allocator<packaged_task<_Res>, _Alloc>
01429     : public true_type { };
01430 
01431 
01432   template<typename _BoundFn, typename _Res>
01433     class __future_base::_Deferred_state final
01434     : public __future_base::_State_base
01435     {
01436     public:
01437       explicit
01438       _Deferred_state(_BoundFn&& __fn)
01439       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
01440       { }
01441 
01442     private:
01443       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
01444       _Ptr_type _M_result;
01445       _BoundFn _M_fn;
01446 
01447       virtual void
01448       _M_run_deferred()
01449       {
01450         // safe to call multiple times so ignore failure
01451         _M_set_result(_S_task_setter(_M_result, _M_fn), true);
01452       }
01453     };
01454 
01455   class __future_base::_Async_state_common : public __future_base::_State_base
01456   {
01457   protected:
01458 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
01459     ~_Async_state_common();
01460 #else
01461     ~_Async_state_common() = default;
01462 #endif
01463 
01464     // Allow non-timed waiting functions to block until the thread completes,
01465     // as if joined.
01466     virtual void _M_run_deferred() { _M_join(); }
01467 
01468     void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
01469 
01470     thread _M_thread;
01471     once_flag _M_once;
01472   };
01473 
01474   template<typename _BoundFn, typename _Res>
01475     class __future_base::_Async_state_impl final
01476     : public __future_base::_Async_state_common
01477     {
01478     public:
01479       explicit
01480       _Async_state_impl(_BoundFn&& __fn)
01481       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
01482       {
01483     _M_thread = std::thread{ [this] {
01484       _M_set_result(_S_task_setter(_M_result, _M_fn));
01485         } };
01486       }
01487 
01488       ~_Async_state_impl() { _M_join(); }
01489 
01490     private:
01491       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
01492       _Ptr_type _M_result;
01493       _BoundFn _M_fn;
01494     };
01495 
01496   template<typename _BoundFn>
01497     inline std::shared_ptr<__future_base::_State_base>
01498     __future_base::_S_make_deferred_state(_BoundFn&& __fn)
01499     {
01500       typedef typename remove_reference<_BoundFn>::type __fn_type;
01501       typedef _Deferred_state<__fn_type> __state_type;
01502       return std::make_shared<__state_type>(std::move(__fn));
01503     }
01504 
01505   template<typename _BoundFn>
01506     inline std::shared_ptr<__future_base::_State_base>
01507     __future_base::_S_make_async_state(_BoundFn&& __fn)
01508     {
01509       typedef typename remove_reference<_BoundFn>::type __fn_type;
01510       typedef _Async_state_impl<__fn_type> __state_type;
01511       return std::make_shared<__state_type>(std::move(__fn));
01512     }
01513 
01514 
01515   /// async
01516   template<typename _Fn, typename... _Args>
01517     future<typename result_of<_Fn(_Args...)>::type>
01518     async(launch __policy, _Fn&& __fn, _Args&&... __args)
01519     {
01520       typedef typename result_of<_Fn(_Args...)>::type result_type;
01521       std::shared_ptr<__future_base::_State_base> __state;
01522       if ((__policy & (launch::async|launch::deferred)) == launch::async)
01523     {
01524       __state = __future_base::_S_make_async_state(std::__bind_simple(
01525               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
01526     }
01527       else
01528     {
01529       __state = __future_base::_S_make_deferred_state(std::__bind_simple(
01530               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
01531     }
01532       return future<result_type>(__state);
01533     }
01534 
01535   /// async, potential overload
01536   template<typename _Fn, typename... _Args>
01537     inline future<typename result_of<_Fn(_Args...)>::type>
01538     async(_Fn&& __fn, _Args&&... __args)
01539     {
01540       return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
01541            std::forward<_Args>(__args)...);
01542     }
01543 
01544 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
01545        // && ATOMIC_INT_LOCK_FREE
01546 
01547   // @} group futures
01548 _GLIBCXX_END_NAMESPACE_VERSION
01549 } // namespace
01550 
01551 #endif // C++11
01552 
01553 #endif // _GLIBCXX_FUTURE