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