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

Comparing libev/ev.c (file contents):
Revision 1.51 by root, Sat Nov 3 21:58:51 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
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#if WIN32
123 151/* note: the comment below could not be substantiated, but what would I care */
124static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 152/* MSDN says this is required to handle SIGFPE */
125static void (*method_modify)(EV_P_ int fd, int oev, int nev); 153volatile double SIGFPE_REQ = 0.0f;
126static void (*method_poll)(EV_P_ ev_tstamp timeout);
127
128static int activecnt; /* number of active events */
129
130#if EV_USE_SELECT
131static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo;
132static int vec_max;
133#endif 154#endif
134 155
135#if EV_USEV_POLL 156/*****************************************************************************/
136static struct pollfd *polls;
137static int pollmax, pollcnt;
138static int *pollidxs; /* maps fds into structure indices */
139static int pollidxmax;
140#endif
141 157
142#if EV_USE_EPOLL 158typedef struct
143static int epoll_fd = -1; 159{
160 struct ev_watcher_list *head;
161 unsigned char events;
162 unsigned char reify;
163} ANFD;
144 164
145static struct epoll_event *events; 165typedef struct
146static int eventmax; 166{
147#endif 167 W w;
168 int events;
169} ANPENDING;
148 170
149#if EV_USE_KQUEUE 171#if EV_MULTIPLICITY
150static int kqueue_fd; 172
151static struct kevent *kqueue_changes; 173struct ev_loop
152static int kqueue_changemax, kqueue_changecnt; 174{
153static struct kevent *kqueue_events; 175# define VAR(name,decl) decl;
154static int kqueue_eventmax; 176# include "ev_vars.h"
177};
178# undef VAR
179# include "ev_wrap.h"
180
181#else
182
183# define VAR(name,decl) static decl;
184# include "ev_vars.h"
185# undef VAR
186
155#endif 187#endif
156 188
157/*****************************************************************************/ 189/*****************************************************************************/
158 190
159inline ev_tstamp 191inline ev_tstamp
206 base = realloc (base, sizeof (*base) * (newcnt)); \ 238 base = realloc (base, sizeof (*base) * (newcnt)); \
207 init (base + cur, newcnt - cur); \ 239 init (base + cur, newcnt - cur); \
208 cur = newcnt; \ 240 cur = newcnt; \
209 } 241 }
210 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
211/*****************************************************************************/ 254/*****************************************************************************/
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 255
223static void 256static void
224anfds_init (ANFD *base, int count) 257anfds_init (ANFD *base, int count)
225{ 258{
226 while (count--) 259 while (count--)
230 base->reify = 0; 263 base->reify = 0;
231 264
232 ++base; 265 ++base;
233 } 266 }
234} 267}
235
236typedef struct
237{
238 W w;
239 int events;
240} ANPENDING;
241
242static ANPENDING *pendings [NUMPRI];
243static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
244 268
245static void 269static void
246event (EV_P_ W w, int events) 270event (EV_P_ W w, int events)
247{ 271{
248 if (w->pending) 272 if (w->pending)
281 } 305 }
282} 306}
283 307
284/*****************************************************************************/ 308/*****************************************************************************/
285 309
286static int *fdchanges;
287static int fdchangemax, fdchangecnt;
288
289static void 310static void
290fd_reify (EV_P) 311fd_reify (EV_P)
291{ 312{
292 int i; 313 int i;
293 314
302 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)
303 events |= w->events; 324 events |= w->events;
304 325
305 anfd->reify = 0; 326 anfd->reify = 0;
306 327
307 if (anfd->events != events)
308 {
309 method_modify (EV_A_ fd, anfd->events, events); 328 method_modify (EV_A_ fd, anfd->events, events);
310 anfd->events = events; 329 anfd->events = events;
311 }
312 } 330 }
313 331
314 fdchangecnt = 0; 332 fdchangecnt = 0;
315} 333}
316 334
353 371
354/* 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 */
355static void 373static void
356fd_enomem (EV_P) 374fd_enomem (EV_P)
357{ 375{
358 int fd = anfdmax; 376 int fd;
359 377
360 while (fd--) 378 for (fd = anfdmax; fd--; )
361 if (anfds [fd].events) 379 if (anfds [fd].events)
362 { 380 {
363 close (fd); 381 close (fd);
364 fd_kill (EV_A_ fd); 382 fd_kill (EV_A_ fd);
365 return; 383 return;
366 } 384 }
367} 385}
368 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
369/*****************************************************************************/ 402/*****************************************************************************/
370 403
371static struct ev_timer **timers;
372static int timermax, timercnt;
373
374static struct ev_periodic **periodics;
375static int periodicmax, periodiccnt;
376
377static void 404static void
378upheap (WT *timers, int k) 405upheap (WT *heap, int k)
379{ 406{
380 WT w = timers [k]; 407 WT w = heap [k];
381 408
382 while (k && timers [k >> 1]->at > w->at) 409 while (k && heap [k >> 1]->at > w->at)
383 { 410 {
384 timers [k] = timers [k >> 1]; 411 heap [k] = heap [k >> 1];
385 timers [k]->active = k + 1; 412 ((W)heap [k])->active = k + 1;
386 k >>= 1; 413 k >>= 1;
387 } 414 }
388 415
389 timers [k] = w; 416 heap [k] = w;
390 timers [k]->active = k + 1; 417 ((W)heap [k])->active = k + 1;
391 418
392} 419}
393 420
394static void 421static void
395downheap (WT *timers, int N, int k) 422downheap (WT *heap, int N, int k)
396{ 423{
397 WT w = timers [k]; 424 WT w = heap [k];
398 425
399 while (k < (N >> 1)) 426 while (k < (N >> 1))
400 { 427 {
401 int j = k << 1; 428 int j = k << 1;
402 429
403 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 430 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
404 ++j; 431 ++j;
405 432
406 if (w->at <= timers [j]->at) 433 if (w->at <= heap [j]->at)
407 break; 434 break;
408 435
409 timers [k] = timers [j]; 436 heap [k] = heap [j];
410 timers [k]->active = k + 1; 437 ((W)heap [k])->active = k + 1;
411 k = j; 438 k = j;
412 } 439 }
413 440
414 timers [k] = w; 441 heap [k] = w;
415 timers [k]->active = k + 1; 442 ((W)heap [k])->active = k + 1;
416} 443}
417 444
418/*****************************************************************************/ 445/*****************************************************************************/
419 446
420typedef struct 447typedef struct
443} 470}
444 471
445static void 472static void
446sighandler (int signum) 473sighandler (int signum)
447{ 474{
475#if WIN32
476 signal (signum, sighandler);
477#endif
478
448 signals [signum - 1].gotsig = 1; 479 signals [signum - 1].gotsig = 1;
449 480
450 if (!gotsig) 481 if (!gotsig)
451 { 482 {
452 int old_errno = errno; 483 int old_errno = errno;
486 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 517 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
487 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 518 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
488#endif 519#endif
489 520
490 ev_io_set (&sigev, sigpipe [0], EV_READ); 521 ev_io_set (&sigev, sigpipe [0], EV_READ);
491 ev_io_start (&sigev); 522 ev_io_start (EV_A_ &sigev);
523 ev_unref (EV_A); /* child watcher should not keep loop alive */
492} 524}
493 525
494/*****************************************************************************/ 526/*****************************************************************************/
495 527
496static struct ev_idle **idles; 528#ifndef WIN32
497static int idlemax, idlecnt;
498
499static struct ev_prepare **prepares;
500static int preparemax, preparecnt;
501
502static struct ev_check **checks;
503static int checkmax, checkcnt;
504
505/*****************************************************************************/
506 529
507static struct ev_child *childs [PID_HASHSIZE]; 530static struct ev_child *childs [PID_HASHSIZE];
508static struct ev_signal childev; 531static struct ev_signal childev;
509
510#ifndef WIN32
511 532
512#ifndef WCONTINUED 533#ifndef WCONTINUED
513# define WCONTINUED 0 534# define WCONTINUED 0
514#endif 535#endif
515 536
519 struct ev_child *w; 540 struct ev_child *w;
520 541
521 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)
522 if (w->pid == pid || !w->pid) 543 if (w->pid == pid || !w->pid)
523 { 544 {
524 w->priority = sw->priority; /* need to do it *now* */ 545 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
525 w->rpid = pid; 546 w->rpid = pid;
526 w->rstatus = status; 547 w->rstatus = status;
527 event (EV_A_ (W)w, EV_CHILD); 548 event (EV_A_ (W)w, EV_CHILD);
528 } 549 }
529} 550}
530 551
531static void 552static void
551# include "ev_kqueue.c" 572# include "ev_kqueue.c"
552#endif 573#endif
553#if EV_USE_EPOLL 574#if EV_USE_EPOLL
554# include "ev_epoll.c" 575# include "ev_epoll.c"
555#endif 576#endif
556#if EV_USEV_POLL 577#if EV_USE_POLL
557# include "ev_poll.c" 578# include "ev_poll.c"
558#endif 579#endif
559#if EV_USE_SELECT 580#if EV_USE_SELECT
560# include "ev_select.c" 581# include "ev_select.c"
561#endif 582#endif
588ev_method (EV_P) 609ev_method (EV_P)
589{ 610{
590 return method; 611 return method;
591} 612}
592 613
593int 614static void
594ev_init (EV_P_ int methods) 615loop_init (EV_P_ int methods)
595{ 616{
596 if (!method) 617 if (!method)
597 { 618 {
598#if EV_USE_MONOTONIC 619#if EV_USE_MONOTONIC
599 { 620 {
604#endif 625#endif
605 626
606 rt_now = ev_time (); 627 rt_now = ev_time ();
607 mn_now = get_clock (); 628 mn_now = get_clock ();
608 now_floor = mn_now; 629 now_floor = mn_now;
609 diff = rt_now - mn_now; 630 rtmn_diff = rt_now - mn_now;
610
611 if (pipe (sigpipe))
612 return 0;
613 631
614 if (methods == EVMETHOD_AUTO) 632 if (methods == EVMETHOD_AUTO)
615 if (!enable_secure () && getenv ("LIBmethodS")) 633 if (!enable_secure () && getenv ("LIBEV_METHODS"))
616 methods = atoi (getenv ("LIBmethodS")); 634 methods = atoi (getenv ("LIBEV_METHODS"));
617 else 635 else
618 methods = EVMETHOD_ANY; 636 methods = EVMETHOD_ANY;
619 637
620 method = 0; 638 method = 0;
639#if EV_USE_WIN32
640 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
641#endif
621#if EV_USE_KQUEUE 642#if EV_USE_KQUEUE
622 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 643 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
623#endif 644#endif
624#if EV_USE_EPOLL 645#if EV_USE_EPOLL
625 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 646 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
626#endif 647#endif
627#if EV_USEV_POLL 648#if EV_USE_POLL
628 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 649 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
629#endif 650#endif
630#if EV_USE_SELECT 651#if EV_USE_SELECT
631 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 652 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
632#endif 653#endif
654 }
655}
633 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
634 if (method) 759 if (ev_method (EV_A))
635 { 760 {
636 ev_watcher_init (&sigev, sigcb); 761 ev_watcher_init (&sigev, sigcb);
637 ev_set_priority (&sigev, EV_MAXPRI); 762 ev_set_priority (&sigev, EV_MAXPRI);
638 siginit (EV_A); 763 siginit (EV_A);
639 764
640#ifndef WIN32 765#ifndef WIN32
641 ev_signal_init (&childev, childcb, SIGCHLD); 766 ev_signal_init (&childev, childcb, SIGCHLD);
642 ev_set_priority (&childev, EV_MAXPRI); 767 ev_set_priority (&childev, EV_MAXPRI);
643 ev_signal_start (EV_A_ &childev); 768 ev_signal_start (EV_A_ &childev);
769 ev_unref (EV_A); /* child watcher should not keep loop alive */
644#endif 770#endif
645 } 771 }
772 else
773 default_loop = 0;
646 } 774 }
647 775
648 return method; 776 return default_loop;
649} 777}
650 778
651/*****************************************************************************/
652
653void 779void
654ev_fork_prepare (void) 780ev_default_destroy (void)
655{ 781{
656 /* nop */ 782#if EV_MULTIPLICITY
657} 783 struct ev_loop *loop = default_loop;
658
659void
660ev_fork_parent (void)
661{
662 /* nop */
663}
664
665void
666ev_fork_child (void)
667{
668#if EV_USE_EPOLL
669 if (method == EVMETHOD_EPOLL)
670 epoll_postfork_child ();
671#endif 784#endif
672 785
786 ev_ref (EV_A); /* child watcher */
787 ev_signal_stop (EV_A_ &childev);
788
789 ev_ref (EV_A); /* signal watcher */
673 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);
674 close (sigpipe [0]); 808 close (sigpipe [0]);
675 close (sigpipe [1]); 809 close (sigpipe [1]);
676 pipe (sigpipe); 810 pipe (sigpipe);
811
812 ev_ref (EV_A); /* signal watcher */
677 siginit (); 813 siginit (EV_A);
678} 814}
679 815
680/*****************************************************************************/ 816/*****************************************************************************/
681 817
682static void 818static void
698} 834}
699 835
700static void 836static void
701timers_reify (EV_P) 837timers_reify (EV_P)
702{ 838{
703 while (timercnt && timers [0]->at <= mn_now) 839 while (timercnt && ((WT)timers [0])->at <= mn_now)
704 { 840 {
705 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)));
706 844
707 /* first reschedule or stop timer */ 845 /* first reschedule or stop timer */
708 if (w->repeat) 846 if (w->repeat)
709 { 847 {
710 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.));
711 w->at = mn_now + w->repeat; 849 ((WT)w)->at = mn_now + w->repeat;
712 downheap ((WT *)timers, timercnt, 0); 850 downheap ((WT *)timers, timercnt, 0);
713 } 851 }
714 else 852 else
715 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 853 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
716 854
717 event ((W)w, EV_TIMEOUT); 855 event (EV_A_ (W)w, EV_TIMEOUT);
718 } 856 }
719} 857}
720 858
721static void 859static void
722periodics_reify (EV_P) 860periodics_reify (EV_P)
723{ 861{
724 while (periodiccnt && periodics [0]->at <= rt_now) 862 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
725 { 863 {
726 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)));
727 867
728 /* first reschedule or stop timer */ 868 /* first reschedule or stop timer */
729 if (w->interval) 869 if (w->interval)
730 { 870 {
731 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;
732 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));
733 downheap ((WT *)periodics, periodiccnt, 0); 873 downheap ((WT *)periodics, periodiccnt, 0);
734 } 874 }
735 else 875 else
736 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 876 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
737 877
738 event (EV_A_ (W)w, EV_PERIODIC); 878 event (EV_A_ (W)w, EV_PERIODIC);
739 } 879 }
740} 880}
741 881
742static void 882static void
743periodics_reschedule (EV_P_ ev_tstamp diff) 883periodics_reschedule (EV_P)
744{ 884{
745 int i; 885 int i;
746 886
747 /* adjust periodics after time jump */ 887 /* adjust periodics after time jump */
748 for (i = 0; i < periodiccnt; ++i) 888 for (i = 0; i < periodiccnt; ++i)
749 { 889 {
750 struct ev_periodic *w = periodics [i]; 890 struct ev_periodic *w = periodics [i];
751 891
752 if (w->interval) 892 if (w->interval)
753 { 893 {
754 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;
755 895
756 if (fabs (diff) >= 1e-4) 896 if (fabs (diff) >= 1e-4)
757 { 897 {
758 ev_periodic_stop (EV_A_ w); 898 ev_periodic_stop (EV_A_ w);
759 ev_periodic_start (EV_A_ w); 899 ev_periodic_start (EV_A_ w);
769{ 909{
770 mn_now = get_clock (); 910 mn_now = get_clock ();
771 911
772 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 912 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
773 { 913 {
774 rt_now = mn_now + diff; 914 rt_now = rtmn_diff + mn_now;
775 return 0; 915 return 0;
776 } 916 }
777 else 917 else
778 { 918 {
779 now_floor = mn_now; 919 now_floor = mn_now;
790#if EV_USE_MONOTONIC 930#if EV_USE_MONOTONIC
791 if (expect_true (have_monotonic)) 931 if (expect_true (have_monotonic))
792 { 932 {
793 if (time_update_monotonic (EV_A)) 933 if (time_update_monotonic (EV_A))
794 { 934 {
795 ev_tstamp odiff = diff; 935 ev_tstamp odiff = rtmn_diff;
796 936
797 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 */
798 { 938 {
799 diff = rt_now - mn_now; 939 rtmn_diff = rt_now - mn_now;
800 940
801 if (fabs (odiff - diff) < MIN_TIMEJUMP) 941 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
802 return; /* all is well */ 942 return; /* all is well */
803 943
804 rt_now = ev_time (); 944 rt_now = ev_time ();
805 mn_now = get_clock (); 945 mn_now = get_clock ();
806 now_floor = mn_now; 946 now_floor = mn_now;
807 } 947 }
808 948
809 periodics_reschedule (EV_A_ diff - odiff); 949 periodics_reschedule (EV_A);
810 /* 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) */
811 } 952 }
812 } 953 }
813 else 954 else
814#endif 955#endif
815 { 956 {
816 rt_now = ev_time (); 957 rt_now = ev_time ();
817 958
818 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))
819 { 960 {
820 periodics_reschedule (EV_A_ rt_now - mn_now); 961 periodics_reschedule (EV_A);
821 962
822 /* 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 */
823 for (i = 0; i < timercnt; ++i) 964 for (i = 0; i < timercnt; ++i)
824 timers [i]->at += diff; 965 ((WT)timers [i])->at += rt_now - mn_now;
825 } 966 }
826 967
827 mn_now = rt_now; 968 mn_now = rt_now;
828 } 969 }
829} 970}
880 { 1021 {
881 block = MAX_BLOCKTIME; 1022 block = MAX_BLOCKTIME;
882 1023
883 if (timercnt) 1024 if (timercnt)
884 { 1025 {
885 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1026 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
886 if (block > to) block = to; 1027 if (block > to) block = to;
887 } 1028 }
888 1029
889 if (periodiccnt) 1030 if (periodiccnt)
890 { 1031 {
891 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1032 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
892 if (block > to) block = to; 1033 if (block > to) block = to;
893 } 1034 }
894 1035
895 if (block < 0.) block = 0.; 1036 if (block < 0.) block = 0.;
896 } 1037 }
911 /* queue check watchers, to be executed first */ 1052 /* queue check watchers, to be executed first */
912 if (checkcnt) 1053 if (checkcnt)
913 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1054 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
914 1055
915 call_pending (EV_A); 1056 call_pending (EV_A);
916 printf ("activecnt %d\n", activecnt);//D
917 } 1057 }
918 while (activecnt && !loop_done); 1058 while (activecnt && !loop_done);
919 1059
920 if (loop_done != 2) 1060 if (loop_done != 2)
921 loop_done = 0; 1061 loop_done = 0;
1014ev_timer_start (EV_P_ struct ev_timer *w) 1154ev_timer_start (EV_P_ struct ev_timer *w)
1015{ 1155{
1016 if (ev_is_active (w)) 1156 if (ev_is_active (w))
1017 return; 1157 return;
1018 1158
1019 w->at += mn_now; 1159 ((WT)w)->at += mn_now;
1020 1160
1021 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.));
1022 1162
1023 ev_start (EV_A_ (W)w, ++timercnt); 1163 ev_start (EV_A_ (W)w, ++timercnt);
1024 array_needsize (timers, timermax, timercnt, ); 1164 array_needsize (timers, timermax, timercnt, );
1025 timers [timercnt - 1] = w; 1165 timers [timercnt - 1] = w;
1026 upheap ((WT *)timers, timercnt - 1); 1166 upheap ((WT *)timers, timercnt - 1);
1167
1168 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1027} 1169}
1028 1170
1029void 1171void
1030ev_timer_stop (EV_P_ struct ev_timer *w) 1172ev_timer_stop (EV_P_ struct ev_timer *w)
1031{ 1173{
1032 ev_clear_pending (EV_A_ (W)w); 1174 ev_clear_pending (EV_A_ (W)w);
1033 if (!ev_is_active (w)) 1175 if (!ev_is_active (w))
1034 return; 1176 return;
1035 1177
1178 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1179
1036 if (w->active < timercnt--) 1180 if (((W)w)->active < timercnt--)
1037 { 1181 {
1038 timers [w->active - 1] = timers [timercnt]; 1182 timers [((W)w)->active - 1] = timers [timercnt];
1039 downheap ((WT *)timers, timercnt, w->active - 1); 1183 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1040 } 1184 }
1041 1185
1042 w->at = w->repeat; 1186 ((WT)w)->at = w->repeat;
1043 1187
1044 ev_stop (EV_A_ (W)w); 1188 ev_stop (EV_A_ (W)w);
1045} 1189}
1046 1190
1047void 1191void
1049{ 1193{
1050 if (ev_is_active (w)) 1194 if (ev_is_active (w))
1051 { 1195 {
1052 if (w->repeat) 1196 if (w->repeat)
1053 { 1197 {
1054 w->at = mn_now + w->repeat; 1198 ((WT)w)->at = mn_now + w->repeat;
1055 downheap ((WT *)timers, timercnt, w->active - 1); 1199 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1056 } 1200 }
1057 else 1201 else
1058 ev_timer_stop (EV_A_ w); 1202 ev_timer_stop (EV_A_ w);
1059 } 1203 }
1060 else if (w->repeat) 1204 else if (w->repeat)
1069 1213
1070 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.));
1071 1215
1072 /* 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 */
1073 if (w->interval) 1217 if (w->interval)
1074 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;
1075 1219
1076 ev_start (EV_A_ (W)w, ++periodiccnt); 1220 ev_start (EV_A_ (W)w, ++periodiccnt);
1077 array_needsize (periodics, periodicmax, periodiccnt, ); 1221 array_needsize (periodics, periodicmax, periodiccnt, );
1078 periodics [periodiccnt - 1] = w; 1222 periodics [periodiccnt - 1] = w;
1079 upheap ((WT *)periodics, periodiccnt - 1); 1223 upheap ((WT *)periodics, periodiccnt - 1);
1224
1225 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1080} 1226}
1081 1227
1082void 1228void
1083ev_periodic_stop (EV_P_ struct ev_periodic *w) 1229ev_periodic_stop (EV_P_ struct ev_periodic *w)
1084{ 1230{
1085 ev_clear_pending (EV_A_ (W)w); 1231 ev_clear_pending (EV_A_ (W)w);
1086 if (!ev_is_active (w)) 1232 if (!ev_is_active (w))
1087 return; 1233 return;
1088 1234
1235 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1236
1089 if (w->active < periodiccnt--) 1237 if (((W)w)->active < periodiccnt--)
1090 { 1238 {
1091 periodics [w->active - 1] = periodics [periodiccnt]; 1239 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1092 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1240 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1093 } 1241 }
1094 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];
1095 ev_stop (EV_A_ (W)w); 1309 ev_stop (EV_A_ (W)w);
1096} 1310}
1097 1311
1098#ifndef SA_RESTART 1312#ifndef SA_RESTART
1099# define SA_RESTART 0 1313# define SA_RESTART 0
1100#endif 1314#endif
1101 1315
1102void 1316void
1103ev_signal_start (EV_P_ struct ev_signal *w) 1317ev_signal_start (EV_P_ struct ev_signal *w)
1104{ 1318{
1319#if EV_MULTIPLICITY
1320 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1321#endif
1105 if (ev_is_active (w)) 1322 if (ev_is_active (w))
1106 return; 1323 return;
1107 1324
1108 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));
1109 1326
1110 ev_start (EV_A_ (W)w, 1); 1327 ev_start (EV_A_ (W)w, 1);
1111 array_needsize (signals, signalmax, w->signum, signals_init); 1328 array_needsize (signals, signalmax, w->signum, signals_init);
1112 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1329 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1113 1330
1114 if (!w->next) 1331 if (!((WL)w)->next)
1115 { 1332 {
1333#if WIN32
1334 signal (w->signum, sighandler);
1335#else
1116 struct sigaction sa; 1336 struct sigaction sa;
1117 sa.sa_handler = sighandler; 1337 sa.sa_handler = sighandler;
1118 sigfillset (&sa.sa_mask); 1338 sigfillset (&sa.sa_mask);
1119 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 */
1120 sigaction (w->signum, &sa, 0); 1340 sigaction (w->signum, &sa, 0);
1341#endif
1121 } 1342 }
1122} 1343}
1123 1344
1124void 1345void
1125ev_signal_stop (EV_P_ struct ev_signal *w) 1346ev_signal_stop (EV_P_ struct ev_signal *w)
1134 if (!signals [w->signum - 1].head) 1355 if (!signals [w->signum - 1].head)
1135 signal (w->signum, SIG_DFL); 1356 signal (w->signum, SIG_DFL);
1136} 1357}
1137 1358
1138void 1359void
1139ev_idle_start (EV_P_ struct ev_idle *w)
1140{
1141 if (ev_is_active (w))
1142 return;
1143
1144 ev_start (EV_A_ (W)w, ++idlecnt);
1145 array_needsize (idles, idlemax, idlecnt, );
1146 idles [idlecnt - 1] = w;
1147}
1148
1149void
1150ev_idle_stop (EV_P_ struct ev_idle *w)
1151{
1152 ev_clear_pending (EV_A_ (W)w);
1153 if (ev_is_active (w))
1154 return;
1155
1156 idles [w->active - 1] = idles [--idlecnt];
1157 ev_stop (EV_A_ (W)w);
1158}
1159
1160void
1161ev_prepare_start (EV_P_ struct ev_prepare *w)
1162{
1163 if (ev_is_active (w))
1164 return;
1165
1166 ev_start (EV_A_ (W)w, ++preparecnt);
1167 array_needsize (prepares, preparemax, preparecnt, );
1168 prepares [preparecnt - 1] = w;
1169}
1170
1171void
1172ev_prepare_stop (EV_P_ struct ev_prepare *w)
1173{
1174 ev_clear_pending (EV_A_ (W)w);
1175 if (ev_is_active (w))
1176 return;
1177
1178 prepares [w->active - 1] = prepares [--preparecnt];
1179 ev_stop (EV_A_ (W)w);
1180}
1181
1182void
1183ev_check_start (EV_P_ struct ev_check *w)
1184{
1185 if (ev_is_active (w))
1186 return;
1187
1188 ev_start (EV_A_ (W)w, ++checkcnt);
1189 array_needsize (checks, checkmax, checkcnt, );
1190 checks [checkcnt - 1] = w;
1191}
1192
1193void
1194ev_check_stop (EV_P_ struct ev_check *w)
1195{
1196 ev_clear_pending (EV_A_ (W)w);
1197 if (ev_is_active (w))
1198 return;
1199
1200 checks [w->active - 1] = checks [--checkcnt];
1201 ev_stop (EV_A_ (W)w);
1202}
1203
1204void
1205ev_child_start (EV_P_ struct ev_child *w) 1360ev_child_start (EV_P_ struct ev_child *w)
1206{ 1361{
1362#if EV_MULTIPLICITY
1363 assert (("child watchers are only supported in the default loop", loop == default_loop));
1364#endif
1207 if (ev_is_active (w)) 1365 if (ev_is_active (w))
1208 return; 1366 return;
1209 1367
1210 ev_start (EV_A_ (W)w, 1); 1368 ev_start (EV_A_ (W)w, 1);
1211 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1369 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1283 ev_timer_start (EV_A_ &once->to); 1441 ev_timer_start (EV_A_ &once->to);
1284 } 1442 }
1285 } 1443 }
1286} 1444}
1287 1445
1288/*****************************************************************************/
1289
1290#if 0
1291
1292struct ev_io wio;
1293
1294static void
1295sin_cb (struct ev_io *w, int revents)
1296{
1297 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1298}
1299
1300static void
1301ocb (struct ev_timer *w, int revents)
1302{
1303 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1304 ev_timer_stop (w);
1305 ev_timer_start (w);
1306}
1307
1308static void
1309scb (struct ev_signal *w, int revents)
1310{
1311 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1312 ev_io_stop (&wio);
1313 ev_io_start (&wio);
1314}
1315
1316static void
1317gcb (struct ev_signal *w, int revents)
1318{
1319 fprintf (stderr, "generic %x\n", revents);
1320
1321}
1322
1323int main (void)
1324{
1325 ev_init (0);
1326
1327 ev_io_init (&wio, sin_cb, 0, EV_READ);
1328 ev_io_start (&wio);
1329
1330 struct ev_timer t[10000];
1331
1332#if 0
1333 int i;
1334 for (i = 0; i < 10000; ++i)
1335 {
1336 struct ev_timer *w = t + i;
1337 ev_watcher_init (w, ocb, i);
1338 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1339 ev_timer_start (w);
1340 if (drand48 () < 0.5)
1341 ev_timer_stop (w);
1342 }
1343#endif
1344
1345 struct ev_timer t1;
1346 ev_timer_init (&t1, ocb, 5, 10);
1347 ev_timer_start (&t1);
1348
1349 struct ev_signal sig;
1350 ev_signal_init (&sig, scb, SIGQUIT);
1351 ev_signal_start (&sig);
1352
1353 struct ev_check cw;
1354 ev_check_init (&cw, gcb);
1355 ev_check_start (&cw);
1356
1357 struct ev_idle iw;
1358 ev_idle_init (&iw, gcb);
1359 ev_idle_start (&iw);
1360
1361 ev_loop (0);
1362
1363 return 0;
1364}
1365
1366#endif
1367
1368
1369
1370

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines