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