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