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

Comparing libev/ev.c (file contents):
Revision 1.58 by root, Sun Nov 4 16:52:52 2007 UTC vs.
Revision 1.71 by root, Tue Nov 6 13:17:55 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#ifndef EV_EMBED 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33
34# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1
36# define EV_USE_REALTIME 1
37# endif
38
39# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1
41# endif
42
43# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1
45# endif
46
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1
49# endif
50
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1
53# endif
54
33#endif 55#endif
34 56
35#include <math.h> 57#include <math.h>
36#include <stdlib.h> 58#include <stdlib.h>
37#include <unistd.h>
38#include <fcntl.h> 59#include <fcntl.h>
39#include <signal.h>
40#include <stddef.h> 60#include <stddef.h>
41 61
42#include <stdio.h> 62#include <stdio.h>
43 63
44#include <assert.h> 64#include <assert.h>
45#include <errno.h> 65#include <errno.h>
46#include <sys/types.h> 66#include <sys/types.h>
67#include <time.h>
68
69#ifndef PERL
70# include <signal.h>
71#endif
72
47#ifndef WIN32 73#ifndef WIN32
74# include <unistd.h>
75# include <sys/time.h>
48# include <sys/wait.h> 76# include <sys/wait.h>
49#endif 77#endif
50#include <sys/time.h>
51#include <time.h>
52
53/**/ 78/**/
54 79
55#ifndef EV_USE_MONOTONIC 80#ifndef EV_USE_MONOTONIC
56# define EV_USE_MONOTONIC 1 81# define EV_USE_MONOTONIC 1
57#endif 82#endif
58 83
59#ifndef EV_USE_SELECT 84#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 85# define EV_USE_SELECT 1
61#endif 86#endif
62 87
63#ifndef EV_USEV_POLL 88#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 89# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 90#endif
66 91
67#ifndef EV_USE_EPOLL 92#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 93# define EV_USE_EPOLL 0
69#endif 94#endif
70 95
71#ifndef EV_USE_KQUEUE 96#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0 97# define EV_USE_KQUEUE 0
98#endif
99
100#ifndef EV_USE_WIN32
101# ifdef WIN32
102# define EV_USE_WIN32 0 /* it does not exist, use select */
103# undef EV_USE_SELECT
104# define EV_USE_SELECT 1
105# else
106# define EV_USE_WIN32 0
107# endif
73#endif 108#endif
74 109
75#ifndef EV_USE_REALTIME 110#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1 111# define EV_USE_REALTIME 1
77#endif 112#endif
93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 128#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
94#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 129#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 130#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 131/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
97 132
98#ifndef EV_EMBED
99# include "ev.h" 133#include "ev.h"
100#endif
101 134
102#if __GNUC__ >= 3 135#if __GNUC__ >= 3
103# define expect(expr,value) __builtin_expect ((expr),(value)) 136# define expect(expr,value) __builtin_expect ((expr),(value))
104# define inline inline 137# define inline inline
105#else 138#else
117typedef struct ev_watcher_list *WL; 150typedef struct ev_watcher_list *WL;
118typedef struct ev_watcher_time *WT; 151typedef struct ev_watcher_time *WT;
119 152
120static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 153static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
121 154
155#if WIN32
156/* note: the comment below could not be substantiated, but what would I care */
157/* MSDN says this is required to handle SIGFPE */
158volatile double SIGFPE_REQ = 0.0f;
159#endif
160
122/*****************************************************************************/ 161/*****************************************************************************/
123 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
124typedef struct 211typedef struct
125{ 212{
126 struct ev_watcher_list *head; 213 WL head;
127 unsigned char events; 214 unsigned char events;
128 unsigned char reify; 215 unsigned char reify;
129} ANFD; 216} ANFD;
130 217
131typedef struct 218typedef struct
189 return rt_now; 276 return rt_now;
190} 277}
191 278
192#define array_roundsize(base,n) ((n) | 4 & ~3) 279#define array_roundsize(base,n) ((n) | 4 & ~3)
193 280
194#define array_needsize(base,cur,cnt,init) \ 281#define array_needsize(base,cur,cnt,init) \
195 if (expect_false ((cnt) > cur)) \ 282 if (expect_false ((cnt) > cur)) \
196 { \ 283 { \
197 int newcnt = cur; \ 284 int newcnt = cur; \
198 do \ 285 do \
199 { \ 286 { \
200 newcnt = array_roundsize (base, newcnt << 1); \ 287 newcnt = array_roundsize (base, newcnt << 1); \
201 } \ 288 } \
202 while ((cnt) > newcnt); \ 289 while ((cnt) > newcnt); \
203 \ 290 \
204 base = realloc (base, sizeof (*base) * (newcnt)); \ 291 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
205 init (base + cur, newcnt - cur); \ 292 init (base + cur, newcnt - cur); \
206 cur = newcnt; \ 293 cur = newcnt; \
207 } 294 }
295
296#define array_slim(stem) \
297 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
298 { \
299 stem ## max = array_roundsize (stem ## cnt >> 1); \
300 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
301 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
302 }
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
309#define array_free(stem, idx) \
310 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
208 311
209/*****************************************************************************/ 312/*****************************************************************************/
210 313
211static void 314static void
212anfds_init (ANFD *base, int count) 315anfds_init (ANFD *base, int count)
229 pendings [ABSPRI (w)][w->pending - 1].events |= events; 332 pendings [ABSPRI (w)][w->pending - 1].events |= events;
230 return; 333 return;
231 } 334 }
232 335
233 w->pending = ++pendingcnt [ABSPRI (w)]; 336 w->pending = ++pendingcnt [ABSPRI (w)];
234 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 337 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void));
235 pendings [ABSPRI (w)][w->pending - 1].w = w; 338 pendings [ABSPRI (w)][w->pending - 1].w = w;
236 pendings [ABSPRI (w)][w->pending - 1].events = events; 339 pendings [ABSPRI (w)][w->pending - 1].events = events;
237} 340}
238 341
239static void 342static void
278 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 381 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
279 events |= w->events; 382 events |= w->events;
280 383
281 anfd->reify = 0; 384 anfd->reify = 0;
282 385
283 if (anfd->events != events)
284 {
285 method_modify (EV_A_ fd, anfd->events, events); 386 method_modify (EV_A_ fd, anfd->events, events);
286 anfd->events = events; 387 anfd->events = events;
287 }
288 } 388 }
289 389
290 fdchangecnt = 0; 390 fdchangecnt = 0;
291} 391}
292 392
293static void 393static void
294fd_change (EV_P_ int fd) 394fd_change (EV_P_ int fd)
295{ 395{
296 if (anfds [fd].reify || fdchangecnt < 0) 396 if (anfds [fd].reify)
297 return; 397 return;
298 398
299 anfds [fd].reify = 1; 399 anfds [fd].reify = 1;
300 400
301 ++fdchangecnt; 401 ++fdchangecnt;
302 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 402 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void));
303 fdchanges [fdchangecnt - 1] = fd; 403 fdchanges [fdchangecnt - 1] = fd;
304} 404}
305 405
306static void 406static void
307fd_kill (EV_P_ int fd) 407fd_kill (EV_P_ int fd)
313 ev_io_stop (EV_A_ w); 413 ev_io_stop (EV_A_ w);
314 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 414 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
315 } 415 }
316} 416}
317 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
318/* called on EBADF to verify fds */ 428/* called on EBADF to verify fds */
319static void 429static void
320fd_ebadf (EV_P) 430fd_ebadf (EV_P)
321{ 431{
322 int fd; 432 int fd;
323 433
324 for (fd = 0; fd < anfdmax; ++fd) 434 for (fd = 0; fd < anfdmax; ++fd)
325 if (anfds [fd].events) 435 if (anfds [fd].events)
326 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 436 if (!fd_valid (fd) == -1 && errno == EBADF)
327 fd_kill (EV_A_ fd); 437 fd_kill (EV_A_ fd);
328} 438}
329 439
330/* 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 */
331static void 441static void
332fd_enomem (EV_P) 442fd_enomem (EV_P)
333{ 443{
334 int fd = anfdmax; 444 int fd;
335 445
336 while (fd--) 446 for (fd = anfdmax; fd--; )
337 if (anfds [fd].events) 447 if (anfds [fd].events)
338 { 448 {
339 close (fd);
340 fd_kill (EV_A_ fd); 449 fd_kill (EV_A_ fd);
341 return; 450 return;
342 } 451 }
343} 452}
344 453
345/* 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 */
346static void 455static void
347fd_rearm_all (EV_P) 456fd_rearm_all (EV_P)
348{ 457{
349 int fd; 458 int fd;
350 459
351 /* this should be highly optimised to not do anything but set a flag */ 460 /* this should be highly optimised to not do anything but set a flag */
352 for (fd = 0; fd < anfdmax; ++fd) 461 for (fd = 0; fd < anfdmax; ++fd)
353 if (anfds [fd].events) 462 if (anfds [fd].events)
354 { 463 {
355 anfds [fd].events = 0; 464 anfds [fd].events = 0;
356 fd_change (fd); 465 fd_change (EV_A_ fd);
357 } 466 }
358} 467}
359 468
360/*****************************************************************************/ 469/*****************************************************************************/
361 470
365 WT w = heap [k]; 474 WT w = heap [k];
366 475
367 while (k && heap [k >> 1]->at > w->at) 476 while (k && heap [k >> 1]->at > w->at)
368 { 477 {
369 heap [k] = heap [k >> 1]; 478 heap [k] = heap [k >> 1];
370 heap [k]->active = k + 1; 479 ((W)heap [k])->active = k + 1;
371 k >>= 1; 480 k >>= 1;
372 } 481 }
373 482
374 heap [k] = w; 483 heap [k] = w;
375 heap [k]->active = k + 1; 484 ((W)heap [k])->active = k + 1;
376 485
377} 486}
378 487
379static void 488static void
380downheap (WT *heap, int N, int k) 489downheap (WT *heap, int N, int k)
390 499
391 if (w->at <= heap [j]->at) 500 if (w->at <= heap [j]->at)
392 break; 501 break;
393 502
394 heap [k] = heap [j]; 503 heap [k] = heap [j];
395 heap [k]->active = k + 1; 504 ((W)heap [k])->active = k + 1;
396 k = j; 505 k = j;
397 } 506 }
398 507
399 heap [k] = w; 508 heap [k] = w;
400 heap [k]->active = k + 1; 509 ((W)heap [k])->active = k + 1;
401} 510}
402 511
403/*****************************************************************************/ 512/*****************************************************************************/
404 513
405typedef struct 514typedef struct
406{ 515{
407 struct ev_watcher_list *head; 516 WL head;
408 sig_atomic_t volatile gotsig; 517 sig_atomic_t volatile gotsig;
409} ANSIG; 518} ANSIG;
410 519
411static ANSIG *signals; 520static ANSIG *signals;
412static int signalmax; 521static int signalmax;
413 522
414static int sigpipe [2]; 523static int sigpipe [2];
415static sig_atomic_t volatile gotsig; 524static sig_atomic_t volatile gotsig;
525static struct ev_io sigev;
416 526
417static void 527static void
418signals_init (ANSIG *base, int count) 528signals_init (ANSIG *base, int count)
419{ 529{
420 while (count--) 530 while (count--)
427} 537}
428 538
429static void 539static void
430sighandler (int signum) 540sighandler (int signum)
431{ 541{
542#if WIN32
543 signal (signum, sighandler);
544#endif
545
432 signals [signum - 1].gotsig = 1; 546 signals [signum - 1].gotsig = 1;
433 547
434 if (!gotsig) 548 if (!gotsig)
435 { 549 {
436 int old_errno = errno; 550 int old_errno = errno;
441} 555}
442 556
443static void 557static void
444sigcb (EV_P_ struct ev_io *iow, int revents) 558sigcb (EV_P_ struct ev_io *iow, int revents)
445{ 559{
446 struct ev_watcher_list *w; 560 WL w;
447 int signum; 561 int signum;
448 562
449 read (sigpipe [0], &revents, 1); 563 read (sigpipe [0], &revents, 1);
450 gotsig = 0; 564 gotsig = 0;
451 565
476 ev_unref (EV_A); /* child watcher should not keep loop alive */ 590 ev_unref (EV_A); /* child watcher should not keep loop alive */
477} 591}
478 592
479/*****************************************************************************/ 593/*****************************************************************************/
480 594
595static struct ev_child *childs [PID_HASHSIZE];
596
481#ifndef WIN32 597#ifndef WIN32
598
599static struct ev_signal childev;
482 600
483#ifndef WCONTINUED 601#ifndef WCONTINUED
484# define WCONTINUED 0 602# define WCONTINUED 0
485#endif 603#endif
486 604
490 struct ev_child *w; 608 struct ev_child *w;
491 609
492 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 610 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
493 if (w->pid == pid || !w->pid) 611 if (w->pid == pid || !w->pid)
494 { 612 {
495 w->priority = sw->priority; /* need to do it *now* */ 613 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
496 w->rpid = pid; 614 w->rpid = pid;
497 w->rstatus = status; 615 w->rstatus = status;
498 event (EV_A_ (W)w, EV_CHILD); 616 event (EV_A_ (W)w, EV_CHILD);
499 } 617 }
500} 618}
501 619
502static void 620static void
522# include "ev_kqueue.c" 640# include "ev_kqueue.c"
523#endif 641#endif
524#if EV_USE_EPOLL 642#if EV_USE_EPOLL
525# include "ev_epoll.c" 643# include "ev_epoll.c"
526#endif 644#endif
527#if EV_USEV_POLL 645#if EV_USE_POLL
528# include "ev_poll.c" 646# include "ev_poll.c"
529#endif 647#endif
530#if EV_USE_SELECT 648#if EV_USE_SELECT
531# include "ev_select.c" 649# include "ev_select.c"
532#endif 650#endif
584 methods = atoi (getenv ("LIBEV_METHODS")); 702 methods = atoi (getenv ("LIBEV_METHODS"));
585 else 703 else
586 methods = EVMETHOD_ANY; 704 methods = EVMETHOD_ANY;
587 705
588 method = 0; 706 method = 0;
707#if EV_USE_WIN32
708 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
709#endif
589#if EV_USE_KQUEUE 710#if EV_USE_KQUEUE
590 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 711 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
591#endif 712#endif
592#if EV_USE_EPOLL 713#if EV_USE_EPOLL
593 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 714 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
594#endif 715#endif
595#if EV_USEV_POLL 716#if EV_USE_POLL
596 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 717 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
597#endif 718#endif
598#if EV_USE_SELECT 719#if EV_USE_SELECT
599 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 720 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
600#endif 721#endif
722
723 ev_watcher_init (&sigev, sigcb);
724 ev_set_priority (&sigev, EV_MAXPRI);
601 } 725 }
602} 726}
603 727
604void 728void
605loop_destroy (EV_P) 729loop_destroy (EV_P)
606{ 730{
731 int i;
732
733#if EV_USE_WIN32
734 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
735#endif
607#if EV_USE_KQUEUE 736#if EV_USE_KQUEUE
608 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 737 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
609#endif 738#endif
610#if EV_USE_EPOLL 739#if EV_USE_EPOLL
611 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 740 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
612#endif 741#endif
613#if EV_USEV_POLL 742#if EV_USE_POLL
614 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 743 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
615#endif 744#endif
616#if EV_USE_SELECT 745#if EV_USE_SELECT
617 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 746 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
618#endif 747#endif
619 748
749 for (i = NUMPRI; i--; )
750 array_free (pending, [i]);
751
752 /* have to use the microsoft-never-gets-it-right macro */
753 array_free_microshit (fdchange);
754 array_free_microshit (timer);
755 array_free_microshit (periodic);
756 array_free_microshit (idle);
757 array_free_microshit (prepare);
758 array_free_microshit (check);
759
620 method = 0; 760 method = 0;
621 /*TODO*/
622} 761}
623 762
624void 763static void
625loop_fork (EV_P) 764loop_fork (EV_P)
626{ 765{
627 /*TODO*/
628#if EV_USE_EPOLL 766#if EV_USE_EPOLL
629 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 767 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
630#endif 768#endif
631#if EV_USE_KQUEUE 769#if EV_USE_KQUEUE
632 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 770 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
633#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;
634} 789}
635 790
636#if EV_MULTIPLICITY 791#if EV_MULTIPLICITY
637struct ev_loop * 792struct ev_loop *
638ev_loop_new (int methods) 793ev_loop_new (int methods)
639{ 794{
640 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));
641 798
642 loop_init (EV_A_ methods); 799 loop_init (EV_A_ methods);
643 800
644 if (ev_methods (EV_A)) 801 if (ev_method (EV_A))
645 return loop; 802 return loop;
646 803
647 return 0; 804 return 0;
648} 805}
649 806
650void 807void
651ev_loop_destroy (EV_P) 808ev_loop_destroy (EV_P)
652{ 809{
653 loop_destroy (EV_A); 810 loop_destroy (EV_A);
654 free (loop); 811 ev_free (loop);
655} 812}
656 813
657void 814void
658ev_loop_fork (EV_P) 815ev_loop_fork (EV_P)
659{ 816{
660 loop_fork (EV_A); 817 postfork = 1;
661} 818}
662 819
663#endif 820#endif
664 821
665#if EV_MULTIPLICITY 822#if EV_MULTIPLICITY
688 845
689 loop_init (EV_A_ methods); 846 loop_init (EV_A_ methods);
690 847
691 if (ev_method (EV_A)) 848 if (ev_method (EV_A))
692 { 849 {
693 ev_watcher_init (&sigev, sigcb);
694 ev_set_priority (&sigev, EV_MAXPRI);
695 siginit (EV_A); 850 siginit (EV_A);
696 851
697#ifndef WIN32 852#ifndef WIN32
698 ev_signal_init (&childev, childcb, SIGCHLD); 853 ev_signal_init (&childev, childcb, SIGCHLD);
699 ev_set_priority (&childev, EV_MAXPRI); 854 ev_set_priority (&childev, EV_MAXPRI);
713{ 868{
714#if EV_MULTIPLICITY 869#if EV_MULTIPLICITY
715 struct ev_loop *loop = default_loop; 870 struct ev_loop *loop = default_loop;
716#endif 871#endif
717 872
873#ifndef WIN32
718 ev_ref (EV_A); /* child watcher */ 874 ev_ref (EV_A); /* child watcher */
719 ev_signal_stop (EV_A_ &childev); 875 ev_signal_stop (EV_A_ &childev);
876#endif
720 877
721 ev_ref (EV_A); /* signal watcher */ 878 ev_ref (EV_A); /* signal watcher */
722 ev_io_stop (EV_A_ &sigev); 879 ev_io_stop (EV_A_ &sigev);
723 880
724 close (sigpipe [0]); sigpipe [0] = 0; 881 close (sigpipe [0]); sigpipe [0] = 0;
726 883
727 loop_destroy (EV_A); 884 loop_destroy (EV_A);
728} 885}
729 886
730void 887void
731ev_default_fork (EV_P) 888ev_default_fork (void)
732{ 889{
733 loop_fork (EV_A); 890#if EV_MULTIPLICITY
891 struct ev_loop *loop = default_loop;
892#endif
734 893
735 ev_io_stop (EV_A_ &sigev); 894 if (method)
736 close (sigpipe [0]); 895 postfork = 1;
737 close (sigpipe [1]);
738 pipe (sigpipe);
739
740 ev_ref (EV_A); /* signal watcher */
741 siginit (EV_A);
742} 896}
743 897
744/*****************************************************************************/ 898/*****************************************************************************/
745 899
746static void 900static void
762} 916}
763 917
764static void 918static void
765timers_reify (EV_P) 919timers_reify (EV_P)
766{ 920{
767 while (timercnt && timers [0]->at <= mn_now) 921 while (timercnt && ((WT)timers [0])->at <= mn_now)
768 { 922 {
769 struct ev_timer *w = timers [0]; 923 struct ev_timer *w = timers [0];
924
925 assert (("inactive timer on timer heap detected", ev_is_active (w)));
770 926
771 /* first reschedule or stop timer */ 927 /* first reschedule or stop timer */
772 if (w->repeat) 928 if (w->repeat)
773 { 929 {
774 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 930 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
775 w->at = mn_now + w->repeat; 931 ((WT)w)->at = mn_now + w->repeat;
776 downheap ((WT *)timers, timercnt, 0); 932 downheap ((WT *)timers, timercnt, 0);
777 } 933 }
778 else 934 else
779 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 935 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
780 936
783} 939}
784 940
785static void 941static void
786periodics_reify (EV_P) 942periodics_reify (EV_P)
787{ 943{
788 while (periodiccnt && periodics [0]->at <= rt_now) 944 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
789 { 945 {
790 struct ev_periodic *w = periodics [0]; 946 struct ev_periodic *w = periodics [0];
947
948 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
791 949
792 /* first reschedule or stop timer */ 950 /* first reschedule or stop timer */
793 if (w->interval) 951 if (w->interval)
794 { 952 {
795 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 953 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
796 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 954 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
797 downheap ((WT *)periodics, periodiccnt, 0); 955 downheap ((WT *)periodics, periodiccnt, 0);
798 } 956 }
799 else 957 else
800 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 958 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
801 959
813 { 971 {
814 struct ev_periodic *w = periodics [i]; 972 struct ev_periodic *w = periodics [i];
815 973
816 if (w->interval) 974 if (w->interval)
817 { 975 {
818 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 976 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
819 977
820 if (fabs (diff) >= 1e-4) 978 if (fabs (diff) >= 1e-4)
821 { 979 {
822 ev_periodic_stop (EV_A_ w); 980 ev_periodic_stop (EV_A_ w);
823 ev_periodic_start (EV_A_ w); 981 ev_periodic_start (EV_A_ w);
884 { 1042 {
885 periodics_reschedule (EV_A); 1043 periodics_reschedule (EV_A);
886 1044
887 /* adjust timers. this is easy, as the offset is the same for all */ 1045 /* adjust timers. this is easy, as the offset is the same for all */
888 for (i = 0; i < timercnt; ++i) 1046 for (i = 0; i < timercnt; ++i)
889 timers [i]->at += rt_now - mn_now; 1047 ((WT)timers [i])->at += rt_now - mn_now;
890 } 1048 }
891 1049
892 mn_now = rt_now; 1050 mn_now = rt_now;
893 } 1051 }
894} 1052}
920 { 1078 {
921 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1079 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
922 call_pending (EV_A); 1080 call_pending (EV_A);
923 } 1081 }
924 1082
1083 /* we might have forked, so reify kernel state if necessary */
1084 if (expect_false (postfork))
1085 loop_fork (EV_A);
1086
925 /* update fd-related kernel structures */ 1087 /* update fd-related kernel structures */
926 fd_reify (EV_A); 1088 fd_reify (EV_A);
927 1089
928 /* calculate blocking time */ 1090 /* calculate blocking time */
929 1091
945 { 1107 {
946 block = MAX_BLOCKTIME; 1108 block = MAX_BLOCKTIME;
947 1109
948 if (timercnt) 1110 if (timercnt)
949 { 1111 {
950 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1112 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
951 if (block > to) block = to; 1113 if (block > to) block = to;
952 } 1114 }
953 1115
954 if (periodiccnt) 1116 if (periodiccnt)
955 { 1117 {
956 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1118 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
957 if (block > to) block = to; 1119 if (block > to) block = to;
958 } 1120 }
959 1121
960 if (block < 0.) block = 0.; 1122 if (block < 0.) block = 0.;
961 } 1123 }
1078ev_timer_start (EV_P_ struct ev_timer *w) 1240ev_timer_start (EV_P_ struct ev_timer *w)
1079{ 1241{
1080 if (ev_is_active (w)) 1242 if (ev_is_active (w))
1081 return; 1243 return;
1082 1244
1083 w->at += mn_now; 1245 ((WT)w)->at += mn_now;
1084 1246
1085 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.));
1086 1248
1087 ev_start (EV_A_ (W)w, ++timercnt); 1249 ev_start (EV_A_ (W)w, ++timercnt);
1088 array_needsize (timers, timermax, timercnt, ); 1250 array_needsize (timers, timermax, timercnt, (void));
1089 timers [timercnt - 1] = w; 1251 timers [timercnt - 1] = w;
1090 upheap ((WT *)timers, timercnt - 1); 1252 upheap ((WT *)timers, timercnt - 1);
1253
1254 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1091} 1255}
1092 1256
1093void 1257void
1094ev_timer_stop (EV_P_ struct ev_timer *w) 1258ev_timer_stop (EV_P_ struct ev_timer *w)
1095{ 1259{
1096 ev_clear_pending (EV_A_ (W)w); 1260 ev_clear_pending (EV_A_ (W)w);
1097 if (!ev_is_active (w)) 1261 if (!ev_is_active (w))
1098 return; 1262 return;
1099 1263
1264 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1265
1100 if (w->active < timercnt--) 1266 if (((W)w)->active < timercnt--)
1101 { 1267 {
1102 timers [w->active - 1] = timers [timercnt]; 1268 timers [((W)w)->active - 1] = timers [timercnt];
1103 downheap ((WT *)timers, timercnt, w->active - 1); 1269 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1104 } 1270 }
1105 1271
1106 w->at = w->repeat; 1272 ((WT)w)->at = w->repeat;
1107 1273
1108 ev_stop (EV_A_ (W)w); 1274 ev_stop (EV_A_ (W)w);
1109} 1275}
1110 1276
1111void 1277void
1113{ 1279{
1114 if (ev_is_active (w)) 1280 if (ev_is_active (w))
1115 { 1281 {
1116 if (w->repeat) 1282 if (w->repeat)
1117 { 1283 {
1118 w->at = mn_now + w->repeat; 1284 ((WT)w)->at = mn_now + w->repeat;
1119 downheap ((WT *)timers, timercnt, w->active - 1); 1285 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1120 } 1286 }
1121 else 1287 else
1122 ev_timer_stop (EV_A_ w); 1288 ev_timer_stop (EV_A_ w);
1123 } 1289 }
1124 else if (w->repeat) 1290 else if (w->repeat)
1133 1299
1134 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1300 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1135 1301
1136 /* 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 */
1137 if (w->interval) 1303 if (w->interval)
1138 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1304 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1139 1305
1140 ev_start (EV_A_ (W)w, ++periodiccnt); 1306 ev_start (EV_A_ (W)w, ++periodiccnt);
1141 array_needsize (periodics, periodicmax, periodiccnt, ); 1307 array_needsize (periodics, periodicmax, periodiccnt, (void));
1142 periodics [periodiccnt - 1] = w; 1308 periodics [periodiccnt - 1] = w;
1143 upheap ((WT *)periodics, periodiccnt - 1); 1309 upheap ((WT *)periodics, periodiccnt - 1);
1310
1311 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1144} 1312}
1145 1313
1146void 1314void
1147ev_periodic_stop (EV_P_ struct ev_periodic *w) 1315ev_periodic_stop (EV_P_ struct ev_periodic *w)
1148{ 1316{
1149 ev_clear_pending (EV_A_ (W)w); 1317 ev_clear_pending (EV_A_ (W)w);
1150 if (!ev_is_active (w)) 1318 if (!ev_is_active (w))
1151 return; 1319 return;
1152 1320
1321 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1322
1153 if (w->active < periodiccnt--) 1323 if (((W)w)->active < periodiccnt--)
1154 { 1324 {
1155 periodics [w->active - 1] = periodics [periodiccnt]; 1325 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1156 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1326 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1157 } 1327 }
1158 1328
1159 ev_stop (EV_A_ (W)w); 1329 ev_stop (EV_A_ (W)w);
1160} 1330}
1161 1331
1164{ 1334{
1165 if (ev_is_active (w)) 1335 if (ev_is_active (w))
1166 return; 1336 return;
1167 1337
1168 ev_start (EV_A_ (W)w, ++idlecnt); 1338 ev_start (EV_A_ (W)w, ++idlecnt);
1169 array_needsize (idles, idlemax, idlecnt, ); 1339 array_needsize (idles, idlemax, idlecnt, (void));
1170 idles [idlecnt - 1] = w; 1340 idles [idlecnt - 1] = w;
1171} 1341}
1172 1342
1173void 1343void
1174ev_idle_stop (EV_P_ struct ev_idle *w) 1344ev_idle_stop (EV_P_ struct ev_idle *w)
1175{ 1345{
1176 ev_clear_pending (EV_A_ (W)w); 1346 ev_clear_pending (EV_A_ (W)w);
1177 if (ev_is_active (w)) 1347 if (ev_is_active (w))
1178 return; 1348 return;
1179 1349
1180 idles [w->active - 1] = idles [--idlecnt]; 1350 idles [((W)w)->active - 1] = idles [--idlecnt];
1181 ev_stop (EV_A_ (W)w); 1351 ev_stop (EV_A_ (W)w);
1182} 1352}
1183 1353
1184void 1354void
1185ev_prepare_start (EV_P_ struct ev_prepare *w) 1355ev_prepare_start (EV_P_ struct ev_prepare *w)
1186{ 1356{
1187 if (ev_is_active (w)) 1357 if (ev_is_active (w))
1188 return; 1358 return;
1189 1359
1190 ev_start (EV_A_ (W)w, ++preparecnt); 1360 ev_start (EV_A_ (W)w, ++preparecnt);
1191 array_needsize (prepares, preparemax, preparecnt, ); 1361 array_needsize (prepares, preparemax, preparecnt, (void));
1192 prepares [preparecnt - 1] = w; 1362 prepares [preparecnt - 1] = w;
1193} 1363}
1194 1364
1195void 1365void
1196ev_prepare_stop (EV_P_ struct ev_prepare *w) 1366ev_prepare_stop (EV_P_ struct ev_prepare *w)
1197{ 1367{
1198 ev_clear_pending (EV_A_ (W)w); 1368 ev_clear_pending (EV_A_ (W)w);
1199 if (ev_is_active (w)) 1369 if (ev_is_active (w))
1200 return; 1370 return;
1201 1371
1202 prepares [w->active - 1] = prepares [--preparecnt]; 1372 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1203 ev_stop (EV_A_ (W)w); 1373 ev_stop (EV_A_ (W)w);
1204} 1374}
1205 1375
1206void 1376void
1207ev_check_start (EV_P_ struct ev_check *w) 1377ev_check_start (EV_P_ struct ev_check *w)
1208{ 1378{
1209 if (ev_is_active (w)) 1379 if (ev_is_active (w))
1210 return; 1380 return;
1211 1381
1212 ev_start (EV_A_ (W)w, ++checkcnt); 1382 ev_start (EV_A_ (W)w, ++checkcnt);
1213 array_needsize (checks, checkmax, checkcnt, ); 1383 array_needsize (checks, checkmax, checkcnt, (void));
1214 checks [checkcnt - 1] = w; 1384 checks [checkcnt - 1] = w;
1215} 1385}
1216 1386
1217void 1387void
1218ev_check_stop (EV_P_ struct ev_check *w) 1388ev_check_stop (EV_P_ struct ev_check *w)
1219{ 1389{
1220 ev_clear_pending (EV_A_ (W)w); 1390 ev_clear_pending (EV_A_ (W)w);
1221 if (ev_is_active (w)) 1391 if (ev_is_active (w))
1222 return; 1392 return;
1223 1393
1224 checks [w->active - 1] = checks [--checkcnt]; 1394 checks [((W)w)->active - 1] = checks [--checkcnt];
1225 ev_stop (EV_A_ (W)w); 1395 ev_stop (EV_A_ (W)w);
1226} 1396}
1227 1397
1228#ifndef SA_RESTART 1398#ifndef SA_RESTART
1229# define SA_RESTART 0 1399# define SA_RESTART 0
1242 1412
1243 ev_start (EV_A_ (W)w, 1); 1413 ev_start (EV_A_ (W)w, 1);
1244 array_needsize (signals, signalmax, w->signum, signals_init); 1414 array_needsize (signals, signalmax, w->signum, signals_init);
1245 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1415 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1246 1416
1247 if (!w->next) 1417 if (!((WL)w)->next)
1248 { 1418 {
1419#if WIN32
1420 signal (w->signum, sighandler);
1421#else
1249 struct sigaction sa; 1422 struct sigaction sa;
1250 sa.sa_handler = sighandler; 1423 sa.sa_handler = sighandler;
1251 sigfillset (&sa.sa_mask); 1424 sigfillset (&sa.sa_mask);
1252 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1425 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1253 sigaction (w->signum, &sa, 0); 1426 sigaction (w->signum, &sa, 0);
1427#endif
1254 } 1428 }
1255} 1429}
1256 1430
1257void 1431void
1258ev_signal_stop (EV_P_ struct ev_signal *w) 1432ev_signal_stop (EV_P_ struct ev_signal *w)
1308 void (*cb)(int revents, void *arg) = once->cb; 1482 void (*cb)(int revents, void *arg) = once->cb;
1309 void *arg = once->arg; 1483 void *arg = once->arg;
1310 1484
1311 ev_io_stop (EV_A_ &once->io); 1485 ev_io_stop (EV_A_ &once->io);
1312 ev_timer_stop (EV_A_ &once->to); 1486 ev_timer_stop (EV_A_ &once->to);
1313 free (once); 1487 ev_free (once);
1314 1488
1315 cb (revents, arg); 1489 cb (revents, arg);
1316} 1490}
1317 1491
1318static void 1492static void
1328} 1502}
1329 1503
1330void 1504void
1331ev_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)
1332{ 1506{
1333 struct ev_once *once = malloc (sizeof (struct ev_once)); 1507 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1334 1508
1335 if (!once) 1509 if (!once)
1336 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1510 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1337 else 1511 else
1338 { 1512 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines