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

Comparing libev/ev.c (file contents):
Revision 1.68 by root, Mon Nov 5 20:19:00 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
150#if WIN32 155#if WIN32
151/* note: the comment below could not be substantiated, but what would I care */ 156/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */ 157/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f; 158volatile double SIGFPE_REQ = 0.0f;
154#endif 159#endif
160
161/*****************************************************************************/
162
163static void (*syserr_cb)(const char *msg);
164
165void ev_set_syserr_cb (void (*cb)(const char *msg))
166{
167 syserr_cb = cb;
168}
169
170static void
171syserr (const char *msg)
172{
173 if (!msg)
174 msg = "(libev) system error";
175
176 if (syserr_cb)
177 syserr_cb (msg);
178 else
179 {
180 perror (msg);
181 abort ();
182 }
183}
184
185static void *(*alloc)(void *ptr, long size);
186
187void ev_set_allocator (void *(*cb)(void *ptr, long size))
188{
189 alloc = cb;
190}
191
192static void *
193ev_realloc (void *ptr, long size)
194{
195 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
196
197 if (!ptr && size)
198 {
199 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
200 abort ();
201 }
202
203 return ptr;
204}
205
206#define ev_malloc(size) ev_realloc (0, (size))
207#define ev_free(ptr) ev_realloc ((ptr), 0)
155 208
156/*****************************************************************************/ 209/*****************************************************************************/
157 210
158typedef struct 211typedef struct
159{ 212{
223 return rt_now; 276 return rt_now;
224} 277}
225 278
226#define array_roundsize(base,n) ((n) | 4 & ~3) 279#define array_roundsize(base,n) ((n) | 4 & ~3)
227 280
228#define array_needsize(base,cur,cnt,init) \ 281#define array_needsize(base,cur,cnt,init) \
229 if (expect_false ((cnt) > cur)) \ 282 if (expect_false ((cnt) > cur)) \
230 { \ 283 { \
231 int newcnt = cur; \ 284 int newcnt = cur; \
232 do \ 285 do \
233 { \ 286 { \
234 newcnt = array_roundsize (base, newcnt << 1); \ 287 newcnt = array_roundsize (base, newcnt << 1); \
235 } \ 288 } \
236 while ((cnt) > newcnt); \ 289 while ((cnt) > newcnt); \
237 \ 290 \
238 base = realloc (base, sizeof (*base) * (newcnt)); \ 291 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
239 init (base + cur, newcnt - cur); \ 292 init (base + cur, newcnt - cur); \
240 cur = newcnt; \ 293 cur = newcnt; \
241 } 294 }
242 295
243#define array_slim(stem) \ 296#define array_slim(stem) \
244 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 297 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
245 { \ 298 { \
246 stem ## max = array_roundsize (stem ## cnt >> 1); \ 299 stem ## max = array_roundsize (stem ## cnt >> 1); \
247 base = realloc (base, sizeof (*base) * (stem ## max)); \ 300 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
248 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 301 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
249 } 302 }
250 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
251#define array_free(stem, idx) \ 309#define array_free(stem, idx) \
252 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 310 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
253 311
254/*****************************************************************************/ 312/*****************************************************************************/
255 313
256static void 314static void
257anfds_init (ANFD *base, int count) 315anfds_init (ANFD *base, int count)
274 pendings [ABSPRI (w)][w->pending - 1].events |= events; 332 pendings [ABSPRI (w)][w->pending - 1].events |= events;
275 return; 333 return;
276 } 334 }
277 335
278 w->pending = ++pendingcnt [ABSPRI (w)]; 336 w->pending = ++pendingcnt [ABSPRI (w)];
279 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 337 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void));
280 pendings [ABSPRI (w)][w->pending - 1].w = w; 338 pendings [ABSPRI (w)][w->pending - 1].w = w;
281 pendings [ABSPRI (w)][w->pending - 1].events = events; 339 pendings [ABSPRI (w)][w->pending - 1].events = events;
282} 340}
283 341
284static void 342static void
333} 391}
334 392
335static void 393static void
336fd_change (EV_P_ int fd) 394fd_change (EV_P_ int fd)
337{ 395{
338 if (anfds [fd].reify || fdchangecnt < 0) 396 if (anfds [fd].reify)
339 return; 397 return;
340 398
341 anfds [fd].reify = 1; 399 anfds [fd].reify = 1;
342 400
343 ++fdchangecnt; 401 ++fdchangecnt;
344 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 402 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void));
345 fdchanges [fdchangecnt - 1] = fd; 403 fdchanges [fdchangecnt - 1] = fd;
346} 404}
347 405
348static void 406static void
349fd_kill (EV_P_ int fd) 407fd_kill (EV_P_ int fd)
355 ev_io_stop (EV_A_ w); 413 ev_io_stop (EV_A_ w);
356 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 414 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
357 } 415 }
358} 416}
359 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
360/* called on EBADF to verify fds */ 428/* called on EBADF to verify fds */
361static void 429static void
362fd_ebadf (EV_P) 430fd_ebadf (EV_P)
363{ 431{
364 int fd; 432 int fd;
365 433
366 for (fd = 0; fd < anfdmax; ++fd) 434 for (fd = 0; fd < anfdmax; ++fd)
367 if (anfds [fd].events) 435 if (anfds [fd].events)
368 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 436 if (!fd_valid (fd) == -1 && errno == EBADF)
369 fd_kill (EV_A_ fd); 437 fd_kill (EV_A_ fd);
370} 438}
371 439
372/* 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 */
373static void 441static void
381 fd_kill (EV_A_ fd); 449 fd_kill (EV_A_ fd);
382 return; 450 return;
383 } 451 }
384} 452}
385 453
386/* 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 */
387static void 455static void
388fd_rearm_all (EV_P) 456fd_rearm_all (EV_P)
389{ 457{
390 int fd; 458 int fd;
391 459
522 ev_unref (EV_A); /* child watcher should not keep loop alive */ 590 ev_unref (EV_A); /* child watcher should not keep loop alive */
523} 591}
524 592
525/*****************************************************************************/ 593/*****************************************************************************/
526 594
595static struct ev_child *childs [PID_HASHSIZE];
596
527#ifndef WIN32 597#ifndef WIN32
528 598
529static struct ev_child *childs [PID_HASHSIZE];
530static struct ev_signal childev; 599static struct ev_signal childev;
531 600
532#ifndef WCONTINUED 601#ifndef WCONTINUED
533# define WCONTINUED 0 602# define WCONTINUED 0
534#endif 603#endif
648 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 717 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
649#endif 718#endif
650#if EV_USE_SELECT 719#if EV_USE_SELECT
651 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 720 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
652#endif 721#endif
722
723 ev_watcher_init (&sigev, sigcb);
724 ev_set_priority (&sigev, EV_MAXPRI);
653 } 725 }
654} 726}
655 727
656void 728void
657loop_destroy (EV_P) 729loop_destroy (EV_P)
675#endif 747#endif
676 748
677 for (i = NUMPRI; i--; ) 749 for (i = NUMPRI; i--; )
678 array_free (pending, [i]); 750 array_free (pending, [i]);
679 751
752 /* have to use the microsoft-never-gets-it-right macro */
680 array_free (fdchange, ); 753 array_free_microshit (fdchange);
681 array_free (timer, ); 754 array_free_microshit (timer);
682 array_free (periodic, ); 755 array_free_microshit (periodic);
683 array_free (idle, ); 756 array_free_microshit (idle);
684 array_free (prepare, ); 757 array_free_microshit (prepare);
685 array_free (check, ); 758 array_free_microshit (check);
686 759
687 method = 0; 760 method = 0;
688 /*TODO*/
689} 761}
690 762
691void 763static void
692loop_fork (EV_P) 764loop_fork (EV_P)
693{ 765{
694 /*TODO*/
695#if EV_USE_EPOLL 766#if EV_USE_EPOLL
696 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 767 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
697#endif 768#endif
698#if EV_USE_KQUEUE 769#if EV_USE_KQUEUE
699 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 770 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
700#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;
701} 789}
702 790
703#if EV_MULTIPLICITY 791#if EV_MULTIPLICITY
704struct ev_loop * 792struct ev_loop *
705ev_loop_new (int methods) 793ev_loop_new (int methods)
706{ 794{
707 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 795 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
796
797 memset (loop, 0, sizeof (struct ev_loop));
708 798
709 loop_init (EV_A_ methods); 799 loop_init (EV_A_ methods);
710 800
711 if (ev_method (EV_A)) 801 if (ev_method (EV_A))
712 return loop; 802 return loop;
716 806
717void 807void
718ev_loop_destroy (EV_P) 808ev_loop_destroy (EV_P)
719{ 809{
720 loop_destroy (EV_A); 810 loop_destroy (EV_A);
721 free (loop); 811 ev_free (loop);
722} 812}
723 813
724void 814void
725ev_loop_fork (EV_P) 815ev_loop_fork (EV_P)
726{ 816{
727 loop_fork (EV_A); 817 postfork = 1;
728} 818}
729 819
730#endif 820#endif
731 821
732#if EV_MULTIPLICITY 822#if EV_MULTIPLICITY
755 845
756 loop_init (EV_A_ methods); 846 loop_init (EV_A_ methods);
757 847
758 if (ev_method (EV_A)) 848 if (ev_method (EV_A))
759 { 849 {
760 ev_watcher_init (&sigev, sigcb);
761 ev_set_priority (&sigev, EV_MAXPRI);
762 siginit (EV_A); 850 siginit (EV_A);
763 851
764#ifndef WIN32 852#ifndef WIN32
765 ev_signal_init (&childev, childcb, SIGCHLD); 853 ev_signal_init (&childev, childcb, SIGCHLD);
766 ev_set_priority (&childev, EV_MAXPRI); 854 ev_set_priority (&childev, EV_MAXPRI);
780{ 868{
781#if EV_MULTIPLICITY 869#if EV_MULTIPLICITY
782 struct ev_loop *loop = default_loop; 870 struct ev_loop *loop = default_loop;
783#endif 871#endif
784 872
873#ifndef WIN32
785 ev_ref (EV_A); /* child watcher */ 874 ev_ref (EV_A); /* child watcher */
786 ev_signal_stop (EV_A_ &childev); 875 ev_signal_stop (EV_A_ &childev);
876#endif
787 877
788 ev_ref (EV_A); /* signal watcher */ 878 ev_ref (EV_A); /* signal watcher */
789 ev_io_stop (EV_A_ &sigev); 879 ev_io_stop (EV_A_ &sigev);
790 880
791 close (sigpipe [0]); sigpipe [0] = 0; 881 close (sigpipe [0]); sigpipe [0] = 0;
799{ 889{
800#if EV_MULTIPLICITY 890#if EV_MULTIPLICITY
801 struct ev_loop *loop = default_loop; 891 struct ev_loop *loop = default_loop;
802#endif 892#endif
803 893
804 loop_fork (EV_A); 894 if (method)
805 895 postfork = 1;
806 ev_io_stop (EV_A_ &sigev);
807 close (sigpipe [0]);
808 close (sigpipe [1]);
809 pipe (sigpipe);
810
811 ev_ref (EV_A); /* signal watcher */
812 siginit (EV_A);
813} 896}
814 897
815/*****************************************************************************/ 898/*****************************************************************************/
816 899
817static void 900static void
995 { 1078 {
996 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1079 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
997 call_pending (EV_A); 1080 call_pending (EV_A);
998 } 1081 }
999 1082
1083 /* we might have forked, so reify kernel state if necessary */
1084 if (expect_false (postfork))
1085 loop_fork (EV_A);
1086
1000 /* update fd-related kernel structures */ 1087 /* update fd-related kernel structures */
1001 fd_reify (EV_A); 1088 fd_reify (EV_A);
1002 1089
1003 /* calculate blocking time */ 1090 /* calculate blocking time */
1004 1091
1158 ((WT)w)->at += mn_now; 1245 ((WT)w)->at += mn_now;
1159 1246
1160 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.));
1161 1248
1162 ev_start (EV_A_ (W)w, ++timercnt); 1249 ev_start (EV_A_ (W)w, ++timercnt);
1163 array_needsize (timers, timermax, timercnt, ); 1250 array_needsize (timers, timermax, timercnt, (void));
1164 timers [timercnt - 1] = w; 1251 timers [timercnt - 1] = w;
1165 upheap ((WT *)timers, timercnt - 1); 1252 upheap ((WT *)timers, timercnt - 1);
1166 1253
1167 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1254 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1168} 1255}
1215 /* 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 */
1216 if (w->interval) 1303 if (w->interval)
1217 ((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;
1218 1305
1219 ev_start (EV_A_ (W)w, ++periodiccnt); 1306 ev_start (EV_A_ (W)w, ++periodiccnt);
1220 array_needsize (periodics, periodicmax, periodiccnt, ); 1307 array_needsize (periodics, periodicmax, periodiccnt, (void));
1221 periodics [periodiccnt - 1] = w; 1308 periodics [periodiccnt - 1] = w;
1222 upheap ((WT *)periodics, periodiccnt - 1); 1309 upheap ((WT *)periodics, periodiccnt - 1);
1223 1310
1224 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1311 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1225} 1312}
1247{ 1334{
1248 if (ev_is_active (w)) 1335 if (ev_is_active (w))
1249 return; 1336 return;
1250 1337
1251 ev_start (EV_A_ (W)w, ++idlecnt); 1338 ev_start (EV_A_ (W)w, ++idlecnt);
1252 array_needsize (idles, idlemax, idlecnt, ); 1339 array_needsize (idles, idlemax, idlecnt, (void));
1253 idles [idlecnt - 1] = w; 1340 idles [idlecnt - 1] = w;
1254} 1341}
1255 1342
1256void 1343void
1257ev_idle_stop (EV_P_ struct ev_idle *w) 1344ev_idle_stop (EV_P_ struct ev_idle *w)
1269{ 1356{
1270 if (ev_is_active (w)) 1357 if (ev_is_active (w))
1271 return; 1358 return;
1272 1359
1273 ev_start (EV_A_ (W)w, ++preparecnt); 1360 ev_start (EV_A_ (W)w, ++preparecnt);
1274 array_needsize (prepares, preparemax, preparecnt, ); 1361 array_needsize (prepares, preparemax, preparecnt, (void));
1275 prepares [preparecnt - 1] = w; 1362 prepares [preparecnt - 1] = w;
1276} 1363}
1277 1364
1278void 1365void
1279ev_prepare_stop (EV_P_ struct ev_prepare *w) 1366ev_prepare_stop (EV_P_ struct ev_prepare *w)
1291{ 1378{
1292 if (ev_is_active (w)) 1379 if (ev_is_active (w))
1293 return; 1380 return;
1294 1381
1295 ev_start (EV_A_ (W)w, ++checkcnt); 1382 ev_start (EV_A_ (W)w, ++checkcnt);
1296 array_needsize (checks, checkmax, checkcnt, ); 1383 array_needsize (checks, checkmax, checkcnt, (void));
1297 checks [checkcnt - 1] = w; 1384 checks [checkcnt - 1] = w;
1298} 1385}
1299 1386
1300void 1387void
1301ev_check_stop (EV_P_ struct ev_check *w) 1388ev_check_stop (EV_P_ struct ev_check *w)
1395 void (*cb)(int revents, void *arg) = once->cb; 1482 void (*cb)(int revents, void *arg) = once->cb;
1396 void *arg = once->arg; 1483 void *arg = once->arg;
1397 1484
1398 ev_io_stop (EV_A_ &once->io); 1485 ev_io_stop (EV_A_ &once->io);
1399 ev_timer_stop (EV_A_ &once->to); 1486 ev_timer_stop (EV_A_ &once->to);
1400 free (once); 1487 ev_free (once);
1401 1488
1402 cb (revents, arg); 1489 cb (revents, arg);
1403} 1490}
1404 1491
1405static void 1492static void
1415} 1502}
1416 1503
1417void 1504void
1418ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1505ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1419{ 1506{
1420 struct ev_once *once = malloc (sizeof (struct ev_once)); 1507 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1421 1508
1422 if (!once) 1509 if (!once)
1423 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1510 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1424 else 1511 else
1425 { 1512 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines