|
|
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> |
… | |
… | |
32 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
62 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
33 | #define MAX_BLOCKTIME 60. |
63 | #define MAX_BLOCKTIME 60. |
34 | |
64 | |
35 | #include "ev.h" |
65 | #include "ev.h" |
36 | |
66 | |
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; |
67 | typedef struct ev_watcher *W; |
46 | typedef struct ev_watcher_list *WL; |
68 | typedef struct ev_watcher_list *WL; |
|
|
69 | typedef struct ev_watcher_time *WT; |
47 | |
70 | |
48 | static ev_tstamp now, diff; /* monotonic clock */ |
71 | static ev_tstamp now, diff; /* monotonic clock */ |
49 | ev_tstamp ev_now; |
72 | ev_tstamp ev_now; |
50 | int ev_method; |
73 | int ev_method; |
51 | |
74 | |
… | |
… | |
88 | |
111 | |
89 | #define array_needsize(base,cur,cnt,init) \ |
112 | #define array_needsize(base,cur,cnt,init) \ |
90 | if ((cnt) > cur) \ |
113 | if ((cnt) > cur) \ |
91 | { \ |
114 | { \ |
92 | int newcnt = cur ? cur << 1 : 16; \ |
115 | 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)); \ |
116 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
95 | init (base + cur, newcnt - cur); \ |
117 | init (base + cur, newcnt - cur); \ |
96 | cur = newcnt; \ |
118 | cur = newcnt; \ |
97 | } |
119 | } |
98 | |
120 | |
… | |
… | |
131 | static int pendingmax, pendingcnt; |
153 | static int pendingmax, pendingcnt; |
132 | |
154 | |
133 | static void |
155 | static void |
134 | event (W w, int events) |
156 | event (W w, int events) |
135 | { |
157 | { |
|
|
158 | if (w->active) |
|
|
159 | { |
136 | w->pending = ++pendingcnt; |
160 | w->pending = ++pendingcnt; |
137 | array_needsize (pendings, pendingmax, pendingcnt, ); |
161 | array_needsize (pendings, pendingmax, pendingcnt, ); |
138 | pendings [pendingcnt - 1].w = w; |
162 | pendings [pendingcnt - 1].w = w; |
139 | pendings [pendingcnt - 1].events = events; |
163 | pendings [pendingcnt - 1].events = events; |
|
|
164 | } |
140 | } |
165 | } |
141 | |
166 | |
142 | static void |
167 | static void |
143 | fd_event (int fd, int events) |
168 | fd_event (int fd, int events) |
144 | { |
169 | { |
… | |
… | |
161 | |
186 | |
162 | for (i = 0; i < eventcnt; ++i) |
187 | for (i = 0; i < eventcnt; ++i) |
163 | event (events [i], type); |
188 | event (events [i], type); |
164 | } |
189 | } |
165 | |
190 | |
166 | /*****************************************************************************/ |
191 | /* called on EBADF to verify fds */ |
|
|
192 | static void |
|
|
193 | fd_recheck () |
|
|
194 | { |
|
|
195 | int fd; |
167 | |
196 | |
|
|
197 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
198 | if (anfds [fd].wev) |
|
|
199 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
200 | while (anfds [fd].head) |
|
|
201 | evio_stop (anfds [fd].head); |
|
|
202 | } |
|
|
203 | |
|
|
204 | /*****************************************************************************/ |
|
|
205 | |
168 | static struct ev_timer **atimers; |
206 | static struct ev_timer **timers; |
169 | static int atimermax, atimercnt; |
207 | static int timermax, timercnt; |
170 | |
208 | |
171 | static struct ev_timer **rtimers; |
209 | static struct ev_periodic **periodics; |
172 | static int rtimermax, rtimercnt; |
210 | static int periodicmax, periodiccnt; |
173 | |
211 | |
174 | static void |
212 | static void |
175 | upheap (struct ev_timer **timers, int k) |
213 | upheap (WT *timers, int k) |
176 | { |
214 | { |
177 | struct ev_timer *w = timers [k]; |
215 | WT w = timers [k]; |
178 | |
216 | |
179 | while (k && timers [k >> 1]->at > w->at) |
217 | while (k && timers [k >> 1]->at > w->at) |
180 | { |
218 | { |
181 | timers [k] = timers [k >> 1]; |
219 | timers [k] = timers [k >> 1]; |
182 | timers [k]->active = k + 1; |
220 | timers [k]->active = k + 1; |
… | |
… | |
187 | timers [k]->active = k + 1; |
225 | timers [k]->active = k + 1; |
188 | |
226 | |
189 | } |
227 | } |
190 | |
228 | |
191 | static void |
229 | static void |
192 | downheap (struct ev_timer **timers, int N, int k) |
230 | downheap (WT *timers, int N, int k) |
193 | { |
231 | { |
194 | struct ev_timer *w = timers [k]; |
232 | WT w = timers [k]; |
195 | |
233 | |
196 | while (k < (N >> 1)) |
234 | while (k < (N >> 1)) |
197 | { |
235 | { |
198 | int j = k << 1; |
236 | int j = k << 1; |
199 | |
237 | |
… | |
… | |
325 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
363 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
326 | #endif |
364 | #endif |
327 | |
365 | |
328 | if (ev_method) |
366 | if (ev_method) |
329 | { |
367 | { |
330 | evw_init (&sigev, sigcb, 0); |
368 | evw_init (&sigev, sigcb); |
331 | siginit (); |
369 | siginit (); |
332 | } |
370 | } |
333 | |
371 | |
334 | return ev_method; |
372 | return ev_method; |
335 | } |
373 | } |
336 | |
374 | |
337 | /*****************************************************************************/ |
375 | /*****************************************************************************/ |
338 | |
376 | |
339 | void ev_prefork (void) |
377 | void ev_prefork (void) |
340 | { |
378 | { |
|
|
379 | /* nop */ |
341 | } |
380 | } |
342 | |
381 | |
343 | void ev_postfork_parent (void) |
382 | void ev_postfork_parent (void) |
344 | { |
383 | { |
|
|
384 | /* nop */ |
345 | } |
385 | } |
346 | |
386 | |
347 | void ev_postfork_child (void) |
387 | void ev_postfork_child (void) |
348 | { |
388 | { |
349 | #if HAVE_EPOLL |
389 | #if HAVE_EPOLL |
… | |
… | |
387 | } |
427 | } |
388 | |
428 | |
389 | static void |
429 | static void |
390 | call_pending () |
430 | call_pending () |
391 | { |
431 | { |
392 | int i; |
432 | while (pendingcnt) |
393 | |
|
|
394 | for (i = 0; i < pendingcnt; ++i) |
|
|
395 | { |
433 | { |
396 | ANPENDING *p = pendings + i; |
434 | ANPENDING *p = pendings + --pendingcnt; |
397 | |
435 | |
398 | if (p->w) |
436 | if (p->w) |
399 | { |
437 | { |
400 | p->w->pending = 0; |
438 | p->w->pending = 0; |
401 | p->w->cb (p->w, p->events); |
439 | p->w->cb (p->w, p->events); |
402 | } |
440 | } |
403 | } |
441 | } |
404 | |
|
|
405 | pendingcnt = 0; |
|
|
406 | } |
442 | } |
407 | |
443 | |
408 | static void |
444 | static void |
409 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
445 | timers_reify () |
410 | { |
446 | { |
411 | while (timercnt && timers [0]->at <= now) |
447 | while (timercnt && timers [0]->at <= now) |
412 | { |
448 | { |
413 | struct ev_timer *w = timers [0]; |
449 | struct ev_timer *w = timers [0]; |
|
|
450 | |
|
|
451 | event ((W)w, EV_TIMEOUT); |
414 | |
452 | |
415 | /* first reschedule or stop timer */ |
453 | /* first reschedule or stop timer */ |
416 | if (w->repeat) |
454 | if (w->repeat) |
417 | { |
455 | { |
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; |
456 | w->at = now + w->repeat; |
422 | |
457 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
423 | assert (w->at > now); |
|
|
424 | |
|
|
425 | downheap (timers, timercnt, 0); |
458 | downheap ((WT *)timers, timercnt, 0); |
426 | } |
459 | } |
427 | else |
460 | else |
428 | { |
|
|
429 | evtimer_stop (w); /* nonrepeating: stop timer */ |
461 | evtimer_stop (w); /* nonrepeating: stop timer */ |
430 | --timercnt; /* maybe pass by reference instead? */ |
462 | } |
|
|
463 | } |
|
|
464 | |
|
|
465 | static void |
|
|
466 | periodics_reify () |
|
|
467 | { |
|
|
468 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
469 | { |
|
|
470 | struct ev_periodic *w = periodics [0]; |
|
|
471 | |
|
|
472 | /* first reschedule or stop timer */ |
|
|
473 | if (w->interval) |
431 | } |
474 | { |
|
|
475 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
476 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
477 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
478 | } |
|
|
479 | else |
|
|
480 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
432 | |
481 | |
433 | event ((W)w, EV_TIMEOUT); |
482 | event ((W)w, EV_TIMEOUT); |
434 | } |
483 | } |
435 | } |
484 | } |
436 | |
485 | |
437 | static void |
486 | static void |
|
|
487 | periodics_reschedule (ev_tstamp diff) |
|
|
488 | { |
|
|
489 | int i; |
|
|
490 | |
|
|
491 | /* adjust periodics after time jump */ |
|
|
492 | for (i = 0; i < periodiccnt; ++i) |
|
|
493 | { |
|
|
494 | struct ev_periodic *w = periodics [i]; |
|
|
495 | |
|
|
496 | if (w->interval) |
|
|
497 | { |
|
|
498 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
499 | |
|
|
500 | if (fabs (diff) >= 1e-4) |
|
|
501 | { |
|
|
502 | evperiodic_stop (w); |
|
|
503 | evperiodic_start (w); |
|
|
504 | |
|
|
505 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
506 | } |
|
|
507 | } |
|
|
508 | } |
|
|
509 | } |
|
|
510 | |
|
|
511 | static void |
438 | time_update () |
512 | time_update () |
439 | { |
513 | { |
440 | int i; |
514 | int i; |
|
|
515 | |
441 | ev_now = ev_time (); |
516 | ev_now = ev_time (); |
442 | |
517 | |
443 | if (have_monotonic) |
518 | if (have_monotonic) |
444 | { |
519 | { |
445 | ev_tstamp odiff = diff; |
520 | ev_tstamp odiff = diff; |
446 | |
521 | |
447 | /* detecting time jumps is much more difficult */ |
|
|
448 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
522 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
449 | { |
523 | { |
450 | now = get_clock (); |
524 | now = get_clock (); |
451 | diff = ev_now - now; |
525 | diff = ev_now - now; |
452 | |
526 | |
453 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
527 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
454 | return; /* all is well */ |
528 | return; /* all is well */ |
455 | |
529 | |
456 | ev_now = ev_time (); |
530 | ev_now = ev_time (); |
457 | } |
531 | } |
458 | |
532 | |
459 | /* time jump detected, reschedule atimers */ |
533 | periodics_reschedule (diff - odiff); |
460 | for (i = 0; i < atimercnt; ++i) |
534 | /* 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 | } |
535 | } |
466 | else |
536 | else |
467 | { |
537 | { |
468 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
538 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
469 | /* time jump detected, adjust rtimers */ |
539 | { |
|
|
540 | periodics_reschedule (ev_now - now); |
|
|
541 | |
|
|
542 | /* adjust timers. this is easy, as the offset is the same for all */ |
470 | for (i = 0; i < rtimercnt; ++i) |
543 | for (i = 0; i < timercnt; ++i) |
471 | rtimers [i]->at += ev_now - now; |
544 | timers [i]->at += diff; |
|
|
545 | } |
472 | |
546 | |
473 | now = ev_now; |
547 | now = ev_now; |
474 | } |
548 | } |
475 | } |
549 | } |
476 | |
550 | |
477 | int ev_loop_done; |
551 | int ev_loop_done; |
478 | |
552 | |
479 | void ev_loop (int flags) |
553 | void ev_loop (int flags) |
480 | { |
554 | { |
481 | double block; |
555 | double block; |
482 | ev_loop_done = flags & EVLOOP_ONESHOT; |
556 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
483 | |
557 | |
484 | if (checkcnt) |
558 | if (checkcnt) |
485 | { |
559 | { |
486 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
560 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
487 | call_pending (); |
561 | call_pending (); |
… | |
… | |
491 | { |
565 | { |
492 | /* update fd-related kernel structures */ |
566 | /* update fd-related kernel structures */ |
493 | fd_reify (); |
567 | fd_reify (); |
494 | |
568 | |
495 | /* calculate blocking time */ |
569 | /* calculate blocking time */ |
|
|
570 | |
|
|
571 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
|
|
572 | ev_now = ev_time (); |
|
|
573 | |
496 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
574 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
497 | block = 0.; |
575 | block = 0.; |
498 | else |
576 | else |
499 | { |
577 | { |
500 | block = MAX_BLOCKTIME; |
578 | block = MAX_BLOCKTIME; |
501 | |
579 | |
502 | if (rtimercnt) |
580 | if (timercnt) |
503 | { |
581 | { |
504 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
582 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
505 | if (block > to) block = to; |
583 | if (block > to) block = to; |
506 | } |
584 | } |
507 | |
585 | |
508 | if (atimercnt) |
586 | if (periodiccnt) |
509 | { |
587 | { |
510 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
588 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
511 | if (block > to) block = to; |
589 | if (block > to) block = to; |
512 | } |
590 | } |
513 | |
591 | |
514 | if (block < 0.) block = 0.; |
592 | if (block < 0.) block = 0.; |
515 | } |
593 | } |
… | |
… | |
518 | |
596 | |
519 | /* update ev_now, do magic */ |
597 | /* update ev_now, do magic */ |
520 | time_update (); |
598 | time_update (); |
521 | |
599 | |
522 | /* queue pending timers and reschedule them */ |
600 | /* queue pending timers and reschedule them */ |
523 | /* absolute timers first */ |
601 | periodics_reify (); /* absolute timers first */ |
524 | timers_reify (atimers, atimercnt, ev_now); |
|
|
525 | /* relative timers second */ |
602 | timers_reify (); /* relative timers second */ |
526 | timers_reify (rtimers, rtimercnt, now); |
|
|
527 | |
603 | |
528 | /* queue idle watchers unless io or timers are pending */ |
604 | /* queue idle watchers unless io or timers are pending */ |
529 | if (!pendingcnt) |
605 | if (!pendingcnt) |
530 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
606 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
531 | |
607 | |
… | |
… | |
533 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
609 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
534 | |
610 | |
535 | call_pending (); |
611 | call_pending (); |
536 | } |
612 | } |
537 | while (!ev_loop_done); |
613 | while (!ev_loop_done); |
|
|
614 | |
|
|
615 | if (ev_loop_done != 2) |
|
|
616 | ev_loop_done = 0; |
538 | } |
617 | } |
539 | |
618 | |
540 | /*****************************************************************************/ |
619 | /*****************************************************************************/ |
541 | |
620 | |
542 | static void |
621 | static void |
… | |
… | |
560 | head = &(*head)->next; |
639 | head = &(*head)->next; |
561 | } |
640 | } |
562 | } |
641 | } |
563 | |
642 | |
564 | static void |
643 | static void |
|
|
644 | ev_clear (W w) |
|
|
645 | { |
|
|
646 | if (w->pending) |
|
|
647 | { |
|
|
648 | pendings [w->pending - 1].w = 0; |
|
|
649 | w->pending = 0; |
|
|
650 | } |
|
|
651 | } |
|
|
652 | |
|
|
653 | static void |
565 | ev_start (W w, int active) |
654 | ev_start (W w, int active) |
566 | { |
655 | { |
567 | w->pending = 0; |
|
|
568 | w->active = active; |
656 | w->active = active; |
569 | } |
657 | } |
570 | |
658 | |
571 | static void |
659 | static void |
572 | ev_stop (W w) |
660 | ev_stop (W w) |
573 | { |
661 | { |
574 | if (w->pending) |
|
|
575 | pendings [w->pending - 1].w = 0; |
|
|
576 | |
|
|
577 | w->active = 0; |
662 | w->active = 0; |
578 | /* nop */ |
|
|
579 | } |
663 | } |
580 | |
664 | |
581 | /*****************************************************************************/ |
665 | /*****************************************************************************/ |
582 | |
666 | |
583 | void |
667 | void |
… | |
… | |
598 | } |
682 | } |
599 | |
683 | |
600 | void |
684 | void |
601 | evio_stop (struct ev_io *w) |
685 | evio_stop (struct ev_io *w) |
602 | { |
686 | { |
|
|
687 | ev_clear ((W)w); |
603 | if (!ev_is_active (w)) |
688 | if (!ev_is_active (w)) |
604 | return; |
689 | return; |
605 | |
690 | |
606 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
691 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
607 | ev_stop ((W)w); |
692 | ev_stop ((W)w); |
… | |
… | |
615 | evtimer_start (struct ev_timer *w) |
700 | evtimer_start (struct ev_timer *w) |
616 | { |
701 | { |
617 | if (ev_is_active (w)) |
702 | if (ev_is_active (w)) |
618 | return; |
703 | return; |
619 | |
704 | |
620 | if (w->is_abs) |
705 | 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 | |
706 | |
|
|
707 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
708 | |
626 | ev_start ((W)w, ++atimercnt); |
709 | ev_start ((W)w, ++timercnt); |
627 | array_needsize (atimers, atimermax, atimercnt, ); |
710 | array_needsize (timers, timermax, timercnt, ); |
628 | atimers [atimercnt - 1] = w; |
711 | timers [timercnt - 1] = w; |
629 | upheap (atimers, atimercnt - 1); |
712 | 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 | } |
713 | } |
642 | |
714 | |
643 | void |
715 | void |
644 | evtimer_stop (struct ev_timer *w) |
716 | evtimer_stop (struct ev_timer *w) |
645 | { |
717 | { |
|
|
718 | ev_clear ((W)w); |
646 | if (!ev_is_active (w)) |
719 | if (!ev_is_active (w)) |
647 | return; |
720 | return; |
648 | |
721 | |
649 | if (w->is_abs) |
|
|
650 | { |
|
|
651 | if (w->active < atimercnt--) |
722 | if (w->active < timercnt--) |
652 | { |
723 | { |
653 | atimers [w->active - 1] = atimers [atimercnt]; |
724 | timers [w->active - 1] = timers [timercnt]; |
|
|
725 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
726 | } |
|
|
727 | |
|
|
728 | w->at = w->repeat; |
|
|
729 | |
|
|
730 | ev_stop ((W)w); |
|
|
731 | } |
|
|
732 | |
|
|
733 | void |
|
|
734 | evtimer_again (struct ev_timer *w) |
|
|
735 | { |
|
|
736 | if (ev_is_active (w)) |
|
|
737 | { |
|
|
738 | if (w->repeat) |
|
|
739 | { |
|
|
740 | w->at = now + w->repeat; |
654 | downheap (atimers, atimercnt, w->active - 1); |
741 | downheap ((WT *)timers, timercnt, w->active - 1); |
655 | } |
|
|
656 | } |
|
|
657 | else |
|
|
658 | { |
|
|
659 | if (w->active < rtimercnt--) |
|
|
660 | { |
742 | } |
661 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
743 | else |
662 | downheap (rtimers, rtimercnt, w->active - 1); |
744 | evtimer_stop (w); |
663 | } |
745 | } |
|
|
746 | else if (w->repeat) |
|
|
747 | evtimer_start (w); |
|
|
748 | } |
|
|
749 | |
|
|
750 | void |
|
|
751 | evperiodic_start (struct ev_periodic *w) |
|
|
752 | { |
|
|
753 | if (ev_is_active (w)) |
|
|
754 | return; |
|
|
755 | |
|
|
756 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
757 | |
|
|
758 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
759 | if (w->interval) |
|
|
760 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
761 | |
|
|
762 | ev_start ((W)w, ++periodiccnt); |
|
|
763 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
764 | periodics [periodiccnt - 1] = w; |
|
|
765 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
766 | } |
|
|
767 | |
|
|
768 | void |
|
|
769 | evperiodic_stop (struct ev_periodic *w) |
|
|
770 | { |
|
|
771 | ev_clear ((W)w); |
|
|
772 | if (!ev_is_active (w)) |
|
|
773 | return; |
|
|
774 | |
|
|
775 | if (w->active < periodiccnt--) |
|
|
776 | { |
|
|
777 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
778 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
664 | } |
779 | } |
665 | |
780 | |
666 | ev_stop ((W)w); |
781 | ev_stop ((W)w); |
667 | } |
782 | } |
668 | |
783 | |
… | |
… | |
687 | } |
802 | } |
688 | |
803 | |
689 | void |
804 | void |
690 | evsignal_stop (struct ev_signal *w) |
805 | evsignal_stop (struct ev_signal *w) |
691 | { |
806 | { |
|
|
807 | ev_clear ((W)w); |
692 | if (!ev_is_active (w)) |
808 | if (!ev_is_active (w)) |
693 | return; |
809 | return; |
694 | |
810 | |
695 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
811 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
696 | ev_stop ((W)w); |
812 | ev_stop ((W)w); |
… | |
… | |
709 | idles [idlecnt - 1] = w; |
825 | idles [idlecnt - 1] = w; |
710 | } |
826 | } |
711 | |
827 | |
712 | void evidle_stop (struct ev_idle *w) |
828 | void evidle_stop (struct ev_idle *w) |
713 | { |
829 | { |
|
|
830 | ev_clear ((W)w); |
|
|
831 | if (ev_is_active (w)) |
|
|
832 | return; |
|
|
833 | |
714 | idles [w->active - 1] = idles [--idlecnt]; |
834 | idles [w->active - 1] = idles [--idlecnt]; |
715 | ev_stop ((W)w); |
835 | ev_stop ((W)w); |
716 | } |
836 | } |
717 | |
837 | |
718 | void evcheck_start (struct ev_check *w) |
838 | void evcheck_start (struct ev_check *w) |
… | |
… | |
725 | checks [checkcnt - 1] = w; |
845 | checks [checkcnt - 1] = w; |
726 | } |
846 | } |
727 | |
847 | |
728 | void evcheck_stop (struct ev_check *w) |
848 | void evcheck_stop (struct ev_check *w) |
729 | { |
849 | { |
|
|
850 | ev_clear ((W)w); |
|
|
851 | if (ev_is_active (w)) |
|
|
852 | return; |
|
|
853 | |
730 | checks [w->active - 1] = checks [--checkcnt]; |
854 | checks [w->active - 1] = checks [--checkcnt]; |
731 | ev_stop ((W)w); |
855 | ev_stop ((W)w); |
732 | } |
856 | } |
733 | |
857 | |
734 | /*****************************************************************************/ |
858 | /*****************************************************************************/ |
735 | |
859 | |
|
|
860 | struct ev_once |
|
|
861 | { |
|
|
862 | struct ev_io io; |
|
|
863 | struct ev_timer to; |
|
|
864 | void (*cb)(int revents, void *arg); |
|
|
865 | void *arg; |
|
|
866 | }; |
|
|
867 | |
|
|
868 | static void |
|
|
869 | once_cb (struct ev_once *once, int revents) |
|
|
870 | { |
|
|
871 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
872 | void *arg = once->arg; |
|
|
873 | |
|
|
874 | evio_stop (&once->io); |
|
|
875 | evtimer_stop (&once->to); |
|
|
876 | free (once); |
|
|
877 | |
|
|
878 | cb (revents, arg); |
|
|
879 | } |
|
|
880 | |
|
|
881 | static void |
|
|
882 | once_cb_io (struct ev_io *w, int revents) |
|
|
883 | { |
|
|
884 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
885 | } |
|
|
886 | |
|
|
887 | static void |
|
|
888 | once_cb_to (struct ev_timer *w, int revents) |
|
|
889 | { |
|
|
890 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
891 | } |
|
|
892 | |
|
|
893 | void |
|
|
894 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
895 | { |
|
|
896 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
897 | |
|
|
898 | if (!once) |
|
|
899 | cb (EV_ERROR, arg); |
|
|
900 | else |
|
|
901 | { |
|
|
902 | once->cb = cb; |
|
|
903 | once->arg = arg; |
|
|
904 | |
|
|
905 | evw_init (&once->io, once_cb_io); |
|
|
906 | |
|
|
907 | if (fd >= 0) |
|
|
908 | { |
|
|
909 | evio_set (&once->io, fd, events); |
|
|
910 | evio_start (&once->io); |
|
|
911 | } |
|
|
912 | |
|
|
913 | evw_init (&once->to, once_cb_to); |
|
|
914 | |
|
|
915 | if (timeout >= 0.) |
|
|
916 | { |
|
|
917 | evtimer_set (&once->to, timeout, 0.); |
|
|
918 | evtimer_start (&once->to); |
|
|
919 | } |
|
|
920 | } |
|
|
921 | } |
|
|
922 | |
|
|
923 | /*****************************************************************************/ |
|
|
924 | |
736 | #if 0 |
925 | #if 0 |
|
|
926 | |
|
|
927 | struct ev_io wio; |
737 | |
928 | |
738 | static void |
929 | static void |
739 | sin_cb (struct ev_io *w, int revents) |
930 | sin_cb (struct ev_io *w, int revents) |
740 | { |
931 | { |
741 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
932 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
751 | |
942 | |
752 | static void |
943 | static void |
753 | scb (struct ev_signal *w, int revents) |
944 | scb (struct ev_signal *w, int revents) |
754 | { |
945 | { |
755 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
946 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
947 | evio_stop (&wio); |
|
|
948 | evio_start (&wio); |
756 | } |
949 | } |
757 | |
950 | |
758 | static void |
951 | static void |
759 | gcb (struct ev_signal *w, int revents) |
952 | gcb (struct ev_signal *w, int revents) |
760 | { |
953 | { |
761 | fprintf (stderr, "generic %x\n", revents); |
954 | fprintf (stderr, "generic %x\n", revents); |
|
|
955 | |
762 | } |
956 | } |
763 | |
957 | |
764 | int main (void) |
958 | int main (void) |
765 | { |
959 | { |
766 | struct ev_io sin; |
|
|
767 | |
|
|
768 | ev_init (0); |
960 | ev_init (0); |
769 | |
961 | |
770 | evw_init (&sin, sin_cb, 55); |
|
|
771 | evio_set (&sin, 0, EV_READ); |
962 | evio_init (&wio, sin_cb, 0, EV_READ); |
772 | evio_start (&sin); |
963 | evio_start (&wio); |
773 | |
964 | |
774 | struct ev_timer t[10000]; |
965 | struct ev_timer t[10000]; |
775 | |
966 | |
776 | #if 0 |
967 | #if 0 |
777 | int i; |
968 | int i; |
778 | for (i = 0; i < 10000; ++i) |
969 | for (i = 0; i < 10000; ++i) |
779 | { |
970 | { |
780 | struct ev_timer *w = t + i; |
971 | struct ev_timer *w = t + i; |
781 | evw_init (w, ocb, i); |
972 | evw_init (w, ocb, i); |
782 | evtimer_set_abs (w, drand48 (), 0.99775533); |
973 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
783 | evtimer_start (w); |
974 | evtimer_start (w); |
784 | if (drand48 () < 0.5) |
975 | if (drand48 () < 0.5) |
785 | evtimer_stop (w); |
976 | evtimer_stop (w); |
786 | } |
977 | } |
787 | #endif |
978 | #endif |
788 | |
979 | |
789 | struct ev_timer t1; |
980 | struct ev_timer t1; |
790 | evw_init (&t1, ocb, 0); |
981 | evtimer_init (&t1, ocb, 5, 10); |
791 | evtimer_set_abs (&t1, 5, 10); |
|
|
792 | evtimer_start (&t1); |
982 | evtimer_start (&t1); |
793 | |
983 | |
794 | struct ev_signal sig; |
984 | struct ev_signal sig; |
795 | evw_init (&sig, scb, 65535); |
|
|
796 | evsignal_set (&sig, SIGQUIT); |
985 | evsignal_init (&sig, scb, SIGQUIT); |
797 | evsignal_start (&sig); |
986 | evsignal_start (&sig); |
798 | |
987 | |
799 | struct ev_check cw; |
988 | struct ev_check cw; |
800 | evw_init (&cw, gcb, 0); |
989 | evcheck_init (&cw, gcb); |
801 | evcheck_start (&cw); |
990 | evcheck_start (&cw); |
802 | |
991 | |
803 | struct ev_idle iw; |
992 | struct ev_idle iw; |
804 | evw_init (&iw, gcb, 0); |
993 | evidle_init (&iw, gcb); |
805 | evidle_start (&iw); |
994 | evidle_start (&iw); |
806 | |
995 | |
807 | ev_loop (0); |
996 | ev_loop (0); |
808 | |
997 | |
809 | return 0; |
998 | return 0; |