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

Comparing libev/ev.c (file contents):
Revision 1.56 by root, Sun Nov 4 15:58:49 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
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 int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
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
119 155
120/*****************************************************************************/ 156/*****************************************************************************/
121 157
122typedef struct 158typedef struct
123{ 159{
202 base = realloc (base, sizeof (*base) * (newcnt)); \ 238 base = realloc (base, sizeof (*base) * (newcnt)); \
203 init (base + cur, newcnt - cur); \ 239 init (base + cur, newcnt - cur); \
204 cur = newcnt; \ 240 cur = newcnt; \
205 } 241 }
206 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
207/*****************************************************************************/ 254/*****************************************************************************/
208 255
209static void 256static void
210anfds_init (ANFD *base, int count) 257anfds_init (ANFD *base, int count)
211{ 258{
276 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)
277 events |= w->events; 324 events |= w->events;
278 325
279 anfd->reify = 0; 326 anfd->reify = 0;
280 327
281 if (anfd->events != events)
282 {
283 method_modify (EV_A_ fd, anfd->events, events); 328 method_modify (EV_A_ fd, anfd->events, events);
284 anfd->events = events; 329 anfd->events = events;
285 }
286 } 330 }
287 331
288 fdchangecnt = 0; 332 fdchangecnt = 0;
289} 333}
290 334
327 371
328/* 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 */
329static void 373static void
330fd_enomem (EV_P) 374fd_enomem (EV_P)
331{ 375{
332 int fd = anfdmax; 376 int fd;
333 377
334 while (fd--) 378 for (fd = anfdmax; fd--; )
335 if (anfds [fd].events) 379 if (anfds [fd].events)
336 { 380 {
337 close (fd); 381 close (fd);
338 fd_kill (EV_A_ fd); 382 fd_kill (EV_A_ fd);
339 return; 383 return;
349 /* this should be highly optimised to not do anything but set a flag */ 393 /* this should be highly optimised to not do anything but set a flag */
350 for (fd = 0; fd < anfdmax; ++fd) 394 for (fd = 0; fd < anfdmax; ++fd)
351 if (anfds [fd].events) 395 if (anfds [fd].events)
352 { 396 {
353 anfds [fd].events = 0; 397 anfds [fd].events = 0;
354 fd_change (fd); 398 fd_change (EV_A_ fd);
355 } 399 }
356} 400}
357 401
358/*****************************************************************************/ 402/*****************************************************************************/
359 403
363 WT w = heap [k]; 407 WT w = heap [k];
364 408
365 while (k && heap [k >> 1]->at > w->at) 409 while (k && heap [k >> 1]->at > w->at)
366 { 410 {
367 heap [k] = heap [k >> 1]; 411 heap [k] = heap [k >> 1];
368 heap [k]->active = k + 1; 412 ((W)heap [k])->active = k + 1;
369 k >>= 1; 413 k >>= 1;
370 } 414 }
371 415
372 heap [k] = w; 416 heap [k] = w;
373 heap [k]->active = k + 1; 417 ((W)heap [k])->active = k + 1;
374 418
375} 419}
376 420
377static void 421static void
378downheap (WT *heap, int N, int k) 422downheap (WT *heap, int N, int k)
388 432
389 if (w->at <= heap [j]->at) 433 if (w->at <= heap [j]->at)
390 break; 434 break;
391 435
392 heap [k] = heap [j]; 436 heap [k] = heap [j];
393 heap [k]->active = k + 1; 437 ((W)heap [k])->active = k + 1;
394 k = j; 438 k = j;
395 } 439 }
396 440
397 heap [k] = w; 441 heap [k] = w;
398 heap [k]->active = k + 1; 442 ((W)heap [k])->active = k + 1;
399} 443}
400 444
401/*****************************************************************************/ 445/*****************************************************************************/
402 446
403typedef struct 447typedef struct
409static ANSIG *signals; 453static ANSIG *signals;
410static int signalmax; 454static int signalmax;
411 455
412static int sigpipe [2]; 456static int sigpipe [2];
413static sig_atomic_t volatile gotsig; 457static sig_atomic_t volatile gotsig;
458static struct ev_io sigev;
414 459
415static void 460static void
416signals_init (ANSIG *base, int count) 461signals_init (ANSIG *base, int count)
417{ 462{
418 while (count--) 463 while (count--)
425} 470}
426 471
427static void 472static void
428sighandler (int signum) 473sighandler (int signum)
429{ 474{
475#if WIN32
476 signal (signum, sighandler);
477#endif
478
430 signals [signum - 1].gotsig = 1; 479 signals [signum - 1].gotsig = 1;
431 480
432 if (!gotsig) 481 if (!gotsig)
433 { 482 {
434 int old_errno = errno; 483 int old_errno = errno;
476 525
477/*****************************************************************************/ 526/*****************************************************************************/
478 527
479#ifndef WIN32 528#ifndef WIN32
480 529
530static struct ev_child *childs [PID_HASHSIZE];
531static struct ev_signal childev;
532
481#ifndef WCONTINUED 533#ifndef WCONTINUED
482# define WCONTINUED 0 534# define WCONTINUED 0
483#endif 535#endif
484 536
485static void 537static void
488 struct ev_child *w; 540 struct ev_child *w;
489 541
490 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)
491 if (w->pid == pid || !w->pid) 543 if (w->pid == pid || !w->pid)
492 { 544 {
493 w->priority = sw->priority; /* need to do it *now* */ 545 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
494 w->rpid = pid; 546 w->rpid = pid;
495 w->rstatus = status; 547 w->rstatus = status;
496 event (EV_A_ (W)w, EV_CHILD); 548 event (EV_A_ (W)w, EV_CHILD);
497 } 549 }
498} 550}
499 551
500static void 552static void
520# include "ev_kqueue.c" 572# include "ev_kqueue.c"
521#endif 573#endif
522#if EV_USE_EPOLL 574#if EV_USE_EPOLL
523# include "ev_epoll.c" 575# include "ev_epoll.c"
524#endif 576#endif
525#if EV_USEV_POLL 577#if EV_USE_POLL
526# include "ev_poll.c" 578# include "ev_poll.c"
527#endif 579#endif
528#if EV_USE_SELECT 580#if EV_USE_SELECT
529# include "ev_select.c" 581# include "ev_select.c"
530#endif 582#endif
582 methods = atoi (getenv ("LIBEV_METHODS")); 634 methods = atoi (getenv ("LIBEV_METHODS"));
583 else 635 else
584 methods = EVMETHOD_ANY; 636 methods = EVMETHOD_ANY;
585 637
586 method = 0; 638 method = 0;
639#if EV_USE_WIN32
640 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
641#endif
587#if EV_USE_KQUEUE 642#if EV_USE_KQUEUE
588 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 643 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
589#endif 644#endif
590#if EV_USE_EPOLL 645#if EV_USE_EPOLL
591 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 646 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
592#endif 647#endif
593#if EV_USEV_POLL 648#if EV_USE_POLL
594 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 649 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
595#endif 650#endif
596#if EV_USE_SELECT 651#if EV_USE_SELECT
597 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 652 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
598#endif 653#endif
600} 655}
601 656
602void 657void
603loop_destroy (EV_P) 658loop_destroy (EV_P)
604{ 659{
660 int i;
661
662#if EV_USE_WIN32
663 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
664#endif
605#if EV_USE_KQUEUE 665#if EV_USE_KQUEUE
606 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 666 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
607#endif 667#endif
608#if EV_USE_EPOLL 668#if EV_USE_EPOLL
609 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 669 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
610#endif 670#endif
611#if EV_USEV_POLL 671#if EV_USE_POLL
612 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 672 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
613#endif 673#endif
614#if EV_USE_SELECT 674#if EV_USE_SELECT
615 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 675 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
616#endif 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, );
617 687
618 method = 0; 688 method = 0;
619 /*TODO*/ 689 /*TODO*/
620} 690}
621 691
637{ 707{
638 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 708 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
639 709
640 loop_init (EV_A_ methods); 710 loop_init (EV_A_ methods);
641 711
642 if (ev_methods (EV_A)) 712 if (ev_method (EV_A))
643 return loop; 713 return loop;
644 714
645 return 0; 715 return 0;
646} 716}
647 717
707} 777}
708 778
709void 779void
710ev_default_destroy (void) 780ev_default_destroy (void)
711{ 781{
782#if EV_MULTIPLICITY
712 struct ev_loop *loop = default_loop; 783 struct ev_loop *loop = default_loop;
784#endif
713 785
714 ev_ref (EV_A); /* child watcher */ 786 ev_ref (EV_A); /* child watcher */
715 ev_signal_stop (EV_A_ &childev); 787 ev_signal_stop (EV_A_ &childev);
716 788
717 ev_ref (EV_A); /* signal watcher */ 789 ev_ref (EV_A); /* signal watcher */
722 794
723 loop_destroy (EV_A); 795 loop_destroy (EV_A);
724} 796}
725 797
726void 798void
727ev_default_fork (EV_P) 799ev_default_fork (void)
728{ 800{
801#if EV_MULTIPLICITY
802 struct ev_loop *loop = default_loop;
803#endif
804
729 loop_fork (EV_A); 805 loop_fork (EV_A);
730 806
731 ev_io_stop (EV_A_ &sigev); 807 ev_io_stop (EV_A_ &sigev);
732 close (sigpipe [0]); 808 close (sigpipe [0]);
733 close (sigpipe [1]); 809 close (sigpipe [1]);
758} 834}
759 835
760static void 836static void
761timers_reify (EV_P) 837timers_reify (EV_P)
762{ 838{
763 while (timercnt && timers [0]->at <= mn_now) 839 while (timercnt && ((WT)timers [0])->at <= mn_now)
764 { 840 {
765 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)));
766 844
767 /* first reschedule or stop timer */ 845 /* first reschedule or stop timer */
768 if (w->repeat) 846 if (w->repeat)
769 { 847 {
770 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.));
771 w->at = mn_now + w->repeat; 849 ((WT)w)->at = mn_now + w->repeat;
772 downheap ((WT *)timers, timercnt, 0); 850 downheap ((WT *)timers, timercnt, 0);
773 } 851 }
774 else 852 else
775 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 853 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
776 854
779} 857}
780 858
781static void 859static void
782periodics_reify (EV_P) 860periodics_reify (EV_P)
783{ 861{
784 while (periodiccnt && periodics [0]->at <= rt_now) 862 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
785 { 863 {
786 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)));
787 867
788 /* first reschedule or stop timer */ 868 /* first reschedule or stop timer */
789 if (w->interval) 869 if (w->interval)
790 { 870 {
791 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;
792 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));
793 downheap ((WT *)periodics, periodiccnt, 0); 873 downheap ((WT *)periodics, periodiccnt, 0);
794 } 874 }
795 else 875 else
796 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 876 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
797 877
809 { 889 {
810 struct ev_periodic *w = periodics [i]; 890 struct ev_periodic *w = periodics [i];
811 891
812 if (w->interval) 892 if (w->interval)
813 { 893 {
814 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;
815 895
816 if (fabs (diff) >= 1e-4) 896 if (fabs (diff) >= 1e-4)
817 { 897 {
818 ev_periodic_stop (EV_A_ w); 898 ev_periodic_stop (EV_A_ w);
819 ev_periodic_start (EV_A_ w); 899 ev_periodic_start (EV_A_ w);
880 { 960 {
881 periodics_reschedule (EV_A); 961 periodics_reschedule (EV_A);
882 962
883 /* 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 */
884 for (i = 0; i < timercnt; ++i) 964 for (i = 0; i < timercnt; ++i)
885 timers [i]->at += rt_now - mn_now; 965 ((WT)timers [i])->at += rt_now - mn_now;
886 } 966 }
887 967
888 mn_now = rt_now; 968 mn_now = rt_now;
889 } 969 }
890} 970}
941 { 1021 {
942 block = MAX_BLOCKTIME; 1022 block = MAX_BLOCKTIME;
943 1023
944 if (timercnt) 1024 if (timercnt)
945 { 1025 {
946 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1026 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
947 if (block > to) block = to; 1027 if (block > to) block = to;
948 } 1028 }
949 1029
950 if (periodiccnt) 1030 if (periodiccnt)
951 { 1031 {
952 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1032 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
953 if (block > to) block = to; 1033 if (block > to) block = to;
954 } 1034 }
955 1035
956 if (block < 0.) block = 0.; 1036 if (block < 0.) block = 0.;
957 } 1037 }
1074ev_timer_start (EV_P_ struct ev_timer *w) 1154ev_timer_start (EV_P_ struct ev_timer *w)
1075{ 1155{
1076 if (ev_is_active (w)) 1156 if (ev_is_active (w))
1077 return; 1157 return;
1078 1158
1079 w->at += mn_now; 1159 ((WT)w)->at += mn_now;
1080 1160
1081 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.));
1082 1162
1083 ev_start (EV_A_ (W)w, ++timercnt); 1163 ev_start (EV_A_ (W)w, ++timercnt);
1084 array_needsize (timers, timermax, timercnt, ); 1164 array_needsize (timers, timermax, timercnt, );
1085 timers [timercnt - 1] = w; 1165 timers [timercnt - 1] = w;
1086 upheap ((WT *)timers, timercnt - 1); 1166 upheap ((WT *)timers, timercnt - 1);
1167
1168 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1087} 1169}
1088 1170
1089void 1171void
1090ev_timer_stop (EV_P_ struct ev_timer *w) 1172ev_timer_stop (EV_P_ struct ev_timer *w)
1091{ 1173{
1092 ev_clear_pending (EV_A_ (W)w); 1174 ev_clear_pending (EV_A_ (W)w);
1093 if (!ev_is_active (w)) 1175 if (!ev_is_active (w))
1094 return; 1176 return;
1095 1177
1178 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1179
1096 if (w->active < timercnt--) 1180 if (((W)w)->active < timercnt--)
1097 { 1181 {
1098 timers [w->active - 1] = timers [timercnt]; 1182 timers [((W)w)->active - 1] = timers [timercnt];
1099 downheap ((WT *)timers, timercnt, w->active - 1); 1183 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1100 } 1184 }
1101 1185
1102 w->at = w->repeat; 1186 ((WT)w)->at = w->repeat;
1103 1187
1104 ev_stop (EV_A_ (W)w); 1188 ev_stop (EV_A_ (W)w);
1105} 1189}
1106 1190
1107void 1191void
1109{ 1193{
1110 if (ev_is_active (w)) 1194 if (ev_is_active (w))
1111 { 1195 {
1112 if (w->repeat) 1196 if (w->repeat)
1113 { 1197 {
1114 w->at = mn_now + w->repeat; 1198 ((WT)w)->at = mn_now + w->repeat;
1115 downheap ((WT *)timers, timercnt, w->active - 1); 1199 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1116 } 1200 }
1117 else 1201 else
1118 ev_timer_stop (EV_A_ w); 1202 ev_timer_stop (EV_A_ w);
1119 } 1203 }
1120 else if (w->repeat) 1204 else if (w->repeat)
1129 1213
1130 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.));
1131 1215
1132 /* 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 */
1133 if (w->interval) 1217 if (w->interval)
1134 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;
1135 1219
1136 ev_start (EV_A_ (W)w, ++periodiccnt); 1220 ev_start (EV_A_ (W)w, ++periodiccnt);
1137 array_needsize (periodics, periodicmax, periodiccnt, ); 1221 array_needsize (periodics, periodicmax, periodiccnt, );
1138 periodics [periodiccnt - 1] = w; 1222 periodics [periodiccnt - 1] = w;
1139 upheap ((WT *)periodics, periodiccnt - 1); 1223 upheap ((WT *)periodics, periodiccnt - 1);
1224
1225 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1140} 1226}
1141 1227
1142void 1228void
1143ev_periodic_stop (EV_P_ struct ev_periodic *w) 1229ev_periodic_stop (EV_P_ struct ev_periodic *w)
1144{ 1230{
1145 ev_clear_pending (EV_A_ (W)w); 1231 ev_clear_pending (EV_A_ (W)w);
1146 if (!ev_is_active (w)) 1232 if (!ev_is_active (w))
1147 return; 1233 return;
1148 1234
1235 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1236
1149 if (w->active < periodiccnt--) 1237 if (((W)w)->active < periodiccnt--)
1150 { 1238 {
1151 periodics [w->active - 1] = periodics [periodiccnt]; 1239 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1152 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1240 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1153 } 1241 }
1154 1242
1155 ev_stop (EV_A_ (W)w); 1243 ev_stop (EV_A_ (W)w);
1156} 1244}
1157 1245
1171{ 1259{
1172 ev_clear_pending (EV_A_ (W)w); 1260 ev_clear_pending (EV_A_ (W)w);
1173 if (ev_is_active (w)) 1261 if (ev_is_active (w))
1174 return; 1262 return;
1175 1263
1176 idles [w->active - 1] = idles [--idlecnt]; 1264 idles [((W)w)->active - 1] = idles [--idlecnt];
1177 ev_stop (EV_A_ (W)w); 1265 ev_stop (EV_A_ (W)w);
1178} 1266}
1179 1267
1180void 1268void
1181ev_prepare_start (EV_P_ struct ev_prepare *w) 1269ev_prepare_start (EV_P_ struct ev_prepare *w)
1193{ 1281{
1194 ev_clear_pending (EV_A_ (W)w); 1282 ev_clear_pending (EV_A_ (W)w);
1195 if (ev_is_active (w)) 1283 if (ev_is_active (w))
1196 return; 1284 return;
1197 1285
1198 prepares [w->active - 1] = prepares [--preparecnt]; 1286 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1199 ev_stop (EV_A_ (W)w); 1287 ev_stop (EV_A_ (W)w);
1200} 1288}
1201 1289
1202void 1290void
1203ev_check_start (EV_P_ struct ev_check *w) 1291ev_check_start (EV_P_ struct ev_check *w)
1215{ 1303{
1216 ev_clear_pending (EV_A_ (W)w); 1304 ev_clear_pending (EV_A_ (W)w);
1217 if (ev_is_active (w)) 1305 if (ev_is_active (w))
1218 return; 1306 return;
1219 1307
1220 checks [w->active - 1] = checks [--checkcnt]; 1308 checks [((W)w)->active - 1] = checks [--checkcnt];
1221 ev_stop (EV_A_ (W)w); 1309 ev_stop (EV_A_ (W)w);
1222} 1310}
1223 1311
1224#ifndef SA_RESTART 1312#ifndef SA_RESTART
1225# define SA_RESTART 0 1313# define SA_RESTART 0
1238 1326
1239 ev_start (EV_A_ (W)w, 1); 1327 ev_start (EV_A_ (W)w, 1);
1240 array_needsize (signals, signalmax, w->signum, signals_init); 1328 array_needsize (signals, signalmax, w->signum, signals_init);
1241 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1329 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1242 1330
1243 if (!w->next) 1331 if (!((WL)w)->next)
1244 { 1332 {
1333#if WIN32
1334 signal (w->signum, sighandler);
1335#else
1245 struct sigaction sa; 1336 struct sigaction sa;
1246 sa.sa_handler = sighandler; 1337 sa.sa_handler = sighandler;
1247 sigfillset (&sa.sa_mask); 1338 sigfillset (&sa.sa_mask);
1248 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 */
1249 sigaction (w->signum, &sa, 0); 1340 sigaction (w->signum, &sa, 0);
1341#endif
1250 } 1342 }
1251} 1343}
1252 1344
1253void 1345void
1254ev_signal_stop (EV_P_ struct ev_signal *w) 1346ev_signal_stop (EV_P_ struct ev_signal *w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines