|
|
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 | } |
… | |
… | |
389 | } |
416 | } |
390 | |
417 | |
391 | static void |
418 | static void |
392 | call_pending () |
419 | call_pending () |
393 | { |
420 | { |
394 | int i; |
421 | while (pendingcnt) |
395 | |
|
|
396 | for (i = 0; i < pendingcnt; ++i) |
|
|
397 | { |
422 | { |
398 | ANPENDING *p = pendings + i; |
423 | ANPENDING *p = pendings + --pendingcnt; |
399 | |
424 | |
400 | if (p->w) |
425 | if (p->w) |
401 | { |
426 | { |
402 | p->w->pending = 0; |
427 | p->w->pending = 0; |
403 | p->w->cb (p->w, p->events); |
428 | p->w->cb (p->w, p->events); |
404 | } |
429 | } |
405 | } |
430 | } |
406 | |
|
|
407 | pendingcnt = 0; |
|
|
408 | } |
431 | } |
409 | |
432 | |
410 | static void |
433 | static void |
411 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
434 | timers_reify () |
412 | { |
435 | { |
413 | while (timercnt && timers [0]->at <= now) |
436 | while (timercnt && timers [0]->at <= now) |
414 | { |
437 | { |
415 | struct ev_timer *w = timers [0]; |
438 | struct ev_timer *w = timers [0]; |
|
|
439 | |
|
|
440 | event ((W)w, EV_TIMEOUT); |
416 | |
441 | |
417 | /* first reschedule or stop timer */ |
442 | /* first reschedule or stop timer */ |
418 | if (w->repeat) |
443 | if (w->repeat) |
419 | { |
444 | { |
420 | if (w->is_abs) |
|
|
421 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
422 | else |
|
|
423 | w->at = now + w->repeat; |
445 | w->at = now + w->repeat; |
424 | |
446 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
425 | assert (w->at > now); |
|
|
426 | |
|
|
427 | downheap (timers, timercnt, 0); |
447 | downheap ((WT *)timers, timercnt, 0); |
428 | } |
448 | } |
429 | else |
449 | else |
430 | { |
|
|
431 | evtimer_stop (w); /* nonrepeating: stop timer */ |
450 | evtimer_stop (w); /* nonrepeating: stop timer */ |
432 | --timercnt; /* maybe pass by reference instead? */ |
451 | } |
|
|
452 | } |
|
|
453 | |
|
|
454 | static void |
|
|
455 | periodics_reify () |
|
|
456 | { |
|
|
457 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
458 | { |
|
|
459 | struct ev_periodic *w = periodics [0]; |
|
|
460 | |
|
|
461 | /* first reschedule or stop timer */ |
|
|
462 | if (w->interval) |
433 | } |
463 | { |
|
|
464 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
465 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
466 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
467 | } |
|
|
468 | else |
|
|
469 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
434 | |
470 | |
435 | event ((W)w, EV_TIMEOUT); |
471 | event ((W)w, EV_TIMEOUT); |
436 | } |
472 | } |
437 | } |
473 | } |
438 | |
474 | |
439 | static void |
475 | static void |
|
|
476 | periodics_reschedule (ev_tstamp diff) |
|
|
477 | { |
|
|
478 | int i; |
|
|
479 | |
|
|
480 | /* adjust periodics after time jump */ |
|
|
481 | for (i = 0; i < periodiccnt; ++i) |
|
|
482 | { |
|
|
483 | struct ev_periodic *w = periodics [i]; |
|
|
484 | |
|
|
485 | if (w->interval) |
|
|
486 | { |
|
|
487 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
488 | |
|
|
489 | if (fabs (diff) >= 1e-4) |
|
|
490 | { |
|
|
491 | evperiodic_stop (w); |
|
|
492 | evperiodic_start (w); |
|
|
493 | |
|
|
494 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
495 | } |
|
|
496 | } |
|
|
497 | } |
|
|
498 | } |
|
|
499 | |
|
|
500 | static void |
440 | time_update () |
501 | time_update () |
441 | { |
502 | { |
442 | int i; |
503 | int i; |
|
|
504 | |
443 | ev_now = ev_time (); |
505 | ev_now = ev_time (); |
444 | |
506 | |
445 | if (have_monotonic) |
507 | if (have_monotonic) |
446 | { |
508 | { |
447 | ev_tstamp odiff = diff; |
509 | ev_tstamp odiff = diff; |
448 | |
510 | |
449 | /* detecting time jumps is much more difficult */ |
|
|
450 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
511 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
451 | { |
512 | { |
452 | now = get_clock (); |
513 | now = get_clock (); |
453 | diff = ev_now - now; |
514 | diff = ev_now - now; |
454 | |
515 | |
455 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
516 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
456 | return; /* all is well */ |
517 | return; /* all is well */ |
457 | |
518 | |
458 | ev_now = ev_time (); |
519 | ev_now = ev_time (); |
459 | } |
520 | } |
460 | |
521 | |
461 | /* time jump detected, reschedule atimers */ |
522 | periodics_reschedule (diff - odiff); |
462 | for (i = 0; i < atimercnt; ++i) |
523 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
463 | { |
|
|
464 | struct ev_timer *w = atimers [i]; |
|
|
465 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
466 | } |
|
|
467 | } |
524 | } |
468 | else |
525 | else |
469 | { |
526 | { |
470 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
527 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
471 | /* time jump detected, adjust rtimers */ |
528 | { |
|
|
529 | periodics_reschedule (ev_now - now); |
|
|
530 | |
|
|
531 | /* adjust timers. this is easy, as the offset is the same for all */ |
472 | for (i = 0; i < rtimercnt; ++i) |
532 | for (i = 0; i < timercnt; ++i) |
473 | rtimers [i]->at += ev_now - now; |
533 | timers [i]->at += diff; |
|
|
534 | } |
474 | |
535 | |
475 | now = ev_now; |
536 | now = ev_now; |
476 | } |
537 | } |
477 | } |
538 | } |
478 | |
539 | |
479 | int ev_loop_done; |
540 | int ev_loop_done; |
480 | |
541 | |
481 | void ev_loop (int flags) |
542 | void ev_loop (int flags) |
482 | { |
543 | { |
483 | double block; |
544 | double block; |
484 | ev_loop_done = flags & EVLOOP_ONESHOT; |
545 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
485 | |
546 | |
486 | if (checkcnt) |
547 | if (checkcnt) |
487 | { |
548 | { |
488 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
549 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
489 | call_pending (); |
550 | call_pending (); |
… | |
… | |
493 | { |
554 | { |
494 | /* update fd-related kernel structures */ |
555 | /* update fd-related kernel structures */ |
495 | fd_reify (); |
556 | fd_reify (); |
496 | |
557 | |
497 | /* calculate blocking time */ |
558 | /* calculate blocking time */ |
|
|
559 | |
|
|
560 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
|
|
561 | ev_now = ev_time (); |
|
|
562 | |
498 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
563 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
499 | block = 0.; |
564 | block = 0.; |
500 | else |
565 | else |
501 | { |
566 | { |
502 | block = MAX_BLOCKTIME; |
567 | block = MAX_BLOCKTIME; |
503 | |
568 | |
504 | if (rtimercnt) |
569 | if (timercnt) |
505 | { |
570 | { |
506 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
571 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
507 | if (block > to) block = to; |
572 | if (block > to) block = to; |
508 | } |
573 | } |
509 | |
574 | |
510 | if (atimercnt) |
575 | if (periodiccnt) |
511 | { |
576 | { |
512 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
577 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
513 | if (block > to) block = to; |
578 | if (block > to) block = to; |
514 | } |
579 | } |
515 | |
580 | |
516 | if (block < 0.) block = 0.; |
581 | if (block < 0.) block = 0.; |
517 | } |
582 | } |
… | |
… | |
520 | |
585 | |
521 | /* update ev_now, do magic */ |
586 | /* update ev_now, do magic */ |
522 | time_update (); |
587 | time_update (); |
523 | |
588 | |
524 | /* queue pending timers and reschedule them */ |
589 | /* queue pending timers and reschedule them */ |
525 | /* absolute timers first */ |
590 | periodics_reify (); /* absolute timers first */ |
526 | timers_reify (atimers, atimercnt, ev_now); |
|
|
527 | /* relative timers second */ |
591 | timers_reify (); /* relative timers second */ |
528 | timers_reify (rtimers, rtimercnt, now); |
|
|
529 | |
592 | |
530 | /* queue idle watchers unless io or timers are pending */ |
593 | /* queue idle watchers unless io or timers are pending */ |
531 | if (!pendingcnt) |
594 | if (!pendingcnt) |
532 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
595 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
533 | |
596 | |
… | |
… | |
535 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
598 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
536 | |
599 | |
537 | call_pending (); |
600 | call_pending (); |
538 | } |
601 | } |
539 | while (!ev_loop_done); |
602 | while (!ev_loop_done); |
|
|
603 | |
|
|
604 | if (ev_loop_done != 2) |
|
|
605 | ev_loop_done = 0; |
540 | } |
606 | } |
541 | |
607 | |
542 | /*****************************************************************************/ |
608 | /*****************************************************************************/ |
543 | |
609 | |
544 | static void |
610 | static void |
… | |
… | |
562 | head = &(*head)->next; |
628 | head = &(*head)->next; |
563 | } |
629 | } |
564 | } |
630 | } |
565 | |
631 | |
566 | static void |
632 | static void |
|
|
633 | ev_clear (W w) |
|
|
634 | { |
|
|
635 | if (w->pending) |
|
|
636 | { |
|
|
637 | pendings [w->pending - 1].w = 0; |
|
|
638 | w->pending = 0; |
|
|
639 | } |
|
|
640 | } |
|
|
641 | |
|
|
642 | static void |
567 | ev_start (W w, int active) |
643 | ev_start (W w, int active) |
568 | { |
644 | { |
569 | w->pending = 0; |
|
|
570 | w->active = active; |
645 | w->active = active; |
571 | } |
646 | } |
572 | |
647 | |
573 | static void |
648 | static void |
574 | ev_stop (W w) |
649 | ev_stop (W w) |
575 | { |
650 | { |
576 | if (w->pending) |
|
|
577 | pendings [w->pending - 1].w = 0; |
|
|
578 | |
|
|
579 | w->active = 0; |
651 | w->active = 0; |
580 | } |
652 | } |
581 | |
653 | |
582 | /*****************************************************************************/ |
654 | /*****************************************************************************/ |
583 | |
655 | |
… | |
… | |
599 | } |
671 | } |
600 | |
672 | |
601 | void |
673 | void |
602 | evio_stop (struct ev_io *w) |
674 | evio_stop (struct ev_io *w) |
603 | { |
675 | { |
|
|
676 | ev_clear ((W)w); |
604 | if (!ev_is_active (w)) |
677 | if (!ev_is_active (w)) |
605 | return; |
678 | return; |
606 | |
679 | |
607 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
680 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
608 | ev_stop ((W)w); |
681 | ev_stop ((W)w); |
… | |
… | |
616 | evtimer_start (struct ev_timer *w) |
689 | evtimer_start (struct ev_timer *w) |
617 | { |
690 | { |
618 | if (ev_is_active (w)) |
691 | if (ev_is_active (w)) |
619 | return; |
692 | return; |
620 | |
693 | |
621 | if (w->is_abs) |
694 | w->at += now; |
622 | { |
|
|
623 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
624 | if (w->repeat) |
|
|
625 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
626 | |
695 | |
|
|
696 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
697 | |
627 | ev_start ((W)w, ++atimercnt); |
698 | ev_start ((W)w, ++timercnt); |
628 | array_needsize (atimers, atimermax, atimercnt, ); |
699 | array_needsize (timers, timermax, timercnt, ); |
629 | atimers [atimercnt - 1] = w; |
700 | timers [timercnt - 1] = w; |
630 | upheap (atimers, atimercnt - 1); |
701 | upheap ((WT *)timers, timercnt - 1); |
631 | } |
|
|
632 | else |
|
|
633 | { |
|
|
634 | w->at += now; |
|
|
635 | |
|
|
636 | ev_start ((W)w, ++rtimercnt); |
|
|
637 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
638 | rtimers [rtimercnt - 1] = w; |
|
|
639 | upheap (rtimers, rtimercnt - 1); |
|
|
640 | } |
|
|
641 | |
|
|
642 | } |
702 | } |
643 | |
703 | |
644 | void |
704 | void |
645 | evtimer_stop (struct ev_timer *w) |
705 | evtimer_stop (struct ev_timer *w) |
646 | { |
706 | { |
|
|
707 | ev_clear ((W)w); |
647 | if (!ev_is_active (w)) |
708 | if (!ev_is_active (w)) |
648 | return; |
709 | return; |
649 | |
710 | |
650 | if (w->is_abs) |
|
|
651 | { |
|
|
652 | if (w->active < atimercnt--) |
711 | if (w->active < timercnt--) |
653 | { |
712 | { |
654 | atimers [w->active - 1] = atimers [atimercnt]; |
713 | timers [w->active - 1] = timers [timercnt]; |
|
|
714 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
715 | } |
|
|
716 | |
|
|
717 | w->at = w->repeat; |
|
|
718 | |
|
|
719 | ev_stop ((W)w); |
|
|
720 | } |
|
|
721 | |
|
|
722 | void |
|
|
723 | evtimer_again (struct ev_timer *w) |
|
|
724 | { |
|
|
725 | if (ev_is_active (w)) |
|
|
726 | { |
|
|
727 | if (w->repeat) |
|
|
728 | { |
|
|
729 | w->at = now + w->repeat; |
655 | downheap (atimers, atimercnt, w->active - 1); |
730 | downheap ((WT *)timers, timercnt, w->active - 1); |
656 | } |
|
|
657 | } |
|
|
658 | else |
|
|
659 | { |
|
|
660 | if (w->active < rtimercnt--) |
|
|
661 | { |
731 | } |
662 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
732 | else |
663 | downheap (rtimers, rtimercnt, w->active - 1); |
733 | evtimer_stop (w); |
664 | } |
734 | } |
|
|
735 | else if (w->repeat) |
|
|
736 | evtimer_start (w); |
|
|
737 | } |
|
|
738 | |
|
|
739 | void |
|
|
740 | evperiodic_start (struct ev_periodic *w) |
|
|
741 | { |
|
|
742 | if (ev_is_active (w)) |
|
|
743 | return; |
|
|
744 | |
|
|
745 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
746 | |
|
|
747 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
748 | if (w->interval) |
|
|
749 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
750 | |
|
|
751 | ev_start ((W)w, ++periodiccnt); |
|
|
752 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
753 | periodics [periodiccnt - 1] = w; |
|
|
754 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
755 | } |
|
|
756 | |
|
|
757 | void |
|
|
758 | evperiodic_stop (struct ev_periodic *w) |
|
|
759 | { |
|
|
760 | ev_clear ((W)w); |
|
|
761 | if (!ev_is_active (w)) |
|
|
762 | return; |
|
|
763 | |
|
|
764 | if (w->active < periodiccnt--) |
|
|
765 | { |
|
|
766 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
767 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
665 | } |
768 | } |
666 | |
769 | |
667 | ev_stop ((W)w); |
770 | ev_stop ((W)w); |
668 | } |
771 | } |
669 | |
772 | |
… | |
… | |
688 | } |
791 | } |
689 | |
792 | |
690 | void |
793 | void |
691 | evsignal_stop (struct ev_signal *w) |
794 | evsignal_stop (struct ev_signal *w) |
692 | { |
795 | { |
|
|
796 | ev_clear ((W)w); |
693 | if (!ev_is_active (w)) |
797 | if (!ev_is_active (w)) |
694 | return; |
798 | return; |
695 | |
799 | |
696 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
800 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
697 | ev_stop ((W)w); |
801 | ev_stop ((W)w); |
… | |
… | |
710 | idles [idlecnt - 1] = w; |
814 | idles [idlecnt - 1] = w; |
711 | } |
815 | } |
712 | |
816 | |
713 | void evidle_stop (struct ev_idle *w) |
817 | void evidle_stop (struct ev_idle *w) |
714 | { |
818 | { |
|
|
819 | ev_clear ((W)w); |
|
|
820 | if (ev_is_active (w)) |
|
|
821 | return; |
|
|
822 | |
715 | idles [w->active - 1] = idles [--idlecnt]; |
823 | idles [w->active - 1] = idles [--idlecnt]; |
716 | ev_stop ((W)w); |
824 | ev_stop ((W)w); |
717 | } |
825 | } |
718 | |
826 | |
719 | void evcheck_start (struct ev_check *w) |
827 | void evcheck_start (struct ev_check *w) |
… | |
… | |
726 | checks [checkcnt - 1] = w; |
834 | checks [checkcnt - 1] = w; |
727 | } |
835 | } |
728 | |
836 | |
729 | void evcheck_stop (struct ev_check *w) |
837 | void evcheck_stop (struct ev_check *w) |
730 | { |
838 | { |
|
|
839 | ev_clear ((W)w); |
|
|
840 | if (ev_is_active (w)) |
|
|
841 | return; |
|
|
842 | |
731 | checks [w->active - 1] = checks [--checkcnt]; |
843 | checks [w->active - 1] = checks [--checkcnt]; |
732 | ev_stop ((W)w); |
844 | ev_stop ((W)w); |
733 | } |
845 | } |
734 | |
846 | |
735 | /*****************************************************************************/ |
847 | /*****************************************************************************/ |
736 | |
848 | |
|
|
849 | struct ev_once |
|
|
850 | { |
|
|
851 | struct ev_io io; |
|
|
852 | struct ev_timer to; |
|
|
853 | void (*cb)(int revents, void *arg); |
|
|
854 | void *arg; |
|
|
855 | }; |
|
|
856 | |
|
|
857 | static void |
|
|
858 | once_cb (struct ev_once *once, int revents) |
|
|
859 | { |
|
|
860 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
861 | void *arg = once->arg; |
|
|
862 | |
|
|
863 | evio_stop (&once->io); |
|
|
864 | evtimer_stop (&once->to); |
|
|
865 | free (once); |
|
|
866 | |
|
|
867 | cb (revents, arg); |
|
|
868 | } |
|
|
869 | |
|
|
870 | static void |
|
|
871 | once_cb_io (struct ev_io *w, int revents) |
|
|
872 | { |
|
|
873 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
874 | } |
|
|
875 | |
|
|
876 | static void |
|
|
877 | once_cb_to (struct ev_timer *w, int revents) |
|
|
878 | { |
|
|
879 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
880 | } |
|
|
881 | |
|
|
882 | void |
|
|
883 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
884 | { |
|
|
885 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
886 | |
|
|
887 | if (!once) |
|
|
888 | cb (EV_ERROR, arg); |
|
|
889 | else |
|
|
890 | { |
|
|
891 | once->cb = cb; |
|
|
892 | once->arg = arg; |
|
|
893 | |
|
|
894 | evw_init (&once->io, once_cb_io); |
|
|
895 | |
|
|
896 | if (fd >= 0) |
|
|
897 | { |
|
|
898 | evio_set (&once->io, fd, events); |
|
|
899 | evio_start (&once->io); |
|
|
900 | } |
|
|
901 | |
|
|
902 | evw_init (&once->to, once_cb_to); |
|
|
903 | |
|
|
904 | if (timeout >= 0.) |
|
|
905 | { |
|
|
906 | evtimer_set (&once->to, timeout, 0.); |
|
|
907 | evtimer_start (&once->to); |
|
|
908 | } |
|
|
909 | } |
|
|
910 | } |
|
|
911 | |
|
|
912 | /*****************************************************************************/ |
|
|
913 | |
737 | #if 0 |
914 | #if 0 |
|
|
915 | |
|
|
916 | struct ev_io wio; |
738 | |
917 | |
739 | static void |
918 | static void |
740 | sin_cb (struct ev_io *w, int revents) |
919 | sin_cb (struct ev_io *w, int revents) |
741 | { |
920 | { |
742 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
921 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
752 | |
931 | |
753 | static void |
932 | static void |
754 | scb (struct ev_signal *w, int revents) |
933 | scb (struct ev_signal *w, int revents) |
755 | { |
934 | { |
756 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
935 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
936 | evio_stop (&wio); |
|
|
937 | evio_start (&wio); |
757 | } |
938 | } |
758 | |
939 | |
759 | static void |
940 | static void |
760 | gcb (struct ev_signal *w, int revents) |
941 | gcb (struct ev_signal *w, int revents) |
761 | { |
942 | { |
762 | fprintf (stderr, "generic %x\n", revents); |
943 | fprintf (stderr, "generic %x\n", revents); |
|
|
944 | |
763 | } |
945 | } |
764 | |
946 | |
765 | int main (void) |
947 | int main (void) |
766 | { |
948 | { |
767 | struct ev_io sin; |
|
|
768 | |
|
|
769 | ev_init (0); |
949 | ev_init (0); |
770 | |
950 | |
771 | evw_init (&sin, sin_cb, 55); |
|
|
772 | evio_set (&sin, 0, EV_READ); |
951 | evio_init (&wio, sin_cb, 0, EV_READ); |
773 | evio_start (&sin); |
952 | evio_start (&wio); |
774 | |
953 | |
775 | struct ev_timer t[10000]; |
954 | struct ev_timer t[10000]; |
776 | |
955 | |
777 | #if 0 |
956 | #if 0 |
778 | int i; |
957 | int i; |
779 | for (i = 0; i < 10000; ++i) |
958 | for (i = 0; i < 10000; ++i) |
780 | { |
959 | { |
781 | struct ev_timer *w = t + i; |
960 | struct ev_timer *w = t + i; |
782 | evw_init (w, ocb, i); |
961 | evw_init (w, ocb, i); |
783 | evtimer_set_abs (w, drand48 (), 0.99775533); |
962 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
784 | evtimer_start (w); |
963 | evtimer_start (w); |
785 | if (drand48 () < 0.5) |
964 | if (drand48 () < 0.5) |
786 | evtimer_stop (w); |
965 | evtimer_stop (w); |
787 | } |
966 | } |
788 | #endif |
967 | #endif |
789 | |
968 | |
790 | struct ev_timer t1; |
969 | struct ev_timer t1; |
791 | evw_init (&t1, ocb, 0); |
970 | evtimer_init (&t1, ocb, 5, 10); |
792 | evtimer_set_abs (&t1, 5, 10); |
|
|
793 | evtimer_start (&t1); |
971 | evtimer_start (&t1); |
794 | |
972 | |
795 | struct ev_signal sig; |
973 | struct ev_signal sig; |
796 | evw_init (&sig, scb, 65535); |
|
|
797 | evsignal_set (&sig, SIGQUIT); |
974 | evsignal_init (&sig, scb, SIGQUIT); |
798 | evsignal_start (&sig); |
975 | evsignal_start (&sig); |
799 | |
976 | |
800 | struct ev_check cw; |
977 | struct ev_check cw; |
801 | evw_init (&cw, gcb, 0); |
978 | evcheck_init (&cw, gcb); |
802 | evcheck_start (&cw); |
979 | evcheck_start (&cw); |
803 | |
980 | |
804 | struct ev_idle iw; |
981 | struct ev_idle iw; |
805 | evw_init (&iw, gcb, 0); |
982 | evidle_init (&iw, gcb); |
806 | evidle_start (&iw); |
983 | evidle_start (&iw); |
807 | |
984 | |
808 | ev_loop (0); |
985 | ev_loop (0); |
809 | |
986 | |
810 | return 0; |
987 | return 0; |