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