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