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