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