1 | #include <math.h> |
1 | #include <math.h> |
2 | #include <stdlib.h> |
2 | #include <stdlib.h> |
3 | #include <unistd.h> |
3 | #include <unistd.h> |
4 | #include <fcntl.h> |
4 | #include <fcntl.h> |
5 | #include <signal.h> |
5 | #include <signal.h> |
|
|
6 | #include <stddef.h> |
6 | |
7 | |
7 | #include <stdio.h> |
8 | #include <stdio.h> |
8 | |
9 | |
9 | #include <assert.h> |
10 | #include <assert.h> |
10 | #include <errno.h> |
11 | #include <errno.h> |
11 | #include <sys/time.h> |
12 | #include <sys/time.h> |
12 | #include <time.h> |
13 | #include <time.h> |
13 | |
14 | |
14 | #ifdef CLOCK_MONOTONIC |
|
|
15 | # define HAVE_MONOTONIC 1 |
|
|
16 | #endif |
|
|
17 | |
|
|
18 | #define HAVE_REALTIME 1 |
|
|
19 | #define HAVE_EPOLL 1 |
15 | #define HAVE_EPOLL 1 |
|
|
16 | |
|
|
17 | #ifndef HAVE_MONOTONIC |
|
|
18 | # ifdef CLOCK_MONOTONIC |
|
|
19 | # define HAVE_MONOTONIC 1 |
|
|
20 | # endif |
|
|
21 | #endif |
|
|
22 | |
|
|
23 | #ifndef HAVE_SELECT |
20 | #define HAVE_SELECT 1 |
24 | # define HAVE_SELECT 1 |
|
|
25 | #endif |
|
|
26 | |
|
|
27 | #ifndef HAVE_EPOLL |
|
|
28 | # define HAVE_EPOLL 0 |
|
|
29 | #endif |
|
|
30 | |
|
|
31 | #ifndef HAVE_REALTIME |
|
|
32 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
|
|
33 | #endif |
21 | |
34 | |
22 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
35 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
23 | #define MAX_BLOCKTIME 60. |
36 | #define MAX_BLOCKTIME 60. |
24 | |
37 | |
25 | #include "ev.h" |
38 | #include "ev.h" |
26 | |
39 | |
27 | struct ev_watcher { |
40 | typedef struct ev_watcher *W; |
28 | EV_WATCHER (ev_watcher); |
|
|
29 | }; |
|
|
30 | |
|
|
31 | struct ev_watcher_list { |
41 | typedef struct ev_watcher_list *WL; |
32 | EV_WATCHER_LIST (ev_watcher_list); |
42 | typedef struct ev_watcher_time *WT; |
33 | }; |
|
|
34 | |
43 | |
35 | static ev_tstamp now, diff; /* monotonic clock */ |
44 | static ev_tstamp now, diff; /* monotonic clock */ |
36 | ev_tstamp ev_now; |
45 | ev_tstamp ev_now; |
37 | int ev_method; |
46 | int ev_method; |
38 | |
47 | |
… | |
… | |
75 | |
84 | |
76 | #define array_needsize(base,cur,cnt,init) \ |
85 | #define array_needsize(base,cur,cnt,init) \ |
77 | if ((cnt) > cur) \ |
86 | if ((cnt) > cur) \ |
78 | { \ |
87 | { \ |
79 | int newcnt = cur ? cur << 1 : 16; \ |
88 | int newcnt = cur ? cur << 1 : 16; \ |
80 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
|
|
81 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
82 | init (base + cur, newcnt - cur); \ |
90 | init (base + cur, newcnt - cur); \ |
83 | cur = newcnt; \ |
91 | cur = newcnt; \ |
84 | } |
92 | } |
85 | |
93 | |
… | |
… | |
108 | } |
116 | } |
109 | } |
117 | } |
110 | |
118 | |
111 | typedef struct |
119 | typedef struct |
112 | { |
120 | { |
113 | struct ev_watcher *w; |
121 | W w; |
114 | int events; |
122 | int events; |
115 | } ANPENDING; |
123 | } ANPENDING; |
116 | |
124 | |
117 | static ANPENDING *pendings; |
125 | static ANPENDING *pendings; |
118 | static int pendingmax, pendingcnt; |
126 | static int pendingmax, pendingcnt; |
119 | |
127 | |
120 | static void |
128 | static void |
121 | event (struct ev_watcher *w, int events) |
129 | event (W w, int events) |
122 | { |
130 | { |
|
|
131 | if (w->active) |
|
|
132 | { |
123 | w->pending = ++pendingcnt; |
133 | w->pending = ++pendingcnt; |
124 | array_needsize (pendings, pendingmax, pendingcnt, ); |
134 | array_needsize (pendings, pendingmax, pendingcnt, ); |
125 | pendings [pendingcnt - 1].w = w; |
135 | pendings [pendingcnt - 1].w = w; |
126 | pendings [pendingcnt - 1].events = events; |
136 | pendings [pendingcnt - 1].events = events; |
|
|
137 | } |
127 | } |
138 | } |
128 | |
139 | |
129 | static void |
140 | static void |
130 | fd_event (int fd, int events) |
141 | fd_event (int fd, int events) |
131 | { |
142 | { |
… | |
… | |
135 | for (w = anfd->head; w; w = w->next) |
146 | for (w = anfd->head; w; w = w->next) |
136 | { |
147 | { |
137 | int ev = w->events & events; |
148 | int ev = w->events & events; |
138 | |
149 | |
139 | if (ev) |
150 | if (ev) |
140 | event ((struct ev_watcher *)w, ev); |
151 | event ((W)w, ev); |
141 | } |
152 | } |
142 | } |
153 | } |
143 | |
154 | |
144 | /*****************************************************************************/ |
155 | static void |
|
|
156 | queue_events (W *events, int eventcnt, int type) |
|
|
157 | { |
|
|
158 | int i; |
145 | |
159 | |
|
|
160 | for (i = 0; i < eventcnt; ++i) |
|
|
161 | event (events [i], type); |
|
|
162 | } |
|
|
163 | |
|
|
164 | /*****************************************************************************/ |
|
|
165 | |
146 | static struct ev_timer **atimers; |
166 | static struct ev_timer **timers; |
147 | static int atimermax, atimercnt; |
167 | static int timermax, timercnt; |
148 | |
168 | |
149 | static struct ev_timer **rtimers; |
169 | static struct ev_periodic **periodics; |
150 | static int rtimermax, rtimercnt; |
170 | static int periodicmax, periodiccnt; |
151 | |
171 | |
152 | static void |
172 | static void |
153 | upheap (struct ev_timer **timers, int k) |
173 | upheap (WT *timers, int k) |
154 | { |
174 | { |
155 | struct ev_timer *w = timers [k]; |
175 | WT w = timers [k]; |
156 | |
176 | |
157 | while (k && timers [k >> 1]->at > w->at) |
177 | while (k && timers [k >> 1]->at > w->at) |
158 | { |
178 | { |
159 | timers [k] = timers [k >> 1]; |
179 | timers [k] = timers [k >> 1]; |
160 | timers [k]->active = k + 1; |
180 | timers [k]->active = k + 1; |
… | |
… | |
165 | timers [k]->active = k + 1; |
185 | timers [k]->active = k + 1; |
166 | |
186 | |
167 | } |
187 | } |
168 | |
188 | |
169 | static void |
189 | static void |
170 | downheap (struct ev_timer **timers, int N, int k) |
190 | downheap (WT *timers, int N, int k) |
171 | { |
191 | { |
172 | struct ev_timer *w = timers [k]; |
192 | WT w = timers [k]; |
173 | |
193 | |
174 | while (k < (N >> 1)) |
194 | while (k < (N >> 1)) |
175 | { |
195 | { |
176 | int j = k << 1; |
196 | int j = k << 1; |
177 | |
197 | |
… | |
… | |
241 | if (signals [sig].gotsig) |
261 | if (signals [sig].gotsig) |
242 | { |
262 | { |
243 | signals [sig].gotsig = 0; |
263 | signals [sig].gotsig = 0; |
244 | |
264 | |
245 | for (w = signals [sig].head; w; w = w->next) |
265 | for (w = signals [sig].head; w; w = w->next) |
246 | event ((struct ev_watcher *)w, EV_SIGNAL); |
266 | event ((W)w, EV_SIGNAL); |
247 | } |
267 | } |
248 | } |
268 | } |
249 | |
269 | |
250 | static void |
270 | static void |
251 | siginit (void) |
271 | siginit (void) |
… | |
… | |
258 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
278 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
259 | |
279 | |
260 | evio_set (&sigev, sigpipe [0], EV_READ); |
280 | evio_set (&sigev, sigpipe [0], EV_READ); |
261 | evio_start (&sigev); |
281 | evio_start (&sigev); |
262 | } |
282 | } |
|
|
283 | |
|
|
284 | /*****************************************************************************/ |
|
|
285 | |
|
|
286 | static struct ev_idle **idles; |
|
|
287 | static int idlemax, idlecnt; |
|
|
288 | |
|
|
289 | static struct ev_check **checks; |
|
|
290 | static int checkmax, checkcnt; |
263 | |
291 | |
264 | /*****************************************************************************/ |
292 | /*****************************************************************************/ |
265 | |
293 | |
266 | #if HAVE_EPOLL |
294 | #if HAVE_EPOLL |
267 | # include "ev_epoll.c" |
295 | # include "ev_epoll.c" |
… | |
… | |
295 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
323 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
296 | #endif |
324 | #endif |
297 | |
325 | |
298 | if (ev_method) |
326 | if (ev_method) |
299 | { |
327 | { |
300 | evw_init (&sigev, sigcb, 0); |
328 | evw_init (&sigev, sigcb); |
301 | siginit (); |
329 | siginit (); |
302 | } |
330 | } |
303 | |
331 | |
304 | return ev_method; |
332 | return ev_method; |
305 | } |
333 | } |
306 | |
334 | |
307 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
308 | |
336 | |
309 | void ev_prefork (void) |
337 | void ev_prefork (void) |
310 | { |
338 | { |
|
|
339 | /* nop */ |
311 | } |
340 | } |
312 | |
341 | |
313 | void ev_postfork_parent (void) |
342 | void ev_postfork_parent (void) |
314 | { |
343 | { |
|
|
344 | /* nop */ |
315 | } |
345 | } |
316 | |
346 | |
317 | void ev_postfork_child (void) |
347 | void ev_postfork_child (void) |
318 | { |
348 | { |
319 | #if HAVE_EPOLL |
349 | #if HAVE_EPOLL |
… | |
… | |
328 | siginit (); |
358 | siginit (); |
329 | } |
359 | } |
330 | |
360 | |
331 | /*****************************************************************************/ |
361 | /*****************************************************************************/ |
332 | |
362 | |
333 | static ev_hook hooks [EVHOOK_NUM]; |
|
|
334 | |
|
|
335 | void |
|
|
336 | ev_hook_register (int type, ev_hook hook) |
|
|
337 | { |
|
|
338 | hooks [type] = hook; |
|
|
339 | } |
|
|
340 | |
|
|
341 | void |
|
|
342 | ev_hook_unregister (int type, ev_hook hook) |
|
|
343 | { |
|
|
344 | hooks [type] = 0; |
|
|
345 | } |
|
|
346 | |
|
|
347 | static void |
|
|
348 | hook_call (int type) |
|
|
349 | { |
|
|
350 | if (hooks [type]) |
|
|
351 | hooks [type] (); |
|
|
352 | } |
|
|
353 | |
|
|
354 | static void |
363 | static void |
355 | fd_reify (void) |
364 | fd_reify (void) |
356 | { |
365 | { |
357 | int i; |
366 | int i; |
358 | |
367 | |
… | |
… | |
395 | |
404 | |
396 | pendingcnt = 0; |
405 | pendingcnt = 0; |
397 | } |
406 | } |
398 | |
407 | |
399 | static void |
408 | static void |
400 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
409 | timers_reify () |
401 | { |
410 | { |
402 | while (timercnt && timers [0]->at <= now) |
411 | while (timercnt && timers [0]->at <= now) |
403 | { |
412 | { |
404 | struct ev_timer *w = timers [0]; |
413 | struct ev_timer *w = timers [0]; |
|
|
414 | |
|
|
415 | event ((W)w, EV_TIMEOUT); |
405 | |
416 | |
406 | /* first reschedule or stop timer */ |
417 | /* first reschedule or stop timer */ |
407 | if (w->repeat) |
418 | if (w->repeat) |
408 | { |
419 | { |
409 | if (w->is_abs) |
|
|
410 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
411 | else |
|
|
412 | w->at = now + w->repeat; |
420 | w->at = now + w->repeat; |
413 | |
421 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
414 | assert (w->at > now); |
|
|
415 | |
|
|
416 | downheap (timers, timercnt, 0); |
422 | downheap ((WT *)timers, timercnt, 0); |
417 | } |
423 | } |
418 | else |
424 | else |
419 | { |
|
|
420 | evtimer_stop (w); /* nonrepeating: stop timer */ |
425 | evtimer_stop (w); /* nonrepeating: stop timer */ |
421 | --timercnt; /* maybe pass by reference instead? */ |
426 | } |
|
|
427 | } |
|
|
428 | |
|
|
429 | static void |
|
|
430 | periodics_reify () |
|
|
431 | { |
|
|
432 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
433 | { |
|
|
434 | struct ev_periodic *w = periodics [0]; |
|
|
435 | |
|
|
436 | /* first reschedule or stop timer */ |
|
|
437 | if (w->interval) |
422 | } |
438 | { |
|
|
439 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
440 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
441 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
442 | } |
|
|
443 | else |
|
|
444 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
423 | |
445 | |
424 | event ((struct ev_watcher *)w, EV_TIMEOUT); |
446 | event ((W)w, EV_TIMEOUT); |
|
|
447 | } |
|
|
448 | } |
|
|
449 | |
|
|
450 | static void |
|
|
451 | periodics_reschedule (ev_tstamp diff) |
|
|
452 | { |
|
|
453 | int i; |
|
|
454 | |
|
|
455 | /* adjust periodics after time jump */ |
|
|
456 | for (i = 0; i < periodiccnt; ++i) |
|
|
457 | { |
|
|
458 | struct ev_periodic *w = periodics [i]; |
|
|
459 | |
|
|
460 | if (w->interval) |
|
|
461 | { |
|
|
462 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
463 | |
|
|
464 | if (fabs (diff) >= 1e-4) |
|
|
465 | { |
|
|
466 | evperiodic_stop (w); |
|
|
467 | evperiodic_start (w); |
|
|
468 | |
|
|
469 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
470 | } |
|
|
471 | } |
425 | } |
472 | } |
426 | } |
473 | } |
427 | |
474 | |
428 | static void |
475 | static void |
429 | time_update () |
476 | time_update () |
430 | { |
477 | { |
431 | int i; |
478 | int i; |
|
|
479 | |
432 | ev_now = ev_time (); |
480 | ev_now = ev_time (); |
433 | |
481 | |
434 | if (have_monotonic) |
482 | if (have_monotonic) |
435 | { |
483 | { |
436 | ev_tstamp odiff = diff; |
484 | ev_tstamp odiff = diff; |
437 | |
485 | |
438 | /* detecting time jumps is much more difficult */ |
|
|
439 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
486 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
440 | { |
487 | { |
441 | now = get_clock (); |
488 | now = get_clock (); |
442 | diff = ev_now - now; |
489 | diff = ev_now - now; |
443 | |
490 | |
444 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
491 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
445 | return; /* all is well */ |
492 | return; /* all is well */ |
446 | |
493 | |
447 | ev_now = ev_time (); |
494 | ev_now = ev_time (); |
448 | } |
495 | } |
449 | |
496 | |
450 | /* time jump detected, reschedule atimers */ |
497 | periodics_reschedule (diff - odiff); |
451 | for (i = 0; i < atimercnt; ++i) |
498 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
452 | { |
|
|
453 | struct ev_timer *w = atimers [i]; |
|
|
454 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
455 | } |
|
|
456 | } |
499 | } |
457 | else |
500 | else |
458 | { |
501 | { |
459 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
502 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
460 | /* time jump detected, adjust rtimers */ |
503 | { |
|
|
504 | periodics_reschedule (ev_now - now); |
|
|
505 | |
|
|
506 | /* adjust timers. this is easy, as the offset is the same for all */ |
461 | for (i = 0; i < rtimercnt; ++i) |
507 | for (i = 0; i < timercnt; ++i) |
462 | rtimers [i]->at += ev_now - now; |
508 | timers [i]->at += diff; |
|
|
509 | } |
463 | |
510 | |
464 | now = ev_now; |
511 | now = ev_now; |
465 | } |
512 | } |
466 | } |
513 | } |
467 | |
514 | |
468 | int ev_loop_done; |
515 | int ev_loop_done; |
469 | |
516 | |
470 | void ev_loop (int flags) |
517 | void ev_loop (int flags) |
471 | { |
518 | { |
472 | double block; |
519 | double block; |
473 | ev_loop_done = flags & EVLOOP_ONESHOT; |
520 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
|
|
521 | |
|
|
522 | if (checkcnt) |
|
|
523 | { |
|
|
524 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
525 | call_pending (); |
|
|
526 | } |
474 | |
527 | |
475 | do |
528 | do |
476 | { |
529 | { |
477 | hook_call (EVHOOK_PREPOLL); |
|
|
478 | |
|
|
479 | /* update fd-related kernel structures */ |
530 | /* update fd-related kernel structures */ |
480 | fd_reify (); |
531 | fd_reify (); |
481 | |
532 | |
482 | /* calculate blocking time */ |
533 | /* calculate blocking time */ |
|
|
534 | |
|
|
535 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
|
|
536 | ev_now = ev_time (); |
|
|
537 | |
483 | if (flags & EVLOOP_NONBLOCK) |
538 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
484 | block = 0.; |
539 | block = 0.; |
485 | else |
540 | else |
486 | { |
541 | { |
487 | block = MAX_BLOCKTIME; |
542 | block = MAX_BLOCKTIME; |
488 | |
543 | |
489 | if (rtimercnt) |
544 | if (timercnt) |
490 | { |
545 | { |
491 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
546 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
492 | if (block > to) block = to; |
547 | if (block > to) block = to; |
493 | } |
548 | } |
494 | |
549 | |
495 | if (atimercnt) |
550 | if (periodiccnt) |
496 | { |
551 | { |
497 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
552 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
498 | if (block > to) block = to; |
553 | if (block > to) block = to; |
499 | } |
554 | } |
500 | |
555 | |
501 | if (block < 0.) block = 0.; |
556 | if (block < 0.) block = 0.; |
502 | } |
557 | } |
… | |
… | |
504 | method_poll (block); |
559 | method_poll (block); |
505 | |
560 | |
506 | /* update ev_now, do magic */ |
561 | /* update ev_now, do magic */ |
507 | time_update (); |
562 | time_update (); |
508 | |
563 | |
509 | hook_call (EVHOOK_POSTPOLL); |
|
|
510 | |
|
|
511 | /* put pending timers into pendign queue and reschedule them */ |
564 | /* queue pending timers and reschedule them */ |
512 | /* absolute timers first */ |
565 | periodics_reify (); /* absolute timers first */ |
513 | timers_reify (atimers, atimercnt, ev_now); |
|
|
514 | /* relative timers second */ |
566 | timers_reify (); /* relative timers second */ |
515 | timers_reify (rtimers, rtimercnt, now); |
567 | |
|
|
568 | /* queue idle watchers unless io or timers are pending */ |
|
|
569 | if (!pendingcnt) |
|
|
570 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
|
|
571 | |
|
|
572 | /* queue check and possibly idle watchers */ |
|
|
573 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
516 | |
574 | |
517 | call_pending (); |
575 | call_pending (); |
518 | } |
576 | } |
519 | while (!ev_loop_done); |
577 | while (!ev_loop_done); |
520 | } |
|
|
521 | |
578 | |
522 | /*****************************************************************************/ |
579 | if (ev_loop_done != 2) |
|
|
580 | ev_loop_done = 0; |
|
|
581 | } |
523 | |
582 | |
|
|
583 | /*****************************************************************************/ |
|
|
584 | |
524 | static void |
585 | static void |
525 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
586 | wlist_add (WL *head, WL elem) |
526 | { |
587 | { |
527 | elem->next = *head; |
588 | elem->next = *head; |
528 | *head = elem; |
589 | *head = elem; |
529 | } |
590 | } |
530 | |
591 | |
531 | static void |
592 | static void |
532 | wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
593 | wlist_del (WL *head, WL elem) |
533 | { |
594 | { |
534 | while (*head) |
595 | while (*head) |
535 | { |
596 | { |
536 | if (*head == elem) |
597 | if (*head == elem) |
537 | { |
598 | { |
… | |
… | |
542 | head = &(*head)->next; |
603 | head = &(*head)->next; |
543 | } |
604 | } |
544 | } |
605 | } |
545 | |
606 | |
546 | static void |
607 | static void |
547 | ev_start (struct ev_watcher *w, int active) |
608 | ev_clear (W w) |
548 | { |
609 | { |
|
|
610 | if (w->pending) |
|
|
611 | { |
|
|
612 | pendings [w->pending - 1].w = 0; |
549 | w->pending = 0; |
613 | w->pending = 0; |
|
|
614 | } |
|
|
615 | } |
|
|
616 | |
|
|
617 | static void |
|
|
618 | ev_start (W w, int active) |
|
|
619 | { |
550 | w->active = active; |
620 | w->active = active; |
551 | } |
621 | } |
552 | |
622 | |
553 | static void |
623 | static void |
554 | ev_stop (struct ev_watcher *w) |
624 | ev_stop (W w) |
555 | { |
625 | { |
556 | if (w->pending) |
|
|
557 | pendings [w->pending - 1].w = 0; |
|
|
558 | |
|
|
559 | w->active = 0; |
626 | w->active = 0; |
560 | /* nop */ |
|
|
561 | } |
627 | } |
562 | |
628 | |
563 | /*****************************************************************************/ |
629 | /*****************************************************************************/ |
564 | |
630 | |
565 | void |
631 | void |
… | |
… | |
568 | if (ev_is_active (w)) |
634 | if (ev_is_active (w)) |
569 | return; |
635 | return; |
570 | |
636 | |
571 | int fd = w->fd; |
637 | int fd = w->fd; |
572 | |
638 | |
573 | ev_start ((struct ev_watcher *)w, 1); |
639 | ev_start ((W)w, 1); |
574 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
640 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
575 | wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); |
641 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
576 | |
642 | |
577 | ++fdchangecnt; |
643 | ++fdchangecnt; |
578 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
644 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
579 | fdchanges [fdchangecnt - 1] = fd; |
645 | fdchanges [fdchangecnt - 1] = fd; |
580 | } |
646 | } |
581 | |
647 | |
582 | void |
648 | void |
583 | evio_stop (struct ev_io *w) |
649 | evio_stop (struct ev_io *w) |
584 | { |
650 | { |
|
|
651 | ev_clear ((W)w); |
585 | if (!ev_is_active (w)) |
652 | if (!ev_is_active (w)) |
586 | return; |
653 | return; |
587 | |
654 | |
588 | wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); |
655 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
589 | ev_stop ((struct ev_watcher *)w); |
656 | ev_stop ((W)w); |
590 | |
657 | |
591 | ++fdchangecnt; |
658 | ++fdchangecnt; |
592 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
659 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
593 | fdchanges [fdchangecnt - 1] = w->fd; |
660 | fdchanges [fdchangecnt - 1] = w->fd; |
594 | } |
661 | } |
… | |
… | |
597 | evtimer_start (struct ev_timer *w) |
664 | evtimer_start (struct ev_timer *w) |
598 | { |
665 | { |
599 | if (ev_is_active (w)) |
666 | if (ev_is_active (w)) |
600 | return; |
667 | return; |
601 | |
668 | |
602 | if (w->is_abs) |
669 | w->at += now; |
603 | { |
|
|
604 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
605 | if (w->repeat) |
|
|
606 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
607 | |
670 | |
608 | ev_start ((struct ev_watcher *)w, ++atimercnt); |
671 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
672 | |
|
|
673 | ev_start ((W)w, ++timercnt); |
609 | array_needsize (atimers, atimermax, atimercnt, ); |
674 | array_needsize (timers, timermax, timercnt, ); |
610 | atimers [atimercnt - 1] = w; |
675 | timers [timercnt - 1] = w; |
611 | upheap (atimers, atimercnt - 1); |
676 | upheap ((WT *)timers, timercnt - 1); |
612 | } |
|
|
613 | else |
|
|
614 | { |
|
|
615 | w->at += now; |
|
|
616 | |
|
|
617 | ev_start ((struct ev_watcher *)w, ++rtimercnt); |
|
|
618 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
619 | rtimers [rtimercnt - 1] = w; |
|
|
620 | upheap (rtimers, rtimercnt - 1); |
|
|
621 | } |
|
|
622 | |
|
|
623 | } |
677 | } |
624 | |
678 | |
625 | void |
679 | void |
626 | evtimer_stop (struct ev_timer *w) |
680 | evtimer_stop (struct ev_timer *w) |
627 | { |
681 | { |
|
|
682 | ev_clear ((W)w); |
628 | if (!ev_is_active (w)) |
683 | if (!ev_is_active (w)) |
629 | return; |
684 | return; |
630 | |
685 | |
631 | if (w->is_abs) |
|
|
632 | { |
|
|
633 | if (w->active < atimercnt--) |
686 | if (w->active < timercnt--) |
634 | { |
687 | { |
635 | atimers [w->active - 1] = atimers [atimercnt]; |
688 | timers [w->active - 1] = timers [timercnt]; |
|
|
689 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
690 | } |
|
|
691 | |
|
|
692 | w->at = w->repeat; |
|
|
693 | |
|
|
694 | ev_stop ((W)w); |
|
|
695 | } |
|
|
696 | |
|
|
697 | void |
|
|
698 | evtimer_again (struct ev_timer *w) |
|
|
699 | { |
|
|
700 | if (ev_is_active (w)) |
|
|
701 | { |
|
|
702 | if (w->repeat) |
|
|
703 | { |
|
|
704 | w->at = now + w->repeat; |
636 | downheap (atimers, atimercnt, w->active - 1); |
705 | downheap ((WT *)timers, timercnt, w->active - 1); |
637 | } |
|
|
638 | } |
|
|
639 | else |
|
|
640 | { |
|
|
641 | if (w->active < rtimercnt--) |
|
|
642 | { |
706 | } |
643 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
707 | else |
644 | downheap (rtimers, rtimercnt, w->active - 1); |
708 | evtimer_stop (w); |
645 | } |
709 | } |
|
|
710 | else if (w->repeat) |
|
|
711 | evtimer_start (w); |
|
|
712 | } |
|
|
713 | |
|
|
714 | void |
|
|
715 | evperiodic_start (struct ev_periodic *w) |
|
|
716 | { |
|
|
717 | if (ev_is_active (w)) |
|
|
718 | return; |
|
|
719 | |
|
|
720 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
721 | |
|
|
722 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
723 | if (w->interval) |
|
|
724 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
725 | |
|
|
726 | ev_start ((W)w, ++periodiccnt); |
|
|
727 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
728 | periodics [periodiccnt - 1] = w; |
|
|
729 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
730 | } |
|
|
731 | |
|
|
732 | void |
|
|
733 | evperiodic_stop (struct ev_periodic *w) |
|
|
734 | { |
|
|
735 | ev_clear ((W)w); |
|
|
736 | if (!ev_is_active (w)) |
|
|
737 | return; |
|
|
738 | |
|
|
739 | if (w->active < periodiccnt--) |
646 | } |
740 | { |
|
|
741 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
742 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
|
|
743 | } |
647 | |
744 | |
648 | ev_stop ((struct ev_watcher *)w); |
745 | ev_stop ((W)w); |
649 | } |
746 | } |
650 | |
747 | |
651 | void |
748 | void |
652 | evsignal_start (struct ev_signal *w) |
749 | evsignal_start (struct ev_signal *w) |
653 | { |
750 | { |
654 | if (ev_is_active (w)) |
751 | if (ev_is_active (w)) |
655 | return; |
752 | return; |
656 | |
753 | |
657 | ev_start ((struct ev_watcher *)w, 1); |
754 | ev_start ((W)w, 1); |
658 | array_needsize (signals, signalmax, w->signum, signals_init); |
755 | array_needsize (signals, signalmax, w->signum, signals_init); |
659 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
756 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
660 | |
757 | |
661 | if (!w->next) |
758 | if (!w->next) |
662 | { |
759 | { |
663 | struct sigaction sa; |
760 | struct sigaction sa; |
664 | sa.sa_handler = sighandler; |
761 | sa.sa_handler = sighandler; |
… | |
… | |
669 | } |
766 | } |
670 | |
767 | |
671 | void |
768 | void |
672 | evsignal_stop (struct ev_signal *w) |
769 | evsignal_stop (struct ev_signal *w) |
673 | { |
770 | { |
|
|
771 | ev_clear ((W)w); |
674 | if (!ev_is_active (w)) |
772 | if (!ev_is_active (w)) |
675 | return; |
773 | return; |
676 | |
774 | |
677 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
775 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
678 | ev_stop ((struct ev_watcher *)w); |
776 | ev_stop ((W)w); |
679 | |
777 | |
680 | if (!signals [w->signum - 1].head) |
778 | if (!signals [w->signum - 1].head) |
681 | signal (w->signum, SIG_DFL); |
779 | signal (w->signum, SIG_DFL); |
682 | } |
780 | } |
683 | |
781 | |
|
|
782 | void evidle_start (struct ev_idle *w) |
|
|
783 | { |
|
|
784 | if (ev_is_active (w)) |
|
|
785 | return; |
|
|
786 | |
|
|
787 | ev_start ((W)w, ++idlecnt); |
|
|
788 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
789 | idles [idlecnt - 1] = w; |
|
|
790 | } |
|
|
791 | |
|
|
792 | void evidle_stop (struct ev_idle *w) |
|
|
793 | { |
|
|
794 | ev_clear ((W)w); |
|
|
795 | if (ev_is_active (w)) |
|
|
796 | return; |
|
|
797 | |
|
|
798 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
799 | ev_stop ((W)w); |
|
|
800 | } |
|
|
801 | |
|
|
802 | void evcheck_start (struct ev_check *w) |
|
|
803 | { |
|
|
804 | if (ev_is_active (w)) |
|
|
805 | return; |
|
|
806 | |
|
|
807 | ev_start ((W)w, ++checkcnt); |
|
|
808 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
809 | checks [checkcnt - 1] = w; |
|
|
810 | } |
|
|
811 | |
|
|
812 | void evcheck_stop (struct ev_check *w) |
|
|
813 | { |
|
|
814 | ev_clear ((W)w); |
|
|
815 | if (ev_is_active (w)) |
|
|
816 | return; |
|
|
817 | |
|
|
818 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
819 | ev_stop ((W)w); |
|
|
820 | } |
|
|
821 | |
684 | /*****************************************************************************/ |
822 | /*****************************************************************************/ |
|
|
823 | |
|
|
824 | struct ev_once |
|
|
825 | { |
|
|
826 | struct ev_io io; |
|
|
827 | struct ev_timer to; |
|
|
828 | void (*cb)(int revents, void *arg); |
|
|
829 | void *arg; |
|
|
830 | }; |
|
|
831 | |
|
|
832 | static void |
|
|
833 | once_cb (struct ev_once *once, int revents) |
|
|
834 | { |
|
|
835 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
836 | void *arg = once->arg; |
|
|
837 | |
|
|
838 | evio_stop (&once->io); |
|
|
839 | evtimer_stop (&once->to); |
|
|
840 | free (once); |
|
|
841 | |
|
|
842 | cb (revents, arg); |
|
|
843 | } |
|
|
844 | |
|
|
845 | static void |
|
|
846 | once_cb_io (struct ev_io *w, int revents) |
|
|
847 | { |
|
|
848 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
849 | } |
|
|
850 | |
|
|
851 | static void |
|
|
852 | once_cb_to (struct ev_timer *w, int revents) |
|
|
853 | { |
|
|
854 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
855 | } |
|
|
856 | |
|
|
857 | void |
|
|
858 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
859 | { |
|
|
860 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
861 | |
|
|
862 | if (!once) |
|
|
863 | cb (EV_ERROR, arg); |
|
|
864 | else |
|
|
865 | { |
|
|
866 | once->cb = cb; |
|
|
867 | once->arg = arg; |
|
|
868 | |
|
|
869 | evw_init (&once->io, once_cb_io); |
|
|
870 | |
|
|
871 | if (fd >= 0) |
|
|
872 | { |
|
|
873 | evio_set (&once->io, fd, events); |
|
|
874 | evio_start (&once->io); |
|
|
875 | } |
|
|
876 | |
|
|
877 | evw_init (&once->to, once_cb_to); |
|
|
878 | |
|
|
879 | if (timeout >= 0.) |
|
|
880 | { |
|
|
881 | evtimer_set (&once->to, timeout, 0.); |
|
|
882 | evtimer_start (&once->to); |
|
|
883 | } |
|
|
884 | } |
|
|
885 | } |
|
|
886 | |
|
|
887 | /*****************************************************************************/ |
|
|
888 | |
685 | #if 1 |
889 | #if 0 |
|
|
890 | |
|
|
891 | struct ev_io wio; |
686 | |
892 | |
687 | static void |
893 | static void |
688 | sin_cb (struct ev_io *w, int revents) |
894 | sin_cb (struct ev_io *w, int revents) |
689 | { |
895 | { |
690 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
896 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
700 | |
906 | |
701 | static void |
907 | static void |
702 | scb (struct ev_signal *w, int revents) |
908 | scb (struct ev_signal *w, int revents) |
703 | { |
909 | { |
704 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
910 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
911 | evio_stop (&wio); |
|
|
912 | evio_start (&wio); |
|
|
913 | } |
|
|
914 | |
|
|
915 | static void |
|
|
916 | gcb (struct ev_signal *w, int revents) |
|
|
917 | { |
|
|
918 | fprintf (stderr, "generic %x\n", revents); |
|
|
919 | |
705 | } |
920 | } |
706 | |
921 | |
707 | int main (void) |
922 | int main (void) |
708 | { |
923 | { |
709 | struct ev_io sin; |
|
|
710 | |
|
|
711 | ev_init (0); |
924 | ev_init (0); |
712 | |
925 | |
713 | evw_init (&sin, sin_cb, 55); |
|
|
714 | evio_set (&sin, 0, EV_READ); |
926 | evio_init (&wio, sin_cb, 0, EV_READ); |
715 | evio_start (&sin); |
927 | evio_start (&wio); |
716 | |
928 | |
717 | struct ev_timer t[10000]; |
929 | struct ev_timer t[10000]; |
718 | |
930 | |
719 | #if 1 |
931 | #if 0 |
720 | int i; |
932 | int i; |
721 | for (i = 0; i < 10000; ++i) |
933 | for (i = 0; i < 10000; ++i) |
722 | { |
934 | { |
723 | struct ev_timer *w = t + i; |
935 | struct ev_timer *w = t + i; |
724 | evw_init (w, ocb, i); |
936 | evw_init (w, ocb, i); |
725 | evtimer_set_abs (w, drand48 (), 0.99775533); |
937 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
726 | evtimer_start (w); |
938 | evtimer_start (w); |
727 | if (drand48 () < 0.5) |
939 | if (drand48 () < 0.5) |
728 | evtimer_stop (w); |
940 | evtimer_stop (w); |
729 | } |
941 | } |
730 | #endif |
942 | #endif |
731 | |
943 | |
732 | struct ev_timer t1; |
944 | struct ev_timer t1; |
733 | evw_init (&t1, ocb, 0); |
945 | evtimer_init (&t1, ocb, 5, 10); |
734 | evtimer_set_abs (&t1, 5, 10); |
|
|
735 | evtimer_start (&t1); |
946 | evtimer_start (&t1); |
736 | |
947 | |
737 | struct ev_signal sig; |
948 | struct ev_signal sig; |
738 | evw_init (&sig, scb, 65535); |
|
|
739 | evsignal_set (&sig, SIGQUIT); |
949 | evsignal_init (&sig, scb, SIGQUIT); |
740 | evsignal_start (&sig); |
950 | evsignal_start (&sig); |
741 | |
951 | |
|
|
952 | struct ev_check cw; |
|
|
953 | evcheck_init (&cw, gcb); |
|
|
954 | evcheck_start (&cw); |
|
|
955 | |
|
|
956 | struct ev_idle iw; |
|
|
957 | evidle_init (&iw, gcb); |
|
|
958 | evidle_start (&iw); |
|
|
959 | |
742 | ev_loop (0); |
960 | ev_loop (0); |
743 | |
961 | |
744 | return 0; |
962 | return 0; |
745 | } |
963 | } |
746 | |
964 | |