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

Comparing libev/ev.c (file contents):
Revision 1.214 by root, Tue Feb 19 19:21:20 2008 UTC vs.
Revision 1.222 by root, Sun Apr 6 12:45:58 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>
274# ifdef __cplusplus
275extern "C" {
276# endif
277int eventfd (unsigned int initval, int flags);
278# ifdef __cplusplus
279}
280# endif
244#endif 281#endif
245 282
246/**/ 283/**/
247 284
248/* 285/*
451 ts.tv_sec = (time_t)delay; 488 ts.tv_sec = (time_t)delay;
452 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9); 489 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
453 490
454 nanosleep (&ts, 0); 491 nanosleep (&ts, 0);
455#elif defined(_WIN32) 492#elif defined(_WIN32)
456 Sleep (delay * 1e3); 493 Sleep ((unsigned long)(delay * 1e3));
457#else 494#else
458 struct timeval tv; 495 struct timeval tv;
459 496
460 tv.tv_sec = (time_t)delay; 497 tv.tv_sec = (time_t)delay;
461 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 498 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
802static void noinline 839static void noinline
803evpipe_init (EV_P) 840evpipe_init (EV_P)
804{ 841{
805 if (!ev_is_active (&pipeev)) 842 if (!ev_is_active (&pipeev))
806 { 843 {
844#if EV_USE_EVENTFD
845 if ((evfd = eventfd (0, 0)) >= 0)
846 {
847 evpipe [0] = -1;
848 fd_intern (evfd);
849 ev_io_set (&pipeev, evfd, EV_READ);
850 }
851 else
852#endif
853 {
807 while (pipe (evpipe)) 854 while (pipe (evpipe))
808 syserr ("(libev) error creating signal/async pipe"); 855 syserr ("(libev) error creating signal/async pipe");
809 856
810 fd_intern (evpipe [0]); 857 fd_intern (evpipe [0]);
811 fd_intern (evpipe [1]); 858 fd_intern (evpipe [1]);
812
813 ev_io_set (&pipeev, evpipe [0], EV_READ); 859 ev_io_set (&pipeev, evpipe [0], EV_READ);
860 }
861
814 ev_io_start (EV_A_ &pipeev); 862 ev_io_start (EV_A_ &pipeev);
815 ev_unref (EV_A); /* watcher should not keep loop alive */ 863 ev_unref (EV_A); /* watcher should not keep loop alive */
816 } 864 }
817} 865}
818 866
819void inline_size 867void inline_size
820evpipe_write (EV_P_ EV_ATOMIC_T *flag) 868evpipe_write (EV_P_ EV_ATOMIC_T *flag)
821{ 869{
822 if (!*flag) 870 if (!*flag)
823 { 871 {
824 int old_errno = errno; /* save errno becaue write might clobber it */ 872 int old_errno = errno; /* save errno because write might clobber it */
825 873
826 *flag = 1; 874 *flag = 1;
875
876#if EV_USE_EVENTFD
877 if (evfd >= 0)
878 {
879 uint64_t counter = 1;
880 write (evfd, &counter, sizeof (uint64_t));
881 }
882 else
883#endif
827 write (evpipe [1], &old_errno, 1); 884 write (evpipe [1], &old_errno, 1);
828 885
829 errno = old_errno; 886 errno = old_errno;
830 } 887 }
831} 888}
832 889
833static void 890static void
834pipecb (EV_P_ ev_io *iow, int revents) 891pipecb (EV_P_ ev_io *iow, int revents)
835{ 892{
893#if EV_USE_EVENTFD
894 if (evfd >= 0)
836 { 895 {
837 int dummy; 896 uint64_t counter = 1;
897 read (evfd, &counter, sizeof (uint64_t));
898 }
899 else
900#endif
901 {
902 char dummy;
838 read (evpipe [0], &dummy, 1); 903 read (evpipe [0], &dummy, 1);
839 } 904 }
840 905
841 if (gotsig && ev_is_default_loop (EV_A)) 906 if (gotsig && ev_is_default_loop (EV_A))
842 { 907 {
843 int signum; 908 int signum;
844 gotsig = 0; 909 gotsig = 0;
865} 930}
866 931
867/*****************************************************************************/ 932/*****************************************************************************/
868 933
869static void 934static void
870sighandler (int signum) 935ev_sighandler (int signum)
871{ 936{
872#if EV_MULTIPLICITY 937#if EV_MULTIPLICITY
873 struct ev_loop *loop = &default_loop_struct; 938 struct ev_loop *loop = &default_loop_struct;
874#endif 939#endif
875 940
876#if _WIN32 941#if _WIN32
877 signal (signum, sighandler); 942 signal (signum, ev_sighandler);
878#endif 943#endif
879 944
880 signals [signum - 1].gotsig = 1; 945 signals [signum - 1].gotsig = 1;
881 evpipe_write (EV_A_ &gotsig); 946 evpipe_write (EV_A_ &gotsig);
882} 947}
912#ifndef WIFCONTINUED 977#ifndef WIFCONTINUED
913# define WIFCONTINUED(status) 0 978# define WIFCONTINUED(status) 0
914#endif 979#endif
915 980
916void inline_speed 981void inline_speed
917child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) 982child_reap (EV_P_ int chain, int pid, int status)
918{ 983{
919 ev_child *w; 984 ev_child *w;
920 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 985 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
921 986
922 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 987 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
923 { 988 {
924 if ((w->pid == pid || !w->pid) 989 if ((w->pid == pid || !w->pid)
925 && (!traced || (w->flags & 1))) 990 && (!traced || (w->flags & 1)))
926 { 991 {
927 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */ 992 ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
928 w->rpid = pid; 993 w->rpid = pid;
929 w->rstatus = status; 994 w->rstatus = status;
930 ev_feed_event (EV_A_ (W)w, EV_CHILD); 995 ev_feed_event (EV_A_ (W)w, EV_CHILD);
931 } 996 }
932 } 997 }
946 if (!WCONTINUED 1011 if (!WCONTINUED
947 || errno != EINVAL 1012 || errno != EINVAL
948 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) 1013 || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
949 return; 1014 return;
950 1015
951 /* make sure we are called again until all childs have been reaped */ 1016 /* make sure we are called again until all children have been reaped */
952 /* we need to do it this way so that the callback gets called before we continue */ 1017 /* we need to do it this way so that the callback gets called before we continue */
953 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 1018 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
954 1019
955 child_reap (EV_A_ sw, pid, pid, status); 1020 child_reap (EV_A_ pid, pid, status);
956 if (EV_PID_HASHSIZE > 1) 1021 if (EV_PID_HASHSIZE > 1)
957 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 1022 child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
958} 1023}
959 1024
960#endif 1025#endif
961 1026
962/*****************************************************************************/ 1027/*****************************************************************************/
1139 if (ev_is_active (&pipeev)) 1204 if (ev_is_active (&pipeev))
1140 { 1205 {
1141 ev_ref (EV_A); /* signal watcher */ 1206 ev_ref (EV_A); /* signal watcher */
1142 ev_io_stop (EV_A_ &pipeev); 1207 ev_io_stop (EV_A_ &pipeev);
1143 1208
1144 close (evpipe [0]); evpipe [0] = 0; 1209#if EV_USE_EVENTFD
1145 close (evpipe [1]); evpipe [1] = 0; 1210 if (evfd >= 0)
1211 close (evfd);
1212#endif
1213
1214 if (evpipe [0] >= 0)
1215 {
1216 close (evpipe [0]);
1217 close (evpipe [1]);
1218 }
1146 } 1219 }
1147 1220
1148#if EV_USE_INOTIFY 1221#if EV_USE_INOTIFY
1149 if (fs_fd >= 0) 1222 if (fs_fd >= 0)
1150 close (fs_fd); 1223 close (fs_fd);
1224 gotasync = 1; 1297 gotasync = 1;
1225#endif 1298#endif
1226 1299
1227 ev_ref (EV_A); 1300 ev_ref (EV_A);
1228 ev_io_stop (EV_A_ &pipeev); 1301 ev_io_stop (EV_A_ &pipeev);
1302
1303#if EV_USE_EVENTFD
1304 if (evfd >= 0)
1305 close (evfd);
1306#endif
1307
1308 if (evpipe [0] >= 0)
1309 {
1229 close (evpipe [0]); 1310 close (evpipe [0]);
1230 close (evpipe [1]); 1311 close (evpipe [1]);
1312 }
1231 1313
1232 evpipe_init (EV_A); 1314 evpipe_init (EV_A);
1233 /* now iterate over everything, in case we missed something */ 1315 /* now iterate over everything, in case we missed something */
1234 pipecb (EV_A_ &pipeev, EV_READ); 1316 pipecb (EV_A_ &pipeev, EV_READ);
1235 } 1317 }
1542static int loop_done; 1624static int loop_done;
1543 1625
1544void 1626void
1545ev_loop (EV_P_ int flags) 1627ev_loop (EV_P_ int flags)
1546{ 1628{
1547 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1629 loop_done = EVUNLOOP_CANCEL;
1548 ? EVUNLOOP_ONE
1549 : EVUNLOOP_CANCEL;
1550 1630
1551 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1631 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1552 1632
1553 do 1633 do
1554 { 1634 {
1650 /* queue check watchers, to be executed first */ 1730 /* queue check watchers, to be executed first */
1651 if (expect_false (checkcnt)) 1731 if (expect_false (checkcnt))
1652 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1732 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1653 1733
1654 call_pending (EV_A); 1734 call_pending (EV_A);
1655
1656 } 1735 }
1657 while (expect_true (activecnt && !loop_done)); 1736 while (expect_true (
1737 activecnt
1738 && !loop_done
1739 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
1740 ));
1658 1741
1659 if (loop_done == EVUNLOOP_ONE) 1742 if (loop_done == EVUNLOOP_ONE)
1660 loop_done = EVUNLOOP_CANCEL; 1743 loop_done = EVUNLOOP_CANCEL;
1661} 1744}
1662 1745
1930 wlist_add (&signals [w->signum - 1].head, (WL)w); 2013 wlist_add (&signals [w->signum - 1].head, (WL)w);
1931 2014
1932 if (!((WL)w)->next) 2015 if (!((WL)w)->next)
1933 { 2016 {
1934#if _WIN32 2017#if _WIN32
1935 signal (w->signum, sighandler); 2018 signal (w->signum, ev_sighandler);
1936#else 2019#else
1937 struct sigaction sa; 2020 struct sigaction sa;
1938 sa.sa_handler = sighandler; 2021 sa.sa_handler = ev_sighandler;
1939 sigfillset (&sa.sa_mask); 2022 sigfillset (&sa.sa_mask);
1940 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2023 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1941 sigaction (w->signum, &sa, 0); 2024 sigaction (w->signum, &sa, 0);
1942#endif 2025#endif
1943 } 2026 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines