|
|
1 | /* |
|
|
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
3 | * All rights reserved. |
|
|
4 | * |
|
|
5 | * Redistribution and use in source and binary forms, with or without |
|
|
6 | * modification, are permitted provided that the following conditions are |
|
|
7 | * met: |
|
|
8 | * |
|
|
9 | * * Redistributions of source code must retain the above copyright |
|
|
10 | * notice, this list of conditions and the following disclaimer. |
|
|
11 | * |
|
|
12 | * * Redistributions in binary form must reproduce the above |
|
|
13 | * copyright notice, this list of conditions and the following |
|
|
14 | * disclaimer in the documentation and/or other materials provided |
|
|
15 | * with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
28 | */ |
|
|
29 | |
1 | #include <math.h> |
30 | #include <math.h> |
2 | #include <stdlib.h> |
31 | #include <stdlib.h> |
3 | #include <unistd.h> |
32 | #include <unistd.h> |
4 | #include <fcntl.h> |
33 | #include <fcntl.h> |
5 | #include <signal.h> |
34 | #include <signal.h> |
|
|
35 | #include <stddef.h> |
6 | |
36 | |
7 | #include <stdio.h> |
37 | #include <stdio.h> |
8 | |
38 | |
9 | #include <assert.h> |
39 | #include <assert.h> |
10 | #include <errno.h> |
40 | #include <errno.h> |
11 | #include <sys/time.h> |
41 | #include <sys/time.h> |
12 | #include <time.h> |
42 | #include <time.h> |
13 | |
43 | |
|
|
44 | #define HAVE_EPOLL 1 |
|
|
45 | |
14 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
15 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
16 | # define HAVE_MONOTONIC 1 |
48 | # define HAVE_MONOTONIC 1 |
17 | # endif |
49 | # endif |
18 | #endif |
50 | #endif |
… | |
… | |
32 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
33 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
34 | |
66 | |
35 | #include "ev.h" |
67 | #include "ev.h" |
36 | |
68 | |
37 | struct ev_watcher { |
|
|
38 | EV_WATCHER (ev_watcher); |
|
|
39 | }; |
|
|
40 | |
|
|
41 | struct ev_watcher_list { |
|
|
42 | EV_WATCHER_LIST (ev_watcher_list); |
|
|
43 | }; |
|
|
44 | |
|
|
45 | typedef struct ev_watcher *W; |
69 | typedef struct ev_watcher *W; |
46 | typedef struct ev_watcher_list *WL; |
70 | typedef struct ev_watcher_list *WL; |
|
|
71 | typedef struct ev_watcher_time *WT; |
47 | |
72 | |
48 | static ev_tstamp now, diff; /* monotonic clock */ |
73 | static ev_tstamp now, diff; /* monotonic clock */ |
49 | ev_tstamp ev_now; |
74 | ev_tstamp ev_now; |
50 | int ev_method; |
75 | int ev_method; |
51 | |
76 | |
… | |
… | |
88 | |
113 | |
89 | #define array_needsize(base,cur,cnt,init) \ |
114 | #define array_needsize(base,cur,cnt,init) \ |
90 | if ((cnt) > cur) \ |
115 | if ((cnt) > cur) \ |
91 | { \ |
116 | { \ |
92 | int newcnt = cur ? cur << 1 : 16; \ |
117 | int newcnt = cur ? cur << 1 : 16; \ |
93 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
|
|
94 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
118 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
95 | init (base + cur, newcnt - cur); \ |
119 | init (base + cur, newcnt - cur); \ |
96 | cur = newcnt; \ |
120 | cur = newcnt; \ |
97 | } |
121 | } |
98 | |
122 | |
… | |
… | |
131 | static int pendingmax, pendingcnt; |
155 | static int pendingmax, pendingcnt; |
132 | |
156 | |
133 | static void |
157 | static void |
134 | event (W w, int events) |
158 | event (W w, int events) |
135 | { |
159 | { |
|
|
160 | if (w->active) |
|
|
161 | { |
136 | w->pending = ++pendingcnt; |
162 | w->pending = ++pendingcnt; |
137 | array_needsize (pendings, pendingmax, pendingcnt, ); |
163 | array_needsize (pendings, pendingmax, pendingcnt, ); |
138 | pendings [pendingcnt - 1].w = w; |
164 | pendings [pendingcnt - 1].w = w; |
139 | pendings [pendingcnt - 1].events = events; |
165 | pendings [pendingcnt - 1].events = events; |
|
|
166 | } |
140 | } |
167 | } |
141 | |
168 | |
142 | static void |
169 | static void |
143 | fd_event (int fd, int events) |
170 | fd_event (int fd, int events) |
144 | { |
171 | { |
… | |
… | |
163 | event (events [i], type); |
190 | event (events [i], type); |
164 | } |
191 | } |
165 | |
192 | |
166 | /*****************************************************************************/ |
193 | /*****************************************************************************/ |
167 | |
194 | |
168 | static struct ev_timer **atimers; |
195 | static struct ev_timer **timers; |
169 | static int atimermax, atimercnt; |
196 | static int timermax, timercnt; |
170 | |
197 | |
171 | static struct ev_timer **rtimers; |
198 | static struct ev_periodic **periodics; |
172 | static int rtimermax, rtimercnt; |
199 | static int periodicmax, periodiccnt; |
173 | |
200 | |
174 | static void |
201 | static void |
175 | upheap (struct ev_timer **timers, int k) |
202 | upheap (WT *timers, int k) |
176 | { |
203 | { |
177 | struct ev_timer *w = timers [k]; |
204 | WT w = timers [k]; |
178 | |
205 | |
179 | while (k && timers [k >> 1]->at > w->at) |
206 | while (k && timers [k >> 1]->at > w->at) |
180 | { |
207 | { |
181 | timers [k] = timers [k >> 1]; |
208 | timers [k] = timers [k >> 1]; |
182 | timers [k]->active = k + 1; |
209 | timers [k]->active = k + 1; |
… | |
… | |
187 | timers [k]->active = k + 1; |
214 | timers [k]->active = k + 1; |
188 | |
215 | |
189 | } |
216 | } |
190 | |
217 | |
191 | static void |
218 | static void |
192 | downheap (struct ev_timer **timers, int N, int k) |
219 | downheap (WT *timers, int N, int k) |
193 | { |
220 | { |
194 | struct ev_timer *w = timers [k]; |
221 | WT w = timers [k]; |
195 | |
222 | |
196 | while (k < (N >> 1)) |
223 | while (k < (N >> 1)) |
197 | { |
224 | { |
198 | int j = k << 1; |
225 | int j = k << 1; |
199 | |
226 | |
… | |
… | |
325 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
352 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
326 | #endif |
353 | #endif |
327 | |
354 | |
328 | if (ev_method) |
355 | if (ev_method) |
329 | { |
356 | { |
330 | evw_init (&sigev, sigcb, 0); |
357 | evw_init (&sigev, sigcb); |
331 | siginit (); |
358 | siginit (); |
332 | } |
359 | } |
333 | |
360 | |
334 | return ev_method; |
361 | return ev_method; |
335 | } |
362 | } |
336 | |
363 | |
337 | /*****************************************************************************/ |
364 | /*****************************************************************************/ |
338 | |
365 | |
339 | void ev_prefork (void) |
366 | void ev_prefork (void) |
340 | { |
367 | { |
|
|
368 | /* nop */ |
341 | } |
369 | } |
342 | |
370 | |
343 | void ev_postfork_parent (void) |
371 | void ev_postfork_parent (void) |
344 | { |
372 | { |
|
|
373 | /* nop */ |
345 | } |
374 | } |
346 | |
375 | |
347 | void ev_postfork_child (void) |
376 | void ev_postfork_child (void) |
348 | { |
377 | { |
349 | #if HAVE_EPOLL |
378 | #if HAVE_EPOLL |
… | |
… | |
404 | |
433 | |
405 | pendingcnt = 0; |
434 | pendingcnt = 0; |
406 | } |
435 | } |
407 | |
436 | |
408 | static void |
437 | static void |
409 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
438 | timers_reify () |
410 | { |
439 | { |
411 | while (timercnt && timers [0]->at <= now) |
440 | while (timercnt && timers [0]->at <= now) |
412 | { |
441 | { |
413 | struct ev_timer *w = timers [0]; |
442 | struct ev_timer *w = timers [0]; |
|
|
443 | |
|
|
444 | event ((W)w, EV_TIMEOUT); |
414 | |
445 | |
415 | /* first reschedule or stop timer */ |
446 | /* first reschedule or stop timer */ |
416 | if (w->repeat) |
447 | if (w->repeat) |
417 | { |
448 | { |
418 | if (w->is_abs) |
|
|
419 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
420 | else |
|
|
421 | w->at = now + w->repeat; |
449 | w->at = now + w->repeat; |
422 | |
450 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
423 | assert (w->at > now); |
|
|
424 | |
|
|
425 | downheap (timers, timercnt, 0); |
451 | downheap ((WT *)timers, timercnt, 0); |
426 | } |
452 | } |
427 | else |
453 | else |
428 | { |
|
|
429 | evtimer_stop (w); /* nonrepeating: stop timer */ |
454 | evtimer_stop (w); /* nonrepeating: stop timer */ |
430 | --timercnt; /* maybe pass by reference instead? */ |
455 | } |
|
|
456 | } |
|
|
457 | |
|
|
458 | static void |
|
|
459 | periodics_reify () |
|
|
460 | { |
|
|
461 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
462 | { |
|
|
463 | struct ev_periodic *w = periodics [0]; |
|
|
464 | |
|
|
465 | /* first reschedule or stop timer */ |
|
|
466 | if (w->interval) |
431 | } |
467 | { |
|
|
468 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
469 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
470 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
471 | } |
|
|
472 | else |
|
|
473 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
432 | |
474 | |
433 | event ((W)w, EV_TIMEOUT); |
475 | event ((W)w, EV_TIMEOUT); |
434 | } |
476 | } |
435 | } |
477 | } |
436 | |
478 | |
437 | static void |
479 | static void |
|
|
480 | periodics_reschedule (ev_tstamp diff) |
|
|
481 | { |
|
|
482 | int i; |
|
|
483 | |
|
|
484 | /* adjust periodics after time jump */ |
|
|
485 | for (i = 0; i < periodiccnt; ++i) |
|
|
486 | { |
|
|
487 | struct ev_periodic *w = periodics [i]; |
|
|
488 | |
|
|
489 | if (w->interval) |
|
|
490 | { |
|
|
491 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
492 | |
|
|
493 | if (fabs (diff) >= 1e-4) |
|
|
494 | { |
|
|
495 | evperiodic_stop (w); |
|
|
496 | evperiodic_start (w); |
|
|
497 | |
|
|
498 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
499 | } |
|
|
500 | } |
|
|
501 | } |
|
|
502 | } |
|
|
503 | |
|
|
504 | static void |
438 | time_update () |
505 | time_update () |
439 | { |
506 | { |
440 | int i; |
507 | int i; |
|
|
508 | |
441 | ev_now = ev_time (); |
509 | ev_now = ev_time (); |
442 | |
510 | |
443 | if (have_monotonic) |
511 | if (have_monotonic) |
444 | { |
512 | { |
445 | ev_tstamp odiff = diff; |
513 | ev_tstamp odiff = diff; |
446 | |
514 | |
447 | /* detecting time jumps is much more difficult */ |
|
|
448 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
515 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
449 | { |
516 | { |
450 | now = get_clock (); |
517 | now = get_clock (); |
451 | diff = ev_now - now; |
518 | diff = ev_now - now; |
452 | |
519 | |
453 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
520 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
454 | return; /* all is well */ |
521 | return; /* all is well */ |
455 | |
522 | |
456 | ev_now = ev_time (); |
523 | ev_now = ev_time (); |
457 | } |
524 | } |
458 | |
525 | |
459 | /* time jump detected, reschedule atimers */ |
526 | periodics_reschedule (diff - odiff); |
460 | for (i = 0; i < atimercnt; ++i) |
527 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
461 | { |
|
|
462 | struct ev_timer *w = atimers [i]; |
|
|
463 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
464 | } |
|
|
465 | } |
528 | } |
466 | else |
529 | else |
467 | { |
530 | { |
468 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
531 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
469 | /* time jump detected, adjust rtimers */ |
532 | { |
|
|
533 | periodics_reschedule (ev_now - now); |
|
|
534 | |
|
|
535 | /* adjust timers. this is easy, as the offset is the same for all */ |
470 | for (i = 0; i < rtimercnt; ++i) |
536 | for (i = 0; i < timercnt; ++i) |
471 | rtimers [i]->at += ev_now - now; |
537 | timers [i]->at += diff; |
|
|
538 | } |
472 | |
539 | |
473 | now = ev_now; |
540 | now = ev_now; |
474 | } |
541 | } |
475 | } |
542 | } |
476 | |
543 | |
477 | int ev_loop_done; |
544 | int ev_loop_done; |
478 | |
545 | |
479 | void ev_loop (int flags) |
546 | void ev_loop (int flags) |
480 | { |
547 | { |
481 | double block; |
548 | double block; |
482 | ev_loop_done = flags & EVLOOP_ONESHOT; |
549 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
483 | |
550 | |
484 | if (checkcnt) |
551 | if (checkcnt) |
485 | { |
552 | { |
486 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
553 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
487 | call_pending (); |
554 | call_pending (); |
… | |
… | |
491 | { |
558 | { |
492 | /* update fd-related kernel structures */ |
559 | /* update fd-related kernel structures */ |
493 | fd_reify (); |
560 | fd_reify (); |
494 | |
561 | |
495 | /* calculate blocking time */ |
562 | /* calculate blocking time */ |
|
|
563 | |
|
|
564 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
|
|
565 | ev_now = ev_time (); |
|
|
566 | |
496 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
567 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
497 | block = 0.; |
568 | block = 0.; |
498 | else |
569 | else |
499 | { |
570 | { |
500 | block = MAX_BLOCKTIME; |
571 | block = MAX_BLOCKTIME; |
501 | |
572 | |
502 | if (rtimercnt) |
573 | if (timercnt) |
503 | { |
574 | { |
504 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
575 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
505 | if (block > to) block = to; |
576 | if (block > to) block = to; |
506 | } |
577 | } |
507 | |
578 | |
508 | if (atimercnt) |
579 | if (periodiccnt) |
509 | { |
580 | { |
510 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
581 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
511 | if (block > to) block = to; |
582 | if (block > to) block = to; |
512 | } |
583 | } |
513 | |
584 | |
514 | if (block < 0.) block = 0.; |
585 | if (block < 0.) block = 0.; |
515 | } |
586 | } |
… | |
… | |
518 | |
589 | |
519 | /* update ev_now, do magic */ |
590 | /* update ev_now, do magic */ |
520 | time_update (); |
591 | time_update (); |
521 | |
592 | |
522 | /* queue pending timers and reschedule them */ |
593 | /* queue pending timers and reschedule them */ |
523 | /* absolute timers first */ |
594 | periodics_reify (); /* absolute timers first */ |
524 | timers_reify (atimers, atimercnt, ev_now); |
|
|
525 | /* relative timers second */ |
595 | timers_reify (); /* relative timers second */ |
526 | timers_reify (rtimers, rtimercnt, now); |
|
|
527 | |
596 | |
528 | /* queue idle watchers unless io or timers are pending */ |
597 | /* queue idle watchers unless io or timers are pending */ |
529 | if (!pendingcnt) |
598 | if (!pendingcnt) |
530 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
599 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
531 | |
600 | |
… | |
… | |
533 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
602 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
534 | |
603 | |
535 | call_pending (); |
604 | call_pending (); |
536 | } |
605 | } |
537 | while (!ev_loop_done); |
606 | while (!ev_loop_done); |
|
|
607 | |
|
|
608 | if (ev_loop_done != 2) |
|
|
609 | ev_loop_done = 0; |
538 | } |
610 | } |
539 | |
611 | |
540 | /*****************************************************************************/ |
612 | /*****************************************************************************/ |
541 | |
613 | |
542 | static void |
614 | static void |
… | |
… | |
560 | head = &(*head)->next; |
632 | head = &(*head)->next; |
561 | } |
633 | } |
562 | } |
634 | } |
563 | |
635 | |
564 | static void |
636 | static void |
|
|
637 | ev_clear (W w) |
|
|
638 | { |
|
|
639 | if (w->pending) |
|
|
640 | { |
|
|
641 | pendings [w->pending - 1].w = 0; |
|
|
642 | w->pending = 0; |
|
|
643 | } |
|
|
644 | } |
|
|
645 | |
|
|
646 | static void |
565 | ev_start (W w, int active) |
647 | ev_start (W w, int active) |
566 | { |
648 | { |
567 | w->pending = 0; |
|
|
568 | w->active = active; |
649 | w->active = active; |
569 | } |
650 | } |
570 | |
651 | |
571 | static void |
652 | static void |
572 | ev_stop (W w) |
653 | ev_stop (W w) |
573 | { |
654 | { |
574 | if (w->pending) |
|
|
575 | pendings [w->pending - 1].w = 0; |
|
|
576 | |
|
|
577 | w->active = 0; |
655 | w->active = 0; |
578 | /* nop */ |
|
|
579 | } |
656 | } |
580 | |
657 | |
581 | /*****************************************************************************/ |
658 | /*****************************************************************************/ |
582 | |
659 | |
583 | void |
660 | void |
… | |
… | |
598 | } |
675 | } |
599 | |
676 | |
600 | void |
677 | void |
601 | evio_stop (struct ev_io *w) |
678 | evio_stop (struct ev_io *w) |
602 | { |
679 | { |
|
|
680 | ev_clear ((W)w); |
603 | if (!ev_is_active (w)) |
681 | if (!ev_is_active (w)) |
604 | return; |
682 | return; |
605 | |
683 | |
606 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
684 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
607 | ev_stop ((W)w); |
685 | ev_stop ((W)w); |
… | |
… | |
615 | evtimer_start (struct ev_timer *w) |
693 | evtimer_start (struct ev_timer *w) |
616 | { |
694 | { |
617 | if (ev_is_active (w)) |
695 | if (ev_is_active (w)) |
618 | return; |
696 | return; |
619 | |
697 | |
620 | if (w->is_abs) |
698 | w->at += now; |
621 | { |
|
|
622 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
623 | if (w->repeat) |
|
|
624 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
625 | |
699 | |
|
|
700 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
701 | |
626 | ev_start ((W)w, ++atimercnt); |
702 | ev_start ((W)w, ++timercnt); |
627 | array_needsize (atimers, atimermax, atimercnt, ); |
703 | array_needsize (timers, timermax, timercnt, ); |
628 | atimers [atimercnt - 1] = w; |
704 | timers [timercnt - 1] = w; |
629 | upheap (atimers, atimercnt - 1); |
705 | upheap ((WT *)timers, timercnt - 1); |
630 | } |
|
|
631 | else |
|
|
632 | { |
|
|
633 | w->at += now; |
|
|
634 | |
|
|
635 | ev_start ((W)w, ++rtimercnt); |
|
|
636 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
637 | rtimers [rtimercnt - 1] = w; |
|
|
638 | upheap (rtimers, rtimercnt - 1); |
|
|
639 | } |
|
|
640 | |
|
|
641 | } |
706 | } |
642 | |
707 | |
643 | void |
708 | void |
644 | evtimer_stop (struct ev_timer *w) |
709 | evtimer_stop (struct ev_timer *w) |
645 | { |
710 | { |
|
|
711 | ev_clear ((W)w); |
646 | if (!ev_is_active (w)) |
712 | if (!ev_is_active (w)) |
647 | return; |
713 | return; |
648 | |
714 | |
649 | if (w->is_abs) |
|
|
650 | { |
|
|
651 | if (w->active < atimercnt--) |
715 | if (w->active < timercnt--) |
652 | { |
716 | { |
653 | atimers [w->active - 1] = atimers [atimercnt]; |
717 | timers [w->active - 1] = timers [timercnt]; |
|
|
718 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
719 | } |
|
|
720 | |
|
|
721 | w->at = w->repeat; |
|
|
722 | |
|
|
723 | ev_stop ((W)w); |
|
|
724 | } |
|
|
725 | |
|
|
726 | void |
|
|
727 | evtimer_again (struct ev_timer *w) |
|
|
728 | { |
|
|
729 | if (ev_is_active (w)) |
|
|
730 | { |
|
|
731 | if (w->repeat) |
|
|
732 | { |
|
|
733 | w->at = now + w->repeat; |
654 | downheap (atimers, atimercnt, w->active - 1); |
734 | downheap ((WT *)timers, timercnt, w->active - 1); |
655 | } |
|
|
656 | } |
|
|
657 | else |
|
|
658 | { |
|
|
659 | if (w->active < rtimercnt--) |
|
|
660 | { |
735 | } |
661 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
736 | else |
662 | downheap (rtimers, rtimercnt, w->active - 1); |
737 | evtimer_stop (w); |
663 | } |
738 | } |
|
|
739 | else if (w->repeat) |
|
|
740 | evtimer_start (w); |
|
|
741 | } |
|
|
742 | |
|
|
743 | void |
|
|
744 | evperiodic_start (struct ev_periodic *w) |
|
|
745 | { |
|
|
746 | if (ev_is_active (w)) |
|
|
747 | return; |
|
|
748 | |
|
|
749 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
750 | |
|
|
751 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
752 | if (w->interval) |
|
|
753 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
754 | |
|
|
755 | ev_start ((W)w, ++periodiccnt); |
|
|
756 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
757 | periodics [periodiccnt - 1] = w; |
|
|
758 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
759 | } |
|
|
760 | |
|
|
761 | void |
|
|
762 | evperiodic_stop (struct ev_periodic *w) |
|
|
763 | { |
|
|
764 | ev_clear ((W)w); |
|
|
765 | if (!ev_is_active (w)) |
|
|
766 | return; |
|
|
767 | |
|
|
768 | if (w->active < periodiccnt--) |
|
|
769 | { |
|
|
770 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
771 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
664 | } |
772 | } |
665 | |
773 | |
666 | ev_stop ((W)w); |
774 | ev_stop ((W)w); |
667 | } |
775 | } |
668 | |
776 | |
… | |
… | |
687 | } |
795 | } |
688 | |
796 | |
689 | void |
797 | void |
690 | evsignal_stop (struct ev_signal *w) |
798 | evsignal_stop (struct ev_signal *w) |
691 | { |
799 | { |
|
|
800 | ev_clear ((W)w); |
692 | if (!ev_is_active (w)) |
801 | if (!ev_is_active (w)) |
693 | return; |
802 | return; |
694 | |
803 | |
695 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
804 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
696 | ev_stop ((W)w); |
805 | ev_stop ((W)w); |
… | |
… | |
709 | idles [idlecnt - 1] = w; |
818 | idles [idlecnt - 1] = w; |
710 | } |
819 | } |
711 | |
820 | |
712 | void evidle_stop (struct ev_idle *w) |
821 | void evidle_stop (struct ev_idle *w) |
713 | { |
822 | { |
|
|
823 | ev_clear ((W)w); |
|
|
824 | if (ev_is_active (w)) |
|
|
825 | return; |
|
|
826 | |
714 | idles [w->active - 1] = idles [--idlecnt]; |
827 | idles [w->active - 1] = idles [--idlecnt]; |
715 | ev_stop ((W)w); |
828 | ev_stop ((W)w); |
716 | } |
829 | } |
717 | |
830 | |
718 | void evcheck_start (struct ev_check *w) |
831 | void evcheck_start (struct ev_check *w) |
… | |
… | |
725 | checks [checkcnt - 1] = w; |
838 | checks [checkcnt - 1] = w; |
726 | } |
839 | } |
727 | |
840 | |
728 | void evcheck_stop (struct ev_check *w) |
841 | void evcheck_stop (struct ev_check *w) |
729 | { |
842 | { |
|
|
843 | ev_clear ((W)w); |
|
|
844 | if (ev_is_active (w)) |
|
|
845 | return; |
|
|
846 | |
730 | checks [w->active - 1] = checks [--checkcnt]; |
847 | checks [w->active - 1] = checks [--checkcnt]; |
731 | ev_stop ((W)w); |
848 | ev_stop ((W)w); |
732 | } |
849 | } |
733 | |
850 | |
734 | /*****************************************************************************/ |
851 | /*****************************************************************************/ |
735 | |
852 | |
|
|
853 | struct ev_once |
|
|
854 | { |
|
|
855 | struct ev_io io; |
|
|
856 | struct ev_timer to; |
|
|
857 | void (*cb)(int revents, void *arg); |
|
|
858 | void *arg; |
|
|
859 | }; |
|
|
860 | |
|
|
861 | static void |
|
|
862 | once_cb (struct ev_once *once, int revents) |
|
|
863 | { |
|
|
864 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
865 | void *arg = once->arg; |
|
|
866 | |
|
|
867 | evio_stop (&once->io); |
|
|
868 | evtimer_stop (&once->to); |
|
|
869 | free (once); |
|
|
870 | |
|
|
871 | cb (revents, arg); |
|
|
872 | } |
|
|
873 | |
|
|
874 | static void |
|
|
875 | once_cb_io (struct ev_io *w, int revents) |
|
|
876 | { |
|
|
877 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
878 | } |
|
|
879 | |
|
|
880 | static void |
|
|
881 | once_cb_to (struct ev_timer *w, int revents) |
|
|
882 | { |
|
|
883 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
884 | } |
|
|
885 | |
|
|
886 | void |
|
|
887 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
888 | { |
|
|
889 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
890 | |
|
|
891 | if (!once) |
|
|
892 | cb (EV_ERROR, arg); |
|
|
893 | else |
|
|
894 | { |
|
|
895 | once->cb = cb; |
|
|
896 | once->arg = arg; |
|
|
897 | |
|
|
898 | evw_init (&once->io, once_cb_io); |
|
|
899 | |
|
|
900 | if (fd >= 0) |
|
|
901 | { |
|
|
902 | evio_set (&once->io, fd, events); |
|
|
903 | evio_start (&once->io); |
|
|
904 | } |
|
|
905 | |
|
|
906 | evw_init (&once->to, once_cb_to); |
|
|
907 | |
|
|
908 | if (timeout >= 0.) |
|
|
909 | { |
|
|
910 | evtimer_set (&once->to, timeout, 0.); |
|
|
911 | evtimer_start (&once->to); |
|
|
912 | } |
|
|
913 | } |
|
|
914 | } |
|
|
915 | |
|
|
916 | /*****************************************************************************/ |
|
|
917 | |
736 | #if 0 |
918 | #if 0 |
|
|
919 | |
|
|
920 | struct ev_io wio; |
737 | |
921 | |
738 | static void |
922 | static void |
739 | sin_cb (struct ev_io *w, int revents) |
923 | sin_cb (struct ev_io *w, int revents) |
740 | { |
924 | { |
741 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
925 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
751 | |
935 | |
752 | static void |
936 | static void |
753 | scb (struct ev_signal *w, int revents) |
937 | scb (struct ev_signal *w, int revents) |
754 | { |
938 | { |
755 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
939 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
940 | evio_stop (&wio); |
|
|
941 | evio_start (&wio); |
756 | } |
942 | } |
757 | |
943 | |
758 | static void |
944 | static void |
759 | gcb (struct ev_signal *w, int revents) |
945 | gcb (struct ev_signal *w, int revents) |
760 | { |
946 | { |
761 | fprintf (stderr, "generic %x\n", revents); |
947 | fprintf (stderr, "generic %x\n", revents); |
|
|
948 | |
762 | } |
949 | } |
763 | |
950 | |
764 | int main (void) |
951 | int main (void) |
765 | { |
952 | { |
766 | struct ev_io sin; |
|
|
767 | |
|
|
768 | ev_init (0); |
953 | ev_init (0); |
769 | |
954 | |
770 | evw_init (&sin, sin_cb, 55); |
|
|
771 | evio_set (&sin, 0, EV_READ); |
955 | evio_init (&wio, sin_cb, 0, EV_READ); |
772 | evio_start (&sin); |
956 | evio_start (&wio); |
773 | |
957 | |
774 | struct ev_timer t[10000]; |
958 | struct ev_timer t[10000]; |
775 | |
959 | |
776 | #if 0 |
960 | #if 0 |
777 | int i; |
961 | int i; |
778 | for (i = 0; i < 10000; ++i) |
962 | for (i = 0; i < 10000; ++i) |
779 | { |
963 | { |
780 | struct ev_timer *w = t + i; |
964 | struct ev_timer *w = t + i; |
781 | evw_init (w, ocb, i); |
965 | evw_init (w, ocb, i); |
782 | evtimer_set_abs (w, drand48 (), 0.99775533); |
966 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
783 | evtimer_start (w); |
967 | evtimer_start (w); |
784 | if (drand48 () < 0.5) |
968 | if (drand48 () < 0.5) |
785 | evtimer_stop (w); |
969 | evtimer_stop (w); |
786 | } |
970 | } |
787 | #endif |
971 | #endif |
788 | |
972 | |
789 | struct ev_timer t1; |
973 | struct ev_timer t1; |
790 | evw_init (&t1, ocb, 0); |
974 | evtimer_init (&t1, ocb, 5, 10); |
791 | evtimer_set_abs (&t1, 5, 10); |
|
|
792 | evtimer_start (&t1); |
975 | evtimer_start (&t1); |
793 | |
976 | |
794 | struct ev_signal sig; |
977 | struct ev_signal sig; |
795 | evw_init (&sig, scb, 65535); |
|
|
796 | evsignal_set (&sig, SIGQUIT); |
978 | evsignal_init (&sig, scb, SIGQUIT); |
797 | evsignal_start (&sig); |
979 | evsignal_start (&sig); |
798 | |
980 | |
799 | struct ev_check cw; |
981 | struct ev_check cw; |
800 | evw_init (&cw, gcb, 0); |
982 | evcheck_init (&cw, gcb); |
801 | evcheck_start (&cw); |
983 | evcheck_start (&cw); |
802 | |
984 | |
803 | struct ev_idle iw; |
985 | struct ev_idle iw; |
804 | evw_init (&iw, gcb, 0); |
986 | evidle_init (&iw, gcb); |
805 | evidle_start (&iw); |
987 | evidle_start (&iw); |
806 | |
988 | |
807 | ev_loop (0); |
989 | ev_loop (0); |
808 | |
990 | |
809 | return 0; |
991 | return 0; |