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

Comparing libev/ev.c (file contents):
Revision 1.15 by root, Wed Oct 31 11:56:34 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; \
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
125static int pendingmax, pendingcnt; 162static int pendingmax, pendingcnt;
126 163
127static void 164static void
128event (W w, int events) 165event (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
136static void 176static void
137fd_event (int fd, int events) 177fd_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 */
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);
158} 211}
159 212
160/*****************************************************************************/ 213/*****************************************************************************/
161 214
162static struct ev_timer **timers; 215static struct ev_timer **timers;
280/*****************************************************************************/ 333/*****************************************************************************/
281 334
282static struct ev_idle **idles; 335static struct ev_idle **idles;
283static int idlemax, idlecnt; 336static int idlemax, idlecnt;
284 337
338static struct ev_prepare **prepares;
339static int preparemax, preparecnt;
340
285static struct ev_check **checks; 341static struct ev_check **checks;
286static int checkmax, checkcnt; 342static int checkmax, checkcnt;
287 343
288/*****************************************************************************/ 344/*****************************************************************************/
289 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
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
297int ev_init (int flags) 377int 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
385static void 471static void
386call_pending () 472call_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
404static void 486static void
405timers_reify () 487timers_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
425static void 507static void
426periodics_reify () 508periodics_reify ()
513void ev_loop (int flags) 595void 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
603static 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
604ev_start (W w, int active) 699ev_start (W w, int active)
605{ 700{
606 w->pending = 0;
607 w->active = active; 701 w->active = active;
608} 702}
609 703
610static void 704static void
611ev_stop (W w) 705ev_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
638void 729void
639evio_stop (struct ev_io *w) 730evio_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
653void 744void
654evtimer_start (struct ev_timer *w) 745evtimer_start (struct ev_timer *w)
655{ 746{
656 if (ev_is_active (w)) 747 if (ev_is_active (w))
667} 758}
668 759
669void 760void
670evtimer_stop (struct ev_timer *w) 761evtimer_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
721void 813void
722evperiodic_stop (struct ev_periodic *w) 814evperiodic_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
756void 849void
757evsignal_stop (struct ev_signal *w) 850evsignal_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
779void evidle_stop (struct ev_idle *w) 873void 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
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];
782 ev_stop ((W)w); 900 ev_stop ((W)w);
783} 901}
784 902
785void evcheck_start (struct ev_check *w) 903void evcheck_start (struct ev_check *w)
786{ 904{
792 checks [checkcnt - 1] = w; 910 checks [checkcnt - 1] = w;
793} 911}
794 912
795void evcheck_stop (struct ev_check *w) 913void 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
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 }
799} 1005}
800 1006
801/*****************************************************************************/ 1007/*****************************************************************************/
802 1008
803#if 0 1009#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines