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

Comparing libev/ev.c (file contents):
Revision 1.67 by root, Mon Nov 5 16:42:15 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
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)
208
209/*****************************************************************************/
210
158typedef struct 211typedef struct
159{ 212{
160 struct ev_watcher_list *head; 213 WL head;
161 unsigned char events; 214 unsigned char events;
162 unsigned char reify; 215 unsigned char reify;
163} ANFD; 216} ANFD;
164 217
165typedef struct 218typedef struct
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
376 int fd; 444 int fd;
377 445
378 for (fd = anfdmax; fd--; ) 446 for (fd = anfdmax; fd--; )
379 if (anfds [fd].events) 447 if (anfds [fd].events)
380 { 448 {
381 close (fd);
382 fd_kill (EV_A_ fd); 449 fd_kill (EV_A_ fd);
383 return; 450 return;
384 } 451 }
385} 452}
386 453
387/* 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 */
388static void 455static void
389fd_rearm_all (EV_P) 456fd_rearm_all (EV_P)
390{ 457{
391 int fd; 458 int fd;
392 459
444 511
445/*****************************************************************************/ 512/*****************************************************************************/
446 513
447typedef struct 514typedef struct
448{ 515{
449 struct ev_watcher_list *head; 516 WL head;
450 sig_atomic_t volatile gotsig; 517 sig_atomic_t volatile gotsig;
451} ANSIG; 518} ANSIG;
452 519
453static ANSIG *signals; 520static ANSIG *signals;
454static int signalmax; 521static int signalmax;
488} 555}
489 556
490static void 557static void
491sigcb (EV_P_ struct ev_io *iow, int revents) 558sigcb (EV_P_ struct ev_io *iow, int revents)
492{ 559{
493 struct ev_watcher_list *w; 560 WL w;
494 int signum; 561 int signum;
495 562
496 read (sigpipe [0], &revents, 1); 563 read (sigpipe [0], &revents, 1);
497 gotsig = 0; 564 gotsig = 0;
498 565
523 ev_unref (EV_A); /* child watcher should not keep loop alive */ 590 ev_unref (EV_A); /* child watcher should not keep loop alive */
524} 591}
525 592
526/*****************************************************************************/ 593/*****************************************************************************/
527 594
595static struct ev_child *childs [PID_HASHSIZE];
596
528#ifndef WIN32 597#ifndef WIN32
529 598
530static struct ev_child *childs [PID_HASHSIZE];
531static struct ev_signal childev; 599static struct ev_signal childev;
532 600
533#ifndef WCONTINUED 601#ifndef WCONTINUED
534# define WCONTINUED 0 602# define WCONTINUED 0
535#endif 603#endif
649 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 717 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
650#endif 718#endif
651#if EV_USE_SELECT 719#if EV_USE_SELECT
652 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 720 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
653#endif 721#endif
722
723 ev_watcher_init (&sigev, sigcb);
724 ev_set_priority (&sigev, EV_MAXPRI);
654 } 725 }
655} 726}
656 727
657void 728void
658loop_destroy (EV_P) 729loop_destroy (EV_P)
676#endif 747#endif
677 748
678 for (i = NUMPRI; i--; ) 749 for (i = NUMPRI; i--; )
679 array_free (pending, [i]); 750 array_free (pending, [i]);
680 751
752 /* have to use the microsoft-never-gets-it-right macro */
681 array_free (fdchange, ); 753 array_free_microshit (fdchange);
682 array_free (timer, ); 754 array_free_microshit (timer);
683 array_free (periodic, ); 755 array_free_microshit (periodic);
684 array_free (idle, ); 756 array_free_microshit (idle);
685 array_free (prepare, ); 757 array_free_microshit (prepare);
686 array_free (check, ); 758 array_free_microshit (check);
687 759
688 method = 0; 760 method = 0;
689 /*TODO*/
690} 761}
691 762
692void 763static void
693loop_fork (EV_P) 764loop_fork (EV_P)
694{ 765{
695 /*TODO*/
696#if EV_USE_EPOLL 766#if EV_USE_EPOLL
697 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 767 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
698#endif 768#endif
699#if EV_USE_KQUEUE 769#if EV_USE_KQUEUE
700 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 770 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
701#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;
702} 789}
703 790
704#if EV_MULTIPLICITY 791#if EV_MULTIPLICITY
705struct ev_loop * 792struct ev_loop *
706ev_loop_new (int methods) 793ev_loop_new (int methods)
707{ 794{
708 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));
709 798
710 loop_init (EV_A_ methods); 799 loop_init (EV_A_ methods);
711 800
712 if (ev_method (EV_A)) 801 if (ev_method (EV_A))
713 return loop; 802 return loop;
717 806
718void 807void
719ev_loop_destroy (EV_P) 808ev_loop_destroy (EV_P)
720{ 809{
721 loop_destroy (EV_A); 810 loop_destroy (EV_A);
722 free (loop); 811 ev_free (loop);
723} 812}
724 813
725void 814void
726ev_loop_fork (EV_P) 815ev_loop_fork (EV_P)
727{ 816{
728 loop_fork (EV_A); 817 postfork = 1;
729} 818}
730 819
731#endif 820#endif
732 821
733#if EV_MULTIPLICITY 822#if EV_MULTIPLICITY
756 845
757 loop_init (EV_A_ methods); 846 loop_init (EV_A_ methods);
758 847
759 if (ev_method (EV_A)) 848 if (ev_method (EV_A))
760 { 849 {
761 ev_watcher_init (&sigev, sigcb);
762 ev_set_priority (&sigev, EV_MAXPRI);
763 siginit (EV_A); 850 siginit (EV_A);
764 851
765#ifndef WIN32 852#ifndef WIN32
766 ev_signal_init (&childev, childcb, SIGCHLD); 853 ev_signal_init (&childev, childcb, SIGCHLD);
767 ev_set_priority (&childev, EV_MAXPRI); 854 ev_set_priority (&childev, EV_MAXPRI);
781{ 868{
782#if EV_MULTIPLICITY 869#if EV_MULTIPLICITY
783 struct ev_loop *loop = default_loop; 870 struct ev_loop *loop = default_loop;
784#endif 871#endif
785 872
873#ifndef WIN32
786 ev_ref (EV_A); /* child watcher */ 874 ev_ref (EV_A); /* child watcher */
787 ev_signal_stop (EV_A_ &childev); 875 ev_signal_stop (EV_A_ &childev);
876#endif
788 877
789 ev_ref (EV_A); /* signal watcher */ 878 ev_ref (EV_A); /* signal watcher */
790 ev_io_stop (EV_A_ &sigev); 879 ev_io_stop (EV_A_ &sigev);
791 880
792 close (sigpipe [0]); sigpipe [0] = 0; 881 close (sigpipe [0]); sigpipe [0] = 0;
800{ 889{
801#if EV_MULTIPLICITY 890#if EV_MULTIPLICITY
802 struct ev_loop *loop = default_loop; 891 struct ev_loop *loop = default_loop;
803#endif 892#endif
804 893
805 loop_fork (EV_A); 894 if (method)
806 895 postfork = 1;
807 ev_io_stop (EV_A_ &sigev);
808 close (sigpipe [0]);
809 close (sigpipe [1]);
810 pipe (sigpipe);
811
812 ev_ref (EV_A); /* signal watcher */
813 siginit (EV_A);
814} 896}
815 897
816/*****************************************************************************/ 898/*****************************************************************************/
817 899
818static void 900static void
996 { 1078 {
997 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1079 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
998 call_pending (EV_A); 1080 call_pending (EV_A);
999 } 1081 }
1000 1082
1083 /* we might have forked, so reify kernel state if necessary */
1084 if (expect_false (postfork))
1085 loop_fork (EV_A);
1086
1001 /* update fd-related kernel structures */ 1087 /* update fd-related kernel structures */
1002 fd_reify (EV_A); 1088 fd_reify (EV_A);
1003 1089
1004 /* calculate blocking time */ 1090 /* calculate blocking time */
1005 1091
1159 ((WT)w)->at += mn_now; 1245 ((WT)w)->at += mn_now;
1160 1246
1161 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.));
1162 1248
1163 ev_start (EV_A_ (W)w, ++timercnt); 1249 ev_start (EV_A_ (W)w, ++timercnt);
1164 array_needsize (timers, timermax, timercnt, ); 1250 array_needsize (timers, timermax, timercnt, (void));
1165 timers [timercnt - 1] = w; 1251 timers [timercnt - 1] = w;
1166 upheap ((WT *)timers, timercnt - 1); 1252 upheap ((WT *)timers, timercnt - 1);
1167 1253
1168 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1254 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1169} 1255}
1216 /* 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 */
1217 if (w->interval) 1303 if (w->interval)
1218 ((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;
1219 1305
1220 ev_start (EV_A_ (W)w, ++periodiccnt); 1306 ev_start (EV_A_ (W)w, ++periodiccnt);
1221 array_needsize (periodics, periodicmax, periodiccnt, ); 1307 array_needsize (periodics, periodicmax, periodiccnt, (void));
1222 periodics [periodiccnt - 1] = w; 1308 periodics [periodiccnt - 1] = w;
1223 upheap ((WT *)periodics, periodiccnt - 1); 1309 upheap ((WT *)periodics, periodiccnt - 1);
1224 1310
1225 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1311 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1226} 1312}
1248{ 1334{
1249 if (ev_is_active (w)) 1335 if (ev_is_active (w))
1250 return; 1336 return;
1251 1337
1252 ev_start (EV_A_ (W)w, ++idlecnt); 1338 ev_start (EV_A_ (W)w, ++idlecnt);
1253 array_needsize (idles, idlemax, idlecnt, ); 1339 array_needsize (idles, idlemax, idlecnt, (void));
1254 idles [idlecnt - 1] = w; 1340 idles [idlecnt - 1] = w;
1255} 1341}
1256 1342
1257void 1343void
1258ev_idle_stop (EV_P_ struct ev_idle *w) 1344ev_idle_stop (EV_P_ struct ev_idle *w)
1270{ 1356{
1271 if (ev_is_active (w)) 1357 if (ev_is_active (w))
1272 return; 1358 return;
1273 1359
1274 ev_start (EV_A_ (W)w, ++preparecnt); 1360 ev_start (EV_A_ (W)w, ++preparecnt);
1275 array_needsize (prepares, preparemax, preparecnt, ); 1361 array_needsize (prepares, preparemax, preparecnt, (void));
1276 prepares [preparecnt - 1] = w; 1362 prepares [preparecnt - 1] = w;
1277} 1363}
1278 1364
1279void 1365void
1280ev_prepare_stop (EV_P_ struct ev_prepare *w) 1366ev_prepare_stop (EV_P_ struct ev_prepare *w)
1292{ 1378{
1293 if (ev_is_active (w)) 1379 if (ev_is_active (w))
1294 return; 1380 return;
1295 1381
1296 ev_start (EV_A_ (W)w, ++checkcnt); 1382 ev_start (EV_A_ (W)w, ++checkcnt);
1297 array_needsize (checks, checkmax, checkcnt, ); 1383 array_needsize (checks, checkmax, checkcnt, (void));
1298 checks [checkcnt - 1] = w; 1384 checks [checkcnt - 1] = w;
1299} 1385}
1300 1386
1301void 1387void
1302ev_check_stop (EV_P_ struct ev_check *w) 1388ev_check_stop (EV_P_ struct ev_check *w)
1396 void (*cb)(int revents, void *arg) = once->cb; 1482 void (*cb)(int revents, void *arg) = once->cb;
1397 void *arg = once->arg; 1483 void *arg = once->arg;
1398 1484
1399 ev_io_stop (EV_A_ &once->io); 1485 ev_io_stop (EV_A_ &once->io);
1400 ev_timer_stop (EV_A_ &once->to); 1486 ev_timer_stop (EV_A_ &once->to);
1401 free (once); 1487 ev_free (once);
1402 1488
1403 cb (revents, arg); 1489 cb (revents, arg);
1404} 1490}
1405 1491
1406static void 1492static void
1416} 1502}
1417 1503
1418void 1504void
1419ev_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)
1420{ 1506{
1421 struct ev_once *once = malloc (sizeof (struct ev_once)); 1507 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1422 1508
1423 if (!once) 1509 if (!once)
1424 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1510 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1425 else 1511 else
1426 { 1512 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines