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