|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright (c) Lewis Baker |
| 3 | +// Licenced under MIT license. See LICENSE.txt for details. |
| 4 | +/////////////////////////////////////////////////////////////////////////////// |
| 5 | +#ifndef CPPCORO_LAZY_TASK_HPP_INCLUDED |
| 6 | +#define CPPCORO_LAZY_TASK_HPP_INCLUDED |
| 7 | + |
| 8 | +#include <cppcoro/broken_promise.hpp> |
| 9 | + |
| 10 | +#include <atomic> |
| 11 | +#include <exception> |
| 12 | +#include <utility> |
| 13 | +#include <type_traits> |
| 14 | + |
| 15 | +#include <experimental/coroutine> |
| 16 | + |
| 17 | +namespace cppcoro |
| 18 | +{ |
| 19 | + template<typename T> class lazy_task; |
| 20 | + |
| 21 | + namespace detail |
| 22 | + { |
| 23 | + class lazy_task_promise_base |
| 24 | + { |
| 25 | + public: |
| 26 | + |
| 27 | + lazy_task_promise_base() noexcept |
| 28 | + : m_awaiter(nullptr) |
| 29 | + {} |
| 30 | + |
| 31 | + auto initial_suspend() noexcept |
| 32 | + { |
| 33 | + return std::experimental::suspend_always{}; |
| 34 | + } |
| 35 | + |
| 36 | + auto final_suspend() noexcept |
| 37 | + { |
| 38 | + struct awaitable |
| 39 | + { |
| 40 | + std::experimental::coroutine_handle<> m_awaiter; |
| 41 | + |
| 42 | + awaitable(std::experimental::coroutine_handle<> awaiter) noexcept |
| 43 | + : m_awaiter(awaiter) |
| 44 | + {} |
| 45 | + |
| 46 | + bool await_ready() const noexcept { return false; } |
| 47 | + |
| 48 | + void await_suspend(std::experimental::coroutine_handle<> coroutine) |
| 49 | + { |
| 50 | + m_awaiter.resume(); |
| 51 | + } |
| 52 | + |
| 53 | + void await_resume() noexcept {} |
| 54 | + }; |
| 55 | + |
| 56 | + return awaitable{ m_awaiter }; |
| 57 | + } |
| 58 | + |
| 59 | + void unhandled_exception() noexcept |
| 60 | + { |
| 61 | + set_exception(std::current_exception()); |
| 62 | + } |
| 63 | + |
| 64 | + void set_exception(std::exception_ptr exception) |
| 65 | + { |
| 66 | + m_exception = std::move(exception); |
| 67 | + } |
| 68 | + |
| 69 | + bool is_ready() const noexcept |
| 70 | + { |
| 71 | + return static_cast<bool>(m_awaiter); |
| 72 | + } |
| 73 | + |
| 74 | + void set_awaiter(std::experimental::coroutine_handle<> awaiter) |
| 75 | + { |
| 76 | + m_awaiter = awaiter; |
| 77 | + } |
| 78 | + |
| 79 | + protected: |
| 80 | + |
| 81 | + bool completed_with_unhandled_exception() |
| 82 | + { |
| 83 | + return m_exception != nullptr; |
| 84 | + } |
| 85 | + |
| 86 | + void rethrow_if_unhandled_exception() |
| 87 | + { |
| 88 | + if (m_exception != nullptr) |
| 89 | + { |
| 90 | + std::rethrow_exception(m_exception); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private: |
| 95 | + |
| 96 | + std::experimental::coroutine_handle<> m_awaiter; |
| 97 | + std::exception_ptr m_exception; |
| 98 | + |
| 99 | + }; |
| 100 | + |
| 101 | + template<typename T> |
| 102 | + class lazy_task_promise : public lazy_task_promise_base |
| 103 | + { |
| 104 | + public: |
| 105 | + |
| 106 | + lazy_task_promise() noexcept = default; |
| 107 | + |
| 108 | + ~lazy_task_promise() |
| 109 | + { |
| 110 | + if (is_ready() && !completed_with_unhandled_exception()) |
| 111 | + { |
| 112 | + reinterpret_cast<T*>(&m_valueStorage)->~T(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + auto get_return_object() noexcept |
| 117 | + { |
| 118 | + return std::experimental::coroutine_handle<lazy_task_promise>::from_promise(*this); |
| 119 | + } |
| 120 | + |
| 121 | + template< |
| 122 | + typename VALUE, |
| 123 | + typename = std::enable_if_t<std::is_convertible_v<VALUE&&, T>>> |
| 124 | + void return_value(VALUE&& value) |
| 125 | + noexcept(std::is_nothrow_constructible_v<T, VALUE&&>) |
| 126 | + { |
| 127 | + new (&m_valueStorage) T(std::forward<VALUE>(value)); |
| 128 | + } |
| 129 | + |
| 130 | + T& result() & |
| 131 | + { |
| 132 | + rethrow_if_unhandled_exception(); |
| 133 | + return *reinterpret_cast<T*>(&m_valueStorage); |
| 134 | + } |
| 135 | + |
| 136 | + T&& result() && |
| 137 | + { |
| 138 | + rethrow_if_unhandled_exception(); |
| 139 | + return std::move(*reinterpret_cast<T*>(&m_valueStorage)); |
| 140 | + } |
| 141 | + |
| 142 | + private: |
| 143 | + |
| 144 | + // Not using std::aligned_storage here due to bug in MSVC 2015 Update 2 |
| 145 | + // that means it doesn't work for types with alignof(T) > 8. |
| 146 | + // See MS-Connect bug #2658635. |
| 147 | + alignas(T) char m_valueStorage[sizeof(T)]; |
| 148 | + |
| 149 | + }; |
| 150 | + |
| 151 | + template<> |
| 152 | + class lazy_task_promise<void> : public lazy_task_promise_base |
| 153 | + { |
| 154 | + public: |
| 155 | + |
| 156 | + lazy_task_promise() noexcept = default; |
| 157 | + |
| 158 | + auto get_return_object() noexcept |
| 159 | + { |
| 160 | + return std::experimental::coroutine_handle<lazy_task_promise>::from_promise(*this); |
| 161 | + } |
| 162 | + |
| 163 | + void return_void() noexcept |
| 164 | + {} |
| 165 | + |
| 166 | + void result() |
| 167 | + { |
| 168 | + rethrow_if_unhandled_exception(); |
| 169 | + } |
| 170 | + |
| 171 | + }; |
| 172 | + |
| 173 | + template<typename T> |
| 174 | + class lazy_task_promise<T&> : public lazy_task_promise_base |
| 175 | + { |
| 176 | + public: |
| 177 | + |
| 178 | + lazy_task_promise() noexcept = default; |
| 179 | + |
| 180 | + auto get_return_object() noexcept |
| 181 | + { |
| 182 | + return std::experimental::coroutine_handle<lazy_task_promise>::from_promise(*this); |
| 183 | + } |
| 184 | + |
| 185 | + void return_value(T& value) noexcept |
| 186 | + { |
| 187 | + m_value = std::addressof(value); |
| 188 | + } |
| 189 | + |
| 190 | + T& result() |
| 191 | + { |
| 192 | + rethrow_if_unhandled_exception(); |
| 193 | + return *m_value; |
| 194 | + } |
| 195 | + |
| 196 | + private: |
| 197 | + |
| 198 | + T* m_value; |
| 199 | + |
| 200 | + }; |
| 201 | + } |
| 202 | + |
| 203 | + /// \brief |
| 204 | + /// A lazy task represents an asynchronous operation that is not started |
| 205 | + /// until it is first awaited. |
| 206 | + /// |
| 207 | + /// When you call a coroutine that returns a lazy_task, the coroutine |
| 208 | + /// simply captures any passed parameters and returns exeuction to the |
| 209 | + /// caller. Execution of the coroutine body does not start until the |
| 210 | + /// coroutine is first co_await'ed. |
| 211 | + /// |
| 212 | + /// Comparison with task<T> |
| 213 | + /// ----------------------- |
| 214 | + /// The lazy task has lower overhead than cppcoro::task<T> as it does not |
| 215 | + /// require the use of atomic operations to synchronise potential races |
| 216 | + /// between the awaiting coroutine suspending and the coroutine completing. |
| 217 | + /// |
| 218 | + /// The awaiting coroutine is suspended prior to the lazy_task being started |
| 219 | + /// which means that when the lazy_task completes it can unconditionally |
| 220 | + /// resume the awaiter. |
| 221 | + /// |
| 222 | + /// One limitation of this approach is that if the lazy_task completes |
| 223 | + /// synchronously then, unless the compiler is able to perform tail-calls, |
| 224 | + /// the awaiting coroutine will be resumed inside a nested stack-frame. |
| 225 | + /// This call lead to stack-overflow if long chains of lazy_tasks complete |
| 226 | + /// synchronously. |
| 227 | + /// |
| 228 | + /// The task<T> type does not have this issue as the awaiting coroutine is |
| 229 | + /// not suspended in the case that the task completes synchronously. |
| 230 | + template<typename T = void> |
| 231 | + class lazy_task |
| 232 | + { |
| 233 | + public: |
| 234 | + |
| 235 | + using promise_type = detail::lazy_task_promise<T>; |
| 236 | + |
| 237 | + private: |
| 238 | + |
| 239 | + struct awaitable_base |
| 240 | + { |
| 241 | + std::experimental::coroutine_handle<promise_type> m_coroutine; |
| 242 | + |
| 243 | + awaitable_base(std::experimental::coroutine_handle<promise_type> coroutine) noexcept |
| 244 | + : m_coroutine(coroutine) |
| 245 | + {} |
| 246 | + |
| 247 | + bool await_ready() const noexcept |
| 248 | + { |
| 249 | + return !m_coroutine || m_coroutine.promise().is_ready(); |
| 250 | + } |
| 251 | + |
| 252 | + void await_suspend(std::experimental::coroutine_handle<> awaiter) noexcept |
| 253 | + { |
| 254 | + m_coroutine.promise().set_awaiter(awaiter); |
| 255 | + m_coroutine.resume(); |
| 256 | + } |
| 257 | + }; |
| 258 | + |
| 259 | + public: |
| 260 | + |
| 261 | + lazy_task() noexcept |
| 262 | + : m_coroutine(nullptr) |
| 263 | + {} |
| 264 | + |
| 265 | + explicit lazy_task(std::experimental::coroutine_handle<promise_type> coroutine) |
| 266 | + : m_coroutine(coroutine) |
| 267 | + {} |
| 268 | + |
| 269 | + lazy_task(lazy_task&& t) noexcept |
| 270 | + : m_coroutine(t.m_coroutine) |
| 271 | + { |
| 272 | + t.m_coroutine = nullptr; |
| 273 | + } |
| 274 | + |
| 275 | + /// Disable copy construction/assignment. |
| 276 | + lazy_task(const lazy_task&) = delete; |
| 277 | + lazy_task& operator=(const lazy_task&) = delete; |
| 278 | + |
| 279 | + /// Frees resources used by this task. |
| 280 | + ~lazy_task() |
| 281 | + { |
| 282 | + if (m_coroutine) |
| 283 | + { |
| 284 | + m_coroutine.destroy(); |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + /// \brief |
| 289 | + /// Query if the task result is complete. |
| 290 | + /// |
| 291 | + /// Awaiting a task that is ready will not block. |
| 292 | + bool is_ready() const noexcept |
| 293 | + { |
| 294 | + return !m_coroutine || m_coroutine.promise().is_ready(); |
| 295 | + } |
| 296 | + |
| 297 | + auto operator co_await() const & noexcept |
| 298 | + { |
| 299 | + struct awaitable : awaitable_base |
| 300 | + { |
| 301 | + using awaitable_base::awaitable_base; |
| 302 | + |
| 303 | + decltype(auto) await_resume() |
| 304 | + { |
| 305 | + if (!m_coroutine) |
| 306 | + { |
| 307 | + throw broken_promise{}; |
| 308 | + } |
| 309 | + |
| 310 | + return m_coroutine.promise().result(); |
| 311 | + } |
| 312 | + }; |
| 313 | + |
| 314 | + return awaitable{ m_coroutine }; |
| 315 | + } |
| 316 | + |
| 317 | + auto operator co_await() const && noexcept |
| 318 | + { |
| 319 | + struct awaitable : awaitable_base |
| 320 | + { |
| 321 | + using awaitable_base::awaitable_base; |
| 322 | + |
| 323 | + decltype(auto) await_resume() |
| 324 | + { |
| 325 | + if (!m_coroutine) |
| 326 | + { |
| 327 | + throw broken_promise{}; |
| 328 | + } |
| 329 | + |
| 330 | + return std::move(m_coroutine.promise()).result(); |
| 331 | + } |
| 332 | + }; |
| 333 | + |
| 334 | + return awaitable{ m_coroutine }; |
| 335 | + } |
| 336 | + |
| 337 | + /// \brief |
| 338 | + /// Returns an awaitable that will await completion of the task without |
| 339 | + /// attempting to retrieve the result. |
| 340 | + auto when_ready() const noexcept |
| 341 | + { |
| 342 | + struct awaitable : awaitable_base |
| 343 | + { |
| 344 | + using awaitable_base::awaitable_base; |
| 345 | + |
| 346 | + void await_resume() const noexcept {} |
| 347 | + }; |
| 348 | + |
| 349 | + return awaitable{ m_coroutine }; |
| 350 | + } |
| 351 | + |
| 352 | + private: |
| 353 | + |
| 354 | + std::experimental::coroutine_handle<promise_type> m_coroutine; |
| 355 | + |
| 356 | + }; |
| 357 | +} |
| 358 | + |
| 359 | +#endif |
0 commit comments