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