libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
signal.cpp
1#include "signal.hpp"
2
3#include <sys/wait.h>
4#include <unistd.h>
5
6#include "libxr_def.hpp"
7
8using namespace LibXR;
9
10extern uint64_t _libxr_webots_time_count; // NOLINT
11
12ErrorCode Signal::Action(Thread &thread, int signal) {
14 ASSERT(signal >= SIGRTMIN && signal <= SIGRTMAX);
15
16 if (pthread_kill(thread, signal) == 0) {
17 return ErrorCode::OK;
18 } else {
19 return ErrorCode::FAILED;
20 }
21}
22
24 UNUSED(in_isr);
25 return Action(thread, signal);
26}
27
28ErrorCode Signal::Wait(int signal, uint32_t timeout) {
31 ASSERT(signal >= SIGRTMIN && signal <= SIGRTMAX);
32
33 uint32_t start_time = _libxr_webots_time_count;
34
38
39 struct timespec ts;
41
42 uint32_t add = 0;
44 static_cast<__syscall_slong_t>(1U * 1000U * 1000U) + ts.tv_nsec;
45 add = raw_time / (static_cast<int64_t>(1000U * 1000U * 1000U));
46
47 ts.tv_sec += add;
48 ts.tv_nsec = raw_time % (static_cast<int64_t>(1000U * 1000U * 1000U));
49
50 int res = sigtimedwait(&waitset, nullptr, &ts);
51
52 while (_libxr_webots_time_count - start_time < timeout) {
53 res = !sigtimedwait(&waitset, nullptr, &ts);
54 if (res) {
55 return ErrorCode::OK;
56 }
57 }
59 return res == signal ? ErrorCode::OK : ErrorCode::TIMEOUT;
60}
static ErrorCode Action(Thread &thread, int signal)
触发目标线程的信号处理操作。 Triggers a signal action on the target thread.
Definition signal.cpp:7
static ErrorCode Wait(int signal, uint32_t timeout=UINT32_MAX)
等待指定信号的到来。 Waits for the specified signal.
Definition signal.cpp:35
static ErrorCode ActionFromCallback(Thread &thread, int signal, bool in_isr)
在回调环境中触发目标线程的信号处理操作。 Triggers a signal action on the target thread from a callback environment.
Definition signal.cpp:16
线程管理类,封装 FreeRTOS 任务创建和调度 Thread management class encapsulating FreeRTOS task creation and scheduling
Definition thread.hpp:15
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值