GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
async.cpp
1#include "core/async.hpp"
2
3using namespace roboctrl::async;
4
5task_context::task_context(){
6 log_info("Task Context initiated");
7}
8
9void task_context::spawn(task_context::task_type&& task){
10 asio::co_spawn(context_, std::move(task), [this](std::exception_ptr error) {
11 if (error) {
12 handle_unhandled_exception(error, "coroutine");
13 }
14 });
15}
16
17void task_context::set_shutdown_handler(shutdown_handler_type handler)
18{
19 shutdown_handler_ = std::move(handler);
20}
21
22void task_context::run() noexcept {
23 log_info("Start running task context");
24 while (!context_.stopped()) {
25 try {
26 context_.run();
27 break;
28 } catch (...) {
29 // A handler submitted directly through the exposed io_context can
30 // still throw. Route it through the same fatal-shutdown path and
31 // continue running so that the handler can finish.
32 handle_unhandled_exception(
33 std::current_exception(), "event-loop handler");
34 }
35 }
36}
37
38void task_context::stop(){
40}
41
42
44 info_ = _info;
45 return true;
46}
47
48void task_context::handle_unhandled_exception(
49 std::exception_ptr error, std::string_view origin) noexcept
50{
51 try {
52 if (error) {
53 std::rethrow_exception(error);
54 }
55 } catch (const std::exception& exception) {
56 try {
57 log_error("unhandled exception from {}: {}", origin, exception.what());
58 } catch (...) {
59 }
60 } catch (...) {
61 try {
62 log_error("unhandled exception from {}: unknown exception", origin);
63 } catch (...) {
64 }
65 }
66
67 failed_ = true;
69}
70
72{
73 if (shutdown_started_) {
74 return;
75 }
76 shutdown_started_ = true;
77
78 if (!shutdown_handler_) {
79 // Applications with actuators must install a bounded safe-stop
80 // handler before run(). Without one, stopping immediately is the only
81 // generic action Core can take without depending on control layers.
82 context_.stop();
83 return;
84 }
85
86 try {
87 auto shutdown_task = shutdown_handler_();
88 asio::co_spawn(
89 context_, std::move(shutdown_task),
90 [this](std::exception_ptr shutdown_error) {
91 finish_shutdown(shutdown_error);
92 });
93 } catch (...) {
94 finish_shutdown(std::current_exception());
95 }
96}
97
98void task_context::finish_shutdown(std::exception_ptr error) noexcept
99{
100 if (error) {
101 try {
102 std::rethrow_exception(error);
103 } catch (const std::exception& exception) {
104 try {
105 log_error("shutdown handler failed: {}", exception.what());
106 } catch (...) {
107 }
108 } catch (...) {
109 try {
110 log_error("shutdown handler failed with an unknown exception");
111 } catch (...) {
112 }
113 }
114 }
115 context_.stop();
116}
异步任务上下文组件。
void request_shutdown() noexcept
请求一次安全停机。
Definition async.cpp:71
void run() noexcept
开始运行任务上下文。
Definition async.cpp:22
bool init(info_type _info)
初始化 task_context
Definition async.cpp:43
void spawn(task_type &&task)
添加一个协程任务到上下文中执行。
Definition async.cpp:9
void set_shutdown_handler(shutdown_handler_type handler)
注册异常或外部请求共用的一次性安全停机任务。
Definition async.cpp:17
void log_info(std::format_string< Args... > fmt, Args &&...args) const
输出info日志
Definition logger.h:214
异步上下文相关功能
Definition async.hpp:38
void request_shutdown()
请求运行全局任务上下文的安全停机任务。
Definition async.hpp:236