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