Subversion Repositories Kolibri OS

Rev

Rev 5134 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. // <future> -*- C++ -*-
  2.  
  3. // Copyright (C) 2009-2013 Free Software Foundation, Inc.
  4. //
  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)
  9. // any later version.
  10.  
  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.
  15.  
  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.
  19.  
  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/>.
  24.  
  25. /** @file include/future
  26.  *  This is a Standard C++ Library header.
  27.  */
  28.  
  29. #ifndef _GLIBCXX_FUTURE
  30. #define _GLIBCXX_FUTURE 1
  31.  
  32. #pragma GCC system_header
  33.  
  34. #if __cplusplus < 201103L
  35. # include <bits/c++0x_warning.h>
  36. #else
  37.  
  38. #include <functional>
  39. #include <mutex>
  40. #include <thread>
  41. #include <condition_variable>
  42. #include <system_error>
  43. #include <atomic>
  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.  
  50. namespace std _GLIBCXX_VISIBILITY(default)
  51. {
  52. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  53.  
  54.   /**
  55.    * @defgroup futures Futures
  56.    * @ingroup concurrency
  57.    *
  58.    * Classes for futures support.
  59.    * @{
  60.    */
  61.  
  62.   /// Error code for futures
  63.   enum class future_errc
  64.   {
  65.     future_already_retrieved = 1,
  66.     promise_already_satisfied,
  67.     no_state,
  68.     broken_promise
  69.   };
  70.  
  71.   /// Specialization.
  72.   template<>
  73.     struct is_error_code_enum<future_errc> : public true_type { };
  74.  
  75.   /// Points to a statically-allocated object derived from error_category.
  76.   const error_category&
  77.   future_category() noexcept;
  78.  
  79.   /// Overload for make_error_code.
  80.   inline error_code
  81.   make_error_code(future_errc __errc) noexcept
  82.   { return error_code(static_cast<int>(__errc), future_category()); }
  83.  
  84.   /// Overload for make_error_condition.
  85.   inline error_condition
  86.   make_error_condition(future_errc __errc) noexcept
  87.   { return error_condition(static_cast<int>(__errc), future_category()); }
  88.  
  89.   /**
  90.    *  @brief Exception type thrown by futures.
  91.    *  @ingroup exceptions
  92.    */
  93.   class future_error : public logic_error
  94.   {
  95.     error_code                  _M_code;
  96.  
  97.   public:
  98.     explicit future_error(error_code __ec)
  99.     : logic_error("std::future_error"), _M_code(__ec)
  100.     { }
  101.  
  102.     virtual ~future_error() noexcept;
  103.  
  104.     virtual const char*
  105.     what() const noexcept;
  106.  
  107.     const error_code&
  108.     code() const noexcept { return _M_code; }
  109.   };
  110.  
  111.   // Forward declarations.
  112.   template<typename _Res>
  113.     class future;
  114.  
  115.   template<typename _Res>
  116.     class shared_future;
  117.  
  118.   template<typename _Signature>
  119.     class packaged_task;
  120.  
  121.   template<typename _Res>
  122.     class promise;
  123.  
  124.   /// Launch code for futures
  125.   enum class launch
  126.   {
  127.     async = 1,
  128.     deferred = 2
  129.   };
  130.  
  131.   constexpr launch operator&(launch __x, launch __y)
  132.   {
  133.     return static_cast<launch>(
  134.         static_cast<int>(__x) & static_cast<int>(__y));
  135.   }
  136.  
  137.   constexpr launch operator|(launch __x, launch __y)
  138.   {
  139.     return static_cast<launch>(
  140.         static_cast<int>(__x) | static_cast<int>(__y));
  141.   }
  142.  
  143.   constexpr launch operator^(launch __x, launch __y)
  144.   {
  145.     return static_cast<launch>(
  146.         static_cast<int>(__x) ^ static_cast<int>(__y));
  147.   }
  148.  
  149.   constexpr launch operator~(launch __x)
  150.   { return static_cast<launch>(~static_cast<int>(__x)); }
  151.  
  152.   inline launch& operator&=(launch& __x, launch __y)
  153.   { return __x = __x & __y; }
  154.  
  155.   inline launch& operator|=(launch& __x, launch __y)
  156.   { return __x = __x | __y; }
  157.  
  158.   inline launch& operator^=(launch& __x, launch __y)
  159.   { return __x = __x ^ __y; }
  160.  
  161.   /// Status code for futures
  162.   enum class future_status
  163.   {
  164.     ready,
  165.     timeout,
  166.     deferred
  167.   };
  168.  
  169.   template<typename _Fn, typename... _Args>
  170.     future<typename result_of<_Fn(_Args...)>::type>
  171.     async(launch __policy, _Fn&& __fn, _Args&&... __args);
  172.  
  173.   template<typename _Fn, typename... _Args>
  174.     future<typename result_of<_Fn(_Args...)>::type>
  175.     async(_Fn&& __fn, _Args&&... __args);
  176.  
  177. #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
  178.   && (ATOMIC_INT_LOCK_FREE > 1)
  179.  
  180.   /// Base class and enclosing scope.
  181.   struct __future_base
  182.   {
  183.     /// Base class for results.
  184.     struct _Result_base
  185.     {
  186.       exception_ptr             _M_error;
  187.  
  188.       _Result_base(const _Result_base&) = delete;
  189.       _Result_base& operator=(const _Result_base&) = delete;
  190.  
  191.       // _M_destroy() allows derived classes to control deallocation
  192.       virtual void _M_destroy() = 0;
  193.  
  194.       struct _Deleter
  195.       {
  196.         void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
  197.       };
  198.  
  199.     protected:
  200.       _Result_base();
  201.       virtual ~_Result_base();
  202.     };
  203.  
  204.     /// Result.
  205.     template<typename _Res>
  206.       struct _Result : _Result_base
  207.       {
  208.       private:
  209.         typedef alignment_of<_Res>                              __a_of;
  210.         typedef aligned_storage<sizeof(_Res), __a_of::value>    __align_storage;
  211.         typedef typename __align_storage::type                  __align_type;
  212.  
  213.         __align_type            _M_storage;
  214.         bool                    _M_initialized;
  215.  
  216.       public:
  217.         typedef _Res result_type;
  218.  
  219.         _Result() noexcept : _M_initialized() { }
  220.        
  221.         ~_Result()
  222.         {
  223.           if (_M_initialized)
  224.             _M_value().~_Res();
  225.         }
  226.  
  227.         // Return lvalue, future will add const or rvalue-reference
  228.         _Res&
  229.         _M_value() noexcept { return *static_cast<_Res*>(_M_addr()); }
  230.  
  231.         void
  232.         _M_set(const _Res& __res)
  233.         {
  234.           ::new (_M_addr()) _Res(__res);
  235.           _M_initialized = true;
  236.         }
  237.  
  238.         void
  239.         _M_set(_Res&& __res)
  240.         {
  241.           ::new (_M_addr()) _Res(std::move(__res));
  242.           _M_initialized = true;
  243.         }
  244.  
  245.       private:
  246.         void _M_destroy() { delete this; }
  247.  
  248.         void* _M_addr() noexcept { return static_cast<void*>(&_M_storage); }
  249.     };
  250.  
  251.     /// A unique_ptr based on the instantiating type.
  252.     template<typename _Res>
  253.       using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
  254.  
  255.     /// Result_alloc.
  256.     template<typename _Res, typename _Alloc>
  257.       struct _Result_alloc final : _Result<_Res>, _Alloc
  258.       {
  259.         typedef typename allocator_traits<_Alloc>::template
  260.           rebind_alloc<_Result_alloc> __allocator_type;
  261.  
  262.         explicit
  263.         _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
  264.         { }
  265.        
  266.       private:
  267.         void _M_destroy()
  268.         {
  269.           typedef allocator_traits<__allocator_type> __traits;
  270.           __allocator_type __a(*this);
  271.           __traits::destroy(__a, this);
  272.           __traits::deallocate(__a, this, 1);
  273.         }
  274.       };
  275.  
  276.     template<typename _Res, typename _Allocator>
  277.       static _Ptr<_Result_alloc<_Res, _Allocator>>
  278.       _S_allocate_result(const _Allocator& __a)
  279.       {
  280.         typedef _Result_alloc<_Res, _Allocator> __result_type;
  281.         typedef allocator_traits<typename __result_type::__allocator_type>
  282.           __traits;
  283.         typename __traits::allocator_type __a2(__a);
  284.         __result_type* __p = __traits::allocate(__a2, 1);
  285.         __try
  286.           {
  287.             __traits::construct(__a2, __p, __a);
  288.           }
  289.         __catch(...)
  290.           {
  291.             __traits::deallocate(__a2, __p, 1);
  292.             __throw_exception_again;
  293.           }
  294.         return _Ptr<__result_type>(__p);
  295.       }
  296.  
  297.     template<typename _Res, typename _Tp>
  298.       static _Ptr<_Result<_Res>>
  299.       _S_allocate_result(const std::allocator<_Tp>& __a)
  300.       {
  301.         return _Ptr<_Result<_Res>>(new _Result<_Res>);
  302.       }
  303.  
  304.     /// Base class for state between a promise and one or more
  305.     /// associated futures.
  306.     class _State_base
  307.     {
  308.       typedef _Ptr<_Result_base> _Ptr_type;
  309.  
  310.       _Ptr_type                 _M_result;
  311.       mutex                     _M_mutex;
  312.       condition_variable        _M_cond;
  313.       atomic_flag               _M_retrieved;
  314.       once_flag                 _M_once;
  315.  
  316.     public:
  317.       _State_base() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
  318.       _State_base(const _State_base&) = delete;
  319.       _State_base& operator=(const _State_base&) = delete;
  320.       virtual ~_State_base();
  321.  
  322.       _Result_base&
  323.       wait()
  324.       {
  325.         _M_run_deferred();
  326.         unique_lock<mutex> __lock(_M_mutex);
  327.         _M_cond.wait(__lock, [&] { return _M_ready(); });
  328.         return *_M_result;
  329.       }
  330.  
  331.       template<typename _Rep, typename _Period>
  332.         future_status
  333.         wait_for(const chrono::duration<_Rep, _Period>& __rel)
  334.         {
  335.           unique_lock<mutex> __lock(_M_mutex);
  336.           if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
  337.             return future_status::ready;
  338.           return future_status::timeout;
  339.         }
  340.  
  341.       template<typename _Clock, typename _Duration>
  342.         future_status
  343.         wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
  344.         {
  345.           unique_lock<mutex> __lock(_M_mutex);
  346.           if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
  347.             return future_status::ready;
  348.           return future_status::timeout;
  349.         }
  350.  
  351.       void
  352.       _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
  353.       {
  354.         bool __set = false;
  355.         // all calls to this function are serialized,
  356.         // side-effects of invoking __res only happen once
  357.         call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
  358.             ref(__set));
  359.         if (__set)
  360.           _M_cond.notify_all();
  361.         else if (!__ignore_failure)
  362.           __throw_future_error(int(future_errc::promise_already_satisfied));
  363.       }
  364.  
  365.       void
  366.       _M_break_promise(_Ptr_type __res)
  367.       {
  368.         if (static_cast<bool>(__res))
  369.           {
  370.             error_code __ec(make_error_code(future_errc::broken_promise));
  371.             __res->_M_error = copy_exception(future_error(__ec));
  372.             {
  373.               lock_guard<mutex> __lock(_M_mutex);
  374.               _M_result.swap(__res);
  375.             }
  376.             _M_cond.notify_all();
  377.           }
  378.       }
  379.  
  380.       // Called when this object is passed to a future.
  381.       void
  382.       _M_set_retrieved_flag()
  383.       {
  384.         if (_M_retrieved.test_and_set())
  385.           __throw_future_error(int(future_errc::future_already_retrieved));
  386.       }
  387.  
  388.       template<typename _Res, typename _Arg>
  389.         struct _Setter;
  390.  
  391.       // set lvalues
  392.       template<typename _Res, typename _Arg>
  393.         struct _Setter<_Res, _Arg&>
  394.         {
  395.           // check this is only used by promise<R>::set_value(const R&)
  396.           // or promise<R>::set_value(R&)
  397.           static_assert(is_same<_Res, _Arg&>::value  // promise<R&>
  398.               || is_same<const _Res, _Arg>::value,  // promise<R>
  399.               "Invalid specialisation");
  400.  
  401.           typename promise<_Res>::_Ptr_type operator()()
  402.           {
  403.             _State_base::_S_check(_M_promise->_M_future);
  404.             _M_promise->_M_storage->_M_set(_M_arg);
  405.             return std::move(_M_promise->_M_storage);
  406.           }
  407.           promise<_Res>*    _M_promise;
  408.           _Arg&             _M_arg;
  409.         };
  410.  
  411.       // set rvalues
  412.       template<typename _Res>
  413.         struct _Setter<_Res, _Res&&>
  414.         {
  415.           typename promise<_Res>::_Ptr_type operator()()
  416.           {
  417.             _State_base::_S_check(_M_promise->_M_future);
  418.             _M_promise->_M_storage->_M_set(std::move(_M_arg));
  419.             return std::move(_M_promise->_M_storage);
  420.           }
  421.           promise<_Res>*    _M_promise;
  422.           _Res&             _M_arg;
  423.         };
  424.  
  425.       struct __exception_ptr_tag { };
  426.  
  427.       // set exceptions
  428.       template<typename _Res>
  429.         struct _Setter<_Res, __exception_ptr_tag>
  430.         {
  431.           typename promise<_Res>::_Ptr_type operator()()
  432.           {
  433.             _State_base::_S_check(_M_promise->_M_future);
  434.             _M_promise->_M_storage->_M_error = _M_ex;
  435.             return std::move(_M_promise->_M_storage);
  436.           }
  437.  
  438.           promise<_Res>*   _M_promise;
  439.           exception_ptr&    _M_ex;
  440.         };
  441.  
  442.       template<typename _Res, typename _Arg>
  443.         static _Setter<_Res, _Arg&&>
  444.         __setter(promise<_Res>* __prom, _Arg&& __arg)
  445.         {
  446.           return _Setter<_Res, _Arg&&>{ __prom, __arg };
  447.         }
  448.  
  449.       template<typename _Res>
  450.         static _Setter<_Res, __exception_ptr_tag>
  451.         __setter(exception_ptr& __ex, promise<_Res>* __prom)
  452.         {
  453.           return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
  454.         }
  455.  
  456.       static _Setter<void, void>
  457.       __setter(promise<void>* __prom);
  458.  
  459.       template<typename _Tp>
  460.         static void
  461.         _S_check(const shared_ptr<_Tp>& __p)
  462.         {
  463.           if (!static_cast<bool>(__p))
  464.             __throw_future_error((int)future_errc::no_state);
  465.         }
  466.  
  467.     private:
  468.       void
  469.       _M_do_set(function<_Ptr_type()>& __f, bool& __set)
  470.       {
  471.         _Ptr_type __res = __f();
  472.         {
  473.           lock_guard<mutex> __lock(_M_mutex);
  474.           _M_result.swap(__res);
  475.         }
  476.         __set = true;
  477.       }
  478.  
  479.       bool _M_ready() const noexcept { return static_cast<bool>(_M_result); }
  480.  
  481.       // Misnamed: waits for completion of async function.
  482.       virtual void _M_run_deferred() { }
  483.     };
  484.  
  485.     template<typename _BoundFn, typename = typename _BoundFn::result_type>
  486.       class _Deferred_state;
  487.  
  488.     class _Async_state_common;
  489.  
  490.     template<typename _BoundFn, typename = typename _BoundFn::result_type>
  491.       class _Async_state_impl;
  492.  
  493.     template<typename _Signature>
  494.       class _Task_state_base;
  495.  
  496.     template<typename _Fn, typename _Alloc, typename _Signature>
  497.       class _Task_state;
  498.  
  499.     template<typename _BoundFn>
  500.       static std::shared_ptr<_State_base>
  501.       _S_make_deferred_state(_BoundFn&& __fn);
  502.  
  503.     template<typename _BoundFn>
  504.       static std::shared_ptr<_State_base>
  505.       _S_make_async_state(_BoundFn&& __fn);
  506.  
  507.     template<typename _Res_ptr,
  508.              typename _Res = typename _Res_ptr::element_type::result_type>
  509.       struct _Task_setter;
  510.  
  511.     template<typename _Res_ptr, typename _BoundFn>
  512.       static _Task_setter<_Res_ptr>
  513.       _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
  514.       {
  515.         return _Task_setter<_Res_ptr>{ __ptr, std::ref(__call) };
  516.       }
  517.   };
  518.  
  519.   /// Partial specialization for reference types.
  520.   template<typename _Res>
  521.     struct __future_base::_Result<_Res&> : __future_base::_Result_base
  522.     {
  523.       typedef _Res& result_type;
  524.  
  525.       _Result() noexcept : _M_value_ptr() { }
  526.  
  527.       void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
  528.  
  529.       _Res& _M_get() noexcept { return *_M_value_ptr; }
  530.  
  531.     private:
  532.       _Res*                     _M_value_ptr;
  533.  
  534.       void _M_destroy() { delete this; }
  535.     };
  536.  
  537.   /// Explicit specialization for void.
  538.   template<>
  539.     struct __future_base::_Result<void> : __future_base::_Result_base
  540.     {
  541.       typedef void result_type;
  542.  
  543.     private:
  544.       void _M_destroy() { delete this; }
  545.     };
  546.  
  547.  
  548.   /// Common implementation for future and shared_future.
  549.   template<typename _Res>
  550.     class __basic_future : public __future_base
  551.     {
  552.     protected:
  553.       typedef shared_ptr<_State_base>           __state_type;
  554.       typedef __future_base::_Result<_Res>&     __result_type;
  555.  
  556.     private:
  557.       __state_type              _M_state;
  558.  
  559.     public:
  560.       // Disable copying.
  561.       __basic_future(const __basic_future&) = delete;
  562.       __basic_future& operator=(const __basic_future&) = delete;
  563.  
  564.       bool
  565.       valid() const noexcept { return static_cast<bool>(_M_state); }
  566.  
  567.       void
  568.       wait() const
  569.       {
  570.         _State_base::_S_check(_M_state);
  571.         _M_state->wait();
  572.       }
  573.  
  574.       template<typename _Rep, typename _Period>
  575.         future_status
  576.         wait_for(const chrono::duration<_Rep, _Period>& __rel) const
  577.         {
  578.           _State_base::_S_check(_M_state);
  579.           return _M_state->wait_for(__rel);
  580.         }
  581.  
  582.       template<typename _Clock, typename _Duration>
  583.         future_status
  584.         wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
  585.         {
  586.           _State_base::_S_check(_M_state);
  587.           return _M_state->wait_until(__abs);
  588.         }
  589.  
  590.     protected:
  591.       /// Wait for the state to be ready and rethrow any stored exception
  592.       __result_type
  593.       _M_get_result() const
  594.       {
  595.         _State_base::_S_check(_M_state);
  596.         _Result_base& __res = _M_state->wait();
  597.         if (!(__res._M_error == 0))
  598.           rethrow_exception(__res._M_error);
  599.         return static_cast<__result_type>(__res);
  600.       }
  601.  
  602.       void _M_swap(__basic_future& __that) noexcept
  603.       {
  604.         _M_state.swap(__that._M_state);
  605.       }
  606.  
  607.       // Construction of a future by promise::get_future()
  608.       explicit
  609.       __basic_future(const __state_type& __state) : _M_state(__state)
  610.       {
  611.         _State_base::_S_check(_M_state);
  612.         _M_state->_M_set_retrieved_flag();
  613.       }
  614.  
  615.       // Copy construction from a shared_future
  616.       explicit
  617.       __basic_future(const shared_future<_Res>&) noexcept;
  618.  
  619.       // Move construction from a shared_future
  620.       explicit
  621.       __basic_future(shared_future<_Res>&&) noexcept;
  622.  
  623.       // Move construction from a future
  624.       explicit
  625.       __basic_future(future<_Res>&&) noexcept;
  626.  
  627.       constexpr __basic_future() noexcept : _M_state() { }
  628.  
  629.       struct _Reset
  630.       {
  631.         explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
  632.         ~_Reset() { _M_fut._M_state.reset(); }
  633.         __basic_future& _M_fut;
  634.       };
  635.     };
  636.  
  637.  
  638.   /// Primary template for future.
  639.   template<typename _Res>
  640.     class future : public __basic_future<_Res>
  641.     {
  642.       friend class promise<_Res>;
  643.       template<typename> friend class packaged_task;
  644.       template<typename _Fn, typename... _Args>
  645.         friend future<typename result_of<_Fn(_Args...)>::type>
  646.         async(launch, _Fn&&, _Args&&...);
  647.  
  648.       typedef __basic_future<_Res> _Base_type;
  649.       typedef typename _Base_type::__state_type __state_type;
  650.  
  651.       explicit
  652.       future(const __state_type& __state) : _Base_type(__state) { }
  653.  
  654.     public:
  655.       constexpr future() noexcept : _Base_type() { }
  656.  
  657.       /// Move constructor
  658.       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
  659.  
  660.       // Disable copying
  661.       future(const future&) = delete;
  662.       future& operator=(const future&) = delete;
  663.  
  664.       future& operator=(future&& __fut) noexcept
  665.       {
  666.         future(std::move(__fut))._M_swap(*this);
  667.         return *this;
  668.       }
  669.  
  670.       /// Retrieving the value
  671.       _Res
  672.       get()
  673.       {
  674.         typename _Base_type::_Reset __reset(*this);
  675.         return std::move(this->_M_get_result()._M_value());
  676.       }
  677.  
  678.       shared_future<_Res> share();
  679.     };
  680.  
  681.   /// Partial specialization for future<R&>
  682.   template<typename _Res>
  683.     class future<_Res&> : public __basic_future<_Res&>
  684.     {
  685.       friend class promise<_Res&>;
  686.       template<typename> friend class packaged_task;
  687.       template<typename _Fn, typename... _Args>
  688.         friend future<typename result_of<_Fn(_Args...)>::type>
  689.         async(launch, _Fn&&, _Args&&...);
  690.  
  691.       typedef __basic_future<_Res&> _Base_type;
  692.       typedef typename _Base_type::__state_type __state_type;
  693.  
  694.       explicit
  695.       future(const __state_type& __state) : _Base_type(__state) { }
  696.  
  697.     public:
  698.       constexpr future() noexcept : _Base_type() { }
  699.  
  700.       /// Move constructor
  701.       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
  702.  
  703.       // Disable copying
  704.       future(const future&) = delete;
  705.       future& operator=(const future&) = delete;
  706.  
  707.       future& operator=(future&& __fut) noexcept
  708.       {
  709.         future(std::move(__fut))._M_swap(*this);
  710.         return *this;
  711.       }
  712.  
  713.       /// Retrieving the value
  714.       _Res&
  715.       get()
  716.       {
  717.         typename _Base_type::_Reset __reset(*this);
  718.         return this->_M_get_result()._M_get();
  719.       }
  720.  
  721.       shared_future<_Res&> share();
  722.     };
  723.  
  724.   /// Explicit specialization for future<void>
  725.   template<>
  726.     class future<void> : public __basic_future<void>
  727.     {
  728.       friend class promise<void>;
  729.       template<typename> friend class packaged_task;
  730.       template<typename _Fn, typename... _Args>
  731.         friend future<typename result_of<_Fn(_Args...)>::type>
  732.         async(launch, _Fn&&, _Args&&...);
  733.  
  734.       typedef __basic_future<void> _Base_type;
  735.       typedef typename _Base_type::__state_type __state_type;
  736.  
  737.       explicit
  738.       future(const __state_type& __state) : _Base_type(__state) { }
  739.  
  740.     public:
  741.       constexpr future() noexcept : _Base_type() { }
  742.  
  743.       /// Move constructor
  744.       future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
  745.  
  746.       // Disable copying
  747.       future(const future&) = delete;
  748.       future& operator=(const future&) = delete;
  749.  
  750.       future& operator=(future&& __fut) noexcept
  751.       {
  752.         future(std::move(__fut))._M_swap(*this);
  753.         return *this;
  754.       }
  755.  
  756.       /// Retrieving the value
  757.       void
  758.       get()
  759.       {
  760.         typename _Base_type::_Reset __reset(*this);
  761.         this->_M_get_result();
  762.       }
  763.  
  764.       shared_future<void> share();
  765.     };
  766.  
  767.  
  768.   /// Primary template for shared_future.
  769.   template<typename _Res>
  770.     class shared_future : public __basic_future<_Res>
  771.     {
  772.       typedef __basic_future<_Res> _Base_type;
  773.  
  774.     public:
  775.       constexpr shared_future() noexcept : _Base_type() { }
  776.  
  777.       /// Copy constructor
  778.       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
  779.  
  780.       /// Construct from a future rvalue
  781.       shared_future(future<_Res>&& __uf) noexcept
  782.       : _Base_type(std::move(__uf))
  783.       { }
  784.  
  785.       /// Construct from a shared_future rvalue
  786.       shared_future(shared_future&& __sf) noexcept
  787.       : _Base_type(std::move(__sf))
  788.       { }
  789.  
  790.       shared_future& operator=(const shared_future& __sf)
  791.       {
  792.         shared_future(__sf)._M_swap(*this);
  793.         return *this;
  794.       }
  795.  
  796.       shared_future& operator=(shared_future&& __sf) noexcept
  797.       {
  798.         shared_future(std::move(__sf))._M_swap(*this);
  799.         return *this;
  800.       }
  801.  
  802.       /// Retrieving the value
  803.       const _Res&
  804.       get() const { return this->_M_get_result()._M_value(); }
  805.     };
  806.  
  807.   /// Partial specialization for shared_future<R&>
  808.   template<typename _Res>
  809.     class shared_future<_Res&> : public __basic_future<_Res&>
  810.     {
  811.       typedef __basic_future<_Res&>           _Base_type;
  812.  
  813.     public:
  814.       constexpr shared_future() noexcept : _Base_type() { }
  815.  
  816.       /// Copy constructor
  817.       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
  818.  
  819.       /// Construct from a future rvalue
  820.       shared_future(future<_Res&>&& __uf) noexcept
  821.       : _Base_type(std::move(__uf))
  822.       { }
  823.  
  824.       /// Construct from a shared_future rvalue
  825.       shared_future(shared_future&& __sf) noexcept
  826.       : _Base_type(std::move(__sf))
  827.       { }
  828.  
  829.       shared_future& operator=(const shared_future& __sf)
  830.       {
  831.         shared_future(__sf)._M_swap(*this);
  832.         return *this;
  833.       }
  834.  
  835.       shared_future& operator=(shared_future&& __sf) noexcept
  836.       {
  837.         shared_future(std::move(__sf))._M_swap(*this);
  838.         return *this;
  839.       }
  840.  
  841.       /// Retrieving the value
  842.       _Res&
  843.       get() const { return this->_M_get_result()._M_get(); }
  844.     };
  845.  
  846.   /// Explicit specialization for shared_future<void>
  847.   template<>
  848.     class shared_future<void> : public __basic_future<void>
  849.     {
  850.       typedef __basic_future<void> _Base_type;
  851.  
  852.     public:
  853.       constexpr shared_future() noexcept : _Base_type() { }
  854.  
  855.       /// Copy constructor
  856.       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
  857.  
  858.       /// Construct from a future rvalue
  859.       shared_future(future<void>&& __uf) noexcept
  860.       : _Base_type(std::move(__uf))
  861.       { }
  862.  
  863.       /// Construct from a shared_future rvalue
  864.       shared_future(shared_future&& __sf) noexcept
  865.       : _Base_type(std::move(__sf))
  866.       { }
  867.  
  868.       shared_future& operator=(const shared_future& __sf)
  869.       {
  870.         shared_future(__sf)._M_swap(*this);
  871.         return *this;
  872.       }
  873.  
  874.       shared_future& operator=(shared_future&& __sf) noexcept
  875.       {
  876.         shared_future(std::move(__sf))._M_swap(*this);
  877.         return *this;
  878.       }
  879.  
  880.       // Retrieving the value
  881.       void
  882.       get() const { this->_M_get_result(); }
  883.     };
  884.  
  885.   // Now we can define the protected __basic_future constructors.
  886.   template<typename _Res>
  887.     inline __basic_future<_Res>::
  888.     __basic_future(const shared_future<_Res>& __sf) noexcept
  889.     : _M_state(__sf._M_state)
  890.     { }
  891.  
  892.   template<typename _Res>
  893.     inline __basic_future<_Res>::
  894.     __basic_future(shared_future<_Res>&& __sf) noexcept
  895.     : _M_state(std::move(__sf._M_state))
  896.     { }
  897.  
  898.   template<typename _Res>
  899.     inline __basic_future<_Res>::
  900.     __basic_future(future<_Res>&& __uf) noexcept
  901.     : _M_state(std::move(__uf._M_state))
  902.     { }
  903.  
  904.   template<typename _Res>
  905.     inline shared_future<_Res>
  906.     future<_Res>::share()
  907.     { return shared_future<_Res>(std::move(*this)); }
  908.  
  909.   template<typename _Res>
  910.     inline shared_future<_Res&>
  911.     future<_Res&>::share()
  912.     { return shared_future<_Res&>(std::move(*this)); }
  913.  
  914.   inline shared_future<void>
  915.   future<void>::share()
  916.   { return shared_future<void>(std::move(*this)); }
  917.  
  918.   /// Primary template for promise
  919.   template<typename _Res>
  920.     class promise
  921.     {
  922.       typedef __future_base::_State_base        _State;
  923.       typedef __future_base::_Result<_Res>      _Res_type;
  924.       typedef __future_base::_Ptr<_Res_type>    _Ptr_type;
  925.       template<typename, typename> friend class _State::_Setter;
  926.  
  927.       shared_ptr<_State>                        _M_future;
  928.       _Ptr_type                                 _M_storage;
  929.  
  930.     public:
  931.       promise()
  932.       : _M_future(std::make_shared<_State>()),
  933.         _M_storage(new _Res_type())
  934.       { }
  935.  
  936.       promise(promise&& __rhs) noexcept
  937.       : _M_future(std::move(__rhs._M_future)),
  938.         _M_storage(std::move(__rhs._M_storage))
  939.       { }
  940.  
  941.       template<typename _Allocator>
  942.         promise(allocator_arg_t, const _Allocator& __a)
  943.         : _M_future(std::allocate_shared<_State>(__a)),
  944.           _M_storage(__future_base::_S_allocate_result<_Res>(__a))
  945.         { }
  946.  
  947.       template<typename _Allocator>
  948.         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
  949.         : _M_future(std::move(__rhs._M_future)),
  950.           _M_storage(std::move(__rhs._M_storage))
  951.         { }
  952.  
  953.       promise(const promise&) = delete;
  954.  
  955.       ~promise()
  956.       {
  957.         if (static_cast<bool>(_M_future) && !_M_future.unique())
  958.           _M_future->_M_break_promise(std::move(_M_storage));
  959.       }
  960.  
  961.       // Assignment
  962.       promise&
  963.       operator=(promise&& __rhs) noexcept
  964.       {
  965.         promise(std::move(__rhs)).swap(*this);
  966.         return *this;
  967.       }
  968.  
  969.       promise& operator=(const promise&) = delete;
  970.  
  971.       void
  972.       swap(promise& __rhs) noexcept
  973.       {
  974.         _M_future.swap(__rhs._M_future);
  975.         _M_storage.swap(__rhs._M_storage);
  976.       }
  977.  
  978.       // Retrieving the result
  979.       future<_Res>
  980.       get_future()
  981.       { return future<_Res>(_M_future); }
  982.  
  983.       // Setting the result
  984.       void
  985.       set_value(const _Res& __r)
  986.       {
  987.         auto __future = _M_future;
  988.         auto __setter = _State::__setter(this, __r);
  989.         __future->_M_set_result(std::move(__setter));
  990.       }
  991.  
  992.       void
  993.       set_value(_Res&& __r)
  994.       {
  995.         auto __future = _M_future;
  996.         auto __setter = _State::__setter(this, std::move(__r));
  997.         __future->_M_set_result(std::move(__setter));
  998.       }
  999.  
  1000.       void
  1001.       set_exception(exception_ptr __p)
  1002.       {
  1003.         auto __future = _M_future;
  1004.         auto __setter = _State::__setter(__p, this);
  1005.         __future->_M_set_result(std::move(__setter));
  1006.       }
  1007.     };
  1008.  
  1009.   template<typename _Res>
  1010.     inline void
  1011.     swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
  1012.     { __x.swap(__y); }
  1013.  
  1014.   template<typename _Res, typename _Alloc>
  1015.     struct uses_allocator<promise<_Res>, _Alloc>
  1016.     : public true_type { };
  1017.  
  1018.  
  1019.   /// Partial specialization for promise<R&>
  1020.   template<typename _Res>
  1021.     class promise<_Res&>
  1022.     {
  1023.       typedef __future_base::_State_base        _State;
  1024.       typedef __future_base::_Result<_Res&>     _Res_type;
  1025.       typedef __future_base::_Ptr<_Res_type>    _Ptr_type;
  1026.       template<typename, typename> friend class _State::_Setter;
  1027.  
  1028.       shared_ptr<_State>                        _M_future;
  1029.       _Ptr_type                                 _M_storage;
  1030.  
  1031.     public:
  1032.       promise()
  1033.       : _M_future(std::make_shared<_State>()),
  1034.         _M_storage(new _Res_type())
  1035.       { }
  1036.  
  1037.       promise(promise&& __rhs) noexcept
  1038.       : _M_future(std::move(__rhs._M_future)),
  1039.         _M_storage(std::move(__rhs._M_storage))
  1040.       { }
  1041.  
  1042.       template<typename _Allocator>
  1043.         promise(allocator_arg_t, const _Allocator& __a)
  1044.         : _M_future(std::allocate_shared<_State>(__a)),
  1045.           _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
  1046.         { }
  1047.  
  1048.       template<typename _Allocator>
  1049.         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
  1050.         : _M_future(std::move(__rhs._M_future)),
  1051.           _M_storage(std::move(__rhs._M_storage))
  1052.         { }
  1053.  
  1054.       promise(const promise&) = delete;
  1055.  
  1056.       ~promise()
  1057.       {
  1058.         if (static_cast<bool>(_M_future) && !_M_future.unique())
  1059.           _M_future->_M_break_promise(std::move(_M_storage));
  1060.       }
  1061.  
  1062.       // Assignment
  1063.       promise&
  1064.       operator=(promise&& __rhs) noexcept
  1065.       {
  1066.         promise(std::move(__rhs)).swap(*this);
  1067.         return *this;
  1068.       }
  1069.  
  1070.       promise& operator=(const promise&) = delete;
  1071.  
  1072.       void
  1073.       swap(promise& __rhs) noexcept
  1074.       {
  1075.         _M_future.swap(__rhs._M_future);
  1076.         _M_storage.swap(__rhs._M_storage);
  1077.       }
  1078.  
  1079.       // Retrieving the result
  1080.       future<_Res&>
  1081.       get_future()
  1082.       { return future<_Res&>(_M_future); }
  1083.  
  1084.       // Setting the result
  1085.       void
  1086.       set_value(_Res& __r)
  1087.       {
  1088.         auto __future = _M_future;
  1089.         auto __setter = _State::__setter(this, __r);
  1090.         __future->_M_set_result(std::move(__setter));
  1091.       }
  1092.  
  1093.       void
  1094.       set_exception(exception_ptr __p)
  1095.       {
  1096.         auto __future = _M_future;
  1097.         auto __setter = _State::__setter(__p, this);
  1098.         __future->_M_set_result(std::move(__setter));
  1099.       }
  1100.     };
  1101.  
  1102.   /// Explicit specialization for promise<void>
  1103.   template<>
  1104.     class promise<void>
  1105.     {
  1106.       typedef __future_base::_State_base        _State;
  1107.       typedef __future_base::_Result<void>      _Res_type;
  1108.       typedef __future_base::_Ptr<_Res_type>    _Ptr_type;
  1109.       template<typename, typename> friend class _State::_Setter;
  1110.  
  1111.       shared_ptr<_State>                        _M_future;
  1112.       _Ptr_type                                 _M_storage;
  1113.  
  1114.     public:
  1115.       promise()
  1116.       : _M_future(std::make_shared<_State>()),
  1117.         _M_storage(new _Res_type())
  1118.       { }
  1119.  
  1120.       promise(promise&& __rhs) noexcept
  1121.       : _M_future(std::move(__rhs._M_future)),
  1122.         _M_storage(std::move(__rhs._M_storage))
  1123.       { }
  1124.  
  1125.       template<typename _Allocator>
  1126.         promise(allocator_arg_t, const _Allocator& __a)
  1127.         : _M_future(std::allocate_shared<_State>(__a)),
  1128.           _M_storage(__future_base::_S_allocate_result<void>(__a))
  1129.         { }
  1130.  
  1131.       // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1132.       // 2095.  missing constructors needed for uses-allocator construction
  1133.       template<typename _Allocator>
  1134.         promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
  1135.         : _M_future(std::move(__rhs._M_future)),
  1136.           _M_storage(std::move(__rhs._M_storage))
  1137.         { }
  1138.  
  1139.       promise(const promise&) = delete;
  1140.  
  1141.       ~promise()
  1142.       {
  1143.         if (static_cast<bool>(_M_future) && !_M_future.unique())
  1144.           _M_future->_M_break_promise(std::move(_M_storage));
  1145.       }
  1146.  
  1147.       // Assignment
  1148.       promise&
  1149.       operator=(promise&& __rhs) noexcept
  1150.       {
  1151.         promise(std::move(__rhs)).swap(*this);
  1152.         return *this;
  1153.       }
  1154.  
  1155.       promise& operator=(const promise&) = delete;
  1156.  
  1157.       void
  1158.       swap(promise& __rhs) noexcept
  1159.       {
  1160.         _M_future.swap(__rhs._M_future);
  1161.         _M_storage.swap(__rhs._M_storage);
  1162.       }
  1163.  
  1164.       // Retrieving the result
  1165.       future<void>
  1166.       get_future()
  1167.       { return future<void>(_M_future); }
  1168.  
  1169.       // Setting the result
  1170.       void set_value();
  1171.  
  1172.       void
  1173.       set_exception(exception_ptr __p)
  1174.       {
  1175.         auto __future = _M_future;
  1176.         auto __setter = _State::__setter(__p, this);
  1177.         __future->_M_set_result(std::move(__setter));
  1178.       }
  1179.     };
  1180.  
  1181.   // set void
  1182.   template<>
  1183.     struct __future_base::_State_base::_Setter<void, void>
  1184.     {
  1185.       promise<void>::_Ptr_type operator()()
  1186.       {
  1187.         _State_base::_S_check(_M_promise->_M_future);
  1188.         return std::move(_M_promise->_M_storage);
  1189.       }
  1190.  
  1191.       promise<void>*    _M_promise;
  1192.     };
  1193.  
  1194.   inline __future_base::_State_base::_Setter<void, void>
  1195.   __future_base::_State_base::__setter(promise<void>* __prom)
  1196.   {
  1197.     return _Setter<void, void>{ __prom };
  1198.   }
  1199.  
  1200.   inline void
  1201.   promise<void>::set_value()
  1202.   {
  1203.     auto __future = _M_future;
  1204.     auto __setter = _State::__setter(this);
  1205.     __future->_M_set_result(std::move(__setter));
  1206.   }
  1207.  
  1208.  
  1209.   template<typename _Ptr_type, typename _Res>
  1210.     struct __future_base::_Task_setter
  1211.     {
  1212.       _Ptr_type operator()()
  1213.       {
  1214.         __try
  1215.           {
  1216.             _M_result->_M_set(_M_fn());
  1217.           }
  1218.         __catch(...)
  1219.           {
  1220.             _M_result->_M_error = current_exception();
  1221.           }
  1222.         return std::move(_M_result);
  1223.       }
  1224.       _Ptr_type&                _M_result;
  1225.       std::function<_Res()>     _M_fn;
  1226.     };
  1227.  
  1228.   template<typename _Ptr_type>
  1229.     struct __future_base::_Task_setter<_Ptr_type, void>
  1230.     {
  1231.       _Ptr_type operator()()
  1232.       {
  1233.         __try
  1234.           {
  1235.             _M_fn();
  1236.           }
  1237.         __catch(...)
  1238.           {
  1239.             _M_result->_M_error = current_exception();
  1240.           }
  1241.         return std::move(_M_result);
  1242.       }
  1243.       _Ptr_type&                _M_result;
  1244.       std::function<void()>     _M_fn;
  1245.     };
  1246.  
  1247.   template<typename _Res, typename... _Args>
  1248.     struct __future_base::_Task_state_base<_Res(_Args...)>
  1249.     : __future_base::_State_base
  1250.     {
  1251.       typedef _Res _Res_type;
  1252.  
  1253.       template<typename _Alloc>
  1254.         _Task_state_base(const _Alloc& __a)
  1255.         : _M_result(_S_allocate_result<_Res>(__a))
  1256.         { }
  1257.  
  1258.       virtual void
  1259.       _M_run(_Args... __args) = 0;
  1260.  
  1261.       virtual shared_ptr<_Task_state_base>
  1262.       _M_reset() = 0;
  1263.  
  1264.       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
  1265.       _Ptr_type _M_result;
  1266.     };
  1267.  
  1268.   template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
  1269.     struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
  1270.     : __future_base::_Task_state_base<_Res(_Args...)>
  1271.     {
  1272.       template<typename _Fn2>
  1273.         _Task_state(_Fn2&& __fn, const _Alloc& __a)
  1274.         : _Task_state_base<_Res(_Args...)>(__a),
  1275.           _M_impl(std::forward<_Fn2>(__fn), __a)
  1276.         { }
  1277.  
  1278.     private:
  1279.       virtual void
  1280.       _M_run(_Args... __args)
  1281.       {
  1282.         // bound arguments decay so wrap lvalue references
  1283.         auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
  1284.             _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
  1285.         auto __setter = _S_task_setter(this->_M_result, std::move(__boundfn));
  1286.         this->_M_set_result(std::move(__setter));
  1287.       }
  1288.  
  1289.       virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
  1290.       _M_reset();
  1291.  
  1292.       template<typename _Tp>
  1293.         static reference_wrapper<_Tp>
  1294.         _S_maybe_wrap_ref(_Tp& __t)
  1295.         { return std::ref(__t); }
  1296.  
  1297.       template<typename _Tp>
  1298.         static
  1299.         typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
  1300.         _S_maybe_wrap_ref(_Tp&& __t)
  1301.         { return std::forward<_Tp>(__t); }
  1302.  
  1303.       struct _Impl : _Alloc
  1304.       {
  1305.         template<typename _Fn2>
  1306.           _Impl(_Fn2&& __fn, const _Alloc& __a)
  1307.           : _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { }
  1308.         _Fn _M_fn;
  1309.       } _M_impl;
  1310.     };
  1311.  
  1312.   template<typename _Signature, typename _Fn, typename _Alloc>
  1313.     static shared_ptr<__future_base::_Task_state_base<_Signature>>
  1314.     __create_task_state(_Fn&& __fn, const _Alloc& __a)
  1315.     {
  1316.       typedef typename decay<_Fn>::type _Fn2;
  1317.       typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
  1318.       return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
  1319.     }
  1320.  
  1321.   template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
  1322.     shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
  1323.     __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
  1324.     {
  1325.       return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
  1326.                                                  static_cast<_Alloc&>(_M_impl));
  1327.     }
  1328.  
  1329.   template<typename _Task, typename _Fn, bool
  1330.            = is_same<_Task, typename decay<_Fn>::type>::value>
  1331.     struct __constrain_pkgdtask
  1332.     { typedef void __type; };
  1333.  
  1334.   template<typename _Task, typename _Fn>
  1335.     struct __constrain_pkgdtask<_Task, _Fn, true>
  1336.     { };
  1337.  
  1338.   /// packaged_task
  1339.   template<typename _Res, typename... _ArgTypes>
  1340.     class packaged_task<_Res(_ArgTypes...)>
  1341.     {
  1342.       typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
  1343.       shared_ptr<_State_type>                   _M_state;
  1344.  
  1345.     public:
  1346.       // Construction and destruction
  1347.       packaged_task() noexcept { }
  1348.  
  1349.       // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1350.       // 2095.  missing constructors needed for uses-allocator construction
  1351.       template<typename _Allocator>
  1352.         packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
  1353.         { }
  1354.  
  1355.       template<typename _Fn, typename = typename
  1356.                __constrain_pkgdtask<packaged_task, _Fn>::__type>
  1357.         explicit
  1358.         packaged_task(_Fn&& __fn)
  1359.         : packaged_task(allocator_arg, std::allocator<int>(),
  1360.                         std::forward<_Fn>(__fn))
  1361.         { }
  1362.  
  1363.       // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1364.       // 2097.  packaged_task constructors should be constrained
  1365.       template<typename _Fn, typename _Alloc, typename = typename
  1366.                __constrain_pkgdtask<packaged_task, _Fn>::__type>
  1367.         explicit
  1368.         packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
  1369.         : _M_state(__create_task_state<_Res(_ArgTypes...)>(
  1370.                     std::forward<_Fn>(__fn), __a))
  1371.         { }
  1372.  
  1373.       ~packaged_task()
  1374.       {
  1375.         if (static_cast<bool>(_M_state) && !_M_state.unique())
  1376.           _M_state->_M_break_promise(std::move(_M_state->_M_result));
  1377.       }
  1378.  
  1379.       // No copy
  1380.       packaged_task(const packaged_task&) = delete;
  1381.       packaged_task& operator=(const packaged_task&) = delete;
  1382.  
  1383.       template<typename _Allocator>
  1384.         packaged_task(allocator_arg_t, const _Allocator&,
  1385.                       const packaged_task&) = delete;
  1386.  
  1387.       // Move support
  1388.       packaged_task(packaged_task&& __other) noexcept
  1389.       { this->swap(__other); }
  1390.  
  1391.       template<typename _Allocator>
  1392.         packaged_task(allocator_arg_t, const _Allocator&,
  1393.                       packaged_task&& __other) noexcept
  1394.         { this->swap(__other); }
  1395.  
  1396.       packaged_task& operator=(packaged_task&& __other) noexcept
  1397.       {
  1398.         packaged_task(std::move(__other)).swap(*this);
  1399.         return *this;
  1400.       }
  1401.  
  1402.       void
  1403.       swap(packaged_task& __other) noexcept
  1404.       { _M_state.swap(__other._M_state); }
  1405.  
  1406.       bool
  1407.       valid() const noexcept
  1408.       { return static_cast<bool>(_M_state); }
  1409.  
  1410.       // Result retrieval
  1411.       future<_Res>
  1412.       get_future()
  1413.       { return future<_Res>(_M_state); }
  1414.  
  1415.       // Execution
  1416.       void
  1417.       operator()(_ArgTypes... __args)
  1418.       {
  1419.         __future_base::_State_base::_S_check(_M_state);
  1420.         auto __state = _M_state;
  1421.         __state->_M_run(std::forward<_ArgTypes>(__args)...);
  1422.       }
  1423.  
  1424.       void
  1425.       reset()
  1426.       {
  1427.         __future_base::_State_base::_S_check(_M_state);
  1428.         packaged_task __tmp;
  1429.         __tmp._M_state = _M_state;
  1430.         _M_state = _M_state->_M_reset();
  1431.       }
  1432.     };
  1433.  
  1434.   /// swap
  1435.   template<typename _Res, typename... _ArgTypes>
  1436.     inline void
  1437.     swap(packaged_task<_Res(_ArgTypes...)>& __x,
  1438.          packaged_task<_Res(_ArgTypes...)>& __y) noexcept
  1439.     { __x.swap(__y); }
  1440.  
  1441.   template<typename _Res, typename _Alloc>
  1442.     struct uses_allocator<packaged_task<_Res>, _Alloc>
  1443.     : public true_type { };
  1444.  
  1445.  
  1446.   template<typename _BoundFn, typename _Res>
  1447.     class __future_base::_Deferred_state final
  1448.     : public __future_base::_State_base
  1449.     {
  1450.     public:
  1451.       explicit
  1452.       _Deferred_state(_BoundFn&& __fn)
  1453.       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
  1454.       { }
  1455.  
  1456.     private:
  1457.       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
  1458.       _Ptr_type _M_result;
  1459.       _BoundFn _M_fn;
  1460.  
  1461.       virtual void
  1462.       _M_run_deferred()
  1463.       {
  1464.         // safe to call multiple times so ignore failure
  1465.         _M_set_result(_S_task_setter(_M_result, _M_fn), true);
  1466.       }
  1467.     };
  1468.  
  1469.   class __future_base::_Async_state_common : public __future_base::_State_base
  1470.   {
  1471.   protected:
  1472. #ifdef _GLIBCXX_ASYNC_ABI_COMPAT
  1473.     ~_Async_state_common();
  1474. #else
  1475.     ~_Async_state_common() = default;
  1476. #endif
  1477.  
  1478.     // Allow non-timed waiting functions to block until the thread completes,
  1479.     // as if joined.
  1480.     virtual void _M_run_deferred() { _M_join(); }
  1481.  
  1482.     void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
  1483.  
  1484.     thread _M_thread;
  1485.     once_flag _M_once;
  1486.   };
  1487.  
  1488.   template<typename _BoundFn, typename _Res>
  1489.     class __future_base::_Async_state_impl final
  1490.     : public __future_base::_Async_state_common
  1491.     {
  1492.     public:
  1493.       explicit
  1494.       _Async_state_impl(_BoundFn&& __fn)
  1495.       : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
  1496.       {
  1497.         _M_thread = std::thread{ [this] {
  1498.           _M_set_result(_S_task_setter(_M_result, _M_fn));
  1499.         } };
  1500.       }
  1501.  
  1502.       ~_Async_state_impl() { _M_join(); }
  1503.  
  1504.     private:
  1505.       typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
  1506.       _Ptr_type _M_result;
  1507.       _BoundFn _M_fn;
  1508.     };
  1509.  
  1510.   template<typename _BoundFn>
  1511.     inline std::shared_ptr<__future_base::_State_base>
  1512.     __future_base::_S_make_deferred_state(_BoundFn&& __fn)
  1513.     {
  1514.       typedef typename remove_reference<_BoundFn>::type __fn_type;
  1515.       typedef _Deferred_state<__fn_type> __state_type;
  1516.       return std::make_shared<__state_type>(std::move(__fn));
  1517.     }
  1518.  
  1519.   template<typename _BoundFn>
  1520.     inline std::shared_ptr<__future_base::_State_base>
  1521.     __future_base::_S_make_async_state(_BoundFn&& __fn)
  1522.     {
  1523.       typedef typename remove_reference<_BoundFn>::type __fn_type;
  1524.       typedef _Async_state_impl<__fn_type> __state_type;
  1525.       return std::make_shared<__state_type>(std::move(__fn));
  1526.     }
  1527.  
  1528.  
  1529.   /// async
  1530.   template<typename _Fn, typename... _Args>
  1531.     future<typename result_of<_Fn(_Args...)>::type>
  1532.     async(launch __policy, _Fn&& __fn, _Args&&... __args)
  1533.     {
  1534.       typedef typename result_of<_Fn(_Args...)>::type result_type;
  1535.       std::shared_ptr<__future_base::_State_base> __state;
  1536.       if ((__policy & (launch::async|launch::deferred)) == launch::async)
  1537.         {
  1538.           __state = __future_base::_S_make_async_state(std::__bind_simple(
  1539.               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
  1540.         }
  1541.       else
  1542.         {
  1543.           __state = __future_base::_S_make_deferred_state(std::__bind_simple(
  1544.               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
  1545.         }
  1546.       return future<result_type>(__state);
  1547.     }
  1548.  
  1549.   /// async, potential overload
  1550.   template<typename _Fn, typename... _Args>
  1551.     inline future<typename result_of<_Fn(_Args...)>::type>
  1552.     async(_Fn&& __fn, _Args&&... __args)
  1553.     {
  1554.       return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
  1555.                    std::forward<_Args>(__args)...);
  1556.     }
  1557.  
  1558. #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
  1559.        // && ATOMIC_INT_LOCK_FREE
  1560.  
  1561.   // @} group futures
  1562. _GLIBCXX_END_NAMESPACE_VERSION
  1563. } // namespace
  1564.  
  1565. #endif // C++11
  1566.  
  1567. #endif // _GLIBCXX_FUTURE
  1568.