libstdc++
future
Go to the documentation of this file.
00001 // <future> -*- C++ -*-
00002 
00003 // Copyright (C) 2009-2015 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/atomic_futex.h>
00045 #include <bits/functexcept.h>
00046 #include <bits/unique_ptr.h>
00047 #include <bits/shared_ptr.h>
00048 #include <bits/uses_allocator.h>
00049 #include <bits/allocated_ptr.h>
00050 #include <ext/aligned_buffer.h>
00051 
00052 namespace std _GLIBCXX_VISIBILITY(default)
00053 {
00054 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00055 
00056   /**
00057    * @defgroup futures Futures
00058    * @ingroup concurrency
00059    *
00060    * Classes for futures support.
00061    * @{
00062    */
00063 
00064   /// Error code for futures
00065   enum class future_errc
00066   {
00067     future_already_retrieved = 1,
00068     promise_already_satisfied,
00069     no_state,
00070     broken_promise
00071   };
00072 
00073   /// Specialization.
00074   template<>
00075     struct is_error_code_enum<future_errc> : public true_type { };
00076 
00077   /// Points to a statically-allocated object derived from error_category.
00078   const error_category&
00079   future_category() noexcept;
00080 
00081   /// Overload for make_error_code.
00082   inline error_code
00083   make_error_code(future_errc __errc) noexcept
00084   { return error_code(static_cast<int>(__errc), future_category()); }
00085 
00086   /// Overload for make_error_condition.
00087   inline error_condition
00088   make_error_condition(future_errc __errc) noexcept
00089   { return error_condition(static_cast<int>(__errc), future_category()); }
00090 
00091   /**
00092    *  @brief Exception type thrown by futures.
00093    *  @ingroup exceptions
00094    */
00095   class future_error : public logic_error
00096   {
00097     error_code                  _M_code;
00098 
00099   public:
00100     explicit future_error(error_code __ec)
00101     : logic_error("std::future_error: " + __ec.message()), _M_code(__ec)
00102     { }
00103 
00104     virtual ~future_error() noexcept;
00105 
00106     virtual const char*
00107     what() const noexcept;
00108 
00109     const error_code&
00110     code() const noexcept { return _M_code; }
00111   };
00112 
00113   // Forward declarations.
00114   template<typename _Res>
00115     class future;
00116 
00117   template<typename _Res>
00118     class shared_future;
00119 
00120   template<typename _Signature>
00121     class packaged_task;
00122 
00123   template<typename _Res>
00124     class promise;
00125 
00126   /// Launch code for futures
00127   enum class launch
00128   {
00129     async = 1,
00130     deferred = 2
00131   };
00132 
00133   constexpr launch operator&(launch __x, launch __y)
00134   {
00135     return static_cast<launch>(
00136         static_cast<int>(__x) & static_cast<int>(__y));
00137   }
00138 
00139   constexpr launch operator|(launch __x, launch __y)
00140   {
00141     return static_cast<launch>(
00142         static_cast<int>(__x) | static_cast<int>(__y));
00143   }
00144 
00145   constexpr launch operator^(launch __x, launch __y)
00146   {
00147     return static_cast<launch>(
00148         static_cast<int>(__x) ^ static_cast<int>(__y));
00149   }
00150 
00151   constexpr launch operator~(launch __x)
00152   { return static_cast<launch>(~static_cast<int>(__x)); }
00153 
00154   inline launch& operator&=(launch& __x, launch __y)
00155   { return __x = __x & __y; }
00156 
00157   inline launch& operator|=(launch& __x, launch __y)
00158   { return __x = __x | __y; }
00159 
00160   inline launch& operator^=(launch& __x, launch __y)
00161   { return __x = __x ^ __y; }
00162 
00163   /// Status code for futures
00164   enum class future_status
00165   {
00166     ready,
00167     timeout,
00168     deferred
00169   };
00170 
00171   template<typename _Fn, typename... _Args>
00172     future<typename result_of<_Fn(_Args...)>::type>
00173     async(launch __policy, _Fn&& __fn, _Args&&... __args);
00174 
00175   template<typename _Fn, typename... _Args>
00176     future<typename result_of<_Fn(_Args...)>::type>
00177     async(_Fn&& __fn, _Args&&... __args);
00178 
00179 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
00180   && (ATOMIC_INT_LOCK_FREE > 1)
00181 
00182   /// Base class and enclosing scope.
00183   struct __future_base
00184   {
00185     /// Base class for results.
00186     struct _Result_base
00187     {
00188       exception_ptr             _M_error;
00189 
00190       _Result_base(const _Result_base&) = delete;
00191       _Result_base& operator=(const _Result_base&) = delete;
00192 
00193       // _M_destroy() allows derived classes to control deallocation
00194       virtual void _M_destroy() = 0;
00195 
00196       struct _Deleter
00197       {
00198         void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
00199       };
00200 
00201     protected:
00202       _Result_base();
00203       virtual ~_Result_base();
00204     };
00205 
00206     /// A unique_ptr for result objects.
00207     template<typename _Res>
00208       using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
00209 
00210     /// A result object that has storage for an object of type _Res.
00211     template<typename _Res>
00212       struct _Result : _Result_base
00213       {
00214       private:
00215         __gnu_cxx::__aligned_buffer<_Res>       _M_storage;
00216         bool                                    _M_initialized;
00217 
00218       public:
00219         typedef _Res result_type;
00220 
00221         _Result() noexcept : _M_initialized() { }
00222         
00223         ~_Result()
00224         {
00225           if (_M_initialized)
00226             _M_value().~_Res();
00227         }
00228 
00229         // Return lvalue, future will add const or rvalue-reference
00230         _Res&
00231         _M_value() noexcept { return *_M_storage._M_ptr(); }
00232 
00233         void
00234         _M_set(const _Res& __res)
00235         {
00236           ::new (_M_storage._M_addr()) _Res(__res);
00237           _M_initialized = true;
00238         }
00239 
00240         void
00241         _M_set(_Res&& __res)
00242         {
00243           ::new (_M_storage._M_addr()) _Res(std::move(__res));
00244           _M_initialized = true;
00245         }
00246 
00247       private:
00248         void _M_destroy() { delete this; }
00249     };
00250 
00251     /// A result object that uses an allocator.
00252     template<typename _Res, typename _Alloc>
00253       struct _Result_alloc final : _Result<_Res>, _Alloc
00254       {
00255         using __allocator_type = __alloc_rebind<_Alloc, _Result_alloc>;
00256 
00257         explicit
00258         _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
00259         { }
00260         
00261       private:
00262         void _M_destroy()
00263         {
00264           __allocator_type __a(*this);
00265           __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
00266           this->~_Result_alloc();
00267         }
00268       };
00269 
00270     // Create a result object that uses an allocator.
00271     template<typename _Res, typename _Allocator>
00272       static _Ptr<_Result_alloc<_Res, _Allocator>>
00273       _S_allocate_result(const _Allocator& __a)
00274       {
00275         using __result_type = _Result_alloc<_Res, _Allocator>;
00276         typename __result_type::__allocator_type __a2(__a);
00277         auto __guard = std::__allocate_guarded(__a2);
00278         __result_type* __p = ::new((void*)__guard.get()) __result_type{__a};
00279         __guard = nullptr;
00280         return _Ptr<__result_type>(__p);
00281       }
00282 
00283     // Keep it simple for std::allocator.
00284     template<typename _Res, typename _Tp>
00285       static _Ptr<_Result<_Res>>
00286       _S_allocate_result(const std::allocator<_Tp>& __a)
00287       {
00288         return _Ptr<_Result<_Res>>(new _Result<_Res>);
00289       }
00290 
00291     // Base class for various types of shared state created by an
00292     // asynchronous provider (such as a std::promise) and shared with one
00293     // or more associated futures.
00294     class _State_baseV2
00295     {
00296       typedef _Ptr<_Result_base> _Ptr_type;
00297 
00298       enum _Status : unsigned {
00299         __not_ready,
00300         __ready
00301       };
00302 
00303       _Ptr_type                 _M_result;
00304       __atomic_futex_unsigned<> _M_status;
00305       atomic_flag               _M_retrieved = ATOMIC_FLAG_INIT;
00306       once_flag                 _M_once;
00307 
00308     public:
00309       _State_baseV2() noexcept : _M_result(), _M_status(_Status::__not_ready)
00310         { }
00311       _State_baseV2(const _State_baseV2&) = delete;
00312       _State_baseV2& operator=(const _State_baseV2&) = delete;
00313       virtual ~_State_baseV2() = default;
00314 
00315       _Result_base&
00316       wait()
00317       {
00318         // Run any deferred function or join any asynchronous thread:
00319         _M_complete_async();
00320         // Acquire MO makes sure this synchronizes with the thread that made
00321         // the future ready.
00322         _M_status._M_load_when_equal(_Status::__ready, memory_order_acquire);
00323         return *_M_result;
00324       }
00325 
00326       template<typename _Rep, typename _Period>
00327         future_status
00328         wait_for(const chrono::duration<_Rep, _Period>& __rel)
00329         {
00330           // First, check if the future has been made ready.  Use acquire MO
00331           // to synchronize with the thread that made it ready.
00332           if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
00333             return future_status::ready;
00334           if (_M_is_deferred_future())
00335             return future_status::deferred;
00336           if (_M_status._M_load_when_equal_for(_Status::__ready,
00337               memory_order_acquire, __rel))
00338             {
00339               // _GLIBCXX_RESOLVE_LIB_DEFECTS
00340               // 2100.  timed waiting functions must also join
00341               // This call is a no-op by default except on an async future,
00342               // in which case the async thread is joined.  It's also not a
00343               // no-op for a deferred future, but such a future will never
00344               // reach this point because it returns future_status::deferred
00345               // instead of waiting for the future to become ready (see
00346               // above).  Async futures synchronize in this call, so we need
00347               // no further synchronization here.
00348               _M_complete_async();
00349 
00350               return future_status::ready;
00351             }
00352           return future_status::timeout;
00353         }
00354 
00355       template<typename _Clock, typename _Duration>
00356         future_status
00357         wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
00358         {
00359           // First, check if the future has been made ready.  Use acquire MO
00360           // to synchronize with the thread that made it ready.
00361           if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
00362             return future_status::ready;
00363           if (_M_is_deferred_future())
00364             return future_status::deferred;
00365           if (_M_status._M_load_when_equal_until(_Status::__ready,
00366               memory_order_acquire, __abs))
00367             {
00368               // _GLIBCXX_RESOLVE_LIB_DEFECTS
00369               // 2100.  timed waiting functions must also join
00370               // See wait_for(...) above.
00371               _M_complete_async();
00372 
00373               return future_status::ready;
00374             }
00375           return future_status::timeout;
00376         }
00377 
00378       // Provide a result to the shared state and make it ready.
00379       // Calls at most once: _M_result = __res();
00380       void
00381       _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
00382       {
00383         bool __did_set = false;
00384         // all calls to this function are serialized,
00385         // side-effects of invoking __res only happen once
00386         call_once(_M_once, &_State_baseV2::_M_do_set, this,
00387                   std::__addressof(__res), std::__addressof(__did_set));
00388         if (__did_set)
00389           // Use release MO to synchronize with observers of the ready state.
00390           _M_status._M_store_notify_all(_Status::__ready,
00391                                         memory_order_release);
00392         else if (!__ignore_failure)
00393           __throw_future_error(int(future_errc::promise_already_satisfied));
00394       }
00395 
00396       // Provide a result to the shared state but delay making it ready
00397       // until the calling thread exits.
00398       // Calls at most once: _M_result = __res();
00399       void
00400       _M_set_delayed_result(function<_Ptr_type()> __res,
00401                             weak_ptr<_State_baseV2> __self)
00402       {
00403         bool __did_set = false;
00404         unique_ptr<_Make_ready> __mr{new _Make_ready};
00405         // all calls to this function are serialized,
00406         // side-effects of invoking __res only happen once
00407         call_once(_M_once, &_State_baseV2::_M_do_set, this,
00408                   std::__addressof(__res), std::__addressof(__did_set));
00409         if (!__did_set)
00410           __throw_future_error(int(future_errc::promise_already_satisfied));
00411         __mr->_M_shared_state = std::move(__self);
00412         __mr->_M_set();
00413         __mr.release();
00414       }
00415 
00416       // Abandon this shared state.
00417       void
00418       _M_break_promise(_Ptr_type __res)
00419       {
00420         if (static_cast<bool>(__res))
00421           {
00422             error_code __ec(make_error_code(future_errc::broken_promise));
00423             __res->_M_error = make_exception_ptr(future_error(__ec));
00424             // This function is only called when the last asynchronous result
00425             // provider is abandoning this shared state, so noone can be
00426             // trying to make the shared state ready at the same time, and
00427             // we can access _M_result directly instead of through call_once.
00428             _M_result.swap(__res);
00429             // Use release MO to synchronize with observers of the ready state.
00430             _M_status._M_store_notify_all(_Status::__ready,
00431                                           memory_order_release);
00432           }
00433       }
00434 
00435       // Called when this object is first passed to a future.
00436       void
00437       _M_set_retrieved_flag()
00438       {
00439         if (_M_retrieved.test_and_set())
00440           __throw_future_error(int(future_errc::future_already_retrieved));
00441       }
00442 
00443       template<typename _Res, typename _Arg>
00444         struct _Setter;
00445 
00446       // set lvalues
00447       template<typename _Res, typename _Arg>
00448         struct _Setter<_Res, _Arg&>
00449         {
00450           // check this is only used by promise<R>::set_value(const R&)
00451           // or promise<R&>::set_value(R&)
00452           static_assert(is_same<_Res, _Arg&>::value  // promise<R&>
00453               || is_same<const _Res, _Arg>::value,   // promise<R>
00454               "Invalid specialisation");
00455 
00456           // Used by std::promise to copy construct the result.
00457           typename promise<_Res>::_Ptr_type operator()() const
00458           {
00459             _State_baseV2::_S_check(_M_promise->_M_future);
00460             _M_promise->_M_storage->_M_set(*_M_arg);
00461             return std::move(_M_promise->_M_storage);
00462           }
00463           promise<_Res>*    _M_promise;
00464           _Arg*             _M_arg;
00465         };
00466 
00467       // set rvalues
00468       template<typename _Res>
00469         struct _Setter<_Res, _Res&&>
00470         {
00471           // Used by std::promise to move construct the result.
00472           typename promise<_Res>::_Ptr_type operator()() const
00473           {
00474             _State_baseV2::_S_check(_M_promise->_M_future);
00475             _M_promise->_M_storage->_M_set(std::move(*_M_arg));
00476             return std::move(_M_promise->_M_storage);
00477           }
00478           promise<_Res>*    _M_promise;
00479           _Res*             _M_arg;
00480         };
00481 
00482       struct __exception_ptr_tag { };
00483 
00484       // set exceptions
00485       template<typename _Res>
00486         struct _Setter<_Res, __exception_ptr_tag>
00487         {
00488           // Used by std::promise to store an exception as the result.
00489           typename promise<_Res>::_Ptr_type operator()() const
00490           {
00491             _State_baseV2::_S_check(_M_promise->_M_future);
00492             _M_promise->_M_storage->_M_error = *_M_ex;
00493             return std::move(_M_promise->_M_storage);
00494           }
00495 
00496           promise<_Res>*   _M_promise;
00497           exception_ptr*    _M_ex;
00498         };
00499 
00500       template<typename _Res, typename _Arg>
00501         static _Setter<_Res, _Arg&&>
00502         __setter(promise<_Res>* __prom, _Arg&& __arg)
00503         {
00504           return _Setter<_Res, _Arg&&>{ __prom, &__arg };
00505         }
00506 
00507       template<typename _Res>
00508         static _Setter<_Res, __exception_ptr_tag>
00509         __setter(exception_ptr& __ex, promise<_Res>* __prom)
00510         {
00511           return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
00512         }
00513 
00514       template<typename _Tp>
00515         static void
00516         _S_check(const shared_ptr<_Tp>& __p)
00517         {
00518           if (!static_cast<bool>(__p))
00519             __throw_future_error((int)future_errc::no_state);
00520         }
00521 
00522     private:
00523       // The function invoked with std::call_once(_M_once, ...).
00524       void
00525       _M_do_set(function<_Ptr_type()>* __f, bool* __did_set)
00526       {
00527         _Ptr_type __res = (*__f)();
00528         // Notify the caller that we did try to set; if we do not throw an
00529         // exception, the caller will be aware that it did set (e.g., see
00530         // _M_set_result).
00531         *__did_set = true;
00532         _M_result.swap(__res); // nothrow
00533       }
00534 
00535       // Wait for completion of async function.
00536       virtual void _M_complete_async() { }
00537 
00538       // Return true if state corresponds to a deferred function.
00539       virtual bool _M_is_deferred_future() const { return false; }
00540 
00541       struct _Make_ready final : __at_thread_exit_elt
00542       {
00543         weak_ptr<_State_baseV2> _M_shared_state;
00544         static void _S_run(void*);
00545         void _M_set();
00546       };
00547     };
00548 
00549 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
00550     class _State_base;
00551     class _Async_state_common;
00552 #else
00553     using _State_base = _State_baseV2;
00554     class _Async_state_commonV2;
00555 #endif
00556 
00557     template<typename _BoundFn, typename = typename _BoundFn::result_type>
00558       class _Deferred_state;
00559 
00560     template<typename _BoundFn, typename = typename _BoundFn::result_type>
00561       class _Async_state_impl;
00562 
00563     template<typename _Signature>
00564       class _Task_state_base;
00565 
00566     template<typename _Fn, typename _Alloc, typename _Signature>
00567       class _Task_state;
00568 
00569     template<typename _BoundFn>
00570       static std::shared_ptr<_State_base>
00571       _S_make_deferred_state(_BoundFn&& __fn);
00572 
00573     template<typename _BoundFn>
00574       static std::shared_ptr<_State_base>
00575       _S_make_async_state(_BoundFn&& __fn);
00576 
00577     template<typename _Res_ptr, typename _Fn,
00578              typename _Res = typename _Res_ptr::element_type::result_type>
00579       struct _Task_setter;
00580 
00581     template<typename _Res_ptr, typename _BoundFn>
00582       static _Task_setter<_Res_ptr, _BoundFn>
00583       _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
00584       {
00585         return { std::__addressof(__ptr), std::__addressof(__call) };
00586       }
00587   };
00588 
00589   /// Partial specialization for reference types.
00590   template<typename _Res>
00591     struct __future_base::_Result<_Res&> : __future_base::_Result_base
00592     {
00593       typedef _Res& result_type;
00594 
00595       _Result() noexcept : _M_value_ptr() { }
00596 
00597       void
00598       _M_set(_Res& __res) noexcept
00599       { _M_value_ptr = std::addressof(__res); }
00600 
00601       _Res& _M_get() noexcept { return *_M_value_ptr; }
00602 
00603     private:
00604       _Res*                     _M_value_ptr;
00605 
00606       void _M_destroy() { delete this; }
00607     };
00608 
00609   /// Explicit specialization for void.
00610   template<>
00611     struct __future_base::_Result<void> : __future_base::_Result_base
00612     {
00613       typedef void result_type;
00614 
00615     private:
00616       void _M_destroy() { delete this; }
00617     };
00618 
00619 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
00620 
00621   // Allow _Setter objects to be stored locally in std::function
00622   template<typename _Res, typename _Arg>
00623     struct __is_location_invariant
00624     <__future_base::_State_base::_Setter<_Res, _Arg>>
00625     : true_type { };
00626 
00627   // Allow _Task_setter objects to be stored locally in std::function
00628   template<typename _Res_ptr, typename _Fn, typename _Res>
00629     struct __is_location_invariant
00630     <__future_base::_Task_setter<_Res_ptr, _Fn, _Res>>
00631     : true_type { };
00632 
00633   /// Common implementation for future and shared_future.
00634   template<typename _Res>
00635     class __basic_future : public __future_base
00636     {
00637     protected:
00638       typedef shared_ptr<_State_base>           __state_type;
00639       typedef __future_base::_Result<_Res>&     __result_type;
00640 
00641     private:
00642       __state_type              _M_state;
00643 
00644     public:
00645       // Disable copying.
00646       __basic_future(const __basic_future&) = delete;
00647       __basic_future& operator=(const __basic_future&) = delete;
00648 
00649       bool
00650       valid() const noexcept { return static_cast<bool>(_M_state); }
00651 
00652       void
00653       wait() const
00654       {
00655         _State_base::_S_check(_M_state);
00656         _M_state->wait();
00657       }
00658 
00659       template<typename _Rep, typename _Period>
00660         future_status
00661         wait_for(const chrono::duration<_Rep, _Period>& __rel) const
00662         {
00663           _State_base::_S_check(_M_state);
00664           return _M_state->wait_for(__rel);
00665         }
00666 
00667       template<typename _Clock, typename _Duration>
00668         future_status
00669         wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
00670         {
00671           _State_base::_S_check(_M_state);
00672           return _M_state->wait_until(__abs);
00673         }
00674 
00675     protected:
00676       /// Wait for the state to be ready and rethrow any stored exception
00677       __result_type
00678       _M_get_result() const
00679       {
00680         _State_base::_S_check(_M_state);
00681         _Result_base& __res = _M_state->wait();
00682         if (!(__res._M_error == 0))
00683           rethrow_exception(__res._M_error);
00684         return static_cast<__result_type>(__res);
00685       }
00686 
00687       void _M_swap(__basic_future& __that) noexcept
00688       {
00689         _M_state.swap(__that._M_state);
00690       }
00691 
00692       // Construction of a future by promise::get_future()
00693       explicit
00694       __basic_future(const __state_type& __state) : _M_state(__state)
00695       {
00696         _State_base::_S_check(_M_state);
00697         _M_state->_M_set_retrieved_flag();
00698       }
00699 
00700       // Copy construction from a shared_future
00701       explicit
00702       __basic_future(const shared_future<_Res>&) noexcept;
00703 
00704       // Move construction from a shared_future
00705       explicit
00706       __basic_future(shared_future<_Res>&&) noexcept;
00707 
00708       // Move construction from a future
00709       explicit
00710       __basic_future(future<_Res>&&) noexcept;
00711 
00712       constexpr __basic_future() noexcept : _M_state() { }
00713 
00714       struct _Reset
00715       {
00716         explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
00717         ~_Reset() { _M_fut._M_state.reset(); }
00718         __basic_future& _M_fut;
00719       };
00720     };
00721 
00722 
00723   /// Primary template for future.
00724   template<typename _Res>
00725     class future : public __basic_future<_Res>
00726     {
00727       friend class promise<_Res>;
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<_Res> _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       _Res
00757       get()
00758       {
00759         typename _Base_type::_Reset __reset(*this);
00760         return std::move(this->_M_get_result()._M_value());
00761       }
00762 
00763       shared_future<_Res> share();
00764     };
00765 
00766   /// Partial specialization for future<R&>
00767   template<typename _Res>
00768     class future<_Res&> : public __basic_future<_Res&>
00769     {
00770       friend class promise<_Res&>;
00771       template<typename> friend class packaged_task;
00772       template<typename _Fn, typename... _Args>
00773         friend future<typename result_of<_Fn(_Args...)>::type>
00774         async(launch, _Fn&&, _Args&&...);
00775 
00776       typedef __basic_future<_Res&> _Base_type;
00777       typedef typename _Base_type::__state_type __state_type;
00778 
00779       explicit
00780       future(const __state_type& __state) : _Base_type(__state) { }
00781 
00782     public:
00783       constexpr future() noexcept : _Base_type() { }
00784 
00785       /// Move constructor
00786       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
00787 
00788       // Disable copying
00789       future(const future&) = delete;
00790       future& operator=(const future&) = delete;
00791 
00792       future& operator=(future&& __fut) noexcept
00793       {
00794         future(std::move(__fut))._M_swap(*this);
00795         return *this;
00796       }
00797 
00798       /// Retrieving the value
00799       _Res&
00800       get()
00801       {
00802         typename _Base_type::_Reset __reset(*this);
00803         return this->_M_get_result()._M_get();
00804       }
00805 
00806       shared_future<_Res&> share();
00807     };
00808 
00809   /// Explicit specialization for future<void>
00810   template<>
00811     class future<void> : public __basic_future<void>
00812     {
00813       friend class promise<void>;
00814       template<typename> friend class packaged_task;
00815       template<typename _Fn, typename... _Args>
00816         friend future<typename result_of<_Fn(_Args...)>::type>
00817         async(launch, _Fn&&, _Args&&...);
00818 
00819       typedef __basic_future<void> _Base_type;
00820       typedef typename _Base_type::__state_type __state_type;
00821 
00822       explicit
00823       future(const __state_type& __state) : _Base_type(__state) { }
00824 
00825     public:
00826       constexpr future() noexcept : _Base_type() { }
00827 
00828       /// Move constructor
00829       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
00830 
00831       // Disable copying
00832       future(const future&) = delete;
00833       future& operator=(const future&) = delete;
00834 
00835       future& operator=(future&& __fut) noexcept
00836       {
00837         future(std::move(__fut))._M_swap(*this);
00838         return *this;
00839       }
00840 
00841       /// Retrieving the value
00842       void
00843       get()
00844       {
00845         typename _Base_type::_Reset __reset(*this);
00846         this->_M_get_result();
00847       }
00848 
00849       shared_future<void> share();
00850     };
00851 
00852 
00853   /// Primary template for shared_future.
00854   template<typename _Res>
00855     class shared_future : public __basic_future<_Res>
00856     {
00857       typedef __basic_future<_Res> _Base_type;
00858 
00859     public:
00860       constexpr shared_future() noexcept : _Base_type() { }
00861 
00862       /// Copy constructor
00863       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
00864 
00865       /// Construct from a future rvalue
00866       shared_future(future<_Res>&& __uf) noexcept
00867       : _Base_type(std::move(__uf))
00868       { }
00869 
00870       /// Construct from a shared_future rvalue
00871       shared_future(shared_future&& __sf) noexcept
00872       : _Base_type(std::move(__sf))
00873       { }
00874 
00875       shared_future& operator=(const shared_future& __sf)
00876       {
00877         shared_future(__sf)._M_swap(*this);
00878         return *this;
00879       }
00880 
00881       shared_future& operator=(shared_future&& __sf) noexcept
00882       {
00883         shared_future(std::move(__sf))._M_swap(*this);
00884         return *this;
00885       }
00886 
00887       /// Retrieving the value
00888       const _Res&
00889       get() const { return this->_M_get_result()._M_value(); }
00890     };
00891 
00892   /// Partial specialization for shared_future<R&>
00893   template<typename _Res>
00894     class shared_future<_Res&> : public __basic_future<_Res&>
00895     {
00896       typedef __basic_future<_Res&>           _Base_type;
00897 
00898     public:
00899       constexpr shared_future() noexcept : _Base_type() { }
00900 
00901       /// Copy constructor
00902       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
00903 
00904       /// Construct from a future rvalue
00905       shared_future(future<_Res&>&& __uf) noexcept
00906       : _Base_type(std::move(__uf))
00907       { }
00908 
00909       /// Construct from a shared_future rvalue
00910       shared_future(shared_future&& __sf) noexcept
00911       : _Base_type(std::move(__sf))
00912       { }
00913 
00914       shared_future& operator=(const shared_future& __sf)
00915       {
00916         shared_future(__sf)._M_swap(*this);
00917         return *this;
00918       }
00919 
00920       shared_future& operator=(shared_future&& __sf) noexcept
00921       {
00922         shared_future(std::move(__sf))._M_swap(*this);
00923         return *this;
00924       }
00925 
00926       /// Retrieving the value
00927       _Res&
00928       get() const { return this->_M_get_result()._M_get(); }
00929     };
00930 
00931   /// Explicit specialization for shared_future<void>
00932   template<>
00933     class shared_future<void> : public __basic_future<void>
00934     {
00935       typedef __basic_future<void> _Base_type;
00936 
00937     public:
00938       constexpr shared_future() noexcept : _Base_type() { }
00939 
00940       /// Copy constructor
00941       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
00942 
00943       /// Construct from a future rvalue
00944       shared_future(future<void>&& __uf) noexcept
00945       : _Base_type(std::move(__uf))
00946       { }
00947 
00948       /// Construct from a shared_future rvalue
00949       shared_future(shared_future&& __sf) noexcept
00950       : _Base_type(std::move(__sf))
00951       { }
00952 
00953       shared_future& operator=(const shared_future& __sf)
00954       {
00955         shared_future(__sf)._M_swap(*this);
00956         return *this;
00957       }
00958 
00959       shared_future& operator=(shared_future&& __sf) noexcept
00960       {
00961         shared_future(std::move(__sf))._M_swap(*this);
00962         return *this;
00963       }
00964 
00965       // Retrieving the value
00966       void
00967       get() const { this->_M_get_result(); }
00968     };
00969 
00970   // Now we can define the protected __basic_future constructors.
00971   template<typename _Res>
00972     inline __basic_future<_Res>::
00973     __basic_future(const shared_future<_Res>& __sf) noexcept
00974     : _M_state(__sf._M_state)
00975     { }
00976 
00977   template<typename _Res>
00978     inline __basic_future<_Res>::
00979     __basic_future(shared_future<_Res>&& __sf) noexcept
00980     : _M_state(std::move(__sf._M_state))
00981     { }
00982 
00983   template<typename _Res>
00984     inline __basic_future<_Res>::
00985     __basic_future(future<_Res>&& __uf) noexcept
00986     : _M_state(std::move(__uf._M_state))
00987     { }
00988 
00989   template<typename _Res>
00990     inline shared_future<_Res>
00991     future<_Res>::share()
00992     { return shared_future<_Res>(std::move(*this)); }
00993 
00994   template<typename _Res>
00995     inline shared_future<_Res&>
00996     future<_Res&>::share()
00997     { return shared_future<_Res&>(std::move(*this)); }
00998 
00999   inline shared_future<void>
01000   future<void>::share()
01001   { return shared_future<void>(std::move(*this)); }
01002 
01003   /// Primary template for promise
01004   template<typename _Res>
01005     class promise
01006     {
01007       typedef __future_base::_State_base        _State;
01008       typedef __future_base::_Result<_Res>      _Res_type;
01009       typedef __future_base::_Ptr<_Res_type>    _Ptr_type;
01010       template<typename, typename> friend class _State::_Setter;
01011 
01012       shared_ptr<_State>                        _M_future;
01013       _Ptr_type                                 _M_storage;
01014 
01015     public:
01016       promise()
01017       : _M_future(std::make_shared<_State>()),
01018         _M_storage(new _Res_type())
01019       { }
01020 
01021       promise(promise&& __rhs) noexcept
01022       : _M_future(std::move(__rhs._M_future)),
01023         _M_storage(std::move(__rhs._M_storage))
01024       { }
01025 
01026       template<typename _Allocator>
01027         promise(allocator_arg_t, const _Allocator& __a)
01028         : _M_future(std::allocate_shared<_State>(__a)),
01029           _M_storage(__future_base::_S_allocate_result<_Res>(__a))
01030         { }
01031 
01032       template<typename _Allocator>
01033         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
01034         : _M_future(std::move(__rhs._M_future)),
01035           _M_storage(std::move(__rhs._M_storage))
01036         { }
01037 
01038       promise(const promise&) = delete;
01039 
01040       ~promise()
01041       {
01042         if (static_cast<bool>(_M_future) && !_M_future.unique())
01043           _M_future->_M_break_promise(std::move(_M_storage));
01044       }
01045 
01046       // Assignment
01047       promise&
01048       operator=(promise&& __rhs) noexcept
01049       {
01050         promise(std::move(__rhs)).swap(*this);
01051         return *this;
01052       }
01053 
01054       promise& operator=(const promise&) = delete;
01055 
01056       void
01057       swap(promise& __rhs) noexcept
01058       {
01059         _M_future.swap(__rhs._M_future);
01060         _M_storage.swap(__rhs._M_storage);
01061       }
01062 
01063       // Retrieving the result
01064       future<_Res>
01065       get_future()
01066       { return future<_Res>(_M_future); }
01067 
01068       // Setting the result
01069       void
01070       set_value(const _Res& __r)
01071       { _M_future->_M_set_result(_State::__setter(this, __r)); }
01072 
01073       void
01074       set_value(_Res&& __r)
01075       { _M_future->_M_set_result(_State::__setter(this, std::move(__r))); }
01076 
01077       void
01078       set_exception(exception_ptr __p)
01079       { _M_future->_M_set_result(_State::__setter(__p, this)); }
01080 
01081       void
01082       set_value_at_thread_exit(const _Res& __r)
01083       {
01084         _M_future->_M_set_delayed_result(_State::__setter(this, __r),
01085                                          _M_future);
01086       }
01087 
01088       void
01089       set_value_at_thread_exit(_Res&& __r)
01090       {
01091         _M_future->_M_set_delayed_result(
01092             _State::__setter(this, std::move(__r)), _M_future);
01093       }
01094 
01095       void
01096       set_exception_at_thread_exit(exception_ptr __p)
01097       {
01098         _M_future->_M_set_delayed_result(_State::__setter(__p, this),
01099                                          _M_future);
01100       }
01101     };
01102 
01103   template<typename _Res>
01104     inline void
01105     swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
01106     { __x.swap(__y); }
01107 
01108   template<typename _Res, typename _Alloc>
01109     struct uses_allocator<promise<_Res>, _Alloc>
01110     : public true_type { };
01111 
01112 
01113   /// Partial specialization for promise<R&>
01114   template<typename _Res>
01115     class promise<_Res&>
01116     {
01117       typedef __future_base::_State_base        _State;
01118       typedef __future_base::_Result<_Res&>     _Res_type;
01119       typedef __future_base::_Ptr<_Res_type>    _Ptr_type;
01120       template<typename, typename> friend class _State::_Setter;
01121 
01122       shared_ptr<_State>                        _M_future;
01123       _Ptr_type                                 _M_storage;
01124 
01125     public:
01126       promise()
01127       : _M_future(std::make_shared<_State>()),
01128         _M_storage(new _Res_type())
01129       { }
01130 
01131       promise(promise&& __rhs) noexcept
01132       : _M_future(std::move(__rhs._M_future)),
01133         _M_storage(std::move(__rhs._M_storage))
01134       { }
01135 
01136       template<typename _Allocator>
01137         promise(allocator_arg_t, const _Allocator& __a)
01138         : _M_future(std::allocate_shared<_State>(__a)),
01139           _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
01140         { }
01141 
01142       template<typename _Allocator>
01143         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
01144         : _M_future(std::move(__rhs._M_future)),
01145           _M_storage(std::move(__rhs._M_storage))
01146         { }
01147 
01148       promise(const promise&) = delete;
01149 
01150       ~promise()
01151       {
01152         if (static_cast<bool>(_M_future) && !_M_future.unique())
01153           _M_future->_M_break_promise(std::move(_M_storage));
01154       }
01155 
01156       // Assignment
01157       promise&
01158       operator=(promise&& __rhs) noexcept
01159       {
01160         promise(std::move(__rhs)).swap(*this);
01161         return *this;
01162       }
01163 
01164       promise& operator=(const promise&) = delete;
01165 
01166       void
01167       swap(promise& __rhs) noexcept
01168       {
01169         _M_future.swap(__rhs._M_future);
01170         _M_storage.swap(__rhs._M_storage);
01171       }
01172 
01173       // Retrieving the result
01174       future<_Res&>
01175       get_future()
01176       { return future<_Res&>(_M_future); }
01177 
01178       // Setting the result
01179       void
01180       set_value(_Res& __r)
01181       { _M_future->_M_set_result(_State::__setter(this, __r)); }
01182 
01183       void
01184       set_exception(exception_ptr __p)
01185       { _M_future->_M_set_result(_State::__setter(__p, this)); }
01186 
01187       void
01188       set_value_at_thread_exit(_Res& __r)
01189       {
01190         _M_future->_M_set_delayed_result(_State::__setter(this, __r),
01191                                          _M_future);
01192       }
01193 
01194       void
01195       set_exception_at_thread_exit(exception_ptr __p)
01196       {
01197         _M_future->_M_set_delayed_result(_State::__setter(__p, this),
01198                                          _M_future);
01199       }
01200     };
01201 
01202   /// Explicit specialization for promise<void>
01203   template<>
01204     class promise<void>
01205     {
01206       typedef __future_base::_State_base        _State;
01207       typedef __future_base::_Result<void>      _Res_type;
01208       typedef __future_base::_Ptr<_Res_type>    _Ptr_type;
01209       template<typename, typename> friend class _State::_Setter;
01210 
01211       shared_ptr<_State>                        _M_future;
01212       _Ptr_type                                 _M_storage;
01213 
01214     public:
01215       promise()
01216       : _M_future(std::make_shared<_State>()),
01217         _M_storage(new _Res_type())
01218       { }
01219 
01220       promise(promise&& __rhs) noexcept
01221       : _M_future(std::move(__rhs._M_future)),
01222         _M_storage(std::move(__rhs._M_storage))
01223       { }
01224 
01225       template<typename _Allocator>
01226         promise(allocator_arg_t, const _Allocator& __a)
01227         : _M_future(std::allocate_shared<_State>(__a)),
01228           _M_storage(__future_base::_S_allocate_result<void>(__a))
01229         { }
01230 
01231       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01232       // 2095.  missing constructors needed for uses-allocator construction
01233       template<typename _Allocator>
01234         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
01235         : _M_future(std::move(__rhs._M_future)),
01236           _M_storage(std::move(__rhs._M_storage))
01237         { }
01238 
01239       promise(const promise&) = delete;
01240 
01241       ~promise()
01242       {
01243         if (static_cast<bool>(_M_future) && !_M_future.unique())
01244           _M_future->_M_break_promise(std::move(_M_storage));
01245       }
01246 
01247       // Assignment
01248       promise&
01249       operator=(promise&& __rhs) noexcept
01250       {
01251         promise(std::move(__rhs)).swap(*this);
01252         return *this;
01253       }
01254 
01255       promise& operator=(const promise&) = delete;
01256 
01257       void
01258       swap(promise& __rhs) noexcept
01259       {
01260         _M_future.swap(__rhs._M_future);
01261         _M_storage.swap(__rhs._M_storage);
01262       }
01263 
01264       // Retrieving the result
01265       future<void>
01266       get_future()
01267       { return future<void>(_M_future); }
01268 
01269       // Setting the result
01270       void set_value();
01271 
01272       void
01273       set_exception(exception_ptr __p)
01274       { _M_future->_M_set_result(_State::__setter(__p, this)); }
01275 
01276       void
01277       set_value_at_thread_exit();
01278 
01279       void
01280       set_exception_at_thread_exit(exception_ptr __p)
01281       {
01282         _M_future->_M_set_delayed_result(_State::__setter(__p, this),
01283                                          _M_future);
01284       }
01285     };
01286 
01287   // set void
01288   template<>
01289     struct __future_base::_State_base::_Setter<void, void>
01290     {
01291       promise<void>::_Ptr_type operator()() const
01292       {
01293         _State_base::_S_check(_M_promise->_M_future);
01294         return std::move(_M_promise->_M_storage);
01295       }
01296 
01297       promise<void>*    _M_promise;
01298     };
01299 
01300   inline void
01301   promise<void>::set_value()
01302   { _M_future->_M_set_result(_State::_Setter<void, void>{ this }); }
01303 
01304   inline void
01305   promise<void>::set_value_at_thread_exit()
01306   {
01307     _M_future->_M_set_delayed_result(_State::_Setter<void, void>{this},
01308                                      _M_future);
01309   }
01310 
01311   template<typename _Ptr_type, typename _Fn, typename _Res>
01312     struct __future_base::_Task_setter
01313     {
01314       // Invoke the function and provide the result to the caller.
01315       _Ptr_type operator()() const
01316       {
01317         __try
01318           {
01319             (*_M_result)->_M_set((*_M_fn)());
01320           }
01321         __catch(const __cxxabiv1::__forced_unwind&)
01322           {
01323             __throw_exception_again; // will cause broken_promise
01324           }
01325         __catch(...)
01326           {
01327             (*_M_result)->_M_error = current_exception();
01328           }
01329         return std::move(*_M_result);
01330       }
01331       _Ptr_type*        _M_result;
01332       _Fn*              _M_fn;
01333     };
01334 
01335   template<typename _Ptr_type, typename _Fn>
01336     struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
01337     {
01338       _Ptr_type operator()() const
01339       {
01340         __try
01341           {
01342             (*_M_fn)();
01343           }
01344         __catch(const __cxxabiv1::__forced_unwind&)
01345           {
01346             __throw_exception_again; // will cause broken_promise
01347           }
01348         __catch(...)
01349           {
01350             (*_M_result)->_M_error = current_exception();
01351           }
01352         return std::move(*_M_result);
01353       }
01354       _Ptr_type*        _M_result;
01355       _Fn*              _M_fn;
01356     };
01357 
01358   // Holds storage for a packaged_task's result.
01359   template<typename _Res, typename... _Args>
01360     struct __future_base::_Task_state_base<_Res(_Args...)>
01361     : __future_base::_State_base
01362     {
01363       typedef _Res _Res_type;
01364 
01365       template<typename _Alloc>
01366         _Task_state_base(const _Alloc& __a)
01367         : _M_result(_S_allocate_result<_Res>(__a))
01368         { }
01369 
01370       // Invoke the stored task and make the state ready.
01371       virtual void
01372       _M_run(_Args&&... __args) = 0;
01373 
01374       // Invoke the stored task and make the state ready at thread exit.
01375       virtual void
01376       _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
01377 
01378       virtual shared_ptr<_Task_state_base>
01379       _M_reset() = 0;
01380 
01381       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
01382       _Ptr_type _M_result;
01383     };
01384 
01385   // Holds a packaged_task's stored task.
01386   template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
01387     struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
01388     : __future_base::_Task_state_base<_Res(_Args...)>
01389     {
01390       template<typename _Fn2>
01391         _Task_state(_Fn2&& __fn, const _Alloc& __a)
01392         : _Task_state_base<_Res(_Args...)>(__a),
01393           _M_impl(std::forward<_Fn2>(__fn), __a)
01394         { }
01395 
01396     private:
01397       virtual void
01398       _M_run(_Args&&... __args)
01399       {
01400         // bound arguments decay so wrap lvalue references
01401         auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
01402             _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
01403         this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
01404       }
01405 
01406       virtual void
01407       _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
01408       {
01409         // bound arguments decay so wrap lvalue references
01410         auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
01411             _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
01412         this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
01413                                     std::move(__self));
01414       }
01415 
01416       virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
01417       _M_reset();
01418 
01419       template<typename _Tp>
01420         static reference_wrapper<_Tp>
01421         _S_maybe_wrap_ref(_Tp& __t)
01422         { return std::ref(__t); }
01423 
01424       template<typename _Tp>
01425         static
01426         typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
01427         _S_maybe_wrap_ref(_Tp&& __t)
01428         { return std::forward<_Tp>(__t); }
01429 
01430       struct _Impl : _Alloc
01431       {
01432         template<typename _Fn2>
01433           _Impl(_Fn2&& __fn, const _Alloc& __a)
01434           : _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { }
01435         _Fn _M_fn;
01436       } _M_impl;
01437     };
01438 
01439   template<typename _Signature, typename _Fn, typename _Alloc>
01440     static shared_ptr<__future_base::_Task_state_base<_Signature>>
01441     __create_task_state(_Fn&& __fn, const _Alloc& __a)
01442     {
01443       typedef typename decay<_Fn>::type _Fn2;
01444       typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
01445       return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
01446     }
01447 
01448   template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
01449     shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
01450     __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
01451     {
01452       return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
01453                                                  static_cast<_Alloc&>(_M_impl));
01454     }
01455 
01456   template<typename _Task, typename _Fn, bool
01457            = is_same<_Task, typename decay<_Fn>::type>::value>
01458     struct __constrain_pkgdtask
01459     { typedef void __type; };
01460 
01461   template<typename _Task, typename _Fn>
01462     struct __constrain_pkgdtask<_Task, _Fn, true>
01463     { };
01464 
01465   /// packaged_task
01466   template<typename _Res, typename... _ArgTypes>
01467     class packaged_task<_Res(_ArgTypes...)>
01468     {
01469       typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
01470       shared_ptr<_State_type>                   _M_state;
01471 
01472     public:
01473       // Construction and destruction
01474       packaged_task() noexcept { }
01475 
01476       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01477       // 2095.  missing constructors needed for uses-allocator construction
01478       template<typename _Allocator>
01479         packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
01480         { }
01481 
01482       template<typename _Fn, typename = typename
01483                __constrain_pkgdtask<packaged_task, _Fn>::__type>
01484         explicit
01485         packaged_task(_Fn&& __fn)
01486         : packaged_task(allocator_arg, std::allocator<int>(),
01487                         std::forward<_Fn>(__fn))
01488         { }
01489 
01490       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01491       // 2097.  packaged_task constructors should be constrained
01492       template<typename _Fn, typename _Alloc, typename = typename
01493                __constrain_pkgdtask<packaged_task, _Fn>::__type>
01494         explicit
01495         packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
01496         : _M_state(__create_task_state<_Res(_ArgTypes...)>(
01497                     std::forward<_Fn>(__fn), __a))
01498         { }
01499 
01500       ~packaged_task()
01501       {
01502         if (static_cast<bool>(_M_state) && !_M_state.unique())
01503           _M_state->_M_break_promise(std::move(_M_state->_M_result));
01504       }
01505 
01506       // No copy
01507       packaged_task(const packaged_task&) = delete;
01508       packaged_task& operator=(const packaged_task&) = delete;
01509 
01510       template<typename _Allocator>
01511         packaged_task(allocator_arg_t, const _Allocator&,
01512                       const packaged_task&) = delete;
01513 
01514       // Move support
01515       packaged_task(packaged_task&& __other) noexcept
01516       { this->swap(__other); }
01517 
01518       template<typename _Allocator>
01519         packaged_task(allocator_arg_t, const _Allocator&,
01520                       packaged_task&& __other) noexcept
01521         { this->swap(__other); }
01522 
01523       packaged_task& operator=(packaged_task&& __other) noexcept
01524       {
01525         packaged_task(std::move(__other)).swap(*this);
01526         return *this;
01527       }
01528 
01529       void
01530       swap(packaged_task& __other) noexcept
01531       { _M_state.swap(__other._M_state); }
01532 
01533       bool
01534       valid() const noexcept
01535       { return static_cast<bool>(_M_state); }
01536 
01537       // Result retrieval
01538       future<_Res>
01539       get_future()
01540       { return future<_Res>(_M_state); }
01541 
01542       // Execution
01543       void
01544       operator()(_ArgTypes... __args)
01545       {
01546         __future_base::_State_base::_S_check(_M_state);
01547         _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
01548       }
01549 
01550       void
01551       make_ready_at_thread_exit(_ArgTypes... __args)
01552       {
01553         __future_base::_State_base::_S_check(_M_state);
01554         _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
01555       }
01556 
01557       void
01558       reset()
01559       {
01560         __future_base::_State_base::_S_check(_M_state);
01561         packaged_task __tmp;
01562         __tmp._M_state = _M_state;
01563         _M_state = _M_state->_M_reset();
01564       }
01565     };
01566 
01567   /// swap
01568   template<typename _Res, typename... _ArgTypes>
01569     inline void
01570     swap(packaged_task<_Res(_ArgTypes...)>& __x,
01571          packaged_task<_Res(_ArgTypes...)>& __y) noexcept
01572     { __x.swap(__y); }
01573 
01574   template<typename _Res, typename _Alloc>
01575     struct uses_allocator<packaged_task<_Res>, _Alloc>
01576     : public true_type { };
01577 
01578 
01579   // Shared state created by std::async().
01580   // Holds a deferred function and storage for its result.
01581   template<typename _BoundFn, typename _Res>
01582     class __future_base::_Deferred_state final
01583     : public __future_base::_State_base
01584     {
01585     public:
01586       explicit
01587       _Deferred_state(_BoundFn&& __fn)
01588       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
01589       { }
01590 
01591     private:
01592       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
01593       _Ptr_type _M_result;
01594       _BoundFn _M_fn;
01595 
01596       // Run the deferred function.
01597       virtual void
01598       _M_complete_async()
01599       {
01600         // Multiple threads can call a waiting function on the future and
01601         // reach this point at the same time. The call_once in _M_set_result
01602         // ensures only the first one run the deferred function, stores the
01603         // result in _M_result, swaps that with the base _M_result and makes
01604         // the state ready. Tell _M_set_result to ignore failure so all later
01605         // calls do nothing.
01606         _M_set_result(_S_task_setter(_M_result, _M_fn), true);
01607       }
01608 
01609       // Caller should check whether the state is ready first, because this
01610       // function will return true even after the deferred function has run.
01611       virtual bool _M_is_deferred_future() const { return true; }
01612     };
01613 
01614   // Common functionality hoisted out of the _Async_state_impl template.
01615   class __future_base::_Async_state_commonV2
01616     : public __future_base::_State_base
01617   {
01618   protected:
01619     ~_Async_state_commonV2() = default;
01620 
01621     // Make waiting functions block until the thread completes, as if joined.
01622     //
01623     // This function is used by wait() to satisfy the first requirement below
01624     // and by wait_for() / wait_until() to satisfy the second.
01625     //
01626     // [futures.async]:
01627     //
01628     // — a call to a waiting function on an asynchronous return object that
01629     // shares the shared state created by this async call shall block until
01630     // the associated thread has completed, as if joined, or else time out.
01631     //
01632     // — the associated thread completion synchronizes with the return from
01633     // the first function that successfully detects the ready status of the
01634     // shared state or with the return from the last function that releases
01635     // the shared state, whichever happens first.
01636     virtual void _M_complete_async() { _M_join(); }
01637 
01638     void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
01639 
01640     thread _M_thread;
01641     once_flag _M_once;
01642   };
01643 
01644   // Shared state created by std::async().
01645   // Starts a new thread that runs a function and makes the shared state ready.
01646   template<typename _BoundFn, typename _Res>
01647     class __future_base::_Async_state_impl final
01648     : public __future_base::_Async_state_commonV2
01649     {
01650     public:
01651       explicit
01652       _Async_state_impl(_BoundFn&& __fn)
01653       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
01654       {
01655         _M_thread = std::thread{ [this] {
01656             __try
01657               {
01658                 _M_set_result(_S_task_setter(_M_result, _M_fn));
01659               }
01660             __catch (const __cxxabiv1::__forced_unwind&)
01661               {
01662                 // make the shared state ready on thread cancellation
01663                 if (static_cast<bool>(_M_result))
01664                   this->_M_break_promise(std::move(_M_result));
01665                 __throw_exception_again;
01666               }
01667         } };
01668       }
01669 
01670       // Must not destroy _M_result and _M_fn until the thread finishes.
01671       // Call join() directly rather than through _M_join() because no other
01672       // thread can be referring to this state if it is being destroyed.
01673       ~_Async_state_impl() { if (_M_thread.joinable()) _M_thread.join(); }
01674 
01675     private:
01676       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
01677       _Ptr_type _M_result;
01678       _BoundFn _M_fn;
01679     };
01680 
01681   template<typename _BoundFn>
01682     inline std::shared_ptr<__future_base::_State_base>
01683     __future_base::_S_make_deferred_state(_BoundFn&& __fn)
01684     {
01685       typedef typename remove_reference<_BoundFn>::type __fn_type;
01686       typedef _Deferred_state<__fn_type> __state_type;
01687       return std::make_shared<__state_type>(std::move(__fn));
01688     }
01689 
01690   template<typename _BoundFn>
01691     inline std::shared_ptr<__future_base::_State_base>
01692     __future_base::_S_make_async_state(_BoundFn&& __fn)
01693     {
01694       typedef typename remove_reference<_BoundFn>::type __fn_type;
01695       typedef _Async_state_impl<__fn_type> __state_type;
01696       return std::make_shared<__state_type>(std::move(__fn));
01697     }
01698 
01699 
01700   /// async
01701   template<typename _Fn, typename... _Args>
01702     future<typename result_of<_Fn(_Args...)>::type>
01703     async(launch __policy, _Fn&& __fn, _Args&&... __args)
01704     {
01705       typedef typename result_of<_Fn(_Args...)>::type result_type;
01706       std::shared_ptr<__future_base::_State_base> __state;
01707       if ((__policy & (launch::async|launch::deferred)) == launch::async)
01708         {
01709           __state = __future_base::_S_make_async_state(std::__bind_simple(
01710               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
01711         }
01712       else
01713         {
01714           __state = __future_base::_S_make_deferred_state(std::__bind_simple(
01715               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
01716         }
01717       return future<result_type>(__state);
01718     }
01719 
01720   /// async, potential overload
01721   template<typename _Fn, typename... _Args>
01722     inline future<typename result_of<_Fn(_Args...)>::type>
01723     async(_Fn&& __fn, _Args&&... __args)
01724     {
01725       return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
01726                    std::forward<_Args>(__args)...);
01727     }
01728 
01729 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
01730 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
01731        // && ATOMIC_INT_LOCK_FREE
01732 
01733   // @} group futures
01734 _GLIBCXX_END_NAMESPACE_VERSION
01735 } // namespace
01736 
01737 #endif // C++11
01738 
01739 #endif // _GLIBCXX_FUTURE