ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.12 by root, Wed Oct 31 09:23:17 2007 UTC vs.
Revision 1.24 by root, Wed Oct 31 20:46:44 2007 UTC

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
39struct ev_watcher {
40 EV_WATCHER (ev_watcher);
41};
42
43struct ev_watcher_list {
44 EV_WATCHER_LIST (ev_watcher_list);
45};
46
47struct ev_watcher_time {
48 EV_WATCHER_TIME (ev_watcher_time);
49};
50 69
51typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
52typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
53typedef struct ev_watcher_time *WT; 72typedef 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
138static int pendingmax, pendingcnt; 162static int pendingmax, pendingcnt;
139 163
140static void 164static void
141event (W w, int events) 165event (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
149static void 176static void
150fd_event (int fd, int events) 177fd_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 */
201static void
202fd_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 }
171} 214}
172 215
173/*****************************************************************************/ 216/*****************************************************************************/
174 217
175static struct ev_timer **timers; 218static struct ev_timer **timers;
293/*****************************************************************************/ 336/*****************************************************************************/
294 337
295static struct ev_idle **idles; 338static struct ev_idle **idles;
296static int idlemax, idlecnt; 339static int idlemax, idlecnt;
297 340
341static struct ev_prepare **prepares;
342static int preparemax, preparecnt;
343
298static struct ev_check **checks; 344static struct ev_check **checks;
299static int checkmax, checkcnt; 345static int checkmax, checkcnt;
300 346
301/*****************************************************************************/ 347/*****************************************************************************/
302 348
349static struct ev_child *childs [PID_HASHSIZE];
350static struct ev_signal childev;
351
352#ifndef WCONTINUED
353# define WCONTINUED 0
354#endif
355
356static void
357childcb (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
303#if HAVE_EPOLL 373#if HAVE_EPOLL
304# include "ev_epoll.c" 374# include "ev_epoll.c"
305#endif 375#endif
306#if HAVE_SELECT 376#if HAVE_SELECT
307# include "ev_select.c" 377# include "ev_select.c"
308#endif 378#endif
309 379
380int
381ev_version_major (void)
382{
383 return EV_VERSION_MAJOR;
384}
385
386int
387ev_version_minor (void)
388{
389 return EV_VERSION_MINOR;
390}
391
310int ev_init (int flags) 392int ev_init (int flags)
311{ 393{
394 if (!ev_method)
395 {
312#if HAVE_MONOTONIC 396#if HAVE_MONOTONIC
313 { 397 {
314 struct timespec ts; 398 struct timespec ts;
315 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 399 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
316 have_monotonic = 1; 400 have_monotonic = 1;
317 } 401 }
318#endif 402#endif
319 403
320 ev_now = ev_time (); 404 ev_now = ev_time ();
321 now = get_clock (); 405 now = get_clock ();
322 diff = ev_now - now; 406 diff = ev_now - now;
323 407
324 if (pipe (sigpipe)) 408 if (pipe (sigpipe))
325 return 0; 409 return 0;
326 410
327 ev_method = EVMETHOD_NONE; 411 ev_method = EVMETHOD_NONE;
328#if HAVE_EPOLL 412#if HAVE_EPOLL
329 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 413 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
330#endif 414#endif
331#if HAVE_SELECT 415#if HAVE_SELECT
332 if (ev_method == EVMETHOD_NONE) select_init (flags); 416 if (ev_method == EVMETHOD_NONE) select_init (flags);
333#endif 417#endif
334 418
335 if (ev_method) 419 if (ev_method)
336 { 420 {
337 evw_init (&sigev, sigcb); 421 evw_init (&sigev, sigcb);
338 siginit (); 422 siginit ();
423
424 evsignal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev);
426 }
339 } 427 }
340 428
341 return ev_method; 429 return ev_method;
342} 430}
343 431
344/*****************************************************************************/ 432/*****************************************************************************/
345 433
434void
346void ev_prefork (void) 435ev_prefork (void)
347{ 436{
348 /* nop */ 437 /* nop */
349} 438}
350 439
440void
351void ev_postfork_parent (void) 441ev_postfork_parent (void)
352{ 442{
353 /* nop */ 443 /* nop */
354} 444}
355 445
446void
356void ev_postfork_child (void) 447ev_postfork_child (void)
357{ 448{
358#if HAVE_EPOLL 449#if HAVE_EPOLL
359 if (ev_method == EVMETHOD_EPOLL) 450 if (ev_method == EVMETHOD_EPOLL)
360 epoll_postfork_child (); 451 epoll_postfork_child ();
361#endif 452#endif
394 485
395 fdchangecnt = 0; 486 fdchangecnt = 0;
396} 487}
397 488
398static void 489static void
399call_pending () 490call_pending (void)
400{ 491{
401 int i; 492 while (pendingcnt)
402
403 for (i = 0; i < pendingcnt; ++i)
404 { 493 {
405 ANPENDING *p = pendings + i; 494 ANPENDING *p = pendings + --pendingcnt;
406 495
407 if (p->w) 496 if (p->w)
408 { 497 {
409 p->w->pending = 0; 498 p->w->pending = 0;
410 p->w->cb (p->w, p->events); 499 p->w->cb (p->w, p->events);
411 } 500 }
412 } 501 }
413
414 pendingcnt = 0;
415} 502}
416 503
417static void 504static void
418timers_reify () 505timers_reify (void)
419{ 506{
420 while (timercnt && timers [0]->at <= now) 507 while (timercnt && timers [0]->at <= now)
421 { 508 {
422 struct ev_timer *w = timers [0]; 509 struct ev_timer *w = timers [0];
510
511 event ((W)w, EV_TIMEOUT);
423 512
424 /* first reschedule or stop timer */ 513 /* first reschedule or stop timer */
425 if (w->repeat) 514 if (w->repeat)
426 { 515 {
427 w->at = now + w->repeat; 516 w->at = now + w->repeat;
428 assert (("timer timeout in the past, negative repeat?", w->at > now)); 517 assert (("timer timeout in the past, negative repeat?", w->at > now));
429 downheap ((WT *)timers, timercnt, 0); 518 downheap ((WT *)timers, timercnt, 0);
430 } 519 }
431 else 520 else
432 evtimer_stop (w); /* nonrepeating: stop timer */ 521 evtimer_stop (w); /* nonrepeating: stop timer */
433
434 event ((W)w, EV_TIMEOUT);
435 } 522 }
436} 523}
437 524
438static void 525static void
439periodics_reify () 526periodics_reify (void)
440{ 527{
441 while (periodiccnt && periodics [0]->at <= ev_now) 528 while (periodiccnt && periodics [0]->at <= ev_now)
442 { 529 {
443 struct ev_periodic *w = periodics [0]; 530 struct ev_periodic *w = periodics [0];
444 531
455 event ((W)w, EV_TIMEOUT); 542 event ((W)w, EV_TIMEOUT);
456 } 543 }
457} 544}
458 545
459static void 546static void
460time_jump (ev_tstamp diff) 547periodics_reschedule (ev_tstamp diff)
461{ 548{
462 int i; 549 int i;
463 550
464 /* adjust periodics */ 551 /* adjust periodics after time jump */
465 for (i = 0; i < periodiccnt; ++i) 552 for (i = 0; i < periodiccnt; ++i)
466 { 553 {
467 struct ev_periodic *w = periodics [i]; 554 struct ev_periodic *w = periodics [i];
468 555
469 if (w->interval) 556 if (w->interval)
477 564
478 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 565 i = 0; /* restart loop, inefficient, but time jumps should be rare */
479 } 566 }
480 } 567 }
481 } 568 }
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} 569}
487 570
488static void 571static void
489time_update () 572time_update (void)
490{ 573{
491 int i; 574 int i;
492 575
493 ev_now = ev_time (); 576 ev_now = ev_time ();
494 577
505 return; /* all is well */ 588 return; /* all is well */
506 589
507 ev_now = ev_time (); 590 ev_now = ev_time ();
508 } 591 }
509 592
510 time_jump (diff - odiff); 593 periodics_reschedule (diff - odiff);
594 /* no timer adjustment, as the monotonic clock doesn't jump */
511 } 595 }
512 else 596 else
513 { 597 {
514 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 598 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
515 time_jump (ev_now - now); 599 {
600 periodics_reschedule (ev_now - now);
601
602 /* adjust timers. this is easy, as the offset is the same for all */
603 for (i = 0; i < timercnt; ++i)
604 timers [i]->at += diff;
605 }
516 606
517 now = ev_now; 607 now = ev_now;
518 } 608 }
519} 609}
520 610
521int ev_loop_done; 611int ev_loop_done;
522 612
523void ev_loop (int flags) 613void ev_loop (int flags)
524{ 614{
525 double block; 615 double block;
526 ev_loop_done = flags & EVLOOP_ONESHOT; 616 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 617
534 do 618 do
535 { 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
536 /* update fd-related kernel structures */ 627 /* update fd-related kernel structures */
537 fd_reify (); 628 fd_reify ();
538 629
539 /* calculate blocking time */ 630 /* calculate blocking time */
540 631
541 /* 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 */
542 ev_now = ev_time (); 634 ev_now = ev_time ();
543 635
544 if (flags & EVLOOP_NONBLOCK || idlecnt) 636 if (flags & EVLOOP_NONBLOCK || idlecnt)
545 block = 0.; 637 block = 0.;
546 else 638 else
547 { 639 {
548 block = MAX_BLOCKTIME; 640 block = MAX_BLOCKTIME;
549 641
550 if (timercnt) 642 if (timercnt)
551 { 643 {
552 ev_tstamp to = timers [0]->at - get_clock () + method_fudge; 644 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
553 if (block > to) block = to; 645 if (block > to) block = to;
554 } 646 }
555 647
556 if (periodiccnt) 648 if (periodiccnt)
557 { 649 {
566 658
567 /* update ev_now, do magic */ 659 /* update ev_now, do magic */
568 time_update (); 660 time_update ();
569 661
570 /* queue pending timers and reschedule them */ 662 /* queue pending timers and reschedule them */
663 timers_reify (); /* relative timers called last */
571 periodics_reify (); /* absolute timers first */ 664 periodics_reify (); /* absolute timers called first */
572 timers_reify (); /* relative timers second */
573 665
574 /* queue idle watchers unless io or timers are pending */ 666 /* queue idle watchers unless io or timers are pending */
575 if (!pendingcnt) 667 if (!pendingcnt)
576 queue_events ((W *)idles, idlecnt, EV_IDLE); 668 queue_events ((W *)idles, idlecnt, EV_IDLE);
577 669
578 /* queue check and possibly idle watchers */ 670 /* queue check watchers, to be executed first */
671 if (checkcnt)
579 queue_events ((W *)checks, checkcnt, EV_CHECK); 672 queue_events ((W *)checks, checkcnt, EV_CHECK);
580 673
581 call_pending (); 674 call_pending ();
582 } 675 }
583 while (!ev_loop_done); 676 while (!ev_loop_done);
677
678 if (ev_loop_done != 2)
679 ev_loop_done = 0;
584} 680}
585 681
586/*****************************************************************************/ 682/*****************************************************************************/
587 683
588static void 684static void
606 head = &(*head)->next; 702 head = &(*head)->next;
607 } 703 }
608} 704}
609 705
610static void 706static void
707ev_clear (W w)
708{
709 if (w->pending)
710 {
711 pendings [w->pending - 1].w = 0;
712 w->pending = 0;
713 }
714}
715
716static void
611ev_start (W w, int active) 717ev_start (W w, int active)
612{ 718{
613 w->pending = 0;
614 w->active = active; 719 w->active = active;
615} 720}
616 721
617static void 722static void
618ev_stop (W w) 723ev_stop (W w)
619{ 724{
620 if (w->pending)
621 pendings [w->pending - 1].w = 0;
622
623 w->active = 0; 725 w->active = 0;
624} 726}
625 727
626/*****************************************************************************/ 728/*****************************************************************************/
627 729
643} 745}
644 746
645void 747void
646evio_stop (struct ev_io *w) 748evio_stop (struct ev_io *w)
647{ 749{
750 ev_clear ((W)w);
648 if (!ev_is_active (w)) 751 if (!ev_is_active (w))
649 return; 752 return;
650 753
651 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 754 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
652 ev_stop ((W)w); 755 ev_stop ((W)w);
654 ++fdchangecnt; 757 ++fdchangecnt;
655 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 758 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
656 fdchanges [fdchangecnt - 1] = w->fd; 759 fdchanges [fdchangecnt - 1] = w->fd;
657} 760}
658 761
659
660void 762void
661evtimer_start (struct ev_timer *w) 763evtimer_start (struct ev_timer *w)
662{ 764{
663 if (ev_is_active (w)) 765 if (ev_is_active (w))
664 return; 766 return;
665 767
666 w->at += now; 768 w->at += now;
769
770 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
667 771
668 ev_start ((W)w, ++timercnt); 772 ev_start ((W)w, ++timercnt);
669 array_needsize (timers, timermax, timercnt, ); 773 array_needsize (timers, timermax, timercnt, );
670 timers [timercnt - 1] = w; 774 timers [timercnt - 1] = w;
671 upheap ((WT *)timers, timercnt - 1); 775 upheap ((WT *)timers, timercnt - 1);
672} 776}
673 777
674void 778void
675evtimer_stop (struct ev_timer *w) 779evtimer_stop (struct ev_timer *w)
676{ 780{
781 ev_clear ((W)w);
677 if (!ev_is_active (w)) 782 if (!ev_is_active (w))
678 return; 783 return;
679 784
680 if (w->active < timercnt--) 785 if (w->active < timercnt--)
681 { 786 {
682 timers [w->active - 1] = timers [timercnt]; 787 timers [w->active - 1] = timers [timercnt];
683 downheap ((WT *)timers, timercnt, w->active - 1); 788 downheap ((WT *)timers, timercnt, w->active - 1);
684 } 789 }
685 790
791 w->at = w->repeat;
792
686 ev_stop ((W)w); 793 ev_stop ((W)w);
687} 794}
688 795
689void 796void
797evtimer_again (struct ev_timer *w)
798{
799 if (ev_is_active (w))
800 {
801 if (w->repeat)
802 {
803 w->at = now + w->repeat;
804 downheap ((WT *)timers, timercnt, w->active - 1);
805 }
806 else
807 evtimer_stop (w);
808 }
809 else if (w->repeat)
810 evtimer_start (w);
811}
812
813void
690evperiodic_start (struct ev_periodic *w) 814evperiodic_start (struct ev_periodic *w)
691{ 815{
692 if (ev_is_active (w)) 816 if (ev_is_active (w))
693 return; 817 return;
818
819 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
694 820
695 /* this formula differs from the one in periodic_reify because we do not always round up */ 821 /* this formula differs from the one in periodic_reify because we do not always round up */
696 if (w->interval) 822 if (w->interval)
697 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 823 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
698 824
703} 829}
704 830
705void 831void
706evperiodic_stop (struct ev_periodic *w) 832evperiodic_stop (struct ev_periodic *w)
707{ 833{
834 ev_clear ((W)w);
708 if (!ev_is_active (w)) 835 if (!ev_is_active (w))
709 return; 836 return;
710 837
711 if (w->active < periodiccnt--) 838 if (w->active < periodiccnt--)
712 { 839 {
738} 865}
739 866
740void 867void
741evsignal_stop (struct ev_signal *w) 868evsignal_stop (struct ev_signal *w)
742{ 869{
870 ev_clear ((W)w);
743 if (!ev_is_active (w)) 871 if (!ev_is_active (w))
744 return; 872 return;
745 873
746 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 874 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
747 ev_stop ((W)w); 875 ev_stop ((W)w);
760 idles [idlecnt - 1] = w; 888 idles [idlecnt - 1] = w;
761} 889}
762 890
763void evidle_stop (struct ev_idle *w) 891void evidle_stop (struct ev_idle *w)
764{ 892{
893 ev_clear ((W)w);
894 if (ev_is_active (w))
895 return;
896
765 idles [w->active - 1] = idles [--idlecnt]; 897 idles [w->active - 1] = idles [--idlecnt];
898 ev_stop ((W)w);
899}
900
901void evprepare_start (struct ev_prepare *w)
902{
903 if (ev_is_active (w))
904 return;
905
906 ev_start ((W)w, ++preparecnt);
907 array_needsize (prepares, preparemax, preparecnt, );
908 prepares [preparecnt - 1] = w;
909}
910
911void evprepare_stop (struct ev_prepare *w)
912{
913 ev_clear ((W)w);
914 if (ev_is_active (w))
915 return;
916
917 prepares [w->active - 1] = prepares [--preparecnt];
766 ev_stop ((W)w); 918 ev_stop ((W)w);
767} 919}
768 920
769void evcheck_start (struct ev_check *w) 921void evcheck_start (struct ev_check *w)
770{ 922{
776 checks [checkcnt - 1] = w; 928 checks [checkcnt - 1] = w;
777} 929}
778 930
779void evcheck_stop (struct ev_check *w) 931void evcheck_stop (struct ev_check *w)
780{ 932{
933 ev_clear ((W)w);
934 if (ev_is_active (w))
935 return;
936
781 checks [w->active - 1] = checks [--checkcnt]; 937 checks [w->active - 1] = checks [--checkcnt];
782 ev_stop ((W)w); 938 ev_stop ((W)w);
783} 939}
784 940
785/*****************************************************************************/ 941void evchild_start (struct ev_child *w)
942{
943 if (ev_is_active (w))
944 return;
786 945
946 ev_start ((W)w, 1);
947 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
948}
949
950void evchild_stop (struct ev_child *w)
951{
952 ev_clear ((W)w);
953 if (ev_is_active (w))
954 return;
955
956 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
957 ev_stop ((W)w);
958}
959
960/*****************************************************************************/
961
962struct ev_once
963{
964 struct ev_io io;
965 struct ev_timer to;
966 void (*cb)(int revents, void *arg);
967 void *arg;
968};
969
970static void
971once_cb (struct ev_once *once, int revents)
972{
973 void (*cb)(int revents, void *arg) = once->cb;
974 void *arg = once->arg;
975
976 evio_stop (&once->io);
977 evtimer_stop (&once->to);
978 free (once);
979
980 cb (revents, arg);
981}
982
983static void
984once_cb_io (struct ev_io *w, int revents)
985{
986 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
987}
988
989static void
990once_cb_to (struct ev_timer *w, int revents)
991{
992 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
993}
994
995void
996ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
997{
998 struct ev_once *once = malloc (sizeof (struct ev_once));
999
1000 if (!once)
1001 cb (EV_ERROR, arg);
1002 else
1003 {
1004 once->cb = cb;
1005 once->arg = arg;
1006
1007 evw_init (&once->io, once_cb_io);
1008
1009 if (fd >= 0)
1010 {
1011 evio_set (&once->io, fd, events);
1012 evio_start (&once->io);
1013 }
1014
1015 evw_init (&once->to, once_cb_to);
1016
1017 if (timeout >= 0.)
1018 {
1019 evtimer_set (&once->to, timeout, 0.);
1020 evtimer_start (&once->to);
1021 }
1022 }
1023}
1024
1025/*****************************************************************************/
1026
787#if 1 1027#if 0
788 1028
789struct ev_io wio; 1029struct ev_io wio;
790 1030
791static void 1031static void
792sin_cb (struct ev_io *w, int revents) 1032sin_cb (struct ev_io *w, int revents)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines