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

Comparing libev/ev.c (file contents):
Revision 1.53 by root, Sat Nov 3 22:31:11 2007 UTC vs.
Revision 1.67 by root, Mon Nov 5 16:42:15 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
113 143
114typedef struct ev_watcher *W; 144typedef struct ev_watcher *W;
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
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
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
118/*****************************************************************************/ 156/*****************************************************************************/
119 157
120typedef struct 158typedef struct
121{ 159{
122 struct ev_watcher_list *head; 160 struct ev_watcher_list *head;
128{ 166{
129 W w; 167 W w;
130 int events; 168 int events;
131} ANPENDING; 169} ANPENDING;
132 170
133#ifdef EV_MULTIPLICITY 171#if EV_MULTIPLICITY
172
134struct ev_loop 173struct ev_loop
135{ 174{
136# define VAR(name,decl) decl 175# define VAR(name,decl) decl;
137# include "ev_vars.h" 176# include "ev_vars.h"
138}; 177};
178# undef VAR
179# include "ev_wrap.h"
180
139#else 181#else
182
140# define VAR(name,decl) static decl 183# define VAR(name,decl) static decl;
141# include "ev_vars.h" 184# include "ev_vars.h"
142#endif
143#undef VAR 185# undef VAR
186
187#endif
144 188
145/*****************************************************************************/ 189/*****************************************************************************/
146 190
147inline ev_tstamp 191inline ev_tstamp
148ev_time (void) 192ev_time (void)
194 base = realloc (base, sizeof (*base) * (newcnt)); \ 238 base = realloc (base, sizeof (*base) * (newcnt)); \
195 init (base + cur, newcnt - cur); \ 239 init (base + cur, newcnt - cur); \
196 cur = newcnt; \ 240 cur = newcnt; \
197 } 241 }
198 242
243#define array_slim(stem) \
244 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
245 { \
246 stem ## max = array_roundsize (stem ## cnt >> 1); \
247 base = realloc (base, sizeof (*base) * (stem ## max)); \
248 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
249 }
250
251#define array_free(stem, idx) \
252 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
253
199/*****************************************************************************/ 254/*****************************************************************************/
200 255
201static void 256static void
202anfds_init (ANFD *base, int count) 257anfds_init (ANFD *base, int count)
203{ 258{
268 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 323 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
269 events |= w->events; 324 events |= w->events;
270 325
271 anfd->reify = 0; 326 anfd->reify = 0;
272 327
273 if (anfd->events != events)
274 {
275 method_modify (EV_A_ fd, anfd->events, events); 328 method_modify (EV_A_ fd, anfd->events, events);
276 anfd->events = events; 329 anfd->events = events;
277 }
278 } 330 }
279 331
280 fdchangecnt = 0; 332 fdchangecnt = 0;
281} 333}
282 334
319 371
320/* called on ENOMEM in select/poll to kill some fds and retry */ 372/* called on ENOMEM in select/poll to kill some fds and retry */
321static void 373static void
322fd_enomem (EV_P) 374fd_enomem (EV_P)
323{ 375{
324 int fd = anfdmax; 376 int fd;
325 377
326 while (fd--) 378 for (fd = anfdmax; fd--; )
327 if (anfds [fd].events) 379 if (anfds [fd].events)
328 { 380 {
329 close (fd); 381 close (fd);
330 fd_kill (EV_A_ fd); 382 fd_kill (EV_A_ fd);
331 return; 383 return;
332 } 384 }
333} 385}
334 386
387/* susually called after fork if method needs to re-arm all fds from scratch */
388static void
389fd_rearm_all (EV_P)
390{
391 int fd;
392
393 /* this should be highly optimised to not do anything but set a flag */
394 for (fd = 0; fd < anfdmax; ++fd)
395 if (anfds [fd].events)
396 {
397 anfds [fd].events = 0;
398 fd_change (EV_A_ fd);
399 }
400}
401
335/*****************************************************************************/ 402/*****************************************************************************/
336 403
337static void 404static void
338upheap (WT *timers, int k) 405upheap (WT *heap, int k)
339{ 406{
340 WT w = timers [k]; 407 WT w = heap [k];
341 408
342 while (k && timers [k >> 1]->at > w->at) 409 while (k && heap [k >> 1]->at > w->at)
343 { 410 {
344 timers [k] = timers [k >> 1]; 411 heap [k] = heap [k >> 1];
345 timers [k]->active = k + 1; 412 ((W)heap [k])->active = k + 1;
346 k >>= 1; 413 k >>= 1;
347 } 414 }
348 415
349 timers [k] = w; 416 heap [k] = w;
350 timers [k]->active = k + 1; 417 ((W)heap [k])->active = k + 1;
351 418
352} 419}
353 420
354static void 421static void
355downheap (WT *timers, int N, int k) 422downheap (WT *heap, int N, int k)
356{ 423{
357 WT w = timers [k]; 424 WT w = heap [k];
358 425
359 while (k < (N >> 1)) 426 while (k < (N >> 1))
360 { 427 {
361 int j = k << 1; 428 int j = k << 1;
362 429
363 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 430 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
364 ++j; 431 ++j;
365 432
366 if (w->at <= timers [j]->at) 433 if (w->at <= heap [j]->at)
367 break; 434 break;
368 435
369 timers [k] = timers [j]; 436 heap [k] = heap [j];
370 timers [k]->active = k + 1; 437 ((W)heap [k])->active = k + 1;
371 k = j; 438 k = j;
372 } 439 }
373 440
374 timers [k] = w; 441 heap [k] = w;
375 timers [k]->active = k + 1; 442 ((W)heap [k])->active = k + 1;
376} 443}
377 444
378/*****************************************************************************/ 445/*****************************************************************************/
379 446
380typedef struct 447typedef struct
386static ANSIG *signals; 453static ANSIG *signals;
387static int signalmax; 454static int signalmax;
388 455
389static int sigpipe [2]; 456static int sigpipe [2];
390static sig_atomic_t volatile gotsig; 457static sig_atomic_t volatile gotsig;
458static struct ev_io sigev;
391 459
392static void 460static void
393signals_init (ANSIG *base, int count) 461signals_init (ANSIG *base, int count)
394{ 462{
395 while (count--) 463 while (count--)
402} 470}
403 471
404static void 472static void
405sighandler (int signum) 473sighandler (int signum)
406{ 474{
475#if WIN32
476 signal (signum, sighandler);
477#endif
478
407 signals [signum - 1].gotsig = 1; 479 signals [signum - 1].gotsig = 1;
408 480
409 if (!gotsig) 481 if (!gotsig)
410 { 482 {
411 int old_errno = errno; 483 int old_errno = errno;
445 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 517 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
446 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 518 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
447#endif 519#endif
448 520
449 ev_io_set (&sigev, sigpipe [0], EV_READ); 521 ev_io_set (&sigev, sigpipe [0], EV_READ);
450 ev_io_start (&sigev); 522 ev_io_start (EV_A_ &sigev);
451 ev_unref (EV_A); /* child watcher should not keep loop alive */ 523 ev_unref (EV_A); /* child watcher should not keep loop alive */
452} 524}
453 525
454/*****************************************************************************/ 526/*****************************************************************************/
455 527
456#ifndef WIN32 528#ifndef WIN32
529
530static struct ev_child *childs [PID_HASHSIZE];
531static struct ev_signal childev;
457 532
458#ifndef WCONTINUED 533#ifndef WCONTINUED
459# define WCONTINUED 0 534# define WCONTINUED 0
460#endif 535#endif
461 536
465 struct ev_child *w; 540 struct ev_child *w;
466 541
467 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 542 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
468 if (w->pid == pid || !w->pid) 543 if (w->pid == pid || !w->pid)
469 { 544 {
470 w->priority = sw->priority; /* need to do it *now* */ 545 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
471 w->rpid = pid; 546 w->rpid = pid;
472 w->rstatus = status; 547 w->rstatus = status;
473 event (EV_A_ (W)w, EV_CHILD); 548 event (EV_A_ (W)w, EV_CHILD);
474 } 549 }
475} 550}
476 551
477static void 552static void
497# include "ev_kqueue.c" 572# include "ev_kqueue.c"
498#endif 573#endif
499#if EV_USE_EPOLL 574#if EV_USE_EPOLL
500# include "ev_epoll.c" 575# include "ev_epoll.c"
501#endif 576#endif
502#if EV_USEV_POLL 577#if EV_USE_POLL
503# include "ev_poll.c" 578# include "ev_poll.c"
504#endif 579#endif
505#if EV_USE_SELECT 580#if EV_USE_SELECT
506# include "ev_select.c" 581# include "ev_select.c"
507#endif 582#endif
534ev_method (EV_P) 609ev_method (EV_P)
535{ 610{
536 return method; 611 return method;
537} 612}
538 613
539int 614static void
540ev_init (EV_P_ int methods) 615loop_init (EV_P_ int methods)
541{ 616{
542#ifdef EV_MULTIPLICITY
543 memset (loop, 0, sizeof (struct ev_loop));
544#endif
545
546 if (!method) 617 if (!method)
547 { 618 {
548#if EV_USE_MONOTONIC 619#if EV_USE_MONOTONIC
549 { 620 {
550 struct timespec ts; 621 struct timespec ts;
554#endif 625#endif
555 626
556 rt_now = ev_time (); 627 rt_now = ev_time ();
557 mn_now = get_clock (); 628 mn_now = get_clock ();
558 now_floor = mn_now; 629 now_floor = mn_now;
559 diff = rt_now - mn_now; 630 rtmn_diff = rt_now - mn_now;
560
561 if (pipe (sigpipe))
562 return 0;
563 631
564 if (methods == EVMETHOD_AUTO) 632 if (methods == EVMETHOD_AUTO)
565 if (!enable_secure () && getenv ("LIBmethodS")) 633 if (!enable_secure () && getenv ("LIBEV_METHODS"))
566 methods = atoi (getenv ("LIBmethodS")); 634 methods = atoi (getenv ("LIBEV_METHODS"));
567 else 635 else
568 methods = EVMETHOD_ANY; 636 methods = EVMETHOD_ANY;
569 637
570 method = 0; 638 method = 0;
639#if EV_USE_WIN32
640 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
641#endif
571#if EV_USE_KQUEUE 642#if EV_USE_KQUEUE
572 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 643 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
573#endif 644#endif
574#if EV_USE_EPOLL 645#if EV_USE_EPOLL
575 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 646 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
576#endif 647#endif
577#if EV_USEV_POLL 648#if EV_USE_POLL
578 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 649 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
579#endif 650#endif
580#if EV_USE_SELECT 651#if EV_USE_SELECT
581 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 652 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
582#endif 653#endif
654 }
655}
583 656
657void
658loop_destroy (EV_P)
659{
660 int i;
661
662#if EV_USE_WIN32
663 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
664#endif
665#if EV_USE_KQUEUE
666 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
667#endif
668#if EV_USE_EPOLL
669 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
670#endif
671#if EV_USE_POLL
672 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
673#endif
674#if EV_USE_SELECT
675 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
676#endif
677
678 for (i = NUMPRI; i--; )
679 array_free (pending, [i]);
680
681 array_free (fdchange, );
682 array_free (timer, );
683 array_free (periodic, );
684 array_free (idle, );
685 array_free (prepare, );
686 array_free (check, );
687
688 method = 0;
689 /*TODO*/
690}
691
692void
693loop_fork (EV_P)
694{
695 /*TODO*/
696#if EV_USE_EPOLL
697 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
698#endif
699#if EV_USE_KQUEUE
700 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
701#endif
702}
703
704#if EV_MULTIPLICITY
705struct ev_loop *
706ev_loop_new (int methods)
707{
708 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
709
710 loop_init (EV_A_ methods);
711
712 if (ev_method (EV_A))
713 return loop;
714
715 return 0;
716}
717
718void
719ev_loop_destroy (EV_P)
720{
721 loop_destroy (EV_A);
722 free (loop);
723}
724
725void
726ev_loop_fork (EV_P)
727{
728 loop_fork (EV_A);
729}
730
731#endif
732
733#if EV_MULTIPLICITY
734struct ev_loop default_loop_struct;
735static struct ev_loop *default_loop;
736
737struct ev_loop *
738#else
739static int default_loop;
740
741int
742#endif
743ev_default_loop (int methods)
744{
745 if (sigpipe [0] == sigpipe [1])
746 if (pipe (sigpipe))
747 return 0;
748
749 if (!default_loop)
750 {
751#if EV_MULTIPLICITY
752 struct ev_loop *loop = default_loop = &default_loop_struct;
753#else
754 default_loop = 1;
755#endif
756
757 loop_init (EV_A_ methods);
758
584 if (method) 759 if (ev_method (EV_A))
585 { 760 {
586 ev_watcher_init (&sigev, sigcb); 761 ev_watcher_init (&sigev, sigcb);
587 ev_set_priority (&sigev, EV_MAXPRI); 762 ev_set_priority (&sigev, EV_MAXPRI);
588 siginit (EV_A); 763 siginit (EV_A);
589 764
592 ev_set_priority (&childev, EV_MAXPRI); 767 ev_set_priority (&childev, EV_MAXPRI);
593 ev_signal_start (EV_A_ &childev); 768 ev_signal_start (EV_A_ &childev);
594 ev_unref (EV_A); /* child watcher should not keep loop alive */ 769 ev_unref (EV_A); /* child watcher should not keep loop alive */
595#endif 770#endif
596 } 771 }
772 else
773 default_loop = 0;
597 } 774 }
598 775
599 return method; 776 return default_loop;
600} 777}
601 778
602/*****************************************************************************/
603
604void 779void
605ev_fork_prepare (void) 780ev_default_destroy (void)
606{ 781{
607 /* nop */ 782#if EV_MULTIPLICITY
608} 783 struct ev_loop *loop = default_loop;
609
610void
611ev_fork_parent (void)
612{
613 /* nop */
614}
615
616void
617ev_fork_child (void)
618{
619#if EV_USE_EPOLL
620 if (method == EVMETHOD_EPOLL)
621 epoll_postfork_child ();
622#endif 784#endif
623 785
786 ev_ref (EV_A); /* child watcher */
787 ev_signal_stop (EV_A_ &childev);
788
789 ev_ref (EV_A); /* signal watcher */
624 ev_io_stop (&sigev); 790 ev_io_stop (EV_A_ &sigev);
791
792 close (sigpipe [0]); sigpipe [0] = 0;
793 close (sigpipe [1]); sigpipe [1] = 0;
794
795 loop_destroy (EV_A);
796}
797
798void
799ev_default_fork (void)
800{
801#if EV_MULTIPLICITY
802 struct ev_loop *loop = default_loop;
803#endif
804
805 loop_fork (EV_A);
806
807 ev_io_stop (EV_A_ &sigev);
625 close (sigpipe [0]); 808 close (sigpipe [0]);
626 close (sigpipe [1]); 809 close (sigpipe [1]);
627 pipe (sigpipe); 810 pipe (sigpipe);
811
812 ev_ref (EV_A); /* signal watcher */
628 siginit (); 813 siginit (EV_A);
629} 814}
630 815
631/*****************************************************************************/ 816/*****************************************************************************/
632 817
633static void 818static void
649} 834}
650 835
651static void 836static void
652timers_reify (EV_P) 837timers_reify (EV_P)
653{ 838{
654 while (timercnt && timers [0]->at <= mn_now) 839 while (timercnt && ((WT)timers [0])->at <= mn_now)
655 { 840 {
656 struct ev_timer *w = timers [0]; 841 struct ev_timer *w = timers [0];
842
843 assert (("inactive timer on timer heap detected", ev_is_active (w)));
657 844
658 /* first reschedule or stop timer */ 845 /* first reschedule or stop timer */
659 if (w->repeat) 846 if (w->repeat)
660 { 847 {
661 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 848 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
662 w->at = mn_now + w->repeat; 849 ((WT)w)->at = mn_now + w->repeat;
663 downheap ((WT *)timers, timercnt, 0); 850 downheap ((WT *)timers, timercnt, 0);
664 } 851 }
665 else 852 else
666 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 853 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
667 854
668 event ((W)w, EV_TIMEOUT); 855 event (EV_A_ (W)w, EV_TIMEOUT);
669 } 856 }
670} 857}
671 858
672static void 859static void
673periodics_reify (EV_P) 860periodics_reify (EV_P)
674{ 861{
675 while (periodiccnt && periodics [0]->at <= rt_now) 862 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
676 { 863 {
677 struct ev_periodic *w = periodics [0]; 864 struct ev_periodic *w = periodics [0];
865
866 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
678 867
679 /* first reschedule or stop timer */ 868 /* first reschedule or stop timer */
680 if (w->interval) 869 if (w->interval)
681 { 870 {
682 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 871 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
683 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 872 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
684 downheap ((WT *)periodics, periodiccnt, 0); 873 downheap ((WT *)periodics, periodiccnt, 0);
685 } 874 }
686 else 875 else
687 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 876 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
688 877
689 event (EV_A_ (W)w, EV_PERIODIC); 878 event (EV_A_ (W)w, EV_PERIODIC);
690 } 879 }
691} 880}
692 881
693static void 882static void
694periodics_reschedule (EV_P_ ev_tstamp diff) 883periodics_reschedule (EV_P)
695{ 884{
696 int i; 885 int i;
697 886
698 /* adjust periodics after time jump */ 887 /* adjust periodics after time jump */
699 for (i = 0; i < periodiccnt; ++i) 888 for (i = 0; i < periodiccnt; ++i)
700 { 889 {
701 struct ev_periodic *w = periodics [i]; 890 struct ev_periodic *w = periodics [i];
702 891
703 if (w->interval) 892 if (w->interval)
704 { 893 {
705 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 894 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
706 895
707 if (fabs (diff) >= 1e-4) 896 if (fabs (diff) >= 1e-4)
708 { 897 {
709 ev_periodic_stop (EV_A_ w); 898 ev_periodic_stop (EV_A_ w);
710 ev_periodic_start (EV_A_ w); 899 ev_periodic_start (EV_A_ w);
720{ 909{
721 mn_now = get_clock (); 910 mn_now = get_clock ();
722 911
723 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 912 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
724 { 913 {
725 rt_now = mn_now + diff; 914 rt_now = rtmn_diff + mn_now;
726 return 0; 915 return 0;
727 } 916 }
728 else 917 else
729 { 918 {
730 now_floor = mn_now; 919 now_floor = mn_now;
741#if EV_USE_MONOTONIC 930#if EV_USE_MONOTONIC
742 if (expect_true (have_monotonic)) 931 if (expect_true (have_monotonic))
743 { 932 {
744 if (time_update_monotonic (EV_A)) 933 if (time_update_monotonic (EV_A))
745 { 934 {
746 ev_tstamp odiff = diff; 935 ev_tstamp odiff = rtmn_diff;
747 936
748 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 937 for (i = 4; --i; ) /* loop a few times, before making important decisions */
749 { 938 {
750 diff = rt_now - mn_now; 939 rtmn_diff = rt_now - mn_now;
751 940
752 if (fabs (odiff - diff) < MIN_TIMEJUMP) 941 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
753 return; /* all is well */ 942 return; /* all is well */
754 943
755 rt_now = ev_time (); 944 rt_now = ev_time ();
756 mn_now = get_clock (); 945 mn_now = get_clock ();
757 now_floor = mn_now; 946 now_floor = mn_now;
758 } 947 }
759 948
760 periodics_reschedule (EV_A_ diff - odiff); 949 periodics_reschedule (EV_A);
761 /* no timer adjustment, as the monotonic clock doesn't jump */ 950 /* no timer adjustment, as the monotonic clock doesn't jump */
951 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
762 } 952 }
763 } 953 }
764 else 954 else
765#endif 955#endif
766 { 956 {
767 rt_now = ev_time (); 957 rt_now = ev_time ();
768 958
769 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 959 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
770 { 960 {
771 periodics_reschedule (EV_A_ rt_now - mn_now); 961 periodics_reschedule (EV_A);
772 962
773 /* adjust timers. this is easy, as the offset is the same for all */ 963 /* adjust timers. this is easy, as the offset is the same for all */
774 for (i = 0; i < timercnt; ++i) 964 for (i = 0; i < timercnt; ++i)
775 timers [i]->at += diff; 965 ((WT)timers [i])->at += rt_now - mn_now;
776 } 966 }
777 967
778 mn_now = rt_now; 968 mn_now = rt_now;
779 } 969 }
780} 970}
831 { 1021 {
832 block = MAX_BLOCKTIME; 1022 block = MAX_BLOCKTIME;
833 1023
834 if (timercnt) 1024 if (timercnt)
835 { 1025 {
836 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1026 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
837 if (block > to) block = to; 1027 if (block > to) block = to;
838 } 1028 }
839 1029
840 if (periodiccnt) 1030 if (periodiccnt)
841 { 1031 {
842 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1032 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
843 if (block > to) block = to; 1033 if (block > to) block = to;
844 } 1034 }
845 1035
846 if (block < 0.) block = 0.; 1036 if (block < 0.) block = 0.;
847 } 1037 }
964ev_timer_start (EV_P_ struct ev_timer *w) 1154ev_timer_start (EV_P_ struct ev_timer *w)
965{ 1155{
966 if (ev_is_active (w)) 1156 if (ev_is_active (w))
967 return; 1157 return;
968 1158
969 w->at += mn_now; 1159 ((WT)w)->at += mn_now;
970 1160
971 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1161 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
972 1162
973 ev_start (EV_A_ (W)w, ++timercnt); 1163 ev_start (EV_A_ (W)w, ++timercnt);
974 array_needsize (timers, timermax, timercnt, ); 1164 array_needsize (timers, timermax, timercnt, );
975 timers [timercnt - 1] = w; 1165 timers [timercnt - 1] = w;
976 upheap ((WT *)timers, timercnt - 1); 1166 upheap ((WT *)timers, timercnt - 1);
1167
1168 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
977} 1169}
978 1170
979void 1171void
980ev_timer_stop (EV_P_ struct ev_timer *w) 1172ev_timer_stop (EV_P_ struct ev_timer *w)
981{ 1173{
982 ev_clear_pending (EV_A_ (W)w); 1174 ev_clear_pending (EV_A_ (W)w);
983 if (!ev_is_active (w)) 1175 if (!ev_is_active (w))
984 return; 1176 return;
985 1177
1178 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1179
986 if (w->active < timercnt--) 1180 if (((W)w)->active < timercnt--)
987 { 1181 {
988 timers [w->active - 1] = timers [timercnt]; 1182 timers [((W)w)->active - 1] = timers [timercnt];
989 downheap ((WT *)timers, timercnt, w->active - 1); 1183 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
990 } 1184 }
991 1185
992 w->at = w->repeat; 1186 ((WT)w)->at = w->repeat;
993 1187
994 ev_stop (EV_A_ (W)w); 1188 ev_stop (EV_A_ (W)w);
995} 1189}
996 1190
997void 1191void
999{ 1193{
1000 if (ev_is_active (w)) 1194 if (ev_is_active (w))
1001 { 1195 {
1002 if (w->repeat) 1196 if (w->repeat)
1003 { 1197 {
1004 w->at = mn_now + w->repeat; 1198 ((WT)w)->at = mn_now + w->repeat;
1005 downheap ((WT *)timers, timercnt, w->active - 1); 1199 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1006 } 1200 }
1007 else 1201 else
1008 ev_timer_stop (EV_A_ w); 1202 ev_timer_stop (EV_A_ w);
1009 } 1203 }
1010 else if (w->repeat) 1204 else if (w->repeat)
1019 1213
1020 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1214 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1021 1215
1022 /* this formula differs from the one in periodic_reify because we do not always round up */ 1216 /* this formula differs from the one in periodic_reify because we do not always round up */
1023 if (w->interval) 1217 if (w->interval)
1024 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1218 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1025 1219
1026 ev_start (EV_A_ (W)w, ++periodiccnt); 1220 ev_start (EV_A_ (W)w, ++periodiccnt);
1027 array_needsize (periodics, periodicmax, periodiccnt, ); 1221 array_needsize (periodics, periodicmax, periodiccnt, );
1028 periodics [periodiccnt - 1] = w; 1222 periodics [periodiccnt - 1] = w;
1029 upheap ((WT *)periodics, periodiccnt - 1); 1223 upheap ((WT *)periodics, periodiccnt - 1);
1224
1225 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1030} 1226}
1031 1227
1032void 1228void
1033ev_periodic_stop (EV_P_ struct ev_periodic *w) 1229ev_periodic_stop (EV_P_ struct ev_periodic *w)
1034{ 1230{
1035 ev_clear_pending (EV_A_ (W)w); 1231 ev_clear_pending (EV_A_ (W)w);
1036 if (!ev_is_active (w)) 1232 if (!ev_is_active (w))
1037 return; 1233 return;
1038 1234
1235 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1236
1039 if (w->active < periodiccnt--) 1237 if (((W)w)->active < periodiccnt--)
1040 { 1238 {
1041 periodics [w->active - 1] = periodics [periodiccnt]; 1239 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1042 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1240 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1043 } 1241 }
1044 1242
1243 ev_stop (EV_A_ (W)w);
1244}
1245
1246void
1247ev_idle_start (EV_P_ struct ev_idle *w)
1248{
1249 if (ev_is_active (w))
1250 return;
1251
1252 ev_start (EV_A_ (W)w, ++idlecnt);
1253 array_needsize (idles, idlemax, idlecnt, );
1254 idles [idlecnt - 1] = w;
1255}
1256
1257void
1258ev_idle_stop (EV_P_ struct ev_idle *w)
1259{
1260 ev_clear_pending (EV_A_ (W)w);
1261 if (ev_is_active (w))
1262 return;
1263
1264 idles [((W)w)->active - 1] = idles [--idlecnt];
1265 ev_stop (EV_A_ (W)w);
1266}
1267
1268void
1269ev_prepare_start (EV_P_ struct ev_prepare *w)
1270{
1271 if (ev_is_active (w))
1272 return;
1273
1274 ev_start (EV_A_ (W)w, ++preparecnt);
1275 array_needsize (prepares, preparemax, preparecnt, );
1276 prepares [preparecnt - 1] = w;
1277}
1278
1279void
1280ev_prepare_stop (EV_P_ struct ev_prepare *w)
1281{
1282 ev_clear_pending (EV_A_ (W)w);
1283 if (ev_is_active (w))
1284 return;
1285
1286 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1287 ev_stop (EV_A_ (W)w);
1288}
1289
1290void
1291ev_check_start (EV_P_ struct ev_check *w)
1292{
1293 if (ev_is_active (w))
1294 return;
1295
1296 ev_start (EV_A_ (W)w, ++checkcnt);
1297 array_needsize (checks, checkmax, checkcnt, );
1298 checks [checkcnt - 1] = w;
1299}
1300
1301void
1302ev_check_stop (EV_P_ struct ev_check *w)
1303{
1304 ev_clear_pending (EV_A_ (W)w);
1305 if (ev_is_active (w))
1306 return;
1307
1308 checks [((W)w)->active - 1] = checks [--checkcnt];
1045 ev_stop (EV_A_ (W)w); 1309 ev_stop (EV_A_ (W)w);
1046} 1310}
1047 1311
1048#ifndef SA_RESTART 1312#ifndef SA_RESTART
1049# define SA_RESTART 0 1313# define SA_RESTART 0
1050#endif 1314#endif
1051 1315
1052void 1316void
1053ev_signal_start (EV_P_ struct ev_signal *w) 1317ev_signal_start (EV_P_ struct ev_signal *w)
1054{ 1318{
1319#if EV_MULTIPLICITY
1320 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1321#endif
1055 if (ev_is_active (w)) 1322 if (ev_is_active (w))
1056 return; 1323 return;
1057 1324
1058 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1325 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1059 1326
1060 ev_start (EV_A_ (W)w, 1); 1327 ev_start (EV_A_ (W)w, 1);
1061 array_needsize (signals, signalmax, w->signum, signals_init); 1328 array_needsize (signals, signalmax, w->signum, signals_init);
1062 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1329 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1063 1330
1064 if (!w->next) 1331 if (!((WL)w)->next)
1065 { 1332 {
1333#if WIN32
1334 signal (w->signum, sighandler);
1335#else
1066 struct sigaction sa; 1336 struct sigaction sa;
1067 sa.sa_handler = sighandler; 1337 sa.sa_handler = sighandler;
1068 sigfillset (&sa.sa_mask); 1338 sigfillset (&sa.sa_mask);
1069 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1339 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1070 sigaction (w->signum, &sa, 0); 1340 sigaction (w->signum, &sa, 0);
1341#endif
1071 } 1342 }
1072} 1343}
1073 1344
1074void 1345void
1075ev_signal_stop (EV_P_ struct ev_signal *w) 1346ev_signal_stop (EV_P_ struct ev_signal *w)
1084 if (!signals [w->signum - 1].head) 1355 if (!signals [w->signum - 1].head)
1085 signal (w->signum, SIG_DFL); 1356 signal (w->signum, SIG_DFL);
1086} 1357}
1087 1358
1088void 1359void
1089ev_idle_start (EV_P_ struct ev_idle *w)
1090{
1091 if (ev_is_active (w))
1092 return;
1093
1094 ev_start (EV_A_ (W)w, ++idlecnt);
1095 array_needsize (idles, idlemax, idlecnt, );
1096 idles [idlecnt - 1] = w;
1097}
1098
1099void
1100ev_idle_stop (EV_P_ struct ev_idle *w)
1101{
1102 ev_clear_pending (EV_A_ (W)w);
1103 if (ev_is_active (w))
1104 return;
1105
1106 idles [w->active - 1] = idles [--idlecnt];
1107 ev_stop (EV_A_ (W)w);
1108}
1109
1110void
1111ev_prepare_start (EV_P_ struct ev_prepare *w)
1112{
1113 if (ev_is_active (w))
1114 return;
1115
1116 ev_start (EV_A_ (W)w, ++preparecnt);
1117 array_needsize (prepares, preparemax, preparecnt, );
1118 prepares [preparecnt - 1] = w;
1119}
1120
1121void
1122ev_prepare_stop (EV_P_ struct ev_prepare *w)
1123{
1124 ev_clear_pending (EV_A_ (W)w);
1125 if (ev_is_active (w))
1126 return;
1127
1128 prepares [w->active - 1] = prepares [--preparecnt];
1129 ev_stop (EV_A_ (W)w);
1130}
1131
1132void
1133ev_check_start (EV_P_ struct ev_check *w)
1134{
1135 if (ev_is_active (w))
1136 return;
1137
1138 ev_start (EV_A_ (W)w, ++checkcnt);
1139 array_needsize (checks, checkmax, checkcnt, );
1140 checks [checkcnt - 1] = w;
1141}
1142
1143void
1144ev_check_stop (EV_P_ struct ev_check *w)
1145{
1146 ev_clear_pending (EV_A_ (W)w);
1147 if (ev_is_active (w))
1148 return;
1149
1150 checks [w->active - 1] = checks [--checkcnt];
1151 ev_stop (EV_A_ (W)w);
1152}
1153
1154void
1155ev_child_start (EV_P_ struct ev_child *w) 1360ev_child_start (EV_P_ struct ev_child *w)
1156{ 1361{
1362#if EV_MULTIPLICITY
1363 assert (("child watchers are only supported in the default loop", loop == default_loop));
1364#endif
1157 if (ev_is_active (w)) 1365 if (ev_is_active (w))
1158 return; 1366 return;
1159 1367
1160 ev_start (EV_A_ (W)w, 1); 1368 ev_start (EV_A_ (W)w, 1);
1161 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1369 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1233 ev_timer_start (EV_A_ &once->to); 1441 ev_timer_start (EV_A_ &once->to);
1234 } 1442 }
1235 } 1443 }
1236} 1444}
1237 1445
1238/*****************************************************************************/
1239
1240#if 0
1241
1242struct ev_io wio;
1243
1244static void
1245sin_cb (struct ev_io *w, int revents)
1246{
1247 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1248}
1249
1250static void
1251ocb (struct ev_timer *w, int revents)
1252{
1253 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1254 ev_timer_stop (w);
1255 ev_timer_start (w);
1256}
1257
1258static void
1259scb (struct ev_signal *w, int revents)
1260{
1261 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1262 ev_io_stop (&wio);
1263 ev_io_start (&wio);
1264}
1265
1266static void
1267gcb (struct ev_signal *w, int revents)
1268{
1269 fprintf (stderr, "generic %x\n", revents);
1270
1271}
1272
1273int main (void)
1274{
1275 ev_init (0);
1276
1277 ev_io_init (&wio, sin_cb, 0, EV_READ);
1278 ev_io_start (&wio);
1279
1280 struct ev_timer t[10000];
1281
1282#if 0
1283 int i;
1284 for (i = 0; i < 10000; ++i)
1285 {
1286 struct ev_timer *w = t + i;
1287 ev_watcher_init (w, ocb, i);
1288 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1289 ev_timer_start (w);
1290 if (drand48 () < 0.5)
1291 ev_timer_stop (w);
1292 }
1293#endif
1294
1295 struct ev_timer t1;
1296 ev_timer_init (&t1, ocb, 5, 10);
1297 ev_timer_start (&t1);
1298
1299 struct ev_signal sig;
1300 ev_signal_init (&sig, scb, SIGQUIT);
1301 ev_signal_start (&sig);
1302
1303 struct ev_check cw;
1304 ev_check_init (&cw, gcb);
1305 ev_check_start (&cw);
1306
1307 struct ev_idle iw;
1308 ev_idle_init (&iw, gcb);
1309 ev_idle_start (&iw);
1310
1311 ev_loop (0);
1312
1313 return 0;
1314}
1315
1316#endif
1317
1318
1319
1320

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines