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