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