1 // <future> -*- C++ -*-
 
    3 // Copyright (C) 2009-2014 Free Software Foundation, Inc.
 
    5 // This file is part of the GNU ISO C++ Library.  This library is free
 
    6 // software; you can redistribute it and/or modify it under the
 
    7 // terms of the GNU General Public License as published by the
 
    8 // Free Software Foundation; either version 3, or (at your option)
 
   11 // This library is distributed in the hope that it will be useful,
 
   12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
   14 // GNU General Public License for more details.
 
   16 // Under Section 7 of GPL version 3, you are granted additional
 
   17 // permissions described in the GCC Runtime Library Exception, version
 
   18 // 3.1, as published by the Free Software Foundation.
 
   20 // You should have received a copy of the GNU General Public License and
 
   21 // a copy of the GCC Runtime Library Exception along with this program;
 
   22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
   23 // <http://www.gnu.org/licenses/>.
 
   25 /** @file include/future
 
   26  *  This is a Standard C++ Library header.
 
   29 #ifndef _GLIBCXX_FUTURE
 
   30 #define _GLIBCXX_FUTURE 1
 
   32 #pragma GCC system_header
 
   34 #if __cplusplus < 201103L
 
   35 # include <bits/c++0x_warning.h>
 
   41 #include <condition_variable>
 
   42 #include <system_error>
 
   44 #include <bits/functexcept.h>
 
   45 #include <bits/unique_ptr.h>
 
   46 #include <bits/shared_ptr.h>
 
   47 #include <bits/uses_allocator.h>
 
   48 #include <bits/alloc_traits.h>
 
   49 #include <ext/aligned_buffer.h>
 
   51 namespace std _GLIBCXX_VISIBILITY(default)
 
   53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   56    * @defgroup futures Futures
 
   57    * @ingroup concurrency
 
   59    * Classes for futures support.
 
   63   /// Error code for futures
 
   64   enum class future_errc
 
   66     future_already_retrieved = 1,
 
   67     promise_already_satisfied,
 
   74     struct is_error_code_enum<future_errc> : public true_type { };
 
   76   /// Points to a statically-allocated object derived from error_category.
 
   78   future_category() noexcept;
 
   80   /// Overload for make_error_code.
 
   82   make_error_code(future_errc __errc) noexcept
 
   83   { return error_code(static_cast<int>(__errc), future_category()); }
 
   85   /// Overload for make_error_condition.
 
   86   inline error_condition
 
   87   make_error_condition(future_errc __errc) noexcept
 
   88   { return error_condition(static_cast<int>(__errc), future_category()); }
 
   91    *  @brief Exception type thrown by futures.
 
   94   class future_error : public logic_error
 
   99     explicit future_error(error_code __ec)
 
  100     : logic_error("std::future_error"), _M_code(__ec)
 
  103     virtual ~future_error() noexcept;
 
  106     what() const noexcept;
 
  109     code() const noexcept { return _M_code; }
 
  112   // Forward declarations.
 
  113   template<typename _Res>
 
  116   template<typename _Res>
 
  119   template<typename _Signature>
 
  122   template<typename _Res>
 
  125   /// Launch code for futures
 
  132   constexpr launch operator&(launch __x, launch __y)
 
  134     return static_cast<launch>(
 
  135    static_cast<int>(__x) & static_cast<int>(__y));
 
  138   constexpr launch operator|(launch __x, launch __y)
 
  140     return static_cast<launch>(
 
  141    static_cast<int>(__x) | static_cast<int>(__y));
 
  144   constexpr launch operator^(launch __x, launch __y)
 
  146     return static_cast<launch>(
 
  147    static_cast<int>(__x) ^ static_cast<int>(__y));
 
  150   constexpr launch operator~(launch __x)
 
  151   { return static_cast<launch>(~static_cast<int>(__x)); }
 
  153   inline launch& operator&=(launch& __x, launch __y)
 
  154   { return __x = __x & __y; }
 
  156   inline launch& operator|=(launch& __x, launch __y)
 
  157   { return __x = __x | __y; }
 
  159   inline launch& operator^=(launch& __x, launch __y)
 
  160   { return __x = __x ^ __y; }
 
  162   /// Status code for futures
 
  163   enum class future_status
 
  170   template<typename _Fn, typename... _Args>
 
  171     future<typename result_of<_Fn(_Args...)>::type>
 
  172     async(launch __policy, _Fn&& __fn, _Args&&... __args);
 
  174   template<typename _Fn, typename... _Args>
 
  175     future<typename result_of<_Fn(_Args...)>::type>
 
  176     async(_Fn&& __fn, _Args&&... __args);
 
  178 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
 
  179   && (ATOMIC_INT_LOCK_FREE > 1)
 
  181   /// Base class and enclosing scope.
 
  184     /// Base class for results.
 
  187       exception_ptr        _M_error;
 
  189       _Result_base(const _Result_base&) = delete;
 
  190       _Result_base& operator=(const _Result_base&) = delete;
 
  192       // _M_destroy() allows derived classes to control deallocation
 
  193       virtual void _M_destroy() = 0;
 
  197    void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
 
  202       virtual ~_Result_base();
 
  206     template<typename _Res>
 
  207       struct _Result : _Result_base
 
  210    __gnu_cxx::__aligned_buffer<_Res>   _M_storage;
 
  214    typedef _Res result_type;
 
  216    _Result() noexcept : _M_initialized() { }
 
  224    // Return lvalue, future will add const or rvalue-reference
 
  226    _M_value() noexcept { return *_M_storage._M_ptr(); }
 
  229    _M_set(const _Res& __res)
 
  231      ::new (_M_storage._M_addr()) _Res(__res);
 
  232      _M_initialized = true;
 
  238      ::new (_M_storage._M_addr()) _Res(std::move(__res));
 
  239      _M_initialized = true;
 
  243    void _M_destroy() { delete this; }
 
  246     /// A unique_ptr based on the instantiating type.
 
  247     template<typename _Res>
 
  248       using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
 
  251     template<typename _Res, typename _Alloc>
 
  252       struct _Result_alloc final : _Result<_Res>, _Alloc
 
  254         typedef typename allocator_traits<_Alloc>::template
 
  255           rebind_alloc<_Result_alloc> __allocator_type;
 
  258    _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
 
  264      typedef allocator_traits<__allocator_type> __traits;
 
  265           __allocator_type __a(*this);
 
  266      __traits::destroy(__a, this);
 
  267      __traits::deallocate(__a, this, 1);
 
  271     template<typename _Res, typename _Allocator>
 
  272       static _Ptr<_Result_alloc<_Res, _Allocator>>
 
  273       _S_allocate_result(const _Allocator& __a)
 
  275         typedef _Result_alloc<_Res, _Allocator>    __result_type;
 
  276    typedef allocator_traits<typename __result_type::__allocator_type>
 
  278         typename __traits::allocator_type __a2(__a);
 
  279         __result_type* __p = __traits::allocate(__a2, 1);
 
  282        __traits::construct(__a2, __p, __a);
 
  286        __traits::deallocate(__a2, __p, 1);
 
  287        __throw_exception_again;
 
  289         return _Ptr<__result_type>(__p);
 
  292     template<typename _Res, typename _Tp>
 
  293       static _Ptr<_Result<_Res>>
 
  294       _S_allocate_result(const std::allocator<_Tp>& __a)
 
  296    return _Ptr<_Result<_Res>>(new _Result<_Res>);
 
  299     /// Base class for state between a promise and one or more
 
  300     /// associated futures.
 
  303       typedef _Ptr<_Result_base> _Ptr_type;
 
  307       condition_variable   _M_cond;
 
  308       atomic_flag          _M_retrieved;
 
  312       _State_baseV2() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT)
 
  314       _State_baseV2(const _State_baseV2&) = delete;
 
  315       _State_baseV2& operator=(const _State_baseV2&) = delete;
 
  316       virtual ~_State_baseV2() = default;
 
  322    unique_lock<mutex> __lock(_M_mutex);
 
  323    _M_cond.wait(__lock, [&] { return _M_ready(); });
 
  327       template<typename _Rep, typename _Period>
 
  329         wait_for(const chrono::duration<_Rep, _Period>& __rel)
 
  331      unique_lock<mutex> __lock(_M_mutex);
 
  333        return future_status::ready;
 
  334      if (_M_has_deferred())
 
  335        return future_status::deferred;
 
  336      if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
 
  338          // _GLIBCXX_RESOLVE_LIB_DEFECTS
 
  339          // 2100.  timed waiting functions must also join
 
  341          return future_status::ready;
 
  343      return future_status::timeout;
 
  346       template<typename _Clock, typename _Duration>
 
  348         wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
 
  350      unique_lock<mutex> __lock(_M_mutex);
 
  352        return future_status::ready;
 
  353      if (_M_has_deferred())
 
  354        return future_status::deferred;
 
  355      if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
 
  357          // _GLIBCXX_RESOLVE_LIB_DEFECTS
 
  358          // 2100.  timed waiting functions must also join
 
  360          return future_status::ready;
 
  362      return future_status::timeout;
 
  366       _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
 
  369         // all calls to this function are serialized,
 
  370         // side-effects of invoking __res only happen once
 
  371         call_once(_M_once, &_State_baseV2::_M_do_set, this, ref(__res),
 
  374      _M_cond.notify_all();
 
  375    else if (!__ignore_failure)
 
  376           __throw_future_error(int(future_errc::promise_already_satisfied));
 
  380       _M_break_promise(_Ptr_type __res)
 
  382    if (static_cast<bool>(__res))
 
  384        error_code __ec(make_error_code(future_errc::broken_promise));
 
  385        __res->_M_error = make_exception_ptr(future_error(__ec));
 
  387          lock_guard<mutex> __lock(_M_mutex);
 
  388          _M_result.swap(__res);
 
  390        _M_cond.notify_all();
 
  394       // Called when this object is passed to a future.
 
  396       _M_set_retrieved_flag()
 
  398    if (_M_retrieved.test_and_set())
 
  399      __throw_future_error(int(future_errc::future_already_retrieved));
 
  402       template<typename _Res, typename _Arg>
 
  406       template<typename _Res, typename _Arg>
 
  407         struct _Setter<_Res, _Arg&>
 
  409           // check this is only used by promise<R>::set_value(const R&)
 
  410           // or promise<R>::set_value(R&)
 
  411           static_assert(is_same<_Res, _Arg&>::value  // promise<R&>
 
  412               || is_same<const _Res, _Arg>::value,  // promise<R>
 
  413               "Invalid specialisation");
 
  415           typename promise<_Res>::_Ptr_type operator()()
 
  417             _State_baseV2::_S_check(_M_promise->_M_future);
 
  418             _M_promise->_M_storage->_M_set(_M_arg);
 
  419             return std::move(_M_promise->_M_storage);
 
  421           promise<_Res>*    _M_promise;
 
  426       template<typename _Res>
 
  427         struct _Setter<_Res, _Res&&>
 
  429           typename promise<_Res>::_Ptr_type operator()()
 
  431             _State_baseV2::_S_check(_M_promise->_M_future);
 
  432             _M_promise->_M_storage->_M_set(std::move(_M_arg));
 
  433             return std::move(_M_promise->_M_storage);
 
  435           promise<_Res>*    _M_promise;
 
  439       struct __exception_ptr_tag { };
 
  442       template<typename _Res>
 
  443         struct _Setter<_Res, __exception_ptr_tag>
 
  445           typename promise<_Res>::_Ptr_type operator()()
 
  447             _State_baseV2::_S_check(_M_promise->_M_future);
 
  448             _M_promise->_M_storage->_M_error = _M_ex;
 
  449             return std::move(_M_promise->_M_storage);
 
  452           promise<_Res>*   _M_promise;
 
  453           exception_ptr&    _M_ex;
 
  456       template<typename _Res, typename _Arg>
 
  457         static _Setter<_Res, _Arg&&>
 
  458         __setter(promise<_Res>* __prom, _Arg&& __arg)
 
  460           return _Setter<_Res, _Arg&&>{ __prom, __arg };
 
  463       template<typename _Res>
 
  464         static _Setter<_Res, __exception_ptr_tag>
 
  465         __setter(exception_ptr& __ex, promise<_Res>* __prom)
 
  467           return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
 
  470       static _Setter<void, void>
 
  471       __setter(promise<void>* __prom);
 
  473       template<typename _Tp>
 
  475         _S_check(const shared_ptr<_Tp>& __p)
 
  477           if (!static_cast<bool>(__p))
 
  478             __throw_future_error((int)future_errc::no_state);
 
  483       _M_do_set(function<_Ptr_type()>& __f, bool& __set)
 
  485         _Ptr_type __res = __f();
 
  487           lock_guard<mutex> __lock(_M_mutex);
 
  488           _M_result.swap(__res);
 
  493       bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
 
  495       // Wait for completion of async function.
 
  496       virtual void _M_complete_async() { }
 
  498       // Return true if state contains a deferred function.
 
  499       // Caller must own _M_mutex.
 
  500       virtual bool _M_has_deferred() const { return false; }
 
  503 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
 
  505     class _Async_state_common;
 
  507     using _State_base = _State_baseV2;
 
  508     class _Async_state_commonV2;
 
  511     template<typename _BoundFn, typename = typename _BoundFn::result_type>
 
  512       class _Deferred_state;
 
  514     template<typename _BoundFn, typename = typename _BoundFn::result_type>
 
  515       class _Async_state_impl;
 
  517     template<typename _Signature>
 
  518       class _Task_state_base;
 
  520     template<typename _Fn, typename _Alloc, typename _Signature>
 
  523     template<typename _BoundFn>
 
  524       static std::shared_ptr<_State_base>
 
  525       _S_make_deferred_state(_BoundFn&& __fn);
 
  527     template<typename _BoundFn>
 
  528       static std::shared_ptr<_State_base>
 
  529       _S_make_async_state(_BoundFn&& __fn);
 
  531     template<typename _Res_ptr,
 
  532         typename _Res = typename _Res_ptr::element_type::result_type>
 
  535     template<typename _Res_ptr, typename _BoundFn>
 
  536       static _Task_setter<_Res_ptr>
 
  537       _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
 
  539    return _Task_setter<_Res_ptr>{ __ptr, std::ref(__call) };
 
  543   /// Partial specialization for reference types.
 
  544   template<typename _Res>
 
  545     struct __future_base::_Result<_Res&> : __future_base::_Result_base
 
  547       typedef _Res& result_type;
 
  549       _Result() noexcept : _M_value_ptr() { }
 
  551       void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
 
  553       _Res& _M_get() noexcept { return *_M_value_ptr; }
 
  558       void _M_destroy() { delete this; }
 
  561   /// Explicit specialization for void.
 
  563     struct __future_base::_Result<void> : __future_base::_Result_base
 
  565       typedef void result_type;
 
  568       void _M_destroy() { delete this; }
 
  571 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT
 
  573   /// Common implementation for future and shared_future.
 
  574   template<typename _Res>
 
  575     class __basic_future : public __future_base
 
  578       typedef shared_ptr<_State_base>      __state_type;
 
  579       typedef __future_base::_Result<_Res>&    __result_type;
 
  582       __state_type         _M_state;
 
  586       __basic_future(const __basic_future&) = delete;
 
  587       __basic_future& operator=(const __basic_future&) = delete;
 
  590       valid() const noexcept { return static_cast<bool>(_M_state); }
 
  595         _State_base::_S_check(_M_state);
 
  599       template<typename _Rep, typename _Period>
 
  601         wait_for(const chrono::duration<_Rep, _Period>& __rel) const
 
  603           _State_base::_S_check(_M_state);
 
  604           return _M_state->wait_for(__rel);
 
  607       template<typename _Clock, typename _Duration>
 
  609         wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
 
  611           _State_base::_S_check(_M_state);
 
  612           return _M_state->wait_until(__abs);
 
  616       /// Wait for the state to be ready and rethrow any stored exception
 
  618       _M_get_result() const
 
  620         _State_base::_S_check(_M_state);
 
  621         _Result_base& __res = _M_state->wait();
 
  622         if (!(__res._M_error == 0))
 
  623           rethrow_exception(__res._M_error);
 
  624         return static_cast<__result_type>(__res);
 
  627       void _M_swap(__basic_future& __that) noexcept
 
  629         _M_state.swap(__that._M_state);
 
  632       // Construction of a future by promise::get_future()
 
  634       __basic_future(const __state_type& __state) : _M_state(__state)
 
  636         _State_base::_S_check(_M_state);
 
  637         _M_state->_M_set_retrieved_flag();
 
  640       // Copy construction from a shared_future
 
  642       __basic_future(const shared_future<_Res>&) noexcept;
 
  644       // Move construction from a shared_future
 
  646       __basic_future(shared_future<_Res>&&) noexcept;
 
  648       // Move construction from a future
 
  650       __basic_future(future<_Res>&&) noexcept;
 
  652       constexpr __basic_future() noexcept : _M_state() { }
 
  656         explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
 
  657         ~_Reset() { _M_fut._M_state.reset(); }
 
  658         __basic_future& _M_fut;
 
  663   /// Primary template for future.
 
  664   template<typename _Res>
 
  665     class future : public __basic_future<_Res>
 
  667       friend class promise<_Res>;
 
  668       template<typename> friend class packaged_task;
 
  669       template<typename _Fn, typename... _Args>
 
  670         friend future<typename result_of<_Fn(_Args...)>::type>
 
  671         async(launch, _Fn&&, _Args&&...);
 
  673       typedef __basic_future<_Res> _Base_type;
 
  674       typedef typename _Base_type::__state_type __state_type;
 
  677       future(const __state_type& __state) : _Base_type(__state) { }
 
  680       constexpr future() noexcept : _Base_type() { }
 
  683       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
 
  686       future(const future&) = delete;
 
  687       future& operator=(const future&) = delete;
 
  689       future& operator=(future&& __fut) noexcept
 
  691         future(std::move(__fut))._M_swap(*this);
 
  695       /// Retrieving the value
 
  699         typename _Base_type::_Reset __reset(*this);
 
  700         return std::move(this->_M_get_result()._M_value());
 
  703       shared_future<_Res> share();
 
  706   /// Partial specialization for future<R&>
 
  707   template<typename _Res>
 
  708     class future<_Res&> : public __basic_future<_Res&>
 
  710       friend class promise<_Res&>;
 
  711       template<typename> friend class packaged_task;
 
  712       template<typename _Fn, typename... _Args>
 
  713         friend future<typename result_of<_Fn(_Args...)>::type>
 
  714         async(launch, _Fn&&, _Args&&...);
 
  716       typedef __basic_future<_Res&> _Base_type;
 
  717       typedef typename _Base_type::__state_type __state_type;
 
  720       future(const __state_type& __state) : _Base_type(__state) { }
 
  723       constexpr future() noexcept : _Base_type() { }
 
  726       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
 
  729       future(const future&) = delete;
 
  730       future& operator=(const future&) = delete;
 
  732       future& operator=(future&& __fut) noexcept
 
  734         future(std::move(__fut))._M_swap(*this);
 
  738       /// Retrieving the value
 
  742         typename _Base_type::_Reset __reset(*this);
 
  743         return this->_M_get_result()._M_get();
 
  746       shared_future<_Res&> share();
 
  749   /// Explicit specialization for future<void>
 
  751     class future<void> : public __basic_future<void>
 
  753       friend class promise<void>;
 
  754       template<typename> friend class packaged_task;
 
  755       template<typename _Fn, typename... _Args>
 
  756         friend future<typename result_of<_Fn(_Args...)>::type>
 
  757         async(launch, _Fn&&, _Args&&...);
 
  759       typedef __basic_future<void> _Base_type;
 
  760       typedef typename _Base_type::__state_type __state_type;
 
  763       future(const __state_type& __state) : _Base_type(__state) { }
 
  766       constexpr future() noexcept : _Base_type() { }
 
  769       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
 
  772       future(const future&) = delete;
 
  773       future& operator=(const future&) = delete;
 
  775       future& operator=(future&& __fut) noexcept
 
  777         future(std::move(__fut))._M_swap(*this);
 
  781       /// Retrieving the value
 
  785         typename _Base_type::_Reset __reset(*this);
 
  786         this->_M_get_result();
 
  789       shared_future<void> share();
 
  793   /// Primary template for shared_future.
 
  794   template<typename _Res>
 
  795     class shared_future : public __basic_future<_Res>
 
  797       typedef __basic_future<_Res> _Base_type;
 
  800       constexpr shared_future() noexcept : _Base_type() { }
 
  803       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
 
  805       /// Construct from a future rvalue
 
  806       shared_future(future<_Res>&& __uf) noexcept
 
  807       : _Base_type(std::move(__uf))
 
  810       /// Construct from a shared_future rvalue
 
  811       shared_future(shared_future&& __sf) noexcept
 
  812       : _Base_type(std::move(__sf))
 
  815       shared_future& operator=(const shared_future& __sf)
 
  817         shared_future(__sf)._M_swap(*this);
 
  821       shared_future& operator=(shared_future&& __sf) noexcept
 
  823         shared_future(std::move(__sf))._M_swap(*this);
 
  827       /// Retrieving the value
 
  829       get() const { return this->_M_get_result()._M_value(); }
 
  832   /// Partial specialization for shared_future<R&>
 
  833   template<typename _Res>
 
  834     class shared_future<_Res&> : public __basic_future<_Res&>
 
  836       typedef __basic_future<_Res&>           _Base_type;
 
  839       constexpr shared_future() noexcept : _Base_type() { }
 
  842       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
 
  844       /// Construct from a future rvalue
 
  845       shared_future(future<_Res&>&& __uf) noexcept
 
  846       : _Base_type(std::move(__uf))
 
  849       /// Construct from a shared_future rvalue
 
  850       shared_future(shared_future&& __sf) noexcept
 
  851       : _Base_type(std::move(__sf))
 
  854       shared_future& operator=(const shared_future& __sf)
 
  856         shared_future(__sf)._M_swap(*this);
 
  860       shared_future& operator=(shared_future&& __sf) noexcept
 
  862         shared_future(std::move(__sf))._M_swap(*this);
 
  866       /// Retrieving the value
 
  868       get() const { return this->_M_get_result()._M_get(); }
 
  871   /// Explicit specialization for shared_future<void>
 
  873     class shared_future<void> : public __basic_future<void>
 
  875       typedef __basic_future<void> _Base_type;
 
  878       constexpr shared_future() noexcept : _Base_type() { }
 
  881       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
 
  883       /// Construct from a future rvalue
 
  884       shared_future(future<void>&& __uf) noexcept
 
  885       : _Base_type(std::move(__uf))
 
  888       /// Construct from a shared_future rvalue
 
  889       shared_future(shared_future&& __sf) noexcept
 
  890       : _Base_type(std::move(__sf))
 
  893       shared_future& operator=(const shared_future& __sf)
 
  895         shared_future(__sf)._M_swap(*this);
 
  899       shared_future& operator=(shared_future&& __sf) noexcept
 
  901         shared_future(std::move(__sf))._M_swap(*this);
 
  905       // Retrieving the value
 
  907       get() const { this->_M_get_result(); }
 
  910   // Now we can define the protected __basic_future constructors.
 
  911   template<typename _Res>
 
  912     inline __basic_future<_Res>::
 
  913     __basic_future(const shared_future<_Res>& __sf) noexcept
 
  914     : _M_state(__sf._M_state)
 
  917   template<typename _Res>
 
  918     inline __basic_future<_Res>::
 
  919     __basic_future(shared_future<_Res>&& __sf) noexcept
 
  920     : _M_state(std::move(__sf._M_state))
 
  923   template<typename _Res>
 
  924     inline __basic_future<_Res>::
 
  925     __basic_future(future<_Res>&& __uf) noexcept
 
  926     : _M_state(std::move(__uf._M_state))
 
  929   template<typename _Res>
 
  930     inline shared_future<_Res>
 
  931     future<_Res>::share()
 
  932     { return shared_future<_Res>(std::move(*this)); }
 
  934   template<typename _Res>
 
  935     inline shared_future<_Res&>
 
  936     future<_Res&>::share()
 
  937     { return shared_future<_Res&>(std::move(*this)); }
 
  939   inline shared_future<void>
 
  940   future<void>::share()
 
  941   { return shared_future<void>(std::move(*this)); }
 
  943   /// Primary template for promise
 
  944   template<typename _Res>
 
  947       typedef __future_base::_State_base   _State;
 
  948       typedef __future_base::_Result<_Res> _Res_type;
 
  949       typedef __future_base::_Ptr<_Res_type>   _Ptr_type;
 
  950       template<typename, typename> friend class _State::_Setter;
 
  952       shared_ptr<_State>                        _M_future;
 
  953       _Ptr_type                                 _M_storage;
 
  957       : _M_future(std::make_shared<_State>()),
 
  958    _M_storage(new _Res_type())
 
  961       promise(promise&& __rhs) noexcept
 
  962       : _M_future(std::move(__rhs._M_future)),
 
  963    _M_storage(std::move(__rhs._M_storage))
 
  966       template<typename _Allocator>
 
  967         promise(allocator_arg_t, const _Allocator& __a)
 
  968         : _M_future(std::allocate_shared<_State>(__a)),
 
  969      _M_storage(__future_base::_S_allocate_result<_Res>(__a))
 
  972       template<typename _Allocator>
 
  973         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
 
  974         : _M_future(std::move(__rhs._M_future)),
 
  975      _M_storage(std::move(__rhs._M_storage))
 
  978       promise(const promise&) = delete;
 
  982         if (static_cast<bool>(_M_future) && !_M_future.unique())
 
  983           _M_future->_M_break_promise(std::move(_M_storage));
 
  988       operator=(promise&& __rhs) noexcept
 
  990         promise(std::move(__rhs)).swap(*this);
 
  994       promise& operator=(const promise&) = delete;
 
  997       swap(promise& __rhs) noexcept
 
  999         _M_future.swap(__rhs._M_future);
 
 1000         _M_storage.swap(__rhs._M_storage);
 
 1003       // Retrieving the result
 
 1006       { return future<_Res>(_M_future); }
 
 1008       // Setting the result
 
 1010       set_value(const _Res& __r)
 
 1012    auto __future = _M_future;
 
 1013         auto __setter = _State::__setter(this, __r);
 
 1014         __future->_M_set_result(std::move(__setter));
 
 1018       set_value(_Res&& __r)
 
 1020    auto __future = _M_future;
 
 1021         auto __setter = _State::__setter(this, std::move(__r));
 
 1022         __future->_M_set_result(std::move(__setter));
 
 1026       set_exception(exception_ptr __p)
 
 1028    auto __future = _M_future;
 
 1029         auto __setter = _State::__setter(__p, this);
 
 1030         __future->_M_set_result(std::move(__setter));
 
 1034   template<typename _Res>
 
 1036     swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
 
 1039   template<typename _Res, typename _Alloc>
 
 1040     struct uses_allocator<promise<_Res>, _Alloc>
 
 1041     : public true_type { };
 
 1044   /// Partial specialization for promise<R&>
 
 1045   template<typename _Res>
 
 1046     class promise<_Res&>
 
 1048       typedef __future_base::_State_base   _State;
 
 1049       typedef __future_base::_Result<_Res&>    _Res_type;
 
 1050       typedef __future_base::_Ptr<_Res_type>   _Ptr_type;
 
 1051       template<typename, typename> friend class _State::_Setter;
 
 1053       shared_ptr<_State>                        _M_future;
 
 1054       _Ptr_type                                 _M_storage;
 
 1058       : _M_future(std::make_shared<_State>()),
 
 1059    _M_storage(new _Res_type())
 
 1062       promise(promise&& __rhs) noexcept
 
 1063       : _M_future(std::move(__rhs._M_future)),
 
 1064    _M_storage(std::move(__rhs._M_storage))
 
 1067       template<typename _Allocator>
 
 1068         promise(allocator_arg_t, const _Allocator& __a)
 
 1069         : _M_future(std::allocate_shared<_State>(__a)),
 
 1070      _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
 
 1073       template<typename _Allocator>
 
 1074         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
 
 1075         : _M_future(std::move(__rhs._M_future)),
 
 1076      _M_storage(std::move(__rhs._M_storage))
 
 1079       promise(const promise&) = delete;
 
 1083         if (static_cast<bool>(_M_future) && !_M_future.unique())
 
 1084           _M_future->_M_break_promise(std::move(_M_storage));
 
 1089       operator=(promise&& __rhs) noexcept
 
 1091         promise(std::move(__rhs)).swap(*this);
 
 1095       promise& operator=(const promise&) = delete;
 
 1098       swap(promise& __rhs) noexcept
 
 1100         _M_future.swap(__rhs._M_future);
 
 1101         _M_storage.swap(__rhs._M_storage);
 
 1104       // Retrieving the result
 
 1107       { return future<_Res&>(_M_future); }
 
 1109       // Setting the result
 
 1111       set_value(_Res& __r)
 
 1113    auto __future = _M_future;
 
 1114         auto __setter = _State::__setter(this, __r);
 
 1115         __future->_M_set_result(std::move(__setter));
 
 1119       set_exception(exception_ptr __p)
 
 1121    auto __future = _M_future;
 
 1122         auto __setter = _State::__setter(__p, this);
 
 1123         __future->_M_set_result(std::move(__setter));
 
 1127   /// Explicit specialization for promise<void>
 
 1131       typedef __future_base::_State_base   _State;
 
 1132       typedef __future_base::_Result<void> _Res_type;
 
 1133       typedef __future_base::_Ptr<_Res_type>   _Ptr_type;
 
 1134       template<typename, typename> friend class _State::_Setter;
 
 1136       shared_ptr<_State>                        _M_future;
 
 1137       _Ptr_type                                 _M_storage;
 
 1141       : _M_future(std::make_shared<_State>()),
 
 1142    _M_storage(new _Res_type())
 
 1145       promise(promise&& __rhs) noexcept
 
 1146       : _M_future(std::move(__rhs._M_future)),
 
 1147    _M_storage(std::move(__rhs._M_storage))
 
 1150       template<typename _Allocator>
 
 1151         promise(allocator_arg_t, const _Allocator& __a)
 
 1152         : _M_future(std::allocate_shared<_State>(__a)),
 
 1153      _M_storage(__future_base::_S_allocate_result<void>(__a))
 
 1156       // _GLIBCXX_RESOLVE_LIB_DEFECTS
 
 1157       // 2095.  missing constructors needed for uses-allocator construction
 
 1158       template<typename _Allocator>
 
 1159         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
 
 1160         : _M_future(std::move(__rhs._M_future)),
 
 1161      _M_storage(std::move(__rhs._M_storage))
 
 1164       promise(const promise&) = delete;
 
 1168         if (static_cast<bool>(_M_future) && !_M_future.unique())
 
 1169           _M_future->_M_break_promise(std::move(_M_storage));
 
 1174       operator=(promise&& __rhs) noexcept
 
 1176         promise(std::move(__rhs)).swap(*this);
 
 1180       promise& operator=(const promise&) = delete;
 
 1183       swap(promise& __rhs) noexcept
 
 1185         _M_future.swap(__rhs._M_future);
 
 1186         _M_storage.swap(__rhs._M_storage);
 
 1189       // Retrieving the result
 
 1192       { return future<void>(_M_future); }
 
 1194       // Setting the result
 
 1198       set_exception(exception_ptr __p)
 
 1200    auto __future = _M_future;
 
 1201         auto __setter = _State::__setter(__p, this);
 
 1202         __future->_M_set_result(std::move(__setter));
 
 1208     struct __future_base::_State_base::_Setter<void, void>
 
 1210       promise<void>::_Ptr_type operator()()
 
 1212         _State_base::_S_check(_M_promise->_M_future);
 
 1213         return std::move(_M_promise->_M_storage);
 
 1216       promise<void>*    _M_promise;
 
 1219   inline __future_base::_State_base::_Setter<void, void>
 
 1220   __future_base::_State_base::__setter(promise<void>* __prom)
 
 1222     return _Setter<void, void>{ __prom };
 
 1226   promise<void>::set_value()
 
 1228     auto __future = _M_future;
 
 1229     auto __setter = _State::__setter(this);
 
 1230     __future->_M_set_result(std::move(__setter));
 
 1234   template<typename _Ptr_type, typename _Res>
 
 1235     struct __future_base::_Task_setter
 
 1237       _Ptr_type operator()()
 
 1241        _M_result->_M_set(_M_fn());
 
 1243    __catch(const __cxxabiv1::__forced_unwind&)
 
 1245        __throw_exception_again; // will cause broken_promise
 
 1249        _M_result->_M_error = current_exception();
 
 1251    return std::move(_M_result);
 
 1253       _Ptr_type&                _M_result;
 
 1254       std::function<_Res()>     _M_fn;
 
 1257   template<typename _Ptr_type>
 
 1258     struct __future_base::_Task_setter<_Ptr_type, void>
 
 1260       _Ptr_type operator()()
 
 1266    __catch(const __cxxabiv1::__forced_unwind&)
 
 1268        __throw_exception_again; // will cause broken_promise
 
 1272        _M_result->_M_error = current_exception();
 
 1274    return std::move(_M_result);
 
 1276       _Ptr_type&                _M_result;
 
 1277       std::function<void()>     _M_fn;
 
 1280   template<typename _Res, typename... _Args>
 
 1281     struct __future_base::_Task_state_base<_Res(_Args...)>
 
 1282     : __future_base::_State_base
 
 1284       typedef _Res _Res_type;
 
 1286       template<typename _Alloc>
 
 1287    _Task_state_base(const _Alloc& __a)
 
 1288    : _M_result(_S_allocate_result<_Res>(__a))
 
 1292       _M_run(_Args... __args) = 0;
 
 1294       virtual shared_ptr<_Task_state_base>
 
 1297       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
 
 1298       _Ptr_type _M_result;
 
 1301   template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
 
 1302     struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
 
 1303     : __future_base::_Task_state_base<_Res(_Args...)>
 
 1305       template<typename _Fn2>
 
 1306    _Task_state(_Fn2&& __fn, const _Alloc& __a)
 
 1307    : _Task_state_base<_Res(_Args...)>(__a),
 
 1308      _M_impl(std::forward<_Fn2>(__fn), __a)
 
 1313       _M_run(_Args... __args)
 
 1315    // bound arguments decay so wrap lvalue references
 
 1316    auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
 
 1317        _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
 
 1318    auto __setter = _S_task_setter(this->_M_result, std::move(__boundfn));
 
 1319    this->_M_set_result(std::move(__setter));
 
 1322       virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
 
 1325       template<typename _Tp>
 
 1326    static reference_wrapper<_Tp>
 
 1327    _S_maybe_wrap_ref(_Tp& __t)
 
 1328    { return std::ref(__t); }
 
 1330       template<typename _Tp>
 
 1332    typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
 
 1333    _S_maybe_wrap_ref(_Tp&& __t)
 
 1334    { return std::forward<_Tp>(__t); }
 
 1336       struct _Impl : _Alloc
 
 1338    template<typename _Fn2>
 
 1339      _Impl(_Fn2&& __fn, const _Alloc& __a)
 
 1340      : _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { }
 
 1345   template<typename _Signature, typename _Fn, typename _Alloc>
 
 1346     static shared_ptr<__future_base::_Task_state_base<_Signature>>
 
 1347     __create_task_state(_Fn&& __fn, const _Alloc& __a)
 
 1349       typedef typename decay<_Fn>::type _Fn2;
 
 1350       typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
 
 1351       return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
 
 1354   template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
 
 1355     shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
 
 1356     __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
 
 1358       return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
 
 1359                         static_cast<_Alloc&>(_M_impl));
 
 1362   template<typename _Task, typename _Fn, bool
 
 1363       = is_same<_Task, typename decay<_Fn>::type>::value>
 
 1364     struct __constrain_pkgdtask
 
 1365     { typedef void __type; };
 
 1367   template<typename _Task, typename _Fn>
 
 1368     struct __constrain_pkgdtask<_Task, _Fn, true>
 
 1372   template<typename _Res, typename... _ArgTypes>
 
 1373     class packaged_task<_Res(_ArgTypes...)>
 
 1375       typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
 
 1376       shared_ptr<_State_type>                   _M_state;
 
 1379       // Construction and destruction
 
 1380       packaged_task() noexcept { }
 
 1382       // _GLIBCXX_RESOLVE_LIB_DEFECTS
 
 1383       // 2095.  missing constructors needed for uses-allocator construction
 
 1384       template<typename _Allocator>
 
 1385    packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
 
 1388       template<typename _Fn, typename = typename
 
 1389           __constrain_pkgdtask<packaged_task, _Fn>::__type>
 
 1391    packaged_task(_Fn&& __fn)
 
 1392    : packaged_task(allocator_arg, std::allocator<int>(),
 
 1393            std::forward<_Fn>(__fn))
 
 1396       // _GLIBCXX_RESOLVE_LIB_DEFECTS
 
 1397       // 2097.  packaged_task constructors should be constrained
 
 1398       template<typename _Fn, typename _Alloc, typename = typename
 
 1399           __constrain_pkgdtask<packaged_task, _Fn>::__type>
 
 1401    packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
 
 1402    : _M_state(__create_task_state<_Res(_ArgTypes...)>(
 
 1403            std::forward<_Fn>(__fn), __a))
 
 1408         if (static_cast<bool>(_M_state) && !_M_state.unique())
 
 1409      _M_state->_M_break_promise(std::move(_M_state->_M_result));
 
 1413       packaged_task(const packaged_task&) = delete;
 
 1414       packaged_task& operator=(const packaged_task&) = delete;
 
 1416       template<typename _Allocator>
 
 1417    packaged_task(allocator_arg_t, const _Allocator&,
 
 1418              const packaged_task&) = delete;
 
 1421       packaged_task(packaged_task&& __other) noexcept
 
 1422       { this->swap(__other); }
 
 1424       template<typename _Allocator>
 
 1425    packaged_task(allocator_arg_t, const _Allocator&,
 
 1426              packaged_task&& __other) noexcept
 
 1427    { this->swap(__other); }
 
 1429       packaged_task& operator=(packaged_task&& __other) noexcept
 
 1431    packaged_task(std::move(__other)).swap(*this);
 
 1436       swap(packaged_task& __other) noexcept
 
 1437       { _M_state.swap(__other._M_state); }
 
 1440       valid() const noexcept
 
 1441       { return static_cast<bool>(_M_state); }
 
 1446       { return future<_Res>(_M_state); }
 
 1450       operator()(_ArgTypes... __args)
 
 1452    __future_base::_State_base::_S_check(_M_state);
 
 1453    _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
 
 1459    __future_base::_State_base::_S_check(_M_state);
 
 1460    packaged_task __tmp;
 
 1461    __tmp._M_state = _M_state;
 
 1462    _M_state = _M_state->_M_reset();
 
 1467   template<typename _Res, typename... _ArgTypes>
 
 1469     swap(packaged_task<_Res(_ArgTypes...)>& __x,
 
 1470     packaged_task<_Res(_ArgTypes...)>& __y) noexcept
 
 1473   template<typename _Res, typename _Alloc>
 
 1474     struct uses_allocator<packaged_task<_Res>, _Alloc>
 
 1475     : public true_type { };
 
 1478   template<typename _BoundFn, typename _Res>
 
 1479     class __future_base::_Deferred_state final
 
 1480     : public __future_base::_State_base
 
 1484       _Deferred_state(_BoundFn&& __fn)
 
 1485       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
 
 1489       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
 
 1490       _Ptr_type _M_result;
 
 1493       // Run the deferred function.
 
 1497         // safe to call multiple times so ignore failure
 
 1498         _M_set_result(_S_task_setter(_M_result, _M_fn), true);
 
 1502       _M_has_deferred() const { return static_cast<bool>(_M_result); }
 
 1505   class __future_base::_Async_state_commonV2
 
 1506     : public __future_base::_State_base
 
 1509     ~_Async_state_commonV2() = default;
 
 1511     // Make waiting functions block until the thread completes, as if joined.
 
 1512     virtual void _M_complete_async() { _M_join(); }
 
 1514     void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
 
 1520   template<typename _BoundFn, typename _Res>
 
 1521     class __future_base::_Async_state_impl final
 
 1522     : public __future_base::_Async_state_commonV2
 
 1526       _Async_state_impl(_BoundFn&& __fn)
 
 1527       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
 
 1529    _M_thread = std::thread{ [this] {
 
 1532        _M_set_result(_S_task_setter(_M_result, _M_fn));
 
 1534        __catch (const __cxxabiv1::__forced_unwind&)
 
 1536        // make the shared state ready on thread cancellation
 
 1537        if (static_cast<bool>(_M_result))
 
 1538          this->_M_break_promise(std::move(_M_result));
 
 1539        __throw_exception_again;
 
 1544       ~_Async_state_impl() { _M_join(); }
 
 1547       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
 
 1548       _Ptr_type _M_result;
 
 1552   template<typename _BoundFn>
 
 1553     inline std::shared_ptr<__future_base::_State_base>
 
 1554     __future_base::_S_make_deferred_state(_BoundFn&& __fn)
 
 1556       typedef typename remove_reference<_BoundFn>::type __fn_type;
 
 1557       typedef _Deferred_state<__fn_type> __state_type;
 
 1558       return std::make_shared<__state_type>(std::move(__fn));
 
 1561   template<typename _BoundFn>
 
 1562     inline std::shared_ptr<__future_base::_State_base>
 
 1563     __future_base::_S_make_async_state(_BoundFn&& __fn)
 
 1565       typedef typename remove_reference<_BoundFn>::type __fn_type;
 
 1566       typedef _Async_state_impl<__fn_type> __state_type;
 
 1567       return std::make_shared<__state_type>(std::move(__fn));
 
 1572   template<typename _Fn, typename... _Args>
 
 1573     future<typename result_of<_Fn(_Args...)>::type>
 
 1574     async(launch __policy, _Fn&& __fn, _Args&&... __args)
 
 1576       typedef typename result_of<_Fn(_Args...)>::type result_type;
 
 1577       std::shared_ptr<__future_base::_State_base> __state;
 
 1578       if ((__policy & (launch::async|launch::deferred)) == launch::async)
 
 1580      __state = __future_base::_S_make_async_state(std::__bind_simple(
 
 1581               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
 
 1585      __state = __future_base::_S_make_deferred_state(std::__bind_simple(
 
 1586               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
 
 1588       return future<result_type>(__state);
 
 1591   /// async, potential overload
 
 1592   template<typename _Fn, typename... _Args>
 
 1593     inline future<typename result_of<_Fn(_Args...)>::type>
 
 1594     async(_Fn&& __fn, _Args&&... __args)
 
 1596       return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
 
 1597           std::forward<_Args>(__args)...);
 
 1600 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
 
 1601 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
 
 1602        // && ATOMIC_INT_LOCK_FREE
 
 1605 _GLIBCXX_END_NAMESPACE_VERSION
 
 1610 #endif // _GLIBCXX_FUTURE