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