|
|
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 | |
69 | |
39 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
40 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
… | |
… | |
82 | } |
113 | } |
83 | |
114 | |
84 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
85 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
86 | { \ |
117 | { \ |
87 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
|
|
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
88 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
89 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
90 | cur = newcnt; \ |
127 | cur = newcnt; \ |
91 | } |
128 | } |
92 | |
129 | |
… | |
… | |
125 | static int pendingmax, pendingcnt; |
162 | static int pendingmax, pendingcnt; |
126 | |
163 | |
127 | static void |
164 | static void |
128 | event (W w, int events) |
165 | event (W w, int events) |
129 | { |
166 | { |
|
|
167 | if (w->active) |
|
|
168 | { |
130 | w->pending = ++pendingcnt; |
169 | w->pending = ++pendingcnt; |
131 | array_needsize (pendings, pendingmax, pendingcnt, ); |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
132 | pendings [pendingcnt - 1].w = w; |
171 | pendings [pendingcnt - 1].w = w; |
133 | pendings [pendingcnt - 1].events = events; |
172 | pendings [pendingcnt - 1].events = events; |
|
|
173 | } |
134 | } |
174 | } |
135 | |
175 | |
136 | static void |
176 | static void |
137 | fd_event (int fd, int events) |
177 | fd_event (int fd, int events) |
138 | { |
178 | { |
… | |
… | |
153 | { |
193 | { |
154 | int i; |
194 | int i; |
155 | |
195 | |
156 | for (i = 0; i < eventcnt; ++i) |
196 | for (i = 0; i < eventcnt; ++i) |
157 | 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); |
158 | } |
211 | } |
159 | |
212 | |
160 | /*****************************************************************************/ |
213 | /*****************************************************************************/ |
161 | |
214 | |
162 | static struct ev_timer **timers; |
215 | static struct ev_timer **timers; |
… | |
… | |
280 | /*****************************************************************************/ |
333 | /*****************************************************************************/ |
281 | |
334 | |
282 | static struct ev_idle **idles; |
335 | static struct ev_idle **idles; |
283 | static int idlemax, idlecnt; |
336 | static int idlemax, idlecnt; |
284 | |
337 | |
|
|
338 | static struct ev_prepare **prepares; |
|
|
339 | static int preparemax, preparecnt; |
|
|
340 | |
285 | static struct ev_check **checks; |
341 | static struct ev_check **checks; |
286 | static int checkmax, checkcnt; |
342 | static int checkmax, checkcnt; |
287 | |
343 | |
288 | /*****************************************************************************/ |
344 | /*****************************************************************************/ |
289 | |
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 | |
290 | #if HAVE_EPOLL |
370 | #if HAVE_EPOLL |
291 | # include "ev_epoll.c" |
371 | # include "ev_epoll.c" |
292 | #endif |
372 | #endif |
293 | #if HAVE_SELECT |
373 | #if HAVE_SELECT |
294 | # include "ev_select.c" |
374 | # include "ev_select.c" |
295 | #endif |
375 | #endif |
296 | |
376 | |
297 | int ev_init (int flags) |
377 | int ev_init (int flags) |
298 | { |
378 | { |
|
|
379 | if (!ev_method) |
|
|
380 | { |
299 | #if HAVE_MONOTONIC |
381 | #if HAVE_MONOTONIC |
300 | { |
382 | { |
301 | struct timespec ts; |
383 | struct timespec ts; |
302 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
384 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
303 | have_monotonic = 1; |
385 | have_monotonic = 1; |
304 | } |
386 | } |
305 | #endif |
387 | #endif |
306 | |
388 | |
307 | ev_now = ev_time (); |
389 | ev_now = ev_time (); |
308 | now = get_clock (); |
390 | now = get_clock (); |
309 | diff = ev_now - now; |
391 | diff = ev_now - now; |
310 | |
392 | |
311 | if (pipe (sigpipe)) |
393 | if (pipe (sigpipe)) |
312 | return 0; |
394 | return 0; |
313 | |
395 | |
314 | ev_method = EVMETHOD_NONE; |
396 | ev_method = EVMETHOD_NONE; |
315 | #if HAVE_EPOLL |
397 | #if HAVE_EPOLL |
316 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
398 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
317 | #endif |
399 | #endif |
318 | #if HAVE_SELECT |
400 | #if HAVE_SELECT |
319 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
401 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
320 | #endif |
402 | #endif |
321 | |
403 | |
322 | if (ev_method) |
404 | if (ev_method) |
323 | { |
405 | { |
324 | evw_init (&sigev, sigcb); |
406 | evw_init (&sigev, sigcb); |
325 | siginit (); |
407 | siginit (); |
|
|
408 | |
|
|
409 | evsignal_init (&childev, childcb, SIGCHLD); |
|
|
410 | evsignal_start (&childev); |
|
|
411 | } |
326 | } |
412 | } |
327 | |
413 | |
328 | return ev_method; |
414 | return ev_method; |
329 | } |
415 | } |
330 | |
416 | |
… | |
… | |
383 | } |
469 | } |
384 | |
470 | |
385 | static void |
471 | static void |
386 | call_pending () |
472 | call_pending () |
387 | { |
473 | { |
388 | int i; |
474 | while (pendingcnt) |
389 | |
|
|
390 | for (i = 0; i < pendingcnt; ++i) |
|
|
391 | { |
475 | { |
392 | ANPENDING *p = pendings + i; |
476 | ANPENDING *p = pendings + --pendingcnt; |
393 | |
477 | |
394 | if (p->w) |
478 | if (p->w) |
395 | { |
479 | { |
396 | p->w->pending = 0; |
480 | p->w->pending = 0; |
397 | p->w->cb (p->w, p->events); |
481 | p->w->cb (p->w, p->events); |
398 | } |
482 | } |
399 | } |
483 | } |
400 | |
|
|
401 | pendingcnt = 0; |
|
|
402 | } |
484 | } |
403 | |
485 | |
404 | static void |
486 | static void |
405 | timers_reify () |
487 | timers_reify () |
406 | { |
488 | { |
407 | while (timercnt && timers [0]->at <= now) |
489 | while (timercnt && timers [0]->at <= now) |
408 | { |
490 | { |
409 | struct ev_timer *w = timers [0]; |
491 | struct ev_timer *w = timers [0]; |
|
|
492 | |
|
|
493 | event ((W)w, EV_TIMEOUT); |
410 | |
494 | |
411 | /* first reschedule or stop timer */ |
495 | /* first reschedule or stop timer */ |
412 | if (w->repeat) |
496 | if (w->repeat) |
413 | { |
497 | { |
414 | w->at = now + w->repeat; |
498 | w->at = now + w->repeat; |
415 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
499 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
416 | downheap ((WT *)timers, timercnt, 0); |
500 | downheap ((WT *)timers, timercnt, 0); |
417 | } |
501 | } |
418 | else |
502 | else |
419 | evtimer_stop (w); /* nonrepeating: stop timer */ |
503 | evtimer_stop (w); /* nonrepeating: stop timer */ |
420 | |
|
|
421 | event ((W)w, EV_TIMEOUT); |
|
|
422 | } |
504 | } |
423 | } |
505 | } |
424 | |
506 | |
425 | static void |
507 | static void |
426 | periodics_reify () |
508 | periodics_reify () |
… | |
… | |
513 | void ev_loop (int flags) |
595 | void ev_loop (int flags) |
514 | { |
596 | { |
515 | double block; |
597 | double block; |
516 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
598 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
517 | |
599 | |
518 | if (checkcnt) |
|
|
519 | { |
|
|
520 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
521 | call_pending (); |
|
|
522 | } |
|
|
523 | |
|
|
524 | do |
600 | do |
525 | { |
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 | |
526 | /* update fd-related kernel structures */ |
609 | /* update fd-related kernel structures */ |
527 | fd_reify (); |
610 | fd_reify (); |
528 | |
611 | |
529 | /* calculate blocking time */ |
612 | /* calculate blocking time */ |
530 | |
613 | |
531 | /* 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 */ |
532 | ev_now = ev_time (); |
616 | ev_now = ev_time (); |
533 | |
617 | |
534 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
618 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
535 | block = 0.; |
619 | block = 0.; |
536 | else |
620 | else |
… | |
… | |
556 | |
640 | |
557 | /* update ev_now, do magic */ |
641 | /* update ev_now, do magic */ |
558 | time_update (); |
642 | time_update (); |
559 | |
643 | |
560 | /* queue pending timers and reschedule them */ |
644 | /* queue pending timers and reschedule them */ |
|
|
645 | timers_reify (); /* relative timers called last */ |
561 | periodics_reify (); /* absolute timers first */ |
646 | periodics_reify (); /* absolute timers called first */ |
562 | timers_reify (); /* relative timers second */ |
|
|
563 | |
647 | |
564 | /* queue idle watchers unless io or timers are pending */ |
648 | /* queue idle watchers unless io or timers are pending */ |
565 | if (!pendingcnt) |
649 | if (!pendingcnt) |
566 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
650 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
567 | |
651 | |
568 | /* queue check and possibly idle watchers */ |
652 | /* queue check watchers, to be executed first */ |
|
|
653 | if (checkcnt) |
569 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
654 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
570 | |
655 | |
571 | call_pending (); |
656 | call_pending (); |
572 | } |
657 | } |
573 | while (!ev_loop_done); |
658 | while (!ev_loop_done); |
574 | |
659 | |
… | |
… | |
599 | head = &(*head)->next; |
684 | head = &(*head)->next; |
600 | } |
685 | } |
601 | } |
686 | } |
602 | |
687 | |
603 | 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 |
604 | ev_start (W w, int active) |
699 | ev_start (W w, int active) |
605 | { |
700 | { |
606 | w->pending = 0; |
|
|
607 | w->active = active; |
701 | w->active = active; |
608 | } |
702 | } |
609 | |
703 | |
610 | static void |
704 | static void |
611 | ev_stop (W w) |
705 | ev_stop (W w) |
612 | { |
706 | { |
613 | if (w->pending) |
|
|
614 | pendings [w->pending - 1].w = 0; |
|
|
615 | |
|
|
616 | w->active = 0; |
707 | w->active = 0; |
617 | } |
708 | } |
618 | |
709 | |
619 | /*****************************************************************************/ |
710 | /*****************************************************************************/ |
620 | |
711 | |
… | |
… | |
636 | } |
727 | } |
637 | |
728 | |
638 | void |
729 | void |
639 | evio_stop (struct ev_io *w) |
730 | evio_stop (struct ev_io *w) |
640 | { |
731 | { |
|
|
732 | ev_clear ((W)w); |
641 | if (!ev_is_active (w)) |
733 | if (!ev_is_active (w)) |
642 | return; |
734 | return; |
643 | |
735 | |
644 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
736 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
645 | ev_stop ((W)w); |
737 | ev_stop ((W)w); |
646 | |
738 | |
647 | ++fdchangecnt; |
739 | ++fdchangecnt; |
648 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
740 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
649 | fdchanges [fdchangecnt - 1] = w->fd; |
741 | fdchanges [fdchangecnt - 1] = w->fd; |
650 | } |
742 | } |
651 | |
|
|
652 | |
743 | |
653 | void |
744 | void |
654 | evtimer_start (struct ev_timer *w) |
745 | evtimer_start (struct ev_timer *w) |
655 | { |
746 | { |
656 | if (ev_is_active (w)) |
747 | if (ev_is_active (w)) |
… | |
… | |
667 | } |
758 | } |
668 | |
759 | |
669 | void |
760 | void |
670 | evtimer_stop (struct ev_timer *w) |
761 | evtimer_stop (struct ev_timer *w) |
671 | { |
762 | { |
|
|
763 | ev_clear ((W)w); |
672 | if (!ev_is_active (w)) |
764 | if (!ev_is_active (w)) |
673 | return; |
765 | return; |
674 | |
766 | |
675 | if (w->active < timercnt--) |
767 | if (w->active < timercnt--) |
676 | { |
768 | { |
… | |
… | |
719 | } |
811 | } |
720 | |
812 | |
721 | void |
813 | void |
722 | evperiodic_stop (struct ev_periodic *w) |
814 | evperiodic_stop (struct ev_periodic *w) |
723 | { |
815 | { |
|
|
816 | ev_clear ((W)w); |
724 | if (!ev_is_active (w)) |
817 | if (!ev_is_active (w)) |
725 | return; |
818 | return; |
726 | |
819 | |
727 | if (w->active < periodiccnt--) |
820 | if (w->active < periodiccnt--) |
728 | { |
821 | { |
… | |
… | |
754 | } |
847 | } |
755 | |
848 | |
756 | void |
849 | void |
757 | evsignal_stop (struct ev_signal *w) |
850 | evsignal_stop (struct ev_signal *w) |
758 | { |
851 | { |
|
|
852 | ev_clear ((W)w); |
759 | if (!ev_is_active (w)) |
853 | if (!ev_is_active (w)) |
760 | return; |
854 | return; |
761 | |
855 | |
762 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
856 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
763 | ev_stop ((W)w); |
857 | ev_stop ((W)w); |
… | |
… | |
776 | idles [idlecnt - 1] = w; |
870 | idles [idlecnt - 1] = w; |
777 | } |
871 | } |
778 | |
872 | |
779 | void evidle_stop (struct ev_idle *w) |
873 | void evidle_stop (struct ev_idle *w) |
780 | { |
874 | { |
|
|
875 | ev_clear ((W)w); |
|
|
876 | if (ev_is_active (w)) |
|
|
877 | return; |
|
|
878 | |
781 | 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]; |
782 | ev_stop ((W)w); |
900 | ev_stop ((W)w); |
783 | } |
901 | } |
784 | |
902 | |
785 | void evcheck_start (struct ev_check *w) |
903 | void evcheck_start (struct ev_check *w) |
786 | { |
904 | { |
… | |
… | |
792 | checks [checkcnt - 1] = w; |
910 | checks [checkcnt - 1] = w; |
793 | } |
911 | } |
794 | |
912 | |
795 | void evcheck_stop (struct ev_check *w) |
913 | void evcheck_stop (struct ev_check *w) |
796 | { |
914 | { |
|
|
915 | ev_clear ((W)w); |
|
|
916 | if (ev_is_active (w)) |
|
|
917 | return; |
|
|
918 | |
797 | checks [w->active - 1] = checks [--checkcnt]; |
919 | checks [w->active - 1] = checks [--checkcnt]; |
798 | ev_stop ((W)w); |
920 | ev_stop ((W)w); |
|
|
921 | } |
|
|
922 | |
|
|
923 | void evchild_start (struct ev_child *w) |
|
|
924 | { |
|
|
925 | if (ev_is_active (w)) |
|
|
926 | return; |
|
|
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 | } |
799 | } |
1005 | } |
800 | |
1006 | |
801 | /*****************************************************************************/ |
1007 | /*****************************************************************************/ |
802 | |
1008 | |
803 | #if 0 |
1009 | #if 0 |