|
|
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; \ |
88 | 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 | \ |
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
90 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
91 | cur = newcnt; \ |
127 | cur = newcnt; \ |
92 | } |
128 | } |
93 | |
129 | |
… | |
… | |
126 | static int pendingmax, pendingcnt; |
162 | static int pendingmax, pendingcnt; |
127 | |
163 | |
128 | static void |
164 | static void |
129 | event (W w, int events) |
165 | event (W w, int events) |
130 | { |
166 | { |
|
|
167 | if (w->active) |
|
|
168 | { |
131 | w->pending = ++pendingcnt; |
169 | w->pending = ++pendingcnt; |
132 | array_needsize (pendings, pendingmax, pendingcnt, ); |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
133 | pendings [pendingcnt - 1].w = w; |
171 | pendings [pendingcnt - 1].w = w; |
134 | pendings [pendingcnt - 1].events = events; |
172 | pendings [pendingcnt - 1].events = events; |
|
|
173 | } |
135 | } |
174 | } |
136 | |
175 | |
137 | static void |
176 | static void |
138 | fd_event (int fd, int events) |
177 | fd_event (int fd, int events) |
139 | { |
178 | { |
… | |
… | |
154 | { |
193 | { |
155 | int i; |
194 | int i; |
156 | |
195 | |
157 | for (i = 0; i < eventcnt; ++i) |
196 | for (i = 0; i < eventcnt; ++i) |
158 | event (events [i], type); |
197 | event (events [i], type); |
|
|
198 | } |
|
|
199 | |
|
|
200 | /* called on EBADF to verify fds */ |
|
|
201 | static void |
|
|
202 | fd_recheck (void) |
|
|
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 | { |
|
|
211 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
212 | evio_stop (anfds [fd].head); |
|
|
213 | } |
159 | } |
214 | } |
160 | |
215 | |
161 | /*****************************************************************************/ |
216 | /*****************************************************************************/ |
162 | |
217 | |
163 | static struct ev_timer **timers; |
218 | static struct ev_timer **timers; |
… | |
… | |
281 | /*****************************************************************************/ |
336 | /*****************************************************************************/ |
282 | |
337 | |
283 | static struct ev_idle **idles; |
338 | static struct ev_idle **idles; |
284 | static int idlemax, idlecnt; |
339 | static int idlemax, idlecnt; |
285 | |
340 | |
|
|
341 | static struct ev_prepare **prepares; |
|
|
342 | static int preparemax, preparecnt; |
|
|
343 | |
286 | static struct ev_check **checks; |
344 | static struct ev_check **checks; |
287 | static int checkmax, checkcnt; |
345 | static int checkmax, checkcnt; |
288 | |
346 | |
289 | /*****************************************************************************/ |
347 | /*****************************************************************************/ |
290 | |
348 | |
|
|
349 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
350 | static struct ev_signal childev; |
|
|
351 | |
|
|
352 | #ifndef WCONTINUED |
|
|
353 | # define WCONTINUED 0 |
|
|
354 | #endif |
|
|
355 | |
|
|
356 | static void |
|
|
357 | childcb (struct ev_signal *sw, int revents) |
|
|
358 | { |
|
|
359 | struct ev_child *w; |
|
|
360 | int pid, status; |
|
|
361 | |
|
|
362 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
363 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
364 | if (w->pid == pid || w->pid == -1) |
|
|
365 | { |
|
|
366 | w->status = status; |
|
|
367 | event ((W)w, EV_CHILD); |
|
|
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | /*****************************************************************************/ |
|
|
372 | |
291 | #if HAVE_EPOLL |
373 | #if HAVE_EPOLL |
292 | # include "ev_epoll.c" |
374 | # include "ev_epoll.c" |
293 | #endif |
375 | #endif |
294 | #if HAVE_SELECT |
376 | #if HAVE_SELECT |
295 | # include "ev_select.c" |
377 | # include "ev_select.c" |
296 | #endif |
378 | #endif |
297 | |
379 | |
|
|
380 | int |
|
|
381 | ev_version_major (void) |
|
|
382 | { |
|
|
383 | return EV_VERSION_MAJOR; |
|
|
384 | } |
|
|
385 | |
|
|
386 | int |
|
|
387 | ev_version_minor (void) |
|
|
388 | { |
|
|
389 | return EV_VERSION_MINOR; |
|
|
390 | } |
|
|
391 | |
298 | int ev_init (int flags) |
392 | int ev_init (int flags) |
299 | { |
393 | { |
|
|
394 | if (!ev_method) |
|
|
395 | { |
300 | #if HAVE_MONOTONIC |
396 | #if HAVE_MONOTONIC |
301 | { |
397 | { |
302 | struct timespec ts; |
398 | struct timespec ts; |
303 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
399 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
304 | have_monotonic = 1; |
400 | have_monotonic = 1; |
305 | } |
401 | } |
306 | #endif |
402 | #endif |
307 | |
403 | |
308 | ev_now = ev_time (); |
404 | ev_now = ev_time (); |
309 | now = get_clock (); |
405 | now = get_clock (); |
310 | diff = ev_now - now; |
406 | diff = ev_now - now; |
311 | |
407 | |
312 | if (pipe (sigpipe)) |
408 | if (pipe (sigpipe)) |
313 | return 0; |
409 | return 0; |
314 | |
410 | |
315 | ev_method = EVMETHOD_NONE; |
411 | ev_method = EVMETHOD_NONE; |
316 | #if HAVE_EPOLL |
412 | #if HAVE_EPOLL |
317 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
413 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
318 | #endif |
414 | #endif |
319 | #if HAVE_SELECT |
415 | #if HAVE_SELECT |
320 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
416 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
321 | #endif |
417 | #endif |
322 | |
418 | |
323 | if (ev_method) |
419 | if (ev_method) |
324 | { |
420 | { |
325 | evw_init (&sigev, sigcb); |
421 | evw_init (&sigev, sigcb); |
326 | siginit (); |
422 | siginit (); |
|
|
423 | |
|
|
424 | evsignal_init (&childev, childcb, SIGCHLD); |
|
|
425 | evsignal_start (&childev); |
|
|
426 | } |
327 | } |
427 | } |
328 | |
428 | |
329 | return ev_method; |
429 | return ev_method; |
330 | } |
430 | } |
331 | |
431 | |
332 | /*****************************************************************************/ |
432 | /*****************************************************************************/ |
333 | |
433 | |
|
|
434 | void |
334 | void ev_prefork (void) |
435 | ev_prefork (void) |
335 | { |
436 | { |
336 | /* nop */ |
437 | /* nop */ |
337 | } |
438 | } |
338 | |
439 | |
|
|
440 | void |
339 | void ev_postfork_parent (void) |
441 | ev_postfork_parent (void) |
340 | { |
442 | { |
341 | /* nop */ |
443 | /* nop */ |
342 | } |
444 | } |
343 | |
445 | |
|
|
446 | void |
344 | void ev_postfork_child (void) |
447 | ev_postfork_child (void) |
345 | { |
448 | { |
346 | #if HAVE_EPOLL |
449 | #if HAVE_EPOLL |
347 | if (ev_method == EVMETHOD_EPOLL) |
450 | if (ev_method == EVMETHOD_EPOLL) |
348 | epoll_postfork_child (); |
451 | epoll_postfork_child (); |
349 | #endif |
452 | #endif |
… | |
… | |
382 | |
485 | |
383 | fdchangecnt = 0; |
486 | fdchangecnt = 0; |
384 | } |
487 | } |
385 | |
488 | |
386 | static void |
489 | static void |
387 | call_pending () |
490 | call_pending (void) |
388 | { |
491 | { |
389 | int i; |
492 | while (pendingcnt) |
390 | |
|
|
391 | for (i = 0; i < pendingcnt; ++i) |
|
|
392 | { |
493 | { |
393 | ANPENDING *p = pendings + i; |
494 | ANPENDING *p = pendings + --pendingcnt; |
394 | |
495 | |
395 | if (p->w) |
496 | if (p->w) |
396 | { |
497 | { |
397 | p->w->pending = 0; |
498 | p->w->pending = 0; |
398 | p->w->cb (p->w, p->events); |
499 | p->w->cb (p->w, p->events); |
399 | } |
500 | } |
400 | } |
501 | } |
401 | |
|
|
402 | pendingcnt = 0; |
|
|
403 | } |
502 | } |
404 | |
503 | |
405 | static void |
504 | static void |
406 | timers_reify () |
505 | timers_reify (void) |
407 | { |
506 | { |
408 | while (timercnt && timers [0]->at <= now) |
507 | while (timercnt && timers [0]->at <= now) |
409 | { |
508 | { |
410 | struct ev_timer *w = timers [0]; |
509 | struct ev_timer *w = timers [0]; |
|
|
510 | |
|
|
511 | event ((W)w, EV_TIMEOUT); |
411 | |
512 | |
412 | /* first reschedule or stop timer */ |
513 | /* first reschedule or stop timer */ |
413 | if (w->repeat) |
514 | if (w->repeat) |
414 | { |
515 | { |
415 | w->at = now + w->repeat; |
516 | w->at = now + w->repeat; |
416 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
517 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
417 | downheap ((WT *)timers, timercnt, 0); |
518 | downheap ((WT *)timers, timercnt, 0); |
418 | } |
519 | } |
419 | else |
520 | else |
420 | evtimer_stop (w); /* nonrepeating: stop timer */ |
521 | evtimer_stop (w); /* nonrepeating: stop timer */ |
421 | |
|
|
422 | event ((W)w, EV_TIMEOUT); |
|
|
423 | } |
522 | } |
424 | } |
523 | } |
425 | |
524 | |
426 | static void |
525 | static void |
427 | periodics_reify () |
526 | periodics_reify (void) |
428 | { |
527 | { |
429 | while (periodiccnt && periodics [0]->at <= ev_now) |
528 | while (periodiccnt && periodics [0]->at <= ev_now) |
430 | { |
529 | { |
431 | struct ev_periodic *w = periodics [0]; |
530 | struct ev_periodic *w = periodics [0]; |
432 | |
531 | |
… | |
… | |
468 | } |
567 | } |
469 | } |
568 | } |
470 | } |
569 | } |
471 | |
570 | |
472 | static void |
571 | static void |
473 | time_update () |
572 | time_update (void) |
474 | { |
573 | { |
475 | int i; |
574 | int i; |
476 | |
575 | |
477 | ev_now = ev_time (); |
576 | ev_now = ev_time (); |
478 | |
577 | |
… | |
… | |
512 | int ev_loop_done; |
611 | int ev_loop_done; |
513 | |
612 | |
514 | void ev_loop (int flags) |
613 | void ev_loop (int flags) |
515 | { |
614 | { |
516 | double block; |
615 | double block; |
517 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
616 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
518 | |
|
|
519 | if (checkcnt) |
|
|
520 | { |
|
|
521 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
522 | call_pending (); |
|
|
523 | } |
|
|
524 | |
617 | |
525 | do |
618 | do |
526 | { |
619 | { |
|
|
620 | /* queue check watchers (and execute them) */ |
|
|
621 | if (preparecnt) |
|
|
622 | { |
|
|
623 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
624 | call_pending (); |
|
|
625 | } |
|
|
626 | |
527 | /* update fd-related kernel structures */ |
627 | /* update fd-related kernel structures */ |
528 | fd_reify (); |
628 | fd_reify (); |
529 | |
629 | |
530 | /* calculate blocking time */ |
630 | /* calculate blocking time */ |
531 | |
631 | |
532 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
632 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
633 | always have timers, we just calculate it always */ |
533 | ev_now = ev_time (); |
634 | ev_now = ev_time (); |
534 | |
635 | |
535 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
636 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
536 | block = 0.; |
637 | block = 0.; |
537 | else |
638 | else |
… | |
… | |
557 | |
658 | |
558 | /* update ev_now, do magic */ |
659 | /* update ev_now, do magic */ |
559 | time_update (); |
660 | time_update (); |
560 | |
661 | |
561 | /* queue pending timers and reschedule them */ |
662 | /* queue pending timers and reschedule them */ |
|
|
663 | timers_reify (); /* relative timers called last */ |
562 | periodics_reify (); /* absolute timers first */ |
664 | periodics_reify (); /* absolute timers called first */ |
563 | timers_reify (); /* relative timers second */ |
|
|
564 | |
665 | |
565 | /* queue idle watchers unless io or timers are pending */ |
666 | /* queue idle watchers unless io or timers are pending */ |
566 | if (!pendingcnt) |
667 | if (!pendingcnt) |
567 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
668 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
568 | |
669 | |
569 | /* queue check and possibly idle watchers */ |
670 | /* queue check watchers, to be executed first */ |
|
|
671 | if (checkcnt) |
570 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
672 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
571 | |
673 | |
572 | call_pending (); |
674 | call_pending (); |
573 | } |
675 | } |
574 | while (!ev_loop_done); |
676 | while (!ev_loop_done); |
575 | |
677 | |
… | |
… | |
600 | head = &(*head)->next; |
702 | head = &(*head)->next; |
601 | } |
703 | } |
602 | } |
704 | } |
603 | |
705 | |
604 | static void |
706 | static void |
|
|
707 | ev_clear (W w) |
|
|
708 | { |
|
|
709 | if (w->pending) |
|
|
710 | { |
|
|
711 | pendings [w->pending - 1].w = 0; |
|
|
712 | w->pending = 0; |
|
|
713 | } |
|
|
714 | } |
|
|
715 | |
|
|
716 | static void |
605 | ev_start (W w, int active) |
717 | ev_start (W w, int active) |
606 | { |
718 | { |
607 | w->pending = 0; |
|
|
608 | w->active = active; |
719 | w->active = active; |
609 | } |
720 | } |
610 | |
721 | |
611 | static void |
722 | static void |
612 | ev_stop (W w) |
723 | ev_stop (W w) |
613 | { |
724 | { |
614 | if (w->pending) |
|
|
615 | pendings [w->pending - 1].w = 0; |
|
|
616 | |
|
|
617 | w->active = 0; |
725 | w->active = 0; |
618 | } |
726 | } |
619 | |
727 | |
620 | /*****************************************************************************/ |
728 | /*****************************************************************************/ |
621 | |
729 | |
… | |
… | |
632 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
740 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
633 | |
741 | |
634 | ++fdchangecnt; |
742 | ++fdchangecnt; |
635 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
743 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
636 | fdchanges [fdchangecnt - 1] = fd; |
744 | fdchanges [fdchangecnt - 1] = fd; |
|
|
745 | |
|
|
746 | if (w->fd == 9) |
|
|
747 | printf ("start %p:%x\n", w, w->events);//D |
637 | } |
748 | } |
638 | |
749 | |
639 | void |
750 | void |
640 | evio_stop (struct ev_io *w) |
751 | evio_stop (struct ev_io *w) |
641 | { |
752 | { |
|
|
753 | if (w->fd == 9) |
|
|
754 | printf ("stop %p:%x\n", w, w->events);//D |
|
|
755 | ev_clear ((W)w); |
642 | if (!ev_is_active (w)) |
756 | if (!ev_is_active (w)) |
643 | return; |
757 | return; |
644 | |
758 | |
645 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
759 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
646 | ev_stop ((W)w); |
760 | ev_stop ((W)w); |
647 | |
761 | |
648 | ++fdchangecnt; |
762 | ++fdchangecnt; |
649 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
763 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
650 | fdchanges [fdchangecnt - 1] = w->fd; |
764 | fdchanges [fdchangecnt - 1] = w->fd; |
651 | } |
765 | } |
652 | |
|
|
653 | |
766 | |
654 | void |
767 | void |
655 | evtimer_start (struct ev_timer *w) |
768 | evtimer_start (struct ev_timer *w) |
656 | { |
769 | { |
657 | if (ev_is_active (w)) |
770 | if (ev_is_active (w)) |
… | |
… | |
668 | } |
781 | } |
669 | |
782 | |
670 | void |
783 | void |
671 | evtimer_stop (struct ev_timer *w) |
784 | evtimer_stop (struct ev_timer *w) |
672 | { |
785 | { |
|
|
786 | ev_clear ((W)w); |
673 | if (!ev_is_active (w)) |
787 | if (!ev_is_active (w)) |
674 | return; |
788 | return; |
675 | |
789 | |
676 | if (w->active < timercnt--) |
790 | if (w->active < timercnt--) |
677 | { |
791 | { |
… | |
… | |
720 | } |
834 | } |
721 | |
835 | |
722 | void |
836 | void |
723 | evperiodic_stop (struct ev_periodic *w) |
837 | evperiodic_stop (struct ev_periodic *w) |
724 | { |
838 | { |
|
|
839 | ev_clear ((W)w); |
725 | if (!ev_is_active (w)) |
840 | if (!ev_is_active (w)) |
726 | return; |
841 | return; |
727 | |
842 | |
728 | if (w->active < periodiccnt--) |
843 | if (w->active < periodiccnt--) |
729 | { |
844 | { |
… | |
… | |
755 | } |
870 | } |
756 | |
871 | |
757 | void |
872 | void |
758 | evsignal_stop (struct ev_signal *w) |
873 | evsignal_stop (struct ev_signal *w) |
759 | { |
874 | { |
|
|
875 | ev_clear ((W)w); |
760 | if (!ev_is_active (w)) |
876 | if (!ev_is_active (w)) |
761 | return; |
877 | return; |
762 | |
878 | |
763 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
879 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
764 | ev_stop ((W)w); |
880 | ev_stop ((W)w); |
… | |
… | |
777 | idles [idlecnt - 1] = w; |
893 | idles [idlecnt - 1] = w; |
778 | } |
894 | } |
779 | |
895 | |
780 | void evidle_stop (struct ev_idle *w) |
896 | void evidle_stop (struct ev_idle *w) |
781 | { |
897 | { |
|
|
898 | ev_clear ((W)w); |
|
|
899 | if (ev_is_active (w)) |
|
|
900 | return; |
|
|
901 | |
782 | idles [w->active - 1] = idles [--idlecnt]; |
902 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
903 | ev_stop ((W)w); |
|
|
904 | } |
|
|
905 | |
|
|
906 | void evprepare_start (struct ev_prepare *w) |
|
|
907 | { |
|
|
908 | if (ev_is_active (w)) |
|
|
909 | return; |
|
|
910 | |
|
|
911 | ev_start ((W)w, ++preparecnt); |
|
|
912 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
913 | prepares [preparecnt - 1] = w; |
|
|
914 | } |
|
|
915 | |
|
|
916 | void evprepare_stop (struct ev_prepare *w) |
|
|
917 | { |
|
|
918 | ev_clear ((W)w); |
|
|
919 | if (ev_is_active (w)) |
|
|
920 | return; |
|
|
921 | |
|
|
922 | prepares [w->active - 1] = prepares [--preparecnt]; |
783 | ev_stop ((W)w); |
923 | ev_stop ((W)w); |
784 | } |
924 | } |
785 | |
925 | |
786 | void evcheck_start (struct ev_check *w) |
926 | void evcheck_start (struct ev_check *w) |
787 | { |
927 | { |
… | |
… | |
793 | checks [checkcnt - 1] = w; |
933 | checks [checkcnt - 1] = w; |
794 | } |
934 | } |
795 | |
935 | |
796 | void evcheck_stop (struct ev_check *w) |
936 | void evcheck_stop (struct ev_check *w) |
797 | { |
937 | { |
|
|
938 | ev_clear ((W)w); |
|
|
939 | if (ev_is_active (w)) |
|
|
940 | return; |
|
|
941 | |
798 | checks [w->active - 1] = checks [--checkcnt]; |
942 | checks [w->active - 1] = checks [--checkcnt]; |
799 | ev_stop ((W)w); |
943 | ev_stop ((W)w); |
|
|
944 | } |
|
|
945 | |
|
|
946 | void evchild_start (struct ev_child *w) |
|
|
947 | { |
|
|
948 | if (ev_is_active (w)) |
|
|
949 | return; |
|
|
950 | |
|
|
951 | ev_start ((W)w, 1); |
|
|
952 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
953 | } |
|
|
954 | |
|
|
955 | void evchild_stop (struct ev_child *w) |
|
|
956 | { |
|
|
957 | ev_clear ((W)w); |
|
|
958 | if (ev_is_active (w)) |
|
|
959 | return; |
|
|
960 | |
|
|
961 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
962 | ev_stop ((W)w); |
|
|
963 | } |
|
|
964 | |
|
|
965 | /*****************************************************************************/ |
|
|
966 | |
|
|
967 | struct ev_once |
|
|
968 | { |
|
|
969 | struct ev_io io; |
|
|
970 | struct ev_timer to; |
|
|
971 | void (*cb)(int revents, void *arg); |
|
|
972 | void *arg; |
|
|
973 | }; |
|
|
974 | |
|
|
975 | static void |
|
|
976 | once_cb (struct ev_once *once, int revents) |
|
|
977 | { |
|
|
978 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
979 | void *arg = once->arg; |
|
|
980 | |
|
|
981 | evio_stop (&once->io); |
|
|
982 | evtimer_stop (&once->to); |
|
|
983 | free (once); |
|
|
984 | |
|
|
985 | cb (revents, arg); |
|
|
986 | } |
|
|
987 | |
|
|
988 | static void |
|
|
989 | once_cb_io (struct ev_io *w, int revents) |
|
|
990 | { |
|
|
991 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
992 | } |
|
|
993 | |
|
|
994 | static void |
|
|
995 | once_cb_to (struct ev_timer *w, int revents) |
|
|
996 | { |
|
|
997 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
998 | } |
|
|
999 | |
|
|
1000 | void |
|
|
1001 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
1002 | { |
|
|
1003 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
1004 | |
|
|
1005 | if (!once) |
|
|
1006 | cb (EV_ERROR, arg); |
|
|
1007 | else |
|
|
1008 | { |
|
|
1009 | once->cb = cb; |
|
|
1010 | once->arg = arg; |
|
|
1011 | |
|
|
1012 | evw_init (&once->io, once_cb_io); |
|
|
1013 | |
|
|
1014 | if (fd >= 0) |
|
|
1015 | { |
|
|
1016 | evio_set (&once->io, fd, events); |
|
|
1017 | evio_start (&once->io); |
|
|
1018 | } |
|
|
1019 | |
|
|
1020 | evw_init (&once->to, once_cb_to); |
|
|
1021 | |
|
|
1022 | if (timeout >= 0.) |
|
|
1023 | { |
|
|
1024 | evtimer_set (&once->to, timeout, 0.); |
|
|
1025 | evtimer_start (&once->to); |
|
|
1026 | } |
|
|
1027 | } |
800 | } |
1028 | } |
801 | |
1029 | |
802 | /*****************************************************************************/ |
1030 | /*****************************************************************************/ |
803 | |
1031 | |
804 | #if 0 |
1032 | #if 0 |