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_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
424
425// enum class future_errc
426_LIBCPP_DECLARE_STRONG_ENUM(future_errc){
427 future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};
428_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
429
430template <>
431struct is_error_code_enum<future_errc> : public true_type {};
432
433# ifdef _LIBCPP_CXX03_LANG
434template <>
435struct is_error_code_enum<future_errc::__lx> : public true_type {};
436# endif
437
438// enum class launch
439_LIBCPP_DECLARE_STRONG_ENUM(launch){
440 async = 1,
441 deferred = 2,
442 any _LIBCPP_DEPRECATED_("std::launch::any is a deprecated extension. "
443 "Use std::launch::async | std::launch::deferred instead. "
444 "It will be removed in LLVM 25.") = async | deferred};
445_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
446
447# ifndef _LIBCPP_CXX03_LANG
448
449using __launch_underlying_type _LIBCPP_NODEBUG = __underlying_type_t<launch>;
450
451inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator&(launch __x, launch __y) {
452 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));
453}
454
455inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator|(launch __x, launch __y) {
456 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | static_cast<__launch_underlying_type>(__y));
457}
458
459inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator^(launch __x, launch __y) {
460 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));
461}
462
463inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator~(launch __x) {
464 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
465}
466
467inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {
468 __x = __x & __y;
469 return __x;
470}
471
472inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {
473 __x = __x | __y;
474 return __x;
475}
476
477inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {
478 __x = __x ^ __y;
479 return __x;
480}
481
482# endif // !_LIBCPP_CXX03_LANG
483
484// enum class future_status
485_LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};
486_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
487
488[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
489
490[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {
491 return error_code(static_cast<int>(__e), future_category());
492}
493
494[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {
495 return error_condition(static_cast<int>(__e), future_category());
496}
497
498[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);
499
500class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {
501 error_code __ec_;
502
503 future_error(error_code);
504 friend void __throw_future_error(future_errc);
505 template <class>
506 friend class promise;
507
508public:
509# if _LIBCPP_STD_VER >= 17
510 _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(e: __ec)) {}
511# endif
512
513 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
514
515 _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
516 ~future_error() _NOEXCEPT override;
517};
518
519// Declared above std::future_error
520void __throw_future_error(future_errc __ev) {
521# if _LIBCPP_HAS_EXCEPTIONS
522 throw future_error(make_error_code(e: __ev));
523# else
524 (void)__ev;
525 _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
526# endif
527}
528
529class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {
530protected:
531 exception_ptr __exception_;
532 mutable mutex __mut_;
533 mutable condition_variable __cv_;
534 unsigned __state_;
535
536 void __on_zero_shared() _NOEXCEPT override;
537 void __sub_wait(unique_lock<mutex>& __lk);
538
539public:
540 enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };
541
542 _LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}
543
544 _LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }
545
546 _LIBCPP_HIDE_FROM_ABI void __attach_future() {
547 lock_guard<mutex> __lk(__mut_);
548 bool __has_future_attached = (__state_ & __future_attached) != 0;
549 if (__has_future_attached)
550 std::__throw_future_error(ev: future_errc::future_already_retrieved);
551 this->__add_shared();
552 __state_ |= __future_attached;
553 }
554
555 _LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }
556
557 void __make_ready();
558 _LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }
559
560 void set_value();
561 void set_value_at_thread_exit();
562
563 void set_exception(exception_ptr __p);
564 void set_exception_at_thread_exit(exception_ptr __p);
565
566 void copy();
567
568 void wait();
569 template <class _Rep, class _Period>
570 future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
571 template <class _Clock, class _Duration>
572 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
573 unique_lock<mutex> __lk(__mut_);
574 if (__state_ & deferred)
575 return future_status::deferred;
576 while (!(__state_ & ready) && _Clock::now() < __abs_time)
577 __cv_.wait_until(__lk, __abs_time);
578 if (__state_ & ready)
579 return future_status::ready;
580 return future_status::timeout;
581 }
582
583 virtual void __execute();
584};
585
586template <class _Rep, class _Period>
587inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
588 return wait_until(chrono::steady_clock::now() + __rel_time);
589}
590
591template <class _Rp>
592class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {
593 typedef __assoc_sub_state base;
594
595protected:
596 _ALIGNAS_TYPE(_Rp) char __value_[sizeof(_Rp)];
597
598 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
599
600public:
601 template <class _Arg>
602 _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
603
604 template <class _Arg>
605 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
606
607 _LIBCPP_HIDE_FROM_ABI _Rp move();
608 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
609};
610
611template <class _Rp>
612void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {
613 if (this->__state_ & base::__constructed)
614 reinterpret_cast<_Rp*>(std::addressof(__value_))->~_Rp();
615 delete this;
616}
617
618template <class _Rp>
619template <class _Arg>
620void __assoc_state<_Rp>::set_value(_Arg&& __arg) {
621 unique_lock<mutex> __lk(this->__mut_);
622 if (this->__has_value())
623 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
624 ::new ((void*)std::addressof(__value_)) _Rp(std::forward<_Arg>(__arg));
625 this->__state_ |= base::__constructed | base::ready;
626 __cv_.notify_all();
627}
628
629template <class _Rp>
630template <class _Arg>
631void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {
632 unique_lock<mutex> __lk(this->__mut_);
633 if (this->__has_value())
634 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
635 ::new ((void*)std::addressof(__value_)) _Rp(std::forward<_Arg>(__arg));
636 this->__state_ |= base::__constructed;
637 __thread_local_data()->__make_ready_at_thread_exit(this);
638}
639
640template <class _Rp>
641_Rp __assoc_state<_Rp>::move() {
642 unique_lock<mutex> __lk(this->__mut_);
643 this->__sub_wait(__lk);
644 if (this->__exception_ != nullptr)
645 std::rethrow_exception(this->__exception_);
646 return std::move(*reinterpret_cast<_Rp*>(std::addressof(__value_)));
647}
648
649template <class _Rp>
650_Rp& __assoc_state<_Rp>::copy() {
651 unique_lock<mutex> __lk(this->__mut_);
652 this->__sub_wait(__lk);
653 if (this->__exception_ != nullptr)
654 std::rethrow_exception(this->__exception_);
655 return *reinterpret_cast<_Rp*>(std::addressof(__value_));
656}
657
658template <class _Rp>
659class __assoc_state<_Rp&> : public __assoc_sub_state {
660 typedef __assoc_sub_state base;
661 typedef _Rp* _Up;
662
663protected:
664 _Up __value_;
665
666 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
667
668public:
669 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
670 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
671
672 _LIBCPP_HIDE_FROM_ABI _Rp& copy();
673};
674
675template <class _Rp>
676void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {
677 delete this;
678}
679
680template <class _Rp>
681void __assoc_state<_Rp&>::set_value(_Rp& __arg) {
682 unique_lock<mutex> __lk(this->__mut_);
683 if (this->__has_value())
684 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
685 __value_ = std::addressof(__arg);
686 this->__state_ |= base::__constructed | base::ready;
687 __cv_.notify_all();
688}
689
690template <class _Rp>
691void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {
692 unique_lock<mutex> __lk(this->__mut_);
693 if (this->__has_value())
694 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
695 __value_ = std::addressof(__arg);
696 this->__state_ |= base::__constructed;
697 __thread_local_data()->__make_ready_at_thread_exit(this);
698}
699
700template <class _Rp>
701_Rp& __assoc_state<_Rp&>::copy() {
702 unique_lock<mutex> __lk(this->__mut_);
703 this->__sub_wait(__lk);
704 if (this->__exception_ != nullptr)
705 std::rethrow_exception(this->__exception_);
706 return *__value_;
707}
708
709template <class _Rp, class _Alloc>
710class __assoc_state_alloc : public __assoc_state<_Rp> {
711 typedef __assoc_state<_Rp> base;
712 _Alloc __alloc_;
713
714 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
715
716public:
717 _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
718};
719
720template <class _Rp, class _Alloc>
721void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {
722 if (this->__state_ & base::__constructed)
723 reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();
724 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
725 typedef allocator_traits<_Al> _ATraits;
726 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
727 _Al __a(__alloc_);
728 this->~__assoc_state_alloc();
729 __a.deallocate(_PTraits::pointer_to(*this), 1);
730}
731
732template <class _Rp, class _Alloc>
733class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {
734 typedef __assoc_state<_Rp&> base;
735 _Alloc __alloc_;
736
737 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
738
739public:
740 _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
741};
742
743template <class _Rp, class _Alloc>
744void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {
745 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
746 typedef allocator_traits<_Al> _ATraits;
747 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
748 _Al __a(__alloc_);
749 this->~__assoc_state_alloc();
750 __a.deallocate(_PTraits::pointer_to(*this), 1);
751}
752
753template <class _Alloc>
754class __assoc_sub_state_alloc : public __assoc_sub_state {
755 typedef __assoc_sub_state base;
756 _Alloc __alloc_;
757
758 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
759
760public:
761 _LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
762};
763
764template <class _Alloc>
765void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {
766 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
767 typedef allocator_traits<_Al> _ATraits;
768 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
769 _Al __a(__alloc_);
770 this->~__assoc_sub_state_alloc();
771 __a.deallocate(_PTraits::pointer_to(*this), 1);
772}
773
774template <class _Rp, class _Fp>
775class __deferred_assoc_state : public __assoc_state<_Rp> {
776 typedef __assoc_state<_Rp> base;
777
778 _Fp __func_;
779
780public:
781 _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
782
783 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
784};
785
786template <class _Rp, class _Fp>
787inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
788 this->__set_deferred();
789}
790
791template <class _Rp, class _Fp>
792void __deferred_assoc_state<_Rp, _Fp>::__execute() {
793# if _LIBCPP_HAS_EXCEPTIONS
794 try {
795# endif // _LIBCPP_HAS_EXCEPTIONS
796 this->set_value(__func_());
797# if _LIBCPP_HAS_EXCEPTIONS
798 } catch (...) {
799 this->set_exception(current_exception());
800 }
801# endif // _LIBCPP_HAS_EXCEPTIONS
802}
803
804template <class _Fp>
805class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {
806 typedef __assoc_sub_state base;
807
808 _Fp __func_;
809
810public:
811 _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
812
813 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
814};
815
816template <class _Fp>
817inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
818 this->__set_deferred();
819}
820
821template <class _Fp>
822void __deferred_assoc_state<void, _Fp>::__execute() {
823# if _LIBCPP_HAS_EXCEPTIONS
824 try {
825# endif // _LIBCPP_HAS_EXCEPTIONS
826 __func_();
827 this->set_value();
828# if _LIBCPP_HAS_EXCEPTIONS
829 } catch (...) {
830 this->set_exception(current_exception());
831 }
832# endif // _LIBCPP_HAS_EXCEPTIONS
833}
834
835template <class _Rp, class _Fp>
836class __async_assoc_state : public __assoc_state<_Rp> {
837 typedef __assoc_state<_Rp> base;
838
839 _Fp __func_;
840
841 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
842
843public:
844 _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
845
846 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
847};
848
849template <class _Rp, class _Fp>
850inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
851
852template <class _Rp, class _Fp>
853void __async_assoc_state<_Rp, _Fp>::__execute() {
854# if _LIBCPP_HAS_EXCEPTIONS
855 try {
856# endif // _LIBCPP_HAS_EXCEPTIONS
857 this->set_value(__func_());
858# if _LIBCPP_HAS_EXCEPTIONS
859 } catch (...) {
860 this->set_exception(current_exception());
861 }
862# endif // _LIBCPP_HAS_EXCEPTIONS
863}
864
865template <class _Rp, class _Fp>
866void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {
867 this->wait();
868 base::__on_zero_shared();
869}
870
871template <class _Fp>
872class __async_assoc_state<void, _Fp> : public __assoc_sub_state {
873 typedef __assoc_sub_state base;
874
875 _Fp __func_;
876
877 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
878
879public:
880 _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
881
882 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
883};
884
885template <class _Fp>
886inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
887
888template <class _Fp>
889void __async_assoc_state<void, _Fp>::__execute() {
890# if _LIBCPP_HAS_EXCEPTIONS
891 try {
892# endif // _LIBCPP_HAS_EXCEPTIONS
893 __func_();
894 this->set_value();
895# if _LIBCPP_HAS_EXCEPTIONS
896 } catch (...) {
897 this->set_exception(current_exception());
898 }
899# endif // _LIBCPP_HAS_EXCEPTIONS
900}
901
902template <class _Fp>
903void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {
904 this->wait();
905 base::__on_zero_shared();
906}
907
908template <class _Rp>
909class promise;
910template <class _Rp>
911class shared_future;
912
913// future
914
915template <class _Rp>
916class future;
917
918template <class _Rp, class _Fp>
919_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);
920
921template <class _Rp, class _Fp>
922_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);
923
924template <class _Rp>
925class future {
926 __assoc_state<_Rp>* __state_;
927
928 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
929
930 template <class>
931 friend class promise;
932 template <class>
933 friend class shared_future;
934
935 template <class _R1, class _Fp>
936 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
937 template <class _R1, class _Fp>
938 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
939
940public:
941 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
942 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
943 future(const future&) = delete;
944 future& operator=(const future&) = delete;
945 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
946 future(std::move(__rhs)).swap(rhs&: *this);
947 return *this;
948 }
949
950 _LIBCPP_HIDE_FROM_ABI ~future();
951 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;
952
953 // retrieving the value
954 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _Rp get();
955
956 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
957
958 // functions to check state
959 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
960
961 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
962 template <class _Rep, class _Period>
963 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
964 return __state_->wait_for(__rel_time);
965 }
966 template <class _Clock, class _Duration>
967 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
968 return __state_->wait_until(__abs_time);
969 }
970};
971
972template <class _Rp>
973future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {
974 __state_->__attach_future();
975}
976
977struct __release_shared_count {
978 _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }
979};
980
981template <class _Rp>
982future<_Rp>::~future() {
983 if (__state_)
984 __state_->__release_shared();
985}
986
987template <class _Rp>
988_Rp future<_Rp>::get() {
989 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
990 __assoc_state<_Rp>* __s = __state_;
991 __state_ = nullptr;
992 return __s->move();
993}
994
995template <class _Rp>
996class future<_Rp&> {
997 __assoc_state<_Rp&>* __state_;
998
999 explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
1000
1001 template <class>
1002 friend class promise;
1003 template <class>
1004 friend class shared_future;
1005
1006 template <class _R1, class _Fp>
1007 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1008 template <class _R1, class _Fp>
1009 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1010
1011public:
1012 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1013 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1014 future(const future&) = delete;
1015 future& operator=(const future&) = delete;
1016 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1017 future(std::move(__rhs)).swap(rhs&: *this);
1018 return *this;
1019 }
1020
1021 _LIBCPP_HIDE_FROM_ABI ~future();
1022 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;
1023
1024 // retrieving the value
1025 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _Rp& get();
1026
1027 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1028
1029 // functions to check state
1030 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1031
1032 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1033 template <class _Rep, class _Period>
1034 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1035 return __state_->wait_for(__rel_time);
1036 }
1037 template <class _Clock, class _Duration>
1038 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1039 return __state_->wait_until(__abs_time);
1040 }
1041};
1042
1043template <class _Rp>
1044future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {
1045 __state_->__attach_future();
1046}
1047
1048template <class _Rp>
1049future<_Rp&>::~future() {
1050 if (__state_)
1051 __state_->__release_shared();
1052}
1053
1054template <class _Rp>
1055_Rp& future<_Rp&>::get() {
1056 unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
1057 __assoc_state<_Rp&>* __s = __state_;
1058 __state_ = nullptr;
1059 return __s->copy();
1060}
1061
1062template <>
1063class _LIBCPP_EXPORTED_FROM_ABI future<void> {
1064 __assoc_sub_state* __state_;
1065
1066 explicit future(__assoc_sub_state* __state);
1067
1068 template <class>
1069 friend class promise;
1070 template <class>
1071 friend class shared_future;
1072
1073 template <class _R1, class _Fp>
1074 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1075 template <class _R1, class _Fp>
1076 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1077
1078public:
1079 _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
1080 _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1081 future(const future&) = delete;
1082 future& operator=(const future&) = delete;
1083 _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
1084 future(std::move(__rhs)).swap(rhs&: *this);
1085 return *this;
1086 }
1087
1088 ~future();
1089 _LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;
1090
1091 // retrieving the value
1092 void get();
1093
1094 _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(x&: __state_, y&: __rhs.__state_); }
1095
1096 // functions to check state
1097 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1098
1099 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1100 template <class _Rep, class _Period>
1101 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1102 return __state_->wait_for(__rel_time);
1103 }
1104 template <class _Clock, class _Duration>
1105 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1106 return __state_->wait_until(__abs_time);
1107 }
1108};
1109
1110template <class _Rp>
1111inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {
1112 __x.swap(__y);
1113}
1114
1115// promise<R>
1116
1117template <class _Callable>
1118class packaged_task;
1119
1120template <class _Rp>
1121class promise {
1122 __assoc_state<_Rp>* __state_;
1123
1124 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1125
1126 template <class>
1127 friend class packaged_task;
1128
1129public:
1130 _LIBCPP_HIDE_FROM_ABI promise();
1131 template <class _Alloc>
1132 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
1133 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1134 promise(const promise& __rhs) = delete;
1135 _LIBCPP_HIDE_FROM_ABI ~promise();
1136
1137 // assignment
1138 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1139 promise(std::move(__rhs)).swap(rhs&: *this);
1140 return *this;
1141 }
1142 promise& operator=(const promise& __rhs) = delete;
1143
1144 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1145
1146 // retrieving the result
1147 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
1148
1149 // setting the result
1150 _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
1151 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
1152 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1153
1154 // setting the result with deferred notification
1155 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
1156 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
1157 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1158};
1159
1160template <class _Rp>
1161promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}
1162
1163template <class _Rp>
1164template <class _Alloc>
1165promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {
1166 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1167 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1168 typedef __allocator_destructor<_A2> _D2;
1169 _A2 __a(__a0);
1170 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1171 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1172 __state_ = std::addressof(*__hold.release());
1173}
1174
1175template <class _Rp>
1176promise<_Rp>::~promise() {
1177 if (__state_) {
1178 if (!__state_->__has_value() && __state_->use_count() > 1)
1179 __state_->set_exception(make_exception_ptr(e: future_error(make_error_code(e: future_errc::broken_promise))));
1180 __state_->__release_shared();
1181 }
1182}
1183
1184template <class _Rp>
1185future<_Rp> promise<_Rp>::get_future() {
1186 if (__state_ == nullptr)
1187 std::__throw_future_error(ev: future_errc::no_state);
1188 return future<_Rp>(__state_);
1189}
1190
1191template <class _Rp>
1192void promise<_Rp>::set_value(const _Rp& __r) {
1193 if (__state_ == nullptr)
1194 std::__throw_future_error(ev: future_errc::no_state);
1195 __state_->set_value(__r);
1196}
1197
1198template <class _Rp>
1199void promise<_Rp>::set_value(_Rp&& __r) {
1200 if (__state_ == nullptr)
1201 std::__throw_future_error(ev: future_errc::no_state);
1202 __state_->set_value(std::move(__r));
1203}
1204
1205template <class _Rp>
1206void promise<_Rp>::set_exception(exception_ptr __p) {
1207 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1208 if (__state_ == nullptr)
1209 std::__throw_future_error(ev: future_errc::no_state);
1210 __state_->set_exception(__p);
1211}
1212
1213template <class _Rp>
1214void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {
1215 if (__state_ == nullptr)
1216 std::__throw_future_error(ev: future_errc::no_state);
1217 __state_->set_value_at_thread_exit(__r);
1218}
1219
1220template <class _Rp>
1221void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {
1222 if (__state_ == nullptr)
1223 std::__throw_future_error(ev: future_errc::no_state);
1224 __state_->set_value_at_thread_exit(std::move(__r));
1225}
1226
1227template <class _Rp>
1228void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {
1229 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1230 if (__state_ == nullptr)
1231 std::__throw_future_error(ev: future_errc::no_state);
1232 __state_->set_exception_at_thread_exit(__p);
1233}
1234
1235// promise<R&>
1236
1237template <class _Rp>
1238class promise<_Rp&> {
1239 __assoc_state<_Rp&>* __state_;
1240
1241 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1242
1243 template <class>
1244 friend class packaged_task;
1245
1246public:
1247 _LIBCPP_HIDE_FROM_ABI promise();
1248 template <class _Allocator>
1249 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
1250 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1251 promise(const promise& __rhs) = delete;
1252 _LIBCPP_HIDE_FROM_ABI ~promise();
1253
1254 // assignment
1255 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1256 promise(std::move(__rhs)).swap(rhs&: *this);
1257 return *this;
1258 }
1259 promise& operator=(const promise& __rhs) = delete;
1260
1261 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1262
1263 // retrieving the result
1264 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
1265
1266 // setting the result
1267 _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
1268 _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
1269
1270 // setting the result with deferred notification
1271 _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
1272 _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
1273};
1274
1275template <class _Rp>
1276promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}
1277
1278template <class _Rp>
1279template <class _Alloc>
1280promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {
1281 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1282 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1283 typedef __allocator_destructor<_A2> _D2;
1284 _A2 __a(__a0);
1285 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1286 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1287 __state_ = std::addressof(*__hold.release());
1288}
1289
1290template <class _Rp>
1291promise<_Rp&>::~promise() {
1292 if (__state_) {
1293 if (!__state_->__has_value() && __state_->use_count() > 1)
1294 __state_->set_exception(make_exception_ptr(e: future_error(make_error_code(e: future_errc::broken_promise))));
1295 __state_->__release_shared();
1296 }
1297}
1298
1299template <class _Rp>
1300future<_Rp&> promise<_Rp&>::get_future() {
1301 if (__state_ == nullptr)
1302 std::__throw_future_error(ev: future_errc::no_state);
1303 return future<_Rp&>(__state_);
1304}
1305
1306template <class _Rp>
1307void promise<_Rp&>::set_value(_Rp& __r) {
1308 if (__state_ == nullptr)
1309 std::__throw_future_error(ev: future_errc::no_state);
1310 __state_->set_value(__r);
1311}
1312
1313template <class _Rp>
1314void promise<_Rp&>::set_exception(exception_ptr __p) {
1315 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
1316 if (__state_ == nullptr)
1317 std::__throw_future_error(ev: future_errc::no_state);
1318 __state_->set_exception(__p);
1319}
1320
1321template <class _Rp>
1322void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {
1323 if (__state_ == nullptr)
1324 std::__throw_future_error(ev: future_errc::no_state);
1325 __state_->set_value_at_thread_exit(__r);
1326}
1327
1328template <class _Rp>
1329void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {
1330 _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
1331 if (__state_ == nullptr)
1332 std::__throw_future_error(ev: future_errc::no_state);
1333 __state_->set_exception_at_thread_exit(__p);
1334}
1335
1336// promise<void>
1337
1338template <>
1339class _LIBCPP_EXPORTED_FROM_ABI promise<void> {
1340 __assoc_sub_state* __state_;
1341
1342 _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1343
1344 template <class>
1345 friend class packaged_task;
1346
1347public:
1348 promise();
1349 template <class _Alloc>
1350 _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a0) {
1351 typedef __assoc_sub_state_alloc<_Alloc> _State;
1352 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1353 typedef __allocator_destructor<_A2> _D2;
1354 _A2 __a(__a0);
1355 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1356 ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
1357 __state_ = std::addressof(*__hold.release());
1358 }
1359
1360 _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
1361 promise(const promise& __rhs) = delete;
1362 ~promise();
1363
1364 // assignment
1365 _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
1366 promise(std::move(__rhs)).swap(rhs&: *this);
1367 return *this;
1368 }
1369 promise& operator=(const promise& __rhs) = delete;
1370
1371 _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(x&: __state_, y&: __rhs.__state_); }
1372
1373 // retrieving the result
1374 [[__nodiscard__]] future<void> get_future();
1375
1376 // setting the result
1377 void set_value();
1378 void set_exception(exception_ptr __p);
1379
1380 // setting the result with deferred notification
1381 void set_value_at_thread_exit();
1382 void set_exception_at_thread_exit(exception_ptr __p);
1383};
1384
1385template <class _Rp>
1386inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {
1387 __x.swap(__y);
1388}
1389
1390template <class _Rp, class _Alloc>
1391struct uses_allocator<promise<_Rp>, _Alloc> : public true_type {};
1392
1393// packaged_task
1394
1395template <class _Fp>
1396class __packaged_task_base;
1397
1398template <class _Rp, class... _ArgTypes>
1399class __packaged_task_base<_Rp(_ArgTypes...)> {
1400public:
1401 _LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}
1402 __packaged_task_base(const __packaged_task_base&) = delete;
1403 __packaged_task_base& operator=(const __packaged_task_base&) = delete;
1404 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1405 virtual ~__packaged_task_base() {}
1406 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1407 virtual void destroy() = 0;
1408 virtual void destroy_deallocate() = 0;
1409 virtual _Rp operator()(_ArgTypes&&...) = 0;
1410};
1411
1412template <class _FD, class _Alloc, class _FB>
1413class __packaged_task_func;
1414
1415template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1416class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
1417 _LIBCPP_COMPRESSED_PAIR(_Fp, __func_, _Alloc, __alloc_);
1418
1419public:
1420 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __func_(__f) {}
1421 _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __func_(std::move(__f)) {}
1422 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __func_(__f), __alloc_(__a) {}
1423 _LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __func_(std::move(__f)), __alloc_(__a) {}
1424 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1425 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
1426 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
1427 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);
1428};
1429
1430template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1431void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1432 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {
1433 ::new ((void*)__p) __packaged_task_func(std::move(__func_), std::move(__alloc_));
1434}
1435
1436template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1437void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
1438 __func_.~_Fp();
1439 __alloc_.~_Alloc();
1440}
1441
1442template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1443void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {
1444 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1445 typedef allocator_traits<_Ap> _ATraits;
1446 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1447 _Ap __a(__alloc_);
1448 __func_.~_Fp();
1449 __alloc_.~_Alloc();
1450 __a.deallocate(_PTraits::pointer_to(*this), 1);
1451}
1452
1453template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
1454_Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
1455 return std::__invoke(__func_, std::forward<_ArgTypes>(__arg)...);
1456}
1457
1458template <class _Callable>
1459class __packaged_task_function;
1460
1461template <class _Rp, class... _ArgTypes>
1462class __packaged_task_function<_Rp(_ArgTypes...)> {
1463 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1464
1465 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }
1466
1467 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1468 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1469 _LIBCPP_SUPPRESS_DEPRECATED_POP
1470 __base* __f_;
1471
1472public:
1473 typedef _Rp result_type;
1474
1475 // construct/copy/destroy:
1476 _LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1477 template <class _Fp>
1478 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
1479 template <class _Fp, class _Alloc>
1480 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1481
1482 _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1483 _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1484
1485 __packaged_task_function(const __packaged_task_function&) = delete;
1486 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1487
1488 _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
1489
1490 _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
1491
1492 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1493};
1494
1495template <class _Rp, class... _ArgTypes>
1496__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {
1497 if (__f.__f_ == nullptr)
1498 __f_ = nullptr;
1499 else if (__f.__f_ == __f.__get_buf()) {
1500 __f.__f_->__move_to(__get_buf());
1501 __f_ = (__base*)&__buf_;
1502 } else {
1503 __f_ = __f.__f_;
1504 __f.__f_ = nullptr;
1505 }
1506}
1507
1508template <class _Rp, class... _ArgTypes>
1509template <class _Fp>
1510__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {
1511 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1512 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1513 if (sizeof(_FF) <= sizeof(__buf_)) {
1514 ::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));
1515 __f_ = (__base*)&__buf_;
1516 } else {
1517 typedef allocator<_FF> _Ap;
1518 _Ap __a;
1519 typedef __allocator_destructor<_Ap> _Dp;
1520 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1521 ::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));
1522 __f_ = __hold.release();
1523 }
1524}
1525
1526template <class _Rp, class... _ArgTypes>
1527template <class _Fp, class _Alloc>
1528__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1529 : __f_(nullptr) {
1530 typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
1531 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1532 if (sizeof(_FF) <= sizeof(__buf_)) {
1533 __f_ = (__base*)&__buf_;
1534 ::new ((void*)__f_) _FF(std::forward<_Fp>(__f));
1535 } else {
1536 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1537 _Ap __a(__a0);
1538 typedef __allocator_destructor<_Ap> _Dp;
1539 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1540 ::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));
1541 __f_ = std::addressof(*__hold.release());
1542 }
1543}
1544
1545template <class _Rp, class... _ArgTypes>
1546__packaged_task_function<_Rp(_ArgTypes...)>&
1547__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {
1548 if (__f_ == __get_buf())
1549 __f_->destroy();
1550 else if (__f_)
1551 __f_->destroy_deallocate();
1552 __f_ = nullptr;
1553 if (__f.__f_ == nullptr)
1554 __f_ = nullptr;
1555 else if (__f.__f_ == __f.__get_buf()) {
1556 __f.__f_->__move_to(__get_buf());
1557 __f_ = __get_buf();
1558 } else {
1559 __f_ = __f.__f_;
1560 __f.__f_ = nullptr;
1561 }
1562 return *this;
1563}
1564
1565template <class _Rp, class... _ArgTypes>
1566__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {
1567 if (__f_ == __get_buf())
1568 __f_->destroy();
1569 else if (__f_)
1570 __f_->destroy_deallocate();
1571}
1572
1573template <class _Rp, class... _ArgTypes>
1574_LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {
1575 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {
1576 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1577 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1578 _LIBCPP_SUPPRESS_DEPRECATED_POP
1579 __base* __t = (__base*)&__tempbuf;
1580 __f_->__move_to(__t);
1581 __f_->destroy();
1582 __f_ = nullptr;
1583 __f.__f_->__move_to((__base*)&__buf_);
1584 __f.__f_->destroy();
1585 __f.__f_ = nullptr;
1586 __f_ = (__base*)&__buf_;
1587 __t->__move_to((__base*)&__f.__buf_);
1588 __t->destroy();
1589 __f.__f_ = (__base*)&__f.__buf_;
1590 } else if (__f_ == (__base*)&__buf_) {
1591 __f_->__move_to((__base*)&__f.__buf_);
1592 __f_->destroy();
1593 __f_ = __f.__f_;
1594 __f.__f_ = (__base*)&__f.__buf_;
1595 } else if (__f.__f_ == (__base*)&__f.__buf_) {
1596 __f.__f_->__move_to((__base*)&__buf_);
1597 __f.__f_->destroy();
1598 __f.__f_ = __f_;
1599 __f_ = (__base*)&__buf_;
1600 } else
1601 std::swap(__f_, __f.__f_);
1602}
1603
1604template <class _Rp, class... _ArgTypes>
1605inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
1606 return (*__f_)(std::forward<_ArgTypes>(__arg)...);
1607}
1608
1609template <class _Rp, class... _ArgTypes>
1610class packaged_task<_Rp(_ArgTypes...)> {
1611private:
1612 __packaged_task_function<_Rp(_ArgTypes...)> __f_;
1613 promise<_Rp> __p_;
1614
1615public:
1616 // construction and destruction
1617 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1618
1619 template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1620 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1621
1622# if _LIBCPP_STD_VER <= 14
1623 template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1624 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1625 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1626# endif
1627 // ~packaged_task() = default;
1628
1629 // no copy
1630 packaged_task(const packaged_task&) = delete;
1631 packaged_task& operator=(const packaged_task&) = delete;
1632
1633 // move support
1634 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1635 : __f_(std::move(__other.__f_)),
1636 __p_(std::move(__other.__p_)) {}
1637 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1638 __f_ = std::move(__other.__f_);
1639 __p_ = std::move(__other.__p_);
1640 return *this;
1641 }
1642 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1643 __f_.swap(__other.__f_);
1644 __p_.swap(__other.__p_);
1645 }
1646
1647 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1648
1649 // result retrieval
1650 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future() { return __p_.get_future(); }
1651
1652 // execution
1653 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1654 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1655
1656 _LIBCPP_HIDE_FROM_ABI void reset();
1657};
1658
1659template <class _Rp, class... _ArgTypes>
1660void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1661 if (__p_.__state_ == nullptr)
1662 std::__throw_future_error(ev: future_errc::no_state);
1663 if (__p_.__state_->__has_value())
1664 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1665# if _LIBCPP_HAS_EXCEPTIONS
1666 try {
1667# endif // _LIBCPP_HAS_EXCEPTIONS
1668 __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
1669# if _LIBCPP_HAS_EXCEPTIONS
1670 } catch (...) {
1671 __p_.set_exception(current_exception());
1672 }
1673# endif // _LIBCPP_HAS_EXCEPTIONS
1674}
1675
1676template <class _Rp, class... _ArgTypes>
1677void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1678 if (__p_.__state_ == nullptr)
1679 std::__throw_future_error(ev: future_errc::no_state);
1680 if (__p_.__state_->__has_value())
1681 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1682# if _LIBCPP_HAS_EXCEPTIONS
1683 try {
1684# endif // _LIBCPP_HAS_EXCEPTIONS
1685 __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
1686# if _LIBCPP_HAS_EXCEPTIONS
1687 } catch (...) {
1688 __p_.set_exception_at_thread_exit(current_exception());
1689 }
1690# endif // _LIBCPP_HAS_EXCEPTIONS
1691}
1692
1693template <class _Rp, class... _ArgTypes>
1694void packaged_task<_Rp(_ArgTypes...)>::reset() {
1695 if (!valid())
1696 std::__throw_future_error(ev: future_errc::no_state);
1697 __p_ = promise<_Rp>();
1698}
1699
1700template <class... _ArgTypes>
1701class packaged_task<void(_ArgTypes...)> {
1702private:
1703 __packaged_task_function<void(_ArgTypes...)> __f_;
1704 promise<void> __p_;
1705
1706public:
1707 // construction and destruction
1708 _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
1709 template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1710 _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
1711# if _LIBCPP_STD_VER <= 14
1712 template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
1713 _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1714 : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
1715# endif
1716 // ~packaged_task() = default;
1717
1718 // no copy
1719 packaged_task(const packaged_task&) = delete;
1720 packaged_task& operator=(const packaged_task&) = delete;
1721
1722 // move support
1723 _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
1724 : __f_(std::move(__other.__f_)),
1725 __p_(std::move(__other.__p_)) {}
1726 _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
1727 __f_ = std::move(__other.__f_);
1728 __p_ = std::move(__other.__p_);
1729 return *this;
1730 }
1731 _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
1732 __f_.swap(__other.__f_);
1733 __p_.swap(rhs&: __other.__p_);
1734 }
1735
1736 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
1737
1738 // result retrieval
1739 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<void> get_future() { return __p_.get_future(); }
1740
1741 // execution
1742 _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
1743 _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
1744
1745 _LIBCPP_HIDE_FROM_ABI void reset();
1746};
1747
1748# if _LIBCPP_STD_VER >= 17
1749
1750template <class _Rp, class... _Args>
1751packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
1752
1753template <class _Fp, class _Stripped = __strip_signature_t<decltype(&_Fp::operator())>>
1754packaged_task(_Fp) -> packaged_task<_Stripped>;
1755
1756# endif
1757
1758template <class... _ArgTypes>
1759void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
1760 if (__p_.__state_ == nullptr)
1761 std::__throw_future_error(ev: future_errc::no_state);
1762 if (__p_.__state_->__has_value())
1763 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1764# if _LIBCPP_HAS_EXCEPTIONS
1765 try {
1766# endif // _LIBCPP_HAS_EXCEPTIONS
1767 __f_(std::forward<_ArgTypes>(__args)...);
1768 __p_.set_value();
1769# if _LIBCPP_HAS_EXCEPTIONS
1770 } catch (...) {
1771 __p_.set_exception(current_exception());
1772 }
1773# endif // _LIBCPP_HAS_EXCEPTIONS
1774}
1775
1776template <class... _ArgTypes>
1777void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
1778 if (__p_.__state_ == nullptr)
1779 std::__throw_future_error(ev: future_errc::no_state);
1780 if (__p_.__state_->__has_value())
1781 std::__throw_future_error(ev: future_errc::promise_already_satisfied);
1782# if _LIBCPP_HAS_EXCEPTIONS
1783 try {
1784# endif // _LIBCPP_HAS_EXCEPTIONS
1785 __f_(std::forward<_ArgTypes>(__args)...);
1786 __p_.set_value_at_thread_exit();
1787# if _LIBCPP_HAS_EXCEPTIONS
1788 } catch (...) {
1789 __p_.set_exception_at_thread_exit(current_exception());
1790 }
1791# endif // _LIBCPP_HAS_EXCEPTIONS
1792}
1793
1794template <class... _ArgTypes>
1795void packaged_task<void(_ArgTypes...)>::reset() {
1796 if (!valid())
1797 std::__throw_future_error(ev: future_errc::no_state);
1798 __p_ = promise<void>();
1799}
1800
1801template <class _Rp, class... _ArgTypes>
1802inline _LIBCPP_HIDE_FROM_ABI void
1803swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
1804 __x.swap(__y);
1805}
1806
1807# if _LIBCPP_STD_VER <= 14
1808template <class _Callable, class _Alloc>
1809struct uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
1810# endif
1811
1812template <class _Rp, class _Fp>
1813_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
1814 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1815 new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1816 return future<_Rp>(__h.get());
1817}
1818
1819template <class _Rp, class _Fp>
1820_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
1821 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
1822 new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
1823 auto __guard = std::__make_exception_guard([&] { __h->__make_ready(); });
1824 std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
1825 __guard.__complete();
1826 return future<_Rp>(__h.get());
1827}
1828
1829# ifndef _LIBCPP_CXX03_LANG
1830
1831template <class _Fp, class... _Args>
1832class _LIBCPP_HIDDEN __async_func {
1833 tuple<_Fp, _Args...> __f_;
1834
1835public:
1836 using _Rp _LIBCPP_NODEBUG = __invoke_result_t<_Fp, _Args...>;
1837
1838 template <class _Gp, class... _BArgs>
1839 _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Gp&& __g, _BArgs&&... __bargs)
1840 : __f_(std::forward<_Gp>(__g), std::forward<_BArgs>(__bargs)...) {}
1841
1842 _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
1843
1844 _LIBCPP_HIDE_FROM_ABI _Rp operator()() {
1845 return [&]<size_t... _Indices>(__index_sequence<_Indices...>) -> _Rp {
1846 return std::__invoke(std::move(std::get<_Indices>(__f_))...);
1847 }(__index_sequence_for<_Fp, _Args...>{});
1848 }
1849};
1850
1851inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
1852 return (int(__policy) & int(__value)) != 0;
1853}
1854
1855template <class _Fp, class... _Args>
1856[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI future<__invoke_result_t<__decay_t<_Fp>, __decay_t<_Args>...> >
1857async(launch __policy, _Fp&& __f, _Args&&... __args) {
1858 static_assert(is_constructible<__decay_t<_Fp>, _Fp>::value, "");
1859 static_assert(_And<is_constructible<__decay_t<_Args>, _Args>...>::value, "");
1860 static_assert(__is_invocable_v<__decay_t<_Fp>, __decay_t<_Args>...>, "");
1861
1862 typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
1863 typedef typename _BF::_Rp _Rp;
1864
1865# if _LIBCPP_HAS_EXCEPTIONS
1866 try {
1867# endif
1868 if (__does_policy_contain(__policy, value: launch::async))
1869 return std::__make_async_assoc_state<_Rp>(_BF(std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
1870# if _LIBCPP_HAS_EXCEPTIONS
1871 } catch (...) {
1872 if (__policy == launch::async)
1873 throw;
1874 }
1875# endif
1876
1877 if (__does_policy_contain(__policy, value: launch::deferred))
1878 return std::__make_deferred_assoc_state<_Rp>(_BF(std::forward<_Fp>(__f), std::forward<_Args>(__args)...));
1879 return future<_Rp>{};
1880}
1881
1882template <class _Fp, class... _Args>
1883[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI future<__invoke_result_t<__decay_t<_Fp>, __decay_t<_Args>...> >
1884async(_Fp&& __f, _Args&&... __args) {
1885 return std::async(launch::async | launch::deferred, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);
1886}
1887
1888# endif // C++03
1889
1890// shared_future
1891
1892template <class _Rp>
1893class shared_future {
1894 __assoc_state<_Rp>* __state_;
1895
1896public:
1897 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1898 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1899 if (__state_)
1900 __state_->__add_shared();
1901 }
1902 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1903 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1904 __rhs.__state_ = nullptr;
1905 }
1906 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1907 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
1908 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1909 shared_future(std::move(__rhs)).swap(rhs&: *this);
1910 return *this;
1911 }
1912
1913 // retrieving the value
1914 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }
1915
1916 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1917
1918 // functions to check state
1919 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1920
1921 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1922 template <class _Rep, class _Period>
1923 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1924 return __state_->wait_for(__rel_time);
1925 }
1926 template <class _Clock, class _Duration>
1927 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1928 return __state_->wait_until(__abs_time);
1929 }
1930};
1931
1932template <class _Rp>
1933shared_future<_Rp>::~shared_future() {
1934 if (__state_)
1935 __state_->__release_shared();
1936}
1937
1938template <class _Rp>
1939shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {
1940 if (__rhs.__state_)
1941 __rhs.__state_->__add_shared();
1942 if (__state_)
1943 __state_->__release_shared();
1944 __state_ = __rhs.__state_;
1945 return *this;
1946}
1947
1948template <class _Rp>
1949class shared_future<_Rp&> {
1950 __assoc_state<_Rp&>* __state_;
1951
1952public:
1953 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
1954 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
1955 if (__state_)
1956 __state_->__add_shared();
1957 }
1958 _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
1959 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
1960 __rhs.__state_ = nullptr;
1961 }
1962 _LIBCPP_HIDE_FROM_ABI ~shared_future();
1963 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
1964 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
1965 shared_future(std::move(__rhs)).swap(rhs&: *this);
1966 return *this;
1967 }
1968
1969 // retrieving the value
1970 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }
1971
1972 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
1973
1974 // functions to check state
1975 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
1976
1977 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
1978 template <class _Rep, class _Period>
1979 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
1980 return __state_->wait_for(__rel_time);
1981 }
1982 template <class _Clock, class _Duration>
1983 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
1984 return __state_->wait_until(__abs_time);
1985 }
1986};
1987
1988template <class _Rp>
1989shared_future<_Rp&>::~shared_future() {
1990 if (__state_)
1991 __state_->__release_shared();
1992}
1993
1994template <class _Rp>
1995shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {
1996 if (__rhs.__state_)
1997 __rhs.__state_->__add_shared();
1998 if (__state_)
1999 __state_->__release_shared();
2000 __state_ = __rhs.__state_;
2001 return *this;
2002}
2003
2004template <>
2005class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
2006 __assoc_sub_state* __state_;
2007
2008public:
2009 _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
2010 _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
2011 if (__state_)
2012 __state_->__add_shared();
2013 }
2014 _LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
2015 _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
2016 __rhs.__state_ = nullptr;
2017 }
2018 ~shared_future();
2019 shared_future& operator=(const shared_future& __rhs);
2020 _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
2021 shared_future(std::move(__rhs)).swap(rhs&: *this);
2022 return *this;
2023 }
2024
2025 // retrieving the value
2026 _LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }
2027
2028 _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(x&: __state_, y&: __rhs.__state_); }
2029
2030 // functions to check state
2031 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
2032
2033 _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
2034 template <class _Rep, class _Period>
2035 _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
2036 return __state_->wait_for(__rel_time);
2037 }
2038 template <class _Clock, class _Duration>
2039 _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
2040 return __state_->wait_until(__abs_time);
2041 }
2042};
2043
2044template <class _Rp>
2045inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
2046 __x.swap(__y);
2047}
2048
2049template <class _Rp>
2050inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
2051 return shared_future<_Rp>(std::move(*this));
2052}
2053
2054template <class _Rp>
2055inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
2056 return shared_future<_Rp&>(std::move(*this));
2057}
2058
2059inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
2060
2061_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
2062_LIBCPP_END_NAMESPACE_STD
2063
2064_LIBCPP_POP_MACROS
2065
2066# endif // _LIBCPP_HAS_THREADS
2067
2068# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 17
2069# include <chrono>
2070# endif
2071
2072# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 20
2073# include <atomic>
2074# include <cstdlib>
2075# include <exception>
2076# include <iosfwd>
2077# include <system_error>
2078# include <thread>
2079# endif
2080#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
2081
2082#endif // _LIBCPP_FUTURE
2083