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

Comparing libev/ev.c (file contents):
Revision 1.57 by root, Sun Nov 4 16:43:53 2007 UTC vs.
Revision 1.70 by root, Tue Nov 6 00:52:32 2007 UTC

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_STANDALONE 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
115typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
116typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
117 147
118static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
119 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
120/*****************************************************************************/ 156/*****************************************************************************/
121 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
122typedef struct 206typedef struct
123{ 207{
124 struct ev_watcher_list *head; 208 WL head;
125 unsigned char events; 209 unsigned char events;
126 unsigned char reify; 210 unsigned char reify;
127} ANFD; 211} ANFD;
128 212
129typedef struct 213typedef struct
187 return rt_now; 271 return rt_now;
188} 272}
189 273
190#define array_roundsize(base,n) ((n) | 4 & ~3) 274#define array_roundsize(base,n) ((n) | 4 & ~3)
191 275
192#define array_needsize(base,cur,cnt,init) \ 276#define array_needsize(base,cur,cnt,init) \
193 if (expect_false ((cnt) > cur)) \ 277 if (expect_false ((cnt) > cur)) \
194 { \ 278 { \
195 int newcnt = cur; \ 279 int newcnt = cur; \
196 do \ 280 do \
197 { \ 281 { \
198 newcnt = array_roundsize (base, newcnt << 1); \ 282 newcnt = array_roundsize (base, newcnt << 1); \
199 } \ 283 } \
200 while ((cnt) > newcnt); \ 284 while ((cnt) > newcnt); \
201 \ 285 \
202 base = realloc (base, sizeof (*base) * (newcnt)); \ 286 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
203 init (base + cur, newcnt - cur); \ 287 init (base + cur, newcnt - cur); \
204 cur = newcnt; \ 288 cur = newcnt; \
205 } 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;
206 301
207/*****************************************************************************/ 302/*****************************************************************************/
208 303
209static void 304static void
210anfds_init (ANFD *base, int count) 305anfds_init (ANFD *base, int count)
276 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)
277 events |= w->events; 372 events |= w->events;
278 373
279 anfd->reify = 0; 374 anfd->reify = 0;
280 375
281 if (anfd->events != events)
282 {
283 method_modify (EV_A_ fd, anfd->events, events); 376 method_modify (EV_A_ fd, anfd->events, events);
284 anfd->events = events; 377 anfd->events = events;
285 }
286 } 378 }
287 379
288 fdchangecnt = 0; 380 fdchangecnt = 0;
289} 381}
290 382
291static void 383static void
292fd_change (EV_P_ int fd) 384fd_change (EV_P_ int fd)
293{ 385{
294 if (anfds [fd].reify || fdchangecnt < 0) 386 if (anfds [fd].reify)
295 return; 387 return;
296 388
297 anfds [fd].reify = 1; 389 anfds [fd].reify = 1;
298 390
299 ++fdchangecnt; 391 ++fdchangecnt;
327 419
328/* 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 */
329static void 421static void
330fd_enomem (EV_P) 422fd_enomem (EV_P)
331{ 423{
332 int fd = anfdmax; 424 int fd;
333 425
334 while (fd--) 426 for (fd = anfdmax; fd--; )
335 if (anfds [fd].events) 427 if (anfds [fd].events)
336 { 428 {
337 close (fd);
338 fd_kill (EV_A_ fd); 429 fd_kill (EV_A_ fd);
339 return; 430 return;
340 } 431 }
341} 432}
342 433
343/* 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 */
344static void 435static void
345fd_rearm_all (EV_P) 436fd_rearm_all (EV_P)
346{ 437{
347 int fd; 438 int fd;
348 439
349 /* 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 */
350 for (fd = 0; fd < anfdmax; ++fd) 441 for (fd = 0; fd < anfdmax; ++fd)
351 if (anfds [fd].events) 442 if (anfds [fd].events)
352 { 443 {
353 anfds [fd].events = 0; 444 anfds [fd].events = 0;
354 fd_change (fd); 445 fd_change (EV_A_ fd);
355 } 446 }
356} 447}
357 448
358/*****************************************************************************/ 449/*****************************************************************************/
359 450
363 WT w = heap [k]; 454 WT w = heap [k];
364 455
365 while (k && heap [k >> 1]->at > w->at) 456 while (k && heap [k >> 1]->at > w->at)
366 { 457 {
367 heap [k] = heap [k >> 1]; 458 heap [k] = heap [k >> 1];
368 heap [k]->active = k + 1; 459 ((W)heap [k])->active = k + 1;
369 k >>= 1; 460 k >>= 1;
370 } 461 }
371 462
372 heap [k] = w; 463 heap [k] = w;
373 heap [k]->active = k + 1; 464 ((W)heap [k])->active = k + 1;
374 465
375} 466}
376 467
377static void 468static void
378downheap (WT *heap, int N, int k) 469downheap (WT *heap, int N, int k)
388 479
389 if (w->at <= heap [j]->at) 480 if (w->at <= heap [j]->at)
390 break; 481 break;
391 482
392 heap [k] = heap [j]; 483 heap [k] = heap [j];
393 heap [k]->active = k + 1; 484 ((W)heap [k])->active = k + 1;
394 k = j; 485 k = j;
395 } 486 }
396 487
397 heap [k] = w; 488 heap [k] = w;
398 heap [k]->active = k + 1; 489 ((W)heap [k])->active = k + 1;
399} 490}
400 491
401/*****************************************************************************/ 492/*****************************************************************************/
402 493
403typedef struct 494typedef struct
404{ 495{
405 struct ev_watcher_list *head; 496 WL head;
406 sig_atomic_t volatile gotsig; 497 sig_atomic_t volatile gotsig;
407} ANSIG; 498} ANSIG;
408 499
409static ANSIG *signals; 500static ANSIG *signals;
410static int signalmax; 501static int signalmax;
411 502
412static int sigpipe [2]; 503static int sigpipe [2];
413static sig_atomic_t volatile gotsig; 504static sig_atomic_t volatile gotsig;
505static struct ev_io sigev;
414 506
415static void 507static void
416signals_init (ANSIG *base, int count) 508signals_init (ANSIG *base, int count)
417{ 509{
418 while (count--) 510 while (count--)
425} 517}
426 518
427static void 519static void
428sighandler (int signum) 520sighandler (int signum)
429{ 521{
522#if WIN32
523 signal (signum, sighandler);
524#endif
525
430 signals [signum - 1].gotsig = 1; 526 signals [signum - 1].gotsig = 1;
431 527
432 if (!gotsig) 528 if (!gotsig)
433 { 529 {
434 int old_errno = errno; 530 int old_errno = errno;
439} 535}
440 536
441static void 537static void
442sigcb (EV_P_ struct ev_io *iow, int revents) 538sigcb (EV_P_ struct ev_io *iow, int revents)
443{ 539{
444 struct ev_watcher_list *w; 540 WL w;
445 int signum; 541 int signum;
446 542
447 read (sigpipe [0], &revents, 1); 543 read (sigpipe [0], &revents, 1);
448 gotsig = 0; 544 gotsig = 0;
449 545
476 572
477/*****************************************************************************/ 573/*****************************************************************************/
478 574
479#ifndef WIN32 575#ifndef WIN32
480 576
577static struct ev_child *childs [PID_HASHSIZE];
578static struct ev_signal childev;
579
481#ifndef WCONTINUED 580#ifndef WCONTINUED
482# define WCONTINUED 0 581# define WCONTINUED 0
483#endif 582#endif
484 583
485static void 584static void
488 struct ev_child *w; 587 struct ev_child *w;
489 588
490 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)
491 if (w->pid == pid || !w->pid) 590 if (w->pid == pid || !w->pid)
492 { 591 {
493 w->priority = sw->priority; /* need to do it *now* */ 592 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
494 w->rpid = pid; 593 w->rpid = pid;
495 w->rstatus = status; 594 w->rstatus = status;
496 event (EV_A_ (W)w, EV_CHILD); 595 event (EV_A_ (W)w, EV_CHILD);
497 } 596 }
498} 597}
499 598
500static void 599static void
520# include "ev_kqueue.c" 619# include "ev_kqueue.c"
521#endif 620#endif
522#if EV_USE_EPOLL 621#if EV_USE_EPOLL
523# include "ev_epoll.c" 622# include "ev_epoll.c"
524#endif 623#endif
525#if EV_USEV_POLL 624#if EV_USE_POLL
526# include "ev_poll.c" 625# include "ev_poll.c"
527#endif 626#endif
528#if EV_USE_SELECT 627#if EV_USE_SELECT
529# include "ev_select.c" 628# include "ev_select.c"
530#endif 629#endif
582 methods = atoi (getenv ("LIBEV_METHODS")); 681 methods = atoi (getenv ("LIBEV_METHODS"));
583 else 682 else
584 methods = EVMETHOD_ANY; 683 methods = EVMETHOD_ANY;
585 684
586 method = 0; 685 method = 0;
686#if EV_USE_WIN32
687 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
688#endif
587#if EV_USE_KQUEUE 689#if EV_USE_KQUEUE
588 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 690 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
589#endif 691#endif
590#if EV_USE_EPOLL 692#if EV_USE_EPOLL
591 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 693 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
592#endif 694#endif
593#if EV_USEV_POLL 695#if EV_USE_POLL
594 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
595#endif 697#endif
596#if EV_USE_SELECT 698#if EV_USE_SELECT
597 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
598#endif 700#endif
701
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
599 } 704 }
600} 705}
601 706
602void 707void
603loop_destroy (EV_P) 708loop_destroy (EV_P)
604{ 709{
710 int i;
711
712#if EV_USE_WIN32
713 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
714#endif
605#if EV_USE_KQUEUE 715#if EV_USE_KQUEUE
606 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 716 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
607#endif 717#endif
608#if EV_USE_EPOLL 718#if EV_USE_EPOLL
609 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 719 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
610#endif 720#endif
611#if EV_USEV_POLL 721#if EV_USE_POLL
612 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 722 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
613#endif 723#endif
614#if EV_USE_SELECT 724#if EV_USE_SELECT
615 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 725 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
616#endif 726#endif
617 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
618 method = 0; 738 method = 0;
619 /*TODO*/
620} 739}
621 740
622void 741static void
623loop_fork (EV_P) 742loop_fork (EV_P)
624{ 743{
625 /*TODO*/
626#if EV_USE_EPOLL 744#if EV_USE_EPOLL
627 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
628#endif 746#endif
629#if EV_USE_KQUEUE 747#if EV_USE_KQUEUE
630 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
631#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;
632} 767}
633 768
634#if EV_MULTIPLICITY 769#if EV_MULTIPLICITY
635struct ev_loop * 770struct ev_loop *
636ev_loop_new (int methods) 771ev_loop_new (int methods)
637{ 772{
638 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));
639 776
640 loop_init (EV_A_ methods); 777 loop_init (EV_A_ methods);
641 778
642 if (ev_methods (EV_A)) 779 if (ev_method (EV_A))
643 return loop; 780 return loop;
644 781
645 return 0; 782 return 0;
646} 783}
647 784
648void 785void
649ev_loop_destroy (EV_P) 786ev_loop_destroy (EV_P)
650{ 787{
651 loop_destroy (EV_A); 788 loop_destroy (EV_A);
652 free (loop); 789 ev_free (loop);
653} 790}
654 791
655void 792void
656ev_loop_fork (EV_P) 793ev_loop_fork (EV_P)
657{ 794{
658 loop_fork (EV_A); 795 postfork = 1;
659} 796}
660 797
661#endif 798#endif
662 799
663#if EV_MULTIPLICITY 800#if EV_MULTIPLICITY
686 823
687 loop_init (EV_A_ methods); 824 loop_init (EV_A_ methods);
688 825
689 if (ev_method (EV_A)) 826 if (ev_method (EV_A))
690 { 827 {
691 ev_watcher_init (&sigev, sigcb);
692 ev_set_priority (&sigev, EV_MAXPRI);
693 siginit (EV_A); 828 siginit (EV_A);
694 829
695#ifndef WIN32 830#ifndef WIN32
696 ev_signal_init (&childev, childcb, SIGCHLD); 831 ev_signal_init (&childev, childcb, SIGCHLD);
697 ev_set_priority (&childev, EV_MAXPRI); 832 ev_set_priority (&childev, EV_MAXPRI);
724 859
725 loop_destroy (EV_A); 860 loop_destroy (EV_A);
726} 861}
727 862
728void 863void
729ev_default_fork (EV_P) 864ev_default_fork (void)
730{ 865{
731 loop_fork (EV_A); 866#if EV_MULTIPLICITY
867 struct ev_loop *loop = default_loop;
868#endif
732 869
733 ev_io_stop (EV_A_ &sigev); 870 if (method)
734 close (sigpipe [0]); 871 postfork = 1;
735 close (sigpipe [1]);
736 pipe (sigpipe);
737
738 ev_ref (EV_A); /* signal watcher */
739 siginit (EV_A);
740} 872}
741 873
742/*****************************************************************************/ 874/*****************************************************************************/
743 875
744static void 876static void
760} 892}
761 893
762static void 894static void
763timers_reify (EV_P) 895timers_reify (EV_P)
764{ 896{
765 while (timercnt && timers [0]->at <= mn_now) 897 while (timercnt && ((WT)timers [0])->at <= mn_now)
766 { 898 {
767 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)));
768 902
769 /* first reschedule or stop timer */ 903 /* first reschedule or stop timer */
770 if (w->repeat) 904 if (w->repeat)
771 { 905 {
772 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.));
773 w->at = mn_now + w->repeat; 907 ((WT)w)->at = mn_now + w->repeat;
774 downheap ((WT *)timers, timercnt, 0); 908 downheap ((WT *)timers, timercnt, 0);
775 } 909 }
776 else 910 else
777 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 911 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
778 912
781} 915}
782 916
783static void 917static void
784periodics_reify (EV_P) 918periodics_reify (EV_P)
785{ 919{
786 while (periodiccnt && periodics [0]->at <= rt_now) 920 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
787 { 921 {
788 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)));
789 925
790 /* first reschedule or stop timer */ 926 /* first reschedule or stop timer */
791 if (w->interval) 927 if (w->interval)
792 { 928 {
793 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;
794 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));
795 downheap ((WT *)periodics, periodiccnt, 0); 931 downheap ((WT *)periodics, periodiccnt, 0);
796 } 932 }
797 else 933 else
798 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 934 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
799 935
811 { 947 {
812 struct ev_periodic *w = periodics [i]; 948 struct ev_periodic *w = periodics [i];
813 949
814 if (w->interval) 950 if (w->interval)
815 { 951 {
816 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;
817 953
818 if (fabs (diff) >= 1e-4) 954 if (fabs (diff) >= 1e-4)
819 { 955 {
820 ev_periodic_stop (EV_A_ w); 956 ev_periodic_stop (EV_A_ w);
821 ev_periodic_start (EV_A_ w); 957 ev_periodic_start (EV_A_ w);
882 { 1018 {
883 periodics_reschedule (EV_A); 1019 periodics_reschedule (EV_A);
884 1020
885 /* 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 */
886 for (i = 0; i < timercnt; ++i) 1022 for (i = 0; i < timercnt; ++i)
887 timers [i]->at += rt_now - mn_now; 1023 ((WT)timers [i])->at += rt_now - mn_now;
888 } 1024 }
889 1025
890 mn_now = rt_now; 1026 mn_now = rt_now;
891 } 1027 }
892} 1028}
918 { 1054 {
919 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
920 call_pending (EV_A); 1056 call_pending (EV_A);
921 } 1057 }
922 1058
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1061 loop_fork (EV_A);
1062
923 /* update fd-related kernel structures */ 1063 /* update fd-related kernel structures */
924 fd_reify (EV_A); 1064 fd_reify (EV_A);
925 1065
926 /* calculate blocking time */ 1066 /* calculate blocking time */
927 1067
943 { 1083 {
944 block = MAX_BLOCKTIME; 1084 block = MAX_BLOCKTIME;
945 1085
946 if (timercnt) 1086 if (timercnt)
947 { 1087 {
948 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1088 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
949 if (block > to) block = to; 1089 if (block > to) block = to;
950 } 1090 }
951 1091
952 if (periodiccnt) 1092 if (periodiccnt)
953 { 1093 {
954 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1094 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
955 if (block > to) block = to; 1095 if (block > to) block = to;
956 } 1096 }
957 1097
958 if (block < 0.) block = 0.; 1098 if (block < 0.) block = 0.;
959 } 1099 }
1076ev_timer_start (EV_P_ struct ev_timer *w) 1216ev_timer_start (EV_P_ struct ev_timer *w)
1077{ 1217{
1078 if (ev_is_active (w)) 1218 if (ev_is_active (w))
1079 return; 1219 return;
1080 1220
1081 w->at += mn_now; 1221 ((WT)w)->at += mn_now;
1082 1222
1083 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.));
1084 1224
1085 ev_start (EV_A_ (W)w, ++timercnt); 1225 ev_start (EV_A_ (W)w, ++timercnt);
1086 array_needsize (timers, timermax, timercnt, ); 1226 array_needsize (timers, timermax, timercnt, );
1087 timers [timercnt - 1] = w; 1227 timers [timercnt - 1] = w;
1088 upheap ((WT *)timers, timercnt - 1); 1228 upheap ((WT *)timers, timercnt - 1);
1229
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1089} 1231}
1090 1232
1091void 1233void
1092ev_timer_stop (EV_P_ struct ev_timer *w) 1234ev_timer_stop (EV_P_ struct ev_timer *w)
1093{ 1235{
1094 ev_clear_pending (EV_A_ (W)w); 1236 ev_clear_pending (EV_A_ (W)w);
1095 if (!ev_is_active (w)) 1237 if (!ev_is_active (w))
1096 return; 1238 return;
1097 1239
1240 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1241
1098 if (w->active < timercnt--) 1242 if (((W)w)->active < timercnt--)
1099 { 1243 {
1100 timers [w->active - 1] = timers [timercnt]; 1244 timers [((W)w)->active - 1] = timers [timercnt];
1101 downheap ((WT *)timers, timercnt, w->active - 1); 1245 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1102 } 1246 }
1103 1247
1104 w->at = w->repeat; 1248 ((WT)w)->at = w->repeat;
1105 1249
1106 ev_stop (EV_A_ (W)w); 1250 ev_stop (EV_A_ (W)w);
1107} 1251}
1108 1252
1109void 1253void
1111{ 1255{
1112 if (ev_is_active (w)) 1256 if (ev_is_active (w))
1113 { 1257 {
1114 if (w->repeat) 1258 if (w->repeat)
1115 { 1259 {
1116 w->at = mn_now + w->repeat; 1260 ((WT)w)->at = mn_now + w->repeat;
1117 downheap ((WT *)timers, timercnt, w->active - 1); 1261 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1118 } 1262 }
1119 else 1263 else
1120 ev_timer_stop (EV_A_ w); 1264 ev_timer_stop (EV_A_ w);
1121 } 1265 }
1122 else if (w->repeat) 1266 else if (w->repeat)
1131 1275
1132 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.));
1133 1277
1134 /* 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 */
1135 if (w->interval) 1279 if (w->interval)
1136 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;
1137 1281
1138 ev_start (EV_A_ (W)w, ++periodiccnt); 1282 ev_start (EV_A_ (W)w, ++periodiccnt);
1139 array_needsize (periodics, periodicmax, periodiccnt, ); 1283 array_needsize (periodics, periodicmax, periodiccnt, );
1140 periodics [periodiccnt - 1] = w; 1284 periodics [periodiccnt - 1] = w;
1141 upheap ((WT *)periodics, periodiccnt - 1); 1285 upheap ((WT *)periodics, periodiccnt - 1);
1286
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1142} 1288}
1143 1289
1144void 1290void
1145ev_periodic_stop (EV_P_ struct ev_periodic *w) 1291ev_periodic_stop (EV_P_ struct ev_periodic *w)
1146{ 1292{
1147 ev_clear_pending (EV_A_ (W)w); 1293 ev_clear_pending (EV_A_ (W)w);
1148 if (!ev_is_active (w)) 1294 if (!ev_is_active (w))
1149 return; 1295 return;
1150 1296
1297 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1298
1151 if (w->active < periodiccnt--) 1299 if (((W)w)->active < periodiccnt--)
1152 { 1300 {
1153 periodics [w->active - 1] = periodics [periodiccnt]; 1301 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1154 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1302 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1155 } 1303 }
1156 1304
1157 ev_stop (EV_A_ (W)w); 1305 ev_stop (EV_A_ (W)w);
1158} 1306}
1159 1307
1173{ 1321{
1174 ev_clear_pending (EV_A_ (W)w); 1322 ev_clear_pending (EV_A_ (W)w);
1175 if (ev_is_active (w)) 1323 if (ev_is_active (w))
1176 return; 1324 return;
1177 1325
1178 idles [w->active - 1] = idles [--idlecnt]; 1326 idles [((W)w)->active - 1] = idles [--idlecnt];
1179 ev_stop (EV_A_ (W)w); 1327 ev_stop (EV_A_ (W)w);
1180} 1328}
1181 1329
1182void 1330void
1183ev_prepare_start (EV_P_ struct ev_prepare *w) 1331ev_prepare_start (EV_P_ struct ev_prepare *w)
1195{ 1343{
1196 ev_clear_pending (EV_A_ (W)w); 1344 ev_clear_pending (EV_A_ (W)w);
1197 if (ev_is_active (w)) 1345 if (ev_is_active (w))
1198 return; 1346 return;
1199 1347
1200 prepares [w->active - 1] = prepares [--preparecnt]; 1348 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1201 ev_stop (EV_A_ (W)w); 1349 ev_stop (EV_A_ (W)w);
1202} 1350}
1203 1351
1204void 1352void
1205ev_check_start (EV_P_ struct ev_check *w) 1353ev_check_start (EV_P_ struct ev_check *w)
1217{ 1365{
1218 ev_clear_pending (EV_A_ (W)w); 1366 ev_clear_pending (EV_A_ (W)w);
1219 if (ev_is_active (w)) 1367 if (ev_is_active (w))
1220 return; 1368 return;
1221 1369
1222 checks [w->active - 1] = checks [--checkcnt]; 1370 checks [((W)w)->active - 1] = checks [--checkcnt];
1223 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1224} 1372}
1225 1373
1226#ifndef SA_RESTART 1374#ifndef SA_RESTART
1227# define SA_RESTART 0 1375# define SA_RESTART 0
1240 1388
1241 ev_start (EV_A_ (W)w, 1); 1389 ev_start (EV_A_ (W)w, 1);
1242 array_needsize (signals, signalmax, w->signum, signals_init); 1390 array_needsize (signals, signalmax, w->signum, signals_init);
1243 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1244 1392
1245 if (!w->next) 1393 if (!((WL)w)->next)
1246 { 1394 {
1395#if WIN32
1396 signal (w->signum, sighandler);
1397#else
1247 struct sigaction sa; 1398 struct sigaction sa;
1248 sa.sa_handler = sighandler; 1399 sa.sa_handler = sighandler;
1249 sigfillset (&sa.sa_mask); 1400 sigfillset (&sa.sa_mask);
1250 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 */
1251 sigaction (w->signum, &sa, 0); 1402 sigaction (w->signum, &sa, 0);
1403#endif
1252 } 1404 }
1253} 1405}
1254 1406
1255void 1407void
1256ev_signal_stop (EV_P_ struct ev_signal *w) 1408ev_signal_stop (EV_P_ struct ev_signal *w)
1306 void (*cb)(int revents, void *arg) = once->cb; 1458 void (*cb)(int revents, void *arg) = once->cb;
1307 void *arg = once->arg; 1459 void *arg = once->arg;
1308 1460
1309 ev_io_stop (EV_A_ &once->io); 1461 ev_io_stop (EV_A_ &once->io);
1310 ev_timer_stop (EV_A_ &once->to); 1462 ev_timer_stop (EV_A_ &once->to);
1311 free (once); 1463 ev_free (once);
1312 1464
1313 cb (revents, arg); 1465 cb (revents, arg);
1314} 1466}
1315 1467
1316static void 1468static void
1326} 1478}
1327 1479
1328void 1480void
1329ev_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)
1330{ 1482{
1331 struct ev_once *once = malloc (sizeof (struct ev_once)); 1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1332 1484
1333 if (!once) 1485 if (!once)
1334 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1335 else 1487 else
1336 { 1488 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines