libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
thread.cpp
1#include "thread.hpp"
2
3#include <sys/time.h>
4
5#include <cerrno>
6
7#include "libxr_def.hpp"
8
9using namespace LibXR;
10
11extern struct timeval libxr_linux_start_time;
12extern struct timespec libxr_linux_start_time_spec;
13
14Thread Thread::Current(void) { return Thread(pthread_self()); }
15
16void Thread::Sleep(uint32_t milliseconds) {
17 struct timespec ts;
18 ts.tv_sec = milliseconds / 1000;
19 ts.tv_nsec = static_cast<__syscall_slong_t>((milliseconds % 1000) * 1000000);
20 UNUSED(clock_nanosleep(CLOCK_REALTIME, 0, &ts, nullptr));
21}
22
23void Thread::SleepUntil(TimestampMS &last_waskup_time, uint32_t time_to_sleep) {
24 last_waskup_time = last_waskup_time + time_to_sleep;
25
26 struct timespec ts = libxr_linux_start_time_spec;
27 uint32_t add = 0;
28 uint32_t secs = last_waskup_time / 1000;
29 int64_t raw_time = static_cast<int64_t>(last_waskup_time) * 1000U * 1000U;
30 add = raw_time / (static_cast<int64_t>(1000U * 1000U * 1000U));
31 ts.tv_sec += (add + secs);
32 ts.tv_nsec = raw_time % (static_cast<int64_t>(1000U * 1000U * 1000U));
33
34 while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, &ts) &&
35 errno == EINTR) {
36 }
37}
38
39uint32_t Thread::GetTime() {
40 struct timeval tv;
41 gettimeofday(&tv, nullptr);
42 return ((tv.tv_sec - libxr_linux_start_time.tv_sec) * 1000 +
43 (tv.tv_usec - libxr_linux_start_time.tv_usec) / 1000) %
44 UINT32_MAX;
45}
46
47void Thread::Yield() { sched_yield(); }
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition thread.hpp:14
Thread()
默认构造函数,初始化空线程 Default constructor initializing an empty thread
Definition thread.hpp:34
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:39
static Thread Current(void)
获取当前线程对象 Gets the current thread object
Definition thread.cpp:14
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
Definition thread.cpp:16
static void Yield()
让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
Definition thread.cpp:47
static void SleepUntil(TimestampMS &last_waskup_time, uint32_t time_to_sleep)
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
Definition thread.cpp:23
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
LibXR 命名空间
Definition ch32_gpio.hpp:9