-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_linux.c.v
143 lines (120 loc) · 3.01 KB
/
loop_linux.c.v
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
module picoev
#include <sys/epoll.h>
$if !musl ? {
#include <sys/cdefs.h> // needed for cross compiling to linux
}
fn C.epoll_create(__flags int) int
fn C.epoll_wait(__epfd int, __events &C.epoll_event, __maxevents int, __timeout int) int
fn C.epoll_ctl(__epfd int, __op int, __fd int, __event &C.epoll_event) int
@[typedef]
union C.epoll_data {
mut:
ptr voidptr
fd int
u32 u32
u64 u64
}
@[packed]
pub struct C.epoll_event {
events u32
data C.epoll_data
}
@[heap]
pub struct EpollLoop {
mut:
id int
epoll_fd int
events [1024]C.epoll_event
now i64
}
type LoopType = EpollLoop
// create_epoll_loop creates a new epoll instance for and returns an
// `EpollLoop` struct with `id`
pub fn create_epoll_loop(id int) !&EpollLoop {
mut loop := &EpollLoop{
id: id
}
loop.epoll_fd = C.epoll_create(max_fds)
if loop.epoll_fd == -1 {
return error('could not create epoll loop!')
}
return loop
}
@[direct_array_access]
fn (mut pv Picoev) update_events(fd int, events int) int {
// check if fd is in range
assert fd < max_fds
mut target := pv.file_descriptors[fd]
mut ev := C.epoll_event{}
// fd belongs to loop
if events & picoev_del != target.events && target.loop_id != pv.loop.id {
return -1
}
if events & picoev_readwrite == target.events {
return 0
}
// vfmt off
ev.events = u32(
(if events & picoev_read != 0 { C.EPOLLIN } else { 0 })
|
(if events & picoev_write != 0 { C.EPOLLOUT } else { 0 })
)
// vfmt on
ev.data.fd = fd
if events & picoev_del != 0 {
// nothing to do
} else if events & picoev_readwrite == 0 {
// delete the file if it exists
epoll_ret := C.epoll_ctl(pv.loop.epoll_fd, C.EPOLL_CTL_DEL, fd, &ev)
// check error
assert epoll_ret == 0
} else {
// change settings to 0
mut epoll_ret := C.epoll_ctl(pv.loop.epoll_fd, C.EPOLL_CTL_MOD, fd, &ev)
if epoll_ret != 0 {
// if the file is not present we want to add it
assert C.errno == C.ENOENT
epoll_ret = C.epoll_ctl(pv.loop.epoll_fd, C.EPOLL_CTL_ADD, fd, &ev)
// check error
assert epoll_ret == 0
}
}
// convert to u32?
target.events = u32(events)
return 0
}
@[direct_array_access]
fn (mut pv Picoev) poll_once(max_wait_in_sec int) int {
nevents := C.epoll_wait(pv.loop.epoll_fd, &pv.loop.events[0], max_fds, max_wait_in_sec * 1000)
if nevents == -1 {
// timeout has occurred
return -1
}
for i := 0; i < nevents; i++ {
mut event := pv.loop.events[i]
target := unsafe { pv.file_descriptors[event.data.fd] }
unsafe {
assert event.data.fd < max_fds
}
if pv.loop.id == target.loop_id && target.events & picoev_readwrite != 0 {
mut read_events := 0
if event.events & u32(C.EPOLLIN) != 0 {
read_events |= picoev_read
}
if event.events & u32(C.EPOLLOUT) != 0 {
read_events |= picoev_write
}
if read_events != 0 {
// do callback!
unsafe { target.cb(event.data.fd, read_events, &pv) }
}
} else {
// defer epoll delete
event.events = 0
unsafe {
C.epoll_ctl(pv.loop.epoll_fd, C.EPOLL_CTL_DEL, event.data.fd, &event)
}
}
}
return 0
}