future

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