GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
async.hpp
浏览该文件的文档.
1
13#pragma once
14#include <chrono>
15#include <exception>
16#include <format>
17#include <functional>
18#include <string_view>
19#include <tuple>
20#include <utility>
21#include <asio.hpp>
22#include <asio/awaitable.hpp>
23#include <asio/io_context.hpp>
24#include <asio/steady_timer.hpp>
25#include <asio/use_awaitable.hpp>
26
27#include "multiton.hpp"
28#include "core/logger.h"
29#include "utils/singleton.hpp"
30
39
45template<typename T = void>
46using awaitable = asio::awaitable<T>;
47
48using duration = std::chrono::steady_clock::duration;
49
56 public utils::singleton_base<task_context>,public logable<task_context>{
57public:
58 using task_type = awaitable<>;
59 using shutdown_handler_type = std::function<task_type()>;
60
62 struct info_type{
64 };
65
66 explicit task_context();
67
83 void spawn(task_type&& task);
84
100 template<typename Fn, typename... Args>
101 inline void post(Fn&& fn, Args&&... args) {
102 asio::post(context_,
103 [this, fn = std::forward<Fn>(fn),
104 args = std::make_tuple(std::decay_t<Args>(std::forward<Args>(args))...)]() mutable {
105 try {
106 std::apply(std::move(fn), std::move(args));
107 } catch (...) {
108 handle_unhandled_exception(
109 std::current_exception(), "posted task");
110 }
111 });
112 }
113
121 void set_shutdown_handler(shutdown_handler_type handler);
122
129 void request_shutdown() noexcept;
130
134 void run() noexcept;
135
136 void stop();
137
139 bool failed() const noexcept { return failed_; }
140
142 bool shutdown_requested() const noexcept { return shutdown_started_; }
143
145 bool init(info_type _info);
146
148 inline std::string desc()const{
149 return std::format("async task context");
150 }
151
155 inline auto get_executor(){
156 return context_.get_executor();
157 }
158
162 inline auto asio_context() -> asio::io_context&{
163 return context_;
164 }
165
166private:
167 void handle_unhandled_exception(std::exception_ptr error,
168 std::string_view origin) noexcept;
169 void finish_shutdown(std::exception_ptr error) noexcept;
170
171 asio::io_context context_;
172 info_type info_;
173 shutdown_handler_type shutdown_handler_;
174 bool failed_ {false};
175 bool shutdown_started_ {false};
176};
177
178static_assert(utils::singleton_info<task_context::info_type>);
179static_assert(utils::singleton<task_context>);
180
191inline auto spawn(task_context::task_type&& task){
192 roboctrl::get<task_context>().spawn(std::forward<task_context::task_type>(task));
193}
194
206template<typename ...Args>
207inline auto post(Args&&... args)
208{
209 roboctrl::get<task_context>().post(std::forward<Args>(args)...);
210}
211
216inline void run(){
217 roboctrl::get<task_context>().run();
218}
219
221inline bool failed(){
222 return roboctrl::get<task_context>().failed();
223}
224
226inline bool shutdown_requested(){
227 return roboctrl::get<task_context>().shutdown_requested();
228}
229
231inline void set_shutdown_handler(task_context::shutdown_handler_type handler){
232 roboctrl::get<task_context>().set_shutdown_handler(std::move(handler));
233}
234
236inline void request_shutdown(){
237 roboctrl::get<task_context>().request_shutdown();
238}
239
249 co_await asio::post(asio::use_awaitable);
250}
251
262inline awaitable<void> wait_for(const duration& duration){
263 auto ex = roboctrl::get<task_context>().get_executor();
264 asio::steady_timer timer(ex, duration);
265 co_await timer.async_wait(asio::use_awaitable);
266}
267
271inline auto executor(){
272 return roboctrl::get<task_context>().get_executor();
273}
274
278inline auto& io_context(){
279 return roboctrl::get<task_context>().asio_context();
280}
281
282inline void stop(){
283 roboctrl::get<task_context>().stop();
284}
285}
286namespace roboctrl{
287 using namespace async;
288}
异步任务上下文类。
Definition async.hpp:56
void request_shutdown() noexcept
请求一次安全停机。
Definition async.cpp:71
auto asio_context() -> asio::io_context &
获取asio的io_context。
Definition async.hpp:162
void run() noexcept
开始运行任务上下文。
Definition async.cpp:22
bool failed() const noexcept
是否观察到过未处理的异步异常。
Definition async.hpp:139
std::string desc() const
task_context的描述
Definition async.hpp:148
bool shutdown_requested() const noexcept
是否已经进入一次性停机流程。
Definition async.hpp:142
bool init(info_type _info)
初始化 task_context
Definition async.cpp:43
auto get_executor()
获取asio的executor。
Definition async.hpp:155
void spawn(task_type &&task)
添加一个协程任务到上下文中执行。
Definition async.cpp:9
void set_shutdown_handler(shutdown_handler_type handler)
注册异常或外部请求共用的一次性安全停机任务。
Definition async.cpp:17
void post(Fn &&fn, Args &&... args)
添加一个任务到上下文中执行。
Definition async.hpp:101
用于提供日志功能的辅助基类 通过CRTP模式实现,要求派生类实现desc()方法以提供描述信息,在继承时传入派生类自身类型作为模板参数。 继承这个类后,派生类可以方便地使用log_debug、log_i...
Definition logger.h:178
提供单例模式的单例基类
Definition singleton.hpp:19
用于日志输出的组件。
用于实现多例模式的通用组件。
异步上下文相关功能
Definition async.hpp:38
awaitable< void > wait_for(const duration &duration)
协程任务等待。
Definition async.hpp:262
awaitable< void > yield()
协程任务等待。用于让出当前函数的执行权。
Definition async.hpp:248
void set_shutdown_handler(task_context::shutdown_handler_type handler)
设置全局任务上下文的一次性安全停机任务。
Definition async.hpp:231
auto spawn(task_context::task_type &&task)
添加一个协程任务到全局任务上下文中执行。
Definition async.hpp:191
auto post(Args &&... args)
添加一个任务到全局任务上下文中执行。
Definition async.hpp:207
void request_shutdown()
请求运行全局任务上下文的安全停机任务。
Definition async.hpp:236
void run()
运行全局任务上下文。
Definition async.hpp:216
bool failed()
查询全局任务上下文是否因未处理异常进入过安全停机。
Definition async.hpp:221
asio::awaitable< T > awaitable
协程任务类型。
Definition async.hpp:46
auto executor()
获取全局任务上下文的executor。
Definition async.hpp:271
auto & io_context()
获取全局任务上下文的io_context。
Definition async.hpp:278
bool shutdown_requested()
查询全局任务上下文是否已经进入停机流程。
Definition async.hpp:226