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