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