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