Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 1 | //===------------------------- future.cpp ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 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: |
| 19 | virtual const char* name() const; |
| 20 | virtual string message(int ev) const; |
| 21 | }; |
| 22 | |
| 23 | const char* |
| 24 | __future_error_category::name() const |
| 25 | { |
| 26 | return "future"; |
| 27 | } |
| 28 | |
| 29 | string |
| 30 | __future_error_category::message(int ev) const |
| 31 | { |
| 32 | switch (ev) |
| 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 | |
| 49 | |
| 50 | const error_category& |
| 51 | future_category() |
| 52 | { |
| 53 | static __future_error_category __f; |
| 54 | return __f; |
| 55 | } |
| 56 | |
| 57 | future_error::future_error(error_code __ec) |
| 58 | : logic_error(__ec.message()), |
| 59 | __ec_(__ec) |
| 60 | { |
| 61 | } |
| 62 | |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 63 | void |
| 64 | __assoc_sub_state::__on_zero_shared() |
| 65 | { |
| 66 | delete this; |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | __assoc_sub_state::set_value() |
| 71 | { |
| 72 | unique_lock<mutex> __lk(__mut_); |
| 73 | if (__has_value()) |
| 74 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 75 | __state_ |= __constructed | ready; |
| 76 | __lk.unlock(); |
| 77 | __cv_.notify_all(); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | __assoc_sub_state::set_value_at_thread_exit() |
| 82 | { |
| 83 | unique_lock<mutex> __lk(__mut_); |
| 84 | if (__has_value()) |
| 85 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 86 | __state_ |= __constructed; |
| 87 | __thread_local_data->__make_ready_at_thread_exit(this); |
| 88 | __lk.unlock(); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | __assoc_sub_state::set_exception(exception_ptr __p) |
| 93 | { |
| 94 | unique_lock<mutex> __lk(__mut_); |
| 95 | if (__has_value()) |
| 96 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 97 | __exception_ = __p; |
| 98 | __state_ |= ready; |
| 99 | __lk.unlock(); |
| 100 | __cv_.notify_all(); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p) |
| 105 | { |
| 106 | unique_lock<mutex> __lk(__mut_); |
| 107 | if (__has_value()) |
| 108 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 109 | __exception_ = __p; |
| 110 | __thread_local_data->__make_ready_at_thread_exit(this); |
| 111 | __lk.unlock(); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | __assoc_sub_state::__make_ready() |
| 116 | { |
| 117 | unique_lock<mutex> __lk(__mut_); |
| 118 | __state_ |= ready; |
| 119 | __lk.unlock(); |
| 120 | __cv_.notify_all(); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | __assoc_sub_state::copy() |
| 125 | { |
| 126 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 | [diff] [blame] | 127 | __sub_wait(__lk); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 128 | if (__exception_ != nullptr) |
| 129 | rethrow_exception(__exception_); |
| 130 | } |
| 131 | |
| 132 | void |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 | [diff] [blame] | 133 | __assoc_sub_state::wait() |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 134 | { |
| 135 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 | [diff] [blame] | 136 | __sub_wait(__lk); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk) |
| 141 | { |
| 142 | if (!__is_ready()) |
| 143 | { |
| 144 | if (__state_ & deferred) |
| 145 | { |
| 146 | __state_ &= ~deferred; |
| 147 | __lk.unlock(); |
| 148 | __execute(); |
| 149 | } |
| 150 | else |
| 151 | while (!__is_ready()) |
| 152 | __cv_.wait(__lk); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | __assoc_sub_state::__execute() |
| 158 | { |
| 159 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | future<void>::future(__assoc_sub_state* __state) |
| 163 | : __state_(__state) |
| 164 | { |
| 165 | if (__state_->__has_future_attached()) |
| 166 | throw future_error(make_error_code(future_errc::future_already_retrieved)); |
| 167 | __state_->__add_shared(); |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 | [diff] [blame] | 168 | __state_->__set_future_attached(); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | future<void>::~future() |
| 172 | { |
| 173 | if (__state_) |
| 174 | __state_->__release_shared(); |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | future<void>::get() |
| 179 | { |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 | [diff] [blame] | 180 | unique_ptr<__shared_count, __release_shared_count> __(__state_); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 181 | __assoc_sub_state* __s = __state_; |
| 182 | __state_ = nullptr; |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 | [diff] [blame] | 183 | __s->copy(); |
Howard Hinnant | 167fd10 | 2010-08-27 20:10:19 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | promise<void>::promise() |
| 187 | : __state_(new __assoc_sub_state) |
| 188 | { |
| 189 | } |
| 190 | |
| 191 | promise<void>::~promise() |
| 192 | { |
| 193 | if (__state_) |
| 194 | { |
| 195 | if (!__state_->__has_value() && __state_->use_count() > 1) |
| 196 | __state_->set_exception(make_exception_ptr( |
| 197 | future_error(make_error_code(future_errc::broken_promise)) |
| 198 | )); |
| 199 | __state_->__release_shared(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | future<void> |
| 204 | promise<void>::get_future() |
| 205 | { |
| 206 | if (__state_ == nullptr) |
| 207 | throw future_error(make_error_code(future_errc::no_state)); |
| 208 | return future<void>(__state_); |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | promise<void>::set_value() |
| 213 | { |
| 214 | if (__state_ == nullptr) |
| 215 | throw future_error(make_error_code(future_errc::no_state)); |
| 216 | __state_->set_value(); |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | promise<void>::set_exception(exception_ptr __p) |
| 221 | { |
| 222 | if (__state_ == nullptr) |
| 223 | throw future_error(make_error_code(future_errc::no_state)); |
| 224 | __state_->set_exception(__p); |
| 225 | } |
| 226 | |
| 227 | void |
| 228 | promise<void>::set_value_at_thread_exit() |
| 229 | { |
| 230 | if (__state_ == nullptr) |
| 231 | throw future_error(make_error_code(future_errc::no_state)); |
| 232 | __state_->set_value_at_thread_exit(); |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | promise<void>::set_exception_at_thread_exit(exception_ptr __p) |
| 237 | { |
| 238 | if (__state_ == nullptr) |
| 239 | throw future_error(make_error_code(future_errc::no_state)); |
| 240 | __state_->set_exception_at_thread_exit(__p); |
| 241 | } |
| 242 | |
Howard Hinnant | ead8550 | 2010-09-03 18:39:25 | [diff] [blame^] | 243 | shared_future<void>::~shared_future() |
| 244 | { |
| 245 | if (__state_) |
| 246 | __state_->__release_shared(); |
| 247 | } |
| 248 | |
| 249 | shared_future<void>& |
| 250 | shared_future<void>::operator=(const shared_future& __rhs) |
| 251 | { |
| 252 | if (__rhs.__state_) |
| 253 | __rhs.__state_->__add_shared(); |
| 254 | if (__state_) |
| 255 | __state_->__release_shared(); |
| 256 | __state_ = __rhs.__state_; |
| 257 | return *this; |
| 258 | } |
| 259 | |
Howard Hinnant | dae3481 | 2010-08-25 17:32:05 | [diff] [blame] | 260 | _LIBCPP_END_NAMESPACE_STD |