Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- future -----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_FUTURE |
| 12 | #define _LIBCPP_FUTURE |
| 13 | |
| 14 | /* |
| 15 | future synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | enum class future_errc |
| 21 | { |
| 22 | broken_promise, |
| 23 | future_already_retrieved, |
| 24 | promise_already_satisfied, |
| 25 | no_state |
| 26 | }; |
| 27 | |
| 28 | enum class launch |
| 29 | { |
Howard Hinnant | 6689564 | 2010-11-23 18:33:54 +0000 | [diff] [blame] | 30 | async = 1, |
| 31 | deferred = 2, |
| 32 | any = async | deferred |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | enum class future_status |
| 36 | { |
| 37 | ready, |
| 38 | timeout, |
| 39 | deferred |
| 40 | }; |
| 41 | |
| 42 | template <> struct is_error_code_enum<future_errc> : public true_type { }; |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 43 | error_code make_error_code(future_errc e) noexcept; |
| 44 | error_condition make_error_condition(future_errc e) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 46 | const error_category& future_category() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 | |
| 48 | class future_error |
| 49 | : public logic_error |
| 50 | { |
| 51 | public: |
| 52 | future_error(error_code ec); // exposition only |
| 53 | |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 54 | const error_code& code() const noexcept; |
| 55 | const char* what() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | template <class R> |
| 59 | class promise |
| 60 | { |
| 61 | public: |
| 62 | promise(); |
| 63 | template <class Allocator> |
| 64 | promise(allocator_arg_t, const Allocator& a); |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 65 | promise(promise&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 66 | promise(const promise& rhs) = delete; |
| 67 | ~promise(); |
| 68 | |
| 69 | // assignment |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 70 | promise& operator=(promise&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 71 | promise& operator=(const promise& rhs) = delete; |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 72 | void swap(promise& other) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 73 | |
| 74 | // retrieving the result |
| 75 | future<R> get_future(); |
| 76 | |
| 77 | // setting the result |
| 78 | void set_value(const R& r); |
| 79 | void set_value(R&& r); |
| 80 | void set_exception(exception_ptr p); |
| 81 | |
| 82 | // setting the result with deferred notification |
| 83 | void set_value_at_thread_exit(const R& r); |
| 84 | void set_value_at_thread_exit(R&& r); |
| 85 | void set_exception_at_thread_exit(exception_ptr p); |
| 86 | }; |
| 87 | |
| 88 | template <class R> |
| 89 | class promise<R&> |
| 90 | { |
| 91 | public: |
| 92 | promise(); |
| 93 | template <class Allocator> |
| 94 | promise(allocator_arg_t, const Allocator& a); |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 95 | promise(promise&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 96 | promise(const promise& rhs) = delete; |
| 97 | ~promise(); |
| 98 | |
| 99 | // assignment |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 100 | promise& operator=(promise&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 101 | promise& operator=(const promise& rhs) = delete; |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 102 | void swap(promise& other) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 103 | |
| 104 | // retrieving the result |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 105 | future<R&> get_future(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 106 | |
| 107 | // setting the result |
| 108 | void set_value(R& r); |
| 109 | void set_exception(exception_ptr p); |
| 110 | |
| 111 | // setting the result with deferred notification |
| 112 | void set_value_at_thread_exit(R&); |
| 113 | void set_exception_at_thread_exit(exception_ptr p); |
| 114 | }; |
| 115 | |
| 116 | template <> |
| 117 | class promise<void> |
| 118 | { |
| 119 | public: |
| 120 | promise(); |
| 121 | template <class Allocator> |
| 122 | promise(allocator_arg_t, const Allocator& a); |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 123 | promise(promise&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 124 | promise(const promise& rhs) = delete; |
| 125 | ~promise(); |
| 126 | |
| 127 | // assignment |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 128 | promise& operator=(promise&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 129 | promise& operator=(const promise& rhs) = delete; |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 130 | void swap(promise& other) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 131 | |
| 132 | // retrieving the result |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 133 | future<void> get_future(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | |
| 135 | // setting the result |
| 136 | void set_value(); |
| 137 | void set_exception(exception_ptr p); |
| 138 | |
| 139 | // setting the result with deferred notification |
| 140 | void set_value_at_thread_exit(); |
| 141 | void set_exception_at_thread_exit(exception_ptr p); |
| 142 | }; |
| 143 | |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 144 | template <class R> void swap(promise<R>& x, promise<R>& y) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 145 | |
| 146 | template <class R, class Alloc> |
| 147 | struct uses_allocator<promise<R>, Alloc> : public true_type {}; |
| 148 | |
| 149 | template <class R> |
| 150 | class future |
| 151 | { |
| 152 | public: |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 153 | future() noexcept; |
| 154 | future(future&&) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 155 | future(const future& rhs) = delete; |
| 156 | ~future(); |
| 157 | future& operator=(const future& rhs) = delete; |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 158 | future& operator=(future&&) noexcept; |
| 159 | shared_future<R> share(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 160 | |
| 161 | // retrieving the value |
| 162 | R get(); |
| 163 | |
| 164 | // functions to check state |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 165 | bool valid() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 166 | |
| 167 | void wait() const; |
| 168 | template <class Rep, class Period> |
| 169 | future_status |
| 170 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 171 | template <class Clock, class Duration> |
| 172 | future_status |
| 173 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 174 | }; |
| 175 | |
| 176 | template <class R> |
| 177 | class future<R&> |
| 178 | { |
| 179 | public: |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 180 | future() noexcept; |
| 181 | future(future&&) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 182 | future(const future& rhs) = delete; |
| 183 | ~future(); |
| 184 | future& operator=(const future& rhs) = delete; |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 185 | future& operator=(future&&) noexcept; |
| 186 | shared_future<R&> share(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 187 | |
| 188 | // retrieving the value |
| 189 | R& get(); |
| 190 | |
| 191 | // functions to check state |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 192 | bool valid() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 193 | |
| 194 | void wait() const; |
| 195 | template <class Rep, class Period> |
| 196 | future_status |
| 197 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 198 | template <class Clock, class Duration> |
| 199 | future_status |
| 200 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 201 | }; |
| 202 | |
| 203 | template <> |
| 204 | class future<void> |
| 205 | { |
| 206 | public: |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 207 | future() noexcept; |
| 208 | future(future&&) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 209 | future(const future& rhs) = delete; |
| 210 | ~future(); |
| 211 | future& operator=(const future& rhs) = delete; |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 212 | future& operator=(future&&) noexcept; |
| 213 | shared_future<void> share(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 214 | |
| 215 | // retrieving the value |
| 216 | void get(); |
| 217 | |
| 218 | // functions to check state |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 219 | bool valid() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 220 | |
| 221 | void wait() const; |
| 222 | template <class Rep, class Period> |
| 223 | future_status |
| 224 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 225 | template <class Clock, class Duration> |
| 226 | future_status |
| 227 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 228 | }; |
| 229 | |
| 230 | template <class R> |
| 231 | class shared_future |
| 232 | { |
| 233 | public: |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 234 | shared_future() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 235 | shared_future(const shared_future& rhs); |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 236 | shared_future(future<R>&&) noexcept; |
| 237 | shared_future(shared_future&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 238 | ~shared_future(); |
| 239 | shared_future& operator=(const shared_future& rhs); |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 240 | shared_future& operator=(shared_future&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 241 | |
| 242 | // retrieving the value |
| 243 | const R& get() const; |
| 244 | |
| 245 | // functions to check state |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 246 | bool valid() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 247 | |
| 248 | void wait() const; |
| 249 | template <class Rep, class Period> |
| 250 | future_status |
| 251 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 252 | template <class Clock, class Duration> |
| 253 | future_status |
| 254 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 255 | }; |
| 256 | |
| 257 | template <class R> |
| 258 | class shared_future<R&> |
| 259 | { |
| 260 | public: |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 261 | shared_future() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 262 | shared_future(const shared_future& rhs); |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 263 | shared_future(future<R&>&&) noexcept; |
| 264 | shared_future(shared_future&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 265 | ~shared_future(); |
| 266 | shared_future& operator=(const shared_future& rhs); |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 267 | shared_future& operator=(shared_future&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 268 | |
| 269 | // retrieving the value |
| 270 | R& get() const; |
| 271 | |
| 272 | // functions to check state |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 273 | bool valid() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 274 | |
| 275 | void wait() const; |
| 276 | template <class Rep, class Period> |
| 277 | future_status |
| 278 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 279 | template <class Clock, class Duration> |
| 280 | future_status |
| 281 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 282 | }; |
| 283 | |
| 284 | template <> |
| 285 | class shared_future<void> |
| 286 | { |
| 287 | public: |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 288 | shared_future() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 289 | shared_future(const shared_future& rhs); |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 290 | shared_future(future<void>&&) noexcept; |
| 291 | shared_future(shared_future&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 292 | ~shared_future(); |
| 293 | shared_future& operator=(const shared_future& rhs); |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 294 | shared_future& operator=(shared_future&& rhs) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 295 | |
| 296 | // retrieving the value |
| 297 | void get() const; |
| 298 | |
| 299 | // functions to check state |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 300 | bool valid() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 301 | |
| 302 | void wait() const; |
| 303 | template <class Rep, class Period> |
| 304 | future_status |
| 305 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 306 | template <class Clock, class Duration> |
| 307 | future_status |
| 308 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 309 | }; |
| 310 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 311 | template <class F, class... Args> |
| 312 | future<typename result_of<F(Args...)>::type> |
| 313 | async(F&& f, Args&&... args); |
| 314 | |
| 315 | template <class F, class... Args> |
| 316 | future<typename result_of<F(Args...)>::type> |
| 317 | async(launch policy, F&& f, Args&&... args); |
| 318 | |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 319 | template <class> class packaged_task; // undefined |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 320 | |
| 321 | template <class R, class... ArgTypes> |
| 322 | class packaged_task<R(ArgTypes...)> |
| 323 | { |
| 324 | public: |
| 325 | typedef R result_type; |
| 326 | |
| 327 | // construction and destruction |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 328 | packaged_task() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 329 | template <class F> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 330 | explicit packaged_task(F&& f); |
| 331 | template <class F, class Allocator> |
| 332 | explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f); |
| 333 | ~packaged_task(); |
| 334 | |
| 335 | // no copy |
Howard Hinnant | 8131a01 | 2012-07-21 19:34:12 +0000 | [diff] [blame] | 336 | packaged_task(const packaged_task&) = delete; |
| 337 | packaged_task& operator=(const packaged_task&) = delete; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 338 | |
| 339 | // move support |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 340 | packaged_task(packaged_task&& other) noexcept; |
| 341 | packaged_task& operator=(packaged_task&& other) noexcept; |
| 342 | void swap(packaged_task& other) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 343 | |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 344 | bool valid() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 345 | |
| 346 | // result retrieval |
| 347 | future<R> get_future(); |
| 348 | |
| 349 | // execution |
| 350 | void operator()(ArgTypes... ); |
| 351 | void make_ready_at_thread_exit(ArgTypes...); |
| 352 | |
| 353 | void reset(); |
| 354 | }; |
| 355 | |
| 356 | template <class R> |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 357 | void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 358 | |
| 359 | template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>; |
| 360 | |
| 361 | } // std |
| 362 | |
| 363 | */ |
| 364 | |
| 365 | #include <__config> |
| 366 | #include <system_error> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 367 | #include <memory> |
| 368 | #include <chrono> |
| 369 | #include <exception> |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 370 | #include <mutex> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 371 | #include <thread> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 372 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 373 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 374 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 375 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 376 | |
| 377 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 378 | |
| 379 | //enum class future_errc |
Howard Hinnant | f6d875f | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 380 | _LIBCPP_DECLARE_STRONG_ENUM(future_errc) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 381 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 382 | broken_promise, |
| 383 | future_already_retrieved, |
| 384 | promise_already_satisfied, |
| 385 | no_state |
| 386 | }; |
Howard Hinnant | f6d875f | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 387 | _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 388 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 389 | template <> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 390 | struct _LIBCPP_TYPE_VIS is_error_code_enum<future_errc> : public true_type {}; |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 391 | |
Howard Hinnant | f6d875f | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 392 | #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS |
| 393 | template <> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 394 | struct _LIBCPP_TYPE_VIS is_error_code_enum<future_errc::__lx> : public true_type { }; |
Howard Hinnant | f6d875f | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 395 | #endif |
| 396 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 397 | //enum class launch |
Howard Hinnant | f6d875f | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 398 | _LIBCPP_DECLARE_STRONG_ENUM(launch) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 399 | { |
Howard Hinnant | 6689564 | 2010-11-23 18:33:54 +0000 | [diff] [blame] | 400 | async = 1, |
| 401 | deferred = 2, |
| 402 | any = async | deferred |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 403 | }; |
Howard Hinnant | f6d875f | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 404 | _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 405 | |
| 406 | //enum class future_status |
Howard Hinnant | f6d875f | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 407 | _LIBCPP_DECLARE_STRONG_ENUM(future_status) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 408 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 409 | ready, |
| 410 | timeout, |
| 411 | deferred |
| 412 | }; |
Howard Hinnant | f6d875f | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 413 | _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 414 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 415 | _LIBCPP_FUNC_VIS |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 416 | const error_category& future_category() _NOEXCEPT; |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 417 | |
| 418 | inline _LIBCPP_INLINE_VISIBILITY |
| 419 | error_code |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 420 | make_error_code(future_errc __e) _NOEXCEPT |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 421 | { |
| 422 | return error_code(static_cast<int>(__e), future_category()); |
| 423 | } |
| 424 | |
| 425 | inline _LIBCPP_INLINE_VISIBILITY |
| 426 | error_condition |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 427 | make_error_condition(future_errc __e) _NOEXCEPT |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 428 | { |
| 429 | return error_condition(static_cast<int>(__e), future_category()); |
| 430 | } |
| 431 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 432 | class _LIBCPP_EXCEPTION_ABI future_error |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 433 | : public logic_error |
| 434 | { |
| 435 | error_code __ec_; |
| 436 | public: |
| 437 | future_error(error_code __ec); |
| 438 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 439 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 440 | const error_code& code() const _NOEXCEPT {return __ec_;} |
Howard Hinnant | ac6de54 | 2011-07-07 21:03:52 +0000 | [diff] [blame] | 441 | |
| 442 | virtual ~future_error() _NOEXCEPT; |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 443 | }; |
| 444 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 445 | class __assoc_sub_state |
| 446 | : public __shared_count |
| 447 | { |
| 448 | protected: |
| 449 | exception_ptr __exception_; |
| 450 | mutable mutex __mut_; |
| 451 | mutable condition_variable __cv_; |
| 452 | unsigned __state_; |
| 453 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 454 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 455 | void __sub_wait(unique_lock<mutex>& __lk); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 456 | public: |
| 457 | enum |
| 458 | { |
| 459 | __constructed = 1, |
| 460 | __future_attached = 2, |
| 461 | ready = 4, |
| 462 | deferred = 8 |
| 463 | }; |
| 464 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 465 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 466 | __assoc_sub_state() : __state_(0) {} |
| 467 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 468 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 469 | bool __has_value() const |
| 470 | {return (__state_ & __constructed) || (__exception_ != nullptr);} |
| 471 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 472 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 1b031c9 | 2013-01-14 20:01:24 +0000 | [diff] [blame] | 473 | void __set_future_attached() |
| 474 | { |
| 475 | lock_guard<mutex> __lk(__mut_); |
| 476 | __state_ |= __future_attached; |
| 477 | } |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 478 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 479 | bool __has_future_attached() const {return __state_ & __future_attached;} |
| 480 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 481 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 482 | void __set_deferred() {__state_ |= deferred;} |
| 483 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 484 | void __make_ready(); |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 485 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 486 | bool __is_ready() const {return __state_ & ready;} |
| 487 | |
| 488 | void set_value(); |
| 489 | void set_value_at_thread_exit(); |
| 490 | |
| 491 | void set_exception(exception_ptr __p); |
| 492 | void set_exception_at_thread_exit(exception_ptr __p); |
| 493 | |
| 494 | void copy(); |
| 495 | |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 496 | void wait(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 497 | template <class _Rep, class _Period> |
| 498 | future_status |
| 499 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const; |
| 500 | template <class _Clock, class _Duration> |
| 501 | future_status |
| 502 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 503 | |
| 504 | virtual void __execute(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 505 | }; |
| 506 | |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 507 | template <class _Clock, class _Duration> |
| 508 | future_status |
| 509 | __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 510 | { |
| 511 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 512 | if (__state_ & deferred) |
| 513 | return future_status::deferred; |
| 514 | while (!(__state_ & ready) && _Clock::now() < __abs_time) |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 515 | __cv_.wait_until(__lk, __abs_time); |
| 516 | if (__state_ & ready) |
| 517 | return future_status::ready; |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 518 | return future_status::timeout; |
| 519 | } |
| 520 | |
| 521 | template <class _Rep, class _Period> |
| 522 | inline _LIBCPP_INLINE_VISIBILITY |
| 523 | future_status |
| 524 | __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 525 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 526 | return wait_until(chrono::steady_clock::now() + __rel_time); |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 529 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 530 | class __assoc_state |
| 531 | : public __assoc_sub_state |
| 532 | { |
| 533 | typedef __assoc_sub_state base; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 534 | typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 535 | protected: |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 536 | _Up __value_; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 537 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 538 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 539 | public: |
| 540 | |
| 541 | template <class _Arg> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 542 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 543 | void set_value(_Arg&& __arg); |
| 544 | #else |
| 545 | void set_value(_Arg& __arg); |
| 546 | #endif |
| 547 | |
| 548 | template <class _Arg> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 549 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 550 | void set_value_at_thread_exit(_Arg&& __arg); |
| 551 | #else |
| 552 | void set_value_at_thread_exit(_Arg& __arg); |
| 553 | #endif |
| 554 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 555 | _Rp move(); |
| 556 | typename add_lvalue_reference<_Rp>::type copy(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 557 | }; |
| 558 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 559 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 560 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 561 | __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 562 | { |
| 563 | if (this->__state_ & base::__constructed) |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 564 | reinterpret_cast<_Rp*>(&__value_)->~_Rp(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 565 | delete this; |
| 566 | } |
| 567 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 568 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 569 | template <class _Arg> |
| 570 | void |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 571 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 572 | __assoc_state<_Rp>::set_value(_Arg&& __arg) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 573 | #else |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 574 | __assoc_state<_Rp>::set_value(_Arg& __arg) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 575 | #endif |
| 576 | { |
| 577 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 578 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 579 | if (this->__has_value()) |
| 580 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 581 | #endif |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 582 | ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg)); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 583 | this->__state_ |= base::__constructed | base::ready; |
| 584 | __lk.unlock(); |
| 585 | __cv_.notify_all(); |
| 586 | } |
| 587 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 588 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 589 | template <class _Arg> |
| 590 | void |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 591 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 592 | __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 593 | #else |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 594 | __assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 595 | #endif |
| 596 | { |
| 597 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 598 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 599 | if (this->__has_value()) |
| 600 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 601 | #endif |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 602 | ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg)); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 603 | this->__state_ |= base::__constructed; |
Howard Hinnant | 5306d68 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 604 | __thread_local_data()->__make_ready_at_thread_exit(this); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 605 | __lk.unlock(); |
| 606 | } |
| 607 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 608 | template <class _Rp> |
| 609 | _Rp |
| 610 | __assoc_state<_Rp>::move() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 611 | { |
| 612 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 613 | this->__sub_wait(__lk); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 614 | if (this->__exception_ != nullptr) |
| 615 | rethrow_exception(this->__exception_); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 616 | return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_)); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 619 | template <class _Rp> |
| 620 | typename add_lvalue_reference<_Rp>::type |
| 621 | __assoc_state<_Rp>::copy() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 622 | { |
| 623 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 624 | this->__sub_wait(__lk); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 625 | if (this->__exception_ != nullptr) |
| 626 | rethrow_exception(this->__exception_); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 627 | return *reinterpret_cast<_Rp*>(&__value_); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 630 | template <class _Rp> |
| 631 | class __assoc_state<_Rp&> |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 632 | : public __assoc_sub_state |
| 633 | { |
| 634 | typedef __assoc_sub_state base; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 635 | typedef _Rp* _Up; |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 636 | protected: |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 637 | _Up __value_; |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 638 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 639 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 640 | public: |
| 641 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 642 | void set_value(_Rp& __arg); |
| 643 | void set_value_at_thread_exit(_Rp& __arg); |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 644 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 645 | _Rp& copy(); |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 646 | }; |
| 647 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 648 | template <class _Rp> |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 649 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 650 | __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 651 | { |
| 652 | delete this; |
| 653 | } |
| 654 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 655 | template <class _Rp> |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 656 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 657 | __assoc_state<_Rp&>::set_value(_Rp& __arg) |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 658 | { |
| 659 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 660 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 661 | if (this->__has_value()) |
| 662 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 663 | #endif |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 664 | __value_ = &__arg; |
| 665 | this->__state_ |= base::__constructed | base::ready; |
| 666 | __lk.unlock(); |
| 667 | __cv_.notify_all(); |
| 668 | } |
| 669 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 670 | template <class _Rp> |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 671 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 672 | __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 673 | { |
| 674 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 675 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 676 | if (this->__has_value()) |
| 677 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 678 | #endif |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 679 | __value_ = &__arg; |
| 680 | this->__state_ |= base::__constructed; |
Howard Hinnant | 5306d68 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 681 | __thread_local_data()->__make_ready_at_thread_exit(this); |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 682 | __lk.unlock(); |
| 683 | } |
| 684 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 685 | template <class _Rp> |
| 686 | _Rp& |
| 687 | __assoc_state<_Rp&>::copy() |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 688 | { |
| 689 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 690 | this->__sub_wait(__lk); |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 691 | if (this->__exception_ != nullptr) |
| 692 | rethrow_exception(this->__exception_); |
| 693 | return *__value_; |
| 694 | } |
| 695 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 696 | template <class _Rp, class _Alloc> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 697 | class __assoc_state_alloc |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 698 | : public __assoc_state<_Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 699 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 700 | typedef __assoc_state<_Rp> base; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 701 | _Alloc __alloc_; |
| 702 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 703 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 704 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 705 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 706 | explicit __assoc_state_alloc(const _Alloc& __a) |
| 707 | : __alloc_(__a) {} |
| 708 | }; |
| 709 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 710 | template <class _Rp, class _Alloc> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 711 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 712 | __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 713 | { |
| 714 | if (this->__state_ & base::__constructed) |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 715 | reinterpret_cast<_Rp*>(&this->__value_)->~_Rp(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 716 | typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_); |
| 717 | this->~__assoc_state_alloc(); |
| 718 | __a.deallocate(this, 1); |
| 719 | } |
| 720 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 721 | template <class _Rp, class _Alloc> |
| 722 | class __assoc_state_alloc<_Rp&, _Alloc> |
| 723 | : public __assoc_state<_Rp&> |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 724 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 725 | typedef __assoc_state<_Rp&> base; |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 726 | _Alloc __alloc_; |
| 727 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 728 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 729 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 730 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 731 | explicit __assoc_state_alloc(const _Alloc& __a) |
| 732 | : __alloc_(__a) {} |
| 733 | }; |
| 734 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 735 | template <class _Rp, class _Alloc> |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 736 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 737 | __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 738 | { |
| 739 | typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_); |
| 740 | this->~__assoc_state_alloc(); |
| 741 | __a.deallocate(this, 1); |
| 742 | } |
| 743 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 744 | template <class _Alloc> |
| 745 | class __assoc_sub_state_alloc |
| 746 | : public __assoc_sub_state |
| 747 | { |
| 748 | typedef __assoc_sub_state base; |
| 749 | _Alloc __alloc_; |
| 750 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 751 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 752 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 753 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 754 | explicit __assoc_sub_state_alloc(const _Alloc& __a) |
| 755 | : __alloc_(__a) {} |
| 756 | }; |
| 757 | |
| 758 | template <class _Alloc> |
| 759 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 760 | __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 761 | { |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 762 | typename _Alloc::template rebind<__assoc_sub_state_alloc>::other __a(__alloc_); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 763 | this->~__assoc_sub_state_alloc(); |
| 764 | __a.deallocate(this, 1); |
| 765 | } |
| 766 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 767 | template <class _Rp, class _Fp> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 768 | class __deferred_assoc_state |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 769 | : public __assoc_state<_Rp> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 770 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 771 | typedef __assoc_state<_Rp> base; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 772 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 773 | _Fp __func_; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 774 | |
| 775 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 776 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 777 | explicit __deferred_assoc_state(_Fp&& __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 778 | #endif |
| 779 | |
| 780 | virtual void __execute(); |
| 781 | }; |
| 782 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 783 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 784 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 785 | template <class _Rp, class _Fp> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 786 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 787 | __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) |
| 788 | : __func_(_VSTD::forward<_Fp>(__f)) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 789 | { |
| 790 | this->__set_deferred(); |
| 791 | } |
| 792 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 793 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 794 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 795 | template <class _Rp, class _Fp> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 796 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 797 | __deferred_assoc_state<_Rp, _Fp>::__execute() |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 798 | { |
| 799 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 800 | try |
| 801 | { |
| 802 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 803 | this->set_value(__func_()); |
| 804 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 805 | } |
| 806 | catch (...) |
| 807 | { |
| 808 | this->set_exception(current_exception()); |
| 809 | } |
| 810 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 811 | } |
| 812 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 813 | template <class _Fp> |
| 814 | class __deferred_assoc_state<void, _Fp> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 815 | : public __assoc_sub_state |
| 816 | { |
| 817 | typedef __assoc_sub_state base; |
| 818 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 819 | _Fp __func_; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 820 | |
| 821 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 822 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 823 | explicit __deferred_assoc_state(_Fp&& __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 824 | #endif |
| 825 | |
| 826 | virtual void __execute(); |
| 827 | }; |
| 828 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 829 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 830 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 831 | template <class _Fp> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 832 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 833 | __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) |
| 834 | : __func_(_VSTD::forward<_Fp>(__f)) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 835 | { |
| 836 | this->__set_deferred(); |
| 837 | } |
| 838 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 839 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 840 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 841 | template <class _Fp> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 842 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 843 | __deferred_assoc_state<void, _Fp>::__execute() |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 844 | { |
| 845 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 846 | try |
| 847 | { |
| 848 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 849 | __func_(); |
| 850 | this->set_value(); |
| 851 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 852 | } |
| 853 | catch (...) |
| 854 | { |
| 855 | this->set_exception(current_exception()); |
| 856 | } |
| 857 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 858 | } |
| 859 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 860 | template <class _Rp, class _Fp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 861 | class __async_assoc_state |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 862 | : public __assoc_state<_Rp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 863 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 864 | typedef __assoc_state<_Rp> base; |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 865 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 866 | _Fp __func_; |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 867 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 868 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 869 | public: |
| 870 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 871 | explicit __async_assoc_state(_Fp&& __f); |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 872 | #endif |
| 873 | |
| 874 | virtual void __execute(); |
| 875 | }; |
| 876 | |
| 877 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 878 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 879 | template <class _Rp, class _Fp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 880 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 881 | __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) |
| 882 | : __func_(_VSTD::forward<_Fp>(__f)) |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 883 | { |
| 884 | } |
| 885 | |
| 886 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 887 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 888 | template <class _Rp, class _Fp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 889 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 890 | __async_assoc_state<_Rp, _Fp>::__execute() |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 891 | { |
| 892 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 893 | try |
| 894 | { |
| 895 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 896 | this->set_value(__func_()); |
| 897 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 898 | } |
| 899 | catch (...) |
| 900 | { |
| 901 | this->set_exception(current_exception()); |
| 902 | } |
| 903 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 904 | } |
| 905 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 906 | template <class _Rp, class _Fp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 907 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 908 | __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 909 | { |
| 910 | this->wait(); |
| 911 | base::__on_zero_shared(); |
| 912 | } |
| 913 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 914 | template <class _Fp> |
| 915 | class __async_assoc_state<void, _Fp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 916 | : public __assoc_sub_state |
| 917 | { |
| 918 | typedef __assoc_sub_state base; |
| 919 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 920 | _Fp __func_; |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 921 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 922 | virtual void __on_zero_shared() _NOEXCEPT; |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 923 | public: |
| 924 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 925 | explicit __async_assoc_state(_Fp&& __f); |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 926 | #endif |
| 927 | |
| 928 | virtual void __execute(); |
| 929 | }; |
| 930 | |
| 931 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 932 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 933 | template <class _Fp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 934 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 935 | __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) |
| 936 | : __func_(_VSTD::forward<_Fp>(__f)) |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 937 | { |
| 938 | } |
| 939 | |
| 940 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 941 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 942 | template <class _Fp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 943 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 944 | __async_assoc_state<void, _Fp>::__execute() |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 945 | { |
| 946 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 947 | try |
| 948 | { |
| 949 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 950 | __func_(); |
| 951 | this->set_value(); |
| 952 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 953 | } |
| 954 | catch (...) |
| 955 | { |
| 956 | this->set_exception(current_exception()); |
| 957 | } |
| 958 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 959 | } |
| 960 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 961 | template <class _Fp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 962 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 963 | __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 964 | { |
| 965 | this->wait(); |
| 966 | base::__on_zero_shared(); |
| 967 | } |
| 968 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 969 | template <class _Rp> class _LIBCPP_TYPE_VIS promise; |
| 970 | template <class _Rp> class _LIBCPP_TYPE_VIS shared_future; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 971 | |
| 972 | // future |
| 973 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 974 | template <class _Rp> class _LIBCPP_TYPE_VIS future; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 975 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 976 | template <class _Rp, class _Fp> |
| 977 | future<_Rp> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 978 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 979 | __make_deferred_assoc_state(_Fp&& __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 980 | #else |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 981 | __make_deferred_assoc_state(_Fp __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 982 | #endif |
| 983 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 984 | template <class _Rp, class _Fp> |
| 985 | future<_Rp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 986 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 987 | __make_async_assoc_state(_Fp&& __f); |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 988 | #else |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 989 | __make_async_assoc_state(_Fp __f); |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 990 | #endif |
| 991 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 992 | template <class _Rp> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 993 | class _LIBCPP_TYPE_VIS future |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 994 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 995 | __assoc_state<_Rp>* __state_; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 996 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 997 | explicit future(__assoc_state<_Rp>* __state); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 998 | |
| 999 | template <class> friend class promise; |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1000 | template <class> friend class shared_future; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1001 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1002 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1003 | template <class _R1, class _Fp> |
| 1004 | friend future<_R1> __make_deferred_assoc_state(_Fp&& __f); |
| 1005 | template <class _R1, class _Fp> |
| 1006 | friend future<_R1> __make_async_assoc_state(_Fp&& __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1007 | #else |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1008 | template <class _R1, class _Fp> |
| 1009 | friend future<_R1> __make_deferred_assoc_state(_Fp __f); |
| 1010 | template <class _R1, class _Fp> |
| 1011 | friend future<_R1> __make_async_assoc_state(_Fp __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1012 | #endif |
| 1013 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1014 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1015 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1016 | future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1017 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1018 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1019 | future(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1020 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1021 | future(const future&) = delete; |
| 1022 | future& operator=(const future&) = delete; |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1023 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1024 | future& operator=(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1025 | { |
| 1026 | future(std::move(__rhs)).swap(*this); |
| 1027 | return *this; |
| 1028 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1029 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1030 | private: |
| 1031 | future(const future&); |
| 1032 | future& operator=(const future&); |
| 1033 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1034 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1035 | ~future(); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1036 | shared_future<_Rp> share(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1037 | |
| 1038 | // retrieving the value |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1039 | _Rp get(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1040 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1041 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1042 | void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1043 | |
| 1044 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1045 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1046 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1047 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1048 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1049 | void wait() const {__state_->wait();} |
| 1050 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1051 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1052 | future_status |
| 1053 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 1054 | {return __state_->wait_for(__rel_time);} |
| 1055 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1056 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1057 | future_status |
| 1058 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 1059 | {return __state_->wait_until(__abs_time);} |
| 1060 | }; |
| 1061 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1062 | template <class _Rp> |
| 1063 | future<_Rp>::future(__assoc_state<_Rp>* __state) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1064 | : __state_(__state) |
| 1065 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1066 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1067 | if (__state_->__has_future_attached()) |
| 1068 | throw future_error(make_error_code(future_errc::future_already_retrieved)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1069 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1070 | __state_->__add_shared(); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1071 | __state_->__set_future_attached(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1074 | struct __release_shared_count |
| 1075 | { |
| 1076 | void operator()(__shared_count* p) {p->__release_shared();} |
| 1077 | }; |
| 1078 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1079 | template <class _Rp> |
| 1080 | future<_Rp>::~future() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1081 | { |
| 1082 | if (__state_) |
| 1083 | __state_->__release_shared(); |
| 1084 | } |
| 1085 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1086 | template <class _Rp> |
| 1087 | _Rp |
| 1088 | future<_Rp>::get() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1089 | { |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1090 | unique_ptr<__shared_count, __release_shared_count> __(__state_); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1091 | __assoc_state<_Rp>* __s = __state_; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1092 | __state_ = nullptr; |
| 1093 | return __s->move(); |
| 1094 | } |
| 1095 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1096 | template <class _Rp> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 1097 | class _LIBCPP_TYPE_VIS future<_Rp&> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1098 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1099 | __assoc_state<_Rp&>* __state_; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1100 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1101 | explicit future(__assoc_state<_Rp&>* __state); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1102 | |
| 1103 | template <class> friend class promise; |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1104 | template <class> friend class shared_future; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1105 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1106 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1107 | template <class _R1, class _Fp> |
| 1108 | friend future<_R1> __make_deferred_assoc_state(_Fp&& __f); |
| 1109 | template <class _R1, class _Fp> |
| 1110 | friend future<_R1> __make_async_assoc_state(_Fp&& __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1111 | #else |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1112 | template <class _R1, class _Fp> |
| 1113 | friend future<_R1> __make_deferred_assoc_state(_Fp __f); |
| 1114 | template <class _R1, class _Fp> |
| 1115 | friend future<_R1> __make_async_assoc_state(_Fp __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1116 | #endif |
| 1117 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1118 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1119 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1120 | future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1121 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1122 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1123 | future(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1124 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1125 | future(const future&) = delete; |
| 1126 | future& operator=(const future&) = delete; |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1127 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1128 | future& operator=(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1129 | { |
| 1130 | future(std::move(__rhs)).swap(*this); |
| 1131 | return *this; |
| 1132 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1133 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1134 | private: |
| 1135 | future(const future&); |
| 1136 | future& operator=(const future&); |
| 1137 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1138 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1139 | ~future(); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1140 | shared_future<_Rp&> share(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1141 | |
| 1142 | // retrieving the value |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1143 | _Rp& get(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1144 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1145 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1146 | void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1147 | |
| 1148 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1149 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1150 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1151 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1152 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1153 | void wait() const {__state_->wait();} |
| 1154 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1155 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1156 | future_status |
| 1157 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 1158 | {return __state_->wait_for(__rel_time);} |
| 1159 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1160 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1161 | future_status |
| 1162 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 1163 | {return __state_->wait_until(__abs_time);} |
| 1164 | }; |
| 1165 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1166 | template <class _Rp> |
| 1167 | future<_Rp&>::future(__assoc_state<_Rp&>* __state) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1168 | : __state_(__state) |
| 1169 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1170 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1171 | if (__state_->__has_future_attached()) |
| 1172 | throw future_error(make_error_code(future_errc::future_already_retrieved)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1173 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1174 | __state_->__add_shared(); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1175 | __state_->__set_future_attached(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1178 | template <class _Rp> |
| 1179 | future<_Rp&>::~future() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1180 | { |
| 1181 | if (__state_) |
| 1182 | __state_->__release_shared(); |
| 1183 | } |
| 1184 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1185 | template <class _Rp> |
| 1186 | _Rp& |
| 1187 | future<_Rp&>::get() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1188 | { |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1189 | unique_ptr<__shared_count, __release_shared_count> __(__state_); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1190 | __assoc_state<_Rp&>* __s = __state_; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1191 | __state_ = nullptr; |
| 1192 | return __s->copy(); |
| 1193 | } |
| 1194 | |
| 1195 | template <> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 1196 | class _LIBCPP_TYPE_VIS future<void> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1197 | { |
| 1198 | __assoc_sub_state* __state_; |
| 1199 | |
| 1200 | explicit future(__assoc_sub_state* __state); |
| 1201 | |
| 1202 | template <class> friend class promise; |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1203 | template <class> friend class shared_future; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1204 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1205 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1206 | template <class _R1, class _Fp> |
| 1207 | friend future<_R1> __make_deferred_assoc_state(_Fp&& __f); |
| 1208 | template <class _R1, class _Fp> |
| 1209 | friend future<_R1> __make_async_assoc_state(_Fp&& __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1210 | #else |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1211 | template <class _R1, class _Fp> |
| 1212 | friend future<_R1> __make_deferred_assoc_state(_Fp __f); |
| 1213 | template <class _R1, class _Fp> |
| 1214 | friend future<_R1> __make_async_assoc_state(_Fp __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1215 | #endif |
| 1216 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1217 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1218 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1219 | future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1220 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1221 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1222 | future(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1223 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1224 | future(const future&) = delete; |
| 1225 | future& operator=(const future&) = delete; |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1226 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1227 | future& operator=(future&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1228 | { |
| 1229 | future(std::move(__rhs)).swap(*this); |
| 1230 | return *this; |
| 1231 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1232 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1233 | private: |
| 1234 | future(const future&); |
| 1235 | future& operator=(const future&); |
| 1236 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1237 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1238 | ~future(); |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 1239 | shared_future<void> share(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1240 | |
| 1241 | // retrieving the value |
| 1242 | void get(); |
| 1243 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1244 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1245 | void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1246 | |
| 1247 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1248 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1249 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1250 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1251 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1252 | void wait() const {__state_->wait();} |
| 1253 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1254 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1255 | future_status |
| 1256 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 1257 | {return __state_->wait_for(__rel_time);} |
| 1258 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1259 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1260 | future_status |
| 1261 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 1262 | {return __state_->wait_until(__abs_time);} |
| 1263 | }; |
| 1264 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1265 | template <class _Rp> |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1266 | inline _LIBCPP_INLINE_VISIBILITY |
| 1267 | void |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1268 | swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1269 | { |
| 1270 | __x.swap(__y); |
| 1271 | } |
| 1272 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1273 | // promise<R> |
| 1274 | |
Howard Hinnant | 2b1b2d4 | 2011-06-14 19:58:17 +0000 | [diff] [blame] | 1275 | template <class _Callable> class packaged_task; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1276 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1277 | template <class _Rp> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 1278 | class _LIBCPP_TYPE_VIS promise |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1279 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1280 | __assoc_state<_Rp>* __state_; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1281 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1282 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1283 | explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1284 | |
| 1285 | template <class> friend class packaged_task; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1286 | public: |
| 1287 | promise(); |
| 1288 | template <class _Alloc> |
| 1289 | promise(allocator_arg_t, const _Alloc& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1290 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1291 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1292 | promise(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1293 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1294 | promise(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1295 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1296 | private: |
| 1297 | promise(const promise& __rhs); |
| 1298 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1299 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1300 | ~promise(); |
| 1301 | |
| 1302 | // assignment |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1303 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1304 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1305 | promise& operator=(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1306 | { |
| 1307 | promise(std::move(__rhs)).swap(*this); |
| 1308 | return *this; |
| 1309 | } |
| 1310 | promise& operator=(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1311 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1312 | private: |
| 1313 | promise& operator=(const promise& __rhs); |
| 1314 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1315 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1316 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1317 | void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1318 | |
| 1319 | // retrieving the result |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1320 | future<_Rp> get_future(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1321 | |
| 1322 | // setting the result |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1323 | void set_value(const _Rp& __r); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1324 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1325 | void set_value(_Rp&& __r); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1326 | #endif |
| 1327 | void set_exception(exception_ptr __p); |
| 1328 | |
| 1329 | // setting the result with deferred notification |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1330 | void set_value_at_thread_exit(const _Rp& __r); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1331 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1332 | void set_value_at_thread_exit(_Rp&& __r); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1333 | #endif |
| 1334 | void set_exception_at_thread_exit(exception_ptr __p); |
| 1335 | }; |
| 1336 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1337 | template <class _Rp> |
| 1338 | promise<_Rp>::promise() |
| 1339 | : __state_(new __assoc_state<_Rp>) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1340 | { |
| 1341 | } |
| 1342 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1343 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1344 | template <class _Alloc> |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1345 | promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1346 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1347 | typedef typename _Alloc::template rebind<__assoc_state_alloc<_Rp, _Alloc> >::other _A2; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1348 | typedef __allocator_destructor<_A2> _D2; |
| 1349 | _A2 __a(__a0); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1350 | unique_ptr<__assoc_state_alloc<_Rp, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1)); |
| 1351 | ::new(__hold.get()) __assoc_state_alloc<_Rp, _Alloc>(__a0); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1352 | __state_ = __hold.release(); |
| 1353 | } |
| 1354 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1355 | template <class _Rp> |
| 1356 | promise<_Rp>::~promise() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1357 | { |
| 1358 | if (__state_) |
| 1359 | { |
| 1360 | if (!__state_->__has_value() && __state_->use_count() > 1) |
| 1361 | __state_->set_exception(make_exception_ptr( |
| 1362 | future_error(make_error_code(future_errc::broken_promise)) |
| 1363 | )); |
| 1364 | __state_->__release_shared(); |
| 1365 | } |
| 1366 | } |
| 1367 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1368 | template <class _Rp> |
| 1369 | future<_Rp> |
| 1370 | promise<_Rp>::get_future() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1371 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1372 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1373 | if (__state_ == nullptr) |
| 1374 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1375 | #endif |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1376 | return future<_Rp>(__state_); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1379 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1380 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1381 | promise<_Rp>::set_value(const _Rp& __r) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1382 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1383 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1384 | if (__state_ == nullptr) |
| 1385 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1386 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1387 | __state_->set_value(__r); |
| 1388 | } |
| 1389 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1390 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1391 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1392 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1393 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1394 | promise<_Rp>::set_value(_Rp&& __r) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1395 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1396 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1397 | if (__state_ == nullptr) |
| 1398 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1399 | #endif |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1400 | __state_->set_value(_VSTD::move(__r)); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1403 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1404 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1405 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1406 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1407 | promise<_Rp>::set_exception(exception_ptr __p) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1408 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1409 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1410 | if (__state_ == nullptr) |
| 1411 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1412 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1413 | __state_->set_exception(__p); |
| 1414 | } |
| 1415 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1416 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1417 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1418 | promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1419 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1420 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1421 | if (__state_ == nullptr) |
| 1422 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1423 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1424 | __state_->set_value_at_thread_exit(__r); |
| 1425 | } |
| 1426 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1427 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1428 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1429 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1430 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1431 | promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1432 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1433 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1434 | if (__state_ == nullptr) |
| 1435 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1436 | #endif |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1437 | __state_->set_value_at_thread_exit(_VSTD::move(__r)); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1440 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1441 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1442 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1443 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1444 | promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1445 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1446 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1447 | if (__state_ == nullptr) |
| 1448 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1449 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1450 | __state_->set_exception_at_thread_exit(__p); |
| 1451 | } |
| 1452 | |
| 1453 | // promise<R&> |
| 1454 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1455 | template <class _Rp> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 1456 | class _LIBCPP_TYPE_VIS promise<_Rp&> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1457 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1458 | __assoc_state<_Rp&>* __state_; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1459 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1460 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1461 | explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1462 | |
| 1463 | template <class> friend class packaged_task; |
| 1464 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1465 | public: |
| 1466 | promise(); |
| 1467 | template <class _Allocator> |
| 1468 | promise(allocator_arg_t, const _Allocator& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1469 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1470 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1471 | promise(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1472 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1473 | promise(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1474 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1475 | private: |
| 1476 | promise(const promise& __rhs); |
| 1477 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1478 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1479 | ~promise(); |
| 1480 | |
| 1481 | // assignment |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1482 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1483 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1484 | promise& operator=(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1485 | { |
| 1486 | promise(std::move(__rhs)).swap(*this); |
| 1487 | return *this; |
| 1488 | } |
| 1489 | promise& operator=(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1490 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1491 | private: |
| 1492 | promise& operator=(const promise& __rhs); |
| 1493 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1494 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1495 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1496 | void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1497 | |
| 1498 | // retrieving the result |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1499 | future<_Rp&> get_future(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1500 | |
| 1501 | // setting the result |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1502 | void set_value(_Rp& __r); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1503 | void set_exception(exception_ptr __p); |
| 1504 | |
| 1505 | // setting the result with deferred notification |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1506 | void set_value_at_thread_exit(_Rp&); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1507 | void set_exception_at_thread_exit(exception_ptr __p); |
| 1508 | }; |
| 1509 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1510 | template <class _Rp> |
| 1511 | promise<_Rp&>::promise() |
| 1512 | : __state_(new __assoc_state<_Rp&>) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1513 | { |
| 1514 | } |
| 1515 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1516 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1517 | template <class _Alloc> |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1518 | promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1519 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1520 | typedef typename _Alloc::template rebind<__assoc_state_alloc<_Rp&, _Alloc> >::other _A2; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1521 | typedef __allocator_destructor<_A2> _D2; |
| 1522 | _A2 __a(__a0); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1523 | unique_ptr<__assoc_state_alloc<_Rp&, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1)); |
| 1524 | ::new(__hold.get()) __assoc_state_alloc<_Rp&, _Alloc>(__a0); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1525 | __state_ = __hold.release(); |
| 1526 | } |
| 1527 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1528 | template <class _Rp> |
| 1529 | promise<_Rp&>::~promise() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1530 | { |
| 1531 | if (__state_) |
| 1532 | { |
| 1533 | if (!__state_->__has_value() && __state_->use_count() > 1) |
| 1534 | __state_->set_exception(make_exception_ptr( |
| 1535 | future_error(make_error_code(future_errc::broken_promise)) |
| 1536 | )); |
| 1537 | __state_->__release_shared(); |
| 1538 | } |
| 1539 | } |
| 1540 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1541 | template <class _Rp> |
| 1542 | future<_Rp&> |
| 1543 | promise<_Rp&>::get_future() |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1544 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1545 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1546 | if (__state_ == nullptr) |
| 1547 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1548 | #endif |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1549 | return future<_Rp&>(__state_); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1552 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1553 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1554 | promise<_Rp&>::set_value(_Rp& __r) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1555 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1556 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1557 | if (__state_ == nullptr) |
| 1558 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1559 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1560 | __state_->set_value(__r); |
| 1561 | } |
| 1562 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1563 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1564 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1565 | promise<_Rp&>::set_exception(exception_ptr __p) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1566 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1567 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1568 | if (__state_ == nullptr) |
| 1569 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1570 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1571 | __state_->set_exception(__p); |
| 1572 | } |
| 1573 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1574 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1575 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1576 | promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1577 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1578 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1579 | if (__state_ == nullptr) |
| 1580 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1581 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1582 | __state_->set_value_at_thread_exit(__r); |
| 1583 | } |
| 1584 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1585 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1586 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1587 | promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1588 | { |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1589 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1590 | if (__state_ == nullptr) |
| 1591 | throw future_error(make_error_code(future_errc::no_state)); |
Howard Hinnant | 22ba71b | 2011-07-13 16:00:50 +0000 | [diff] [blame] | 1592 | #endif |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1593 | __state_->set_exception_at_thread_exit(__p); |
| 1594 | } |
| 1595 | |
| 1596 | // promise<void> |
| 1597 | |
| 1598 | template <> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 1599 | class _LIBCPP_TYPE_VIS promise<void> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1600 | { |
| 1601 | __assoc_sub_state* __state_; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1602 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1603 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1604 | explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1605 | |
| 1606 | template <class> friend class packaged_task; |
| 1607 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1608 | public: |
| 1609 | promise(); |
| 1610 | template <class _Allocator> |
| 1611 | promise(allocator_arg_t, const _Allocator& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1612 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1613 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1614 | promise(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1615 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1616 | promise(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1617 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1618 | private: |
| 1619 | promise(const promise& __rhs); |
| 1620 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1621 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1622 | ~promise(); |
| 1623 | |
| 1624 | // assignment |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1625 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1626 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1627 | promise& operator=(promise&& __rhs) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1628 | { |
| 1629 | promise(std::move(__rhs)).swap(*this); |
| 1630 | return *this; |
| 1631 | } |
| 1632 | promise& operator=(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1633 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1634 | private: |
| 1635 | promise& operator=(const promise& __rhs); |
| 1636 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1637 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1638 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1639 | void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1640 | |
| 1641 | // retrieving the result |
| 1642 | future<void> get_future(); |
| 1643 | |
| 1644 | // setting the result |
| 1645 | void set_value(); |
| 1646 | void set_exception(exception_ptr __p); |
| 1647 | |
| 1648 | // setting the result with deferred notification |
| 1649 | void set_value_at_thread_exit(); |
| 1650 | void set_exception_at_thread_exit(exception_ptr __p); |
| 1651 | }; |
| 1652 | |
| 1653 | template <class _Alloc> |
| 1654 | promise<void>::promise(allocator_arg_t, const _Alloc& __a0) |
| 1655 | { |
| 1656 | typedef typename _Alloc::template rebind<__assoc_sub_state_alloc<_Alloc> >::other _A2; |
| 1657 | typedef __allocator_destructor<_A2> _D2; |
| 1658 | _A2 __a(__a0); |
| 1659 | unique_ptr<__assoc_sub_state_alloc<_Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1)); |
| 1660 | ::new(__hold.get()) __assoc_sub_state_alloc<_Alloc>(__a0); |
| 1661 | __state_ = __hold.release(); |
| 1662 | } |
| 1663 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1664 | template <class _Rp> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1665 | inline _LIBCPP_INLINE_VISIBILITY |
| 1666 | void |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1667 | swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1668 | { |
| 1669 | __x.swap(__y); |
| 1670 | } |
| 1671 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1672 | template <class _Rp, class _Alloc> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 1673 | struct _LIBCPP_TYPE_VIS uses_allocator<promise<_Rp>, _Alloc> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1674 | : public true_type {}; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1675 | |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1676 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 1677 | |
| 1678 | // packaged_task |
| 1679 | |
| 1680 | template<class _Fp> class __packaged_task_base; |
| 1681 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1682 | template<class _Rp, class ..._ArgTypes> |
| 1683 | class __packaged_task_base<_Rp(_ArgTypes...)> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1684 | { |
| 1685 | __packaged_task_base(const __packaged_task_base&); |
| 1686 | __packaged_task_base& operator=(const __packaged_task_base&); |
| 1687 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1688 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1689 | __packaged_task_base() {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1690 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1691 | virtual ~__packaged_task_base() {} |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1692 | virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1693 | virtual void destroy() = 0; |
| 1694 | virtual void destroy_deallocate() = 0; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1695 | virtual _Rp operator()(_ArgTypes&& ...) = 0; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1696 | }; |
| 1697 | |
| 1698 | template<class _FD, class _Alloc, class _FB> class __packaged_task_func; |
| 1699 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1700 | template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> |
| 1701 | class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> |
| 1702 | : public __packaged_task_base<_Rp(_ArgTypes...)> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1703 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1704 | __compressed_pair<_Fp, _Alloc> __f_; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1705 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1706 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1707 | explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1708 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1709 | explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1710 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1711 | __packaged_task_func(const _Fp& __f, const _Alloc& __a) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1712 | : __f_(__f, __a) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1713 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1714 | __packaged_task_func(_Fp&& __f, const _Alloc& __a) |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1715 | : __f_(_VSTD::move(__f), __a) {} |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1716 | virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1717 | virtual void destroy(); |
| 1718 | virtual void destroy_deallocate(); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1719 | virtual _Rp operator()(_ArgTypes&& ... __args); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1720 | }; |
| 1721 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1722 | template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1723 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1724 | __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to( |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1725 | __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1726 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1727 | ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second())); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1730 | template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1731 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1732 | __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1733 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1734 | __f_.~__compressed_pair<_Fp, _Alloc>(); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1737 | template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1738 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1739 | __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1740 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1741 | typedef typename _Alloc::template rebind<__packaged_task_func>::other _Ap; |
| 1742 | _Ap __a(__f_.second()); |
| 1743 | __f_.~__compressed_pair<_Fp, _Alloc>(); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1744 | __a.deallocate(this, 1); |
| 1745 | } |
| 1746 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1747 | template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> |
| 1748 | _Rp |
| 1749 | __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1750 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1751 | return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Howard Hinnant | 2b1b2d4 | 2011-06-14 19:58:17 +0000 | [diff] [blame] | 1754 | template <class _Callable> class __packaged_task_function; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1755 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1756 | template<class _Rp, class ..._ArgTypes> |
| 1757 | class __packaged_task_function<_Rp(_ArgTypes...)> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1758 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1759 | typedef __packaged_task_base<_Rp(_ArgTypes...)> __base; |
Howard Hinnant | 78f0de2 | 2013-01-21 17:26:55 +0000 | [diff] [blame] | 1760 | typename aligned_storage<3*sizeof(void*)>::type __buf_; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1761 | __base* __f_; |
| 1762 | |
| 1763 | public: |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1764 | typedef _Rp result_type; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1765 | |
| 1766 | // construct/copy/destroy: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1767 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1768 | __packaged_task_function() _NOEXCEPT : __f_(nullptr) {} |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1769 | template<class _Fp> |
| 1770 | __packaged_task_function(_Fp&& __f); |
| 1771 | template<class _Fp, class _Alloc> |
| 1772 | __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1773 | |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1774 | __packaged_task_function(__packaged_task_function&&) _NOEXCEPT; |
| 1775 | __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1776 | |
| 1777 | __packaged_task_function(const __packaged_task_function&) = delete; |
| 1778 | __packaged_task_function& operator=(const __packaged_task_function&) = delete; |
| 1779 | |
| 1780 | ~__packaged_task_function(); |
| 1781 | |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1782 | void swap(__packaged_task_function&) _NOEXCEPT; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1783 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1784 | _Rp operator()(_ArgTypes...) const; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1785 | }; |
| 1786 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1787 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1788 | __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1789 | { |
| 1790 | if (__f.__f_ == nullptr) |
| 1791 | __f_ = nullptr; |
| 1792 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1793 | { |
| 1794 | __f_ = (__base*)&__buf_; |
| 1795 | __f.__f_->__move_to(__f_); |
| 1796 | } |
| 1797 | else |
| 1798 | { |
| 1799 | __f_ = __f.__f_; |
| 1800 | __f.__f_ = nullptr; |
| 1801 | } |
| 1802 | } |
| 1803 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1804 | template<class _Rp, class ..._ArgTypes> |
| 1805 | template <class _Fp> |
| 1806 | __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1807 | : __f_(nullptr) |
| 1808 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1809 | typedef typename remove_reference<_Fp>::type _FR; |
| 1810 | typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1811 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1812 | { |
| 1813 | __f_ = (__base*)&__buf_; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1814 | ::new (__f_) _FF(_VSTD::forward<_Fp>(__f)); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1815 | } |
| 1816 | else |
| 1817 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1818 | typedef allocator<_FF> _Ap; |
| 1819 | _Ap __a; |
| 1820 | typedef __allocator_destructor<_Ap> _Dp; |
| 1821 | unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); |
| 1822 | ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a)); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1823 | __f_ = __hold.release(); |
| 1824 | } |
| 1825 | } |
| 1826 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1827 | template<class _Rp, class ..._ArgTypes> |
| 1828 | template <class _Fp, class _Alloc> |
| 1829 | __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function( |
| 1830 | allocator_arg_t, const _Alloc& __a0, _Fp&& __f) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1831 | : __f_(nullptr) |
| 1832 | { |
| 1833 | typedef allocator_traits<_Alloc> __alloc_traits; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1834 | typedef typename remove_reference<_Fp>::type _FR; |
| 1835 | typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1836 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1837 | { |
| 1838 | __f_ = (__base*)&__buf_; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1839 | ::new (__f_) _FF(_VSTD::forward<_Fp>(__f)); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1840 | } |
| 1841 | else |
| 1842 | { |
| 1843 | typedef typename __alloc_traits::template |
| 1844 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 1845 | rebind_alloc<_FF> |
| 1846 | #else |
| 1847 | rebind_alloc<_FF>::other |
| 1848 | #endif |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1849 | _Ap; |
| 1850 | _Ap __a(__a0); |
| 1851 | typedef __allocator_destructor<_Ap> _Dp; |
| 1852 | unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); |
| 1853 | ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a)); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1854 | __f_ = __hold.release(); |
| 1855 | } |
| 1856 | } |
| 1857 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1858 | template<class _Rp, class ..._ArgTypes> |
| 1859 | __packaged_task_function<_Rp(_ArgTypes...)>& |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1860 | __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1861 | { |
| 1862 | if (__f_ == (__base*)&__buf_) |
| 1863 | __f_->destroy(); |
| 1864 | else if (__f_) |
| 1865 | __f_->destroy_deallocate(); |
| 1866 | __f_ = nullptr; |
| 1867 | if (__f.__f_ == nullptr) |
| 1868 | __f_ = nullptr; |
| 1869 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1870 | { |
| 1871 | __f_ = (__base*)&__buf_; |
| 1872 | __f.__f_->__move_to(__f_); |
| 1873 | } |
| 1874 | else |
| 1875 | { |
| 1876 | __f_ = __f.__f_; |
| 1877 | __f.__f_ = nullptr; |
| 1878 | } |
Argyrios Kyrtzidis | 1dc6f7a | 2012-10-13 02:03:45 +0000 | [diff] [blame] | 1879 | return *this; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1882 | template<class _Rp, class ..._ArgTypes> |
| 1883 | __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1884 | { |
| 1885 | if (__f_ == (__base*)&__buf_) |
| 1886 | __f_->destroy(); |
| 1887 | else if (__f_) |
| 1888 | __f_->destroy_deallocate(); |
| 1889 | } |
| 1890 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1891 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1892 | void |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1893 | __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1894 | { |
| 1895 | if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) |
| 1896 | { |
| 1897 | typename aligned_storage<sizeof(__buf_)>::type __tempbuf; |
| 1898 | __base* __t = (__base*)&__tempbuf; |
| 1899 | __f_->__move_to(__t); |
| 1900 | __f_->destroy(); |
| 1901 | __f_ = nullptr; |
| 1902 | __f.__f_->__move_to((__base*)&__buf_); |
| 1903 | __f.__f_->destroy(); |
| 1904 | __f.__f_ = nullptr; |
| 1905 | __f_ = (__base*)&__buf_; |
| 1906 | __t->__move_to((__base*)&__f.__buf_); |
| 1907 | __t->destroy(); |
| 1908 | __f.__f_ = (__base*)&__f.__buf_; |
| 1909 | } |
| 1910 | else if (__f_ == (__base*)&__buf_) |
| 1911 | { |
| 1912 | __f_->__move_to((__base*)&__f.__buf_); |
| 1913 | __f_->destroy(); |
| 1914 | __f_ = __f.__f_; |
| 1915 | __f.__f_ = (__base*)&__f.__buf_; |
| 1916 | } |
| 1917 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1918 | { |
| 1919 | __f.__f_->__move_to((__base*)&__buf_); |
| 1920 | __f.__f_->destroy(); |
| 1921 | __f.__f_ = __f_; |
| 1922 | __f_ = (__base*)&__buf_; |
| 1923 | } |
| 1924 | else |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1925 | _VSTD::swap(__f_, __f.__f_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1928 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1929 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1930 | _Rp |
| 1931 | __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1932 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1933 | return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1936 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 1937 | class _LIBCPP_TYPE_VIS packaged_task<_Rp(_ArgTypes...)> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1938 | { |
| 1939 | public: |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1940 | typedef _Rp result_type; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1941 | |
| 1942 | private: |
| 1943 | __packaged_task_function<result_type(_ArgTypes...)> __f_; |
| 1944 | promise<result_type> __p_; |
| 1945 | |
| 1946 | public: |
| 1947 | // construction and destruction |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1948 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1949 | packaged_task() _NOEXCEPT : __p_(nullptr) {} |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1950 | template <class _Fp> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1951 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1952 | explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {} |
| 1953 | template <class _Fp, class _Allocator> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1954 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1955 | explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f) |
| 1956 | : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)), |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1957 | __p_(allocator_arg, __a) {} |
| 1958 | // ~packaged_task() = default; |
| 1959 | |
| 1960 | // no copy |
Howard Hinnant | 8131a01 | 2012-07-21 19:34:12 +0000 | [diff] [blame] | 1961 | packaged_task(const packaged_task&) = delete; |
| 1962 | packaged_task& operator=(const packaged_task&) = delete; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1963 | |
| 1964 | // move support |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1965 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1966 | packaged_task(packaged_task&& __other) _NOEXCEPT |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1967 | : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1968 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1969 | packaged_task& operator=(packaged_task&& __other) _NOEXCEPT |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1970 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1971 | __f_ = _VSTD::move(__other.__f_); |
| 1972 | __p_ = _VSTD::move(__other.__p_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1973 | return *this; |
| 1974 | } |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1975 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1976 | void swap(packaged_task& __other) _NOEXCEPT |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1977 | { |
| 1978 | __f_.swap(__other.__f_); |
| 1979 | __p_.swap(__other.__p_); |
| 1980 | } |
| 1981 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1982 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 1983 | bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;} |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1984 | |
| 1985 | // result retrieval |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1986 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1987 | future<result_type> get_future() {return __p_.get_future();} |
| 1988 | |
| 1989 | // execution |
| 1990 | void operator()(_ArgTypes... __args); |
| 1991 | void make_ready_at_thread_exit(_ArgTypes... __args); |
| 1992 | |
| 1993 | void reset(); |
| 1994 | }; |
| 1995 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1996 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1997 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1998 | packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1999 | { |
| 2000 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2001 | if (__p_.__state_ == nullptr) |
| 2002 | throw future_error(make_error_code(future_errc::no_state)); |
| 2003 | if (__p_.__state_->__has_value()) |
| 2004 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 2005 | try |
| 2006 | { |
| 2007 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2008 | __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...)); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2009 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2010 | } |
| 2011 | catch (...) |
| 2012 | { |
| 2013 | __p_.set_exception(current_exception()); |
| 2014 | } |
| 2015 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2016 | } |
| 2017 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2018 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2019 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2020 | packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2021 | { |
| 2022 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2023 | if (__p_.__state_ == nullptr) |
| 2024 | throw future_error(make_error_code(future_errc::no_state)); |
| 2025 | if (__p_.__state_->__has_value()) |
| 2026 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 2027 | try |
| 2028 | { |
| 2029 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2030 | __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...)); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2031 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2032 | } |
| 2033 | catch (...) |
| 2034 | { |
| 2035 | __p_.set_exception_at_thread_exit(current_exception()); |
| 2036 | } |
| 2037 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2038 | } |
| 2039 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2040 | template<class _Rp, class ..._ArgTypes> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2041 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2042 | packaged_task<_Rp(_ArgTypes...)>::reset() |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2043 | { |
| 2044 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 2045 | if (!valid()) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2046 | throw future_error(make_error_code(future_errc::no_state)); |
| 2047 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2048 | __p_ = promise<result_type>(); |
| 2049 | } |
| 2050 | |
| 2051 | template<class ..._ArgTypes> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 2052 | class _LIBCPP_TYPE_VIS packaged_task<void(_ArgTypes...)> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2053 | { |
| 2054 | public: |
| 2055 | typedef void result_type; |
| 2056 | |
| 2057 | private: |
| 2058 | __packaged_task_function<result_type(_ArgTypes...)> __f_; |
| 2059 | promise<result_type> __p_; |
| 2060 | |
| 2061 | public: |
| 2062 | // construction and destruction |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2063 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2064 | packaged_task() _NOEXCEPT : __p_(nullptr) {} |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2065 | template <class _Fp> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2066 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2067 | explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {} |
| 2068 | template <class _Fp, class _Allocator> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2069 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2070 | explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f) |
| 2071 | : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)), |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2072 | __p_(allocator_arg, __a) {} |
| 2073 | // ~packaged_task() = default; |
| 2074 | |
| 2075 | // no copy |
Howard Hinnant | 8131a01 | 2012-07-21 19:34:12 +0000 | [diff] [blame] | 2076 | packaged_task(const packaged_task&) = delete; |
| 2077 | packaged_task& operator=(const packaged_task&) = delete; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2078 | |
| 2079 | // move support |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2080 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2081 | packaged_task(packaged_task&& __other) _NOEXCEPT |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2082 | : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2083 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2084 | packaged_task& operator=(packaged_task&& __other) _NOEXCEPT |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2085 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2086 | __f_ = _VSTD::move(__other.__f_); |
| 2087 | __p_ = _VSTD::move(__other.__p_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2088 | return *this; |
| 2089 | } |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2090 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2091 | void swap(packaged_task& __other) _NOEXCEPT |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2092 | { |
| 2093 | __f_.swap(__other.__f_); |
| 2094 | __p_.swap(__other.__p_); |
| 2095 | } |
| 2096 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2097 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2098 | bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;} |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2099 | |
| 2100 | // result retrieval |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2101 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2102 | future<result_type> get_future() {return __p_.get_future();} |
| 2103 | |
| 2104 | // execution |
| 2105 | void operator()(_ArgTypes... __args); |
| 2106 | void make_ready_at_thread_exit(_ArgTypes... __args); |
| 2107 | |
| 2108 | void reset(); |
| 2109 | }; |
| 2110 | |
| 2111 | template<class ..._ArgTypes> |
| 2112 | void |
| 2113 | packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) |
| 2114 | { |
| 2115 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2116 | if (__p_.__state_ == nullptr) |
| 2117 | throw future_error(make_error_code(future_errc::no_state)); |
| 2118 | if (__p_.__state_->__has_value()) |
| 2119 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 2120 | try |
| 2121 | { |
| 2122 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2123 | __f_(_VSTD::forward<_ArgTypes>(__args)...); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2124 | __p_.set_value(); |
| 2125 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2126 | } |
| 2127 | catch (...) |
| 2128 | { |
| 2129 | __p_.set_exception(current_exception()); |
| 2130 | } |
| 2131 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2132 | } |
| 2133 | |
| 2134 | template<class ..._ArgTypes> |
| 2135 | void |
| 2136 | packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) |
| 2137 | { |
| 2138 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2139 | if (__p_.__state_ == nullptr) |
| 2140 | throw future_error(make_error_code(future_errc::no_state)); |
| 2141 | if (__p_.__state_->__has_value()) |
| 2142 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 2143 | try |
| 2144 | { |
| 2145 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2146 | __f_(_VSTD::forward<_ArgTypes>(__args)...); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2147 | __p_.set_value_at_thread_exit(); |
| 2148 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2149 | } |
| 2150 | catch (...) |
| 2151 | { |
| 2152 | __p_.set_exception_at_thread_exit(current_exception()); |
| 2153 | } |
| 2154 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2155 | } |
| 2156 | |
| 2157 | template<class ..._ArgTypes> |
| 2158 | void |
| 2159 | packaged_task<void(_ArgTypes...)>::reset() |
| 2160 | { |
| 2161 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 2162 | if (!valid()) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2163 | throw future_error(make_error_code(future_errc::no_state)); |
| 2164 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2165 | __p_ = promise<result_type>(); |
| 2166 | } |
| 2167 | |
| 2168 | template <class _Callable> |
| 2169 | inline _LIBCPP_INLINE_VISIBILITY |
| 2170 | void |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2171 | swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2172 | { |
| 2173 | __x.swap(__y); |
| 2174 | } |
| 2175 | |
| 2176 | template <class _Callable, class _Alloc> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 2177 | struct _LIBCPP_TYPE_VIS uses_allocator<packaged_task<_Callable>, _Alloc> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2178 | : public true_type {}; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2179 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2180 | template <class _Rp, class _Fp> |
| 2181 | future<_Rp> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2182 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2183 | __make_deferred_assoc_state(_Fp&& __f) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2184 | #else |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2185 | __make_deferred_assoc_state(_Fp __f) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2186 | #endif |
| 2187 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2188 | unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> |
| 2189 | __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f))); |
| 2190 | return future<_Rp>(__h.get()); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2193 | template <class _Rp, class _Fp> |
| 2194 | future<_Rp> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2195 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2196 | __make_async_assoc_state(_Fp&& __f) |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2197 | #else |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2198 | __make_async_assoc_state(_Fp __f) |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2199 | #endif |
| 2200 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2201 | unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> |
| 2202 | __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f))); |
| 2203 | _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach(); |
| 2204 | return future<_Rp>(__h.get()); |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2207 | template <class _Fp, class... _Args> |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2208 | class __async_func |
| 2209 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2210 | tuple<_Fp, _Args...> __f_; |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2211 | |
| 2212 | public: |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2213 | typedef typename __invoke_of<_Fp, _Args...>::type _Rp; |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2214 | |
| 2215 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2216 | explicit __async_func(_Fp&& __f, _Args&&... __args) |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2217 | : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {} |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2218 | |
| 2219 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2220 | __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {} |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2221 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2222 | _Rp operator()() |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2223 | { |
| 2224 | typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index; |
| 2225 | return __execute(_Index()); |
| 2226 | } |
| 2227 | private: |
| 2228 | template <size_t ..._Indices> |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2229 | _Rp |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2230 | __execute(__tuple_indices<_Indices...>) |
| 2231 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2232 | return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...); |
Howard Hinnant | 57cff29 | 2011-05-19 15:05:04 +0000 | [diff] [blame] | 2233 | } |
| 2234 | }; |
| 2235 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2236 | template <class _Fp, class... _Args> |
| 2237 | future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type> |
| 2238 | async(launch __policy, _Fp&& __f, _Args&&... __args) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2239 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2240 | typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF; |
| 2241 | typedef typename _BF::_Rp _Rp; |
| 2242 | future<_Rp> __r; |
Howard Hinnant | f6d875f | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 2243 | if (int(__policy) & int(launch::async)) |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2244 | __r = _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)), |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2245 | __decay_copy(_VSTD::forward<_Args>(__args))...)); |
Howard Hinnant | f6d875f | 2011-12-02 19:36:40 +0000 | [diff] [blame] | 2246 | else if (int(__policy) & int(launch::deferred)) |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2247 | __r = _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)), |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2248 | __decay_copy(_VSTD::forward<_Args>(__args))...)); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2249 | return __r; |
| 2250 | } |
| 2251 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2252 | template <class _Fp, class... _Args> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2253 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2254 | future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type> |
| 2255 | async(_Fp&& __f, _Args&&... __args) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2256 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2257 | return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f), |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2258 | _VSTD::forward<_Args>(__args)...); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 2262 | |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2263 | // shared_future |
| 2264 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2265 | template <class _Rp> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 2266 | class _LIBCPP_TYPE_VIS shared_future |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2267 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2268 | __assoc_state<_Rp>* __state_; |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2269 | |
| 2270 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2271 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2272 | shared_future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2273 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2274 | shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) |
| 2275 | {if (__state_) __state_->__add_shared();} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2276 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2277 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2278 | shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2279 | {__f.__state_ = nullptr;} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2280 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2281 | shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2282 | {__rhs.__state_ = nullptr;} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2283 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2284 | ~shared_future(); |
| 2285 | shared_future& operator=(const shared_future& __rhs); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2286 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2287 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2288 | shared_future& operator=(shared_future&& __rhs) _NOEXCEPT |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2289 | { |
| 2290 | shared_future(std::move(__rhs)).swap(*this); |
| 2291 | return *this; |
| 2292 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2293 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2294 | |
| 2295 | // retrieving the value |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2296 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2297 | const _Rp& get() const {return __state_->copy();} |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2298 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2299 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2300 | void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2301 | |
| 2302 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2303 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2304 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2305 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2306 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2307 | void wait() const {__state_->wait();} |
| 2308 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2309 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2310 | future_status |
| 2311 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 2312 | {return __state_->wait_for(__rel_time);} |
| 2313 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2314 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2315 | future_status |
| 2316 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 2317 | {return __state_->wait_until(__abs_time);} |
| 2318 | }; |
| 2319 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2320 | template <class _Rp> |
| 2321 | shared_future<_Rp>::~shared_future() |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2322 | { |
| 2323 | if (__state_) |
| 2324 | __state_->__release_shared(); |
| 2325 | } |
| 2326 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2327 | template <class _Rp> |
| 2328 | shared_future<_Rp>& |
| 2329 | shared_future<_Rp>::operator=(const shared_future& __rhs) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2330 | { |
| 2331 | if (__rhs.__state_) |
| 2332 | __rhs.__state_->__add_shared(); |
| 2333 | if (__state_) |
| 2334 | __state_->__release_shared(); |
| 2335 | __state_ = __rhs.__state_; |
| 2336 | return *this; |
| 2337 | } |
| 2338 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2339 | template <class _Rp> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 2340 | class _LIBCPP_TYPE_VIS shared_future<_Rp&> |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2341 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2342 | __assoc_state<_Rp&>* __state_; |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2343 | |
| 2344 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2345 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2346 | shared_future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2347 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2348 | shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) |
| 2349 | {if (__state_) __state_->__add_shared();} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2350 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2351 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2352 | shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2353 | {__f.__state_ = nullptr;} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2354 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2355 | shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2356 | {__rhs.__state_ = nullptr;} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2357 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2358 | ~shared_future(); |
| 2359 | shared_future& operator=(const shared_future& __rhs); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2360 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2361 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2362 | shared_future& operator=(shared_future&& __rhs) _NOEXCEPT |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2363 | { |
| 2364 | shared_future(std::move(__rhs)).swap(*this); |
| 2365 | return *this; |
| 2366 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2367 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2368 | |
| 2369 | // retrieving the value |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2370 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2371 | _Rp& get() const {return __state_->copy();} |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2372 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2373 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2374 | void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2375 | |
| 2376 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2377 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2378 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2379 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2380 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2381 | void wait() const {__state_->wait();} |
| 2382 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2383 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2384 | future_status |
| 2385 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 2386 | {return __state_->wait_for(__rel_time);} |
| 2387 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2388 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2389 | future_status |
| 2390 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 2391 | {return __state_->wait_until(__abs_time);} |
| 2392 | }; |
| 2393 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2394 | template <class _Rp> |
| 2395 | shared_future<_Rp&>::~shared_future() |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2396 | { |
| 2397 | if (__state_) |
| 2398 | __state_->__release_shared(); |
| 2399 | } |
| 2400 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2401 | template <class _Rp> |
| 2402 | shared_future<_Rp&>& |
| 2403 | shared_future<_Rp&>::operator=(const shared_future& __rhs) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2404 | { |
| 2405 | if (__rhs.__state_) |
| 2406 | __rhs.__state_->__add_shared(); |
| 2407 | if (__state_) |
| 2408 | __state_->__release_shared(); |
| 2409 | __state_ = __rhs.__state_; |
| 2410 | return *this; |
| 2411 | } |
| 2412 | |
| 2413 | template <> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 2414 | class _LIBCPP_TYPE_VIS shared_future<void> |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2415 | { |
| 2416 | __assoc_sub_state* __state_; |
| 2417 | |
| 2418 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2419 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2420 | shared_future() _NOEXCEPT : __state_(nullptr) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2421 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2422 | shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) |
| 2423 | {if (__state_) __state_->__add_shared();} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2424 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2425 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2426 | shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2427 | {__f.__state_ = nullptr;} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2428 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2429 | shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2430 | {__rhs.__state_ = nullptr;} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2431 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2432 | ~shared_future(); |
| 2433 | shared_future& operator=(const shared_future& __rhs); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2434 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2435 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2436 | shared_future& operator=(shared_future&& __rhs) _NOEXCEPT |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2437 | { |
| 2438 | shared_future(std::move(__rhs)).swap(*this); |
| 2439 | return *this; |
| 2440 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2441 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2442 | |
| 2443 | // retrieving the value |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2444 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2445 | void get() const {__state_->copy();} |
| 2446 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2447 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2448 | void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);} |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2449 | |
| 2450 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2451 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2452 | bool valid() const _NOEXCEPT {return __state_ != nullptr;} |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2453 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2454 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2455 | void wait() const {__state_->wait();} |
| 2456 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2457 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2458 | future_status |
| 2459 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 2460 | {return __state_->wait_for(__rel_time);} |
| 2461 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2462 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2463 | future_status |
| 2464 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 2465 | {return __state_->wait_until(__abs_time);} |
| 2466 | }; |
| 2467 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2468 | template <class _Rp> |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2469 | inline _LIBCPP_INLINE_VISIBILITY |
| 2470 | void |
Howard Hinnant | 8bf01dd | 2012-07-21 17:46:55 +0000 | [diff] [blame] | 2471 | swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2472 | { |
| 2473 | __x.swap(__y); |
| 2474 | } |
| 2475 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2476 | template <class _Rp> |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 2477 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2478 | shared_future<_Rp> |
| 2479 | future<_Rp>::share() |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2480 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2481 | return shared_future<_Rp>(_VSTD::move(*this)); |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2484 | template <class _Rp> |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2485 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2486 | shared_future<_Rp&> |
| 2487 | future<_Rp&>::share() |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2488 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2489 | return shared_future<_Rp&>(_VSTD::move(*this)); |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Howard Hinnant | a445151 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 2492 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 2493 | |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame] | 2494 | inline _LIBCPP_INLINE_VISIBILITY |
| 2495 | shared_future<void> |
| 2496 | future<void>::share() |
| 2497 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2498 | return shared_future<void>(_VSTD::move(*this)); |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
Howard Hinnant | a445151 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 2501 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 2502 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2503 | _LIBCPP_END_NAMESPACE_STD |
| 2504 | |
| 2505 | #endif // _LIBCPP_FUTURE |