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