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

Generated on 9 Feb 2010 for libstdc++ by  doxygen 1.6.1