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.69 by root, Tue Nov 6 00:10:04 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)(void);
159
160void ev_set_syserr_cb (void (*cb)(void))
161{
162 syserr_cb = cb;
163}
164
165static void
166syserr (void)
167{
168 if (syserr_cb)
169 syserr_cb ();
170 else
171 {
172 perror ("libev");
173 abort ();
174 }
175}
176
177static void *(*alloc)(void *ptr, long size);
178
179void ev_set_allocator (void *(*cb)(void *ptr, long size))
180{
181 alloc = cb;
182}
183
184static void *
185ev_realloc (void *ptr, long size)
186{
187 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
188
189 if (!ptr && size)
190 {
191 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
192 abort ();
193 }
194
195 return ptr;
196}
197
198#define ev_malloc(size) ev_realloc (0, (size))
199#define ev_free(ptr) ev_realloc ((ptr), 0)
200
201/*****************************************************************************/
202
122typedef struct 203typedef struct
123{ 204{
124 struct ev_watcher_list *head; 205 WL head;
125 unsigned char events; 206 unsigned char events;
126 unsigned char reify; 207 unsigned char reify;
127} ANFD; 208} ANFD;
128 209
129typedef struct 210typedef struct
187 return rt_now; 268 return rt_now;
188} 269}
189 270
190#define array_roundsize(base,n) ((n) | 4 & ~3) 271#define array_roundsize(base,n) ((n) | 4 & ~3)
191 272
192#define array_needsize(base,cur,cnt,init) \ 273#define array_needsize(base,cur,cnt,init) \
193 if (expect_false ((cnt) > cur)) \ 274 if (expect_false ((cnt) > cur)) \
194 { \ 275 { \
195 int newcnt = cur; \ 276 int newcnt = cur; \
196 do \ 277 do \
197 { \ 278 { \
198 newcnt = array_roundsize (base, newcnt << 1); \ 279 newcnt = array_roundsize (base, newcnt << 1); \
199 } \ 280 } \
200 while ((cnt) > newcnt); \ 281 while ((cnt) > newcnt); \
201 \ 282 \
202 base = realloc (base, sizeof (*base) * (newcnt)); \ 283 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
203 init (base + cur, newcnt - cur); \ 284 init (base + cur, newcnt - cur); \
204 cur = newcnt; \ 285 cur = newcnt; \
205 } 286 }
287
288#define array_slim(stem) \
289 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
290 { \
291 stem ## max = array_roundsize (stem ## cnt >> 1); \
292 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
293 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
294 }
295
296#define array_free(stem, idx) \
297 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
206 298
207/*****************************************************************************/ 299/*****************************************************************************/
208 300
209static void 301static void
210anfds_init (ANFD *base, int count) 302anfds_init (ANFD *base, int count)
276 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 368 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
277 events |= w->events; 369 events |= w->events;
278 370
279 anfd->reify = 0; 371 anfd->reify = 0;
280 372
281 if (anfd->events != events)
282 {
283 method_modify (EV_A_ fd, anfd->events, events); 373 method_modify (EV_A_ fd, anfd->events, events);
284 anfd->events = events; 374 anfd->events = events;
285 }
286 } 375 }
287 376
288 fdchangecnt = 0; 377 fdchangecnt = 0;
289} 378}
290 379
327 416
328/* called on ENOMEM in select/poll to kill some fds and retry */ 417/* called on ENOMEM in select/poll to kill some fds and retry */
329static void 418static void
330fd_enomem (EV_P) 419fd_enomem (EV_P)
331{ 420{
332 int fd = anfdmax; 421 int fd;
333 422
334 while (fd--) 423 for (fd = anfdmax; fd--; )
335 if (anfds [fd].events) 424 if (anfds [fd].events)
336 { 425 {
337 close (fd);
338 fd_kill (EV_A_ fd); 426 fd_kill (EV_A_ fd);
339 return; 427 return;
340 } 428 }
341} 429}
342 430
349 /* this should be highly optimised to not do anything but set a flag */ 437 /* this should be highly optimised to not do anything but set a flag */
350 for (fd = 0; fd < anfdmax; ++fd) 438 for (fd = 0; fd < anfdmax; ++fd)
351 if (anfds [fd].events) 439 if (anfds [fd].events)
352 { 440 {
353 anfds [fd].events = 0; 441 anfds [fd].events = 0;
354 fd_change (fd); 442 fd_change (EV_A_ fd);
355 } 443 }
356} 444}
357 445
358/*****************************************************************************/ 446/*****************************************************************************/
359 447
363 WT w = heap [k]; 451 WT w = heap [k];
364 452
365 while (k && heap [k >> 1]->at > w->at) 453 while (k && heap [k >> 1]->at > w->at)
366 { 454 {
367 heap [k] = heap [k >> 1]; 455 heap [k] = heap [k >> 1];
368 heap [k]->active = k + 1; 456 ((W)heap [k])->active = k + 1;
369 k >>= 1; 457 k >>= 1;
370 } 458 }
371 459
372 heap [k] = w; 460 heap [k] = w;
373 heap [k]->active = k + 1; 461 ((W)heap [k])->active = k + 1;
374 462
375} 463}
376 464
377static void 465static void
378downheap (WT *heap, int N, int k) 466downheap (WT *heap, int N, int k)
388 476
389 if (w->at <= heap [j]->at) 477 if (w->at <= heap [j]->at)
390 break; 478 break;
391 479
392 heap [k] = heap [j]; 480 heap [k] = heap [j];
393 heap [k]->active = k + 1; 481 ((W)heap [k])->active = k + 1;
394 k = j; 482 k = j;
395 } 483 }
396 484
397 heap [k] = w; 485 heap [k] = w;
398 heap [k]->active = k + 1; 486 ((W)heap [k])->active = k + 1;
399} 487}
400 488
401/*****************************************************************************/ 489/*****************************************************************************/
402 490
403typedef struct 491typedef struct
404{ 492{
405 struct ev_watcher_list *head; 493 WL head;
406 sig_atomic_t volatile gotsig; 494 sig_atomic_t volatile gotsig;
407} ANSIG; 495} ANSIG;
408 496
409static ANSIG *signals; 497static ANSIG *signals;
410static int signalmax; 498static int signalmax;
426} 514}
427 515
428static void 516static void
429sighandler (int signum) 517sighandler (int signum)
430{ 518{
519#if WIN32
520 signal (signum, sighandler);
521#endif
522
431 signals [signum - 1].gotsig = 1; 523 signals [signum - 1].gotsig = 1;
432 524
433 if (!gotsig) 525 if (!gotsig)
434 { 526 {
435 int old_errno = errno; 527 int old_errno = errno;
440} 532}
441 533
442static void 534static void
443sigcb (EV_P_ struct ev_io *iow, int revents) 535sigcb (EV_P_ struct ev_io *iow, int revents)
444{ 536{
445 struct ev_watcher_list *w; 537 WL w;
446 int signum; 538 int signum;
447 539
448 read (sigpipe [0], &revents, 1); 540 read (sigpipe [0], &revents, 1);
449 gotsig = 0; 541 gotsig = 0;
450 542
492 struct ev_child *w; 584 struct ev_child *w;
493 585
494 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 586 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) 587 if (w->pid == pid || !w->pid)
496 { 588 {
497 w->priority = sw->priority; /* need to do it *now* */ 589 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
498 w->rpid = pid; 590 w->rpid = pid;
499 w->rstatus = status; 591 w->rstatus = status;
500 event (EV_A_ (W)w, EV_CHILD); 592 event (EV_A_ (W)w, EV_CHILD);
501 } 593 }
502} 594}
503 595
504static void 596static void
586 methods = atoi (getenv ("LIBEV_METHODS")); 678 methods = atoi (getenv ("LIBEV_METHODS"));
587 else 679 else
588 methods = EVMETHOD_ANY; 680 methods = EVMETHOD_ANY;
589 681
590 method = 0; 682 method = 0;
683#if EV_USE_WIN32
684 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
685#endif
591#if EV_USE_KQUEUE 686#if EV_USE_KQUEUE
592 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 687 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
593#endif 688#endif
594#if EV_USE_EPOLL 689#if EV_USE_EPOLL
595 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 690 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
604} 699}
605 700
606void 701void
607loop_destroy (EV_P) 702loop_destroy (EV_P)
608{ 703{
704 int i;
705
706#if EV_USE_WIN32
707 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
708#endif
609#if EV_USE_KQUEUE 709#if EV_USE_KQUEUE
610 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 710 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
611#endif 711#endif
612#if EV_USE_EPOLL 712#if EV_USE_EPOLL
613 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 713 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
616 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 716 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
617#endif 717#endif
618#if EV_USE_SELECT 718#if EV_USE_SELECT
619 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 719 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
620#endif 720#endif
721
722 for (i = NUMPRI; i--; )
723 array_free (pending, [i]);
724
725 array_free (fdchange, );
726 array_free (timer, );
727 array_free (periodic, );
728 array_free (idle, );
729 array_free (prepare, );
730 array_free (check, );
621 731
622 method = 0; 732 method = 0;
623 /*TODO*/ 733 /*TODO*/
624} 734}
625 735
637 747
638#if EV_MULTIPLICITY 748#if EV_MULTIPLICITY
639struct ev_loop * 749struct ev_loop *
640ev_loop_new (int methods) 750ev_loop_new (int methods)
641{ 751{
642 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 752 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
753
754 memset (loop, 0, sizeof (struct ev_loop));
643 755
644 loop_init (EV_A_ methods); 756 loop_init (EV_A_ methods);
645 757
646 if (ev_methods (EV_A)) 758 if (ev_method (EV_A))
647 return loop; 759 return loop;
648 760
649 return 0; 761 return 0;
650} 762}
651 763
652void 764void
653ev_loop_destroy (EV_P) 765ev_loop_destroy (EV_P)
654{ 766{
655 loop_destroy (EV_A); 767 loop_destroy (EV_A);
656 free (loop); 768 ev_free (loop);
657} 769}
658 770
659void 771void
660ev_loop_fork (EV_P) 772ev_loop_fork (EV_P)
661{ 773{
728 840
729 loop_destroy (EV_A); 841 loop_destroy (EV_A);
730} 842}
731 843
732void 844void
733ev_default_fork (EV_P) 845ev_default_fork (void)
734{ 846{
847#if EV_MULTIPLICITY
848 struct ev_loop *loop = default_loop;
849#endif
850
735 loop_fork (EV_A); 851 loop_fork (EV_A);
736 852
737 ev_io_stop (EV_A_ &sigev); 853 ev_io_stop (EV_A_ &sigev);
738 close (sigpipe [0]); 854 close (sigpipe [0]);
739 close (sigpipe [1]); 855 close (sigpipe [1]);
764} 880}
765 881
766static void 882static void
767timers_reify (EV_P) 883timers_reify (EV_P)
768{ 884{
769 while (timercnt && timers [0]->at <= mn_now) 885 while (timercnt && ((WT)timers [0])->at <= mn_now)
770 { 886 {
771 struct ev_timer *w = timers [0]; 887 struct ev_timer *w = timers [0];
888
889 assert (("inactive timer on timer heap detected", ev_is_active (w)));
772 890
773 /* first reschedule or stop timer */ 891 /* first reschedule or stop timer */
774 if (w->repeat) 892 if (w->repeat)
775 { 893 {
776 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 894 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
777 w->at = mn_now + w->repeat; 895 ((WT)w)->at = mn_now + w->repeat;
778 downheap ((WT *)timers, timercnt, 0); 896 downheap ((WT *)timers, timercnt, 0);
779 } 897 }
780 else 898 else
781 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 899 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
782 900
785} 903}
786 904
787static void 905static void
788periodics_reify (EV_P) 906periodics_reify (EV_P)
789{ 907{
790 while (periodiccnt && periodics [0]->at <= rt_now) 908 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
791 { 909 {
792 struct ev_periodic *w = periodics [0]; 910 struct ev_periodic *w = periodics [0];
911
912 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
793 913
794 /* first reschedule or stop timer */ 914 /* first reschedule or stop timer */
795 if (w->interval) 915 if (w->interval)
796 { 916 {
797 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 917 ((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)); 918 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
799 downheap ((WT *)periodics, periodiccnt, 0); 919 downheap ((WT *)periodics, periodiccnt, 0);
800 } 920 }
801 else 921 else
802 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 922 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
803 923
815 { 935 {
816 struct ev_periodic *w = periodics [i]; 936 struct ev_periodic *w = periodics [i];
817 937
818 if (w->interval) 938 if (w->interval)
819 { 939 {
820 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 940 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
821 941
822 if (fabs (diff) >= 1e-4) 942 if (fabs (diff) >= 1e-4)
823 { 943 {
824 ev_periodic_stop (EV_A_ w); 944 ev_periodic_stop (EV_A_ w);
825 ev_periodic_start (EV_A_ w); 945 ev_periodic_start (EV_A_ w);
886 { 1006 {
887 periodics_reschedule (EV_A); 1007 periodics_reschedule (EV_A);
888 1008
889 /* adjust timers. this is easy, as the offset is the same for all */ 1009 /* adjust timers. this is easy, as the offset is the same for all */
890 for (i = 0; i < timercnt; ++i) 1010 for (i = 0; i < timercnt; ++i)
891 timers [i]->at += rt_now - mn_now; 1011 ((WT)timers [i])->at += rt_now - mn_now;
892 } 1012 }
893 1013
894 mn_now = rt_now; 1014 mn_now = rt_now;
895 } 1015 }
896} 1016}
947 { 1067 {
948 block = MAX_BLOCKTIME; 1068 block = MAX_BLOCKTIME;
949 1069
950 if (timercnt) 1070 if (timercnt)
951 { 1071 {
952 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1072 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
953 if (block > to) block = to; 1073 if (block > to) block = to;
954 } 1074 }
955 1075
956 if (periodiccnt) 1076 if (periodiccnt)
957 { 1077 {
958 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1078 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
959 if (block > to) block = to; 1079 if (block > to) block = to;
960 } 1080 }
961 1081
962 if (block < 0.) block = 0.; 1082 if (block < 0.) block = 0.;
963 } 1083 }
1080ev_timer_start (EV_P_ struct ev_timer *w) 1200ev_timer_start (EV_P_ struct ev_timer *w)
1081{ 1201{
1082 if (ev_is_active (w)) 1202 if (ev_is_active (w))
1083 return; 1203 return;
1084 1204
1085 w->at += mn_now; 1205 ((WT)w)->at += mn_now;
1086 1206
1087 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1207 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1088 1208
1089 ev_start (EV_A_ (W)w, ++timercnt); 1209 ev_start (EV_A_ (W)w, ++timercnt);
1090 array_needsize (timers, timermax, timercnt, ); 1210 array_needsize (timers, timermax, timercnt, );
1091 timers [timercnt - 1] = w; 1211 timers [timercnt - 1] = w;
1092 upheap ((WT *)timers, timercnt - 1); 1212 upheap ((WT *)timers, timercnt - 1);
1213
1214 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1093} 1215}
1094 1216
1095void 1217void
1096ev_timer_stop (EV_P_ struct ev_timer *w) 1218ev_timer_stop (EV_P_ struct ev_timer *w)
1097{ 1219{
1098 ev_clear_pending (EV_A_ (W)w); 1220 ev_clear_pending (EV_A_ (W)w);
1099 if (!ev_is_active (w)) 1221 if (!ev_is_active (w))
1100 return; 1222 return;
1101 1223
1224 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1225
1102 if (w->active < timercnt--) 1226 if (((W)w)->active < timercnt--)
1103 { 1227 {
1104 timers [w->active - 1] = timers [timercnt]; 1228 timers [((W)w)->active - 1] = timers [timercnt];
1105 downheap ((WT *)timers, timercnt, w->active - 1); 1229 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1106 } 1230 }
1107 1231
1108 w->at = w->repeat; 1232 ((WT)w)->at = w->repeat;
1109 1233
1110 ev_stop (EV_A_ (W)w); 1234 ev_stop (EV_A_ (W)w);
1111} 1235}
1112 1236
1113void 1237void
1115{ 1239{
1116 if (ev_is_active (w)) 1240 if (ev_is_active (w))
1117 { 1241 {
1118 if (w->repeat) 1242 if (w->repeat)
1119 { 1243 {
1120 w->at = mn_now + w->repeat; 1244 ((WT)w)->at = mn_now + w->repeat;
1121 downheap ((WT *)timers, timercnt, w->active - 1); 1245 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1122 } 1246 }
1123 else 1247 else
1124 ev_timer_stop (EV_A_ w); 1248 ev_timer_stop (EV_A_ w);
1125 } 1249 }
1126 else if (w->repeat) 1250 else if (w->repeat)
1135 1259
1136 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1260 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1137 1261
1138 /* this formula differs from the one in periodic_reify because we do not always round up */ 1262 /* this formula differs from the one in periodic_reify because we do not always round up */
1139 if (w->interval) 1263 if (w->interval)
1140 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1264 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1141 1265
1142 ev_start (EV_A_ (W)w, ++periodiccnt); 1266 ev_start (EV_A_ (W)w, ++periodiccnt);
1143 array_needsize (periodics, periodicmax, periodiccnt, ); 1267 array_needsize (periodics, periodicmax, periodiccnt, );
1144 periodics [periodiccnt - 1] = w; 1268 periodics [periodiccnt - 1] = w;
1145 upheap ((WT *)periodics, periodiccnt - 1); 1269 upheap ((WT *)periodics, periodiccnt - 1);
1270
1271 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1146} 1272}
1147 1273
1148void 1274void
1149ev_periodic_stop (EV_P_ struct ev_periodic *w) 1275ev_periodic_stop (EV_P_ struct ev_periodic *w)
1150{ 1276{
1151 ev_clear_pending (EV_A_ (W)w); 1277 ev_clear_pending (EV_A_ (W)w);
1152 if (!ev_is_active (w)) 1278 if (!ev_is_active (w))
1153 return; 1279 return;
1154 1280
1281 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1282
1155 if (w->active < periodiccnt--) 1283 if (((W)w)->active < periodiccnt--)
1156 { 1284 {
1157 periodics [w->active - 1] = periodics [periodiccnt]; 1285 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1158 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1286 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1159 } 1287 }
1160 1288
1161 ev_stop (EV_A_ (W)w); 1289 ev_stop (EV_A_ (W)w);
1162} 1290}
1163 1291
1177{ 1305{
1178 ev_clear_pending (EV_A_ (W)w); 1306 ev_clear_pending (EV_A_ (W)w);
1179 if (ev_is_active (w)) 1307 if (ev_is_active (w))
1180 return; 1308 return;
1181 1309
1182 idles [w->active - 1] = idles [--idlecnt]; 1310 idles [((W)w)->active - 1] = idles [--idlecnt];
1183 ev_stop (EV_A_ (W)w); 1311 ev_stop (EV_A_ (W)w);
1184} 1312}
1185 1313
1186void 1314void
1187ev_prepare_start (EV_P_ struct ev_prepare *w) 1315ev_prepare_start (EV_P_ struct ev_prepare *w)
1199{ 1327{
1200 ev_clear_pending (EV_A_ (W)w); 1328 ev_clear_pending (EV_A_ (W)w);
1201 if (ev_is_active (w)) 1329 if (ev_is_active (w))
1202 return; 1330 return;
1203 1331
1204 prepares [w->active - 1] = prepares [--preparecnt]; 1332 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1205 ev_stop (EV_A_ (W)w); 1333 ev_stop (EV_A_ (W)w);
1206} 1334}
1207 1335
1208void 1336void
1209ev_check_start (EV_P_ struct ev_check *w) 1337ev_check_start (EV_P_ struct ev_check *w)
1221{ 1349{
1222 ev_clear_pending (EV_A_ (W)w); 1350 ev_clear_pending (EV_A_ (W)w);
1223 if (ev_is_active (w)) 1351 if (ev_is_active (w))
1224 return; 1352 return;
1225 1353
1226 checks [w->active - 1] = checks [--checkcnt]; 1354 checks [((W)w)->active - 1] = checks [--checkcnt];
1227 ev_stop (EV_A_ (W)w); 1355 ev_stop (EV_A_ (W)w);
1228} 1356}
1229 1357
1230#ifndef SA_RESTART 1358#ifndef SA_RESTART
1231# define SA_RESTART 0 1359# define SA_RESTART 0
1244 1372
1245 ev_start (EV_A_ (W)w, 1); 1373 ev_start (EV_A_ (W)w, 1);
1246 array_needsize (signals, signalmax, w->signum, signals_init); 1374 array_needsize (signals, signalmax, w->signum, signals_init);
1247 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1375 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1248 1376
1249 if (!w->next) 1377 if (!((WL)w)->next)
1250 { 1378 {
1379#if WIN32
1380 signal (w->signum, sighandler);
1381#else
1251 struct sigaction sa; 1382 struct sigaction sa;
1252 sa.sa_handler = sighandler; 1383 sa.sa_handler = sighandler;
1253 sigfillset (&sa.sa_mask); 1384 sigfillset (&sa.sa_mask);
1254 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1385 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1255 sigaction (w->signum, &sa, 0); 1386 sigaction (w->signum, &sa, 0);
1387#endif
1256 } 1388 }
1257} 1389}
1258 1390
1259void 1391void
1260ev_signal_stop (EV_P_ struct ev_signal *w) 1392ev_signal_stop (EV_P_ struct ev_signal *w)
1310 void (*cb)(int revents, void *arg) = once->cb; 1442 void (*cb)(int revents, void *arg) = once->cb;
1311 void *arg = once->arg; 1443 void *arg = once->arg;
1312 1444
1313 ev_io_stop (EV_A_ &once->io); 1445 ev_io_stop (EV_A_ &once->io);
1314 ev_timer_stop (EV_A_ &once->to); 1446 ev_timer_stop (EV_A_ &once->to);
1315 free (once); 1447 ev_free (once);
1316 1448
1317 cb (revents, arg); 1449 cb (revents, arg);
1318} 1450}
1319 1451
1320static void 1452static void
1330} 1462}
1331 1463
1332void 1464void
1333ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1465ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1334{ 1466{
1335 struct ev_once *once = malloc (sizeof (struct ev_once)); 1467 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1336 1468
1337 if (!once) 1469 if (!once)
1338 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1470 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1339 else 1471 else
1340 { 1472 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines