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

Comparing libev/ev.c (file contents):
Revision 1.61 by root, Sun Nov 4 19:45:09 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
90# define EV_USE_EPOLL 0 93# define EV_USE_EPOLL 0
91#endif 94#endif
92 95
93#ifndef EV_USE_KQUEUE 96#ifndef EV_USE_KQUEUE
94# 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
95#endif 108#endif
96 109
97#ifndef EV_USE_REALTIME 110#ifndef EV_USE_REALTIME
98# define EV_USE_REALTIME 1 111# define EV_USE_REALTIME 1
99#endif 112#endif
137typedef struct ev_watcher_list *WL; 150typedef struct ev_watcher_list *WL;
138typedef struct ev_watcher_time *WT; 151typedef struct ev_watcher_time *WT;
139 152
140static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 153static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
141 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
142/*****************************************************************************/ 161/*****************************************************************************/
143 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
144typedef struct 211typedef struct
145{ 212{
146 struct ev_watcher_list *head; 213 WL head;
147 unsigned char events; 214 unsigned char events;
148 unsigned char reify; 215 unsigned char reify;
149} ANFD; 216} ANFD;
150 217
151typedef struct 218typedef struct
209 return rt_now; 276 return rt_now;
210} 277}
211 278
212#define array_roundsize(base,n) ((n) | 4 & ~3) 279#define array_roundsize(base,n) ((n) | 4 & ~3)
213 280
214#define array_needsize(base,cur,cnt,init) \ 281#define array_needsize(base,cur,cnt,init) \
215 if (expect_false ((cnt) > cur)) \ 282 if (expect_false ((cnt) > cur)) \
216 { \ 283 { \
217 int newcnt = cur; \ 284 int newcnt = cur; \
218 do \ 285 do \
219 { \ 286 { \
220 newcnt = array_roundsize (base, newcnt << 1); \ 287 newcnt = array_roundsize (base, newcnt << 1); \
221 } \ 288 } \
222 while ((cnt) > newcnt); \ 289 while ((cnt) > newcnt); \
223 \ 290 \
224 base = realloc (base, sizeof (*base) * (newcnt)); \ 291 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
225 init (base + cur, newcnt - cur); \ 292 init (base + cur, newcnt - cur); \
226 cur = newcnt; \ 293 cur = newcnt; \
227 } 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;
228 311
229/*****************************************************************************/ 312/*****************************************************************************/
230 313
231static void 314static void
232anfds_init (ANFD *base, int count) 315anfds_init (ANFD *base, int count)
249 pendings [ABSPRI (w)][w->pending - 1].events |= events; 332 pendings [ABSPRI (w)][w->pending - 1].events |= events;
250 return; 333 return;
251 } 334 }
252 335
253 w->pending = ++pendingcnt [ABSPRI (w)]; 336 w->pending = ++pendingcnt [ABSPRI (w)];
254 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 337 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void));
255 pendings [ABSPRI (w)][w->pending - 1].w = w; 338 pendings [ABSPRI (w)][w->pending - 1].w = w;
256 pendings [ABSPRI (w)][w->pending - 1].events = events; 339 pendings [ABSPRI (w)][w->pending - 1].events = events;
257} 340}
258 341
259static void 342static void
298 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)
299 events |= w->events; 382 events |= w->events;
300 383
301 anfd->reify = 0; 384 anfd->reify = 0;
302 385
303 if (anfd->events != events)
304 {
305 method_modify (EV_A_ fd, anfd->events, events); 386 method_modify (EV_A_ fd, anfd->events, events);
306 anfd->events = events; 387 anfd->events = events;
307 }
308 } 388 }
309 389
310 fdchangecnt = 0; 390 fdchangecnt = 0;
311} 391}
312 392
313static void 393static void
314fd_change (EV_P_ int fd) 394fd_change (EV_P_ int fd)
315{ 395{
316 if (anfds [fd].reify || fdchangecnt < 0) 396 if (anfds [fd].reify)
317 return; 397 return;
318 398
319 anfds [fd].reify = 1; 399 anfds [fd].reify = 1;
320 400
321 ++fdchangecnt; 401 ++fdchangecnt;
322 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 402 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void));
323 fdchanges [fdchangecnt - 1] = fd; 403 fdchanges [fdchangecnt - 1] = fd;
324} 404}
325 405
326static void 406static void
327fd_kill (EV_P_ int fd) 407fd_kill (EV_P_ int fd)
333 ev_io_stop (EV_A_ w); 413 ev_io_stop (EV_A_ w);
334 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 414 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
335 } 415 }
336} 416}
337 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
338/* called on EBADF to verify fds */ 428/* called on EBADF to verify fds */
339static void 429static void
340fd_ebadf (EV_P) 430fd_ebadf (EV_P)
341{ 431{
342 int fd; 432 int fd;
343 433
344 for (fd = 0; fd < anfdmax; ++fd) 434 for (fd = 0; fd < anfdmax; ++fd)
345 if (anfds [fd].events) 435 if (anfds [fd].events)
346 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 436 if (!fd_valid (fd) == -1 && errno == EBADF)
347 fd_kill (EV_A_ fd); 437 fd_kill (EV_A_ fd);
348} 438}
349 439
350/* 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 */
351static void 441static void
352fd_enomem (EV_P) 442fd_enomem (EV_P)
353{ 443{
354 int fd = anfdmax; 444 int fd;
355 445
356 while (fd--) 446 for (fd = anfdmax; fd--; )
357 if (anfds [fd].events) 447 if (anfds [fd].events)
358 { 448 {
359 close (fd);
360 fd_kill (EV_A_ fd); 449 fd_kill (EV_A_ fd);
361 return; 450 return;
362 } 451 }
363} 452}
364 453
365/* 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 */
366static void 455static void
367fd_rearm_all (EV_P) 456fd_rearm_all (EV_P)
368{ 457{
369 int fd; 458 int fd;
370 459
385 WT w = heap [k]; 474 WT w = heap [k];
386 475
387 while (k && heap [k >> 1]->at > w->at) 476 while (k && heap [k >> 1]->at > w->at)
388 { 477 {
389 heap [k] = heap [k >> 1]; 478 heap [k] = heap [k >> 1];
390 heap [k]->active = k + 1; 479 ((W)heap [k])->active = k + 1;
391 k >>= 1; 480 k >>= 1;
392 } 481 }
393 482
394 heap [k] = w; 483 heap [k] = w;
395 heap [k]->active = k + 1; 484 ((W)heap [k])->active = k + 1;
396 485
397} 486}
398 487
399static void 488static void
400downheap (WT *heap, int N, int k) 489downheap (WT *heap, int N, int k)
410 499
411 if (w->at <= heap [j]->at) 500 if (w->at <= heap [j]->at)
412 break; 501 break;
413 502
414 heap [k] = heap [j]; 503 heap [k] = heap [j];
415 heap [k]->active = k + 1; 504 ((W)heap [k])->active = k + 1;
416 k = j; 505 k = j;
417 } 506 }
418 507
419 heap [k] = w; 508 heap [k] = w;
420 heap [k]->active = k + 1; 509 ((W)heap [k])->active = k + 1;
421} 510}
422 511
423/*****************************************************************************/ 512/*****************************************************************************/
424 513
425typedef struct 514typedef struct
426{ 515{
427 struct ev_watcher_list *head; 516 WL head;
428 sig_atomic_t volatile gotsig; 517 sig_atomic_t volatile gotsig;
429} ANSIG; 518} ANSIG;
430 519
431static ANSIG *signals; 520static ANSIG *signals;
432static int signalmax; 521static int signalmax;
448} 537}
449 538
450static void 539static void
451sighandler (int signum) 540sighandler (int signum)
452{ 541{
542#if WIN32
543 signal (signum, sighandler);
544#endif
545
453 signals [signum - 1].gotsig = 1; 546 signals [signum - 1].gotsig = 1;
454 547
455 if (!gotsig) 548 if (!gotsig)
456 { 549 {
457 int old_errno = errno; 550 int old_errno = errno;
462} 555}
463 556
464static void 557static void
465sigcb (EV_P_ struct ev_io *iow, int revents) 558sigcb (EV_P_ struct ev_io *iow, int revents)
466{ 559{
467 struct ev_watcher_list *w; 560 WL w;
468 int signum; 561 int signum;
469 562
470 read (sigpipe [0], &revents, 1); 563 read (sigpipe [0], &revents, 1);
471 gotsig = 0; 564 gotsig = 0;
472 565
497 ev_unref (EV_A); /* child watcher should not keep loop alive */ 590 ev_unref (EV_A); /* child watcher should not keep loop alive */
498} 591}
499 592
500/*****************************************************************************/ 593/*****************************************************************************/
501 594
595static struct ev_child *childs [PID_HASHSIZE];
596
502#ifndef WIN32 597#ifndef WIN32
503 598
504static struct ev_child *childs [PID_HASHSIZE];
505static struct ev_signal childev; 599static struct ev_signal childev;
506 600
507#ifndef WCONTINUED 601#ifndef WCONTINUED
508# define WCONTINUED 0 602# define WCONTINUED 0
509#endif 603#endif
514 struct ev_child *w; 608 struct ev_child *w;
515 609
516 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)
517 if (w->pid == pid || !w->pid) 611 if (w->pid == pid || !w->pid)
518 { 612 {
519 w->priority = sw->priority; /* need to do it *now* */ 613 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
520 w->rpid = pid; 614 w->rpid = pid;
521 w->rstatus = status; 615 w->rstatus = status;
522 event (EV_A_ (W)w, EV_CHILD); 616 event (EV_A_ (W)w, EV_CHILD);
523 } 617 }
524} 618}
525 619
526static void 620static void
608 methods = atoi (getenv ("LIBEV_METHODS")); 702 methods = atoi (getenv ("LIBEV_METHODS"));
609 else 703 else
610 methods = EVMETHOD_ANY; 704 methods = EVMETHOD_ANY;
611 705
612 method = 0; 706 method = 0;
707#if EV_USE_WIN32
708 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
709#endif
613#if EV_USE_KQUEUE 710#if EV_USE_KQUEUE
614 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 711 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
615#endif 712#endif
616#if EV_USE_EPOLL 713#if EV_USE_EPOLL
617 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 714 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
620 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 717 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
621#endif 718#endif
622#if EV_USE_SELECT 719#if EV_USE_SELECT
623 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 720 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
624#endif 721#endif
722
723 ev_watcher_init (&sigev, sigcb);
724 ev_set_priority (&sigev, EV_MAXPRI);
625 } 725 }
626} 726}
627 727
628void 728void
629loop_destroy (EV_P) 729loop_destroy (EV_P)
630{ 730{
731 int i;
732
733#if EV_USE_WIN32
734 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
735#endif
631#if EV_USE_KQUEUE 736#if EV_USE_KQUEUE
632 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 737 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
633#endif 738#endif
634#if EV_USE_EPOLL 739#if EV_USE_EPOLL
635 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 740 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
639#endif 744#endif
640#if EV_USE_SELECT 745#if EV_USE_SELECT
641 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 746 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
642#endif 747#endif
643 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
644 method = 0; 760 method = 0;
645 /*TODO*/
646} 761}
647 762
648void 763static void
649loop_fork (EV_P) 764loop_fork (EV_P)
650{ 765{
651 /*TODO*/
652#if EV_USE_EPOLL 766#if EV_USE_EPOLL
653 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 767 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
654#endif 768#endif
655#if EV_USE_KQUEUE 769#if EV_USE_KQUEUE
656 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 770 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
657#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;
658} 789}
659 790
660#if EV_MULTIPLICITY 791#if EV_MULTIPLICITY
661struct ev_loop * 792struct ev_loop *
662ev_loop_new (int methods) 793ev_loop_new (int methods)
663{ 794{
664 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));
665 798
666 loop_init (EV_A_ methods); 799 loop_init (EV_A_ methods);
667 800
668 if (ev_method (EV_A)) 801 if (ev_method (EV_A))
669 return loop; 802 return loop;
673 806
674void 807void
675ev_loop_destroy (EV_P) 808ev_loop_destroy (EV_P)
676{ 809{
677 loop_destroy (EV_A); 810 loop_destroy (EV_A);
678 free (loop); 811 ev_free (loop);
679} 812}
680 813
681void 814void
682ev_loop_fork (EV_P) 815ev_loop_fork (EV_P)
683{ 816{
684 loop_fork (EV_A); 817 postfork = 1;
685} 818}
686 819
687#endif 820#endif
688 821
689#if EV_MULTIPLICITY 822#if EV_MULTIPLICITY
712 845
713 loop_init (EV_A_ methods); 846 loop_init (EV_A_ methods);
714 847
715 if (ev_method (EV_A)) 848 if (ev_method (EV_A))
716 { 849 {
717 ev_watcher_init (&sigev, sigcb);
718 ev_set_priority (&sigev, EV_MAXPRI);
719 siginit (EV_A); 850 siginit (EV_A);
720 851
721#ifndef WIN32 852#ifndef WIN32
722 ev_signal_init (&childev, childcb, SIGCHLD); 853 ev_signal_init (&childev, childcb, SIGCHLD);
723 ev_set_priority (&childev, EV_MAXPRI); 854 ev_set_priority (&childev, EV_MAXPRI);
737{ 868{
738#if EV_MULTIPLICITY 869#if EV_MULTIPLICITY
739 struct ev_loop *loop = default_loop; 870 struct ev_loop *loop = default_loop;
740#endif 871#endif
741 872
873#ifndef WIN32
742 ev_ref (EV_A); /* child watcher */ 874 ev_ref (EV_A); /* child watcher */
743 ev_signal_stop (EV_A_ &childev); 875 ev_signal_stop (EV_A_ &childev);
876#endif
744 877
745 ev_ref (EV_A); /* signal watcher */ 878 ev_ref (EV_A); /* signal watcher */
746 ev_io_stop (EV_A_ &sigev); 879 ev_io_stop (EV_A_ &sigev);
747 880
748 close (sigpipe [0]); sigpipe [0] = 0; 881 close (sigpipe [0]); sigpipe [0] = 0;
756{ 889{
757#if EV_MULTIPLICITY 890#if EV_MULTIPLICITY
758 struct ev_loop *loop = default_loop; 891 struct ev_loop *loop = default_loop;
759#endif 892#endif
760 893
761 loop_fork (EV_A); 894 if (method)
762 895 postfork = 1;
763 ev_io_stop (EV_A_ &sigev);
764 close (sigpipe [0]);
765 close (sigpipe [1]);
766 pipe (sigpipe);
767
768 ev_ref (EV_A); /* signal watcher */
769 siginit (EV_A);
770} 896}
771 897
772/*****************************************************************************/ 898/*****************************************************************************/
773 899
774static void 900static void
790} 916}
791 917
792static void 918static void
793timers_reify (EV_P) 919timers_reify (EV_P)
794{ 920{
795 while (timercnt && timers [0]->at <= mn_now) 921 while (timercnt && ((WT)timers [0])->at <= mn_now)
796 { 922 {
797 struct ev_timer *w = timers [0]; 923 struct ev_timer *w = timers [0];
798 924
799 assert (("inactive timer on timer heap detected", ev_is_active (w))); 925 assert (("inactive timer on timer heap detected", ev_is_active (w)));
800 926
801 /* first reschedule or stop timer */ 927 /* first reschedule or stop timer */
802 if (w->repeat) 928 if (w->repeat)
803 { 929 {
804 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.));
805 w->at = mn_now + w->repeat; 931 ((WT)w)->at = mn_now + w->repeat;
806 downheap ((WT *)timers, timercnt, 0); 932 downheap ((WT *)timers, timercnt, 0);
807 } 933 }
808 else 934 else
809 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 935 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
810 936
813} 939}
814 940
815static void 941static void
816periodics_reify (EV_P) 942periodics_reify (EV_P)
817{ 943{
818 while (periodiccnt && periodics [0]->at <= rt_now) 944 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
819 { 945 {
820 struct ev_periodic *w = periodics [0]; 946 struct ev_periodic *w = periodics [0];
821 947
822 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 948 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
823 949
824 /* first reschedule or stop timer */ 950 /* first reschedule or stop timer */
825 if (w->interval) 951 if (w->interval)
826 { 952 {
827 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;
828 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));
829 downheap ((WT *)periodics, periodiccnt, 0); 955 downheap ((WT *)periodics, periodiccnt, 0);
830 } 956 }
831 else 957 else
832 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 958 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
833 959
845 { 971 {
846 struct ev_periodic *w = periodics [i]; 972 struct ev_periodic *w = periodics [i];
847 973
848 if (w->interval) 974 if (w->interval)
849 { 975 {
850 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;
851 977
852 if (fabs (diff) >= 1e-4) 978 if (fabs (diff) >= 1e-4)
853 { 979 {
854 ev_periodic_stop (EV_A_ w); 980 ev_periodic_stop (EV_A_ w);
855 ev_periodic_start (EV_A_ w); 981 ev_periodic_start (EV_A_ w);
916 { 1042 {
917 periodics_reschedule (EV_A); 1043 periodics_reschedule (EV_A);
918 1044
919 /* 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 */
920 for (i = 0; i < timercnt; ++i) 1046 for (i = 0; i < timercnt; ++i)
921 timers [i]->at += rt_now - mn_now; 1047 ((WT)timers [i])->at += rt_now - mn_now;
922 } 1048 }
923 1049
924 mn_now = rt_now; 1050 mn_now = rt_now;
925 } 1051 }
926} 1052}
952 { 1078 {
953 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1079 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
954 call_pending (EV_A); 1080 call_pending (EV_A);
955 } 1081 }
956 1082
1083 /* we might have forked, so reify kernel state if necessary */
1084 if (expect_false (postfork))
1085 loop_fork (EV_A);
1086
957 /* update fd-related kernel structures */ 1087 /* update fd-related kernel structures */
958 fd_reify (EV_A); 1088 fd_reify (EV_A);
959 1089
960 /* calculate blocking time */ 1090 /* calculate blocking time */
961 1091
977 { 1107 {
978 block = MAX_BLOCKTIME; 1108 block = MAX_BLOCKTIME;
979 1109
980 if (timercnt) 1110 if (timercnt)
981 { 1111 {
982 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1112 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
983 if (block > to) block = to; 1113 if (block > to) block = to;
984 } 1114 }
985 1115
986 if (periodiccnt) 1116 if (periodiccnt)
987 { 1117 {
988 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1118 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
989 if (block > to) block = to; 1119 if (block > to) block = to;
990 } 1120 }
991 1121
992 if (block < 0.) block = 0.; 1122 if (block < 0.) block = 0.;
993 } 1123 }
1110ev_timer_start (EV_P_ struct ev_timer *w) 1240ev_timer_start (EV_P_ struct ev_timer *w)
1111{ 1241{
1112 if (ev_is_active (w)) 1242 if (ev_is_active (w))
1113 return; 1243 return;
1114 1244
1115 w->at += mn_now; 1245 ((WT)w)->at += mn_now;
1116 1246
1117 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.));
1118 1248
1119 ev_start (EV_A_ (W)w, ++timercnt); 1249 ev_start (EV_A_ (W)w, ++timercnt);
1120 array_needsize (timers, timermax, timercnt, ); 1250 array_needsize (timers, timermax, timercnt, (void));
1121 timers [timercnt - 1] = w; 1251 timers [timercnt - 1] = w;
1122 upheap ((WT *)timers, timercnt - 1); 1252 upheap ((WT *)timers, timercnt - 1);
1253
1254 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1123} 1255}
1124 1256
1125void 1257void
1126ev_timer_stop (EV_P_ struct ev_timer *w) 1258ev_timer_stop (EV_P_ struct ev_timer *w)
1127{ 1259{
1128 ev_clear_pending (EV_A_ (W)w); 1260 ev_clear_pending (EV_A_ (W)w);
1129 if (!ev_is_active (w)) 1261 if (!ev_is_active (w))
1130 return; 1262 return;
1131 1263
1264 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1265
1132 if (w->active < timercnt--) 1266 if (((W)w)->active < timercnt--)
1133 { 1267 {
1134 timers [w->active - 1] = timers [timercnt]; 1268 timers [((W)w)->active - 1] = timers [timercnt];
1135 downheap ((WT *)timers, timercnt, w->active - 1); 1269 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1136 } 1270 }
1137 1271
1138 w->at = w->repeat; 1272 ((WT)w)->at = w->repeat;
1139 1273
1140 ev_stop (EV_A_ (W)w); 1274 ev_stop (EV_A_ (W)w);
1141} 1275}
1142 1276
1143void 1277void
1145{ 1279{
1146 if (ev_is_active (w)) 1280 if (ev_is_active (w))
1147 { 1281 {
1148 if (w->repeat) 1282 if (w->repeat)
1149 { 1283 {
1150 w->at = mn_now + w->repeat; 1284 ((WT)w)->at = mn_now + w->repeat;
1151 downheap ((WT *)timers, timercnt, w->active - 1); 1285 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1152 } 1286 }
1153 else 1287 else
1154 ev_timer_stop (EV_A_ w); 1288 ev_timer_stop (EV_A_ w);
1155 } 1289 }
1156 else if (w->repeat) 1290 else if (w->repeat)
1165 1299
1166 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.));
1167 1301
1168 /* 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 */
1169 if (w->interval) 1303 if (w->interval)
1170 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;
1171 1305
1172 ev_start (EV_A_ (W)w, ++periodiccnt); 1306 ev_start (EV_A_ (W)w, ++periodiccnt);
1173 array_needsize (periodics, periodicmax, periodiccnt, ); 1307 array_needsize (periodics, periodicmax, periodiccnt, (void));
1174 periodics [periodiccnt - 1] = w; 1308 periodics [periodiccnt - 1] = w;
1175 upheap ((WT *)periodics, periodiccnt - 1); 1309 upheap ((WT *)periodics, periodiccnt - 1);
1310
1311 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1176} 1312}
1177 1313
1178void 1314void
1179ev_periodic_stop (EV_P_ struct ev_periodic *w) 1315ev_periodic_stop (EV_P_ struct ev_periodic *w)
1180{ 1316{
1181 ev_clear_pending (EV_A_ (W)w); 1317 ev_clear_pending (EV_A_ (W)w);
1182 if (!ev_is_active (w)) 1318 if (!ev_is_active (w))
1183 return; 1319 return;
1184 1320
1321 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1322
1185 if (w->active < periodiccnt--) 1323 if (((W)w)->active < periodiccnt--)
1186 { 1324 {
1187 periodics [w->active - 1] = periodics [periodiccnt]; 1325 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1188 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1326 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1189 } 1327 }
1190 1328
1191 ev_stop (EV_A_ (W)w); 1329 ev_stop (EV_A_ (W)w);
1192} 1330}
1193 1331
1196{ 1334{
1197 if (ev_is_active (w)) 1335 if (ev_is_active (w))
1198 return; 1336 return;
1199 1337
1200 ev_start (EV_A_ (W)w, ++idlecnt); 1338 ev_start (EV_A_ (W)w, ++idlecnt);
1201 array_needsize (idles, idlemax, idlecnt, ); 1339 array_needsize (idles, idlemax, idlecnt, (void));
1202 idles [idlecnt - 1] = w; 1340 idles [idlecnt - 1] = w;
1203} 1341}
1204 1342
1205void 1343void
1206ev_idle_stop (EV_P_ struct ev_idle *w) 1344ev_idle_stop (EV_P_ struct ev_idle *w)
1207{ 1345{
1208 ev_clear_pending (EV_A_ (W)w); 1346 ev_clear_pending (EV_A_ (W)w);
1209 if (ev_is_active (w)) 1347 if (ev_is_active (w))
1210 return; 1348 return;
1211 1349
1212 idles [w->active - 1] = idles [--idlecnt]; 1350 idles [((W)w)->active - 1] = idles [--idlecnt];
1213 ev_stop (EV_A_ (W)w); 1351 ev_stop (EV_A_ (W)w);
1214} 1352}
1215 1353
1216void 1354void
1217ev_prepare_start (EV_P_ struct ev_prepare *w) 1355ev_prepare_start (EV_P_ struct ev_prepare *w)
1218{ 1356{
1219 if (ev_is_active (w)) 1357 if (ev_is_active (w))
1220 return; 1358 return;
1221 1359
1222 ev_start (EV_A_ (W)w, ++preparecnt); 1360 ev_start (EV_A_ (W)w, ++preparecnt);
1223 array_needsize (prepares, preparemax, preparecnt, ); 1361 array_needsize (prepares, preparemax, preparecnt, (void));
1224 prepares [preparecnt - 1] = w; 1362 prepares [preparecnt - 1] = w;
1225} 1363}
1226 1364
1227void 1365void
1228ev_prepare_stop (EV_P_ struct ev_prepare *w) 1366ev_prepare_stop (EV_P_ struct ev_prepare *w)
1229{ 1367{
1230 ev_clear_pending (EV_A_ (W)w); 1368 ev_clear_pending (EV_A_ (W)w);
1231 if (ev_is_active (w)) 1369 if (ev_is_active (w))
1232 return; 1370 return;
1233 1371
1234 prepares [w->active - 1] = prepares [--preparecnt]; 1372 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1235 ev_stop (EV_A_ (W)w); 1373 ev_stop (EV_A_ (W)w);
1236} 1374}
1237 1375
1238void 1376void
1239ev_check_start (EV_P_ struct ev_check *w) 1377ev_check_start (EV_P_ struct ev_check *w)
1240{ 1378{
1241 if (ev_is_active (w)) 1379 if (ev_is_active (w))
1242 return; 1380 return;
1243 1381
1244 ev_start (EV_A_ (W)w, ++checkcnt); 1382 ev_start (EV_A_ (W)w, ++checkcnt);
1245 array_needsize (checks, checkmax, checkcnt, ); 1383 array_needsize (checks, checkmax, checkcnt, (void));
1246 checks [checkcnt - 1] = w; 1384 checks [checkcnt - 1] = w;
1247} 1385}
1248 1386
1249void 1387void
1250ev_check_stop (EV_P_ struct ev_check *w) 1388ev_check_stop (EV_P_ struct ev_check *w)
1251{ 1389{
1252 ev_clear_pending (EV_A_ (W)w); 1390 ev_clear_pending (EV_A_ (W)w);
1253 if (ev_is_active (w)) 1391 if (ev_is_active (w))
1254 return; 1392 return;
1255 1393
1256 checks [w->active - 1] = checks [--checkcnt]; 1394 checks [((W)w)->active - 1] = checks [--checkcnt];
1257 ev_stop (EV_A_ (W)w); 1395 ev_stop (EV_A_ (W)w);
1258} 1396}
1259 1397
1260#ifndef SA_RESTART 1398#ifndef SA_RESTART
1261# define SA_RESTART 0 1399# define SA_RESTART 0
1274 1412
1275 ev_start (EV_A_ (W)w, 1); 1413 ev_start (EV_A_ (W)w, 1);
1276 array_needsize (signals, signalmax, w->signum, signals_init); 1414 array_needsize (signals, signalmax, w->signum, signals_init);
1277 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1415 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1278 1416
1279 if (!w->next) 1417 if (!((WL)w)->next)
1280 { 1418 {
1419#if WIN32
1420 signal (w->signum, sighandler);
1421#else
1281 struct sigaction sa; 1422 struct sigaction sa;
1282 sa.sa_handler = sighandler; 1423 sa.sa_handler = sighandler;
1283 sigfillset (&sa.sa_mask); 1424 sigfillset (&sa.sa_mask);
1284 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 */
1285 sigaction (w->signum, &sa, 0); 1426 sigaction (w->signum, &sa, 0);
1427#endif
1286 } 1428 }
1287} 1429}
1288 1430
1289void 1431void
1290ev_signal_stop (EV_P_ struct ev_signal *w) 1432ev_signal_stop (EV_P_ struct ev_signal *w)
1340 void (*cb)(int revents, void *arg) = once->cb; 1482 void (*cb)(int revents, void *arg) = once->cb;
1341 void *arg = once->arg; 1483 void *arg = once->arg;
1342 1484
1343 ev_io_stop (EV_A_ &once->io); 1485 ev_io_stop (EV_A_ &once->io);
1344 ev_timer_stop (EV_A_ &once->to); 1486 ev_timer_stop (EV_A_ &once->to);
1345 free (once); 1487 ev_free (once);
1346 1488
1347 cb (revents, arg); 1489 cb (revents, arg);
1348} 1490}
1349 1491
1350static void 1492static void
1360} 1502}
1361 1503
1362void 1504void
1363ev_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)
1364{ 1506{
1365 struct ev_once *once = malloc (sizeof (struct ev_once)); 1507 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1366 1508
1367 if (!once) 1509 if (!once)
1368 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1510 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1369 else 1511 else
1370 { 1512 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines