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

Comparing libev/ev.c (file contents):
Revision 1.52 by root, Sat Nov 3 22:10:39 2007 UTC vs.
Revision 1.66 by root, Sun Nov 4 23:30:53 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
118static ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
119static ev_tstamp rt_now;
120static int method;
121 149
122static int have_monotonic; /* runtime */ 150/*****************************************************************************/
123 151
124static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 152typedef struct
125static void (*method_modify)(EV_P_ int fd, int oev, int nev); 153{
126static void (*method_poll)(EV_P_ ev_tstamp timeout); 154 struct ev_watcher_list *head;
155 unsigned char events;
156 unsigned char reify;
157} ANFD;
127 158
128static int activecnt; /* number of active events */ 159typedef struct
160{
161 W w;
162 int events;
163} ANPENDING;
129 164
130#if EV_USE_SELECT 165#if EV_MULTIPLICITY
131static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo;
132static int vec_max;
133#endif
134 166
135#if EV_USEV_POLL 167struct ev_loop
136static struct pollfd *polls; 168{
137static int pollmax, pollcnt; 169# define VAR(name,decl) decl;
138static int *pollidxs; /* maps fds into structure indices */ 170# include "ev_vars.h"
139static int pollidxmax; 171};
140#endif 172# undef VAR
173# include "ev_wrap.h"
141 174
142#if EV_USE_EPOLL 175#else
143static int epoll_fd = -1;
144 176
145static struct epoll_event *events; 177# define VAR(name,decl) static decl;
146static int eventmax; 178# include "ev_vars.h"
147#endif 179# undef VAR
148 180
149#if EV_USE_KQUEUE
150static int kqueue_fd;
151static struct kevent *kqueue_changes;
152static int kqueue_changemax, kqueue_changecnt;
153static struct kevent *kqueue_events;
154static int kqueue_eventmax;
155#endif 181#endif
156 182
157/*****************************************************************************/ 183/*****************************************************************************/
158 184
159inline ev_tstamp 185inline ev_tstamp
206 base = realloc (base, sizeof (*base) * (newcnt)); \ 232 base = realloc (base, sizeof (*base) * (newcnt)); \
207 init (base + cur, newcnt - cur); \ 233 init (base + cur, newcnt - cur); \
208 cur = newcnt; \ 234 cur = newcnt; \
209 } 235 }
210 236
237#define array_free(stem, idx) \
238 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
239
211/*****************************************************************************/ 240/*****************************************************************************/
212
213typedef struct
214{
215 struct ev_watcher_list *head;
216 unsigned char events;
217 unsigned char reify;
218} ANFD;
219
220static ANFD *anfds;
221static int anfdmax;
222 241
223static void 242static void
224anfds_init (ANFD *base, int count) 243anfds_init (ANFD *base, int count)
225{ 244{
226 while (count--) 245 while (count--)
230 base->reify = 0; 249 base->reify = 0;
231 250
232 ++base; 251 ++base;
233 } 252 }
234} 253}
235
236typedef struct
237{
238 W w;
239 int events;
240} ANPENDING;
241
242static ANPENDING *pendings [NUMPRI];
243static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
244 254
245static void 255static void
246event (EV_P_ W w, int events) 256event (EV_P_ W w, int events)
247{ 257{
248 if (w->pending) 258 if (w->pending)
281 } 291 }
282} 292}
283 293
284/*****************************************************************************/ 294/*****************************************************************************/
285 295
286static int *fdchanges;
287static int fdchangemax, fdchangecnt;
288
289static void 296static void
290fd_reify (EV_P) 297fd_reify (EV_P)
291{ 298{
292 int i; 299 int i;
293 300
302 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 309 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
303 events |= w->events; 310 events |= w->events;
304 311
305 anfd->reify = 0; 312 anfd->reify = 0;
306 313
307 if (anfd->events != events)
308 {
309 method_modify (EV_A_ fd, anfd->events, events); 314 method_modify (EV_A_ fd, anfd->events, events);
310 anfd->events = events; 315 anfd->events = events;
311 }
312 } 316 }
313 317
314 fdchangecnt = 0; 318 fdchangecnt = 0;
315} 319}
316 320
353 357
354/* called on ENOMEM in select/poll to kill some fds and retry */ 358/* called on ENOMEM in select/poll to kill some fds and retry */
355static void 359static void
356fd_enomem (EV_P) 360fd_enomem (EV_P)
357{ 361{
358 int fd = anfdmax; 362 int fd;
359 363
360 while (fd--) 364 for (fd = anfdmax; fd--; )
361 if (anfds [fd].events) 365 if (anfds [fd].events)
362 { 366 {
363 close (fd); 367 close (fd);
364 fd_kill (EV_A_ fd); 368 fd_kill (EV_A_ fd);
365 return; 369 return;
366 } 370 }
367} 371}
368 372
373/* susually called after fork if method needs to re-arm all fds from scratch */
374static void
375fd_rearm_all (EV_P)
376{
377 int fd;
378
379 /* this should be highly optimised to not do anything but set a flag */
380 for (fd = 0; fd < anfdmax; ++fd)
381 if (anfds [fd].events)
382 {
383 anfds [fd].events = 0;
384 fd_change (EV_A_ fd);
385 }
386}
387
369/*****************************************************************************/ 388/*****************************************************************************/
370 389
371static struct ev_timer **timers;
372static int timermax, timercnt;
373
374static struct ev_periodic **periodics;
375static int periodicmax, periodiccnt;
376
377static void 390static void
378upheap (WT *timers, int k) 391upheap (WT *heap, int k)
379{ 392{
380 WT w = timers [k]; 393 WT w = heap [k];
381 394
382 while (k && timers [k >> 1]->at > w->at) 395 while (k && heap [k >> 1]->at > w->at)
383 { 396 {
384 timers [k] = timers [k >> 1]; 397 heap [k] = heap [k >> 1];
385 timers [k]->active = k + 1; 398 ((W)heap [k])->active = k + 1;
386 k >>= 1; 399 k >>= 1;
387 } 400 }
388 401
389 timers [k] = w; 402 heap [k] = w;
390 timers [k]->active = k + 1; 403 ((W)heap [k])->active = k + 1;
391 404
392} 405}
393 406
394static void 407static void
395downheap (WT *timers, int N, int k) 408downheap (WT *heap, int N, int k)
396{ 409{
397 WT w = timers [k]; 410 WT w = heap [k];
398 411
399 while (k < (N >> 1)) 412 while (k < (N >> 1))
400 { 413 {
401 int j = k << 1; 414 int j = k << 1;
402 415
403 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 416 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
404 ++j; 417 ++j;
405 418
406 if (w->at <= timers [j]->at) 419 if (w->at <= heap [j]->at)
407 break; 420 break;
408 421
409 timers [k] = timers [j]; 422 heap [k] = heap [j];
410 timers [k]->active = k + 1; 423 ((W)heap [k])->active = k + 1;
411 k = j; 424 k = j;
412 } 425 }
413 426
414 timers [k] = w; 427 heap [k] = w;
415 timers [k]->active = k + 1; 428 ((W)heap [k])->active = k + 1;
416} 429}
417 430
418/*****************************************************************************/ 431/*****************************************************************************/
419 432
420typedef struct 433typedef struct
486 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 499 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
487 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 500 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
488#endif 501#endif
489 502
490 ev_io_set (&sigev, sigpipe [0], EV_READ); 503 ev_io_set (&sigev, sigpipe [0], EV_READ);
491 ev_io_start (&sigev); 504 ev_io_start (EV_A_ &sigev);
492 ev_unref (EV_A); /* child watcher should not keep loop alive */ 505 ev_unref (EV_A); /* child watcher should not keep loop alive */
493} 506}
494 507
495/*****************************************************************************/ 508/*****************************************************************************/
496 509
497static struct ev_idle **idles; 510#ifndef WIN32
498static int idlemax, idlecnt;
499
500static struct ev_prepare **prepares;
501static int preparemax, preparecnt;
502
503static struct ev_check **checks;
504static int checkmax, checkcnt;
505
506/*****************************************************************************/
507 511
508static struct ev_child *childs [PID_HASHSIZE]; 512static struct ev_child *childs [PID_HASHSIZE];
509static struct ev_signal childev; 513static struct ev_signal childev;
510
511#ifndef WIN32
512 514
513#ifndef WCONTINUED 515#ifndef WCONTINUED
514# define WCONTINUED 0 516# define WCONTINUED 0
515#endif 517#endif
516 518
520 struct ev_child *w; 522 struct ev_child *w;
521 523
522 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 524 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
523 if (w->pid == pid || !w->pid) 525 if (w->pid == pid || !w->pid)
524 { 526 {
525 w->priority = sw->priority; /* need to do it *now* */ 527 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
526 w->rpid = pid; 528 w->rpid = pid;
527 w->rstatus = status; 529 w->rstatus = status;
528 event (EV_A_ (W)w, EV_CHILD); 530 event (EV_A_ (W)w, EV_CHILD);
529 } 531 }
530} 532}
531 533
532static void 534static void
552# include "ev_kqueue.c" 554# include "ev_kqueue.c"
553#endif 555#endif
554#if EV_USE_EPOLL 556#if EV_USE_EPOLL
555# include "ev_epoll.c" 557# include "ev_epoll.c"
556#endif 558#endif
557#if EV_USEV_POLL 559#if EV_USE_POLL
558# include "ev_poll.c" 560# include "ev_poll.c"
559#endif 561#endif
560#if EV_USE_SELECT 562#if EV_USE_SELECT
561# include "ev_select.c" 563# include "ev_select.c"
562#endif 564#endif
589ev_method (EV_P) 591ev_method (EV_P)
590{ 592{
591 return method; 593 return method;
592} 594}
593 595
594int 596static void
595ev_init (EV_P_ int methods) 597loop_init (EV_P_ int methods)
596{ 598{
597 if (!method) 599 if (!method)
598 { 600 {
599#if EV_USE_MONOTONIC 601#if EV_USE_MONOTONIC
600 { 602 {
605#endif 607#endif
606 608
607 rt_now = ev_time (); 609 rt_now = ev_time ();
608 mn_now = get_clock (); 610 mn_now = get_clock ();
609 now_floor = mn_now; 611 now_floor = mn_now;
610 diff = rt_now - mn_now; 612 rtmn_diff = rt_now - mn_now;
611
612 if (pipe (sigpipe))
613 return 0;
614 613
615 if (methods == EVMETHOD_AUTO) 614 if (methods == EVMETHOD_AUTO)
616 if (!enable_secure () && getenv ("LIBmethodS")) 615 if (!enable_secure () && getenv ("LIBEV_METHODS"))
617 methods = atoi (getenv ("LIBmethodS")); 616 methods = atoi (getenv ("LIBEV_METHODS"));
618 else 617 else
619 methods = EVMETHOD_ANY; 618 methods = EVMETHOD_ANY;
620 619
621 method = 0; 620 method = 0;
621#if EV_USE_WIN32
622 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
623#endif
622#if EV_USE_KQUEUE 624#if EV_USE_KQUEUE
623 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 625 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
624#endif 626#endif
625#if EV_USE_EPOLL 627#if EV_USE_EPOLL
626 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 628 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
627#endif 629#endif
628#if EV_USEV_POLL 630#if EV_USE_POLL
629 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 631 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
630#endif 632#endif
631#if EV_USE_SELECT 633#if EV_USE_SELECT
632 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 634 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
633#endif 635#endif
636 }
637}
634 638
639void
640loop_destroy (EV_P)
641{
642 int i;
643
644#if EV_USE_WIN32
645 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
646#endif
647#if EV_USE_KQUEUE
648 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
649#endif
650#if EV_USE_EPOLL
651 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
652#endif
653#if EV_USE_POLL
654 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
655#endif
656#if EV_USE_SELECT
657 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
658#endif
659
660 for (i = NUMPRI; i--; )
661 array_free (pending, [i]);
662
663 array_free (fdchange, );
664 array_free (timer, );
665 array_free (periodic, );
666 array_free (idle, );
667 array_free (prepare, );
668 array_free (check, );
669
670 method = 0;
671 /*TODO*/
672}
673
674void
675loop_fork (EV_P)
676{
677 /*TODO*/
678#if EV_USE_EPOLL
679 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
680#endif
681#if EV_USE_KQUEUE
682 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
683#endif
684}
685
686#if EV_MULTIPLICITY
687struct ev_loop *
688ev_loop_new (int methods)
689{
690 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
691
692 loop_init (EV_A_ methods);
693
694 if (ev_method (EV_A))
695 return loop;
696
697 return 0;
698}
699
700void
701ev_loop_destroy (EV_P)
702{
703 loop_destroy (EV_A);
704 free (loop);
705}
706
707void
708ev_loop_fork (EV_P)
709{
710 loop_fork (EV_A);
711}
712
713#endif
714
715#if EV_MULTIPLICITY
716struct ev_loop default_loop_struct;
717static struct ev_loop *default_loop;
718
719struct ev_loop *
720#else
721static int default_loop;
722
723int
724#endif
725ev_default_loop (int methods)
726{
727 if (sigpipe [0] == sigpipe [1])
728 if (pipe (sigpipe))
729 return 0;
730
731 if (!default_loop)
732 {
733#if EV_MULTIPLICITY
734 struct ev_loop *loop = default_loop = &default_loop_struct;
735#else
736 default_loop = 1;
737#endif
738
739 loop_init (EV_A_ methods);
740
635 if (method) 741 if (ev_method (EV_A))
636 { 742 {
637 ev_watcher_init (&sigev, sigcb); 743 ev_watcher_init (&sigev, sigcb);
638 ev_set_priority (&sigev, EV_MAXPRI); 744 ev_set_priority (&sigev, EV_MAXPRI);
639 siginit (EV_A); 745 siginit (EV_A);
640 746
643 ev_set_priority (&childev, EV_MAXPRI); 749 ev_set_priority (&childev, EV_MAXPRI);
644 ev_signal_start (EV_A_ &childev); 750 ev_signal_start (EV_A_ &childev);
645 ev_unref (EV_A); /* child watcher should not keep loop alive */ 751 ev_unref (EV_A); /* child watcher should not keep loop alive */
646#endif 752#endif
647 } 753 }
754 else
755 default_loop = 0;
648 } 756 }
649 757
650 return method; 758 return default_loop;
651} 759}
652 760
653/*****************************************************************************/
654
655void 761void
656ev_fork_prepare (void) 762ev_default_destroy (void)
657{ 763{
658 /* nop */ 764#if EV_MULTIPLICITY
659} 765 struct ev_loop *loop = default_loop;
660
661void
662ev_fork_parent (void)
663{
664 /* nop */
665}
666
667void
668ev_fork_child (void)
669{
670#if EV_USE_EPOLL
671 if (method == EVMETHOD_EPOLL)
672 epoll_postfork_child ();
673#endif 766#endif
674 767
768 ev_ref (EV_A); /* child watcher */
769 ev_signal_stop (EV_A_ &childev);
770
771 ev_ref (EV_A); /* signal watcher */
675 ev_io_stop (&sigev); 772 ev_io_stop (EV_A_ &sigev);
773
774 close (sigpipe [0]); sigpipe [0] = 0;
775 close (sigpipe [1]); sigpipe [1] = 0;
776
777 loop_destroy (EV_A);
778}
779
780void
781ev_default_fork (void)
782{
783#if EV_MULTIPLICITY
784 struct ev_loop *loop = default_loop;
785#endif
786
787 loop_fork (EV_A);
788
789 ev_io_stop (EV_A_ &sigev);
676 close (sigpipe [0]); 790 close (sigpipe [0]);
677 close (sigpipe [1]); 791 close (sigpipe [1]);
678 pipe (sigpipe); 792 pipe (sigpipe);
793
794 ev_ref (EV_A); /* signal watcher */
679 siginit (); 795 siginit (EV_A);
680} 796}
681 797
682/*****************************************************************************/ 798/*****************************************************************************/
683 799
684static void 800static void
700} 816}
701 817
702static void 818static void
703timers_reify (EV_P) 819timers_reify (EV_P)
704{ 820{
705 while (timercnt && timers [0]->at <= mn_now) 821 while (timercnt && ((WT)timers [0])->at <= mn_now)
706 { 822 {
707 struct ev_timer *w = timers [0]; 823 struct ev_timer *w = timers [0];
824
825 assert (("inactive timer on timer heap detected", ev_is_active (w)));
708 826
709 /* first reschedule or stop timer */ 827 /* first reschedule or stop timer */
710 if (w->repeat) 828 if (w->repeat)
711 { 829 {
712 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 830 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
713 w->at = mn_now + w->repeat; 831 ((WT)w)->at = mn_now + w->repeat;
714 downheap ((WT *)timers, timercnt, 0); 832 downheap ((WT *)timers, timercnt, 0);
715 } 833 }
716 else 834 else
717 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 835 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
718 836
719 event ((W)w, EV_TIMEOUT); 837 event (EV_A_ (W)w, EV_TIMEOUT);
720 } 838 }
721} 839}
722 840
723static void 841static void
724periodics_reify (EV_P) 842periodics_reify (EV_P)
725{ 843{
726 while (periodiccnt && periodics [0]->at <= rt_now) 844 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
727 { 845 {
728 struct ev_periodic *w = periodics [0]; 846 struct ev_periodic *w = periodics [0];
847
848 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
729 849
730 /* first reschedule or stop timer */ 850 /* first reschedule or stop timer */
731 if (w->interval) 851 if (w->interval)
732 { 852 {
733 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 853 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
734 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 854 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
735 downheap ((WT *)periodics, periodiccnt, 0); 855 downheap ((WT *)periodics, periodiccnt, 0);
736 } 856 }
737 else 857 else
738 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 858 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
739 859
740 event (EV_A_ (W)w, EV_PERIODIC); 860 event (EV_A_ (W)w, EV_PERIODIC);
741 } 861 }
742} 862}
743 863
744static void 864static void
745periodics_reschedule (EV_P_ ev_tstamp diff) 865periodics_reschedule (EV_P)
746{ 866{
747 int i; 867 int i;
748 868
749 /* adjust periodics after time jump */ 869 /* adjust periodics after time jump */
750 for (i = 0; i < periodiccnt; ++i) 870 for (i = 0; i < periodiccnt; ++i)
751 { 871 {
752 struct ev_periodic *w = periodics [i]; 872 struct ev_periodic *w = periodics [i];
753 873
754 if (w->interval) 874 if (w->interval)
755 { 875 {
756 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 876 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
757 877
758 if (fabs (diff) >= 1e-4) 878 if (fabs (diff) >= 1e-4)
759 { 879 {
760 ev_periodic_stop (EV_A_ w); 880 ev_periodic_stop (EV_A_ w);
761 ev_periodic_start (EV_A_ w); 881 ev_periodic_start (EV_A_ w);
771{ 891{
772 mn_now = get_clock (); 892 mn_now = get_clock ();
773 893
774 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 894 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
775 { 895 {
776 rt_now = mn_now + diff; 896 rt_now = rtmn_diff + mn_now;
777 return 0; 897 return 0;
778 } 898 }
779 else 899 else
780 { 900 {
781 now_floor = mn_now; 901 now_floor = mn_now;
792#if EV_USE_MONOTONIC 912#if EV_USE_MONOTONIC
793 if (expect_true (have_monotonic)) 913 if (expect_true (have_monotonic))
794 { 914 {
795 if (time_update_monotonic (EV_A)) 915 if (time_update_monotonic (EV_A))
796 { 916 {
797 ev_tstamp odiff = diff; 917 ev_tstamp odiff = rtmn_diff;
798 918
799 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 919 for (i = 4; --i; ) /* loop a few times, before making important decisions */
800 { 920 {
801 diff = rt_now - mn_now; 921 rtmn_diff = rt_now - mn_now;
802 922
803 if (fabs (odiff - diff) < MIN_TIMEJUMP) 923 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
804 return; /* all is well */ 924 return; /* all is well */
805 925
806 rt_now = ev_time (); 926 rt_now = ev_time ();
807 mn_now = get_clock (); 927 mn_now = get_clock ();
808 now_floor = mn_now; 928 now_floor = mn_now;
809 } 929 }
810 930
811 periodics_reschedule (EV_A_ diff - odiff); 931 periodics_reschedule (EV_A);
812 /* no timer adjustment, as the monotonic clock doesn't jump */ 932 /* no timer adjustment, as the monotonic clock doesn't jump */
933 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
813 } 934 }
814 } 935 }
815 else 936 else
816#endif 937#endif
817 { 938 {
818 rt_now = ev_time (); 939 rt_now = ev_time ();
819 940
820 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 941 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
821 { 942 {
822 periodics_reschedule (EV_A_ rt_now - mn_now); 943 periodics_reschedule (EV_A);
823 944
824 /* adjust timers. this is easy, as the offset is the same for all */ 945 /* adjust timers. this is easy, as the offset is the same for all */
825 for (i = 0; i < timercnt; ++i) 946 for (i = 0; i < timercnt; ++i)
826 timers [i]->at += diff; 947 ((WT)timers [i])->at += rt_now - mn_now;
827 } 948 }
828 949
829 mn_now = rt_now; 950 mn_now = rt_now;
830 } 951 }
831} 952}
882 { 1003 {
883 block = MAX_BLOCKTIME; 1004 block = MAX_BLOCKTIME;
884 1005
885 if (timercnt) 1006 if (timercnt)
886 { 1007 {
887 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1008 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
888 if (block > to) block = to; 1009 if (block > to) block = to;
889 } 1010 }
890 1011
891 if (periodiccnt) 1012 if (periodiccnt)
892 { 1013 {
893 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1014 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
894 if (block > to) block = to; 1015 if (block > to) block = to;
895 } 1016 }
896 1017
897 if (block < 0.) block = 0.; 1018 if (block < 0.) block = 0.;
898 } 1019 }
1015ev_timer_start (EV_P_ struct ev_timer *w) 1136ev_timer_start (EV_P_ struct ev_timer *w)
1016{ 1137{
1017 if (ev_is_active (w)) 1138 if (ev_is_active (w))
1018 return; 1139 return;
1019 1140
1020 w->at += mn_now; 1141 ((WT)w)->at += mn_now;
1021 1142
1022 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1143 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1023 1144
1024 ev_start (EV_A_ (W)w, ++timercnt); 1145 ev_start (EV_A_ (W)w, ++timercnt);
1025 array_needsize (timers, timermax, timercnt, ); 1146 array_needsize (timers, timermax, timercnt, );
1026 timers [timercnt - 1] = w; 1147 timers [timercnt - 1] = w;
1027 upheap ((WT *)timers, timercnt - 1); 1148 upheap ((WT *)timers, timercnt - 1);
1149
1150 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1028} 1151}
1029 1152
1030void 1153void
1031ev_timer_stop (EV_P_ struct ev_timer *w) 1154ev_timer_stop (EV_P_ struct ev_timer *w)
1032{ 1155{
1033 ev_clear_pending (EV_A_ (W)w); 1156 ev_clear_pending (EV_A_ (W)w);
1034 if (!ev_is_active (w)) 1157 if (!ev_is_active (w))
1035 return; 1158 return;
1036 1159
1160 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1161
1037 if (w->active < timercnt--) 1162 if (((W)w)->active < timercnt--)
1038 { 1163 {
1039 timers [w->active - 1] = timers [timercnt]; 1164 timers [((W)w)->active - 1] = timers [timercnt];
1040 downheap ((WT *)timers, timercnt, w->active - 1); 1165 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1041 } 1166 }
1042 1167
1043 w->at = w->repeat; 1168 ((WT)w)->at = w->repeat;
1044 1169
1045 ev_stop (EV_A_ (W)w); 1170 ev_stop (EV_A_ (W)w);
1046} 1171}
1047 1172
1048void 1173void
1050{ 1175{
1051 if (ev_is_active (w)) 1176 if (ev_is_active (w))
1052 { 1177 {
1053 if (w->repeat) 1178 if (w->repeat)
1054 { 1179 {
1055 w->at = mn_now + w->repeat; 1180 ((WT)w)->at = mn_now + w->repeat;
1056 downheap ((WT *)timers, timercnt, w->active - 1); 1181 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1057 } 1182 }
1058 else 1183 else
1059 ev_timer_stop (EV_A_ w); 1184 ev_timer_stop (EV_A_ w);
1060 } 1185 }
1061 else if (w->repeat) 1186 else if (w->repeat)
1070 1195
1071 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1196 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1072 1197
1073 /* this formula differs from the one in periodic_reify because we do not always round up */ 1198 /* this formula differs from the one in periodic_reify because we do not always round up */
1074 if (w->interval) 1199 if (w->interval)
1075 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1200 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1076 1201
1077 ev_start (EV_A_ (W)w, ++periodiccnt); 1202 ev_start (EV_A_ (W)w, ++periodiccnt);
1078 array_needsize (periodics, periodicmax, periodiccnt, ); 1203 array_needsize (periodics, periodicmax, periodiccnt, );
1079 periodics [periodiccnt - 1] = w; 1204 periodics [periodiccnt - 1] = w;
1080 upheap ((WT *)periodics, periodiccnt - 1); 1205 upheap ((WT *)periodics, periodiccnt - 1);
1206
1207 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1081} 1208}
1082 1209
1083void 1210void
1084ev_periodic_stop (EV_P_ struct ev_periodic *w) 1211ev_periodic_stop (EV_P_ struct ev_periodic *w)
1085{ 1212{
1086 ev_clear_pending (EV_A_ (W)w); 1213 ev_clear_pending (EV_A_ (W)w);
1087 if (!ev_is_active (w)) 1214 if (!ev_is_active (w))
1088 return; 1215 return;
1089 1216
1217 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1218
1090 if (w->active < periodiccnt--) 1219 if (((W)w)->active < periodiccnt--)
1091 { 1220 {
1092 periodics [w->active - 1] = periodics [periodiccnt]; 1221 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1093 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1222 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1094 } 1223 }
1095 1224
1225 ev_stop (EV_A_ (W)w);
1226}
1227
1228void
1229ev_idle_start (EV_P_ struct ev_idle *w)
1230{
1231 if (ev_is_active (w))
1232 return;
1233
1234 ev_start (EV_A_ (W)w, ++idlecnt);
1235 array_needsize (idles, idlemax, idlecnt, );
1236 idles [idlecnt - 1] = w;
1237}
1238
1239void
1240ev_idle_stop (EV_P_ struct ev_idle *w)
1241{
1242 ev_clear_pending (EV_A_ (W)w);
1243 if (ev_is_active (w))
1244 return;
1245
1246 idles [((W)w)->active - 1] = idles [--idlecnt];
1247 ev_stop (EV_A_ (W)w);
1248}
1249
1250void
1251ev_prepare_start (EV_P_ struct ev_prepare *w)
1252{
1253 if (ev_is_active (w))
1254 return;
1255
1256 ev_start (EV_A_ (W)w, ++preparecnt);
1257 array_needsize (prepares, preparemax, preparecnt, );
1258 prepares [preparecnt - 1] = w;
1259}
1260
1261void
1262ev_prepare_stop (EV_P_ struct ev_prepare *w)
1263{
1264 ev_clear_pending (EV_A_ (W)w);
1265 if (ev_is_active (w))
1266 return;
1267
1268 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1269 ev_stop (EV_A_ (W)w);
1270}
1271
1272void
1273ev_check_start (EV_P_ struct ev_check *w)
1274{
1275 if (ev_is_active (w))
1276 return;
1277
1278 ev_start (EV_A_ (W)w, ++checkcnt);
1279 array_needsize (checks, checkmax, checkcnt, );
1280 checks [checkcnt - 1] = w;
1281}
1282
1283void
1284ev_check_stop (EV_P_ struct ev_check *w)
1285{
1286 ev_clear_pending (EV_A_ (W)w);
1287 if (ev_is_active (w))
1288 return;
1289
1290 checks [((W)w)->active - 1] = checks [--checkcnt];
1096 ev_stop (EV_A_ (W)w); 1291 ev_stop (EV_A_ (W)w);
1097} 1292}
1098 1293
1099#ifndef SA_RESTART 1294#ifndef SA_RESTART
1100# define SA_RESTART 0 1295# define SA_RESTART 0
1101#endif 1296#endif
1102 1297
1103void 1298void
1104ev_signal_start (EV_P_ struct ev_signal *w) 1299ev_signal_start (EV_P_ struct ev_signal *w)
1105{ 1300{
1301#if EV_MULTIPLICITY
1302 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1303#endif
1106 if (ev_is_active (w)) 1304 if (ev_is_active (w))
1107 return; 1305 return;
1108 1306
1109 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1307 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1110 1308
1111 ev_start (EV_A_ (W)w, 1); 1309 ev_start (EV_A_ (W)w, 1);
1112 array_needsize (signals, signalmax, w->signum, signals_init); 1310 array_needsize (signals, signalmax, w->signum, signals_init);
1113 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1311 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1114 1312
1115 if (!w->next) 1313 if (!((WL)w)->next)
1116 { 1314 {
1117 struct sigaction sa; 1315 struct sigaction sa;
1118 sa.sa_handler = sighandler; 1316 sa.sa_handler = sighandler;
1119 sigfillset (&sa.sa_mask); 1317 sigfillset (&sa.sa_mask);
1120 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1318 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1135 if (!signals [w->signum - 1].head) 1333 if (!signals [w->signum - 1].head)
1136 signal (w->signum, SIG_DFL); 1334 signal (w->signum, SIG_DFL);
1137} 1335}
1138 1336
1139void 1337void
1140ev_idle_start (EV_P_ struct ev_idle *w)
1141{
1142 if (ev_is_active (w))
1143 return;
1144
1145 ev_start (EV_A_ (W)w, ++idlecnt);
1146 array_needsize (idles, idlemax, idlecnt, );
1147 idles [idlecnt - 1] = w;
1148}
1149
1150void
1151ev_idle_stop (EV_P_ struct ev_idle *w)
1152{
1153 ev_clear_pending (EV_A_ (W)w);
1154 if (ev_is_active (w))
1155 return;
1156
1157 idles [w->active - 1] = idles [--idlecnt];
1158 ev_stop (EV_A_ (W)w);
1159}
1160
1161void
1162ev_prepare_start (EV_P_ struct ev_prepare *w)
1163{
1164 if (ev_is_active (w))
1165 return;
1166
1167 ev_start (EV_A_ (W)w, ++preparecnt);
1168 array_needsize (prepares, preparemax, preparecnt, );
1169 prepares [preparecnt - 1] = w;
1170}
1171
1172void
1173ev_prepare_stop (EV_P_ struct ev_prepare *w)
1174{
1175 ev_clear_pending (EV_A_ (W)w);
1176 if (ev_is_active (w))
1177 return;
1178
1179 prepares [w->active - 1] = prepares [--preparecnt];
1180 ev_stop (EV_A_ (W)w);
1181}
1182
1183void
1184ev_check_start (EV_P_ struct ev_check *w)
1185{
1186 if (ev_is_active (w))
1187 return;
1188
1189 ev_start (EV_A_ (W)w, ++checkcnt);
1190 array_needsize (checks, checkmax, checkcnt, );
1191 checks [checkcnt - 1] = w;
1192}
1193
1194void
1195ev_check_stop (EV_P_ struct ev_check *w)
1196{
1197 ev_clear_pending (EV_A_ (W)w);
1198 if (ev_is_active (w))
1199 return;
1200
1201 checks [w->active - 1] = checks [--checkcnt];
1202 ev_stop (EV_A_ (W)w);
1203}
1204
1205void
1206ev_child_start (EV_P_ struct ev_child *w) 1338ev_child_start (EV_P_ struct ev_child *w)
1207{ 1339{
1340#if EV_MULTIPLICITY
1341 assert (("child watchers are only supported in the default loop", loop == default_loop));
1342#endif
1208 if (ev_is_active (w)) 1343 if (ev_is_active (w))
1209 return; 1344 return;
1210 1345
1211 ev_start (EV_A_ (W)w, 1); 1346 ev_start (EV_A_ (W)w, 1);
1212 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1347 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1284 ev_timer_start (EV_A_ &once->to); 1419 ev_timer_start (EV_A_ &once->to);
1285 } 1420 }
1286 } 1421 }
1287} 1422}
1288 1423
1289/*****************************************************************************/
1290
1291#if 0
1292
1293struct ev_io wio;
1294
1295static void
1296sin_cb (struct ev_io *w, int revents)
1297{
1298 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1299}
1300
1301static void
1302ocb (struct ev_timer *w, int revents)
1303{
1304 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1305 ev_timer_stop (w);
1306 ev_timer_start (w);
1307}
1308
1309static void
1310scb (struct ev_signal *w, int revents)
1311{
1312 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1313 ev_io_stop (&wio);
1314 ev_io_start (&wio);
1315}
1316
1317static void
1318gcb (struct ev_signal *w, int revents)
1319{
1320 fprintf (stderr, "generic %x\n", revents);
1321
1322}
1323
1324int main (void)
1325{
1326 ev_init (0);
1327
1328 ev_io_init (&wio, sin_cb, 0, EV_READ);
1329 ev_io_start (&wio);
1330
1331 struct ev_timer t[10000];
1332
1333#if 0
1334 int i;
1335 for (i = 0; i < 10000; ++i)
1336 {
1337 struct ev_timer *w = t + i;
1338 ev_watcher_init (w, ocb, i);
1339 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1340 ev_timer_start (w);
1341 if (drand48 () < 0.5)
1342 ev_timer_stop (w);
1343 }
1344#endif
1345
1346 struct ev_timer t1;
1347 ev_timer_init (&t1, ocb, 5, 10);
1348 ev_timer_start (&t1);
1349
1350 struct ev_signal sig;
1351 ev_signal_init (&sig, scb, SIGQUIT);
1352 ev_signal_start (&sig);
1353
1354 struct ev_check cw;
1355 ev_check_init (&cw, gcb);
1356 ev_check_start (&cw);
1357
1358 struct ev_idle iw;
1359 ev_idle_init (&iw, gcb);
1360 ev_idle_start (&iw);
1361
1362 ev_loop (0);
1363
1364 return 0;
1365}
1366
1367#endif
1368
1369
1370
1371

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines