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

Comparing libev/ev.c (file contents):
Revision 1.157 by root, Wed Nov 28 20:58:32 2007 UTC vs.
Revision 1.176 by root, Tue Dec 11 04:31:55 2007 UTC

216# include <sys/inotify.h> 216# include <sys/inotify.h>
217#endif 217#endif
218 218
219/**/ 219/**/
220 220
221/*
222 * This is used to avoid floating point rounding problems.
223 * It is added to ev_rt_now when scheduling periodics
224 * to ensure progress, time-wise, even when rounding
225 * errors are against us.
226 * This value is good at least till the year 4000
227 * and intervals up to 20 years.
228 * Better solutions welcome.
229 */
230#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
231
221#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 232#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
222#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 233#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
223/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 234/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
224 235
225#if __GNUC__ >= 3 236#if __GNUC__ >= 3
226# define expect(expr,value) __builtin_expect ((expr),(value)) 237# define expect(expr,value) __builtin_expect ((expr),(value))
227# define inline_size static inline /* inline for codesize */
228# if EV_MINIMAL
229# define noinline __attribute__ ((noinline)) 238# define noinline __attribute__ ((noinline))
230# define inline_speed static noinline
231# else
232# define noinline
233# define inline_speed static inline
234# endif
235#else 239#else
236# define expect(expr,value) (expr) 240# define expect(expr,value) (expr)
237# define inline_speed static
238# define inline_size static
239# define noinline 241# define noinline
242# if __STDC_VERSION__ < 199901L
243# define inline
244# endif
240#endif 245#endif
241 246
242#define expect_false(expr) expect ((expr) != 0, 0) 247#define expect_false(expr) expect ((expr) != 0, 0)
243#define expect_true(expr) expect ((expr) != 0, 1) 248#define expect_true(expr) expect ((expr) != 0, 1)
249#define inline_size static inline
250
251#if EV_MINIMAL
252# define inline_speed static noinline
253#else
254# define inline_speed static inline
255#endif
244 256
245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 257#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
246#define ABSPRI(w) ((w)->priority - EV_MINPRI) 258#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
247 259
248#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 260#define EMPTY /* required for microsofts broken pseudo-c compiler */
249#define EMPTY2(a,b) /* used to suppress some warnings */ 261#define EMPTY2(a,b) /* used to suppress some warnings */
250 262
251typedef ev_watcher *W; 263typedef ev_watcher *W;
252typedef ev_watcher_list *WL; 264typedef ev_watcher_list *WL;
253typedef ev_watcher_time *WT; 265typedef ev_watcher_time *WT;
396{ 408{
397 return ev_rt_now; 409 return ev_rt_now;
398} 410}
399#endif 411#endif
400 412
401#define array_roundsize(type,n) (((n) | 4) & ~3) 413int inline_size
414array_nextsize (int elem, int cur, int cnt)
415{
416 int ncur = cur + 1;
417
418 do
419 ncur <<= 1;
420 while (cnt > ncur);
421
422 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
423 if (elem * ncur > 4096)
424 {
425 ncur *= elem;
426 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
427 ncur = ncur - sizeof (void *) * 4;
428 ncur /= elem;
429 }
430
431 return ncur;
432}
433
434static noinline void *
435array_realloc (int elem, void *base, int *cur, int cnt)
436{
437 *cur = array_nextsize (elem, *cur, cnt);
438 return ev_realloc (base, elem * *cur);
439}
402 440
403#define array_needsize(type,base,cur,cnt,init) \ 441#define array_needsize(type,base,cur,cnt,init) \
404 if (expect_false ((cnt) > cur)) \ 442 if (expect_false ((cnt) > (cur))) \
405 { \ 443 { \
406 int newcnt = cur; \ 444 int ocur_ = (cur); \
407 do \ 445 (base) = (type *)array_realloc \
408 { \ 446 (sizeof (type), (base), &(cur), (cnt)); \
409 newcnt = array_roundsize (type, newcnt << 1); \ 447 init ((base) + (ocur_), (cur) - ocur_); \
410 } \
411 while ((cnt) > newcnt); \
412 \
413 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
414 init (base + cur, newcnt - cur); \
415 cur = newcnt; \
416 } 448 }
417 449
450#if 0
418#define array_slim(type,stem) \ 451#define array_slim(type,stem) \
419 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 452 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
420 { \ 453 { \
421 stem ## max = array_roundsize (stem ## cnt >> 1); \ 454 stem ## max = array_roundsize (stem ## cnt >> 1); \
422 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 455 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
423 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 456 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
424 } 457 }
458#endif
425 459
426#define array_free(stem, idx) \ 460#define array_free(stem, idx) \
427 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 461 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
428 462
429/*****************************************************************************/ 463/*****************************************************************************/
430 464
431void noinline 465void noinline
432ev_feed_event (EV_P_ void *w, int revents) 466ev_feed_event (EV_P_ void *w, int revents)
433{ 467{
434 W w_ = (W)w; 468 W w_ = (W)w;
469 int pri = ABSPRI (w_);
435 470
436 if (expect_false (w_->pending)) 471 if (expect_false (w_->pending))
472 pendings [pri][w_->pending - 1].events |= revents;
473 else
437 { 474 {
475 w_->pending = ++pendingcnt [pri];
476 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
477 pendings [pri][w_->pending - 1].w = w_;
438 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 478 pendings [pri][w_->pending - 1].events = revents;
439 return;
440 } 479 }
441
442 w_->pending = ++pendingcnt [ABSPRI (w_)];
443 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
444 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
445 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
446} 480}
447 481
448void inline_size 482void inline_size
449queue_events (EV_P_ W *events, int eventcnt, int type) 483queue_events (EV_P_ W *events, int eventcnt, int type)
450{ 484{
485} 519}
486 520
487void 521void
488ev_feed_fd_event (EV_P_ int fd, int revents) 522ev_feed_fd_event (EV_P_ int fd, int revents)
489{ 523{
524 if (fd >= 0 && fd < anfdmax)
490 fd_event (EV_A_ fd, revents); 525 fd_event (EV_A_ fd, revents);
491} 526}
492 527
493void inline_size 528void inline_size
494fd_reify (EV_P) 529fd_reify (EV_P)
495{ 530{
725 for (signum = signalmax; signum--; ) 760 for (signum = signalmax; signum--; )
726 if (signals [signum].gotsig) 761 if (signals [signum].gotsig)
727 ev_feed_signal_event (EV_A_ signum + 1); 762 ev_feed_signal_event (EV_A_ signum + 1);
728} 763}
729 764
730void inline_size 765void inline_speed
731fd_intern (int fd) 766fd_intern (int fd)
732{ 767{
733#ifdef _WIN32 768#ifdef _WIN32
734 int arg = 1; 769 int arg = 1;
735 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 770 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
764 ev_child *w; 799 ev_child *w;
765 800
766 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 801 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
767 if (w->pid == pid || !w->pid) 802 if (w->pid == pid || !w->pid)
768 { 803 {
769 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 804 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
770 w->rpid = pid; 805 w->rpid = pid;
771 w->rstatus = status; 806 w->rstatus = status;
772 ev_feed_event (EV_A_ (W)w, EV_CHILD); 807 ev_feed_event (EV_A_ (W)w, EV_CHILD);
773 } 808 }
774} 809}
775 810
776#ifndef WCONTINUED 811#ifndef WCONTINUED
886ev_backend (EV_P) 921ev_backend (EV_P)
887{ 922{
888 return backend; 923 return backend;
889} 924}
890 925
926unsigned int
927ev_loop_count (EV_P)
928{
929 return loop_count;
930}
931
891static void noinline 932static void noinline
892loop_init (EV_P_ unsigned int flags) 933loop_init (EV_P_ unsigned int flags)
893{ 934{
894 if (!backend) 935 if (!backend)
895 { 936 {
904 ev_rt_now = ev_time (); 945 ev_rt_now = ev_time ();
905 mn_now = get_clock (); 946 mn_now = get_clock ();
906 now_floor = mn_now; 947 now_floor = mn_now;
907 rtmn_diff = ev_rt_now - mn_now; 948 rtmn_diff = ev_rt_now - mn_now;
908 949
950 /* pid check not overridable via env */
951#ifndef _WIN32
952 if (flags & EVFLAG_FORKCHECK)
953 curpid = getpid ();
954#endif
955
909 if (!(flags & EVFLAG_NOENV) 956 if (!(flags & EVFLAG_NOENV)
910 && !enable_secure () 957 && !enable_secure ()
911 && getenv ("LIBEV_FLAGS")) 958 && getenv ("LIBEV_FLAGS"))
912 flags = atoi (getenv ("LIBEV_FLAGS")); 959 flags = atoi (getenv ("LIBEV_FLAGS"));
913 960
969#if EV_USE_SELECT 1016#if EV_USE_SELECT
970 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1017 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
971#endif 1018#endif
972 1019
973 for (i = NUMPRI; i--; ) 1020 for (i = NUMPRI; i--; )
1021 {
974 array_free (pending, [i]); 1022 array_free (pending, [i]);
1023#if EV_IDLE_ENABLE
1024 array_free (idle, [i]);
1025#endif
1026 }
975 1027
976 /* have to use the microsoft-never-gets-it-right macro */ 1028 /* have to use the microsoft-never-gets-it-right macro */
977 array_free (fdchange, EMPTY0); 1029 array_free (fdchange, EMPTY);
978 array_free (timer, EMPTY0); 1030 array_free (timer, EMPTY);
979#if EV_PERIODIC_ENABLE 1031#if EV_PERIODIC_ENABLE
980 array_free (periodic, EMPTY0); 1032 array_free (periodic, EMPTY);
981#endif 1033#endif
982 array_free (idle, EMPTY0);
983 array_free (prepare, EMPTY0); 1034 array_free (prepare, EMPTY);
984 array_free (check, EMPTY0); 1035 array_free (check, EMPTY);
985 1036
986 backend = 0; 1037 backend = 0;
987} 1038}
988 1039
989void inline_size infy_fork (EV_P); 1040void inline_size infy_fork (EV_P);
1125 postfork = 1; 1176 postfork = 1;
1126} 1177}
1127 1178
1128/*****************************************************************************/ 1179/*****************************************************************************/
1129 1180
1130int inline_size 1181void
1131any_pending (EV_P) 1182ev_invoke (EV_P_ void *w, int revents)
1132{ 1183{
1133 int pri; 1184 EV_CB_INVOKE ((W)w, revents);
1134
1135 for (pri = NUMPRI; pri--; )
1136 if (pendingcnt [pri])
1137 return 1;
1138
1139 return 0;
1140} 1185}
1141 1186
1142void inline_speed 1187void inline_speed
1143call_pending (EV_P) 1188call_pending (EV_P)
1144{ 1189{
1197 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1242 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1198 1243
1199 /* first reschedule or stop timer */ 1244 /* first reschedule or stop timer */
1200 if (w->reschedule_cb) 1245 if (w->reschedule_cb)
1201 { 1246 {
1202 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1247 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1203 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1248 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1204 downheap ((WT *)periodics, periodiccnt, 0); 1249 downheap ((WT *)periodics, periodiccnt, 0);
1205 } 1250 }
1206 else if (w->interval) 1251 else if (w->interval)
1207 { 1252 {
1208 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1253 ((WT)w)->at = w->offset + floor ((ev_rt_now + TIME_EPSILON - w->offset) / w->interval + 1.) * w->interval;
1209 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); 1254 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1210 downheap ((WT *)periodics, periodiccnt, 0); 1255 downheap ((WT *)periodics, periodiccnt, 0);
1211 } 1256 }
1212 else 1257 else
1213 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1258 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1227 ev_periodic *w = periodics [i]; 1272 ev_periodic *w = periodics [i];
1228 1273
1229 if (w->reschedule_cb) 1274 if (w->reschedule_cb)
1230 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1275 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1231 else if (w->interval) 1276 else if (w->interval)
1232 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1277 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1233 } 1278 }
1234 1279
1235 /* now rebuild the heap */ 1280 /* now rebuild the heap */
1236 for (i = periodiccnt >> 1; i--; ) 1281 for (i = periodiccnt >> 1; i--; )
1237 downheap ((WT *)periodics, periodiccnt, i); 1282 downheap ((WT *)periodics, periodiccnt, i);
1283}
1284#endif
1285
1286#if EV_IDLE_ENABLE
1287void inline_size
1288idle_reify (EV_P)
1289{
1290 if (expect_false (idleall))
1291 {
1292 int pri;
1293
1294 for (pri = NUMPRI; pri--; )
1295 {
1296 if (pendingcnt [pri])
1297 break;
1298
1299 if (idlecnt [pri])
1300 {
1301 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1302 break;
1303 }
1304 }
1305 }
1238} 1306}
1239#endif 1307#endif
1240 1308
1241int inline_size 1309int inline_size
1242time_update_monotonic (EV_P) 1310time_update_monotonic (EV_P)
1334{ 1402{
1335 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1403 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1336 ? EVUNLOOP_ONE 1404 ? EVUNLOOP_ONE
1337 : EVUNLOOP_CANCEL; 1405 : EVUNLOOP_CANCEL;
1338 1406
1339 while (activecnt) 1407 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1408
1409 do
1340 { 1410 {
1411#ifndef _WIN32
1412 if (expect_false (curpid)) /* penalise the forking check even more */
1413 if (expect_false (getpid () != curpid))
1414 {
1415 curpid = getpid ();
1416 postfork = 1;
1417 }
1418#endif
1419
1341#if EV_FORK_ENABLE 1420#if EV_FORK_ENABLE
1342 /* we might have forked, so queue fork handlers */ 1421 /* we might have forked, so queue fork handlers */
1343 if (expect_false (postfork)) 1422 if (expect_false (postfork))
1344 if (forkcnt) 1423 if (forkcnt)
1345 { 1424 {
1346 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1425 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1347 call_pending (EV_A); 1426 call_pending (EV_A);
1348 } 1427 }
1349#endif 1428#endif
1350 1429
1351 /* queue check watchers (and execute them) */ 1430 /* queue prepare watchers (and execute them) */
1352 if (expect_false (preparecnt)) 1431 if (expect_false (preparecnt))
1353 { 1432 {
1354 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1433 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1355 call_pending (EV_A); 1434 call_pending (EV_A);
1356 } 1435 }
1357 1436
1437 if (expect_false (!activecnt))
1438 break;
1439
1358 /* we might have forked, so reify kernel state if necessary */ 1440 /* we might have forked, so reify kernel state if necessary */
1359 if (expect_false (postfork)) 1441 if (expect_false (postfork))
1360 loop_fork (EV_A); 1442 loop_fork (EV_A);
1361 1443
1362 /* update fd-related kernel structures */ 1444 /* update fd-related kernel structures */
1364 1446
1365 /* calculate blocking time */ 1447 /* calculate blocking time */
1366 { 1448 {
1367 ev_tstamp block; 1449 ev_tstamp block;
1368 1450
1369 if (flags & EVLOOP_NONBLOCK || idlecnt) 1451 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1370 block = 0.; /* do not block at all */ 1452 block = 0.; /* do not block at all */
1371 else 1453 else
1372 { 1454 {
1373 /* update time to cancel out callback processing overhead */ 1455 /* update time to cancel out callback processing overhead */
1374#if EV_USE_MONOTONIC 1456#if EV_USE_MONOTONIC
1398#endif 1480#endif
1399 1481
1400 if (expect_false (block < 0.)) block = 0.; 1482 if (expect_false (block < 0.)) block = 0.;
1401 } 1483 }
1402 1484
1485 ++loop_count;
1403 backend_poll (EV_A_ block); 1486 backend_poll (EV_A_ block);
1404 } 1487 }
1405 1488
1406 /* update ev_rt_now, do magic */ 1489 /* update ev_rt_now, do magic */
1407 time_update (EV_A); 1490 time_update (EV_A);
1410 timers_reify (EV_A); /* relative timers called last */ 1493 timers_reify (EV_A); /* relative timers called last */
1411#if EV_PERIODIC_ENABLE 1494#if EV_PERIODIC_ENABLE
1412 periodics_reify (EV_A); /* absolute timers called first */ 1495 periodics_reify (EV_A); /* absolute timers called first */
1413#endif 1496#endif
1414 1497
1498#if EV_IDLE_ENABLE
1415 /* queue idle watchers unless other events are pending */ 1499 /* queue idle watchers unless other events are pending */
1416 if (idlecnt && !any_pending (EV_A)) 1500 idle_reify (EV_A);
1417 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1501#endif
1418 1502
1419 /* queue check watchers, to be executed first */ 1503 /* queue check watchers, to be executed first */
1420 if (expect_false (checkcnt)) 1504 if (expect_false (checkcnt))
1421 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1505 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1422 1506
1423 call_pending (EV_A); 1507 call_pending (EV_A);
1424 1508
1425 if (expect_false (loop_done))
1426 break;
1427 } 1509 }
1510 while (expect_true (activecnt && !loop_done));
1428 1511
1429 if (loop_done == EVUNLOOP_ONE) 1512 if (loop_done == EVUNLOOP_ONE)
1430 loop_done = EVUNLOOP_CANCEL; 1513 loop_done = EVUNLOOP_CANCEL;
1431} 1514}
1432 1515
1459 head = &(*head)->next; 1542 head = &(*head)->next;
1460 } 1543 }
1461} 1544}
1462 1545
1463void inline_speed 1546void inline_speed
1464ev_clear_pending (EV_P_ W w) 1547clear_pending (EV_P_ W w)
1465{ 1548{
1466 if (w->pending) 1549 if (w->pending)
1467 { 1550 {
1468 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1551 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1469 w->pending = 0; 1552 w->pending = 0;
1470 } 1553 }
1471} 1554}
1472 1555
1556int
1557ev_clear_pending (EV_P_ void *w)
1558{
1559 W w_ = (W)w;
1560 int pending = w_->pending;
1561
1562 if (expect_true (pending))
1563 {
1564 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1565 w_->pending = 0;
1566 p->w = 0;
1567 return p->events;
1568 }
1569 else
1570 return 0;
1571}
1572
1573void inline_size
1574pri_adjust (EV_P_ W w)
1575{
1576 int pri = w->priority;
1577 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1578 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1579 w->priority = pri;
1580}
1581
1473void inline_speed 1582void inline_speed
1474ev_start (EV_P_ W w, int active) 1583ev_start (EV_P_ W w, int active)
1475{ 1584{
1476 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1585 pri_adjust (EV_A_ w);
1477 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1478
1479 w->active = active; 1586 w->active = active;
1480 ev_ref (EV_A); 1587 ev_ref (EV_A);
1481} 1588}
1482 1589
1483void inline_size 1590void inline_size
1487 w->active = 0; 1594 w->active = 0;
1488} 1595}
1489 1596
1490/*****************************************************************************/ 1597/*****************************************************************************/
1491 1598
1492void 1599void noinline
1493ev_io_start (EV_P_ ev_io *w) 1600ev_io_start (EV_P_ ev_io *w)
1494{ 1601{
1495 int fd = w->fd; 1602 int fd = w->fd;
1496 1603
1497 if (expect_false (ev_is_active (w))) 1604 if (expect_false (ev_is_active (w)))
1504 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1611 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1505 1612
1506 fd_change (EV_A_ fd); 1613 fd_change (EV_A_ fd);
1507} 1614}
1508 1615
1509void 1616void noinline
1510ev_io_stop (EV_P_ ev_io *w) 1617ev_io_stop (EV_P_ ev_io *w)
1511{ 1618{
1512 ev_clear_pending (EV_A_ (W)w); 1619 clear_pending (EV_A_ (W)w);
1513 if (expect_false (!ev_is_active (w))) 1620 if (expect_false (!ev_is_active (w)))
1514 return; 1621 return;
1515 1622
1516 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1623 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1517 1624
1519 ev_stop (EV_A_ (W)w); 1626 ev_stop (EV_A_ (W)w);
1520 1627
1521 fd_change (EV_A_ w->fd); 1628 fd_change (EV_A_ w->fd);
1522} 1629}
1523 1630
1524void 1631void noinline
1525ev_timer_start (EV_P_ ev_timer *w) 1632ev_timer_start (EV_P_ ev_timer *w)
1526{ 1633{
1527 if (expect_false (ev_is_active (w))) 1634 if (expect_false (ev_is_active (w)))
1528 return; 1635 return;
1529 1636
1537 upheap ((WT *)timers, timercnt - 1); 1644 upheap ((WT *)timers, timercnt - 1);
1538 1645
1539 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1646 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1540} 1647}
1541 1648
1542void 1649void noinline
1543ev_timer_stop (EV_P_ ev_timer *w) 1650ev_timer_stop (EV_P_ ev_timer *w)
1544{ 1651{
1545 ev_clear_pending (EV_A_ (W)w); 1652 clear_pending (EV_A_ (W)w);
1546 if (expect_false (!ev_is_active (w))) 1653 if (expect_false (!ev_is_active (w)))
1547 return; 1654 return;
1548 1655
1549 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1656 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1550 1657
1561 ((WT)w)->at -= mn_now; 1668 ((WT)w)->at -= mn_now;
1562 1669
1563 ev_stop (EV_A_ (W)w); 1670 ev_stop (EV_A_ (W)w);
1564} 1671}
1565 1672
1566void 1673void noinline
1567ev_timer_again (EV_P_ ev_timer *w) 1674ev_timer_again (EV_P_ ev_timer *w)
1568{ 1675{
1569 if (ev_is_active (w)) 1676 if (ev_is_active (w))
1570 { 1677 {
1571 if (w->repeat) 1678 if (w->repeat)
1582 ev_timer_start (EV_A_ w); 1689 ev_timer_start (EV_A_ w);
1583 } 1690 }
1584} 1691}
1585 1692
1586#if EV_PERIODIC_ENABLE 1693#if EV_PERIODIC_ENABLE
1587void 1694void noinline
1588ev_periodic_start (EV_P_ ev_periodic *w) 1695ev_periodic_start (EV_P_ ev_periodic *w)
1589{ 1696{
1590 if (expect_false (ev_is_active (w))) 1697 if (expect_false (ev_is_active (w)))
1591 return; 1698 return;
1592 1699
1594 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1701 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1595 else if (w->interval) 1702 else if (w->interval)
1596 { 1703 {
1597 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1704 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1598 /* this formula differs from the one in periodic_reify because we do not always round up */ 1705 /* this formula differs from the one in periodic_reify because we do not always round up */
1599 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1706 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1600 } 1707 }
1708 else
1709 ((WT)w)->at = w->offset;
1601 1710
1602 ev_start (EV_A_ (W)w, ++periodiccnt); 1711 ev_start (EV_A_ (W)w, ++periodiccnt);
1603 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1712 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1604 periodics [periodiccnt - 1] = w; 1713 periodics [periodiccnt - 1] = w;
1605 upheap ((WT *)periodics, periodiccnt - 1); 1714 upheap ((WT *)periodics, periodiccnt - 1);
1606 1715
1607 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1716 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1608} 1717}
1609 1718
1610void 1719void noinline
1611ev_periodic_stop (EV_P_ ev_periodic *w) 1720ev_periodic_stop (EV_P_ ev_periodic *w)
1612{ 1721{
1613 ev_clear_pending (EV_A_ (W)w); 1722 clear_pending (EV_A_ (W)w);
1614 if (expect_false (!ev_is_active (w))) 1723 if (expect_false (!ev_is_active (w)))
1615 return; 1724 return;
1616 1725
1617 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1726 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1618 1727
1627 } 1736 }
1628 1737
1629 ev_stop (EV_A_ (W)w); 1738 ev_stop (EV_A_ (W)w);
1630} 1739}
1631 1740
1632void 1741void noinline
1633ev_periodic_again (EV_P_ ev_periodic *w) 1742ev_periodic_again (EV_P_ ev_periodic *w)
1634{ 1743{
1635 /* TODO: use adjustheap and recalculation */ 1744 /* TODO: use adjustheap and recalculation */
1636 ev_periodic_stop (EV_A_ w); 1745 ev_periodic_stop (EV_A_ w);
1637 ev_periodic_start (EV_A_ w); 1746 ev_periodic_start (EV_A_ w);
1640 1749
1641#ifndef SA_RESTART 1750#ifndef SA_RESTART
1642# define SA_RESTART 0 1751# define SA_RESTART 0
1643#endif 1752#endif
1644 1753
1645void 1754void noinline
1646ev_signal_start (EV_P_ ev_signal *w) 1755ev_signal_start (EV_P_ ev_signal *w)
1647{ 1756{
1648#if EV_MULTIPLICITY 1757#if EV_MULTIPLICITY
1649 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1758 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1650#endif 1759#endif
1669 sigaction (w->signum, &sa, 0); 1778 sigaction (w->signum, &sa, 0);
1670#endif 1779#endif
1671 } 1780 }
1672} 1781}
1673 1782
1674void 1783void noinline
1675ev_signal_stop (EV_P_ ev_signal *w) 1784ev_signal_stop (EV_P_ ev_signal *w)
1676{ 1785{
1677 ev_clear_pending (EV_A_ (W)w); 1786 clear_pending (EV_A_ (W)w);
1678 if (expect_false (!ev_is_active (w))) 1787 if (expect_false (!ev_is_active (w)))
1679 return; 1788 return;
1680 1789
1681 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1790 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1682 ev_stop (EV_A_ (W)w); 1791 ev_stop (EV_A_ (W)w);
1699} 1808}
1700 1809
1701void 1810void
1702ev_child_stop (EV_P_ ev_child *w) 1811ev_child_stop (EV_P_ ev_child *w)
1703{ 1812{
1704 ev_clear_pending (EV_A_ (W)w); 1813 clear_pending (EV_A_ (W)w);
1705 if (expect_false (!ev_is_active (w))) 1814 if (expect_false (!ev_is_active (w)))
1706 return; 1815 return;
1707 1816
1708 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1817 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1709 ev_stop (EV_A_ (W)w); 1818 ev_stop (EV_A_ (W)w);
1945} 2054}
1946 2055
1947void 2056void
1948ev_stat_stop (EV_P_ ev_stat *w) 2057ev_stat_stop (EV_P_ ev_stat *w)
1949{ 2058{
1950 ev_clear_pending (EV_A_ (W)w); 2059 clear_pending (EV_A_ (W)w);
1951 if (expect_false (!ev_is_active (w))) 2060 if (expect_false (!ev_is_active (w)))
1952 return; 2061 return;
1953 2062
1954#if EV_USE_INOTIFY 2063#if EV_USE_INOTIFY
1955 infy_del (EV_A_ w); 2064 infy_del (EV_A_ w);
1958 2067
1959 ev_stop (EV_A_ (W)w); 2068 ev_stop (EV_A_ (W)w);
1960} 2069}
1961#endif 2070#endif
1962 2071
2072#if EV_IDLE_ENABLE
1963void 2073void
1964ev_idle_start (EV_P_ ev_idle *w) 2074ev_idle_start (EV_P_ ev_idle *w)
1965{ 2075{
1966 if (expect_false (ev_is_active (w))) 2076 if (expect_false (ev_is_active (w)))
1967 return; 2077 return;
1968 2078
2079 pri_adjust (EV_A_ (W)w);
2080
2081 {
2082 int active = ++idlecnt [ABSPRI (w)];
2083
2084 ++idleall;
1969 ev_start (EV_A_ (W)w, ++idlecnt); 2085 ev_start (EV_A_ (W)w, active);
2086
1970 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2087 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1971 idles [idlecnt - 1] = w; 2088 idles [ABSPRI (w)][active - 1] = w;
2089 }
1972} 2090}
1973 2091
1974void 2092void
1975ev_idle_stop (EV_P_ ev_idle *w) 2093ev_idle_stop (EV_P_ ev_idle *w)
1976{ 2094{
1977 ev_clear_pending (EV_A_ (W)w); 2095 clear_pending (EV_A_ (W)w);
1978 if (expect_false (!ev_is_active (w))) 2096 if (expect_false (!ev_is_active (w)))
1979 return; 2097 return;
1980 2098
1981 { 2099 {
1982 int active = ((W)w)->active; 2100 int active = ((W)w)->active;
1983 idles [active - 1] = idles [--idlecnt]; 2101
2102 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
1984 ((W)idles [active - 1])->active = active; 2103 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2104
2105 ev_stop (EV_A_ (W)w);
2106 --idleall;
1985 } 2107 }
1986
1987 ev_stop (EV_A_ (W)w);
1988} 2108}
2109#endif
1989 2110
1990void 2111void
1991ev_prepare_start (EV_P_ ev_prepare *w) 2112ev_prepare_start (EV_P_ ev_prepare *w)
1992{ 2113{
1993 if (expect_false (ev_is_active (w))) 2114 if (expect_false (ev_is_active (w)))
1999} 2120}
2000 2121
2001void 2122void
2002ev_prepare_stop (EV_P_ ev_prepare *w) 2123ev_prepare_stop (EV_P_ ev_prepare *w)
2003{ 2124{
2004 ev_clear_pending (EV_A_ (W)w); 2125 clear_pending (EV_A_ (W)w);
2005 if (expect_false (!ev_is_active (w))) 2126 if (expect_false (!ev_is_active (w)))
2006 return; 2127 return;
2007 2128
2008 { 2129 {
2009 int active = ((W)w)->active; 2130 int active = ((W)w)->active;
2026} 2147}
2027 2148
2028void 2149void
2029ev_check_stop (EV_P_ ev_check *w) 2150ev_check_stop (EV_P_ ev_check *w)
2030{ 2151{
2031 ev_clear_pending (EV_A_ (W)w); 2152 clear_pending (EV_A_ (W)w);
2032 if (expect_false (!ev_is_active (w))) 2153 if (expect_false (!ev_is_active (w)))
2033 return; 2154 return;
2034 2155
2035 { 2156 {
2036 int active = ((W)w)->active; 2157 int active = ((W)w)->active;
2078} 2199}
2079 2200
2080void 2201void
2081ev_embed_stop (EV_P_ ev_embed *w) 2202ev_embed_stop (EV_P_ ev_embed *w)
2082{ 2203{
2083 ev_clear_pending (EV_A_ (W)w); 2204 clear_pending (EV_A_ (W)w);
2084 if (expect_false (!ev_is_active (w))) 2205 if (expect_false (!ev_is_active (w)))
2085 return; 2206 return;
2086 2207
2087 ev_io_stop (EV_A_ &w->io); 2208 ev_io_stop (EV_A_ &w->io);
2088 2209
2103} 2224}
2104 2225
2105void 2226void
2106ev_fork_stop (EV_P_ ev_fork *w) 2227ev_fork_stop (EV_P_ ev_fork *w)
2107{ 2228{
2108 ev_clear_pending (EV_A_ (W)w); 2229 clear_pending (EV_A_ (W)w);
2109 if (expect_false (!ev_is_active (w))) 2230 if (expect_false (!ev_is_active (w)))
2110 return; 2231 return;
2111 2232
2112 { 2233 {
2113 int active = ((W)w)->active; 2234 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines