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 { }; |
| 43 | error_code make_error_code(future_errc e); |
| 44 | error_condition make_error_condition(future_errc e); |
| 45 | |
| 46 | const error_category& future_category(); |
| 47 | |
| 48 | class future_error |
| 49 | : public logic_error |
| 50 | { |
| 51 | public: |
| 52 | future_error(error_code ec); // exposition only |
| 53 | |
| 54 | const error_code& code() const throw(); |
| 55 | const char* what() const throw(); |
| 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); |
| 65 | promise(promise&& rhs); |
| 66 | promise(const promise& rhs) = delete; |
| 67 | ~promise(); |
| 68 | |
| 69 | // assignment |
| 70 | promise& operator=(promise&& rhs); |
| 71 | promise& operator=(const promise& rhs) = delete; |
| 72 | void swap(promise& other); |
| 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); |
| 95 | promise(promise&& rhs); |
| 96 | promise(const promise& rhs) = delete; |
| 97 | ~promise(); |
| 98 | |
| 99 | // assignment |
| 100 | promise& operator=(promise&& rhs); |
| 101 | promise& operator=(const promise& rhs) = delete; |
| 102 | void swap(promise& other); |
| 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); |
| 123 | promise(promise&& rhs); |
| 124 | promise(const promise& rhs) = delete; |
| 125 | ~promise(); |
| 126 | |
| 127 | // assignment |
| 128 | promise& operator=(promise&& rhs); |
| 129 | promise& operator=(const promise& rhs) = delete; |
| 130 | void swap(promise& other); |
| 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 | |
| 144 | template <class R> void swap(promise<R>& x, promise<R>& y); |
| 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: |
| 153 | future(); |
| 154 | future(future&&); |
| 155 | future(const future& rhs) = delete; |
| 156 | ~future(); |
| 157 | future& operator=(const future& rhs) = delete; |
| 158 | future& operator=(future&&); |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 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 |
| 165 | bool valid() const; |
| 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: |
| 180 | future(); |
| 181 | future(future&&); |
| 182 | future(const future& rhs) = delete; |
| 183 | ~future(); |
| 184 | future& operator=(const future& rhs) = delete; |
| 185 | future& operator=(future&&); |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 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 |
| 192 | bool valid() const; |
| 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: |
| 207 | future(); |
| 208 | future(future&&); |
| 209 | future(const future& rhs) = delete; |
| 210 | ~future(); |
| 211 | future& operator=(const future& rhs) = delete; |
| 212 | future& operator=(future&&); |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 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 |
| 219 | bool valid() const; |
| 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: |
| 234 | shared_future(); |
| 235 | shared_future(const shared_future& rhs); |
| 236 | shared_future(future<R>&&); |
| 237 | shared_future(shared_future&& rhs); |
| 238 | ~shared_future(); |
| 239 | shared_future& operator=(const shared_future& rhs); |
| 240 | shared_future& operator=(shared_future&& rhs); |
| 241 | |
| 242 | // retrieving the value |
| 243 | const R& get() const; |
| 244 | |
| 245 | // functions to check state |
| 246 | bool valid() const; |
| 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: |
| 261 | shared_future(); |
| 262 | shared_future(const shared_future& rhs); |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 263 | shared_future(future<R&>&&); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 264 | shared_future(shared_future&& rhs); |
| 265 | ~shared_future(); |
| 266 | shared_future& operator=(const shared_future& rhs); |
| 267 | shared_future& operator=(shared_future&& rhs); |
| 268 | |
| 269 | // retrieving the value |
| 270 | R& get() const; |
| 271 | |
| 272 | // functions to check state |
| 273 | bool valid() const; |
| 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: |
| 288 | shared_future(); |
| 289 | shared_future(const shared_future& rhs); |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 290 | shared_future(future<void>&&); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 291 | shared_future(shared_future&& rhs); |
| 292 | ~shared_future(); |
| 293 | shared_future& operator=(const shared_future& rhs); |
| 294 | shared_future& operator=(shared_future&& rhs); |
| 295 | |
| 296 | // retrieving the value |
| 297 | void get() const; |
| 298 | |
| 299 | // functions to check state |
| 300 | bool valid() const; |
| 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 |
| 328 | packaged_task(); |
| 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 |
| 336 | packaged_task(packaged_task&) = delete; |
| 337 | packaged_task& operator=(packaged_task&) = delete; |
| 338 | |
| 339 | // move support |
| 340 | packaged_task(packaged_task&& other); |
| 341 | packaged_task& operator=(packaged_task&& other); |
| 342 | void swap(packaged_task& other); |
| 343 | |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 344 | bool valid() const; |
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> |
| 357 | void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&); |
| 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 | |
| 373 | #pragma GCC system_header |
| 374 | |
| 375 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 376 | |
| 377 | //enum class future_errc |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 378 | struct _LIBCPP_VISIBLE future_errc |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 379 | { |
| 380 | enum _ { |
| 381 | broken_promise, |
| 382 | future_already_retrieved, |
| 383 | promise_already_satisfied, |
| 384 | no_state |
| 385 | }; |
| 386 | |
| 387 | _ __v_; |
| 388 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 389 | _LIBCPP_INLINE_VISIBILITY future_errc(_ __v) : __v_(__v) {} |
| 390 | _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 391 | |
| 392 | }; |
| 393 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 394 | template <> |
| 395 | struct _LIBCPP_VISIBLE is_error_code_enum<future_errc> : public true_type {}; |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 396 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 397 | //enum class launch |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 398 | struct _LIBCPP_VISIBLE launch |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 399 | { |
| 400 | enum _ { |
Howard Hinnant | 6689564 | 2010-11-23 18:33:54 +0000 | [diff] [blame] | 401 | async = 1, |
| 402 | deferred = 2, |
| 403 | any = async | deferred |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | _ __v_; |
| 407 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 408 | _LIBCPP_INLINE_VISIBILITY launch(_ __v) : __v_(__v) {} |
| 409 | _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 410 | |
| 411 | }; |
| 412 | |
| 413 | //enum class future_status |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 414 | struct _LIBCPP_VISIBLE future_status |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 415 | { |
| 416 | enum _ { |
| 417 | ready, |
| 418 | timeout, |
| 419 | deferred |
| 420 | }; |
| 421 | |
| 422 | _ __v_; |
| 423 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 424 | _LIBCPP_INLINE_VISIBILITY future_status(_ __v) : __v_(__v) {} |
| 425 | _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 426 | |
| 427 | }; |
| 428 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 429 | _LIBCPP_VISIBLE |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 430 | const error_category& future_category(); |
| 431 | |
| 432 | inline _LIBCPP_INLINE_VISIBILITY |
| 433 | error_code |
| 434 | make_error_code(future_errc __e) |
| 435 | { |
| 436 | return error_code(static_cast<int>(__e), future_category()); |
| 437 | } |
| 438 | |
| 439 | inline _LIBCPP_INLINE_VISIBILITY |
| 440 | error_condition |
| 441 | make_error_condition(future_errc __e) |
| 442 | { |
| 443 | return error_condition(static_cast<int>(__e), future_category()); |
| 444 | } |
| 445 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 446 | class _LIBCPP_EXCEPTION_ABI future_error |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 447 | : public logic_error |
| 448 | { |
| 449 | error_code __ec_; |
| 450 | public: |
| 451 | future_error(error_code __ec); |
| 452 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 453 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame] | 454 | const error_code& code() const throw() {return __ec_;} |
| 455 | }; |
| 456 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 457 | class __assoc_sub_state |
| 458 | : public __shared_count |
| 459 | { |
| 460 | protected: |
| 461 | exception_ptr __exception_; |
| 462 | mutable mutex __mut_; |
| 463 | mutable condition_variable __cv_; |
| 464 | unsigned __state_; |
| 465 | |
| 466 | virtual void __on_zero_shared(); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 467 | void __sub_wait(unique_lock<mutex>& __lk); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 468 | public: |
| 469 | enum |
| 470 | { |
| 471 | __constructed = 1, |
| 472 | __future_attached = 2, |
| 473 | ready = 4, |
| 474 | deferred = 8 |
| 475 | }; |
| 476 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 477 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 478 | __assoc_sub_state() : __state_(0) {} |
| 479 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 480 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 481 | bool __has_value() const |
| 482 | {return (__state_ & __constructed) || (__exception_ != nullptr);} |
| 483 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 484 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 485 | void __set_future_attached() {__state_ |= __future_attached;} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 486 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 487 | bool __has_future_attached() const {return __state_ & __future_attached;} |
| 488 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 489 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 490 | void __set_deferred() {__state_ |= deferred;} |
| 491 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 492 | void __make_ready(); |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 493 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 494 | bool __is_ready() const {return __state_ & ready;} |
| 495 | |
| 496 | void set_value(); |
| 497 | void set_value_at_thread_exit(); |
| 498 | |
| 499 | void set_exception(exception_ptr __p); |
| 500 | void set_exception_at_thread_exit(exception_ptr __p); |
| 501 | |
| 502 | void copy(); |
| 503 | |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 504 | void wait(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 505 | template <class _Rep, class _Period> |
| 506 | future_status |
| 507 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const; |
| 508 | template <class _Clock, class _Duration> |
| 509 | future_status |
| 510 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 511 | |
| 512 | virtual void __execute(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 513 | }; |
| 514 | |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 515 | template <class _Clock, class _Duration> |
| 516 | future_status |
| 517 | __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 518 | { |
| 519 | unique_lock<mutex> __lk(__mut_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 520 | if (__state_ & deferred) |
| 521 | return future_status::deferred; |
| 522 | while (!(__state_ & ready) && _Clock::now() < __abs_time) |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 523 | __cv_.wait_until(__lk, __abs_time); |
| 524 | if (__state_ & ready) |
| 525 | return future_status::ready; |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 526 | return future_status::timeout; |
| 527 | } |
| 528 | |
| 529 | template <class _Rep, class _Period> |
| 530 | inline _LIBCPP_INLINE_VISIBILITY |
| 531 | future_status |
| 532 | __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 533 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 534 | return wait_until(chrono::steady_clock::now() + __rel_time); |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 537 | template <class _R> |
| 538 | class __assoc_state |
| 539 | : public __assoc_sub_state |
| 540 | { |
| 541 | typedef __assoc_sub_state base; |
| 542 | typedef typename aligned_storage<sizeof(_R), alignment_of<_R>::value>::type _U; |
| 543 | protected: |
| 544 | _U __value_; |
| 545 | |
| 546 | virtual void __on_zero_shared(); |
| 547 | public: |
| 548 | |
| 549 | template <class _Arg> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 550 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 551 | void set_value(_Arg&& __arg); |
| 552 | #else |
| 553 | void set_value(_Arg& __arg); |
| 554 | #endif |
| 555 | |
| 556 | template <class _Arg> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 557 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 558 | void set_value_at_thread_exit(_Arg&& __arg); |
| 559 | #else |
| 560 | void set_value_at_thread_exit(_Arg& __arg); |
| 561 | #endif |
| 562 | |
| 563 | _R move(); |
| 564 | typename add_lvalue_reference<_R>::type copy(); |
| 565 | }; |
| 566 | |
| 567 | template <class _R> |
| 568 | void |
| 569 | __assoc_state<_R>::__on_zero_shared() |
| 570 | { |
| 571 | if (this->__state_ & base::__constructed) |
| 572 | reinterpret_cast<_R*>(&__value_)->~_R(); |
| 573 | delete this; |
| 574 | } |
| 575 | |
| 576 | template <class _R> |
| 577 | template <class _Arg> |
| 578 | void |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 579 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 580 | __assoc_state<_R>::set_value(_Arg&& __arg) |
| 581 | #else |
| 582 | __assoc_state<_R>::set_value(_Arg& __arg) |
| 583 | #endif |
| 584 | { |
| 585 | unique_lock<mutex> __lk(this->__mut_); |
| 586 | if (this->__has_value()) |
| 587 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 588 | ::new(&__value_) _R(_STD::forward<_Arg>(__arg)); |
| 589 | this->__state_ |= base::__constructed | base::ready; |
| 590 | __lk.unlock(); |
| 591 | __cv_.notify_all(); |
| 592 | } |
| 593 | |
| 594 | template <class _R> |
| 595 | template <class _Arg> |
| 596 | void |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 597 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 598 | __assoc_state<_R>::set_value_at_thread_exit(_Arg&& __arg) |
| 599 | #else |
| 600 | __assoc_state<_R>::set_value_at_thread_exit(_Arg& __arg) |
| 601 | #endif |
| 602 | { |
| 603 | unique_lock<mutex> __lk(this->__mut_); |
| 604 | if (this->__has_value()) |
| 605 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 606 | ::new(&__value_) _R(_STD::forward<_Arg>(__arg)); |
| 607 | this->__state_ |= base::__constructed; |
Howard Hinnant | 5306d68 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 608 | __thread_local_data()->__make_ready_at_thread_exit(this); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 609 | __lk.unlock(); |
| 610 | } |
| 611 | |
| 612 | template <class _R> |
| 613 | _R |
| 614 | __assoc_state<_R>::move() |
| 615 | { |
| 616 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 617 | this->__sub_wait(__lk); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 618 | if (this->__exception_ != nullptr) |
| 619 | rethrow_exception(this->__exception_); |
| 620 | return _STD::move(*reinterpret_cast<_R*>(&__value_)); |
| 621 | } |
| 622 | |
| 623 | template <class _R> |
| 624 | typename add_lvalue_reference<_R>::type |
| 625 | __assoc_state<_R>::copy() |
| 626 | { |
| 627 | unique_lock<mutex> __lk(this->__mut_); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 628 | this->__sub_wait(__lk); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 629 | if (this->__exception_ != nullptr) |
| 630 | rethrow_exception(this->__exception_); |
| 631 | return *reinterpret_cast<_R*>(&__value_); |
| 632 | } |
| 633 | |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 634 | template <class _R> |
| 635 | class __assoc_state<_R&> |
| 636 | : public __assoc_sub_state |
| 637 | { |
| 638 | typedef __assoc_sub_state base; |
| 639 | typedef _R* _U; |
| 640 | protected: |
| 641 | _U __value_; |
| 642 | |
| 643 | virtual void __on_zero_shared(); |
| 644 | public: |
| 645 | |
| 646 | void set_value(_R& __arg); |
| 647 | void set_value_at_thread_exit(_R& __arg); |
| 648 | |
| 649 | _R& copy(); |
| 650 | }; |
| 651 | |
| 652 | template <class _R> |
| 653 | void |
| 654 | __assoc_state<_R&>::__on_zero_shared() |
| 655 | { |
| 656 | delete this; |
| 657 | } |
| 658 | |
| 659 | template <class _R> |
| 660 | void |
| 661 | __assoc_state<_R&>::set_value(_R& __arg) |
| 662 | { |
| 663 | unique_lock<mutex> __lk(this->__mut_); |
| 664 | if (this->__has_value()) |
| 665 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 666 | __value_ = &__arg; |
| 667 | this->__state_ |= base::__constructed | base::ready; |
| 668 | __lk.unlock(); |
| 669 | __cv_.notify_all(); |
| 670 | } |
| 671 | |
| 672 | template <class _R> |
| 673 | void |
| 674 | __assoc_state<_R&>::set_value_at_thread_exit(_R& __arg) |
| 675 | { |
| 676 | unique_lock<mutex> __lk(this->__mut_); |
| 677 | if (this->__has_value()) |
| 678 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 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 | |
| 685 | template <class _R> |
| 686 | _R& |
| 687 | __assoc_state<_R&>::copy() |
| 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 | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 696 | template <class _R, class _Alloc> |
| 697 | class __assoc_state_alloc |
| 698 | : public __assoc_state<_R> |
| 699 | { |
| 700 | typedef __assoc_state<_R> base; |
| 701 | _Alloc __alloc_; |
| 702 | |
| 703 | virtual void __on_zero_shared(); |
| 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 | |
| 710 | template <class _R, class _Alloc> |
| 711 | void |
| 712 | __assoc_state_alloc<_R, _Alloc>::__on_zero_shared() |
| 713 | { |
| 714 | if (this->__state_ & base::__constructed) |
| 715 | reinterpret_cast<_R*>(&this->__value_)->~_R(); |
| 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 | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 721 | template <class _R, class _Alloc> |
| 722 | class __assoc_state_alloc<_R&, _Alloc> |
| 723 | : public __assoc_state<_R&> |
| 724 | { |
| 725 | typedef __assoc_state<_R&> base; |
| 726 | _Alloc __alloc_; |
| 727 | |
| 728 | virtual void __on_zero_shared(); |
| 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 | |
| 735 | template <class _R, class _Alloc> |
| 736 | void |
| 737 | __assoc_state_alloc<_R&, _Alloc>::__on_zero_shared() |
| 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 | |
| 751 | virtual void __on_zero_shared(); |
| 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 |
| 760 | __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() |
| 761 | { |
| 762 | this->~base(); |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 763 | typename _Alloc::template rebind<__assoc_sub_state_alloc>::other __a(__alloc_); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 764 | this->~__assoc_sub_state_alloc(); |
| 765 | __a.deallocate(this, 1); |
| 766 | } |
| 767 | |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 768 | template <class _R, class _F> |
| 769 | class __deferred_assoc_state |
| 770 | : public __assoc_state<_R> |
| 771 | { |
| 772 | typedef __assoc_state<_R> base; |
| 773 | |
| 774 | _F __func_; |
| 775 | |
| 776 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 777 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 778 | explicit __deferred_assoc_state(_F&& __f); |
| 779 | #endif |
| 780 | |
| 781 | virtual void __execute(); |
| 782 | }; |
| 783 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 784 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 785 | |
| 786 | template <class _R, class _F> |
| 787 | inline _LIBCPP_INLINE_VISIBILITY |
| 788 | __deferred_assoc_state<_R, _F>::__deferred_assoc_state(_F&& __f) |
| 789 | : __func_(_STD::forward<_F>(__f)) |
| 790 | { |
| 791 | this->__set_deferred(); |
| 792 | } |
| 793 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 794 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 795 | |
| 796 | template <class _R, class _F> |
| 797 | void |
| 798 | __deferred_assoc_state<_R, _F>::__execute() |
| 799 | { |
| 800 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 801 | try |
| 802 | { |
| 803 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 804 | this->set_value(__func_()); |
| 805 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 806 | } |
| 807 | catch (...) |
| 808 | { |
| 809 | this->set_exception(current_exception()); |
| 810 | } |
| 811 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 812 | } |
| 813 | |
| 814 | template <class _F> |
| 815 | class __deferred_assoc_state<void, _F> |
| 816 | : public __assoc_sub_state |
| 817 | { |
| 818 | typedef __assoc_sub_state base; |
| 819 | |
| 820 | _F __func_; |
| 821 | |
| 822 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 823 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 824 | explicit __deferred_assoc_state(_F&& __f); |
| 825 | #endif |
| 826 | |
| 827 | virtual void __execute(); |
| 828 | }; |
| 829 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 830 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 831 | |
| 832 | template <class _F> |
| 833 | inline _LIBCPP_INLINE_VISIBILITY |
| 834 | __deferred_assoc_state<void, _F>::__deferred_assoc_state(_F&& __f) |
| 835 | : __func_(_STD::forward<_F>(__f)) |
| 836 | { |
| 837 | this->__set_deferred(); |
| 838 | } |
| 839 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 840 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 841 | |
| 842 | template <class _F> |
| 843 | void |
| 844 | __deferred_assoc_state<void, _F>::__execute() |
| 845 | { |
| 846 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 847 | try |
| 848 | { |
| 849 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 850 | __func_(); |
| 851 | this->set_value(); |
| 852 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 853 | } |
| 854 | catch (...) |
| 855 | { |
| 856 | this->set_exception(current_exception()); |
| 857 | } |
| 858 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 859 | } |
| 860 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 861 | template <class> class promise; |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 862 | template <class> class shared_future; |
| 863 | template <class> class atomic_future; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 864 | |
| 865 | // future |
| 866 | |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 867 | template <class _R> class future; |
| 868 | |
| 869 | template <class _R, class _F> |
| 870 | future<_R> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 871 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 872 | __make_deferred_assoc_state(_F&& __f); |
| 873 | #else |
| 874 | __make_deferred_assoc_state(_F __f); |
| 875 | #endif |
| 876 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 877 | template <class _R> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 878 | class _LIBCPP_VISIBLE future |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 879 | { |
| 880 | __assoc_state<_R>* __state_; |
| 881 | |
| 882 | explicit future(__assoc_state<_R>* __state); |
| 883 | |
| 884 | template <class> friend class promise; |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 885 | template <class> friend class shared_future; |
| 886 | template <class> friend class atomic_future; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 887 | |
| 888 | template <class _R1, class _F> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 889 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 890 | friend future<_R1> __make_deferred_assoc_state(_F&& __f); |
| 891 | #else |
| 892 | friend future<_R1> __make_deferred_assoc_state(_F __f); |
| 893 | #endif |
| 894 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 895 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 896 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 897 | future() : __state_(nullptr) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 898 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 899 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 900 | future(future&& __rhs) |
| 901 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 902 | future(const future&) = delete; |
| 903 | future& operator=(const future&) = delete; |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 904 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 905 | future& operator=(future&& __rhs) |
| 906 | { |
| 907 | future(std::move(__rhs)).swap(*this); |
| 908 | return *this; |
| 909 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 910 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 911 | private: |
| 912 | future(const future&); |
| 913 | future& operator=(const future&); |
| 914 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 915 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 916 | ~future(); |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 917 | shared_future<_R> share(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 918 | |
| 919 | // retrieving the value |
| 920 | _R get(); |
| 921 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 922 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 923 | void swap(future& __rhs) {_STD::swap(__state_, __rhs.__state_);} |
| 924 | |
| 925 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 926 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 927 | bool valid() const {return __state_ != nullptr;} |
| 928 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 929 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 930 | void wait() const {__state_->wait();} |
| 931 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 932 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 933 | future_status |
| 934 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 935 | {return __state_->wait_for(__rel_time);} |
| 936 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 937 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 938 | future_status |
| 939 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 940 | {return __state_->wait_until(__abs_time);} |
| 941 | }; |
| 942 | |
| 943 | template <class _R> |
| 944 | future<_R>::future(__assoc_state<_R>* __state) |
| 945 | : __state_(__state) |
| 946 | { |
| 947 | if (__state_->__has_future_attached()) |
| 948 | throw future_error(make_error_code(future_errc::future_already_retrieved)); |
| 949 | __state_->__add_shared(); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 950 | __state_->__set_future_attached(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 953 | struct __release_shared_count |
| 954 | { |
| 955 | void operator()(__shared_count* p) {p->__release_shared();} |
| 956 | }; |
| 957 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 958 | template <class _R> |
| 959 | future<_R>::~future() |
| 960 | { |
| 961 | if (__state_) |
| 962 | __state_->__release_shared(); |
| 963 | } |
| 964 | |
| 965 | template <class _R> |
| 966 | _R |
| 967 | future<_R>::get() |
| 968 | { |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 969 | unique_ptr<__shared_count, __release_shared_count> __(__state_); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 970 | __assoc_state<_R>* __s = __state_; |
| 971 | __state_ = nullptr; |
| 972 | return __s->move(); |
| 973 | } |
| 974 | |
| 975 | template <class _R> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 976 | class _LIBCPP_VISIBLE future<_R&> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 977 | { |
| 978 | __assoc_state<_R&>* __state_; |
| 979 | |
| 980 | explicit future(__assoc_state<_R&>* __state); |
| 981 | |
| 982 | template <class> friend class promise; |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 983 | template <class> friend class shared_future; |
| 984 | template <class> friend class atomic_future; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 985 | |
| 986 | template <class _R1, class _F> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 987 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 988 | friend future<_R1> __make_deferred_assoc_state(_F&& __f); |
| 989 | #else |
| 990 | friend future<_R1> __make_deferred_assoc_state(_F __f); |
| 991 | #endif |
| 992 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 993 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 994 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 995 | future() : __state_(nullptr) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 996 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 997 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 998 | future(future&& __rhs) |
| 999 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1000 | future(const future&) = delete; |
| 1001 | future& operator=(const future&) = delete; |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1002 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1003 | future& operator=(future&& __rhs) |
| 1004 | { |
| 1005 | future(std::move(__rhs)).swap(*this); |
| 1006 | return *this; |
| 1007 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1008 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1009 | private: |
| 1010 | future(const future&); |
| 1011 | future& operator=(const future&); |
| 1012 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1013 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1014 | ~future(); |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 1015 | shared_future<_R&> share(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1016 | |
| 1017 | // retrieving the value |
| 1018 | _R& get(); |
| 1019 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1020 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1021 | void swap(future& __rhs) {_STD::swap(__state_, __rhs.__state_);} |
| 1022 | |
| 1023 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1024 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1025 | bool valid() const {return __state_ != nullptr;} |
| 1026 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1027 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1028 | void wait() const {__state_->wait();} |
| 1029 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1030 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1031 | future_status |
| 1032 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 1033 | {return __state_->wait_for(__rel_time);} |
| 1034 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1035 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1036 | future_status |
| 1037 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 1038 | {return __state_->wait_until(__abs_time);} |
| 1039 | }; |
| 1040 | |
| 1041 | template <class _R> |
| 1042 | future<_R&>::future(__assoc_state<_R&>* __state) |
| 1043 | : __state_(__state) |
| 1044 | { |
| 1045 | if (__state_->__has_future_attached()) |
| 1046 | throw future_error(make_error_code(future_errc::future_already_retrieved)); |
| 1047 | __state_->__add_shared(); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1048 | __state_->__set_future_attached(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | template <class _R> |
| 1052 | future<_R&>::~future() |
| 1053 | { |
| 1054 | if (__state_) |
| 1055 | __state_->__release_shared(); |
| 1056 | } |
| 1057 | |
| 1058 | template <class _R> |
| 1059 | _R& |
| 1060 | future<_R&>::get() |
| 1061 | { |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1062 | unique_ptr<__shared_count, __release_shared_count> __(__state_); |
Howard Hinnant | f39daa8 | 2010-08-28 21:01:06 +0000 | [diff] [blame] | 1063 | __assoc_state<_R&>* __s = __state_; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1064 | __state_ = nullptr; |
| 1065 | return __s->copy(); |
| 1066 | } |
| 1067 | |
| 1068 | template <> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1069 | class _LIBCPP_VISIBLE future<void> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1070 | { |
| 1071 | __assoc_sub_state* __state_; |
| 1072 | |
| 1073 | explicit future(__assoc_sub_state* __state); |
| 1074 | |
| 1075 | template <class> friend class promise; |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1076 | template <class> friend class shared_future; |
| 1077 | template <class> friend class atomic_future; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1078 | |
| 1079 | template <class _R1, class _F> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1080 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1081 | friend future<_R1> __make_deferred_assoc_state(_F&& __f); |
| 1082 | #else |
| 1083 | friend future<_R1> __make_deferred_assoc_state(_F __f); |
| 1084 | #endif |
| 1085 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1086 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1087 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1088 | future() : __state_(nullptr) {} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1089 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1090 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1091 | future(future&& __rhs) |
| 1092 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1093 | future(const future&) = delete; |
| 1094 | future& operator=(const future&) = delete; |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1095 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1096 | future& operator=(future&& __rhs) |
| 1097 | { |
| 1098 | future(std::move(__rhs)).swap(*this); |
| 1099 | return *this; |
| 1100 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1101 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1102 | private: |
| 1103 | future(const future&); |
| 1104 | future& operator=(const future&); |
| 1105 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1106 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1107 | ~future(); |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 1108 | shared_future<void> share(); |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1109 | |
| 1110 | // retrieving the value |
| 1111 | void get(); |
| 1112 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1113 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1114 | void swap(future& __rhs) {_STD::swap(__state_, __rhs.__state_);} |
| 1115 | |
| 1116 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1117 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1118 | bool valid() const {return __state_ != nullptr;} |
| 1119 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1120 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1121 | void wait() const {__state_->wait();} |
| 1122 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1123 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1124 | future_status |
| 1125 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 1126 | {return __state_->wait_for(__rel_time);} |
| 1127 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1128 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1129 | future_status |
| 1130 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 1131 | {return __state_->wait_until(__abs_time);} |
| 1132 | }; |
| 1133 | |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 1134 | template <class _R> |
| 1135 | inline _LIBCPP_INLINE_VISIBILITY |
| 1136 | void |
| 1137 | swap(future<_R>& __x, future<_R>& __y) |
| 1138 | { |
| 1139 | __x.swap(__y); |
| 1140 | } |
| 1141 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1142 | // promise<R> |
| 1143 | |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1144 | template <class> class packaged_task; |
| 1145 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1146 | template <class _R> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1147 | class _LIBCPP_VISIBLE promise |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1148 | { |
| 1149 | __assoc_state<_R>* __state_; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1150 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1151 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1152 | explicit promise(nullptr_t) : __state_(nullptr) {} |
| 1153 | |
| 1154 | template <class> friend class packaged_task; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1155 | public: |
| 1156 | promise(); |
| 1157 | template <class _Alloc> |
| 1158 | promise(allocator_arg_t, const _Alloc& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1159 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
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 | promise(promise&& __rhs) |
| 1162 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1163 | promise(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1164 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1165 | private: |
| 1166 | promise(const promise& __rhs); |
| 1167 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1168 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1169 | ~promise(); |
| 1170 | |
| 1171 | // assignment |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1172 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1173 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1174 | promise& operator=(promise&& __rhs) |
| 1175 | { |
| 1176 | promise(std::move(__rhs)).swap(*this); |
| 1177 | return *this; |
| 1178 | } |
| 1179 | promise& operator=(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1180 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1181 | private: |
| 1182 | promise& operator=(const promise& __rhs); |
| 1183 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1184 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1185 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1186 | void swap(promise& __rhs) {_STD::swap(__state_, __rhs.__state_);} |
| 1187 | |
| 1188 | // retrieving the result |
| 1189 | future<_R> get_future(); |
| 1190 | |
| 1191 | // setting the result |
| 1192 | void set_value(const _R& __r); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1193 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1194 | void set_value(_R&& __r); |
| 1195 | #endif |
| 1196 | void set_exception(exception_ptr __p); |
| 1197 | |
| 1198 | // setting the result with deferred notification |
| 1199 | void set_value_at_thread_exit(const _R& __r); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1200 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1201 | void set_value_at_thread_exit(_R&& __r); |
| 1202 | #endif |
| 1203 | void set_exception_at_thread_exit(exception_ptr __p); |
| 1204 | }; |
| 1205 | |
| 1206 | template <class _R> |
| 1207 | promise<_R>::promise() |
| 1208 | : __state_(new __assoc_state<_R>) |
| 1209 | { |
| 1210 | } |
| 1211 | |
| 1212 | template <class _R> |
| 1213 | template <class _Alloc> |
| 1214 | promise<_R>::promise(allocator_arg_t, const _Alloc& __a0) |
| 1215 | { |
| 1216 | typedef typename _Alloc::template rebind<__assoc_state_alloc<_R, _Alloc> >::other _A2; |
| 1217 | typedef __allocator_destructor<_A2> _D2; |
| 1218 | _A2 __a(__a0); |
| 1219 | unique_ptr<__assoc_state_alloc<_R, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1)); |
| 1220 | ::new(__hold.get()) __assoc_state_alloc<_R, _Alloc>(__a0); |
| 1221 | __state_ = __hold.release(); |
| 1222 | } |
| 1223 | |
| 1224 | template <class _R> |
| 1225 | promise<_R>::~promise() |
| 1226 | { |
| 1227 | if (__state_) |
| 1228 | { |
| 1229 | if (!__state_->__has_value() && __state_->use_count() > 1) |
| 1230 | __state_->set_exception(make_exception_ptr( |
| 1231 | future_error(make_error_code(future_errc::broken_promise)) |
| 1232 | )); |
| 1233 | __state_->__release_shared(); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | template <class _R> |
| 1238 | future<_R> |
| 1239 | promise<_R>::get_future() |
| 1240 | { |
| 1241 | if (__state_ == nullptr) |
| 1242 | throw future_error(make_error_code(future_errc::no_state)); |
| 1243 | return future<_R>(__state_); |
| 1244 | } |
| 1245 | |
| 1246 | template <class _R> |
| 1247 | void |
| 1248 | promise<_R>::set_value(const _R& __r) |
| 1249 | { |
| 1250 | if (__state_ == nullptr) |
| 1251 | throw future_error(make_error_code(future_errc::no_state)); |
| 1252 | __state_->set_value(__r); |
| 1253 | } |
| 1254 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1255 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1256 | |
| 1257 | template <class _R> |
| 1258 | void |
| 1259 | promise<_R>::set_value(_R&& __r) |
| 1260 | { |
| 1261 | if (__state_ == nullptr) |
| 1262 | throw future_error(make_error_code(future_errc::no_state)); |
| 1263 | __state_->set_value(_STD::move(__r)); |
| 1264 | } |
| 1265 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1266 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1267 | |
| 1268 | template <class _R> |
| 1269 | void |
| 1270 | promise<_R>::set_exception(exception_ptr __p) |
| 1271 | { |
| 1272 | if (__state_ == nullptr) |
| 1273 | throw future_error(make_error_code(future_errc::no_state)); |
| 1274 | __state_->set_exception(__p); |
| 1275 | } |
| 1276 | |
| 1277 | template <class _R> |
| 1278 | void |
| 1279 | promise<_R>::set_value_at_thread_exit(const _R& __r) |
| 1280 | { |
| 1281 | if (__state_ == nullptr) |
| 1282 | throw future_error(make_error_code(future_errc::no_state)); |
| 1283 | __state_->set_value_at_thread_exit(__r); |
| 1284 | } |
| 1285 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1286 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1287 | |
| 1288 | template <class _R> |
| 1289 | void |
| 1290 | promise<_R>::set_value_at_thread_exit(_R&& __r) |
| 1291 | { |
| 1292 | if (__state_ == nullptr) |
| 1293 | throw future_error(make_error_code(future_errc::no_state)); |
| 1294 | __state_->set_value_at_thread_exit(_STD::move(__r)); |
| 1295 | } |
| 1296 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1297 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1298 | |
| 1299 | template <class _R> |
| 1300 | void |
| 1301 | promise<_R>::set_exception_at_thread_exit(exception_ptr __p) |
| 1302 | { |
| 1303 | if (__state_ == nullptr) |
| 1304 | throw future_error(make_error_code(future_errc::no_state)); |
| 1305 | __state_->set_exception_at_thread_exit(__p); |
| 1306 | } |
| 1307 | |
| 1308 | // promise<R&> |
| 1309 | |
| 1310 | template <class _R> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1311 | class _LIBCPP_VISIBLE promise<_R&> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1312 | { |
| 1313 | __assoc_state<_R&>* __state_; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1314 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1315 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1316 | explicit promise(nullptr_t) : __state_(nullptr) {} |
| 1317 | |
| 1318 | template <class> friend class packaged_task; |
| 1319 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1320 | public: |
| 1321 | promise(); |
| 1322 | template <class _Allocator> |
| 1323 | promise(allocator_arg_t, const _Allocator& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1324 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1325 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1326 | promise(promise&& __rhs) |
| 1327 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1328 | promise(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1329 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1330 | private: |
| 1331 | promise(const promise& __rhs); |
| 1332 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1333 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1334 | ~promise(); |
| 1335 | |
| 1336 | // assignment |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1337 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1338 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1339 | promise& operator=(promise&& __rhs) |
| 1340 | { |
| 1341 | promise(std::move(__rhs)).swap(*this); |
| 1342 | return *this; |
| 1343 | } |
| 1344 | promise& operator=(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1345 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1346 | private: |
| 1347 | promise& operator=(const promise& __rhs); |
| 1348 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1349 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1350 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1351 | void swap(promise& __rhs) {_STD::swap(__state_, __rhs.__state_);} |
| 1352 | |
| 1353 | // retrieving the result |
| 1354 | future<_R&> get_future(); |
| 1355 | |
| 1356 | // setting the result |
| 1357 | void set_value(_R& __r); |
| 1358 | void set_exception(exception_ptr __p); |
| 1359 | |
| 1360 | // setting the result with deferred notification |
| 1361 | void set_value_at_thread_exit(_R&); |
| 1362 | void set_exception_at_thread_exit(exception_ptr __p); |
| 1363 | }; |
| 1364 | |
| 1365 | template <class _R> |
| 1366 | promise<_R&>::promise() |
| 1367 | : __state_(new __assoc_state<_R&>) |
| 1368 | { |
| 1369 | } |
| 1370 | |
| 1371 | template <class _R> |
| 1372 | template <class _Alloc> |
| 1373 | promise<_R&>::promise(allocator_arg_t, const _Alloc& __a0) |
| 1374 | { |
| 1375 | typedef typename _Alloc::template rebind<__assoc_state_alloc<_R&, _Alloc> >::other _A2; |
| 1376 | typedef __allocator_destructor<_A2> _D2; |
| 1377 | _A2 __a(__a0); |
| 1378 | unique_ptr<__assoc_state_alloc<_R&, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1)); |
| 1379 | ::new(__hold.get()) __assoc_state_alloc<_R&, _Alloc>(__a0); |
| 1380 | __state_ = __hold.release(); |
| 1381 | } |
| 1382 | |
| 1383 | template <class _R> |
| 1384 | promise<_R&>::~promise() |
| 1385 | { |
| 1386 | if (__state_) |
| 1387 | { |
| 1388 | if (!__state_->__has_value() && __state_->use_count() > 1) |
| 1389 | __state_->set_exception(make_exception_ptr( |
| 1390 | future_error(make_error_code(future_errc::broken_promise)) |
| 1391 | )); |
| 1392 | __state_->__release_shared(); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | template <class _R> |
| 1397 | future<_R&> |
| 1398 | promise<_R&>::get_future() |
| 1399 | { |
| 1400 | if (__state_ == nullptr) |
| 1401 | throw future_error(make_error_code(future_errc::no_state)); |
| 1402 | return future<_R&>(__state_); |
| 1403 | } |
| 1404 | |
| 1405 | template <class _R> |
| 1406 | void |
| 1407 | promise<_R&>::set_value(_R& __r) |
| 1408 | { |
| 1409 | if (__state_ == nullptr) |
| 1410 | throw future_error(make_error_code(future_errc::no_state)); |
| 1411 | __state_->set_value(__r); |
| 1412 | } |
| 1413 | |
| 1414 | template <class _R> |
| 1415 | void |
| 1416 | promise<_R&>::set_exception(exception_ptr __p) |
| 1417 | { |
| 1418 | if (__state_ == nullptr) |
| 1419 | throw future_error(make_error_code(future_errc::no_state)); |
| 1420 | __state_->set_exception(__p); |
| 1421 | } |
| 1422 | |
| 1423 | template <class _R> |
| 1424 | void |
| 1425 | promise<_R&>::set_value_at_thread_exit(_R& __r) |
| 1426 | { |
| 1427 | if (__state_ == nullptr) |
| 1428 | throw future_error(make_error_code(future_errc::no_state)); |
| 1429 | __state_->set_value_at_thread_exit(__r); |
| 1430 | } |
| 1431 | |
| 1432 | template <class _R> |
| 1433 | void |
| 1434 | promise<_R&>::set_exception_at_thread_exit(exception_ptr __p) |
| 1435 | { |
| 1436 | if (__state_ == nullptr) |
| 1437 | throw future_error(make_error_code(future_errc::no_state)); |
| 1438 | __state_->set_exception_at_thread_exit(__p); |
| 1439 | } |
| 1440 | |
| 1441 | // promise<void> |
| 1442 | |
| 1443 | template <> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1444 | class _LIBCPP_VISIBLE promise<void> |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1445 | { |
| 1446 | __assoc_sub_state* __state_; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1447 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1448 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1449 | explicit promise(nullptr_t) : __state_(nullptr) {} |
| 1450 | |
| 1451 | template <class> friend class packaged_task; |
| 1452 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1453 | public: |
| 1454 | promise(); |
| 1455 | template <class _Allocator> |
| 1456 | promise(allocator_arg_t, const _Allocator& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1457 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1458 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1459 | promise(promise&& __rhs) |
| 1460 | : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;} |
| 1461 | promise(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1462 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1463 | private: |
| 1464 | promise(const promise& __rhs); |
| 1465 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1466 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1467 | ~promise(); |
| 1468 | |
| 1469 | // assignment |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1470 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1471 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1472 | promise& operator=(promise&& __rhs) |
| 1473 | { |
| 1474 | promise(std::move(__rhs)).swap(*this); |
| 1475 | return *this; |
| 1476 | } |
| 1477 | promise& operator=(const promise& __rhs) = delete; |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1478 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1479 | private: |
| 1480 | promise& operator=(const promise& __rhs); |
| 1481 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1482 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1483 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1484 | void swap(promise& __rhs) {_STD::swap(__state_, __rhs.__state_);} |
| 1485 | |
| 1486 | // retrieving the result |
| 1487 | future<void> get_future(); |
| 1488 | |
| 1489 | // setting the result |
| 1490 | void set_value(); |
| 1491 | void set_exception(exception_ptr __p); |
| 1492 | |
| 1493 | // setting the result with deferred notification |
| 1494 | void set_value_at_thread_exit(); |
| 1495 | void set_exception_at_thread_exit(exception_ptr __p); |
| 1496 | }; |
| 1497 | |
| 1498 | template <class _Alloc> |
| 1499 | promise<void>::promise(allocator_arg_t, const _Alloc& __a0) |
| 1500 | { |
| 1501 | typedef typename _Alloc::template rebind<__assoc_sub_state_alloc<_Alloc> >::other _A2; |
| 1502 | typedef __allocator_destructor<_A2> _D2; |
| 1503 | _A2 __a(__a0); |
| 1504 | unique_ptr<__assoc_sub_state_alloc<_Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1)); |
| 1505 | ::new(__hold.get()) __assoc_sub_state_alloc<_Alloc>(__a0); |
| 1506 | __state_ = __hold.release(); |
| 1507 | } |
| 1508 | |
| 1509 | template <class _R> |
| 1510 | inline _LIBCPP_INLINE_VISIBILITY |
| 1511 | void |
| 1512 | swap(promise<_R>& __x, promise<_R>& __y) |
| 1513 | { |
| 1514 | __x.swap(__y); |
| 1515 | } |
| 1516 | |
| 1517 | template <class _R, class _Alloc> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1518 | struct _LIBCPP_VISIBLE uses_allocator<promise<_R>, _Alloc> |
| 1519 | : public true_type {}; |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 1520 | |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1521 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 1522 | |
| 1523 | // packaged_task |
| 1524 | |
| 1525 | template<class _Fp> class __packaged_task_base; |
| 1526 | |
| 1527 | template<class _R, class ..._ArgTypes> |
| 1528 | class __packaged_task_base<_R(_ArgTypes...)> |
| 1529 | { |
| 1530 | __packaged_task_base(const __packaged_task_base&); |
| 1531 | __packaged_task_base& operator=(const __packaged_task_base&); |
| 1532 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1533 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1534 | __packaged_task_base() {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1535 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1536 | virtual ~__packaged_task_base() {} |
| 1537 | virtual void __move_to(__packaged_task_base*) = 0; |
| 1538 | virtual void destroy() = 0; |
| 1539 | virtual void destroy_deallocate() = 0; |
| 1540 | virtual _R operator()(_ArgTypes&& ...) = 0; |
| 1541 | }; |
| 1542 | |
| 1543 | template<class _FD, class _Alloc, class _FB> class __packaged_task_func; |
| 1544 | |
| 1545 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1546 | class __packaged_task_func<_F, _Alloc, _R(_ArgTypes...)> |
| 1547 | : public __packaged_task_base<_R(_ArgTypes...)> |
| 1548 | { |
| 1549 | __compressed_pair<_F, _Alloc> __f_; |
| 1550 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1551 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1552 | explicit __packaged_task_func(const _F& __f) : __f_(__f) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1553 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1554 | explicit __packaged_task_func(_F&& __f) : __f_(_STD::move(__f)) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1555 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1556 | __packaged_task_func(const _F& __f, const _Alloc& __a) |
| 1557 | : __f_(__f, __a) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1558 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1559 | __packaged_task_func(_F&& __f, const _Alloc& __a) |
| 1560 | : __f_(_STD::move(__f), __a) {} |
| 1561 | virtual void __move_to(__packaged_task_base<_R(_ArgTypes...)>*); |
| 1562 | virtual void destroy(); |
| 1563 | virtual void destroy_deallocate(); |
| 1564 | virtual _R operator()(_ArgTypes&& ... __args); |
| 1565 | }; |
| 1566 | |
| 1567 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1568 | void |
| 1569 | __packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::__move_to( |
| 1570 | __packaged_task_base<_R(_ArgTypes...)>* __p) |
| 1571 | { |
| 1572 | ::new (__p) __packaged_task_func(_STD::move(__f_.first()), _STD::move(__f_.second())); |
| 1573 | } |
| 1574 | |
| 1575 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1576 | void |
| 1577 | __packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy() |
| 1578 | { |
| 1579 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 1580 | } |
| 1581 | |
| 1582 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1583 | void |
| 1584 | __packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy_deallocate() |
| 1585 | { |
| 1586 | typedef typename _Alloc::template rebind<__packaged_task_func>::other _A; |
| 1587 | _A __a(__f_.second()); |
| 1588 | __f_.~__compressed_pair<_F, _Alloc>(); |
| 1589 | __a.deallocate(this, 1); |
| 1590 | } |
| 1591 | |
| 1592 | template<class _F, class _Alloc, class _R, class ..._ArgTypes> |
| 1593 | _R |
| 1594 | __packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg) |
| 1595 | { |
| 1596 | return __invoke(__f_.first(), _STD::forward<_ArgTypes>(__arg)...); |
| 1597 | } |
| 1598 | |
| 1599 | template <class> class __packaged_task_function; |
| 1600 | |
| 1601 | template<class _R, class ..._ArgTypes> |
| 1602 | class __packaged_task_function<_R(_ArgTypes...)> |
| 1603 | { |
| 1604 | typedef __packaged_task_base<_R(_ArgTypes...)> __base; |
| 1605 | aligned_storage<3*sizeof(void*)>::type __buf_; |
| 1606 | __base* __f_; |
| 1607 | |
| 1608 | public: |
| 1609 | typedef _R result_type; |
| 1610 | |
| 1611 | // construct/copy/destroy: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1612 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1613 | __packaged_task_function() : __f_(nullptr) {} |
| 1614 | template<class _F> |
| 1615 | __packaged_task_function(_F&& __f); |
| 1616 | template<class _F, class _Alloc> |
| 1617 | __packaged_task_function(allocator_arg_t, const _Alloc& __a, _F&& __f); |
| 1618 | |
| 1619 | __packaged_task_function(__packaged_task_function&&); |
| 1620 | __packaged_task_function& operator=(__packaged_task_function&&); |
| 1621 | |
| 1622 | __packaged_task_function(const __packaged_task_function&) = delete; |
| 1623 | __packaged_task_function& operator=(const __packaged_task_function&) = delete; |
| 1624 | |
| 1625 | ~__packaged_task_function(); |
| 1626 | |
| 1627 | void swap(__packaged_task_function&); |
| 1628 | |
| 1629 | _R operator()(_ArgTypes...) const; |
| 1630 | }; |
| 1631 | |
| 1632 | template<class _R, class ..._ArgTypes> |
| 1633 | __packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) |
| 1634 | { |
| 1635 | if (__f.__f_ == nullptr) |
| 1636 | __f_ = nullptr; |
| 1637 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1638 | { |
| 1639 | __f_ = (__base*)&__buf_; |
| 1640 | __f.__f_->__move_to(__f_); |
| 1641 | } |
| 1642 | else |
| 1643 | { |
| 1644 | __f_ = __f.__f_; |
| 1645 | __f.__f_ = nullptr; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | template<class _R, class ..._ArgTypes> |
| 1650 | template <class _F> |
| 1651 | __packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(_F&& __f) |
| 1652 | : __f_(nullptr) |
| 1653 | { |
| 1654 | typedef typename remove_reference<_F>::type _FR; |
| 1655 | typedef __packaged_task_func<_FR, allocator<_FR>, _R(_ArgTypes...)> _FF; |
| 1656 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1657 | { |
| 1658 | __f_ = (__base*)&__buf_; |
| 1659 | ::new (__f_) _FF(_STD::forward<_F>(__f)); |
| 1660 | } |
| 1661 | else |
| 1662 | { |
| 1663 | typedef allocator<_FF> _A; |
| 1664 | _A __a; |
| 1665 | typedef __allocator_destructor<_A> _D; |
| 1666 | unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 1667 | ::new (__hold.get()) _FF(_STD::forward<_F>(__f), allocator<_FR>(__a)); |
| 1668 | __f_ = __hold.release(); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | template<class _R, class ..._ArgTypes> |
| 1673 | template <class _F, class _Alloc> |
| 1674 | __packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function( |
| 1675 | allocator_arg_t, const _Alloc& __a0, _F&& __f) |
| 1676 | : __f_(nullptr) |
| 1677 | { |
| 1678 | typedef allocator_traits<_Alloc> __alloc_traits; |
| 1679 | typedef typename remove_reference<_F>::type _FR; |
| 1680 | typedef __packaged_task_func<_FR, _Alloc, _R(_ArgTypes...)> _FF; |
| 1681 | if (sizeof(_FF) <= sizeof(__buf_)) |
| 1682 | { |
| 1683 | __f_ = (__base*)&__buf_; |
| 1684 | ::new (__f_) _FF(_STD::forward<_F>(__f)); |
| 1685 | } |
| 1686 | else |
| 1687 | { |
| 1688 | typedef typename __alloc_traits::template |
| 1689 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 1690 | rebind_alloc<_FF> |
| 1691 | #else |
| 1692 | rebind_alloc<_FF>::other |
| 1693 | #endif |
| 1694 | _A; |
| 1695 | _A __a(__a0); |
| 1696 | typedef __allocator_destructor<_A> _D; |
| 1697 | unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); |
| 1698 | ::new (__hold.get()) _FF(_STD::forward<_F>(__f), _Alloc(__a)); |
| 1699 | __f_ = __hold.release(); |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | template<class _R, class ..._ArgTypes> |
| 1704 | __packaged_task_function<_R(_ArgTypes...)>& |
| 1705 | __packaged_task_function<_R(_ArgTypes...)>::operator=(__packaged_task_function&& __f) |
| 1706 | { |
| 1707 | if (__f_ == (__base*)&__buf_) |
| 1708 | __f_->destroy(); |
| 1709 | else if (__f_) |
| 1710 | __f_->destroy_deallocate(); |
| 1711 | __f_ = nullptr; |
| 1712 | if (__f.__f_ == nullptr) |
| 1713 | __f_ = nullptr; |
| 1714 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1715 | { |
| 1716 | __f_ = (__base*)&__buf_; |
| 1717 | __f.__f_->__move_to(__f_); |
| 1718 | } |
| 1719 | else |
| 1720 | { |
| 1721 | __f_ = __f.__f_; |
| 1722 | __f.__f_ = nullptr; |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | template<class _R, class ..._ArgTypes> |
| 1727 | __packaged_task_function<_R(_ArgTypes...)>::~__packaged_task_function() |
| 1728 | { |
| 1729 | if (__f_ == (__base*)&__buf_) |
| 1730 | __f_->destroy(); |
| 1731 | else if (__f_) |
| 1732 | __f_->destroy_deallocate(); |
| 1733 | } |
| 1734 | |
| 1735 | template<class _R, class ..._ArgTypes> |
| 1736 | void |
| 1737 | __packaged_task_function<_R(_ArgTypes...)>::swap(__packaged_task_function& __f) |
| 1738 | { |
| 1739 | if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) |
| 1740 | { |
| 1741 | typename aligned_storage<sizeof(__buf_)>::type __tempbuf; |
| 1742 | __base* __t = (__base*)&__tempbuf; |
| 1743 | __f_->__move_to(__t); |
| 1744 | __f_->destroy(); |
| 1745 | __f_ = nullptr; |
| 1746 | __f.__f_->__move_to((__base*)&__buf_); |
| 1747 | __f.__f_->destroy(); |
| 1748 | __f.__f_ = nullptr; |
| 1749 | __f_ = (__base*)&__buf_; |
| 1750 | __t->__move_to((__base*)&__f.__buf_); |
| 1751 | __t->destroy(); |
| 1752 | __f.__f_ = (__base*)&__f.__buf_; |
| 1753 | } |
| 1754 | else if (__f_ == (__base*)&__buf_) |
| 1755 | { |
| 1756 | __f_->__move_to((__base*)&__f.__buf_); |
| 1757 | __f_->destroy(); |
| 1758 | __f_ = __f.__f_; |
| 1759 | __f.__f_ = (__base*)&__f.__buf_; |
| 1760 | } |
| 1761 | else if (__f.__f_ == (__base*)&__f.__buf_) |
| 1762 | { |
| 1763 | __f.__f_->__move_to((__base*)&__buf_); |
| 1764 | __f.__f_->destroy(); |
| 1765 | __f.__f_ = __f_; |
| 1766 | __f_ = (__base*)&__buf_; |
| 1767 | } |
| 1768 | else |
| 1769 | _STD::swap(__f_, __f.__f_); |
| 1770 | } |
| 1771 | |
| 1772 | template<class _R, class ..._ArgTypes> |
| 1773 | inline _LIBCPP_INLINE_VISIBILITY |
| 1774 | _R |
| 1775 | __packaged_task_function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const |
| 1776 | { |
| 1777 | return (*__f_)(_STD::forward<_ArgTypes>(__arg)...); |
| 1778 | } |
| 1779 | |
| 1780 | template<class _R, class ..._ArgTypes> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1781 | class _LIBCPP_VISIBLE packaged_task<_R(_ArgTypes...)> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1782 | { |
| 1783 | public: |
| 1784 | typedef _R result_type; |
| 1785 | |
| 1786 | private: |
| 1787 | __packaged_task_function<result_type(_ArgTypes...)> __f_; |
| 1788 | promise<result_type> __p_; |
| 1789 | |
| 1790 | public: |
| 1791 | // construction and destruction |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1792 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1793 | packaged_task() : __p_(nullptr) {} |
| 1794 | template <class _F> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1795 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1796 | explicit packaged_task(_F&& __f) : __f_(_STD::forward<_F>(__f)) {} |
| 1797 | template <class _F, class _Allocator> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1798 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1799 | explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f) |
| 1800 | : __f_(allocator_arg, __a, _STD::forward<_F>(__f)), |
| 1801 | __p_(allocator_arg, __a) {} |
| 1802 | // ~packaged_task() = default; |
| 1803 | |
| 1804 | // no copy |
| 1805 | packaged_task(packaged_task&) = delete; |
| 1806 | packaged_task& operator=(packaged_task&) = delete; |
| 1807 | |
| 1808 | // move support |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1809 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1810 | packaged_task(packaged_task&& __other) |
| 1811 | : __f_(_STD::move(__other.__f_)), __p_(_STD::move(__other.__p_)) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1812 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1813 | packaged_task& operator=(packaged_task&& __other) |
| 1814 | { |
| 1815 | __f_ = _STD::move(__other.__f_); |
| 1816 | __p_ = _STD::move(__other.__p_); |
| 1817 | return *this; |
| 1818 | } |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1819 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1820 | void swap(packaged_task& __other) |
| 1821 | { |
| 1822 | __f_.swap(__other.__f_); |
| 1823 | __p_.swap(__other.__p_); |
| 1824 | } |
| 1825 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1826 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 1827 | bool valid() const {return __p_.__state_ != nullptr;} |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1828 | |
| 1829 | // result retrieval |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1830 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1831 | future<result_type> get_future() {return __p_.get_future();} |
| 1832 | |
| 1833 | // execution |
| 1834 | void operator()(_ArgTypes... __args); |
| 1835 | void make_ready_at_thread_exit(_ArgTypes... __args); |
| 1836 | |
| 1837 | void reset(); |
| 1838 | }; |
| 1839 | |
| 1840 | template<class _R, class ..._ArgTypes> |
| 1841 | void |
| 1842 | packaged_task<_R(_ArgTypes...)>::operator()(_ArgTypes... __args) |
| 1843 | { |
| 1844 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1845 | if (__p_.__state_ == nullptr) |
| 1846 | throw future_error(make_error_code(future_errc::no_state)); |
| 1847 | if (__p_.__state_->__has_value()) |
| 1848 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 1849 | try |
| 1850 | { |
| 1851 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1852 | __p_.set_value(__f_(_STD::forward<_ArgTypes>(__args)...)); |
| 1853 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1854 | } |
| 1855 | catch (...) |
| 1856 | { |
| 1857 | __p_.set_exception(current_exception()); |
| 1858 | } |
| 1859 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1860 | } |
| 1861 | |
| 1862 | template<class _R, class ..._ArgTypes> |
| 1863 | void |
| 1864 | packaged_task<_R(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) |
| 1865 | { |
| 1866 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1867 | if (__p_.__state_ == nullptr) |
| 1868 | throw future_error(make_error_code(future_errc::no_state)); |
| 1869 | if (__p_.__state_->__has_value()) |
| 1870 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 1871 | try |
| 1872 | { |
| 1873 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1874 | __p_.set_value_at_thread_exit(__f_(_STD::forward<_ArgTypes>(__args)...)); |
| 1875 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1876 | } |
| 1877 | catch (...) |
| 1878 | { |
| 1879 | __p_.set_exception_at_thread_exit(current_exception()); |
| 1880 | } |
| 1881 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1882 | } |
| 1883 | |
| 1884 | template<class _R, class ..._ArgTypes> |
| 1885 | void |
| 1886 | packaged_task<_R(_ArgTypes...)>::reset() |
| 1887 | { |
| 1888 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 1889 | if (!valid()) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1890 | throw future_error(make_error_code(future_errc::no_state)); |
| 1891 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1892 | __p_ = promise<result_type>(); |
| 1893 | } |
| 1894 | |
| 1895 | template<class ..._ArgTypes> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1896 | class _LIBCPP_VISIBLE packaged_task<void(_ArgTypes...)> |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1897 | { |
| 1898 | public: |
| 1899 | typedef void result_type; |
| 1900 | |
| 1901 | private: |
| 1902 | __packaged_task_function<result_type(_ArgTypes...)> __f_; |
| 1903 | promise<result_type> __p_; |
| 1904 | |
| 1905 | public: |
| 1906 | // construction and destruction |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1907 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1908 | packaged_task() : __p_(nullptr) {} |
| 1909 | template <class _F> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1910 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1911 | explicit packaged_task(_F&& __f) : __f_(_STD::forward<_F>(__f)) {} |
| 1912 | template <class _F, class _Allocator> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1913 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1914 | explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f) |
| 1915 | : __f_(allocator_arg, __a, _STD::forward<_F>(__f)), |
| 1916 | __p_(allocator_arg, __a) {} |
| 1917 | // ~packaged_task() = default; |
| 1918 | |
| 1919 | // no copy |
| 1920 | packaged_task(packaged_task&) = delete; |
| 1921 | packaged_task& operator=(packaged_task&) = delete; |
| 1922 | |
| 1923 | // move support |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1924 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1925 | packaged_task(packaged_task&& __other) |
| 1926 | : __f_(_STD::move(__other.__f_)), __p_(_STD::move(__other.__p_)) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1927 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1928 | packaged_task& operator=(packaged_task&& __other) |
| 1929 | { |
| 1930 | __f_ = _STD::move(__other.__f_); |
| 1931 | __p_ = _STD::move(__other.__p_); |
| 1932 | return *this; |
| 1933 | } |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1934 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1935 | void swap(packaged_task& __other) |
| 1936 | { |
| 1937 | __f_.swap(__other.__f_); |
| 1938 | __p_.swap(__other.__p_); |
| 1939 | } |
| 1940 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1941 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 1942 | bool valid() const {return __p_.__state_ != nullptr;} |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1943 | |
| 1944 | // result retrieval |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 1945 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1946 | future<result_type> get_future() {return __p_.get_future();} |
| 1947 | |
| 1948 | // execution |
| 1949 | void operator()(_ArgTypes... __args); |
| 1950 | void make_ready_at_thread_exit(_ArgTypes... __args); |
| 1951 | |
| 1952 | void reset(); |
| 1953 | }; |
| 1954 | |
| 1955 | template<class ..._ArgTypes> |
| 1956 | void |
| 1957 | packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) |
| 1958 | { |
| 1959 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1960 | if (__p_.__state_ == nullptr) |
| 1961 | throw future_error(make_error_code(future_errc::no_state)); |
| 1962 | if (__p_.__state_->__has_value()) |
| 1963 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 1964 | try |
| 1965 | { |
| 1966 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1967 | __f_(_STD::forward<_ArgTypes>(__args)...); |
| 1968 | __p_.set_value(); |
| 1969 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1970 | } |
| 1971 | catch (...) |
| 1972 | { |
| 1973 | __p_.set_exception(current_exception()); |
| 1974 | } |
| 1975 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1976 | } |
| 1977 | |
| 1978 | template<class ..._ArgTypes> |
| 1979 | void |
| 1980 | packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) |
| 1981 | { |
| 1982 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1983 | if (__p_.__state_ == nullptr) |
| 1984 | throw future_error(make_error_code(future_errc::no_state)); |
| 1985 | if (__p_.__state_->__has_value()) |
| 1986 | throw future_error(make_error_code(future_errc::promise_already_satisfied)); |
| 1987 | try |
| 1988 | { |
| 1989 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1990 | __f_(_STD::forward<_ArgTypes>(__args)...); |
| 1991 | __p_.set_value_at_thread_exit(); |
| 1992 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1993 | } |
| 1994 | catch (...) |
| 1995 | { |
| 1996 | __p_.set_exception_at_thread_exit(current_exception()); |
| 1997 | } |
| 1998 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 1999 | } |
| 2000 | |
| 2001 | template<class ..._ArgTypes> |
| 2002 | void |
| 2003 | packaged_task<void(_ArgTypes...)>::reset() |
| 2004 | { |
| 2005 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 2006 | if (!valid()) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2007 | throw future_error(make_error_code(future_errc::no_state)); |
| 2008 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 2009 | __p_ = promise<result_type>(); |
| 2010 | } |
| 2011 | |
| 2012 | template <class _Callable> |
| 2013 | inline _LIBCPP_INLINE_VISIBILITY |
| 2014 | void |
| 2015 | swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) |
| 2016 | { |
| 2017 | __x.swap(__y); |
| 2018 | } |
| 2019 | |
| 2020 | template <class _Callable, class _Alloc> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2021 | struct _LIBCPP_VISIBLE uses_allocator<packaged_task<_Callable>, _Alloc> |
| 2022 | : public true_type {}; |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2023 | |
| 2024 | template <class _R, class _F> |
| 2025 | future<_R> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2026 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2027 | __make_deferred_assoc_state(_F&& __f) |
| 2028 | #else |
| 2029 | __make_deferred_assoc_state(_F __f) |
| 2030 | #endif |
| 2031 | { |
| 2032 | unique_ptr<__deferred_assoc_state<_R, _F>, __release_shared_count> |
| 2033 | __h(new __deferred_assoc_state<_R, _F>(_STD::forward<_F>(__f))); |
| 2034 | return future<_R>(__h.get()); |
| 2035 | } |
| 2036 | |
| 2037 | template <class _F, class... _Args> |
| 2038 | future<typename result_of<_F(_Args...)>::type> |
| 2039 | async(launch __policy, _F&& __f, _Args&&... __args) |
| 2040 | { |
| 2041 | typedef typename result_of<_F(_Args...)>::type _R; |
| 2042 | future<_R> __r; |
Howard Hinnant | 6689564 | 2010-11-23 18:33:54 +0000 | [diff] [blame] | 2043 | if (__policy & launch::async) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2044 | { |
| 2045 | packaged_task<_R()> __pk(bind(_STD::forward<_F>(__f), |
| 2046 | _STD::forward<_Args>(__args)...)); |
| 2047 | __r = __pk.get_future(); |
| 2048 | thread(_STD::move(__pk)).detach(); |
| 2049 | } |
Howard Hinnant | 6689564 | 2010-11-23 18:33:54 +0000 | [diff] [blame] | 2050 | else if (__policy & launch::deferred) |
| 2051 | __r = _STD::__make_deferred_assoc_state<_R>(bind(_STD::forward<_F>(__f), |
| 2052 | _STD::forward<_Args>(__args)...)); |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 2053 | return __r; |
| 2054 | } |
| 2055 | |
| 2056 | template <class _F, class... _Args> |
| 2057 | inline _LIBCPP_INLINE_VISIBILITY |
| 2058 | typename enable_if |
| 2059 | < |
| 2060 | !is_same<typename decay<_F>::type, launch>::value, |
| 2061 | future<typename result_of<_F(_Args...)>::type> |
| 2062 | >::type |
| 2063 | async(_F&& __f, _Args&&... __args) |
| 2064 | { |
| 2065 | return async(launch::any, _STD::forward<_F>(__f), |
| 2066 | _STD::forward<_Args>(__args)...); |
| 2067 | } |
| 2068 | |
| 2069 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 2070 | |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2071 | // shared_future |
| 2072 | |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2073 | template <class _R> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2074 | class _LIBCPP_VISIBLE shared_future |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2075 | { |
| 2076 | __assoc_state<_R>* __state_; |
| 2077 | |
| 2078 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2079 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2080 | shared_future() : __state_(nullptr) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2081 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2082 | shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) |
| 2083 | {if (__state_) __state_->__add_shared();} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2084 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2085 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2086 | shared_future(future<_R>&& __f) : __state_(__f.__state_) |
| 2087 | {__f.__state_ = nullptr;} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2088 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2089 | shared_future(shared_future&& __rhs) : __state_(__rhs.__state_) |
| 2090 | {__rhs.__state_ = nullptr;} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2091 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2092 | ~shared_future(); |
| 2093 | shared_future& operator=(const shared_future& __rhs); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2094 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2095 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2096 | shared_future& operator=(shared_future&& __rhs) |
| 2097 | { |
| 2098 | shared_future(std::move(__rhs)).swap(*this); |
| 2099 | return *this; |
| 2100 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2101 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2102 | |
| 2103 | // retrieving the value |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2104 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2105 | const _R& get() const {return __state_->copy();} |
| 2106 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2107 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2108 | void swap(shared_future& __rhs) {_STD::swap(__state_, __rhs.__state_);} |
| 2109 | |
| 2110 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2111 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2112 | bool valid() const {return __state_ != nullptr;} |
| 2113 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2114 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2115 | void wait() const {__state_->wait();} |
| 2116 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2117 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2118 | future_status |
| 2119 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 2120 | {return __state_->wait_for(__rel_time);} |
| 2121 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2122 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2123 | future_status |
| 2124 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 2125 | {return __state_->wait_until(__abs_time);} |
| 2126 | }; |
| 2127 | |
| 2128 | template <class _R> |
| 2129 | shared_future<_R>::~shared_future() |
| 2130 | { |
| 2131 | if (__state_) |
| 2132 | __state_->__release_shared(); |
| 2133 | } |
| 2134 | |
| 2135 | template <class _R> |
| 2136 | shared_future<_R>& |
| 2137 | shared_future<_R>::operator=(const shared_future& __rhs) |
| 2138 | { |
| 2139 | if (__rhs.__state_) |
| 2140 | __rhs.__state_->__add_shared(); |
| 2141 | if (__state_) |
| 2142 | __state_->__release_shared(); |
| 2143 | __state_ = __rhs.__state_; |
| 2144 | return *this; |
| 2145 | } |
| 2146 | |
| 2147 | template <class _R> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2148 | class _LIBCPP_VISIBLE shared_future<_R&> |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2149 | { |
| 2150 | __assoc_state<_R&>* __state_; |
| 2151 | |
| 2152 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2153 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2154 | shared_future() : __state_(nullptr) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2155 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2156 | shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) |
| 2157 | {if (__state_) __state_->__add_shared();} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2158 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2159 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2160 | shared_future(future<_R&>&& __f) : __state_(__f.__state_) |
| 2161 | {__f.__state_ = nullptr;} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2162 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2163 | shared_future(shared_future&& __rhs) : __state_(__rhs.__state_) |
| 2164 | {__rhs.__state_ = nullptr;} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2165 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2166 | ~shared_future(); |
| 2167 | shared_future& operator=(const shared_future& __rhs); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2168 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2169 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2170 | shared_future& operator=(shared_future&& __rhs) |
| 2171 | { |
| 2172 | shared_future(std::move(__rhs)).swap(*this); |
| 2173 | return *this; |
| 2174 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2175 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2176 | |
| 2177 | // retrieving the value |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2178 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2179 | _R& get() const {return __state_->copy();} |
| 2180 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2181 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2182 | void swap(shared_future& __rhs) {_STD::swap(__state_, __rhs.__state_);} |
| 2183 | |
| 2184 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2185 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2186 | bool valid() const {return __state_ != nullptr;} |
| 2187 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2188 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2189 | void wait() const {__state_->wait();} |
| 2190 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2191 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2192 | future_status |
| 2193 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 2194 | {return __state_->wait_for(__rel_time);} |
| 2195 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2196 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2197 | future_status |
| 2198 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 2199 | {return __state_->wait_until(__abs_time);} |
| 2200 | }; |
| 2201 | |
| 2202 | template <class _R> |
| 2203 | shared_future<_R&>::~shared_future() |
| 2204 | { |
| 2205 | if (__state_) |
| 2206 | __state_->__release_shared(); |
| 2207 | } |
| 2208 | |
| 2209 | template <class _R> |
| 2210 | shared_future<_R&>& |
| 2211 | shared_future<_R&>::operator=(const shared_future& __rhs) |
| 2212 | { |
| 2213 | if (__rhs.__state_) |
| 2214 | __rhs.__state_->__add_shared(); |
| 2215 | if (__state_) |
| 2216 | __state_->__release_shared(); |
| 2217 | __state_ = __rhs.__state_; |
| 2218 | return *this; |
| 2219 | } |
| 2220 | |
| 2221 | template <> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2222 | class _LIBCPP_VISIBLE shared_future<void> |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2223 | { |
| 2224 | __assoc_sub_state* __state_; |
| 2225 | |
| 2226 | public: |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2227 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2228 | shared_future() : __state_(nullptr) {} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2229 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2230 | shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) |
| 2231 | {if (__state_) __state_->__add_shared();} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2232 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2233 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2234 | shared_future(future<void>&& __f) : __state_(__f.__state_) |
| 2235 | {__f.__state_ = nullptr;} |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2236 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2237 | shared_future(shared_future&& __rhs) : __state_(__rhs.__state_) |
| 2238 | {__rhs.__state_ = nullptr;} |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2239 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2240 | ~shared_future(); |
| 2241 | shared_future& operator=(const shared_future& __rhs); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2242 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2243 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2244 | shared_future& operator=(shared_future&& __rhs) |
| 2245 | { |
| 2246 | shared_future(std::move(__rhs)).swap(*this); |
| 2247 | return *this; |
| 2248 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2249 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2250 | |
| 2251 | // retrieving the value |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2252 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2253 | void get() const {__state_->copy();} |
| 2254 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2255 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2256 | void swap(shared_future& __rhs) {_STD::swap(__state_, __rhs.__state_);} |
| 2257 | |
| 2258 | // functions to check state |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2259 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2260 | bool valid() const {return __state_ != nullptr;} |
| 2261 | |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2262 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2263 | void wait() const {__state_->wait();} |
| 2264 | template <class _Rep, class _Period> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2265 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2266 | future_status |
| 2267 | wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const |
| 2268 | {return __state_->wait_for(__rel_time);} |
| 2269 | template <class _Clock, class _Duration> |
Howard Hinnant | 8c6cbb2 | 2010-09-22 14:16:26 +0000 | [diff] [blame] | 2270 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 2271 | future_status |
| 2272 | wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const |
| 2273 | {return __state_->wait_until(__abs_time);} |
| 2274 | }; |
| 2275 | |
| 2276 | template <class _R> |
| 2277 | inline _LIBCPP_INLINE_VISIBILITY |
| 2278 | void |
| 2279 | swap(shared_future<_R>& __x, shared_future<_R>& __y) |
| 2280 | { |
| 2281 | __x.swap(__y); |
| 2282 | } |
| 2283 | |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2284 | template <class _R> |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 2285 | inline _LIBCPP_INLINE_VISIBILITY |
| 2286 | shared_future<_R> |
| 2287 | future<_R>::share() |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2288 | { |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 2289 | return shared_future<_R>(_STD::move(*this)); |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | template <class _R> |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2293 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 2294 | shared_future<_R&> |
| 2295 | future<_R&>::share() |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2296 | { |
Howard Hinnant | 7de4790 | 2010-11-30 20:23:32 +0000 | [diff] [blame^] | 2297 | return shared_future<_R&>(_STD::move(*this)); |
| 2298 | } |
| 2299 | |
| 2300 | inline _LIBCPP_INLINE_VISIBILITY |
| 2301 | shared_future<void> |
| 2302 | future<void>::share() |
| 2303 | { |
| 2304 | return shared_future<void>(_STD::move(*this)); |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2307 | _LIBCPP_END_NAMESPACE_STD |
| 2308 | |
| 2309 | #endif // _LIBCPP_FUTURE |