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