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

Comparing libev/ev.c (file contents):
Revision 1.62 by root, Sun Nov 4 20:38:07 2007 UTC vs.
Revision 1.69 by root, Tue Nov 6 00:10:04 2007 UTC

145typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
146typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
147 147
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149 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
150/*****************************************************************************/ 156/*****************************************************************************/
151 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
152typedef struct 203typedef struct
153{ 204{
154 struct ev_watcher_list *head; 205 WL head;
155 unsigned char events; 206 unsigned char events;
156 unsigned char reify; 207 unsigned char reify;
157} ANFD; 208} ANFD;
158 209
159typedef struct 210typedef struct
217 return rt_now; 268 return rt_now;
218} 269}
219 270
220#define array_roundsize(base,n) ((n) | 4 & ~3) 271#define array_roundsize(base,n) ((n) | 4 & ~3)
221 272
222#define array_needsize(base,cur,cnt,init) \ 273#define array_needsize(base,cur,cnt,init) \
223 if (expect_false ((cnt) > cur)) \ 274 if (expect_false ((cnt) > cur)) \
224 { \ 275 { \
225 int newcnt = cur; \ 276 int newcnt = cur; \
226 do \ 277 do \
227 { \ 278 { \
228 newcnt = array_roundsize (base, newcnt << 1); \ 279 newcnt = array_roundsize (base, newcnt << 1); \
229 } \ 280 } \
230 while ((cnt) > newcnt); \ 281 while ((cnt) > newcnt); \
231 \ 282 \
232 base = realloc (base, sizeof (*base) * (newcnt)); \ 283 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
233 init (base + cur, newcnt - cur); \ 284 init (base + cur, newcnt - cur); \
234 cur = newcnt; \ 285 cur = newcnt; \
235 } 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;
236 298
237/*****************************************************************************/ 299/*****************************************************************************/
238 300
239static void 301static void
240anfds_init (ANFD *base, int count) 302anfds_init (ANFD *base, int count)
306 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)
307 events |= w->events; 369 events |= w->events;
308 370
309 anfd->reify = 0; 371 anfd->reify = 0;
310 372
311 if (anfd->events != events)
312 {
313 method_modify (EV_A_ fd, anfd->events, events); 373 method_modify (EV_A_ fd, anfd->events, events);
314 anfd->events = events; 374 anfd->events = events;
315 }
316 } 375 }
317 376
318 fdchangecnt = 0; 377 fdchangecnt = 0;
319} 378}
320 379
362 int fd; 421 int fd;
363 422
364 for (fd = anfdmax; fd--; ) 423 for (fd = anfdmax; fd--; )
365 if (anfds [fd].events) 424 if (anfds [fd].events)
366 { 425 {
367 close (fd);
368 fd_kill (EV_A_ fd); 426 fd_kill (EV_A_ fd);
369 return; 427 return;
370 } 428 }
371} 429}
372 430
430 488
431/*****************************************************************************/ 489/*****************************************************************************/
432 490
433typedef struct 491typedef struct
434{ 492{
435 struct ev_watcher_list *head; 493 WL head;
436 sig_atomic_t volatile gotsig; 494 sig_atomic_t volatile gotsig;
437} ANSIG; 495} ANSIG;
438 496
439static ANSIG *signals; 497static ANSIG *signals;
440static int signalmax; 498static int signalmax;
456} 514}
457 515
458static void 516static void
459sighandler (int signum) 517sighandler (int signum)
460{ 518{
519#if WIN32
520 signal (signum, sighandler);
521#endif
522
461 signals [signum - 1].gotsig = 1; 523 signals [signum - 1].gotsig = 1;
462 524
463 if (!gotsig) 525 if (!gotsig)
464 { 526 {
465 int old_errno = errno; 527 int old_errno = errno;
470} 532}
471 533
472static void 534static void
473sigcb (EV_P_ struct ev_io *iow, int revents) 535sigcb (EV_P_ struct ev_io *iow, int revents)
474{ 536{
475 struct ev_watcher_list *w; 537 WL w;
476 int signum; 538 int signum;
477 539
478 read (sigpipe [0], &revents, 1); 540 read (sigpipe [0], &revents, 1);
479 gotsig = 0; 541 gotsig = 0;
480 542
522 struct ev_child *w; 584 struct ev_child *w;
523 585
524 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)
525 if (w->pid == pid || !w->pid) 587 if (w->pid == pid || !w->pid)
526 { 588 {
527 w->priority = sw->priority; /* need to do it *now* */ 589 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
528 w->rpid = pid; 590 w->rpid = pid;
529 w->rstatus = status; 591 w->rstatus = status;
530 event (EV_A_ (W)w, EV_CHILD); 592 event (EV_A_ (W)w, EV_CHILD);
531 } 593 }
532} 594}
533 595
534static void 596static void
637} 699}
638 700
639void 701void
640loop_destroy (EV_P) 702loop_destroy (EV_P)
641{ 703{
704 int i;
705
642#if EV_USE_WIN32 706#if EV_USE_WIN32
643 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); 707 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
644#endif 708#endif
645#if EV_USE_KQUEUE 709#if EV_USE_KQUEUE
646 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 710 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
652 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 716 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
653#endif 717#endif
654#if EV_USE_SELECT 718#if EV_USE_SELECT
655 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 719 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
656#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, );
657 731
658 method = 0; 732 method = 0;
659 /*TODO*/ 733 /*TODO*/
660} 734}
661 735
673 747
674#if EV_MULTIPLICITY 748#if EV_MULTIPLICITY
675struct ev_loop * 749struct ev_loop *
676ev_loop_new (int methods) 750ev_loop_new (int methods)
677{ 751{
678 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));
679 755
680 loop_init (EV_A_ methods); 756 loop_init (EV_A_ methods);
681 757
682 if (ev_method (EV_A)) 758 if (ev_method (EV_A))
683 return loop; 759 return loop;
687 763
688void 764void
689ev_loop_destroy (EV_P) 765ev_loop_destroy (EV_P)
690{ 766{
691 loop_destroy (EV_A); 767 loop_destroy (EV_A);
692 free (loop); 768 ev_free (loop);
693} 769}
694 770
695void 771void
696ev_loop_fork (EV_P) 772ev_loop_fork (EV_P)
697{ 773{
804} 880}
805 881
806static void 882static void
807timers_reify (EV_P) 883timers_reify (EV_P)
808{ 884{
809 while (timercnt && timers [0]->at <= mn_now) 885 while (timercnt && ((WT)timers [0])->at <= mn_now)
810 { 886 {
811 struct ev_timer *w = timers [0]; 887 struct ev_timer *w = timers [0];
812 888
813 assert (("inactive timer on timer heap detected", ev_is_active (w))); 889 assert (("inactive timer on timer heap detected", ev_is_active (w)));
814 890
815 /* first reschedule or stop timer */ 891 /* first reschedule or stop timer */
816 if (w->repeat) 892 if (w->repeat)
817 { 893 {
818 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.));
819 w->at = mn_now + w->repeat; 895 ((WT)w)->at = mn_now + w->repeat;
820 downheap ((WT *)timers, timercnt, 0); 896 downheap ((WT *)timers, timercnt, 0);
821 } 897 }
822 else 898 else
823 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 899 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
824 900
827} 903}
828 904
829static void 905static void
830periodics_reify (EV_P) 906periodics_reify (EV_P)
831{ 907{
832 while (periodiccnt && periodics [0]->at <= rt_now) 908 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
833 { 909 {
834 struct ev_periodic *w = periodics [0]; 910 struct ev_periodic *w = periodics [0];
835 911
836 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 912 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
837 913
838 /* first reschedule or stop timer */ 914 /* first reschedule or stop timer */
839 if (w->interval) 915 if (w->interval)
840 { 916 {
841 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;
842 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));
843 downheap ((WT *)periodics, periodiccnt, 0); 919 downheap ((WT *)periodics, periodiccnt, 0);
844 } 920 }
845 else 921 else
846 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 922 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
847 923
859 { 935 {
860 struct ev_periodic *w = periodics [i]; 936 struct ev_periodic *w = periodics [i];
861 937
862 if (w->interval) 938 if (w->interval)
863 { 939 {
864 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;
865 941
866 if (fabs (diff) >= 1e-4) 942 if (fabs (diff) >= 1e-4)
867 { 943 {
868 ev_periodic_stop (EV_A_ w); 944 ev_periodic_stop (EV_A_ w);
869 ev_periodic_start (EV_A_ w); 945 ev_periodic_start (EV_A_ w);
930 { 1006 {
931 periodics_reschedule (EV_A); 1007 periodics_reschedule (EV_A);
932 1008
933 /* 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 */
934 for (i = 0; i < timercnt; ++i) 1010 for (i = 0; i < timercnt; ++i)
935 timers [i]->at += rt_now - mn_now; 1011 ((WT)timers [i])->at += rt_now - mn_now;
936 } 1012 }
937 1013
938 mn_now = rt_now; 1014 mn_now = rt_now;
939 } 1015 }
940} 1016}
991 { 1067 {
992 block = MAX_BLOCKTIME; 1068 block = MAX_BLOCKTIME;
993 1069
994 if (timercnt) 1070 if (timercnt)
995 { 1071 {
996 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1072 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
997 if (block > to) block = to; 1073 if (block > to) block = to;
998 } 1074 }
999 1075
1000 if (periodiccnt) 1076 if (periodiccnt)
1001 { 1077 {
1002 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1078 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
1003 if (block > to) block = to; 1079 if (block > to) block = to;
1004 } 1080 }
1005 1081
1006 if (block < 0.) block = 0.; 1082 if (block < 0.) block = 0.;
1007 } 1083 }
1124ev_timer_start (EV_P_ struct ev_timer *w) 1200ev_timer_start (EV_P_ struct ev_timer *w)
1125{ 1201{
1126 if (ev_is_active (w)) 1202 if (ev_is_active (w))
1127 return; 1203 return;
1128 1204
1129 w->at += mn_now; 1205 ((WT)w)->at += mn_now;
1130 1206
1131 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.));
1132 1208
1133 ev_start (EV_A_ (W)w, ++timercnt); 1209 ev_start (EV_A_ (W)w, ++timercnt);
1134 array_needsize (timers, timermax, timercnt, ); 1210 array_needsize (timers, timermax, timercnt, );
1151 { 1227 {
1152 timers [((W)w)->active - 1] = timers [timercnt]; 1228 timers [((W)w)->active - 1] = timers [timercnt];
1153 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1229 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1154 } 1230 }
1155 1231
1156 w->at = w->repeat; 1232 ((WT)w)->at = w->repeat;
1157 1233
1158 ev_stop (EV_A_ (W)w); 1234 ev_stop (EV_A_ (W)w);
1159} 1235}
1160 1236
1161void 1237void
1163{ 1239{
1164 if (ev_is_active (w)) 1240 if (ev_is_active (w))
1165 { 1241 {
1166 if (w->repeat) 1242 if (w->repeat)
1167 { 1243 {
1168 w->at = mn_now + w->repeat; 1244 ((WT)w)->at = mn_now + w->repeat;
1169 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1245 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1170 } 1246 }
1171 else 1247 else
1172 ev_timer_stop (EV_A_ w); 1248 ev_timer_stop (EV_A_ w);
1173 } 1249 }
1183 1259
1184 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.));
1185 1261
1186 /* 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 */
1187 if (w->interval) 1263 if (w->interval)
1188 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;
1189 1265
1190 ev_start (EV_A_ (W)w, ++periodiccnt); 1266 ev_start (EV_A_ (W)w, ++periodiccnt);
1191 array_needsize (periodics, periodicmax, periodiccnt, ); 1267 array_needsize (periodics, periodicmax, periodiccnt, );
1192 periodics [periodiccnt - 1] = w; 1268 periodics [periodiccnt - 1] = w;
1193 upheap ((WT *)periodics, periodiccnt - 1); 1269 upheap ((WT *)periodics, periodiccnt - 1);
1296 1372
1297 ev_start (EV_A_ (W)w, 1); 1373 ev_start (EV_A_ (W)w, 1);
1298 array_needsize (signals, signalmax, w->signum, signals_init); 1374 array_needsize (signals, signalmax, w->signum, signals_init);
1299 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1375 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1300 1376
1301 if (!w->next) 1377 if (!((WL)w)->next)
1302 { 1378 {
1379#if WIN32
1380 signal (w->signum, sighandler);
1381#else
1303 struct sigaction sa; 1382 struct sigaction sa;
1304 sa.sa_handler = sighandler; 1383 sa.sa_handler = sighandler;
1305 sigfillset (&sa.sa_mask); 1384 sigfillset (&sa.sa_mask);
1306 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 */
1307 sigaction (w->signum, &sa, 0); 1386 sigaction (w->signum, &sa, 0);
1387#endif
1308 } 1388 }
1309} 1389}
1310 1390
1311void 1391void
1312ev_signal_stop (EV_P_ struct ev_signal *w) 1392ev_signal_stop (EV_P_ struct ev_signal *w)
1362 void (*cb)(int revents, void *arg) = once->cb; 1442 void (*cb)(int revents, void *arg) = once->cb;
1363 void *arg = once->arg; 1443 void *arg = once->arg;
1364 1444
1365 ev_io_stop (EV_A_ &once->io); 1445 ev_io_stop (EV_A_ &once->io);
1366 ev_timer_stop (EV_A_ &once->to); 1446 ev_timer_stop (EV_A_ &once->to);
1367 free (once); 1447 ev_free (once);
1368 1448
1369 cb (revents, arg); 1449 cb (revents, arg);
1370} 1450}
1371 1451
1372static void 1452static void
1382} 1462}
1383 1463
1384void 1464void
1385ev_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)
1386{ 1466{
1387 struct ev_once *once = malloc (sizeof (struct ev_once)); 1467 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1388 1468
1389 if (!once) 1469 if (!once)
1390 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1470 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1391 else 1471 else
1392 { 1472 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines