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

Comparing libev/ev.c (file contents):
Revision 1.158 by root, Thu Nov 29 17:28:13 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 {
975#if EV_USE_SELECT 1016#if EV_USE_SELECT
976 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1017 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
977#endif 1018#endif
978 1019
979 for (i = NUMPRI; i--; ) 1020 for (i = NUMPRI; i--; )
1021 {
980 array_free (pending, [i]); 1022 array_free (pending, [i]);
1023#if EV_IDLE_ENABLE
1024 array_free (idle, [i]);
1025#endif
1026 }
981 1027
982 /* have to use the microsoft-never-gets-it-right macro */ 1028 /* have to use the microsoft-never-gets-it-right macro */
983 array_free (fdchange, EMPTY0); 1029 array_free (fdchange, EMPTY);
984 array_free (timer, EMPTY0); 1030 array_free (timer, EMPTY);
985#if EV_PERIODIC_ENABLE 1031#if EV_PERIODIC_ENABLE
986 array_free (periodic, EMPTY0); 1032 array_free (periodic, EMPTY);
987#endif 1033#endif
988 array_free (idle, EMPTY0);
989 array_free (prepare, EMPTY0); 1034 array_free (prepare, EMPTY);
990 array_free (check, EMPTY0); 1035 array_free (check, EMPTY);
991 1036
992 backend = 0; 1037 backend = 0;
993} 1038}
994 1039
995void inline_size infy_fork (EV_P); 1040void inline_size infy_fork (EV_P);
1131 postfork = 1; 1176 postfork = 1;
1132} 1177}
1133 1178
1134/*****************************************************************************/ 1179/*****************************************************************************/
1135 1180
1136int inline_size 1181void
1137any_pending (EV_P) 1182ev_invoke (EV_P_ void *w, int revents)
1138{ 1183{
1139 int pri; 1184 EV_CB_INVOKE ((W)w, revents);
1140
1141 for (pri = NUMPRI; pri--; )
1142 if (pendingcnt [pri])
1143 return 1;
1144
1145 return 0;
1146} 1185}
1147 1186
1148void inline_speed 1187void inline_speed
1149call_pending (EV_P) 1188call_pending (EV_P)
1150{ 1189{
1203 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1242 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1204 1243
1205 /* first reschedule or stop timer */ 1244 /* first reschedule or stop timer */
1206 if (w->reschedule_cb) 1245 if (w->reschedule_cb)
1207 { 1246 {
1208 ((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);
1209 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));
1210 downheap ((WT *)periodics, periodiccnt, 0); 1249 downheap ((WT *)periodics, periodiccnt, 0);
1211 } 1250 }
1212 else if (w->interval) 1251 else if (w->interval)
1213 { 1252 {
1214 ((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;
1215 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));
1216 downheap ((WT *)periodics, periodiccnt, 0); 1255 downheap ((WT *)periodics, periodiccnt, 0);
1217 } 1256 }
1218 else 1257 else
1219 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1258 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1233 ev_periodic *w = periodics [i]; 1272 ev_periodic *w = periodics [i];
1234 1273
1235 if (w->reschedule_cb) 1274 if (w->reschedule_cb)
1236 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1275 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1237 else if (w->interval) 1276 else if (w->interval)
1238 ((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;
1239 } 1278 }
1240 1279
1241 /* now rebuild the heap */ 1280 /* now rebuild the heap */
1242 for (i = periodiccnt >> 1; i--; ) 1281 for (i = periodiccnt >> 1; i--; )
1243 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 }
1244} 1306}
1245#endif 1307#endif
1246 1308
1247int inline_size 1309int inline_size
1248time_update_monotonic (EV_P) 1310time_update_monotonic (EV_P)
1342 ? EVUNLOOP_ONE 1404 ? EVUNLOOP_ONE
1343 : EVUNLOOP_CANCEL; 1405 : EVUNLOOP_CANCEL;
1344 1406
1345 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1407 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1346 1408
1347 while (activecnt) 1409 do
1348 { 1410 {
1349#ifndef _WIN32 1411#ifndef _WIN32
1350 if (expect_false (curpid)) /* penalise the forking check even more */ 1412 if (expect_false (curpid)) /* penalise the forking check even more */
1351 if (expect_false (getpid () != curpid)) 1413 if (expect_false (getpid () != curpid))
1352 { 1414 {
1363 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1425 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1364 call_pending (EV_A); 1426 call_pending (EV_A);
1365 } 1427 }
1366#endif 1428#endif
1367 1429
1368 /* queue check watchers (and execute them) */ 1430 /* queue prepare watchers (and execute them) */
1369 if (expect_false (preparecnt)) 1431 if (expect_false (preparecnt))
1370 { 1432 {
1371 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1433 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1372 call_pending (EV_A); 1434 call_pending (EV_A);
1373 } 1435 }
1374 1436
1437 if (expect_false (!activecnt))
1438 break;
1439
1375 /* we might have forked, so reify kernel state if necessary */ 1440 /* we might have forked, so reify kernel state if necessary */
1376 if (expect_false (postfork)) 1441 if (expect_false (postfork))
1377 loop_fork (EV_A); 1442 loop_fork (EV_A);
1378 1443
1379 /* update fd-related kernel structures */ 1444 /* update fd-related kernel structures */
1381 1446
1382 /* calculate blocking time */ 1447 /* calculate blocking time */
1383 { 1448 {
1384 ev_tstamp block; 1449 ev_tstamp block;
1385 1450
1386 if (flags & EVLOOP_NONBLOCK || idlecnt) 1451 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1387 block = 0.; /* do not block at all */ 1452 block = 0.; /* do not block at all */
1388 else 1453 else
1389 { 1454 {
1390 /* update time to cancel out callback processing overhead */ 1455 /* update time to cancel out callback processing overhead */
1391#if EV_USE_MONOTONIC 1456#if EV_USE_MONOTONIC
1415#endif 1480#endif
1416 1481
1417 if (expect_false (block < 0.)) block = 0.; 1482 if (expect_false (block < 0.)) block = 0.;
1418 } 1483 }
1419 1484
1485 ++loop_count;
1420 backend_poll (EV_A_ block); 1486 backend_poll (EV_A_ block);
1421 } 1487 }
1422 1488
1423 /* update ev_rt_now, do magic */ 1489 /* update ev_rt_now, do magic */
1424 time_update (EV_A); 1490 time_update (EV_A);
1427 timers_reify (EV_A); /* relative timers called last */ 1493 timers_reify (EV_A); /* relative timers called last */
1428#if EV_PERIODIC_ENABLE 1494#if EV_PERIODIC_ENABLE
1429 periodics_reify (EV_A); /* absolute timers called first */ 1495 periodics_reify (EV_A); /* absolute timers called first */
1430#endif 1496#endif
1431 1497
1498#if EV_IDLE_ENABLE
1432 /* queue idle watchers unless other events are pending */ 1499 /* queue idle watchers unless other events are pending */
1433 if (idlecnt && !any_pending (EV_A)) 1500 idle_reify (EV_A);
1434 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1501#endif
1435 1502
1436 /* queue check watchers, to be executed first */ 1503 /* queue check watchers, to be executed first */
1437 if (expect_false (checkcnt)) 1504 if (expect_false (checkcnt))
1438 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1505 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1439 1506
1440 call_pending (EV_A); 1507 call_pending (EV_A);
1441 1508
1442 if (expect_false (loop_done))
1443 break;
1444 } 1509 }
1510 while (expect_true (activecnt && !loop_done));
1445 1511
1446 if (loop_done == EVUNLOOP_ONE) 1512 if (loop_done == EVUNLOOP_ONE)
1447 loop_done = EVUNLOOP_CANCEL; 1513 loop_done = EVUNLOOP_CANCEL;
1448} 1514}
1449 1515
1476 head = &(*head)->next; 1542 head = &(*head)->next;
1477 } 1543 }
1478} 1544}
1479 1545
1480void inline_speed 1546void inline_speed
1481ev_clear_pending (EV_P_ W w) 1547clear_pending (EV_P_ W w)
1482{ 1548{
1483 if (w->pending) 1549 if (w->pending)
1484 { 1550 {
1485 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1551 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1486 w->pending = 0; 1552 w->pending = 0;
1487 } 1553 }
1488} 1554}
1489 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
1490void inline_speed 1582void inline_speed
1491ev_start (EV_P_ W w, int active) 1583ev_start (EV_P_ W w, int active)
1492{ 1584{
1493 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1585 pri_adjust (EV_A_ w);
1494 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1495
1496 w->active = active; 1586 w->active = active;
1497 ev_ref (EV_A); 1587 ev_ref (EV_A);
1498} 1588}
1499 1589
1500void inline_size 1590void inline_size
1504 w->active = 0; 1594 w->active = 0;
1505} 1595}
1506 1596
1507/*****************************************************************************/ 1597/*****************************************************************************/
1508 1598
1509void 1599void noinline
1510ev_io_start (EV_P_ ev_io *w) 1600ev_io_start (EV_P_ ev_io *w)
1511{ 1601{
1512 int fd = w->fd; 1602 int fd = w->fd;
1513 1603
1514 if (expect_false (ev_is_active (w))) 1604 if (expect_false (ev_is_active (w)))
1521 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1611 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1522 1612
1523 fd_change (EV_A_ fd); 1613 fd_change (EV_A_ fd);
1524} 1614}
1525 1615
1526void 1616void noinline
1527ev_io_stop (EV_P_ ev_io *w) 1617ev_io_stop (EV_P_ ev_io *w)
1528{ 1618{
1529 ev_clear_pending (EV_A_ (W)w); 1619 clear_pending (EV_A_ (W)w);
1530 if (expect_false (!ev_is_active (w))) 1620 if (expect_false (!ev_is_active (w)))
1531 return; 1621 return;
1532 1622
1533 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));
1534 1624
1536 ev_stop (EV_A_ (W)w); 1626 ev_stop (EV_A_ (W)w);
1537 1627
1538 fd_change (EV_A_ w->fd); 1628 fd_change (EV_A_ w->fd);
1539} 1629}
1540 1630
1541void 1631void noinline
1542ev_timer_start (EV_P_ ev_timer *w) 1632ev_timer_start (EV_P_ ev_timer *w)
1543{ 1633{
1544 if (expect_false (ev_is_active (w))) 1634 if (expect_false (ev_is_active (w)))
1545 return; 1635 return;
1546 1636
1554 upheap ((WT *)timers, timercnt - 1); 1644 upheap ((WT *)timers, timercnt - 1);
1555 1645
1556 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1646 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1557} 1647}
1558 1648
1559void 1649void noinline
1560ev_timer_stop (EV_P_ ev_timer *w) 1650ev_timer_stop (EV_P_ ev_timer *w)
1561{ 1651{
1562 ev_clear_pending (EV_A_ (W)w); 1652 clear_pending (EV_A_ (W)w);
1563 if (expect_false (!ev_is_active (w))) 1653 if (expect_false (!ev_is_active (w)))
1564 return; 1654 return;
1565 1655
1566 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1656 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1567 1657
1578 ((WT)w)->at -= mn_now; 1668 ((WT)w)->at -= mn_now;
1579 1669
1580 ev_stop (EV_A_ (W)w); 1670 ev_stop (EV_A_ (W)w);
1581} 1671}
1582 1672
1583void 1673void noinline
1584ev_timer_again (EV_P_ ev_timer *w) 1674ev_timer_again (EV_P_ ev_timer *w)
1585{ 1675{
1586 if (ev_is_active (w)) 1676 if (ev_is_active (w))
1587 { 1677 {
1588 if (w->repeat) 1678 if (w->repeat)
1599 ev_timer_start (EV_A_ w); 1689 ev_timer_start (EV_A_ w);
1600 } 1690 }
1601} 1691}
1602 1692
1603#if EV_PERIODIC_ENABLE 1693#if EV_PERIODIC_ENABLE
1604void 1694void noinline
1605ev_periodic_start (EV_P_ ev_periodic *w) 1695ev_periodic_start (EV_P_ ev_periodic *w)
1606{ 1696{
1607 if (expect_false (ev_is_active (w))) 1697 if (expect_false (ev_is_active (w)))
1608 return; 1698 return;
1609 1699
1611 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1701 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1612 else if (w->interval) 1702 else if (w->interval)
1613 { 1703 {
1614 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.));
1615 /* 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 */
1616 ((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;
1617 } 1707 }
1708 else
1709 ((WT)w)->at = w->offset;
1618 1710
1619 ev_start (EV_A_ (W)w, ++periodiccnt); 1711 ev_start (EV_A_ (W)w, ++periodiccnt);
1620 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1712 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1621 periodics [periodiccnt - 1] = w; 1713 periodics [periodiccnt - 1] = w;
1622 upheap ((WT *)periodics, periodiccnt - 1); 1714 upheap ((WT *)periodics, periodiccnt - 1);
1623 1715
1624 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1716 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1625} 1717}
1626 1718
1627void 1719void noinline
1628ev_periodic_stop (EV_P_ ev_periodic *w) 1720ev_periodic_stop (EV_P_ ev_periodic *w)
1629{ 1721{
1630 ev_clear_pending (EV_A_ (W)w); 1722 clear_pending (EV_A_ (W)w);
1631 if (expect_false (!ev_is_active (w))) 1723 if (expect_false (!ev_is_active (w)))
1632 return; 1724 return;
1633 1725
1634 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1726 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1635 1727
1644 } 1736 }
1645 1737
1646 ev_stop (EV_A_ (W)w); 1738 ev_stop (EV_A_ (W)w);
1647} 1739}
1648 1740
1649void 1741void noinline
1650ev_periodic_again (EV_P_ ev_periodic *w) 1742ev_periodic_again (EV_P_ ev_periodic *w)
1651{ 1743{
1652 /* TODO: use adjustheap and recalculation */ 1744 /* TODO: use adjustheap and recalculation */
1653 ev_periodic_stop (EV_A_ w); 1745 ev_periodic_stop (EV_A_ w);
1654 ev_periodic_start (EV_A_ w); 1746 ev_periodic_start (EV_A_ w);
1657 1749
1658#ifndef SA_RESTART 1750#ifndef SA_RESTART
1659# define SA_RESTART 0 1751# define SA_RESTART 0
1660#endif 1752#endif
1661 1753
1662void 1754void noinline
1663ev_signal_start (EV_P_ ev_signal *w) 1755ev_signal_start (EV_P_ ev_signal *w)
1664{ 1756{
1665#if EV_MULTIPLICITY 1757#if EV_MULTIPLICITY
1666 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));
1667#endif 1759#endif
1686 sigaction (w->signum, &sa, 0); 1778 sigaction (w->signum, &sa, 0);
1687#endif 1779#endif
1688 } 1780 }
1689} 1781}
1690 1782
1691void 1783void noinline
1692ev_signal_stop (EV_P_ ev_signal *w) 1784ev_signal_stop (EV_P_ ev_signal *w)
1693{ 1785{
1694 ev_clear_pending (EV_A_ (W)w); 1786 clear_pending (EV_A_ (W)w);
1695 if (expect_false (!ev_is_active (w))) 1787 if (expect_false (!ev_is_active (w)))
1696 return; 1788 return;
1697 1789
1698 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1790 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1699 ev_stop (EV_A_ (W)w); 1791 ev_stop (EV_A_ (W)w);
1716} 1808}
1717 1809
1718void 1810void
1719ev_child_stop (EV_P_ ev_child *w) 1811ev_child_stop (EV_P_ ev_child *w)
1720{ 1812{
1721 ev_clear_pending (EV_A_ (W)w); 1813 clear_pending (EV_A_ (W)w);
1722 if (expect_false (!ev_is_active (w))) 1814 if (expect_false (!ev_is_active (w)))
1723 return; 1815 return;
1724 1816
1725 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);
1726 ev_stop (EV_A_ (W)w); 1818 ev_stop (EV_A_ (W)w);
1962} 2054}
1963 2055
1964void 2056void
1965ev_stat_stop (EV_P_ ev_stat *w) 2057ev_stat_stop (EV_P_ ev_stat *w)
1966{ 2058{
1967 ev_clear_pending (EV_A_ (W)w); 2059 clear_pending (EV_A_ (W)w);
1968 if (expect_false (!ev_is_active (w))) 2060 if (expect_false (!ev_is_active (w)))
1969 return; 2061 return;
1970 2062
1971#if EV_USE_INOTIFY 2063#if EV_USE_INOTIFY
1972 infy_del (EV_A_ w); 2064 infy_del (EV_A_ w);
1975 2067
1976 ev_stop (EV_A_ (W)w); 2068 ev_stop (EV_A_ (W)w);
1977} 2069}
1978#endif 2070#endif
1979 2071
2072#if EV_IDLE_ENABLE
1980void 2073void
1981ev_idle_start (EV_P_ ev_idle *w) 2074ev_idle_start (EV_P_ ev_idle *w)
1982{ 2075{
1983 if (expect_false (ev_is_active (w))) 2076 if (expect_false (ev_is_active (w)))
1984 return; 2077 return;
1985 2078
2079 pri_adjust (EV_A_ (W)w);
2080
2081 {
2082 int active = ++idlecnt [ABSPRI (w)];
2083
2084 ++idleall;
1986 ev_start (EV_A_ (W)w, ++idlecnt); 2085 ev_start (EV_A_ (W)w, active);
2086
1987 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2087 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1988 idles [idlecnt - 1] = w; 2088 idles [ABSPRI (w)][active - 1] = w;
2089 }
1989} 2090}
1990 2091
1991void 2092void
1992ev_idle_stop (EV_P_ ev_idle *w) 2093ev_idle_stop (EV_P_ ev_idle *w)
1993{ 2094{
1994 ev_clear_pending (EV_A_ (W)w); 2095 clear_pending (EV_A_ (W)w);
1995 if (expect_false (!ev_is_active (w))) 2096 if (expect_false (!ev_is_active (w)))
1996 return; 2097 return;
1997 2098
1998 { 2099 {
1999 int active = ((W)w)->active; 2100 int active = ((W)w)->active;
2000 idles [active - 1] = idles [--idlecnt]; 2101
2102 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2001 ((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;
2002 } 2107 }
2003
2004 ev_stop (EV_A_ (W)w);
2005} 2108}
2109#endif
2006 2110
2007void 2111void
2008ev_prepare_start (EV_P_ ev_prepare *w) 2112ev_prepare_start (EV_P_ ev_prepare *w)
2009{ 2113{
2010 if (expect_false (ev_is_active (w))) 2114 if (expect_false (ev_is_active (w)))
2016} 2120}
2017 2121
2018void 2122void
2019ev_prepare_stop (EV_P_ ev_prepare *w) 2123ev_prepare_stop (EV_P_ ev_prepare *w)
2020{ 2124{
2021 ev_clear_pending (EV_A_ (W)w); 2125 clear_pending (EV_A_ (W)w);
2022 if (expect_false (!ev_is_active (w))) 2126 if (expect_false (!ev_is_active (w)))
2023 return; 2127 return;
2024 2128
2025 { 2129 {
2026 int active = ((W)w)->active; 2130 int active = ((W)w)->active;
2043} 2147}
2044 2148
2045void 2149void
2046ev_check_stop (EV_P_ ev_check *w) 2150ev_check_stop (EV_P_ ev_check *w)
2047{ 2151{
2048 ev_clear_pending (EV_A_ (W)w); 2152 clear_pending (EV_A_ (W)w);
2049 if (expect_false (!ev_is_active (w))) 2153 if (expect_false (!ev_is_active (w)))
2050 return; 2154 return;
2051 2155
2052 { 2156 {
2053 int active = ((W)w)->active; 2157 int active = ((W)w)->active;
2095} 2199}
2096 2200
2097void 2201void
2098ev_embed_stop (EV_P_ ev_embed *w) 2202ev_embed_stop (EV_P_ ev_embed *w)
2099{ 2203{
2100 ev_clear_pending (EV_A_ (W)w); 2204 clear_pending (EV_A_ (W)w);
2101 if (expect_false (!ev_is_active (w))) 2205 if (expect_false (!ev_is_active (w)))
2102 return; 2206 return;
2103 2207
2104 ev_io_stop (EV_A_ &w->io); 2208 ev_io_stop (EV_A_ &w->io);
2105 2209
2120} 2224}
2121 2225
2122void 2226void
2123ev_fork_stop (EV_P_ ev_fork *w) 2227ev_fork_stop (EV_P_ ev_fork *w)
2124{ 2228{
2125 ev_clear_pending (EV_A_ (W)w); 2229 clear_pending (EV_A_ (W)w);
2126 if (expect_false (!ev_is_active (w))) 2230 if (expect_false (!ev_is_active (w)))
2127 return; 2231 return;
2128 2232
2129 { 2233 {
2130 int active = ((W)w)->active; 2234 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines