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