|
|
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> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
11 | #include <sys/time.h> |
43 | #include <sys/time.h> |
12 | #include <time.h> |
44 | #include <time.h> |
13 | |
|
|
14 | #define HAVE_EPOLL 1 |
|
|
15 | |
45 | |
16 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
17 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
18 | # define HAVE_MONOTONIC 1 |
48 | # define HAVE_MONOTONIC 1 |
19 | # endif |
49 | # endif |
… | |
… | |
31 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
32 | #endif |
62 | #endif |
33 | |
63 | |
34 | #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) */ |
35 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
36 | |
67 | |
37 | #include "ev.h" |
68 | #include "ev.h" |
38 | |
|
|
39 | struct ev_watcher { |
|
|
40 | EV_WATCHER (ev_watcher); |
|
|
41 | }; |
|
|
42 | |
|
|
43 | struct ev_watcher_list { |
|
|
44 | EV_WATCHER_LIST (ev_watcher_list); |
|
|
45 | }; |
|
|
46 | |
|
|
47 | struct ev_watcher_time { |
|
|
48 | EV_WATCHER_TIME (ev_watcher_time); |
|
|
49 | }; |
|
|
50 | |
69 | |
51 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
52 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
53 | typedef struct ev_watcher_time *WT; |
72 | typedef struct ev_watcher_time *WT; |
54 | |
73 | |
… | |
… | |
94 | } |
113 | } |
95 | |
114 | |
96 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
97 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
98 | { \ |
117 | { \ |
99 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
100 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
101 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
102 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
103 | cur = newcnt; \ |
127 | cur = newcnt; \ |
104 | } |
128 | } |
105 | |
129 | |
… | |
… | |
138 | static int pendingmax, pendingcnt; |
162 | static int pendingmax, pendingcnt; |
139 | |
163 | |
140 | static void |
164 | static void |
141 | event (W w, int events) |
165 | event (W w, int events) |
142 | { |
166 | { |
|
|
167 | if (w->active) |
|
|
168 | { |
143 | w->pending = ++pendingcnt; |
169 | w->pending = ++pendingcnt; |
144 | array_needsize (pendings, pendingmax, pendingcnt, ); |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
145 | pendings [pendingcnt - 1].w = w; |
171 | pendings [pendingcnt - 1].w = w; |
146 | pendings [pendingcnt - 1].events = events; |
172 | pendings [pendingcnt - 1].events = events; |
|
|
173 | } |
147 | } |
174 | } |
148 | |
175 | |
149 | static void |
176 | static void |
150 | fd_event (int fd, int events) |
177 | fd_event (int fd, int events) |
151 | { |
178 | { |
… | |
… | |
166 | { |
193 | { |
167 | int i; |
194 | int i; |
168 | |
195 | |
169 | for (i = 0; i < eventcnt; ++i) |
196 | for (i = 0; i < eventcnt; ++i) |
170 | event (events [i], type); |
197 | event (events [i], type); |
|
|
198 | } |
|
|
199 | |
|
|
200 | /* called on EBADF to verify fds */ |
|
|
201 | static void |
|
|
202 | fd_recheck () |
|
|
203 | { |
|
|
204 | int fd; |
|
|
205 | |
|
|
206 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
207 | if (anfds [fd].wev) |
|
|
208 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
209 | while (anfds [fd].head) |
|
|
210 | evio_stop (anfds [fd].head); |
171 | } |
211 | } |
172 | |
212 | |
173 | /*****************************************************************************/ |
213 | /*****************************************************************************/ |
174 | |
214 | |
175 | static struct ev_timer **timers; |
215 | static struct ev_timer **timers; |
… | |
… | |
293 | /*****************************************************************************/ |
333 | /*****************************************************************************/ |
294 | |
334 | |
295 | static struct ev_idle **idles; |
335 | static struct ev_idle **idles; |
296 | static int idlemax, idlecnt; |
336 | static int idlemax, idlecnt; |
297 | |
337 | |
|
|
338 | static struct ev_prepare **prepares; |
|
|
339 | static int preparemax, preparecnt; |
|
|
340 | |
298 | static struct ev_check **checks; |
341 | static struct ev_check **checks; |
299 | static int checkmax, checkcnt; |
342 | static int checkmax, checkcnt; |
300 | |
343 | |
301 | /*****************************************************************************/ |
344 | /*****************************************************************************/ |
302 | |
345 | |
|
|
346 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
347 | static struct ev_signal childev; |
|
|
348 | |
|
|
349 | #ifndef WCONTINUED |
|
|
350 | # define WCONTINUED 0 |
|
|
351 | #endif |
|
|
352 | |
|
|
353 | static void |
|
|
354 | childcb (struct ev_signal *sw, int revents) |
|
|
355 | { |
|
|
356 | struct ev_child *w; |
|
|
357 | int pid, status; |
|
|
358 | |
|
|
359 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
360 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
361 | if (w->pid == pid || w->pid == -1) |
|
|
362 | { |
|
|
363 | w->status = status; |
|
|
364 | event ((W)w, EV_CHILD); |
|
|
365 | } |
|
|
366 | } |
|
|
367 | |
|
|
368 | /*****************************************************************************/ |
|
|
369 | |
303 | #if HAVE_EPOLL |
370 | #if HAVE_EPOLL |
304 | # include "ev_epoll.c" |
371 | # include "ev_epoll.c" |
305 | #endif |
372 | #endif |
306 | #if HAVE_SELECT |
373 | #if HAVE_SELECT |
307 | # include "ev_select.c" |
374 | # include "ev_select.c" |
308 | #endif |
375 | #endif |
309 | |
376 | |
310 | int ev_init (int flags) |
377 | int ev_init (int flags) |
311 | { |
378 | { |
|
|
379 | if (!ev_method) |
|
|
380 | { |
312 | #if HAVE_MONOTONIC |
381 | #if HAVE_MONOTONIC |
313 | { |
382 | { |
314 | struct timespec ts; |
383 | struct timespec ts; |
315 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
384 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
316 | have_monotonic = 1; |
385 | have_monotonic = 1; |
317 | } |
386 | } |
318 | #endif |
387 | #endif |
319 | |
388 | |
320 | ev_now = ev_time (); |
389 | ev_now = ev_time (); |
321 | now = get_clock (); |
390 | now = get_clock (); |
322 | diff = ev_now - now; |
391 | diff = ev_now - now; |
323 | |
392 | |
324 | if (pipe (sigpipe)) |
393 | if (pipe (sigpipe)) |
325 | return 0; |
394 | return 0; |
326 | |
395 | |
327 | ev_method = EVMETHOD_NONE; |
396 | ev_method = EVMETHOD_NONE; |
328 | #if HAVE_EPOLL |
397 | #if HAVE_EPOLL |
329 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
398 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
330 | #endif |
399 | #endif |
331 | #if HAVE_SELECT |
400 | #if HAVE_SELECT |
332 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
401 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
333 | #endif |
402 | #endif |
334 | |
403 | |
335 | if (ev_method) |
404 | if (ev_method) |
336 | { |
405 | { |
337 | evw_init (&sigev, sigcb); |
406 | evw_init (&sigev, sigcb); |
338 | siginit (); |
407 | siginit (); |
|
|
408 | |
|
|
409 | evsignal_init (&childev, childcb, SIGCHLD); |
|
|
410 | evsignal_start (&childev); |
|
|
411 | } |
339 | } |
412 | } |
340 | |
413 | |
341 | return ev_method; |
414 | return ev_method; |
342 | } |
415 | } |
343 | |
416 | |
… | |
… | |
396 | } |
469 | } |
397 | |
470 | |
398 | static void |
471 | static void |
399 | call_pending () |
472 | call_pending () |
400 | { |
473 | { |
401 | int i; |
474 | while (pendingcnt) |
402 | |
|
|
403 | for (i = 0; i < pendingcnt; ++i) |
|
|
404 | { |
475 | { |
405 | ANPENDING *p = pendings + i; |
476 | ANPENDING *p = pendings + --pendingcnt; |
406 | |
477 | |
407 | if (p->w) |
478 | if (p->w) |
408 | { |
479 | { |
409 | p->w->pending = 0; |
480 | p->w->pending = 0; |
410 | p->w->cb (p->w, p->events); |
481 | p->w->cb (p->w, p->events); |
411 | } |
482 | } |
412 | } |
483 | } |
413 | |
|
|
414 | pendingcnt = 0; |
|
|
415 | } |
484 | } |
416 | |
485 | |
417 | static void |
486 | static void |
418 | timers_reify () |
487 | timers_reify () |
419 | { |
488 | { |
420 | while (timercnt && timers [0]->at <= now) |
489 | while (timercnt && timers [0]->at <= now) |
421 | { |
490 | { |
422 | struct ev_timer *w = timers [0]; |
491 | struct ev_timer *w = timers [0]; |
|
|
492 | |
|
|
493 | event ((W)w, EV_TIMEOUT); |
423 | |
494 | |
424 | /* first reschedule or stop timer */ |
495 | /* first reschedule or stop timer */ |
425 | if (w->repeat) |
496 | if (w->repeat) |
426 | { |
497 | { |
427 | w->at = now + w->repeat; |
498 | w->at = now + w->repeat; |
428 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
499 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
429 | downheap ((WT *)timers, timercnt, 0); |
500 | downheap ((WT *)timers, timercnt, 0); |
430 | } |
501 | } |
431 | else |
502 | else |
432 | evtimer_stop (w); /* nonrepeating: stop timer */ |
503 | evtimer_stop (w); /* nonrepeating: stop timer */ |
433 | |
|
|
434 | event ((W)w, EV_TIMEOUT); |
|
|
435 | } |
504 | } |
436 | } |
505 | } |
437 | |
506 | |
438 | static void |
507 | static void |
439 | periodics_reify () |
508 | periodics_reify () |
… | |
… | |
455 | event ((W)w, EV_TIMEOUT); |
524 | event ((W)w, EV_TIMEOUT); |
456 | } |
525 | } |
457 | } |
526 | } |
458 | |
527 | |
459 | static void |
528 | static void |
460 | time_jump (ev_tstamp diff) |
529 | periodics_reschedule (ev_tstamp diff) |
461 | { |
530 | { |
462 | int i; |
531 | int i; |
463 | |
532 | |
464 | /* adjust periodics */ |
533 | /* adjust periodics after time jump */ |
465 | for (i = 0; i < periodiccnt; ++i) |
534 | for (i = 0; i < periodiccnt; ++i) |
466 | { |
535 | { |
467 | struct ev_periodic *w = periodics [i]; |
536 | struct ev_periodic *w = periodics [i]; |
468 | |
537 | |
469 | if (w->interval) |
538 | if (w->interval) |
… | |
… | |
477 | |
546 | |
478 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
547 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
479 | } |
548 | } |
480 | } |
549 | } |
481 | } |
550 | } |
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; |
|
|
486 | } |
551 | } |
487 | |
552 | |
488 | static void |
553 | static void |
489 | time_update () |
554 | time_update () |
490 | { |
555 | { |
… | |
… | |
505 | return; /* all is well */ |
570 | return; /* all is well */ |
506 | |
571 | |
507 | ev_now = ev_time (); |
572 | ev_now = ev_time (); |
508 | } |
573 | } |
509 | |
574 | |
510 | time_jump (diff - odiff); |
575 | periodics_reschedule (diff - odiff); |
|
|
576 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
511 | } |
577 | } |
512 | else |
578 | else |
513 | { |
579 | { |
514 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
580 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
515 | time_jump (ev_now - now); |
581 | { |
|
|
582 | periodics_reschedule (ev_now - now); |
|
|
583 | |
|
|
584 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
585 | for (i = 0; i < timercnt; ++i) |
|
|
586 | timers [i]->at += diff; |
|
|
587 | } |
516 | |
588 | |
517 | now = ev_now; |
589 | now = ev_now; |
518 | } |
590 | } |
519 | } |
591 | } |
520 | |
592 | |
521 | int ev_loop_done; |
593 | int ev_loop_done; |
522 | |
594 | |
523 | void ev_loop (int flags) |
595 | void ev_loop (int flags) |
524 | { |
596 | { |
525 | double block; |
597 | double block; |
526 | ev_loop_done = flags & EVLOOP_ONESHOT; |
598 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
527 | |
|
|
528 | if (checkcnt) |
|
|
529 | { |
|
|
530 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
531 | call_pending (); |
|
|
532 | } |
|
|
533 | |
599 | |
534 | do |
600 | do |
535 | { |
601 | { |
|
|
602 | /* queue check watchers (and execute them) */ |
|
|
603 | if (preparecnt) |
|
|
604 | { |
|
|
605 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
606 | call_pending (); |
|
|
607 | } |
|
|
608 | |
536 | /* update fd-related kernel structures */ |
609 | /* update fd-related kernel structures */ |
537 | fd_reify (); |
610 | fd_reify (); |
538 | |
611 | |
539 | /* calculate blocking time */ |
612 | /* calculate blocking time */ |
540 | |
613 | |
541 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
614 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
615 | always have timers, we just calculate it always */ |
542 | ev_now = ev_time (); |
616 | ev_now = ev_time (); |
543 | |
617 | |
544 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
618 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
545 | block = 0.; |
619 | block = 0.; |
546 | else |
620 | else |
547 | { |
621 | { |
548 | block = MAX_BLOCKTIME; |
622 | block = MAX_BLOCKTIME; |
549 | |
623 | |
550 | if (timercnt) |
624 | if (timercnt) |
551 | { |
625 | { |
552 | ev_tstamp to = timers [0]->at - get_clock () + method_fudge; |
626 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
553 | if (block > to) block = to; |
627 | if (block > to) block = to; |
554 | } |
628 | } |
555 | |
629 | |
556 | if (periodiccnt) |
630 | if (periodiccnt) |
557 | { |
631 | { |
… | |
… | |
566 | |
640 | |
567 | /* update ev_now, do magic */ |
641 | /* update ev_now, do magic */ |
568 | time_update (); |
642 | time_update (); |
569 | |
643 | |
570 | /* queue pending timers and reschedule them */ |
644 | /* queue pending timers and reschedule them */ |
|
|
645 | timers_reify (); /* relative timers called last */ |
571 | periodics_reify (); /* absolute timers first */ |
646 | periodics_reify (); /* absolute timers called first */ |
572 | timers_reify (); /* relative timers second */ |
|
|
573 | |
647 | |
574 | /* queue idle watchers unless io or timers are pending */ |
648 | /* queue idle watchers unless io or timers are pending */ |
575 | if (!pendingcnt) |
649 | if (!pendingcnt) |
576 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
650 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
577 | |
651 | |
578 | /* queue check and possibly idle watchers */ |
652 | /* queue check watchers, to be executed first */ |
|
|
653 | if (checkcnt) |
579 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
654 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
580 | |
655 | |
581 | call_pending (); |
656 | call_pending (); |
582 | } |
657 | } |
583 | while (!ev_loop_done); |
658 | while (!ev_loop_done); |
|
|
659 | |
|
|
660 | if (ev_loop_done != 2) |
|
|
661 | ev_loop_done = 0; |
584 | } |
662 | } |
585 | |
663 | |
586 | /*****************************************************************************/ |
664 | /*****************************************************************************/ |
587 | |
665 | |
588 | static void |
666 | static void |
… | |
… | |
606 | head = &(*head)->next; |
684 | head = &(*head)->next; |
607 | } |
685 | } |
608 | } |
686 | } |
609 | |
687 | |
610 | static void |
688 | static void |
|
|
689 | ev_clear (W w) |
|
|
690 | { |
|
|
691 | if (w->pending) |
|
|
692 | { |
|
|
693 | pendings [w->pending - 1].w = 0; |
|
|
694 | w->pending = 0; |
|
|
695 | } |
|
|
696 | } |
|
|
697 | |
|
|
698 | static void |
611 | ev_start (W w, int active) |
699 | ev_start (W w, int active) |
612 | { |
700 | { |
613 | w->pending = 0; |
|
|
614 | w->active = active; |
701 | w->active = active; |
615 | } |
702 | } |
616 | |
703 | |
617 | static void |
704 | static void |
618 | ev_stop (W w) |
705 | ev_stop (W w) |
619 | { |
706 | { |
620 | if (w->pending) |
|
|
621 | pendings [w->pending - 1].w = 0; |
|
|
622 | |
|
|
623 | w->active = 0; |
707 | w->active = 0; |
624 | } |
708 | } |
625 | |
709 | |
626 | /*****************************************************************************/ |
710 | /*****************************************************************************/ |
627 | |
711 | |
… | |
… | |
643 | } |
727 | } |
644 | |
728 | |
645 | void |
729 | void |
646 | evio_stop (struct ev_io *w) |
730 | evio_stop (struct ev_io *w) |
647 | { |
731 | { |
|
|
732 | ev_clear ((W)w); |
648 | if (!ev_is_active (w)) |
733 | if (!ev_is_active (w)) |
649 | return; |
734 | return; |
650 | |
735 | |
651 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
736 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
652 | ev_stop ((W)w); |
737 | ev_stop ((W)w); |
… | |
… | |
654 | ++fdchangecnt; |
739 | ++fdchangecnt; |
655 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
740 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
656 | fdchanges [fdchangecnt - 1] = w->fd; |
741 | fdchanges [fdchangecnt - 1] = w->fd; |
657 | } |
742 | } |
658 | |
743 | |
659 | |
|
|
660 | void |
744 | void |
661 | evtimer_start (struct ev_timer *w) |
745 | evtimer_start (struct ev_timer *w) |
662 | { |
746 | { |
663 | if (ev_is_active (w)) |
747 | if (ev_is_active (w)) |
664 | return; |
748 | return; |
665 | |
749 | |
666 | w->at += now; |
750 | w->at += now; |
|
|
751 | |
|
|
752 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
667 | |
753 | |
668 | ev_start ((W)w, ++timercnt); |
754 | ev_start ((W)w, ++timercnt); |
669 | array_needsize (timers, timermax, timercnt, ); |
755 | array_needsize (timers, timermax, timercnt, ); |
670 | timers [timercnt - 1] = w; |
756 | timers [timercnt - 1] = w; |
671 | upheap ((WT *)timers, timercnt - 1); |
757 | upheap ((WT *)timers, timercnt - 1); |
672 | } |
758 | } |
673 | |
759 | |
674 | void |
760 | void |
675 | evtimer_stop (struct ev_timer *w) |
761 | evtimer_stop (struct ev_timer *w) |
676 | { |
762 | { |
|
|
763 | ev_clear ((W)w); |
677 | if (!ev_is_active (w)) |
764 | if (!ev_is_active (w)) |
678 | return; |
765 | return; |
679 | |
766 | |
680 | if (w->active < timercnt--) |
767 | if (w->active < timercnt--) |
681 | { |
768 | { |
682 | timers [w->active - 1] = timers [timercnt]; |
769 | timers [w->active - 1] = timers [timercnt]; |
683 | downheap ((WT *)timers, timercnt, w->active - 1); |
770 | downheap ((WT *)timers, timercnt, w->active - 1); |
684 | } |
771 | } |
685 | |
772 | |
|
|
773 | w->at = w->repeat; |
|
|
774 | |
686 | ev_stop ((W)w); |
775 | ev_stop ((W)w); |
|
|
776 | } |
|
|
777 | |
|
|
778 | void |
|
|
779 | evtimer_again (struct ev_timer *w) |
|
|
780 | { |
|
|
781 | if (ev_is_active (w)) |
|
|
782 | { |
|
|
783 | if (w->repeat) |
|
|
784 | { |
|
|
785 | w->at = now + w->repeat; |
|
|
786 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
787 | } |
|
|
788 | else |
|
|
789 | evtimer_stop (w); |
|
|
790 | } |
|
|
791 | else if (w->repeat) |
|
|
792 | evtimer_start (w); |
687 | } |
793 | } |
688 | |
794 | |
689 | void |
795 | void |
690 | evperiodic_start (struct ev_periodic *w) |
796 | evperiodic_start (struct ev_periodic *w) |
691 | { |
797 | { |
692 | if (ev_is_active (w)) |
798 | if (ev_is_active (w)) |
693 | return; |
799 | return; |
|
|
800 | |
|
|
801 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
694 | |
802 | |
695 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
803 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
696 | if (w->interval) |
804 | if (w->interval) |
697 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
805 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
698 | |
806 | |
… | |
… | |
703 | } |
811 | } |
704 | |
812 | |
705 | void |
813 | void |
706 | evperiodic_stop (struct ev_periodic *w) |
814 | evperiodic_stop (struct ev_periodic *w) |
707 | { |
815 | { |
|
|
816 | ev_clear ((W)w); |
708 | if (!ev_is_active (w)) |
817 | if (!ev_is_active (w)) |
709 | return; |
818 | return; |
710 | |
819 | |
711 | if (w->active < periodiccnt--) |
820 | if (w->active < periodiccnt--) |
712 | { |
821 | { |
… | |
… | |
738 | } |
847 | } |
739 | |
848 | |
740 | void |
849 | void |
741 | evsignal_stop (struct ev_signal *w) |
850 | evsignal_stop (struct ev_signal *w) |
742 | { |
851 | { |
|
|
852 | ev_clear ((W)w); |
743 | if (!ev_is_active (w)) |
853 | if (!ev_is_active (w)) |
744 | return; |
854 | return; |
745 | |
855 | |
746 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
856 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
747 | ev_stop ((W)w); |
857 | ev_stop ((W)w); |
… | |
… | |
760 | idles [idlecnt - 1] = w; |
870 | idles [idlecnt - 1] = w; |
761 | } |
871 | } |
762 | |
872 | |
763 | void evidle_stop (struct ev_idle *w) |
873 | void evidle_stop (struct ev_idle *w) |
764 | { |
874 | { |
|
|
875 | ev_clear ((W)w); |
|
|
876 | if (ev_is_active (w)) |
|
|
877 | return; |
|
|
878 | |
765 | idles [w->active - 1] = idles [--idlecnt]; |
879 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
880 | ev_stop ((W)w); |
|
|
881 | } |
|
|
882 | |
|
|
883 | void evprepare_start (struct ev_prepare *w) |
|
|
884 | { |
|
|
885 | if (ev_is_active (w)) |
|
|
886 | return; |
|
|
887 | |
|
|
888 | ev_start ((W)w, ++preparecnt); |
|
|
889 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
890 | prepares [preparecnt - 1] = w; |
|
|
891 | } |
|
|
892 | |
|
|
893 | void evprepare_stop (struct ev_prepare *w) |
|
|
894 | { |
|
|
895 | ev_clear ((W)w); |
|
|
896 | if (ev_is_active (w)) |
|
|
897 | return; |
|
|
898 | |
|
|
899 | prepares [w->active - 1] = prepares [--preparecnt]; |
766 | ev_stop ((W)w); |
900 | ev_stop ((W)w); |
767 | } |
901 | } |
768 | |
902 | |
769 | void evcheck_start (struct ev_check *w) |
903 | void evcheck_start (struct ev_check *w) |
770 | { |
904 | { |
… | |
… | |
776 | checks [checkcnt - 1] = w; |
910 | checks [checkcnt - 1] = w; |
777 | } |
911 | } |
778 | |
912 | |
779 | void evcheck_stop (struct ev_check *w) |
913 | void evcheck_stop (struct ev_check *w) |
780 | { |
914 | { |
|
|
915 | ev_clear ((W)w); |
|
|
916 | if (ev_is_active (w)) |
|
|
917 | return; |
|
|
918 | |
781 | checks [w->active - 1] = checks [--checkcnt]; |
919 | checks [w->active - 1] = checks [--checkcnt]; |
782 | ev_stop ((W)w); |
920 | ev_stop ((W)w); |
783 | } |
921 | } |
784 | |
922 | |
785 | /*****************************************************************************/ |
923 | void evchild_start (struct ev_child *w) |
|
|
924 | { |
|
|
925 | if (ev_is_active (w)) |
|
|
926 | return; |
786 | |
927 | |
|
|
928 | ev_start ((W)w, 1); |
|
|
929 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
930 | } |
|
|
931 | |
|
|
932 | void evchild_stop (struct ev_child *w) |
|
|
933 | { |
|
|
934 | ev_clear ((W)w); |
|
|
935 | if (ev_is_active (w)) |
|
|
936 | return; |
|
|
937 | |
|
|
938 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
939 | ev_stop ((W)w); |
|
|
940 | } |
|
|
941 | |
|
|
942 | /*****************************************************************************/ |
|
|
943 | |
|
|
944 | struct ev_once |
|
|
945 | { |
|
|
946 | struct ev_io io; |
|
|
947 | struct ev_timer to; |
|
|
948 | void (*cb)(int revents, void *arg); |
|
|
949 | void *arg; |
|
|
950 | }; |
|
|
951 | |
|
|
952 | static void |
|
|
953 | once_cb (struct ev_once *once, int revents) |
|
|
954 | { |
|
|
955 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
956 | void *arg = once->arg; |
|
|
957 | |
|
|
958 | evio_stop (&once->io); |
|
|
959 | evtimer_stop (&once->to); |
|
|
960 | free (once); |
|
|
961 | |
|
|
962 | cb (revents, arg); |
|
|
963 | } |
|
|
964 | |
|
|
965 | static void |
|
|
966 | once_cb_io (struct ev_io *w, int revents) |
|
|
967 | { |
|
|
968 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
969 | } |
|
|
970 | |
|
|
971 | static void |
|
|
972 | once_cb_to (struct ev_timer *w, int revents) |
|
|
973 | { |
|
|
974 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
975 | } |
|
|
976 | |
|
|
977 | void |
|
|
978 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
979 | { |
|
|
980 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
981 | |
|
|
982 | if (!once) |
|
|
983 | cb (EV_ERROR, arg); |
|
|
984 | else |
|
|
985 | { |
|
|
986 | once->cb = cb; |
|
|
987 | once->arg = arg; |
|
|
988 | |
|
|
989 | evw_init (&once->io, once_cb_io); |
|
|
990 | |
|
|
991 | if (fd >= 0) |
|
|
992 | { |
|
|
993 | evio_set (&once->io, fd, events); |
|
|
994 | evio_start (&once->io); |
|
|
995 | } |
|
|
996 | |
|
|
997 | evw_init (&once->to, once_cb_to); |
|
|
998 | |
|
|
999 | if (timeout >= 0.) |
|
|
1000 | { |
|
|
1001 | evtimer_set (&once->to, timeout, 0.); |
|
|
1002 | evtimer_start (&once->to); |
|
|
1003 | } |
|
|
1004 | } |
|
|
1005 | } |
|
|
1006 | |
|
|
1007 | /*****************************************************************************/ |
|
|
1008 | |
787 | #if 1 |
1009 | #if 0 |
788 | |
1010 | |
789 | struct ev_io wio; |
1011 | struct ev_io wio; |
790 | |
1012 | |
791 | static void |
1013 | static void |
792 | sin_cb (struct ev_io *w, int revents) |
1014 | sin_cb (struct ev_io *w, int revents) |