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/compressed_pair.h>
383# include <__memory/pointer_traits.h>
384# include <__memory/shared_count.h>
385# include <__memory/unique_ptr.h>
386# include <__memory/uses_allocator.h>
387# include <__mutex/lock_guard.h>
388# include <__mutex/mutex.h>
389# include <__mutex/unique_lock.h>
390# include <__system_error/error_category.h>
391# include <__system_error/error_code.h>
392# include <__system_error/error_condition.h>
393# include <__thread/thread.h>
394# include <__type_traits/add_reference.h>
395# include <__type_traits/aligned_storage.h>
396# include <__type_traits/conditional.h>
397# include <__type_traits/decay.h>
398# include <__type_traits/enable_if.h>
399# include <__type_traits/invoke.h>
400# include <__type_traits/is_constructible.h>
401# include <__type_traits/is_same.h>
402# include <__type_traits/remove_cvref.h>
403# include <__type_traits/remove_reference.h>
404# include <__type_traits/strip_signature.h>
405# include <__type_traits/underlying_type.h>
406# include <__utility/auto_cast.h>
407# include <__utility/exception_guard.h>
408# include <__utility/forward.h>
409# include <__utility/move.h>
410# include <__utility/swap.h>
411# include <stdexcept>
412# include <tuple>
413# include <version>
414
415# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
416# pragma GCC system_header
417# endif
418
419_LIBCPP_PUSH_MACROS
420# include <__undef_macros>
421
422_LIBCPP_BEGIN_NAMESPACE_STD
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(*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(*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(*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(*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 __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
1416 _LIBCPP_COMPRESSED_PAIR(_Fp, __func_, _Alloc, __alloc_);
1417
1418public:
1419 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __func_(__f) {}
1420 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __func_(std::move(__f)) {}
1421 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __func_(__f), __alloc_(__a) {}
1422 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __func_(std::move(__f)), __alloc_(__a) {}
1423 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1424 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1425 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1426 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);
1427};
1428
1429template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1430void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1431 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {
1432 ::new ((void*)__p) __packaged_task_func(std::move(__func_), std::move(__alloc_));
1433}
1434
1435template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1436void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
1437 __func_.~_Fp();
1438 __alloc_.~_Alloc();
1439}
1440
1441template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1442void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {
1443 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1444 typedef allocator_traits<_Ap> _ATraits;
1445 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1446 _Ap __a(__alloc_);
1447 __func_.~_Fp();
1448 __alloc_.~_Alloc();
1449 __a.deallocate(_PTraits::pointer_to(*this), 1);
1450}
1451
1452template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1453_Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
1454 return std::__invoke(__func_, std::forward<_ArgTypes>(__arg)...);
1455}
1456
1457template <class _Callable>
1458class __packaged_task_function;
1459
1460template <class _Rp, class... _ArgTypes>
1461class __packaged_task_function<_Rp(_ArgTypes...)> {
1462 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1463
1464 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }
1465
1466 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1467 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1468 _LIBCPP_SUPPRESS_DEPRECATED_POP
1469 __base* __f_;
1470
1471public:
1472 typedef _Rp result_type;
1473
1474 // construct/copy/destroy:
1475 _LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1476 template <class _Fp>
1477 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1478 template <class _Fp, class _Alloc>
1479 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1480
1481 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1482 _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1483
1484 __packaged_task_function(const __packaged_task_function&) = delete;
1485 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1486
1487 _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1488
1489 _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1490
1491 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1492};
1493
1494template <class _Rp, class... _ArgTypes>
1495__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {
1496 if (__f.__f_ == nullptr)
1497 __f_ = nullptr;
1498 else if (__f.__f_ == __f.__get_buf()) {
1499 __f.__f_->__move_to(__get_buf());
1500 __f_ = (__base*)&__buf_;
1501 } else {
1502 __f_ = __f.__f_;
1503 __f.__f_ = nullptr;
1504 }
1505}
1506
1507template <class _Rp, class... _ArgTypes>
1508template <class _Fp>
1509__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {
1510 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1511 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1512 if (sizeof(_FF) <= sizeof(__buf_)) {
1513 ::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));
1514 __f_ = (__base*)&__buf_;
1515 } else {
1516 typedef allocator<_FF> _Ap;
1517 _Ap __a;
1518 typedef __allocator_destructor<_Ap> _Dp;
1519 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1520 ::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));
1521 __f_ = __hold.release();
1522 }
1523}
1524
1525template <class _Rp, class... _ArgTypes>
1526template <class _Fp, class _Alloc>
1527__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1528 : __f_(nullptr) {
1529 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1530 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1531 if (sizeof(_FF) <= sizeof(__buf_)) {
1532 __f_ = (__base*)&__buf_;
1533 ::new ((void*)__f_) _FF(std::forward<_Fp>(__f));
1534 } else {
1535 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1536 _Ap __a(__a0);
1537 typedef __allocator_destructor<_Ap> _Dp;
1538 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1539 ::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));
1540 __f_ = std::addressof(*__hold.release());
1541 }
1542}
1543
1544template <class _Rp, class... _ArgTypes>
1545__packaged_task_function<_Rp(_ArgTypes...)>&
1546__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {
1547 if (__f_ == __get_buf())
1548 __f_->destroy();
1549 else if (__f_)
1550 __f_->destroy_deallocate();
1551 __f_ = nullptr;
1552 if (__f.__f_ == nullptr)
1553 __f_ = nullptr;
1554 else if (__f.__f_ == __f.__get_buf()) {
1555 __f.__f_->__move_to(__get_buf());
1556 __f_ = __get_buf();
1557 } else {
1558 __f_ = __f.__f_;
1559 __f.__f_ = nullptr;
1560 }
1561 return *this;
1562}
1563
1564template <class _Rp, class... _ArgTypes>
1565__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {
1566 if (__f_ == __get_buf())
1567 __f_->destroy();
1568 else if (__f_)
1569 __f_->destroy_deallocate();
1570}
1571
1572template <class _Rp, class... _ArgTypes>
1573_LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {
1574 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {
1575 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1576 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1577 _LIBCPP_SUPPRESS_DEPRECATED_POP
1578 __base* __t = (__base*)&__tempbuf;
1579 __f_->__move_to(__t);
1580 __f_->destroy();
1581 __f_ = nullptr;
1582 __f.__f_->__move_to((__base*)&__buf_);
1583 __f.__f_->destroy();
1584 __f.__f_ = nullptr;
1585 __f_ = (__base*)&__buf_;
1586 __t->__move_to((__base*)&__f.__buf_);
1587 __t->destroy();
1588 __f.__f_ = (__base*)&__f.__buf_;
1589 } else if (__f_ == (__base*)&__buf_) {
1590 __f_->__move_to((__base*)&__f.__buf_);
1591 __f_->destroy();
1592 __f_ = __f.__f_;
1593 __f.__f_ = (__base*)&__f.__buf_;
1594 } else if (__f.__f_ == (__base*)&__f.__buf_) {
1595 __f.__f_->__move_to((__base*)&__buf_);
1596 __f.__f_->destroy();
1597 __f.__f_ = __f_;
1598 __f_ = (__base*)&__buf_;
1599 } else
1600 std::swap(__f_, __f.__f_);
1601}
1602
1603template <class _Rp, class... _ArgTypes>
1604inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
1605 return (*__f_)(std::forward<_ArgTypes>(__arg)...);
1606}
1607
1608template <class _Rp, class... _ArgTypes>
1609class packaged_task<_Rp(_ArgTypes...)> {
1610private:
1611 __packaged_task_function<_Rp(_ArgTypes...)> __f_;
1612 promise<_Rp> __p_;
1613
1614public:
1615 // construction and destruction
1616 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1617
1618 template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1619 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1620
1621# if _LIBCPP_STD_VER <= 14
1622 template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1623 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1624 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1625# endif
1626 // ~packaged_task() = default;
1627
1628 // no copy
1629 packaged_task(const packaged_task&) = delete;
1630 packaged_task& operator=(const packaged_task&) = delete;
1631
1632 // move support
1633 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1634 : __f_(std::move(__other.__f_)),
1635 __p_(std::move(__other.__p_)) {}
1636 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1637 __f_ = std::move(__other.__f_);
1638 __p_ = std::move(__other.__p_);
1639 return *this;
1640 }
1641 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1642 __f_.swap(__other.__f_);
1643 __p_.swap(__other.__p_);
1644 }
1645
1646 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1647
1648 // result retrieval
1649 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future() { return __p_.get_future(); }
1650
1651 // execution
1652 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1653 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1654
1655 _LIBCPP_HIDE_FROM_ABI void reset();
1656};
1657
1658template <class _Rp, class... _ArgTypes>
1659void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1660 if (__p_.__state_ == nullptr)
1661 std::__throw_future_error(ev: future_errc::no_state);
1662 if (__p_.__state_->__has_value())
1663 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1664# if _LIBCPP_HAS_EXCEPTIONS
1665 try {
1666# endif // _LIBCPP_HAS_EXCEPTIONS
1667 __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
1668# if _LIBCPP_HAS_EXCEPTIONS
1669 } catch (...) {
1670 __p_.set_exception(current_exception());
1671 }
1672# endif // _LIBCPP_HAS_EXCEPTIONS
1673}
1674
1675template <class _Rp, class... _ArgTypes>
1676void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1677 if (__p_.__state_ == nullptr)
1678 std::__throw_future_error(ev: future_errc::no_state);
1679 if (__p_.__state_->__has_value())
1680 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1681# if _LIBCPP_HAS_EXCEPTIONS
1682 try {
1683# endif // _LIBCPP_HAS_EXCEPTIONS
1684 __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
1685# if _LIBCPP_HAS_EXCEPTIONS
1686 } catch (...) {
1687 __p_.set_exception_at_thread_exit(current_exception());
1688 }
1689# endif // _LIBCPP_HAS_EXCEPTIONS
1690}
1691
1692template <class _Rp, class... _ArgTypes>
1693void packaged_task<_Rp(_ArgTypes...)>::reset() {
1694 if (!valid())
1695 std::__throw_future_error(ev: future_errc::no_state);
1696 __p_ = promise<_Rp>();
1697}
1698
1699template <class... _ArgTypes>
1700class packaged_task<void(_ArgTypes...)> {
1701private:
1702 __packaged_task_function<void(_ArgTypes...)> __f_;
1703 promise<void> __p_;
1704
1705public:
1706 // construction and destruction
1707 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1708 template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1709 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1710# if _LIBCPP_STD_VER <= 14
1711 template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1712 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1713 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1714# endif
1715 // ~packaged_task() = default;
1716
1717 // no copy
1718 packaged_task(const packaged_task&) = delete;
1719 packaged_task& operator=(const packaged_task&) = delete;
1720
1721 // move support
1722 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1723 : __f_(std::move(__other.__f_)),
1724 __p_(std::move(__other.__p_)) {}
1725 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1726 __f_ = std::move(__other.__f_);
1727 __p_ = std::move(__other.__p_);
1728 return *this;
1729 }
1730 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1731 __f_.swap(__other.__f_);
1732 __p_.swap(rhs&: __other.__p_);
1733 }
1734
1735 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1736
1737 // result retrieval
1738 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<void> get_future() { return __p_.get_future(); }
1739
1740 // execution
1741 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1742 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1743
1744 _LIBCPP_HIDE_FROM_ABI void reset();
1745};
1746
1747# if _LIBCPP_STD_VER >= 17
1748
1749template <class _Rp, class... _Args>
1750packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
1751
1752template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1753packaged_task(_Fp) -> packaged_task<_Stripped>;
1754
1755# endif
1756
1757template <class... _ArgTypes>
1758void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1759 if (__p_.__state_ == nullptr)
1760 std::__throw_future_error(ev: future_errc::no_state);
1761 if (__p_.__state_->__has_value())
1762 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1763# if _LIBCPP_HAS_EXCEPTIONS
1764 try {
1765# endif // _LIBCPP_HAS_EXCEPTIONS
1766 __f_(std::forward<_ArgTypes>(__args)...);
1767 __p_.set_value();
1768# if _LIBCPP_HAS_EXCEPTIONS
1769 } catch (...) {
1770 __p_.set_exception(current_exception());
1771 }
1772# endif // _LIBCPP_HAS_EXCEPTIONS
1773}
1774
1775template <class... _ArgTypes>
1776void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1777 if (__p_.__state_ == nullptr)
1778 std::__throw_future_error(ev: future_errc::no_state);
1779 if (__p_.__state_->__has_value())
1780 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1781# if _LIBCPP_HAS_EXCEPTIONS
1782 try {
1783# endif // _LIBCPP_HAS_EXCEPTIONS
1784 __f_(std::forward<_ArgTypes>(__args)...);
1785 __p_.set_value_at_thread_exit();
1786# if _LIBCPP_HAS_EXCEPTIONS
1787 } catch (...) {
1788 __p_.set_exception_at_thread_exit(current_exception());
1789 }
1790# endif // _LIBCPP_HAS_EXCEPTIONS
1791}
1792
1793template <class... _ArgTypes>
1794void packaged_task<void(_ArgTypes...)>::reset() {
1795 if (!valid())
1796 std::__throw_future_error(ev: future_errc::no_state);
1797 __p_ = promise<void>();
1798}
1799
1800template <class _Rp, class... _ArgTypes>
1801inline _LIBCPP_HIDE_FROM_ABI void
1802swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1803 __x.swap(__y);
1804}
1805
1806# if _LIBCPP_STD_VER <= 14
1807template <class _Callable, class _Alloc>
1808struct uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
1809# endif
1810
1811template <class _Rp, class _Fp>
1812_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
1813 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1814 new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1815 return future<_Rp>(__h.get());
1816}
1817
1818template <class _Rp, class _Fp>
1819_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
1820 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1821 new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1822 auto __guard = std::__make_exception_guard([&] { __h->__make_ready(); });
1823 std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
1824 __guard.__complete();
1825 return future<_Rp>(__h.get());
1826}
1827
1828# ifndef _LIBCPP_CXX03_LANG
1829
1830template <class _Fp, class... _Args>
1831class _LIBCPP_HIDDEN __async_func {
1832 tuple<_Fp, _Args...> __f_;
1833
1834public:
1835 using _Rp _LIBCPP_NODEBUG = __invoke_result_t<_Fp, _Args...>;
1836
1837 template <class _Gp, class... _BArgs>
1838 _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Gp&& __g, _BArgs&&... __bargs)
1839 : __f_(std::forward<_Gp>(__g), std::forward<_BArgs>(__bargs)...) {}
1840
1841 _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
1842
1843 _LIBCPP_HIDE_FROM_ABI _Rp operator()() {
1844 return [&]<size_t... _Indices>(__index_sequence<_Indices...>) -> _Rp {
1845 return std::__invoke(std::move(std::get<_Indices>(__f_))...);
1846 }(__index_sequence_for<_Fp, _Args...>{});
1847 }
1848};
1849
1850inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
1851 return (int(__policy) & int(__value)) != 0;
1852}
1853
1854template <class _Fp, class... _Args>
1855[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<__invoke_result_t<__decay_t<_Fp>, __decay_t<_Args>...> >
1856async(launch __policy, _Fp&& __f, _Args&&... __args) {
1857 static_assert(is_constructible<__decay_t<_Fp>, _Fp>::value, "");
1858 static_assert(_And<is_constructible<__decay_t<_Args>, _Args>...>::value, "");
1859 static_assert(__is_invocable_v<__decay_t<_Fp>, __decay_t<_Args>...>, "");
1860
1861 typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
1862 typedef typename _BF::_Rp _Rp;
1863
1864# if _LIBCPP_HAS_EXCEPTIONS
1865 try {
1866# endif
1867 if (__does_policy_contain(__policy, value: launch::async))
1868 return std::__make_async_assoc_state<_Rp>(_BF(std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
1869# if _LIBCPP_HAS_EXCEPTIONS
1870 } catch (...) {
1871 if (__policy == launch::async)
1872 throw;
1873 }
1874# endif
1875
1876 if (__does_policy_contain(__policy, value: launch::deferred))
1877 return std::__make_deferred_assoc_state<_Rp>(_BF(std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
1878 return future<_Rp>{};
1879}
1880
1881template <class _Fp, class... _Args>
1882[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI future<__invoke_result_t<__decay_t<_Fp>, __decay_t<_Args>...> >
1883async(_Fp&& __f, _Args&&... __args) {
1884 return std::async(launch::async | launch::deferred, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);
1885}
1886
1887# endif // C++03
1888
1889// shared_future
1890
1891template <class _Rp>
1892class shared_future {
1893 __assoc_state<_Rp>* __state_;
1894
1895public:
1896 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1897 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1898 if (__state_)
1899 __state_->__add_shared();
1900 }
1901 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1902 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1903 __rhs.__state_ = nullptr;
1904 }
1905 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1906 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
1907 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1908 shared_future(std::move(__rhs)).swap(*this);
1909 return *this;
1910 }
1911
1912 // retrieving the value
1913 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }
1914
1915 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1916
1917 // functions to check state
1918 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1919
1920 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1921 template <class _Rep, class _Period>
1922 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1923 return __state_->wait_for(__rel_time);
1924 }
1925 template <class _Clock, class _Duration>
1926 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1927 return __state_->wait_until(__abs_time);
1928 }
1929};
1930
1931template <class _Rp>
1932shared_future<_Rp>::~shared_future() {
1933 if (__state_)
1934 __state_->__release_shared();
1935}
1936
1937template <class _Rp>
1938shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {
1939 if (__rhs.__state_)
1940 __rhs.__state_->__add_shared();
1941 if (__state_)
1942 __state_->__release_shared();
1943 __state_ = __rhs.__state_;
1944 return *this;
1945}
1946
1947template <class _Rp>
1948class shared_future<_Rp&> {
1949 __assoc_state<_Rp&>* __state_;
1950
1951public:
1952 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1953 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
1954 if (__state_)
1955 __state_->__add_shared();
1956 }
1957 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1958 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1959 __rhs.__state_ = nullptr;
1960 }
1961 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1962 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
1963 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1964 shared_future(std::move(__rhs)).swap(*this);
1965 return *this;
1966 }
1967
1968 // retrieving the value
1969 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }
1970
1971 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1972
1973 // functions to check state
1974 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1975
1976 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1977 template <class _Rep, class _Period>
1978 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1979 return __state_->wait_for(__rel_time);
1980 }
1981 template <class _Clock, class _Duration>
1982 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1983 return __state_->wait_until(__abs_time);
1984 }
1985};
1986
1987template <class _Rp>
1988shared_future<_Rp&>::~shared_future() {
1989 if (__state_)
1990 __state_->__release_shared();
1991}
1992
1993template <class _Rp>
1994shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {
1995 if (__rhs.__state_)
1996 __rhs.__state_->__add_shared();
1997 if (__state_)
1998 __state_->__release_shared();
1999 __state_ = __rhs.__state_;
2000 return *this;
2001}
2002
2003template <>
2004class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
2005 __assoc_sub_state* __state_;
2006
2007public:
2008 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
2009 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
2010 if (__state_)
2011 __state_->__add_shared();
2012 }
2013 _LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
2014 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
2015 __rhs.__state_ = nullptr;
2016 }
2017 ~shared_future();
2018 shared_future& operator=(const shared_future& __rhs);
2019 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
2020 shared_future(std::move(__rhs)).swap(rhs&: *this);
2021 return *this;
2022 }
2023
2024 // retrieving the value
2025 _LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }
2026
2027 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(x&: __state_, y&: __rhs.__state_); }
2028
2029 // functions to check state
2030 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
2031
2032 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
2033 template <class _Rep, class _Period>
2034 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
2035 return __state_->wait_for(__rel_time);
2036 }
2037 template <class _Clock, class _Duration>
2038 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
2039 return __state_->wait_until(__abs_time);
2040 }
2041};
2042
2043template <class _Rp>
2044inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
2045 __x.swap(__y);
2046}
2047
2048template <class _Rp>
2049inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
2050 return shared_future<_Rp>(std::move(*this));
2051}
2052
2053template <class _Rp>
2054inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
2055 return shared_future<_Rp&>(std::move(*this));
2056}
2057
2058inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
2059
2060_LIBCPP_END_NAMESPACE_STD
2061
2062_LIBCPP_POP_MACROS
2063
2064# endif // _LIBCPP_HAS_THREADS
2065
2066# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2067# include <chrono>
2068# endif
2069
2070# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2071# include <atomic>
2072# include <cstdlib>
2073# include <exception>
2074# include <iosfwd>
2075# include <system_error>
2076# include <thread>
2077# endif
2078#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
2079
2080#endif // _LIBCPP_FUTURE
2081