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.76 by root, Wed Nov 7 18:47:26 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;
870 if (method) 896 if (method)
871 postfork = 1; 897 postfork = 1;
872} 898}
873 899
874/*****************************************************************************/ 900/*****************************************************************************/
901
902static int
903any_pending (EV_P)
904{
905 int pri;
906
907 for (pri = NUMPRI; pri--; )
908 if (pendingcnt [pri])
909 return 1;
910
911 return 0;
912}
875 913
876static void 914static void
877call_pending (EV_P) 915call_pending (EV_P)
878{ 916{
879 int pri; 917 int pri;
1063 /* update fd-related kernel structures */ 1101 /* update fd-related kernel structures */
1064 fd_reify (EV_A); 1102 fd_reify (EV_A);
1065 1103
1066 /* calculate blocking time */ 1104 /* calculate blocking time */
1067 1105
1068 /* we only need this for !monotonic clockor timers, but as we basically 1106 /* we only need this for !monotonic clock or timers, but as we basically
1069 always have timers, we just calculate it always */ 1107 always have timers, we just calculate it always */
1070#if EV_USE_MONOTONIC 1108#if EV_USE_MONOTONIC
1071 if (expect_true (have_monotonic)) 1109 if (expect_true (have_monotonic))
1072 time_update_monotonic (EV_A); 1110 time_update_monotonic (EV_A);
1073 else 1111 else
1106 /* queue pending timers and reschedule them */ 1144 /* queue pending timers and reschedule them */
1107 timers_reify (EV_A); /* relative timers called last */ 1145 timers_reify (EV_A); /* relative timers called last */
1108 periodics_reify (EV_A); /* absolute timers called first */ 1146 periodics_reify (EV_A); /* absolute timers called first */
1109 1147
1110 /* queue idle watchers unless io or timers are pending */ 1148 /* queue idle watchers unless io or timers are pending */
1111 if (!pendingcnt) 1149 if (idlecnt && !any_pending (EV_A))
1112 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1150 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1113 1151
1114 /* queue check watchers, to be executed first */ 1152 /* queue check watchers, to be executed first */
1115 if (checkcnt) 1153 if (checkcnt)
1116 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1154 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1191 return; 1229 return;
1192 1230
1193 assert (("ev_io_start called with negative fd", fd >= 0)); 1231 assert (("ev_io_start called with negative fd", fd >= 0));
1194 1232
1195 ev_start (EV_A_ (W)w, 1); 1233 ev_start (EV_A_ (W)w, 1);
1196 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1234 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1197 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1235 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1198 1236
1199 fd_change (EV_A_ fd); 1237 fd_change (EV_A_ fd);
1200} 1238}
1201 1239
1221 ((WT)w)->at += mn_now; 1259 ((WT)w)->at += mn_now;
1222 1260
1223 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1261 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1224 1262
1225 ev_start (EV_A_ (W)w, ++timercnt); 1263 ev_start (EV_A_ (W)w, ++timercnt);
1226 array_needsize (timers, timermax, timercnt, ); 1264 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1227 timers [timercnt - 1] = w; 1265 timers [timercnt - 1] = w;
1228 upheap ((WT *)timers, timercnt - 1); 1266 upheap ((WT *)timers, timercnt - 1);
1229 1267
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1268 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1231} 1269}
1278 /* this formula differs from the one in periodic_reify because we do not always round up */ 1316 /* this formula differs from the one in periodic_reify because we do not always round up */
1279 if (w->interval) 1317 if (w->interval)
1280 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1318 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1281 1319
1282 ev_start (EV_A_ (W)w, ++periodiccnt); 1320 ev_start (EV_A_ (W)w, ++periodiccnt);
1283 array_needsize (periodics, periodicmax, periodiccnt, ); 1321 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1284 periodics [periodiccnt - 1] = w; 1322 periodics [periodiccnt - 1] = w;
1285 upheap ((WT *)periodics, periodiccnt - 1); 1323 upheap ((WT *)periodics, periodiccnt - 1);
1286 1324
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1325 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1288} 1326}
1310{ 1348{
1311 if (ev_is_active (w)) 1349 if (ev_is_active (w))
1312 return; 1350 return;
1313 1351
1314 ev_start (EV_A_ (W)w, ++idlecnt); 1352 ev_start (EV_A_ (W)w, ++idlecnt);
1315 array_needsize (idles, idlemax, idlecnt, ); 1353 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1316 idles [idlecnt - 1] = w; 1354 idles [idlecnt - 1] = w;
1317} 1355}
1318 1356
1319void 1357void
1320ev_idle_stop (EV_P_ struct ev_idle *w) 1358ev_idle_stop (EV_P_ struct ev_idle *w)
1332{ 1370{
1333 if (ev_is_active (w)) 1371 if (ev_is_active (w))
1334 return; 1372 return;
1335 1373
1336 ev_start (EV_A_ (W)w, ++preparecnt); 1374 ev_start (EV_A_ (W)w, ++preparecnt);
1337 array_needsize (prepares, preparemax, preparecnt, ); 1375 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1338 prepares [preparecnt - 1] = w; 1376 prepares [preparecnt - 1] = w;
1339} 1377}
1340 1378
1341void 1379void
1342ev_prepare_stop (EV_P_ struct ev_prepare *w) 1380ev_prepare_stop (EV_P_ struct ev_prepare *w)
1354{ 1392{
1355 if (ev_is_active (w)) 1393 if (ev_is_active (w))
1356 return; 1394 return;
1357 1395
1358 ev_start (EV_A_ (W)w, ++checkcnt); 1396 ev_start (EV_A_ (W)w, ++checkcnt);
1359 array_needsize (checks, checkmax, checkcnt, ); 1397 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1360 checks [checkcnt - 1] = w; 1398 checks [checkcnt - 1] = w;
1361} 1399}
1362 1400
1363void 1401void
1364ev_check_stop (EV_P_ struct ev_check *w) 1402ev_check_stop (EV_P_ struct ev_check *w)
1385 return; 1423 return;
1386 1424
1387 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1425 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1388 1426
1389 ev_start (EV_A_ (W)w, 1); 1427 ev_start (EV_A_ (W)w, 1);
1390 array_needsize (signals, signalmax, w->signum, signals_init); 1428 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1429 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1392 1430
1393 if (!((WL)w)->next) 1431 if (!((WL)w)->next)
1394 { 1432 {
1395#if WIN32 1433#if WIN32
1478} 1516}
1479 1517
1480void 1518void
1481ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1519ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1482{ 1520{
1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1521 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1484 1522
1485 if (!once) 1523 if (!once)
1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1524 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1487 else 1525 else
1488 { 1526 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines