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 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 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 | { |
| 30 | any, |
| 31 | async, |
| 32 | sync |
| 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 |
| 105 | future<R> get_future(); |
| 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 |
| 133 | future<R> get_future(); |
| 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&&); |
| 159 | |
| 160 | // retrieving the value |
| 161 | R get(); |
| 162 | |
| 163 | // functions to check state |
| 164 | bool valid() const; |
| 165 | |
| 166 | void wait() const; |
| 167 | template <class Rep, class Period> |
| 168 | future_status |
| 169 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 170 | template <class Clock, class Duration> |
| 171 | future_status |
| 172 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 173 | }; |
| 174 | |
| 175 | template <class R> |
| 176 | class future<R&> |
| 177 | { |
| 178 | public: |
| 179 | future(); |
| 180 | future(future&&); |
| 181 | future(const future& rhs) = delete; |
| 182 | ~future(); |
| 183 | future& operator=(const future& rhs) = delete; |
| 184 | future& operator=(future&&); |
| 185 | |
| 186 | // retrieving the value |
| 187 | R& get(); |
| 188 | |
| 189 | // functions to check state |
| 190 | bool valid() const; |
| 191 | |
| 192 | void wait() const; |
| 193 | template <class Rep, class Period> |
| 194 | future_status |
| 195 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 196 | template <class Clock, class Duration> |
| 197 | future_status |
| 198 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 199 | }; |
| 200 | |
| 201 | template <> |
| 202 | class future<void> |
| 203 | { |
| 204 | public: |
| 205 | future(); |
| 206 | future(future&&); |
| 207 | future(const future& rhs) = delete; |
| 208 | ~future(); |
| 209 | future& operator=(const future& rhs) = delete; |
| 210 | future& operator=(future&&); |
| 211 | |
| 212 | // retrieving the value |
| 213 | void get(); |
| 214 | |
| 215 | // functions to check state |
| 216 | bool valid() const; |
| 217 | |
| 218 | void wait() const; |
| 219 | template <class Rep, class Period> |
| 220 | future_status |
| 221 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 222 | template <class Clock, class Duration> |
| 223 | future_status |
| 224 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 225 | }; |
| 226 | |
| 227 | template <class R> |
| 228 | class shared_future |
| 229 | { |
| 230 | public: |
| 231 | shared_future(); |
| 232 | shared_future(const shared_future& rhs); |
| 233 | shared_future(future<R>&&); |
| 234 | shared_future(shared_future&& rhs); |
| 235 | ~shared_future(); |
| 236 | shared_future& operator=(const shared_future& rhs); |
| 237 | shared_future& operator=(shared_future&& rhs); |
| 238 | |
| 239 | // retrieving the value |
| 240 | const R& get() const; |
| 241 | |
| 242 | // functions to check state |
| 243 | bool valid() const; |
| 244 | |
| 245 | void wait() const; |
| 246 | template <class Rep, class Period> |
| 247 | future_status |
| 248 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 249 | template <class Clock, class Duration> |
| 250 | future_status |
| 251 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 252 | }; |
| 253 | |
| 254 | template <class R> |
| 255 | class shared_future<R&> |
| 256 | { |
| 257 | public: |
| 258 | shared_future(); |
| 259 | shared_future(const shared_future& rhs); |
| 260 | shared_future(future<R>&&); |
| 261 | shared_future(shared_future&& rhs); |
| 262 | ~shared_future(); |
| 263 | shared_future& operator=(const shared_future& rhs); |
| 264 | shared_future& operator=(shared_future&& rhs); |
| 265 | |
| 266 | // retrieving the value |
| 267 | R& get() const; |
| 268 | |
| 269 | // functions to check state |
| 270 | bool valid() const; |
| 271 | |
| 272 | void wait() const; |
| 273 | template <class Rep, class Period> |
| 274 | future_status |
| 275 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 276 | template <class Clock, class Duration> |
| 277 | future_status |
| 278 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 279 | }; |
| 280 | |
| 281 | template <> |
| 282 | class shared_future<void> |
| 283 | { |
| 284 | public: |
| 285 | shared_future(); |
| 286 | shared_future(const shared_future& rhs); |
| 287 | shared_future(future<R>&&); |
| 288 | shared_future(shared_future&& rhs); |
| 289 | ~shared_future(); |
| 290 | shared_future& operator=(const shared_future& rhs); |
| 291 | shared_future& operator=(shared_future&& rhs); |
| 292 | |
| 293 | // retrieving the value |
| 294 | void get() const; |
| 295 | |
| 296 | // functions to check state |
| 297 | bool valid() const; |
| 298 | |
| 299 | void wait() const; |
| 300 | template <class Rep, class Period> |
| 301 | future_status |
| 302 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 303 | template <class Clock, class Duration> |
| 304 | future_status |
| 305 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 306 | }; |
| 307 | |
| 308 | template <class R> |
| 309 | class atomic_future |
| 310 | { |
| 311 | public: |
| 312 | atomic_future(); |
| 313 | atomic_future(const atomic_future& rhs); |
| 314 | atomic_future(future<R>&&); |
| 315 | ~atomic_future(); |
| 316 | atomic_future& operator=(const atomic_future& rhs); |
| 317 | |
| 318 | // retrieving the value |
| 319 | const R& get() const; |
| 320 | |
| 321 | // functions to check state |
| 322 | bool valid() const; |
| 323 | |
| 324 | void wait() const; |
| 325 | template <class Rep, class Period> |
| 326 | future_status |
| 327 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 328 | template <class Clock, class Duration> |
| 329 | future_status |
| 330 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 331 | }; |
| 332 | |
| 333 | template <class R> |
| 334 | class atomic_future<R&> |
| 335 | { |
| 336 | public: |
| 337 | atomic_future(); |
| 338 | atomic_future(const atomic_future& rhs); |
| 339 | atomic_future(future<R>&&); |
| 340 | ~atomic_future(); |
| 341 | atomic_future& operator=(const atomic_future& rhs); |
| 342 | |
| 343 | // retrieving the value |
| 344 | R& get() const; |
| 345 | |
| 346 | // functions to check state |
| 347 | bool valid() const; |
| 348 | |
| 349 | void wait() const; |
| 350 | template <class Rep, class Period> |
| 351 | future_status |
| 352 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 353 | template <class Clock, class Duration> |
| 354 | future_status |
| 355 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 356 | }; |
| 357 | |
| 358 | template <> |
| 359 | class atomic_future<void> |
| 360 | { |
| 361 | public: |
| 362 | atomic_future(); |
| 363 | atomic_future(const atomic_future& rhs); |
| 364 | atomic_future(future<R>&&); |
| 365 | ~atomic_future(); |
| 366 | atomic_future& operator=(const atomic_future& rhs); |
| 367 | |
| 368 | // retrieving the value |
| 369 | void get() const; |
| 370 | |
| 371 | // functions to check state |
| 372 | bool valid() const; |
| 373 | |
| 374 | void wait() const; |
| 375 | template <class Rep, class Period> |
| 376 | future_status |
| 377 | wait_for(const chrono::duration<Rep, Period>& rel_time) const; |
| 378 | template <class Clock, class Duration> |
| 379 | future_status |
| 380 | wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; |
| 381 | }; |
| 382 | |
| 383 | template <class F, class... Args> |
| 384 | future<typename result_of<F(Args...)>::type> |
| 385 | async(F&& f, Args&&... args); |
| 386 | |
| 387 | template <class F, class... Args> |
| 388 | future<typename result_of<F(Args...)>::type> |
| 389 | async(launch policy, F&& f, Args&&... args); |
| 390 | |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 391 | template <class> class packaged_task; // undefined |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 392 | |
| 393 | template <class R, class... ArgTypes> |
| 394 | class packaged_task<R(ArgTypes...)> |
| 395 | { |
| 396 | public: |
| 397 | typedef R result_type; |
| 398 | |
| 399 | // construction and destruction |
| 400 | packaged_task(); |
| 401 | template <class F> |
| 402 | explicit packaged_task(F f); |
| 403 | template <class F, class Allocator> |
| 404 | explicit packaged_task(allocator_arg_t, const Allocator& a, F f); |
| 405 | explicit packaged_task(R(*f)(ArgTypes...)); |
| 406 | template <class F> |
| 407 | explicit packaged_task(F&& f); |
| 408 | template <class F, class Allocator> |
| 409 | explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f); |
| 410 | ~packaged_task(); |
| 411 | |
| 412 | // no copy |
| 413 | packaged_task(packaged_task&) = delete; |
| 414 | packaged_task& operator=(packaged_task&) = delete; |
| 415 | |
| 416 | // move support |
| 417 | packaged_task(packaged_task&& other); |
| 418 | packaged_task& operator=(packaged_task&& other); |
| 419 | void swap(packaged_task& other); |
| 420 | |
| 421 | explicit operator bool() const; |
| 422 | |
| 423 | // result retrieval |
| 424 | future<R> get_future(); |
| 425 | |
| 426 | // execution |
| 427 | void operator()(ArgTypes... ); |
| 428 | void make_ready_at_thread_exit(ArgTypes...); |
| 429 | |
| 430 | void reset(); |
| 431 | }; |
| 432 | |
| 433 | template <class R> |
| 434 | void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&); |
| 435 | |
| 436 | template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>; |
| 437 | |
| 438 | } // std |
| 439 | |
| 440 | */ |
| 441 | |
| 442 | #include <__config> |
| 443 | #include <system_error> |
| 444 | |
| 445 | #pragma GCC system_header |
| 446 | |
| 447 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 448 | |
| 449 | //enum class future_errc |
| 450 | struct future_errc |
| 451 | { |
| 452 | enum _ { |
| 453 | broken_promise, |
| 454 | future_already_retrieved, |
| 455 | promise_already_satisfied, |
| 456 | no_state |
| 457 | }; |
| 458 | |
| 459 | _ __v_; |
| 460 | |
| 461 | future_errc(_ __v) : __v_(__v) {} |
| 462 | operator int() const {return __v_;} |
| 463 | |
| 464 | }; |
| 465 | |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame^] | 466 | template <> struct is_error_code_enum<future_errc> : public true_type {}; |
| 467 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 468 | //enum class launch |
| 469 | struct launch |
| 470 | { |
| 471 | enum _ { |
| 472 | any, |
| 473 | async, |
| 474 | sync |
| 475 | }; |
| 476 | |
| 477 | _ __v_; |
| 478 | |
| 479 | launch(_ __v) : __v_(__v) {} |
| 480 | operator int() const {return __v_;} |
| 481 | |
| 482 | }; |
| 483 | |
| 484 | //enum class future_status |
| 485 | struct future_status |
| 486 | { |
| 487 | enum _ { |
| 488 | ready, |
| 489 | timeout, |
| 490 | deferred |
| 491 | }; |
| 492 | |
| 493 | _ __v_; |
| 494 | |
| 495 | future_status(_ __v) : __v_(__v) {} |
| 496 | operator int() const {return __v_;} |
| 497 | |
| 498 | }; |
| 499 | |
Howard Hinnant | a652172 | 2010-08-25 17:32:05 +0000 | [diff] [blame^] | 500 | const error_category& future_category(); |
| 501 | |
| 502 | inline _LIBCPP_INLINE_VISIBILITY |
| 503 | error_code |
| 504 | make_error_code(future_errc __e) |
| 505 | { |
| 506 | return error_code(static_cast<int>(__e), future_category()); |
| 507 | } |
| 508 | |
| 509 | inline _LIBCPP_INLINE_VISIBILITY |
| 510 | error_condition |
| 511 | make_error_condition(future_errc __e) |
| 512 | { |
| 513 | return error_condition(static_cast<int>(__e), future_category()); |
| 514 | } |
| 515 | |
| 516 | class future_error |
| 517 | : public logic_error |
| 518 | { |
| 519 | error_code __ec_; |
| 520 | public: |
| 521 | future_error(error_code __ec); |
| 522 | |
| 523 | const error_code& code() const throw() {return __ec_;} |
| 524 | }; |
| 525 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 526 | _LIBCPP_END_NAMESPACE_STD |
| 527 | |
| 528 | #endif // _LIBCPP_FUTURE |