-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPOLL.h
76 lines (58 loc) · 1.75 KB
/
POLL.h
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
#ifndef ABC_POLL_H
#define ABC_POLL_H
#include <limits.h>
#include <poll.h>
#include "INT.h"
#define POLL_MAX_FILES 1024
con ok64 POLLfail = 0x658555aa5b70;
con ok64 POLLnone = 0x658555cb3ca9;
con ok64 POLLnoroom = 0x658555cb3db3cf1;
con ok64 POLLnodata = 0x658555cb3a25e25;
con ok64 POLLaccept = 0x6585559679e9d38;
struct POLLstate;
struct POLLctl;
typedef ok64 (*POLLfunT)(struct POLLstate* state);
typedef ok64 (*POLLfunI)(struct POLLctl* ctl);
struct POLLctl {
B$u8c writes;
Bu8 readbuf;
Bu8 writebuf;
$u8c name;
ok64 o;
POLLfunI fn;
int fd;
};
typedef struct POLLctl POLLctl;
typedef POLLctl POLLstate[POLL_MAX_FILES];
fun POLLctl* POLLfind(POLLstate state, int fd) {
for (int i = 0; i < POLL_MAX_FILES && state[i].fn != nil; ++i)
if (state[i].fd == fd) return state + i;
return nil;
}
ok64 POLLadd(POLLstate state, int fd, $u8c name, POLLfunI fi);
ok64 POLLlisten(POLLstate state, int fd, $u8c name, POLLfunI fi);
ok64 POLLdelctl(POLLstate state, POLLctl* ctl, ok64 o);
fun ok64 POLLdel(POLLstate state, int fd, ok64 o) {
POLLctl* ctl = POLLfind(state, fd);
if (ctl == nil) return POLLnone;
return POLLdelctl(state, ctl, o);
}
fun ok64 POLLfeed$(POLLctl* ctl, u8c$ data) {
$u8c$ idle = B$u8cidle(ctl->writes);
if ($empty(idle)) return POLLnoroom;
$$u8cfeed1(idle, data);
return OK;
}
fun ok64 POLLfeed(POLLctl* ctl, $u8c data) {
$u8c n = {ctl->writebuf[2], nil};
ok64 o = $u8feed(Bu8idle(ctl->writebuf), data);
if (o != OK) return o;
n[1] = ctl->writebuf[2];
return POLLfeed$(ctl, n);
}
fun ok64 POLLdrain($u8 into, POLLctl* ctl) {
return $u8drain(into, Bu8cdata(ctl->readbuf));
}
ok64 POLLonce(POLLstate state, size_t ms);
ok64 POLLloop(POLLstate state, size_t ms);
#endif