Louis Dionne | eb8650a | 2021-11-17 21:25:01 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 9 | #include <future> |
| 10 | #include <string> |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 11 | |
| 12 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 13 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 14 | class _LIBCPP_HIDDEN __future_error_category : public __do_message { |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 15 | public: |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 16 | virtual const char* name() const noexcept; |
| 17 | virtual string message(int ev) const; |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 18 | }; |
| 19 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 20 | const char* __future_error_category::name() const noexcept { return "future"; } |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 21 | |
Nikolas Klauser | a7c2a62 | 2022-02-14 17:52:28 | [diff] [blame] | 22 | _LIBCPP_DIAGNOSTIC_PUSH |
| 23 | _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wswitch") |
| 24 | _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wswitch") |
Howard Hinnant | 3b2d7ee | 2013-09-14 18:20:10 | [diff] [blame] | 25 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 26 | string __future_error_category::message(int ev) const { |
| 27 | switch (static_cast<future_errc>(ev)) { |
| 28 | case future_errc(0): // For backwards compatibility with C++11 (LWG 2056) |
| 29 | case future_errc::broken_promise: |
| 30 | return string("The associated promise has been destructed prior " |
| 31 | "to the associated state becoming ready."); |
| 32 | case future_errc::future_already_retrieved: |
| 33 | return string("The future has already been retrieved from " |
| 34 | "the promise or packaged_task."); |
| 35 | case future_errc::promise_already_satisfied: |
| 36 | return string("The state of the promise has already been set."); |
| 37 | case future_errc::no_state: |
| 38 | return string("Operation not permitted on an object without " |
| 39 | "an associated state."); |
| 40 | } |
| 41 | return string("unspecified future_errc value\n"); |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 42 | } |
| 43 | |
Nikolas Klauser | a7c2a62 | 2022-02-14 17:52:28 | [diff] [blame] | 44 | _LIBCPP_DIAGNOSTIC_POP |
Howard Hinnant | 3b2d7ee | 2013-09-14 18:20:10 | [diff] [blame] | 45 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 46 | const error_category& future_category() noexcept { |
| 47 | union AvoidDestroyingFutureCategory { |
| 48 | __future_error_category future_error_category; |
| 49 | constexpr explicit AvoidDestroyingFutureCategory() : future_error_category() {} |
| 50 | ~AvoidDestroyingFutureCategory() {} |
| 51 | }; |
| 52 | constinit static AvoidDestroyingFutureCategory helper; |
| 53 | return helper.future_error_category; |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 54 | } |
| 55 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 56 | future_error::future_error(error_code __ec) : logic_error(__ec.message()), __ec_(__ec) {} |
| 57 | |
| 58 | future_error::~future_error() noexcept {} |
| 59 | |
| 60 | void __assoc_sub_state::__on_zero_shared() noexcept { delete this; } |
| 61 | |
| 62 | void __assoc_sub_state::set_value() { |
| 63 | unique_lock<mutex> __lk(__mut_); |
| 64 | if (__has_value()) |
| 65 | __throw_future_error(future_errc::promise_already_satisfied); |
| 66 | __state_ |= __constructed | ready; |
| 67 | __cv_.notify_all(); |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 68 | } |
| 69 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 70 | void __assoc_sub_state::set_value_at_thread_exit() { |
| 71 | unique_lock<mutex> __lk(__mut_); |
| 72 | if (__has_value()) |
| 73 | __throw_future_error(future_errc::promise_already_satisfied); |
| 74 | __state_ |= __constructed; |
| 75 | __thread_local_data()->__make_ready_at_thread_exit(this); |
Howard Hinnant | 3aa229f | 2011-07-08 00:04:40 | [diff] [blame] | 76 | } |
| 77 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 78 | void __assoc_sub_state::set_exception(exception_ptr __p) { |
| 79 | unique_lock<mutex> __lk(__mut_); |
| 80 | if (__has_value()) |
| 81 | __throw_future_error(future_errc::promise_already_satisfied); |
| 82 | __exception_ = __p; |
| 83 | __state_ |= ready; |
| 84 | __cv_.notify_all(); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 85 | } |
| 86 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 87 | void __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p) { |
| 88 | unique_lock<mutex> __lk(__mut_); |
| 89 | if (__has_value()) |
| 90 | __throw_future_error(future_errc::promise_already_satisfied); |
| 91 | __exception_ = __p; |
| 92 | __thread_local_data()->__make_ready_at_thread_exit(this); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 93 | } |
| 94 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 95 | void __assoc_sub_state::__make_ready() { |
| 96 | unique_lock<mutex> __lk(__mut_); |
| 97 | __state_ |= ready; |
| 98 | __cv_.notify_all(); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 99 | } |
| 100 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 101 | void __assoc_sub_state::copy() { |
| 102 | unique_lock<mutex> __lk(__mut_); |
| 103 | __sub_wait(__lk); |
| 104 | if (__exception_ != nullptr) |
| 105 | rethrow_exception(__exception_); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 106 | } |
| 107 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 108 | void __assoc_sub_state::wait() { |
| 109 | unique_lock<mutex> __lk(__mut_); |
| 110 | __sub_wait(__lk); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 111 | } |
| 112 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 113 | void __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk) { |
| 114 | if (!__is_ready()) { |
| 115 | if (__state_ & static_cast<unsigned>(deferred)) { |
| 116 | __state_ &= ~static_cast<unsigned>(deferred); |
| 117 | __lk.unlock(); |
| 118 | __execute(); |
| 119 | } else |
| 120 | while (!__is_ready()) |
| 121 | __cv_.wait(__lk); |
| 122 | } |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 123 | } |
| 124 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 125 | void __assoc_sub_state::__execute() { __throw_future_error(future_errc::no_state); } |
| 126 | |
| 127 | future<void>::future(__assoc_sub_state* __state) : __state_(__state) { __state_->__attach_future(); } |
| 128 | |
| 129 | future<void>::~future() { |
| 130 | if (__state_) |
| 131 | __state_->__release_shared(); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 132 | } |
| 133 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 134 | void future<void>::get() { |
| 135 | unique_ptr<__shared_count, __release_shared_count> __(__state_); |
| 136 | __assoc_sub_state* __s = __state_; |
| 137 | __state_ = nullptr; |
| 138 | __s->copy(); |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 | [diff] [blame] | 139 | } |
| 140 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 141 | promise<void>::promise() : __state_(new __assoc_sub_state) {} |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 | [diff] [blame] | 142 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 143 | promise<void>::~promise() { |
| 144 | if (__state_) { |
Nikolas Klauser | ba87515 | 2024-10-12 07:49:52 | [diff] [blame^] | 145 | #if _LIBCPP_HAS_EXCEPTIONS |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 146 | if (!__state_->__has_value() && __state_->use_count() > 1) |
| 147 | __state_->set_exception(make_exception_ptr(future_error(future_errc::broken_promise))); |
Nikolas Klauser | ba87515 | 2024-10-12 07:49:52 | [diff] [blame^] | 148 | #endif // _LIBCPP_HAS_EXCEPTIONS |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 149 | __state_->__release_shared(); |
| 150 | } |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 151 | } |
| 152 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 153 | future<void> promise<void>::get_future() { |
| 154 | if (__state_ == nullptr) |
| 155 | __throw_future_error(future_errc::no_state); |
| 156 | return future<void>(__state_); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 157 | } |
| 158 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 159 | void promise<void>::set_value() { |
| 160 | if (__state_ == nullptr) |
| 161 | __throw_future_error(future_errc::no_state); |
| 162 | __state_->set_value(); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 163 | } |
| 164 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 165 | void promise<void>::set_exception(exception_ptr __p) { |
| 166 | if (__state_ == nullptr) |
| 167 | __throw_future_error(future_errc::no_state); |
| 168 | __state_->set_exception(__p); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 169 | } |
| 170 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 171 | void promise<void>::set_value_at_thread_exit() { |
| 172 | if (__state_ == nullptr) |
| 173 | __throw_future_error(future_errc::no_state); |
| 174 | __state_->set_value_at_thread_exit(); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 175 | } |
| 176 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 177 | void promise<void>::set_exception_at_thread_exit(exception_ptr __p) { |
| 178 | if (__state_ == nullptr) |
| 179 | __throw_future_error(future_errc::no_state); |
| 180 | __state_->set_exception_at_thread_exit(__p); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 181 | } |
| 182 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 183 | shared_future<void>::~shared_future() { |
| 184 | if (__state_) |
| 185 | __state_->__release_shared(); |
Howard Hinnant | ead8550 | 2010-09-03 18:39:25 | [diff] [blame] | 186 | } |
| 187 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 188 | shared_future<void>& shared_future<void>::operator=(const shared_future& __rhs) { |
| 189 | if (__rhs.__state_) |
| 190 | __rhs.__state_->__add_shared(); |
| 191 | if (__state_) |
| 192 | __state_->__release_shared(); |
| 193 | __state_ = __rhs.__state_; |
| 194 | return *this; |
Howard Hinnant | ead8550 | 2010-09-03 18:39:25 | [diff] [blame] | 195 | } |
| 196 | |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 197 | _LIBCPP_END_NAMESPACE_STD |