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