xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/std/future (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj// <future> -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj// Copyright (C) 2009-2018 Free Software Foundation, Inc.
4*38fd1498Szrj//
5*38fd1498Szrj// This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj// software; you can redistribute it and/or modify it under the
7*38fd1498Szrj// terms of the GNU General Public License as published by the
8*38fd1498Szrj// Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj// any later version.
10*38fd1498Szrj
11*38fd1498Szrj// This library is distributed in the hope that it will be useful,
12*38fd1498Szrj// but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj// GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj// Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj// permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj// 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj// You should have received a copy of the GNU General Public License and
21*38fd1498Szrj// a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj// <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj/** @file include/future
26*38fd1498Szrj *  This is a Standard C++ Library header.
27*38fd1498Szrj */
28*38fd1498Szrj
29*38fd1498Szrj#ifndef _GLIBCXX_FUTURE
30*38fd1498Szrj#define _GLIBCXX_FUTURE 1
31*38fd1498Szrj
32*38fd1498Szrj#pragma GCC system_header
33*38fd1498Szrj
34*38fd1498Szrj#if __cplusplus < 201103L
35*38fd1498Szrj# include <bits/c++0x_warning.h>
36*38fd1498Szrj#else
37*38fd1498Szrj
38*38fd1498Szrj#include <mutex>
39*38fd1498Szrj#include <thread>
40*38fd1498Szrj#include <condition_variable>
41*38fd1498Szrj#include <system_error>
42*38fd1498Szrj#include <atomic>
43*38fd1498Szrj#include <bits/atomic_futex.h>
44*38fd1498Szrj#include <bits/functexcept.h>
45*38fd1498Szrj#include <bits/invoke.h>
46*38fd1498Szrj#include <bits/unique_ptr.h>
47*38fd1498Szrj#include <bits/shared_ptr.h>
48*38fd1498Szrj#include <bits/std_function.h>
49*38fd1498Szrj#include <bits/uses_allocator.h>
50*38fd1498Szrj#include <bits/allocated_ptr.h>
51*38fd1498Szrj#include <ext/aligned_buffer.h>
52*38fd1498Szrj
53*38fd1498Szrjnamespace std _GLIBCXX_VISIBILITY(default)
54*38fd1498Szrj{
55*38fd1498Szrj_GLIBCXX_BEGIN_NAMESPACE_VERSION
56*38fd1498Szrj
57*38fd1498Szrj  /**
58*38fd1498Szrj   * @defgroup futures Futures
59*38fd1498Szrj   * @ingroup concurrency
60*38fd1498Szrj   *
61*38fd1498Szrj   * Classes for futures support.
62*38fd1498Szrj   * @{
63*38fd1498Szrj   */
64*38fd1498Szrj
65*38fd1498Szrj  /// Error code for futures
66*38fd1498Szrj  enum class future_errc
67*38fd1498Szrj  {
68*38fd1498Szrj    future_already_retrieved = 1,
69*38fd1498Szrj    promise_already_satisfied,
70*38fd1498Szrj    no_state,
71*38fd1498Szrj    broken_promise
72*38fd1498Szrj  };
73*38fd1498Szrj
74*38fd1498Szrj  /// Specialization.
75*38fd1498Szrj  template<>
76*38fd1498Szrj    struct is_error_code_enum<future_errc> : public true_type { };
77*38fd1498Szrj
78*38fd1498Szrj  /// Points to a statically-allocated object derived from error_category.
79*38fd1498Szrj  const error_category&
80*38fd1498Szrj  future_category() noexcept;
81*38fd1498Szrj
82*38fd1498Szrj  /// Overload for make_error_code.
83*38fd1498Szrj  inline error_code
84*38fd1498Szrj  make_error_code(future_errc __errc) noexcept
85*38fd1498Szrj  { return error_code(static_cast<int>(__errc), future_category()); }
86*38fd1498Szrj
87*38fd1498Szrj  /// Overload for make_error_condition.
88*38fd1498Szrj  inline error_condition
89*38fd1498Szrj  make_error_condition(future_errc __errc) noexcept
90*38fd1498Szrj  { return error_condition(static_cast<int>(__errc), future_category()); }
91*38fd1498Szrj
92*38fd1498Szrj  /**
93*38fd1498Szrj   *  @brief Exception type thrown by futures.
94*38fd1498Szrj   *  @ingroup exceptions
95*38fd1498Szrj   */
96*38fd1498Szrj  class future_error : public logic_error
97*38fd1498Szrj  {
98*38fd1498Szrj  public:
99*38fd1498Szrj    explicit
100*38fd1498Szrj    future_error(future_errc __errc)
101*38fd1498Szrj    : future_error(std::make_error_code(__errc))
102*38fd1498Szrj    { }
103*38fd1498Szrj
104*38fd1498Szrj    virtual ~future_error() noexcept;
105*38fd1498Szrj
106*38fd1498Szrj    virtual const char*
107*38fd1498Szrj    what() const noexcept;
108*38fd1498Szrj
109*38fd1498Szrj    const error_code&
110*38fd1498Szrj    code() const noexcept { return _M_code; }
111*38fd1498Szrj
112*38fd1498Szrj  private:
113*38fd1498Szrj    explicit
114*38fd1498Szrj    future_error(error_code __ec)
115*38fd1498Szrj    : logic_error("std::future_error: " + __ec.message()), _M_code(__ec)
116*38fd1498Szrj    { }
117*38fd1498Szrj
118*38fd1498Szrj    friend void __throw_future_error(int);
119*38fd1498Szrj
120*38fd1498Szrj    error_code 			_M_code;
121*38fd1498Szrj  };
122*38fd1498Szrj
123*38fd1498Szrj  // Forward declarations.
124*38fd1498Szrj  template<typename _Res>
125*38fd1498Szrj    class future;
126*38fd1498Szrj
127*38fd1498Szrj  template<typename _Res>
128*38fd1498Szrj    class shared_future;
129*38fd1498Szrj
130*38fd1498Szrj  template<typename _Signature>
131*38fd1498Szrj    class packaged_task;
132*38fd1498Szrj
133*38fd1498Szrj  template<typename _Res>
134*38fd1498Szrj    class promise;
135*38fd1498Szrj
136*38fd1498Szrj  /// Launch code for futures
137*38fd1498Szrj  enum class launch
138*38fd1498Szrj  {
139*38fd1498Szrj    async = 1,
140*38fd1498Szrj    deferred = 2
141*38fd1498Szrj  };
142*38fd1498Szrj
143*38fd1498Szrj  constexpr launch operator&(launch __x, launch __y)
144*38fd1498Szrj  {
145*38fd1498Szrj    return static_cast<launch>(
146*38fd1498Szrj	static_cast<int>(__x) & static_cast<int>(__y));
147*38fd1498Szrj  }
148*38fd1498Szrj
149*38fd1498Szrj  constexpr launch operator|(launch __x, launch __y)
150*38fd1498Szrj  {
151*38fd1498Szrj    return static_cast<launch>(
152*38fd1498Szrj	static_cast<int>(__x) | static_cast<int>(__y));
153*38fd1498Szrj  }
154*38fd1498Szrj
155*38fd1498Szrj  constexpr launch operator^(launch __x, launch __y)
156*38fd1498Szrj  {
157*38fd1498Szrj    return static_cast<launch>(
158*38fd1498Szrj	static_cast<int>(__x) ^ static_cast<int>(__y));
159*38fd1498Szrj  }
160*38fd1498Szrj
161*38fd1498Szrj  constexpr launch operator~(launch __x)
162*38fd1498Szrj  { return static_cast<launch>(~static_cast<int>(__x)); }
163*38fd1498Szrj
164*38fd1498Szrj  inline launch& operator&=(launch& __x, launch __y)
165*38fd1498Szrj  { return __x = __x & __y; }
166*38fd1498Szrj
167*38fd1498Szrj  inline launch& operator|=(launch& __x, launch __y)
168*38fd1498Szrj  { return __x = __x | __y; }
169*38fd1498Szrj
170*38fd1498Szrj  inline launch& operator^=(launch& __x, launch __y)
171*38fd1498Szrj  { return __x = __x ^ __y; }
172*38fd1498Szrj
173*38fd1498Szrj  /// Status code for futures
174*38fd1498Szrj  enum class future_status
175*38fd1498Szrj  {
176*38fd1498Szrj    ready,
177*38fd1498Szrj    timeout,
178*38fd1498Szrj    deferred
179*38fd1498Szrj  };
180*38fd1498Szrj
181*38fd1498Szrj  // _GLIBCXX_RESOLVE_LIB_DEFECTS
182*38fd1498Szrj  // 2021. Further incorrect usages of result_of
183*38fd1498Szrj  template<typename _Fn, typename... _Args>
184*38fd1498Szrj    using __async_result_of = typename result_of<
185*38fd1498Szrj      typename decay<_Fn>::type(typename decay<_Args>::type...)>::type;
186*38fd1498Szrj
187*38fd1498Szrj  template<typename _Fn, typename... _Args>
188*38fd1498Szrj    future<__async_result_of<_Fn, _Args...>>
189*38fd1498Szrj    async(launch __policy, _Fn&& __fn, _Args&&... __args);
190*38fd1498Szrj
191*38fd1498Szrj  template<typename _Fn, typename... _Args>
192*38fd1498Szrj    future<__async_result_of<_Fn, _Args...>>
193*38fd1498Szrj    async(_Fn&& __fn, _Args&&... __args);
194*38fd1498Szrj
195*38fd1498Szrj#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
196*38fd1498Szrj
197*38fd1498Szrj  /// Base class and enclosing scope.
198*38fd1498Szrj  struct __future_base
199*38fd1498Szrj  {
200*38fd1498Szrj    /// Base class for results.
201*38fd1498Szrj    struct _Result_base
202*38fd1498Szrj    {
203*38fd1498Szrj      exception_ptr		_M_error;
204*38fd1498Szrj
205*38fd1498Szrj      _Result_base(const _Result_base&) = delete;
206*38fd1498Szrj      _Result_base& operator=(const _Result_base&) = delete;
207*38fd1498Szrj
208*38fd1498Szrj      // _M_destroy() allows derived classes to control deallocation
209*38fd1498Szrj      virtual void _M_destroy() = 0;
210*38fd1498Szrj
211*38fd1498Szrj      struct _Deleter
212*38fd1498Szrj      {
213*38fd1498Szrj	void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
214*38fd1498Szrj      };
215*38fd1498Szrj
216*38fd1498Szrj    protected:
217*38fd1498Szrj      _Result_base();
218*38fd1498Szrj      virtual ~_Result_base();
219*38fd1498Szrj    };
220*38fd1498Szrj
221*38fd1498Szrj    /// A unique_ptr for result objects.
222*38fd1498Szrj    template<typename _Res>
223*38fd1498Szrj      using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
224*38fd1498Szrj
225*38fd1498Szrj    /// A result object that has storage for an object of type _Res.
226*38fd1498Szrj    template<typename _Res>
227*38fd1498Szrj      struct _Result : _Result_base
228*38fd1498Szrj      {
229*38fd1498Szrj      private:
230*38fd1498Szrj	__gnu_cxx::__aligned_buffer<_Res>	_M_storage;
231*38fd1498Szrj	bool 					_M_initialized;
232*38fd1498Szrj
233*38fd1498Szrj      public:
234*38fd1498Szrj	typedef _Res result_type;
235*38fd1498Szrj
236*38fd1498Szrj	_Result() noexcept : _M_initialized() { }
237*38fd1498Szrj
238*38fd1498Szrj	~_Result()
239*38fd1498Szrj	{
240*38fd1498Szrj	  if (_M_initialized)
241*38fd1498Szrj	    _M_value().~_Res();
242*38fd1498Szrj	}
243*38fd1498Szrj
244*38fd1498Szrj	// Return lvalue, future will add const or rvalue-reference
245*38fd1498Szrj	_Res&
246*38fd1498Szrj	_M_value() noexcept { return *_M_storage._M_ptr(); }
247*38fd1498Szrj
248*38fd1498Szrj	void
249*38fd1498Szrj	_M_set(const _Res& __res)
250*38fd1498Szrj	{
251*38fd1498Szrj	  ::new (_M_storage._M_addr()) _Res(__res);
252*38fd1498Szrj	  _M_initialized = true;
253*38fd1498Szrj	}
254*38fd1498Szrj
255*38fd1498Szrj	void
256*38fd1498Szrj	_M_set(_Res&& __res)
257*38fd1498Szrj	{
258*38fd1498Szrj	  ::new (_M_storage._M_addr()) _Res(std::move(__res));
259*38fd1498Szrj	  _M_initialized = true;
260*38fd1498Szrj	}
261*38fd1498Szrj
262*38fd1498Szrj      private:
263*38fd1498Szrj	void _M_destroy() { delete this; }
264*38fd1498Szrj    };
265*38fd1498Szrj
266*38fd1498Szrj    /// A result object that uses an allocator.
267*38fd1498Szrj    template<typename _Res, typename _Alloc>
268*38fd1498Szrj      struct _Result_alloc final : _Result<_Res>, _Alloc
269*38fd1498Szrj      {
270*38fd1498Szrj	using __allocator_type = __alloc_rebind<_Alloc, _Result_alloc>;
271*38fd1498Szrj
272*38fd1498Szrj        explicit
273*38fd1498Szrj	_Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
274*38fd1498Szrj	{ }
275*38fd1498Szrj
276*38fd1498Szrj      private:
277*38fd1498Szrj	void _M_destroy()
278*38fd1498Szrj	{
279*38fd1498Szrj	  __allocator_type __a(*this);
280*38fd1498Szrj	  __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
281*38fd1498Szrj	  this->~_Result_alloc();
282*38fd1498Szrj	}
283*38fd1498Szrj      };
284*38fd1498Szrj
285*38fd1498Szrj    // Create a result object that uses an allocator.
286*38fd1498Szrj    template<typename _Res, typename _Allocator>
287*38fd1498Szrj      static _Ptr<_Result_alloc<_Res, _Allocator>>
288*38fd1498Szrj      _S_allocate_result(const _Allocator& __a)
289*38fd1498Szrj      {
290*38fd1498Szrj	using __result_type = _Result_alloc<_Res, _Allocator>;
291*38fd1498Szrj	typename __result_type::__allocator_type __a2(__a);
292*38fd1498Szrj	auto __guard = std::__allocate_guarded(__a2);
293*38fd1498Szrj	__result_type* __p = ::new((void*)__guard.get()) __result_type{__a};
294*38fd1498Szrj	__guard = nullptr;
295*38fd1498Szrj	return _Ptr<__result_type>(__p);
296*38fd1498Szrj      }
297*38fd1498Szrj
298*38fd1498Szrj    // Keep it simple for std::allocator.
299*38fd1498Szrj    template<typename _Res, typename _Tp>
300*38fd1498Szrj      static _Ptr<_Result<_Res>>
301*38fd1498Szrj      _S_allocate_result(const std::allocator<_Tp>& __a)
302*38fd1498Szrj      {
303*38fd1498Szrj	return _Ptr<_Result<_Res>>(new _Result<_Res>);
304*38fd1498Szrj      }
305*38fd1498Szrj
306*38fd1498Szrj    // Base class for various types of shared state created by an
307*38fd1498Szrj    // asynchronous provider (such as a std::promise) and shared with one
308*38fd1498Szrj    // or more associated futures.
309*38fd1498Szrj    class _State_baseV2
310*38fd1498Szrj    {
311*38fd1498Szrj      typedef _Ptr<_Result_base> _Ptr_type;
312*38fd1498Szrj
313*38fd1498Szrj      enum _Status : unsigned {
314*38fd1498Szrj	__not_ready,
315*38fd1498Szrj	__ready
316*38fd1498Szrj      };
317*38fd1498Szrj
318*38fd1498Szrj      _Ptr_type			_M_result;
319*38fd1498Szrj      __atomic_futex_unsigned<>	_M_status;
320*38fd1498Szrj      atomic_flag         	_M_retrieved = ATOMIC_FLAG_INIT;
321*38fd1498Szrj      once_flag			_M_once;
322*38fd1498Szrj
323*38fd1498Szrj    public:
324*38fd1498Szrj      _State_baseV2() noexcept : _M_result(), _M_status(_Status::__not_ready)
325*38fd1498Szrj	{ }
326*38fd1498Szrj      _State_baseV2(const _State_baseV2&) = delete;
327*38fd1498Szrj      _State_baseV2& operator=(const _State_baseV2&) = delete;
328*38fd1498Szrj      virtual ~_State_baseV2() = default;
329*38fd1498Szrj
330*38fd1498Szrj      _Result_base&
331*38fd1498Szrj      wait()
332*38fd1498Szrj      {
333*38fd1498Szrj	// Run any deferred function or join any asynchronous thread:
334*38fd1498Szrj	_M_complete_async();
335*38fd1498Szrj	// Acquire MO makes sure this synchronizes with the thread that made
336*38fd1498Szrj	// the future ready.
337*38fd1498Szrj	_M_status._M_load_when_equal(_Status::__ready, memory_order_acquire);
338*38fd1498Szrj	return *_M_result;
339*38fd1498Szrj      }
340*38fd1498Szrj
341*38fd1498Szrj      template<typename _Rep, typename _Period>
342*38fd1498Szrj        future_status
343*38fd1498Szrj        wait_for(const chrono::duration<_Rep, _Period>& __rel)
344*38fd1498Szrj        {
345*38fd1498Szrj	  // First, check if the future has been made ready.  Use acquire MO
346*38fd1498Szrj	  // to synchronize with the thread that made it ready.
347*38fd1498Szrj	  if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
348*38fd1498Szrj	    return future_status::ready;
349*38fd1498Szrj	  if (_M_is_deferred_future())
350*38fd1498Szrj	    return future_status::deferred;
351*38fd1498Szrj	  if (_M_status._M_load_when_equal_for(_Status::__ready,
352*38fd1498Szrj	      memory_order_acquire, __rel))
353*38fd1498Szrj	    {
354*38fd1498Szrj	      // _GLIBCXX_RESOLVE_LIB_DEFECTS
355*38fd1498Szrj	      // 2100.  timed waiting functions must also join
356*38fd1498Szrj	      // This call is a no-op by default except on an async future,
357*38fd1498Szrj	      // in which case the async thread is joined.  It's also not a
358*38fd1498Szrj	      // no-op for a deferred future, but such a future will never
359*38fd1498Szrj	      // reach this point because it returns future_status::deferred
360*38fd1498Szrj	      // instead of waiting for the future to become ready (see
361*38fd1498Szrj	      // above).  Async futures synchronize in this call, so we need
362*38fd1498Szrj	      // no further synchronization here.
363*38fd1498Szrj	      _M_complete_async();
364*38fd1498Szrj
365*38fd1498Szrj	      return future_status::ready;
366*38fd1498Szrj	    }
367*38fd1498Szrj	  return future_status::timeout;
368*38fd1498Szrj	}
369*38fd1498Szrj
370*38fd1498Szrj      template<typename _Clock, typename _Duration>
371*38fd1498Szrj        future_status
372*38fd1498Szrj        wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
373*38fd1498Szrj        {
374*38fd1498Szrj	  // First, check if the future has been made ready.  Use acquire MO
375*38fd1498Szrj	  // to synchronize with the thread that made it ready.
376*38fd1498Szrj	  if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
377*38fd1498Szrj	    return future_status::ready;
378*38fd1498Szrj	  if (_M_is_deferred_future())
379*38fd1498Szrj	    return future_status::deferred;
380*38fd1498Szrj	  if (_M_status._M_load_when_equal_until(_Status::__ready,
381*38fd1498Szrj	      memory_order_acquire, __abs))
382*38fd1498Szrj	    {
383*38fd1498Szrj	      // _GLIBCXX_RESOLVE_LIB_DEFECTS
384*38fd1498Szrj	      // 2100.  timed waiting functions must also join
385*38fd1498Szrj	      // See wait_for(...) above.
386*38fd1498Szrj	      _M_complete_async();
387*38fd1498Szrj
388*38fd1498Szrj	      return future_status::ready;
389*38fd1498Szrj	    }
390*38fd1498Szrj	  return future_status::timeout;
391*38fd1498Szrj	}
392*38fd1498Szrj
393*38fd1498Szrj      // Provide a result to the shared state and make it ready.
394*38fd1498Szrj      // Calls at most once: _M_result = __res();
395*38fd1498Szrj      void
396*38fd1498Szrj      _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
397*38fd1498Szrj      {
398*38fd1498Szrj	bool __did_set = false;
399*38fd1498Szrj        // all calls to this function are serialized,
400*38fd1498Szrj        // side-effects of invoking __res only happen once
401*38fd1498Szrj	call_once(_M_once, &_State_baseV2::_M_do_set, this,
402*38fd1498Szrj		  std::__addressof(__res), std::__addressof(__did_set));
403*38fd1498Szrj	if (__did_set)
404*38fd1498Szrj	  // Use release MO to synchronize with observers of the ready state.
405*38fd1498Szrj	  _M_status._M_store_notify_all(_Status::__ready,
406*38fd1498Szrj					memory_order_release);
407*38fd1498Szrj	else if (!__ignore_failure)
408*38fd1498Szrj          __throw_future_error(int(future_errc::promise_already_satisfied));
409*38fd1498Szrj      }
410*38fd1498Szrj
411*38fd1498Szrj      // Provide a result to the shared state but delay making it ready
412*38fd1498Szrj      // until the calling thread exits.
413*38fd1498Szrj      // Calls at most once: _M_result = __res();
414*38fd1498Szrj      void
415*38fd1498Szrj      _M_set_delayed_result(function<_Ptr_type()> __res,
416*38fd1498Szrj			    weak_ptr<_State_baseV2> __self)
417*38fd1498Szrj      {
418*38fd1498Szrj	bool __did_set = false;
419*38fd1498Szrj	unique_ptr<_Make_ready> __mr{new _Make_ready};
420*38fd1498Szrj        // all calls to this function are serialized,
421*38fd1498Szrj        // side-effects of invoking __res only happen once
422*38fd1498Szrj	call_once(_M_once, &_State_baseV2::_M_do_set, this,
423*38fd1498Szrj		  std::__addressof(__res), std::__addressof(__did_set));
424*38fd1498Szrj	if (!__did_set)
425*38fd1498Szrj          __throw_future_error(int(future_errc::promise_already_satisfied));
426*38fd1498Szrj	__mr->_M_shared_state = std::move(__self);
427*38fd1498Szrj	__mr->_M_set();
428*38fd1498Szrj	__mr.release();
429*38fd1498Szrj      }
430*38fd1498Szrj
431*38fd1498Szrj      // Abandon this shared state.
432*38fd1498Szrj      void
433*38fd1498Szrj      _M_break_promise(_Ptr_type __res)
434*38fd1498Szrj      {
435*38fd1498Szrj	if (static_cast<bool>(__res))
436*38fd1498Szrj	  {
437*38fd1498Szrj	    __res->_M_error =
438*38fd1498Szrj	      make_exception_ptr(future_error(future_errc::broken_promise));
439*38fd1498Szrj	    // This function is only called when the last asynchronous result
440*38fd1498Szrj	    // provider is abandoning this shared state, so noone can be
441*38fd1498Szrj	    // trying to make the shared state ready at the same time, and
442*38fd1498Szrj	    // we can access _M_result directly instead of through call_once.
443*38fd1498Szrj	    _M_result.swap(__res);
444*38fd1498Szrj	    // Use release MO to synchronize with observers of the ready state.
445*38fd1498Szrj	    _M_status._M_store_notify_all(_Status::__ready,
446*38fd1498Szrj					  memory_order_release);
447*38fd1498Szrj	  }
448*38fd1498Szrj      }
449*38fd1498Szrj
450*38fd1498Szrj      // Called when this object is first passed to a future.
451*38fd1498Szrj      void
452*38fd1498Szrj      _M_set_retrieved_flag()
453*38fd1498Szrj      {
454*38fd1498Szrj	if (_M_retrieved.test_and_set())
455*38fd1498Szrj	  __throw_future_error(int(future_errc::future_already_retrieved));
456*38fd1498Szrj      }
457*38fd1498Szrj
458*38fd1498Szrj      template<typename _Res, typename _Arg>
459*38fd1498Szrj        struct _Setter;
460*38fd1498Szrj
461*38fd1498Szrj      // set lvalues
462*38fd1498Szrj      template<typename _Res, typename _Arg>
463*38fd1498Szrj        struct _Setter<_Res, _Arg&>
464*38fd1498Szrj        {
465*38fd1498Szrj          // check this is only used by promise<R>::set_value(const R&)
466*38fd1498Szrj          // or promise<R&>::set_value(R&)
467*38fd1498Szrj          static_assert(is_same<_Res, _Arg&>::value  // promise<R&>
468*38fd1498Szrj              || is_same<const _Res, _Arg>::value,   // promise<R>
469*38fd1498Szrj              "Invalid specialisation");
470*38fd1498Szrj
471*38fd1498Szrj	  // Used by std::promise to copy construct the result.
472*38fd1498Szrj          typename promise<_Res>::_Ptr_type operator()() const
473*38fd1498Szrj          {
474*38fd1498Szrj            _M_promise->_M_storage->_M_set(*_M_arg);
475*38fd1498Szrj            return std::move(_M_promise->_M_storage);
476*38fd1498Szrj          }
477*38fd1498Szrj          promise<_Res>*    _M_promise;
478*38fd1498Szrj          _Arg*             _M_arg;
479*38fd1498Szrj        };
480*38fd1498Szrj
481*38fd1498Szrj      // set rvalues
482*38fd1498Szrj      template<typename _Res>
483*38fd1498Szrj        struct _Setter<_Res, _Res&&>
484*38fd1498Szrj        {
485*38fd1498Szrj	  // Used by std::promise to move construct the result.
486*38fd1498Szrj          typename promise<_Res>::_Ptr_type operator()() const
487*38fd1498Szrj          {
488*38fd1498Szrj            _M_promise->_M_storage->_M_set(std::move(*_M_arg));
489*38fd1498Szrj            return std::move(_M_promise->_M_storage);
490*38fd1498Szrj          }
491*38fd1498Szrj          promise<_Res>*    _M_promise;
492*38fd1498Szrj          _Res*             _M_arg;
493*38fd1498Szrj        };
494*38fd1498Szrj
495*38fd1498Szrj      // set void
496*38fd1498Szrj      template<typename _Res>
497*38fd1498Szrj	struct _Setter<_Res, void>
498*38fd1498Szrj	{
499*38fd1498Szrj	  static_assert(is_void<_Res>::value, "Only used for promise<void>");
500*38fd1498Szrj
501*38fd1498Szrj	  typename promise<_Res>::_Ptr_type operator()() const
502*38fd1498Szrj	  { return std::move(_M_promise->_M_storage); }
503*38fd1498Szrj
504*38fd1498Szrj	  promise<_Res>*    _M_promise;
505*38fd1498Szrj	};
506*38fd1498Szrj
507*38fd1498Szrj      struct __exception_ptr_tag { };
508*38fd1498Szrj
509*38fd1498Szrj      // set exceptions
510*38fd1498Szrj      template<typename _Res>
511*38fd1498Szrj        struct _Setter<_Res, __exception_ptr_tag>
512*38fd1498Szrj        {
513*38fd1498Szrj	  // Used by std::promise to store an exception as the result.
514*38fd1498Szrj          typename promise<_Res>::_Ptr_type operator()() const
515*38fd1498Szrj          {
516*38fd1498Szrj            _M_promise->_M_storage->_M_error = *_M_ex;
517*38fd1498Szrj            return std::move(_M_promise->_M_storage);
518*38fd1498Szrj          }
519*38fd1498Szrj
520*38fd1498Szrj          promise<_Res>*   _M_promise;
521*38fd1498Szrj          exception_ptr*    _M_ex;
522*38fd1498Szrj        };
523*38fd1498Szrj
524*38fd1498Szrj      template<typename _Res, typename _Arg>
525*38fd1498Szrj        static _Setter<_Res, _Arg&&>
526*38fd1498Szrj        __setter(promise<_Res>* __prom, _Arg&& __arg)
527*38fd1498Szrj        {
528*38fd1498Szrj	  _S_check(__prom->_M_future);
529*38fd1498Szrj          return _Setter<_Res, _Arg&&>{ __prom, std::__addressof(__arg) };
530*38fd1498Szrj        }
531*38fd1498Szrj
532*38fd1498Szrj      template<typename _Res>
533*38fd1498Szrj        static _Setter<_Res, __exception_ptr_tag>
534*38fd1498Szrj        __setter(exception_ptr& __ex, promise<_Res>* __prom)
535*38fd1498Szrj        {
536*38fd1498Szrj	  _S_check(__prom->_M_future);
537*38fd1498Szrj          return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
538*38fd1498Szrj        }
539*38fd1498Szrj
540*38fd1498Szrj      template<typename _Res>
541*38fd1498Szrj	static _Setter<_Res, void>
542*38fd1498Szrj	__setter(promise<_Res>* __prom)
543*38fd1498Szrj	{
544*38fd1498Szrj	  _S_check(__prom->_M_future);
545*38fd1498Szrj	  return _Setter<_Res, void>{ __prom };
546*38fd1498Szrj	}
547*38fd1498Szrj
548*38fd1498Szrj      template<typename _Tp>
549*38fd1498Szrj        static void
550*38fd1498Szrj        _S_check(const shared_ptr<_Tp>& __p)
551*38fd1498Szrj        {
552*38fd1498Szrj          if (!static_cast<bool>(__p))
553*38fd1498Szrj            __throw_future_error((int)future_errc::no_state);
554*38fd1498Szrj        }
555*38fd1498Szrj
556*38fd1498Szrj    private:
557*38fd1498Szrj      // The function invoked with std::call_once(_M_once, ...).
558*38fd1498Szrj      void
559*38fd1498Szrj      _M_do_set(function<_Ptr_type()>* __f, bool* __did_set)
560*38fd1498Szrj      {
561*38fd1498Szrj        _Ptr_type __res = (*__f)();
562*38fd1498Szrj        // Notify the caller that we did try to set; if we do not throw an
563*38fd1498Szrj        // exception, the caller will be aware that it did set (e.g., see
564*38fd1498Szrj        // _M_set_result).
565*38fd1498Szrj	*__did_set = true;
566*38fd1498Szrj        _M_result.swap(__res); // nothrow
567*38fd1498Szrj      }
568*38fd1498Szrj
569*38fd1498Szrj      // Wait for completion of async function.
570*38fd1498Szrj      virtual void _M_complete_async() { }
571*38fd1498Szrj
572*38fd1498Szrj      // Return true if state corresponds to a deferred function.
573*38fd1498Szrj      virtual bool _M_is_deferred_future() const { return false; }
574*38fd1498Szrj
575*38fd1498Szrj      struct _Make_ready final : __at_thread_exit_elt
576*38fd1498Szrj      {
577*38fd1498Szrj	weak_ptr<_State_baseV2> _M_shared_state;
578*38fd1498Szrj	static void _S_run(void*);
579*38fd1498Szrj	void _M_set();
580*38fd1498Szrj      };
581*38fd1498Szrj    };
582*38fd1498Szrj
583*38fd1498Szrj#ifdef _GLIBCXX_ASYNC_ABI_COMPAT
584*38fd1498Szrj    class _State_base;
585*38fd1498Szrj    class _Async_state_common;
586*38fd1498Szrj#else
587*38fd1498Szrj    using _State_base = _State_baseV2;
588*38fd1498Szrj    class _Async_state_commonV2;
589*38fd1498Szrj#endif
590*38fd1498Szrj
591*38fd1498Szrj    template<typename _BoundFn,
592*38fd1498Szrj	     typename _Res = decltype(std::declval<_BoundFn&>()())>
593*38fd1498Szrj      class _Deferred_state;
594*38fd1498Szrj
595*38fd1498Szrj    template<typename _BoundFn,
596*38fd1498Szrj	     typename _Res = decltype(std::declval<_BoundFn&>()())>
597*38fd1498Szrj      class _Async_state_impl;
598*38fd1498Szrj
599*38fd1498Szrj    template<typename _Signature>
600*38fd1498Szrj      class _Task_state_base;
601*38fd1498Szrj
602*38fd1498Szrj    template<typename _Fn, typename _Alloc, typename _Signature>
603*38fd1498Szrj      class _Task_state;
604*38fd1498Szrj
605*38fd1498Szrj    template<typename _BoundFn>
606*38fd1498Szrj      static std::shared_ptr<_State_base>
607*38fd1498Szrj      _S_make_deferred_state(_BoundFn&& __fn);
608*38fd1498Szrj
609*38fd1498Szrj    template<typename _BoundFn>
610*38fd1498Szrj      static std::shared_ptr<_State_base>
611*38fd1498Szrj      _S_make_async_state(_BoundFn&& __fn);
612*38fd1498Szrj
613*38fd1498Szrj    template<typename _Res_ptr, typename _Fn,
614*38fd1498Szrj	     typename _Res = typename _Res_ptr::element_type::result_type>
615*38fd1498Szrj      struct _Task_setter;
616*38fd1498Szrj
617*38fd1498Szrj    template<typename _Res_ptr, typename _BoundFn>
618*38fd1498Szrj      static _Task_setter<_Res_ptr, _BoundFn>
619*38fd1498Szrj      _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
620*38fd1498Szrj      {
621*38fd1498Szrj	return { std::__addressof(__ptr), std::__addressof(__call) };
622*38fd1498Szrj      }
623*38fd1498Szrj  };
624*38fd1498Szrj
625*38fd1498Szrj  /// Partial specialization for reference types.
626*38fd1498Szrj  template<typename _Res>
627*38fd1498Szrj    struct __future_base::_Result<_Res&> : __future_base::_Result_base
628*38fd1498Szrj    {
629*38fd1498Szrj      typedef _Res& result_type;
630*38fd1498Szrj
631*38fd1498Szrj      _Result() noexcept : _M_value_ptr() { }
632*38fd1498Szrj
633*38fd1498Szrj      void
634*38fd1498Szrj      _M_set(_Res& __res) noexcept
635*38fd1498Szrj      { _M_value_ptr = std::addressof(__res); }
636*38fd1498Szrj
637*38fd1498Szrj      _Res& _M_get() noexcept { return *_M_value_ptr; }
638*38fd1498Szrj
639*38fd1498Szrj    private:
640*38fd1498Szrj      _Res* 			_M_value_ptr;
641*38fd1498Szrj
642*38fd1498Szrj      void _M_destroy() { delete this; }
643*38fd1498Szrj    };
644*38fd1498Szrj
645*38fd1498Szrj  /// Explicit specialization for void.
646*38fd1498Szrj  template<>
647*38fd1498Szrj    struct __future_base::_Result<void> : __future_base::_Result_base
648*38fd1498Szrj    {
649*38fd1498Szrj      typedef void result_type;
650*38fd1498Szrj
651*38fd1498Szrj    private:
652*38fd1498Szrj      void _M_destroy() { delete this; }
653*38fd1498Szrj    };
654*38fd1498Szrj
655*38fd1498Szrj#ifndef _GLIBCXX_ASYNC_ABI_COMPAT
656*38fd1498Szrj
657*38fd1498Szrj  // Allow _Setter objects to be stored locally in std::function
658*38fd1498Szrj  template<typename _Res, typename _Arg>
659*38fd1498Szrj    struct __is_location_invariant
660*38fd1498Szrj    <__future_base::_State_base::_Setter<_Res, _Arg>>
661*38fd1498Szrj    : true_type { };
662*38fd1498Szrj
663*38fd1498Szrj  // Allow _Task_setter objects to be stored locally in std::function
664*38fd1498Szrj  template<typename _Res_ptr, typename _Fn, typename _Res>
665*38fd1498Szrj    struct __is_location_invariant
666*38fd1498Szrj    <__future_base::_Task_setter<_Res_ptr, _Fn, _Res>>
667*38fd1498Szrj    : true_type { };
668*38fd1498Szrj
669*38fd1498Szrj  /// Common implementation for future and shared_future.
670*38fd1498Szrj  template<typename _Res>
671*38fd1498Szrj    class __basic_future : public __future_base
672*38fd1498Szrj    {
673*38fd1498Szrj    protected:
674*38fd1498Szrj      typedef shared_ptr<_State_base>		__state_type;
675*38fd1498Szrj      typedef __future_base::_Result<_Res>&	__result_type;
676*38fd1498Szrj
677*38fd1498Szrj    private:
678*38fd1498Szrj      __state_type 		_M_state;
679*38fd1498Szrj
680*38fd1498Szrj    public:
681*38fd1498Szrj      // Disable copying.
682*38fd1498Szrj      __basic_future(const __basic_future&) = delete;
683*38fd1498Szrj      __basic_future& operator=(const __basic_future&) = delete;
684*38fd1498Szrj
685*38fd1498Szrj      bool
686*38fd1498Szrj      valid() const noexcept { return static_cast<bool>(_M_state); }
687*38fd1498Szrj
688*38fd1498Szrj      void
689*38fd1498Szrj      wait() const
690*38fd1498Szrj      {
691*38fd1498Szrj        _State_base::_S_check(_M_state);
692*38fd1498Szrj        _M_state->wait();
693*38fd1498Szrj      }
694*38fd1498Szrj
695*38fd1498Szrj      template<typename _Rep, typename _Period>
696*38fd1498Szrj        future_status
697*38fd1498Szrj        wait_for(const chrono::duration<_Rep, _Period>& __rel) const
698*38fd1498Szrj        {
699*38fd1498Szrj          _State_base::_S_check(_M_state);
700*38fd1498Szrj          return _M_state->wait_for(__rel);
701*38fd1498Szrj        }
702*38fd1498Szrj
703*38fd1498Szrj      template<typename _Clock, typename _Duration>
704*38fd1498Szrj        future_status
705*38fd1498Szrj        wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
706*38fd1498Szrj        {
707*38fd1498Szrj          _State_base::_S_check(_M_state);
708*38fd1498Szrj          return _M_state->wait_until(__abs);
709*38fd1498Szrj        }
710*38fd1498Szrj
711*38fd1498Szrj    protected:
712*38fd1498Szrj      /// Wait for the state to be ready and rethrow any stored exception
713*38fd1498Szrj      __result_type
714*38fd1498Szrj      _M_get_result() const
715*38fd1498Szrj      {
716*38fd1498Szrj        _State_base::_S_check(_M_state);
717*38fd1498Szrj        _Result_base& __res = _M_state->wait();
718*38fd1498Szrj        if (!(__res._M_error == 0))
719*38fd1498Szrj          rethrow_exception(__res._M_error);
720*38fd1498Szrj        return static_cast<__result_type>(__res);
721*38fd1498Szrj      }
722*38fd1498Szrj
723*38fd1498Szrj      void _M_swap(__basic_future& __that) noexcept
724*38fd1498Szrj      {
725*38fd1498Szrj        _M_state.swap(__that._M_state);
726*38fd1498Szrj      }
727*38fd1498Szrj
728*38fd1498Szrj      // Construction of a future by promise::get_future()
729*38fd1498Szrj      explicit
730*38fd1498Szrj      __basic_future(const __state_type& __state) : _M_state(__state)
731*38fd1498Szrj      {
732*38fd1498Szrj        _State_base::_S_check(_M_state);
733*38fd1498Szrj        _M_state->_M_set_retrieved_flag();
734*38fd1498Szrj      }
735*38fd1498Szrj
736*38fd1498Szrj      // Copy construction from a shared_future
737*38fd1498Szrj      explicit
738*38fd1498Szrj      __basic_future(const shared_future<_Res>&) noexcept;
739*38fd1498Szrj
740*38fd1498Szrj      // Move construction from a shared_future
741*38fd1498Szrj      explicit
742*38fd1498Szrj      __basic_future(shared_future<_Res>&&) noexcept;
743*38fd1498Szrj
744*38fd1498Szrj      // Move construction from a future
745*38fd1498Szrj      explicit
746*38fd1498Szrj      __basic_future(future<_Res>&&) noexcept;
747*38fd1498Szrj
748*38fd1498Szrj      constexpr __basic_future() noexcept : _M_state() { }
749*38fd1498Szrj
750*38fd1498Szrj      struct _Reset
751*38fd1498Szrj      {
752*38fd1498Szrj        explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
753*38fd1498Szrj        ~_Reset() { _M_fut._M_state.reset(); }
754*38fd1498Szrj        __basic_future& _M_fut;
755*38fd1498Szrj      };
756*38fd1498Szrj    };
757*38fd1498Szrj
758*38fd1498Szrj
759*38fd1498Szrj  /// Primary template for future.
760*38fd1498Szrj  template<typename _Res>
761*38fd1498Szrj    class future : public __basic_future<_Res>
762*38fd1498Szrj    {
763*38fd1498Szrj      friend class promise<_Res>;
764*38fd1498Szrj      template<typename> friend class packaged_task;
765*38fd1498Szrj      template<typename _Fn, typename... _Args>
766*38fd1498Szrj        friend future<__async_result_of<_Fn, _Args...>>
767*38fd1498Szrj        async(launch, _Fn&&, _Args&&...);
768*38fd1498Szrj
769*38fd1498Szrj      typedef __basic_future<_Res> _Base_type;
770*38fd1498Szrj      typedef typename _Base_type::__state_type __state_type;
771*38fd1498Szrj
772*38fd1498Szrj      explicit
773*38fd1498Szrj      future(const __state_type& __state) : _Base_type(__state) { }
774*38fd1498Szrj
775*38fd1498Szrj    public:
776*38fd1498Szrj      constexpr future() noexcept : _Base_type() { }
777*38fd1498Szrj
778*38fd1498Szrj      /// Move constructor
779*38fd1498Szrj      future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
780*38fd1498Szrj
781*38fd1498Szrj      // Disable copying
782*38fd1498Szrj      future(const future&) = delete;
783*38fd1498Szrj      future& operator=(const future&) = delete;
784*38fd1498Szrj
785*38fd1498Szrj      future& operator=(future&& __fut) noexcept
786*38fd1498Szrj      {
787*38fd1498Szrj        future(std::move(__fut))._M_swap(*this);
788*38fd1498Szrj        return *this;
789*38fd1498Szrj      }
790*38fd1498Szrj
791*38fd1498Szrj      /// Retrieving the value
792*38fd1498Szrj      _Res
793*38fd1498Szrj      get()
794*38fd1498Szrj      {
795*38fd1498Szrj        typename _Base_type::_Reset __reset(*this);
796*38fd1498Szrj        return std::move(this->_M_get_result()._M_value());
797*38fd1498Szrj      }
798*38fd1498Szrj
799*38fd1498Szrj      shared_future<_Res> share() noexcept;
800*38fd1498Szrj    };
801*38fd1498Szrj
802*38fd1498Szrj  /// Partial specialization for future<R&>
803*38fd1498Szrj  template<typename _Res>
804*38fd1498Szrj    class future<_Res&> : public __basic_future<_Res&>
805*38fd1498Szrj    {
806*38fd1498Szrj      friend class promise<_Res&>;
807*38fd1498Szrj      template<typename> friend class packaged_task;
808*38fd1498Szrj      template<typename _Fn, typename... _Args>
809*38fd1498Szrj        friend future<__async_result_of<_Fn, _Args...>>
810*38fd1498Szrj        async(launch, _Fn&&, _Args&&...);
811*38fd1498Szrj
812*38fd1498Szrj      typedef __basic_future<_Res&> _Base_type;
813*38fd1498Szrj      typedef typename _Base_type::__state_type __state_type;
814*38fd1498Szrj
815*38fd1498Szrj      explicit
816*38fd1498Szrj      future(const __state_type& __state) : _Base_type(__state) { }
817*38fd1498Szrj
818*38fd1498Szrj    public:
819*38fd1498Szrj      constexpr future() noexcept : _Base_type() { }
820*38fd1498Szrj
821*38fd1498Szrj      /// Move constructor
822*38fd1498Szrj      future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
823*38fd1498Szrj
824*38fd1498Szrj      // Disable copying
825*38fd1498Szrj      future(const future&) = delete;
826*38fd1498Szrj      future& operator=(const future&) = delete;
827*38fd1498Szrj
828*38fd1498Szrj      future& operator=(future&& __fut) noexcept
829*38fd1498Szrj      {
830*38fd1498Szrj        future(std::move(__fut))._M_swap(*this);
831*38fd1498Szrj        return *this;
832*38fd1498Szrj      }
833*38fd1498Szrj
834*38fd1498Szrj      /// Retrieving the value
835*38fd1498Szrj      _Res&
836*38fd1498Szrj      get()
837*38fd1498Szrj      {
838*38fd1498Szrj        typename _Base_type::_Reset __reset(*this);
839*38fd1498Szrj        return this->_M_get_result()._M_get();
840*38fd1498Szrj      }
841*38fd1498Szrj
842*38fd1498Szrj      shared_future<_Res&> share() noexcept;
843*38fd1498Szrj    };
844*38fd1498Szrj
845*38fd1498Szrj  /// Explicit specialization for future<void>
846*38fd1498Szrj  template<>
847*38fd1498Szrj    class future<void> : public __basic_future<void>
848*38fd1498Szrj    {
849*38fd1498Szrj      friend class promise<void>;
850*38fd1498Szrj      template<typename> friend class packaged_task;
851*38fd1498Szrj      template<typename _Fn, typename... _Args>
852*38fd1498Szrj        friend future<__async_result_of<_Fn, _Args...>>
853*38fd1498Szrj        async(launch, _Fn&&, _Args&&...);
854*38fd1498Szrj
855*38fd1498Szrj      typedef __basic_future<void> _Base_type;
856*38fd1498Szrj      typedef typename _Base_type::__state_type __state_type;
857*38fd1498Szrj
858*38fd1498Szrj      explicit
859*38fd1498Szrj      future(const __state_type& __state) : _Base_type(__state) { }
860*38fd1498Szrj
861*38fd1498Szrj    public:
862*38fd1498Szrj      constexpr future() noexcept : _Base_type() { }
863*38fd1498Szrj
864*38fd1498Szrj      /// Move constructor
865*38fd1498Szrj      future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
866*38fd1498Szrj
867*38fd1498Szrj      // Disable copying
868*38fd1498Szrj      future(const future&) = delete;
869*38fd1498Szrj      future& operator=(const future&) = delete;
870*38fd1498Szrj
871*38fd1498Szrj      future& operator=(future&& __fut) noexcept
872*38fd1498Szrj      {
873*38fd1498Szrj        future(std::move(__fut))._M_swap(*this);
874*38fd1498Szrj        return *this;
875*38fd1498Szrj      }
876*38fd1498Szrj
877*38fd1498Szrj      /// Retrieving the value
878*38fd1498Szrj      void
879*38fd1498Szrj      get()
880*38fd1498Szrj      {
881*38fd1498Szrj        typename _Base_type::_Reset __reset(*this);
882*38fd1498Szrj        this->_M_get_result();
883*38fd1498Szrj      }
884*38fd1498Szrj
885*38fd1498Szrj      shared_future<void> share() noexcept;
886*38fd1498Szrj    };
887*38fd1498Szrj
888*38fd1498Szrj
889*38fd1498Szrj  /// Primary template for shared_future.
890*38fd1498Szrj  template<typename _Res>
891*38fd1498Szrj    class shared_future : public __basic_future<_Res>
892*38fd1498Szrj    {
893*38fd1498Szrj      typedef __basic_future<_Res> _Base_type;
894*38fd1498Szrj
895*38fd1498Szrj    public:
896*38fd1498Szrj      constexpr shared_future() noexcept : _Base_type() { }
897*38fd1498Szrj
898*38fd1498Szrj      /// Copy constructor
899*38fd1498Szrj      shared_future(const shared_future& __sf) noexcept : _Base_type(__sf) { }
900*38fd1498Szrj
901*38fd1498Szrj      /// Construct from a future rvalue
902*38fd1498Szrj      shared_future(future<_Res>&& __uf) noexcept
903*38fd1498Szrj      : _Base_type(std::move(__uf))
904*38fd1498Szrj      { }
905*38fd1498Szrj
906*38fd1498Szrj      /// Construct from a shared_future rvalue
907*38fd1498Szrj      shared_future(shared_future&& __sf) noexcept
908*38fd1498Szrj      : _Base_type(std::move(__sf))
909*38fd1498Szrj      { }
910*38fd1498Szrj
911*38fd1498Szrj      shared_future& operator=(const shared_future& __sf) noexcept
912*38fd1498Szrj      {
913*38fd1498Szrj        shared_future(__sf)._M_swap(*this);
914*38fd1498Szrj        return *this;
915*38fd1498Szrj      }
916*38fd1498Szrj
917*38fd1498Szrj      shared_future& operator=(shared_future&& __sf) noexcept
918*38fd1498Szrj      {
919*38fd1498Szrj        shared_future(std::move(__sf))._M_swap(*this);
920*38fd1498Szrj        return *this;
921*38fd1498Szrj      }
922*38fd1498Szrj
923*38fd1498Szrj      /// Retrieving the value
924*38fd1498Szrj      const _Res&
925*38fd1498Szrj      get() const { return this->_M_get_result()._M_value(); }
926*38fd1498Szrj    };
927*38fd1498Szrj
928*38fd1498Szrj  /// Partial specialization for shared_future<R&>
929*38fd1498Szrj  template<typename _Res>
930*38fd1498Szrj    class shared_future<_Res&> : public __basic_future<_Res&>
931*38fd1498Szrj    {
932*38fd1498Szrj      typedef __basic_future<_Res&>           _Base_type;
933*38fd1498Szrj
934*38fd1498Szrj    public:
935*38fd1498Szrj      constexpr shared_future() noexcept : _Base_type() { }
936*38fd1498Szrj
937*38fd1498Szrj      /// Copy constructor
938*38fd1498Szrj      shared_future(const shared_future& __sf) : _Base_type(__sf) { }
939*38fd1498Szrj
940*38fd1498Szrj      /// Construct from a future rvalue
941*38fd1498Szrj      shared_future(future<_Res&>&& __uf) noexcept
942*38fd1498Szrj      : _Base_type(std::move(__uf))
943*38fd1498Szrj      { }
944*38fd1498Szrj
945*38fd1498Szrj      /// Construct from a shared_future rvalue
946*38fd1498Szrj      shared_future(shared_future&& __sf) noexcept
947*38fd1498Szrj      : _Base_type(std::move(__sf))
948*38fd1498Szrj      { }
949*38fd1498Szrj
950*38fd1498Szrj      shared_future& operator=(const shared_future& __sf)
951*38fd1498Szrj      {
952*38fd1498Szrj        shared_future(__sf)._M_swap(*this);
953*38fd1498Szrj        return *this;
954*38fd1498Szrj      }
955*38fd1498Szrj
956*38fd1498Szrj      shared_future& operator=(shared_future&& __sf) noexcept
957*38fd1498Szrj      {
958*38fd1498Szrj        shared_future(std::move(__sf))._M_swap(*this);
959*38fd1498Szrj        return *this;
960*38fd1498Szrj      }
961*38fd1498Szrj
962*38fd1498Szrj      /// Retrieving the value
963*38fd1498Szrj      _Res&
964*38fd1498Szrj      get() const { return this->_M_get_result()._M_get(); }
965*38fd1498Szrj    };
966*38fd1498Szrj
967*38fd1498Szrj  /// Explicit specialization for shared_future<void>
968*38fd1498Szrj  template<>
969*38fd1498Szrj    class shared_future<void> : public __basic_future<void>
970*38fd1498Szrj    {
971*38fd1498Szrj      typedef __basic_future<void> _Base_type;
972*38fd1498Szrj
973*38fd1498Szrj    public:
974*38fd1498Szrj      constexpr shared_future() noexcept : _Base_type() { }
975*38fd1498Szrj
976*38fd1498Szrj      /// Copy constructor
977*38fd1498Szrj      shared_future(const shared_future& __sf) : _Base_type(__sf) { }
978*38fd1498Szrj
979*38fd1498Szrj      /// Construct from a future rvalue
980*38fd1498Szrj      shared_future(future<void>&& __uf) noexcept
981*38fd1498Szrj      : _Base_type(std::move(__uf))
982*38fd1498Szrj      { }
983*38fd1498Szrj
984*38fd1498Szrj      /// Construct from a shared_future rvalue
985*38fd1498Szrj      shared_future(shared_future&& __sf) noexcept
986*38fd1498Szrj      : _Base_type(std::move(__sf))
987*38fd1498Szrj      { }
988*38fd1498Szrj
989*38fd1498Szrj      shared_future& operator=(const shared_future& __sf)
990*38fd1498Szrj      {
991*38fd1498Szrj        shared_future(__sf)._M_swap(*this);
992*38fd1498Szrj        return *this;
993*38fd1498Szrj      }
994*38fd1498Szrj
995*38fd1498Szrj      shared_future& operator=(shared_future&& __sf) noexcept
996*38fd1498Szrj      {
997*38fd1498Szrj        shared_future(std::move(__sf))._M_swap(*this);
998*38fd1498Szrj        return *this;
999*38fd1498Szrj      }
1000*38fd1498Szrj
1001*38fd1498Szrj      // Retrieving the value
1002*38fd1498Szrj      void
1003*38fd1498Szrj      get() const { this->_M_get_result(); }
1004*38fd1498Szrj    };
1005*38fd1498Szrj
1006*38fd1498Szrj  // Now we can define the protected __basic_future constructors.
1007*38fd1498Szrj  template<typename _Res>
1008*38fd1498Szrj    inline __basic_future<_Res>::
1009*38fd1498Szrj    __basic_future(const shared_future<_Res>& __sf) noexcept
1010*38fd1498Szrj    : _M_state(__sf._M_state)
1011*38fd1498Szrj    { }
1012*38fd1498Szrj
1013*38fd1498Szrj  template<typename _Res>
1014*38fd1498Szrj    inline __basic_future<_Res>::
1015*38fd1498Szrj    __basic_future(shared_future<_Res>&& __sf) noexcept
1016*38fd1498Szrj    : _M_state(std::move(__sf._M_state))
1017*38fd1498Szrj    { }
1018*38fd1498Szrj
1019*38fd1498Szrj  template<typename _Res>
1020*38fd1498Szrj    inline __basic_future<_Res>::
1021*38fd1498Szrj    __basic_future(future<_Res>&& __uf) noexcept
1022*38fd1498Szrj    : _M_state(std::move(__uf._M_state))
1023*38fd1498Szrj    { }
1024*38fd1498Szrj
1025*38fd1498Szrj  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1026*38fd1498Szrj  // 2556. Wide contract for future::share()
1027*38fd1498Szrj  template<typename _Res>
1028*38fd1498Szrj    inline shared_future<_Res>
1029*38fd1498Szrj    future<_Res>::share() noexcept
1030*38fd1498Szrj    { return shared_future<_Res>(std::move(*this)); }
1031*38fd1498Szrj
1032*38fd1498Szrj  template<typename _Res>
1033*38fd1498Szrj    inline shared_future<_Res&>
1034*38fd1498Szrj    future<_Res&>::share() noexcept
1035*38fd1498Szrj    { return shared_future<_Res&>(std::move(*this)); }
1036*38fd1498Szrj
1037*38fd1498Szrj  inline shared_future<void>
1038*38fd1498Szrj  future<void>::share() noexcept
1039*38fd1498Szrj  { return shared_future<void>(std::move(*this)); }
1040*38fd1498Szrj
1041*38fd1498Szrj  /// Primary template for promise
1042*38fd1498Szrj  template<typename _Res>
1043*38fd1498Szrj    class promise
1044*38fd1498Szrj    {
1045*38fd1498Szrj      typedef __future_base::_State_base 	_State;
1046*38fd1498Szrj      typedef __future_base::_Result<_Res>	_Res_type;
1047*38fd1498Szrj      typedef __future_base::_Ptr<_Res_type>	_Ptr_type;
1048*38fd1498Szrj      template<typename, typename> friend class _State::_Setter;
1049*38fd1498Szrj      friend _State;
1050*38fd1498Szrj
1051*38fd1498Szrj      shared_ptr<_State>                        _M_future;
1052*38fd1498Szrj      _Ptr_type                                 _M_storage;
1053*38fd1498Szrj
1054*38fd1498Szrj    public:
1055*38fd1498Szrj      promise()
1056*38fd1498Szrj      : _M_future(std::make_shared<_State>()),
1057*38fd1498Szrj	_M_storage(new _Res_type())
1058*38fd1498Szrj      { }
1059*38fd1498Szrj
1060*38fd1498Szrj      promise(promise&& __rhs) noexcept
1061*38fd1498Szrj      : _M_future(std::move(__rhs._M_future)),
1062*38fd1498Szrj	_M_storage(std::move(__rhs._M_storage))
1063*38fd1498Szrj      { }
1064*38fd1498Szrj
1065*38fd1498Szrj      template<typename _Allocator>
1066*38fd1498Szrj        promise(allocator_arg_t, const _Allocator& __a)
1067*38fd1498Szrj        : _M_future(std::allocate_shared<_State>(__a)),
1068*38fd1498Szrj	  _M_storage(__future_base::_S_allocate_result<_Res>(__a))
1069*38fd1498Szrj        { }
1070*38fd1498Szrj
1071*38fd1498Szrj      template<typename _Allocator>
1072*38fd1498Szrj        promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1073*38fd1498Szrj        : _M_future(std::move(__rhs._M_future)),
1074*38fd1498Szrj	  _M_storage(std::move(__rhs._M_storage))
1075*38fd1498Szrj        { }
1076*38fd1498Szrj
1077*38fd1498Szrj      promise(const promise&) = delete;
1078*38fd1498Szrj
1079*38fd1498Szrj      ~promise()
1080*38fd1498Szrj      {
1081*38fd1498Szrj        if (static_cast<bool>(_M_future) && !_M_future.unique())
1082*38fd1498Szrj          _M_future->_M_break_promise(std::move(_M_storage));
1083*38fd1498Szrj      }
1084*38fd1498Szrj
1085*38fd1498Szrj      // Assignment
1086*38fd1498Szrj      promise&
1087*38fd1498Szrj      operator=(promise&& __rhs) noexcept
1088*38fd1498Szrj      {
1089*38fd1498Szrj        promise(std::move(__rhs)).swap(*this);
1090*38fd1498Szrj        return *this;
1091*38fd1498Szrj      }
1092*38fd1498Szrj
1093*38fd1498Szrj      promise& operator=(const promise&) = delete;
1094*38fd1498Szrj
1095*38fd1498Szrj      void
1096*38fd1498Szrj      swap(promise& __rhs) noexcept
1097*38fd1498Szrj      {
1098*38fd1498Szrj        _M_future.swap(__rhs._M_future);
1099*38fd1498Szrj        _M_storage.swap(__rhs._M_storage);
1100*38fd1498Szrj      }
1101*38fd1498Szrj
1102*38fd1498Szrj      // Retrieving the result
1103*38fd1498Szrj      future<_Res>
1104*38fd1498Szrj      get_future()
1105*38fd1498Szrj      { return future<_Res>(_M_future); }
1106*38fd1498Szrj
1107*38fd1498Szrj      // Setting the result
1108*38fd1498Szrj      void
1109*38fd1498Szrj      set_value(const _Res& __r)
1110*38fd1498Szrj      { _M_future->_M_set_result(_State::__setter(this, __r)); }
1111*38fd1498Szrj
1112*38fd1498Szrj      void
1113*38fd1498Szrj      set_value(_Res&& __r)
1114*38fd1498Szrj      { _M_future->_M_set_result(_State::__setter(this, std::move(__r))); }
1115*38fd1498Szrj
1116*38fd1498Szrj      void
1117*38fd1498Szrj      set_exception(exception_ptr __p)
1118*38fd1498Szrj      { _M_future->_M_set_result(_State::__setter(__p, this)); }
1119*38fd1498Szrj
1120*38fd1498Szrj      void
1121*38fd1498Szrj      set_value_at_thread_exit(const _Res& __r)
1122*38fd1498Szrj      {
1123*38fd1498Szrj	_M_future->_M_set_delayed_result(_State::__setter(this, __r),
1124*38fd1498Szrj					 _M_future);
1125*38fd1498Szrj      }
1126*38fd1498Szrj
1127*38fd1498Szrj      void
1128*38fd1498Szrj      set_value_at_thread_exit(_Res&& __r)
1129*38fd1498Szrj      {
1130*38fd1498Szrj	_M_future->_M_set_delayed_result(
1131*38fd1498Szrj	    _State::__setter(this, std::move(__r)), _M_future);
1132*38fd1498Szrj      }
1133*38fd1498Szrj
1134*38fd1498Szrj      void
1135*38fd1498Szrj      set_exception_at_thread_exit(exception_ptr __p)
1136*38fd1498Szrj      {
1137*38fd1498Szrj	_M_future->_M_set_delayed_result(_State::__setter(__p, this),
1138*38fd1498Szrj					 _M_future);
1139*38fd1498Szrj      }
1140*38fd1498Szrj    };
1141*38fd1498Szrj
1142*38fd1498Szrj  template<typename _Res>
1143*38fd1498Szrj    inline void
1144*38fd1498Szrj    swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1145*38fd1498Szrj    { __x.swap(__y); }
1146*38fd1498Szrj
1147*38fd1498Szrj  template<typename _Res, typename _Alloc>
1148*38fd1498Szrj    struct uses_allocator<promise<_Res>, _Alloc>
1149*38fd1498Szrj    : public true_type { };
1150*38fd1498Szrj
1151*38fd1498Szrj
1152*38fd1498Szrj  /// Partial specialization for promise<R&>
1153*38fd1498Szrj  template<typename _Res>
1154*38fd1498Szrj    class promise<_Res&>
1155*38fd1498Szrj    {
1156*38fd1498Szrj      typedef __future_base::_State_base	_State;
1157*38fd1498Szrj      typedef __future_base::_Result<_Res&>	_Res_type;
1158*38fd1498Szrj      typedef __future_base::_Ptr<_Res_type> 	_Ptr_type;
1159*38fd1498Szrj      template<typename, typename> friend class _State::_Setter;
1160*38fd1498Szrj      friend _State;
1161*38fd1498Szrj
1162*38fd1498Szrj      shared_ptr<_State>                        _M_future;
1163*38fd1498Szrj      _Ptr_type                                 _M_storage;
1164*38fd1498Szrj
1165*38fd1498Szrj    public:
1166*38fd1498Szrj      promise()
1167*38fd1498Szrj      : _M_future(std::make_shared<_State>()),
1168*38fd1498Szrj	_M_storage(new _Res_type())
1169*38fd1498Szrj      { }
1170*38fd1498Szrj
1171*38fd1498Szrj      promise(promise&& __rhs) noexcept
1172*38fd1498Szrj      : _M_future(std::move(__rhs._M_future)),
1173*38fd1498Szrj	_M_storage(std::move(__rhs._M_storage))
1174*38fd1498Szrj      { }
1175*38fd1498Szrj
1176*38fd1498Szrj      template<typename _Allocator>
1177*38fd1498Szrj        promise(allocator_arg_t, const _Allocator& __a)
1178*38fd1498Szrj        : _M_future(std::allocate_shared<_State>(__a)),
1179*38fd1498Szrj	  _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1180*38fd1498Szrj        { }
1181*38fd1498Szrj
1182*38fd1498Szrj      template<typename _Allocator>
1183*38fd1498Szrj        promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1184*38fd1498Szrj        : _M_future(std::move(__rhs._M_future)),
1185*38fd1498Szrj	  _M_storage(std::move(__rhs._M_storage))
1186*38fd1498Szrj        { }
1187*38fd1498Szrj
1188*38fd1498Szrj      promise(const promise&) = delete;
1189*38fd1498Szrj
1190*38fd1498Szrj      ~promise()
1191*38fd1498Szrj      {
1192*38fd1498Szrj        if (static_cast<bool>(_M_future) && !_M_future.unique())
1193*38fd1498Szrj          _M_future->_M_break_promise(std::move(_M_storage));
1194*38fd1498Szrj      }
1195*38fd1498Szrj
1196*38fd1498Szrj      // Assignment
1197*38fd1498Szrj      promise&
1198*38fd1498Szrj      operator=(promise&& __rhs) noexcept
1199*38fd1498Szrj      {
1200*38fd1498Szrj        promise(std::move(__rhs)).swap(*this);
1201*38fd1498Szrj        return *this;
1202*38fd1498Szrj      }
1203*38fd1498Szrj
1204*38fd1498Szrj      promise& operator=(const promise&) = delete;
1205*38fd1498Szrj
1206*38fd1498Szrj      void
1207*38fd1498Szrj      swap(promise& __rhs) noexcept
1208*38fd1498Szrj      {
1209*38fd1498Szrj        _M_future.swap(__rhs._M_future);
1210*38fd1498Szrj        _M_storage.swap(__rhs._M_storage);
1211*38fd1498Szrj      }
1212*38fd1498Szrj
1213*38fd1498Szrj      // Retrieving the result
1214*38fd1498Szrj      future<_Res&>
1215*38fd1498Szrj      get_future()
1216*38fd1498Szrj      { return future<_Res&>(_M_future); }
1217*38fd1498Szrj
1218*38fd1498Szrj      // Setting the result
1219*38fd1498Szrj      void
1220*38fd1498Szrj      set_value(_Res& __r)
1221*38fd1498Szrj      { _M_future->_M_set_result(_State::__setter(this, __r)); }
1222*38fd1498Szrj
1223*38fd1498Szrj      void
1224*38fd1498Szrj      set_exception(exception_ptr __p)
1225*38fd1498Szrj      { _M_future->_M_set_result(_State::__setter(__p, this)); }
1226*38fd1498Szrj
1227*38fd1498Szrj      void
1228*38fd1498Szrj      set_value_at_thread_exit(_Res& __r)
1229*38fd1498Szrj      {
1230*38fd1498Szrj	_M_future->_M_set_delayed_result(_State::__setter(this, __r),
1231*38fd1498Szrj					 _M_future);
1232*38fd1498Szrj      }
1233*38fd1498Szrj
1234*38fd1498Szrj      void
1235*38fd1498Szrj      set_exception_at_thread_exit(exception_ptr __p)
1236*38fd1498Szrj      {
1237*38fd1498Szrj	_M_future->_M_set_delayed_result(_State::__setter(__p, this),
1238*38fd1498Szrj					 _M_future);
1239*38fd1498Szrj      }
1240*38fd1498Szrj    };
1241*38fd1498Szrj
1242*38fd1498Szrj  /// Explicit specialization for promise<void>
1243*38fd1498Szrj  template<>
1244*38fd1498Szrj    class promise<void>
1245*38fd1498Szrj    {
1246*38fd1498Szrj      typedef __future_base::_State_base	_State;
1247*38fd1498Szrj      typedef __future_base::_Result<void>	_Res_type;
1248*38fd1498Szrj      typedef __future_base::_Ptr<_Res_type> 	_Ptr_type;
1249*38fd1498Szrj      template<typename, typename> friend class _State::_Setter;
1250*38fd1498Szrj      friend _State;
1251*38fd1498Szrj
1252*38fd1498Szrj      shared_ptr<_State>                        _M_future;
1253*38fd1498Szrj      _Ptr_type                                 _M_storage;
1254*38fd1498Szrj
1255*38fd1498Szrj    public:
1256*38fd1498Szrj      promise()
1257*38fd1498Szrj      : _M_future(std::make_shared<_State>()),
1258*38fd1498Szrj	_M_storage(new _Res_type())
1259*38fd1498Szrj      { }
1260*38fd1498Szrj
1261*38fd1498Szrj      promise(promise&& __rhs) noexcept
1262*38fd1498Szrj      : _M_future(std::move(__rhs._M_future)),
1263*38fd1498Szrj	_M_storage(std::move(__rhs._M_storage))
1264*38fd1498Szrj      { }
1265*38fd1498Szrj
1266*38fd1498Szrj      template<typename _Allocator>
1267*38fd1498Szrj        promise(allocator_arg_t, const _Allocator& __a)
1268*38fd1498Szrj        : _M_future(std::allocate_shared<_State>(__a)),
1269*38fd1498Szrj	  _M_storage(__future_base::_S_allocate_result<void>(__a))
1270*38fd1498Szrj        { }
1271*38fd1498Szrj
1272*38fd1498Szrj      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1273*38fd1498Szrj      // 2095.  missing constructors needed for uses-allocator construction
1274*38fd1498Szrj      template<typename _Allocator>
1275*38fd1498Szrj        promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1276*38fd1498Szrj        : _M_future(std::move(__rhs._M_future)),
1277*38fd1498Szrj	  _M_storage(std::move(__rhs._M_storage))
1278*38fd1498Szrj        { }
1279*38fd1498Szrj
1280*38fd1498Szrj      promise(const promise&) = delete;
1281*38fd1498Szrj
1282*38fd1498Szrj      ~promise()
1283*38fd1498Szrj      {
1284*38fd1498Szrj        if (static_cast<bool>(_M_future) && !_M_future.unique())
1285*38fd1498Szrj          _M_future->_M_break_promise(std::move(_M_storage));
1286*38fd1498Szrj      }
1287*38fd1498Szrj
1288*38fd1498Szrj      // Assignment
1289*38fd1498Szrj      promise&
1290*38fd1498Szrj      operator=(promise&& __rhs) noexcept
1291*38fd1498Szrj      {
1292*38fd1498Szrj        promise(std::move(__rhs)).swap(*this);
1293*38fd1498Szrj        return *this;
1294*38fd1498Szrj      }
1295*38fd1498Szrj
1296*38fd1498Szrj      promise& operator=(const promise&) = delete;
1297*38fd1498Szrj
1298*38fd1498Szrj      void
1299*38fd1498Szrj      swap(promise& __rhs) noexcept
1300*38fd1498Szrj      {
1301*38fd1498Szrj        _M_future.swap(__rhs._M_future);
1302*38fd1498Szrj        _M_storage.swap(__rhs._M_storage);
1303*38fd1498Szrj      }
1304*38fd1498Szrj
1305*38fd1498Szrj      // Retrieving the result
1306*38fd1498Szrj      future<void>
1307*38fd1498Szrj      get_future()
1308*38fd1498Szrj      { return future<void>(_M_future); }
1309*38fd1498Szrj
1310*38fd1498Szrj      // Setting the result
1311*38fd1498Szrj      void
1312*38fd1498Szrj      set_value()
1313*38fd1498Szrj      { _M_future->_M_set_result(_State::__setter(this)); }
1314*38fd1498Szrj
1315*38fd1498Szrj      void
1316*38fd1498Szrj      set_exception(exception_ptr __p)
1317*38fd1498Szrj      { _M_future->_M_set_result(_State::__setter(__p, this)); }
1318*38fd1498Szrj
1319*38fd1498Szrj      void
1320*38fd1498Szrj      set_value_at_thread_exit()
1321*38fd1498Szrj      { _M_future->_M_set_delayed_result(_State::__setter(this), _M_future); }
1322*38fd1498Szrj
1323*38fd1498Szrj      void
1324*38fd1498Szrj      set_exception_at_thread_exit(exception_ptr __p)
1325*38fd1498Szrj      {
1326*38fd1498Szrj	_M_future->_M_set_delayed_result(_State::__setter(__p, this),
1327*38fd1498Szrj					 _M_future);
1328*38fd1498Szrj      }
1329*38fd1498Szrj    };
1330*38fd1498Szrj
1331*38fd1498Szrj  template<typename _Ptr_type, typename _Fn, typename _Res>
1332*38fd1498Szrj    struct __future_base::_Task_setter
1333*38fd1498Szrj    {
1334*38fd1498Szrj      // Invoke the function and provide the result to the caller.
1335*38fd1498Szrj      _Ptr_type operator()() const
1336*38fd1498Szrj      {
1337*38fd1498Szrj	__try
1338*38fd1498Szrj	  {
1339*38fd1498Szrj	    (*_M_result)->_M_set((*_M_fn)());
1340*38fd1498Szrj	  }
1341*38fd1498Szrj	__catch(const __cxxabiv1::__forced_unwind&)
1342*38fd1498Szrj	  {
1343*38fd1498Szrj	    __throw_exception_again; // will cause broken_promise
1344*38fd1498Szrj	  }
1345*38fd1498Szrj	__catch(...)
1346*38fd1498Szrj	  {
1347*38fd1498Szrj	    (*_M_result)->_M_error = current_exception();
1348*38fd1498Szrj	  }
1349*38fd1498Szrj	return std::move(*_M_result);
1350*38fd1498Szrj      }
1351*38fd1498Szrj      _Ptr_type*	_M_result;
1352*38fd1498Szrj      _Fn*		_M_fn;
1353*38fd1498Szrj    };
1354*38fd1498Szrj
1355*38fd1498Szrj  template<typename _Ptr_type, typename _Fn>
1356*38fd1498Szrj    struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
1357*38fd1498Szrj    {
1358*38fd1498Szrj      _Ptr_type operator()() const
1359*38fd1498Szrj      {
1360*38fd1498Szrj	__try
1361*38fd1498Szrj	  {
1362*38fd1498Szrj	    (*_M_fn)();
1363*38fd1498Szrj	  }
1364*38fd1498Szrj	__catch(const __cxxabiv1::__forced_unwind&)
1365*38fd1498Szrj	  {
1366*38fd1498Szrj	    __throw_exception_again; // will cause broken_promise
1367*38fd1498Szrj	  }
1368*38fd1498Szrj	__catch(...)
1369*38fd1498Szrj	  {
1370*38fd1498Szrj	    (*_M_result)->_M_error = current_exception();
1371*38fd1498Szrj	  }
1372*38fd1498Szrj	return std::move(*_M_result);
1373*38fd1498Szrj      }
1374*38fd1498Szrj      _Ptr_type*	_M_result;
1375*38fd1498Szrj      _Fn*		_M_fn;
1376*38fd1498Szrj    };
1377*38fd1498Szrj
1378*38fd1498Szrj  // Holds storage for a packaged_task's result.
1379*38fd1498Szrj  template<typename _Res, typename... _Args>
1380*38fd1498Szrj    struct __future_base::_Task_state_base<_Res(_Args...)>
1381*38fd1498Szrj    : __future_base::_State_base
1382*38fd1498Szrj    {
1383*38fd1498Szrj      typedef _Res _Res_type;
1384*38fd1498Szrj
1385*38fd1498Szrj      template<typename _Alloc>
1386*38fd1498Szrj	_Task_state_base(const _Alloc& __a)
1387*38fd1498Szrj	: _M_result(_S_allocate_result<_Res>(__a))
1388*38fd1498Szrj	{ }
1389*38fd1498Szrj
1390*38fd1498Szrj      // Invoke the stored task and make the state ready.
1391*38fd1498Szrj      virtual void
1392*38fd1498Szrj      _M_run(_Args&&... __args) = 0;
1393*38fd1498Szrj
1394*38fd1498Szrj      // Invoke the stored task and make the state ready at thread exit.
1395*38fd1498Szrj      virtual void
1396*38fd1498Szrj      _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
1397*38fd1498Szrj
1398*38fd1498Szrj      virtual shared_ptr<_Task_state_base>
1399*38fd1498Szrj      _M_reset() = 0;
1400*38fd1498Szrj
1401*38fd1498Szrj      typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1402*38fd1498Szrj      _Ptr_type _M_result;
1403*38fd1498Szrj    };
1404*38fd1498Szrj
1405*38fd1498Szrj  // Holds a packaged_task's stored task.
1406*38fd1498Szrj  template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
1407*38fd1498Szrj    struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
1408*38fd1498Szrj    : __future_base::_Task_state_base<_Res(_Args...)>
1409*38fd1498Szrj    {
1410*38fd1498Szrj      template<typename _Fn2>
1411*38fd1498Szrj	_Task_state(_Fn2&& __fn, const _Alloc& __a)
1412*38fd1498Szrj	: _Task_state_base<_Res(_Args...)>(__a),
1413*38fd1498Szrj	  _M_impl(std::forward<_Fn2>(__fn), __a)
1414*38fd1498Szrj	{ }
1415*38fd1498Szrj
1416*38fd1498Szrj    private:
1417*38fd1498Szrj      virtual void
1418*38fd1498Szrj      _M_run(_Args&&... __args)
1419*38fd1498Szrj      {
1420*38fd1498Szrj	auto __boundfn = [&] () -> typename result_of<_Fn&(_Args&&...)>::type {
1421*38fd1498Szrj	    return std::__invoke(_M_impl._M_fn, std::forward<_Args>(__args)...);
1422*38fd1498Szrj	};
1423*38fd1498Szrj	this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
1424*38fd1498Szrj      }
1425*38fd1498Szrj
1426*38fd1498Szrj      virtual void
1427*38fd1498Szrj      _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
1428*38fd1498Szrj      {
1429*38fd1498Szrj	auto __boundfn = [&] () -> typename result_of<_Fn&(_Args&&...)>::type {
1430*38fd1498Szrj	    return std::__invoke(_M_impl._M_fn, std::forward<_Args>(__args)...);
1431*38fd1498Szrj	};
1432*38fd1498Szrj	this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
1433*38fd1498Szrj				    std::move(__self));
1434*38fd1498Szrj      }
1435*38fd1498Szrj
1436*38fd1498Szrj      virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
1437*38fd1498Szrj      _M_reset();
1438*38fd1498Szrj
1439*38fd1498Szrj      struct _Impl : _Alloc
1440*38fd1498Szrj      {
1441*38fd1498Szrj	template<typename _Fn2>
1442*38fd1498Szrj	  _Impl(_Fn2&& __fn, const _Alloc& __a)
1443*38fd1498Szrj	  : _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { }
1444*38fd1498Szrj	_Fn _M_fn;
1445*38fd1498Szrj      } _M_impl;
1446*38fd1498Szrj    };
1447*38fd1498Szrj
1448*38fd1498Szrj  template<typename _Signature, typename _Fn, typename _Alloc>
1449*38fd1498Szrj    static shared_ptr<__future_base::_Task_state_base<_Signature>>
1450*38fd1498Szrj    __create_task_state(_Fn&& __fn, const _Alloc& __a)
1451*38fd1498Szrj    {
1452*38fd1498Szrj      typedef typename decay<_Fn>::type _Fn2;
1453*38fd1498Szrj      typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
1454*38fd1498Szrj      return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
1455*38fd1498Szrj    }
1456*38fd1498Szrj
1457*38fd1498Szrj  template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
1458*38fd1498Szrj    shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
1459*38fd1498Szrj    __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
1460*38fd1498Szrj    {
1461*38fd1498Szrj      return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
1462*38fd1498Szrj						 static_cast<_Alloc&>(_M_impl));
1463*38fd1498Szrj    }
1464*38fd1498Szrj
1465*38fd1498Szrj  template<typename _Task, typename _Fn, bool
1466*38fd1498Szrj	   = is_same<_Task, typename decay<_Fn>::type>::value>
1467*38fd1498Szrj    struct __constrain_pkgdtask
1468*38fd1498Szrj    { typedef void __type; };
1469*38fd1498Szrj
1470*38fd1498Szrj  template<typename _Task, typename _Fn>
1471*38fd1498Szrj    struct __constrain_pkgdtask<_Task, _Fn, true>
1472*38fd1498Szrj    { };
1473*38fd1498Szrj
1474*38fd1498Szrj  /// packaged_task
1475*38fd1498Szrj  template<typename _Res, typename... _ArgTypes>
1476*38fd1498Szrj    class packaged_task<_Res(_ArgTypes...)>
1477*38fd1498Szrj    {
1478*38fd1498Szrj      typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
1479*38fd1498Szrj      shared_ptr<_State_type>                   _M_state;
1480*38fd1498Szrj
1481*38fd1498Szrj    public:
1482*38fd1498Szrj      // Construction and destruction
1483*38fd1498Szrj      packaged_task() noexcept { }
1484*38fd1498Szrj
1485*38fd1498Szrj      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1486*38fd1498Szrj      // 2095.  missing constructors needed for uses-allocator construction
1487*38fd1498Szrj      template<typename _Allocator>
1488*38fd1498Szrj	packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
1489*38fd1498Szrj	{ }
1490*38fd1498Szrj
1491*38fd1498Szrj      template<typename _Fn, typename = typename
1492*38fd1498Szrj	       __constrain_pkgdtask<packaged_task, _Fn>::__type>
1493*38fd1498Szrj	explicit
1494*38fd1498Szrj	packaged_task(_Fn&& __fn)
1495*38fd1498Szrj	: packaged_task(allocator_arg, std::allocator<int>(),
1496*38fd1498Szrj			std::forward<_Fn>(__fn))
1497*38fd1498Szrj	{ }
1498*38fd1498Szrj
1499*38fd1498Szrj      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1500*38fd1498Szrj      // 2097.  packaged_task constructors should be constrained
1501*38fd1498Szrj      // 2407. [this constructor should not be] explicit
1502*38fd1498Szrj      template<typename _Fn, typename _Alloc, typename = typename
1503*38fd1498Szrj	       __constrain_pkgdtask<packaged_task, _Fn>::__type>
1504*38fd1498Szrj	packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
1505*38fd1498Szrj	: _M_state(__create_task_state<_Res(_ArgTypes...)>(
1506*38fd1498Szrj		    std::forward<_Fn>(__fn), __a))
1507*38fd1498Szrj	{ }
1508*38fd1498Szrj
1509*38fd1498Szrj      ~packaged_task()
1510*38fd1498Szrj      {
1511*38fd1498Szrj        if (static_cast<bool>(_M_state) && !_M_state.unique())
1512*38fd1498Szrj	  _M_state->_M_break_promise(std::move(_M_state->_M_result));
1513*38fd1498Szrj      }
1514*38fd1498Szrj
1515*38fd1498Szrj      // No copy
1516*38fd1498Szrj      packaged_task(const packaged_task&) = delete;
1517*38fd1498Szrj      packaged_task& operator=(const packaged_task&) = delete;
1518*38fd1498Szrj
1519*38fd1498Szrj      template<typename _Allocator>
1520*38fd1498Szrj	packaged_task(allocator_arg_t, const _Allocator&,
1521*38fd1498Szrj		      const packaged_task&) = delete;
1522*38fd1498Szrj
1523*38fd1498Szrj      // Move support
1524*38fd1498Szrj      packaged_task(packaged_task&& __other) noexcept
1525*38fd1498Szrj      { this->swap(__other); }
1526*38fd1498Szrj
1527*38fd1498Szrj      template<typename _Allocator>
1528*38fd1498Szrj	packaged_task(allocator_arg_t, const _Allocator&,
1529*38fd1498Szrj		      packaged_task&& __other) noexcept
1530*38fd1498Szrj	{ this->swap(__other); }
1531*38fd1498Szrj
1532*38fd1498Szrj      packaged_task& operator=(packaged_task&& __other) noexcept
1533*38fd1498Szrj      {
1534*38fd1498Szrj	packaged_task(std::move(__other)).swap(*this);
1535*38fd1498Szrj	return *this;
1536*38fd1498Szrj      }
1537*38fd1498Szrj
1538*38fd1498Szrj      void
1539*38fd1498Szrj      swap(packaged_task& __other) noexcept
1540*38fd1498Szrj      { _M_state.swap(__other._M_state); }
1541*38fd1498Szrj
1542*38fd1498Szrj      bool
1543*38fd1498Szrj      valid() const noexcept
1544*38fd1498Szrj      { return static_cast<bool>(_M_state); }
1545*38fd1498Szrj
1546*38fd1498Szrj      // Result retrieval
1547*38fd1498Szrj      future<_Res>
1548*38fd1498Szrj      get_future()
1549*38fd1498Szrj      { return future<_Res>(_M_state); }
1550*38fd1498Szrj
1551*38fd1498Szrj      // Execution
1552*38fd1498Szrj      void
1553*38fd1498Szrj      operator()(_ArgTypes... __args)
1554*38fd1498Szrj      {
1555*38fd1498Szrj	__future_base::_State_base::_S_check(_M_state);
1556*38fd1498Szrj	_M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1557*38fd1498Szrj      }
1558*38fd1498Szrj
1559*38fd1498Szrj      void
1560*38fd1498Szrj      make_ready_at_thread_exit(_ArgTypes... __args)
1561*38fd1498Szrj      {
1562*38fd1498Szrj	__future_base::_State_base::_S_check(_M_state);
1563*38fd1498Szrj	_M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
1564*38fd1498Szrj      }
1565*38fd1498Szrj
1566*38fd1498Szrj      void
1567*38fd1498Szrj      reset()
1568*38fd1498Szrj      {
1569*38fd1498Szrj	__future_base::_State_base::_S_check(_M_state);
1570*38fd1498Szrj	packaged_task __tmp;
1571*38fd1498Szrj	__tmp._M_state = _M_state;
1572*38fd1498Szrj	_M_state = _M_state->_M_reset();
1573*38fd1498Szrj      }
1574*38fd1498Szrj    };
1575*38fd1498Szrj
1576*38fd1498Szrj  /// swap
1577*38fd1498Szrj  template<typename _Res, typename... _ArgTypes>
1578*38fd1498Szrj    inline void
1579*38fd1498Szrj    swap(packaged_task<_Res(_ArgTypes...)>& __x,
1580*38fd1498Szrj	 packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1581*38fd1498Szrj    { __x.swap(__y); }
1582*38fd1498Szrj
1583*38fd1498Szrj  template<typename _Res, typename _Alloc>
1584*38fd1498Szrj    struct uses_allocator<packaged_task<_Res>, _Alloc>
1585*38fd1498Szrj    : public true_type { };
1586*38fd1498Szrj
1587*38fd1498Szrj
1588*38fd1498Szrj  // Shared state created by std::async().
1589*38fd1498Szrj  // Holds a deferred function and storage for its result.
1590*38fd1498Szrj  template<typename _BoundFn, typename _Res>
1591*38fd1498Szrj    class __future_base::_Deferred_state final
1592*38fd1498Szrj    : public __future_base::_State_base
1593*38fd1498Szrj    {
1594*38fd1498Szrj    public:
1595*38fd1498Szrj      explicit
1596*38fd1498Szrj      _Deferred_state(_BoundFn&& __fn)
1597*38fd1498Szrj      : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1598*38fd1498Szrj      { }
1599*38fd1498Szrj
1600*38fd1498Szrj    private:
1601*38fd1498Szrj      typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1602*38fd1498Szrj      _Ptr_type _M_result;
1603*38fd1498Szrj      _BoundFn _M_fn;
1604*38fd1498Szrj
1605*38fd1498Szrj      // Run the deferred function.
1606*38fd1498Szrj      virtual void
1607*38fd1498Szrj      _M_complete_async()
1608*38fd1498Szrj      {
1609*38fd1498Szrj	// Multiple threads can call a waiting function on the future and
1610*38fd1498Szrj	// reach this point at the same time. The call_once in _M_set_result
1611*38fd1498Szrj	// ensures only the first one run the deferred function, stores the
1612*38fd1498Szrj	// result in _M_result, swaps that with the base _M_result and makes
1613*38fd1498Szrj	// the state ready. Tell _M_set_result to ignore failure so all later
1614*38fd1498Szrj	// calls do nothing.
1615*38fd1498Szrj        _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1616*38fd1498Szrj      }
1617*38fd1498Szrj
1618*38fd1498Szrj      // Caller should check whether the state is ready first, because this
1619*38fd1498Szrj      // function will return true even after the deferred function has run.
1620*38fd1498Szrj      virtual bool _M_is_deferred_future() const { return true; }
1621*38fd1498Szrj    };
1622*38fd1498Szrj
1623*38fd1498Szrj  // Common functionality hoisted out of the _Async_state_impl template.
1624*38fd1498Szrj  class __future_base::_Async_state_commonV2
1625*38fd1498Szrj    : public __future_base::_State_base
1626*38fd1498Szrj  {
1627*38fd1498Szrj  protected:
1628*38fd1498Szrj    ~_Async_state_commonV2() = default;
1629*38fd1498Szrj
1630*38fd1498Szrj    // Make waiting functions block until the thread completes, as if joined.
1631*38fd1498Szrj    //
1632*38fd1498Szrj    // This function is used by wait() to satisfy the first requirement below
1633*38fd1498Szrj    // and by wait_for() / wait_until() to satisfy the second.
1634*38fd1498Szrj    //
1635*38fd1498Szrj    // [futures.async]:
1636*38fd1498Szrj    //
1637*38fd1498Szrj    // — a call to a waiting function on an asynchronous return object that
1638*38fd1498Szrj    // shares the shared state created by this async call shall block until
1639*38fd1498Szrj    // the associated thread has completed, as if joined, or else time out.
1640*38fd1498Szrj    //
1641*38fd1498Szrj    // — the associated thread completion synchronizes with the return from
1642*38fd1498Szrj    // the first function that successfully detects the ready status of the
1643*38fd1498Szrj    // shared state or with the return from the last function that releases
1644*38fd1498Szrj    // the shared state, whichever happens first.
1645*38fd1498Szrj    virtual void _M_complete_async() { _M_join(); }
1646*38fd1498Szrj
1647*38fd1498Szrj    void _M_join() { std::call_once(_M_once, &thread::join, &_M_thread); }
1648*38fd1498Szrj
1649*38fd1498Szrj    thread _M_thread;
1650*38fd1498Szrj    once_flag _M_once;
1651*38fd1498Szrj  };
1652*38fd1498Szrj
1653*38fd1498Szrj  // Shared state created by std::async().
1654*38fd1498Szrj  // Starts a new thread that runs a function and makes the shared state ready.
1655*38fd1498Szrj  template<typename _BoundFn, typename _Res>
1656*38fd1498Szrj    class __future_base::_Async_state_impl final
1657*38fd1498Szrj    : public __future_base::_Async_state_commonV2
1658*38fd1498Szrj    {
1659*38fd1498Szrj    public:
1660*38fd1498Szrj      explicit
1661*38fd1498Szrj      _Async_state_impl(_BoundFn&& __fn)
1662*38fd1498Szrj      : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1663*38fd1498Szrj      {
1664*38fd1498Szrj	_M_thread = std::thread{ [this] {
1665*38fd1498Szrj	    __try
1666*38fd1498Szrj	      {
1667*38fd1498Szrj		_M_set_result(_S_task_setter(_M_result, _M_fn));
1668*38fd1498Szrj	      }
1669*38fd1498Szrj	    __catch (const __cxxabiv1::__forced_unwind&)
1670*38fd1498Szrj	      {
1671*38fd1498Szrj		// make the shared state ready on thread cancellation
1672*38fd1498Szrj		if (static_cast<bool>(_M_result))
1673*38fd1498Szrj		  this->_M_break_promise(std::move(_M_result));
1674*38fd1498Szrj		__throw_exception_again;
1675*38fd1498Szrj	      }
1676*38fd1498Szrj        } };
1677*38fd1498Szrj      }
1678*38fd1498Szrj
1679*38fd1498Szrj      // Must not destroy _M_result and _M_fn until the thread finishes.
1680*38fd1498Szrj      // Call join() directly rather than through _M_join() because no other
1681*38fd1498Szrj      // thread can be referring to this state if it is being destroyed.
1682*38fd1498Szrj      ~_Async_state_impl() { if (_M_thread.joinable()) _M_thread.join(); }
1683*38fd1498Szrj
1684*38fd1498Szrj    private:
1685*38fd1498Szrj      typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1686*38fd1498Szrj      _Ptr_type _M_result;
1687*38fd1498Szrj      _BoundFn _M_fn;
1688*38fd1498Szrj    };
1689*38fd1498Szrj
1690*38fd1498Szrj  template<typename _BoundFn>
1691*38fd1498Szrj    inline std::shared_ptr<__future_base::_State_base>
1692*38fd1498Szrj    __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1693*38fd1498Szrj    {
1694*38fd1498Szrj      typedef typename remove_reference<_BoundFn>::type __fn_type;
1695*38fd1498Szrj      typedef _Deferred_state<__fn_type> __state_type;
1696*38fd1498Szrj      return std::make_shared<__state_type>(std::move(__fn));
1697*38fd1498Szrj    }
1698*38fd1498Szrj
1699*38fd1498Szrj  template<typename _BoundFn>
1700*38fd1498Szrj    inline std::shared_ptr<__future_base::_State_base>
1701*38fd1498Szrj    __future_base::_S_make_async_state(_BoundFn&& __fn)
1702*38fd1498Szrj    {
1703*38fd1498Szrj      typedef typename remove_reference<_BoundFn>::type __fn_type;
1704*38fd1498Szrj      typedef _Async_state_impl<__fn_type> __state_type;
1705*38fd1498Szrj      return std::make_shared<__state_type>(std::move(__fn));
1706*38fd1498Szrj    }
1707*38fd1498Szrj
1708*38fd1498Szrj
1709*38fd1498Szrj  /// async
1710*38fd1498Szrj  template<typename _Fn, typename... _Args>
1711*38fd1498Szrj    future<__async_result_of<_Fn, _Args...>>
1712*38fd1498Szrj    async(launch __policy, _Fn&& __fn, _Args&&... __args)
1713*38fd1498Szrj    {
1714*38fd1498Szrj      std::shared_ptr<__future_base::_State_base> __state;
1715*38fd1498Szrj      if ((__policy & launch::async) == launch::async)
1716*38fd1498Szrj	{
1717*38fd1498Szrj	  __try
1718*38fd1498Szrj	    {
1719*38fd1498Szrj	      __state = __future_base::_S_make_async_state(
1720*38fd1498Szrj		  std::thread::__make_invoker(std::forward<_Fn>(__fn),
1721*38fd1498Szrj					      std::forward<_Args>(__args)...)
1722*38fd1498Szrj		  );
1723*38fd1498Szrj	    }
1724*38fd1498Szrj#if __cpp_exceptions
1725*38fd1498Szrj	  catch(const system_error& __e)
1726*38fd1498Szrj	    {
1727*38fd1498Szrj	      if (__e.code() != errc::resource_unavailable_try_again
1728*38fd1498Szrj		  || (__policy & launch::deferred) != launch::deferred)
1729*38fd1498Szrj		throw;
1730*38fd1498Szrj	    }
1731*38fd1498Szrj#endif
1732*38fd1498Szrj	}
1733*38fd1498Szrj      if (!__state)
1734*38fd1498Szrj	{
1735*38fd1498Szrj	  __state = __future_base::_S_make_deferred_state(
1736*38fd1498Szrj	      std::thread::__make_invoker(std::forward<_Fn>(__fn),
1737*38fd1498Szrj					  std::forward<_Args>(__args)...));
1738*38fd1498Szrj	}
1739*38fd1498Szrj      return future<__async_result_of<_Fn, _Args...>>(__state);
1740*38fd1498Szrj    }
1741*38fd1498Szrj
1742*38fd1498Szrj  /// async, potential overload
1743*38fd1498Szrj  template<typename _Fn, typename... _Args>
1744*38fd1498Szrj    inline future<__async_result_of<_Fn, _Args...>>
1745*38fd1498Szrj    async(_Fn&& __fn, _Args&&... __args)
1746*38fd1498Szrj    {
1747*38fd1498Szrj      return std::async(launch::async|launch::deferred,
1748*38fd1498Szrj			std::forward<_Fn>(__fn),
1749*38fd1498Szrj			std::forward<_Args>(__args)...);
1750*38fd1498Szrj    }
1751*38fd1498Szrj
1752*38fd1498Szrj#endif // _GLIBCXX_ASYNC_ABI_COMPAT
1753*38fd1498Szrj#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1754*38fd1498Szrj
1755*38fd1498Szrj  // @} group futures
1756*38fd1498Szrj_GLIBCXX_END_NAMESPACE_VERSION
1757*38fd1498Szrj} // namespace
1758*38fd1498Szrj
1759*38fd1498Szrj#endif // C++11
1760*38fd1498Szrj
1761*38fd1498Szrj#endif // _GLIBCXX_FUTURE
1762