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