1 |
root |
1.1 |
#include <math.h> |
2 |
|
|
#include <stdlib.h> |
3 |
root |
1.7 |
#include <unistd.h> |
4 |
|
|
#include <fcntl.h> |
5 |
|
|
#include <signal.h> |
6 |
root |
1.1 |
|
7 |
|
|
#include <stdio.h> |
8 |
|
|
|
9 |
root |
1.4 |
#include <assert.h> |
10 |
root |
1.1 |
#include <errno.h> |
11 |
|
|
#include <sys/time.h> |
12 |
|
|
#include <time.h> |
13 |
|
|
|
14 |
root |
1.12 |
#define HAVE_EPOLL 1 |
15 |
|
|
|
16 |
root |
1.10 |
#ifndef HAVE_MONOTONIC |
17 |
|
|
# ifdef CLOCK_MONOTONIC |
18 |
|
|
# define HAVE_MONOTONIC 1 |
19 |
|
|
# endif |
20 |
root |
1.1 |
#endif |
21 |
|
|
|
22 |
root |
1.10 |
#ifndef HAVE_SELECT |
23 |
|
|
# define HAVE_SELECT 1 |
24 |
|
|
#endif |
25 |
|
|
|
26 |
|
|
#ifndef HAVE_EPOLL |
27 |
|
|
# define HAVE_EPOLL 0 |
28 |
|
|
#endif |
29 |
|
|
|
30 |
|
|
#ifndef HAVE_REALTIME |
31 |
|
|
# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
32 |
|
|
#endif |
33 |
root |
1.1 |
|
34 |
root |
1.4 |
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
35 |
root |
1.1 |
#define MAX_BLOCKTIME 60. |
36 |
|
|
|
37 |
|
|
#include "ev.h" |
38 |
|
|
|
39 |
root |
1.10 |
typedef struct ev_watcher *W; |
40 |
|
|
typedef struct ev_watcher_list *WL; |
41 |
root |
1.12 |
typedef struct ev_watcher_time *WT; |
42 |
root |
1.10 |
|
43 |
root |
1.4 |
static ev_tstamp now, diff; /* monotonic clock */ |
44 |
root |
1.1 |
ev_tstamp ev_now; |
45 |
|
|
int ev_method; |
46 |
|
|
|
47 |
|
|
static int have_monotonic; /* runtime */ |
48 |
|
|
|
49 |
|
|
static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
50 |
root |
1.5 |
static void (*method_modify)(int fd, int oev, int nev); |
51 |
root |
1.1 |
static void (*method_poll)(ev_tstamp timeout); |
52 |
|
|
|
53 |
root |
1.8 |
/*****************************************************************************/ |
54 |
|
|
|
55 |
root |
1.1 |
ev_tstamp |
56 |
|
|
ev_time (void) |
57 |
|
|
{ |
58 |
|
|
#if HAVE_REALTIME |
59 |
|
|
struct timespec ts; |
60 |
|
|
clock_gettime (CLOCK_REALTIME, &ts); |
61 |
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9; |
62 |
|
|
#else |
63 |
|
|
struct timeval tv; |
64 |
|
|
gettimeofday (&tv, 0); |
65 |
|
|
return tv.tv_sec + tv.tv_usec * 1e-6; |
66 |
|
|
#endif |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
static ev_tstamp |
70 |
|
|
get_clock (void) |
71 |
|
|
{ |
72 |
|
|
#if HAVE_MONOTONIC |
73 |
|
|
if (have_monotonic) |
74 |
|
|
{ |
75 |
|
|
struct timespec ts; |
76 |
|
|
clock_gettime (CLOCK_MONOTONIC, &ts); |
77 |
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9; |
78 |
|
|
} |
79 |
|
|
#endif |
80 |
|
|
|
81 |
|
|
return ev_time (); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
#define array_needsize(base,cur,cnt,init) \ |
85 |
|
|
if ((cnt) > cur) \ |
86 |
|
|
{ \ |
87 |
root |
1.2 |
int newcnt = cur ? cur << 1 : 16; \ |
88 |
root |
1.1 |
fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
89 |
|
|
base = realloc (base, sizeof (*base) * (newcnt)); \ |
90 |
|
|
init (base + cur, newcnt - cur); \ |
91 |
|
|
cur = newcnt; \ |
92 |
|
|
} |
93 |
|
|
|
94 |
root |
1.8 |
/*****************************************************************************/ |
95 |
|
|
|
96 |
root |
1.1 |
typedef struct |
97 |
|
|
{ |
98 |
|
|
struct ev_io *head; |
99 |
|
|
unsigned char wev, rev; /* want, received event set */ |
100 |
|
|
} ANFD; |
101 |
|
|
|
102 |
|
|
static ANFD *anfds; |
103 |
|
|
static int anfdmax; |
104 |
|
|
|
105 |
|
|
static int *fdchanges; |
106 |
|
|
static int fdchangemax, fdchangecnt; |
107 |
|
|
|
108 |
|
|
static void |
109 |
|
|
anfds_init (ANFD *base, int count) |
110 |
|
|
{ |
111 |
|
|
while (count--) |
112 |
|
|
{ |
113 |
|
|
base->head = 0; |
114 |
|
|
base->wev = base->rev = EV_NONE; |
115 |
|
|
++base; |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
typedef struct |
120 |
|
|
{ |
121 |
root |
1.10 |
W w; |
122 |
root |
1.1 |
int events; |
123 |
|
|
} ANPENDING; |
124 |
|
|
|
125 |
|
|
static ANPENDING *pendings; |
126 |
|
|
static int pendingmax, pendingcnt; |
127 |
|
|
|
128 |
|
|
static void |
129 |
root |
1.10 |
event (W w, int events) |
130 |
root |
1.1 |
{ |
131 |
|
|
w->pending = ++pendingcnt; |
132 |
|
|
array_needsize (pendings, pendingmax, pendingcnt, ); |
133 |
|
|
pendings [pendingcnt - 1].w = w; |
134 |
|
|
pendings [pendingcnt - 1].events = events; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
static void |
138 |
|
|
fd_event (int fd, int events) |
139 |
|
|
{ |
140 |
|
|
ANFD *anfd = anfds + fd; |
141 |
|
|
struct ev_io *w; |
142 |
|
|
|
143 |
|
|
for (w = anfd->head; w; w = w->next) |
144 |
|
|
{ |
145 |
|
|
int ev = w->events & events; |
146 |
|
|
|
147 |
|
|
if (ev) |
148 |
root |
1.10 |
event ((W)w, ev); |
149 |
root |
1.1 |
} |
150 |
|
|
} |
151 |
|
|
|
152 |
root |
1.9 |
static void |
153 |
root |
1.10 |
queue_events (W *events, int eventcnt, int type) |
154 |
root |
1.9 |
{ |
155 |
|
|
int i; |
156 |
|
|
|
157 |
|
|
for (i = 0; i < eventcnt; ++i) |
158 |
|
|
event (events [i], type); |
159 |
|
|
} |
160 |
|
|
|
161 |
root |
1.8 |
/*****************************************************************************/ |
162 |
|
|
|
163 |
root |
1.12 |
static struct ev_timer **timers; |
164 |
|
|
static int timermax, timercnt; |
165 |
root |
1.4 |
|
166 |
root |
1.12 |
static struct ev_periodic **periodics; |
167 |
|
|
static int periodicmax, periodiccnt; |
168 |
root |
1.1 |
|
169 |
|
|
static void |
170 |
root |
1.12 |
upheap (WT *timers, int k) |
171 |
root |
1.1 |
{ |
172 |
root |
1.12 |
WT w = timers [k]; |
173 |
root |
1.1 |
|
174 |
|
|
while (k && timers [k >> 1]->at > w->at) |
175 |
|
|
{ |
176 |
|
|
timers [k] = timers [k >> 1]; |
177 |
|
|
timers [k]->active = k + 1; |
178 |
|
|
k >>= 1; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
timers [k] = w; |
182 |
|
|
timers [k]->active = k + 1; |
183 |
|
|
|
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
static void |
187 |
root |
1.12 |
downheap (WT *timers, int N, int k) |
188 |
root |
1.1 |
{ |
189 |
root |
1.12 |
WT w = timers [k]; |
190 |
root |
1.1 |
|
191 |
root |
1.4 |
while (k < (N >> 1)) |
192 |
root |
1.1 |
{ |
193 |
|
|
int j = k << 1; |
194 |
|
|
|
195 |
root |
1.4 |
if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
196 |
root |
1.1 |
++j; |
197 |
|
|
|
198 |
|
|
if (w->at <= timers [j]->at) |
199 |
|
|
break; |
200 |
|
|
|
201 |
|
|
timers [k] = timers [j]; |
202 |
root |
1.2 |
timers [k]->active = k + 1; |
203 |
root |
1.1 |
k = j; |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
timers [k] = w; |
207 |
|
|
timers [k]->active = k + 1; |
208 |
|
|
} |
209 |
|
|
|
210 |
root |
1.8 |
/*****************************************************************************/ |
211 |
|
|
|
212 |
root |
1.7 |
typedef struct |
213 |
|
|
{ |
214 |
|
|
struct ev_signal *head; |
215 |
|
|
sig_atomic_t gotsig; |
216 |
|
|
} ANSIG; |
217 |
|
|
|
218 |
|
|
static ANSIG *signals; |
219 |
root |
1.4 |
static int signalmax; |
220 |
root |
1.1 |
|
221 |
root |
1.7 |
static int sigpipe [2]; |
222 |
|
|
static sig_atomic_t gotsig; |
223 |
|
|
static struct ev_io sigev; |
224 |
|
|
|
225 |
root |
1.1 |
static void |
226 |
root |
1.7 |
signals_init (ANSIG *base, int count) |
227 |
root |
1.1 |
{ |
228 |
|
|
while (count--) |
229 |
root |
1.7 |
{ |
230 |
|
|
base->head = 0; |
231 |
|
|
base->gotsig = 0; |
232 |
|
|
++base; |
233 |
|
|
} |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
static void |
237 |
|
|
sighandler (int signum) |
238 |
|
|
{ |
239 |
|
|
signals [signum - 1].gotsig = 1; |
240 |
|
|
|
241 |
|
|
if (!gotsig) |
242 |
|
|
{ |
243 |
|
|
gotsig = 1; |
244 |
|
|
write (sigpipe [1], &gotsig, 1); |
245 |
|
|
} |
246 |
|
|
} |
247 |
|
|
|
248 |
|
|
static void |
249 |
|
|
sigcb (struct ev_io *iow, int revents) |
250 |
|
|
{ |
251 |
|
|
struct ev_signal *w; |
252 |
|
|
int sig; |
253 |
|
|
|
254 |
|
|
gotsig = 0; |
255 |
|
|
read (sigpipe [0], &revents, 1); |
256 |
|
|
|
257 |
|
|
for (sig = signalmax; sig--; ) |
258 |
|
|
if (signals [sig].gotsig) |
259 |
|
|
{ |
260 |
|
|
signals [sig].gotsig = 0; |
261 |
|
|
|
262 |
|
|
for (w = signals [sig].head; w; w = w->next) |
263 |
root |
1.10 |
event ((W)w, EV_SIGNAL); |
264 |
root |
1.7 |
} |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
static void |
268 |
|
|
siginit (void) |
269 |
|
|
{ |
270 |
|
|
fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
271 |
|
|
fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
272 |
|
|
|
273 |
|
|
/* rather than sort out wether we really need nb, set it */ |
274 |
|
|
fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
275 |
|
|
fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
276 |
|
|
|
277 |
|
|
evio_set (&sigev, sigpipe [0], EV_READ); |
278 |
|
|
evio_start (&sigev); |
279 |
root |
1.1 |
} |
280 |
|
|
|
281 |
root |
1.8 |
/*****************************************************************************/ |
282 |
|
|
|
283 |
root |
1.9 |
static struct ev_idle **idles; |
284 |
|
|
static int idlemax, idlecnt; |
285 |
|
|
|
286 |
|
|
static struct ev_check **checks; |
287 |
|
|
static int checkmax, checkcnt; |
288 |
|
|
|
289 |
|
|
/*****************************************************************************/ |
290 |
|
|
|
291 |
root |
1.1 |
#if HAVE_EPOLL |
292 |
|
|
# include "ev_epoll.c" |
293 |
|
|
#endif |
294 |
|
|
#if HAVE_SELECT |
295 |
|
|
# include "ev_select.c" |
296 |
|
|
#endif |
297 |
|
|
|
298 |
|
|
int ev_init (int flags) |
299 |
|
|
{ |
300 |
|
|
#if HAVE_MONOTONIC |
301 |
|
|
{ |
302 |
|
|
struct timespec ts; |
303 |
|
|
if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
304 |
|
|
have_monotonic = 1; |
305 |
|
|
} |
306 |
|
|
#endif |
307 |
|
|
|
308 |
|
|
ev_now = ev_time (); |
309 |
root |
1.4 |
now = get_clock (); |
310 |
|
|
diff = ev_now - now; |
311 |
root |
1.1 |
|
312 |
root |
1.7 |
if (pipe (sigpipe)) |
313 |
|
|
return 0; |
314 |
|
|
|
315 |
|
|
ev_method = EVMETHOD_NONE; |
316 |
root |
1.1 |
#if HAVE_EPOLL |
317 |
root |
1.7 |
if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
318 |
root |
1.1 |
#endif |
319 |
|
|
#if HAVE_SELECT |
320 |
root |
1.7 |
if (ev_method == EVMETHOD_NONE) select_init (flags); |
321 |
root |
1.1 |
#endif |
322 |
|
|
|
323 |
root |
1.7 |
if (ev_method) |
324 |
|
|
{ |
325 |
root |
1.12 |
evw_init (&sigev, sigcb); |
326 |
root |
1.7 |
siginit (); |
327 |
|
|
} |
328 |
|
|
|
329 |
root |
1.1 |
return ev_method; |
330 |
|
|
} |
331 |
|
|
|
332 |
root |
1.8 |
/*****************************************************************************/ |
333 |
|
|
|
334 |
root |
1.1 |
void ev_prefork (void) |
335 |
|
|
{ |
336 |
root |
1.11 |
/* nop */ |
337 |
root |
1.1 |
} |
338 |
|
|
|
339 |
|
|
void ev_postfork_parent (void) |
340 |
|
|
{ |
341 |
root |
1.11 |
/* nop */ |
342 |
root |
1.1 |
} |
343 |
|
|
|
344 |
|
|
void ev_postfork_child (void) |
345 |
|
|
{ |
346 |
|
|
#if HAVE_EPOLL |
347 |
root |
1.5 |
if (ev_method == EVMETHOD_EPOLL) |
348 |
|
|
epoll_postfork_child (); |
349 |
root |
1.1 |
#endif |
350 |
root |
1.7 |
|
351 |
|
|
evio_stop (&sigev); |
352 |
|
|
close (sigpipe [0]); |
353 |
|
|
close (sigpipe [1]); |
354 |
|
|
pipe (sigpipe); |
355 |
|
|
siginit (); |
356 |
root |
1.1 |
} |
357 |
|
|
|
358 |
root |
1.8 |
/*****************************************************************************/ |
359 |
|
|
|
360 |
root |
1.1 |
static void |
361 |
root |
1.5 |
fd_reify (void) |
362 |
|
|
{ |
363 |
|
|
int i; |
364 |
|
|
|
365 |
|
|
for (i = 0; i < fdchangecnt; ++i) |
366 |
|
|
{ |
367 |
|
|
int fd = fdchanges [i]; |
368 |
|
|
ANFD *anfd = anfds + fd; |
369 |
|
|
struct ev_io *w; |
370 |
|
|
|
371 |
|
|
int wev = 0; |
372 |
|
|
|
373 |
|
|
for (w = anfd->head; w; w = w->next) |
374 |
|
|
wev |= w->events; |
375 |
|
|
|
376 |
|
|
if (anfd->wev != wev) |
377 |
|
|
{ |
378 |
|
|
method_modify (fd, anfd->wev, wev); |
379 |
|
|
anfd->wev = wev; |
380 |
|
|
} |
381 |
|
|
} |
382 |
|
|
|
383 |
|
|
fdchangecnt = 0; |
384 |
|
|
} |
385 |
|
|
|
386 |
|
|
static void |
387 |
root |
1.1 |
call_pending () |
388 |
|
|
{ |
389 |
|
|
int i; |
390 |
|
|
|
391 |
|
|
for (i = 0; i < pendingcnt; ++i) |
392 |
|
|
{ |
393 |
|
|
ANPENDING *p = pendings + i; |
394 |
|
|
|
395 |
|
|
if (p->w) |
396 |
|
|
{ |
397 |
|
|
p->w->pending = 0; |
398 |
|
|
p->w->cb (p->w, p->events); |
399 |
|
|
} |
400 |
|
|
} |
401 |
|
|
|
402 |
|
|
pendingcnt = 0; |
403 |
|
|
} |
404 |
|
|
|
405 |
|
|
static void |
406 |
root |
1.12 |
timers_reify () |
407 |
root |
1.1 |
{ |
408 |
root |
1.4 |
while (timercnt && timers [0]->at <= now) |
409 |
root |
1.1 |
{ |
410 |
|
|
struct ev_timer *w = timers [0]; |
411 |
|
|
|
412 |
root |
1.4 |
/* first reschedule or stop timer */ |
413 |
root |
1.1 |
if (w->repeat) |
414 |
|
|
{ |
415 |
root |
1.12 |
w->at = now + w->repeat; |
416 |
|
|
assert (("timer timeout in the past, negative repeat?", w->at > now)); |
417 |
|
|
downheap ((WT *)timers, timercnt, 0); |
418 |
|
|
} |
419 |
|
|
else |
420 |
|
|
evtimer_stop (w); /* nonrepeating: stop timer */ |
421 |
|
|
|
422 |
|
|
event ((W)w, EV_TIMEOUT); |
423 |
|
|
} |
424 |
|
|
} |
425 |
root |
1.4 |
|
426 |
root |
1.12 |
static void |
427 |
|
|
periodics_reify () |
428 |
|
|
{ |
429 |
|
|
while (periodiccnt && periodics [0]->at <= ev_now) |
430 |
|
|
{ |
431 |
|
|
struct ev_periodic *w = periodics [0]; |
432 |
root |
1.1 |
|
433 |
root |
1.12 |
/* first reschedule or stop timer */ |
434 |
|
|
if (w->interval) |
435 |
|
|
{ |
436 |
|
|
w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
437 |
|
|
assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
438 |
|
|
downheap ((WT *)periodics, periodiccnt, 0); |
439 |
root |
1.1 |
} |
440 |
|
|
else |
441 |
root |
1.12 |
evperiodic_stop (w); /* nonrepeating: stop timer */ |
442 |
|
|
|
443 |
|
|
event ((W)w, EV_TIMEOUT); |
444 |
|
|
} |
445 |
|
|
} |
446 |
|
|
|
447 |
|
|
static void |
448 |
root |
1.13 |
periodics_reschedule (ev_tstamp diff) |
449 |
root |
1.12 |
{ |
450 |
|
|
int i; |
451 |
|
|
|
452 |
root |
1.13 |
/* adjust periodics after time jump */ |
453 |
root |
1.12 |
for (i = 0; i < periodiccnt; ++i) |
454 |
|
|
{ |
455 |
|
|
struct ev_periodic *w = periodics [i]; |
456 |
|
|
|
457 |
|
|
if (w->interval) |
458 |
root |
1.4 |
{ |
459 |
root |
1.12 |
ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
460 |
|
|
|
461 |
|
|
if (fabs (diff) >= 1e-4) |
462 |
|
|
{ |
463 |
|
|
evperiodic_stop (w); |
464 |
|
|
evperiodic_start (w); |
465 |
|
|
|
466 |
|
|
i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
467 |
|
|
} |
468 |
root |
1.4 |
} |
469 |
root |
1.12 |
} |
470 |
root |
1.1 |
} |
471 |
|
|
|
472 |
root |
1.4 |
static void |
473 |
|
|
time_update () |
474 |
|
|
{ |
475 |
|
|
int i; |
476 |
root |
1.12 |
|
477 |
root |
1.4 |
ev_now = ev_time (); |
478 |
|
|
|
479 |
|
|
if (have_monotonic) |
480 |
|
|
{ |
481 |
|
|
ev_tstamp odiff = diff; |
482 |
|
|
|
483 |
root |
1.12 |
for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
484 |
root |
1.4 |
{ |
485 |
|
|
now = get_clock (); |
486 |
|
|
diff = ev_now - now; |
487 |
|
|
|
488 |
|
|
if (fabs (odiff - diff) < MIN_TIMEJUMP) |
489 |
|
|
return; /* all is well */ |
490 |
|
|
|
491 |
|
|
ev_now = ev_time (); |
492 |
|
|
} |
493 |
|
|
|
494 |
root |
1.13 |
periodics_reschedule (diff - odiff); |
495 |
|
|
/* no timer adjustment, as the monotonic clock doesn't jump */ |
496 |
root |
1.4 |
} |
497 |
|
|
else |
498 |
|
|
{ |
499 |
|
|
if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
500 |
root |
1.13 |
{ |
501 |
|
|
periodics_reschedule (ev_now - now); |
502 |
|
|
|
503 |
|
|
/* adjust timers. this is easy, as the offset is the same for all */ |
504 |
|
|
for (i = 0; i < timercnt; ++i) |
505 |
|
|
timers [i]->at += diff; |
506 |
|
|
} |
507 |
root |
1.4 |
|
508 |
|
|
now = ev_now; |
509 |
|
|
} |
510 |
|
|
} |
511 |
|
|
|
512 |
root |
1.1 |
int ev_loop_done; |
513 |
|
|
|
514 |
root |
1.4 |
void ev_loop (int flags) |
515 |
root |
1.1 |
{ |
516 |
|
|
double block; |
517 |
root |
1.13 |
ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
518 |
root |
1.1 |
|
519 |
root |
1.9 |
if (checkcnt) |
520 |
|
|
{ |
521 |
root |
1.10 |
queue_events ((W *)checks, checkcnt, EV_CHECK); |
522 |
root |
1.9 |
call_pending (); |
523 |
|
|
} |
524 |
|
|
|
525 |
root |
1.1 |
do |
526 |
|
|
{ |
527 |
|
|
/* update fd-related kernel structures */ |
528 |
root |
1.5 |
fd_reify (); |
529 |
root |
1.1 |
|
530 |
|
|
/* calculate blocking time */ |
531 |
root |
1.12 |
|
532 |
|
|
/* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
533 |
|
|
ev_now = ev_time (); |
534 |
|
|
|
535 |
root |
1.9 |
if (flags & EVLOOP_NONBLOCK || idlecnt) |
536 |
root |
1.1 |
block = 0.; |
537 |
|
|
else |
538 |
|
|
{ |
539 |
root |
1.4 |
block = MAX_BLOCKTIME; |
540 |
|
|
|
541 |
root |
1.12 |
if (timercnt) |
542 |
root |
1.4 |
{ |
543 |
root |
1.14 |
ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
544 |
root |
1.4 |
if (block > to) block = to; |
545 |
|
|
} |
546 |
|
|
|
547 |
root |
1.12 |
if (periodiccnt) |
548 |
root |
1.4 |
{ |
549 |
root |
1.12 |
ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
550 |
root |
1.4 |
if (block > to) block = to; |
551 |
|
|
} |
552 |
|
|
|
553 |
root |
1.1 |
if (block < 0.) block = 0.; |
554 |
|
|
} |
555 |
|
|
|
556 |
|
|
method_poll (block); |
557 |
|
|
|
558 |
root |
1.4 |
/* update ev_now, do magic */ |
559 |
|
|
time_update (); |
560 |
|
|
|
561 |
root |
1.9 |
/* queue pending timers and reschedule them */ |
562 |
root |
1.12 |
periodics_reify (); /* absolute timers first */ |
563 |
|
|
timers_reify (); /* relative timers second */ |
564 |
root |
1.1 |
|
565 |
root |
1.9 |
/* queue idle watchers unless io or timers are pending */ |
566 |
|
|
if (!pendingcnt) |
567 |
root |
1.10 |
queue_events ((W *)idles, idlecnt, EV_IDLE); |
568 |
root |
1.9 |
|
569 |
|
|
/* queue check and possibly idle watchers */ |
570 |
root |
1.10 |
queue_events ((W *)checks, checkcnt, EV_CHECK); |
571 |
root |
1.9 |
|
572 |
root |
1.1 |
call_pending (); |
573 |
|
|
} |
574 |
|
|
while (!ev_loop_done); |
575 |
root |
1.13 |
|
576 |
|
|
if (ev_loop_done != 2) |
577 |
|
|
ev_loop_done = 0; |
578 |
root |
1.1 |
} |
579 |
|
|
|
580 |
root |
1.8 |
/*****************************************************************************/ |
581 |
|
|
|
582 |
root |
1.1 |
static void |
583 |
root |
1.10 |
wlist_add (WL *head, WL elem) |
584 |
root |
1.1 |
{ |
585 |
|
|
elem->next = *head; |
586 |
|
|
*head = elem; |
587 |
|
|
} |
588 |
|
|
|
589 |
|
|
static void |
590 |
root |
1.10 |
wlist_del (WL *head, WL elem) |
591 |
root |
1.1 |
{ |
592 |
|
|
while (*head) |
593 |
|
|
{ |
594 |
|
|
if (*head == elem) |
595 |
|
|
{ |
596 |
|
|
*head = elem->next; |
597 |
|
|
return; |
598 |
|
|
} |
599 |
|
|
|
600 |
|
|
head = &(*head)->next; |
601 |
|
|
} |
602 |
|
|
} |
603 |
|
|
|
604 |
|
|
static void |
605 |
root |
1.10 |
ev_start (W w, int active) |
606 |
root |
1.1 |
{ |
607 |
|
|
w->pending = 0; |
608 |
|
|
w->active = active; |
609 |
|
|
} |
610 |
|
|
|
611 |
|
|
static void |
612 |
root |
1.10 |
ev_stop (W w) |
613 |
root |
1.1 |
{ |
614 |
|
|
if (w->pending) |
615 |
|
|
pendings [w->pending - 1].w = 0; |
616 |
|
|
|
617 |
|
|
w->active = 0; |
618 |
|
|
} |
619 |
|
|
|
620 |
root |
1.8 |
/*****************************************************************************/ |
621 |
|
|
|
622 |
root |
1.1 |
void |
623 |
|
|
evio_start (struct ev_io *w) |
624 |
|
|
{ |
625 |
|
|
if (ev_is_active (w)) |
626 |
|
|
return; |
627 |
|
|
|
628 |
|
|
int fd = w->fd; |
629 |
|
|
|
630 |
root |
1.10 |
ev_start ((W)w, 1); |
631 |
root |
1.1 |
array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
632 |
root |
1.10 |
wlist_add ((WL *)&anfds[fd].head, (WL)w); |
633 |
root |
1.1 |
|
634 |
|
|
++fdchangecnt; |
635 |
|
|
array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
636 |
|
|
fdchanges [fdchangecnt - 1] = fd; |
637 |
|
|
} |
638 |
|
|
|
639 |
|
|
void |
640 |
|
|
evio_stop (struct ev_io *w) |
641 |
|
|
{ |
642 |
|
|
if (!ev_is_active (w)) |
643 |
|
|
return; |
644 |
|
|
|
645 |
root |
1.10 |
wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
646 |
|
|
ev_stop ((W)w); |
647 |
root |
1.1 |
|
648 |
|
|
++fdchangecnt; |
649 |
|
|
array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
650 |
|
|
fdchanges [fdchangecnt - 1] = w->fd; |
651 |
|
|
} |
652 |
|
|
|
653 |
root |
1.12 |
|
654 |
root |
1.1 |
void |
655 |
|
|
evtimer_start (struct ev_timer *w) |
656 |
|
|
{ |
657 |
|
|
if (ev_is_active (w)) |
658 |
|
|
return; |
659 |
|
|
|
660 |
root |
1.12 |
w->at += now; |
661 |
|
|
|
662 |
root |
1.13 |
assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
663 |
|
|
|
664 |
root |
1.12 |
ev_start ((W)w, ++timercnt); |
665 |
|
|
array_needsize (timers, timermax, timercnt, ); |
666 |
|
|
timers [timercnt - 1] = w; |
667 |
|
|
upheap ((WT *)timers, timercnt - 1); |
668 |
|
|
} |
669 |
|
|
|
670 |
|
|
void |
671 |
|
|
evtimer_stop (struct ev_timer *w) |
672 |
|
|
{ |
673 |
|
|
if (!ev_is_active (w)) |
674 |
|
|
return; |
675 |
|
|
|
676 |
|
|
if (w->active < timercnt--) |
677 |
root |
1.1 |
{ |
678 |
root |
1.12 |
timers [w->active - 1] = timers [timercnt]; |
679 |
|
|
downheap ((WT *)timers, timercnt, w->active - 1); |
680 |
|
|
} |
681 |
root |
1.4 |
|
682 |
root |
1.14 |
w->at = w->repeat; |
683 |
|
|
|
684 |
root |
1.12 |
ev_stop ((W)w); |
685 |
|
|
} |
686 |
root |
1.4 |
|
687 |
root |
1.12 |
void |
688 |
root |
1.14 |
evtimer_again (struct ev_timer *w) |
689 |
|
|
{ |
690 |
|
|
if (ev_is_active (w)) |
691 |
|
|
{ |
692 |
|
|
if (w->repeat) |
693 |
|
|
{ |
694 |
|
|
w->at = now + w->repeat; |
695 |
|
|
downheap ((WT *)timers, timercnt, w->active - 1); |
696 |
|
|
} |
697 |
|
|
else |
698 |
|
|
evtimer_stop (w); |
699 |
|
|
} |
700 |
|
|
else if (w->repeat) |
701 |
|
|
evtimer_start (w); |
702 |
|
|
} |
703 |
|
|
|
704 |
|
|
void |
705 |
root |
1.12 |
evperiodic_start (struct ev_periodic *w) |
706 |
|
|
{ |
707 |
|
|
if (ev_is_active (w)) |
708 |
|
|
return; |
709 |
root |
1.1 |
|
710 |
root |
1.13 |
assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
711 |
|
|
|
712 |
root |
1.12 |
/* this formula differs from the one in periodic_reify because we do not always round up */ |
713 |
|
|
if (w->interval) |
714 |
|
|
w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
715 |
|
|
|
716 |
|
|
ev_start ((W)w, ++periodiccnt); |
717 |
|
|
array_needsize (periodics, periodicmax, periodiccnt, ); |
718 |
|
|
periodics [periodiccnt - 1] = w; |
719 |
|
|
upheap ((WT *)periodics, periodiccnt - 1); |
720 |
root |
1.1 |
} |
721 |
|
|
|
722 |
|
|
void |
723 |
root |
1.12 |
evperiodic_stop (struct ev_periodic *w) |
724 |
root |
1.1 |
{ |
725 |
|
|
if (!ev_is_active (w)) |
726 |
|
|
return; |
727 |
|
|
|
728 |
root |
1.12 |
if (w->active < periodiccnt--) |
729 |
root |
1.2 |
{ |
730 |
root |
1.12 |
periodics [w->active - 1] = periodics [periodiccnt]; |
731 |
|
|
downheap ((WT *)periodics, periodiccnt, w->active - 1); |
732 |
root |
1.2 |
} |
733 |
|
|
|
734 |
root |
1.10 |
ev_stop ((W)w); |
735 |
root |
1.1 |
} |
736 |
|
|
|
737 |
|
|
void |
738 |
|
|
evsignal_start (struct ev_signal *w) |
739 |
|
|
{ |
740 |
|
|
if (ev_is_active (w)) |
741 |
|
|
return; |
742 |
|
|
|
743 |
root |
1.10 |
ev_start ((W)w, 1); |
744 |
root |
1.1 |
array_needsize (signals, signalmax, w->signum, signals_init); |
745 |
root |
1.10 |
wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
746 |
root |
1.7 |
|
747 |
|
|
if (!w->next) |
748 |
|
|
{ |
749 |
|
|
struct sigaction sa; |
750 |
|
|
sa.sa_handler = sighandler; |
751 |
|
|
sigfillset (&sa.sa_mask); |
752 |
|
|
sa.sa_flags = 0; |
753 |
|
|
sigaction (w->signum, &sa, 0); |
754 |
|
|
} |
755 |
root |
1.1 |
} |
756 |
|
|
|
757 |
|
|
void |
758 |
|
|
evsignal_stop (struct ev_signal *w) |
759 |
|
|
{ |
760 |
|
|
if (!ev_is_active (w)) |
761 |
|
|
return; |
762 |
|
|
|
763 |
root |
1.10 |
wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
764 |
|
|
ev_stop ((W)w); |
765 |
root |
1.7 |
|
766 |
|
|
if (!signals [w->signum - 1].head) |
767 |
|
|
signal (w->signum, SIG_DFL); |
768 |
root |
1.1 |
} |
769 |
|
|
|
770 |
root |
1.9 |
void evidle_start (struct ev_idle *w) |
771 |
|
|
{ |
772 |
|
|
if (ev_is_active (w)) |
773 |
|
|
return; |
774 |
|
|
|
775 |
root |
1.10 |
ev_start ((W)w, ++idlecnt); |
776 |
root |
1.9 |
array_needsize (idles, idlemax, idlecnt, ); |
777 |
|
|
idles [idlecnt - 1] = w; |
778 |
|
|
} |
779 |
|
|
|
780 |
|
|
void evidle_stop (struct ev_idle *w) |
781 |
|
|
{ |
782 |
|
|
idles [w->active - 1] = idles [--idlecnt]; |
783 |
root |
1.10 |
ev_stop ((W)w); |
784 |
root |
1.9 |
} |
785 |
|
|
|
786 |
|
|
void evcheck_start (struct ev_check *w) |
787 |
|
|
{ |
788 |
|
|
if (ev_is_active (w)) |
789 |
|
|
return; |
790 |
|
|
|
791 |
root |
1.10 |
ev_start ((W)w, ++checkcnt); |
792 |
root |
1.9 |
array_needsize (checks, checkmax, checkcnt, ); |
793 |
|
|
checks [checkcnt - 1] = w; |
794 |
|
|
} |
795 |
|
|
|
796 |
|
|
void evcheck_stop (struct ev_check *w) |
797 |
|
|
{ |
798 |
|
|
checks [w->active - 1] = checks [--checkcnt]; |
799 |
root |
1.10 |
ev_stop ((W)w); |
800 |
root |
1.9 |
} |
801 |
|
|
|
802 |
root |
1.1 |
/*****************************************************************************/ |
803 |
root |
1.10 |
|
804 |
root |
1.13 |
#if 0 |
805 |
root |
1.12 |
|
806 |
|
|
struct ev_io wio; |
807 |
root |
1.1 |
|
808 |
|
|
static void |
809 |
|
|
sin_cb (struct ev_io *w, int revents) |
810 |
|
|
{ |
811 |
|
|
fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
812 |
|
|
} |
813 |
|
|
|
814 |
|
|
static void |
815 |
|
|
ocb (struct ev_timer *w, int revents) |
816 |
|
|
{ |
817 |
root |
1.4 |
//fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
818 |
|
|
evtimer_stop (w); |
819 |
|
|
evtimer_start (w); |
820 |
root |
1.1 |
} |
821 |
|
|
|
822 |
root |
1.7 |
static void |
823 |
|
|
scb (struct ev_signal *w, int revents) |
824 |
|
|
{ |
825 |
|
|
fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
826 |
root |
1.12 |
evio_stop (&wio); |
827 |
|
|
evio_start (&wio); |
828 |
root |
1.7 |
} |
829 |
|
|
|
830 |
root |
1.9 |
static void |
831 |
|
|
gcb (struct ev_signal *w, int revents) |
832 |
|
|
{ |
833 |
|
|
fprintf (stderr, "generic %x\n", revents); |
834 |
root |
1.12 |
|
835 |
root |
1.9 |
} |
836 |
|
|
|
837 |
root |
1.1 |
int main (void) |
838 |
|
|
{ |
839 |
|
|
ev_init (0); |
840 |
|
|
|
841 |
root |
1.12 |
evio_init (&wio, sin_cb, 0, EV_READ); |
842 |
|
|
evio_start (&wio); |
843 |
root |
1.1 |
|
844 |
root |
1.4 |
struct ev_timer t[10000]; |
845 |
root |
1.2 |
|
846 |
root |
1.9 |
#if 0 |
847 |
root |
1.2 |
int i; |
848 |
root |
1.4 |
for (i = 0; i < 10000; ++i) |
849 |
root |
1.2 |
{ |
850 |
|
|
struct ev_timer *w = t + i; |
851 |
|
|
evw_init (w, ocb, i); |
852 |
root |
1.12 |
evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
853 |
root |
1.2 |
evtimer_start (w); |
854 |
|
|
if (drand48 () < 0.5) |
855 |
|
|
evtimer_stop (w); |
856 |
|
|
} |
857 |
root |
1.4 |
#endif |
858 |
|
|
|
859 |
|
|
struct ev_timer t1; |
860 |
root |
1.12 |
evtimer_init (&t1, ocb, 5, 10); |
861 |
root |
1.4 |
evtimer_start (&t1); |
862 |
root |
1.1 |
|
863 |
root |
1.7 |
struct ev_signal sig; |
864 |
root |
1.12 |
evsignal_init (&sig, scb, SIGQUIT); |
865 |
root |
1.7 |
evsignal_start (&sig); |
866 |
|
|
|
867 |
root |
1.9 |
struct ev_check cw; |
868 |
root |
1.12 |
evcheck_init (&cw, gcb); |
869 |
root |
1.9 |
evcheck_start (&cw); |
870 |
|
|
|
871 |
|
|
struct ev_idle iw; |
872 |
root |
1.12 |
evidle_init (&iw, gcb); |
873 |
root |
1.9 |
evidle_start (&iw); |
874 |
|
|
|
875 |
root |
1.1 |
ev_loop (0); |
876 |
|
|
|
877 |
|
|
return 0; |
878 |
|
|
} |
879 |
|
|
|
880 |
|
|
#endif |
881 |
|
|
|
882 |
|
|
|
883 |
|
|
|
884 |
|
|
|