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

Comparing libev/ev.c (file contents):
Revision 1.60 by root, Sun Nov 4 18:29:44 2007 UTC vs.
Revision 1.69 by root, Tue Nov 6 00:10:04 2007 UTC

92 92
93#ifndef EV_USE_KQUEUE 93#ifndef EV_USE_KQUEUE
94# define EV_USE_KQUEUE 0 94# define EV_USE_KQUEUE 0
95#endif 95#endif
96 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
97#ifndef EV_USE_REALTIME 105#ifndef EV_USE_REALTIME
98# define EV_USE_REALTIME 1 106# define EV_USE_REALTIME 1
99#endif 107#endif
100 108
101/**/ 109/**/
137typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
138typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
139 147
140static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
141 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
142/*****************************************************************************/ 156/*****************************************************************************/
143 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
144typedef struct 203typedef struct
145{ 204{
146 struct ev_watcher_list *head; 205 WL head;
147 unsigned char events; 206 unsigned char events;
148 unsigned char reify; 207 unsigned char reify;
149} ANFD; 208} ANFD;
150 209
151typedef struct 210typedef struct
209 return rt_now; 268 return rt_now;
210} 269}
211 270
212#define array_roundsize(base,n) ((n) | 4 & ~3) 271#define array_roundsize(base,n) ((n) | 4 & ~3)
213 272
214#define array_needsize(base,cur,cnt,init) \ 273#define array_needsize(base,cur,cnt,init) \
215 if (expect_false ((cnt) > cur)) \ 274 if (expect_false ((cnt) > cur)) \
216 { \ 275 { \
217 int newcnt = cur; \ 276 int newcnt = cur; \
218 do \ 277 do \
219 { \ 278 { \
220 newcnt = array_roundsize (base, newcnt << 1); \ 279 newcnt = array_roundsize (base, newcnt << 1); \
221 } \ 280 } \
222 while ((cnt) > newcnt); \ 281 while ((cnt) > newcnt); \
223 \ 282 \
224 base = realloc (base, sizeof (*base) * (newcnt)); \ 283 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
225 init (base + cur, newcnt - cur); \ 284 init (base + cur, newcnt - cur); \
226 cur = newcnt; \ 285 cur = newcnt; \
227 } 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;
228 298
229/*****************************************************************************/ 299/*****************************************************************************/
230 300
231static void 301static void
232anfds_init (ANFD *base, int count) 302anfds_init (ANFD *base, int count)
298 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)
299 events |= w->events; 369 events |= w->events;
300 370
301 anfd->reify = 0; 371 anfd->reify = 0;
302 372
303 if (anfd->events != events)
304 {
305 method_modify (EV_A_ fd, anfd->events, events); 373 method_modify (EV_A_ fd, anfd->events, events);
306 anfd->events = events; 374 anfd->events = events;
307 }
308 } 375 }
309 376
310 fdchangecnt = 0; 377 fdchangecnt = 0;
311} 378}
312 379
349 416
350/* 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 */
351static void 418static void
352fd_enomem (EV_P) 419fd_enomem (EV_P)
353{ 420{
354 int fd = anfdmax; 421 int fd;
355 422
356 while (fd--) 423 for (fd = anfdmax; fd--; )
357 if (anfds [fd].events) 424 if (anfds [fd].events)
358 { 425 {
359 close (fd);
360 fd_kill (EV_A_ fd); 426 fd_kill (EV_A_ fd);
361 return; 427 return;
362 } 428 }
363} 429}
364 430
385 WT w = heap [k]; 451 WT w = heap [k];
386 452
387 while (k && heap [k >> 1]->at > w->at) 453 while (k && heap [k >> 1]->at > w->at)
388 { 454 {
389 heap [k] = heap [k >> 1]; 455 heap [k] = heap [k >> 1];
390 heap [k]->active = k + 1; 456 ((W)heap [k])->active = k + 1;
391 k >>= 1; 457 k >>= 1;
392 } 458 }
393 459
394 heap [k] = w; 460 heap [k] = w;
395 heap [k]->active = k + 1; 461 ((W)heap [k])->active = k + 1;
396 462
397} 463}
398 464
399static void 465static void
400downheap (WT *heap, int N, int k) 466downheap (WT *heap, int N, int k)
410 476
411 if (w->at <= heap [j]->at) 477 if (w->at <= heap [j]->at)
412 break; 478 break;
413 479
414 heap [k] = heap [j]; 480 heap [k] = heap [j];
415 heap [k]->active = k + 1; 481 ((W)heap [k])->active = k + 1;
416 k = j; 482 k = j;
417 } 483 }
418 484
419 heap [k] = w; 485 heap [k] = w;
420 heap [k]->active = k + 1; 486 ((W)heap [k])->active = k + 1;
421} 487}
422 488
423/*****************************************************************************/ 489/*****************************************************************************/
424 490
425typedef struct 491typedef struct
426{ 492{
427 struct ev_watcher_list *head; 493 WL head;
428 sig_atomic_t volatile gotsig; 494 sig_atomic_t volatile gotsig;
429} ANSIG; 495} ANSIG;
430 496
431static ANSIG *signals; 497static ANSIG *signals;
432static int signalmax; 498static int signalmax;
448} 514}
449 515
450static void 516static void
451sighandler (int signum) 517sighandler (int signum)
452{ 518{
519#if WIN32
520 signal (signum, sighandler);
521#endif
522
453 signals [signum - 1].gotsig = 1; 523 signals [signum - 1].gotsig = 1;
454 524
455 if (!gotsig) 525 if (!gotsig)
456 { 526 {
457 int old_errno = errno; 527 int old_errno = errno;
462} 532}
463 533
464static void 534static void
465sigcb (EV_P_ struct ev_io *iow, int revents) 535sigcb (EV_P_ struct ev_io *iow, int revents)
466{ 536{
467 struct ev_watcher_list *w; 537 WL w;
468 int signum; 538 int signum;
469 539
470 read (sigpipe [0], &revents, 1); 540 read (sigpipe [0], &revents, 1);
471 gotsig = 0; 541 gotsig = 0;
472 542
514 struct ev_child *w; 584 struct ev_child *w;
515 585
516 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)
517 if (w->pid == pid || !w->pid) 587 if (w->pid == pid || !w->pid)
518 { 588 {
519 w->priority = sw->priority; /* need to do it *now* */ 589 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
520 w->rpid = pid; 590 w->rpid = pid;
521 w->rstatus = status; 591 w->rstatus = status;
522 event (EV_A_ (W)w, EV_CHILD); 592 event (EV_A_ (W)w, EV_CHILD);
523 } 593 }
524} 594}
525 595
526static void 596static void
608 methods = atoi (getenv ("LIBEV_METHODS")); 678 methods = atoi (getenv ("LIBEV_METHODS"));
609 else 679 else
610 methods = EVMETHOD_ANY; 680 methods = EVMETHOD_ANY;
611 681
612 method = 0; 682 method = 0;
683#if EV_USE_WIN32
684 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
685#endif
613#if EV_USE_KQUEUE 686#if EV_USE_KQUEUE
614 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 687 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
615#endif 688#endif
616#if EV_USE_EPOLL 689#if EV_USE_EPOLL
617 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 690 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
626} 699}
627 700
628void 701void
629loop_destroy (EV_P) 702loop_destroy (EV_P)
630{ 703{
704 int i;
705
706#if EV_USE_WIN32
707 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
708#endif
631#if EV_USE_KQUEUE 709#if EV_USE_KQUEUE
632 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 710 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
633#endif 711#endif
634#if EV_USE_EPOLL 712#if EV_USE_EPOLL
635 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 713 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
638 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 716 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
639#endif 717#endif
640#if EV_USE_SELECT 718#if EV_USE_SELECT
641 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 719 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
642#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, );
643 731
644 method = 0; 732 method = 0;
645 /*TODO*/ 733 /*TODO*/
646} 734}
647 735
659 747
660#if EV_MULTIPLICITY 748#if EV_MULTIPLICITY
661struct ev_loop * 749struct ev_loop *
662ev_loop_new (int methods) 750ev_loop_new (int methods)
663{ 751{
664 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));
665 755
666 loop_init (EV_A_ methods); 756 loop_init (EV_A_ methods);
667 757
668 if (ev_method (EV_A)) 758 if (ev_method (EV_A))
669 return loop; 759 return loop;
673 763
674void 764void
675ev_loop_destroy (EV_P) 765ev_loop_destroy (EV_P)
676{ 766{
677 loop_destroy (EV_A); 767 loop_destroy (EV_A);
678 free (loop); 768 ev_free (loop);
679} 769}
680 770
681void 771void
682ev_loop_fork (EV_P) 772ev_loop_fork (EV_P)
683{ 773{
790} 880}
791 881
792static void 882static void
793timers_reify (EV_P) 883timers_reify (EV_P)
794{ 884{
795 while (timercnt && timers [0]->at <= mn_now) 885 while (timercnt && ((WT)timers [0])->at <= mn_now)
796 { 886 {
797 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)));
798 890
799 /* first reschedule or stop timer */ 891 /* first reschedule or stop timer */
800 if (w->repeat) 892 if (w->repeat)
801 { 893 {
802 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.));
803 w->at = mn_now + w->repeat; 895 ((WT)w)->at = mn_now + w->repeat;
804 downheap ((WT *)timers, timercnt, 0); 896 downheap ((WT *)timers, timercnt, 0);
805 } 897 }
806 else 898 else
807 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 899 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
808 900
811} 903}
812 904
813static void 905static void
814periodics_reify (EV_P) 906periodics_reify (EV_P)
815{ 907{
816 while (periodiccnt && periodics [0]->at <= rt_now) 908 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
817 { 909 {
818 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)));
819 913
820 /* first reschedule or stop timer */ 914 /* first reschedule or stop timer */
821 if (w->interval) 915 if (w->interval)
822 { 916 {
823 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;
824 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));
825 downheap ((WT *)periodics, periodiccnt, 0); 919 downheap ((WT *)periodics, periodiccnt, 0);
826 } 920 }
827 else 921 else
828 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 922 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
829 923
841 { 935 {
842 struct ev_periodic *w = periodics [i]; 936 struct ev_periodic *w = periodics [i];
843 937
844 if (w->interval) 938 if (w->interval)
845 { 939 {
846 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;
847 941
848 if (fabs (diff) >= 1e-4) 942 if (fabs (diff) >= 1e-4)
849 { 943 {
850 ev_periodic_stop (EV_A_ w); 944 ev_periodic_stop (EV_A_ w);
851 ev_periodic_start (EV_A_ w); 945 ev_periodic_start (EV_A_ w);
912 { 1006 {
913 periodics_reschedule (EV_A); 1007 periodics_reschedule (EV_A);
914 1008
915 /* 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 */
916 for (i = 0; i < timercnt; ++i) 1010 for (i = 0; i < timercnt; ++i)
917 timers [i]->at += rt_now - mn_now; 1011 ((WT)timers [i])->at += rt_now - mn_now;
918 } 1012 }
919 1013
920 mn_now = rt_now; 1014 mn_now = rt_now;
921 } 1015 }
922} 1016}
973 { 1067 {
974 block = MAX_BLOCKTIME; 1068 block = MAX_BLOCKTIME;
975 1069
976 if (timercnt) 1070 if (timercnt)
977 { 1071 {
978 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1072 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
979 if (block > to) block = to; 1073 if (block > to) block = to;
980 } 1074 }
981 1075
982 if (periodiccnt) 1076 if (periodiccnt)
983 { 1077 {
984 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1078 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
985 if (block > to) block = to; 1079 if (block > to) block = to;
986 } 1080 }
987 1081
988 if (block < 0.) block = 0.; 1082 if (block < 0.) block = 0.;
989 } 1083 }
1106ev_timer_start (EV_P_ struct ev_timer *w) 1200ev_timer_start (EV_P_ struct ev_timer *w)
1107{ 1201{
1108 if (ev_is_active (w)) 1202 if (ev_is_active (w))
1109 return; 1203 return;
1110 1204
1111 w->at += mn_now; 1205 ((WT)w)->at += mn_now;
1112 1206
1113 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.));
1114 1208
1115 ev_start (EV_A_ (W)w, ++timercnt); 1209 ev_start (EV_A_ (W)w, ++timercnt);
1116 array_needsize (timers, timermax, timercnt, ); 1210 array_needsize (timers, timermax, timercnt, );
1117 timers [timercnt - 1] = w; 1211 timers [timercnt - 1] = w;
1118 upheap ((WT *)timers, timercnt - 1); 1212 upheap ((WT *)timers, timercnt - 1);
1213
1214 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1119} 1215}
1120 1216
1121void 1217void
1122ev_timer_stop (EV_P_ struct ev_timer *w) 1218ev_timer_stop (EV_P_ struct ev_timer *w)
1123{ 1219{
1124 ev_clear_pending (EV_A_ (W)w); 1220 ev_clear_pending (EV_A_ (W)w);
1125 if (!ev_is_active (w)) 1221 if (!ev_is_active (w))
1126 return; 1222 return;
1127 1223
1224 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1225
1128 if (w->active < timercnt--) 1226 if (((W)w)->active < timercnt--)
1129 { 1227 {
1130 timers [w->active - 1] = timers [timercnt]; 1228 timers [((W)w)->active - 1] = timers [timercnt];
1131 downheap ((WT *)timers, timercnt, w->active - 1); 1229 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1132 } 1230 }
1133 1231
1134 w->at = w->repeat; 1232 ((WT)w)->at = w->repeat;
1135 1233
1136 ev_stop (EV_A_ (W)w); 1234 ev_stop (EV_A_ (W)w);
1137} 1235}
1138 1236
1139void 1237void
1141{ 1239{
1142 if (ev_is_active (w)) 1240 if (ev_is_active (w))
1143 { 1241 {
1144 if (w->repeat) 1242 if (w->repeat)
1145 { 1243 {
1146 w->at = mn_now + w->repeat; 1244 ((WT)w)->at = mn_now + w->repeat;
1147 downheap ((WT *)timers, timercnt, w->active - 1); 1245 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1148 } 1246 }
1149 else 1247 else
1150 ev_timer_stop (EV_A_ w); 1248 ev_timer_stop (EV_A_ w);
1151 } 1249 }
1152 else if (w->repeat) 1250 else if (w->repeat)
1161 1259
1162 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.));
1163 1261
1164 /* 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 */
1165 if (w->interval) 1263 if (w->interval)
1166 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;
1167 1265
1168 ev_start (EV_A_ (W)w, ++periodiccnt); 1266 ev_start (EV_A_ (W)w, ++periodiccnt);
1169 array_needsize (periodics, periodicmax, periodiccnt, ); 1267 array_needsize (periodics, periodicmax, periodiccnt, );
1170 periodics [periodiccnt - 1] = w; 1268 periodics [periodiccnt - 1] = w;
1171 upheap ((WT *)periodics, periodiccnt - 1); 1269 upheap ((WT *)periodics, periodiccnt - 1);
1270
1271 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1172} 1272}
1173 1273
1174void 1274void
1175ev_periodic_stop (EV_P_ struct ev_periodic *w) 1275ev_periodic_stop (EV_P_ struct ev_periodic *w)
1176{ 1276{
1177 ev_clear_pending (EV_A_ (W)w); 1277 ev_clear_pending (EV_A_ (W)w);
1178 if (!ev_is_active (w)) 1278 if (!ev_is_active (w))
1179 return; 1279 return;
1180 1280
1281 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1282
1181 if (w->active < periodiccnt--) 1283 if (((W)w)->active < periodiccnt--)
1182 { 1284 {
1183 periodics [w->active - 1] = periodics [periodiccnt]; 1285 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1184 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1286 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1185 } 1287 }
1186 1288
1187 ev_stop (EV_A_ (W)w); 1289 ev_stop (EV_A_ (W)w);
1188} 1290}
1189 1291
1203{ 1305{
1204 ev_clear_pending (EV_A_ (W)w); 1306 ev_clear_pending (EV_A_ (W)w);
1205 if (ev_is_active (w)) 1307 if (ev_is_active (w))
1206 return; 1308 return;
1207 1309
1208 idles [w->active - 1] = idles [--idlecnt]; 1310 idles [((W)w)->active - 1] = idles [--idlecnt];
1209 ev_stop (EV_A_ (W)w); 1311 ev_stop (EV_A_ (W)w);
1210} 1312}
1211 1313
1212void 1314void
1213ev_prepare_start (EV_P_ struct ev_prepare *w) 1315ev_prepare_start (EV_P_ struct ev_prepare *w)
1225{ 1327{
1226 ev_clear_pending (EV_A_ (W)w); 1328 ev_clear_pending (EV_A_ (W)w);
1227 if (ev_is_active (w)) 1329 if (ev_is_active (w))
1228 return; 1330 return;
1229 1331
1230 prepares [w->active - 1] = prepares [--preparecnt]; 1332 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1231 ev_stop (EV_A_ (W)w); 1333 ev_stop (EV_A_ (W)w);
1232} 1334}
1233 1335
1234void 1336void
1235ev_check_start (EV_P_ struct ev_check *w) 1337ev_check_start (EV_P_ struct ev_check *w)
1247{ 1349{
1248 ev_clear_pending (EV_A_ (W)w); 1350 ev_clear_pending (EV_A_ (W)w);
1249 if (ev_is_active (w)) 1351 if (ev_is_active (w))
1250 return; 1352 return;
1251 1353
1252 checks [w->active - 1] = checks [--checkcnt]; 1354 checks [((W)w)->active - 1] = checks [--checkcnt];
1253 ev_stop (EV_A_ (W)w); 1355 ev_stop (EV_A_ (W)w);
1254} 1356}
1255 1357
1256#ifndef SA_RESTART 1358#ifndef SA_RESTART
1257# define SA_RESTART 0 1359# define SA_RESTART 0
1270 1372
1271 ev_start (EV_A_ (W)w, 1); 1373 ev_start (EV_A_ (W)w, 1);
1272 array_needsize (signals, signalmax, w->signum, signals_init); 1374 array_needsize (signals, signalmax, w->signum, signals_init);
1273 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1375 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1274 1376
1275 if (!w->next) 1377 if (!((WL)w)->next)
1276 { 1378 {
1379#if WIN32
1380 signal (w->signum, sighandler);
1381#else
1277 struct sigaction sa; 1382 struct sigaction sa;
1278 sa.sa_handler = sighandler; 1383 sa.sa_handler = sighandler;
1279 sigfillset (&sa.sa_mask); 1384 sigfillset (&sa.sa_mask);
1280 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 */
1281 sigaction (w->signum, &sa, 0); 1386 sigaction (w->signum, &sa, 0);
1387#endif
1282 } 1388 }
1283} 1389}
1284 1390
1285void 1391void
1286ev_signal_stop (EV_P_ struct ev_signal *w) 1392ev_signal_stop (EV_P_ struct ev_signal *w)
1336 void (*cb)(int revents, void *arg) = once->cb; 1442 void (*cb)(int revents, void *arg) = once->cb;
1337 void *arg = once->arg; 1443 void *arg = once->arg;
1338 1444
1339 ev_io_stop (EV_A_ &once->io); 1445 ev_io_stop (EV_A_ &once->io);
1340 ev_timer_stop (EV_A_ &once->to); 1446 ev_timer_stop (EV_A_ &once->to);
1341 free (once); 1447 ev_free (once);
1342 1448
1343 cb (revents, arg); 1449 cb (revents, arg);
1344} 1450}
1345 1451
1346static void 1452static void
1356} 1462}
1357 1463
1358void 1464void
1359ev_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)
1360{ 1466{
1361 struct ev_once *once = malloc (sizeof (struct ev_once)); 1467 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1362 1468
1363 if (!once) 1469 if (!once)
1364 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1470 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1365 else 1471 else
1366 { 1472 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines