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