libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
libxr_system.cpp
1#include "libxr_system.hpp"
2
3#include <emscripten.h>
4
5#include "libxr_assert.hpp"
6#include "libxr_def.hpp"
7#include "libxr_rw.hpp"
8#include "libxr_type.hpp"
9#include "list.hpp"
10#include "queue.hpp"
11#include "semaphore.hpp"
12#include "thread.hpp"
13#include "timebase.hpp"
14#include "timer.hpp"
15#include "webasm_timebase.hpp"
16
17extern "C"
18{
19 // JS 会调用它,传字符串进来
20 void receive_input(const char *js_input)
21 {
23 {
24 LibXR::STDIO::read_->queue_data_->PushBatch(
25 reinterpret_cast<const uint8_t *>(js_input), strlen(js_input));
27 }
28 }
29}
30
32{
33 static LibXR::WebAsmTimebase libxr_webasm_timebase;
34
35 auto write_fun = [](WritePort &port)
36 {
37 static uint8_t write_buff[1024];
38 WritePort::WriteInfo info;
39 while (true)
40 {
41 if (port.queue_info_->Pop(info) != ErrorCode::OK)
42 {
43 return ErrorCode::OK;
44 }
45
46 port.queue_data_->PopBatch(write_buff, info.size);
47 EM_ASM(
48 {
49 var ptr = $0;
50 var len = $1;
51 for (var i = 0; i < len; i++)
52 {
53 Module.put_char(String.fromCharCode(HEAPU8[ptr + i]));
54 }
55 },
56 reinterpret_cast<uintptr_t>(write_buff), info.size);
57
58 port.queue_info_->Pop(info);
59
60 port.UpdateStatus(false, ErrorCode::OK, info.op, info.size);
61 }
62
63 return ErrorCode::OK;
64 };
65
67 new LibXR::WritePort(32, static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE));
68
69 *LibXR::STDIO::write_ = write_fun;
70
71 auto read_fun = [](ReadPort &port)
72 {
73 ReadInfoBlock block;
74
75 if (port.queue_block_->Peek(block) != ErrorCode::OK)
76 {
77 return ErrorCode::EMPTY;
78 }
79
80 block.op_.MarkAsRunning();
81
82 if (port.queue_data_->Size() >= block.data_.size_)
83 {
84 port.queue_data_->PopBatch(block.data_.addr_, block.data_.size_);
85 port.queue_block_->Pop();
86
87 port.read_size_ = block.data_.size_;
88 block.op_.UpdateStatus(false, ErrorCode::OK);
89 return ErrorCode::OK;
90 }
91 else
92 {
93 return ErrorCode::EMPTY;
94 }
95 };
96
98 new LibXR::ReadPort(32, static_cast<size_t>(4 * LIBXR_PRINTF_BUFFER_SIZE));
99
100 *LibXR::STDIO::read_ = read_fun;
101}
102
104{
105 static bool in_timer = false;
106 if (in_timer)
107 {
108 return;
109 }
110
112
113 while (true)
114 {
116 {
117 return;
118 }
119
120 in_timer = true;
123 in_timer = false;
124 }
125}
ErrorCode PushBatch(const void *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.hpp:175
ErrorCode PopBatch(Data *data, size_t batch_size)
批量弹出数据 / Pops multiple elements from the queue
ReadPort class for handling read operations.
Definition libxr_rw.hpp:309
void ProcessPendingReads()
Processes pending reads.
Definition libxr_rw.hpp:469
static ReadPort * read_
Read port instance. 读取端口。
Definition libxr_rw.hpp:693
static WritePort * write_
Write port instance. 写入端口。
Definition libxr_rw.hpp:694
static TimestampMS GetMilliseconds()
获取当前时间的毫秒级时间戳。 Gets the current timestamp in milliseconds.
Definition timebase.hpp:63
static void Refresh()
刷新定时任务状态 Refreshes the state of periodic tasks
Definition timer.hpp:197
static void RefreshTimerInIdle()
在空闲时刷新定时器 Refreshes the timer during idle time
WebAsmTimebase 类,用于获取 WebAssembly 系统的时间基准。
WritePort class for handling write operations.
Definition libxr_rw.hpp:503
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值
void PlatformInit()
平台初始化函数 Platform initialization function