-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevnt.c
191 lines (170 loc) · 3.02 KB
/
evnt.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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include "evnt.h"
void die(char *);
int exiting;
#define tgt(a, b) \
((a).tv_sec == (b).tv_sec ? \
(a).tv_usec > (b).tv_usec : \
(a).tv_sec > (b).tv_sec)
typedef struct evnt Evnt;
typedef struct alarm Alarm;
struct evnt {
int valid;
int fd;
int flags;
void (*f)(int, int, void *);
void *p;
};
struct alarm {
struct timeval t;
void (*f)();
};
static void pushalarm(Alarm);
static void popalarm(void);
static Alarm ah[MaxAlarms + 1];
static int na;
static Evnt *elist;
static int ne;
static struct timeval curtime;
void
ev_time(struct timeval *t)
{
if (!curtime.tv_sec)
gettimeofday(&curtime, 0);
*t = curtime;
}
int
ev_alarm(int ms, void (*f)())
{
Alarm a;
if (na >= MaxAlarms)
return 1;
a.f = f;
ev_time(&a.t);
a.t.tv_usec += ms * 1000;
if (a.t.tv_usec >= 1000000) {
a.t.tv_sec++;
a.t.tv_usec -= 1000000;
}
pushalarm(a);
return 0;
}
void
ev_register(int fd, int flags, void (*f)(int, int, void *), void *p)
{
elist = realloc(elist, (ne + 1) * sizeof(Evnt));
assert(elist);
elist[ne] = (Evnt){1, fd, flags, f, p};
ne++;
}
void
ev_cancel(int fd)
{
int i;
for (i=0; i<ne; i++)
if (elist[i].fd == fd) {
elist[i].valid = 0;
return;
}
assert(0);
}
void
ev_loop()
{
struct timeval tv;
Alarm a;
fd_set rfds, wfds;
int i, j, maxfd, flags;
while (!exiting) {
maxfd = -1;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
for (i=0; i < ne; i++) {
assert(elist[i].valid);
if (elist[i].flags & ERead)
FD_SET(elist[i].fd, &rfds);
if (elist[i].flags & EWrite)
FD_SET(elist[i].fd, &wfds);
if (elist[i].fd > maxfd)
maxfd = elist[i].fd;
}
if (na) {
gettimeofday(&tv, 0);
if (!tgt(ah[1].t, tv))
tv = (struct timeval){0, 0};
else {
tv.tv_sec = ah[1].t.tv_sec - tv.tv_sec;
tv.tv_usec = ah[1].t.tv_usec - tv.tv_usec;
if (tv.tv_usec < 0) {
tv.tv_sec--;
tv.tv_usec += 1000000;
}
}
} else
tv = (struct timeval){10000, 0};
if (select(maxfd+1, &rfds, &wfds, 0, &tv) == -1) {
if (errno == EINTR)
continue;
die("select error");
}
gettimeofday(&curtime, 0);
while (na && !tgt(ah[1].t, curtime)) {
a = ah[1];
popalarm();
a.f();
}
for (i=0; i<ne; i++) {
flags = 0;
if (FD_ISSET(elist[i].fd, &rfds))
flags |= ERead;
if (FD_ISSET(elist[i].fd, &wfds))
flags |= EWrite;
if (flags == 0 || !elist[i].valid)
continue;
elist[i].f(elist[i].fd, flags, elist[i].p);
}
for (i=j=0; i<ne; i++)
if (elist[i].valid)
elist[j++] = elist[i];
ne = j;
}
}
static void
pushalarm(Alarm a)
{
Alarm t;
int i, j;
i = ++na;
ah[i] = a;
while ((j = i / 2) && tgt(ah[j].t, ah[i].t)) {
t = ah[j];
ah[j] = ah[i];
ah[i] = t;
i = j;
}
}
static void
popalarm()
{
Alarm t;
int i, j;
assert(na);
i = 1;
ah[i] = ah[na--];
while ((j = 2 * i) <= na) {
if (j+1 <= na)
if (tgt(ah[j].t, ah[j+1].t))
j++;
if (!tgt(ah[i].t, ah[j].t))
return;
t = ah[j];
ah[j] = ah[i];
ah[i] = t;
i = j;
}
}