Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 1 | //===------------------------- future.cpp ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "future" |
| 11 | #include "string" |
| 12 | |
| 13 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 14 | |
| 15 | class _LIBCPP_HIDDEN __future_error_category |
| 16 | : public __do_message |
| 17 | { |
| 18 | public: |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 19 | virtual const char* name() const _NOEXCEPT; |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 20 | virtual string message(int ev) const; |
| 21 | }; |
| 22 | |
| 23 | const char* |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 24 | __future_error_category::name() const _NOEXCEPT |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 25 | { |
| 26 | return "future"; |
| 27 | } |
| 28 | |
| 29 | string |
| 30 | __future_error_category::message(int ev) const |
| 31 | { |
Howard Hinnant | b1bc0c4 | 2012-02-02 20:31:36 +0000 | [diff] [blame] | 32 | switch (static_cast<future_errc>(ev)) |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 33 | { |
| 34 | case future_errc::broken_promise: |
| 35 | return string("The associated promise has been destructed prior " |
| 36 | "to the associated state becoming ready."); |
| 37 | case future_errc::future_already_retrieved: |
| 38 | return string("The future has already been retrieved from " |
| 39 | "the promise or packaged_task."); |
| 40 | case future_errc::promise_already_satisfied: |
| 41 | return string("The state of the promise has already been set."); |
| 42 | case future_errc::no_state: |
| 43 | return string("Operation not permitted on an object without " |
| 44 | "an associated state."); |
| 45 | } |
| 46 | return string("unspecified future_errc value\n"); |
| 47 | } |
| 48 | |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 49 | const error_category& |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 50 | future_category() _NOEXCEPT |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 51 | { |
| 52 | static __future_error_category __f; |
| 53 | return __f; |
| 54 | } |
| 55 | |
| 56 | future_error::future_error(error_code __ec) |
| 57 | : logic_error(__ec.message()), |
| 58 | __ec_(__ec) |
| 59 | { |
| 60 | } |
| 61 | |
Howard Hinnant | 043fe1d | 2011-07-08 00:04:40 +0000 | [diff] [blame] | 62 | future_error::~future_error() _NOEXCEPT |
| 63 | { |
| 64 | } |
| 65 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 66 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 67 | __assoc_sub_state::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 68 | { |
| 69 | delete this; |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | __assoc_sub_state::set_value() |
| 74 | { |
| 75 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 76 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 77 | if (__has_value()) |
| 78 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 79 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 80 | __state_ |= __constructed | ready; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 81 | __cv_.notify_all(); |
Howard Hinnant | 1b031c9 | 2013-01-14 20:01:24 +0000 | [diff] [blame] | 82 | __lk.unlock(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void |
| 86 | __assoc_sub_state::set_value_at_thread_exit() |
| 87 | { |
| 88 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 89 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 90 | if (__has_value()) |
| 91 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 92 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 93 | __state_ |= __constructed; |
Howard Hinnant | 5306d68 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 94 | __thread_local_data()->__make_ready_at_thread_exit(this); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 95 | __lk.unlock(); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | __assoc_sub_state::set_exception(exception_ptr __p) |
| 100 | { |
| 101 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 102 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 103 | if (__has_value()) |
| 104 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 105 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 106 | __exception_ = __p; |
| 107 | __state_ |= ready; |
| 108 | __lk.unlock(); |
| 109 | __cv_.notify_all(); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p) |
| 114 | { |
| 115 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 116 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 117 | if (__has_value()) |
| 118 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 119 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 120 | __exception_ = __p; |
Howard Hinnant | 5306d68 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 121 | __thread_local_data()->__make_ready_at_thread_exit(this); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 122 | __lk.unlock(); |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | __assoc_sub_state::__make_ready() |
| 127 | { |
| 128 | unique_lock<mutex> __lk(__mut_); |
| 129 | __state_ |= ready; |
| 130 | __lk.unlock(); |
| 131 | __cv_.notify_all(); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | __assoc_sub_state::copy() |
| 136 | { |
| 137 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 138 | __sub_wait(__lk); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 139 | if (__exception_ != nullptr) |
| 140 | rethrow_exception(__exception_); |
| 141 | } |
| 142 | |
| 143 | void |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 144 | __assoc_sub_state::wait() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 145 | { |
| 146 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 147 | __sub_wait(__lk); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk) |
| 152 | { |
| 153 | if (!__is_ready()) |
| 154 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 155 | if (__state_ & static_cast<unsigned>(deferred)) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 156 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 157 | __state_ &= ~static_cast<unsigned>(deferred); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 158 | __lk.unlock(); |
| 159 | __execute(); |
| 160 | } |
| 161 | else |
| 162 | while (!__is_ready()) |
| 163 | __cv_.wait(__lk); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | __assoc_sub_state::__execute() |
| 169 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 170 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 171 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 172 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | future<void>::future(__assoc_sub_state* __state) |
| 176 | : __state_(__state) |
| 177 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 178 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 179 | if (__state_->__has_future_attached()) |
| 180 | throw future_error(make_error_code(future_errc::future_already_retrieved)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 181 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 182 | __state_->__add_shared(); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 183 | __state_->__set_future_attached(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | future<void>::~future() |
| 187 | { |
| 188 | if (__state_) |
| 189 | __state_->__release_shared(); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | future<void>::get() |
| 194 | { |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 195 | unique_ptr<__shared_count, __release_shared_count> __(__state_); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 196 | __assoc_sub_state* __s = __state_; |
| 197 | __state_ = nullptr; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 198 | __s->copy(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | promise<void>::promise() |
| 202 | : __state_(new __assoc_sub_state) |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | promise<void>::~promise() |
| 207 | { |
| 208 | if (__state_) |
| 209 | { |
| 210 | if (!__state_->__has_value() && __state_->use_count() > 1) |
| 211 | __state_->set_exception(make_exception_ptr( |
| 212 | future_error(make_error_code(future_errc::broken_promise)) |
| 213 | )); |
| 214 | __state_->__release_shared(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | future<void> |
| 219 | promise<void>::get_future() |
| 220 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 221 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 222 | if (__state_ == nullptr) |
| 223 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 224 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 225 | return future<void>(__state_); |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | promise<void>::set_value() |
| 230 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 231 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 232 | if (__state_ == nullptr) |
| 233 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 234 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 235 | __state_->set_value(); |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | promise<void>::set_exception(exception_ptr __p) |
| 240 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 241 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 242 | if (__state_ == nullptr) |
| 243 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 244 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 245 | __state_->set_exception(__p); |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | promise<void>::set_value_at_thread_exit() |
| 250 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 251 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 252 | if (__state_ == nullptr) |
| 253 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 254 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 255 | __state_->set_value_at_thread_exit(); |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | promise<void>::set_exception_at_thread_exit(exception_ptr __p) |
| 260 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 261 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 262 | if (__state_ == nullptr) |
| 263 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 264 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 265 | __state_->set_exception_at_thread_exit(__p); |
| 266 | } |
| 267 | |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 268 | shared_future<void>::~shared_future() |
| 269 | { |
| 270 | if (__state_) |
| 271 | __state_->__release_shared(); |
| 272 | } |
| 273 | |
| 274 | shared_future<void>& |
| 275 | shared_future<void>::operator=(const shared_future& __rhs) |
| 276 | { |
| 277 | if (__rhs.__state_) |
| 278 | __rhs.__state_->__add_shared(); |
| 279 | if (__state_) |
| 280 | __state_->__release_shared(); |
| 281 | __state_ = __rhs.__state_; |
| 282 | return *this; |
| 283 | } |
| 284 | |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 285 | _LIBCPP_END_NAMESPACE_STD |