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

Comparing libev/ev.c (file contents):
Revision 1.54 by root, Sun Nov 4 00:24:16 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
130{ 214{
131 W w; 215 W w;
132 int events; 216 int events;
133} ANPENDING; 217} ANPENDING;
134 218
135#ifdef EV_MULTIPLICITY 219#if EV_MULTIPLICITY
136 220
137struct ev_loop 221struct ev_loop
138{ 222{
139# define VAR(name,decl) decl; 223# define VAR(name,decl) decl;
140# include "ev_vars.h" 224# include "ev_vars.h"
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
434/* usually called after fork if method needs to re-arm all fds from scratch */
435static void
436fd_rearm_all (EV_P)
437{
438 int fd;
439
440 /* this should be highly optimised to not do anything but set a flag */
441 for (fd = 0; fd < anfdmax; ++fd)
442 if (anfds [fd].events)
443 {
444 anfds [fd].events = 0;
445 fd_change (EV_A_ fd);
446 }
447}
448
343/*****************************************************************************/ 449/*****************************************************************************/
344 450
345static void 451static void
346upheap (WT *heap, int k) 452upheap (WT *heap, int k)
347{ 453{
348 WT w = heap [k]; 454 WT w = heap [k];
349 455
350 while (k && heap [k >> 1]->at > w->at) 456 while (k && heap [k >> 1]->at > w->at)
351 { 457 {
352 heap [k] = heap [k >> 1]; 458 heap [k] = heap [k >> 1];
353 heap [k]->active = k + 1; 459 ((W)heap [k])->active = k + 1;
354 k >>= 1; 460 k >>= 1;
355 } 461 }
356 462
357 heap [k] = w; 463 heap [k] = w;
358 heap [k]->active = k + 1; 464 ((W)heap [k])->active = k + 1;
359 465
360} 466}
361 467
362static void 468static void
363downheap (WT *heap, int N, int k) 469downheap (WT *heap, int N, int k)
373 479
374 if (w->at <= heap [j]->at) 480 if (w->at <= heap [j]->at)
375 break; 481 break;
376 482
377 heap [k] = heap [j]; 483 heap [k] = heap [j];
378 heap [k]->active = k + 1; 484 ((W)heap [k])->active = k + 1;
379 k = j; 485 k = j;
380 } 486 }
381 487
382 heap [k] = w; 488 heap [k] = w;
383 heap [k]->active = k + 1; 489 ((W)heap [k])->active = k + 1;
384} 490}
385 491
386/*****************************************************************************/ 492/*****************************************************************************/
387 493
388typedef struct 494typedef struct
389{ 495{
390 struct ev_watcher_list *head; 496 WL head;
391 sig_atomic_t volatile gotsig; 497 sig_atomic_t volatile gotsig;
392} ANSIG; 498} ANSIG;
393 499
394static ANSIG *signals; 500static ANSIG *signals;
395static int signalmax; 501static int signalmax;
396 502
397static int sigpipe [2]; 503static int sigpipe [2];
398static sig_atomic_t volatile gotsig; 504static sig_atomic_t volatile gotsig;
505static struct ev_io sigev;
399 506
400static void 507static void
401signals_init (ANSIG *base, int count) 508signals_init (ANSIG *base, int count)
402{ 509{
403 while (count--) 510 while (count--)
410} 517}
411 518
412static void 519static void
413sighandler (int signum) 520sighandler (int signum)
414{ 521{
522#if WIN32
523 signal (signum, sighandler);
524#endif
525
415 signals [signum - 1].gotsig = 1; 526 signals [signum - 1].gotsig = 1;
416 527
417 if (!gotsig) 528 if (!gotsig)
418 { 529 {
419 int old_errno = errno; 530 int old_errno = errno;
424} 535}
425 536
426static void 537static void
427sigcb (EV_P_ struct ev_io *iow, int revents) 538sigcb (EV_P_ struct ev_io *iow, int revents)
428{ 539{
429 struct ev_watcher_list *w; 540 WL w;
430 int signum; 541 int signum;
431 542
432 read (sigpipe [0], &revents, 1); 543 read (sigpipe [0], &revents, 1);
433 gotsig = 0; 544 gotsig = 0;
434 545
461 572
462/*****************************************************************************/ 573/*****************************************************************************/
463 574
464#ifndef WIN32 575#ifndef WIN32
465 576
577static struct ev_child *childs [PID_HASHSIZE];
578static struct ev_signal childev;
579
466#ifndef WCONTINUED 580#ifndef WCONTINUED
467# define WCONTINUED 0 581# define WCONTINUED 0
468#endif 582#endif
469 583
470static void 584static void
473 struct ev_child *w; 587 struct ev_child *w;
474 588
475 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)
476 if (w->pid == pid || !w->pid) 590 if (w->pid == pid || !w->pid)
477 { 591 {
478 w->priority = sw->priority; /* need to do it *now* */ 592 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
479 w->rpid = pid; 593 w->rpid = pid;
480 w->rstatus = status; 594 w->rstatus = status;
481 event (EV_A_ (W)w, EV_CHILD); 595 event (EV_A_ (W)w, EV_CHILD);
482 } 596 }
483} 597}
484 598
485static void 599static void
505# include "ev_kqueue.c" 619# include "ev_kqueue.c"
506#endif 620#endif
507#if EV_USE_EPOLL 621#if EV_USE_EPOLL
508# include "ev_epoll.c" 622# include "ev_epoll.c"
509#endif 623#endif
510#if EV_USEV_POLL 624#if EV_USE_POLL
511# include "ev_poll.c" 625# include "ev_poll.c"
512#endif 626#endif
513#if EV_USE_SELECT 627#if EV_USE_SELECT
514# include "ev_select.c" 628# include "ev_select.c"
515#endif 629#endif
560 rt_now = ev_time (); 674 rt_now = ev_time ();
561 mn_now = get_clock (); 675 mn_now = get_clock ();
562 now_floor = mn_now; 676 now_floor = mn_now;
563 rtmn_diff = rt_now - mn_now; 677 rtmn_diff = rt_now - mn_now;
564 678
565 if (pipe (sigpipe))
566 return 0;
567
568 if (methods == EVMETHOD_AUTO) 679 if (methods == EVMETHOD_AUTO)
569 if (!enable_secure () && getenv ("LIBmethodS")) 680 if (!enable_secure () && getenv ("LIBEV_METHODS"))
570 methods = atoi (getenv ("LIBmethodS")); 681 methods = atoi (getenv ("LIBEV_METHODS"));
571 else 682 else
572 methods = EVMETHOD_ANY; 683 methods = EVMETHOD_ANY;
573 684
574 method = 0; 685 method = 0;
686#if EV_USE_WIN32
687 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
688#endif
575#if EV_USE_KQUEUE 689#if EV_USE_KQUEUE
576 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 690 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
577#endif 691#endif
578#if EV_USE_EPOLL 692#if EV_USE_EPOLL
579 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 693 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
580#endif 694#endif
581#if EV_USEV_POLL 695#if EV_USE_POLL
582 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
583#endif 697#endif
584#if EV_USE_SELECT 698#if EV_USE_SELECT
585 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
586#endif 700#endif
587 701
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
704 }
705}
706
707void
708loop_destroy (EV_P)
709{
710 int i;
711
712#if EV_USE_WIN32
713 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
714#endif
715#if EV_USE_KQUEUE
716 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
717#endif
718#if EV_USE_EPOLL
719 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
720#endif
721#if EV_USE_POLL
722 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
723#endif
724#if EV_USE_SELECT
725 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
726#endif
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
738 method = 0;
739}
740
741static void
742loop_fork (EV_P)
743{
744#if EV_USE_EPOLL
745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
746#endif
747#if EV_USE_KQUEUE
748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
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;
767}
768
769#if EV_MULTIPLICITY
770struct ev_loop *
771ev_loop_new (int methods)
772{
773 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
774
775 memset (loop, 0, sizeof (struct ev_loop));
776
777 loop_init (EV_A_ methods);
778
779 if (ev_method (EV_A))
780 return loop;
781
782 return 0;
783}
784
785void
786ev_loop_destroy (EV_P)
787{
788 loop_destroy (EV_A);
789 ev_free (loop);
790}
791
792void
793ev_loop_fork (EV_P)
794{
795 postfork = 1;
796}
797
798#endif
799
800#if EV_MULTIPLICITY
801struct ev_loop default_loop_struct;
802static struct ev_loop *default_loop;
803
804struct ev_loop *
805#else
806static int default_loop;
807
808int
809#endif
810ev_default_loop (int methods)
811{
812 if (sigpipe [0] == sigpipe [1])
813 if (pipe (sigpipe))
814 return 0;
815
816 if (!default_loop)
817 {
818#if EV_MULTIPLICITY
819 struct ev_loop *loop = default_loop = &default_loop_struct;
820#else
821 default_loop = 1;
822#endif
823
824 loop_init (EV_A_ methods);
825
588 if (method) 826 if (ev_method (EV_A))
589 { 827 {
590 ev_watcher_init (&sigev, sigcb);
591 ev_set_priority (&sigev, EV_MAXPRI);
592 siginit (EV_A); 828 siginit (EV_A);
593 829
594#ifndef WIN32 830#ifndef WIN32
595 ev_signal_init (&childev, childcb, SIGCHLD); 831 ev_signal_init (&childev, childcb, SIGCHLD);
596 ev_set_priority (&childev, EV_MAXPRI); 832 ev_set_priority (&childev, EV_MAXPRI);
597 ev_signal_start (EV_A_ &childev); 833 ev_signal_start (EV_A_ &childev);
598 ev_unref (EV_A); /* child watcher should not keep loop alive */ 834 ev_unref (EV_A); /* child watcher should not keep loop alive */
599#endif 835#endif
600 } 836 }
837 else
838 default_loop = 0;
601 } 839 }
602 840
603 return method;
604}
605
606#ifdef EV_MULTIPLICITY
607
608struct ev_loop *
609ev_loop_new (int methods)
610{
611 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
612
613 loop_init (EV_A_ methods);
614
615 return loop; 841 return default_loop;
616} 842}
617 843
618void 844void
619ev_loop_delete (EV_P) 845ev_default_destroy (void)
620{ 846{
621 /*TODO*/
622 free (loop);
623}
624
625#else
626
627int
628ev_init (int methods)
629{
630 loop_init ();
631}
632
633#endif
634
635/*****************************************************************************/
636
637void
638ev_fork_prepare (void)
639{
640 /* nop */
641}
642
643void
644ev_fork_parent (void)
645{
646 /* nop */
647}
648
649void
650ev_fork_child (void)
651{
652 /*TODO*/
653#if !EV_MULTIPLICITY 847#if EV_MULTIPLICITY
654#if EV_USE_EPOLL 848 struct ev_loop *loop = default_loop;
655 if (method == EVMETHOD_EPOLL)
656 epoll_postfork_child (EV_A);
657#endif 849#endif
658 850
851 ev_ref (EV_A); /* child watcher */
852 ev_signal_stop (EV_A_ &childev);
853
854 ev_ref (EV_A); /* signal watcher */
659 ev_io_stop (EV_A_ &sigev); 855 ev_io_stop (EV_A_ &sigev);
660 close (sigpipe [0]); 856
661 close (sigpipe [1]); 857 close (sigpipe [0]); sigpipe [0] = 0;
662 pipe (sigpipe); 858 close (sigpipe [1]); sigpipe [1] = 0;
663 siginit (EV_A); 859
860 loop_destroy (EV_A);
861}
862
863void
864ev_default_fork (void)
865{
866#if EV_MULTIPLICITY
867 struct ev_loop *loop = default_loop;
664#endif 868#endif
869
870 if (method)
871 postfork = 1;
665} 872}
666 873
667/*****************************************************************************/ 874/*****************************************************************************/
668 875
669static void 876static void
685} 892}
686 893
687static void 894static void
688timers_reify (EV_P) 895timers_reify (EV_P)
689{ 896{
690 while (timercnt && timers [0]->at <= mn_now) 897 while (timercnt && ((WT)timers [0])->at <= mn_now)
691 { 898 {
692 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)));
693 902
694 /* first reschedule or stop timer */ 903 /* first reschedule or stop timer */
695 if (w->repeat) 904 if (w->repeat)
696 { 905 {
697 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.));
698 w->at = mn_now + w->repeat; 907 ((WT)w)->at = mn_now + w->repeat;
699 downheap ((WT *)timers, timercnt, 0); 908 downheap ((WT *)timers, timercnt, 0);
700 } 909 }
701 else 910 else
702 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 911 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
703 912
706} 915}
707 916
708static void 917static void
709periodics_reify (EV_P) 918periodics_reify (EV_P)
710{ 919{
711 while (periodiccnt && periodics [0]->at <= rt_now) 920 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
712 { 921 {
713 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)));
714 925
715 /* first reschedule or stop timer */ 926 /* first reschedule or stop timer */
716 if (w->interval) 927 if (w->interval)
717 { 928 {
718 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;
719 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));
720 downheap ((WT *)periodics, periodiccnt, 0); 931 downheap ((WT *)periodics, periodiccnt, 0);
721 } 932 }
722 else 933 else
723 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 934 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
724 935
736 { 947 {
737 struct ev_periodic *w = periodics [i]; 948 struct ev_periodic *w = periodics [i];
738 949
739 if (w->interval) 950 if (w->interval)
740 { 951 {
741 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;
742 953
743 if (fabs (diff) >= 1e-4) 954 if (fabs (diff) >= 1e-4)
744 { 955 {
745 ev_periodic_stop (EV_A_ w); 956 ev_periodic_stop (EV_A_ w);
746 ev_periodic_start (EV_A_ w); 957 ev_periodic_start (EV_A_ w);
807 { 1018 {
808 periodics_reschedule (EV_A); 1019 periodics_reschedule (EV_A);
809 1020
810 /* 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 */
811 for (i = 0; i < timercnt; ++i) 1022 for (i = 0; i < timercnt; ++i)
812 timers [i]->at += rt_now - mn_now; 1023 ((WT)timers [i])->at += rt_now - mn_now;
813 } 1024 }
814 1025
815 mn_now = rt_now; 1026 mn_now = rt_now;
816 } 1027 }
817} 1028}
843 { 1054 {
844 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
845 call_pending (EV_A); 1056 call_pending (EV_A);
846 } 1057 }
847 1058
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1061 loop_fork (EV_A);
1062
848 /* update fd-related kernel structures */ 1063 /* update fd-related kernel structures */
849 fd_reify (EV_A); 1064 fd_reify (EV_A);
850 1065
851 /* calculate blocking time */ 1066 /* calculate blocking time */
852 1067
868 { 1083 {
869 block = MAX_BLOCKTIME; 1084 block = MAX_BLOCKTIME;
870 1085
871 if (timercnt) 1086 if (timercnt)
872 { 1087 {
873 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1088 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
874 if (block > to) block = to; 1089 if (block > to) block = to;
875 } 1090 }
876 1091
877 if (periodiccnt) 1092 if (periodiccnt)
878 { 1093 {
879 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1094 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
880 if (block > to) block = to; 1095 if (block > to) block = to;
881 } 1096 }
882 1097
883 if (block < 0.) block = 0.; 1098 if (block < 0.) block = 0.;
884 } 1099 }
1001ev_timer_start (EV_P_ struct ev_timer *w) 1216ev_timer_start (EV_P_ struct ev_timer *w)
1002{ 1217{
1003 if (ev_is_active (w)) 1218 if (ev_is_active (w))
1004 return; 1219 return;
1005 1220
1006 w->at += mn_now; 1221 ((WT)w)->at += mn_now;
1007 1222
1008 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.));
1009 1224
1010 ev_start (EV_A_ (W)w, ++timercnt); 1225 ev_start (EV_A_ (W)w, ++timercnt);
1011 array_needsize (timers, timermax, timercnt, ); 1226 array_needsize (timers, timermax, timercnt, );
1012 timers [timercnt - 1] = w; 1227 timers [timercnt - 1] = w;
1013 upheap ((WT *)timers, timercnt - 1); 1228 upheap ((WT *)timers, timercnt - 1);
1229
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1014} 1231}
1015 1232
1016void 1233void
1017ev_timer_stop (EV_P_ struct ev_timer *w) 1234ev_timer_stop (EV_P_ struct ev_timer *w)
1018{ 1235{
1019 ev_clear_pending (EV_A_ (W)w); 1236 ev_clear_pending (EV_A_ (W)w);
1020 if (!ev_is_active (w)) 1237 if (!ev_is_active (w))
1021 return; 1238 return;
1022 1239
1240 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1241
1023 if (w->active < timercnt--) 1242 if (((W)w)->active < timercnt--)
1024 { 1243 {
1025 timers [w->active - 1] = timers [timercnt]; 1244 timers [((W)w)->active - 1] = timers [timercnt];
1026 downheap ((WT *)timers, timercnt, w->active - 1); 1245 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1027 } 1246 }
1028 1247
1029 w->at = w->repeat; 1248 ((WT)w)->at = w->repeat;
1030 1249
1031 ev_stop (EV_A_ (W)w); 1250 ev_stop (EV_A_ (W)w);
1032} 1251}
1033 1252
1034void 1253void
1036{ 1255{
1037 if (ev_is_active (w)) 1256 if (ev_is_active (w))
1038 { 1257 {
1039 if (w->repeat) 1258 if (w->repeat)
1040 { 1259 {
1041 w->at = mn_now + w->repeat; 1260 ((WT)w)->at = mn_now + w->repeat;
1042 downheap ((WT *)timers, timercnt, w->active - 1); 1261 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1043 } 1262 }
1044 else 1263 else
1045 ev_timer_stop (EV_A_ w); 1264 ev_timer_stop (EV_A_ w);
1046 } 1265 }
1047 else if (w->repeat) 1266 else if (w->repeat)
1056 1275
1057 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.));
1058 1277
1059 /* 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 */
1060 if (w->interval) 1279 if (w->interval)
1061 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;
1062 1281
1063 ev_start (EV_A_ (W)w, ++periodiccnt); 1282 ev_start (EV_A_ (W)w, ++periodiccnt);
1064 array_needsize (periodics, periodicmax, periodiccnt, ); 1283 array_needsize (periodics, periodicmax, periodiccnt, );
1065 periodics [periodiccnt - 1] = w; 1284 periodics [periodiccnt - 1] = w;
1066 upheap ((WT *)periodics, periodiccnt - 1); 1285 upheap ((WT *)periodics, periodiccnt - 1);
1286
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1067} 1288}
1068 1289
1069void 1290void
1070ev_periodic_stop (EV_P_ struct ev_periodic *w) 1291ev_periodic_stop (EV_P_ struct ev_periodic *w)
1071{ 1292{
1072 ev_clear_pending (EV_A_ (W)w); 1293 ev_clear_pending (EV_A_ (W)w);
1073 if (!ev_is_active (w)) 1294 if (!ev_is_active (w))
1074 return; 1295 return;
1075 1296
1297 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1298
1076 if (w->active < periodiccnt--) 1299 if (((W)w)->active < periodiccnt--)
1077 { 1300 {
1078 periodics [w->active - 1] = periodics [periodiccnt]; 1301 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1079 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1302 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1080 } 1303 }
1081 1304
1305 ev_stop (EV_A_ (W)w);
1306}
1307
1308void
1309ev_idle_start (EV_P_ struct ev_idle *w)
1310{
1311 if (ev_is_active (w))
1312 return;
1313
1314 ev_start (EV_A_ (W)w, ++idlecnt);
1315 array_needsize (idles, idlemax, idlecnt, );
1316 idles [idlecnt - 1] = w;
1317}
1318
1319void
1320ev_idle_stop (EV_P_ struct ev_idle *w)
1321{
1322 ev_clear_pending (EV_A_ (W)w);
1323 if (ev_is_active (w))
1324 return;
1325
1326 idles [((W)w)->active - 1] = idles [--idlecnt];
1327 ev_stop (EV_A_ (W)w);
1328}
1329
1330void
1331ev_prepare_start (EV_P_ struct ev_prepare *w)
1332{
1333 if (ev_is_active (w))
1334 return;
1335
1336 ev_start (EV_A_ (W)w, ++preparecnt);
1337 array_needsize (prepares, preparemax, preparecnt, );
1338 prepares [preparecnt - 1] = w;
1339}
1340
1341void
1342ev_prepare_stop (EV_P_ struct ev_prepare *w)
1343{
1344 ev_clear_pending (EV_A_ (W)w);
1345 if (ev_is_active (w))
1346 return;
1347
1348 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1349 ev_stop (EV_A_ (W)w);
1350}
1351
1352void
1353ev_check_start (EV_P_ struct ev_check *w)
1354{
1355 if (ev_is_active (w))
1356 return;
1357
1358 ev_start (EV_A_ (W)w, ++checkcnt);
1359 array_needsize (checks, checkmax, checkcnt, );
1360 checks [checkcnt - 1] = w;
1361}
1362
1363void
1364ev_check_stop (EV_P_ struct ev_check *w)
1365{
1366 ev_clear_pending (EV_A_ (W)w);
1367 if (ev_is_active (w))
1368 return;
1369
1370 checks [((W)w)->active - 1] = checks [--checkcnt];
1082 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1083} 1372}
1084 1373
1085#ifndef SA_RESTART 1374#ifndef SA_RESTART
1086# define SA_RESTART 0 1375# define SA_RESTART 0
1087#endif 1376#endif
1088 1377
1089void 1378void
1090ev_signal_start (EV_P_ struct ev_signal *w) 1379ev_signal_start (EV_P_ struct ev_signal *w)
1091{ 1380{
1381#if EV_MULTIPLICITY
1382 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1383#endif
1092 if (ev_is_active (w)) 1384 if (ev_is_active (w))
1093 return; 1385 return;
1094 1386
1095 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1387 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1096 1388
1097 ev_start (EV_A_ (W)w, 1); 1389 ev_start (EV_A_ (W)w, 1);
1098 array_needsize (signals, signalmax, w->signum, signals_init); 1390 array_needsize (signals, signalmax, w->signum, signals_init);
1099 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1100 1392
1101 if (!w->next) 1393 if (!((WL)w)->next)
1102 { 1394 {
1395#if WIN32
1396 signal (w->signum, sighandler);
1397#else
1103 struct sigaction sa; 1398 struct sigaction sa;
1104 sa.sa_handler = sighandler; 1399 sa.sa_handler = sighandler;
1105 sigfillset (&sa.sa_mask); 1400 sigfillset (&sa.sa_mask);
1106 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 */
1107 sigaction (w->signum, &sa, 0); 1402 sigaction (w->signum, &sa, 0);
1403#endif
1108 } 1404 }
1109} 1405}
1110 1406
1111void 1407void
1112ev_signal_stop (EV_P_ struct ev_signal *w) 1408ev_signal_stop (EV_P_ struct ev_signal *w)
1121 if (!signals [w->signum - 1].head) 1417 if (!signals [w->signum - 1].head)
1122 signal (w->signum, SIG_DFL); 1418 signal (w->signum, SIG_DFL);
1123} 1419}
1124 1420
1125void 1421void
1126ev_idle_start (EV_P_ struct ev_idle *w)
1127{
1128 if (ev_is_active (w))
1129 return;
1130
1131 ev_start (EV_A_ (W)w, ++idlecnt);
1132 array_needsize (idles, idlemax, idlecnt, );
1133 idles [idlecnt - 1] = w;
1134}
1135
1136void
1137ev_idle_stop (EV_P_ struct ev_idle *w)
1138{
1139 ev_clear_pending (EV_A_ (W)w);
1140 if (ev_is_active (w))
1141 return;
1142
1143 idles [w->active - 1] = idles [--idlecnt];
1144 ev_stop (EV_A_ (W)w);
1145}
1146
1147void
1148ev_prepare_start (EV_P_ struct ev_prepare *w)
1149{
1150 if (ev_is_active (w))
1151 return;
1152
1153 ev_start (EV_A_ (W)w, ++preparecnt);
1154 array_needsize (prepares, preparemax, preparecnt, );
1155 prepares [preparecnt - 1] = w;
1156}
1157
1158void
1159ev_prepare_stop (EV_P_ struct ev_prepare *w)
1160{
1161 ev_clear_pending (EV_A_ (W)w);
1162 if (ev_is_active (w))
1163 return;
1164
1165 prepares [w->active - 1] = prepares [--preparecnt];
1166 ev_stop (EV_A_ (W)w);
1167}
1168
1169void
1170ev_check_start (EV_P_ struct ev_check *w)
1171{
1172 if (ev_is_active (w))
1173 return;
1174
1175 ev_start (EV_A_ (W)w, ++checkcnt);
1176 array_needsize (checks, checkmax, checkcnt, );
1177 checks [checkcnt - 1] = w;
1178}
1179
1180void
1181ev_check_stop (EV_P_ struct ev_check *w)
1182{
1183 ev_clear_pending (EV_A_ (W)w);
1184 if (ev_is_active (w))
1185 return;
1186
1187 checks [w->active - 1] = checks [--checkcnt];
1188 ev_stop (EV_A_ (W)w);
1189}
1190
1191void
1192ev_child_start (EV_P_ struct ev_child *w) 1422ev_child_start (EV_P_ struct ev_child *w)
1193{ 1423{
1424#if EV_MULTIPLICITY
1425 assert (("child watchers are only supported in the default loop", loop == default_loop));
1426#endif
1194 if (ev_is_active (w)) 1427 if (ev_is_active (w))
1195 return; 1428 return;
1196 1429
1197 ev_start (EV_A_ (W)w, 1); 1430 ev_start (EV_A_ (W)w, 1);
1198 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1431 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1225 void (*cb)(int revents, void *arg) = once->cb; 1458 void (*cb)(int revents, void *arg) = once->cb;
1226 void *arg = once->arg; 1459 void *arg = once->arg;
1227 1460
1228 ev_io_stop (EV_A_ &once->io); 1461 ev_io_stop (EV_A_ &once->io);
1229 ev_timer_stop (EV_A_ &once->to); 1462 ev_timer_stop (EV_A_ &once->to);
1230 free (once); 1463 ev_free (once);
1231 1464
1232 cb (revents, arg); 1465 cb (revents, arg);
1233} 1466}
1234 1467
1235static void 1468static void
1245} 1478}
1246 1479
1247void 1480void
1248ev_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)
1249{ 1482{
1250 struct ev_once *once = malloc (sizeof (struct ev_once)); 1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1251 1484
1252 if (!once) 1485 if (!once)
1253 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1254 else 1487 else
1255 { 1488 {
1270 ev_timer_start (EV_A_ &once->to); 1503 ev_timer_start (EV_A_ &once->to);
1271 } 1504 }
1272 } 1505 }
1273} 1506}
1274 1507
1275/*****************************************************************************/
1276
1277#if 0
1278
1279struct ev_io wio;
1280
1281static void
1282sin_cb (struct ev_io *w, int revents)
1283{
1284 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1285}
1286
1287static void
1288ocb (struct ev_timer *w, int revents)
1289{
1290 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1291 ev_timer_stop (w);
1292 ev_timer_start (w);
1293}
1294
1295static void
1296scb (struct ev_signal *w, int revents)
1297{
1298 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1299 ev_io_stop (&wio);
1300 ev_io_start (&wio);
1301}
1302
1303static void
1304gcb (struct ev_signal *w, int revents)
1305{
1306 fprintf (stderr, "generic %x\n", revents);
1307
1308}
1309
1310int main (void)
1311{
1312 ev_init (0);
1313
1314 ev_io_init (&wio, sin_cb, 0, EV_READ);
1315 ev_io_start (&wio);
1316
1317 struct ev_timer t[10000];
1318
1319#if 0
1320 int i;
1321 for (i = 0; i < 10000; ++i)
1322 {
1323 struct ev_timer *w = t + i;
1324 ev_watcher_init (w, ocb, i);
1325 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1326 ev_timer_start (w);
1327 if (drand48 () < 0.5)
1328 ev_timer_stop (w);
1329 }
1330#endif
1331
1332 struct ev_timer t1;
1333 ev_timer_init (&t1, ocb, 5, 10);
1334 ev_timer_start (&t1);
1335
1336 struct ev_signal sig;
1337 ev_signal_init (&sig, scb, SIGQUIT);
1338 ev_signal_start (&sig);
1339
1340 struct ev_check cw;
1341 ev_check_init (&cw, gcb);
1342 ev_check_start (&cw);
1343
1344 struct ev_idle iw;
1345 ev_idle_init (&iw, gcb);
1346 ev_idle_start (&iw);
1347
1348 ev_loop (0);
1349
1350 return 0;
1351}
1352
1353#endif
1354
1355
1356
1357

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines