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

Comparing libev/ev.c (file contents):
Revision 1.58 by root, Sun Nov 4 16:52:52 2007 UTC vs.
Revision 1.68 by root, Mon Nov 5 20:19:00 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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_EMBED 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
93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 123#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
94#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 124#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 125#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 126/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
97 127
98#ifndef EV_EMBED
99# include "ev.h" 128#include "ev.h"
100#endif
101 129
102#if __GNUC__ >= 3 130#if __GNUC__ >= 3
103# define expect(expr,value) __builtin_expect ((expr),(value)) 131# define expect(expr,value) __builtin_expect ((expr),(value))
104# define inline inline 132# define inline inline
105#else 133#else
117typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
118typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
119 147
120static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
121 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
122/*****************************************************************************/ 156/*****************************************************************************/
123 157
124typedef struct 158typedef struct
125{ 159{
126 struct ev_watcher_list *head; 160 WL head;
127 unsigned char events; 161 unsigned char events;
128 unsigned char reify; 162 unsigned char reify;
129} ANFD; 163} ANFD;
130 164
131typedef struct 165typedef struct
204 base = realloc (base, sizeof (*base) * (newcnt)); \ 238 base = realloc (base, sizeof (*base) * (newcnt)); \
205 init (base + cur, newcnt - cur); \ 239 init (base + cur, newcnt - cur); \
206 cur = newcnt; \ 240 cur = newcnt; \
207 } 241 }
208 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
209/*****************************************************************************/ 254/*****************************************************************************/
210 255
211static void 256static void
212anfds_init (ANFD *base, int count) 257anfds_init (ANFD *base, int count)
213{ 258{
278 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)
279 events |= w->events; 324 events |= w->events;
280 325
281 anfd->reify = 0; 326 anfd->reify = 0;
282 327
283 if (anfd->events != events)
284 {
285 method_modify (EV_A_ fd, anfd->events, events); 328 method_modify (EV_A_ fd, anfd->events, events);
286 anfd->events = events; 329 anfd->events = events;
287 }
288 } 330 }
289 331
290 fdchangecnt = 0; 332 fdchangecnt = 0;
291} 333}
292 334
329 371
330/* 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 */
331static void 373static void
332fd_enomem (EV_P) 374fd_enomem (EV_P)
333{ 375{
334 int fd = anfdmax; 376 int fd;
335 377
336 while (fd--) 378 for (fd = anfdmax; fd--; )
337 if (anfds [fd].events) 379 if (anfds [fd].events)
338 { 380 {
339 close (fd);
340 fd_kill (EV_A_ fd); 381 fd_kill (EV_A_ fd);
341 return; 382 return;
342 } 383 }
343} 384}
344 385
351 /* this should be highly optimised to not do anything but set a flag */ 392 /* this should be highly optimised to not do anything but set a flag */
352 for (fd = 0; fd < anfdmax; ++fd) 393 for (fd = 0; fd < anfdmax; ++fd)
353 if (anfds [fd].events) 394 if (anfds [fd].events)
354 { 395 {
355 anfds [fd].events = 0; 396 anfds [fd].events = 0;
356 fd_change (fd); 397 fd_change (EV_A_ fd);
357 } 398 }
358} 399}
359 400
360/*****************************************************************************/ 401/*****************************************************************************/
361 402
365 WT w = heap [k]; 406 WT w = heap [k];
366 407
367 while (k && heap [k >> 1]->at > w->at) 408 while (k && heap [k >> 1]->at > w->at)
368 { 409 {
369 heap [k] = heap [k >> 1]; 410 heap [k] = heap [k >> 1];
370 heap [k]->active = k + 1; 411 ((W)heap [k])->active = k + 1;
371 k >>= 1; 412 k >>= 1;
372 } 413 }
373 414
374 heap [k] = w; 415 heap [k] = w;
375 heap [k]->active = k + 1; 416 ((W)heap [k])->active = k + 1;
376 417
377} 418}
378 419
379static void 420static void
380downheap (WT *heap, int N, int k) 421downheap (WT *heap, int N, int k)
390 431
391 if (w->at <= heap [j]->at) 432 if (w->at <= heap [j]->at)
392 break; 433 break;
393 434
394 heap [k] = heap [j]; 435 heap [k] = heap [j];
395 heap [k]->active = k + 1; 436 ((W)heap [k])->active = k + 1;
396 k = j; 437 k = j;
397 } 438 }
398 439
399 heap [k] = w; 440 heap [k] = w;
400 heap [k]->active = k + 1; 441 ((W)heap [k])->active = k + 1;
401} 442}
402 443
403/*****************************************************************************/ 444/*****************************************************************************/
404 445
405typedef struct 446typedef struct
406{ 447{
407 struct ev_watcher_list *head; 448 WL head;
408 sig_atomic_t volatile gotsig; 449 sig_atomic_t volatile gotsig;
409} ANSIG; 450} ANSIG;
410 451
411static ANSIG *signals; 452static ANSIG *signals;
412static int signalmax; 453static int signalmax;
413 454
414static int sigpipe [2]; 455static int sigpipe [2];
415static sig_atomic_t volatile gotsig; 456static sig_atomic_t volatile gotsig;
457static struct ev_io sigev;
416 458
417static void 459static void
418signals_init (ANSIG *base, int count) 460signals_init (ANSIG *base, int count)
419{ 461{
420 while (count--) 462 while (count--)
427} 469}
428 470
429static void 471static void
430sighandler (int signum) 472sighandler (int signum)
431{ 473{
474#if WIN32
475 signal (signum, sighandler);
476#endif
477
432 signals [signum - 1].gotsig = 1; 478 signals [signum - 1].gotsig = 1;
433 479
434 if (!gotsig) 480 if (!gotsig)
435 { 481 {
436 int old_errno = errno; 482 int old_errno = errno;
441} 487}
442 488
443static void 489static void
444sigcb (EV_P_ struct ev_io *iow, int revents) 490sigcb (EV_P_ struct ev_io *iow, int revents)
445{ 491{
446 struct ev_watcher_list *w; 492 WL w;
447 int signum; 493 int signum;
448 494
449 read (sigpipe [0], &revents, 1); 495 read (sigpipe [0], &revents, 1);
450 gotsig = 0; 496 gotsig = 0;
451 497
478 524
479/*****************************************************************************/ 525/*****************************************************************************/
480 526
481#ifndef WIN32 527#ifndef WIN32
482 528
529static struct ev_child *childs [PID_HASHSIZE];
530static struct ev_signal childev;
531
483#ifndef WCONTINUED 532#ifndef WCONTINUED
484# define WCONTINUED 0 533# define WCONTINUED 0
485#endif 534#endif
486 535
487static void 536static void
490 struct ev_child *w; 539 struct ev_child *w;
491 540
492 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 541 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
493 if (w->pid == pid || !w->pid) 542 if (w->pid == pid || !w->pid)
494 { 543 {
495 w->priority = sw->priority; /* need to do it *now* */ 544 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
496 w->rpid = pid; 545 w->rpid = pid;
497 w->rstatus = status; 546 w->rstatus = status;
498 event (EV_A_ (W)w, EV_CHILD); 547 event (EV_A_ (W)w, EV_CHILD);
499 } 548 }
500} 549}
501 550
502static void 551static void
522# include "ev_kqueue.c" 571# include "ev_kqueue.c"
523#endif 572#endif
524#if EV_USE_EPOLL 573#if EV_USE_EPOLL
525# include "ev_epoll.c" 574# include "ev_epoll.c"
526#endif 575#endif
527#if EV_USEV_POLL 576#if EV_USE_POLL
528# include "ev_poll.c" 577# include "ev_poll.c"
529#endif 578#endif
530#if EV_USE_SELECT 579#if EV_USE_SELECT
531# include "ev_select.c" 580# include "ev_select.c"
532#endif 581#endif
584 methods = atoi (getenv ("LIBEV_METHODS")); 633 methods = atoi (getenv ("LIBEV_METHODS"));
585 else 634 else
586 methods = EVMETHOD_ANY; 635 methods = EVMETHOD_ANY;
587 636
588 method = 0; 637 method = 0;
638#if EV_USE_WIN32
639 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
640#endif
589#if EV_USE_KQUEUE 641#if EV_USE_KQUEUE
590 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 642 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
591#endif 643#endif
592#if EV_USE_EPOLL 644#if EV_USE_EPOLL
593 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 645 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
594#endif 646#endif
595#if EV_USEV_POLL 647#if EV_USE_POLL
596 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 648 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
597#endif 649#endif
598#if EV_USE_SELECT 650#if EV_USE_SELECT
599 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 651 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
600#endif 652#endif
602} 654}
603 655
604void 656void
605loop_destroy (EV_P) 657loop_destroy (EV_P)
606{ 658{
659 int i;
660
661#if EV_USE_WIN32
662 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
663#endif
607#if EV_USE_KQUEUE 664#if EV_USE_KQUEUE
608 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 665 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
609#endif 666#endif
610#if EV_USE_EPOLL 667#if EV_USE_EPOLL
611 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 668 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
612#endif 669#endif
613#if EV_USEV_POLL 670#if EV_USE_POLL
614 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 671 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
615#endif 672#endif
616#if EV_USE_SELECT 673#if EV_USE_SELECT
617 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 674 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
618#endif 675#endif
676
677 for (i = NUMPRI; i--; )
678 array_free (pending, [i]);
679
680 array_free (fdchange, );
681 array_free (timer, );
682 array_free (periodic, );
683 array_free (idle, );
684 array_free (prepare, );
685 array_free (check, );
619 686
620 method = 0; 687 method = 0;
621 /*TODO*/ 688 /*TODO*/
622} 689}
623 690
639{ 706{
640 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 707 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
641 708
642 loop_init (EV_A_ methods); 709 loop_init (EV_A_ methods);
643 710
644 if (ev_methods (EV_A)) 711 if (ev_method (EV_A))
645 return loop; 712 return loop;
646 713
647 return 0; 714 return 0;
648} 715}
649 716
726 793
727 loop_destroy (EV_A); 794 loop_destroy (EV_A);
728} 795}
729 796
730void 797void
731ev_default_fork (EV_P) 798ev_default_fork (void)
732{ 799{
800#if EV_MULTIPLICITY
801 struct ev_loop *loop = default_loop;
802#endif
803
733 loop_fork (EV_A); 804 loop_fork (EV_A);
734 805
735 ev_io_stop (EV_A_ &sigev); 806 ev_io_stop (EV_A_ &sigev);
736 close (sigpipe [0]); 807 close (sigpipe [0]);
737 close (sigpipe [1]); 808 close (sigpipe [1]);
762} 833}
763 834
764static void 835static void
765timers_reify (EV_P) 836timers_reify (EV_P)
766{ 837{
767 while (timercnt && timers [0]->at <= mn_now) 838 while (timercnt && ((WT)timers [0])->at <= mn_now)
768 { 839 {
769 struct ev_timer *w = timers [0]; 840 struct ev_timer *w = timers [0];
841
842 assert (("inactive timer on timer heap detected", ev_is_active (w)));
770 843
771 /* first reschedule or stop timer */ 844 /* first reschedule or stop timer */
772 if (w->repeat) 845 if (w->repeat)
773 { 846 {
774 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 847 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
775 w->at = mn_now + w->repeat; 848 ((WT)w)->at = mn_now + w->repeat;
776 downheap ((WT *)timers, timercnt, 0); 849 downheap ((WT *)timers, timercnt, 0);
777 } 850 }
778 else 851 else
779 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 852 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
780 853
783} 856}
784 857
785static void 858static void
786periodics_reify (EV_P) 859periodics_reify (EV_P)
787{ 860{
788 while (periodiccnt && periodics [0]->at <= rt_now) 861 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
789 { 862 {
790 struct ev_periodic *w = periodics [0]; 863 struct ev_periodic *w = periodics [0];
864
865 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
791 866
792 /* first reschedule or stop timer */ 867 /* first reschedule or stop timer */
793 if (w->interval) 868 if (w->interval)
794 { 869 {
795 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 870 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
796 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 871 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
797 downheap ((WT *)periodics, periodiccnt, 0); 872 downheap ((WT *)periodics, periodiccnt, 0);
798 } 873 }
799 else 874 else
800 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 875 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
801 876
813 { 888 {
814 struct ev_periodic *w = periodics [i]; 889 struct ev_periodic *w = periodics [i];
815 890
816 if (w->interval) 891 if (w->interval)
817 { 892 {
818 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 893 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
819 894
820 if (fabs (diff) >= 1e-4) 895 if (fabs (diff) >= 1e-4)
821 { 896 {
822 ev_periodic_stop (EV_A_ w); 897 ev_periodic_stop (EV_A_ w);
823 ev_periodic_start (EV_A_ w); 898 ev_periodic_start (EV_A_ w);
884 { 959 {
885 periodics_reschedule (EV_A); 960 periodics_reschedule (EV_A);
886 961
887 /* adjust timers. this is easy, as the offset is the same for all */ 962 /* adjust timers. this is easy, as the offset is the same for all */
888 for (i = 0; i < timercnt; ++i) 963 for (i = 0; i < timercnt; ++i)
889 timers [i]->at += rt_now - mn_now; 964 ((WT)timers [i])->at += rt_now - mn_now;
890 } 965 }
891 966
892 mn_now = rt_now; 967 mn_now = rt_now;
893 } 968 }
894} 969}
945 { 1020 {
946 block = MAX_BLOCKTIME; 1021 block = MAX_BLOCKTIME;
947 1022
948 if (timercnt) 1023 if (timercnt)
949 { 1024 {
950 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1025 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
951 if (block > to) block = to; 1026 if (block > to) block = to;
952 } 1027 }
953 1028
954 if (periodiccnt) 1029 if (periodiccnt)
955 { 1030 {
956 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1031 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
957 if (block > to) block = to; 1032 if (block > to) block = to;
958 } 1033 }
959 1034
960 if (block < 0.) block = 0.; 1035 if (block < 0.) block = 0.;
961 } 1036 }
1078ev_timer_start (EV_P_ struct ev_timer *w) 1153ev_timer_start (EV_P_ struct ev_timer *w)
1079{ 1154{
1080 if (ev_is_active (w)) 1155 if (ev_is_active (w))
1081 return; 1156 return;
1082 1157
1083 w->at += mn_now; 1158 ((WT)w)->at += mn_now;
1084 1159
1085 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1160 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1086 1161
1087 ev_start (EV_A_ (W)w, ++timercnt); 1162 ev_start (EV_A_ (W)w, ++timercnt);
1088 array_needsize (timers, timermax, timercnt, ); 1163 array_needsize (timers, timermax, timercnt, );
1089 timers [timercnt - 1] = w; 1164 timers [timercnt - 1] = w;
1090 upheap ((WT *)timers, timercnt - 1); 1165 upheap ((WT *)timers, timercnt - 1);
1166
1167 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1091} 1168}
1092 1169
1093void 1170void
1094ev_timer_stop (EV_P_ struct ev_timer *w) 1171ev_timer_stop (EV_P_ struct ev_timer *w)
1095{ 1172{
1096 ev_clear_pending (EV_A_ (W)w); 1173 ev_clear_pending (EV_A_ (W)w);
1097 if (!ev_is_active (w)) 1174 if (!ev_is_active (w))
1098 return; 1175 return;
1099 1176
1177 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1178
1100 if (w->active < timercnt--) 1179 if (((W)w)->active < timercnt--)
1101 { 1180 {
1102 timers [w->active - 1] = timers [timercnt]; 1181 timers [((W)w)->active - 1] = timers [timercnt];
1103 downheap ((WT *)timers, timercnt, w->active - 1); 1182 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1104 } 1183 }
1105 1184
1106 w->at = w->repeat; 1185 ((WT)w)->at = w->repeat;
1107 1186
1108 ev_stop (EV_A_ (W)w); 1187 ev_stop (EV_A_ (W)w);
1109} 1188}
1110 1189
1111void 1190void
1113{ 1192{
1114 if (ev_is_active (w)) 1193 if (ev_is_active (w))
1115 { 1194 {
1116 if (w->repeat) 1195 if (w->repeat)
1117 { 1196 {
1118 w->at = mn_now + w->repeat; 1197 ((WT)w)->at = mn_now + w->repeat;
1119 downheap ((WT *)timers, timercnt, w->active - 1); 1198 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1120 } 1199 }
1121 else 1200 else
1122 ev_timer_stop (EV_A_ w); 1201 ev_timer_stop (EV_A_ w);
1123 } 1202 }
1124 else if (w->repeat) 1203 else if (w->repeat)
1133 1212
1134 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1213 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1135 1214
1136 /* this formula differs from the one in periodic_reify because we do not always round up */ 1215 /* this formula differs from the one in periodic_reify because we do not always round up */
1137 if (w->interval) 1216 if (w->interval)
1138 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1217 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1139 1218
1140 ev_start (EV_A_ (W)w, ++periodiccnt); 1219 ev_start (EV_A_ (W)w, ++periodiccnt);
1141 array_needsize (periodics, periodicmax, periodiccnt, ); 1220 array_needsize (periodics, periodicmax, periodiccnt, );
1142 periodics [periodiccnt - 1] = w; 1221 periodics [periodiccnt - 1] = w;
1143 upheap ((WT *)periodics, periodiccnt - 1); 1222 upheap ((WT *)periodics, periodiccnt - 1);
1223
1224 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1144} 1225}
1145 1226
1146void 1227void
1147ev_periodic_stop (EV_P_ struct ev_periodic *w) 1228ev_periodic_stop (EV_P_ struct ev_periodic *w)
1148{ 1229{
1149 ev_clear_pending (EV_A_ (W)w); 1230 ev_clear_pending (EV_A_ (W)w);
1150 if (!ev_is_active (w)) 1231 if (!ev_is_active (w))
1151 return; 1232 return;
1152 1233
1234 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1235
1153 if (w->active < periodiccnt--) 1236 if (((W)w)->active < periodiccnt--)
1154 { 1237 {
1155 periodics [w->active - 1] = periodics [periodiccnt]; 1238 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1156 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1239 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1157 } 1240 }
1158 1241
1159 ev_stop (EV_A_ (W)w); 1242 ev_stop (EV_A_ (W)w);
1160} 1243}
1161 1244
1175{ 1258{
1176 ev_clear_pending (EV_A_ (W)w); 1259 ev_clear_pending (EV_A_ (W)w);
1177 if (ev_is_active (w)) 1260 if (ev_is_active (w))
1178 return; 1261 return;
1179 1262
1180 idles [w->active - 1] = idles [--idlecnt]; 1263 idles [((W)w)->active - 1] = idles [--idlecnt];
1181 ev_stop (EV_A_ (W)w); 1264 ev_stop (EV_A_ (W)w);
1182} 1265}
1183 1266
1184void 1267void
1185ev_prepare_start (EV_P_ struct ev_prepare *w) 1268ev_prepare_start (EV_P_ struct ev_prepare *w)
1197{ 1280{
1198 ev_clear_pending (EV_A_ (W)w); 1281 ev_clear_pending (EV_A_ (W)w);
1199 if (ev_is_active (w)) 1282 if (ev_is_active (w))
1200 return; 1283 return;
1201 1284
1202 prepares [w->active - 1] = prepares [--preparecnt]; 1285 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1203 ev_stop (EV_A_ (W)w); 1286 ev_stop (EV_A_ (W)w);
1204} 1287}
1205 1288
1206void 1289void
1207ev_check_start (EV_P_ struct ev_check *w) 1290ev_check_start (EV_P_ struct ev_check *w)
1219{ 1302{
1220 ev_clear_pending (EV_A_ (W)w); 1303 ev_clear_pending (EV_A_ (W)w);
1221 if (ev_is_active (w)) 1304 if (ev_is_active (w))
1222 return; 1305 return;
1223 1306
1224 checks [w->active - 1] = checks [--checkcnt]; 1307 checks [((W)w)->active - 1] = checks [--checkcnt];
1225 ev_stop (EV_A_ (W)w); 1308 ev_stop (EV_A_ (W)w);
1226} 1309}
1227 1310
1228#ifndef SA_RESTART 1311#ifndef SA_RESTART
1229# define SA_RESTART 0 1312# define SA_RESTART 0
1242 1325
1243 ev_start (EV_A_ (W)w, 1); 1326 ev_start (EV_A_ (W)w, 1);
1244 array_needsize (signals, signalmax, w->signum, signals_init); 1327 array_needsize (signals, signalmax, w->signum, signals_init);
1245 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1328 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1246 1329
1247 if (!w->next) 1330 if (!((WL)w)->next)
1248 { 1331 {
1332#if WIN32
1333 signal (w->signum, sighandler);
1334#else
1249 struct sigaction sa; 1335 struct sigaction sa;
1250 sa.sa_handler = sighandler; 1336 sa.sa_handler = sighandler;
1251 sigfillset (&sa.sa_mask); 1337 sigfillset (&sa.sa_mask);
1252 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1338 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1253 sigaction (w->signum, &sa, 0); 1339 sigaction (w->signum, &sa, 0);
1340#endif
1254 } 1341 }
1255} 1342}
1256 1343
1257void 1344void
1258ev_signal_stop (EV_P_ struct ev_signal *w) 1345ev_signal_stop (EV_P_ struct ev_signal *w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines