-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheio.c
265 lines (189 loc) · 5.17 KB
/
eio.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Implemention of nimo eio network library
*
* */
#include "eio.h"
#include <linux/version.h>
/**
* new_eio_loop
*/
eio_loop* new_eio_loop(unsigned int max) {
eio_loop *eio = (eio_loop*) calloc(1, sizeof(eio_loop));
if (eio == NULL)
return NULL;
eio->run = 0;
//just a hint for kernel
eio->epfd = epoll_create(max);
if (-1 == eio->epfd)
goto error;
max = (max >= 64 ? max : 64);
eio->max_events = max;
eio->poll_evs = (struct epoll_event*) calloc(max, sizeof(struct epoll_event));
eio->eio_evs = (struct eio_event*) calloc(max, sizeof(struct eio_event));
eio->hz = DEFAULT_EIO_LOOP_HZ;
return eio;
error:
free(eio);
return NULL;
}
/**
* eio_loop_file_event
*/
int eio_loop_file_event(eio_loop *eio, int fd, int mask, int op, ev_file_proc proc, void* context) {
assert(eio != NULL);
assert(fd > 0);
assert(eio->max_events > fd);
// assert(((op & EIO_EVENT_CLEAR) && mask == EIO_NONE) || (!(op & EIO_EVENT_CLEAR) && mask != EIO_NONE));
struct epoll_event ev = {0};
ev.data.fd = fd;
// get current event bitset
ev.events = eio->eio_evs[fd].ev.file.ep_mask;
// decide op on fd MOD or ADD
int ep_ctl = 0;
// ev.events |= EPOLLET;
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,17)
ev.events |= EPOLLRDHUP;
#endif
switch (op) {
case EIO_EVENT_ADD:
if (mask & EIO_WRITEABLE) {
ev.events |= EPOLLOUT;
eio->eio_evs[fd].ev.file.wproc = proc;
}
if (mask & EIO_READABLE) {
ev.events |= EPOLLIN;
eio->eio_evs[fd].ev.file.rproc = proc;
}
if (mask & EIO_ERR)
eio->eio_evs[fd].ev.file.errproc = proc;
// add or update
ep_ctl = eio->eio_evs[fd].mask ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
eio->eio_evs[fd].mask |= mask;
eio->eio_evs[fd].ev.file.ep_mask = ev.events;
break;
case EIO_EVENT_DEL:
assert(proc == NULL);
if (eio->eio_evs[fd].mask == EIO_NONE)
return eio_fail;
// unregister
if (mask & EIO_WRITEABLE) {
ev.events &= ~EPOLLOUT;
eio->eio_evs[fd].ev.file.wproc = NULL;
}
if (mask & EIO_READABLE) {
ev.events &= ~EPOLLIN;
eio->eio_evs[fd].ev.file.rproc = NULL;
}
if (mask & EIO_ERR)
eio->eio_evs[fd].ev.file.errproc = NULL;
eio->eio_evs[fd].mask &= ~mask;
eio->eio_evs[fd].ev.file.ep_mask = ev.events;
// update or delete
ep_ctl = (eio->eio_evs[fd].mask) ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;
break;
case EIO_EVENT_CLEAR:
assert(proc == NULL);
eio->eio_evs[fd].mask = EIO_NONE;
eio->eio_evs[fd].ev.file.ep_mask = 0;
// delete
ep_ctl = EPOLL_CTL_DEL;
break;
case EIO_EVENT_GET:
assert(proc == NULL);
return eio->eio_evs[fd].mask;
}
eio->eio_evs[fd].user_data = context;
/*
* we discard return -1 and error. because of duplicated register
* and epoll_ctl an existing file event. and for syscall performance
* consideration. so we return -1 directly and throw it to user
*
* Note: Kernel < 2.6.9 requires a non null event pointer even for *EPOLL_CTL_DEL*
*
*/
return epoll_ctl(eio->epfd, ep_ctl, fd, &ev) != -1 ? eio->eio_evs[fd].mask : -1;
}
/**
* eio_loop_destroy
*/
void eio_loop_destroy(eio_loop *eio) {
assert(eio != NULL);
assert(eio->run != 1);
close(eio->epfd);
free(eio);
}
/**
* eio_loop_before_proc
*/
void eio_loop_before_proc(eio_loop *loop, eio_loop_before proc) {
assert(loop != NULL);
loop->before = proc;
}
inline void eio_loop_stop(eio_loop *eio) {
eio->run = 0;
}
static int poll_time_event(eio_loop* eio) {
return 0;
}
static int poll_file_event(eio_loop* eio) {
int n = epoll_wait(eio->epfd, eio->poll_evs, eio->max_events, 1000 / eio->hz);
// if we encounter the SIGTRAP , gdb/pstack
if (n <= 0 && errno == EINTR)
return 0;
if (0 == eio->poll_evs[0].data.fd && errno == EINTR)
return 0;
for (int i=0; i!=n; i++) {
struct eio_event *event = &eio->eio_evs[eio->poll_evs[i].data.fd];
// events may not be cared in this time
// discard in this time !!
if (0 == event->mask)
continue;
uint32_t happen = eio->poll_evs[i].events;
if ((happen & EPOLLERR) || (happen & EPOLLHUP)
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,17)
|| (happen & EPOLLRDHUP)
#endif
) {
// for ERROR
if (event->ev.file.errproc) {
event->ev.file.errproc(eio, eio->poll_evs[i].data.fd, EIO_ERR, event->user_data);
// don't trigger EPOLLIN
continue;
}
}
if ((happen & EPOLLIN) && event->ev.file.rproc) {
// for READ
// event->mask &= ~EIO_READABLE;
// event->ev.file.ep_mask &= ~EPOLLIN;
event->ev.file.rproc(eio, eio->poll_evs[i].data.fd, EIO_READABLE, event->user_data);
}
if ((eio->poll_evs[i].events & EPOLLOUT) && event->ev.file.wproc) {
// for WRITE
// event->mask &= ~EIO_WRITEABLE;
// event->ev.file.ep_mask &= ~EPOLLOUT;
event->ev.file.wproc(eio, eio->poll_evs[i].data.fd, EIO_WRITEABLE, event->user_data);
}
}
return n;
}
/**
* eio_loop_run
*/
eio_loop* eio_loop_run(eio_loop *eio) {
assert(eio != NULL);
eio->run = 1;
// do an infinite loop for epoll_wait
while(eio->run) {
if (eio->before != NULL)
eio->before(eio);
/**
* check the timer event
*/
eio->stats.timer_events += poll_time_event(eio);
/**
* check the timer event
*/
eio->stats.file_events += poll_file_event(eio);
}
return eio;
}