mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
task.impl.hpp
1#ifndef MRS_LIB_CORO_TASK_IMPL_HPP_
2#define MRS_LIB_CORO_TASK_IMPL_HPP_
3
4#include "mrs_lib/coro/task.hpp"
5
6#include "mrs_lib/coro/internal/thread_local_continuation_scheduler.hpp"
7
8namespace mrs_lib::coro
9{
10 namespace internal
11 {
12
13 template <typename Derived>
14 inline bool BasePromiseType<Derived>::FinalAwaitable::await_ready() noexcept
15 {
16 return false;
17 }
18
19 template <typename Derived>
20 inline void BasePromiseType<Derived>::FinalAwaitable::await_suspend(std::coroutine_handle<Derived> task_handle) noexcept
21 {
22 // Destructor of task would destroy the continuation so we need to release the continuation
23 schedule_coroutine_continuation(task_handle.promise().continuation_.release());
24 }
25
26 template <typename Derived>
27 inline void BasePromiseType<Derived>::FinalAwaitable::await_resume() noexcept
28 {
29 assert(false);
30 }
31
32 template <typename Derived>
33 inline std::suspend_always BasePromiseType<Derived>::initial_suspend()
34 {
35 return {};
36 }
37
38 template <typename Derived>
39 inline auto BasePromiseType<Derived>::final_suspend() noexcept -> FinalAwaitable
40 {
41 return {};
42 }
43
44 template <typename Derived>
45 inline void BasePromiseType<Derived>::set_continuation(CancellableContinuation continuation)
46 {
47 continuation_ = std::move(continuation);
48 }
49
50 template <typename T>
51 Task<T> PromiseType<T>::get_return_object()
52 {
53 return Task<T>(std::coroutine_handle<PromiseType>::from_promise(*this));
54 }
55
56 template <typename T>
57 void PromiseType<T>::return_value(T&& ret_val)
58 {
59 result_.set_value(std::move(ret_val));
60 }
61
62 template <typename T>
63 void PromiseType<T>::unhandled_exception()
64 {
65 result_.set_exception(std::current_exception());
66 }
67
68 inline Task<void> PromiseType<void>::get_return_object()
69 {
70 return Task<void>(std::coroutine_handle<PromiseType>::from_promise(*this));
71 }
72
73 inline void PromiseType<void>::return_void()
74 {
75 }
76
77 inline void PromiseType<void>::unhandled_exception()
78 {
79 exception_ = std::current_exception();
80 }
81
82 } // namespace internal
83} // namespace mrs_lib::coro
84
85
86#endif // MRS_LIB_CORO_TASK_IMPL_HPP_