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

Comparing libev/ev.c (file contents):
Revision 1.13 by root, Wed Oct 31 10:50:05 2007 UTC vs.
Revision 1.23 by root, Wed Oct 31 20:10:17 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 69
39typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
40typedef struct ev_watcher_list *WL; 71typedef 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
126static int pendingmax, pendingcnt; 162static int pendingmax, pendingcnt;
127 163
128static void 164static void
129event (W w, int events) 165event (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
137static void 176static void
138fd_event (int fd, int events) 177fd_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 */
201static void
202fd_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);
159} 211}
160 212
161/*****************************************************************************/ 213/*****************************************************************************/
162 214
163static struct ev_timer **timers; 215static struct ev_timer **timers;
281/*****************************************************************************/ 333/*****************************************************************************/
282 334
283static struct ev_idle **idles; 335static struct ev_idle **idles;
284static int idlemax, idlecnt; 336static int idlemax, idlecnt;
285 337
338static struct ev_prepare **prepares;
339static int preparemax, preparecnt;
340
286static struct ev_check **checks; 341static struct ev_check **checks;
287static int checkmax, checkcnt; 342static int checkmax, checkcnt;
288 343
289/*****************************************************************************/ 344/*****************************************************************************/
290 345
346static struct ev_child *childs [PID_HASHSIZE];
347static struct ev_signal childev;
348
349#ifndef WCONTINUED
350# define WCONTINUED 0
351#endif
352
353static void
354childcb (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
291#if HAVE_EPOLL 370#if HAVE_EPOLL
292# include "ev_epoll.c" 371# include "ev_epoll.c"
293#endif 372#endif
294#if HAVE_SELECT 373#if HAVE_SELECT
295# include "ev_select.c" 374# include "ev_select.c"
296#endif 375#endif
297 376
298int ev_init (int flags) 377int ev_init (int flags)
299{ 378{
379 if (!ev_method)
380 {
300#if HAVE_MONOTONIC 381#if HAVE_MONOTONIC
301 { 382 {
302 struct timespec ts; 383 struct timespec ts;
303 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 384 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
304 have_monotonic = 1; 385 have_monotonic = 1;
305 } 386 }
306#endif 387#endif
307 388
308 ev_now = ev_time (); 389 ev_now = ev_time ();
309 now = get_clock (); 390 now = get_clock ();
310 diff = ev_now - now; 391 diff = ev_now - now;
311 392
312 if (pipe (sigpipe)) 393 if (pipe (sigpipe))
313 return 0; 394 return 0;
314 395
315 ev_method = EVMETHOD_NONE; 396 ev_method = EVMETHOD_NONE;
316#if HAVE_EPOLL 397#if HAVE_EPOLL
317 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 398 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
318#endif 399#endif
319#if HAVE_SELECT 400#if HAVE_SELECT
320 if (ev_method == EVMETHOD_NONE) select_init (flags); 401 if (ev_method == EVMETHOD_NONE) select_init (flags);
321#endif 402#endif
322 403
323 if (ev_method) 404 if (ev_method)
324 { 405 {
325 evw_init (&sigev, sigcb); 406 evw_init (&sigev, sigcb);
326 siginit (); 407 siginit ();
408
409 evsignal_init (&childev, childcb, SIGCHLD);
410 evsignal_start (&childev);
411 }
327 } 412 }
328 413
329 return ev_method; 414 return ev_method;
330} 415}
331 416
384} 469}
385 470
386static void 471static void
387call_pending () 472call_pending ()
388{ 473{
389 int i; 474 while (pendingcnt)
390
391 for (i = 0; i < pendingcnt; ++i)
392 { 475 {
393 ANPENDING *p = pendings + i; 476 ANPENDING *p = pendings + --pendingcnt;
394 477
395 if (p->w) 478 if (p->w)
396 { 479 {
397 p->w->pending = 0; 480 p->w->pending = 0;
398 p->w->cb (p->w, p->events); 481 p->w->cb (p->w, p->events);
399 } 482 }
400 } 483 }
401
402 pendingcnt = 0;
403} 484}
404 485
405static void 486static void
406timers_reify () 487timers_reify ()
407{ 488{
408 while (timercnt && timers [0]->at <= now) 489 while (timercnt && timers [0]->at <= now)
409 { 490 {
410 struct ev_timer *w = timers [0]; 491 struct ev_timer *w = timers [0];
492
493 event ((W)w, EV_TIMEOUT);
411 494
412 /* first reschedule or stop timer */ 495 /* first reschedule or stop timer */
413 if (w->repeat) 496 if (w->repeat)
414 { 497 {
415 w->at = now + w->repeat; 498 w->at = now + w->repeat;
416 assert (("timer timeout in the past, negative repeat?", w->at > now)); 499 assert (("timer timeout in the past, negative repeat?", w->at > now));
417 downheap ((WT *)timers, timercnt, 0); 500 downheap ((WT *)timers, timercnt, 0);
418 } 501 }
419 else 502 else
420 evtimer_stop (w); /* nonrepeating: stop timer */ 503 evtimer_stop (w); /* nonrepeating: stop timer */
421
422 event ((W)w, EV_TIMEOUT);
423 } 504 }
424} 505}
425 506
426static void 507static void
427periodics_reify () 508periodics_reify ()
514void ev_loop (int flags) 595void ev_loop (int flags)
515{ 596{
516 double block; 597 double block;
517 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 598 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
518 599
519 if (checkcnt)
520 {
521 queue_events ((W *)checks, checkcnt, EV_CHECK);
522 call_pending ();
523 }
524
525 do 600 do
526 { 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
527 /* update fd-related kernel structures */ 609 /* update fd-related kernel structures */
528 fd_reify (); 610 fd_reify ();
529 611
530 /* calculate blocking time */ 612 /* calculate blocking time */
531 613
532 /* 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 */
533 ev_now = ev_time (); 616 ev_now = ev_time ();
534 617
535 if (flags & EVLOOP_NONBLOCK || idlecnt) 618 if (flags & EVLOOP_NONBLOCK || idlecnt)
536 block = 0.; 619 block = 0.;
537 else 620 else
538 { 621 {
539 block = MAX_BLOCKTIME; 622 block = MAX_BLOCKTIME;
540 623
541 if (timercnt) 624 if (timercnt)
542 { 625 {
543 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;
544 if (block > to) block = to; 627 if (block > to) block = to;
545 } 628 }
546 629
547 if (periodiccnt) 630 if (periodiccnt)
548 { 631 {
557 640
558 /* update ev_now, do magic */ 641 /* update ev_now, do magic */
559 time_update (); 642 time_update ();
560 643
561 /* queue pending timers and reschedule them */ 644 /* queue pending timers and reschedule them */
645 timers_reify (); /* relative timers called last */
562 periodics_reify (); /* absolute timers first */ 646 periodics_reify (); /* absolute timers called first */
563 timers_reify (); /* relative timers second */
564 647
565 /* queue idle watchers unless io or timers are pending */ 648 /* queue idle watchers unless io or timers are pending */
566 if (!pendingcnt) 649 if (!pendingcnt)
567 queue_events ((W *)idles, idlecnt, EV_IDLE); 650 queue_events ((W *)idles, idlecnt, EV_IDLE);
568 651
569 /* queue check and possibly idle watchers */ 652 /* queue check watchers, to be executed first */
653 if (checkcnt)
570 queue_events ((W *)checks, checkcnt, EV_CHECK); 654 queue_events ((W *)checks, checkcnt, EV_CHECK);
571 655
572 call_pending (); 656 call_pending ();
573 } 657 }
574 while (!ev_loop_done); 658 while (!ev_loop_done);
575 659
600 head = &(*head)->next; 684 head = &(*head)->next;
601 } 685 }
602} 686}
603 687
604static void 688static void
689ev_clear (W w)
690{
691 if (w->pending)
692 {
693 pendings [w->pending - 1].w = 0;
694 w->pending = 0;
695 }
696}
697
698static void
605ev_start (W w, int active) 699ev_start (W w, int active)
606{ 700{
607 w->pending = 0;
608 w->active = active; 701 w->active = active;
609} 702}
610 703
611static void 704static void
612ev_stop (W w) 705ev_stop (W w)
613{ 706{
614 if (w->pending)
615 pendings [w->pending - 1].w = 0;
616
617 w->active = 0; 707 w->active = 0;
618} 708}
619 709
620/*****************************************************************************/ 710/*****************************************************************************/
621 711
637} 727}
638 728
639void 729void
640evio_stop (struct ev_io *w) 730evio_stop (struct ev_io *w)
641{ 731{
732 ev_clear ((W)w);
642 if (!ev_is_active (w)) 733 if (!ev_is_active (w))
643 return; 734 return;
644 735
645 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 736 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
646 ev_stop ((W)w); 737 ev_stop ((W)w);
647 738
648 ++fdchangecnt; 739 ++fdchangecnt;
649 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 740 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
650 fdchanges [fdchangecnt - 1] = w->fd; 741 fdchanges [fdchangecnt - 1] = w->fd;
651} 742}
652
653 743
654void 744void
655evtimer_start (struct ev_timer *w) 745evtimer_start (struct ev_timer *w)
656{ 746{
657 if (ev_is_active (w)) 747 if (ev_is_active (w))
668} 758}
669 759
670void 760void
671evtimer_stop (struct ev_timer *w) 761evtimer_stop (struct ev_timer *w)
672{ 762{
763 ev_clear ((W)w);
673 if (!ev_is_active (w)) 764 if (!ev_is_active (w))
674 return; 765 return;
675 766
676 if (w->active < timercnt--) 767 if (w->active < timercnt--)
677 { 768 {
678 timers [w->active - 1] = timers [timercnt]; 769 timers [w->active - 1] = timers [timercnt];
679 downheap ((WT *)timers, timercnt, w->active - 1); 770 downheap ((WT *)timers, timercnt, w->active - 1);
680 } 771 }
681 772
773 w->at = w->repeat;
774
682 ev_stop ((W)w); 775 ev_stop ((W)w);
776}
777
778void
779evtimer_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);
683} 793}
684 794
685void 795void
686evperiodic_start (struct ev_periodic *w) 796evperiodic_start (struct ev_periodic *w)
687{ 797{
701} 811}
702 812
703void 813void
704evperiodic_stop (struct ev_periodic *w) 814evperiodic_stop (struct ev_periodic *w)
705{ 815{
816 ev_clear ((W)w);
706 if (!ev_is_active (w)) 817 if (!ev_is_active (w))
707 return; 818 return;
708 819
709 if (w->active < periodiccnt--) 820 if (w->active < periodiccnt--)
710 { 821 {
736} 847}
737 848
738void 849void
739evsignal_stop (struct ev_signal *w) 850evsignal_stop (struct ev_signal *w)
740{ 851{
852 ev_clear ((W)w);
741 if (!ev_is_active (w)) 853 if (!ev_is_active (w))
742 return; 854 return;
743 855
744 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 856 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
745 ev_stop ((W)w); 857 ev_stop ((W)w);
758 idles [idlecnt - 1] = w; 870 idles [idlecnt - 1] = w;
759} 871}
760 872
761void evidle_stop (struct ev_idle *w) 873void evidle_stop (struct ev_idle *w)
762{ 874{
875 ev_clear ((W)w);
876 if (ev_is_active (w))
877 return;
878
763 idles [w->active - 1] = idles [--idlecnt]; 879 idles [w->active - 1] = idles [--idlecnt];
880 ev_stop ((W)w);
881}
882
883void 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
893void 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];
764 ev_stop ((W)w); 900 ev_stop ((W)w);
765} 901}
766 902
767void evcheck_start (struct ev_check *w) 903void evcheck_start (struct ev_check *w)
768{ 904{
774 checks [checkcnt - 1] = w; 910 checks [checkcnt - 1] = w;
775} 911}
776 912
777void evcheck_stop (struct ev_check *w) 913void evcheck_stop (struct ev_check *w)
778{ 914{
915 ev_clear ((W)w);
916 if (ev_is_active (w))
917 return;
918
779 checks [w->active - 1] = checks [--checkcnt]; 919 checks [w->active - 1] = checks [--checkcnt];
780 ev_stop ((W)w); 920 ev_stop ((W)w);
921}
922
923void 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
932void 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
944struct 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
952static void
953once_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
965static void
966once_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
971static void
972once_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
977void
978ev_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 }
781} 1005}
782 1006
783/*****************************************************************************/ 1007/*****************************************************************************/
784 1008
785#if 0 1009#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines