mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
runners.hpp
1#ifndef MRS_LIB_CORO_RUNNERS_HPP_
2#define MRS_LIB_CORO_RUNNERS_HPP_
3
4#include <concepts>
5#include <coroutine>
6#include <stop_token>
7#include <utility>
8
9#include "mrs_lib/coro/internal/thread_local_continuation_scheduler.hpp"
10#include "mrs_lib/coro/task.hpp"
11
12namespace mrs_lib::coro
13{
14
15 namespace internal
16 {
17
25 class AsyncRun
26 {
27 public:
29 {
33 template <typename... Args>
34 promise_type(std::stop_token stop_token, Args&&...) : stop_token_(std::move(stop_token))
35 {
36 }
37
38 AsyncRun get_return_object()
39 {
40 return {};
41 }
42 MoveToThreadLocalContinuationScheduler initial_suspend()
43 {
44 return {};
45 }
46 std::suspend_never final_suspend() noexcept
47 {
48 return {};
49 }
50 void return_void()
51 {
52 }
53 void unhandled_exception();
54
55 std::stop_token stop_token_;
56 };
57
58 private:
59 AsyncRun() = default;
60 };
61
62 template <>
64 {
65 static CancellableContinuation release_continuation(std::coroutine_handle<AsyncRun::promise_type>)
66 {
67 return {};
68 };
69
70 static std::stop_token get_token(std::coroutine_handle<AsyncRun::promise_type> handle)
71 {
72 return handle.promise().stop_token_;
73 }
74 };
75
89 template <typename F, typename... Args>
90 requires std::invocable<std::decay_t<F>, std::decay_t<Args>...> && std::same_as<Task<void>, std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>>
91 internal::AsyncRun start_task_impl(std::stop_token, F task, Args... args)
92 {
93 // The `decay-copy` of the arguments ensures that they are copied into
94 // the coroutine frame, preventing dangling references like `std::thread`.
95 co_await std::invoke(std::move(task), std::move(args)...);
96 }
97
119 template <typename F, typename... Args>
120 requires std::invocable<std::decay_t<F>, std::decay_t<Args>...> && std::same_as<Task<void>, std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>>
121 void start_task(F&& task, Args&&... args)
122 {
123 internal::start_task_impl(std::stop_token(), std::forward<F>(task), std::forward<Args>(args)...);
124 }
125
148 template <typename F, typename... Args>
149 requires std::invocable<std::decay_t<F>, std::decay_t<Args>...> && std::same_as<Task<void>, std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>>
150 void start_task(std::stop_token token, F&& task, Args&&... args)
151 {
152 internal::start_task_impl(token, std::forward<F>(task), std::forward<Args>(args)...);
153 }
154
155 } // namespace internal
156
157} // namespace mrs_lib::coro
158
159#endif // MRS_LIB_CORO_RUNNERS_HPP_
Coroutine type used to start asynchronous computation.
Definition runners.hpp:26
Owning coroutine handle supporting cancellation.
Definition continuation.hpp:54
promise_type(std::stop_token stop_token, Args &&...)
Construct the promise type, storing the associated stop token.
Definition runners.hpp:34
Type trait to obtain data necessary for creating CancellableContinuation.
Definition continuation.hpp:37