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