Repository navigation
Expand file tree
/
Copy pathevent.wave
More file actions
154 lines (144 loc) · 5.6 KB
/
Copy pathevent.wave
File metadata and controls
154 lines (144 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import("std::sys::fs")::{close};
pub const EVENT_BACKEND_KIND: i32 = 1;
pub const EVENT_READABLE: i32 = 1;
pub const EVENT_WRITABLE: i32 = 2;
pub const EVENT_ERROR: i32 = 4;
pub const EVENT_HANGUP: i32 = 8;
const EPOLL_CTL_ADD: i32 = 1;
const EPOLL_CTL_DEL: i32 = 2;
const EPOLL_CTL_MOD: i32 = 3;
const EPOLLIN: u32 = 1;
const EPOLLOUT: u32 = 4;
const EPOLLERR: u32 = 8;
const EPOLLHUP: u32 = 16;
const EPOLLRDHUP: u32 = 8192;
pub struct NativeEvent { fd: i64; token: u64; flags: i32; }
import("std::sys::linux::syscall")::{syscall1, syscall4, syscall6};
#[target(arch="x86_64")]
const EPOLL_CREATE_SYSCALL: i64 = 291;
#[target(arch="x86_64")]
const EPOLL_CTL_SYSCALL: i64 = 233;
#[target(arch="x86_64")]
const EPOLL_PWAIT_SYSCALL: i64 = 281;
#[target(arch="aarch64")]
const EPOLL_CREATE_SYSCALL: i64 = 20;
#[target(arch="aarch64")]
const EPOLL_CTL_SYSCALL: i64 = 21;
#[target(arch="aarch64")]
const EPOLL_PWAIT_SYSCALL: i64 = 22;
#[target(arch="riscv64")]
const EPOLL_CREATE_SYSCALL: i64 = 20;
#[target(arch="riscv64")]
const EPOLL_CTL_SYSCALL: i64 = 21;
#[target(arch="riscv64")]
const EPOLL_PWAIT_SYSCALL: i64 = 22;
#[target(arch="loongarch64")]
const EPOLL_CREATE_SYSCALL: i64 = 20;
#[target(arch="loongarch64")]
const EPOLL_CTL_SYSCALL: i64 = 21;
#[target(arch="loongarch64")]
const EPOLL_PWAIT_SYSCALL: i64 = 22;
fun _store_u32(output: ptr<u8>, value: u32) {
deref output[0] = (value & 255) as u8;
deref output[1] = ((value >> 8) & 255) as u8;
deref output[2] = ((value >> 16) & 255) as u8;
deref output[3] = ((value >> 24) & 255) as u8;
}
fun _store_u64(output: ptr<u8>, value: u64) {
deref output[0] = (value & 255) as u8;
deref output[1] = ((value >> 8) & 255) as u8;
deref output[2] = ((value >> 16) & 255) as u8;
deref output[3] = ((value >> 24) & 255) as u8;
deref output[4] = ((value >> 32) & 255) as u8;
deref output[5] = ((value >> 40) & 255) as u8;
deref output[6] = ((value >> 48) & 255) as u8;
deref output[7] = ((value >> 56) & 255) as u8;
}
fun _read_u32(bytes: ptr<u8>) -> u32 {
return bytes[0] as u32
| (bytes[1] as u32 << 8)
| (bytes[2] as u32 << 16)
| (bytes[3] as u32 << 24);
}
fun _read_u64(bytes: ptr<u8>) -> u64 {
return bytes[0] as u64
| (bytes[1] as u64 << 8)
| (bytes[2] as u64 << 16)
| (bytes[3] as u64 << 24)
| (bytes[4] as u64 << 32)
| (bytes[5] as u64 << 40)
| (bytes[6] as u64 << 48)
| (bytes[7] as u64 << 56);
}
fun _epoll_flags(flags: i32) -> u32 {
var native: u32 = EPOLLRDHUP;
if ((flags & EVENT_READABLE) != 0) { native = native | EPOLLIN; }
if ((flags & EVENT_WRITABLE) != 0) { native = native | EPOLLOUT; }
return native;
}
// Linux keeps epoll_event packed on x86_64 for 32-bit compatibility. The
// generic 64-bit ABIs naturally align epoll_data_t, so their data begins at
// byte 8 and each event occupies 16 bytes.
#[target(arch="x86_64")]
fun _epoll_event_stride() -> i32 { return 12; }
#[target(arch="x86_64")]
fun _epoll_data_offset() -> i32 { return 4; }
#[target(arch="aarch64")]
fun _epoll_event_stride() -> i32 { return 16; }
#[target(arch="aarch64")]
fun _epoll_data_offset() -> i32 { return 8; }
#[target(arch="riscv64")]
fun _epoll_event_stride() -> i32 { return 16; }
#[target(arch="riscv64")]
fun _epoll_data_offset() -> i32 { return 8; }
#[target(arch="loongarch64")]
fun _epoll_event_stride() -> i32 { return 16; }
#[target(arch="loongarch64")]
fun _epoll_data_offset() -> i32 { return 8; }
fun _control(handle: i64, operation: i32, fd: i64, token: u64, flags: i32) -> i64 {
var raw: array<u8, 16>;
var data_offset: i32 = _epoll_data_offset();
_store_u32(&raw[0], _epoll_flags(flags));
_store_u64(&raw[data_offset], token);
return syscall4(EPOLL_CTL_SYSCALL, handle, operation as i64, fd, &raw[0] as i64);
}
pub fun event_create(capacity: i32) -> i64 {
if (capacity <= 0) { return -22; }
return syscall1(EPOLL_CREATE_SYSCALL, 0);
}
pub fun event_add(handle: i64, fd: i64, token: u64, flags: i32) -> i64 {
if ((flags & (EVENT_READABLE | EVENT_WRITABLE)) == 0) { return -22; }
return _control(handle, EPOLL_CTL_ADD, fd, token, flags);
}
pub fun event_modify(handle: i64, fd: i64, token: u64, flags: i32) -> i64 {
if ((flags & (EVENT_READABLE | EVENT_WRITABLE)) == 0) { return -22; }
return _control(handle, EPOLL_CTL_MOD, fd, token, flags);
}
pub fun event_remove(handle: i64, fd: i64) -> i64 {
return syscall4(EPOLL_CTL_SYSCALL, handle, EPOLL_CTL_DEL as i64, fd, 0);
}
pub fun event_wait(
handle: i64, output: ptr<NativeEvent>, capacity: i32, timeout_ms: i32
) -> i64 {
if (capacity <= 0 || capacity > 64 || output == null || timeout_ms < -1) { return -22; }
var raw: array<u8, 1024>;
var count: i64 = syscall6(EPOLL_PWAIT_SYSCALL, handle, &raw[0] as i64, capacity as i64, timeout_ms as i64, 0, 8);
if (count < 0) { return count; }
var i: i32 = 0;
while (i < count as i32) {
var offset: i32 = i * _epoll_event_stride();
var native_flags: u32 = _read_u32(&raw[offset]);
var token_offset: i32 = offset + _epoll_data_offset();
var flags: i32 = 0;
if ((native_flags & EPOLLIN) != 0) { flags = flags | EVENT_READABLE; }
if ((native_flags & EPOLLOUT) != 0) { flags = flags | EVENT_WRITABLE; }
if ((native_flags & EPOLLERR) != 0) { flags = flags | EVENT_ERROR; }
if ((native_flags & (EPOLLHUP | EPOLLRDHUP)) != 0) { flags = flags | EVENT_HANGUP; }
deref output[i] = NativeEvent {
fd: -1, token: _read_u64(&raw[token_offset]), flags: flags
};
i += 1;
}
return count;
}
pub fun event_close(handle: i64) -> i64 { return close(handle); }