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

Comparing libev/ev.c (file contents):
Revision 1.161 by root, Sat Dec 1 23:43:45 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)
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 }
1384 1446
1385 /* calculate blocking time */ 1447 /* calculate blocking time */
1386 { 1448 {
1387 ev_tstamp block; 1449 ev_tstamp block;
1388 1450
1389 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt)) 1451 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1390 block = 0.; /* do not block at all */ 1452 block = 0.; /* do not block at all */
1391 else 1453 else
1392 { 1454 {
1393 /* update time to cancel out callback processing overhead */ 1455 /* update time to cancel out callback processing overhead */
1394#if EV_USE_MONOTONIC 1456#if EV_USE_MONOTONIC
1418#endif 1480#endif
1419 1481
1420 if (expect_false (block < 0.)) block = 0.; 1482 if (expect_false (block < 0.)) block = 0.;
1421 } 1483 }
1422 1484
1485 ++loop_count;
1423 backend_poll (EV_A_ block); 1486 backend_poll (EV_A_ block);
1424 } 1487 }
1425 1488
1426 /* update ev_rt_now, do magic */ 1489 /* update ev_rt_now, do magic */
1427 time_update (EV_A); 1490 time_update (EV_A);
1430 timers_reify (EV_A); /* relative timers called last */ 1493 timers_reify (EV_A); /* relative timers called last */
1431#if EV_PERIODIC_ENABLE 1494#if EV_PERIODIC_ENABLE
1432 periodics_reify (EV_A); /* absolute timers called first */ 1495 periodics_reify (EV_A); /* absolute timers called first */
1433#endif 1496#endif
1434 1497
1498#if EV_IDLE_ENABLE
1435 /* queue idle watchers unless other events are pending */ 1499 /* queue idle watchers unless other events are pending */
1436 if (idlecnt && !any_pending (EV_A)) 1500 idle_reify (EV_A);
1437 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1501#endif
1438 1502
1439 /* queue check watchers, to be executed first */ 1503 /* queue check watchers, to be executed first */
1440 if (expect_false (checkcnt)) 1504 if (expect_false (checkcnt))
1441 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1505 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1442 1506
1478 head = &(*head)->next; 1542 head = &(*head)->next;
1479 } 1543 }
1480} 1544}
1481 1545
1482void inline_speed 1546void inline_speed
1483ev_clear_pending (EV_P_ W w) 1547clear_pending (EV_P_ W w)
1484{ 1548{
1485 if (w->pending) 1549 if (w->pending)
1486 { 1550 {
1487 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1551 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1488 w->pending = 0; 1552 w->pending = 0;
1489 } 1553 }
1490} 1554}
1491 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
1492void inline_speed 1582void inline_speed
1493ev_start (EV_P_ W w, int active) 1583ev_start (EV_P_ W w, int active)
1494{ 1584{
1495 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1585 pri_adjust (EV_A_ w);
1496 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1497
1498 w->active = active; 1586 w->active = active;
1499 ev_ref (EV_A); 1587 ev_ref (EV_A);
1500} 1588}
1501 1589
1502void inline_size 1590void inline_size
1506 w->active = 0; 1594 w->active = 0;
1507} 1595}
1508 1596
1509/*****************************************************************************/ 1597/*****************************************************************************/
1510 1598
1511void 1599void noinline
1512ev_io_start (EV_P_ ev_io *w) 1600ev_io_start (EV_P_ ev_io *w)
1513{ 1601{
1514 int fd = w->fd; 1602 int fd = w->fd;
1515 1603
1516 if (expect_false (ev_is_active (w))) 1604 if (expect_false (ev_is_active (w)))
1523 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1611 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1524 1612
1525 fd_change (EV_A_ fd); 1613 fd_change (EV_A_ fd);
1526} 1614}
1527 1615
1528void 1616void noinline
1529ev_io_stop (EV_P_ ev_io *w) 1617ev_io_stop (EV_P_ ev_io *w)
1530{ 1618{
1531 ev_clear_pending (EV_A_ (W)w); 1619 clear_pending (EV_A_ (W)w);
1532 if (expect_false (!ev_is_active (w))) 1620 if (expect_false (!ev_is_active (w)))
1533 return; 1621 return;
1534 1622
1535 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));
1536 1624
1538 ev_stop (EV_A_ (W)w); 1626 ev_stop (EV_A_ (W)w);
1539 1627
1540 fd_change (EV_A_ w->fd); 1628 fd_change (EV_A_ w->fd);
1541} 1629}
1542 1630
1543void 1631void noinline
1544ev_timer_start (EV_P_ ev_timer *w) 1632ev_timer_start (EV_P_ ev_timer *w)
1545{ 1633{
1546 if (expect_false (ev_is_active (w))) 1634 if (expect_false (ev_is_active (w)))
1547 return; 1635 return;
1548 1636
1556 upheap ((WT *)timers, timercnt - 1); 1644 upheap ((WT *)timers, timercnt - 1);
1557 1645
1558 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1646 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1559} 1647}
1560 1648
1561void 1649void noinline
1562ev_timer_stop (EV_P_ ev_timer *w) 1650ev_timer_stop (EV_P_ ev_timer *w)
1563{ 1651{
1564 ev_clear_pending (EV_A_ (W)w); 1652 clear_pending (EV_A_ (W)w);
1565 if (expect_false (!ev_is_active (w))) 1653 if (expect_false (!ev_is_active (w)))
1566 return; 1654 return;
1567 1655
1568 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1656 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1569 1657
1580 ((WT)w)->at -= mn_now; 1668 ((WT)w)->at -= mn_now;
1581 1669
1582 ev_stop (EV_A_ (W)w); 1670 ev_stop (EV_A_ (W)w);
1583} 1671}
1584 1672
1585void 1673void noinline
1586ev_timer_again (EV_P_ ev_timer *w) 1674ev_timer_again (EV_P_ ev_timer *w)
1587{ 1675{
1588 if (ev_is_active (w)) 1676 if (ev_is_active (w))
1589 { 1677 {
1590 if (w->repeat) 1678 if (w->repeat)
1601 ev_timer_start (EV_A_ w); 1689 ev_timer_start (EV_A_ w);
1602 } 1690 }
1603} 1691}
1604 1692
1605#if EV_PERIODIC_ENABLE 1693#if EV_PERIODIC_ENABLE
1606void 1694void noinline
1607ev_periodic_start (EV_P_ ev_periodic *w) 1695ev_periodic_start (EV_P_ ev_periodic *w)
1608{ 1696{
1609 if (expect_false (ev_is_active (w))) 1697 if (expect_false (ev_is_active (w)))
1610 return; 1698 return;
1611 1699
1613 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1701 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1614 else if (w->interval) 1702 else if (w->interval)
1615 { 1703 {
1616 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.));
1617 /* 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 */
1618 ((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;
1619 } 1707 }
1708 else
1709 ((WT)w)->at = w->offset;
1620 1710
1621 ev_start (EV_A_ (W)w, ++periodiccnt); 1711 ev_start (EV_A_ (W)w, ++periodiccnt);
1622 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1712 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1623 periodics [periodiccnt - 1] = w; 1713 periodics [periodiccnt - 1] = w;
1624 upheap ((WT *)periodics, periodiccnt - 1); 1714 upheap ((WT *)periodics, periodiccnt - 1);
1625 1715
1626 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1716 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1627} 1717}
1628 1718
1629void 1719void noinline
1630ev_periodic_stop (EV_P_ ev_periodic *w) 1720ev_periodic_stop (EV_P_ ev_periodic *w)
1631{ 1721{
1632 ev_clear_pending (EV_A_ (W)w); 1722 clear_pending (EV_A_ (W)w);
1633 if (expect_false (!ev_is_active (w))) 1723 if (expect_false (!ev_is_active (w)))
1634 return; 1724 return;
1635 1725
1636 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1726 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1637 1727
1646 } 1736 }
1647 1737
1648 ev_stop (EV_A_ (W)w); 1738 ev_stop (EV_A_ (W)w);
1649} 1739}
1650 1740
1651void 1741void noinline
1652ev_periodic_again (EV_P_ ev_periodic *w) 1742ev_periodic_again (EV_P_ ev_periodic *w)
1653{ 1743{
1654 /* TODO: use adjustheap and recalculation */ 1744 /* TODO: use adjustheap and recalculation */
1655 ev_periodic_stop (EV_A_ w); 1745 ev_periodic_stop (EV_A_ w);
1656 ev_periodic_start (EV_A_ w); 1746 ev_periodic_start (EV_A_ w);
1659 1749
1660#ifndef SA_RESTART 1750#ifndef SA_RESTART
1661# define SA_RESTART 0 1751# define SA_RESTART 0
1662#endif 1752#endif
1663 1753
1664void 1754void noinline
1665ev_signal_start (EV_P_ ev_signal *w) 1755ev_signal_start (EV_P_ ev_signal *w)
1666{ 1756{
1667#if EV_MULTIPLICITY 1757#if EV_MULTIPLICITY
1668 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));
1669#endif 1759#endif
1688 sigaction (w->signum, &sa, 0); 1778 sigaction (w->signum, &sa, 0);
1689#endif 1779#endif
1690 } 1780 }
1691} 1781}
1692 1782
1693void 1783void noinline
1694ev_signal_stop (EV_P_ ev_signal *w) 1784ev_signal_stop (EV_P_ ev_signal *w)
1695{ 1785{
1696 ev_clear_pending (EV_A_ (W)w); 1786 clear_pending (EV_A_ (W)w);
1697 if (expect_false (!ev_is_active (w))) 1787 if (expect_false (!ev_is_active (w)))
1698 return; 1788 return;
1699 1789
1700 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1790 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1701 ev_stop (EV_A_ (W)w); 1791 ev_stop (EV_A_ (W)w);
1718} 1808}
1719 1809
1720void 1810void
1721ev_child_stop (EV_P_ ev_child *w) 1811ev_child_stop (EV_P_ ev_child *w)
1722{ 1812{
1723 ev_clear_pending (EV_A_ (W)w); 1813 clear_pending (EV_A_ (W)w);
1724 if (expect_false (!ev_is_active (w))) 1814 if (expect_false (!ev_is_active (w)))
1725 return; 1815 return;
1726 1816
1727 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);
1728 ev_stop (EV_A_ (W)w); 1818 ev_stop (EV_A_ (W)w);
1964} 2054}
1965 2055
1966void 2056void
1967ev_stat_stop (EV_P_ ev_stat *w) 2057ev_stat_stop (EV_P_ ev_stat *w)
1968{ 2058{
1969 ev_clear_pending (EV_A_ (W)w); 2059 clear_pending (EV_A_ (W)w);
1970 if (expect_false (!ev_is_active (w))) 2060 if (expect_false (!ev_is_active (w)))
1971 return; 2061 return;
1972 2062
1973#if EV_USE_INOTIFY 2063#if EV_USE_INOTIFY
1974 infy_del (EV_A_ w); 2064 infy_del (EV_A_ w);
1977 2067
1978 ev_stop (EV_A_ (W)w); 2068 ev_stop (EV_A_ (W)w);
1979} 2069}
1980#endif 2070#endif
1981 2071
2072#if EV_IDLE_ENABLE
1982void 2073void
1983ev_idle_start (EV_P_ ev_idle *w) 2074ev_idle_start (EV_P_ ev_idle *w)
1984{ 2075{
1985 if (expect_false (ev_is_active (w))) 2076 if (expect_false (ev_is_active (w)))
1986 return; 2077 return;
1987 2078
2079 pri_adjust (EV_A_ (W)w);
2080
2081 {
2082 int active = ++idlecnt [ABSPRI (w)];
2083
2084 ++idleall;
1988 ev_start (EV_A_ (W)w, ++idlecnt); 2085 ev_start (EV_A_ (W)w, active);
2086
1989 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2087 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1990 idles [idlecnt - 1] = w; 2088 idles [ABSPRI (w)][active - 1] = w;
2089 }
1991} 2090}
1992 2091
1993void 2092void
1994ev_idle_stop (EV_P_ ev_idle *w) 2093ev_idle_stop (EV_P_ ev_idle *w)
1995{ 2094{
1996 ev_clear_pending (EV_A_ (W)w); 2095 clear_pending (EV_A_ (W)w);
1997 if (expect_false (!ev_is_active (w))) 2096 if (expect_false (!ev_is_active (w)))
1998 return; 2097 return;
1999 2098
2000 { 2099 {
2001 int active = ((W)w)->active; 2100 int active = ((W)w)->active;
2002 idles [active - 1] = idles [--idlecnt]; 2101
2102 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2003 ((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;
2004 } 2107 }
2005
2006 ev_stop (EV_A_ (W)w);
2007} 2108}
2109#endif
2008 2110
2009void 2111void
2010ev_prepare_start (EV_P_ ev_prepare *w) 2112ev_prepare_start (EV_P_ ev_prepare *w)
2011{ 2113{
2012 if (expect_false (ev_is_active (w))) 2114 if (expect_false (ev_is_active (w)))
2018} 2120}
2019 2121
2020void 2122void
2021ev_prepare_stop (EV_P_ ev_prepare *w) 2123ev_prepare_stop (EV_P_ ev_prepare *w)
2022{ 2124{
2023 ev_clear_pending (EV_A_ (W)w); 2125 clear_pending (EV_A_ (W)w);
2024 if (expect_false (!ev_is_active (w))) 2126 if (expect_false (!ev_is_active (w)))
2025 return; 2127 return;
2026 2128
2027 { 2129 {
2028 int active = ((W)w)->active; 2130 int active = ((W)w)->active;
2045} 2147}
2046 2148
2047void 2149void
2048ev_check_stop (EV_P_ ev_check *w) 2150ev_check_stop (EV_P_ ev_check *w)
2049{ 2151{
2050 ev_clear_pending (EV_A_ (W)w); 2152 clear_pending (EV_A_ (W)w);
2051 if (expect_false (!ev_is_active (w))) 2153 if (expect_false (!ev_is_active (w)))
2052 return; 2154 return;
2053 2155
2054 { 2156 {
2055 int active = ((W)w)->active; 2157 int active = ((W)w)->active;
2097} 2199}
2098 2200
2099void 2201void
2100ev_embed_stop (EV_P_ ev_embed *w) 2202ev_embed_stop (EV_P_ ev_embed *w)
2101{ 2203{
2102 ev_clear_pending (EV_A_ (W)w); 2204 clear_pending (EV_A_ (W)w);
2103 if (expect_false (!ev_is_active (w))) 2205 if (expect_false (!ev_is_active (w)))
2104 return; 2206 return;
2105 2207
2106 ev_io_stop (EV_A_ &w->io); 2208 ev_io_stop (EV_A_ &w->io);
2107 2209
2122} 2224}
2123 2225
2124void 2226void
2125ev_fork_stop (EV_P_ ev_fork *w) 2227ev_fork_stop (EV_P_ ev_fork *w)
2126{ 2228{
2127 ev_clear_pending (EV_A_ (W)w); 2229 clear_pending (EV_A_ (W)w);
2128 if (expect_false (!ev_is_active (w))) 2230 if (expect_false (!ev_is_active (w)))
2129 return; 2231 return;
2130 2232
2131 { 2233 {
2132 int active = ((W)w)->active; 2234 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines