1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_FUTURE
11#define _LIBCPP_FUTURE
12
13/*
14 future synopsis
15
16namespace std
17{
18
19enum class future_errc
20{
21 future_already_retrieved = 1,
22 promise_already_satisfied,
23 no_state,
24 broken_promise
25};
26
27enum class launch
28{
29 async = 1,
30 deferred = 2,
31 any = async | deferred
32};
33
34enum class future_status
35{
36 ready,
37 timeout,
38 deferred
39};
40
41template <> struct is_error_code_enum<future_errc> : public true_type { };
42error_code make_error_code(future_errc e) noexcept;
43error_condition make_error_condition(future_errc e) noexcept;
44
45const error_category& future_category() noexcept;
46
47class future_error : public logic_error {
48public:
49 explicit future_error(future_errc e); // since C++17
50
51 const error_code& code() const noexcept;
52 const char* what() const noexcept;
53
54private:
55 error_code ec_; // exposition only
56};
57
58template <class R>
59class promise
60{
61public:
62 promise();
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
65 promise(promise&& rhs) noexcept;
66 promise(const promise& rhs) = delete;
67 ~promise();
68
69 // assignment
70 promise& operator=(promise&& rhs) noexcept;
71 promise& operator=(const promise& rhs) = delete;
72 void swap(promise& other) noexcept;
73
74 // retrieving the result
75 future<R> get_future();
76
77 // setting the result
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
81
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
86};
87
88template <class R>
89class promise<R&>
90{
91public:
92 promise();
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
95 promise(promise&& rhs) noexcept;
96 promise(const promise& rhs) = delete;
97 ~promise();
98
99 // assignment
100 promise& operator=(promise&& rhs) noexcept;
101 promise& operator=(const promise& rhs) = delete;
102 void swap(promise& other) noexcept;
103
104 // retrieving the result
105 future<R&> get_future();
106
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
110
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
114};
115
116template <>
117class promise<void>
118{
119public:
120 promise();
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
123 promise(promise&& rhs) noexcept;
124 promise(const promise& rhs) = delete;
125 ~promise();
126
127 // assignment
128 promise& operator=(promise&& rhs) noexcept;
129 promise& operator=(const promise& rhs) = delete;
130 void swap(promise& other) noexcept;
131
132 // retrieving the result
133 future<void> get_future();
134
135 // setting the result
136 void set_value();
137 void set_exception(exception_ptr p);
138
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
142};
143
144template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
145
146template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
148
149template <class R>
150class future
151{
152public:
153 future() noexcept;
154 future(future&&) noexcept;
155 future(const future& rhs) = delete;
156 ~future();
157 future& operator=(const future& rhs) = delete;
158 future& operator=(future&&) noexcept;
159 shared_future<R> share() noexcept;
160
161 // retrieving the value
162 R get();
163
164 // functions to check state
165 bool valid() const noexcept;
166
167 void wait() const;
168 template <class Rep, class Period>
169 future_status
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
172 future_status
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
174};
175
176template <class R>
177class future<R&>
178{
179public:
180 future() noexcept;
181 future(future&&) noexcept;
182 future(const future& rhs) = delete;
183 ~future();
184 future& operator=(const future& rhs) = delete;
185 future& operator=(future&&) noexcept;
186 shared_future<R&> share() noexcept;
187
188 // retrieving the value
189 R& get();
190
191 // functions to check state
192 bool valid() const noexcept;
193
194 void wait() const;
195 template <class Rep, class Period>
196 future_status
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
199 future_status
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
201};
202
203template <>
204class future<void>
205{
206public:
207 future() noexcept;
208 future(future&&) noexcept;
209 future(const future& rhs) = delete;
210 ~future();
211 future& operator=(const future& rhs) = delete;
212 future& operator=(future&&) noexcept;
213 shared_future<void> share() noexcept;
214
215 // retrieving the value
216 void get();
217
218 // functions to check state
219 bool valid() const noexcept;
220
221 void wait() const;
222 template <class Rep, class Period>
223 future_status
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
226 future_status
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
228};
229
230template <class R>
231class shared_future
232{
233public:
234 shared_future() noexcept;
235 shared_future(const shared_future& rhs);
236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
238 ~shared_future();
239 shared_future& operator=(const shared_future& rhs);
240 shared_future& operator=(shared_future&& rhs) noexcept;
241
242 // retrieving the value
243 const R& get() const;
244
245 // functions to check state
246 bool valid() const noexcept;
247
248 void wait() const;
249 template <class Rep, class Period>
250 future_status
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
253 future_status
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
255};
256
257template <class R>
258class shared_future<R&>
259{
260public:
261 shared_future() noexcept;
262 shared_future(const shared_future& rhs);
263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
265 ~shared_future();
266 shared_future& operator=(const shared_future& rhs);
267 shared_future& operator=(shared_future&& rhs) noexcept;
268
269 // retrieving the value
270 R& get() const;
271
272 // functions to check state
273 bool valid() const noexcept;
274
275 void wait() const;
276 template <class Rep, class Period>
277 future_status
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
280 future_status
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
282};
283
284template <>
285class shared_future<void>
286{
287public:
288 shared_future() noexcept;
289 shared_future(const shared_future& rhs);
290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
292 ~shared_future();
293 shared_future& operator=(const shared_future& rhs);
294 shared_future& operator=(shared_future&& rhs) noexcept;
295
296 // retrieving the value
297 void get() const;
298
299 // functions to check state
300 bool valid() const noexcept;
301
302 void wait() const;
303 template <class Rep, class Period>
304 future_status
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
307 future_status
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
309};
310
311template <class F, class... Args>
312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
313 async(F&& f, Args&&... args);
314
315template <class F, class... Args>
316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
317 async(launch policy, F&& f, Args&&... args);
318
319template <class> class packaged_task; // undefined
320
321template <class R, class... ArgTypes>
322class packaged_task<R(ArgTypes...)>
323{
324public:
325 // construction and destruction
326 packaged_task() noexcept;
327 template <class F>
328 explicit packaged_task(F&& f);
329 template <class F, class Allocator>
330 packaged_task(allocator_arg_t, const Allocator& a, F&& f); // removed in C++17
331 ~packaged_task();
332
333 // no copy
334 packaged_task(const packaged_task&) = delete;
335 packaged_task& operator=(const packaged_task&) = delete;
336
337 // move support
338 packaged_task(packaged_task&& other) noexcept;
339 packaged_task& operator=(packaged_task&& other) noexcept;
340 void swap(packaged_task& other) noexcept;
341
342 bool valid() const noexcept;
343
344 // result retrieval
345 future<R> get_future();
346
347 // execution
348 void operator()(ArgTypes... );
349 void make_ready_at_thread_exit(ArgTypes...);
350
351 void reset();
352};
353
354template <class R>
355 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
356
357template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>; // removed in C++17
358
359} // std
360
361*/
362
363#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
364# include <__cxx03/future>
365#else
366# include <__config>
367
368# if _LIBCPP_HAS_THREADS
369
370# include <__assert>
371# include <__chrono/duration.h>
372# include <__chrono/steady_clock.h>
373# include <__chrono/time_point.h>
374# include <__condition_variable/condition_variable.h>
375# include <__cstddef/nullptr_t.h>
376# include <__exception/exception_ptr.h>
377# include <__memory/addressof.h>
378# include <__memory/allocator.h>
379# include <__memory/allocator_arg_t.h>
380# include <__memory/allocator_destructor.h>
381# include <__memory/allocator_traits.h>
382# include <__memory/pointer_traits.h>
383# include <__memory/shared_count.h>
384# include <__memory/unique_ptr.h>
385# include <__memory/uses_allocator.h>
386# include <__mutex/lock_guard.h>
387# include <__mutex/mutex.h>
388# include <__mutex/unique_lock.h>
389# include <__system_error/error_category.h>
390# include <__system_error/error_code.h>
391# include <__system_error/error_condition.h>
392# include <__thread/thread.h>
393# include <__type_traits/add_reference.h>
394# include <__type_traits/aligned_storage.h>
395# include <__type_traits/conditional.h>
396# include <__type_traits/decay.h>
397# include <__type_traits/enable_if.h>
398# include <__type_traits/invoke.h>
399# include <__type_traits/is_constructible.h>
400# include <__type_traits/is_same.h>
401# include <__type_traits/remove_cvref.h>
402# include <__type_traits/remove_reference.h>
403# include <__type_traits/strip_signature.h>
404# include <__type_traits/underlying_type.h>
405# include <__utility/auto_cast.h>
406# include <__utility/exception_guard.h>
407# include <__utility/forward.h>
408# include <__utility/move.h>
409# include <__utility/swap.h>
410# include <stdexcept>
411# include <tuple>
412# include <version>
413
414# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
415# pragma GCC system_header
416# endif
417
418_LIBCPP_PUSH_MACROS
419# include <__undef_macros>
420
421_LIBCPP_BEGIN_NAMESPACE_STD
422_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
423
424// enum class future_errc
425_LIBCPP_DECLARE_STRONG_ENUM(future_errc){
426 future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};
427_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
428
429template <>
430struct is_error_code_enum<future_errc> : public true_type {};
431
432# ifdef _LIBCPP_CXX03_LANG
433template <>
434struct is_error_code_enum<future_errc::__lx> : public true_type {};
435# endif
436
437// enum class launch
438_LIBCPP_DECLARE_STRONG_ENUM(launch){
439 async = 1,
440 deferred = 2,
441 any _LIBCPP_DEPRECATED_("std::launch::any is a deprecated extension. "
442 "Use std::launch::async | std::launch::deferred instead. "
443 "It will be removed in LLVM 25.") = async | deferred};
444_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
445
446# ifndef _LIBCPP_CXX03_LANG
447
448using __launch_underlying_type _LIBCPP_NODEBUG = __underlying_type_t<launch>;
449
450inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator&(launch __x, launch __y) {
451 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));
452}
453
454inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator|(launch __x, launch __y) {
455 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | static_cast<__launch_underlying_type>(__y));
456}
457
458inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator^(launch __x, launch __y) {
459 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));
460}
461
462inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator~(launch __x) {
463 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
464}
465
466inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {
467 __x = __x & __y;
468 return __x;
469}
470
471inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {
472 __x = __x | __y;
473 return __x;
474}
475
476inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {
477 __x = __x ^ __y;
478 return __x;
479}
480
481# endif // !_LIBCPP_CXX03_LANG
482
483// enum class future_status
484_LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};
485_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
486
487[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
488
489[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {
490 return error_code(static_cast<int>(__e), future_category());
491}
492
493[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {
494 return error_condition(static_cast<int>(__e), future_category());
495}
496
497[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);
498
499class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {
500 error_code __ec_;
501
502 future_error(error_code);
503 friend void __throw_future_error(future_errc);
504 template <class>
505 friend class promise;
506
507public:
508# if _LIBCPP_STD_VER >= 17
509 _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(e: __ec)) {}
510# endif
511
512 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
513
514 _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
515 ~future_error() _NOEXCEPT override;
516};
517
518// Declared above std::future_error
519void __throw_future_error(future_errc __ev) {
520# if _LIBCPP_HAS_EXCEPTIONS
521 throw future_error(make_error_code(e: __ev));
522# else
523 (void)__ev;
524 _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
525# endif
526}
527
528class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {
529protected:
530 exception_ptr __exception_;
531 mutable mutex __mut_;
532 mutable condition_variable __cv_;
533 unsigned __state_;
534
535 void __on_zero_shared() _NOEXCEPT override;
536 void __sub_wait(unique_lock<mutex>& __lk);
537
538public:
539 enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };
540
541 _LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}
542
543 _LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }
544
545 _LIBCPP_HIDE_FROM_ABI void __attach_future() {
546 lock_guard<mutex> __lk(__mut_);
547 bool __has_future_attached = (__state_ & __future_attached) != 0;
548 if (__has_future_attached)
549 std::__throw_future_error(ev: future_errc::future_already_retrieved);
550 this->__add_shared();
551 __state_ |= __future_attached;
552 }
553
554 _LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }
555
556 void __make_ready();
557 _LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }
558
559 void set_value();
560 void set_value_at_thread_exit();
561
562 void set_exception(exception_ptr __p);
563 void set_exception_at_thread_exit(exception_ptr __p);
564
565 void copy();
566
567 void wait();
568 template <class _Rep, class _Period>
569 future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
570 template <class _Clock, class _Duration>
571 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
572 unique_lock<mutex> __lk(__mut_);
573 if (__state_ & deferred)
574 return future_status::deferred;
575 while (!(__state_ & ready) && _Clock::now() < __abs_time)
576 __cv_.wait_until(__lk, __abs_time);
577 if (__state_ & ready)
578 return future_status::ready;
579 return future_status::timeout;
580 }
581
582 virtual void __execute();
583};
584
585template <class _Rep, class _Period>
586inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
587 return wait_until(chrono::steady_clock::now() + __rel_time);
588}
589
590template <class _Rp>
591class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {
592 typedef __assoc_sub_state base;
593
594protected:
595 _ALIGNAS_TYPE(_Rp) char __value_[sizeof(_Rp)];
596
597 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
598
599public:
600 template <class _Arg>
601 _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
602
603 template <class _Arg>
604 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
605
606 _LIBCPP_HIDE_FROM_ABI _Rp move();
607 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
608};
609
610template <class _Rp>
611void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {
612 if (this->__state_ & base::__constructed)
613 reinterpret_cast<_Rp*>(std::addressof(__value_))->~_Rp();
614 delete this;
615}
616
617template <class _Rp>
618template <class _Arg>
619void __assoc_state<_Rp>::set_value(_Arg&& __arg) {
620 unique_lock<mutex> __lk(this->__mut_);
621 if (this->__has_value())
622 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
623 ::new ((void*)std::addressof(__value_)) _Rp(std::forward<_Arg>(__arg));
624 this->__state_ |= base::__constructed | base::ready;
625 __cv_.notify_all();
626}
627
628template <class _Rp>
629template <class _Arg>
630void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {
631 unique_lock<mutex> __lk(this->__mut_);
632 if (this->__has_value())
633 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
634 ::new ((void*)std::addressof(__value_)) _Rp(std::forward<_Arg>(__arg));
635 this->__state_ |= base::__constructed;
636 __thread_local_data()->__make_ready_at_thread_exit(this);
637}
638
639template <class _Rp>
640_Rp __assoc_state<_Rp>::move() {
641 unique_lock<mutex> __lk(this->__mut_);
642 this->__sub_wait(__lk);
643 if (this->__exception_ != nullptr)
644 std::rethrow_exception(this->__exception_);
645 return std::move(*reinterpret_cast<_Rp*>(std::addressof(__value_)));
646}
647
648template <class _Rp>
649_Rp& __assoc_state<_Rp>::copy() {
650 unique_lock<mutex> __lk(this->__mut_);
651 this->__sub_wait(__lk);
652 if (this->__exception_ != nullptr)
653 std::rethrow_exception(this->__exception_);
654 return *reinterpret_cast<_Rp*>(std::addressof(__value_));
655}
656
657template <class _Rp>
658class __assoc_state<_Rp&> : public __assoc_sub_state {
659 typedef __assoc_sub_state base;
660 typedef _Rp* _Up;
661
662protected:
663 _Up __value_;
664
665 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
666
667public:
668 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
669 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
670
671 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
672};
673
674template <class _Rp>
675void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {
676 delete this;
677}
678
679template <class _Rp>
680void __assoc_state<_Rp&>::set_value(_Rp& __arg) {
681 unique_lock<mutex> __lk(this->__mut_);
682 if (this->__has_value())
683 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
684 __value_ = std::addressof(__arg);
685 this->__state_ |= base::__constructed | base::ready;
686 __cv_.notify_all();
687}
688
689template <class _Rp>
690void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {
691 unique_lock<mutex> __lk(this->__mut_);
692 if (this->__has_value())
693 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
694 __value_ = std::addressof(__arg);
695 this->__state_ |= base::__constructed;
696 __thread_local_data()->__make_ready_at_thread_exit(this);
697}
698
699template <class _Rp>
700_Rp& __assoc_state<_Rp&>::copy() {
701 unique_lock<mutex> __lk(this->__mut_);
702 this->__sub_wait(__lk);
703 if (this->__exception_ != nullptr)
704 std::rethrow_exception(this->__exception_);
705 return *__value_;
706}
707
708template <class _Rp, class _Alloc>
709class __assoc_state_alloc : public __assoc_state<_Rp> {
710 typedef __assoc_state<_Rp> base;
711 _Alloc __alloc_;
712
713 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
714
715public:
716 _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
717};
718
719template <class _Rp, class _Alloc>
720void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {
721 if (this->__state_ & base::__constructed)
722 reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();
723 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
724 typedef allocator_traits<_Al> _ATraits;
725 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
726 _Al __a(__alloc_);
727 this->~__assoc_state_alloc();
728 __a.deallocate(_PTraits::pointer_to(*this), 1);
729}
730
731template <class _Rp, class _Alloc>
732class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {
733 typedef __assoc_state<_Rp&> base;
734 _Alloc __alloc_;
735
736 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
737
738public:
739 _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
740};
741
742template <class _Rp, class _Alloc>
743void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {
744 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
745 typedef allocator_traits<_Al> _ATraits;
746 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
747 _Al __a(__alloc_);
748 this->~__assoc_state_alloc();
749 __a.deallocate(_PTraits::pointer_to(*this), 1);
750}
751
752template <class _Alloc>
753class __assoc_sub_state_alloc : public __assoc_sub_state {
754 typedef __assoc_sub_state base;
755 _Alloc __alloc_;
756
757 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
758
759public:
760 _LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
761};
762
763template <class _Alloc>
764void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {
765 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
766 typedef allocator_traits<_Al> _ATraits;
767 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
768 _Al __a(__alloc_);
769 this->~__assoc_sub_state_alloc();
770 __a.deallocate(_PTraits::pointer_to(*this), 1);
771}
772
773template <class _Rp, class _Fp>
774class __deferred_assoc_state : public __assoc_state<_Rp> {
775 typedef __assoc_state<_Rp> base;
776
777 _Fp __func_;
778
779public:
780 _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
781
782 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
783};
784
785template <class _Rp, class _Fp>
786inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
787 this->__set_deferred();
788}
789
790template <class _Rp, class _Fp>
791void __deferred_assoc_state<_Rp, _Fp>::__execute() {
792# if _LIBCPP_HAS_EXCEPTIONS
793 try {
794# endif // _LIBCPP_HAS_EXCEPTIONS
795 this->set_value(__func_());
796# if _LIBCPP_HAS_EXCEPTIONS
797 } catch (...) {
798 this->set_exception(current_exception());
799 }
800# endif // _LIBCPP_HAS_EXCEPTIONS
801}
802
803template <class _Fp>
804class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {
805 typedef __assoc_sub_state base;
806
807 _Fp __func_;
808
809public:
810 _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
811
812 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
813};
814
815template <class _Fp>
816inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
817 this->__set_deferred();
818}
819
820template <class _Fp>
821void __deferred_assoc_state<void, _Fp>::__execute() {
822# if _LIBCPP_HAS_EXCEPTIONS
823 try {
824# endif // _LIBCPP_HAS_EXCEPTIONS
825 __func_();
826 this->set_value();
827# if _LIBCPP_HAS_EXCEPTIONS
828 } catch (...) {
829 this->set_exception(current_exception());
830 }
831# endif // _LIBCPP_HAS_EXCEPTIONS
832}
833
834template <class _Rp, class _Fp>
835class __async_assoc_state : public __assoc_state<_Rp> {
836 typedef __assoc_state<_Rp> base;
837
838 _Fp __func_;
839
840 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
841
842public:
843 _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
844
845 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
846};
847
848template <class _Rp, class _Fp>
849inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
850
851template <class _Rp, class _Fp>
852void __async_assoc_state<_Rp, _Fp>::__execute() {
853# if _LIBCPP_HAS_EXCEPTIONS
854 try {
855# endif // _LIBCPP_HAS_EXCEPTIONS
856 this->set_value(__func_());
857# if _LIBCPP_HAS_EXCEPTIONS
858 } catch (...) {
859 this->set_exception(current_exception());
860 }
861# endif // _LIBCPP_HAS_EXCEPTIONS
862}
863
864template <class _Rp, class _Fp>
865void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {
866 this->wait();
867 base::__on_zero_shared();
868}
869
870template <class _Fp>
871class __async_assoc_state<void, _Fp> : public __assoc_sub_state {
872 typedef __assoc_sub_state base;
873
874 _Fp __func_;
875
876 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
877
878public:
879 _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
880
881 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
882};
883
884template <class _Fp>
885inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
886
887template <class _Fp>
888void __async_assoc_state<void, _Fp>::__execute() {
889# if _LIBCPP_HAS_EXCEPTIONS
890 try {
891# endif // _LIBCPP_HAS_EXCEPTIONS
892 __func_();
893 this->set_value();
894# if _LIBCPP_HAS_EXCEPTIONS
895 } catch (...) {
896 this->set_exception(current_exception());
897 }
898# endif // _LIBCPP_HAS_EXCEPTIONS
899}
900
901template <class _Fp>
902void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {
903 this->wait();
904 base::__on_zero_shared();
905}
906
907template <class _Rp>
908class promise;
909template <class _Rp>
910class shared_future;
911
912// future
913
914template <class _Rp>
915class future;
916
917template <class _Rp, class _Fp>
918_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);
919
920template <class _Rp, class _Fp>
921_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);
922
923template <class _Rp>
924class future {
925 __assoc_state<_Rp>* __state_;
926
927 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
928
929 template <class>
930 friend class promise;
931 template <class>
932 friend class shared_future;
933
934 template <class _R1, class _Fp>
935 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
936 template <class _R1, class _Fp>
937 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
938
939public:
940 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
941 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
942 future(const future&) = delete;
943 future& operator=(const future&) = delete;
944 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
945 future(std::move(__rhs)).swap(rhs&: *this);
946 return *this;
947 }
948
949 _LIBCPP_HIDE_FROM_ABI ~future();
950 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;
951
952 // retrieving the value
953 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _Rp get();
954
955 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
956
957 // functions to check state
958 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
959
960 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
961 template <class _Rep, class _Period>
962 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
963 return __state_->wait_for(__rel_time);
964 }
965 template <class _Clock, class _Duration>
966 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
967 return __state_->wait_until(__abs_time);
968 }
969};
970
971template <class _Rp>
972future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {
973 __state_->__attach_future();
974}
975
976struct __release_shared_count {
977 _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }
978};
979
980template <class _Rp>
981future<_Rp>::~future() {
982 if (__state_)
983 __state_->__release_shared();
984}
985
986template <class _Rp>
987_Rp future<_Rp>::get() {
988 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
989 __assoc_state<_Rp>* __s = __state_;
990 __state_ = nullptr;
991 return __s->move();
992}
993
994template <class _Rp>
995class future<_Rp&> {
996 __assoc_state<_Rp&>* __state_;
997
998 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
999
1000 template <class>
1001 friend class promise;
1002 template <class>
1003 friend class shared_future;
1004
1005 template <class _R1, class _Fp>
1006 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1007 template <class _R1, class _Fp>
1008 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1009
1010public:
1011 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1012 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1013 future(const future&) = delete;
1014 future& operator=(const future&) = delete;
1015 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1016 future(std::move(__rhs)).swap(rhs&: *this);
1017 return *this;
1018 }
1019
1020 _LIBCPP_HIDE_FROM_ABI ~future();
1021 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;
1022
1023 // retrieving the value
1024 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _Rp& get();
1025
1026 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1027
1028 // functions to check state
1029 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1030
1031 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1032 template <class _Rep, class _Period>
1033 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1034 return __state_->wait_for(__rel_time);
1035 }
1036 template <class _Clock, class _Duration>
1037 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1038 return __state_->wait_until(__abs_time);
1039 }
1040};
1041
1042template <class _Rp>
1043future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {
1044 __state_->__attach_future();
1045}
1046
1047template <class _Rp>
1048future<_Rp&>::~future() {
1049 if (__state_)
1050 __state_->__release_shared();
1051}
1052
1053template <class _Rp>
1054_Rp& future<_Rp&>::get() {
1055 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1056 __assoc_state<_Rp&>* __s = __state_;
1057 __state_ = nullptr;
1058 return __s->copy();
1059}
1060
1061template <>
1062class _LIBCPP_EXPORTED_FROM_ABI future<void> {
1063 __assoc_sub_state* __state_;
1064
1065 explicit future(__assoc_sub_state* __state);
1066
1067 template <class>
1068 friend class promise;
1069 template <class>
1070 friend class shared_future;
1071
1072 template <class _R1, class _Fp>
1073 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1074 template <class _R1, class _Fp>
1075 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1076
1077public:
1078 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1079 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1080 future(const future&) = delete;
1081 future& operator=(const future&) = delete;
1082 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1083 future(std::move(__rhs)).swap(rhs&: *this);
1084 return *this;
1085 }
1086
1087 ~future();
1088 _LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;
1089
1090 // retrieving the value
1091 void get();
1092
1093 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(x&: __state_, y&: __rhs.__state_); }
1094
1095 // functions to check state
1096 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1097
1098 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1099 template <class _Rep, class _Period>
1100 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1101 return __state_->wait_for(__rel_time);
1102 }
1103 template <class _Clock, class _Duration>
1104 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1105 return __state_->wait_until(__abs_time);
1106 }
1107};
1108
1109template <class _Rp>
1110inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {
1111 __x.swap(__y);
1112}
1113
1114// promise<R>
1115
1116template <class _Callable>
1117class packaged_task;
1118
1119template <class _Rp>
1120class promise {
1121 __assoc_state<_Rp>* __state_;
1122
1123 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1124
1125 template <class>
1126 friend class packaged_task;
1127
1128public:
1129 _LIBCPP_HIDE_FROM_ABI promise();
1130 template <class _Alloc>
1131 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1132 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1133 promise(const promise& __rhs) = delete;
1134 _LIBCPP_HIDE_FROM_ABI ~promise();
1135
1136 // assignment
1137 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1138 promise(std::move(__rhs)).swap(rhs&: *this);
1139 return *this;
1140 }
1141 promise& operator=(const promise& __rhs) = delete;
1142
1143 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1144
1145 // retrieving the result
1146 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1147
1148 // setting the result
1149 _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1150 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1151 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1152
1153 // setting the result with deferred notification
1154 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1155 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1156 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1157};
1158
1159template <class _Rp>
1160promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}
1161
1162template <class _Rp>
1163template <class _Alloc>
1164promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {
1165 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1166 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1167 typedef __allocator_destructor<_A2> _D2;
1168 _A2 __a(__a0);
1169 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1170 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1171 __state_ = std::addressof(*__hold.release());
1172}
1173
1174template <class _Rp>
1175promise<_Rp>::~promise() {
1176 if (__state_) {
1177 if (!__state_->__has_value() && __state_->use_count() > 1)
1178 __state_->set_exception(make_exception_ptr(e: future_error(make_error_code(e: future_errc::broken_promise))));
1179 __state_->__release_shared();
1180 }
1181}
1182
1183template <class _Rp>
1184future<_Rp> promise<_Rp>::get_future() {
1185 if (__state_ == nullptr)
1186 std::__throw_future_error(ev: future_errc::no_state);
1187 return future<_Rp>(__state_);
1188}
1189
1190template <class _Rp>
1191void promise<_Rp>::set_value(const _Rp& __r) {
1192 if (__state_ == nullptr)
1193 std::__throw_future_error(ev: future_errc::no_state);
1194 __state_->set_value(__r);
1195}
1196
1197template <class _Rp>
1198void promise<_Rp>::set_value(_Rp&& __r) {
1199 if (__state_ == nullptr)
1200 std::__throw_future_error(ev: future_errc::no_state);
1201 __state_->set_value(std::move(__r));
1202}
1203
1204template <class _Rp>
1205void promise<_Rp>::set_exception(exception_ptr __p) {
1206 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1207 if (__state_ == nullptr)
1208 std::__throw_future_error(ev: future_errc::no_state);
1209 __state_->set_exception(__p);
1210}
1211
1212template <class _Rp>
1213void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {
1214 if (__state_ == nullptr)
1215 std::__throw_future_error(ev: future_errc::no_state);
1216 __state_->set_value_at_thread_exit(__r);
1217}
1218
1219template <class _Rp>
1220void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {
1221 if (__state_ == nullptr)
1222 std::__throw_future_error(ev: future_errc::no_state);
1223 __state_->set_value_at_thread_exit(std::move(__r));
1224}
1225
1226template <class _Rp>
1227void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {
1228 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1229 if (__state_ == nullptr)
1230 std::__throw_future_error(ev: future_errc::no_state);
1231 __state_->set_exception_at_thread_exit(__p);
1232}
1233
1234// promise<R&>
1235
1236template <class _Rp>
1237class promise<_Rp&> {
1238 __assoc_state<_Rp&>* __state_;
1239
1240 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1241
1242 template <class>
1243 friend class packaged_task;
1244
1245public:
1246 _LIBCPP_HIDE_FROM_ABI promise();
1247 template <class _Allocator>
1248 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1249 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1250 promise(const promise& __rhs) = delete;
1251 _LIBCPP_HIDE_FROM_ABI ~promise();
1252
1253 // assignment
1254 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1255 promise(std::move(__rhs)).swap(rhs&: *this);
1256 return *this;
1257 }
1258 promise& operator=(const promise& __rhs) = delete;
1259
1260 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1261
1262 // retrieving the result
1263 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1264
1265 // setting the result
1266 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1267 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1268
1269 // setting the result with deferred notification
1270 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1271 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1272};
1273
1274template <class _Rp>
1275promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}
1276
1277template <class _Rp>
1278template <class _Alloc>
1279promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {
1280 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1281 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1282 typedef __allocator_destructor<_A2> _D2;
1283 _A2 __a(__a0);
1284 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1285 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1286 __state_ = std::addressof(*__hold.release());
1287}
1288
1289template <class _Rp>
1290promise<_Rp&>::~promise() {
1291 if (__state_) {
1292 if (!__state_->__has_value() && __state_->use_count() > 1)
1293 __state_->set_exception(make_exception_ptr(e: future_error(make_error_code(e: future_errc::broken_promise))));
1294 __state_->__release_shared();
1295 }
1296}
1297
1298template <class _Rp>
1299future<_Rp&> promise<_Rp&>::get_future() {
1300 if (__state_ == nullptr)
1301 std::__throw_future_error(ev: future_errc::no_state);
1302 return future<_Rp&>(__state_);
1303}
1304
1305template <class _Rp>
1306void promise<_Rp&>::set_value(_Rp& __r) {
1307 if (__state_ == nullptr)
1308 std::__throw_future_error(ev: future_errc::no_state);
1309 __state_->set_value(__r);
1310}
1311
1312template <class _Rp>
1313void promise<_Rp&>::set_exception(exception_ptr __p) {
1314 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1315 if (__state_ == nullptr)
1316 std::__throw_future_error(ev: future_errc::no_state);
1317 __state_->set_exception(__p);
1318}
1319
1320template <class _Rp>
1321void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {
1322 if (__state_ == nullptr)
1323 std::__throw_future_error(ev: future_errc::no_state);
1324 __state_->set_value_at_thread_exit(__r);
1325}
1326
1327template <class _Rp>
1328void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {
1329 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1330 if (__state_ == nullptr)
1331 std::__throw_future_error(ev: future_errc::no_state);
1332 __state_->set_exception_at_thread_exit(__p);
1333}
1334
1335// promise<void>
1336
1337template <>
1338class _LIBCPP_EXPORTED_FROM_ABI promise<void> {
1339 __assoc_sub_state* __state_;
1340
1341 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1342
1343 template <class>
1344 friend class packaged_task;
1345
1346public:
1347 promise();
1348 template <class _Alloc>
1349 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a0) {
1350 typedef __assoc_sub_state_alloc<_Alloc> _State;
1351 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1352 typedef __allocator_destructor<_A2> _D2;
1353 _A2 __a(__a0);
1354 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1355 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1356 __state_ = std::addressof(*__hold.release());
1357 }
1358
1359 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1360 promise(const promise& __rhs) = delete;
1361 ~promise();
1362
1363 // assignment
1364 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1365 promise(std::move(__rhs)).swap(rhs&: *this);
1366 return *this;
1367 }
1368 promise& operator=(const promise& __rhs) = delete;
1369
1370 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(x&: __state_, y&: __rhs.__state_); }
1371
1372 // retrieving the result
1373 [[__nodiscard__]] future<void> get_future();
1374
1375 // setting the result
1376 void set_value();
1377 void set_exception(exception_ptr __p);
1378
1379 // setting the result with deferred notification
1380 void set_value_at_thread_exit();
1381 void set_exception_at_thread_exit(exception_ptr __p);
1382};
1383
1384template <class _Rp>
1385inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {
1386 __x.swap(__y);
1387}
1388
1389template <class _Rp, class _Alloc>
1390struct uses_allocator<promise<_Rp>, _Alloc> : public true_type {};
1391
1392// packaged_task
1393
1394template <class _Fp>
1395class __packaged_task_base;
1396
1397template <class _Rp, class... _ArgTypes>
1398class __packaged_task_base<_Rp(_ArgTypes...)> {
1399public:
1400 _LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}
1401 __packaged_task_base(const __packaged_task_base&) = delete;
1402 __packaged_task_base& operator=(const __packaged_task_base&) = delete;
1403 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1404 virtual ~__packaged_task_base() {}
1405 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1406 virtual void destroy() = 0;
1407 virtual void destroy_deallocate() = 0;
1408 virtual _Rp operator()(_ArgTypes&&...) = 0;
1409};
1410
1411template <class _FD, class _Alloc, class _FB>
1412class __packaged_task_func;
1413
1414template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1415class _LIBCPP_HIDE_STRUCT_FROM_ABI
1416 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
1417 _LIBCPP_NO_UNIQUE_ADDRESS _Fp __func_;
1418 _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;
1419
1420public:
1421 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __func_(__f) {}
1422 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __func_(std::move(__f)) {}
1423 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __func_(__f), __alloc_(__a) {}
1424 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __func_(std::move(__f)), __alloc_(__a) {}
1425 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1426 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1427 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1428 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);
1429};
1430
1431template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1432void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1433 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {
1434 ::new ((void*)__p) __packaged_task_func(std::move(__func_), std::move(__alloc_));
1435}
1436
1437template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1438void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
1439 __alloc_.~_Alloc();
1440 __func_.~_Fp();
1441}
1442
1443template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1444void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {
1445 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1446 typedef allocator_traits<_Ap> _ATraits;
1447 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1448 _Ap __a(__alloc_);
1449 __alloc_.~_Alloc();
1450 __func_.~_Fp();
1451 __a.deallocate(_PTraits::pointer_to(*this), 1);
1452}
1453
1454template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1455_Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
1456 return std::__invoke(__func_, std::forward<_ArgTypes>(__arg)...);
1457}
1458
1459template <class _Callable>
1460class __packaged_task_function;
1461
1462template <class _Rp, class... _ArgTypes>
1463class __packaged_task_function<_Rp(_ArgTypes...)> {
1464 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1465
1466 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }
1467
1468 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1469 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1470 _LIBCPP_SUPPRESS_DEPRECATED_POP
1471 __base* __f_;
1472
1473public:
1474 typedef _Rp result_type;
1475
1476 // construct/copy/destroy:
1477 _LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1478 template <class _Fp>
1479 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1480 template <class _Fp, class _Alloc>
1481 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1482
1483 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1484 _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1485
1486 __packaged_task_function(const __packaged_task_function&) = delete;
1487 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1488
1489 _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1490
1491 _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1492
1493 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1494};
1495
1496template <class _Rp, class... _ArgTypes>
1497__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {
1498 if (__f.__f_ == nullptr)
1499 __f_ = nullptr;
1500 else if (__f.__f_ == __f.__get_buf()) {
1501 __f.__f_->__move_to(__get_buf());
1502 __f_ = (__base*)&__buf_;
1503 } else {
1504 __f_ = __f.__f_;
1505 __f.__f_ = nullptr;
1506 }
1507}
1508
1509template <class _Rp, class... _ArgTypes>
1510template <class _Fp>
1511__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {
1512 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1513 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1514 if (sizeof(_FF) <= sizeof(__buf_)) {
1515 ::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));
1516 __f_ = (__base*)&__buf_;
1517 } else {
1518 typedef allocator<_FF> _Ap;
1519 _Ap __a;
1520 typedef __allocator_destructor<_Ap> _Dp;
1521 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1522 ::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));
1523 __f_ = __hold.release();
1524 }
1525}
1526
1527template <class _Rp, class... _ArgTypes>
1528template <class _Fp, class _Alloc>
1529__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1530 : __f_(nullptr) {
1531 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1532 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1533 if (sizeof(_FF) <= sizeof(__buf_)) {
1534 __f_ = (__base*)&__buf_;
1535 ::new ((void*)__f_) _FF(std::forward<_Fp>(__f));
1536 } else {
1537 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1538 _Ap __a(__a0);
1539 typedef __allocator_destructor<_Ap> _Dp;
1540 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1541 ::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));
1542 __f_ = std::addressof(*__hold.release());
1543 }
1544}
1545
1546template <class _Rp, class... _ArgTypes>
1547__packaged_task_function<_Rp(_ArgTypes...)>&
1548__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {
1549 if (__f_ == __get_buf())
1550 __f_->destroy();
1551 else if (__f_)
1552 __f_->destroy_deallocate();
1553 __f_ = nullptr;
1554 if (__f.__f_ == nullptr)
1555 __f_ = nullptr;
1556 else if (__f.__f_ == __f.__get_buf()) {
1557 __f.__f_->__move_to(__get_buf());
1558 __f_ = __get_buf();
1559 } else {
1560 __f_ = __f.__f_;
1561 __f.__f_ = nullptr;
1562 }
1563 return *this;
1564}
1565
1566template <class _Rp, class... _ArgTypes>
1567__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {
1568 if (__f_ == __get_buf())
1569 __f_->destroy();
1570 else if (__f_)
1571 __f_->destroy_deallocate();
1572}
1573
1574template <class _Rp, class... _ArgTypes>
1575_LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {
1576 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {
1577 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1578 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1579 _LIBCPP_SUPPRESS_DEPRECATED_POP
1580 __base* __t = (__base*)&__tempbuf;
1581 __f_->__move_to(__t);
1582 __f_->destroy();
1583 __f_ = nullptr;
1584 __f.__f_->__move_to((__base*)&__buf_);
1585 __f.__f_->destroy();
1586 __f.__f_ = nullptr;
1587 __f_ = (__base*)&__buf_;
1588 __t->__move_to((__base*)&__f.__buf_);
1589 __t->destroy();
1590 __f.__f_ = (__base*)&__f.__buf_;
1591 } else if (__f_ == (__base*)&__buf_) {
1592 __f_->__move_to((__base*)&__f.__buf_);
1593 __f_->destroy();
1594 __f_ = __f.__f_;
1595 __f.__f_ = (__base*)&__f.__buf_;
1596 } else if (__f.__f_ == (__base*)&__f.__buf_) {
1597 __f.__f_->__move_to((__base*)&__buf_);
1598 __f.__f_->destroy();
1599 __f.__f_ = __f_;
1600 __f_ = (__base*)&__buf_;
1601 } else
1602 std::swap(__f_, __f.__f_);
1603}
1604
1605template <class _Rp, class... _ArgTypes>
1606inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
1607 return (*__f_)(std::forward<_ArgTypes>(__arg)...);
1608}
1609
1610template <class _Rp, class... _ArgTypes>
1611class packaged_task<_Rp(_ArgTypes...)> {
1612private:
1613 __packaged_task_function<_Rp(_ArgTypes...)> __f_;
1614 promise<_Rp> __p_;
1615
1616public:
1617 // construction and destruction
1618 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1619
1620 template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1621 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1622
1623# if _LIBCPP_STD_VER <= 14
1624 template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1625 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1626 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1627# endif
1628 // ~packaged_task() = default;
1629
1630 // no copy
1631 packaged_task(const packaged_task&) = delete;
1632 packaged_task& operator=(const packaged_task&) = delete;
1633
1634 // move support
1635 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1636 : __f_(std::move(__other.__f_)),
1637 __p_(std::move(__other.__p_)) {}
1638 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1639 __f_ = std::move(__other.__f_);
1640 __p_ = std::move(__other.__p_);
1641 return *this;
1642 }
1643 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1644 __f_.swap(__other.__f_);
1645 __p_.swap(__other.__p_);
1646 }
1647
1648 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1649
1650 // result retrieval
1651 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future() { return __p_.get_future(); }
1652
1653 // execution
1654 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1655 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1656
1657 _LIBCPP_HIDE_FROM_ABI void reset();
1658};
1659
1660template <class _Rp, class... _ArgTypes>
1661void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1662 if (__p_.__state_ == nullptr)
1663 std::__throw_future_error(ev: future_errc::no_state);
1664 if (__p_.__state_->__has_value())
1665 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1666# if _LIBCPP_HAS_EXCEPTIONS
1667 try {
1668# endif // _LIBCPP_HAS_EXCEPTIONS
1669 __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
1670# if _LIBCPP_HAS_EXCEPTIONS
1671 } catch (...) {
1672 __p_.set_exception(current_exception());
1673 }
1674# endif // _LIBCPP_HAS_EXCEPTIONS
1675}
1676
1677template <class _Rp, class... _ArgTypes>
1678void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1679 if (__p_.__state_ == nullptr)
1680 std::__throw_future_error(ev: future_errc::no_state);
1681 if (__p_.__state_->__has_value())
1682 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1683# if _LIBCPP_HAS_EXCEPTIONS
1684 try {
1685# endif // _LIBCPP_HAS_EXCEPTIONS
1686 __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
1687# if _LIBCPP_HAS_EXCEPTIONS
1688 } catch (...) {
1689 __p_.set_exception_at_thread_exit(current_exception());
1690 }
1691# endif // _LIBCPP_HAS_EXCEPTIONS
1692}
1693
1694template <class _Rp, class... _ArgTypes>
1695void packaged_task<_Rp(_ArgTypes...)>::reset() {
1696 if (!valid())
1697 std::__throw_future_error(ev: future_errc::no_state);
1698 __p_ = promise<_Rp>();
1699}
1700
1701template <class... _ArgTypes>
1702class packaged_task<void(_ArgTypes...)> {
1703private:
1704 __packaged_task_function<void(_ArgTypes...)> __f_;
1705 promise<void> __p_;
1706
1707public:
1708 // construction and destruction
1709 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1710 template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1711 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1712# if _LIBCPP_STD_VER <= 14
1713 template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1714 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1715 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1716# endif
1717 // ~packaged_task() = default;
1718
1719 // no copy
1720 packaged_task(const packaged_task&) = delete;
1721 packaged_task& operator=(const packaged_task&) = delete;
1722
1723 // move support
1724 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1725 : __f_(std::move(__other.__f_)),
1726 __p_(std::move(__other.__p_)) {}
1727 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1728 __f_ = std::move(__other.__f_);
1729 __p_ = std::move(__other.__p_);
1730 return *this;
1731 }
1732 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1733 __f_.swap(__other.__f_);
1734 __p_.swap(rhs&: __other.__p_);
1735 }
1736
1737 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1738
1739 // result retrieval
1740 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<void> get_future() { return __p_.get_future(); }
1741
1742 // execution
1743 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1744 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1745
1746 _LIBCPP_HIDE_FROM_ABI void reset();
1747};
1748
1749# if _LIBCPP_STD_VER >= 17
1750
1751template <class _Rp, class... _Args>
1752packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
1753
1754template <class _Fp, class _Stripped = __strip_signature_t<decltype(&_Fp::operator())>>
1755packaged_task(_Fp) -> packaged_task<_Stripped>;
1756
1757# endif
1758
1759template <class... _ArgTypes>
1760void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1761 if (__p_.__state_ == nullptr)
1762 std::__throw_future_error(ev: future_errc::no_state);
1763 if (__p_.__state_->__has_value())
1764 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1765# if _LIBCPP_HAS_EXCEPTIONS
1766 try {
1767# endif // _LIBCPP_HAS_EXCEPTIONS
1768 __f_(std::forward<_ArgTypes>(__args)...);
1769 __p_.set_value();
1770# if _LIBCPP_HAS_EXCEPTIONS
1771 } catch (...) {
1772 __p_.set_exception(current_exception());
1773 }
1774# endif // _LIBCPP_HAS_EXCEPTIONS
1775}
1776
1777template <class... _ArgTypes>
1778void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1779 if (__p_.__state_ == nullptr)
1780 std::__throw_future_error(ev: future_errc::no_state);
1781 if (__p_.__state_->__has_value())
1782 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1783# if _LIBCPP_HAS_EXCEPTIONS
1784 try {
1785# endif // _LIBCPP_HAS_EXCEPTIONS
1786 __f_(std::forward<_ArgTypes>(__args)...);
1787 __p_.set_value_at_thread_exit();
1788# if _LIBCPP_HAS_EXCEPTIONS
1789 } catch (...) {
1790 __p_.set_exception_at_thread_exit(current_exception());
1791 }
1792# endif // _LIBCPP_HAS_EXCEPTIONS
1793}
1794
1795template <class... _ArgTypes>
1796void packaged_task<void(_ArgTypes...)>::reset() {
1797 if (!valid())
1798 std::__throw_future_error(ev: future_errc::no_state);
1799 __p_ = promise<void>();
1800}
1801
1802template <class _Rp, class... _ArgTypes>
1803inline _LIBCPP_HIDE_FROM_ABI void
1804swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1805 __x.swap(__y);
1806}
1807
1808# if _LIBCPP_STD_VER <= 14
1809template <class _Callable, class _Alloc>
1810struct uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
1811# endif
1812
1813template <class _Rp, class _Fp>
1814_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
1815 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1816 new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1817 return future<_Rp>(__h.get());
1818}
1819
1820template <class _Rp, class _Fp>
1821_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
1822 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1823 new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1824 auto __guard = std::__make_exception_guard([&] { __h->__make_ready(); });
1825 std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
1826 __guard.__complete();
1827 return future<_Rp>(__h.get());
1828}
1829
1830# ifndef _LIBCPP_CXX03_LANG
1831
1832template <class _Fp, class... _Args>
1833class _LIBCPP_HIDDEN __async_func {
1834 tuple<_Fp, _Args...> __f_;
1835
1836public:
1837 using _Rp _LIBCPP_NODEBUG = __invoke_result_t<_Fp, _Args...>;
1838
1839 template <class _Gp, class... _BArgs>
1840 _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Gp&& __g, _BArgs&&... __bargs)
1841 : __f_(std::forward<_Gp>(__g), std::forward<_BArgs>(__bargs)...) {}
1842
1843 _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
1844
1845 _LIBCPP_HIDE_FROM_ABI _Rp operator()() {
1846 return [&]<size_t... _Indices>(__index_sequence<_Indices...>) -> _Rp {
1847 return std::__invoke(std::move(std::get<_Indices>(__f_))...);
1848 }(__index_sequence_for<_Fp, _Args...>{});
1849 }
1850};
1851
1852inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
1853 return (int(__policy) & int(__value)) != 0;
1854}
1855
1856template <class _Fp, class... _Args>
1857[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<__invoke_result_t<__decay_t<_Fp>, __decay_t<_Args>...> >
1858async(launch __policy, _Fp&& __f, _Args&&... __args) {
1859 static_assert(is_constructible<__decay_t<_Fp>, _Fp>::value, "");
1860 static_assert(_And<is_constructible<__decay_t<_Args>, _Args>...>::value, "");
1861 static_assert(__is_invocable_v<__decay_t<_Fp>, __decay_t<_Args>...>, "");
1862
1863 typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
1864 typedef typename _BF::_Rp _Rp;
1865
1866# if _LIBCPP_HAS_EXCEPTIONS
1867 try {
1868# endif
1869 if (__does_policy_contain(__policy, value: launch::async))
1870 return std::__make_async_assoc_state<_Rp>(_BF(std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
1871# if _LIBCPP_HAS_EXCEPTIONS
1872 } catch (...) {
1873 if (__policy == launch::async)
1874 throw;
1875 }
1876# endif
1877
1878 if (__does_policy_contain(__policy, value: launch::deferred))
1879 return std::__make_deferred_assoc_state<_Rp>(_BF(std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
1880 return future<_Rp>{};
1881}
1882
1883template <class _Fp, class... _Args>
1884[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI future<__invoke_result_t<__decay_t<_Fp>, __decay_t<_Args>...> >
1885async(_Fp&& __f, _Args&&... __args) {
1886 return std::async(launch::async | launch::deferred, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);
1887}
1888
1889# endif // C++03
1890
1891// shared_future
1892
1893template <class _Rp>
1894class shared_future {
1895 __assoc_state<_Rp>* __state_;
1896
1897public:
1898 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1899 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1900 if (__state_)
1901 __state_->__add_shared();
1902 }
1903 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1904 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1905 __rhs.__state_ = nullptr;
1906 }
1907 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1908 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
1909 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1910 shared_future(std::move(__rhs)).swap(rhs&: *this);
1911 return *this;
1912 }
1913
1914 // retrieving the value
1915 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }
1916
1917 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1918
1919 // functions to check state
1920 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1921
1922 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1923 template <class _Rep, class _Period>
1924 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1925 return __state_->wait_for(__rel_time);
1926 }
1927 template <class _Clock, class _Duration>
1928 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1929 return __state_->wait_until(__abs_time);
1930 }
1931};
1932
1933template <class _Rp>
1934shared_future<_Rp>::~shared_future() {
1935 if (__state_)
1936 __state_->__release_shared();
1937}
1938
1939template <class _Rp>
1940shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {
1941 if (__rhs.__state_)
1942 __rhs.__state_->__add_shared();
1943 if (__state_)
1944 __state_->__release_shared();
1945 __state_ = __rhs.__state_;
1946 return *this;
1947}
1948
1949template <class _Rp>
1950class shared_future<_Rp&> {
1951 __assoc_state<_Rp&>* __state_;
1952
1953public:
1954 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1955 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
1956 if (__state_)
1957 __state_->__add_shared();
1958 }
1959 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1960 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1961 __rhs.__state_ = nullptr;
1962 }
1963 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1964 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
1965 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1966 shared_future(std::move(__rhs)).swap(rhs&: *this);
1967 return *this;
1968 }
1969
1970 // retrieving the value
1971 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }
1972
1973 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1974
1975 // functions to check state
1976 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1977
1978 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1979 template <class _Rep, class _Period>
1980 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1981 return __state_->wait_for(__rel_time);
1982 }
1983 template <class _Clock, class _Duration>
1984 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1985 return __state_->wait_until(__abs_time);
1986 }
1987};
1988
1989template <class _Rp>
1990shared_future<_Rp&>::~shared_future() {
1991 if (__state_)
1992 __state_->__release_shared();
1993}
1994
1995template <class _Rp>
1996shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {
1997 if (__rhs.__state_)
1998 __rhs.__state_->__add_shared();
1999 if (__state_)
2000 __state_->__release_shared();
2001 __state_ = __rhs.__state_;
2002 return *this;
2003}
2004
2005template <>
2006class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
2007 __assoc_sub_state* __state_;
2008
2009public:
2010 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
2011 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
2012 if (__state_)
2013 __state_->__add_shared();
2014 }
2015 _LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
2016 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
2017 __rhs.__state_ = nullptr;
2018 }
2019 ~shared_future();
2020 shared_future& operator=(const shared_future& __rhs);
2021 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
2022 shared_future(std::move(__rhs)).swap(rhs&: *this);
2023 return *this;
2024 }
2025
2026 // retrieving the value
2027 _LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }
2028
2029 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(x&: __state_, y&: __rhs.__state_); }
2030
2031 // functions to check state
2032 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
2033
2034 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
2035 template <class _Rep, class _Period>
2036 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
2037 return __state_->wait_for(__rel_time);
2038 }
2039 template <class _Clock, class _Duration>
2040 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
2041 return __state_->wait_until(__abs_time);
2042 }
2043};
2044
2045template <class _Rp>
2046inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
2047 __x.swap(__y);
2048}
2049
2050template <class _Rp>
2051inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
2052 return shared_future<_Rp>(std::move(*this));
2053}
2054
2055template <class _Rp>
2056inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
2057 return shared_future<_Rp&>(std::move(*this));
2058}
2059
2060inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
2061
2062_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
2063_LIBCPP_END_NAMESPACE_STD
2064
2065_LIBCPP_POP_MACROS
2066
2067# endif // _LIBCPP_HAS_THREADS
2068
2069# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 17
2070# include <chrono>
2071# endif
2072
2073# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 20
2074# include <atomic>
2075# include <cstdlib>
2076# include <exception>
2077# include <iosfwd>
2078# include <system_error>
2079# include <thread>
2080# endif
2081
2082# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 23
2083# include <sstream>
2084# endif
2085
2086#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
2087
2088#endif // _LIBCPP_FUTURE
2089