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

Comparing libev/ev.c (file contents):
Revision 1.69 by root, Tue Nov 6 00:10:04 2007 UTC vs.
Revision 1.72 by root, Tue Nov 6 16:09:37 2007 UTC

54 54
55#endif 55#endif
56 56
57#include <math.h> 57#include <math.h>
58#include <stdlib.h> 58#include <stdlib.h>
59#include <unistd.h>
60#include <fcntl.h> 59#include <fcntl.h>
61#include <signal.h>
62#include <stddef.h> 60#include <stddef.h>
63 61
64#include <stdio.h> 62#include <stdio.h>
65 63
66#include <assert.h> 64#include <assert.h>
67#include <errno.h> 65#include <errno.h>
68#include <sys/types.h> 66#include <sys/types.h>
67#include <time.h>
68
69#include <signal.h>
70
69#ifndef WIN32 71#ifndef WIN32
72# include <unistd.h>
73# include <sys/time.h>
70# include <sys/wait.h> 74# include <sys/wait.h>
71#endif 75#endif
72#include <sys/time.h>
73#include <time.h>
74
75/**/ 76/**/
76 77
77#ifndef EV_USE_MONOTONIC 78#ifndef EV_USE_MONOTONIC
78# define EV_USE_MONOTONIC 1 79# define EV_USE_MONOTONIC 1
79#endif 80#endif
94# define EV_USE_KQUEUE 0 95# define EV_USE_KQUEUE 0
95#endif 96#endif
96 97
97#ifndef EV_USE_WIN32 98#ifndef EV_USE_WIN32
98# ifdef WIN32 99# ifdef WIN32
100# define EV_USE_WIN32 0 /* it does not exist, use select */
101# undef EV_USE_SELECT
99# define EV_USE_WIN32 1 102# define EV_USE_SELECT 1
100# else 103# else
101# define EV_USE_WIN32 0 104# define EV_USE_WIN32 0
102# endif 105# endif
103#endif 106#endif
104 107
149 152
150#if WIN32 153#if WIN32
151/* note: the comment below could not be substantiated, but what would I care */ 154/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */ 155/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f; 156volatile double SIGFPE_REQ = 0.0f;
157
158static int
159ev_socketpair_tcp (int filedes [2])
160{
161 struct sockaddr_in addr = { 0 };
162 int addr_size = sizeof (addr);
163 SOCKET listener;
164 SOCKET sock [2] = { -1, -1 };
165
166 if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
167 return -1;
168
169 addr.sin_family = AF_INET;
170 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
171 addr.sin_port = 0;
172
173 if (bind (listener, (struct sockaddr *)&addr, addr_size))
174 goto fail;
175
176 if (getsockname(listener, (struct sockaddr *)&addr, &addr_size))
177 goto fail;
178
179 if (listen (listener, 1))
180 goto fail;
181
182 if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
183 goto fail;
184
185 if (connect (sock[0], (struct sockaddr *)&addr, addr_size))
186 goto fail;
187
188 if ((sock[1] = accept (listener, 0, 0)) < 0)
189 goto fail;
190
191 closesocket (listener);
192
193 filedes [0] = sock [0];
194 filedes [1] = sock [1];
195
196 return 0;
197
198fail:
199 closesocket (listener);
200
201 if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
202 if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
203
204 return -1;
205}
206
207# define ev_pipe(filedes) ev_socketpair_tcp (filedes)
208#else
209# define ev_pipe(filedes) pipe (filedes)
154#endif 210#endif
155 211
156/*****************************************************************************/ 212/*****************************************************************************/
157 213
158static void (*syserr_cb)(void); 214static void (*syserr_cb)(const char *msg);
159 215
160void ev_set_syserr_cb (void (*cb)(void)) 216void ev_set_syserr_cb (void (*cb)(const char *msg))
161{ 217{
162 syserr_cb = cb; 218 syserr_cb = cb;
163} 219}
164 220
165static void 221static void
166syserr (void) 222syserr (const char *msg)
167{ 223{
224 if (!msg)
225 msg = "(libev) system error";
226
168 if (syserr_cb) 227 if (syserr_cb)
169 syserr_cb (); 228 syserr_cb (msg);
170 else 229 else
171 { 230 {
172 perror ("libev"); 231 perror (msg);
173 abort (); 232 abort ();
174 } 233 }
175} 234}
176 235
177static void *(*alloc)(void *ptr, long size); 236static void *(*alloc)(void *ptr, long size);
291 stem ## max = array_roundsize (stem ## cnt >> 1); \ 350 stem ## max = array_roundsize (stem ## cnt >> 1); \
292 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 351 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
293 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 352 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
294 } 353 }
295 354
355/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
356/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
357#define array_free_microshit(stem) \
358 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
359
296#define array_free(stem, idx) \ 360#define array_free(stem, idx) \
297 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 361 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
298 362
299/*****************************************************************************/ 363/*****************************************************************************/
300 364
319 pendings [ABSPRI (w)][w->pending - 1].events |= events; 383 pendings [ABSPRI (w)][w->pending - 1].events |= events;
320 return; 384 return;
321 } 385 }
322 386
323 w->pending = ++pendingcnt [ABSPRI (w)]; 387 w->pending = ++pendingcnt [ABSPRI (w)];
324 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 388 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void));
325 pendings [ABSPRI (w)][w->pending - 1].w = w; 389 pendings [ABSPRI (w)][w->pending - 1].w = w;
326 pendings [ABSPRI (w)][w->pending - 1].events = events; 390 pendings [ABSPRI (w)][w->pending - 1].events = events;
327} 391}
328 392
329static void 393static void
378} 442}
379 443
380static void 444static void
381fd_change (EV_P_ int fd) 445fd_change (EV_P_ int fd)
382{ 446{
383 if (anfds [fd].reify || fdchangecnt < 0) 447 if (anfds [fd].reify)
384 return; 448 return;
385 449
386 anfds [fd].reify = 1; 450 anfds [fd].reify = 1;
387 451
388 ++fdchangecnt; 452 ++fdchangecnt;
389 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 453 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void));
390 fdchanges [fdchangecnt - 1] = fd; 454 fdchanges [fdchangecnt - 1] = fd;
391} 455}
392 456
393static void 457static void
394fd_kill (EV_P_ int fd) 458fd_kill (EV_P_ int fd)
400 ev_io_stop (EV_A_ w); 464 ev_io_stop (EV_A_ w);
401 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 465 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
402 } 466 }
403} 467}
404 468
469static int
470fd_valid (int fd)
471{
472#ifdef WIN32
473 return !!win32_get_osfhandle (fd);
474#else
475 return fcntl (fd, F_GETFD) != -1;
476#endif
477}
478
405/* called on EBADF to verify fds */ 479/* called on EBADF to verify fds */
406static void 480static void
407fd_ebadf (EV_P) 481fd_ebadf (EV_P)
408{ 482{
409 int fd; 483 int fd;
410 484
411 for (fd = 0; fd < anfdmax; ++fd) 485 for (fd = 0; fd < anfdmax; ++fd)
412 if (anfds [fd].events) 486 if (anfds [fd].events)
413 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 487 if (!fd_valid (fd) == -1 && errno == EBADF)
414 fd_kill (EV_A_ fd); 488 fd_kill (EV_A_ fd);
415} 489}
416 490
417/* called on ENOMEM in select/poll to kill some fds and retry */ 491/* called on ENOMEM in select/poll to kill some fds and retry */
418static void 492static void
426 fd_kill (EV_A_ fd); 500 fd_kill (EV_A_ fd);
427 return; 501 return;
428 } 502 }
429} 503}
430 504
431/* susually called after fork if method needs to re-arm all fds from scratch */ 505/* usually called after fork if method needs to re-arm all fds from scratch */
432static void 506static void
433fd_rearm_all (EV_P) 507fd_rearm_all (EV_P)
434{ 508{
435 int fd; 509 int fd;
436 510
567 ev_unref (EV_A); /* child watcher should not keep loop alive */ 641 ev_unref (EV_A); /* child watcher should not keep loop alive */
568} 642}
569 643
570/*****************************************************************************/ 644/*****************************************************************************/
571 645
646static struct ev_child *childs [PID_HASHSIZE];
647
572#ifndef WIN32 648#ifndef WIN32
573 649
574static struct ev_child *childs [PID_HASHSIZE];
575static struct ev_signal childev; 650static struct ev_signal childev;
576 651
577#ifndef WCONTINUED 652#ifndef WCONTINUED
578# define WCONTINUED 0 653# define WCONTINUED 0
579#endif 654#endif
693 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 768 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
694#endif 769#endif
695#if EV_USE_SELECT 770#if EV_USE_SELECT
696 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 771 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
697#endif 772#endif
773
774 ev_watcher_init (&sigev, sigcb);
775 ev_set_priority (&sigev, EV_MAXPRI);
698 } 776 }
699} 777}
700 778
701void 779void
702loop_destroy (EV_P) 780loop_destroy (EV_P)
720#endif 798#endif
721 799
722 for (i = NUMPRI; i--; ) 800 for (i = NUMPRI; i--; )
723 array_free (pending, [i]); 801 array_free (pending, [i]);
724 802
803 /* have to use the microsoft-never-gets-it-right macro */
725 array_free (fdchange, ); 804 array_free_microshit (fdchange);
726 array_free (timer, ); 805 array_free_microshit (timer);
727 array_free (periodic, ); 806 array_free_microshit (periodic);
728 array_free (idle, ); 807 array_free_microshit (idle);
729 array_free (prepare, ); 808 array_free_microshit (prepare);
730 array_free (check, ); 809 array_free_microshit (check);
731 810
732 method = 0; 811 method = 0;
733 /*TODO*/
734} 812}
735 813
736void 814static void
737loop_fork (EV_P) 815loop_fork (EV_P)
738{ 816{
739 /*TODO*/
740#if EV_USE_EPOLL 817#if EV_USE_EPOLL
741 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 818 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
742#endif 819#endif
743#if EV_USE_KQUEUE 820#if EV_USE_KQUEUE
744 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 821 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
745#endif 822#endif
823
824 if (ev_is_active (&sigev))
825 {
826 /* default loop */
827
828 ev_ref (EV_A);
829 ev_io_stop (EV_A_ &sigev);
830 close (sigpipe [0]);
831 close (sigpipe [1]);
832
833 while (ev_pipe (sigpipe))
834 syserr ("(libev) error creating pipe");
835
836 siginit (EV_A);
837 }
838
839 postfork = 0;
746} 840}
747 841
748#if EV_MULTIPLICITY 842#if EV_MULTIPLICITY
749struct ev_loop * 843struct ev_loop *
750ev_loop_new (int methods) 844ev_loop_new (int methods)
769} 863}
770 864
771void 865void
772ev_loop_fork (EV_P) 866ev_loop_fork (EV_P)
773{ 867{
774 loop_fork (EV_A); 868 postfork = 1;
775} 869}
776 870
777#endif 871#endif
778 872
779#if EV_MULTIPLICITY 873#if EV_MULTIPLICITY
787int 881int
788#endif 882#endif
789ev_default_loop (int methods) 883ev_default_loop (int methods)
790{ 884{
791 if (sigpipe [0] == sigpipe [1]) 885 if (sigpipe [0] == sigpipe [1])
792 if (pipe (sigpipe)) 886 if (ev_pipe (sigpipe))
793 return 0; 887 return 0;
794 888
795 if (!default_loop) 889 if (!default_loop)
796 { 890 {
797#if EV_MULTIPLICITY 891#if EV_MULTIPLICITY
802 896
803 loop_init (EV_A_ methods); 897 loop_init (EV_A_ methods);
804 898
805 if (ev_method (EV_A)) 899 if (ev_method (EV_A))
806 { 900 {
807 ev_watcher_init (&sigev, sigcb);
808 ev_set_priority (&sigev, EV_MAXPRI);
809 siginit (EV_A); 901 siginit (EV_A);
810 902
811#ifndef WIN32 903#ifndef WIN32
812 ev_signal_init (&childev, childcb, SIGCHLD); 904 ev_signal_init (&childev, childcb, SIGCHLD);
813 ev_set_priority (&childev, EV_MAXPRI); 905 ev_set_priority (&childev, EV_MAXPRI);
827{ 919{
828#if EV_MULTIPLICITY 920#if EV_MULTIPLICITY
829 struct ev_loop *loop = default_loop; 921 struct ev_loop *loop = default_loop;
830#endif 922#endif
831 923
924#ifndef WIN32
832 ev_ref (EV_A); /* child watcher */ 925 ev_ref (EV_A); /* child watcher */
833 ev_signal_stop (EV_A_ &childev); 926 ev_signal_stop (EV_A_ &childev);
927#endif
834 928
835 ev_ref (EV_A); /* signal watcher */ 929 ev_ref (EV_A); /* signal watcher */
836 ev_io_stop (EV_A_ &sigev); 930 ev_io_stop (EV_A_ &sigev);
837 931
838 close (sigpipe [0]); sigpipe [0] = 0; 932 close (sigpipe [0]); sigpipe [0] = 0;
846{ 940{
847#if EV_MULTIPLICITY 941#if EV_MULTIPLICITY
848 struct ev_loop *loop = default_loop; 942 struct ev_loop *loop = default_loop;
849#endif 943#endif
850 944
851 loop_fork (EV_A); 945 if (method)
852 946 postfork = 1;
853 ev_io_stop (EV_A_ &sigev);
854 close (sigpipe [0]);
855 close (sigpipe [1]);
856 pipe (sigpipe);
857
858 ev_ref (EV_A); /* signal watcher */
859 siginit (EV_A);
860} 947}
861 948
862/*****************************************************************************/ 949/*****************************************************************************/
863 950
864static void 951static void
1042 { 1129 {
1043 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1130 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1044 call_pending (EV_A); 1131 call_pending (EV_A);
1045 } 1132 }
1046 1133
1134 /* we might have forked, so reify kernel state if necessary */
1135 if (expect_false (postfork))
1136 loop_fork (EV_A);
1137
1047 /* update fd-related kernel structures */ 1138 /* update fd-related kernel structures */
1048 fd_reify (EV_A); 1139 fd_reify (EV_A);
1049 1140
1050 /* calculate blocking time */ 1141 /* calculate blocking time */
1051 1142
1205 ((WT)w)->at += mn_now; 1296 ((WT)w)->at += mn_now;
1206 1297
1207 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1298 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1208 1299
1209 ev_start (EV_A_ (W)w, ++timercnt); 1300 ev_start (EV_A_ (W)w, ++timercnt);
1210 array_needsize (timers, timermax, timercnt, ); 1301 array_needsize (timers, timermax, timercnt, (void));
1211 timers [timercnt - 1] = w; 1302 timers [timercnt - 1] = w;
1212 upheap ((WT *)timers, timercnt - 1); 1303 upheap ((WT *)timers, timercnt - 1);
1213 1304
1214 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1305 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1215} 1306}
1262 /* this formula differs from the one in periodic_reify because we do not always round up */ 1353 /* this formula differs from the one in periodic_reify because we do not always round up */
1263 if (w->interval) 1354 if (w->interval)
1264 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1355 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1265 1356
1266 ev_start (EV_A_ (W)w, ++periodiccnt); 1357 ev_start (EV_A_ (W)w, ++periodiccnt);
1267 array_needsize (periodics, periodicmax, periodiccnt, ); 1358 array_needsize (periodics, periodicmax, periodiccnt, (void));
1268 periodics [periodiccnt - 1] = w; 1359 periodics [periodiccnt - 1] = w;
1269 upheap ((WT *)periodics, periodiccnt - 1); 1360 upheap ((WT *)periodics, periodiccnt - 1);
1270 1361
1271 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1362 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1272} 1363}
1294{ 1385{
1295 if (ev_is_active (w)) 1386 if (ev_is_active (w))
1296 return; 1387 return;
1297 1388
1298 ev_start (EV_A_ (W)w, ++idlecnt); 1389 ev_start (EV_A_ (W)w, ++idlecnt);
1299 array_needsize (idles, idlemax, idlecnt, ); 1390 array_needsize (idles, idlemax, idlecnt, (void));
1300 idles [idlecnt - 1] = w; 1391 idles [idlecnt - 1] = w;
1301} 1392}
1302 1393
1303void 1394void
1304ev_idle_stop (EV_P_ struct ev_idle *w) 1395ev_idle_stop (EV_P_ struct ev_idle *w)
1316{ 1407{
1317 if (ev_is_active (w)) 1408 if (ev_is_active (w))
1318 return; 1409 return;
1319 1410
1320 ev_start (EV_A_ (W)w, ++preparecnt); 1411 ev_start (EV_A_ (W)w, ++preparecnt);
1321 array_needsize (prepares, preparemax, preparecnt, ); 1412 array_needsize (prepares, preparemax, preparecnt, (void));
1322 prepares [preparecnt - 1] = w; 1413 prepares [preparecnt - 1] = w;
1323} 1414}
1324 1415
1325void 1416void
1326ev_prepare_stop (EV_P_ struct ev_prepare *w) 1417ev_prepare_stop (EV_P_ struct ev_prepare *w)
1338{ 1429{
1339 if (ev_is_active (w)) 1430 if (ev_is_active (w))
1340 return; 1431 return;
1341 1432
1342 ev_start (EV_A_ (W)w, ++checkcnt); 1433 ev_start (EV_A_ (W)w, ++checkcnt);
1343 array_needsize (checks, checkmax, checkcnt, ); 1434 array_needsize (checks, checkmax, checkcnt, (void));
1344 checks [checkcnt - 1] = w; 1435 checks [checkcnt - 1] = w;
1345} 1436}
1346 1437
1347void 1438void
1348ev_check_stop (EV_P_ struct ev_check *w) 1439ev_check_stop (EV_P_ struct ev_check *w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines