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

Comparing libev/ev.c (file contents):
Revision 1.156 by root, Wed Nov 28 17:50: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{
589static void noinline 624static void noinline
590fd_rearm_all (EV_P) 625fd_rearm_all (EV_P)
591{ 626{
592 int fd; 627 int fd;
593 628
594 /* this should be highly optimised to not do anything but set a flag */
595 for (fd = 0; fd < anfdmax; ++fd) 629 for (fd = 0; fd < anfdmax; ++fd)
596 if (anfds [fd].events) 630 if (anfds [fd].events)
597 { 631 {
598 anfds [fd].events = 0; 632 anfds [fd].events = 0;
599 fd_change (EV_A_ fd); 633 fd_change (EV_A_ fd);
726 for (signum = signalmax; signum--; ) 760 for (signum = signalmax; signum--; )
727 if (signals [signum].gotsig) 761 if (signals [signum].gotsig)
728 ev_feed_signal_event (EV_A_ signum + 1); 762 ev_feed_signal_event (EV_A_ signum + 1);
729} 763}
730 764
731void inline_size 765void inline_speed
732fd_intern (int fd) 766fd_intern (int fd)
733{ 767{
734#ifdef _WIN32 768#ifdef _WIN32
735 int arg = 1; 769 int arg = 1;
736 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 770 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
765 ev_child *w; 799 ev_child *w;
766 800
767 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)
768 if (w->pid == pid || !w->pid) 802 if (w->pid == pid || !w->pid)
769 { 803 {
770 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 804 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
771 w->rpid = pid; 805 w->rpid = pid;
772 w->rstatus = status; 806 w->rstatus = status;
773 ev_feed_event (EV_A_ (W)w, EV_CHILD); 807 ev_feed_event (EV_A_ (W)w, EV_CHILD);
774 } 808 }
775} 809}
776 810
777#ifndef WCONTINUED 811#ifndef WCONTINUED
887ev_backend (EV_P) 921ev_backend (EV_P)
888{ 922{
889 return backend; 923 return backend;
890} 924}
891 925
926unsigned int
927ev_loop_count (EV_P)
928{
929 return loop_count;
930}
931
892static void noinline 932static void noinline
893loop_init (EV_P_ unsigned int flags) 933loop_init (EV_P_ unsigned int flags)
894{ 934{
895 if (!backend) 935 if (!backend)
896 { 936 {
905 ev_rt_now = ev_time (); 945 ev_rt_now = ev_time ();
906 mn_now = get_clock (); 946 mn_now = get_clock ();
907 now_floor = mn_now; 947 now_floor = mn_now;
908 rtmn_diff = ev_rt_now - mn_now; 948 rtmn_diff = ev_rt_now - mn_now;
909 949
950 /* pid check not overridable via env */
951#ifndef _WIN32
952 if (flags & EVFLAG_FORKCHECK)
953 curpid = getpid ();
954#endif
955
910 if (!(flags & EVFLAG_NOENV) 956 if (!(flags & EVFLAG_NOENV)
911 && !enable_secure () 957 && !enable_secure ()
912 && getenv ("LIBEV_FLAGS")) 958 && getenv ("LIBEV_FLAGS"))
913 flags = atoi (getenv ("LIBEV_FLAGS")); 959 flags = atoi (getenv ("LIBEV_FLAGS"));
914 960
970#if EV_USE_SELECT 1016#if EV_USE_SELECT
971 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1017 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
972#endif 1018#endif
973 1019
974 for (i = NUMPRI; i--; ) 1020 for (i = NUMPRI; i--; )
1021 {
975 array_free (pending, [i]); 1022 array_free (pending, [i]);
1023#if EV_IDLE_ENABLE
1024 array_free (idle, [i]);
1025#endif
1026 }
976 1027
977 /* have to use the microsoft-never-gets-it-right macro */ 1028 /* have to use the microsoft-never-gets-it-right macro */
978 array_free (fdchange, EMPTY0); 1029 array_free (fdchange, EMPTY);
979 array_free (timer, EMPTY0); 1030 array_free (timer, EMPTY);
980#if EV_PERIODIC_ENABLE 1031#if EV_PERIODIC_ENABLE
981 array_free (periodic, EMPTY0); 1032 array_free (periodic, EMPTY);
982#endif 1033#endif
983 array_free (idle, EMPTY0);
984 array_free (prepare, EMPTY0); 1034 array_free (prepare, EMPTY);
985 array_free (check, EMPTY0); 1035 array_free (check, EMPTY);
986 1036
987 backend = 0; 1037 backend = 0;
988} 1038}
989 1039
990void inline_size infy_fork (EV_P); 1040void inline_size infy_fork (EV_P);
1126 postfork = 1; 1176 postfork = 1;
1127} 1177}
1128 1178
1129/*****************************************************************************/ 1179/*****************************************************************************/
1130 1180
1131int inline_size 1181void
1132any_pending (EV_P) 1182ev_invoke (EV_P_ void *w, int revents)
1133{ 1183{
1134 int pri; 1184 EV_CB_INVOKE ((W)w, revents);
1135
1136 for (pri = NUMPRI; pri--; )
1137 if (pendingcnt [pri])
1138 return 1;
1139
1140 return 0;
1141} 1185}
1142 1186
1143void inline_speed 1187void inline_speed
1144call_pending (EV_P) 1188call_pending (EV_P)
1145{ 1189{
1198 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1242 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1199 1243
1200 /* first reschedule or stop timer */ 1244 /* first reschedule or stop timer */
1201 if (w->reschedule_cb) 1245 if (w->reschedule_cb)
1202 { 1246 {
1203 ((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);
1204 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));
1205 downheap ((WT *)periodics, periodiccnt, 0); 1249 downheap ((WT *)periodics, periodiccnt, 0);
1206 } 1250 }
1207 else if (w->interval) 1251 else if (w->interval)
1208 { 1252 {
1209 ((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;
1210 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));
1211 downheap ((WT *)periodics, periodiccnt, 0); 1255 downheap ((WT *)periodics, periodiccnt, 0);
1212 } 1256 }
1213 else 1257 else
1214 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1258 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1228 ev_periodic *w = periodics [i]; 1272 ev_periodic *w = periodics [i];
1229 1273
1230 if (w->reschedule_cb) 1274 if (w->reschedule_cb)
1231 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1275 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1232 else if (w->interval) 1276 else if (w->interval)
1233 ((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;
1234 } 1278 }
1235 1279
1236 /* now rebuild the heap */ 1280 /* now rebuild the heap */
1237 for (i = periodiccnt >> 1; i--; ) 1281 for (i = periodiccnt >> 1; i--; )
1238 downheap ((WT *)periodics, periodiccnt, i); 1282 downheap ((WT *)periodics, periodiccnt, i);
1239} 1283}
1240#endif 1284#endif
1241 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 }
1306}
1307#endif
1308
1242int inline_size 1309int inline_size
1243time_update_monotonic (EV_P) 1310time_update_monotonic (EV_P)
1244{ 1311{
1245 mn_now = get_clock (); 1312 mn_now = get_clock ();
1246 1313
1270 ev_tstamp odiff = rtmn_diff; 1337 ev_tstamp odiff = rtmn_diff;
1271 1338
1272 /* loop a few times, before making important decisions. 1339 /* loop a few times, before making important decisions.
1273 * on the choice of "4": one iteration isn't enough, 1340 * on the choice of "4": one iteration isn't enough,
1274 * in case we get preempted during the calls to 1341 * in case we get preempted during the calls to
1275 * ev_time and get_clock. a second call is almost guarenteed 1342 * ev_time and get_clock. a second call is almost guaranteed
1276 * to succeed in that case, though. and looping a few more times 1343 * to succeed in that case, though. and looping a few more times
1277 * doesn't hurt either as we only do this on time-jumps or 1344 * doesn't hurt either as we only do this on time-jumps or
1278 * in the unlikely event of getting preempted here. 1345 * in the unlikely event of having been preempted here.
1279 */ 1346 */
1280 for (i = 4; --i; ) 1347 for (i = 4; --i; )
1281 { 1348 {
1282 rtmn_diff = ev_rt_now - mn_now; 1349 rtmn_diff = ev_rt_now - mn_now;
1283 1350
1305 { 1372 {
1306#if EV_PERIODIC_ENABLE 1373#if EV_PERIODIC_ENABLE
1307 periodics_reschedule (EV_A); 1374 periodics_reschedule (EV_A);
1308#endif 1375#endif
1309 1376
1310 /* adjust timers. this is easy, as the offset is the same for all */ 1377 /* adjust timers. this is easy, as the offset is the same for all of them */
1311 for (i = 0; i < timercnt; ++i) 1378 for (i = 0; i < timercnt; ++i)
1312 ((WT)timers [i])->at += ev_rt_now - mn_now; 1379 ((WT)timers [i])->at += ev_rt_now - mn_now;
1313 } 1380 }
1314 1381
1315 mn_now = ev_rt_now; 1382 mn_now = ev_rt_now;
1335{ 1402{
1336 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1403 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1337 ? EVUNLOOP_ONE 1404 ? EVUNLOOP_ONE
1338 : EVUNLOOP_CANCEL; 1405 : EVUNLOOP_CANCEL;
1339 1406
1340 while (activecnt) 1407 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1408
1409 do
1341 { 1410 {
1342 /* we might have forked, so reify kernel state if necessary */ 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
1343 #if EV_FORK_ENABLE 1420#if EV_FORK_ENABLE
1421 /* we might have forked, so queue fork handlers */
1344 if (expect_false (postfork)) 1422 if (expect_false (postfork))
1345 if (forkcnt) 1423 if (forkcnt)
1346 { 1424 {
1347 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1425 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1348 call_pending (EV_A); 1426 call_pending (EV_A);
1349 } 1427 }
1350 #endif 1428#endif
1351 1429
1352 /* queue check watchers (and execute them) */ 1430 /* queue prepare watchers (and execute them) */
1353 if (expect_false (preparecnt)) 1431 if (expect_false (preparecnt))
1354 { 1432 {
1355 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1433 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1356 call_pending (EV_A); 1434 call_pending (EV_A);
1357 } 1435 }
1358 1436
1437 if (expect_false (!activecnt))
1438 break;
1439
1359 /* we might have forked, so reify kernel state if necessary */ 1440 /* we might have forked, so reify kernel state if necessary */
1360 if (expect_false (postfork)) 1441 if (expect_false (postfork))
1361 loop_fork (EV_A); 1442 loop_fork (EV_A);
1362 1443
1363 /* update fd-related kernel structures */ 1444 /* update fd-related kernel structures */
1364 fd_reify (EV_A); 1445 fd_reify (EV_A);
1365 1446
1366 /* calculate blocking time */ 1447 /* calculate blocking time */
1367 { 1448 {
1368 double block; 1449 ev_tstamp block;
1369 1450
1370 if (flags & EVLOOP_NONBLOCK || idlecnt) 1451 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1371 block = 0.; /* do not block at all */ 1452 block = 0.; /* do not block at all */
1372 else 1453 else
1373 { 1454 {
1374 /* update time to cancel out callback processing overhead */ 1455 /* update time to cancel out callback processing overhead */
1375#if EV_USE_MONOTONIC 1456#if EV_USE_MONOTONIC
1399#endif 1480#endif
1400 1481
1401 if (expect_false (block < 0.)) block = 0.; 1482 if (expect_false (block < 0.)) block = 0.;
1402 } 1483 }
1403 1484
1485 ++loop_count;
1404 backend_poll (EV_A_ block); 1486 backend_poll (EV_A_ block);
1405 } 1487 }
1406 1488
1407 /* update ev_rt_now, do magic */ 1489 /* update ev_rt_now, do magic */
1408 time_update (EV_A); 1490 time_update (EV_A);
1411 timers_reify (EV_A); /* relative timers called last */ 1493 timers_reify (EV_A); /* relative timers called last */
1412#if EV_PERIODIC_ENABLE 1494#if EV_PERIODIC_ENABLE
1413 periodics_reify (EV_A); /* absolute timers called first */ 1495 periodics_reify (EV_A); /* absolute timers called first */
1414#endif 1496#endif
1415 1497
1498#if EV_IDLE_ENABLE
1416 /* queue idle watchers unless other events are pending */ 1499 /* queue idle watchers unless other events are pending */
1417 if (idlecnt && !any_pending (EV_A)) 1500 idle_reify (EV_A);
1418 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1501#endif
1419 1502
1420 /* queue check watchers, to be executed first */ 1503 /* queue check watchers, to be executed first */
1421 if (expect_false (checkcnt)) 1504 if (expect_false (checkcnt))
1422 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1505 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1423 1506
1424 call_pending (EV_A); 1507 call_pending (EV_A);
1425 1508
1426 if (expect_false (loop_done))
1427 break;
1428 } 1509 }
1510 while (expect_true (activecnt && !loop_done));
1429 1511
1430 if (loop_done == EVUNLOOP_ONE) 1512 if (loop_done == EVUNLOOP_ONE)
1431 loop_done = EVUNLOOP_CANCEL; 1513 loop_done = EVUNLOOP_CANCEL;
1432} 1514}
1433 1515
1460 head = &(*head)->next; 1542 head = &(*head)->next;
1461 } 1543 }
1462} 1544}
1463 1545
1464void inline_speed 1546void inline_speed
1465ev_clear_pending (EV_P_ W w) 1547clear_pending (EV_P_ W w)
1466{ 1548{
1467 if (w->pending) 1549 if (w->pending)
1468 { 1550 {
1469 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1551 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1470 w->pending = 0; 1552 w->pending = 0;
1471 } 1553 }
1472} 1554}
1473 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
1474void inline_speed 1582void inline_speed
1475ev_start (EV_P_ W w, int active) 1583ev_start (EV_P_ W w, int active)
1476{ 1584{
1477 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1585 pri_adjust (EV_A_ w);
1478 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1479
1480 w->active = active; 1586 w->active = active;
1481 ev_ref (EV_A); 1587 ev_ref (EV_A);
1482} 1588}
1483 1589
1484void inline_size 1590void inline_size
1488 w->active = 0; 1594 w->active = 0;
1489} 1595}
1490 1596
1491/*****************************************************************************/ 1597/*****************************************************************************/
1492 1598
1493void 1599void noinline
1494ev_io_start (EV_P_ ev_io *w) 1600ev_io_start (EV_P_ ev_io *w)
1495{ 1601{
1496 int fd = w->fd; 1602 int fd = w->fd;
1497 1603
1498 if (expect_false (ev_is_active (w))) 1604 if (expect_false (ev_is_active (w)))
1505 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1611 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1506 1612
1507 fd_change (EV_A_ fd); 1613 fd_change (EV_A_ fd);
1508} 1614}
1509 1615
1510void 1616void noinline
1511ev_io_stop (EV_P_ ev_io *w) 1617ev_io_stop (EV_P_ ev_io *w)
1512{ 1618{
1513 ev_clear_pending (EV_A_ (W)w); 1619 clear_pending (EV_A_ (W)w);
1514 if (expect_false (!ev_is_active (w))) 1620 if (expect_false (!ev_is_active (w)))
1515 return; 1621 return;
1516 1622
1517 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));
1518 1624
1520 ev_stop (EV_A_ (W)w); 1626 ev_stop (EV_A_ (W)w);
1521 1627
1522 fd_change (EV_A_ w->fd); 1628 fd_change (EV_A_ w->fd);
1523} 1629}
1524 1630
1525void 1631void noinline
1526ev_timer_start (EV_P_ ev_timer *w) 1632ev_timer_start (EV_P_ ev_timer *w)
1527{ 1633{
1528 if (expect_false (ev_is_active (w))) 1634 if (expect_false (ev_is_active (w)))
1529 return; 1635 return;
1530 1636
1538 upheap ((WT *)timers, timercnt - 1); 1644 upheap ((WT *)timers, timercnt - 1);
1539 1645
1540 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1646 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1541} 1647}
1542 1648
1543void 1649void noinline
1544ev_timer_stop (EV_P_ ev_timer *w) 1650ev_timer_stop (EV_P_ ev_timer *w)
1545{ 1651{
1546 ev_clear_pending (EV_A_ (W)w); 1652 clear_pending (EV_A_ (W)w);
1547 if (expect_false (!ev_is_active (w))) 1653 if (expect_false (!ev_is_active (w)))
1548 return; 1654 return;
1549 1655
1550 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1656 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1551 1657
1562 ((WT)w)->at -= mn_now; 1668 ((WT)w)->at -= mn_now;
1563 1669
1564 ev_stop (EV_A_ (W)w); 1670 ev_stop (EV_A_ (W)w);
1565} 1671}
1566 1672
1567void 1673void noinline
1568ev_timer_again (EV_P_ ev_timer *w) 1674ev_timer_again (EV_P_ ev_timer *w)
1569{ 1675{
1570 if (ev_is_active (w)) 1676 if (ev_is_active (w))
1571 { 1677 {
1572 if (w->repeat) 1678 if (w->repeat)
1583 ev_timer_start (EV_A_ w); 1689 ev_timer_start (EV_A_ w);
1584 } 1690 }
1585} 1691}
1586 1692
1587#if EV_PERIODIC_ENABLE 1693#if EV_PERIODIC_ENABLE
1588void 1694void noinline
1589ev_periodic_start (EV_P_ ev_periodic *w) 1695ev_periodic_start (EV_P_ ev_periodic *w)
1590{ 1696{
1591 if (expect_false (ev_is_active (w))) 1697 if (expect_false (ev_is_active (w)))
1592 return; 1698 return;
1593 1699
1595 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1701 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1596 else if (w->interval) 1702 else if (w->interval)
1597 { 1703 {
1598 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.));
1599 /* 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 */
1600 ((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;
1601 } 1707 }
1708 else
1709 ((WT)w)->at = w->offset;
1602 1710
1603 ev_start (EV_A_ (W)w, ++periodiccnt); 1711 ev_start (EV_A_ (W)w, ++periodiccnt);
1604 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1712 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1605 periodics [periodiccnt - 1] = w; 1713 periodics [periodiccnt - 1] = w;
1606 upheap ((WT *)periodics, periodiccnt - 1); 1714 upheap ((WT *)periodics, periodiccnt - 1);
1607 1715
1608 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1716 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1609} 1717}
1610 1718
1611void 1719void noinline
1612ev_periodic_stop (EV_P_ ev_periodic *w) 1720ev_periodic_stop (EV_P_ ev_periodic *w)
1613{ 1721{
1614 ev_clear_pending (EV_A_ (W)w); 1722 clear_pending (EV_A_ (W)w);
1615 if (expect_false (!ev_is_active (w))) 1723 if (expect_false (!ev_is_active (w)))
1616 return; 1724 return;
1617 1725
1618 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1726 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1619 1727
1628 } 1736 }
1629 1737
1630 ev_stop (EV_A_ (W)w); 1738 ev_stop (EV_A_ (W)w);
1631} 1739}
1632 1740
1633void 1741void noinline
1634ev_periodic_again (EV_P_ ev_periodic *w) 1742ev_periodic_again (EV_P_ ev_periodic *w)
1635{ 1743{
1636 /* TODO: use adjustheap and recalculation */ 1744 /* TODO: use adjustheap and recalculation */
1637 ev_periodic_stop (EV_A_ w); 1745 ev_periodic_stop (EV_A_ w);
1638 ev_periodic_start (EV_A_ w); 1746 ev_periodic_start (EV_A_ w);
1641 1749
1642#ifndef SA_RESTART 1750#ifndef SA_RESTART
1643# define SA_RESTART 0 1751# define SA_RESTART 0
1644#endif 1752#endif
1645 1753
1646void 1754void noinline
1647ev_signal_start (EV_P_ ev_signal *w) 1755ev_signal_start (EV_P_ ev_signal *w)
1648{ 1756{
1649#if EV_MULTIPLICITY 1757#if EV_MULTIPLICITY
1650 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));
1651#endif 1759#endif
1670 sigaction (w->signum, &sa, 0); 1778 sigaction (w->signum, &sa, 0);
1671#endif 1779#endif
1672 } 1780 }
1673} 1781}
1674 1782
1675void 1783void noinline
1676ev_signal_stop (EV_P_ ev_signal *w) 1784ev_signal_stop (EV_P_ ev_signal *w)
1677{ 1785{
1678 ev_clear_pending (EV_A_ (W)w); 1786 clear_pending (EV_A_ (W)w);
1679 if (expect_false (!ev_is_active (w))) 1787 if (expect_false (!ev_is_active (w)))
1680 return; 1788 return;
1681 1789
1682 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1790 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1683 ev_stop (EV_A_ (W)w); 1791 ev_stop (EV_A_ (W)w);
1700} 1808}
1701 1809
1702void 1810void
1703ev_child_stop (EV_P_ ev_child *w) 1811ev_child_stop (EV_P_ ev_child *w)
1704{ 1812{
1705 ev_clear_pending (EV_A_ (W)w); 1813 clear_pending (EV_A_ (W)w);
1706 if (expect_false (!ev_is_active (w))) 1814 if (expect_false (!ev_is_active (w)))
1707 return; 1815 return;
1708 1816
1709 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);
1710 ev_stop (EV_A_ (W)w); 1818 ev_stop (EV_A_ (W)w);
1718# endif 1826# endif
1719 1827
1720#define DEF_STAT_INTERVAL 5.0074891 1828#define DEF_STAT_INTERVAL 5.0074891
1721#define MIN_STAT_INTERVAL 0.1074891 1829#define MIN_STAT_INTERVAL 0.1074891
1722 1830
1723void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); 1831static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
1724 1832
1725#if EV_USE_INOTIFY 1833#if EV_USE_INOTIFY
1726# define EV_INOTIFY_BUFSIZE 8192 1834# define EV_INOTIFY_BUFSIZE 8192
1727 1835
1728static void noinline 1836static void noinline
1879 w->attr.st_nlink = 0; 1987 w->attr.st_nlink = 0;
1880 else if (!w->attr.st_nlink) 1988 else if (!w->attr.st_nlink)
1881 w->attr.st_nlink = 1; 1989 w->attr.st_nlink = 1;
1882} 1990}
1883 1991
1884void noinline 1992static void noinline
1885stat_timer_cb (EV_P_ ev_timer *w_, int revents) 1993stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1886{ 1994{
1887 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 1995 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1888 1996
1889 /* we copy this here each the time so that */ 1997 /* we copy this here each the time so that */
1946} 2054}
1947 2055
1948void 2056void
1949ev_stat_stop (EV_P_ ev_stat *w) 2057ev_stat_stop (EV_P_ ev_stat *w)
1950{ 2058{
1951 ev_clear_pending (EV_A_ (W)w); 2059 clear_pending (EV_A_ (W)w);
1952 if (expect_false (!ev_is_active (w))) 2060 if (expect_false (!ev_is_active (w)))
1953 return; 2061 return;
1954 2062
1955#if EV_USE_INOTIFY 2063#if EV_USE_INOTIFY
1956 infy_del (EV_A_ w); 2064 infy_del (EV_A_ w);
1959 2067
1960 ev_stop (EV_A_ (W)w); 2068 ev_stop (EV_A_ (W)w);
1961} 2069}
1962#endif 2070#endif
1963 2071
2072#if EV_IDLE_ENABLE
1964void 2073void
1965ev_idle_start (EV_P_ ev_idle *w) 2074ev_idle_start (EV_P_ ev_idle *w)
1966{ 2075{
1967 if (expect_false (ev_is_active (w))) 2076 if (expect_false (ev_is_active (w)))
1968 return; 2077 return;
1969 2078
2079 pri_adjust (EV_A_ (W)w);
2080
2081 {
2082 int active = ++idlecnt [ABSPRI (w)];
2083
2084 ++idleall;
1970 ev_start (EV_A_ (W)w, ++idlecnt); 2085 ev_start (EV_A_ (W)w, active);
2086
1971 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2087 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1972 idles [idlecnt - 1] = w; 2088 idles [ABSPRI (w)][active - 1] = w;
2089 }
1973} 2090}
1974 2091
1975void 2092void
1976ev_idle_stop (EV_P_ ev_idle *w) 2093ev_idle_stop (EV_P_ ev_idle *w)
1977{ 2094{
1978 ev_clear_pending (EV_A_ (W)w); 2095 clear_pending (EV_A_ (W)w);
1979 if (expect_false (!ev_is_active (w))) 2096 if (expect_false (!ev_is_active (w)))
1980 return; 2097 return;
1981 2098
1982 { 2099 {
1983 int active = ((W)w)->active; 2100 int active = ((W)w)->active;
1984 idles [active - 1] = idles [--idlecnt]; 2101
2102 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
1985 ((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;
1986 } 2107 }
1987
1988 ev_stop (EV_A_ (W)w);
1989} 2108}
2109#endif
1990 2110
1991void 2111void
1992ev_prepare_start (EV_P_ ev_prepare *w) 2112ev_prepare_start (EV_P_ ev_prepare *w)
1993{ 2113{
1994 if (expect_false (ev_is_active (w))) 2114 if (expect_false (ev_is_active (w)))
2000} 2120}
2001 2121
2002void 2122void
2003ev_prepare_stop (EV_P_ ev_prepare *w) 2123ev_prepare_stop (EV_P_ ev_prepare *w)
2004{ 2124{
2005 ev_clear_pending (EV_A_ (W)w); 2125 clear_pending (EV_A_ (W)w);
2006 if (expect_false (!ev_is_active (w))) 2126 if (expect_false (!ev_is_active (w)))
2007 return; 2127 return;
2008 2128
2009 { 2129 {
2010 int active = ((W)w)->active; 2130 int active = ((W)w)->active;
2027} 2147}
2028 2148
2029void 2149void
2030ev_check_stop (EV_P_ ev_check *w) 2150ev_check_stop (EV_P_ ev_check *w)
2031{ 2151{
2032 ev_clear_pending (EV_A_ (W)w); 2152 clear_pending (EV_A_ (W)w);
2033 if (expect_false (!ev_is_active (w))) 2153 if (expect_false (!ev_is_active (w)))
2034 return; 2154 return;
2035 2155
2036 { 2156 {
2037 int active = ((W)w)->active; 2157 int active = ((W)w)->active;
2079} 2199}
2080 2200
2081void 2201void
2082ev_embed_stop (EV_P_ ev_embed *w) 2202ev_embed_stop (EV_P_ ev_embed *w)
2083{ 2203{
2084 ev_clear_pending (EV_A_ (W)w); 2204 clear_pending (EV_A_ (W)w);
2085 if (expect_false (!ev_is_active (w))) 2205 if (expect_false (!ev_is_active (w)))
2086 return; 2206 return;
2087 2207
2088 ev_io_stop (EV_A_ &w->io); 2208 ev_io_stop (EV_A_ &w->io);
2089 2209
2104} 2224}
2105 2225
2106void 2226void
2107ev_fork_stop (EV_P_ ev_fork *w) 2227ev_fork_stop (EV_P_ ev_fork *w)
2108{ 2228{
2109 ev_clear_pending (EV_A_ (W)w); 2229 clear_pending (EV_A_ (W)w);
2110 if (expect_false (!ev_is_active (w))) 2230 if (expect_false (!ev_is_active (w)))
2111 return; 2231 return;
2112 2232
2113 { 2233 {
2114 int active = ((W)w)->active; 2234 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines