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