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

Comparing libev/ev.c (file contents):
Revision 1.152 by root, Wed Nov 28 11:15:55 2007 UTC vs.
Revision 1.168 by root, Sat Dec 8 14:12:07 2007 UTC

241 241
242#define expect_false(expr) expect ((expr) != 0, 0) 242#define expect_false(expr) expect ((expr) != 0, 0)
243#define expect_true(expr) expect ((expr) != 0, 1) 243#define expect_true(expr) expect ((expr) != 0, 1)
244 244
245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
246#define ABSPRI(w) ((w)->priority - EV_MINPRI) 246#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
247 247
248#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 248#define EMPTY /* required for microsofts broken pseudo-c compiler */
249#define EMPTY2(a,b) /* used to suppress some warnings */ 249#define EMPTY2(a,b) /* used to suppress some warnings */
250 250
251typedef ev_watcher *W; 251typedef ev_watcher *W;
252typedef ev_watcher_list *WL; 252typedef ev_watcher_list *WL;
253typedef ev_watcher_time *WT; 253typedef ev_watcher_time *WT;
281 perror (msg); 281 perror (msg);
282 abort (); 282 abort ();
283 } 283 }
284} 284}
285 285
286static void *(*alloc)(void *ptr, size_t size) = realloc; 286static void *(*alloc)(void *ptr, long size);
287 287
288void 288void
289ev_set_allocator (void *(*cb)(void *ptr, size_t size)) 289ev_set_allocator (void *(*cb)(void *ptr, long size))
290{ 290{
291 alloc = cb; 291 alloc = cb;
292} 292}
293 293
294inline_speed void * 294inline_speed void *
295ev_realloc (void *ptr, size_t size) 295ev_realloc (void *ptr, long size)
296{ 296{
297 ptr = alloc (ptr, size); 297 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
298 298
299 if (!ptr && size) 299 if (!ptr && size)
300 { 300 {
301 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", (long)size); 301 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
302 abort (); 302 abort ();
303 } 303 }
304 304
305 return ptr; 305 return ptr;
306} 306}
324{ 324{
325 W w; 325 W w;
326 int events; 326 int events;
327} ANPENDING; 327} ANPENDING;
328 328
329#if EV_USE_INOTIFY
329typedef struct 330typedef struct
330{ 331{
331#if EV_USE_INOTIFY
332 WL head; 332 WL head;
333#endif
334} ANFS; 333} ANFS;
334#endif
335 335
336#if EV_MULTIPLICITY 336#if EV_MULTIPLICITY
337 337
338 struct ev_loop 338 struct ev_loop
339 { 339 {
396{ 396{
397 return ev_rt_now; 397 return ev_rt_now;
398} 398}
399#endif 399#endif
400 400
401#define array_roundsize(type,n) (((n) | 4) & ~3) 401int inline_size
402array_nextsize (int elem, int cur, int cnt)
403{
404 int ncur = cur + 1;
405
406 do
407 ncur <<= 1;
408 while (cnt > ncur);
409
410 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
411 if (elem * ncur > 4096)
412 {
413 ncur *= elem;
414 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
415 ncur = ncur - sizeof (void *) * 4;
416 ncur /= elem;
417 }
418
419 return ncur;
420}
421
422inline_speed void *
423array_realloc (int elem, void *base, int *cur, int cnt)
424{
425 *cur = array_nextsize (elem, *cur, cnt);
426 return ev_realloc (base, elem * *cur);
427}
402 428
403#define array_needsize(type,base,cur,cnt,init) \ 429#define array_needsize(type,base,cur,cnt,init) \
404 if (expect_false ((cnt) > cur)) \ 430 if (expect_false ((cnt) > (cur))) \
405 { \ 431 { \
406 int newcnt = cur; \ 432 int ocur_ = (cur); \
407 do \ 433 (base) = (type *)array_realloc \
408 { \ 434 (sizeof (type), (base), &(cur), (cnt)); \
409 newcnt = array_roundsize (type, newcnt << 1); \ 435 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 } 436 }
417 437
438#if 0
418#define array_slim(type,stem) \ 439#define array_slim(type,stem) \
419 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 440 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
420 { \ 441 { \
421 stem ## max = array_roundsize (stem ## cnt >> 1); \ 442 stem ## max = array_roundsize (stem ## cnt >> 1); \
422 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 443 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
423 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 444 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
424 } 445 }
446#endif
425 447
426#define array_free(stem, idx) \ 448#define array_free(stem, idx) \
427 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 449 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
428 450
429/*****************************************************************************/ 451/*****************************************************************************/
485} 507}
486 508
487void 509void
488ev_feed_fd_event (EV_P_ int fd, int revents) 510ev_feed_fd_event (EV_P_ int fd, int revents)
489{ 511{
512 if (fd >= 0 && fd < anfdmax)
490 fd_event (EV_A_ fd, revents); 513 fd_event (EV_A_ fd, revents);
491} 514}
492 515
493void inline_size 516void inline_size
494fd_reify (EV_P) 517fd_reify (EV_P)
495{ 518{
589static void noinline 612static void noinline
590fd_rearm_all (EV_P) 613fd_rearm_all (EV_P)
591{ 614{
592 int fd; 615 int fd;
593 616
594 /* this should be highly optimised to not do anything but set a flag */
595 for (fd = 0; fd < anfdmax; ++fd) 617 for (fd = 0; fd < anfdmax; ++fd)
596 if (anfds [fd].events) 618 if (anfds [fd].events)
597 { 619 {
598 anfds [fd].events = 0; 620 anfds [fd].events = 0;
599 fd_change (EV_A_ fd); 621 fd_change (EV_A_ fd);
765 ev_child *w; 787 ev_child *w;
766 788
767 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 789 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
768 if (w->pid == pid || !w->pid) 790 if (w->pid == pid || !w->pid)
769 { 791 {
770 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 792 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
771 w->rpid = pid; 793 w->rpid = pid;
772 w->rstatus = status; 794 w->rstatus = status;
773 ev_feed_event (EV_A_ (W)w, EV_CHILD); 795 ev_feed_event (EV_A_ (W)w, EV_CHILD);
774 } 796 }
775} 797}
776 798
777#ifndef WCONTINUED 799#ifndef WCONTINUED
887ev_backend (EV_P) 909ev_backend (EV_P)
888{ 910{
889 return backend; 911 return backend;
890} 912}
891 913
914unsigned int
915ev_loop_count (EV_P)
916{
917 return loop_count;
918}
919
892static void noinline 920static void noinline
893loop_init (EV_P_ unsigned int flags) 921loop_init (EV_P_ unsigned int flags)
894{ 922{
895 if (!backend) 923 if (!backend)
896 { 924 {
905 ev_rt_now = ev_time (); 933 ev_rt_now = ev_time ();
906 mn_now = get_clock (); 934 mn_now = get_clock ();
907 now_floor = mn_now; 935 now_floor = mn_now;
908 rtmn_diff = ev_rt_now - mn_now; 936 rtmn_diff = ev_rt_now - mn_now;
909 937
938 /* pid check not overridable via env */
939#ifndef _WIN32
940 if (flags & EVFLAG_FORKCHECK)
941 curpid = getpid ();
942#endif
943
910 if (!(flags & EVFLAG_NOENV) 944 if (!(flags & EVFLAG_NOENV)
911 && !enable_secure () 945 && !enable_secure ()
912 && getenv ("LIBEV_FLAGS")) 946 && getenv ("LIBEV_FLAGS"))
913 flags = atoi (getenv ("LIBEV_FLAGS")); 947 flags = atoi (getenv ("LIBEV_FLAGS"));
914 948
970#if EV_USE_SELECT 1004#if EV_USE_SELECT
971 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1005 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
972#endif 1006#endif
973 1007
974 for (i = NUMPRI; i--; ) 1008 for (i = NUMPRI; i--; )
1009 {
975 array_free (pending, [i]); 1010 array_free (pending, [i]);
1011#if EV_IDLE_ENABLE
1012 array_free (idle, [i]);
1013#endif
1014 }
976 1015
977 /* have to use the microsoft-never-gets-it-right macro */ 1016 /* have to use the microsoft-never-gets-it-right macro */
978 array_free (fdchange, EMPTY0); 1017 array_free (fdchange, EMPTY);
979 array_free (timer, EMPTY0); 1018 array_free (timer, EMPTY);
980#if EV_PERIODIC_ENABLE 1019#if EV_PERIODIC_ENABLE
981 array_free (periodic, EMPTY0); 1020 array_free (periodic, EMPTY);
982#endif 1021#endif
983 array_free (idle, EMPTY0);
984 array_free (prepare, EMPTY0); 1022 array_free (prepare, EMPTY);
985 array_free (check, EMPTY0); 1023 array_free (check, EMPTY);
986 1024
987 backend = 0; 1025 backend = 0;
988} 1026}
1027
1028void inline_size infy_fork (EV_P);
989 1029
990void inline_size 1030void inline_size
991loop_fork (EV_P) 1031loop_fork (EV_P)
992{ 1032{
993#if EV_USE_PORT 1033#if EV_USE_PORT
996#if EV_USE_KQUEUE 1036#if EV_USE_KQUEUE
997 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 1037 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
998#endif 1038#endif
999#if EV_USE_EPOLL 1039#if EV_USE_EPOLL
1000 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 1040 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
1041#endif
1042#if EV_USE_INOTIFY
1043 infy_fork (EV_A);
1001#endif 1044#endif
1002 1045
1003 if (ev_is_active (&sigev)) 1046 if (ev_is_active (&sigev))
1004 { 1047 {
1005 /* default loop */ 1048 /* default loop */
1121 postfork = 1; 1164 postfork = 1;
1122} 1165}
1123 1166
1124/*****************************************************************************/ 1167/*****************************************************************************/
1125 1168
1126int inline_size 1169void
1127any_pending (EV_P) 1170ev_invoke (EV_P_ void *w, int revents)
1128{ 1171{
1129 int pri; 1172 EV_CB_INVOKE ((W)w, revents);
1130
1131 for (pri = NUMPRI; pri--; )
1132 if (pendingcnt [pri])
1133 return 1;
1134
1135 return 0;
1136} 1173}
1137 1174
1138void inline_speed 1175void inline_speed
1139call_pending (EV_P) 1176call_pending (EV_P)
1140{ 1177{
1232 for (i = periodiccnt >> 1; i--; ) 1269 for (i = periodiccnt >> 1; i--; )
1233 downheap ((WT *)periodics, periodiccnt, i); 1270 downheap ((WT *)periodics, periodiccnt, i);
1234} 1271}
1235#endif 1272#endif
1236 1273
1274#if EV_IDLE_ENABLE
1275void inline_size
1276idle_reify (EV_P)
1277{
1278 if (expect_false (idleall))
1279 {
1280 int pri;
1281
1282 for (pri = NUMPRI; pri--; )
1283 {
1284 if (pendingcnt [pri])
1285 break;
1286
1287 if (idlecnt [pri])
1288 {
1289 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1290 break;
1291 }
1292 }
1293 }
1294}
1295#endif
1296
1237int inline_size 1297int inline_size
1238time_update_monotonic (EV_P) 1298time_update_monotonic (EV_P)
1239{ 1299{
1240 mn_now = get_clock (); 1300 mn_now = get_clock ();
1241 1301
1265 ev_tstamp odiff = rtmn_diff; 1325 ev_tstamp odiff = rtmn_diff;
1266 1326
1267 /* loop a few times, before making important decisions. 1327 /* loop a few times, before making important decisions.
1268 * on the choice of "4": one iteration isn't enough, 1328 * on the choice of "4": one iteration isn't enough,
1269 * in case we get preempted during the calls to 1329 * in case we get preempted during the calls to
1270 * ev_time and get_clock. a second call is almost guarenteed 1330 * ev_time and get_clock. a second call is almost guaranteed
1271 * to succeed in that case, though. and looping a few more times 1331 * to succeed in that case, though. and looping a few more times
1272 * doesn't hurt either as we only do this on time-jumps or 1332 * doesn't hurt either as we only do this on time-jumps or
1273 * in the unlikely event of getting preempted here. 1333 * in the unlikely event of having been preempted here.
1274 */ 1334 */
1275 for (i = 4; --i; ) 1335 for (i = 4; --i; )
1276 { 1336 {
1277 rtmn_diff = ev_rt_now - mn_now; 1337 rtmn_diff = ev_rt_now - mn_now;
1278 1338
1300 { 1360 {
1301#if EV_PERIODIC_ENABLE 1361#if EV_PERIODIC_ENABLE
1302 periodics_reschedule (EV_A); 1362 periodics_reschedule (EV_A);
1303#endif 1363#endif
1304 1364
1305 /* adjust timers. this is easy, as the offset is the same for all */ 1365 /* adjust timers. this is easy, as the offset is the same for all of them */
1306 for (i = 0; i < timercnt; ++i) 1366 for (i = 0; i < timercnt; ++i)
1307 ((WT)timers [i])->at += ev_rt_now - mn_now; 1367 ((WT)timers [i])->at += ev_rt_now - mn_now;
1308 } 1368 }
1309 1369
1310 mn_now = ev_rt_now; 1370 mn_now = ev_rt_now;
1330{ 1390{
1331 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1391 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1332 ? EVUNLOOP_ONE 1392 ? EVUNLOOP_ONE
1333 : EVUNLOOP_CANCEL; 1393 : EVUNLOOP_CANCEL;
1334 1394
1335 while (activecnt) 1395 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1396
1397 do
1336 { 1398 {
1337 /* we might have forked, so reify kernel state if necessary */ 1399#ifndef _WIN32
1400 if (expect_false (curpid)) /* penalise the forking check even more */
1401 if (expect_false (getpid () != curpid))
1402 {
1403 curpid = getpid ();
1404 postfork = 1;
1405 }
1406#endif
1407
1338 #if EV_FORK_ENABLE 1408#if EV_FORK_ENABLE
1409 /* we might have forked, so queue fork handlers */
1339 if (expect_false (postfork)) 1410 if (expect_false (postfork))
1340 if (forkcnt) 1411 if (forkcnt)
1341 { 1412 {
1342 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1413 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1343 call_pending (EV_A); 1414 call_pending (EV_A);
1344 } 1415 }
1345 #endif 1416#endif
1346 1417
1347 /* queue check watchers (and execute them) */ 1418 /* queue check watchers (and execute them) */
1348 if (expect_false (preparecnt)) 1419 if (expect_false (preparecnt))
1349 { 1420 {
1350 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1421 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1351 call_pending (EV_A); 1422 call_pending (EV_A);
1352 } 1423 }
1353 1424
1425 if (expect_false (!activecnt))
1426 break;
1427
1354 /* we might have forked, so reify kernel state if necessary */ 1428 /* we might have forked, so reify kernel state if necessary */
1355 if (expect_false (postfork)) 1429 if (expect_false (postfork))
1356 loop_fork (EV_A); 1430 loop_fork (EV_A);
1357 1431
1358 /* update fd-related kernel structures */ 1432 /* update fd-related kernel structures */
1359 fd_reify (EV_A); 1433 fd_reify (EV_A);
1360 1434
1361 /* calculate blocking time */ 1435 /* calculate blocking time */
1362 { 1436 {
1363 double block; 1437 ev_tstamp block;
1364 1438
1365 if (flags & EVLOOP_NONBLOCK || idlecnt) 1439 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1366 block = 0.; /* do not block at all */ 1440 block = 0.; /* do not block at all */
1367 else 1441 else
1368 { 1442 {
1369 /* update time to cancel out callback processing overhead */ 1443 /* update time to cancel out callback processing overhead */
1370#if EV_USE_MONOTONIC 1444#if EV_USE_MONOTONIC
1394#endif 1468#endif
1395 1469
1396 if (expect_false (block < 0.)) block = 0.; 1470 if (expect_false (block < 0.)) block = 0.;
1397 } 1471 }
1398 1472
1473 ++loop_count;
1399 backend_poll (EV_A_ block); 1474 backend_poll (EV_A_ block);
1400 } 1475 }
1401 1476
1402 /* update ev_rt_now, do magic */ 1477 /* update ev_rt_now, do magic */
1403 time_update (EV_A); 1478 time_update (EV_A);
1406 timers_reify (EV_A); /* relative timers called last */ 1481 timers_reify (EV_A); /* relative timers called last */
1407#if EV_PERIODIC_ENABLE 1482#if EV_PERIODIC_ENABLE
1408 periodics_reify (EV_A); /* absolute timers called first */ 1483 periodics_reify (EV_A); /* absolute timers called first */
1409#endif 1484#endif
1410 1485
1486#if EV_IDLE_ENABLE
1411 /* queue idle watchers unless other events are pending */ 1487 /* queue idle watchers unless other events are pending */
1412 if (idlecnt && !any_pending (EV_A)) 1488 idle_reify (EV_A);
1413 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1489#endif
1414 1490
1415 /* queue check watchers, to be executed first */ 1491 /* queue check watchers, to be executed first */
1416 if (expect_false (checkcnt)) 1492 if (expect_false (checkcnt))
1417 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1493 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1418 1494
1419 call_pending (EV_A); 1495 call_pending (EV_A);
1420 1496
1421 if (expect_false (loop_done))
1422 break;
1423 } 1497 }
1498 while (expect_true (activecnt && !loop_done));
1424 1499
1425 if (loop_done == EVUNLOOP_ONE) 1500 if (loop_done == EVUNLOOP_ONE)
1426 loop_done = EVUNLOOP_CANCEL; 1501 loop_done = EVUNLOOP_CANCEL;
1427} 1502}
1428 1503
1455 head = &(*head)->next; 1530 head = &(*head)->next;
1456 } 1531 }
1457} 1532}
1458 1533
1459void inline_speed 1534void inline_speed
1460ev_clear_pending (EV_P_ W w) 1535clear_pending (EV_P_ W w)
1461{ 1536{
1462 if (w->pending) 1537 if (w->pending)
1463 { 1538 {
1464 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1539 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1465 w->pending = 0; 1540 w->pending = 0;
1466 } 1541 }
1467} 1542}
1468 1543
1544int
1545ev_clear_pending (EV_P_ void *w)
1546{
1547 W w_ = (W)w;
1548 int pending = w_->pending;
1549
1550 if (!pending)
1551 return 0;
1552
1553 w_->pending = 0;
1554 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1555 p->w = 0;
1556
1557 return p->events;
1558}
1559
1560void inline_size
1561pri_adjust (EV_P_ W w)
1562{
1563 int pri = w->priority;
1564 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1565 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1566 w->priority = pri;
1567}
1568
1469void inline_speed 1569void inline_speed
1470ev_start (EV_P_ W w, int active) 1570ev_start (EV_P_ W w, int active)
1471{ 1571{
1472 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1572 pri_adjust (EV_A_ w);
1473 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1474
1475 w->active = active; 1573 w->active = active;
1476 ev_ref (EV_A); 1574 ev_ref (EV_A);
1477} 1575}
1478 1576
1479void inline_size 1577void inline_size
1503} 1601}
1504 1602
1505void 1603void
1506ev_io_stop (EV_P_ ev_io *w) 1604ev_io_stop (EV_P_ ev_io *w)
1507{ 1605{
1508 ev_clear_pending (EV_A_ (W)w); 1606 clear_pending (EV_A_ (W)w);
1509 if (expect_false (!ev_is_active (w))) 1607 if (expect_false (!ev_is_active (w)))
1510 return; 1608 return;
1511 1609
1512 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1610 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1513 1611
1536} 1634}
1537 1635
1538void 1636void
1539ev_timer_stop (EV_P_ ev_timer *w) 1637ev_timer_stop (EV_P_ ev_timer *w)
1540{ 1638{
1541 ev_clear_pending (EV_A_ (W)w); 1639 clear_pending (EV_A_ (W)w);
1542 if (expect_false (!ev_is_active (w))) 1640 if (expect_false (!ev_is_active (w)))
1543 return; 1641 return;
1544 1642
1545 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1643 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1546 1644
1604} 1702}
1605 1703
1606void 1704void
1607ev_periodic_stop (EV_P_ ev_periodic *w) 1705ev_periodic_stop (EV_P_ ev_periodic *w)
1608{ 1706{
1609 ev_clear_pending (EV_A_ (W)w); 1707 clear_pending (EV_A_ (W)w);
1610 if (expect_false (!ev_is_active (w))) 1708 if (expect_false (!ev_is_active (w)))
1611 return; 1709 return;
1612 1710
1613 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1711 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1614 1712
1668} 1766}
1669 1767
1670void 1768void
1671ev_signal_stop (EV_P_ ev_signal *w) 1769ev_signal_stop (EV_P_ ev_signal *w)
1672{ 1770{
1673 ev_clear_pending (EV_A_ (W)w); 1771 clear_pending (EV_A_ (W)w);
1674 if (expect_false (!ev_is_active (w))) 1772 if (expect_false (!ev_is_active (w)))
1675 return; 1773 return;
1676 1774
1677 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1775 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1678 ev_stop (EV_A_ (W)w); 1776 ev_stop (EV_A_ (W)w);
1695} 1793}
1696 1794
1697void 1795void
1698ev_child_stop (EV_P_ ev_child *w) 1796ev_child_stop (EV_P_ ev_child *w)
1699{ 1797{
1700 ev_clear_pending (EV_A_ (W)w); 1798 clear_pending (EV_A_ (W)w);
1701 if (expect_false (!ev_is_active (w))) 1799 if (expect_false (!ev_is_active (w)))
1702 return; 1800 return;
1703 1801
1704 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1802 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1705 ev_stop (EV_A_ (W)w); 1803 ev_stop (EV_A_ (W)w);
1713# endif 1811# endif
1714 1812
1715#define DEF_STAT_INTERVAL 5.0074891 1813#define DEF_STAT_INTERVAL 5.0074891
1716#define MIN_STAT_INTERVAL 0.1074891 1814#define MIN_STAT_INTERVAL 0.1074891
1717 1815
1718void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); 1816static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
1719 1817
1720#if EV_USE_INOTIFY 1818#if EV_USE_INOTIFY
1721# define EV_INOTIFY_BUFSIZE ((PATH_MAX + sizeof (struct inotify_event)) + 2048) 1819# define EV_INOTIFY_BUFSIZE 8192
1722 1820
1723static void noinline 1821static void noinline
1724infy_add (EV_P_ ev_stat *w) 1822infy_add (EV_P_ ev_stat *w)
1725{ 1823{
1726 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); 1824 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD);
1728 if (w->wd < 0) 1826 if (w->wd < 0)
1729 { 1827 {
1730 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 1828 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
1731 1829
1732 /* monitor some parent directory for speedup hints */ 1830 /* monitor some parent directory for speedup hints */
1733 if (errno == ENOENT || errno == EACCES) 1831 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
1734 { 1832 {
1735 char path [PATH_MAX]; 1833 char path [4096];
1736 strcpy (path, w->path); 1834 strcpy (path, w->path);
1737 1835
1738 do 1836 do
1739 { 1837 {
1740 int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF 1838 int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF
1744 1842
1745 if (!pend) 1843 if (!pend)
1746 break; /* whoops, no '/', complain to your admin */ 1844 break; /* whoops, no '/', complain to your admin */
1747 1845
1748 *pend = 0; 1846 *pend = 0;
1749 w->wd = inotify_add_watch (fs_fd, path, IN_DELETE_SELF | IN_CREATE | IN_MOVED_TO | IN_MASK_ADD); 1847 w->wd = inotify_add_watch (fs_fd, path, mask);
1750 } 1848 }
1751 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 1849 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
1752 } 1850 }
1753 } 1851 }
1754 else 1852 else
1759} 1857}
1760 1858
1761static void noinline 1859static void noinline
1762infy_del (EV_P_ ev_stat *w) 1860infy_del (EV_P_ ev_stat *w)
1763{ 1861{
1764 WL w_;
1765 int slot; 1862 int slot;
1766 int wd = w->wd; 1863 int wd = w->wd;
1767 1864
1768 if (wd < 0) 1865 if (wd < 0)
1769 return; 1866 return;
1798 { 1895 {
1799 w->wd = -1; 1896 w->wd = -1;
1800 infy_add (EV_A_ w); /* re-add, no matter what */ 1897 infy_add (EV_A_ w); /* re-add, no matter what */
1801 } 1898 }
1802 1899
1803 stat_timer_cb (EV_P_ &w->timer, 0); 1900 stat_timer_cb (EV_A_ &w->timer, 0);
1804 } 1901 }
1805 } 1902 }
1806 } 1903 }
1807} 1904}
1808 1905
1832 ev_set_priority (&fs_w, EV_MAXPRI); 1929 ev_set_priority (&fs_w, EV_MAXPRI);
1833 ev_io_start (EV_A_ &fs_w); 1930 ev_io_start (EV_A_ &fs_w);
1834 } 1931 }
1835} 1932}
1836 1933
1934void inline_size
1935infy_fork (EV_P)
1936{
1937 int slot;
1938
1939 if (fs_fd < 0)
1940 return;
1941
1942 close (fs_fd);
1943 fs_fd = inotify_init ();
1944
1945 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1946 {
1947 WL w_ = fs_hash [slot].head;
1948 fs_hash [slot].head = 0;
1949
1950 while (w_)
1951 {
1952 ev_stat *w = (ev_stat *)w_;
1953 w_ = w_->next; /* lets us add this watcher */
1954
1955 w->wd = -1;
1956
1957 if (fs_fd >= 0)
1958 infy_add (EV_A_ w); /* re-add, no matter what */
1959 else
1960 ev_timer_start (EV_A_ &w->timer);
1961 }
1962
1963 }
1964}
1965
1837#endif 1966#endif
1838 1967
1839void 1968void
1840ev_stat_stat (EV_P_ ev_stat *w) 1969ev_stat_stat (EV_P_ ev_stat *w)
1841{ 1970{
1843 w->attr.st_nlink = 0; 1972 w->attr.st_nlink = 0;
1844 else if (!w->attr.st_nlink) 1973 else if (!w->attr.st_nlink)
1845 w->attr.st_nlink = 1; 1974 w->attr.st_nlink = 1;
1846} 1975}
1847 1976
1848void noinline 1977static void noinline
1849stat_timer_cb (EV_P_ ev_timer *w_, int revents) 1978stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1850{ 1979{
1851 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 1980 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1852 1981
1853 /* we copy this here each the time so that */ 1982 /* we copy this here each the time so that */
1854 /* prev has the old value when the callback gets invoked */ 1983 /* prev has the old value when the callback gets invoked */
1855 w->prev = w->attr; 1984 w->prev = w->attr;
1856 ev_stat_stat (EV_A_ w); 1985 ev_stat_stat (EV_A_ w);
1857 1986
1858 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) 1987 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
1988 if (
1989 w->prev.st_dev != w->attr.st_dev
1990 || w->prev.st_ino != w->attr.st_ino
1991 || w->prev.st_mode != w->attr.st_mode
1992 || w->prev.st_nlink != w->attr.st_nlink
1993 || w->prev.st_uid != w->attr.st_uid
1994 || w->prev.st_gid != w->attr.st_gid
1995 || w->prev.st_rdev != w->attr.st_rdev
1996 || w->prev.st_size != w->attr.st_size
1997 || w->prev.st_atime != w->attr.st_atime
1998 || w->prev.st_mtime != w->attr.st_mtime
1999 || w->prev.st_ctime != w->attr.st_ctime
1859 { 2000 ) {
1860 #if EV_USE_INOTIFY 2001 #if EV_USE_INOTIFY
1861 infy_del (EV_A_ w); 2002 infy_del (EV_A_ w);
1862 infy_add (EV_A_ w); 2003 infy_add (EV_A_ w);
1863 ev_stat_stat (EV_A_ w); /* avoid race... */ 2004 ev_stat_stat (EV_A_ w); /* avoid race... */
1864 #endif 2005 #endif
1898} 2039}
1899 2040
1900void 2041void
1901ev_stat_stop (EV_P_ ev_stat *w) 2042ev_stat_stop (EV_P_ ev_stat *w)
1902{ 2043{
1903 ev_clear_pending (EV_A_ (W)w); 2044 clear_pending (EV_A_ (W)w);
1904 if (expect_false (!ev_is_active (w))) 2045 if (expect_false (!ev_is_active (w)))
1905 return; 2046 return;
1906 2047
1907#if EV_USE_INOTIFY 2048#if EV_USE_INOTIFY
1908 infy_del (EV_A_ w); 2049 infy_del (EV_A_ w);
1911 2052
1912 ev_stop (EV_A_ (W)w); 2053 ev_stop (EV_A_ (W)w);
1913} 2054}
1914#endif 2055#endif
1915 2056
2057#if EV_IDLE_ENABLE
1916void 2058void
1917ev_idle_start (EV_P_ ev_idle *w) 2059ev_idle_start (EV_P_ ev_idle *w)
1918{ 2060{
1919 if (expect_false (ev_is_active (w))) 2061 if (expect_false (ev_is_active (w)))
1920 return; 2062 return;
1921 2063
2064 pri_adjust (EV_A_ (W)w);
2065
2066 {
2067 int active = ++idlecnt [ABSPRI (w)];
2068
2069 ++idleall;
1922 ev_start (EV_A_ (W)w, ++idlecnt); 2070 ev_start (EV_A_ (W)w, active);
2071
1923 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2072 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1924 idles [idlecnt - 1] = w; 2073 idles [ABSPRI (w)][active - 1] = w;
2074 }
1925} 2075}
1926 2076
1927void 2077void
1928ev_idle_stop (EV_P_ ev_idle *w) 2078ev_idle_stop (EV_P_ ev_idle *w)
1929{ 2079{
1930 ev_clear_pending (EV_A_ (W)w); 2080 clear_pending (EV_A_ (W)w);
1931 if (expect_false (!ev_is_active (w))) 2081 if (expect_false (!ev_is_active (w)))
1932 return; 2082 return;
1933 2083
1934 { 2084 {
1935 int active = ((W)w)->active; 2085 int active = ((W)w)->active;
1936 idles [active - 1] = idles [--idlecnt]; 2086
2087 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
1937 ((W)idles [active - 1])->active = active; 2088 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2089
2090 ev_stop (EV_A_ (W)w);
2091 --idleall;
1938 } 2092 }
1939
1940 ev_stop (EV_A_ (W)w);
1941} 2093}
2094#endif
1942 2095
1943void 2096void
1944ev_prepare_start (EV_P_ ev_prepare *w) 2097ev_prepare_start (EV_P_ ev_prepare *w)
1945{ 2098{
1946 if (expect_false (ev_is_active (w))) 2099 if (expect_false (ev_is_active (w)))
1952} 2105}
1953 2106
1954void 2107void
1955ev_prepare_stop (EV_P_ ev_prepare *w) 2108ev_prepare_stop (EV_P_ ev_prepare *w)
1956{ 2109{
1957 ev_clear_pending (EV_A_ (W)w); 2110 clear_pending (EV_A_ (W)w);
1958 if (expect_false (!ev_is_active (w))) 2111 if (expect_false (!ev_is_active (w)))
1959 return; 2112 return;
1960 2113
1961 { 2114 {
1962 int active = ((W)w)->active; 2115 int active = ((W)w)->active;
1979} 2132}
1980 2133
1981void 2134void
1982ev_check_stop (EV_P_ ev_check *w) 2135ev_check_stop (EV_P_ ev_check *w)
1983{ 2136{
1984 ev_clear_pending (EV_A_ (W)w); 2137 clear_pending (EV_A_ (W)w);
1985 if (expect_false (!ev_is_active (w))) 2138 if (expect_false (!ev_is_active (w)))
1986 return; 2139 return;
1987 2140
1988 { 2141 {
1989 int active = ((W)w)->active; 2142 int active = ((W)w)->active;
2031} 2184}
2032 2185
2033void 2186void
2034ev_embed_stop (EV_P_ ev_embed *w) 2187ev_embed_stop (EV_P_ ev_embed *w)
2035{ 2188{
2036 ev_clear_pending (EV_A_ (W)w); 2189 clear_pending (EV_A_ (W)w);
2037 if (expect_false (!ev_is_active (w))) 2190 if (expect_false (!ev_is_active (w)))
2038 return; 2191 return;
2039 2192
2040 ev_io_stop (EV_A_ &w->io); 2193 ev_io_stop (EV_A_ &w->io);
2041 2194
2056} 2209}
2057 2210
2058void 2211void
2059ev_fork_stop (EV_P_ ev_fork *w) 2212ev_fork_stop (EV_P_ ev_fork *w)
2060{ 2213{
2061 ev_clear_pending (EV_A_ (W)w); 2214 clear_pending (EV_A_ (W)w);
2062 if (expect_false (!ev_is_active (w))) 2215 if (expect_false (!ev_is_active (w)))
2063 return; 2216 return;
2064 2217
2065 { 2218 {
2066 int active = ((W)w)->active; 2219 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines