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