|
|
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 |
… | |
… | |
387 | } |
416 | } |
388 | |
417 | |
389 | static void |
418 | static void |
390 | call_pending () |
419 | call_pending () |
391 | { |
420 | { |
392 | int i; |
421 | while (pendingcnt) |
393 | |
|
|
394 | for (i = 0; i < pendingcnt; ++i) |
|
|
395 | { |
422 | { |
396 | ANPENDING *p = pendings + i; |
423 | ANPENDING *p = pendings + --pendingcnt; |
397 | |
424 | |
398 | if (p->w) |
425 | if (p->w) |
399 | { |
426 | { |
400 | p->w->pending = 0; |
427 | p->w->pending = 0; |
401 | p->w->cb (p->w, p->events); |
428 | p->w->cb (p->w, p->events); |
402 | } |
429 | } |
403 | } |
430 | } |
404 | |
|
|
405 | pendingcnt = 0; |
|
|
406 | } |
431 | } |
407 | |
432 | |
408 | static void |
433 | static void |
409 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
434 | timers_reify () |
410 | { |
435 | { |
411 | while (timercnt && timers [0]->at <= now) |
436 | while (timercnt && timers [0]->at <= now) |
412 | { |
437 | { |
413 | struct ev_timer *w = timers [0]; |
438 | struct ev_timer *w = timers [0]; |
|
|
439 | |
|
|
440 | event ((W)w, EV_TIMEOUT); |
414 | |
441 | |
415 | /* first reschedule or stop timer */ |
442 | /* first reschedule or stop timer */ |
416 | if (w->repeat) |
443 | if (w->repeat) |
417 | { |
444 | { |
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; |
445 | w->at = now + w->repeat; |
422 | |
446 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
423 | assert (w->at > now); |
|
|
424 | |
|
|
425 | downheap (timers, timercnt, 0); |
447 | downheap ((WT *)timers, timercnt, 0); |
426 | } |
448 | } |
427 | else |
449 | else |
428 | { |
|
|
429 | evtimer_stop (w); /* nonrepeating: stop timer */ |
450 | evtimer_stop (w); /* nonrepeating: stop timer */ |
430 | --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) |
431 | } |
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 */ |
432 | |
470 | |
433 | event ((W)w, EV_TIMEOUT); |
471 | event ((W)w, EV_TIMEOUT); |
434 | } |
472 | } |
435 | } |
473 | } |
436 | |
474 | |
437 | 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 |
438 | time_update () |
501 | time_update () |
439 | { |
502 | { |
440 | int i; |
503 | int i; |
|
|
504 | |
441 | ev_now = ev_time (); |
505 | ev_now = ev_time (); |
442 | |
506 | |
443 | if (have_monotonic) |
507 | if (have_monotonic) |
444 | { |
508 | { |
445 | ev_tstamp odiff = diff; |
509 | ev_tstamp odiff = diff; |
446 | |
510 | |
447 | /* detecting time jumps is much more difficult */ |
|
|
448 | 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 */ |
449 | { |
512 | { |
450 | now = get_clock (); |
513 | now = get_clock (); |
451 | diff = ev_now - now; |
514 | diff = ev_now - now; |
452 | |
515 | |
453 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
516 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
454 | return; /* all is well */ |
517 | return; /* all is well */ |
455 | |
518 | |
456 | ev_now = ev_time (); |
519 | ev_now = ev_time (); |
457 | } |
520 | } |
458 | |
521 | |
459 | /* time jump detected, reschedule atimers */ |
522 | periodics_reschedule (diff - odiff); |
460 | for (i = 0; i < atimercnt; ++i) |
523 | /* 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 | } |
524 | } |
466 | else |
525 | else |
467 | { |
526 | { |
468 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
527 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
469 | /* 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 */ |
470 | for (i = 0; i < rtimercnt; ++i) |
532 | for (i = 0; i < timercnt; ++i) |
471 | rtimers [i]->at += ev_now - now; |
533 | timers [i]->at += diff; |
|
|
534 | } |
472 | |
535 | |
473 | now = ev_now; |
536 | now = ev_now; |
474 | } |
537 | } |
475 | } |
538 | } |
476 | |
539 | |
477 | int ev_loop_done; |
540 | int ev_loop_done; |
478 | |
541 | |
479 | void ev_loop (int flags) |
542 | void ev_loop (int flags) |
480 | { |
543 | { |
481 | double block; |
544 | double block; |
482 | ev_loop_done = flags & EVLOOP_ONESHOT; |
545 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
483 | |
546 | |
484 | if (checkcnt) |
547 | if (checkcnt) |
485 | { |
548 | { |
486 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
549 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
487 | call_pending (); |
550 | call_pending (); |
… | |
… | |
491 | { |
554 | { |
492 | /* update fd-related kernel structures */ |
555 | /* update fd-related kernel structures */ |
493 | fd_reify (); |
556 | fd_reify (); |
494 | |
557 | |
495 | /* 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 | |
496 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
563 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
497 | block = 0.; |
564 | block = 0.; |
498 | else |
565 | else |
499 | { |
566 | { |
500 | block = MAX_BLOCKTIME; |
567 | block = MAX_BLOCKTIME; |
501 | |
568 | |
502 | if (rtimercnt) |
569 | if (timercnt) |
503 | { |
570 | { |
504 | 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; |
505 | if (block > to) block = to; |
572 | if (block > to) block = to; |
506 | } |
573 | } |
507 | |
574 | |
508 | if (atimercnt) |
575 | if (periodiccnt) |
509 | { |
576 | { |
510 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
577 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
511 | if (block > to) block = to; |
578 | if (block > to) block = to; |
512 | } |
579 | } |
513 | |
580 | |
514 | if (block < 0.) block = 0.; |
581 | if (block < 0.) block = 0.; |
515 | } |
582 | } |
… | |
… | |
518 | |
585 | |
519 | /* update ev_now, do magic */ |
586 | /* update ev_now, do magic */ |
520 | time_update (); |
587 | time_update (); |
521 | |
588 | |
522 | /* queue pending timers and reschedule them */ |
589 | /* queue pending timers and reschedule them */ |
523 | /* absolute timers first */ |
590 | periodics_reify (); /* absolute timers first */ |
524 | timers_reify (atimers, atimercnt, ev_now); |
|
|
525 | /* relative timers second */ |
591 | timers_reify (); /* relative timers second */ |
526 | timers_reify (rtimers, rtimercnt, now); |
|
|
527 | |
592 | |
528 | /* queue idle watchers unless io or timers are pending */ |
593 | /* queue idle watchers unless io or timers are pending */ |
529 | if (!pendingcnt) |
594 | if (!pendingcnt) |
530 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
595 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
531 | |
596 | |
… | |
… | |
533 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
598 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
534 | |
599 | |
535 | call_pending (); |
600 | call_pending (); |
536 | } |
601 | } |
537 | while (!ev_loop_done); |
602 | while (!ev_loop_done); |
|
|
603 | |
|
|
604 | if (ev_loop_done != 2) |
|
|
605 | ev_loop_done = 0; |
538 | } |
606 | } |
539 | |
607 | |
540 | /*****************************************************************************/ |
608 | /*****************************************************************************/ |
541 | |
609 | |
542 | static void |
610 | static void |
… | |
… | |
560 | head = &(*head)->next; |
628 | head = &(*head)->next; |
561 | } |
629 | } |
562 | } |
630 | } |
563 | |
631 | |
564 | 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 |
565 | ev_start (W w, int active) |
643 | ev_start (W w, int active) |
566 | { |
644 | { |
567 | w->pending = 0; |
|
|
568 | w->active = active; |
645 | w->active = active; |
569 | } |
646 | } |
570 | |
647 | |
571 | static void |
648 | static void |
572 | ev_stop (W w) |
649 | ev_stop (W w) |
573 | { |
650 | { |
574 | if (w->pending) |
|
|
575 | pendings [w->pending - 1].w = 0; |
|
|
576 | |
|
|
577 | w->active = 0; |
651 | w->active = 0; |
578 | /* nop */ |
|
|
579 | } |
652 | } |
580 | |
653 | |
581 | /*****************************************************************************/ |
654 | /*****************************************************************************/ |
582 | |
655 | |
583 | void |
656 | void |
… | |
… | |
598 | } |
671 | } |
599 | |
672 | |
600 | void |
673 | void |
601 | evio_stop (struct ev_io *w) |
674 | evio_stop (struct ev_io *w) |
602 | { |
675 | { |
|
|
676 | ev_clear ((W)w); |
603 | if (!ev_is_active (w)) |
677 | if (!ev_is_active (w)) |
604 | return; |
678 | return; |
605 | |
679 | |
606 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
680 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
607 | ev_stop ((W)w); |
681 | ev_stop ((W)w); |
… | |
… | |
615 | evtimer_start (struct ev_timer *w) |
689 | evtimer_start (struct ev_timer *w) |
616 | { |
690 | { |
617 | if (ev_is_active (w)) |
691 | if (ev_is_active (w)) |
618 | return; |
692 | return; |
619 | |
693 | |
620 | if (w->is_abs) |
694 | 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 | |
695 | |
|
|
696 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
697 | |
626 | ev_start ((W)w, ++atimercnt); |
698 | ev_start ((W)w, ++timercnt); |
627 | array_needsize (atimers, atimermax, atimercnt, ); |
699 | array_needsize (timers, timermax, timercnt, ); |
628 | atimers [atimercnt - 1] = w; |
700 | timers [timercnt - 1] = w; |
629 | upheap (atimers, atimercnt - 1); |
701 | 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 | } |
702 | } |
642 | |
703 | |
643 | void |
704 | void |
644 | evtimer_stop (struct ev_timer *w) |
705 | evtimer_stop (struct ev_timer *w) |
645 | { |
706 | { |
|
|
707 | ev_clear ((W)w); |
646 | if (!ev_is_active (w)) |
708 | if (!ev_is_active (w)) |
647 | return; |
709 | return; |
648 | |
710 | |
649 | if (w->is_abs) |
|
|
650 | { |
|
|
651 | if (w->active < atimercnt--) |
711 | if (w->active < timercnt--) |
652 | { |
712 | { |
653 | 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; |
654 | downheap (atimers, atimercnt, w->active - 1); |
730 | downheap ((WT *)timers, timercnt, w->active - 1); |
655 | } |
|
|
656 | } |
|
|
657 | else |
|
|
658 | { |
|
|
659 | if (w->active < rtimercnt--) |
|
|
660 | { |
731 | } |
661 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
732 | else |
662 | downheap (rtimers, rtimercnt, w->active - 1); |
733 | evtimer_stop (w); |
663 | } |
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); |
664 | } |
768 | } |
665 | |
769 | |
666 | ev_stop ((W)w); |
770 | ev_stop ((W)w); |
667 | } |
771 | } |
668 | |
772 | |
… | |
… | |
687 | } |
791 | } |
688 | |
792 | |
689 | void |
793 | void |
690 | evsignal_stop (struct ev_signal *w) |
794 | evsignal_stop (struct ev_signal *w) |
691 | { |
795 | { |
|
|
796 | ev_clear ((W)w); |
692 | if (!ev_is_active (w)) |
797 | if (!ev_is_active (w)) |
693 | return; |
798 | return; |
694 | |
799 | |
695 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
800 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
696 | ev_stop ((W)w); |
801 | ev_stop ((W)w); |
… | |
… | |
709 | idles [idlecnt - 1] = w; |
814 | idles [idlecnt - 1] = w; |
710 | } |
815 | } |
711 | |
816 | |
712 | void evidle_stop (struct ev_idle *w) |
817 | void evidle_stop (struct ev_idle *w) |
713 | { |
818 | { |
|
|
819 | ev_clear ((W)w); |
|
|
820 | if (ev_is_active (w)) |
|
|
821 | return; |
|
|
822 | |
714 | idles [w->active - 1] = idles [--idlecnt]; |
823 | idles [w->active - 1] = idles [--idlecnt]; |
715 | ev_stop ((W)w); |
824 | ev_stop ((W)w); |
716 | } |
825 | } |
717 | |
826 | |
718 | void evcheck_start (struct ev_check *w) |
827 | void evcheck_start (struct ev_check *w) |
… | |
… | |
725 | checks [checkcnt - 1] = w; |
834 | checks [checkcnt - 1] = w; |
726 | } |
835 | } |
727 | |
836 | |
728 | void evcheck_stop (struct ev_check *w) |
837 | void evcheck_stop (struct ev_check *w) |
729 | { |
838 | { |
|
|
839 | ev_clear ((W)w); |
|
|
840 | if (ev_is_active (w)) |
|
|
841 | return; |
|
|
842 | |
730 | checks [w->active - 1] = checks [--checkcnt]; |
843 | checks [w->active - 1] = checks [--checkcnt]; |
731 | ev_stop ((W)w); |
844 | ev_stop ((W)w); |
732 | } |
845 | } |
733 | |
846 | |
734 | /*****************************************************************************/ |
847 | /*****************************************************************************/ |
735 | |
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 | |
736 | #if 0 |
914 | #if 0 |
|
|
915 | |
|
|
916 | struct ev_io wio; |
737 | |
917 | |
738 | static void |
918 | static void |
739 | sin_cb (struct ev_io *w, int revents) |
919 | sin_cb (struct ev_io *w, int revents) |
740 | { |
920 | { |
741 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
921 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
751 | |
931 | |
752 | static void |
932 | static void |
753 | scb (struct ev_signal *w, int revents) |
933 | scb (struct ev_signal *w, int revents) |
754 | { |
934 | { |
755 | 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); |
756 | } |
938 | } |
757 | |
939 | |
758 | static void |
940 | static void |
759 | gcb (struct ev_signal *w, int revents) |
941 | gcb (struct ev_signal *w, int revents) |
760 | { |
942 | { |
761 | fprintf (stderr, "generic %x\n", revents); |
943 | fprintf (stderr, "generic %x\n", revents); |
|
|
944 | |
762 | } |
945 | } |
763 | |
946 | |
764 | int main (void) |
947 | int main (void) |
765 | { |
948 | { |
766 | struct ev_io sin; |
|
|
767 | |
|
|
768 | ev_init (0); |
949 | ev_init (0); |
769 | |
950 | |
770 | evw_init (&sin, sin_cb, 55); |
|
|
771 | evio_set (&sin, 0, EV_READ); |
951 | evio_init (&wio, sin_cb, 0, EV_READ); |
772 | evio_start (&sin); |
952 | evio_start (&wio); |
773 | |
953 | |
774 | struct ev_timer t[10000]; |
954 | struct ev_timer t[10000]; |
775 | |
955 | |
776 | #if 0 |
956 | #if 0 |
777 | int i; |
957 | int i; |
778 | for (i = 0; i < 10000; ++i) |
958 | for (i = 0; i < 10000; ++i) |
779 | { |
959 | { |
780 | struct ev_timer *w = t + i; |
960 | struct ev_timer *w = t + i; |
781 | evw_init (w, ocb, i); |
961 | evw_init (w, ocb, i); |
782 | evtimer_set_abs (w, drand48 (), 0.99775533); |
962 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
783 | evtimer_start (w); |
963 | evtimer_start (w); |
784 | if (drand48 () < 0.5) |
964 | if (drand48 () < 0.5) |
785 | evtimer_stop (w); |
965 | evtimer_stop (w); |
786 | } |
966 | } |
787 | #endif |
967 | #endif |
788 | |
968 | |
789 | struct ev_timer t1; |
969 | struct ev_timer t1; |
790 | evw_init (&t1, ocb, 0); |
970 | evtimer_init (&t1, ocb, 5, 10); |
791 | evtimer_set_abs (&t1, 5, 10); |
|
|
792 | evtimer_start (&t1); |
971 | evtimer_start (&t1); |
793 | |
972 | |
794 | struct ev_signal sig; |
973 | struct ev_signal sig; |
795 | evw_init (&sig, scb, 65535); |
|
|
796 | evsignal_set (&sig, SIGQUIT); |
974 | evsignal_init (&sig, scb, SIGQUIT); |
797 | evsignal_start (&sig); |
975 | evsignal_start (&sig); |
798 | |
976 | |
799 | struct ev_check cw; |
977 | struct ev_check cw; |
800 | evw_init (&cw, gcb, 0); |
978 | evcheck_init (&cw, gcb); |
801 | evcheck_start (&cw); |
979 | evcheck_start (&cw); |
802 | |
980 | |
803 | struct ev_idle iw; |
981 | struct ev_idle iw; |
804 | evw_init (&iw, gcb, 0); |
982 | evidle_init (&iw, gcb); |
805 | evidle_start (&iw); |
983 | evidle_start (&iw); |
806 | |
984 | |
807 | ev_loop (0); |
985 | ev_loop (0); |
808 | |
986 | |
809 | return 0; |
987 | return 0; |