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

Comparing libev/ev.c (file contents):
Revision 1.209 by root, Tue Feb 5 23:56:33 2008 UTC vs.
Revision 1.220 by root, Sun Apr 6 09:53:17 2008 UTC

39 39
40#ifdef __cplusplus 40#ifdef __cplusplus
41extern "C" { 41extern "C" {
42#endif 42#endif
43 43
44/* this big block deduces configuration from config.h */
44#ifndef EV_STANDALONE 45#ifndef EV_STANDALONE
45# ifdef EV_CONFIG_H 46# ifdef EV_CONFIG_H
46# include EV_CONFIG_H 47# include EV_CONFIG_H
47# else 48# else
48# include "config.h" 49# include "config.h"
118# else 119# else
119# define EV_USE_INOTIFY 0 120# define EV_USE_INOTIFY 0
120# endif 121# endif
121# endif 122# endif
122 123
124# ifndef EV_USE_EVENTFD
125# if HAVE_EVENTFD
126# define EV_USE_EVENTFD 1
127# else
128# define EV_USE_EVENTFD 0
129# endif
130# endif
131
123#endif 132#endif
124 133
125#include <math.h> 134#include <math.h>
126#include <stdlib.h> 135#include <stdlib.h>
127#include <fcntl.h> 136#include <fcntl.h>
152# ifndef EV_SELECT_IS_WINSOCKET 161# ifndef EV_SELECT_IS_WINSOCKET
153# define EV_SELECT_IS_WINSOCKET 1 162# define EV_SELECT_IS_WINSOCKET 1
154# endif 163# endif
155#endif 164#endif
156 165
157/**/ 166/* this block tries to deduce configuration from header-defined symbols and defaults */
158 167
159#ifndef EV_USE_MONOTONIC 168#ifndef EV_USE_MONOTONIC
160# define EV_USE_MONOTONIC 0 169# define EV_USE_MONOTONIC 0
161#endif 170#endif
162 171
179# define EV_USE_POLL 1 188# define EV_USE_POLL 1
180# endif 189# endif
181#endif 190#endif
182 191
183#ifndef EV_USE_EPOLL 192#ifndef EV_USE_EPOLL
193# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
194# define EV_USE_EPOLL 1
195# else
184# define EV_USE_EPOLL 0 196# define EV_USE_EPOLL 0
197# endif
185#endif 198#endif
186 199
187#ifndef EV_USE_KQUEUE 200#ifndef EV_USE_KQUEUE
188# define EV_USE_KQUEUE 0 201# define EV_USE_KQUEUE 0
189#endif 202#endif
191#ifndef EV_USE_PORT 204#ifndef EV_USE_PORT
192# define EV_USE_PORT 0 205# define EV_USE_PORT 0
193#endif 206#endif
194 207
195#ifndef EV_USE_INOTIFY 208#ifndef EV_USE_INOTIFY
209# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
210# define EV_USE_INOTIFY 1
211# else
196# define EV_USE_INOTIFY 0 212# define EV_USE_INOTIFY 0
213# endif
197#endif 214#endif
198 215
199#ifndef EV_PID_HASHSIZE 216#ifndef EV_PID_HASHSIZE
200# if EV_MINIMAL 217# if EV_MINIMAL
201# define EV_PID_HASHSIZE 1 218# define EV_PID_HASHSIZE 1
210# else 227# else
211# define EV_INOTIFY_HASHSIZE 16 228# define EV_INOTIFY_HASHSIZE 16
212# endif 229# endif
213#endif 230#endif
214 231
215/**/ 232#ifndef EV_USE_EVENTFD
233# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
234# define EV_USE_EVENTFD 1
235# else
236# define EV_USE_EVENTFD 0
237# endif
238#endif
239
240/* this block fixes any misconfiguration where we know we run into trouble otherwise */
216 241
217#ifndef CLOCK_MONOTONIC 242#ifndef CLOCK_MONOTONIC
218# undef EV_USE_MONOTONIC 243# undef EV_USE_MONOTONIC
219# define EV_USE_MONOTONIC 0 244# define EV_USE_MONOTONIC 0
220#endif 245#endif
239# include <sys/inotify.h> 264# include <sys/inotify.h>
240#endif 265#endif
241 266
242#if EV_SELECT_IS_WINSOCKET 267#if EV_SELECT_IS_WINSOCKET
243# include <winsock.h> 268# include <winsock.h>
269#endif
270
271#if EV_USE_EVENTFD
272/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
273int eventfd (unsigned int initval, int flags);
244#endif 274#endif
245 275
246/**/ 276/**/
247 277
248/* 278/*
451 ts.tv_sec = (time_t)delay; 481 ts.tv_sec = (time_t)delay;
452 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9); 482 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
453 483
454 nanosleep (&ts, 0); 484 nanosleep (&ts, 0);
455#elif defined(_WIN32) 485#elif defined(_WIN32)
456 Sleep (delay * 1e3); 486 Sleep ((unsigned long)(delay * 1e3));
457#else 487#else
458 struct timeval tv; 488 struct timeval tv;
459 489
460 tv.tv_sec = (time_t)delay; 490 tv.tv_sec = (time_t)delay;
461 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 491 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
802static void noinline 832static void noinline
803evpipe_init (EV_P) 833evpipe_init (EV_P)
804{ 834{
805 if (!ev_is_active (&pipeev)) 835 if (!ev_is_active (&pipeev))
806 { 836 {
837#if EV_USE_EVENTFD
838 if ((evfd = eventfd (0, 0)) >= 0)
839 {
840 evpipe [0] = -1;
841 fd_intern (evfd);
842 ev_io_set (&pipeev, evfd, EV_READ);
843 }
844 else
845#endif
846 {
807 while (pipe (evpipe)) 847 while (pipe (evpipe))
808 syserr ("(libev) error creating signal/async pipe"); 848 syserr ("(libev) error creating signal/async pipe");
809 849
810 fd_intern (evpipe [0]); 850 fd_intern (evpipe [0]);
811 fd_intern (evpipe [1]); 851 fd_intern (evpipe [1]);
812
813 ev_io_set (&pipeev, evpipe [0], EV_READ); 852 ev_io_set (&pipeev, evpipe [0], EV_READ);
853 }
854
814 ev_io_start (EV_A_ &pipeev); 855 ev_io_start (EV_A_ &pipeev);
815 ev_unref (EV_A); /* child watcher should not keep loop alive */ 856 ev_unref (EV_A); /* watcher should not keep loop alive */
816 } 857 }
817} 858}
818 859
819void inline_size 860void inline_size
820evpipe_write (EV_P_ int sig, int async) 861evpipe_write (EV_P_ EV_ATOMIC_T *flag)
821{ 862{
822 if (!(gotasync || gotsig)) 863 if (!*flag)
823 { 864 {
824 int old_errno = errno; 865 int old_errno = errno; /* save errno because write might clobber it */
825 866
826 if (sig) gotsig = 1; 867 *flag = 1;
827 if (async) gotasync = 1;
828 868
869#if EV_USE_EVENTFD
870 if (evfd >= 0)
871 {
872 uint64_t counter = 1;
873 write (evfd, &counter, sizeof (uint64_t));
874 }
875 else
876#endif
829 write (evpipe [1], &old_errno, 1); 877 write (evpipe [1], &old_errno, 1);
878
830 errno = old_errno; 879 errno = old_errno;
831 } 880 }
832} 881}
833 882
834static void 883static void
835pipecb (EV_P_ ev_io *iow, int revents) 884pipecb (EV_P_ ev_io *iow, int revents)
836{ 885{
886#if EV_USE_EVENTFD
887 if (evfd >= 0)
837 { 888 {
838 int dummy; 889 uint64_t counter = 1;
890 read (evfd, &counter, sizeof (uint64_t));
891 }
892 else
893#endif
894 {
895 char dummy;
839 read (evpipe [0], &dummy, 1); 896 read (evpipe [0], &dummy, 1);
840 } 897 }
841 898
842 if (gotsig) 899 if (gotsig && ev_is_default_loop (EV_A))
843 { 900 {
844 int signum; 901 int signum;
845 gotsig = 0; 902 gotsig = 0;
846 903
847 for (signum = signalmax; signum--; ) 904 for (signum = signalmax; signum--; )
866} 923}
867 924
868/*****************************************************************************/ 925/*****************************************************************************/
869 926
870static void 927static void
871sighandler (int signum) 928ev_sighandler (int signum)
872{ 929{
873#if EV_MULTIPLICITY 930#if EV_MULTIPLICITY
874 struct ev_loop *loop = &default_loop_struct; 931 struct ev_loop *loop = &default_loop_struct;
875#endif 932#endif
876 933
877#if _WIN32 934#if _WIN32
878 signal (signum, sighandler); 935 signal (signum, ev_sighandler);
879#endif 936#endif
880 937
881 signals [signum - 1].gotsig = 1; 938 signals [signum - 1].gotsig = 1;
882 evpipe_write (EV_A_ 1, 0); 939 evpipe_write (EV_A_ &gotsig);
883} 940}
884 941
885void noinline 942void noinline
886ev_feed_signal_event (EV_P_ int signum) 943ev_feed_signal_event (EV_P_ int signum)
887{ 944{
913#ifndef WIFCONTINUED 970#ifndef WIFCONTINUED
914# define WIFCONTINUED(status) 0 971# define WIFCONTINUED(status) 0
915#endif 972#endif
916 973
917void inline_speed 974void inline_speed
918child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) 975child_reap (EV_P_ int chain, int pid, int status)
919{ 976{
920 ev_child *w; 977 ev_child *w;
921 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 978 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
922 979
923 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 980 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
924 { 981 {
925 if ((w->pid == pid || !w->pid) 982 if ((w->pid == pid || !w->pid)
926 && (!traced || (w->flags & 1))) 983 && (!traced || (w->flags & 1)))
927 { 984 {
928 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */ 985 ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
929 w->rpid = pid; 986 w->rpid = pid;
930 w->rstatus = status; 987 w->rstatus = status;
931 ev_feed_event (EV_A_ (W)w, EV_CHILD); 988 ev_feed_event (EV_A_ (W)w, EV_CHILD);
932 } 989 }
933 } 990 }
947 if (!WCONTINUED 1004 if (!WCONTINUED
948 || errno != EINVAL 1005 || errno != EINVAL
949 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) 1006 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
950 return; 1007 return;
951 1008
952 /* make sure we are called again until all childs have been reaped */ 1009 /* make sure we are called again until all children have been reaped */
953 /* we need to do it this way so that the callback gets called before we continue */ 1010 /* we need to do it this way so that the callback gets called before we continue */
954 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 1011 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
955 1012
956 child_reap (EV_A_ sw, pid, pid, status); 1013 child_reap (EV_A_ pid, pid, status);
957 if (EV_PID_HASHSIZE > 1) 1014 if (EV_PID_HASHSIZE > 1)
958 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 1015 child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
959} 1016}
960 1017
961#endif 1018#endif
962 1019
963/*****************************************************************************/ 1020/*****************************************************************************/
1140 if (ev_is_active (&pipeev)) 1197 if (ev_is_active (&pipeev))
1141 { 1198 {
1142 ev_ref (EV_A); /* signal watcher */ 1199 ev_ref (EV_A); /* signal watcher */
1143 ev_io_stop (EV_A_ &pipeev); 1200 ev_io_stop (EV_A_ &pipeev);
1144 1201
1145 close (evpipe [0]); evpipe [0] = 0; 1202#if EV_USE_EVENTFD
1146 close (evpipe [1]); evpipe [1] = 0; 1203 if (evfd >= 0)
1204 close (evfd);
1205#endif
1206
1207 if (evpipe [0] >= 0)
1208 {
1209 close (evpipe [0]);
1210 close (evpipe [1]);
1211 }
1147 } 1212 }
1148 1213
1149#if EV_USE_INOTIFY 1214#if EV_USE_INOTIFY
1150 if (fs_fd >= 0) 1215 if (fs_fd >= 0)
1151 close (fs_fd); 1216 close (fs_fd);
1217#endif 1282#endif
1218 1283
1219 if (ev_is_active (&pipeev)) 1284 if (ev_is_active (&pipeev))
1220 { 1285 {
1221 /* this "locks" the handlers against writing to the pipe */ 1286 /* this "locks" the handlers against writing to the pipe */
1287 /* while we modify the fd vars */
1288 gotsig = 1;
1289#if EV_ASYNC_ENABLE
1222 gotsig = gotasync = 1; 1290 gotasync = 1;
1291#endif
1223 1292
1224 ev_ref (EV_A); 1293 ev_ref (EV_A);
1225 ev_io_stop (EV_A_ &pipeev); 1294 ev_io_stop (EV_A_ &pipeev);
1295
1296#if EV_USE_EVENTFD
1297 if (evfd >= 0)
1298 close (evfd);
1299#endif
1300
1301 if (evpipe [0] >= 0)
1302 {
1226 close (evpipe [0]); 1303 close (evpipe [0]);
1227 close (evpipe [1]); 1304 close (evpipe [1]);
1305 }
1228 1306
1229 evpipe_init (EV_A); 1307 evpipe_init (EV_A);
1230 /* now iterate over everything, in case we missed something */ 1308 /* now iterate over everything, in case we missed something */
1231 pipecb (EV_A_ &pipeev, EV_READ); 1309 pipecb (EV_A_ &pipeev, EV_READ);
1232 } 1310 }
1539static int loop_done; 1617static int loop_done;
1540 1618
1541void 1619void
1542ev_loop (EV_P_ int flags) 1620ev_loop (EV_P_ int flags)
1543{ 1621{
1544 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1622 loop_done = EVUNLOOP_CANCEL;
1545 ? EVUNLOOP_ONE
1546 : EVUNLOOP_CANCEL;
1547 1623
1548 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1624 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1549 1625
1550 do 1626 do
1551 { 1627 {
1647 /* queue check watchers, to be executed first */ 1723 /* queue check watchers, to be executed first */
1648 if (expect_false (checkcnt)) 1724 if (expect_false (checkcnt))
1649 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1725 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1650 1726
1651 call_pending (EV_A); 1727 call_pending (EV_A);
1652
1653 } 1728 }
1654 while (expect_true (activecnt && !loop_done)); 1729 while (expect_true (
1730 activecnt
1731 && !loop_done
1732 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
1733 ));
1655 1734
1656 if (loop_done == EVUNLOOP_ONE) 1735 if (loop_done == EVUNLOOP_ONE)
1657 loop_done = EVUNLOOP_CANCEL; 1736 loop_done = EVUNLOOP_CANCEL;
1658} 1737}
1659 1738
1927 wlist_add (&signals [w->signum - 1].head, (WL)w); 2006 wlist_add (&signals [w->signum - 1].head, (WL)w);
1928 2007
1929 if (!((WL)w)->next) 2008 if (!((WL)w)->next)
1930 { 2009 {
1931#if _WIN32 2010#if _WIN32
1932 signal (w->signum, sighandler); 2011 signal (w->signum, ev_sighandler);
1933#else 2012#else
1934 struct sigaction sa; 2013 struct sigaction sa;
1935 sa.sa_handler = sighandler; 2014 sa.sa_handler = ev_sighandler;
1936 sigfillset (&sa.sa_mask); 2015 sigfillset (&sa.sa_mask);
1937 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2016 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1938 sigaction (w->signum, &sa, 0); 2017 sigaction (w->signum, &sa, 0);
1939#endif 2018#endif
1940 } 2019 }
2463 2542
2464void 2543void
2465ev_async_send (EV_P_ ev_async *w) 2544ev_async_send (EV_P_ ev_async *w)
2466{ 2545{
2467 w->sent = 1; 2546 w->sent = 1;
2468 evpipe_write (EV_A_ 0, 1); 2547 evpipe_write (EV_A_ &gotasync);
2469} 2548}
2470#endif 2549#endif
2471 2550
2472/*****************************************************************************/ 2551/*****************************************************************************/
2473 2552

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines