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

Comparing libev/ev.c (file contents):
Revision 1.59 by root, Sun Nov 4 18:15:16 2007 UTC vs.
Revision 1.70 by root, Tue Nov 6 00:52:32 2007 UTC

28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#ifndef EV_STANDALONE 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33
34# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1
36# define EV_USE_REALTIME 1
37# endif
38
39# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1
41# endif
42
43# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1
45# endif
46
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1
49# endif
50
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1
53# endif
54
33#endif 55#endif
34 56
35#include <math.h> 57#include <math.h>
36#include <stdlib.h> 58#include <stdlib.h>
37#include <unistd.h> 59#include <unistd.h>
70 92
71#ifndef EV_USE_KQUEUE 93#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0 94# define EV_USE_KQUEUE 0
73#endif 95#endif
74 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
103#endif
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
78 108
79/**/ 109/**/
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
343/* susually called after fork if method needs to re-arm all fds from scratch */ 434/* usually called after fork if method needs to re-arm all fds from scratch */
344static void 435static void
345fd_rearm_all (EV_P) 436fd_rearm_all (EV_P)
346{ 437{
347 int fd; 438 int fd;
348 439
349 /* this should be highly optimised to not do anything but set a flag */ 440 /* this should be highly optimised to not do anything but set a flag */
350 for (fd = 0; fd < anfdmax; ++fd) 441 for (fd = 0; fd < anfdmax; ++fd)
351 if (anfds [fd].events) 442 if (anfds [fd].events)
352 { 443 {
353 anfds [fd].events = 0; 444 anfds [fd].events = 0;
354 fd_change (fd); 445 fd_change (EV_A_ fd);
355 } 446 }
356} 447}
357 448
358/*****************************************************************************/ 449/*****************************************************************************/
359 450
363 WT w = heap [k]; 454 WT w = heap [k];
364 455
365 while (k && heap [k >> 1]->at > w->at) 456 while (k && heap [k >> 1]->at > w->at)
366 { 457 {
367 heap [k] = heap [k >> 1]; 458 heap [k] = heap [k >> 1];
368 heap [k]->active = k + 1; 459 ((W)heap [k])->active = k + 1;
369 k >>= 1; 460 k >>= 1;
370 } 461 }
371 462
372 heap [k] = w; 463 heap [k] = w;
373 heap [k]->active = k + 1; 464 ((W)heap [k])->active = k + 1;
374 465
375} 466}
376 467
377static void 468static void
378downheap (WT *heap, int N, int k) 469downheap (WT *heap, int N, int k)
388 479
389 if (w->at <= heap [j]->at) 480 if (w->at <= heap [j]->at)
390 break; 481 break;
391 482
392 heap [k] = heap [j]; 483 heap [k] = heap [j];
393 heap [k]->active = k + 1; 484 ((W)heap [k])->active = k + 1;
394 k = j; 485 k = j;
395 } 486 }
396 487
397 heap [k] = w; 488 heap [k] = w;
398 heap [k]->active = k + 1; 489 ((W)heap [k])->active = k + 1;
399} 490}
400 491
401/*****************************************************************************/ 492/*****************************************************************************/
402 493
403typedef struct 494typedef struct
404{ 495{
405 struct ev_watcher_list *head; 496 WL head;
406 sig_atomic_t volatile gotsig; 497 sig_atomic_t volatile gotsig;
407} ANSIG; 498} ANSIG;
408 499
409static ANSIG *signals; 500static ANSIG *signals;
410static int signalmax; 501static int signalmax;
426} 517}
427 518
428static void 519static void
429sighandler (int signum) 520sighandler (int signum)
430{ 521{
522#if WIN32
523 signal (signum, sighandler);
524#endif
525
431 signals [signum - 1].gotsig = 1; 526 signals [signum - 1].gotsig = 1;
432 527
433 if (!gotsig) 528 if (!gotsig)
434 { 529 {
435 int old_errno = errno; 530 int old_errno = errno;
440} 535}
441 536
442static void 537static void
443sigcb (EV_P_ struct ev_io *iow, int revents) 538sigcb (EV_P_ struct ev_io *iow, int revents)
444{ 539{
445 struct ev_watcher_list *w; 540 WL w;
446 int signum; 541 int signum;
447 542
448 read (sigpipe [0], &revents, 1); 543 read (sigpipe [0], &revents, 1);
449 gotsig = 0; 544 gotsig = 0;
450 545
492 struct ev_child *w; 587 struct ev_child *w;
493 588
494 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)
495 if (w->pid == pid || !w->pid) 590 if (w->pid == pid || !w->pid)
496 { 591 {
497 w->priority = sw->priority; /* need to do it *now* */ 592 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
498 w->rpid = pid; 593 w->rpid = pid;
499 w->rstatus = status; 594 w->rstatus = status;
500 event (EV_A_ (W)w, EV_CHILD); 595 event (EV_A_ (W)w, EV_CHILD);
501 } 596 }
502} 597}
503 598
504static void 599static void
586 methods = atoi (getenv ("LIBEV_METHODS")); 681 methods = atoi (getenv ("LIBEV_METHODS"));
587 else 682 else
588 methods = EVMETHOD_ANY; 683 methods = EVMETHOD_ANY;
589 684
590 method = 0; 685 method = 0;
686#if EV_USE_WIN32
687 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
688#endif
591#if EV_USE_KQUEUE 689#if EV_USE_KQUEUE
592 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 690 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
593#endif 691#endif
594#if EV_USE_EPOLL 692#if EV_USE_EPOLL
595 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 693 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
598 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
599#endif 697#endif
600#if EV_USE_SELECT 698#if EV_USE_SELECT
601 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
602#endif 700#endif
701
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
603 } 704 }
604} 705}
605 706
606void 707void
607loop_destroy (EV_P) 708loop_destroy (EV_P)
608{ 709{
710 int i;
711
712#if EV_USE_WIN32
713 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
714#endif
609#if EV_USE_KQUEUE 715#if EV_USE_KQUEUE
610 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 716 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
611#endif 717#endif
612#if EV_USE_EPOLL 718#if EV_USE_EPOLL
613 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 719 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
617#endif 723#endif
618#if EV_USE_SELECT 724#if EV_USE_SELECT
619 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 725 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
620#endif 726#endif
621 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
622 method = 0; 738 method = 0;
623 /*TODO*/
624} 739}
625 740
626void 741static void
627loop_fork (EV_P) 742loop_fork (EV_P)
628{ 743{
629 /*TODO*/
630#if EV_USE_EPOLL 744#if EV_USE_EPOLL
631 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
632#endif 746#endif
633#if EV_USE_KQUEUE 747#if EV_USE_KQUEUE
634 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
635#endif 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;
636} 767}
637 768
638#if EV_MULTIPLICITY 769#if EV_MULTIPLICITY
639struct ev_loop * 770struct ev_loop *
640ev_loop_new (int methods) 771ev_loop_new (int methods)
641{ 772{
642 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 773 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
774
775 memset (loop, 0, sizeof (struct ev_loop));
643 776
644 loop_init (EV_A_ methods); 777 loop_init (EV_A_ methods);
645 778
646 if (ev_methods (EV_A)) 779 if (ev_method (EV_A))
647 return loop; 780 return loop;
648 781
649 return 0; 782 return 0;
650} 783}
651 784
652void 785void
653ev_loop_destroy (EV_P) 786ev_loop_destroy (EV_P)
654{ 787{
655 loop_destroy (EV_A); 788 loop_destroy (EV_A);
656 free (loop); 789 ev_free (loop);
657} 790}
658 791
659void 792void
660ev_loop_fork (EV_P) 793ev_loop_fork (EV_P)
661{ 794{
662 loop_fork (EV_A); 795 postfork = 1;
663} 796}
664 797
665#endif 798#endif
666 799
667#if EV_MULTIPLICITY 800#if EV_MULTIPLICITY
690 823
691 loop_init (EV_A_ methods); 824 loop_init (EV_A_ methods);
692 825
693 if (ev_method (EV_A)) 826 if (ev_method (EV_A))
694 { 827 {
695 ev_watcher_init (&sigev, sigcb);
696 ev_set_priority (&sigev, EV_MAXPRI);
697 siginit (EV_A); 828 siginit (EV_A);
698 829
699#ifndef WIN32 830#ifndef WIN32
700 ev_signal_init (&childev, childcb, SIGCHLD); 831 ev_signal_init (&childev, childcb, SIGCHLD);
701 ev_set_priority (&childev, EV_MAXPRI); 832 ev_set_priority (&childev, EV_MAXPRI);
728 859
729 loop_destroy (EV_A); 860 loop_destroy (EV_A);
730} 861}
731 862
732void 863void
733ev_default_fork (EV_P) 864ev_default_fork (void)
734{ 865{
735 loop_fork (EV_A); 866#if EV_MULTIPLICITY
867 struct ev_loop *loop = default_loop;
868#endif
736 869
737 ev_io_stop (EV_A_ &sigev); 870 if (method)
738 close (sigpipe [0]); 871 postfork = 1;
739 close (sigpipe [1]);
740 pipe (sigpipe);
741
742 ev_ref (EV_A); /* signal watcher */
743 siginit (EV_A);
744} 872}
745 873
746/*****************************************************************************/ 874/*****************************************************************************/
747 875
748static void 876static void
764} 892}
765 893
766static void 894static void
767timers_reify (EV_P) 895timers_reify (EV_P)
768{ 896{
769 while (timercnt && timers [0]->at <= mn_now) 897 while (timercnt && ((WT)timers [0])->at <= mn_now)
770 { 898 {
771 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)));
772 902
773 /* first reschedule or stop timer */ 903 /* first reschedule or stop timer */
774 if (w->repeat) 904 if (w->repeat)
775 { 905 {
776 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.));
777 w->at = mn_now + w->repeat; 907 ((WT)w)->at = mn_now + w->repeat;
778 downheap ((WT *)timers, timercnt, 0); 908 downheap ((WT *)timers, timercnt, 0);
779 } 909 }
780 else 910 else
781 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 911 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
782 912
785} 915}
786 916
787static void 917static void
788periodics_reify (EV_P) 918periodics_reify (EV_P)
789{ 919{
790 while (periodiccnt && periodics [0]->at <= rt_now) 920 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
791 { 921 {
792 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)));
793 925
794 /* first reschedule or stop timer */ 926 /* first reschedule or stop timer */
795 if (w->interval) 927 if (w->interval)
796 { 928 {
797 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;
798 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));
799 downheap ((WT *)periodics, periodiccnt, 0); 931 downheap ((WT *)periodics, periodiccnt, 0);
800 } 932 }
801 else 933 else
802 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 934 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
803 935
815 { 947 {
816 struct ev_periodic *w = periodics [i]; 948 struct ev_periodic *w = periodics [i];
817 949
818 if (w->interval) 950 if (w->interval)
819 { 951 {
820 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;
821 953
822 if (fabs (diff) >= 1e-4) 954 if (fabs (diff) >= 1e-4)
823 { 955 {
824 ev_periodic_stop (EV_A_ w); 956 ev_periodic_stop (EV_A_ w);
825 ev_periodic_start (EV_A_ w); 957 ev_periodic_start (EV_A_ w);
886 { 1018 {
887 periodics_reschedule (EV_A); 1019 periodics_reschedule (EV_A);
888 1020
889 /* 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 */
890 for (i = 0; i < timercnt; ++i) 1022 for (i = 0; i < timercnt; ++i)
891 timers [i]->at += rt_now - mn_now; 1023 ((WT)timers [i])->at += rt_now - mn_now;
892 } 1024 }
893 1025
894 mn_now = rt_now; 1026 mn_now = rt_now;
895 } 1027 }
896} 1028}
922 { 1054 {
923 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
924 call_pending (EV_A); 1056 call_pending (EV_A);
925 } 1057 }
926 1058
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1061 loop_fork (EV_A);
1062
927 /* update fd-related kernel structures */ 1063 /* update fd-related kernel structures */
928 fd_reify (EV_A); 1064 fd_reify (EV_A);
929 1065
930 /* calculate blocking time */ 1066 /* calculate blocking time */
931 1067
947 { 1083 {
948 block = MAX_BLOCKTIME; 1084 block = MAX_BLOCKTIME;
949 1085
950 if (timercnt) 1086 if (timercnt)
951 { 1087 {
952 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1088 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
953 if (block > to) block = to; 1089 if (block > to) block = to;
954 } 1090 }
955 1091
956 if (periodiccnt) 1092 if (periodiccnt)
957 { 1093 {
958 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1094 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
959 if (block > to) block = to; 1095 if (block > to) block = to;
960 } 1096 }
961 1097
962 if (block < 0.) block = 0.; 1098 if (block < 0.) block = 0.;
963 } 1099 }
1080ev_timer_start (EV_P_ struct ev_timer *w) 1216ev_timer_start (EV_P_ struct ev_timer *w)
1081{ 1217{
1082 if (ev_is_active (w)) 1218 if (ev_is_active (w))
1083 return; 1219 return;
1084 1220
1085 w->at += mn_now; 1221 ((WT)w)->at += mn_now;
1086 1222
1087 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.));
1088 1224
1089 ev_start (EV_A_ (W)w, ++timercnt); 1225 ev_start (EV_A_ (W)w, ++timercnt);
1090 array_needsize (timers, timermax, timercnt, ); 1226 array_needsize (timers, timermax, timercnt, );
1091 timers [timercnt - 1] = w; 1227 timers [timercnt - 1] = w;
1092 upheap ((WT *)timers, timercnt - 1); 1228 upheap ((WT *)timers, timercnt - 1);
1229
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1093} 1231}
1094 1232
1095void 1233void
1096ev_timer_stop (EV_P_ struct ev_timer *w) 1234ev_timer_stop (EV_P_ struct ev_timer *w)
1097{ 1235{
1098 ev_clear_pending (EV_A_ (W)w); 1236 ev_clear_pending (EV_A_ (W)w);
1099 if (!ev_is_active (w)) 1237 if (!ev_is_active (w))
1100 return; 1238 return;
1101 1239
1240 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1241
1102 if (w->active < timercnt--) 1242 if (((W)w)->active < timercnt--)
1103 { 1243 {
1104 timers [w->active - 1] = timers [timercnt]; 1244 timers [((W)w)->active - 1] = timers [timercnt];
1105 downheap ((WT *)timers, timercnt, w->active - 1); 1245 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1106 } 1246 }
1107 1247
1108 w->at = w->repeat; 1248 ((WT)w)->at = w->repeat;
1109 1249
1110 ev_stop (EV_A_ (W)w); 1250 ev_stop (EV_A_ (W)w);
1111} 1251}
1112 1252
1113void 1253void
1115{ 1255{
1116 if (ev_is_active (w)) 1256 if (ev_is_active (w))
1117 { 1257 {
1118 if (w->repeat) 1258 if (w->repeat)
1119 { 1259 {
1120 w->at = mn_now + w->repeat; 1260 ((WT)w)->at = mn_now + w->repeat;
1121 downheap ((WT *)timers, timercnt, w->active - 1); 1261 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1122 } 1262 }
1123 else 1263 else
1124 ev_timer_stop (EV_A_ w); 1264 ev_timer_stop (EV_A_ w);
1125 } 1265 }
1126 else if (w->repeat) 1266 else if (w->repeat)
1135 1275
1136 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.));
1137 1277
1138 /* 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 */
1139 if (w->interval) 1279 if (w->interval)
1140 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;
1141 1281
1142 ev_start (EV_A_ (W)w, ++periodiccnt); 1282 ev_start (EV_A_ (W)w, ++periodiccnt);
1143 array_needsize (periodics, periodicmax, periodiccnt, ); 1283 array_needsize (periodics, periodicmax, periodiccnt, );
1144 periodics [periodiccnt - 1] = w; 1284 periodics [periodiccnt - 1] = w;
1145 upheap ((WT *)periodics, periodiccnt - 1); 1285 upheap ((WT *)periodics, periodiccnt - 1);
1286
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1146} 1288}
1147 1289
1148void 1290void
1149ev_periodic_stop (EV_P_ struct ev_periodic *w) 1291ev_periodic_stop (EV_P_ struct ev_periodic *w)
1150{ 1292{
1151 ev_clear_pending (EV_A_ (W)w); 1293 ev_clear_pending (EV_A_ (W)w);
1152 if (!ev_is_active (w)) 1294 if (!ev_is_active (w))
1153 return; 1295 return;
1154 1296
1297 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1298
1155 if (w->active < periodiccnt--) 1299 if (((W)w)->active < periodiccnt--)
1156 { 1300 {
1157 periodics [w->active - 1] = periodics [periodiccnt]; 1301 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1158 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1302 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1159 } 1303 }
1160 1304
1161 ev_stop (EV_A_ (W)w); 1305 ev_stop (EV_A_ (W)w);
1162} 1306}
1163 1307
1177{ 1321{
1178 ev_clear_pending (EV_A_ (W)w); 1322 ev_clear_pending (EV_A_ (W)w);
1179 if (ev_is_active (w)) 1323 if (ev_is_active (w))
1180 return; 1324 return;
1181 1325
1182 idles [w->active - 1] = idles [--idlecnt]; 1326 idles [((W)w)->active - 1] = idles [--idlecnt];
1183 ev_stop (EV_A_ (W)w); 1327 ev_stop (EV_A_ (W)w);
1184} 1328}
1185 1329
1186void 1330void
1187ev_prepare_start (EV_P_ struct ev_prepare *w) 1331ev_prepare_start (EV_P_ struct ev_prepare *w)
1199{ 1343{
1200 ev_clear_pending (EV_A_ (W)w); 1344 ev_clear_pending (EV_A_ (W)w);
1201 if (ev_is_active (w)) 1345 if (ev_is_active (w))
1202 return; 1346 return;
1203 1347
1204 prepares [w->active - 1] = prepares [--preparecnt]; 1348 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1205 ev_stop (EV_A_ (W)w); 1349 ev_stop (EV_A_ (W)w);
1206} 1350}
1207 1351
1208void 1352void
1209ev_check_start (EV_P_ struct ev_check *w) 1353ev_check_start (EV_P_ struct ev_check *w)
1221{ 1365{
1222 ev_clear_pending (EV_A_ (W)w); 1366 ev_clear_pending (EV_A_ (W)w);
1223 if (ev_is_active (w)) 1367 if (ev_is_active (w))
1224 return; 1368 return;
1225 1369
1226 checks [w->active - 1] = checks [--checkcnt]; 1370 checks [((W)w)->active - 1] = checks [--checkcnt];
1227 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1228} 1372}
1229 1373
1230#ifndef SA_RESTART 1374#ifndef SA_RESTART
1231# define SA_RESTART 0 1375# define SA_RESTART 0
1244 1388
1245 ev_start (EV_A_ (W)w, 1); 1389 ev_start (EV_A_ (W)w, 1);
1246 array_needsize (signals, signalmax, w->signum, signals_init); 1390 array_needsize (signals, signalmax, w->signum, signals_init);
1247 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1248 1392
1249 if (!w->next) 1393 if (!((WL)w)->next)
1250 { 1394 {
1395#if WIN32
1396 signal (w->signum, sighandler);
1397#else
1251 struct sigaction sa; 1398 struct sigaction sa;
1252 sa.sa_handler = sighandler; 1399 sa.sa_handler = sighandler;
1253 sigfillset (&sa.sa_mask); 1400 sigfillset (&sa.sa_mask);
1254 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 */
1255 sigaction (w->signum, &sa, 0); 1402 sigaction (w->signum, &sa, 0);
1403#endif
1256 } 1404 }
1257} 1405}
1258 1406
1259void 1407void
1260ev_signal_stop (EV_P_ struct ev_signal *w) 1408ev_signal_stop (EV_P_ struct ev_signal *w)
1310 void (*cb)(int revents, void *arg) = once->cb; 1458 void (*cb)(int revents, void *arg) = once->cb;
1311 void *arg = once->arg; 1459 void *arg = once->arg;
1312 1460
1313 ev_io_stop (EV_A_ &once->io); 1461 ev_io_stop (EV_A_ &once->io);
1314 ev_timer_stop (EV_A_ &once->to); 1462 ev_timer_stop (EV_A_ &once->to);
1315 free (once); 1463 ev_free (once);
1316 1464
1317 cb (revents, arg); 1465 cb (revents, arg);
1318} 1466}
1319 1467
1320static void 1468static void
1330} 1478}
1331 1479
1332void 1480void
1333ev_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)
1334{ 1482{
1335 struct ev_once *once = malloc (sizeof (struct ev_once)); 1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1336 1484
1337 if (!once) 1485 if (!once)
1338 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1339 else 1487 else
1340 { 1488 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines