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

Comparing libev/ev.c (file contents):
Revision 1.162 by root, Mon Dec 3 13:41:24 2007 UTC vs.
Revision 1.174 by root, Tue Dec 11 03:18:33 2007 UTC

222#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 222#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 */ 223/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
224 224
225#if __GNUC__ >= 3 225#if __GNUC__ >= 3
226# define expect(expr,value) __builtin_expect ((expr),(value)) 226# 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)) 227# 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 228#else
236# define expect(expr,value) (expr) 229# define expect(expr,value) (expr)
237# define inline_speed static
238# define inline_size static
239# define noinline 230# define noinline
231# if __STDC_VERSION__ < 199901L
232# define inline
233# endif
240#endif 234#endif
241 235
242#define expect_false(expr) expect ((expr) != 0, 0) 236#define expect_false(expr) expect ((expr) != 0, 0)
243#define expect_true(expr) expect ((expr) != 0, 1) 237#define expect_true(expr) expect ((expr) != 0, 1)
238#define inline_size static inline
239
240#if EV_MINIMAL
241# define inline_speed static noinline
242#else
243# define inline_speed static inline
244#endif
244 245
245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 246#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
246#define ABSPRI(w) ((w)->priority - EV_MINPRI) 247#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
247 248
248#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 249#define EMPTY /* required for microsofts broken pseudo-c compiler */
249#define EMPTY2(a,b) /* used to suppress some warnings */ 250#define EMPTY2(a,b) /* used to suppress some warnings */
250 251
251typedef ev_watcher *W; 252typedef ev_watcher *W;
252typedef ev_watcher_list *WL; 253typedef ev_watcher_list *WL;
253typedef ev_watcher_time *WT; 254typedef ev_watcher_time *WT;
396{ 397{
397 return ev_rt_now; 398 return ev_rt_now;
398} 399}
399#endif 400#endif
400 401
401#define array_roundsize(type,n) (((n) | 4) & ~3) 402int inline_size
403array_nextsize (int elem, int cur, int cnt)
404{
405 int ncur = cur + 1;
406
407 do
408 ncur <<= 1;
409 while (cnt > ncur);
410
411 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
412 if (elem * ncur > 4096)
413 {
414 ncur *= elem;
415 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
416 ncur = ncur - sizeof (void *) * 4;
417 ncur /= elem;
418 }
419
420 return ncur;
421}
422
423static noinline void *
424array_realloc (int elem, void *base, int *cur, int cnt)
425{
426 *cur = array_nextsize (elem, *cur, cnt);
427 return ev_realloc (base, elem * *cur);
428}
402 429
403#define array_needsize(type,base,cur,cnt,init) \ 430#define array_needsize(type,base,cur,cnt,init) \
404 if (expect_false ((cnt) > cur)) \ 431 if (expect_false ((cnt) > (cur))) \
405 { \ 432 { \
406 int newcnt = cur; \ 433 int ocur_ = (cur); \
407 do \ 434 (base) = (type *)array_realloc \
408 { \ 435 (sizeof (type), (base), &(cur), (cnt)); \
409 newcnt = array_roundsize (type, newcnt << 1); \ 436 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 } 437 }
417 438
439#if 0
418#define array_slim(type,stem) \ 440#define array_slim(type,stem) \
419 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 441 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
420 { \ 442 { \
421 stem ## max = array_roundsize (stem ## cnt >> 1); \ 443 stem ## max = array_roundsize (stem ## cnt >> 1); \
422 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 444 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
423 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 445 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
424 } 446 }
447#endif
425 448
426#define array_free(stem, idx) \ 449#define array_free(stem, idx) \
427 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 450 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
428 451
429/*****************************************************************************/ 452/*****************************************************************************/
430 453
431void noinline 454void noinline
432ev_feed_event (EV_P_ void *w, int revents) 455ev_feed_event (EV_P_ void *w, int revents)
433{ 456{
434 W w_ = (W)w; 457 W w_ = (W)w;
458 int pri = ABSPRI (w_);
435 459
436 if (expect_false (w_->pending)) 460 if (expect_false (w_->pending))
461 pendings [pri][w_->pending - 1].events |= revents;
462 else
437 { 463 {
464 w_->pending = ++pendingcnt [pri];
465 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
466 pendings [pri][w_->pending - 1].w = w_;
438 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 467 pendings [pri][w_->pending - 1].events = revents;
439 return;
440 } 468 }
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} 469}
447 470
448void inline_size 471void inline_size
449queue_events (EV_P_ W *events, int eventcnt, int type) 472queue_events (EV_P_ W *events, int eventcnt, int type)
450{ 473{
485} 508}
486 509
487void 510void
488ev_feed_fd_event (EV_P_ int fd, int revents) 511ev_feed_fd_event (EV_P_ int fd, int revents)
489{ 512{
513 if (fd >= 0 && fd < anfdmax)
490 fd_event (EV_A_ fd, revents); 514 fd_event (EV_A_ fd, revents);
491} 515}
492 516
493void inline_size 517void inline_size
494fd_reify (EV_P) 518fd_reify (EV_P)
495{ 519{
725 for (signum = signalmax; signum--; ) 749 for (signum = signalmax; signum--; )
726 if (signals [signum].gotsig) 750 if (signals [signum].gotsig)
727 ev_feed_signal_event (EV_A_ signum + 1); 751 ev_feed_signal_event (EV_A_ signum + 1);
728} 752}
729 753
730void inline_size 754void inline_speed
731fd_intern (int fd) 755fd_intern (int fd)
732{ 756{
733#ifdef _WIN32 757#ifdef _WIN32
734 int arg = 1; 758 int arg = 1;
735 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 759 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
764 ev_child *w; 788 ev_child *w;
765 789
766 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 790 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
767 if (w->pid == pid || !w->pid) 791 if (w->pid == pid || !w->pid)
768 { 792 {
769 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 793 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
770 w->rpid = pid; 794 w->rpid = pid;
771 w->rstatus = status; 795 w->rstatus = status;
772 ev_feed_event (EV_A_ (W)w, EV_CHILD); 796 ev_feed_event (EV_A_ (W)w, EV_CHILD);
773 } 797 }
774} 798}
775 799
776#ifndef WCONTINUED 800#ifndef WCONTINUED
981#if EV_USE_SELECT 1005#if EV_USE_SELECT
982 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1006 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
983#endif 1007#endif
984 1008
985 for (i = NUMPRI; i--; ) 1009 for (i = NUMPRI; i--; )
1010 {
986 array_free (pending, [i]); 1011 array_free (pending, [i]);
1012#if EV_IDLE_ENABLE
1013 array_free (idle, [i]);
1014#endif
1015 }
987 1016
988 /* have to use the microsoft-never-gets-it-right macro */ 1017 /* have to use the microsoft-never-gets-it-right macro */
989 array_free (fdchange, EMPTY0); 1018 array_free (fdchange, EMPTY);
990 array_free (timer, EMPTY0); 1019 array_free (timer, EMPTY);
991#if EV_PERIODIC_ENABLE 1020#if EV_PERIODIC_ENABLE
992 array_free (periodic, EMPTY0); 1021 array_free (periodic, EMPTY);
993#endif 1022#endif
994 array_free (idle, EMPTY0);
995 array_free (prepare, EMPTY0); 1023 array_free (prepare, EMPTY);
996 array_free (check, EMPTY0); 1024 array_free (check, EMPTY);
997 1025
998 backend = 0; 1026 backend = 0;
999} 1027}
1000 1028
1001void inline_size infy_fork (EV_P); 1029void inline_size infy_fork (EV_P);
1137 postfork = 1; 1165 postfork = 1;
1138} 1166}
1139 1167
1140/*****************************************************************************/ 1168/*****************************************************************************/
1141 1169
1142int inline_size 1170void
1143any_pending (EV_P) 1171ev_invoke (EV_P_ void *w, int revents)
1144{ 1172{
1145 int pri; 1173 EV_CB_INVOKE ((W)w, revents);
1146
1147 for (pri = NUMPRI; pri--; )
1148 if (pendingcnt [pri])
1149 return 1;
1150
1151 return 0;
1152} 1174}
1153 1175
1154void inline_speed 1176void inline_speed
1155call_pending (EV_P) 1177call_pending (EV_P)
1156{ 1178{
1209 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1231 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1210 1232
1211 /* first reschedule or stop timer */ 1233 /* first reschedule or stop timer */
1212 if (w->reschedule_cb) 1234 if (w->reschedule_cb)
1213 { 1235 {
1214 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1236 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001220703125 /* 1/8192 */);
1215 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1237 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1216 downheap ((WT *)periodics, periodiccnt, 0); 1238 downheap ((WT *)periodics, periodiccnt, 0);
1217 } 1239 }
1218 else if (w->interval) 1240 else if (w->interval)
1219 { 1241 {
1220 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1242 ((WT)w)->at = w->offset + floor ((ev_rt_now - w->offset) / w->interval + 1.) * w->interval;
1221 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); 1243 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1222 downheap ((WT *)periodics, periodiccnt, 0); 1244 downheap ((WT *)periodics, periodiccnt, 0);
1223 } 1245 }
1224 else 1246 else
1225 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1247 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1239 ev_periodic *w = periodics [i]; 1261 ev_periodic *w = periodics [i];
1240 1262
1241 if (w->reschedule_cb) 1263 if (w->reschedule_cb)
1242 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1264 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1243 else if (w->interval) 1265 else if (w->interval)
1244 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1266 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1245 } 1267 }
1246 1268
1247 /* now rebuild the heap */ 1269 /* now rebuild the heap */
1248 for (i = periodiccnt >> 1; i--; ) 1270 for (i = periodiccnt >> 1; i--; )
1249 downheap ((WT *)periodics, periodiccnt, i); 1271 downheap ((WT *)periodics, periodiccnt, i);
1272}
1273#endif
1274
1275#if EV_IDLE_ENABLE
1276void inline_size
1277idle_reify (EV_P)
1278{
1279 if (expect_false (idleall))
1280 {
1281 int pri;
1282
1283 for (pri = NUMPRI; pri--; )
1284 {
1285 if (pendingcnt [pri])
1286 break;
1287
1288 if (idlecnt [pri])
1289 {
1290 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1291 break;
1292 }
1293 }
1294 }
1250} 1295}
1251#endif 1296#endif
1252 1297
1253int inline_size 1298int inline_size
1254time_update_monotonic (EV_P) 1299time_update_monotonic (EV_P)
1369 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1414 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1370 call_pending (EV_A); 1415 call_pending (EV_A);
1371 } 1416 }
1372#endif 1417#endif
1373 1418
1374 /* queue check watchers (and execute them) */ 1419 /* queue prepare watchers (and execute them) */
1375 if (expect_false (preparecnt)) 1420 if (expect_false (preparecnt))
1376 { 1421 {
1377 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1422 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1378 call_pending (EV_A); 1423 call_pending (EV_A);
1379 } 1424 }
1390 1435
1391 /* calculate blocking time */ 1436 /* calculate blocking time */
1392 { 1437 {
1393 ev_tstamp block; 1438 ev_tstamp block;
1394 1439
1395 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt)) 1440 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1396 block = 0.; /* do not block at all */ 1441 block = 0.; /* do not block at all */
1397 else 1442 else
1398 { 1443 {
1399 /* update time to cancel out callback processing overhead */ 1444 /* update time to cancel out callback processing overhead */
1400#if EV_USE_MONOTONIC 1445#if EV_USE_MONOTONIC
1437 timers_reify (EV_A); /* relative timers called last */ 1482 timers_reify (EV_A); /* relative timers called last */
1438#if EV_PERIODIC_ENABLE 1483#if EV_PERIODIC_ENABLE
1439 periodics_reify (EV_A); /* absolute timers called first */ 1484 periodics_reify (EV_A); /* absolute timers called first */
1440#endif 1485#endif
1441 1486
1487#if EV_IDLE_ENABLE
1442 /* queue idle watchers unless other events are pending */ 1488 /* queue idle watchers unless other events are pending */
1443 if (idlecnt && !any_pending (EV_A)) 1489 idle_reify (EV_A);
1444 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1490#endif
1445 1491
1446 /* queue check watchers, to be executed first */ 1492 /* queue check watchers, to be executed first */
1447 if (expect_false (checkcnt)) 1493 if (expect_false (checkcnt))
1448 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1494 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1449 1495
1485 head = &(*head)->next; 1531 head = &(*head)->next;
1486 } 1532 }
1487} 1533}
1488 1534
1489void inline_speed 1535void inline_speed
1490ev_clear_pending (EV_P_ W w) 1536clear_pending (EV_P_ W w)
1491{ 1537{
1492 if (w->pending) 1538 if (w->pending)
1493 { 1539 {
1494 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1540 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1495 w->pending = 0; 1541 w->pending = 0;
1496 } 1542 }
1497} 1543}
1498 1544
1545int
1546ev_clear_pending (EV_P_ void *w)
1547{
1548 W w_ = (W)w;
1549 int pending = w_->pending;
1550
1551 if (expect_true (pending))
1552 {
1553 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1554 w_->pending = 0;
1555 p->w = 0;
1556 return p->events;
1557 }
1558 else
1559 return 0;
1560}
1561
1562void inline_size
1563pri_adjust (EV_P_ W w)
1564{
1565 int pri = w->priority;
1566 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1567 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1568 w->priority = pri;
1569}
1570
1499void inline_speed 1571void inline_speed
1500ev_start (EV_P_ W w, int active) 1572ev_start (EV_P_ W w, int active)
1501{ 1573{
1502 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1574 pri_adjust (EV_A_ w);
1503 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1504
1505 w->active = active; 1575 w->active = active;
1506 ev_ref (EV_A); 1576 ev_ref (EV_A);
1507} 1577}
1508 1578
1509void inline_size 1579void inline_size
1513 w->active = 0; 1583 w->active = 0;
1514} 1584}
1515 1585
1516/*****************************************************************************/ 1586/*****************************************************************************/
1517 1587
1518void 1588void noinline
1519ev_io_start (EV_P_ ev_io *w) 1589ev_io_start (EV_P_ ev_io *w)
1520{ 1590{
1521 int fd = w->fd; 1591 int fd = w->fd;
1522 1592
1523 if (expect_false (ev_is_active (w))) 1593 if (expect_false (ev_is_active (w)))
1530 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1600 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1531 1601
1532 fd_change (EV_A_ fd); 1602 fd_change (EV_A_ fd);
1533} 1603}
1534 1604
1535void 1605void noinline
1536ev_io_stop (EV_P_ ev_io *w) 1606ev_io_stop (EV_P_ ev_io *w)
1537{ 1607{
1538 ev_clear_pending (EV_A_ (W)w); 1608 clear_pending (EV_A_ (W)w);
1539 if (expect_false (!ev_is_active (w))) 1609 if (expect_false (!ev_is_active (w)))
1540 return; 1610 return;
1541 1611
1542 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1612 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1543 1613
1545 ev_stop (EV_A_ (W)w); 1615 ev_stop (EV_A_ (W)w);
1546 1616
1547 fd_change (EV_A_ w->fd); 1617 fd_change (EV_A_ w->fd);
1548} 1618}
1549 1619
1550void 1620void noinline
1551ev_timer_start (EV_P_ ev_timer *w) 1621ev_timer_start (EV_P_ ev_timer *w)
1552{ 1622{
1553 if (expect_false (ev_is_active (w))) 1623 if (expect_false (ev_is_active (w)))
1554 return; 1624 return;
1555 1625
1563 upheap ((WT *)timers, timercnt - 1); 1633 upheap ((WT *)timers, timercnt - 1);
1564 1634
1565 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1635 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1566} 1636}
1567 1637
1568void 1638void noinline
1569ev_timer_stop (EV_P_ ev_timer *w) 1639ev_timer_stop (EV_P_ ev_timer *w)
1570{ 1640{
1571 ev_clear_pending (EV_A_ (W)w); 1641 clear_pending (EV_A_ (W)w);
1572 if (expect_false (!ev_is_active (w))) 1642 if (expect_false (!ev_is_active (w)))
1573 return; 1643 return;
1574 1644
1575 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1645 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1576 1646
1587 ((WT)w)->at -= mn_now; 1657 ((WT)w)->at -= mn_now;
1588 1658
1589 ev_stop (EV_A_ (W)w); 1659 ev_stop (EV_A_ (W)w);
1590} 1660}
1591 1661
1592void 1662void noinline
1593ev_timer_again (EV_P_ ev_timer *w) 1663ev_timer_again (EV_P_ ev_timer *w)
1594{ 1664{
1595 if (ev_is_active (w)) 1665 if (ev_is_active (w))
1596 { 1666 {
1597 if (w->repeat) 1667 if (w->repeat)
1608 ev_timer_start (EV_A_ w); 1678 ev_timer_start (EV_A_ w);
1609 } 1679 }
1610} 1680}
1611 1681
1612#if EV_PERIODIC_ENABLE 1682#if EV_PERIODIC_ENABLE
1613void 1683void noinline
1614ev_periodic_start (EV_P_ ev_periodic *w) 1684ev_periodic_start (EV_P_ ev_periodic *w)
1615{ 1685{
1616 if (expect_false (ev_is_active (w))) 1686 if (expect_false (ev_is_active (w)))
1617 return; 1687 return;
1618 1688
1620 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1690 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1621 else if (w->interval) 1691 else if (w->interval)
1622 { 1692 {
1623 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1693 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1624 /* this formula differs from the one in periodic_reify because we do not always round up */ 1694 /* this formula differs from the one in periodic_reify because we do not always round up */
1625 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1695 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1626 } 1696 }
1697 else
1698 ((WT)w)->at = w->offset;
1627 1699
1628 ev_start (EV_A_ (W)w, ++periodiccnt); 1700 ev_start (EV_A_ (W)w, ++periodiccnt);
1629 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1701 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1630 periodics [periodiccnt - 1] = w; 1702 periodics [periodiccnt - 1] = w;
1631 upheap ((WT *)periodics, periodiccnt - 1); 1703 upheap ((WT *)periodics, periodiccnt - 1);
1632 1704
1633 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1705 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1634} 1706}
1635 1707
1636void 1708void noinline
1637ev_periodic_stop (EV_P_ ev_periodic *w) 1709ev_periodic_stop (EV_P_ ev_periodic *w)
1638{ 1710{
1639 ev_clear_pending (EV_A_ (W)w); 1711 clear_pending (EV_A_ (W)w);
1640 if (expect_false (!ev_is_active (w))) 1712 if (expect_false (!ev_is_active (w)))
1641 return; 1713 return;
1642 1714
1643 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1715 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1644 1716
1653 } 1725 }
1654 1726
1655 ev_stop (EV_A_ (W)w); 1727 ev_stop (EV_A_ (W)w);
1656} 1728}
1657 1729
1658void 1730void noinline
1659ev_periodic_again (EV_P_ ev_periodic *w) 1731ev_periodic_again (EV_P_ ev_periodic *w)
1660{ 1732{
1661 /* TODO: use adjustheap and recalculation */ 1733 /* TODO: use adjustheap and recalculation */
1662 ev_periodic_stop (EV_A_ w); 1734 ev_periodic_stop (EV_A_ w);
1663 ev_periodic_start (EV_A_ w); 1735 ev_periodic_start (EV_A_ w);
1666 1738
1667#ifndef SA_RESTART 1739#ifndef SA_RESTART
1668# define SA_RESTART 0 1740# define SA_RESTART 0
1669#endif 1741#endif
1670 1742
1671void 1743void noinline
1672ev_signal_start (EV_P_ ev_signal *w) 1744ev_signal_start (EV_P_ ev_signal *w)
1673{ 1745{
1674#if EV_MULTIPLICITY 1746#if EV_MULTIPLICITY
1675 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1747 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1676#endif 1748#endif
1695 sigaction (w->signum, &sa, 0); 1767 sigaction (w->signum, &sa, 0);
1696#endif 1768#endif
1697 } 1769 }
1698} 1770}
1699 1771
1700void 1772void noinline
1701ev_signal_stop (EV_P_ ev_signal *w) 1773ev_signal_stop (EV_P_ ev_signal *w)
1702{ 1774{
1703 ev_clear_pending (EV_A_ (W)w); 1775 clear_pending (EV_A_ (W)w);
1704 if (expect_false (!ev_is_active (w))) 1776 if (expect_false (!ev_is_active (w)))
1705 return; 1777 return;
1706 1778
1707 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1779 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1708 ev_stop (EV_A_ (W)w); 1780 ev_stop (EV_A_ (W)w);
1725} 1797}
1726 1798
1727void 1799void
1728ev_child_stop (EV_P_ ev_child *w) 1800ev_child_stop (EV_P_ ev_child *w)
1729{ 1801{
1730 ev_clear_pending (EV_A_ (W)w); 1802 clear_pending (EV_A_ (W)w);
1731 if (expect_false (!ev_is_active (w))) 1803 if (expect_false (!ev_is_active (w)))
1732 return; 1804 return;
1733 1805
1734 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1806 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1735 ev_stop (EV_A_ (W)w); 1807 ev_stop (EV_A_ (W)w);
1971} 2043}
1972 2044
1973void 2045void
1974ev_stat_stop (EV_P_ ev_stat *w) 2046ev_stat_stop (EV_P_ ev_stat *w)
1975{ 2047{
1976 ev_clear_pending (EV_A_ (W)w); 2048 clear_pending (EV_A_ (W)w);
1977 if (expect_false (!ev_is_active (w))) 2049 if (expect_false (!ev_is_active (w)))
1978 return; 2050 return;
1979 2051
1980#if EV_USE_INOTIFY 2052#if EV_USE_INOTIFY
1981 infy_del (EV_A_ w); 2053 infy_del (EV_A_ w);
1984 2056
1985 ev_stop (EV_A_ (W)w); 2057 ev_stop (EV_A_ (W)w);
1986} 2058}
1987#endif 2059#endif
1988 2060
2061#if EV_IDLE_ENABLE
1989void 2062void
1990ev_idle_start (EV_P_ ev_idle *w) 2063ev_idle_start (EV_P_ ev_idle *w)
1991{ 2064{
1992 if (expect_false (ev_is_active (w))) 2065 if (expect_false (ev_is_active (w)))
1993 return; 2066 return;
1994 2067
2068 pri_adjust (EV_A_ (W)w);
2069
2070 {
2071 int active = ++idlecnt [ABSPRI (w)];
2072
2073 ++idleall;
1995 ev_start (EV_A_ (W)w, ++idlecnt); 2074 ev_start (EV_A_ (W)w, active);
2075
1996 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2076 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1997 idles [idlecnt - 1] = w; 2077 idles [ABSPRI (w)][active - 1] = w;
2078 }
1998} 2079}
1999 2080
2000void 2081void
2001ev_idle_stop (EV_P_ ev_idle *w) 2082ev_idle_stop (EV_P_ ev_idle *w)
2002{ 2083{
2003 ev_clear_pending (EV_A_ (W)w); 2084 clear_pending (EV_A_ (W)w);
2004 if (expect_false (!ev_is_active (w))) 2085 if (expect_false (!ev_is_active (w)))
2005 return; 2086 return;
2006 2087
2007 { 2088 {
2008 int active = ((W)w)->active; 2089 int active = ((W)w)->active;
2009 idles [active - 1] = idles [--idlecnt]; 2090
2091 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2010 ((W)idles [active - 1])->active = active; 2092 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2093
2094 ev_stop (EV_A_ (W)w);
2095 --idleall;
2011 } 2096 }
2012
2013 ev_stop (EV_A_ (W)w);
2014} 2097}
2098#endif
2015 2099
2016void 2100void
2017ev_prepare_start (EV_P_ ev_prepare *w) 2101ev_prepare_start (EV_P_ ev_prepare *w)
2018{ 2102{
2019 if (expect_false (ev_is_active (w))) 2103 if (expect_false (ev_is_active (w)))
2025} 2109}
2026 2110
2027void 2111void
2028ev_prepare_stop (EV_P_ ev_prepare *w) 2112ev_prepare_stop (EV_P_ ev_prepare *w)
2029{ 2113{
2030 ev_clear_pending (EV_A_ (W)w); 2114 clear_pending (EV_A_ (W)w);
2031 if (expect_false (!ev_is_active (w))) 2115 if (expect_false (!ev_is_active (w)))
2032 return; 2116 return;
2033 2117
2034 { 2118 {
2035 int active = ((W)w)->active; 2119 int active = ((W)w)->active;
2052} 2136}
2053 2137
2054void 2138void
2055ev_check_stop (EV_P_ ev_check *w) 2139ev_check_stop (EV_P_ ev_check *w)
2056{ 2140{
2057 ev_clear_pending (EV_A_ (W)w); 2141 clear_pending (EV_A_ (W)w);
2058 if (expect_false (!ev_is_active (w))) 2142 if (expect_false (!ev_is_active (w)))
2059 return; 2143 return;
2060 2144
2061 { 2145 {
2062 int active = ((W)w)->active; 2146 int active = ((W)w)->active;
2104} 2188}
2105 2189
2106void 2190void
2107ev_embed_stop (EV_P_ ev_embed *w) 2191ev_embed_stop (EV_P_ ev_embed *w)
2108{ 2192{
2109 ev_clear_pending (EV_A_ (W)w); 2193 clear_pending (EV_A_ (W)w);
2110 if (expect_false (!ev_is_active (w))) 2194 if (expect_false (!ev_is_active (w)))
2111 return; 2195 return;
2112 2196
2113 ev_io_stop (EV_A_ &w->io); 2197 ev_io_stop (EV_A_ &w->io);
2114 2198
2129} 2213}
2130 2214
2131void 2215void
2132ev_fork_stop (EV_P_ ev_fork *w) 2216ev_fork_stop (EV_P_ ev_fork *w)
2133{ 2217{
2134 ev_clear_pending (EV_A_ (W)w); 2218 clear_pending (EV_A_ (W)w);
2135 if (expect_false (!ev_is_active (w))) 2219 if (expect_false (!ev_is_active (w)))
2136 return; 2220 return;
2137 2221
2138 { 2222 {
2139 int active = ((W)w)->active; 2223 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines