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

Comparing libev/ev.c (file contents):
Revision 1.73 by root, Tue Nov 6 16:27:10 2007 UTC vs.
Revision 1.78 by root, Thu Nov 8 21:08:56 2007 UTC

268ev_now (EV_P) 268ev_now (EV_P)
269{ 269{
270 return rt_now; 270 return rt_now;
271} 271}
272 272
273#define array_roundsize(base,n) ((n) | 4 & ~3) 273#define array_roundsize(type,n) ((n) | 4 & ~3)
274 274
275#define array_needsize(base,cur,cnt,init) \ 275#define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \ 276 if (expect_false ((cnt) > cur)) \
277 { \ 277 { \
278 int newcnt = cur; \ 278 int newcnt = cur; \
279 do \ 279 do \
280 { \ 280 { \
281 newcnt = array_roundsize (base, newcnt << 1); \ 281 newcnt = array_roundsize (type, newcnt << 1); \
282 } \ 282 } \
283 while ((cnt) > newcnt); \ 283 while ((cnt) > newcnt); \
284 \ 284 \
285 base = ev_realloc (base, sizeof (*base) * (newcnt)); \ 285 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
286 init (base + cur, newcnt - cur); \ 286 init (base + cur, newcnt - cur); \
287 cur = newcnt; \ 287 cur = newcnt; \
288 } 288 }
289 289
290#define array_slim(stem) \ 290#define array_slim(type,stem) \
291 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 291 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
292 { \ 292 { \
293 stem ## max = array_roundsize (stem ## cnt >> 1); \ 293 stem ## max = array_roundsize (stem ## cnt >> 1); \
294 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 294 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
296 } 296 }
297 297
298/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ 298/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
299/* bringing us everlasting joy in form of stupid extra macros that are not required in C */ 299/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
316 316
317 ++base; 317 ++base;
318 } 318 }
319} 319}
320 320
321static void 321void
322event (EV_P_ W w, int events) 322ev_feed_event (EV_P_ void *w, int revents)
323{ 323{
324 W w_ = (W)w;
325
324 if (w->pending) 326 if (w_->pending)
325 { 327 {
326 pendings [ABSPRI (w)][w->pending - 1].events |= events; 328 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
327 return; 329 return;
328 } 330 }
329 331
330 w->pending = ++pendingcnt [ABSPRI (w)]; 332 w_->pending = ++pendingcnt [ABSPRI (w_)];
331 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); 333 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
332 pendings [ABSPRI (w)][w->pending - 1].w = w; 334 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
333 pendings [ABSPRI (w)][w->pending - 1].events = events; 335 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
334} 336}
335 337
336static void 338static void
337queue_events (EV_P_ W *events, int eventcnt, int type) 339queue_events (EV_P_ W *events, int eventcnt, int type)
338{ 340{
339 int i; 341 int i;
340 342
341 for (i = 0; i < eventcnt; ++i) 343 for (i = 0; i < eventcnt; ++i)
342 event (EV_A_ events [i], type); 344 ev_feed_event (EV_A_ events [i], type);
343} 345}
344 346
345static void 347static void
346fd_event (EV_P_ int fd, int events) 348fd_event (EV_P_ int fd, int events)
347{ 349{
351 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 353 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
352 { 354 {
353 int ev = w->events & events; 355 int ev = w->events & events;
354 356
355 if (ev) 357 if (ev)
356 event (EV_A_ (W)w, ev); 358 ev_feed_event (EV_A_ (W)w, ev);
357 } 359 }
358} 360}
359 361
360/*****************************************************************************/ 362/*****************************************************************************/
361 363
391 return; 393 return;
392 394
393 anfds [fd].reify = 1; 395 anfds [fd].reify = 1;
394 396
395 ++fdchangecnt; 397 ++fdchangecnt;
396 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); 398 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
397 fdchanges [fdchangecnt - 1] = fd; 399 fdchanges [fdchangecnt - 1] = fd;
398} 400}
399 401
400static void 402static void
401fd_kill (EV_P_ int fd) 403fd_kill (EV_P_ int fd)
403 struct ev_io *w; 405 struct ev_io *w;
404 406
405 while ((w = (struct ev_io *)anfds [fd].head)) 407 while ((w = (struct ev_io *)anfds [fd].head))
406 { 408 {
407 ev_io_stop (EV_A_ w); 409 ev_io_stop (EV_A_ w);
408 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 410 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
409 } 411 }
410} 412}
411 413
412static int 414static int
413fd_valid (int fd) 415fd_valid (int fd)
541 543
542 if (!gotsig) 544 if (!gotsig)
543 { 545 {
544 int old_errno = errno; 546 int old_errno = errno;
545 gotsig = 1; 547 gotsig = 1;
548#ifdef WIN32
549 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
550#else
546 write (sigpipe [1], &signum, 1); 551 write (sigpipe [1], &signum, 1);
552#endif
547 errno = old_errno; 553 errno = old_errno;
548 } 554 }
549} 555}
550 556
551static void 557static void
552sigcb (EV_P_ struct ev_io *iow, int revents) 558sigcb (EV_P_ struct ev_io *iow, int revents)
553{ 559{
554 WL w; 560 WL w;
555 int signum; 561 int signum;
556 562
563#ifdef WIN32
564 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
565#else
557 read (sigpipe [0], &revents, 1); 566 read (sigpipe [0], &revents, 1);
567#endif
558 gotsig = 0; 568 gotsig = 0;
559 569
560 for (signum = signalmax; signum--; ) 570 for (signum = signalmax; signum--; )
561 if (signals [signum].gotsig) 571 if (signals [signum].gotsig)
562 { 572 {
563 signals [signum].gotsig = 0; 573 signals [signum].gotsig = 0;
564 574
565 for (w = signals [signum].head; w; w = w->next) 575 for (w = signals [signum].head; w; w = w->next)
566 event (EV_A_ (W)w, EV_SIGNAL); 576 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
567 } 577 }
568} 578}
569 579
570static void 580static void
571siginit (EV_P) 581siginit (EV_P)
605 if (w->pid == pid || !w->pid) 615 if (w->pid == pid || !w->pid)
606 { 616 {
607 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 617 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
608 w->rpid = pid; 618 w->rpid = pid;
609 w->rstatus = status; 619 w->rstatus = status;
610 event (EV_A_ (W)w, EV_CHILD); 620 ev_feed_event (EV_A_ (W)w, EV_CHILD);
611 } 621 }
612} 622}
613 623
614static void 624static void
615childcb (EV_P_ struct ev_signal *sw, int revents) 625childcb (EV_P_ struct ev_signal *sw, int revents)
617 int pid, status; 627 int pid, status;
618 628
619 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 629 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
620 { 630 {
621 /* make sure we are called again until all childs have been reaped */ 631 /* make sure we are called again until all childs have been reaped */
622 event (EV_A_ (W)sw, EV_SIGNAL); 632 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
623 633
624 child_reap (EV_A_ sw, pid, pid, status); 634 child_reap (EV_A_ sw, pid, pid, status);
625 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 635 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
626 } 636 }
627} 637}
888 if (method) 898 if (method)
889 postfork = 1; 899 postfork = 1;
890} 900}
891 901
892/*****************************************************************************/ 902/*****************************************************************************/
903
904static int
905any_pending (EV_P)
906{
907 int pri;
908
909 for (pri = NUMPRI; pri--; )
910 if (pendingcnt [pri])
911 return 1;
912
913 return 0;
914}
893 915
894static void 916static void
895call_pending (EV_P) 917call_pending (EV_P)
896{ 918{
897 int pri; 919 int pri;
926 downheap ((WT *)timers, timercnt, 0); 948 downheap ((WT *)timers, timercnt, 0);
927 } 949 }
928 else 950 else
929 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 951 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
930 952
931 event (EV_A_ (W)w, EV_TIMEOUT); 953 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
932 } 954 }
933} 955}
934 956
935static void 957static void
936periodics_reify (EV_P) 958periodics_reify (EV_P)
940 struct ev_periodic *w = periodics [0]; 962 struct ev_periodic *w = periodics [0];
941 963
942 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 964 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
943 965
944 /* first reschedule or stop timer */ 966 /* first reschedule or stop timer */
967 if (w->reschedule_cb)
968 {
969 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001);
970
971 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now));
972 downheap ((WT *)periodics, periodiccnt, 0);
973 }
945 if (w->interval) 974 else if (w->interval)
946 { 975 {
947 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 976 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
948 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 977 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
949 downheap ((WT *)periodics, periodiccnt, 0); 978 downheap ((WT *)periodics, periodiccnt, 0);
950 } 979 }
951 else 980 else
952 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 981 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
953 982
954 event (EV_A_ (W)w, EV_PERIODIC); 983 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
955 } 984 }
956} 985}
957 986
958static void 987static void
959periodics_reschedule (EV_P) 988periodics_reschedule (EV_P)
963 /* adjust periodics after time jump */ 992 /* adjust periodics after time jump */
964 for (i = 0; i < periodiccnt; ++i) 993 for (i = 0; i < periodiccnt; ++i)
965 { 994 {
966 struct ev_periodic *w = periodics [i]; 995 struct ev_periodic *w = periodics [i];
967 996
997 if (w->reschedule_cb)
998 ((WT)w)->at = w->reschedule_cb (w, rt_now);
968 if (w->interval) 999 else if (w->interval)
969 {
970 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1000 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
971
972 if (fabs (diff) >= 1e-4)
973 {
974 ev_periodic_stop (EV_A_ w);
975 ev_periodic_start (EV_A_ w);
976
977 i = 0; /* restart loop, inefficient, but time jumps should be rare */
978 }
979 }
980 } 1001 }
1002
1003 /* now rebuild the heap */
1004 for (i = periodiccnt >> 1; i--; )
1005 downheap ((WT *)periodics, periodiccnt, i);
981} 1006}
982 1007
983inline int 1008inline int
984time_update_monotonic (EV_P) 1009time_update_monotonic (EV_P)
985{ 1010{
1081 /* update fd-related kernel structures */ 1106 /* update fd-related kernel structures */
1082 fd_reify (EV_A); 1107 fd_reify (EV_A);
1083 1108
1084 /* calculate blocking time */ 1109 /* calculate blocking time */
1085 1110
1086 /* we only need this for !monotonic clockor timers, but as we basically 1111 /* we only need this for !monotonic clock or timers, but as we basically
1087 always have timers, we just calculate it always */ 1112 always have timers, we just calculate it always */
1088#if EV_USE_MONOTONIC 1113#if EV_USE_MONOTONIC
1089 if (expect_true (have_monotonic)) 1114 if (expect_true (have_monotonic))
1090 time_update_monotonic (EV_A); 1115 time_update_monotonic (EV_A);
1091 else 1116 else
1124 /* queue pending timers and reschedule them */ 1149 /* queue pending timers and reschedule them */
1125 timers_reify (EV_A); /* relative timers called last */ 1150 timers_reify (EV_A); /* relative timers called last */
1126 periodics_reify (EV_A); /* absolute timers called first */ 1151 periodics_reify (EV_A); /* absolute timers called first */
1127 1152
1128 /* queue idle watchers unless io or timers are pending */ 1153 /* queue idle watchers unless io or timers are pending */
1129 if (!pendingcnt) 1154 if (idlecnt && !any_pending (EV_A))
1130 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1155 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1131 1156
1132 /* queue check watchers, to be executed first */ 1157 /* queue check watchers, to be executed first */
1133 if (checkcnt) 1158 if (checkcnt)
1134 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1159 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1209 return; 1234 return;
1210 1235
1211 assert (("ev_io_start called with negative fd", fd >= 0)); 1236 assert (("ev_io_start called with negative fd", fd >= 0));
1212 1237
1213 ev_start (EV_A_ (W)w, 1); 1238 ev_start (EV_A_ (W)w, 1);
1214 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1239 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1215 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1240 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1216 1241
1217 fd_change (EV_A_ fd); 1242 fd_change (EV_A_ fd);
1218} 1243}
1219 1244
1239 ((WT)w)->at += mn_now; 1264 ((WT)w)->at += mn_now;
1240 1265
1241 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1266 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1242 1267
1243 ev_start (EV_A_ (W)w, ++timercnt); 1268 ev_start (EV_A_ (W)w, ++timercnt);
1244 array_needsize (timers, timermax, timercnt, (void)); 1269 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1245 timers [timercnt - 1] = w; 1270 timers [timercnt - 1] = w;
1246 upheap ((WT *)timers, timercnt - 1); 1271 upheap ((WT *)timers, timercnt - 1);
1247 1272
1248 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1273 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1249} 1274}
1289ev_periodic_start (EV_P_ struct ev_periodic *w) 1314ev_periodic_start (EV_P_ struct ev_periodic *w)
1290{ 1315{
1291 if (ev_is_active (w)) 1316 if (ev_is_active (w))
1292 return; 1317 return;
1293 1318
1319 if (w->reschedule_cb)
1320 ((WT)w)->at = w->reschedule_cb (w, rt_now);
1321 else if (w->interval)
1322 {
1294 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1323 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1295
1296 /* this formula differs from the one in periodic_reify because we do not always round up */ 1324 /* this formula differs from the one in periodic_reify because we do not always round up */
1297 if (w->interval)
1298 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1325 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1326 }
1299 1327
1300 ev_start (EV_A_ (W)w, ++periodiccnt); 1328 ev_start (EV_A_ (W)w, ++periodiccnt);
1301 array_needsize (periodics, periodicmax, periodiccnt, (void)); 1329 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1302 periodics [periodiccnt - 1] = w; 1330 periodics [periodiccnt - 1] = w;
1303 upheap ((WT *)periodics, periodiccnt - 1); 1331 upheap ((WT *)periodics, periodiccnt - 1);
1304 1332
1305 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1333 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1306} 1334}
1322 1350
1323 ev_stop (EV_A_ (W)w); 1351 ev_stop (EV_A_ (W)w);
1324} 1352}
1325 1353
1326void 1354void
1355ev_periodic_again (EV_P_ struct ev_periodic *w)
1356{
1357 ev_periodic_stop (EV_A_ w);
1358 ev_periodic_start (EV_A_ w);
1359}
1360
1361void
1327ev_idle_start (EV_P_ struct ev_idle *w) 1362ev_idle_start (EV_P_ struct ev_idle *w)
1328{ 1363{
1329 if (ev_is_active (w)) 1364 if (ev_is_active (w))
1330 return; 1365 return;
1331 1366
1332 ev_start (EV_A_ (W)w, ++idlecnt); 1367 ev_start (EV_A_ (W)w, ++idlecnt);
1333 array_needsize (idles, idlemax, idlecnt, (void)); 1368 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1334 idles [idlecnt - 1] = w; 1369 idles [idlecnt - 1] = w;
1335} 1370}
1336 1371
1337void 1372void
1338ev_idle_stop (EV_P_ struct ev_idle *w) 1373ev_idle_stop (EV_P_ struct ev_idle *w)
1350{ 1385{
1351 if (ev_is_active (w)) 1386 if (ev_is_active (w))
1352 return; 1387 return;
1353 1388
1354 ev_start (EV_A_ (W)w, ++preparecnt); 1389 ev_start (EV_A_ (W)w, ++preparecnt);
1355 array_needsize (prepares, preparemax, preparecnt, (void)); 1390 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1356 prepares [preparecnt - 1] = w; 1391 prepares [preparecnt - 1] = w;
1357} 1392}
1358 1393
1359void 1394void
1360ev_prepare_stop (EV_P_ struct ev_prepare *w) 1395ev_prepare_stop (EV_P_ struct ev_prepare *w)
1372{ 1407{
1373 if (ev_is_active (w)) 1408 if (ev_is_active (w))
1374 return; 1409 return;
1375 1410
1376 ev_start (EV_A_ (W)w, ++checkcnt); 1411 ev_start (EV_A_ (W)w, ++checkcnt);
1377 array_needsize (checks, checkmax, checkcnt, (void)); 1412 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1378 checks [checkcnt - 1] = w; 1413 checks [checkcnt - 1] = w;
1379} 1414}
1380 1415
1381void 1416void
1382ev_check_stop (EV_P_ struct ev_check *w) 1417ev_check_stop (EV_P_ struct ev_check *w)
1403 return; 1438 return;
1404 1439
1405 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1440 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1406 1441
1407 ev_start (EV_A_ (W)w, 1); 1442 ev_start (EV_A_ (W)w, 1);
1408 array_needsize (signals, signalmax, w->signum, signals_init); 1443 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1409 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1444 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1410 1445
1411 if (!((WL)w)->next) 1446 if (!((WL)w)->next)
1412 { 1447 {
1413#if WIN32 1448#if WIN32
1496} 1531}
1497 1532
1498void 1533void
1499ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1534ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1500{ 1535{
1501 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1536 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1502 1537
1503 if (!once) 1538 if (!once)
1504 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1539 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1505 else 1540 else
1506 { 1541 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines