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