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

Comparing libev/ev.c (file contents):
Revision 1.55 by root, Sun Nov 4 00:39:24 2007 UTC vs.
Revision 1.70 by root, Tue Nov 6 00:52:32 2007 UTC

28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#ifndef EV_STANDALONE 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33
34# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1
36# define EV_USE_REALTIME 1
37# endif
38
39# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1
41# endif
42
43# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1
45# endif
46
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1
49# endif
50
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1
53# endif
54
33#endif 55#endif
34 56
35#include <math.h> 57#include <math.h>
36#include <stdlib.h> 58#include <stdlib.h>
37#include <unistd.h> 59#include <unistd.h>
58 80
59#ifndef EV_USE_SELECT 81#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 82# define EV_USE_SELECT 1
61#endif 83#endif
62 84
63#ifndef EV_USEV_POLL 85#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 86# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 87#endif
66 88
67#ifndef EV_USE_EPOLL 89#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 90# define EV_USE_EPOLL 0
69#endif 91#endif
70 92
71#ifndef EV_USE_KQUEUE 93#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0 94# define EV_USE_KQUEUE 0
95#endif
96
97#ifndef EV_USE_WIN32
98# ifdef WIN32
99# define EV_USE_WIN32 1
100# else
101# define EV_USE_WIN32 0
102# endif
73#endif 103#endif
74 104
75#ifndef EV_USE_REALTIME 105#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1 106# define EV_USE_REALTIME 1
77#endif 107#endif
115typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
116typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
117 147
118static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
119 149
150#if WIN32
151/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f;
154#endif
155
120/*****************************************************************************/ 156/*****************************************************************************/
121 157
158static void (*syserr_cb)(const char *msg);
159
160void ev_set_syserr_cb (void (*cb)(const char *msg))
161{
162 syserr_cb = cb;
163}
164
165static void
166syserr (const char *msg)
167{
168 if (!msg)
169 msg = "(libev) system error";
170
171 if (syserr_cb)
172 syserr_cb (msg);
173 else
174 {
175 perror (msg);
176 abort ();
177 }
178}
179
180static void *(*alloc)(void *ptr, long size);
181
182void ev_set_allocator (void *(*cb)(void *ptr, long size))
183{
184 alloc = cb;
185}
186
187static void *
188ev_realloc (void *ptr, long size)
189{
190 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
191
192 if (!ptr && size)
193 {
194 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
195 abort ();
196 }
197
198 return ptr;
199}
200
201#define ev_malloc(size) ev_realloc (0, (size))
202#define ev_free(ptr) ev_realloc ((ptr), 0)
203
204/*****************************************************************************/
205
122typedef struct 206typedef struct
123{ 207{
124 struct ev_watcher_list *head; 208 WL head;
125 unsigned char events; 209 unsigned char events;
126 unsigned char reify; 210 unsigned char reify;
127} ANFD; 211} ANFD;
128 212
129typedef struct 213typedef struct
187 return rt_now; 271 return rt_now;
188} 272}
189 273
190#define array_roundsize(base,n) ((n) | 4 & ~3) 274#define array_roundsize(base,n) ((n) | 4 & ~3)
191 275
192#define array_needsize(base,cur,cnt,init) \ 276#define array_needsize(base,cur,cnt,init) \
193 if (expect_false ((cnt) > cur)) \ 277 if (expect_false ((cnt) > cur)) \
194 { \ 278 { \
195 int newcnt = cur; \ 279 int newcnt = cur; \
196 do \ 280 do \
197 { \ 281 { \
198 newcnt = array_roundsize (base, newcnt << 1); \ 282 newcnt = array_roundsize (base, newcnt << 1); \
199 } \ 283 } \
200 while ((cnt) > newcnt); \ 284 while ((cnt) > newcnt); \
201 \ 285 \
202 base = realloc (base, sizeof (*base) * (newcnt)); \ 286 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
203 init (base + cur, newcnt - cur); \ 287 init (base + cur, newcnt - cur); \
204 cur = newcnt; \ 288 cur = newcnt; \
205 } 289 }
290
291#define array_slim(stem) \
292 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
293 { \
294 stem ## max = array_roundsize (stem ## cnt >> 1); \
295 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
296 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
297 }
298
299#define array_free(stem, idx) \
300 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
206 301
207/*****************************************************************************/ 302/*****************************************************************************/
208 303
209static void 304static void
210anfds_init (ANFD *base, int count) 305anfds_init (ANFD *base, int count)
276 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 371 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
277 events |= w->events; 372 events |= w->events;
278 373
279 anfd->reify = 0; 374 anfd->reify = 0;
280 375
281 if (anfd->events != events)
282 {
283 method_modify (EV_A_ fd, anfd->events, events); 376 method_modify (EV_A_ fd, anfd->events, events);
284 anfd->events = events; 377 anfd->events = events;
285 }
286 } 378 }
287 379
288 fdchangecnt = 0; 380 fdchangecnt = 0;
289} 381}
290 382
291static void 383static void
292fd_change (EV_P_ int fd) 384fd_change (EV_P_ int fd)
293{ 385{
294 if (anfds [fd].reify || fdchangecnt < 0) 386 if (anfds [fd].reify)
295 return; 387 return;
296 388
297 anfds [fd].reify = 1; 389 anfds [fd].reify = 1;
298 390
299 ++fdchangecnt; 391 ++fdchangecnt;
327 419
328/* called on ENOMEM in select/poll to kill some fds and retry */ 420/* called on ENOMEM in select/poll to kill some fds and retry */
329static void 421static void
330fd_enomem (EV_P) 422fd_enomem (EV_P)
331{ 423{
332 int fd = anfdmax; 424 int fd;
333 425
334 while (fd--) 426 for (fd = anfdmax; fd--; )
335 if (anfds [fd].events) 427 if (anfds [fd].events)
336 { 428 {
337 close (fd);
338 fd_kill (EV_A_ fd); 429 fd_kill (EV_A_ fd);
339 return; 430 return;
340 } 431 }
341} 432}
342 433
434/* usually called after fork if method needs to re-arm all fds from scratch */
435static void
436fd_rearm_all (EV_P)
437{
438 int fd;
439
440 /* this should be highly optimised to not do anything but set a flag */
441 for (fd = 0; fd < anfdmax; ++fd)
442 if (anfds [fd].events)
443 {
444 anfds [fd].events = 0;
445 fd_change (EV_A_ fd);
446 }
447}
448
343/*****************************************************************************/ 449/*****************************************************************************/
344 450
345static void 451static void
346upheap (WT *heap, int k) 452upheap (WT *heap, int k)
347{ 453{
348 WT w = heap [k]; 454 WT w = heap [k];
349 455
350 while (k && heap [k >> 1]->at > w->at) 456 while (k && heap [k >> 1]->at > w->at)
351 { 457 {
352 heap [k] = heap [k >> 1]; 458 heap [k] = heap [k >> 1];
353 heap [k]->active = k + 1; 459 ((W)heap [k])->active = k + 1;
354 k >>= 1; 460 k >>= 1;
355 } 461 }
356 462
357 heap [k] = w; 463 heap [k] = w;
358 heap [k]->active = k + 1; 464 ((W)heap [k])->active = k + 1;
359 465
360} 466}
361 467
362static void 468static void
363downheap (WT *heap, int N, int k) 469downheap (WT *heap, int N, int k)
373 479
374 if (w->at <= heap [j]->at) 480 if (w->at <= heap [j]->at)
375 break; 481 break;
376 482
377 heap [k] = heap [j]; 483 heap [k] = heap [j];
378 heap [k]->active = k + 1; 484 ((W)heap [k])->active = k + 1;
379 k = j; 485 k = j;
380 } 486 }
381 487
382 heap [k] = w; 488 heap [k] = w;
383 heap [k]->active = k + 1; 489 ((W)heap [k])->active = k + 1;
384} 490}
385 491
386/*****************************************************************************/ 492/*****************************************************************************/
387 493
388typedef struct 494typedef struct
389{ 495{
390 struct ev_watcher_list *head; 496 WL head;
391 sig_atomic_t volatile gotsig; 497 sig_atomic_t volatile gotsig;
392} ANSIG; 498} ANSIG;
393 499
394static ANSIG *signals; 500static ANSIG *signals;
395static int signalmax; 501static int signalmax;
396 502
397static int sigpipe [2]; 503static int sigpipe [2];
398static sig_atomic_t volatile gotsig; 504static sig_atomic_t volatile gotsig;
505static struct ev_io sigev;
399 506
400static void 507static void
401signals_init (ANSIG *base, int count) 508signals_init (ANSIG *base, int count)
402{ 509{
403 while (count--) 510 while (count--)
410} 517}
411 518
412static void 519static void
413sighandler (int signum) 520sighandler (int signum)
414{ 521{
522#if WIN32
523 signal (signum, sighandler);
524#endif
525
415 signals [signum - 1].gotsig = 1; 526 signals [signum - 1].gotsig = 1;
416 527
417 if (!gotsig) 528 if (!gotsig)
418 { 529 {
419 int old_errno = errno; 530 int old_errno = errno;
424} 535}
425 536
426static void 537static void
427sigcb (EV_P_ struct ev_io *iow, int revents) 538sigcb (EV_P_ struct ev_io *iow, int revents)
428{ 539{
429 struct ev_watcher_list *w; 540 WL w;
430 int signum; 541 int signum;
431 542
432 read (sigpipe [0], &revents, 1); 543 read (sigpipe [0], &revents, 1);
433 gotsig = 0; 544 gotsig = 0;
434 545
461 572
462/*****************************************************************************/ 573/*****************************************************************************/
463 574
464#ifndef WIN32 575#ifndef WIN32
465 576
577static struct ev_child *childs [PID_HASHSIZE];
578static struct ev_signal childev;
579
466#ifndef WCONTINUED 580#ifndef WCONTINUED
467# define WCONTINUED 0 581# define WCONTINUED 0
468#endif 582#endif
469 583
470static void 584static void
473 struct ev_child *w; 587 struct ev_child *w;
474 588
475 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 589 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
476 if (w->pid == pid || !w->pid) 590 if (w->pid == pid || !w->pid)
477 { 591 {
478 w->priority = sw->priority; /* need to do it *now* */ 592 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
479 w->rpid = pid; 593 w->rpid = pid;
480 w->rstatus = status; 594 w->rstatus = status;
481 event (EV_A_ (W)w, EV_CHILD); 595 event (EV_A_ (W)w, EV_CHILD);
482 } 596 }
483} 597}
484 598
485static void 599static void
505# include "ev_kqueue.c" 619# include "ev_kqueue.c"
506#endif 620#endif
507#if EV_USE_EPOLL 621#if EV_USE_EPOLL
508# include "ev_epoll.c" 622# include "ev_epoll.c"
509#endif 623#endif
510#if EV_USEV_POLL 624#if EV_USE_POLL
511# include "ev_poll.c" 625# include "ev_poll.c"
512#endif 626#endif
513#if EV_USE_SELECT 627#if EV_USE_SELECT
514# include "ev_select.c" 628# include "ev_select.c"
515#endif 629#endif
542ev_method (EV_P) 656ev_method (EV_P)
543{ 657{
544 return method; 658 return method;
545} 659}
546 660
547inline int 661static void
548loop_init (EV_P_ int methods) 662loop_init (EV_P_ int methods)
549{ 663{
550 if (!method) 664 if (!method)
551 { 665 {
552#if EV_USE_MONOTONIC 666#if EV_USE_MONOTONIC
560 rt_now = ev_time (); 674 rt_now = ev_time ();
561 mn_now = get_clock (); 675 mn_now = get_clock ();
562 now_floor = mn_now; 676 now_floor = mn_now;
563 rtmn_diff = rt_now - mn_now; 677 rtmn_diff = rt_now - mn_now;
564 678
565 if (pipe (sigpipe))
566 return 0;
567
568 if (methods == EVMETHOD_AUTO) 679 if (methods == EVMETHOD_AUTO)
569 if (!enable_secure () && getenv ("LIBmethodS")) 680 if (!enable_secure () && getenv ("LIBEV_METHODS"))
570 methods = atoi (getenv ("LIBmethodS")); 681 methods = atoi (getenv ("LIBEV_METHODS"));
571 else 682 else
572 methods = EVMETHOD_ANY; 683 methods = EVMETHOD_ANY;
573 684
574 method = 0; 685 method = 0;
686#if EV_USE_WIN32
687 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
688#endif
575#if EV_USE_KQUEUE 689#if EV_USE_KQUEUE
576 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 690 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
577#endif 691#endif
578#if EV_USE_EPOLL 692#if EV_USE_EPOLL
579 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 693 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
580#endif 694#endif
581#if EV_USEV_POLL 695#if EV_USE_POLL
582 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
583#endif 697#endif
584#if EV_USE_SELECT 698#if EV_USE_SELECT
585 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
586#endif 700#endif
587 701
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
704 }
705}
706
707void
708loop_destroy (EV_P)
709{
710 int i;
711
712#if EV_USE_WIN32
713 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
714#endif
715#if EV_USE_KQUEUE
716 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
717#endif
718#if EV_USE_EPOLL
719 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
720#endif
721#if EV_USE_POLL
722 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
723#endif
724#if EV_USE_SELECT
725 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
726#endif
727
728 for (i = NUMPRI; i--; )
729 array_free (pending, [i]);
730
731 array_free (fdchange, );
732 array_free (timer, );
733 array_free (periodic, );
734 array_free (idle, );
735 array_free (prepare, );
736 array_free (check, );
737
738 method = 0;
739}
740
741static void
742loop_fork (EV_P)
743{
744#if EV_USE_EPOLL
745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
746#endif
747#if EV_USE_KQUEUE
748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
749#endif
750
751 if (ev_is_active (&sigev))
752 {
753 /* default loop */
754
755 ev_ref (EV_A);
756 ev_io_stop (EV_A_ &sigev);
757 close (sigpipe [0]);
758 close (sigpipe [1]);
759
760 while (pipe (sigpipe))
761 syserr ("(libev) error creating pipe");
762
763 siginit (EV_A);
764 }
765
766 postfork = 0;
767}
768
769#if EV_MULTIPLICITY
770struct ev_loop *
771ev_loop_new (int methods)
772{
773 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
774
775 memset (loop, 0, sizeof (struct ev_loop));
776
777 loop_init (EV_A_ methods);
778
779 if (ev_method (EV_A))
780 return loop;
781
782 return 0;
783}
784
785void
786ev_loop_destroy (EV_P)
787{
788 loop_destroy (EV_A);
789 ev_free (loop);
790}
791
792void
793ev_loop_fork (EV_P)
794{
795 postfork = 1;
796}
797
798#endif
799
800#if EV_MULTIPLICITY
801struct ev_loop default_loop_struct;
802static struct ev_loop *default_loop;
803
804struct ev_loop *
805#else
806static int default_loop;
807
808int
809#endif
810ev_default_loop (int methods)
811{
812 if (sigpipe [0] == sigpipe [1])
813 if (pipe (sigpipe))
814 return 0;
815
816 if (!default_loop)
817 {
818#if EV_MULTIPLICITY
819 struct ev_loop *loop = default_loop = &default_loop_struct;
820#else
821 default_loop = 1;
822#endif
823
824 loop_init (EV_A_ methods);
825
588 if (method) 826 if (ev_method (EV_A))
589 { 827 {
590 ev_watcher_init (&sigev, sigcb);
591 ev_set_priority (&sigev, EV_MAXPRI);
592 siginit (EV_A); 828 siginit (EV_A);
593 829
594#ifndef WIN32 830#ifndef WIN32
595 ev_signal_init (&childev, childcb, SIGCHLD); 831 ev_signal_init (&childev, childcb, SIGCHLD);
596 ev_set_priority (&childev, EV_MAXPRI); 832 ev_set_priority (&childev, EV_MAXPRI);
597 ev_signal_start (EV_A_ &childev); 833 ev_signal_start (EV_A_ &childev);
598 ev_unref (EV_A); /* child watcher should not keep loop alive */ 834 ev_unref (EV_A); /* child watcher should not keep loop alive */
599#endif 835#endif
600 } 836 }
837 else
838 default_loop = 0;
601 } 839 }
602 840
603 return method; 841 return default_loop;
604} 842}
605 843
844void
845ev_default_destroy (void)
846{
606#if EV_MULTIPLICITY 847#if EV_MULTIPLICITY
607 848 struct ev_loop *loop = default_loop;
608struct ev_loop *
609ev_loop_new (int methods)
610{
611 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
612
613 if (loop_init (EV_A_ methods))
614 return loop;
615
616 ev_loop_delete (loop);
617
618 return 0;
619}
620
621void
622ev_loop_delete (EV_P)
623{
624 /*TODO*/
625 free (loop);
626}
627
628#else
629
630int
631ev_init (int methods)
632{
633 return loop_init (methods);
634}
635
636#endif 849#endif
637 850
638/*****************************************************************************/ 851 ev_ref (EV_A); /* child watcher */
852 ev_signal_stop (EV_A_ &childev);
639 853
640void 854 ev_ref (EV_A); /* signal watcher */
641ev_fork_prepare (void)
642{
643 /* nop */
644}
645
646void
647ev_fork_parent (void)
648{
649 /* nop */
650}
651
652void
653ev_fork_child (void)
654{
655 /*TODO*/
656#if !EV_MULTIPLICITY
657#if EV_USE_EPOLL
658 if (method == EVMETHOD_EPOLL)
659 epoll_postfork_child (EV_A);
660#endif
661
662 ev_io_stop (EV_A_ &sigev); 855 ev_io_stop (EV_A_ &sigev);
663 close (sigpipe [0]); 856
664 close (sigpipe [1]); 857 close (sigpipe [0]); sigpipe [0] = 0;
665 pipe (sigpipe); 858 close (sigpipe [1]); sigpipe [1] = 0;
666 siginit (EV_A); 859
860 loop_destroy (EV_A);
861}
862
863void
864ev_default_fork (void)
865{
866#if EV_MULTIPLICITY
867 struct ev_loop *loop = default_loop;
667#endif 868#endif
869
870 if (method)
871 postfork = 1;
668} 872}
669 873
670/*****************************************************************************/ 874/*****************************************************************************/
671 875
672static void 876static void
688} 892}
689 893
690static void 894static void
691timers_reify (EV_P) 895timers_reify (EV_P)
692{ 896{
693 while (timercnt && timers [0]->at <= mn_now) 897 while (timercnt && ((WT)timers [0])->at <= mn_now)
694 { 898 {
695 struct ev_timer *w = timers [0]; 899 struct ev_timer *w = timers [0];
900
901 assert (("inactive timer on timer heap detected", ev_is_active (w)));
696 902
697 /* first reschedule or stop timer */ 903 /* first reschedule or stop timer */
698 if (w->repeat) 904 if (w->repeat)
699 { 905 {
700 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 906 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
701 w->at = mn_now + w->repeat; 907 ((WT)w)->at = mn_now + w->repeat;
702 downheap ((WT *)timers, timercnt, 0); 908 downheap ((WT *)timers, timercnt, 0);
703 } 909 }
704 else 910 else
705 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 911 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
706 912
709} 915}
710 916
711static void 917static void
712periodics_reify (EV_P) 918periodics_reify (EV_P)
713{ 919{
714 while (periodiccnt && periodics [0]->at <= rt_now) 920 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
715 { 921 {
716 struct ev_periodic *w = periodics [0]; 922 struct ev_periodic *w = periodics [0];
923
924 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
717 925
718 /* first reschedule or stop timer */ 926 /* first reschedule or stop timer */
719 if (w->interval) 927 if (w->interval)
720 { 928 {
721 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 929 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
722 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 930 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
723 downheap ((WT *)periodics, periodiccnt, 0); 931 downheap ((WT *)periodics, periodiccnt, 0);
724 } 932 }
725 else 933 else
726 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 934 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
727 935
739 { 947 {
740 struct ev_periodic *w = periodics [i]; 948 struct ev_periodic *w = periodics [i];
741 949
742 if (w->interval) 950 if (w->interval)
743 { 951 {
744 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 952 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
745 953
746 if (fabs (diff) >= 1e-4) 954 if (fabs (diff) >= 1e-4)
747 { 955 {
748 ev_periodic_stop (EV_A_ w); 956 ev_periodic_stop (EV_A_ w);
749 ev_periodic_start (EV_A_ w); 957 ev_periodic_start (EV_A_ w);
810 { 1018 {
811 periodics_reschedule (EV_A); 1019 periodics_reschedule (EV_A);
812 1020
813 /* adjust timers. this is easy, as the offset is the same for all */ 1021 /* adjust timers. this is easy, as the offset is the same for all */
814 for (i = 0; i < timercnt; ++i) 1022 for (i = 0; i < timercnt; ++i)
815 timers [i]->at += rt_now - mn_now; 1023 ((WT)timers [i])->at += rt_now - mn_now;
816 } 1024 }
817 1025
818 mn_now = rt_now; 1026 mn_now = rt_now;
819 } 1027 }
820} 1028}
846 { 1054 {
847 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
848 call_pending (EV_A); 1056 call_pending (EV_A);
849 } 1057 }
850 1058
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1061 loop_fork (EV_A);
1062
851 /* update fd-related kernel structures */ 1063 /* update fd-related kernel structures */
852 fd_reify (EV_A); 1064 fd_reify (EV_A);
853 1065
854 /* calculate blocking time */ 1066 /* calculate blocking time */
855 1067
871 { 1083 {
872 block = MAX_BLOCKTIME; 1084 block = MAX_BLOCKTIME;
873 1085
874 if (timercnt) 1086 if (timercnt)
875 { 1087 {
876 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1088 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
877 if (block > to) block = to; 1089 if (block > to) block = to;
878 } 1090 }
879 1091
880 if (periodiccnt) 1092 if (periodiccnt)
881 { 1093 {
882 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1094 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
883 if (block > to) block = to; 1095 if (block > to) block = to;
884 } 1096 }
885 1097
886 if (block < 0.) block = 0.; 1098 if (block < 0.) block = 0.;
887 } 1099 }
1004ev_timer_start (EV_P_ struct ev_timer *w) 1216ev_timer_start (EV_P_ struct ev_timer *w)
1005{ 1217{
1006 if (ev_is_active (w)) 1218 if (ev_is_active (w))
1007 return; 1219 return;
1008 1220
1009 w->at += mn_now; 1221 ((WT)w)->at += mn_now;
1010 1222
1011 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1223 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1012 1224
1013 ev_start (EV_A_ (W)w, ++timercnt); 1225 ev_start (EV_A_ (W)w, ++timercnt);
1014 array_needsize (timers, timermax, timercnt, ); 1226 array_needsize (timers, timermax, timercnt, );
1015 timers [timercnt - 1] = w; 1227 timers [timercnt - 1] = w;
1016 upheap ((WT *)timers, timercnt - 1); 1228 upheap ((WT *)timers, timercnt - 1);
1229
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1017} 1231}
1018 1232
1019void 1233void
1020ev_timer_stop (EV_P_ struct ev_timer *w) 1234ev_timer_stop (EV_P_ struct ev_timer *w)
1021{ 1235{
1022 ev_clear_pending (EV_A_ (W)w); 1236 ev_clear_pending (EV_A_ (W)w);
1023 if (!ev_is_active (w)) 1237 if (!ev_is_active (w))
1024 return; 1238 return;
1025 1239
1240 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1241
1026 if (w->active < timercnt--) 1242 if (((W)w)->active < timercnt--)
1027 { 1243 {
1028 timers [w->active - 1] = timers [timercnt]; 1244 timers [((W)w)->active - 1] = timers [timercnt];
1029 downheap ((WT *)timers, timercnt, w->active - 1); 1245 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1030 } 1246 }
1031 1247
1032 w->at = w->repeat; 1248 ((WT)w)->at = w->repeat;
1033 1249
1034 ev_stop (EV_A_ (W)w); 1250 ev_stop (EV_A_ (W)w);
1035} 1251}
1036 1252
1037void 1253void
1039{ 1255{
1040 if (ev_is_active (w)) 1256 if (ev_is_active (w))
1041 { 1257 {
1042 if (w->repeat) 1258 if (w->repeat)
1043 { 1259 {
1044 w->at = mn_now + w->repeat; 1260 ((WT)w)->at = mn_now + w->repeat;
1045 downheap ((WT *)timers, timercnt, w->active - 1); 1261 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1046 } 1262 }
1047 else 1263 else
1048 ev_timer_stop (EV_A_ w); 1264 ev_timer_stop (EV_A_ w);
1049 } 1265 }
1050 else if (w->repeat) 1266 else if (w->repeat)
1059 1275
1060 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1276 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1061 1277
1062 /* this formula differs from the one in periodic_reify because we do not always round up */ 1278 /* this formula differs from the one in periodic_reify because we do not always round up */
1063 if (w->interval) 1279 if (w->interval)
1064 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1280 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1065 1281
1066 ev_start (EV_A_ (W)w, ++periodiccnt); 1282 ev_start (EV_A_ (W)w, ++periodiccnt);
1067 array_needsize (periodics, periodicmax, periodiccnt, ); 1283 array_needsize (periodics, periodicmax, periodiccnt, );
1068 periodics [periodiccnt - 1] = w; 1284 periodics [periodiccnt - 1] = w;
1069 upheap ((WT *)periodics, periodiccnt - 1); 1285 upheap ((WT *)periodics, periodiccnt - 1);
1286
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1070} 1288}
1071 1289
1072void 1290void
1073ev_periodic_stop (EV_P_ struct ev_periodic *w) 1291ev_periodic_stop (EV_P_ struct ev_periodic *w)
1074{ 1292{
1075 ev_clear_pending (EV_A_ (W)w); 1293 ev_clear_pending (EV_A_ (W)w);
1076 if (!ev_is_active (w)) 1294 if (!ev_is_active (w))
1077 return; 1295 return;
1078 1296
1297 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1298
1079 if (w->active < periodiccnt--) 1299 if (((W)w)->active < periodiccnt--)
1080 { 1300 {
1081 periodics [w->active - 1] = periodics [periodiccnt]; 1301 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1082 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1302 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1083 } 1303 }
1084 1304
1305 ev_stop (EV_A_ (W)w);
1306}
1307
1308void
1309ev_idle_start (EV_P_ struct ev_idle *w)
1310{
1311 if (ev_is_active (w))
1312 return;
1313
1314 ev_start (EV_A_ (W)w, ++idlecnt);
1315 array_needsize (idles, idlemax, idlecnt, );
1316 idles [idlecnt - 1] = w;
1317}
1318
1319void
1320ev_idle_stop (EV_P_ struct ev_idle *w)
1321{
1322 ev_clear_pending (EV_A_ (W)w);
1323 if (ev_is_active (w))
1324 return;
1325
1326 idles [((W)w)->active - 1] = idles [--idlecnt];
1327 ev_stop (EV_A_ (W)w);
1328}
1329
1330void
1331ev_prepare_start (EV_P_ struct ev_prepare *w)
1332{
1333 if (ev_is_active (w))
1334 return;
1335
1336 ev_start (EV_A_ (W)w, ++preparecnt);
1337 array_needsize (prepares, preparemax, preparecnt, );
1338 prepares [preparecnt - 1] = w;
1339}
1340
1341void
1342ev_prepare_stop (EV_P_ struct ev_prepare *w)
1343{
1344 ev_clear_pending (EV_A_ (W)w);
1345 if (ev_is_active (w))
1346 return;
1347
1348 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1349 ev_stop (EV_A_ (W)w);
1350}
1351
1352void
1353ev_check_start (EV_P_ struct ev_check *w)
1354{
1355 if (ev_is_active (w))
1356 return;
1357
1358 ev_start (EV_A_ (W)w, ++checkcnt);
1359 array_needsize (checks, checkmax, checkcnt, );
1360 checks [checkcnt - 1] = w;
1361}
1362
1363void
1364ev_check_stop (EV_P_ struct ev_check *w)
1365{
1366 ev_clear_pending (EV_A_ (W)w);
1367 if (ev_is_active (w))
1368 return;
1369
1370 checks [((W)w)->active - 1] = checks [--checkcnt];
1085 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1086} 1372}
1087 1373
1088#ifndef SA_RESTART 1374#ifndef SA_RESTART
1089# define SA_RESTART 0 1375# define SA_RESTART 0
1090#endif 1376#endif
1091 1377
1092void 1378void
1093ev_signal_start (EV_P_ struct ev_signal *w) 1379ev_signal_start (EV_P_ struct ev_signal *w)
1094{ 1380{
1381#if EV_MULTIPLICITY
1382 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1383#endif
1095 if (ev_is_active (w)) 1384 if (ev_is_active (w))
1096 return; 1385 return;
1097 1386
1098 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1387 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1099 1388
1100 ev_start (EV_A_ (W)w, 1); 1389 ev_start (EV_A_ (W)w, 1);
1101 array_needsize (signals, signalmax, w->signum, signals_init); 1390 array_needsize (signals, signalmax, w->signum, signals_init);
1102 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1103 1392
1104 if (!w->next) 1393 if (!((WL)w)->next)
1105 { 1394 {
1395#if WIN32
1396 signal (w->signum, sighandler);
1397#else
1106 struct sigaction sa; 1398 struct sigaction sa;
1107 sa.sa_handler = sighandler; 1399 sa.sa_handler = sighandler;
1108 sigfillset (&sa.sa_mask); 1400 sigfillset (&sa.sa_mask);
1109 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1401 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1110 sigaction (w->signum, &sa, 0); 1402 sigaction (w->signum, &sa, 0);
1403#endif
1111 } 1404 }
1112} 1405}
1113 1406
1114void 1407void
1115ev_signal_stop (EV_P_ struct ev_signal *w) 1408ev_signal_stop (EV_P_ struct ev_signal *w)
1124 if (!signals [w->signum - 1].head) 1417 if (!signals [w->signum - 1].head)
1125 signal (w->signum, SIG_DFL); 1418 signal (w->signum, SIG_DFL);
1126} 1419}
1127 1420
1128void 1421void
1129ev_idle_start (EV_P_ struct ev_idle *w)
1130{
1131 if (ev_is_active (w))
1132 return;
1133
1134 ev_start (EV_A_ (W)w, ++idlecnt);
1135 array_needsize (idles, idlemax, idlecnt, );
1136 idles [idlecnt - 1] = w;
1137}
1138
1139void
1140ev_idle_stop (EV_P_ struct ev_idle *w)
1141{
1142 ev_clear_pending (EV_A_ (W)w);
1143 if (ev_is_active (w))
1144 return;
1145
1146 idles [w->active - 1] = idles [--idlecnt];
1147 ev_stop (EV_A_ (W)w);
1148}
1149
1150void
1151ev_prepare_start (EV_P_ struct ev_prepare *w)
1152{
1153 if (ev_is_active (w))
1154 return;
1155
1156 ev_start (EV_A_ (W)w, ++preparecnt);
1157 array_needsize (prepares, preparemax, preparecnt, );
1158 prepares [preparecnt - 1] = w;
1159}
1160
1161void
1162ev_prepare_stop (EV_P_ struct ev_prepare *w)
1163{
1164 ev_clear_pending (EV_A_ (W)w);
1165 if (ev_is_active (w))
1166 return;
1167
1168 prepares [w->active - 1] = prepares [--preparecnt];
1169 ev_stop (EV_A_ (W)w);
1170}
1171
1172void
1173ev_check_start (EV_P_ struct ev_check *w)
1174{
1175 if (ev_is_active (w))
1176 return;
1177
1178 ev_start (EV_A_ (W)w, ++checkcnt);
1179 array_needsize (checks, checkmax, checkcnt, );
1180 checks [checkcnt - 1] = w;
1181}
1182
1183void
1184ev_check_stop (EV_P_ struct ev_check *w)
1185{
1186 ev_clear_pending (EV_A_ (W)w);
1187 if (ev_is_active (w))
1188 return;
1189
1190 checks [w->active - 1] = checks [--checkcnt];
1191 ev_stop (EV_A_ (W)w);
1192}
1193
1194void
1195ev_child_start (EV_P_ struct ev_child *w) 1422ev_child_start (EV_P_ struct ev_child *w)
1196{ 1423{
1424#if EV_MULTIPLICITY
1425 assert (("child watchers are only supported in the default loop", loop == default_loop));
1426#endif
1197 if (ev_is_active (w)) 1427 if (ev_is_active (w))
1198 return; 1428 return;
1199 1429
1200 ev_start (EV_A_ (W)w, 1); 1430 ev_start (EV_A_ (W)w, 1);
1201 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1431 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1228 void (*cb)(int revents, void *arg) = once->cb; 1458 void (*cb)(int revents, void *arg) = once->cb;
1229 void *arg = once->arg; 1459 void *arg = once->arg;
1230 1460
1231 ev_io_stop (EV_A_ &once->io); 1461 ev_io_stop (EV_A_ &once->io);
1232 ev_timer_stop (EV_A_ &once->to); 1462 ev_timer_stop (EV_A_ &once->to);
1233 free (once); 1463 ev_free (once);
1234 1464
1235 cb (revents, arg); 1465 cb (revents, arg);
1236} 1466}
1237 1467
1238static void 1468static void
1248} 1478}
1249 1479
1250void 1480void
1251ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1481ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1252{ 1482{
1253 struct ev_once *once = malloc (sizeof (struct ev_once)); 1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1254 1484
1255 if (!once) 1485 if (!once)
1256 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1257 else 1487 else
1258 { 1488 {
1273 ev_timer_start (EV_A_ &once->to); 1503 ev_timer_start (EV_A_ &once->to);
1274 } 1504 }
1275 } 1505 }
1276} 1506}
1277 1507
1278/*****************************************************************************/
1279
1280#if 0
1281
1282struct ev_io wio;
1283
1284static void
1285sin_cb (struct ev_io *w, int revents)
1286{
1287 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1288}
1289
1290static void
1291ocb (struct ev_timer *w, int revents)
1292{
1293 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1294 ev_timer_stop (w);
1295 ev_timer_start (w);
1296}
1297
1298static void
1299scb (struct ev_signal *w, int revents)
1300{
1301 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1302 ev_io_stop (&wio);
1303 ev_io_start (&wio);
1304}
1305
1306static void
1307gcb (struct ev_signal *w, int revents)
1308{
1309 fprintf (stderr, "generic %x\n", revents);
1310
1311}
1312
1313int main (void)
1314{
1315 ev_init (0);
1316
1317 ev_io_init (&wio, sin_cb, 0, EV_READ);
1318 ev_io_start (&wio);
1319
1320 struct ev_timer t[10000];
1321
1322#if 0
1323 int i;
1324 for (i = 0; i < 10000; ++i)
1325 {
1326 struct ev_timer *w = t + i;
1327 ev_watcher_init (w, ocb, i);
1328 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1329 ev_timer_start (w);
1330 if (drand48 () < 0.5)
1331 ev_timer_stop (w);
1332 }
1333#endif
1334
1335 struct ev_timer t1;
1336 ev_timer_init (&t1, ocb, 5, 10);
1337 ev_timer_start (&t1);
1338
1339 struct ev_signal sig;
1340 ev_signal_init (&sig, scb, SIGQUIT);
1341 ev_signal_start (&sig);
1342
1343 struct ev_check cw;
1344 ev_check_init (&cw, gcb);
1345 ev_check_start (&cw);
1346
1347 struct ev_idle iw;
1348 ev_idle_init (&iw, gcb);
1349 ev_idle_start (&iw);
1350
1351 ev_loop (0);
1352
1353 return 0;
1354}
1355
1356#endif
1357
1358
1359
1360

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines