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