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

Comparing libev/ev.c (file contents):
Revision 1.207 by root, Thu Jan 31 13:10:56 2008 UTC vs.
Revision 1.221 by root, Sun Apr 6 12:44:49 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 */
273# include <stdint.h>
274int eventfd (unsigned int initval, int flags);
244#endif 275#endif
245 276
246/**/ 277/**/
247 278
248/* 279/*
451 ts.tv_sec = (time_t)delay; 482 ts.tv_sec = (time_t)delay;
452 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9); 483 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
453 484
454 nanosleep (&ts, 0); 485 nanosleep (&ts, 0);
455#elif defined(_WIN32) 486#elif defined(_WIN32)
456 Sleep (delay * 1e3); 487 Sleep ((unsigned long)(delay * 1e3));
457#else 488#else
458 struct timeval tv; 489 struct timeval tv;
459 490
460 tv.tv_sec = (time_t)delay; 491 tv.tv_sec = (time_t)delay;
461 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 492 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
802static void noinline 833static void noinline
803evpipe_init (EV_P) 834evpipe_init (EV_P)
804{ 835{
805 if (!ev_is_active (&pipeev)) 836 if (!ev_is_active (&pipeev))
806 { 837 {
838#if EV_USE_EVENTFD
839 if ((evfd = eventfd (0, 0)) >= 0)
840 {
841 evpipe [0] = -1;
842 fd_intern (evfd);
843 ev_io_set (&pipeev, evfd, EV_READ);
844 }
845 else
846#endif
847 {
807 while (pipe (evpipe)) 848 while (pipe (evpipe))
808 syserr ("(libev) error creating signal/async pipe"); 849 syserr ("(libev) error creating signal/async pipe");
809 850
810 fd_intern (evpipe [0]); 851 fd_intern (evpipe [0]);
811 fd_intern (evpipe [1]); 852 fd_intern (evpipe [1]);
812
813 ev_io_set (&pipeev, evpipe [0], EV_READ); 853 ev_io_set (&pipeev, evpipe [0], EV_READ);
854 }
855
814 ev_io_start (EV_A_ &pipeev); 856 ev_io_start (EV_A_ &pipeev);
815 ev_unref (EV_A); /* child watcher should not keep loop alive */ 857 ev_unref (EV_A); /* watcher should not keep loop alive */
816 } 858 }
817} 859}
818 860
819void inline_size 861void inline_size
820evpipe_write (EV_P_ int sig, int async) 862evpipe_write (EV_P_ EV_ATOMIC_T *flag)
821{ 863{
822 if (!(gotasync || gotsig)) 864 if (!*flag)
823 { 865 {
824 int old_errno = errno; 866 int old_errno = errno; /* save errno because write might clobber it */
825 867
826 if (sig) gotsig = 1; 868 *flag = 1;
827 if (async) gotasync = 1;
828 869
870#if EV_USE_EVENTFD
871 if (evfd >= 0)
872 {
873 uint64_t counter = 1;
874 write (evfd, &counter, sizeof (uint64_t));
875 }
876 else
877#endif
829 write (evpipe [1], &old_errno, 1); 878 write (evpipe [1], &old_errno, 1);
879
830 errno = old_errno; 880 errno = old_errno;
831 } 881 }
832} 882}
833 883
834static void 884static void
835pipecb (EV_P_ ev_io *iow, int revents) 885pipecb (EV_P_ ev_io *iow, int revents)
836{ 886{
887#if EV_USE_EVENTFD
888 if (evfd >= 0)
837 { 889 {
838 int dummy; 890 uint64_t counter = 1;
891 read (evfd, &counter, sizeof (uint64_t));
892 }
893 else
894#endif
895 {
896 char dummy;
839 read (evpipe [0], &dummy, 1); 897 read (evpipe [0], &dummy, 1);
840 } 898 }
841 899
842 if (gotsig) 900 if (gotsig && ev_is_default_loop (EV_A))
843 { 901 {
844 int signum; 902 int signum;
845 gotsig = 0; 903 gotsig = 0;
846 904
847 for (signum = signalmax; signum--; ) 905 for (signum = signalmax; signum--; )
848 if (signals [signum].gotsig) 906 if (signals [signum].gotsig)
849 ev_feed_signal_event (EV_A_ signum + 1); 907 ev_feed_signal_event (EV_A_ signum + 1);
850 } 908 }
851 909
910#if EV_ASYNC_ENABLE
852 if (gotasync) 911 if (gotasync)
853 { 912 {
854 int i; 913 int i;
855 gotasync = 0; 914 gotasync = 0;
856 915
859 { 918 {
860 asyncs [i]->sent = 0; 919 asyncs [i]->sent = 0;
861 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); 920 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
862 } 921 }
863 } 922 }
923#endif
864} 924}
865 925
866/*****************************************************************************/ 926/*****************************************************************************/
867 927
868static void 928static void
869sighandler (int signum) 929ev_sighandler (int signum)
870{ 930{
871#if EV_MULTIPLICITY 931#if EV_MULTIPLICITY
872 struct ev_loop *loop = &default_loop_struct; 932 struct ev_loop *loop = &default_loop_struct;
873#endif 933#endif
874 934
875#if _WIN32 935#if _WIN32
876 signal (signum, sighandler); 936 signal (signum, ev_sighandler);
877#endif 937#endif
878 938
879 signals [signum - 1].gotsig = 1; 939 signals [signum - 1].gotsig = 1;
880 evpipe_write (EV_A_ 1, 0); 940 evpipe_write (EV_A_ &gotsig);
881} 941}
882 942
883void noinline 943void noinline
884ev_feed_signal_event (EV_P_ int signum) 944ev_feed_signal_event (EV_P_ int signum)
885{ 945{
911#ifndef WIFCONTINUED 971#ifndef WIFCONTINUED
912# define WIFCONTINUED(status) 0 972# define WIFCONTINUED(status) 0
913#endif 973#endif
914 974
915void inline_speed 975void inline_speed
916child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) 976child_reap (EV_P_ int chain, int pid, int status)
917{ 977{
918 ev_child *w; 978 ev_child *w;
919 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 979 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
920 980
921 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 981 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
922 { 982 {
923 if ((w->pid == pid || !w->pid) 983 if ((w->pid == pid || !w->pid)
924 && (!traced || (w->flags & 1))) 984 && (!traced || (w->flags & 1)))
925 { 985 {
926 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */ 986 ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
927 w->rpid = pid; 987 w->rpid = pid;
928 w->rstatus = status; 988 w->rstatus = status;
929 ev_feed_event (EV_A_ (W)w, EV_CHILD); 989 ev_feed_event (EV_A_ (W)w, EV_CHILD);
930 } 990 }
931 } 991 }
945 if (!WCONTINUED 1005 if (!WCONTINUED
946 || errno != EINVAL 1006 || errno != EINVAL
947 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) 1007 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
948 return; 1008 return;
949 1009
950 /* make sure we are called again until all childs have been reaped */ 1010 /* make sure we are called again until all children have been reaped */
951 /* we need to do it this way so that the callback gets called before we continue */ 1011 /* we need to do it this way so that the callback gets called before we continue */
952 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 1012 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
953 1013
954 child_reap (EV_A_ sw, pid, pid, status); 1014 child_reap (EV_A_ pid, pid, status);
955 if (EV_PID_HASHSIZE > 1) 1015 if (EV_PID_HASHSIZE > 1)
956 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 1016 child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
957} 1017}
958 1018
959#endif 1019#endif
960 1020
961/*****************************************************************************/ 1021/*****************************************************************************/
1079 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 1139 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
1080 have_monotonic = 1; 1140 have_monotonic = 1;
1081 } 1141 }
1082#endif 1142#endif
1083 1143
1084 ev_rt_now = ev_time (); 1144 ev_rt_now = ev_time ();
1085 mn_now = get_clock (); 1145 mn_now = get_clock ();
1086 now_floor = mn_now; 1146 now_floor = mn_now;
1087 rtmn_diff = ev_rt_now - mn_now; 1147 rtmn_diff = ev_rt_now - mn_now;
1088 1148
1089 io_blocktime = 0.; 1149 io_blocktime = 0.;
1090 timeout_blocktime = 0.; 1150 timeout_blocktime = 0.;
1151 backend = 0;
1152 backend_fd = -1;
1153 gotasync = 0;
1154#if EV_USE_INOTIFY
1155 fs_fd = -2;
1156#endif
1091 1157
1092 /* pid check not overridable via env */ 1158 /* pid check not overridable via env */
1093#ifndef _WIN32 1159#ifndef _WIN32
1094 if (flags & EVFLAG_FORKCHECK) 1160 if (flags & EVFLAG_FORKCHECK)
1095 curpid = getpid (); 1161 curpid = getpid ();
1101 flags = atoi (getenv ("LIBEV_FLAGS")); 1167 flags = atoi (getenv ("LIBEV_FLAGS"));
1102 1168
1103 if (!(flags & 0x0000ffffUL)) 1169 if (!(flags & 0x0000ffffUL))
1104 flags |= ev_recommended_backends (); 1170 flags |= ev_recommended_backends ();
1105 1171
1106 backend = 0;
1107 backend_fd = -1;
1108#if EV_USE_INOTIFY
1109 fs_fd = -2;
1110#endif
1111
1112#if EV_USE_PORT 1172#if EV_USE_PORT
1113 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1173 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1114#endif 1174#endif
1115#if EV_USE_KQUEUE 1175#if EV_USE_KQUEUE
1116 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); 1176 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
1138 if (ev_is_active (&pipeev)) 1198 if (ev_is_active (&pipeev))
1139 { 1199 {
1140 ev_ref (EV_A); /* signal watcher */ 1200 ev_ref (EV_A); /* signal watcher */
1141 ev_io_stop (EV_A_ &pipeev); 1201 ev_io_stop (EV_A_ &pipeev);
1142 1202
1143 close (evpipe [0]); evpipe [0] = 0; 1203#if EV_USE_EVENTFD
1144 close (evpipe [1]); evpipe [1] = 0; 1204 if (evfd >= 0)
1205 close (evfd);
1206#endif
1207
1208 if (evpipe [0] >= 0)
1209 {
1210 close (evpipe [0]);
1211 close (evpipe [1]);
1212 }
1145 } 1213 }
1146 1214
1147#if EV_USE_INOTIFY 1215#if EV_USE_INOTIFY
1148 if (fs_fd >= 0) 1216 if (fs_fd >= 0)
1149 close (fs_fd); 1217 close (fs_fd);
1187#if EV_FORK_ENABLE 1255#if EV_FORK_ENABLE
1188 array_free (fork, EMPTY); 1256 array_free (fork, EMPTY);
1189#endif 1257#endif
1190 array_free (prepare, EMPTY); 1258 array_free (prepare, EMPTY);
1191 array_free (check, EMPTY); 1259 array_free (check, EMPTY);
1260#if EV_ASYNC_ENABLE
1261 array_free (async, EMPTY);
1262#endif
1192 1263
1193 backend = 0; 1264 backend = 0;
1194} 1265}
1195 1266
1196void inline_size infy_fork (EV_P); 1267void inline_size infy_fork (EV_P);
1212#endif 1283#endif
1213 1284
1214 if (ev_is_active (&pipeev)) 1285 if (ev_is_active (&pipeev))
1215 { 1286 {
1216 /* this "locks" the handlers against writing to the pipe */ 1287 /* this "locks" the handlers against writing to the pipe */
1288 /* while we modify the fd vars */
1289 gotsig = 1;
1290#if EV_ASYNC_ENABLE
1217 gotsig = gotasync = 1; 1291 gotasync = 1;
1292#endif
1218 1293
1219 ev_ref (EV_A); 1294 ev_ref (EV_A);
1220 ev_io_stop (EV_A_ &pipeev); 1295 ev_io_stop (EV_A_ &pipeev);
1296
1297#if EV_USE_EVENTFD
1298 if (evfd >= 0)
1299 close (evfd);
1300#endif
1301
1302 if (evpipe [0] >= 0)
1303 {
1221 close (evpipe [0]); 1304 close (evpipe [0]);
1222 close (evpipe [1]); 1305 close (evpipe [1]);
1306 }
1223 1307
1224 evpipe_init (EV_A); 1308 evpipe_init (EV_A);
1225 /* now iterate over everything */ 1309 /* now iterate over everything, in case we missed something */
1226 evcb (EV_A_ &pipeev, EV_READ); 1310 pipecb (EV_A_ &pipeev, EV_READ);
1227 } 1311 }
1228 1312
1229 postfork = 0; 1313 postfork = 0;
1230} 1314}
1231 1315
1534static int loop_done; 1618static int loop_done;
1535 1619
1536void 1620void
1537ev_loop (EV_P_ int flags) 1621ev_loop (EV_P_ int flags)
1538{ 1622{
1539 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1623 loop_done = EVUNLOOP_CANCEL;
1540 ? EVUNLOOP_ONE
1541 : EVUNLOOP_CANCEL;
1542 1624
1543 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1625 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1544 1626
1545 do 1627 do
1546 { 1628 {
1642 /* queue check watchers, to be executed first */ 1724 /* queue check watchers, to be executed first */
1643 if (expect_false (checkcnt)) 1725 if (expect_false (checkcnt))
1644 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1726 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1645 1727
1646 call_pending (EV_A); 1728 call_pending (EV_A);
1647
1648 } 1729 }
1649 while (expect_true (activecnt && !loop_done)); 1730 while (expect_true (
1731 activecnt
1732 && !loop_done
1733 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
1734 ));
1650 1735
1651 if (loop_done == EVUNLOOP_ONE) 1736 if (loop_done == EVUNLOOP_ONE)
1652 loop_done = EVUNLOOP_CANCEL; 1737 loop_done = EVUNLOOP_CANCEL;
1653} 1738}
1654 1739
1922 wlist_add (&signals [w->signum - 1].head, (WL)w); 2007 wlist_add (&signals [w->signum - 1].head, (WL)w);
1923 2008
1924 if (!((WL)w)->next) 2009 if (!((WL)w)->next)
1925 { 2010 {
1926#if _WIN32 2011#if _WIN32
1927 signal (w->signum, sighandler); 2012 signal (w->signum, ev_sighandler);
1928#else 2013#else
1929 struct sigaction sa; 2014 struct sigaction sa;
1930 sa.sa_handler = sighandler; 2015 sa.sa_handler = ev_sighandler;
1931 sigfillset (&sa.sa_mask); 2016 sigfillset (&sa.sa_mask);
1932 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2017 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1933 sigaction (w->signum, &sa, 0); 2018 sigaction (w->signum, &sa, 0);
1934#endif 2019#endif
1935 } 2020 }
2458 2543
2459void 2544void
2460ev_async_send (EV_P_ ev_async *w) 2545ev_async_send (EV_P_ ev_async *w)
2461{ 2546{
2462 w->sent = 1; 2547 w->sent = 1;
2463 evpipe_write (EV_A_ 0, 1); 2548 evpipe_write (EV_A_ &gotasync);
2464} 2549}
2465#endif 2550#endif
2466 2551
2467/*****************************************************************************/ 2552/*****************************************************************************/
2468 2553

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines