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