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

Comparing libev/ev.c (file contents):
Revision 1.70 by root, Tue Nov 6 00:52:32 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
294 stem ## max = array_roundsize (stem ## cnt >> 1); \ 299 stem ## max = array_roundsize (stem ## cnt >> 1); \
295 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 300 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
296 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 301 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
297 } 302 }
298 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
299#define array_free(stem, idx) \ 309#define array_free(stem, idx) \
300 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;
301 311
302/*****************************************************************************/ 312/*****************************************************************************/
303 313
322 pendings [ABSPRI (w)][w->pending - 1].events |= events; 332 pendings [ABSPRI (w)][w->pending - 1].events |= events;
323 return; 333 return;
324 } 334 }
325 335
326 w->pending = ++pendingcnt [ABSPRI (w)]; 336 w->pending = ++pendingcnt [ABSPRI (w)];
327 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 337 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void));
328 pendings [ABSPRI (w)][w->pending - 1].w = w; 338 pendings [ABSPRI (w)][w->pending - 1].w = w;
329 pendings [ABSPRI (w)][w->pending - 1].events = events; 339 pendings [ABSPRI (w)][w->pending - 1].events = events;
330} 340}
331 341
332static void 342static void
387 return; 397 return;
388 398
389 anfds [fd].reify = 1; 399 anfds [fd].reify = 1;
390 400
391 ++fdchangecnt; 401 ++fdchangecnt;
392 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 402 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void));
393 fdchanges [fdchangecnt - 1] = fd; 403 fdchanges [fdchangecnt - 1] = fd;
394} 404}
395 405
396static void 406static void
397fd_kill (EV_P_ int fd) 407fd_kill (EV_P_ int fd)
403 ev_io_stop (EV_A_ w); 413 ev_io_stop (EV_A_ w);
404 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 414 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
405 } 415 }
406} 416}
407 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
408/* called on EBADF to verify fds */ 428/* called on EBADF to verify fds */
409static void 429static void
410fd_ebadf (EV_P) 430fd_ebadf (EV_P)
411{ 431{
412 int fd; 432 int fd;
413 433
414 for (fd = 0; fd < anfdmax; ++fd) 434 for (fd = 0; fd < anfdmax; ++fd)
415 if (anfds [fd].events) 435 if (anfds [fd].events)
416 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 436 if (!fd_valid (fd) == -1 && errno == EBADF)
417 fd_kill (EV_A_ fd); 437 fd_kill (EV_A_ fd);
418} 438}
419 439
420/* 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 */
421static void 441static void
570 ev_unref (EV_A); /* child watcher should not keep loop alive */ 590 ev_unref (EV_A); /* child watcher should not keep loop alive */
571} 591}
572 592
573/*****************************************************************************/ 593/*****************************************************************************/
574 594
595static struct ev_child *childs [PID_HASHSIZE];
596
575#ifndef WIN32 597#ifndef WIN32
576 598
577static struct ev_child *childs [PID_HASHSIZE];
578static struct ev_signal childev; 599static struct ev_signal childev;
579 600
580#ifndef WCONTINUED 601#ifndef WCONTINUED
581# define WCONTINUED 0 602# define WCONTINUED 0
582#endif 603#endif
726#endif 747#endif
727 748
728 for (i = NUMPRI; i--; ) 749 for (i = NUMPRI; i--; )
729 array_free (pending, [i]); 750 array_free (pending, [i]);
730 751
752 /* have to use the microsoft-never-gets-it-right macro */
731 array_free (fdchange, ); 753 array_free_microshit (fdchange);
732 array_free (timer, ); 754 array_free_microshit (timer);
733 array_free (periodic, ); 755 array_free_microshit (periodic);
734 array_free (idle, ); 756 array_free_microshit (idle);
735 array_free (prepare, ); 757 array_free_microshit (prepare);
736 array_free (check, ); 758 array_free_microshit (check);
737 759
738 method = 0; 760 method = 0;
739} 761}
740 762
741static void 763static void
846{ 868{
847#if EV_MULTIPLICITY 869#if EV_MULTIPLICITY
848 struct ev_loop *loop = default_loop; 870 struct ev_loop *loop = default_loop;
849#endif 871#endif
850 872
873#ifndef WIN32
851 ev_ref (EV_A); /* child watcher */ 874 ev_ref (EV_A); /* child watcher */
852 ev_signal_stop (EV_A_ &childev); 875 ev_signal_stop (EV_A_ &childev);
876#endif
853 877
854 ev_ref (EV_A); /* signal watcher */ 878 ev_ref (EV_A); /* signal watcher */
855 ev_io_stop (EV_A_ &sigev); 879 ev_io_stop (EV_A_ &sigev);
856 880
857 close (sigpipe [0]); sigpipe [0] = 0; 881 close (sigpipe [0]); sigpipe [0] = 0;
1221 ((WT)w)->at += mn_now; 1245 ((WT)w)->at += mn_now;
1222 1246
1223 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.));
1224 1248
1225 ev_start (EV_A_ (W)w, ++timercnt); 1249 ev_start (EV_A_ (W)w, ++timercnt);
1226 array_needsize (timers, timermax, timercnt, ); 1250 array_needsize (timers, timermax, timercnt, (void));
1227 timers [timercnt - 1] = w; 1251 timers [timercnt - 1] = w;
1228 upheap ((WT *)timers, timercnt - 1); 1252 upheap ((WT *)timers, timercnt - 1);
1229 1253
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1254 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1231} 1255}
1278 /* 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 */
1279 if (w->interval) 1303 if (w->interval)
1280 ((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;
1281 1305
1282 ev_start (EV_A_ (W)w, ++periodiccnt); 1306 ev_start (EV_A_ (W)w, ++periodiccnt);
1283 array_needsize (periodics, periodicmax, periodiccnt, ); 1307 array_needsize (periodics, periodicmax, periodiccnt, (void));
1284 periodics [periodiccnt - 1] = w; 1308 periodics [periodiccnt - 1] = w;
1285 upheap ((WT *)periodics, periodiccnt - 1); 1309 upheap ((WT *)periodics, periodiccnt - 1);
1286 1310
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1311 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1288} 1312}
1310{ 1334{
1311 if (ev_is_active (w)) 1335 if (ev_is_active (w))
1312 return; 1336 return;
1313 1337
1314 ev_start (EV_A_ (W)w, ++idlecnt); 1338 ev_start (EV_A_ (W)w, ++idlecnt);
1315 array_needsize (idles, idlemax, idlecnt, ); 1339 array_needsize (idles, idlemax, idlecnt, (void));
1316 idles [idlecnt - 1] = w; 1340 idles [idlecnt - 1] = w;
1317} 1341}
1318 1342
1319void 1343void
1320ev_idle_stop (EV_P_ struct ev_idle *w) 1344ev_idle_stop (EV_P_ struct ev_idle *w)
1332{ 1356{
1333 if (ev_is_active (w)) 1357 if (ev_is_active (w))
1334 return; 1358 return;
1335 1359
1336 ev_start (EV_A_ (W)w, ++preparecnt); 1360 ev_start (EV_A_ (W)w, ++preparecnt);
1337 array_needsize (prepares, preparemax, preparecnt, ); 1361 array_needsize (prepares, preparemax, preparecnt, (void));
1338 prepares [preparecnt - 1] = w; 1362 prepares [preparecnt - 1] = w;
1339} 1363}
1340 1364
1341void 1365void
1342ev_prepare_stop (EV_P_ struct ev_prepare *w) 1366ev_prepare_stop (EV_P_ struct ev_prepare *w)
1354{ 1378{
1355 if (ev_is_active (w)) 1379 if (ev_is_active (w))
1356 return; 1380 return;
1357 1381
1358 ev_start (EV_A_ (W)w, ++checkcnt); 1382 ev_start (EV_A_ (W)w, ++checkcnt);
1359 array_needsize (checks, checkmax, checkcnt, ); 1383 array_needsize (checks, checkmax, checkcnt, (void));
1360 checks [checkcnt - 1] = w; 1384 checks [checkcnt - 1] = w;
1361} 1385}
1362 1386
1363void 1387void
1364ev_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