-
Notifications
You must be signed in to change notification settings - Fork 0
/
ev.c
236 lines (205 loc) · 4.79 KB
/
ev.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
/*only for epoll now*/
#include "proxy.h"
unsigned int ev_hash(void* id)
{
return murmur_hash2(id, sizeof(unsigned int));
}
int ev_comp_func(void* d1, void*d2)
{
return *((int*)d1) - *((int*)d2) == 0 ? 1 : 0;
}
ev_t* ev_create2(void* data, size_t size)
{
ev_t* ev;
int fd = epoll_create(1024/*kernel hint*/);
if (fd > 0) {
ev = (ev_t*)calloc(1, sizeof(ev_t));
if (!ev) {
return NULL;
}
ev->fd = fd;
ev->data = data;
ev->next_time_id = 0;
ev->ti = NULL;
ev->api_data=malloc(sizeof(struct epoll_event)*EV_COUNT);
if (!ev->api_data) {
free(ev);
return NULL;
}
ev->events_size = size;
ev->events = calloc(size, sizeof(ev_file_item_t));
if (!ev->events) {
free(ev->api_data);
free(ev);
return NULL;
}
ev->table = create_hashtable(16, ev_hash, ev_comp_func);
ev->after = NULL;
ev->root = RB_ROOT;
return ev;
}
return NULL;
}
ev_t* ev_create(void* data)
{
return ev_create2(data, 1024);
}
int ev_add_file_item(ev_t* ev, int fd, int mask, void* d,
ev_file_func* rf, ev_file_func* wf)
{
if (fd >= ev->events_size) {
W("fd %d is large than size : %zu",
fd, ev->events_size);
return -1;
}
ev_file_item_t *item = ev->events + fd;
if (item->mask > 0) {
return -1;
}
struct epoll_event epev = {0};
epev.events = mask;
epev.data.fd = fd;
if (epoll_ctl(ev->fd, EV_CTL_ADD, fd, &epev) == 0) {
item->mask = mask;
item->data = d;
item->wfunc = wf;
item->rfunc = rf;
item->fd = fd;
return 0;
}
return -1;
}
int ev_del_file_item(ev_t *ev, int fd)
{
if (fd >= ev->events_size) return -1;
ev_file_item_t* fi = ev->events + fd;
if (fi->mask == 0) return 0;
fi->mask = 0;
int op = EV_CTL_DEL;
struct epoll_event epev;
epev.events = fi->events;
epev.data.fd = fd;
return epoll_ctl(ev->fd, op ,fd, &epev);
}
static int _ev_insert_timer(ev_t *ev, _ev_time_item_inner_t* ti)
{
struct hashtable *table = ev->table;
int r = hashtable_insert(table, &(ti->id), ti);
//this hashtable_insert return 0 faile, -1 sucess.....
if (r == -1) {
D("insert ok! ti->id: %u", ti->id);
return 0;
}
return -1;
}
static void _ev_delete_from_timer(ev_t* ev, _ev_time_item_inner_t* ti)
{
struct hashtable *table = ev->table;
if (NULL == hashtable_remove(table, &(ti->id)) ) {
E("remove hashtable error %u" , ti->id);
}
}
int
ev_time_item_ctl(ev_t* ev, int op, ev_time_item_t* item)
{
int idx;
time_t t = time(NULL);
time_t p = item->time - t;
if (op == EV_CTL_ADD) {
if (p > 3600 || p < 0) {
//only support 1 hour
return -1;
}
_ev_time_item_inner_t* tii = calloc(1, sizeof(*tii));
if (!tii) {
W("no mem for tii");
return -1;
}
tii->id = ++ ev->next_time_id;
if (_ev_insert_timer(ev, tii) < 0) {
free(tii);
W("_ev_insert_timmer failed!");
return -1;
}
item->__inner = tii;
// tii->id = ++ ev->next_time_id;
tii->item = item;
idx = (p + ev->timer_current_task) % EV_TIMER_SIZE;
_ev_time_item_inner_t* tmp = ev->timer_task_list[idx];
tii->next = tmp;
ev->timer_task_list[idx] = tii;
}
else if (op == EV_CTL_DEL) {
if (item->__executed) {
return 0;
}
_ev_time_item_inner_t* tii = item->__inner;
_ev_delete_from_timer(ev, tii);
tii->deleted = 1;
}
return 0;
}
void
ev_main(ev_t* ev)
{
D("ev_main started");
while (ev->stop <= 0) {
//try to process the timer first
int idx = ev->timer_current_task % EV_TIMER_SIZE;
_ev_time_item_inner_t* tii = ev->timer_task_list[idx];
while (tii) {
ev_time_item_t *ti = tii->item;
time_t now = time(NULL);
_ev_time_item_inner_t* tmp = tii;
if (tii->deleted == 1) {
D("got delete item %u", tii->id);
ev->timer_task_list[idx] = tii->next;
tii = tii->next;
free(tmp);
}
else if (ti->time > now) {
//D("the timer should be called later");
break;
}
else {
D("got timer #%u to run", tii->id);
ti->__executed = 1;
ti->func(ev, ti);
_ev_delete_from_timer(ev,tmp);
ev->timer_task_list[idx] = tii->next;
tii = tii->next;
free(tmp);
}
}
if (!tii) {
ev->timer_current_task++;
}
//process file events
int i,j;
struct epoll_event* e = ev->api_data;
j = epoll_wait(ev->fd,e,EV_COUNT,100);
for(i=0; i<j ;i++) {
int fd = e[i].data.fd;
ev_file_item_t* fi = ev->events + fd;
int evts = e[i].events;
D("events %d", evts);
if (evts & EV_READABLE & fi->mask) {
if (fi->rfunc) {
D("RFUNC");
fi->rfunc(ev,fi);
}
}
if (evts & EV_WRITABLE & fi->mask) {
if (fi->wfunc) {
D("WFUNC");
fi->wfunc(ev,fi);
}
}
}
//Handle the after event Handle
if (ev->after != NULL) {
ev->after(ev);
}
} /*while*/
D("worker EV_MAIN stopped");
}