OSDN Git Service

PR libstdc++/52104
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / future
index e32b2a0..962400b 100644 (file)
@@ -1,6 +1,6 @@
 // <future> -*- C++ -*-
 
-// Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
+// Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
@@ -60,10 +60,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   /// Error code for futures
   enum class future_errc
   {
-    broken_promise,
-    future_already_retrieved,
+    future_already_retrieved = 1,
     promise_already_satisfied,
-    no_state
+    no_state,
+    broken_promise
   };
 
   /// Specialization.
@@ -72,16 +72,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   /// Points to a statically-allocated object derived from error_category.
   const error_category&
-  future_category();
+  future_category() noexcept;
 
   /// Overload for make_error_code.
-  inline error_code 
-  make_error_code(future_errc __errc)
+  inline error_code
+  make_error_code(future_errc __errc) noexcept
   { return error_code(static_cast<int>(__errc), future_category()); }
 
   /// Overload for make_error_condition.
-  inline error_condition 
-  make_error_condition(future_errc __errc)
+  inline error_condition
+  make_error_condition(future_errc __errc) noexcept
   { return error_condition(static_cast<int>(__errc), future_category()); }
 
   /**
@@ -97,13 +97,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     : logic_error("std::future_error"), _M_code(__ec)
     { }
 
-    virtual ~future_error() throw();
+    virtual ~future_error() noexcept;
 
-    virtual const char* 
-    what() const throw();
+    virtual const char*
+    what() const noexcept;
 
-    const error_code& 
-    code() const throw() { return _M_code; }
+    const error_code&
+    code() const noexcept { return _M_code; }
   };
 
   // Forward declarations.
@@ -116,22 +116,51 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Res>
     class atomic_future;
 
-  template<typename _Signature> 
+  template<typename _Signature>
     class packaged_task;
 
   template<typename _Res>
     class promise;
 
   /// Launch code for futures
-  enum class launch 
-  { 
-    any, 
-    async, 
-    sync 
+  enum class launch
+  {
+    async = 1,
+    deferred = 2
   };
 
+  constexpr launch operator&(launch __x, launch __y)
+  {
+    return static_cast<launch>(
+       static_cast<int>(__x) & static_cast<int>(__y));
+  }
+
+  constexpr launch operator|(launch __x, launch __y)
+  {
+    return static_cast<launch>(
+       static_cast<int>(__x) | static_cast<int>(__y));
+  }
+
+  constexpr launch operator^(launch __x, launch __y)
+  {
+    return static_cast<launch>(
+       static_cast<int>(__x) ^ static_cast<int>(__y));
+  }
+
+  constexpr launch operator~(launch __x)
+  { return static_cast<launch>(~static_cast<int>(__x)); }
+
+  inline launch& operator&=(launch& __x, launch __y)
+  { return __x = __x & __y; }
+
+  inline launch& operator|=(launch& __x, launch __y)
+  { return __x = __x | __y; }
+
+  inline launch& operator^=(launch& __x, launch __y)
+  { return __x = __x ^ __y; }
+
   /// Status code for futures
-  enum class future_status 
+  enum class future_status
   {
     ready,
     timeout,
@@ -142,15 +171,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     future<typename result_of<_Fn(_Args...)>::type>
     async(launch __policy, _Fn&& __fn, _Args&&... __args);
 
+  template<typename _FnCheck, typename _Fn, typename... _Args>
+    struct __async_sfinae_helper
+    {
+      typedef future<typename result_of<_Fn(_Args...)>::type> type;
+    };
+
+  template<typename _Fn, typename... _Args>
+    struct __async_sfinae_helper<launch, _Fn, _Args...>
+    { };
+
   template<typename _Fn, typename... _Args>
     typename
-    enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
-              future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
-             >::type
+    __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
     async(_Fn&& __fn, _Args&&... __args);
 
 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
-  && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
+  && (ATOMIC_INT_LOCK_FREE > 1)
 
   /// Base class and enclosing scope.
   struct __future_base
@@ -160,7 +197,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
       exception_ptr            _M_error;
 
-      _Result_base() = default;
       _Result_base(const _Result_base&) = delete;
       _Result_base& operator=(const _Result_base&) = delete;
 
@@ -173,7 +209,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       };
 
     protected:
-      ~_Result_base();
+      _Result_base();
+      virtual ~_Result_base();
     };
 
     /// Result.
@@ -189,7 +226,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        bool                    _M_initialized;
 
       public:
-       _Result() : _M_initialized() { }
+       _Result() noexcept : _M_initialized() { }
        
        ~_Result()
        {
@@ -198,8 +235,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        }
 
        // Return lvalue, future will add const or rvalue-reference
-       _Res& 
-       _M_value() { return *static_cast<_Res*>(_M_addr()); }
+       _Res&
+       _M_value() noexcept { return *static_cast<_Res*>(_M_addr()); }
 
        void
        _M_set(const _Res& __res)
@@ -218,27 +255,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       private:
        void _M_destroy() { delete this; }
 
-       void* _M_addr() { return static_cast<void*>(&_M_storage); }
+       void* _M_addr() noexcept { return static_cast<void*>(&_M_storage); }
     };
 
-    // TODO: use template alias when available
-    /*
-      template<typename _Res>
-      using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
-    */
     /// A unique_ptr based on the instantiating type.
     template<typename _Res>
-      struct _Ptr
-      {
-       typedef unique_ptr<_Res, _Result_base::_Deleter> type;
-      };
+      using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
 
     /// Result_alloc.
     template<typename _Res, typename _Alloc>
-      struct _Result_alloc : _Result<_Res>, _Alloc
+      struct _Result_alloc final : _Result<_Res>, _Alloc
       {
-        typedef typename _Alloc::template rebind<_Result_alloc>::other
-          __allocator_type;
+        typedef typename allocator_traits<_Alloc>::template
+          rebind_alloc<_Result_alloc> __allocator_type;
 
         explicit
        _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
@@ -247,36 +276,40 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       private:
        void _M_destroy()
         {
+         typedef allocator_traits<__allocator_type> __traits;
           __allocator_type __a(*this);
-          __a.destroy(this);
-          __a.deallocate(this, 1);
+         __traits::destroy(__a, this);
+         __traits::deallocate(__a, this, 1);
         }
       };
 
     template<typename _Res, typename _Allocator>
-      static typename _Ptr<_Result_alloc<_Res, _Allocator>>::type
+      static _Ptr<_Result_alloc<_Res, _Allocator>>
       _S_allocate_result(const _Allocator& __a)
       {
         typedef _Result_alloc<_Res, _Allocator>        __result_type;
-        typename __result_type::__allocator_type __a2(__a);
-        __result_type* __p = __a2.allocate(1);
+       typedef allocator_traits<typename __result_type::__allocator_type>
+         __traits;
+        typename __traits::allocator_type __a2(__a);
+        __result_type* __p = __traits::allocate(__a2, 1);
         __try
        {
-          __a2.construct(__p, __a);
+         __traits::construct(__a2, __p, __a);
         }
         __catch(...)
         {
-          __a2.deallocate(__p, 1);
+         __traits::deallocate(__a2, __p, 1);
           __throw_exception_again;
         }
-        return typename _Ptr<__result_type>::type(__p);
+        return _Ptr<__result_type>(__p);
       }
 
 
-    /// Shared state between a promise and one or more associated futures.
-    class _State
+    /// Base class for state between a promise and one or more
+    /// associated futures.
+    class _State_base
     {
-      typedef _Ptr<_Result_base>::type _Ptr_type;
+      typedef _Ptr<_Result_base> _Ptr_type;
 
       _Ptr_type                        _M_result;
       mutex                    _M_mutex;
@@ -285,37 +318,38 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       once_flag                        _M_once;
 
     public:
-      _State() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
-
-      _State(const _State&) = delete;
-      _State& operator=(const _State&) = delete;
+      _State_base() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
+      _State_base(const _State_base&) = delete;
+      _State_base& operator=(const _State_base&) = delete;
+      virtual ~_State_base();
 
       _Result_base&
       wait()
       {
        _M_run_deferred();
        unique_lock<mutex> __lock(_M_mutex);
-       if (!_M_ready())
-         _M_cond.wait(__lock, std::bind<bool>(&_State::_M_ready, this));
+       _M_cond.wait(__lock, [&] { return _M_ready(); });
        return *_M_result;
       }
 
       template<typename _Rep, typename _Period>
-        bool
+        future_status
         wait_for(const chrono::duration<_Rep, _Period>& __rel)
         {
          unique_lock<mutex> __lock(_M_mutex);
-         auto __bound = std::bind<bool>(&_State::_M_ready, this);
-         return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
+         if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
+           return future_status::ready;
+         return future_status::timeout;
        }
 
       template<typename _Clock, typename _Duration>
-        bool
+        future_status
         wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
         {
          unique_lock<mutex> __lock(_M_mutex);
-         auto __bound = std::bind<bool>(&_State::_M_ready, this);
-         return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
+         if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
+           return future_status::ready;
+         return future_status::timeout;
        }
 
       void
@@ -324,7 +358,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         bool __set = __ignore_failure;
         // all calls to this function are serialized,
         // side-effects of invoking __res only happen once
-        call_once(_M_once, &_State::_M_do_set, this, ref(__res),
+        call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
             ref(__set));
         if (!__set)
           __throw_future_error(int(future_errc::promise_already_satisfied));
@@ -368,7 +402,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
           typename promise<_Res>::_Ptr_type operator()()
           {
-            _State::_S_check(_M_promise->_M_future);
+            _State_base::_S_check(_M_promise->_M_future);
             _M_promise->_M_storage->_M_set(_M_arg);
             return std::move(_M_promise->_M_storage);
           }
@@ -382,7 +416,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         {
           typename promise<_Res>::_Ptr_type operator()()
           {
-            _State::_S_check(_M_promise->_M_future);
+            _State_base::_S_check(_M_promise->_M_future);
             _M_promise->_M_storage->_M_set(std::move(_M_arg));
             return std::move(_M_promise->_M_storage);
           }
@@ -398,7 +432,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         {
           typename promise<_Res>::_Ptr_type operator()()
           {
-            _State::_S_check(_M_promise->_M_future);
+            _State_base::_S_check(_M_promise->_M_future);
             _M_promise->_M_storage->_M_error = _M_ex;
             return std::move(_M_promise->_M_storage);
           }
@@ -445,39 +479,65 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         __set = true;
       }
 
-      bool _M_ready() const { return static_cast<bool>(_M_result); }
+      bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
 
+      // Misnamed: waits for completion of async function.
       virtual void _M_run_deferred() { }
     };
 
-    template<typename _Res>
+    template<typename _BoundFn, typename = typename _BoundFn::result_type>
       class _Deferred_state;
 
-    template<typename _Res>
-      class _Async_state;
+    class _Async_state_common;
+
+    template<typename _BoundFn, typename = typename _BoundFn::result_type>
+      class _Async_state_impl;
 
     template<typename _Signature>
       class _Task_state;
 
-    template<typename _StateT, typename _Res = typename _StateT::_Res_type>
+    template<typename _BoundFn>
+      static std::shared_ptr<_State_base>
+      _S_make_deferred_state(_BoundFn&& __fn);
+
+    template<typename _BoundFn>
+      static std::shared_ptr<_State_base>
+      _S_make_async_state(_BoundFn&& __fn);
+
+    template<typename _Res_ptr, typename _Res>
       struct _Task_setter;
-  };
 
-  inline __future_base::_Result_base::~_Result_base() = default;
+    template<typename _Res_ptr, typename _BoundFn>
+      class _Task_setter_helper
+      {
+       typedef typename remove_reference<_BoundFn>::type::result_type __res;
+      public:
+       typedef _Task_setter<_Res_ptr, __res> __type;
+      };
+
+    template<typename _Res_ptr, typename _BoundFn>
+      static typename _Task_setter_helper<_Res_ptr, _BoundFn>::__type
+      _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
+      {
+       typedef _Task_setter_helper<_Res_ptr, _BoundFn> __helper_type;
+       typedef typename __helper_type::__type _Setter;
+       return _Setter{ __ptr, std::ref(__call) };
+      }
+  };
 
   /// Partial specialization for reference types.
   template<typename _Res>
     struct __future_base::_Result<_Res&> : __future_base::_Result_base
     {
-      _Result() : _M_value_ptr() { }
+      _Result() noexcept : _M_value_ptr() { }
 
-      void _M_set(_Res& __res) { _M_value_ptr = &__res; }
+      void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
 
-      _Res& _M_get() { return *_M_value_ptr; }
+      _Res& _M_get() noexcept { return *_M_value_ptr; }
 
     private:
       _Res*                    _M_value_ptr;
-      
+
       void _M_destroy() { delete this; }
     };
 
@@ -495,7 +555,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     class __basic_future : public __future_base
     {
     protected:
-      typedef shared_ptr<_State>               __state_type;
+      typedef shared_ptr<_State_base>          __state_type;
       typedef __future_base::_Result<_Res>&    __result_type;
 
     private:
@@ -506,29 +566,29 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __basic_future(const __basic_future&) = delete;
       __basic_future& operator=(const __basic_future&) = delete;
 
-      bool 
-      valid() const { return static_cast<bool>(_M_state); }
+      bool
+      valid() const noexcept { return static_cast<bool>(_M_state); }
 
-      void 
+      void
       wait() const
       {
-        _State::_S_check(_M_state);
+        _State_base::_S_check(_M_state);
         _M_state->wait();
       }
 
       template<typename _Rep, typename _Period>
-        bool
+        future_status
         wait_for(const chrono::duration<_Rep, _Period>& __rel) const
         {
-          _State::_S_check(_M_state);
+          _State_base::_S_check(_M_state);
           return _M_state->wait_for(__rel);
         }
 
       template<typename _Clock, typename _Duration>
-        bool
+        future_status
         wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
         {
-          _State::_S_check(_M_state);
+          _State_base::_S_check(_M_state);
           return _M_state->wait_until(__abs);
         }
 
@@ -537,14 +597,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __result_type
       _M_get_result()
       {
-        _State::_S_check(_M_state);
+        _State_base::_S_check(_M_state);
         _Result_base& __res = _M_state->wait();
         if (!(__res._M_error == 0))
           rethrow_exception(__res._M_error);
         return static_cast<__result_type>(__res);
       }
 
-      void _M_swap(__basic_future& __that)
+      void _M_swap(__basic_future& __that) noexcept
       {
         _M_state.swap(__that._M_state);
       }
@@ -553,27 +613,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       explicit
       __basic_future(const __state_type& __state) : _M_state(__state)
       {
-        _State::_S_check(_M_state);
+        _State_base::_S_check(_M_state);
         _M_state->_M_set_retrieved_flag();
       }
 
       // Copy construction from a shared_future
       explicit
-      __basic_future(const shared_future<_Res>&);
+      __basic_future(const shared_future<_Res>&) noexcept;
 
       // Move construction from a shared_future
       explicit
-      __basic_future(shared_future<_Res>&&);
+      __basic_future(shared_future<_Res>&&) noexcept;
 
       // Move construction from a future
       explicit
-      __basic_future(future<_Res>&&);
+      __basic_future(future<_Res>&&) noexcept;
 
-      constexpr __basic_future() : _M_state() { }
+      constexpr __basic_future() noexcept : _M_state() { }
 
       struct _Reset
       {
-        explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { }
+        explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
         ~_Reset() { _M_fut._M_state.reset(); }
         __basic_future& _M_fut;
       };
@@ -597,16 +657,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       future(const __state_type& __state) : _Base_type(__state) { }
 
     public:
-      constexpr future() : _Base_type() { }
+      constexpr future() noexcept : _Base_type() { }
 
       /// Move constructor
-      future(future&& __uf) : _Base_type(std::move(__uf)) { }
+      future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
 
       // Disable copying
       future(const future&) = delete;
       future& operator=(const future&) = delete;
 
-      future& operator=(future&& __fut)
+      future& operator=(future&& __fut) noexcept
       {
         future(std::move(__fut))._M_swap(*this);
         return *this;
@@ -619,8 +679,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         typename _Base_type::_Reset __reset(*this);
         return std::move(this->_M_get_result()._M_value());
       }
+
+      shared_future<_Res> share();
     };
+
   /// Partial specialization for future<R&>
   template<typename _Res>
     class future<_Res&> : public __basic_future<_Res&>
@@ -638,28 +700,30 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       future(const __state_type& __state) : _Base_type(__state) { }
 
     public:
-      constexpr future() : _Base_type() { }
+      constexpr future() noexcept : _Base_type() { }
 
       /// Move constructor
-      future(future&& __uf) : _Base_type(std::move(__uf)) { }
+      future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
 
       // Disable copying
       future(const future&) = delete;
       future& operator=(const future&) = delete;
 
-      future& operator=(future&& __fut)
+      future& operator=(future&& __fut) noexcept
       {
         future(std::move(__fut))._M_swap(*this);
         return *this;
       }
 
       /// Retrieving the value
-      _Res& 
+      _Res&
       get()
       {
         typename _Base_type::_Reset __reset(*this);
         return this->_M_get_result()._M_get();
       }
+
+      shared_future<_Res&> share();
     };
 
   /// Explicit specialization for future<void>
@@ -679,28 +743,30 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       future(const __state_type& __state) : _Base_type(__state) { }
 
     public:
-      constexpr future() : _Base_type() { }
+      constexpr future() noexcept : _Base_type() { }
 
       /// Move constructor
-      future(future&& __uf) : _Base_type(std::move(__uf)) { }
+      future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
 
       // Disable copying
       future(const future&) = delete;
       future& operator=(const future&) = delete;
 
-      future& operator=(future&& __fut)
+      future& operator=(future&& __fut) noexcept
       {
         future(std::move(__fut))._M_swap(*this);
         return *this;
       }
 
       /// Retrieving the value
-      void 
+      void
       get()
       {
         typename _Base_type::_Reset __reset(*this);
         this->_M_get_result();
       }
+
+      shared_future<void> share();
     };
 
 
@@ -711,18 +777,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       typedef __basic_future<_Res> _Base_type;
 
     public:
-      constexpr shared_future() : _Base_type() { }
+      constexpr shared_future() noexcept : _Base_type() { }
 
       /// Copy constructor
       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
 
       /// Construct from a future rvalue
-      shared_future(future<_Res>&& __uf)
+      shared_future(future<_Res>&& __uf) noexcept
       : _Base_type(std::move(__uf))
       { }
 
       /// Construct from a shared_future rvalue
-      shared_future(shared_future&& __sf)
+      shared_future(shared_future&& __sf) noexcept
       : _Base_type(std::move(__sf))
       { }
 
@@ -732,7 +798,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         return *this;
       }
 
-      shared_future& operator=(shared_future&& __sf)
+      shared_future& operator=(shared_future&& __sf) noexcept
       {
         shared_future(std::move(__sf))._M_swap(*this);
         return *this;
@@ -747,7 +813,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        return __rs;
       }
     };
+
   /// Partial specialization for shared_future<R&>
   template<typename _Res>
     class shared_future<_Res&> : public __basic_future<_Res&>
@@ -755,18 +821,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       typedef __basic_future<_Res&>           _Base_type;
 
     public:
-      constexpr shared_future() : _Base_type() { }
+      constexpr shared_future() noexcept : _Base_type() { }
 
       /// Copy constructor
       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
 
       /// Construct from a future rvalue
-      shared_future(future<_Res&>&& __uf)
+      shared_future(future<_Res&>&& __uf) noexcept
       : _Base_type(std::move(__uf))
       { }
 
       /// Construct from a shared_future rvalue
-      shared_future(shared_future&& __sf)
+      shared_future(shared_future&& __sf) noexcept
       : _Base_type(std::move(__sf))
       { }
 
@@ -776,14 +842,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         return *this;
       }
 
-      shared_future& operator=(shared_future&& __sf)
+      shared_future& operator=(shared_future&& __sf) noexcept
       {
         shared_future(std::move(__sf))._M_swap(*this);
         return *this;
       }
 
       /// Retrieving the value
-      _Res& 
+      _Res&
       get() { return this->_M_get_result()._M_get(); }
     };
 
@@ -794,18 +860,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       typedef __basic_future<void> _Base_type;
 
     public:
-      constexpr shared_future() : _Base_type() { }
+      constexpr shared_future() noexcept : _Base_type() { }
 
       /// Copy constructor
       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
 
       /// Construct from a future rvalue
-      shared_future(future<void>&& __uf)
+      shared_future(future<void>&& __uf) noexcept
       : _Base_type(std::move(__uf))
       { }
 
       /// Construct from a shared_future rvalue
-      shared_future(shared_future&& __sf)
+      shared_future(shared_future&& __sf) noexcept
       : _Base_type(std::move(__sf))
       { }
 
@@ -815,46 +881,59 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         return *this;
       }
 
-      shared_future& operator=(shared_future&& __sf)
+      shared_future& operator=(shared_future&& __sf) noexcept
       {
         shared_future(std::move(__sf))._M_swap(*this);
         return *this;
       }
 
       // Retrieving the value
-      void 
+      void
       get() { this->_M_get_result(); }
     };
 
   // Now we can define the protected __basic_future constructors.
   template<typename _Res>
     inline __basic_future<_Res>::
-    __basic_future(const shared_future<_Res>& __sf)
+    __basic_future(const shared_future<_Res>& __sf) noexcept
     : _M_state(__sf._M_state)
     { }
 
   template<typename _Res>
     inline __basic_future<_Res>::
-    __basic_future(shared_future<_Res>&& __sf)
+    __basic_future(shared_future<_Res>&& __sf) noexcept
     : _M_state(std::move(__sf._M_state))
     { }
 
   template<typename _Res>
     inline __basic_future<_Res>::
-    __basic_future(future<_Res>&& __uf)
+    __basic_future(future<_Res>&& __uf) noexcept
     : _M_state(std::move(__uf._M_state))
     { }
 
+  template<typename _Res>
+    inline shared_future<_Res>
+    future<_Res>::share()
+    { return shared_future<_Res>(std::move(*this)); }
+
+  template<typename _Res>
+    inline shared_future<_Res&>
+    future<_Res&>::share()
+    { return shared_future<_Res&>(std::move(*this)); }
+
+  inline shared_future<void>
+  future<void>::share()
+  { return shared_future<void>(std::move(*this)); }
 
   /// Primary template for promise
   template<typename _Res>
     class promise
     {
-      typedef __future_base::_State            _State;
+      typedef __future_base::_State_base       _State;
       typedef __future_base::_Result<_Res>     _Res_type;
-      typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
+      typedef __future_base::_Ptr<_Res_type>   _Ptr_type;
       template<typename, typename> friend class _State::_Setter;
-      
+
       shared_ptr<_State>                        _M_future;
       _Ptr_type                                 _M_storage;
 
@@ -864,7 +943,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        _M_storage(new _Res_type())
       { }
 
-      promise(promise&& __rhs)
+      promise(promise&& __rhs) noexcept
       : _M_future(std::move(__rhs._M_future)),
        _M_storage(std::move(__rhs._M_storage))
       { }
@@ -875,6 +954,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          _M_storage(__future_base::_S_allocate_result<_Res>(__a))
         { }
 
+      template<typename _Allocator>
+        promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
+        : _M_future(std::move(__rhs._M_future)),
+         _M_storage(std::move(__rhs._M_storage))
+        { }
+
       promise(const promise&) = delete;
 
       ~promise()
@@ -885,7 +970,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       // Assignment
       promise&
-      operator=(promise&& __rhs)
+      operator=(promise&& __rhs) noexcept
       {
         promise(std::move(__rhs)).swap(*this);
         return *this;
@@ -894,7 +979,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       promise& operator=(const promise&) = delete;
 
       void
-      swap(promise& __rhs)
+      swap(promise& __rhs) noexcept
       {
         _M_future.swap(__rhs._M_future);
         _M_storage.swap(__rhs._M_storage);
@@ -930,7 +1015,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   template<typename _Res>
     inline void
-    swap(promise<_Res>& __x, promise<_Res>& __y)
+    swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
     { __x.swap(__y); }
 
   template<typename _Res, typename _Alloc>
@@ -942,9 +1027,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Res>
     class promise<_Res&>
     {
-      typedef __future_base::_State            _State;
+      typedef __future_base::_State_base       _State;
       typedef __future_base::_Result<_Res&>    _Res_type;
-      typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
+      typedef __future_base::_Ptr<_Res_type>   _Ptr_type;
       template<typename, typename> friend class _State::_Setter;
 
       shared_ptr<_State>                        _M_future;
@@ -956,8 +1041,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        _M_storage(new _Res_type())
       { }
 
-      promise(promise&& __rhs)
-      : _M_future(std::move(__rhs._M_future)), 
+      promise(promise&& __rhs) noexcept
+      : _M_future(std::move(__rhs._M_future)),
        _M_storage(std::move(__rhs._M_storage))
       { }
 
@@ -967,6 +1052,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
         { }
 
+      template<typename _Allocator>
+        promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
+        : _M_future(std::move(__rhs._M_future)),
+         _M_storage(std::move(__rhs._M_storage))
+        { }
+
       promise(const promise&) = delete;
 
       ~promise()
@@ -977,7 +1068,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       // Assignment
       promise&
-      operator=(promise&& __rhs)
+      operator=(promise&& __rhs) noexcept
       {
         promise(std::move(__rhs)).swap(*this);
         return *this;
@@ -986,7 +1077,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       promise& operator=(const promise&) = delete;
 
       void
-      swap(promise& __rhs)
+      swap(promise& __rhs) noexcept
       {
         _M_future.swap(__rhs._M_future);
         _M_storage.swap(__rhs._M_storage);
@@ -1017,9 +1108,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<>
     class promise<void>
     {
-      typedef __future_base::_State            _State;
+      typedef __future_base::_State_base       _State;
       typedef __future_base::_Result<void>     _Res_type;
-      typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
+      typedef __future_base::_Ptr<_Res_type>   _Ptr_type;
       template<typename, typename> friend class _State::_Setter;
 
       shared_ptr<_State>                        _M_future;
@@ -1031,7 +1122,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        _M_storage(new _Res_type())
       { }
 
-      promise(promise&& __rhs)
+      promise(promise&& __rhs) noexcept
       : _M_future(std::move(__rhs._M_future)),
        _M_storage(std::move(__rhs._M_storage))
       { }
@@ -1042,6 +1133,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          _M_storage(__future_base::_S_allocate_result<void>(__a))
         { }
 
+      template<typename _Allocator>
+        promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
+        : _M_future(std::move(__rhs._M_future)),
+         _M_storage(std::move(__rhs._M_storage))
+        { }
+
       promise(const promise&) = delete;
 
       ~promise()
@@ -1052,7 +1149,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       // Assignment
       promise&
-      operator=(promise&& __rhs)
+      operator=(promise&& __rhs) noexcept
       {
         promise(std::move(__rhs)).swap(*this);
         return *this;
@@ -1061,7 +1158,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       promise& operator=(const promise&) = delete;
 
       void
-      swap(promise& __rhs)
+      swap(promise& __rhs) noexcept
       {
         _M_future.swap(__rhs._M_future);
         _M_storage.swap(__rhs._M_storage);
@@ -1085,19 +1182,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   // set void
   template<>
-    struct __future_base::_State::_Setter<void, void>
+    struct __future_base::_State_base::_Setter<void, void>
     {
       promise<void>::_Ptr_type operator()()
       {
-        _State::_S_check(_M_promise->_M_future);
+        _State_base::_S_check(_M_promise->_M_future);
         return std::move(_M_promise->_M_storage);
       }
 
       promise<void>*    _M_promise;
     };
 
-  inline __future_base::_State::_Setter<void, void>
-  __future_base::_State::__setter(promise<void>* __prom)
+  inline __future_base::_State_base::_Setter<void, void>
+  __future_base::_State_base::__setter(promise<void>* __prom)
   {
     return _Setter<void, void>{ __prom };
   }
@@ -1110,29 +1207,29 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   }
 
 
-  template<typename _StateT, typename _Res>
+  template<typename _Ptr_type, typename _Res>
     struct __future_base::_Task_setter
     {
-      typename _StateT::_Ptr_type operator()()
+      _Ptr_type operator()()
       {
         __try
          {
-           _M_state->_M_result->_M_set(_M_fn());
+           _M_result->_M_set(_M_fn());
          }
        __catch(...)
          {
-           _M_state->_M_result->_M_error = current_exception();
+           _M_result->_M_error = current_exception();
          }
-        return std::move(_M_state->_M_result);
+        return std::move(_M_result);
       }
-      _StateT*                  _M_state;
+      _Ptr_type&                _M_result;
       std::function<_Res()>     _M_fn;
     };
 
-  template<typename _StateT>
-    struct __future_base::_Task_setter<_StateT, void>
+  template<typename _Ptr_type>
+    struct __future_base::_Task_setter<_Ptr_type, void>
     {
-      typename _StateT::_Ptr_type operator()()
+      _Ptr_type operator()()
       {
         __try
          {
@@ -1140,16 +1237,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          }
        __catch(...)
          {
-           _M_state->_M_result->_M_error = current_exception();
+           _M_result->_M_error = current_exception();
          }
-       return std::move(_M_state->_M_result);
+       return std::move(_M_result);
       }
-      _StateT*                  _M_state;
+      _Ptr_type&                _M_result;
       std::function<void()>     _M_fn;
     };
 
   template<typename _Res, typename... _Args>
-    struct __future_base::_Task_state<_Res(_Args...)> : __future_base::_State
+    struct __future_base::_Task_state<_Res(_Args...)> final
+    : __future_base::_State_base
     {
       typedef _Res _Res_type;
 
@@ -1167,14 +1265,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       _M_run(_Args... __args)
       {
         // bound arguments decay so wrap lvalue references
-        auto __bound = std::bind<_Res>(std::ref(_M_task),
-            _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
-        _Task_setter<_Task_state> __setter{ this, std::move(__bound) };
+       auto __boundfn = std::__bind_simple(std::ref(_M_task),
+           _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
+        auto __setter = _S_task_setter(_M_result, std::move(__boundfn));
         _M_set_result(std::move(__setter));
       }
 
-      template<typename, typename> friend class _Task_setter;
-      typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
+      typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
       _Ptr_type _M_result;
       std::function<_Res(_Args...)> _M_task;
 
@@ -1190,6 +1287,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         { return std::forward<_Tp>(__t); }
     };
 
+  template<typename _Task, typename _Fn, bool
+           = is_same<_Task, typename decay<_Fn>::type>::value>
+    struct __constrain_pkgdtask
+    { typedef void __type; };
+
+  template<typename _Task, typename _Fn>
+    struct __constrain_pkgdtask<_Task, _Fn, true>
+    { };
+
   /// packaged_task
   template<typename _Res, typename... _ArgTypes>
     class packaged_task<_Res(_ArgTypes...)>
@@ -1198,32 +1304,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       shared_ptr<_State_type>                   _M_state;
 
     public:
-      typedef _Res result_type;
-
       // Construction and destruction
-      packaged_task() { }
+      packaged_task() noexcept { }
 
-      template<typename _Fn>
+      template<typename _Allocator>
         explicit
-        packaged_task(const _Fn& __fn)
-        : _M_state(std::make_shared<_State_type>(__fn))
+        packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
         { }
 
-      template<typename _Fn>
+      template<typename _Fn, typename = typename
+               __constrain_pkgdtask<packaged_task, _Fn>::__type>
         explicit
         packaged_task(_Fn&& __fn)
-        : _M_state(std::make_shared<_State_type>(std::move(__fn)))
+        : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn)))
         { }
 
-      explicit
-      packaged_task(_Res(*__fn)(_ArgTypes...))
-      : _M_state(std::make_shared<_State_type>(__fn))
-      { }
-
-      template<typename _Fn, typename _Allocator>
+      template<typename _Fn, typename _Allocator, typename = typename
+               __constrain_pkgdtask<packaged_task, _Fn>::__type>
         explicit
-        packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn __fn)
-        : _M_state(std::allocate_shared<_State_type>(__a, std::move(__fn)))
+        packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn)
+        : _M_state(std::allocate_shared<_State_type>(__a,
+                                                     std::forward<_Fn>(__fn)))
         { }
 
       ~packaged_task()
@@ -1233,25 +1334,36 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       }
 
       // No copy
-      packaged_task(packaged_task&) = delete;
-      packaged_task& operator=(packaged_task&) = delete;
+      packaged_task(const packaged_task&) = delete;
+      packaged_task& operator=(const packaged_task&) = delete;
+
+      template<typename _Allocator>
+        explicit
+        packaged_task(allocator_arg_t, const _Allocator&,
+                      const packaged_task&) = delete;
 
       // Move support
-      packaged_task(packaged_task&& __other)
+      packaged_task(packaged_task&& __other) noexcept
       { this->swap(__other); }
 
-      packaged_task& operator=(packaged_task&& __other)
+      template<typename _Allocator>
+        explicit
+        packaged_task(allocator_arg_t, const _Allocator&,
+                      packaged_task&& __other) noexcept
+        { this->swap(__other); }
+
+      packaged_task& operator=(packaged_task&& __other) noexcept
       {
         packaged_task(std::move(__other)).swap(*this);
         return *this;
       }
 
       void
-      swap(packaged_task& __other)
+      swap(packaged_task& __other) noexcept
       { _M_state.swap(__other._M_state); }
 
       bool
-      valid() const
+      valid() const noexcept
       { return static_cast<bool>(_M_state); }
 
       // Result retrieval
@@ -1263,14 +1375,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       void
       operator()(_ArgTypes... __args)
       {
-        __future_base::_State::_S_check(_M_state);
+        __future_base::_State_base::_S_check(_M_state);
         _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
       }
 
       void
       reset()
       {
-        __future_base::_State::_S_check(_M_state);
+        __future_base::_State_base::_S_check(_M_state);
         packaged_task(std::move(_M_state->_M_task)).swap(*this);
       }
     };
@@ -1279,7 +1391,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Res, typename... _ArgTypes>
     inline void
     swap(packaged_task<_Res(_ArgTypes...)>& __x,
-        packaged_task<_Res(_ArgTypes...)>& __y)
+        packaged_task<_Res(_ArgTypes...)>& __y) noexcept
     { __x.swap(__y); }
 
   template<typename _Res, typename _Alloc>
@@ -1287,77 +1399,102 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     : public true_type { };
 
 
-  template<typename _Res>
-    class __future_base::_Deferred_state : public __future_base::_State
+  template<typename _BoundFn, typename _Res>
+    class __future_base::_Deferred_state final
+    : public __future_base::_State_base
     {
     public:
-      typedef _Res _Res_type;
-
       explicit
-      _Deferred_state(std::function<_Res()>&& __fn)
+      _Deferred_state(_BoundFn&& __fn)
       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
       { }
 
     private:
-      template<typename, typename> friend class _Task_setter;
-      typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
+      typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
       _Ptr_type _M_result;
-      std::function<_Res()> _M_fn;
+      _BoundFn _M_fn;
 
       virtual void
       _M_run_deferred()
       {
-        _Task_setter<_Deferred_state> __setter{ this, _M_fn };
         // safe to call multiple times so ignore failure
-        _M_set_result(std::move(__setter), true);
+        _M_set_result(_S_task_setter(_M_result, _M_fn), true);
       }
     };
 
-  template<typename _Res>
-    class __future_base::_Async_state : public __future_base::_State
-    {
-    public:
-      typedef _Res _Res_type;
+  class __future_base::_Async_state_common : public __future_base::_State_base
+  {
+  protected:
+#ifdef _GLIBCXX_HAVE_TLS
+    ~_Async_state_common();
+#else
+    ~_Async_state_common() { _M_join(); }
+#endif
 
-      explicit 
-      _Async_state(std::function<_Res()>&& __fn)
-      : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
-       _M_thread(mem_fn(&_Async_state::_M_do_run), this)
-      { }
+    // Allow non-timed waiting functions to block until the thread completes,
+    // as if joined.
+    virtual void _M_run_deferred() { _M_join(); }
 
-      ~_Async_state() { _M_thread.join(); }
+    void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
 
-    private:
-      void _M_do_run()
+    thread _M_thread;
+    once_flag _M_once;
+  };
+
+  template<typename _BoundFn, typename _Res>
+    class __future_base::_Async_state_impl final
+    : public __future_base::_Async_state_common
+    {
+    public:
+      explicit
+      _Async_state_impl(_BoundFn&& __fn)
+      : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
       {
-        _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) };
-        _M_set_result(std::move(__setter));
+       _M_thread = std::thread{ [this] {
+         _M_set_result(_S_task_setter(_M_result, _M_fn));
+        } };
       }
 
-      template<typename, typename> friend class _Task_setter;
-      typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
+    private:
+      typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
       _Ptr_type _M_result;
-      std::function<_Res()> _M_fn;
-      thread _M_thread;
+      _BoundFn _M_fn;
     };
 
-  /// async 
+  template<typename _BoundFn>
+    inline std::shared_ptr<__future_base::_State_base>
+    __future_base::_S_make_deferred_state(_BoundFn&& __fn)
+    {
+      typedef typename remove_reference<_BoundFn>::type __fn_type;
+      typedef _Deferred_state<__fn_type> __state_type;
+      return std::make_shared<__state_type>(std::move(__fn));
+    }
+
+  template<typename _BoundFn>
+    inline std::shared_ptr<__future_base::_State_base>
+    __future_base::_S_make_async_state(_BoundFn&& __fn)
+    {
+      typedef typename remove_reference<_BoundFn>::type __fn_type;
+      typedef _Async_state_impl<__fn_type> __state_type;
+      return std::make_shared<__state_type>(std::move(__fn));
+    }
+
+
+  /// async
   template<typename _Fn, typename... _Args>
     future<typename result_of<_Fn(_Args...)>::type>
     async(launch __policy, _Fn&& __fn, _Args&&... __args)
     {
       typedef typename result_of<_Fn(_Args...)>::type result_type;
-      std::shared_ptr<__future_base::_State> __state;
-      if (__policy == launch::async)
+      std::shared_ptr<__future_base::_State_base> __state;
+      if ((__policy & (launch::async|launch::deferred)) == launch::async)
        {
-         typedef typename __future_base::_Async_state<result_type> _State;
-         __state = std::make_shared<_State>(std::bind<result_type>(
+         __state = __future_base::_S_make_async_state(std::__bind_simple(
               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
        }
       else
        {
-         typedef typename __future_base::_Deferred_state<result_type> _State;
-         __state = std::make_shared<_State>(std::bind<result_type>(
+         __state = __future_base::_S_make_deferred_state(std::__bind_simple(
               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
        }
       return future<result_type>(__state);
@@ -1366,17 +1503,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   /// async, potential overload
   template<typename _Fn, typename... _Args>
     inline typename
-    enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
-              future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
-             >::type
+    __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type
     async(_Fn&& __fn, _Args&&... __args)
     {
-      return async(launch::any, std::forward<_Fn>(__fn),
+      return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
                   std::forward<_Args>(__args)...);
     }
 
 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
-       // && _GLIBCXX_ATOMIC_BUILTINS_4
+       // && ATOMIC_INT_LOCK_FREE
 
   // @} group futures
 _GLIBCXX_END_NAMESPACE_VERSION