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.70 by root, Tue Nov 6 00:52:32 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> 59#include <unistd.h>
58 80
59#ifndef EV_USE_SELECT 81#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 82# define EV_USE_SELECT 1
61#endif 83#endif
62 84
63#ifndef EV_USEV_POLL 85#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 86# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 87#endif
66 88
67#ifndef EV_USE_EPOLL 89#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 90# define EV_USE_EPOLL 0
69#endif 91#endif
70 92
71#ifndef EV_USE_KQUEUE 93#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0 94# define EV_USE_KQUEUE 0
95#endif
96
97#ifndef EV_USE_WIN32
98# ifdef WIN32
99# define EV_USE_WIN32 1
100# else
101# define EV_USE_WIN32 0
102# endif
73#endif 103#endif
74 104
75#ifndef EV_USE_REALTIME 105#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1 106# define EV_USE_REALTIME 1
77#endif 107#endif
93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 123#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) */ 124#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 */ 125#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 */ 126/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
97 127
98#ifndef EV_EMBED
99# include "ev.h" 128#include "ev.h"
100#endif
101 129
102#if __GNUC__ >= 3 130#if __GNUC__ >= 3
103# define expect(expr,value) __builtin_expect ((expr),(value)) 131# define expect(expr,value) __builtin_expect ((expr),(value))
104# define inline inline 132# define inline inline
105#else 133#else
117typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
118typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
119 147
120static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
121 149
150#if WIN32
151/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f;
154#endif
155
122/*****************************************************************************/ 156/*****************************************************************************/
123 157
158static void (*syserr_cb)(const char *msg);
159
160void ev_set_syserr_cb (void (*cb)(const char *msg))
161{
162 syserr_cb = cb;
163}
164
165static void
166syserr (const char *msg)
167{
168 if (!msg)
169 msg = "(libev) system error";
170
171 if (syserr_cb)
172 syserr_cb (msg);
173 else
174 {
175 perror (msg);
176 abort ();
177 }
178}
179
180static void *(*alloc)(void *ptr, long size);
181
182void ev_set_allocator (void *(*cb)(void *ptr, long size))
183{
184 alloc = cb;
185}
186
187static void *
188ev_realloc (void *ptr, long size)
189{
190 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
191
192 if (!ptr && size)
193 {
194 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
195 abort ();
196 }
197
198 return ptr;
199}
200
201#define ev_malloc(size) ev_realloc (0, (size))
202#define ev_free(ptr) ev_realloc ((ptr), 0)
203
204/*****************************************************************************/
205
124typedef struct 206typedef struct
125{ 207{
126 struct ev_watcher_list *head; 208 WL head;
127 unsigned char events; 209 unsigned char events;
128 unsigned char reify; 210 unsigned char reify;
129} ANFD; 211} ANFD;
130 212
131typedef struct 213typedef struct
189 return rt_now; 271 return rt_now;
190} 272}
191 273
192#define array_roundsize(base,n) ((n) | 4 & ~3) 274#define array_roundsize(base,n) ((n) | 4 & ~3)
193 275
194#define array_needsize(base,cur,cnt,init) \ 276#define array_needsize(base,cur,cnt,init) \
195 if (expect_false ((cnt) > cur)) \ 277 if (expect_false ((cnt) > cur)) \
196 { \ 278 { \
197 int newcnt = cur; \ 279 int newcnt = cur; \
198 do \ 280 do \
199 { \ 281 { \
200 newcnt = array_roundsize (base, newcnt << 1); \ 282 newcnt = array_roundsize (base, newcnt << 1); \
201 } \ 283 } \
202 while ((cnt) > newcnt); \ 284 while ((cnt) > newcnt); \
203 \ 285 \
204 base = realloc (base, sizeof (*base) * (newcnt)); \ 286 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
205 init (base + cur, newcnt - cur); \ 287 init (base + cur, newcnt - cur); \
206 cur = newcnt; \ 288 cur = newcnt; \
207 } 289 }
290
291#define array_slim(stem) \
292 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
293 { \
294 stem ## max = array_roundsize (stem ## cnt >> 1); \
295 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
296 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
297 }
298
299#define array_free(stem, idx) \
300 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
208 301
209/*****************************************************************************/ 302/*****************************************************************************/
210 303
211static void 304static void
212anfds_init (ANFD *base, int count) 305anfds_init (ANFD *base, int count)
278 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 371 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
279 events |= w->events; 372 events |= w->events;
280 373
281 anfd->reify = 0; 374 anfd->reify = 0;
282 375
283 if (anfd->events != events)
284 {
285 method_modify (EV_A_ fd, anfd->events, events); 376 method_modify (EV_A_ fd, anfd->events, events);
286 anfd->events = events; 377 anfd->events = events;
287 }
288 } 378 }
289 379
290 fdchangecnt = 0; 380 fdchangecnt = 0;
291} 381}
292 382
293static void 383static void
294fd_change (EV_P_ int fd) 384fd_change (EV_P_ int fd)
295{ 385{
296 if (anfds [fd].reify || fdchangecnt < 0) 386 if (anfds [fd].reify)
297 return; 387 return;
298 388
299 anfds [fd].reify = 1; 389 anfds [fd].reify = 1;
300 390
301 ++fdchangecnt; 391 ++fdchangecnt;
329 419
330/* called on ENOMEM in select/poll to kill some fds and retry */ 420/* called on ENOMEM in select/poll to kill some fds and retry */
331static void 421static void
332fd_enomem (EV_P) 422fd_enomem (EV_P)
333{ 423{
334 int fd = anfdmax; 424 int fd;
335 425
336 while (fd--) 426 for (fd = anfdmax; fd--; )
337 if (anfds [fd].events) 427 if (anfds [fd].events)
338 { 428 {
339 close (fd);
340 fd_kill (EV_A_ fd); 429 fd_kill (EV_A_ fd);
341 return; 430 return;
342 } 431 }
343} 432}
344 433
345/* susually called after fork if method needs to re-arm all fds from scratch */ 434/* usually called after fork if method needs to re-arm all fds from scratch */
346static void 435static void
347fd_rearm_all (EV_P) 436fd_rearm_all (EV_P)
348{ 437{
349 int fd; 438 int fd;
350 439
351 /* this should be highly optimised to not do anything but set a flag */ 440 /* this should be highly optimised to not do anything but set a flag */
352 for (fd = 0; fd < anfdmax; ++fd) 441 for (fd = 0; fd < anfdmax; ++fd)
353 if (anfds [fd].events) 442 if (anfds [fd].events)
354 { 443 {
355 anfds [fd].events = 0; 444 anfds [fd].events = 0;
356 fd_change (fd); 445 fd_change (EV_A_ fd);
357 } 446 }
358} 447}
359 448
360/*****************************************************************************/ 449/*****************************************************************************/
361 450
365 WT w = heap [k]; 454 WT w = heap [k];
366 455
367 while (k && heap [k >> 1]->at > w->at) 456 while (k && heap [k >> 1]->at > w->at)
368 { 457 {
369 heap [k] = heap [k >> 1]; 458 heap [k] = heap [k >> 1];
370 heap [k]->active = k + 1; 459 ((W)heap [k])->active = k + 1;
371 k >>= 1; 460 k >>= 1;
372 } 461 }
373 462
374 heap [k] = w; 463 heap [k] = w;
375 heap [k]->active = k + 1; 464 ((W)heap [k])->active = k + 1;
376 465
377} 466}
378 467
379static void 468static void
380downheap (WT *heap, int N, int k) 469downheap (WT *heap, int N, int k)
390 479
391 if (w->at <= heap [j]->at) 480 if (w->at <= heap [j]->at)
392 break; 481 break;
393 482
394 heap [k] = heap [j]; 483 heap [k] = heap [j];
395 heap [k]->active = k + 1; 484 ((W)heap [k])->active = k + 1;
396 k = j; 485 k = j;
397 } 486 }
398 487
399 heap [k] = w; 488 heap [k] = w;
400 heap [k]->active = k + 1; 489 ((W)heap [k])->active = k + 1;
401} 490}
402 491
403/*****************************************************************************/ 492/*****************************************************************************/
404 493
405typedef struct 494typedef struct
406{ 495{
407 struct ev_watcher_list *head; 496 WL head;
408 sig_atomic_t volatile gotsig; 497 sig_atomic_t volatile gotsig;
409} ANSIG; 498} ANSIG;
410 499
411static ANSIG *signals; 500static ANSIG *signals;
412static int signalmax; 501static int signalmax;
413 502
414static int sigpipe [2]; 503static int sigpipe [2];
415static sig_atomic_t volatile gotsig; 504static sig_atomic_t volatile gotsig;
505static struct ev_io sigev;
416 506
417static void 507static void
418signals_init (ANSIG *base, int count) 508signals_init (ANSIG *base, int count)
419{ 509{
420 while (count--) 510 while (count--)
427} 517}
428 518
429static void 519static void
430sighandler (int signum) 520sighandler (int signum)
431{ 521{
522#if WIN32
523 signal (signum, sighandler);
524#endif
525
432 signals [signum - 1].gotsig = 1; 526 signals [signum - 1].gotsig = 1;
433 527
434 if (!gotsig) 528 if (!gotsig)
435 { 529 {
436 int old_errno = errno; 530 int old_errno = errno;
441} 535}
442 536
443static void 537static void
444sigcb (EV_P_ struct ev_io *iow, int revents) 538sigcb (EV_P_ struct ev_io *iow, int revents)
445{ 539{
446 struct ev_watcher_list *w; 540 WL w;
447 int signum; 541 int signum;
448 542
449 read (sigpipe [0], &revents, 1); 543 read (sigpipe [0], &revents, 1);
450 gotsig = 0; 544 gotsig = 0;
451 545
478 572
479/*****************************************************************************/ 573/*****************************************************************************/
480 574
481#ifndef WIN32 575#ifndef WIN32
482 576
577static struct ev_child *childs [PID_HASHSIZE];
578static struct ev_signal childev;
579
483#ifndef WCONTINUED 580#ifndef WCONTINUED
484# define WCONTINUED 0 581# define WCONTINUED 0
485#endif 582#endif
486 583
487static void 584static void
490 struct ev_child *w; 587 struct ev_child *w;
491 588
492 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 589 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) 590 if (w->pid == pid || !w->pid)
494 { 591 {
495 w->priority = sw->priority; /* need to do it *now* */ 592 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
496 w->rpid = pid; 593 w->rpid = pid;
497 w->rstatus = status; 594 w->rstatus = status;
498 event (EV_A_ (W)w, EV_CHILD); 595 event (EV_A_ (W)w, EV_CHILD);
499 } 596 }
500} 597}
501 598
502static void 599static void
522# include "ev_kqueue.c" 619# include "ev_kqueue.c"
523#endif 620#endif
524#if EV_USE_EPOLL 621#if EV_USE_EPOLL
525# include "ev_epoll.c" 622# include "ev_epoll.c"
526#endif 623#endif
527#if EV_USEV_POLL 624#if EV_USE_POLL
528# include "ev_poll.c" 625# include "ev_poll.c"
529#endif 626#endif
530#if EV_USE_SELECT 627#if EV_USE_SELECT
531# include "ev_select.c" 628# include "ev_select.c"
532#endif 629#endif
584 methods = atoi (getenv ("LIBEV_METHODS")); 681 methods = atoi (getenv ("LIBEV_METHODS"));
585 else 682 else
586 methods = EVMETHOD_ANY; 683 methods = EVMETHOD_ANY;
587 684
588 method = 0; 685 method = 0;
686#if EV_USE_WIN32
687 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
688#endif
589#if EV_USE_KQUEUE 689#if EV_USE_KQUEUE
590 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 690 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
591#endif 691#endif
592#if EV_USE_EPOLL 692#if EV_USE_EPOLL
593 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 693 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
594#endif 694#endif
595#if EV_USEV_POLL 695#if EV_USE_POLL
596 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
597#endif 697#endif
598#if EV_USE_SELECT 698#if EV_USE_SELECT
599 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
600#endif 700#endif
701
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
601 } 704 }
602} 705}
603 706
604void 707void
605loop_destroy (EV_P) 708loop_destroy (EV_P)
606{ 709{
710 int i;
711
712#if EV_USE_WIN32
713 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
714#endif
607#if EV_USE_KQUEUE 715#if EV_USE_KQUEUE
608 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 716 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
609#endif 717#endif
610#if EV_USE_EPOLL 718#if EV_USE_EPOLL
611 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 719 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
612#endif 720#endif
613#if EV_USEV_POLL 721#if EV_USE_POLL
614 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 722 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
615#endif 723#endif
616#if EV_USE_SELECT 724#if EV_USE_SELECT
617 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 725 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
618#endif 726#endif
619 727
728 for (i = NUMPRI; i--; )
729 array_free (pending, [i]);
730
731 array_free (fdchange, );
732 array_free (timer, );
733 array_free (periodic, );
734 array_free (idle, );
735 array_free (prepare, );
736 array_free (check, );
737
620 method = 0; 738 method = 0;
621 /*TODO*/
622} 739}
623 740
624void 741static void
625loop_fork (EV_P) 742loop_fork (EV_P)
626{ 743{
627 /*TODO*/
628#if EV_USE_EPOLL 744#if EV_USE_EPOLL
629 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
630#endif 746#endif
631#if EV_USE_KQUEUE 747#if EV_USE_KQUEUE
632 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
633#endif 749#endif
750
751 if (ev_is_active (&sigev))
752 {
753 /* default loop */
754
755 ev_ref (EV_A);
756 ev_io_stop (EV_A_ &sigev);
757 close (sigpipe [0]);
758 close (sigpipe [1]);
759
760 while (pipe (sigpipe))
761 syserr ("(libev) error creating pipe");
762
763 siginit (EV_A);
764 }
765
766 postfork = 0;
634} 767}
635 768
636#if EV_MULTIPLICITY 769#if EV_MULTIPLICITY
637struct ev_loop * 770struct ev_loop *
638ev_loop_new (int methods) 771ev_loop_new (int methods)
639{ 772{
640 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 773 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
774
775 memset (loop, 0, sizeof (struct ev_loop));
641 776
642 loop_init (EV_A_ methods); 777 loop_init (EV_A_ methods);
643 778
644 if (ev_methods (EV_A)) 779 if (ev_method (EV_A))
645 return loop; 780 return loop;
646 781
647 return 0; 782 return 0;
648} 783}
649 784
650void 785void
651ev_loop_destroy (EV_P) 786ev_loop_destroy (EV_P)
652{ 787{
653 loop_destroy (EV_A); 788 loop_destroy (EV_A);
654 free (loop); 789 ev_free (loop);
655} 790}
656 791
657void 792void
658ev_loop_fork (EV_P) 793ev_loop_fork (EV_P)
659{ 794{
660 loop_fork (EV_A); 795 postfork = 1;
661} 796}
662 797
663#endif 798#endif
664 799
665#if EV_MULTIPLICITY 800#if EV_MULTIPLICITY
688 823
689 loop_init (EV_A_ methods); 824 loop_init (EV_A_ methods);
690 825
691 if (ev_method (EV_A)) 826 if (ev_method (EV_A))
692 { 827 {
693 ev_watcher_init (&sigev, sigcb);
694 ev_set_priority (&sigev, EV_MAXPRI);
695 siginit (EV_A); 828 siginit (EV_A);
696 829
697#ifndef WIN32 830#ifndef WIN32
698 ev_signal_init (&childev, childcb, SIGCHLD); 831 ev_signal_init (&childev, childcb, SIGCHLD);
699 ev_set_priority (&childev, EV_MAXPRI); 832 ev_set_priority (&childev, EV_MAXPRI);
726 859
727 loop_destroy (EV_A); 860 loop_destroy (EV_A);
728} 861}
729 862
730void 863void
731ev_default_fork (EV_P) 864ev_default_fork (void)
732{ 865{
733 loop_fork (EV_A); 866#if EV_MULTIPLICITY
867 struct ev_loop *loop = default_loop;
868#endif
734 869
735 ev_io_stop (EV_A_ &sigev); 870 if (method)
736 close (sigpipe [0]); 871 postfork = 1;
737 close (sigpipe [1]);
738 pipe (sigpipe);
739
740 ev_ref (EV_A); /* signal watcher */
741 siginit (EV_A);
742} 872}
743 873
744/*****************************************************************************/ 874/*****************************************************************************/
745 875
746static void 876static void
762} 892}
763 893
764static void 894static void
765timers_reify (EV_P) 895timers_reify (EV_P)
766{ 896{
767 while (timercnt && timers [0]->at <= mn_now) 897 while (timercnt && ((WT)timers [0])->at <= mn_now)
768 { 898 {
769 struct ev_timer *w = timers [0]; 899 struct ev_timer *w = timers [0];
900
901 assert (("inactive timer on timer heap detected", ev_is_active (w)));
770 902
771 /* first reschedule or stop timer */ 903 /* first reschedule or stop timer */
772 if (w->repeat) 904 if (w->repeat)
773 { 905 {
774 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 906 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
775 w->at = mn_now + w->repeat; 907 ((WT)w)->at = mn_now + w->repeat;
776 downheap ((WT *)timers, timercnt, 0); 908 downheap ((WT *)timers, timercnt, 0);
777 } 909 }
778 else 910 else
779 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 911 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
780 912
783} 915}
784 916
785static void 917static void
786periodics_reify (EV_P) 918periodics_reify (EV_P)
787{ 919{
788 while (periodiccnt && periodics [0]->at <= rt_now) 920 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
789 { 921 {
790 struct ev_periodic *w = periodics [0]; 922 struct ev_periodic *w = periodics [0];
923
924 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
791 925
792 /* first reschedule or stop timer */ 926 /* first reschedule or stop timer */
793 if (w->interval) 927 if (w->interval)
794 { 928 {
795 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 929 ((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)); 930 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
797 downheap ((WT *)periodics, periodiccnt, 0); 931 downheap ((WT *)periodics, periodiccnt, 0);
798 } 932 }
799 else 933 else
800 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 934 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
801 935
813 { 947 {
814 struct ev_periodic *w = periodics [i]; 948 struct ev_periodic *w = periodics [i];
815 949
816 if (w->interval) 950 if (w->interval)
817 { 951 {
818 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 952 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
819 953
820 if (fabs (diff) >= 1e-4) 954 if (fabs (diff) >= 1e-4)
821 { 955 {
822 ev_periodic_stop (EV_A_ w); 956 ev_periodic_stop (EV_A_ w);
823 ev_periodic_start (EV_A_ w); 957 ev_periodic_start (EV_A_ w);
884 { 1018 {
885 periodics_reschedule (EV_A); 1019 periodics_reschedule (EV_A);
886 1020
887 /* adjust timers. this is easy, as the offset is the same for all */ 1021 /* adjust timers. this is easy, as the offset is the same for all */
888 for (i = 0; i < timercnt; ++i) 1022 for (i = 0; i < timercnt; ++i)
889 timers [i]->at += rt_now - mn_now; 1023 ((WT)timers [i])->at += rt_now - mn_now;
890 } 1024 }
891 1025
892 mn_now = rt_now; 1026 mn_now = rt_now;
893 } 1027 }
894} 1028}
920 { 1054 {
921 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
922 call_pending (EV_A); 1056 call_pending (EV_A);
923 } 1057 }
924 1058
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1061 loop_fork (EV_A);
1062
925 /* update fd-related kernel structures */ 1063 /* update fd-related kernel structures */
926 fd_reify (EV_A); 1064 fd_reify (EV_A);
927 1065
928 /* calculate blocking time */ 1066 /* calculate blocking time */
929 1067
945 { 1083 {
946 block = MAX_BLOCKTIME; 1084 block = MAX_BLOCKTIME;
947 1085
948 if (timercnt) 1086 if (timercnt)
949 { 1087 {
950 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1088 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
951 if (block > to) block = to; 1089 if (block > to) block = to;
952 } 1090 }
953 1091
954 if (periodiccnt) 1092 if (periodiccnt)
955 { 1093 {
956 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1094 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
957 if (block > to) block = to; 1095 if (block > to) block = to;
958 } 1096 }
959 1097
960 if (block < 0.) block = 0.; 1098 if (block < 0.) block = 0.;
961 } 1099 }
1078ev_timer_start (EV_P_ struct ev_timer *w) 1216ev_timer_start (EV_P_ struct ev_timer *w)
1079{ 1217{
1080 if (ev_is_active (w)) 1218 if (ev_is_active (w))
1081 return; 1219 return;
1082 1220
1083 w->at += mn_now; 1221 ((WT)w)->at += mn_now;
1084 1222
1085 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1223 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1086 1224
1087 ev_start (EV_A_ (W)w, ++timercnt); 1225 ev_start (EV_A_ (W)w, ++timercnt);
1088 array_needsize (timers, timermax, timercnt, ); 1226 array_needsize (timers, timermax, timercnt, );
1089 timers [timercnt - 1] = w; 1227 timers [timercnt - 1] = w;
1090 upheap ((WT *)timers, timercnt - 1); 1228 upheap ((WT *)timers, timercnt - 1);
1229
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1091} 1231}
1092 1232
1093void 1233void
1094ev_timer_stop (EV_P_ struct ev_timer *w) 1234ev_timer_stop (EV_P_ struct ev_timer *w)
1095{ 1235{
1096 ev_clear_pending (EV_A_ (W)w); 1236 ev_clear_pending (EV_A_ (W)w);
1097 if (!ev_is_active (w)) 1237 if (!ev_is_active (w))
1098 return; 1238 return;
1099 1239
1240 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1241
1100 if (w->active < timercnt--) 1242 if (((W)w)->active < timercnt--)
1101 { 1243 {
1102 timers [w->active - 1] = timers [timercnt]; 1244 timers [((W)w)->active - 1] = timers [timercnt];
1103 downheap ((WT *)timers, timercnt, w->active - 1); 1245 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1104 } 1246 }
1105 1247
1106 w->at = w->repeat; 1248 ((WT)w)->at = w->repeat;
1107 1249
1108 ev_stop (EV_A_ (W)w); 1250 ev_stop (EV_A_ (W)w);
1109} 1251}
1110 1252
1111void 1253void
1113{ 1255{
1114 if (ev_is_active (w)) 1256 if (ev_is_active (w))
1115 { 1257 {
1116 if (w->repeat) 1258 if (w->repeat)
1117 { 1259 {
1118 w->at = mn_now + w->repeat; 1260 ((WT)w)->at = mn_now + w->repeat;
1119 downheap ((WT *)timers, timercnt, w->active - 1); 1261 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1120 } 1262 }
1121 else 1263 else
1122 ev_timer_stop (EV_A_ w); 1264 ev_timer_stop (EV_A_ w);
1123 } 1265 }
1124 else if (w->repeat) 1266 else if (w->repeat)
1133 1275
1134 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1276 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1135 1277
1136 /* this formula differs from the one in periodic_reify because we do not always round up */ 1278 /* this formula differs from the one in periodic_reify because we do not always round up */
1137 if (w->interval) 1279 if (w->interval)
1138 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1280 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1139 1281
1140 ev_start (EV_A_ (W)w, ++periodiccnt); 1282 ev_start (EV_A_ (W)w, ++periodiccnt);
1141 array_needsize (periodics, periodicmax, periodiccnt, ); 1283 array_needsize (periodics, periodicmax, periodiccnt, );
1142 periodics [periodiccnt - 1] = w; 1284 periodics [periodiccnt - 1] = w;
1143 upheap ((WT *)periodics, periodiccnt - 1); 1285 upheap ((WT *)periodics, periodiccnt - 1);
1286
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1144} 1288}
1145 1289
1146void 1290void
1147ev_periodic_stop (EV_P_ struct ev_periodic *w) 1291ev_periodic_stop (EV_P_ struct ev_periodic *w)
1148{ 1292{
1149 ev_clear_pending (EV_A_ (W)w); 1293 ev_clear_pending (EV_A_ (W)w);
1150 if (!ev_is_active (w)) 1294 if (!ev_is_active (w))
1151 return; 1295 return;
1152 1296
1297 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1298
1153 if (w->active < periodiccnt--) 1299 if (((W)w)->active < periodiccnt--)
1154 { 1300 {
1155 periodics [w->active - 1] = periodics [periodiccnt]; 1301 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1156 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1302 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1157 } 1303 }
1158 1304
1159 ev_stop (EV_A_ (W)w); 1305 ev_stop (EV_A_ (W)w);
1160} 1306}
1161 1307
1175{ 1321{
1176 ev_clear_pending (EV_A_ (W)w); 1322 ev_clear_pending (EV_A_ (W)w);
1177 if (ev_is_active (w)) 1323 if (ev_is_active (w))
1178 return; 1324 return;
1179 1325
1180 idles [w->active - 1] = idles [--idlecnt]; 1326 idles [((W)w)->active - 1] = idles [--idlecnt];
1181 ev_stop (EV_A_ (W)w); 1327 ev_stop (EV_A_ (W)w);
1182} 1328}
1183 1329
1184void 1330void
1185ev_prepare_start (EV_P_ struct ev_prepare *w) 1331ev_prepare_start (EV_P_ struct ev_prepare *w)
1197{ 1343{
1198 ev_clear_pending (EV_A_ (W)w); 1344 ev_clear_pending (EV_A_ (W)w);
1199 if (ev_is_active (w)) 1345 if (ev_is_active (w))
1200 return; 1346 return;
1201 1347
1202 prepares [w->active - 1] = prepares [--preparecnt]; 1348 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1203 ev_stop (EV_A_ (W)w); 1349 ev_stop (EV_A_ (W)w);
1204} 1350}
1205 1351
1206void 1352void
1207ev_check_start (EV_P_ struct ev_check *w) 1353ev_check_start (EV_P_ struct ev_check *w)
1219{ 1365{
1220 ev_clear_pending (EV_A_ (W)w); 1366 ev_clear_pending (EV_A_ (W)w);
1221 if (ev_is_active (w)) 1367 if (ev_is_active (w))
1222 return; 1368 return;
1223 1369
1224 checks [w->active - 1] = checks [--checkcnt]; 1370 checks [((W)w)->active - 1] = checks [--checkcnt];
1225 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1226} 1372}
1227 1373
1228#ifndef SA_RESTART 1374#ifndef SA_RESTART
1229# define SA_RESTART 0 1375# define SA_RESTART 0
1242 1388
1243 ev_start (EV_A_ (W)w, 1); 1389 ev_start (EV_A_ (W)w, 1);
1244 array_needsize (signals, signalmax, w->signum, signals_init); 1390 array_needsize (signals, signalmax, w->signum, signals_init);
1245 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1246 1392
1247 if (!w->next) 1393 if (!((WL)w)->next)
1248 { 1394 {
1395#if WIN32
1396 signal (w->signum, sighandler);
1397#else
1249 struct sigaction sa; 1398 struct sigaction sa;
1250 sa.sa_handler = sighandler; 1399 sa.sa_handler = sighandler;
1251 sigfillset (&sa.sa_mask); 1400 sigfillset (&sa.sa_mask);
1252 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1401 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1253 sigaction (w->signum, &sa, 0); 1402 sigaction (w->signum, &sa, 0);
1403#endif
1254 } 1404 }
1255} 1405}
1256 1406
1257void 1407void
1258ev_signal_stop (EV_P_ struct ev_signal *w) 1408ev_signal_stop (EV_P_ struct ev_signal *w)
1308 void (*cb)(int revents, void *arg) = once->cb; 1458 void (*cb)(int revents, void *arg) = once->cb;
1309 void *arg = once->arg; 1459 void *arg = once->arg;
1310 1460
1311 ev_io_stop (EV_A_ &once->io); 1461 ev_io_stop (EV_A_ &once->io);
1312 ev_timer_stop (EV_A_ &once->to); 1462 ev_timer_stop (EV_A_ &once->to);
1313 free (once); 1463 ev_free (once);
1314 1464
1315 cb (revents, arg); 1465 cb (revents, arg);
1316} 1466}
1317 1467
1318static void 1468static void
1328} 1478}
1329 1479
1330void 1480void
1331ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1481ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1332{ 1482{
1333 struct ev_once *once = malloc (sizeof (struct ev_once)); 1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1334 1484
1335 if (!once) 1485 if (!once)
1336 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1337 else 1487 else
1338 { 1488 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines