Home | History | Annotate | Download | only in include
      1 // <future> -*- C++ -*-
      2 
      3 // Copyright (C) 2009-2014 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /** @file include/future
     26  *  This is a Standard C++ Library header.
     27  */
     28 
     29 #ifndef _GLIBCXX_FUTURE
     30 #define _GLIBCXX_FUTURE 1
     31 
     32 #pragma GCC system_header
     33 
     34 #if __cplusplus < 201103L
     35 # include <bits/c++0x_warning.h>
     36 #else
     37 
     38 #include <functional>
     39 #include <mutex>
     40 #include <thread>
     41 #include <condition_variable>
     42 #include <system_error>
     43 #include <atomic>
     44 #include <bits/functexcept.h>
     45 #include <bits/unique_ptr.h>
     46 #include <bits/shared_ptr.h>
     47 #include <bits/uses_allocator.h>
     48 #include <bits/alloc_traits.h>
     49 #include <ext/aligned_buffer.h>
     50 
     51 namespace std _GLIBCXX_VISIBILITY(default)
     52 {
     53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     54 
     55   /**
     56    * @defgroup futures Futures
     57    * @ingroup concurrency
     58    *
     59    * Classes for futures support.
     60    * @{
     61    */
     62 
     63   /// Error code for futures
     64   enum class future_errc
     65   {
     66     future_already_retrieved = 1,
     67     promise_already_satisfied,
     68     no_state,
     69     broken_promise
     70   };
     71 
     72   /// Specialization.
     73   template<>
     74     struct is_error_code_enum<future_errc> : public true_type { };
     75 
     76   /// Points to a statically-allocated object derived from error_category.
     77   const error_category&
     78   future_category() noexcept;
     79 
     80   /// Overload for make_error_code.
     81   inline error_code
     82   make_error_code(future_errc __errc) noexcept
     83   { return error_code(static_cast<int>(__errc), future_category()); }
     84 
     85   /// Overload for make_error_condition.
     86   inline error_condition
     87   make_error_condition(future_errc __errc) noexcept
     88   { return error_condition(static_cast<int>(__errc), future_category()); }
     89 
     90   /**
     91    *  @brief Exception type thrown by futures.
     92    *  @ingroup exceptions
     93    */
     94   class future_error : public logic_error
     95   {
     96     error_code 			_M_code;
     97 
     98   public:
     99     explicit future_error(error_code __ec)
    100     : logic_error("std::future_error"), _M_code(__ec)
    101     { }
    102 
    103     virtual ~future_error() noexcept;
    104 
    105     virtual const char*
    106     what() const noexcept;
    107 
    108     const error_code&
    109     code() const noexcept { return _M_code; }
    110   };
    111 
    112   // Forward declarations.
    113   template<typename _Res>
    114     class future;
    115 
    116   template<typename _Res>
    117     class shared_future;
    118 
    119   template<typename _Signature>
    120     class packaged_task;
    121 
    122   template<typename _Res>
    123     class promise;
    124 
    125   /// Launch code for futures
    126   enum class launch
    127   {
    128     async = 1,
    129     deferred = 2
    130   };
    131 
    132   constexpr launch operator&(launch __x, launch __y)
    133   {
    134     return static_cast<launch>(
    135 	static_cast<int>(__x) & static_cast<int>(__y));
    136   }
    137 
    138   constexpr launch operator|(launch __x, launch __y)
    139   {
    140     return static_cast<launch>(
    141 	static_cast<int>(__x) | static_cast<int>(__y));
    142   }
    143 
    144   constexpr launch operator^(launch __x, launch __y)
    145   {
    146     return static_cast<launch>(
    147 	static_cast<int>(__x) ^ static_cast<int>(__y));
    148   }
    149 
    150   constexpr launch operator~(launch __x)
    151   { return static_cast<launch>(~static_cast<int>(__x)); }
    152 
    153   inline launch& operator&=(launch& __x, launch __y)
    154   { return __x = __x & __y; }
    155 
    156   inline launch& operator|=(launch& __x, launch __y)
    157   { return __x = __x | __y; }
    158 
    159   inline launch& operator^=(launch& __x, launch __y)
    160   { return __x = __x ^ __y; }
    161 
    162   /// Status code for futures
    163   enum class future_status
    164   {
    165     ready,
    166     timeout,
    167     deferred
    168   };
    169 
    170   template<typename _Fn, typename... _Args>
    171     future<typename result_of<_Fn(_Args...)>::type>
    172     async(launch __policy, _Fn&& __fn, _Args&&... __args);
    173 
    174   template<typename _Fn, typename... _Args>
    175     future<typename result_of<_Fn(_Args...)>::type>
    176     async(_Fn&& __fn, _Args&&... __args);
    177 
    178 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
    179   && (ATOMIC_INT_LOCK_FREE > 1)
    180 
    181   /// Base class and enclosing scope.
    182   struct __future_base
    183   {
    184     /// Base class for results.
    185     struct _Result_base
    186     {
    187       exception_ptr		_M_error;
    188 
    189       _Result_base(const _Result_base&) = delete;
    190       _Result_base& operator=(const _Result_base&) = delete;
    191 
    192       // _M_destroy() allows derived classes to control deallocation
    193       virtual void _M_destroy() = 0;
    194 
    195       struct _Deleter
    196       {
    197 	void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
    198       };
    199 
    200     protected:
    201       _Result_base();
    202       virtual ~_Result_base();
    203     };
    204 
    205     /// Result.
    206     template<typename _Res>
    207       struct _Result : _Result_base
    208       {
    209       private:
    210 	__gnu_cxx::__aligned_buffer<_Res>	_M_storage;
    211 	bool 					_M_initialized;
    212 
    213       public:
    214 	typedef _Res result_type;
    215 
    216 	_Result() noexcept : _M_initialized() { }
    217 	
    218 	~_Result()
    219 	{
    220 	  if (_M_initialized)
    221 	    _M_value().~_Res();
    222 	}
    223 
    224 	// Return lvalue, future will add const or rvalue-reference
    225 	_Res&
    226 	_M_value() noexcept { return *_M_storage._M_ptr(); }
    227 
    228 	void
    229 	_M_set(const _Res& __res)
    230 	{
    231 	  ::new (_M_storage._M_addr()) _Res(__res);
    232 	  _M_initialized = true;
    233 	}
    234 
    235 	void
    236 	_M_set(_Res&& __res)
    237 	{
    238 	  ::new (_M_storage._M_addr()) _Res(std::move(__res));
    239 	  _M_initialized = true;
    240 	}
    241 
    242       private:
    243 	void _M_destroy() { delete this; }
    244     };
    245 
    246     /// A unique_ptr based on the instantiating type.
    247     template<typename _Res>
    248       using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
    249 
    250     /// Result_alloc.
    251     template<typename _Res, typename _Alloc>
    252       struct _Result_alloc final : _Result<_Res>, _Alloc
    253       {
    254         typedef typename allocator_traits<_Alloc>::template
    255           rebind_alloc<_Result_alloc> __allocator_type;
    256 
    257         explicit
    258 	_Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
    259         { }
    260 	
    261       private:
    262 	void _M_destroy()
    263         {
    264 	  typedef allocator_traits<__allocator_type> __traits;
    265           __allocator_type __a(*this);
    266 	  __traits::destroy(__a, this);
    267 	  __traits::deallocate(__a, this, 1);
    268         }
    269       };
    270 
    271     template<typename _Res, typename _Allocator>
    272       static _Ptr<_Result_alloc<_Res, _Allocator>>
    273       _S_allocate_result(const _Allocator& __a)
    274       {
    275         typedef _Result_alloc<_Res, _Allocator>	__result_type;
    276 	typedef allocator_traits<typename __result_type::__allocator_type>
    277 	  __traits;
    278         typename __traits::allocator_type __a2(__a);
    279         __result_type* __p = __traits::allocate(__a2, 1);
    280         __try
    281 	  {
    282 	    __traits::construct(__a2, __p, __a);
    283 	  }
    284         __catch(...)
    285 	  {
    286 	    __traits::deallocate(__a2, __p, 1);
    287 	    __throw_exception_again;
    288 	  }
    289         return _Ptr<__result_type>(__p);
    290       }
    291 
    292     template<typename _Res, typename _Tp>
    293       static _Ptr<_Result<_Res>>
    294       _S_allocate_result(const std::allocator<_Tp>& __a)
    295       {
    296 	return _Ptr<_Result<_Res>>(new _Result<_Res>);
    297       }
    298 
    299     /// Base class for state between a promise and one or more
    300     /// associated futures.
    301     class _State_baseV2
    302     {
    303       typedef _Ptr<_Result_base> _Ptr_type;
    304 
    305       _Ptr_type			_M_result;
    306       mutex               	_M_mutex;
    307       condition_variable  	_M_cond;
    308       atomic_flag         	_M_retrieved;
    309       once_flag			_M_once;
    310 
    311     public:
    312       _State_baseV2() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT)
    313 	{ }
    314       _State_baseV2(const _State_baseV2&) = delete;
    315       _State_baseV2& operator=(const _State_baseV2&) = delete;
    316       virtual ~_State_baseV2() = default;
    317 
    318       _Result_base&
    319       wait()
    320       {
    321 	_M_complete_async();
    322 	unique_lock<mutex> __lock(_M_mutex);
    323 	_M_cond.wait(__lock, [&] { return _M_ready(); });
    324 	return *_M_result;
    325       }
    326 
    327       template<typename _Rep, typename _Period>
    328         future_status
    329         wait_for(const chrono::duration<_Rep, _Period>& __rel)
    330         {
    331 	  unique_lock<mutex> __lock(_M_mutex);
    332 	  if (_M_ready())
    333 	    return future_status::ready;
    334 	  if (_M_has_deferred())
    335 	    return future_status::deferred;
    336 	  if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
    337 	    {
    338 	      // _GLIBCXX_RESOLVE_LIB_DEFECTS
    339 	      // 2100.  timed waiting functions must also join
    340 	      _M_complete_async();
    341 	      return future_status::ready;
    342 	    }
    343 	  return future_status::timeout;
    344 	}
    345 
    346       template<typename _Clock, typename _Duration>
    347         future_status
    348         wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
    349         {
    350 	  unique_lock<mutex> __lock(_M_mutex);
    351 	  if (_M_ready())
    352 	    return future_status::ready;
    353 	  if (_M_has_deferred())
    354 	    return future_status::deferred;
    355 	  if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
    356 	    {
    357 	      // _GLIBCXX_RESOLVE_LIB_DEFECTS
    358 	      // 2100.  timed waiting functions must also join
    359 	      _M_complete_async();
    360 	      return future_status::ready;
    361 	    }
    362 	  return future_status::timeout;
    363 	}
    364 
    365       void
    366       _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
    367       {
    368         bool __set = __ignore_failure;
    369         // all calls to this function are serialized,
    370         // side-effects of invoking __res only happen once
    371         call_once(_M_once, &_State_baseV2::_M_do_set, this, ref(__res),
    372             ref(__set));
    373         if (!__set)
    374           __throw_future_error(int(future_errc::promise_already_satisfied));
    375       }
    376 
    377       void
    378       _M_break_promise(_Ptr_type __res)
    379       {
    380 	if (static_cast<bool>(__res))
    381 	  {
    382 	    error_code __ec(make_error_code(future_errc::broken_promise));
    383 	    __res->_M_error = make_exception_ptr(future_error(__ec));
    384 	    {
    385 	      lock_guard<mutex> __lock(_M_mutex);
    386 	      _M_result.swap(__res);
    387 	    }
    388 	    _M_cond.notify_all();
    389 	  }
    390       }
    391 
    392       // Called when this object is passed to a future.
    393       void
    394       _M_set_retrieved_flag()
    395       {
    396 	if (_M_retrieved.test_and_set())
    397 	  __throw_future_error(int(future_errc::future_already_retrieved));
    398       }
    399 
    400       template<typename _Res, typename _Arg>
    401         struct _Setter;
    402 
    403       // set lvalues
    404       template<typename _Res, typename _Arg>
    405         struct _Setter<_Res, _Arg&>
    406         {
    407           // check this is only used by promise<R>::set_value(const R&)
    408           // or promise<R>::set_value(R&)
    409           static_assert(is_same<_Res, _Arg&>::value  // promise<R&>
    410               || is_same<const _Res, _Arg>::value,  // promise<R>
    411               "Invalid specialisation");
    412 
    413           typename promise<_Res>::_Ptr_type operator()()
    414           {
    415             _State_baseV2::_S_check(_M_promise->_M_future);
    416             _M_promise->_M_storage->_M_set(_M_arg);
    417             return std::move(_M_promise->_M_storage);
    418           }
    419           promise<_Res>*    _M_promise;
    420           _Arg&             _M_arg;
    421         };
    422 
    423       // set rvalues
    424       template<typename _Res>
    425         struct _Setter<_Res, _Res&&>
    426         {
    427           typename promise<_Res>::_Ptr_type operator()()
    428           {
    429             _State_baseV2::_S_check(_M_promise->_M_future);
    430             _M_promise->_M_storage->_M_set(std::move(_M_arg));
    431             return std::move(_M_promise->_M_storage);
    432           }
    433           promise<_Res>*    _M_promise;
    434           _Res&             _M_arg;
    435         };
    436 
    437       struct __exception_ptr_tag { };
    438 
    439       // set exceptions
    440       template<typename _Res>
    441         struct _Setter<_Res, __exception_ptr_tag>
    442         {
    443           typename promise<_Res>::_Ptr_type operator()()
    444           {
    445             _State_baseV2::_S_check(_M_promise->_M_future);
    446             _M_promise->_M_storage->_M_error = _M_ex;
    447             return std::move(_M_promise->_M_storage);
    448           }
    449 
    450           promise<_Res>*   _M_promise;
    451           exception_ptr&    _M_ex;
    452         };
    453 
    454       template<typename _Res, typename _Arg>
    455         static _Setter<_Res, _Arg&&>
    456         __setter(promise<_Res>* __prom, _Arg&& __arg)
    457         {
    458           return _Setter<_Res, _Arg&&>{ __prom, __arg };
    459         }
    460 
    461       template<typename _Res>
    462         static _Setter<_Res, __exception_ptr_tag>
    463         __setter(exception_ptr& __ex, promise<_Res>* __prom)
    464         {
    465           return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
    466         }
    467 
    468       static _Setter<void, void>
    469       __setter(promise<void>* __prom);
    470 
    471       template<typename _Tp>
    472         static void
    473         _S_check(const shared_ptr<_Tp>& __p)
    474         {
    475           if (!static_cast<bool>(__p))
    476             __throw_future_error((int)future_errc::no_state);
    477         }
    478 
    479     private:
    480       void
    481       _M_do_set(function<_Ptr_type()>& __f, bool& __set)
    482       {
    483         _Ptr_type __res = __f();
    484         {
    485           lock_guard<mutex> __lock(_M_mutex);
    486           _M_result.swap(__res);
    487         }
    488         _M_cond.notify_all();
    489         __set = true;
    490       }
    491 
    492       bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
    493 
    494       // Wait for completion of async function.
    495       virtual void _M_complete_async() { }
    496 
    497       // Return true if state contains a deferred function.
    498       virtual bool _M_has_deferred() const { return false; }
    499     };
    500 
    501 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
    502     class _State_base;
    503     class _Async_state_common;
    504 #else
    505     using _State_base = _State_baseV2;
    506     class _Async_state_commonV2;
    507 #endif
    508 
    509     template<typename _BoundFn, typename = typename _BoundFn::result_type>
    510       class _Deferred_state;
    511 
    512     template<typename _BoundFn, typename = typename _BoundFn::result_type>
    513       class _Async_state_impl;
    514 
    515     template<typename _Signature>
    516       class _Task_state_base;
    517 
    518     template<typename _Fn, typename _Alloc, typename _Signature>
    519       class _Task_state;
    520 
    521     template<typename _BoundFn>
    522       static std::shared_ptr<_State_base>
    523       _S_make_deferred_state(_BoundFn&& __fn);
    524 
    525     template<typename _BoundFn>
    526       static std::shared_ptr<_State_base>
    527       _S_make_async_state(_BoundFn&& __fn);
    528 
    529     template<typename _Res_ptr,
    530 	     typename _Res = typename _Res_ptr::element_type::result_type>
    531       struct _Task_setter;
    532 
    533     template<typename _Res_ptr, typename _BoundFn>
    534       static _Task_setter<_Res_ptr>
    535       _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
    536       {
    537 	return _Task_setter<_Res_ptr>{ __ptr, std::ref(__call) };
    538       }
    539   };
    540 
    541   /// Partial specialization for reference types.
    542   template<typename _Res>
    543     struct __future_base::_Result<_Res&> : __future_base::_Result_base
    544     {
    545       typedef _Res& result_type;
    546 
    547       _Result() noexcept : _M_value_ptr() { }
    548 
    549       void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
    550 
    551       _Res& _M_get() noexcept { return *_M_value_ptr; }
    552 
    553     private:
    554       _Res* 			_M_value_ptr;
    555 
    556       void _M_destroy() { delete this; }
    557     };
    558 
    559   /// Explicit specialization for void.
    560   template<>
    561     struct __future_base::_Result<void> : __future_base::_Result_base
    562     {
    563       typedef void result_type;
    564 
    565     private:
    566       void _M_destroy() { delete this; }
    567     };
    568 
    569 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
    570 
    571   /// Common implementation for future and shared_future.
    572   template<typename _Res>
    573     class __basic_future : public __future_base
    574     {
    575     protected:
    576       typedef shared_ptr<_State_base>		__state_type;
    577       typedef __future_base::_Result<_Res>&	__result_type;
    578 
    579     private:
    580       __state_type 		_M_state;
    581 
    582     public:
    583       // Disable copying.
    584       __basic_future(const __basic_future&) = delete;
    585       __basic_future& operator=(const __basic_future&) = delete;
    586 
    587       bool
    588       valid() const noexcept { return static_cast<bool>(_M_state); }
    589 
    590       void
    591       wait() const
    592       {
    593         _State_base::_S_check(_M_state);
    594         _M_state->wait();
    595       }
    596 
    597       template<typename _Rep, typename _Period>
    598         future_status
    599         wait_for(const chrono::duration<_Rep, _Period>& __rel) const
    600         {
    601           _State_base::_S_check(_M_state);
    602           return _M_state->wait_for(__rel);
    603         }
    604 
    605       template<typename _Clock, typename _Duration>
    606         future_status
    607         wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
    608         {
    609           _State_base::_S_check(_M_state);
    610           return _M_state->wait_until(__abs);
    611         }
    612 
    613     protected:
    614       /// Wait for the state to be ready and rethrow any stored exception
    615       __result_type
    616       _M_get_result() const
    617       {
    618         _State_base::_S_check(_M_state);
    619         _Result_base& __res = _M_state->wait();
    620         if (!(__res._M_error == 0))
    621           rethrow_exception(__res._M_error);
    622         return static_cast<__result_type>(__res);
    623       }
    624 
    625       void _M_swap(__basic_future& __that) noexcept
    626       {
    627         _M_state.swap(__that._M_state);
    628       }
    629 
    630       // Construction of a future by promise::get_future()
    631       explicit
    632       __basic_future(const __state_type& __state) : _M_state(__state)
    633       {
    634         _State_base::_S_check(_M_state);
    635         _M_state->_M_set_retrieved_flag();
    636       }
    637 
    638       // Copy construction from a shared_future
    639       explicit
    640       __basic_future(const shared_future<_Res>&) noexcept;
    641 
    642       // Move construction from a shared_future
    643       explicit
    644       __basic_future(shared_future<_Res>&&) noexcept;
    645 
    646       // Move construction from a future
    647       explicit
    648       __basic_future(future<_Res>&&) noexcept;
    649 
    650       constexpr __basic_future() noexcept : _M_state() { }
    651 
    652       struct _Reset
    653       {
    654         explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
    655         ~_Reset() { _M_fut._M_state.reset(); }
    656         __basic_future& _M_fut;
    657       };
    658     };
    659 
    660 
    661   /// Primary template for future.
    662   template<typename _Res>
    663     class future : public __basic_future<_Res>
    664     {
    665       friend class promise<_Res>;
    666       template<typename> friend class packaged_task;
    667       template<typename _Fn, typename... _Args>
    668         friend future<typename result_of<_Fn(_Args...)>::type>
    669         async(launch, _Fn&&, _Args&&...);
    670 
    671       typedef __basic_future<_Res> _Base_type;
    672       typedef typename _Base_type::__state_type __state_type;
    673 
    674       explicit
    675       future(const __state_type& __state) : _Base_type(__state) { }
    676 
    677     public:
    678       constexpr future() noexcept : _Base_type() { }
    679 
    680       /// Move constructor
    681       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
    682 
    683       // Disable copying
    684       future(const future&) = delete;
    685       future& operator=(const future&) = delete;
    686 
    687       future& operator=(future&& __fut) noexcept
    688       {
    689         future(std::move(__fut))._M_swap(*this);
    690         return *this;
    691       }
    692 
    693       /// Retrieving the value
    694       _Res
    695       get()
    696       {
    697         typename _Base_type::_Reset __reset(*this);
    698         return std::move(this->_M_get_result()._M_value());
    699       }
    700 
    701       shared_future<_Res> share();
    702     };
    703 
    704   /// Partial specialization for future<R&>
    705   template<typename _Res>
    706     class future<_Res&> : public __basic_future<_Res&>
    707     {
    708       friend class promise<_Res&>;
    709       template<typename> friend class packaged_task;
    710       template<typename _Fn, typename... _Args>
    711         friend future<typename result_of<_Fn(_Args...)>::type>
    712         async(launch, _Fn&&, _Args&&...);
    713 
    714       typedef __basic_future<_Res&> _Base_type;
    715       typedef typename _Base_type::__state_type __state_type;
    716 
    717       explicit
    718       future(const __state_type& __state) : _Base_type(__state) { }
    719 
    720     public:
    721       constexpr future() noexcept : _Base_type() { }
    722 
    723       /// Move constructor
    724       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
    725 
    726       // Disable copying
    727       future(const future&) = delete;
    728       future& operator=(const future&) = delete;
    729 
    730       future& operator=(future&& __fut) noexcept
    731       {
    732         future(std::move(__fut))._M_swap(*this);
    733         return *this;
    734       }
    735 
    736       /// Retrieving the value
    737       _Res&
    738       get()
    739       {
    740         typename _Base_type::_Reset __reset(*this);
    741         return this->_M_get_result()._M_get();
    742       }
    743 
    744       shared_future<_Res&> share();
    745     };
    746 
    747   /// Explicit specialization for future<void>
    748   template<>
    749     class future<void> : public __basic_future<void>
    750     {
    751       friend class promise<void>;
    752       template<typename> friend class packaged_task;
    753       template<typename _Fn, typename... _Args>
    754         friend future<typename result_of<_Fn(_Args...)>::type>
    755         async(launch, _Fn&&, _Args&&...);
    756 
    757       typedef __basic_future<void> _Base_type;
    758       typedef typename _Base_type::__state_type __state_type;
    759 
    760       explicit
    761       future(const __state_type& __state) : _Base_type(__state) { }
    762 
    763     public:
    764       constexpr future() noexcept : _Base_type() { }
    765 
    766       /// Move constructor
    767       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
    768 
    769       // Disable copying
    770       future(const future&) = delete;
    771       future& operator=(const future&) = delete;
    772 
    773       future& operator=(future&& __fut) noexcept
    774       {
    775         future(std::move(__fut))._M_swap(*this);
    776         return *this;
    777       }
    778 
    779       /// Retrieving the value
    780       void
    781       get()
    782       {
    783         typename _Base_type::_Reset __reset(*this);
    784         this->_M_get_result();
    785       }
    786 
    787       shared_future<void> share();
    788     };
    789 
    790 
    791   /// Primary template for shared_future.
    792   template<typename _Res>
    793     class shared_future : public __basic_future<_Res>
    794     {
    795       typedef __basic_future<_Res> _Base_type;
    796 
    797     public:
    798       constexpr shared_future() noexcept : _Base_type() { }
    799 
    800       /// Copy constructor
    801       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
    802 
    803       /// Construct from a future rvalue
    804       shared_future(future<_Res>&& __uf) noexcept
    805       : _Base_type(std::move(__uf))
    806       { }
    807 
    808       /// Construct from a shared_future rvalue
    809       shared_future(shared_future&& __sf) noexcept
    810       : _Base_type(std::move(__sf))
    811       { }
    812 
    813       shared_future& operator=(const shared_future& __sf)
    814       {
    815         shared_future(__sf)._M_swap(*this);
    816         return *this;
    817       }
    818 
    819       shared_future& operator=(shared_future&& __sf) noexcept
    820       {
    821         shared_future(std::move(__sf))._M_swap(*this);
    822         return *this;
    823       }
    824 
    825       /// Retrieving the value
    826       const _Res&
    827       get() const { return this->_M_get_result()._M_value(); }
    828     };
    829 
    830   /// Partial specialization for shared_future<R&>
    831   template<typename _Res>
    832     class shared_future<_Res&> : public __basic_future<_Res&>
    833     {
    834       typedef __basic_future<_Res&>           _Base_type;
    835 
    836     public:
    837       constexpr shared_future() noexcept : _Base_type() { }
    838 
    839       /// Copy constructor
    840       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
    841 
    842       /// Construct from a future rvalue
    843       shared_future(future<_Res&>&& __uf) noexcept
    844       : _Base_type(std::move(__uf))
    845       { }
    846 
    847       /// Construct from a shared_future rvalue
    848       shared_future(shared_future&& __sf) noexcept
    849       : _Base_type(std::move(__sf))
    850       { }
    851 
    852       shared_future& operator=(const shared_future& __sf)
    853       {
    854         shared_future(__sf)._M_swap(*this);
    855         return *this;
    856       }
    857 
    858       shared_future& operator=(shared_future&& __sf) noexcept
    859       {
    860         shared_future(std::move(__sf))._M_swap(*this);
    861         return *this;
    862       }
    863 
    864       /// Retrieving the value
    865       _Res&
    866       get() const { return this->_M_get_result()._M_get(); }
    867     };
    868 
    869   /// Explicit specialization for shared_future<void>
    870   template<>
    871     class shared_future<void> : public __basic_future<void>
    872     {
    873       typedef __basic_future<void> _Base_type;
    874 
    875     public:
    876       constexpr shared_future() noexcept : _Base_type() { }
    877 
    878       /// Copy constructor
    879       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
    880 
    881       /// Construct from a future rvalue
    882       shared_future(future<void>&& __uf) noexcept
    883       : _Base_type(std::move(__uf))
    884       { }
    885 
    886       /// Construct from a shared_future rvalue
    887       shared_future(shared_future&& __sf) noexcept
    888       : _Base_type(std::move(__sf))
    889       { }
    890 
    891       shared_future& operator=(const shared_future& __sf)
    892       {
    893         shared_future(__sf)._M_swap(*this);
    894         return *this;
    895       }
    896 
    897       shared_future& operator=(shared_future&& __sf) noexcept
    898       {
    899         shared_future(std::move(__sf))._M_swap(*this);
    900         return *this;
    901       }
    902 
    903       // Retrieving the value
    904       void
    905       get() const { this->_M_get_result(); }
    906     };
    907 
    908   // Now we can define the protected __basic_future constructors.
    909   template<typename _Res>
    910     inline __basic_future<_Res>::
    911     __basic_future(const shared_future<_Res>& __sf) noexcept
    912     : _M_state(__sf._M_state)
    913     { }
    914 
    915   template<typename _Res>
    916     inline __basic_future<_Res>::
    917     __basic_future(shared_future<_Res>&& __sf) noexcept
    918     : _M_state(std::move(__sf._M_state))
    919     { }
    920 
    921   template<typename _Res>
    922     inline __basic_future<_Res>::
    923     __basic_future(future<_Res>&& __uf) noexcept
    924     : _M_state(std::move(__uf._M_state))
    925     { }
    926 
    927   template<typename _Res>
    928     inline shared_future<_Res>
    929     future<_Res>::share()
    930     { return shared_future<_Res>(std::move(*this)); }
    931 
    932   template<typename _Res>
    933     inline shared_future<_Res&>
    934     future<_Res&>::share()
    935     { return shared_future<_Res&>(std::move(*this)); }
    936 
    937   inline shared_future<void>
    938   future<void>::share()
    939   { return shared_future<void>(std::move(*this)); }
    940 
    941   /// Primary template for promise
    942   template<typename _Res>
    943     class promise
    944     {
    945       typedef __future_base::_State_base 	_State;
    946       typedef __future_base::_Result<_Res>	_Res_type;
    947       typedef __future_base::_Ptr<_Res_type>	_Ptr_type;
    948       template<typename, typename> friend class _State::_Setter;
    949 
    950       shared_ptr<_State>                        _M_future;
    951       _Ptr_type                                 _M_storage;
    952 
    953     public:
    954       promise()
    955       : _M_future(std::make_shared<_State>()),
    956 	_M_storage(new _Res_type())
    957       { }
    958 
    959       promise(promise&& __rhs) noexcept
    960       : _M_future(std::move(__rhs._M_future)),
    961 	_M_storage(std::move(__rhs._M_storage))
    962       { }
    963 
    964       template<typename _Allocator>
    965         promise(allocator_arg_t, const _Allocator& __a)
    966         : _M_future(std::allocate_shared<_State>(__a)),
    967 	  _M_storage(__future_base::_S_allocate_result<_Res>(__a))
    968         { }
    969 
    970       template<typename _Allocator>
    971         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
    972         : _M_future(std::move(__rhs._M_future)),
    973 	  _M_storage(std::move(__rhs._M_storage))
    974         { }
    975 
    976       promise(const promise&) = delete;
    977 
    978       ~promise()
    979       {
    980         if (static_cast<bool>(_M_future) && !_M_future.unique())
    981           _M_future->_M_break_promise(std::move(_M_storage));
    982       }
    983 
    984       // Assignment
    985       promise&
    986       operator=(promise&& __rhs) noexcept
    987       {
    988         promise(std::move(__rhs)).swap(*this);
    989         return *this;
    990       }
    991 
    992       promise& operator=(const promise&) = delete;
    993 
    994       void
    995       swap(promise& __rhs) noexcept
    996       {
    997         _M_future.swap(__rhs._M_future);
    998         _M_storage.swap(__rhs._M_storage);
    999       }
   1000 
   1001       // Retrieving the result
   1002       future<_Res>
   1003       get_future()
   1004       { return future<_Res>(_M_future); }
   1005 
   1006       // Setting the result
   1007       void
   1008       set_value(const _Res& __r)
   1009       {
   1010         auto __setter = _State::__setter(this, __r);
   1011         _M_future->_M_set_result(std::move(__setter));
   1012       }
   1013 
   1014       void
   1015       set_value(_Res&& __r)
   1016       {
   1017         auto __setter = _State::__setter(this, std::move(__r));
   1018         _M_future->_M_set_result(std::move(__setter));
   1019       }
   1020 
   1021       void
   1022       set_exception(exception_ptr __p)
   1023       {
   1024         auto __setter = _State::__setter(__p, this);
   1025         _M_future->_M_set_result(std::move(__setter));
   1026       }
   1027     };
   1028 
   1029   template<typename _Res>
   1030     inline void
   1031     swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
   1032     { __x.swap(__y); }
   1033 
   1034   template<typename _Res, typename _Alloc>
   1035     struct uses_allocator<promise<_Res>, _Alloc>
   1036     : public true_type { };
   1037 
   1038 
   1039   /// Partial specialization for promise<R&>
   1040   template<typename _Res>
   1041     class promise<_Res&>
   1042     {
   1043       typedef __future_base::_State_base	_State;
   1044       typedef __future_base::_Result<_Res&>	_Res_type;
   1045       typedef __future_base::_Ptr<_Res_type> 	_Ptr_type;
   1046       template<typename, typename> friend class _State::_Setter;
   1047 
   1048       shared_ptr<_State>                        _M_future;
   1049       _Ptr_type                                 _M_storage;
   1050 
   1051     public:
   1052       promise()
   1053       : _M_future(std::make_shared<_State>()),
   1054 	_M_storage(new _Res_type())
   1055       { }
   1056 
   1057       promise(promise&& __rhs) noexcept
   1058       : _M_future(std::move(__rhs._M_future)),
   1059 	_M_storage(std::move(__rhs._M_storage))
   1060       { }
   1061 
   1062       template<typename _Allocator>
   1063         promise(allocator_arg_t, const _Allocator& __a)
   1064         : _M_future(std::allocate_shared<_State>(__a)),
   1065 	  _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
   1066         { }
   1067 
   1068       template<typename _Allocator>
   1069         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
   1070         : _M_future(std::move(__rhs._M_future)),
   1071 	  _M_storage(std::move(__rhs._M_storage))
   1072         { }
   1073 
   1074       promise(const promise&) = delete;
   1075 
   1076       ~promise()
   1077       {
   1078         if (static_cast<bool>(_M_future) && !_M_future.unique())
   1079           _M_future->_M_break_promise(std::move(_M_storage));
   1080       }
   1081 
   1082       // Assignment
   1083       promise&
   1084       operator=(promise&& __rhs) noexcept
   1085       {
   1086         promise(std::move(__rhs)).swap(*this);
   1087         return *this;
   1088       }
   1089 
   1090       promise& operator=(const promise&) = delete;
   1091 
   1092       void
   1093       swap(promise& __rhs) noexcept
   1094       {
   1095         _M_future.swap(__rhs._M_future);
   1096         _M_storage.swap(__rhs._M_storage);
   1097       }
   1098 
   1099       // Retrieving the result
   1100       future<_Res&>
   1101       get_future()
   1102       { return future<_Res&>(_M_future); }
   1103 
   1104       // Setting the result
   1105       void
   1106       set_value(_Res& __r)
   1107       {
   1108         auto __setter = _State::__setter(this, __r);
   1109         _M_future->_M_set_result(std::move(__setter));
   1110       }
   1111 
   1112       void
   1113       set_exception(exception_ptr __p)
   1114       {
   1115         auto __setter = _State::__setter(__p, this);
   1116         _M_future->_M_set_result(std::move(__setter));
   1117       }
   1118     };
   1119 
   1120   /// Explicit specialization for promise<void>
   1121   template<>
   1122     class promise<void>
   1123     {
   1124       typedef __future_base::_State_base	_State;
   1125       typedef __future_base::_Result<void>	_Res_type;
   1126       typedef __future_base::_Ptr<_Res_type> 	_Ptr_type;
   1127       template<typename, typename> friend class _State::_Setter;
   1128 
   1129       shared_ptr<_State>                        _M_future;
   1130       _Ptr_type                                 _M_storage;
   1131 
   1132     public:
   1133       promise()
   1134       : _M_future(std::make_shared<_State>()),
   1135 	_M_storage(new _Res_type())
   1136       { }
   1137 
   1138       promise(promise&& __rhs) noexcept
   1139       : _M_future(std::move(__rhs._M_future)),
   1140 	_M_storage(std::move(__rhs._M_storage))
   1141       { }
   1142 
   1143       template<typename _Allocator>
   1144         promise(allocator_arg_t, const _Allocator& __a)
   1145         : _M_future(std::allocate_shared<_State>(__a)),
   1146 	  _M_storage(__future_base::_S_allocate_result<void>(__a))
   1147         { }
   1148 
   1149       // _GLIBCXX_RESOLVE_LIB_DEFECTS
   1150       // 2095.  missing constructors needed for uses-allocator construction
   1151       template<typename _Allocator>
   1152         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
   1153         : _M_future(std::move(__rhs._M_future)),
   1154 	  _M_storage(std::move(__rhs._M_storage))
   1155         { }
   1156 
   1157       promise(const promise&) = delete;
   1158 
   1159       ~promise()
   1160       {
   1161         if (static_cast<bool>(_M_future) && !_M_future.unique())
   1162           _M_future->_M_break_promise(std::move(_M_storage));
   1163       }
   1164 
   1165       // Assignment
   1166       promise&
   1167       operator=(promise&& __rhs) noexcept
   1168       {
   1169         promise(std::move(__rhs)).swap(*this);
   1170         return *this;
   1171       }
   1172 
   1173       promise& operator=(const promise&) = delete;
   1174 
   1175       void
   1176       swap(promise& __rhs) noexcept
   1177       {
   1178         _M_future.swap(__rhs._M_future);
   1179         _M_storage.swap(__rhs._M_storage);
   1180       }
   1181 
   1182       // Retrieving the result
   1183       future<void>
   1184       get_future()
   1185       { return future<void>(_M_future); }
   1186 
   1187       // Setting the result
   1188       void set_value();
   1189 
   1190       void
   1191       set_exception(exception_ptr __p)
   1192       {
   1193         auto __setter = _State::__setter(__p, this);
   1194         _M_future->_M_set_result(std::move(__setter));
   1195       }
   1196     };
   1197 
   1198   // set void
   1199   template<>
   1200     struct __future_base::_State_base::_Setter<void, void>
   1201     {
   1202       promise<void>::_Ptr_type operator()()
   1203       {
   1204         _State_base::_S_check(_M_promise->_M_future);
   1205         return std::move(_M_promise->_M_storage);
   1206       }
   1207 
   1208       promise<void>*    _M_promise;
   1209     };
   1210 
   1211   inline __future_base::_State_base::_Setter<void, void>
   1212   __future_base::_State_base::__setter(promise<void>* __prom)
   1213   {
   1214     return _Setter<void, void>{ __prom };
   1215   }
   1216 
   1217   inline void
   1218   promise<void>::set_value()
   1219   {
   1220     auto __setter = _State::__setter(this);
   1221     _M_future->_M_set_result(std::move(__setter));
   1222   }
   1223 
   1224 
   1225   template<typename _Ptr_type, typename _Res>
   1226     struct __future_base::_Task_setter
   1227     {
   1228       _Ptr_type operator()()
   1229       {
   1230 	__try
   1231 	  {
   1232 	    _M_result->_M_set(_M_fn());
   1233 	  }
   1234 	__catch(...)
   1235 	  {
   1236 	    _M_result->_M_error = current_exception();
   1237 	  }
   1238 	return std::move(_M_result);
   1239       }
   1240       _Ptr_type&                _M_result;
   1241       std::function<_Res()>     _M_fn;
   1242     };
   1243 
   1244   template<typename _Ptr_type>
   1245     struct __future_base::_Task_setter<_Ptr_type, void>
   1246     {
   1247       _Ptr_type operator()()
   1248       {
   1249 	__try
   1250 	  {
   1251 	    _M_fn();
   1252 	  }
   1253 	__catch(...)
   1254 	  {
   1255 	    _M_result->_M_error = current_exception();
   1256 	  }
   1257 	return std::move(_M_result);
   1258       }
   1259       _Ptr_type&                _M_result;
   1260       std::function<void()>     _M_fn;
   1261     };
   1262 
   1263   template<typename _Res, typename... _Args>
   1264     struct __future_base::_Task_state_base<_Res(_Args...)>
   1265     : __future_base::_State_base
   1266     {
   1267       typedef _Res _Res_type;
   1268 
   1269       template<typename _Alloc>
   1270 	_Task_state_base(const _Alloc& __a)
   1271 	: _M_result(_S_allocate_result<_Res>(__a))
   1272 	{ }
   1273 
   1274       virtual void
   1275       _M_run(_Args... __args) = 0;
   1276 
   1277       virtual shared_ptr<_Task_state_base>
   1278       _M_reset() = 0;
   1279 
   1280       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
   1281       _Ptr_type _M_result;
   1282     };
   1283 
   1284   template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
   1285     struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
   1286     : __future_base::_Task_state_base<_Res(_Args...)>
   1287     {
   1288       template<typename _Fn2>
   1289 	_Task_state(_Fn2&& __fn, const _Alloc& __a)
   1290 	: _Task_state_base<_Res(_Args...)>(__a),
   1291 	  _M_impl(std::forward<_Fn2>(__fn), __a)
   1292 	{ }
   1293 
   1294     private:
   1295       virtual void
   1296       _M_run(_Args... __args)
   1297       {
   1298 	// bound arguments decay so wrap lvalue references
   1299 	auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
   1300 	    _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
   1301 	auto __setter = _S_task_setter(this->_M_result, std::move(__boundfn));
   1302 	this->_M_set_result(std::move(__setter));
   1303       }
   1304 
   1305       virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
   1306       _M_reset();
   1307 
   1308       template<typename _Tp>
   1309 	static reference_wrapper<_Tp>
   1310 	_S_maybe_wrap_ref(_Tp& __t)
   1311 	{ return std::ref(__t); }
   1312 
   1313       template<typename _Tp>
   1314 	static
   1315 	typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
   1316 	_S_maybe_wrap_ref(_Tp&& __t)
   1317 	{ return std::forward<_Tp>(__t); }
   1318 
   1319       struct _Impl : _Alloc
   1320       {
   1321 	template<typename _Fn2>
   1322 	  _Impl(_Fn2&& __fn, const _Alloc& __a)
   1323 	  : _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { }
   1324 	_Fn _M_fn;
   1325       } _M_impl;
   1326     };
   1327 
   1328   template<typename _Signature, typename _Fn, typename _Alloc>
   1329     static shared_ptr<__future_base::_Task_state_base<_Signature>>
   1330     __create_task_state(_Fn&& __fn, const _Alloc& __a)
   1331     {
   1332       typedef typename decay<_Fn>::type _Fn2;
   1333       typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
   1334       return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
   1335     }
   1336 
   1337   template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
   1338     shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
   1339     __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
   1340     {
   1341       return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
   1342 						 static_cast<_Alloc&>(_M_impl));
   1343     }
   1344 
   1345   template<typename _Task, typename _Fn, bool
   1346 	   = is_same<_Task, typename decay<_Fn>::type>::value>
   1347     struct __constrain_pkgdtask
   1348     { typedef void __type; };
   1349 
   1350   template<typename _Task, typename _Fn>
   1351     struct __constrain_pkgdtask<_Task, _Fn, true>
   1352     { };
   1353 
   1354   /// packaged_task
   1355   template<typename _Res, typename... _ArgTypes>
   1356     class packaged_task<_Res(_ArgTypes...)>
   1357     {
   1358       typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
   1359       shared_ptr<_State_type>                   _M_state;
   1360 
   1361     public:
   1362       // Construction and destruction
   1363       packaged_task() noexcept { }
   1364 
   1365       // _GLIBCXX_RESOLVE_LIB_DEFECTS
   1366       // 2095.  missing constructors needed for uses-allocator construction
   1367       template<typename _Allocator>
   1368 	packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
   1369 	{ }
   1370 
   1371       template<typename _Fn, typename = typename
   1372 	       __constrain_pkgdtask<packaged_task, _Fn>::__type>
   1373 	explicit
   1374 	packaged_task(_Fn&& __fn)
   1375 	: packaged_task(allocator_arg, std::allocator<int>(),
   1376 			std::forward<_Fn>(__fn))
   1377 	{ }
   1378 
   1379       // _GLIBCXX_RESOLVE_LIB_DEFECTS
   1380       // 2097.  packaged_task constructors should be constrained
   1381       template<typename _Fn, typename _Alloc, typename = typename
   1382 	       __constrain_pkgdtask<packaged_task, _Fn>::__type>
   1383 	explicit
   1384 	packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
   1385 	: _M_state(__create_task_state<_Res(_ArgTypes...)>(
   1386 		    std::forward<_Fn>(__fn), __a))
   1387 	{ }
   1388 
   1389       ~packaged_task()
   1390       {
   1391         if (static_cast<bool>(_M_state) && !_M_state.unique())
   1392 	  _M_state->_M_break_promise(std::move(_M_state->_M_result));
   1393       }
   1394 
   1395       // No copy
   1396       packaged_task(const packaged_task&) = delete;
   1397       packaged_task& operator=(const packaged_task&) = delete;
   1398 
   1399       template<typename _Allocator>
   1400 	packaged_task(allocator_arg_t, const _Allocator&,
   1401 		      const packaged_task&) = delete;
   1402 
   1403       // Move support
   1404       packaged_task(packaged_task&& __other) noexcept
   1405       { this->swap(__other); }
   1406 
   1407       template<typename _Allocator>
   1408 	packaged_task(allocator_arg_t, const _Allocator&,
   1409 		      packaged_task&& __other) noexcept
   1410 	{ this->swap(__other); }
   1411 
   1412       packaged_task& operator=(packaged_task&& __other) noexcept
   1413       {
   1414 	packaged_task(std::move(__other)).swap(*this);
   1415 	return *this;
   1416       }
   1417 
   1418       void
   1419       swap(packaged_task& __other) noexcept
   1420       { _M_state.swap(__other._M_state); }
   1421 
   1422       bool
   1423       valid() const noexcept
   1424       { return static_cast<bool>(_M_state); }
   1425 
   1426       // Result retrieval
   1427       future<_Res>
   1428       get_future()
   1429       { return future<_Res>(_M_state); }
   1430 
   1431       // Execution
   1432       void
   1433       operator()(_ArgTypes... __args)
   1434       {
   1435 	__future_base::_State_base::_S_check(_M_state);
   1436 	_M_state->_M_run(std::forward<_ArgTypes>(__args)...);
   1437       }
   1438 
   1439       void
   1440       reset()
   1441       {
   1442 	__future_base::_State_base::_S_check(_M_state);
   1443 	packaged_task __tmp;
   1444 	__tmp._M_state = _M_state;
   1445 	_M_state = _M_state->_M_reset();
   1446       }
   1447     };
   1448 
   1449   /// swap
   1450   template<typename _Res, typename... _ArgTypes>
   1451     inline void
   1452     swap(packaged_task<_Res(_ArgTypes...)>& __x,
   1453 	 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
   1454     { __x.swap(__y); }
   1455 
   1456   template<typename _Res, typename _Alloc>
   1457     struct uses_allocator<packaged_task<_Res>, _Alloc>
   1458     : public true_type { };
   1459 
   1460 
   1461   template<typename _BoundFn, typename _Res>
   1462     class __future_base::_Deferred_state final
   1463     : public __future_base::_State_base
   1464     {
   1465     public:
   1466       explicit
   1467       _Deferred_state(_BoundFn&& __fn)
   1468       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
   1469       { }
   1470 
   1471     private:
   1472       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
   1473       _Ptr_type _M_result;
   1474       _BoundFn _M_fn;
   1475 
   1476       // Run the deferred function.
   1477       virtual void
   1478       _M_complete_async()
   1479       {
   1480         // safe to call multiple times so ignore failure
   1481         _M_set_result(_S_task_setter(_M_result, _M_fn), true);
   1482       }
   1483 
   1484       virtual bool
   1485       _M_has_deferred() const { return static_cast<bool>(_M_result); }
   1486     };
   1487 
   1488   class __future_base::_Async_state_commonV2
   1489     : public __future_base::_State_base
   1490   {
   1491   protected:
   1492     ~_Async_state_commonV2() = default;
   1493 
   1494     // Make waiting functions block until the thread completes, as if joined.
   1495     virtual void _M_complete_async() { _M_join(); }
   1496 
   1497     void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
   1498 
   1499     thread _M_thread;
   1500     once_flag _M_once;
   1501   };
   1502 
   1503   template<typename _BoundFn, typename _Res>
   1504     class __future_base::_Async_state_impl final
   1505     : public __future_base::_Async_state_commonV2
   1506     {
   1507     public:
   1508       explicit
   1509       _Async_state_impl(_BoundFn&& __fn)
   1510       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
   1511       {
   1512 	_M_thread = std::thread{ [this] {
   1513 	  _M_set_result(_S_task_setter(_M_result, _M_fn));
   1514         } };
   1515       }
   1516 
   1517       ~_Async_state_impl() { _M_join(); }
   1518 
   1519     private:
   1520       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
   1521       _Ptr_type _M_result;
   1522       _BoundFn _M_fn;
   1523     };
   1524 
   1525   template<typename _BoundFn>
   1526     inline std::shared_ptr<__future_base::_State_base>
   1527     __future_base::_S_make_deferred_state(_BoundFn&& __fn)
   1528     {
   1529       typedef typename remove_reference<_BoundFn>::type __fn_type;
   1530       typedef _Deferred_state<__fn_type> __state_type;
   1531       return std::make_shared<__state_type>(std::move(__fn));
   1532     }
   1533 
   1534   template<typename _BoundFn>
   1535     inline std::shared_ptr<__future_base::_State_base>
   1536     __future_base::_S_make_async_state(_BoundFn&& __fn)
   1537     {
   1538       typedef typename remove_reference<_BoundFn>::type __fn_type;
   1539       typedef _Async_state_impl<__fn_type> __state_type;
   1540       return std::make_shared<__state_type>(std::move(__fn));
   1541     }
   1542 
   1543 
   1544   /// async
   1545   template<typename _Fn, typename... _Args>
   1546     future<typename result_of<_Fn(_Args...)>::type>
   1547     async(launch __policy, _Fn&& __fn, _Args&&... __args)
   1548     {
   1549       typedef typename result_of<_Fn(_Args...)>::type result_type;
   1550       std::shared_ptr<__future_base::_State_base> __state;
   1551       if ((__policy & (launch::async|launch::deferred)) == launch::async)
   1552 	{
   1553 	  __state = __future_base::_S_make_async_state(std::__bind_simple(
   1554               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
   1555 	}
   1556       else
   1557 	{
   1558 	  __state = __future_base::_S_make_deferred_state(std::__bind_simple(
   1559               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
   1560 	}
   1561       return future<result_type>(__state);
   1562     }
   1563 
   1564   /// async, potential overload
   1565   template<typename _Fn, typename... _Args>
   1566     inline future<typename result_of<_Fn(_Args...)>::type>
   1567     async(_Fn&& __fn, _Args&&... __args)
   1568     {
   1569       return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
   1570 		   std::forward<_Args>(__args)...);
   1571     }
   1572 
   1573 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
   1574 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
   1575        // && ATOMIC_INT_LOCK_FREE
   1576 
   1577   // @} group futures
   1578 _GLIBCXX_END_NAMESPACE_VERSION
   1579 } // namespace
   1580 
   1581 #endif // C++11
   1582 
   1583 #endif // _GLIBCXX_FUTURE
   1584