1#include "libxr_system.hpp"
12#include "libxr_def.hpp"
13#include "libxr_rw.hpp"
14#include "libxr_type.hpp"
15#include "linux_timebase.hpp"
20struct timespec libxr_linux_start_time_spec;
28static constexpr size_t host_stdio_queue_bytes = 4096;
36 : ReadPort(size), space_sem_(space_sem)
41 void OnReadQueueSpaceAvailable(
bool in_isr)
override
43 space_sem_.PostFromCallback(in_isr);
53 static uint8_t read_buff[host_stdio_queue_bytes];
55 if (!isatty(STDIN_FILENO))
57 XR_LOG_WARN(
"STDIO.read_: stdin is not a TTY, parking thread forever");
68 FD_SET(STDIN_FILENO, &rfds);
70 int ret = select(STDIN_FILENO + 1, &rfds, NULL, NULL, NULL);
72 if (ret > 0 && FD_ISSET(STDIN_FILENO, &rfds))
75 if (ioctl(STDIN_FILENO, FIONREAD, &ready) != -1 && ready > 0)
78 const size_t read_size = std::min(
static_cast<size_t>(ready), queue.EmptySize());
85 wait_result = stdi_space_sem.
Wait(UINT32_MAX);
91 auto size = fread(read_buff,
sizeof(
char), read_size, stdin);
97 [[maybe_unused]]
const auto push_batch_result = queue.PushBatch(read_buff, size);
122 queue.AvailableSize(),
123 [&failed](
const uint8_t* first,
size_t first_size,
const uint8_t* second,
124 size_t second_size) ->
size_t
126 const size_t first_written =
127 fwrite(first, sizeof(char), first_size, stdout);
128 if (first_written != first_size)
131 return first_written;
134 const size_t second_written =
135 fwrite(second,
sizeof(
char), second_size, stdout);
136 if (second_written != second_size)
140 return first_written + second_written;
143 UNUSED(fflush(stdout));
149 if (!failed_queue.Empty())
163 auto write_fun = [](
WritePort& port, bool)
175 struct termios tty = {};
176 if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &tty) == 0)
179 tty.c_lflag &= ~(ICANON | ECHO);
180 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Linux 时间基准实现 / Linux timebase implementation.
基于 SPSC 字节队列的读端口 / Read endpoint backed by an SPSC byte queue.
ReadQueue GetReadQueue(bool in_isr=false)
获取后端接收入队接口 / Obtain the backend RX producer interface.
static ReadPort * read_
读取端口 / Read port instance.
static WritePort * write_
写入端口 / Write port instance.
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
void Post()
释放(增加)信号量 Releases (increments) the semaphore
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Priority
线程优先级枚举 Enumeration for thread priorities
@ MEDIUM
中等优先级 / Medium priority
void Create(ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
创建新线程 Creates a new thread
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
static uint32_t stack_depth_
线程栈深度 / Thread stack depth
static LibXR::Thread::Priority priority_
线程优先级 / Thread priority
size_t PopWithWriter(size_t limit, Writer &&writer)
通过回调取走队头前缀 / Consume a front prefix through a callback.
基于 SPSC 字节队列的写端口 / Write endpoint backed by an SPSC byte queue.
WriteQueue GetWriteQueue(bool in_isr=false)
获取已提交队头的出队接口 / Obtain a consumer interface for the released front.
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ FAILED
操作失败 / Operation failed
@ OK
操作成功 / Operation successful
void PlatformInit(uint32_t timer_pri=2, uint32_t timer_stack_depth=65536)
平台初始化函数 Platform initialization function