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.71 by root, Tue Nov 6 13:17:55 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#ifndef PERL
70# include <signal.h>
71#endif
72
69#ifndef WIN32 73#ifndef WIN32
74# include <unistd.h>
75# include <sys/time.h>
70# include <sys/wait.h> 76# include <sys/wait.h>
71#endif 77#endif
72#include <sys/time.h>
73#include <time.h>
74
75/**/ 78/**/
76 79
77#ifndef EV_USE_MONOTONIC 80#ifndef EV_USE_MONOTONIC
78# define EV_USE_MONOTONIC 1 81# define EV_USE_MONOTONIC 1
79#endif 82#endif
94# define EV_USE_KQUEUE 0 97# define EV_USE_KQUEUE 0
95#endif 98#endif
96 99
97#ifndef EV_USE_WIN32 100#ifndef EV_USE_WIN32
98# ifdef WIN32 101# ifdef WIN32
102# define EV_USE_WIN32 0 /* it does not exist, use select */
103# undef EV_USE_SELECT
99# define EV_USE_WIN32 1 104# define EV_USE_SELECT 1
100# else 105# else
101# define EV_USE_WIN32 0 106# define EV_USE_WIN32 0
102# endif 107# endif
103#endif 108#endif
104 109
153volatile double SIGFPE_REQ = 0.0f; 158volatile double SIGFPE_REQ = 0.0f;
154#endif 159#endif
155 160
156/*****************************************************************************/ 161/*****************************************************************************/
157 162
158static void (*syserr_cb)(void); 163static void (*syserr_cb)(const char *msg);
159 164
160void ev_set_syserr_cb (void (*cb)(void)) 165void ev_set_syserr_cb (void (*cb)(const char *msg))
161{ 166{
162 syserr_cb = cb; 167 syserr_cb = cb;
163} 168}
164 169
165static void 170static void
166syserr (void) 171syserr (const char *msg)
167{ 172{
173 if (!msg)
174 msg = "(libev) system error";
175
168 if (syserr_cb) 176 if (syserr_cb)
169 syserr_cb (); 177 syserr_cb (msg);
170 else 178 else
171 { 179 {
172 perror ("libev"); 180 perror (msg);
173 abort (); 181 abort ();
174 } 182 }
175} 183}
176 184
177static void *(*alloc)(void *ptr, long size); 185static void *(*alloc)(void *ptr, long size);
291 stem ## max = array_roundsize (stem ## cnt >> 1); \ 299 stem ## max = array_roundsize (stem ## cnt >> 1); \
292 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 300 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
293 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 301 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
294 } 302 }
295 303
304/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
305/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
306#define array_free_microshit(stem) \
307 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
308
296#define array_free(stem, idx) \ 309#define array_free(stem, idx) \
297 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 310 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
298 311
299/*****************************************************************************/ 312/*****************************************************************************/
300 313
319 pendings [ABSPRI (w)][w->pending - 1].events |= events; 332 pendings [ABSPRI (w)][w->pending - 1].events |= events;
320 return; 333 return;
321 } 334 }
322 335
323 w->pending = ++pendingcnt [ABSPRI (w)]; 336 w->pending = ++pendingcnt [ABSPRI (w)];
324 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 337 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void));
325 pendings [ABSPRI (w)][w->pending - 1].w = w; 338 pendings [ABSPRI (w)][w->pending - 1].w = w;
326 pendings [ABSPRI (w)][w->pending - 1].events = events; 339 pendings [ABSPRI (w)][w->pending - 1].events = events;
327} 340}
328 341
329static void 342static void
378} 391}
379 392
380static void 393static void
381fd_change (EV_P_ int fd) 394fd_change (EV_P_ int fd)
382{ 395{
383 if (anfds [fd].reify || fdchangecnt < 0) 396 if (anfds [fd].reify)
384 return; 397 return;
385 398
386 anfds [fd].reify = 1; 399 anfds [fd].reify = 1;
387 400
388 ++fdchangecnt; 401 ++fdchangecnt;
389 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 402 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void));
390 fdchanges [fdchangecnt - 1] = fd; 403 fdchanges [fdchangecnt - 1] = fd;
391} 404}
392 405
393static void 406static void
394fd_kill (EV_P_ int fd) 407fd_kill (EV_P_ int fd)
400 ev_io_stop (EV_A_ w); 413 ev_io_stop (EV_A_ w);
401 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 414 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
402 } 415 }
403} 416}
404 417
418static int
419fd_valid (int fd)
420{
421#ifdef WIN32
422 return !!win32_get_osfhandle (fd);
423#else
424 return fcntl (fd, F_GETFD) != -1;
425#endif
426}
427
405/* called on EBADF to verify fds */ 428/* called on EBADF to verify fds */
406static void 429static void
407fd_ebadf (EV_P) 430fd_ebadf (EV_P)
408{ 431{
409 int fd; 432 int fd;
410 433
411 for (fd = 0; fd < anfdmax; ++fd) 434 for (fd = 0; fd < anfdmax; ++fd)
412 if (anfds [fd].events) 435 if (anfds [fd].events)
413 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 436 if (!fd_valid (fd) == -1 && errno == EBADF)
414 fd_kill (EV_A_ fd); 437 fd_kill (EV_A_ fd);
415} 438}
416 439
417/* called on ENOMEM in select/poll to kill some fds and retry */ 440/* called on ENOMEM in select/poll to kill some fds and retry */
418static void 441static void
426 fd_kill (EV_A_ fd); 449 fd_kill (EV_A_ fd);
427 return; 450 return;
428 } 451 }
429} 452}
430 453
431/* susually called after fork if method needs to re-arm all fds from scratch */ 454/* usually called after fork if method needs to re-arm all fds from scratch */
432static void 455static void
433fd_rearm_all (EV_P) 456fd_rearm_all (EV_P)
434{ 457{
435 int fd; 458 int fd;
436 459
567 ev_unref (EV_A); /* child watcher should not keep loop alive */ 590 ev_unref (EV_A); /* child watcher should not keep loop alive */
568} 591}
569 592
570/*****************************************************************************/ 593/*****************************************************************************/
571 594
595static struct ev_child *childs [PID_HASHSIZE];
596
572#ifndef WIN32 597#ifndef WIN32
573 598
574static struct ev_child *childs [PID_HASHSIZE];
575static struct ev_signal childev; 599static struct ev_signal childev;
576 600
577#ifndef WCONTINUED 601#ifndef WCONTINUED
578# define WCONTINUED 0 602# define WCONTINUED 0
579#endif 603#endif
693 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 717 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
694#endif 718#endif
695#if EV_USE_SELECT 719#if EV_USE_SELECT
696 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 720 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
697#endif 721#endif
722
723 ev_watcher_init (&sigev, sigcb);
724 ev_set_priority (&sigev, EV_MAXPRI);
698 } 725 }
699} 726}
700 727
701void 728void
702loop_destroy (EV_P) 729loop_destroy (EV_P)
720#endif 747#endif
721 748
722 for (i = NUMPRI; i--; ) 749 for (i = NUMPRI; i--; )
723 array_free (pending, [i]); 750 array_free (pending, [i]);
724 751
752 /* have to use the microsoft-never-gets-it-right macro */
725 array_free (fdchange, ); 753 array_free_microshit (fdchange);
726 array_free (timer, ); 754 array_free_microshit (timer);
727 array_free (periodic, ); 755 array_free_microshit (periodic);
728 array_free (idle, ); 756 array_free_microshit (idle);
729 array_free (prepare, ); 757 array_free_microshit (prepare);
730 array_free (check, ); 758 array_free_microshit (check);
731 759
732 method = 0; 760 method = 0;
733 /*TODO*/
734} 761}
735 762
736void 763static void
737loop_fork (EV_P) 764loop_fork (EV_P)
738{ 765{
739 /*TODO*/
740#if EV_USE_EPOLL 766#if EV_USE_EPOLL
741 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 767 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
742#endif 768#endif
743#if EV_USE_KQUEUE 769#if EV_USE_KQUEUE
744 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 770 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
745#endif 771#endif
772
773 if (ev_is_active (&sigev))
774 {
775 /* default loop */
776
777 ev_ref (EV_A);
778 ev_io_stop (EV_A_ &sigev);
779 close (sigpipe [0]);
780 close (sigpipe [1]);
781
782 while (pipe (sigpipe))
783 syserr ("(libev) error creating pipe");
784
785 siginit (EV_A);
786 }
787
788 postfork = 0;
746} 789}
747 790
748#if EV_MULTIPLICITY 791#if EV_MULTIPLICITY
749struct ev_loop * 792struct ev_loop *
750ev_loop_new (int methods) 793ev_loop_new (int methods)
769} 812}
770 813
771void 814void
772ev_loop_fork (EV_P) 815ev_loop_fork (EV_P)
773{ 816{
774 loop_fork (EV_A); 817 postfork = 1;
775} 818}
776 819
777#endif 820#endif
778 821
779#if EV_MULTIPLICITY 822#if EV_MULTIPLICITY
802 845
803 loop_init (EV_A_ methods); 846 loop_init (EV_A_ methods);
804 847
805 if (ev_method (EV_A)) 848 if (ev_method (EV_A))
806 { 849 {
807 ev_watcher_init (&sigev, sigcb);
808 ev_set_priority (&sigev, EV_MAXPRI);
809 siginit (EV_A); 850 siginit (EV_A);
810 851
811#ifndef WIN32 852#ifndef WIN32
812 ev_signal_init (&childev, childcb, SIGCHLD); 853 ev_signal_init (&childev, childcb, SIGCHLD);
813 ev_set_priority (&childev, EV_MAXPRI); 854 ev_set_priority (&childev, EV_MAXPRI);
827{ 868{
828#if EV_MULTIPLICITY 869#if EV_MULTIPLICITY
829 struct ev_loop *loop = default_loop; 870 struct ev_loop *loop = default_loop;
830#endif 871#endif
831 872
873#ifndef WIN32
832 ev_ref (EV_A); /* child watcher */ 874 ev_ref (EV_A); /* child watcher */
833 ev_signal_stop (EV_A_ &childev); 875 ev_signal_stop (EV_A_ &childev);
876#endif
834 877
835 ev_ref (EV_A); /* signal watcher */ 878 ev_ref (EV_A); /* signal watcher */
836 ev_io_stop (EV_A_ &sigev); 879 ev_io_stop (EV_A_ &sigev);
837 880
838 close (sigpipe [0]); sigpipe [0] = 0; 881 close (sigpipe [0]); sigpipe [0] = 0;
846{ 889{
847#if EV_MULTIPLICITY 890#if EV_MULTIPLICITY
848 struct ev_loop *loop = default_loop; 891 struct ev_loop *loop = default_loop;
849#endif 892#endif
850 893
851 loop_fork (EV_A); 894 if (method)
852 895 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} 896}
861 897
862/*****************************************************************************/ 898/*****************************************************************************/
863 899
864static void 900static void
1042 { 1078 {
1043 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1079 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1044 call_pending (EV_A); 1080 call_pending (EV_A);
1045 } 1081 }
1046 1082
1083 /* we might have forked, so reify kernel state if necessary */
1084 if (expect_false (postfork))
1085 loop_fork (EV_A);
1086
1047 /* update fd-related kernel structures */ 1087 /* update fd-related kernel structures */
1048 fd_reify (EV_A); 1088 fd_reify (EV_A);
1049 1089
1050 /* calculate blocking time */ 1090 /* calculate blocking time */
1051 1091
1205 ((WT)w)->at += mn_now; 1245 ((WT)w)->at += mn_now;
1206 1246
1207 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1247 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1208 1248
1209 ev_start (EV_A_ (W)w, ++timercnt); 1249 ev_start (EV_A_ (W)w, ++timercnt);
1210 array_needsize (timers, timermax, timercnt, ); 1250 array_needsize (timers, timermax, timercnt, (void));
1211 timers [timercnt - 1] = w; 1251 timers [timercnt - 1] = w;
1212 upheap ((WT *)timers, timercnt - 1); 1252 upheap ((WT *)timers, timercnt - 1);
1213 1253
1214 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1254 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1215} 1255}
1262 /* this formula differs from the one in periodic_reify because we do not always round up */ 1302 /* this formula differs from the one in periodic_reify because we do not always round up */
1263 if (w->interval) 1303 if (w->interval)
1264 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1304 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1265 1305
1266 ev_start (EV_A_ (W)w, ++periodiccnt); 1306 ev_start (EV_A_ (W)w, ++periodiccnt);
1267 array_needsize (periodics, periodicmax, periodiccnt, ); 1307 array_needsize (periodics, periodicmax, periodiccnt, (void));
1268 periodics [periodiccnt - 1] = w; 1308 periodics [periodiccnt - 1] = w;
1269 upheap ((WT *)periodics, periodiccnt - 1); 1309 upheap ((WT *)periodics, periodiccnt - 1);
1270 1310
1271 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1311 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1272} 1312}
1294{ 1334{
1295 if (ev_is_active (w)) 1335 if (ev_is_active (w))
1296 return; 1336 return;
1297 1337
1298 ev_start (EV_A_ (W)w, ++idlecnt); 1338 ev_start (EV_A_ (W)w, ++idlecnt);
1299 array_needsize (idles, idlemax, idlecnt, ); 1339 array_needsize (idles, idlemax, idlecnt, (void));
1300 idles [idlecnt - 1] = w; 1340 idles [idlecnt - 1] = w;
1301} 1341}
1302 1342
1303void 1343void
1304ev_idle_stop (EV_P_ struct ev_idle *w) 1344ev_idle_stop (EV_P_ struct ev_idle *w)
1316{ 1356{
1317 if (ev_is_active (w)) 1357 if (ev_is_active (w))
1318 return; 1358 return;
1319 1359
1320 ev_start (EV_A_ (W)w, ++preparecnt); 1360 ev_start (EV_A_ (W)w, ++preparecnt);
1321 array_needsize (prepares, preparemax, preparecnt, ); 1361 array_needsize (prepares, preparemax, preparecnt, (void));
1322 prepares [preparecnt - 1] = w; 1362 prepares [preparecnt - 1] = w;
1323} 1363}
1324 1364
1325void 1365void
1326ev_prepare_stop (EV_P_ struct ev_prepare *w) 1366ev_prepare_stop (EV_P_ struct ev_prepare *w)
1338{ 1378{
1339 if (ev_is_active (w)) 1379 if (ev_is_active (w))
1340 return; 1380 return;
1341 1381
1342 ev_start (EV_A_ (W)w, ++checkcnt); 1382 ev_start (EV_A_ (W)w, ++checkcnt);
1343 array_needsize (checks, checkmax, checkcnt, ); 1383 array_needsize (checks, checkmax, checkcnt, (void));
1344 checks [checkcnt - 1] = w; 1384 checks [checkcnt - 1] = w;
1345} 1385}
1346 1386
1347void 1387void
1348ev_check_stop (EV_P_ struct ev_check *w) 1388ev_check_stop (EV_P_ struct ev_check *w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines