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.75 by root, Tue Nov 6 19:29:20 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
145typedef struct ev_watcher_list *WL; 148typedef struct ev_watcher_list *WL;
146typedef struct ev_watcher_time *WT; 149typedef struct ev_watcher_time *WT;
147 150
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149 152
150#if WIN32 153#include "ev_win32.c"
151/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f;
154#endif
155 154
156/*****************************************************************************/ 155/*****************************************************************************/
157 156
158static void (*syserr_cb)(const char *msg); 157static void (*syserr_cb)(const char *msg);
159 158
269ev_now (EV_P) 268ev_now (EV_P)
270{ 269{
271 return rt_now; 270 return rt_now;
272} 271}
273 272
274#define array_roundsize(base,n) ((n) | 4 & ~3) 273#define array_roundsize(type,n) ((n) | 4 & ~3)
275 274
276#define array_needsize(base,cur,cnt,init) \ 275#define array_needsize(type,base,cur,cnt,init) \
277 if (expect_false ((cnt) > cur)) \ 276 if (expect_false ((cnt) > cur)) \
278 { \ 277 { \
279 int newcnt = cur; \ 278 int newcnt = cur; \
280 do \ 279 do \
281 { \ 280 { \
282 newcnt = array_roundsize (base, newcnt << 1); \ 281 newcnt = array_roundsize (type, newcnt << 1); \
283 } \ 282 } \
284 while ((cnt) > newcnt); \ 283 while ((cnt) > newcnt); \
285 \ 284 \
286 base = ev_realloc (base, sizeof (*base) * (newcnt)); \ 285 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
287 init (base + cur, newcnt - cur); \ 286 init (base + cur, newcnt - cur); \
288 cur = newcnt; \ 287 cur = newcnt; \
289 } 288 }
290 289
291#define array_slim(stem) \ 290#define array_slim(type,stem) \
292 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 291 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
293 { \ 292 { \
294 stem ## max = array_roundsize (stem ## cnt >> 1); \ 293 stem ## max = array_roundsize (stem ## cnt >> 1); \
295 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 294 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
296 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
297 } 296 }
297
298/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
299/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
300#define array_free_microshit(stem) \
301 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
298 302
299#define array_free(stem, idx) \ 303#define array_free(stem, idx) \
300 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 304 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
301 305
302/*****************************************************************************/ 306/*****************************************************************************/
322 pendings [ABSPRI (w)][w->pending - 1].events |= events; 326 pendings [ABSPRI (w)][w->pending - 1].events |= events;
323 return; 327 return;
324 } 328 }
325 329
326 w->pending = ++pendingcnt [ABSPRI (w)]; 330 w->pending = ++pendingcnt [ABSPRI (w)];
327 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 331 array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void));
328 pendings [ABSPRI (w)][w->pending - 1].w = w; 332 pendings [ABSPRI (w)][w->pending - 1].w = w;
329 pendings [ABSPRI (w)][w->pending - 1].events = events; 333 pendings [ABSPRI (w)][w->pending - 1].events = events;
330} 334}
331 335
332static void 336static void
387 return; 391 return;
388 392
389 anfds [fd].reify = 1; 393 anfds [fd].reify = 1;
390 394
391 ++fdchangecnt; 395 ++fdchangecnt;
392 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 396 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
393 fdchanges [fdchangecnt - 1] = fd; 397 fdchanges [fdchangecnt - 1] = fd;
394} 398}
395 399
396static void 400static void
397fd_kill (EV_P_ int fd) 401fd_kill (EV_P_ int fd)
403 ev_io_stop (EV_A_ w); 407 ev_io_stop (EV_A_ w);
404 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 408 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
405 } 409 }
406} 410}
407 411
412static int
413fd_valid (int fd)
414{
415#ifdef WIN32
416 return !!win32_get_osfhandle (fd);
417#else
418 return fcntl (fd, F_GETFD) != -1;
419#endif
420}
421
408/* called on EBADF to verify fds */ 422/* called on EBADF to verify fds */
409static void 423static void
410fd_ebadf (EV_P) 424fd_ebadf (EV_P)
411{ 425{
412 int fd; 426 int fd;
413 427
414 for (fd = 0; fd < anfdmax; ++fd) 428 for (fd = 0; fd < anfdmax; ++fd)
415 if (anfds [fd].events) 429 if (anfds [fd].events)
416 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 430 if (!fd_valid (fd) == -1 && errno == EBADF)
417 fd_kill (EV_A_ fd); 431 fd_kill (EV_A_ fd);
418} 432}
419 433
420/* called on ENOMEM in select/poll to kill some fds and retry */ 434/* called on ENOMEM in select/poll to kill some fds and retry */
421static void 435static void
527 541
528 if (!gotsig) 542 if (!gotsig)
529 { 543 {
530 int old_errno = errno; 544 int old_errno = errno;
531 gotsig = 1; 545 gotsig = 1;
546#ifdef WIN32
547 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
548#else
532 write (sigpipe [1], &signum, 1); 549 write (sigpipe [1], &signum, 1);
550#endif
533 errno = old_errno; 551 errno = old_errno;
534 } 552 }
535} 553}
536 554
537static void 555static void
538sigcb (EV_P_ struct ev_io *iow, int revents) 556sigcb (EV_P_ struct ev_io *iow, int revents)
539{ 557{
540 WL w; 558 WL w;
541 int signum; 559 int signum;
542 560
561#ifdef WIN32
562 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
563#else
543 read (sigpipe [0], &revents, 1); 564 read (sigpipe [0], &revents, 1);
565#endif
544 gotsig = 0; 566 gotsig = 0;
545 567
546 for (signum = signalmax; signum--; ) 568 for (signum = signalmax; signum--; )
547 if (signals [signum].gotsig) 569 if (signals [signum].gotsig)
548 { 570 {
570 ev_unref (EV_A); /* child watcher should not keep loop alive */ 592 ev_unref (EV_A); /* child watcher should not keep loop alive */
571} 593}
572 594
573/*****************************************************************************/ 595/*****************************************************************************/
574 596
597static struct ev_child *childs [PID_HASHSIZE];
598
575#ifndef WIN32 599#ifndef WIN32
576 600
577static struct ev_child *childs [PID_HASHSIZE];
578static struct ev_signal childev; 601static struct ev_signal childev;
579 602
580#ifndef WCONTINUED 603#ifndef WCONTINUED
581# define WCONTINUED 0 604# define WCONTINUED 0
582#endif 605#endif
726#endif 749#endif
727 750
728 for (i = NUMPRI; i--; ) 751 for (i = NUMPRI; i--; )
729 array_free (pending, [i]); 752 array_free (pending, [i]);
730 753
754 /* have to use the microsoft-never-gets-it-right macro */
731 array_free (fdchange, ); 755 array_free_microshit (fdchange);
732 array_free (timer, ); 756 array_free_microshit (timer);
733 array_free (periodic, ); 757 array_free_microshit (periodic);
734 array_free (idle, ); 758 array_free_microshit (idle);
735 array_free (prepare, ); 759 array_free_microshit (prepare);
736 array_free (check, ); 760 array_free_microshit (check);
737 761
738 method = 0; 762 method = 0;
739} 763}
740 764
741static void 765static void
846{ 870{
847#if EV_MULTIPLICITY 871#if EV_MULTIPLICITY
848 struct ev_loop *loop = default_loop; 872 struct ev_loop *loop = default_loop;
849#endif 873#endif
850 874
875#ifndef WIN32
851 ev_ref (EV_A); /* child watcher */ 876 ev_ref (EV_A); /* child watcher */
852 ev_signal_stop (EV_A_ &childev); 877 ev_signal_stop (EV_A_ &childev);
878#endif
853 879
854 ev_ref (EV_A); /* signal watcher */ 880 ev_ref (EV_A); /* signal watcher */
855 ev_io_stop (EV_A_ &sigev); 881 ev_io_stop (EV_A_ &sigev);
856 882
857 close (sigpipe [0]); sigpipe [0] = 0; 883 close (sigpipe [0]); sigpipe [0] = 0;
1191 return; 1217 return;
1192 1218
1193 assert (("ev_io_start called with negative fd", fd >= 0)); 1219 assert (("ev_io_start called with negative fd", fd >= 0));
1194 1220
1195 ev_start (EV_A_ (W)w, 1); 1221 ev_start (EV_A_ (W)w, 1);
1196 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1222 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1197 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1223 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1198 1224
1199 fd_change (EV_A_ fd); 1225 fd_change (EV_A_ fd);
1200} 1226}
1201 1227
1221 ((WT)w)->at += mn_now; 1247 ((WT)w)->at += mn_now;
1222 1248
1223 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1249 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1224 1250
1225 ev_start (EV_A_ (W)w, ++timercnt); 1251 ev_start (EV_A_ (W)w, ++timercnt);
1226 array_needsize (timers, timermax, timercnt, ); 1252 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1227 timers [timercnt - 1] = w; 1253 timers [timercnt - 1] = w;
1228 upheap ((WT *)timers, timercnt - 1); 1254 upheap ((WT *)timers, timercnt - 1);
1229 1255
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1256 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1231} 1257}
1278 /* this formula differs from the one in periodic_reify because we do not always round up */ 1304 /* this formula differs from the one in periodic_reify because we do not always round up */
1279 if (w->interval) 1305 if (w->interval)
1280 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1306 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1281 1307
1282 ev_start (EV_A_ (W)w, ++periodiccnt); 1308 ev_start (EV_A_ (W)w, ++periodiccnt);
1283 array_needsize (periodics, periodicmax, periodiccnt, ); 1309 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1284 periodics [periodiccnt - 1] = w; 1310 periodics [periodiccnt - 1] = w;
1285 upheap ((WT *)periodics, periodiccnt - 1); 1311 upheap ((WT *)periodics, periodiccnt - 1);
1286 1312
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1313 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1288} 1314}
1310{ 1336{
1311 if (ev_is_active (w)) 1337 if (ev_is_active (w))
1312 return; 1338 return;
1313 1339
1314 ev_start (EV_A_ (W)w, ++idlecnt); 1340 ev_start (EV_A_ (W)w, ++idlecnt);
1315 array_needsize (idles, idlemax, idlecnt, ); 1341 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1316 idles [idlecnt - 1] = w; 1342 idles [idlecnt - 1] = w;
1317} 1343}
1318 1344
1319void 1345void
1320ev_idle_stop (EV_P_ struct ev_idle *w) 1346ev_idle_stop (EV_P_ struct ev_idle *w)
1332{ 1358{
1333 if (ev_is_active (w)) 1359 if (ev_is_active (w))
1334 return; 1360 return;
1335 1361
1336 ev_start (EV_A_ (W)w, ++preparecnt); 1362 ev_start (EV_A_ (W)w, ++preparecnt);
1337 array_needsize (prepares, preparemax, preparecnt, ); 1363 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1338 prepares [preparecnt - 1] = w; 1364 prepares [preparecnt - 1] = w;
1339} 1365}
1340 1366
1341void 1367void
1342ev_prepare_stop (EV_P_ struct ev_prepare *w) 1368ev_prepare_stop (EV_P_ struct ev_prepare *w)
1354{ 1380{
1355 if (ev_is_active (w)) 1381 if (ev_is_active (w))
1356 return; 1382 return;
1357 1383
1358 ev_start (EV_A_ (W)w, ++checkcnt); 1384 ev_start (EV_A_ (W)w, ++checkcnt);
1359 array_needsize (checks, checkmax, checkcnt, ); 1385 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1360 checks [checkcnt - 1] = w; 1386 checks [checkcnt - 1] = w;
1361} 1387}
1362 1388
1363void 1389void
1364ev_check_stop (EV_P_ struct ev_check *w) 1390ev_check_stop (EV_P_ struct ev_check *w)
1385 return; 1411 return;
1386 1412
1387 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1413 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1388 1414
1389 ev_start (EV_A_ (W)w, 1); 1415 ev_start (EV_A_ (W)w, 1);
1390 array_needsize (signals, signalmax, w->signum, signals_init); 1416 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1417 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1392 1418
1393 if (!((WL)w)->next) 1419 if (!((WL)w)->next)
1394 { 1420 {
1395#if WIN32 1421#if WIN32
1478} 1504}
1479 1505
1480void 1506void
1481ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1507ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1482{ 1508{
1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1509 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1484 1510
1485 if (!once) 1511 if (!once)
1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1512 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1487 else 1513 else
1488 { 1514 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines