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

Comparing libev/ev.c (file contents):
Revision 1.153 by root, Wed Nov 28 11:41:18 2007 UTC vs.
Revision 1.167 by root, Sat Dec 8 04:02:31 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/*****************************************************************************/
589static void noinline 611static void noinline
590fd_rearm_all (EV_P) 612fd_rearm_all (EV_P)
591{ 613{
592 int fd; 614 int fd;
593 615
594 /* this should be highly optimised to not do anything but set a flag */
595 for (fd = 0; fd < anfdmax; ++fd) 616 for (fd = 0; fd < anfdmax; ++fd)
596 if (anfds [fd].events) 617 if (anfds [fd].events)
597 { 618 {
598 anfds [fd].events = 0; 619 anfds [fd].events = 0;
599 fd_change (EV_A_ fd); 620 fd_change (EV_A_ fd);
765 ev_child *w; 786 ev_child *w;
766 787
767 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 788 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
768 if (w->pid == pid || !w->pid) 789 if (w->pid == pid || !w->pid)
769 { 790 {
770 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 791 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
771 w->rpid = pid; 792 w->rpid = pid;
772 w->rstatus = status; 793 w->rstatus = status;
773 ev_feed_event (EV_A_ (W)w, EV_CHILD); 794 ev_feed_event (EV_A_ (W)w, EV_CHILD);
774 } 795 }
775} 796}
776 797
777#ifndef WCONTINUED 798#ifndef WCONTINUED
887ev_backend (EV_P) 908ev_backend (EV_P)
888{ 909{
889 return backend; 910 return backend;
890} 911}
891 912
913unsigned int
914ev_loop_count (EV_P)
915{
916 return loop_count;
917}
918
892static void noinline 919static void noinline
893loop_init (EV_P_ unsigned int flags) 920loop_init (EV_P_ unsigned int flags)
894{ 921{
895 if (!backend) 922 if (!backend)
896 { 923 {
905 ev_rt_now = ev_time (); 932 ev_rt_now = ev_time ();
906 mn_now = get_clock (); 933 mn_now = get_clock ();
907 now_floor = mn_now; 934 now_floor = mn_now;
908 rtmn_diff = ev_rt_now - mn_now; 935 rtmn_diff = ev_rt_now - mn_now;
909 936
937 /* pid check not overridable via env */
938#ifndef _WIN32
939 if (flags & EVFLAG_FORKCHECK)
940 curpid = getpid ();
941#endif
942
910 if (!(flags & EVFLAG_NOENV) 943 if (!(flags & EVFLAG_NOENV)
911 && !enable_secure () 944 && !enable_secure ()
912 && getenv ("LIBEV_FLAGS")) 945 && getenv ("LIBEV_FLAGS"))
913 flags = atoi (getenv ("LIBEV_FLAGS")); 946 flags = atoi (getenv ("LIBEV_FLAGS"));
914 947
970#if EV_USE_SELECT 1003#if EV_USE_SELECT
971 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1004 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
972#endif 1005#endif
973 1006
974 for (i = NUMPRI; i--; ) 1007 for (i = NUMPRI; i--; )
1008 {
975 array_free (pending, [i]); 1009 array_free (pending, [i]);
1010#if EV_IDLE_ENABLE
1011 array_free (idle, [i]);
1012#endif
1013 }
976 1014
977 /* have to use the microsoft-never-gets-it-right macro */ 1015 /* have to use the microsoft-never-gets-it-right macro */
978 array_free (fdchange, EMPTY0); 1016 array_free (fdchange, EMPTY);
979 array_free (timer, EMPTY0); 1017 array_free (timer, EMPTY);
980#if EV_PERIODIC_ENABLE 1018#if EV_PERIODIC_ENABLE
981 array_free (periodic, EMPTY0); 1019 array_free (periodic, EMPTY);
982#endif 1020#endif
983 array_free (idle, EMPTY0);
984 array_free (prepare, EMPTY0); 1021 array_free (prepare, EMPTY);
985 array_free (check, EMPTY0); 1022 array_free (check, EMPTY);
986 1023
987 backend = 0; 1024 backend = 0;
988} 1025}
1026
1027void inline_size infy_fork (EV_P);
989 1028
990void inline_size 1029void inline_size
991loop_fork (EV_P) 1030loop_fork (EV_P)
992{ 1031{
993#if EV_USE_PORT 1032#if EV_USE_PORT
996#if EV_USE_KQUEUE 1035#if EV_USE_KQUEUE
997 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 1036 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
998#endif 1037#endif
999#if EV_USE_EPOLL 1038#if EV_USE_EPOLL
1000 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 1039 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
1040#endif
1041#if EV_USE_INOTIFY
1042 infy_fork (EV_A);
1001#endif 1043#endif
1002 1044
1003 if (ev_is_active (&sigev)) 1045 if (ev_is_active (&sigev))
1004 { 1046 {
1005 /* default loop */ 1047 /* default loop */
1120 if (backend) 1162 if (backend)
1121 postfork = 1; 1163 postfork = 1;
1122} 1164}
1123 1165
1124/*****************************************************************************/ 1166/*****************************************************************************/
1125
1126int inline_size
1127any_pending (EV_P)
1128{
1129 int pri;
1130
1131 for (pri = NUMPRI; pri--; )
1132 if (pendingcnt [pri])
1133 return 1;
1134
1135 return 0;
1136}
1137 1167
1138void inline_speed 1168void inline_speed
1139call_pending (EV_P) 1169call_pending (EV_P)
1140{ 1170{
1141 int pri; 1171 int pri;
1232 for (i = periodiccnt >> 1; i--; ) 1262 for (i = periodiccnt >> 1; i--; )
1233 downheap ((WT *)periodics, periodiccnt, i); 1263 downheap ((WT *)periodics, periodiccnt, i);
1234} 1264}
1235#endif 1265#endif
1236 1266
1267#if EV_IDLE_ENABLE
1268void inline_size
1269idle_reify (EV_P)
1270{
1271 if (expect_false (idleall))
1272 {
1273 int pri;
1274
1275 for (pri = NUMPRI; pri--; )
1276 {
1277 if (pendingcnt [pri])
1278 break;
1279
1280 if (idlecnt [pri])
1281 {
1282 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1283 break;
1284 }
1285 }
1286 }
1287}
1288#endif
1289
1237int inline_size 1290int inline_size
1238time_update_monotonic (EV_P) 1291time_update_monotonic (EV_P)
1239{ 1292{
1240 mn_now = get_clock (); 1293 mn_now = get_clock ();
1241 1294
1265 ev_tstamp odiff = rtmn_diff; 1318 ev_tstamp odiff = rtmn_diff;
1266 1319
1267 /* loop a few times, before making important decisions. 1320 /* loop a few times, before making important decisions.
1268 * on the choice of "4": one iteration isn't enough, 1321 * on the choice of "4": one iteration isn't enough,
1269 * in case we get preempted during the calls to 1322 * in case we get preempted during the calls to
1270 * ev_time and get_clock. a second call is almost guarenteed 1323 * ev_time and get_clock. a second call is almost guaranteed
1271 * to succeed in that case, though. and looping a few more times 1324 * 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 1325 * doesn't hurt either as we only do this on time-jumps or
1273 * in the unlikely event of getting preempted here. 1326 * in the unlikely event of having been preempted here.
1274 */ 1327 */
1275 for (i = 4; --i; ) 1328 for (i = 4; --i; )
1276 { 1329 {
1277 rtmn_diff = ev_rt_now - mn_now; 1330 rtmn_diff = ev_rt_now - mn_now;
1278 1331
1300 { 1353 {
1301#if EV_PERIODIC_ENABLE 1354#if EV_PERIODIC_ENABLE
1302 periodics_reschedule (EV_A); 1355 periodics_reschedule (EV_A);
1303#endif 1356#endif
1304 1357
1305 /* adjust timers. this is easy, as the offset is the same for all */ 1358 /* adjust timers. this is easy, as the offset is the same for all of them */
1306 for (i = 0; i < timercnt; ++i) 1359 for (i = 0; i < timercnt; ++i)
1307 ((WT)timers [i])->at += ev_rt_now - mn_now; 1360 ((WT)timers [i])->at += ev_rt_now - mn_now;
1308 } 1361 }
1309 1362
1310 mn_now = ev_rt_now; 1363 mn_now = ev_rt_now;
1330{ 1383{
1331 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1384 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1332 ? EVUNLOOP_ONE 1385 ? EVUNLOOP_ONE
1333 : EVUNLOOP_CANCEL; 1386 : EVUNLOOP_CANCEL;
1334 1387
1335 while (activecnt) 1388 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1389
1390 do
1336 { 1391 {
1337 /* we might have forked, so reify kernel state if necessary */ 1392#ifndef _WIN32
1393 if (expect_false (curpid)) /* penalise the forking check even more */
1394 if (expect_false (getpid () != curpid))
1395 {
1396 curpid = getpid ();
1397 postfork = 1;
1398 }
1399#endif
1400
1338 #if EV_FORK_ENABLE 1401#if EV_FORK_ENABLE
1402 /* we might have forked, so queue fork handlers */
1339 if (expect_false (postfork)) 1403 if (expect_false (postfork))
1340 if (forkcnt) 1404 if (forkcnt)
1341 { 1405 {
1342 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1406 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1343 call_pending (EV_A); 1407 call_pending (EV_A);
1344 } 1408 }
1345 #endif 1409#endif
1346 1410
1347 /* queue check watchers (and execute them) */ 1411 /* queue check watchers (and execute them) */
1348 if (expect_false (preparecnt)) 1412 if (expect_false (preparecnt))
1349 { 1413 {
1350 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1414 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1351 call_pending (EV_A); 1415 call_pending (EV_A);
1352 } 1416 }
1353 1417
1418 if (expect_false (!activecnt))
1419 break;
1420
1354 /* we might have forked, so reify kernel state if necessary */ 1421 /* we might have forked, so reify kernel state if necessary */
1355 if (expect_false (postfork)) 1422 if (expect_false (postfork))
1356 loop_fork (EV_A); 1423 loop_fork (EV_A);
1357 1424
1358 /* update fd-related kernel structures */ 1425 /* update fd-related kernel structures */
1359 fd_reify (EV_A); 1426 fd_reify (EV_A);
1360 1427
1361 /* calculate blocking time */ 1428 /* calculate blocking time */
1362 { 1429 {
1363 double block; 1430 ev_tstamp block;
1364 1431
1365 if (flags & EVLOOP_NONBLOCK || idlecnt) 1432 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1366 block = 0.; /* do not block at all */ 1433 block = 0.; /* do not block at all */
1367 else 1434 else
1368 { 1435 {
1369 /* update time to cancel out callback processing overhead */ 1436 /* update time to cancel out callback processing overhead */
1370#if EV_USE_MONOTONIC 1437#if EV_USE_MONOTONIC
1394#endif 1461#endif
1395 1462
1396 if (expect_false (block < 0.)) block = 0.; 1463 if (expect_false (block < 0.)) block = 0.;
1397 } 1464 }
1398 1465
1466 ++loop_count;
1399 backend_poll (EV_A_ block); 1467 backend_poll (EV_A_ block);
1400 } 1468 }
1401 1469
1402 /* update ev_rt_now, do magic */ 1470 /* update ev_rt_now, do magic */
1403 time_update (EV_A); 1471 time_update (EV_A);
1406 timers_reify (EV_A); /* relative timers called last */ 1474 timers_reify (EV_A); /* relative timers called last */
1407#if EV_PERIODIC_ENABLE 1475#if EV_PERIODIC_ENABLE
1408 periodics_reify (EV_A); /* absolute timers called first */ 1476 periodics_reify (EV_A); /* absolute timers called first */
1409#endif 1477#endif
1410 1478
1479#if EV_IDLE_ENABLE
1411 /* queue idle watchers unless other events are pending */ 1480 /* queue idle watchers unless other events are pending */
1412 if (idlecnt && !any_pending (EV_A)) 1481 idle_reify (EV_A);
1413 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1482#endif
1414 1483
1415 /* queue check watchers, to be executed first */ 1484 /* queue check watchers, to be executed first */
1416 if (expect_false (checkcnt)) 1485 if (expect_false (checkcnt))
1417 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1486 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1418 1487
1419 call_pending (EV_A); 1488 call_pending (EV_A);
1420 1489
1421 if (expect_false (loop_done))
1422 break;
1423 } 1490 }
1491 while (expect_true (activecnt && !loop_done));
1424 1492
1425 if (loop_done == EVUNLOOP_ONE) 1493 if (loop_done == EVUNLOOP_ONE)
1426 loop_done = EVUNLOOP_CANCEL; 1494 loop_done = EVUNLOOP_CANCEL;
1427} 1495}
1428 1496
1455 head = &(*head)->next; 1523 head = &(*head)->next;
1456 } 1524 }
1457} 1525}
1458 1526
1459void inline_speed 1527void inline_speed
1460ev_clear_pending (EV_P_ W w) 1528clear_pending (EV_P_ W w)
1461{ 1529{
1462 if (w->pending) 1530 if (w->pending)
1463 { 1531 {
1464 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1532 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1465 w->pending = 0; 1533 w->pending = 0;
1466 } 1534 }
1467} 1535}
1468 1536
1537int
1538ev_clear_pending (EV_P_ void *w)
1539{
1540 W w_ = (W)w;
1541 int pending = w_->pending;
1542
1543 if (!pending)
1544 return 0;
1545
1546 w_->pending = 0;
1547 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1548 p->w = 0;
1549
1550 return p->events;
1551}
1552
1553void inline_size
1554pri_adjust (EV_P_ W w)
1555{
1556 int pri = w->priority;
1557 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1558 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1559 w->priority = pri;
1560}
1561
1469void inline_speed 1562void inline_speed
1470ev_start (EV_P_ W w, int active) 1563ev_start (EV_P_ W w, int active)
1471{ 1564{
1472 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1565 pri_adjust (EV_A_ w);
1473 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1474
1475 w->active = active; 1566 w->active = active;
1476 ev_ref (EV_A); 1567 ev_ref (EV_A);
1477} 1568}
1478 1569
1479void inline_size 1570void inline_size
1503} 1594}
1504 1595
1505void 1596void
1506ev_io_stop (EV_P_ ev_io *w) 1597ev_io_stop (EV_P_ ev_io *w)
1507{ 1598{
1508 ev_clear_pending (EV_A_ (W)w); 1599 clear_pending (EV_A_ (W)w);
1509 if (expect_false (!ev_is_active (w))) 1600 if (expect_false (!ev_is_active (w)))
1510 return; 1601 return;
1511 1602
1512 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1603 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1513 1604
1536} 1627}
1537 1628
1538void 1629void
1539ev_timer_stop (EV_P_ ev_timer *w) 1630ev_timer_stop (EV_P_ ev_timer *w)
1540{ 1631{
1541 ev_clear_pending (EV_A_ (W)w); 1632 clear_pending (EV_A_ (W)w);
1542 if (expect_false (!ev_is_active (w))) 1633 if (expect_false (!ev_is_active (w)))
1543 return; 1634 return;
1544 1635
1545 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1636 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1546 1637
1604} 1695}
1605 1696
1606void 1697void
1607ev_periodic_stop (EV_P_ ev_periodic *w) 1698ev_periodic_stop (EV_P_ ev_periodic *w)
1608{ 1699{
1609 ev_clear_pending (EV_A_ (W)w); 1700 clear_pending (EV_A_ (W)w);
1610 if (expect_false (!ev_is_active (w))) 1701 if (expect_false (!ev_is_active (w)))
1611 return; 1702 return;
1612 1703
1613 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1704 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1614 1705
1668} 1759}
1669 1760
1670void 1761void
1671ev_signal_stop (EV_P_ ev_signal *w) 1762ev_signal_stop (EV_P_ ev_signal *w)
1672{ 1763{
1673 ev_clear_pending (EV_A_ (W)w); 1764 clear_pending (EV_A_ (W)w);
1674 if (expect_false (!ev_is_active (w))) 1765 if (expect_false (!ev_is_active (w)))
1675 return; 1766 return;
1676 1767
1677 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1768 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1678 ev_stop (EV_A_ (W)w); 1769 ev_stop (EV_A_ (W)w);
1695} 1786}
1696 1787
1697void 1788void
1698ev_child_stop (EV_P_ ev_child *w) 1789ev_child_stop (EV_P_ ev_child *w)
1699{ 1790{
1700 ev_clear_pending (EV_A_ (W)w); 1791 clear_pending (EV_A_ (W)w);
1701 if (expect_false (!ev_is_active (w))) 1792 if (expect_false (!ev_is_active (w)))
1702 return; 1793 return;
1703 1794
1704 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1795 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1705 ev_stop (EV_A_ (W)w); 1796 ev_stop (EV_A_ (W)w);
1713# endif 1804# endif
1714 1805
1715#define DEF_STAT_INTERVAL 5.0074891 1806#define DEF_STAT_INTERVAL 5.0074891
1716#define MIN_STAT_INTERVAL 0.1074891 1807#define MIN_STAT_INTERVAL 0.1074891
1717 1808
1718void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); 1809static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
1719 1810
1720#if EV_USE_INOTIFY 1811#if EV_USE_INOTIFY
1721# define EV_INOTIFY_BUFSIZE 8192 1812# define EV_INOTIFY_BUFSIZE 8192
1722 1813
1723static void noinline 1814static void noinline
1831 ev_set_priority (&fs_w, EV_MAXPRI); 1922 ev_set_priority (&fs_w, EV_MAXPRI);
1832 ev_io_start (EV_A_ &fs_w); 1923 ev_io_start (EV_A_ &fs_w);
1833 } 1924 }
1834} 1925}
1835 1926
1927void inline_size
1928infy_fork (EV_P)
1929{
1930 int slot;
1931
1932 if (fs_fd < 0)
1933 return;
1934
1935 close (fs_fd);
1936 fs_fd = inotify_init ();
1937
1938 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1939 {
1940 WL w_ = fs_hash [slot].head;
1941 fs_hash [slot].head = 0;
1942
1943 while (w_)
1944 {
1945 ev_stat *w = (ev_stat *)w_;
1946 w_ = w_->next; /* lets us add this watcher */
1947
1948 w->wd = -1;
1949
1950 if (fs_fd >= 0)
1951 infy_add (EV_A_ w); /* re-add, no matter what */
1952 else
1953 ev_timer_start (EV_A_ &w->timer);
1954 }
1955
1956 }
1957}
1958
1836#endif 1959#endif
1837 1960
1838void 1961void
1839ev_stat_stat (EV_P_ ev_stat *w) 1962ev_stat_stat (EV_P_ ev_stat *w)
1840{ 1963{
1842 w->attr.st_nlink = 0; 1965 w->attr.st_nlink = 0;
1843 else if (!w->attr.st_nlink) 1966 else if (!w->attr.st_nlink)
1844 w->attr.st_nlink = 1; 1967 w->attr.st_nlink = 1;
1845} 1968}
1846 1969
1847void noinline 1970static void noinline
1848stat_timer_cb (EV_P_ ev_timer *w_, int revents) 1971stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1849{ 1972{
1850 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 1973 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1851 1974
1852 /* we copy this here each the time so that */ 1975 /* we copy this here each the time so that */
1853 /* prev has the old value when the callback gets invoked */ 1976 /* prev has the old value when the callback gets invoked */
1854 w->prev = w->attr; 1977 w->prev = w->attr;
1855 ev_stat_stat (EV_A_ w); 1978 ev_stat_stat (EV_A_ w);
1856 1979
1857 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) 1980 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
1981 if (
1982 w->prev.st_dev != w->attr.st_dev
1983 || w->prev.st_ino != w->attr.st_ino
1984 || w->prev.st_mode != w->attr.st_mode
1985 || w->prev.st_nlink != w->attr.st_nlink
1986 || w->prev.st_uid != w->attr.st_uid
1987 || w->prev.st_gid != w->attr.st_gid
1988 || w->prev.st_rdev != w->attr.st_rdev
1989 || w->prev.st_size != w->attr.st_size
1990 || w->prev.st_atime != w->attr.st_atime
1991 || w->prev.st_mtime != w->attr.st_mtime
1992 || w->prev.st_ctime != w->attr.st_ctime
1858 { 1993 ) {
1859 #if EV_USE_INOTIFY 1994 #if EV_USE_INOTIFY
1860 infy_del (EV_A_ w); 1995 infy_del (EV_A_ w);
1861 infy_add (EV_A_ w); 1996 infy_add (EV_A_ w);
1862 ev_stat_stat (EV_A_ w); /* avoid race... */ 1997 ev_stat_stat (EV_A_ w); /* avoid race... */
1863 #endif 1998 #endif
1897} 2032}
1898 2033
1899void 2034void
1900ev_stat_stop (EV_P_ ev_stat *w) 2035ev_stat_stop (EV_P_ ev_stat *w)
1901{ 2036{
1902 ev_clear_pending (EV_A_ (W)w); 2037 clear_pending (EV_A_ (W)w);
1903 if (expect_false (!ev_is_active (w))) 2038 if (expect_false (!ev_is_active (w)))
1904 return; 2039 return;
1905 2040
1906#if EV_USE_INOTIFY 2041#if EV_USE_INOTIFY
1907 infy_del (EV_A_ w); 2042 infy_del (EV_A_ w);
1910 2045
1911 ev_stop (EV_A_ (W)w); 2046 ev_stop (EV_A_ (W)w);
1912} 2047}
1913#endif 2048#endif
1914 2049
2050#if EV_IDLE_ENABLE
1915void 2051void
1916ev_idle_start (EV_P_ ev_idle *w) 2052ev_idle_start (EV_P_ ev_idle *w)
1917{ 2053{
1918 if (expect_false (ev_is_active (w))) 2054 if (expect_false (ev_is_active (w)))
1919 return; 2055 return;
1920 2056
2057 pri_adjust (EV_A_ (W)w);
2058
2059 {
2060 int active = ++idlecnt [ABSPRI (w)];
2061
2062 ++idleall;
1921 ev_start (EV_A_ (W)w, ++idlecnt); 2063 ev_start (EV_A_ (W)w, active);
2064
1922 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2065 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1923 idles [idlecnt - 1] = w; 2066 idles [ABSPRI (w)][active - 1] = w;
2067 }
1924} 2068}
1925 2069
1926void 2070void
1927ev_idle_stop (EV_P_ ev_idle *w) 2071ev_idle_stop (EV_P_ ev_idle *w)
1928{ 2072{
1929 ev_clear_pending (EV_A_ (W)w); 2073 clear_pending (EV_A_ (W)w);
1930 if (expect_false (!ev_is_active (w))) 2074 if (expect_false (!ev_is_active (w)))
1931 return; 2075 return;
1932 2076
1933 { 2077 {
1934 int active = ((W)w)->active; 2078 int active = ((W)w)->active;
1935 idles [active - 1] = idles [--idlecnt]; 2079
2080 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
1936 ((W)idles [active - 1])->active = active; 2081 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2082
2083 ev_stop (EV_A_ (W)w);
2084 --idleall;
1937 } 2085 }
1938
1939 ev_stop (EV_A_ (W)w);
1940} 2086}
2087#endif
1941 2088
1942void 2089void
1943ev_prepare_start (EV_P_ ev_prepare *w) 2090ev_prepare_start (EV_P_ ev_prepare *w)
1944{ 2091{
1945 if (expect_false (ev_is_active (w))) 2092 if (expect_false (ev_is_active (w)))
1951} 2098}
1952 2099
1953void 2100void
1954ev_prepare_stop (EV_P_ ev_prepare *w) 2101ev_prepare_stop (EV_P_ ev_prepare *w)
1955{ 2102{
1956 ev_clear_pending (EV_A_ (W)w); 2103 clear_pending (EV_A_ (W)w);
1957 if (expect_false (!ev_is_active (w))) 2104 if (expect_false (!ev_is_active (w)))
1958 return; 2105 return;
1959 2106
1960 { 2107 {
1961 int active = ((W)w)->active; 2108 int active = ((W)w)->active;
1978} 2125}
1979 2126
1980void 2127void
1981ev_check_stop (EV_P_ ev_check *w) 2128ev_check_stop (EV_P_ ev_check *w)
1982{ 2129{
1983 ev_clear_pending (EV_A_ (W)w); 2130 clear_pending (EV_A_ (W)w);
1984 if (expect_false (!ev_is_active (w))) 2131 if (expect_false (!ev_is_active (w)))
1985 return; 2132 return;
1986 2133
1987 { 2134 {
1988 int active = ((W)w)->active; 2135 int active = ((W)w)->active;
2030} 2177}
2031 2178
2032void 2179void
2033ev_embed_stop (EV_P_ ev_embed *w) 2180ev_embed_stop (EV_P_ ev_embed *w)
2034{ 2181{
2035 ev_clear_pending (EV_A_ (W)w); 2182 clear_pending (EV_A_ (W)w);
2036 if (expect_false (!ev_is_active (w))) 2183 if (expect_false (!ev_is_active (w)))
2037 return; 2184 return;
2038 2185
2039 ev_io_stop (EV_A_ &w->io); 2186 ev_io_stop (EV_A_ &w->io);
2040 2187
2055} 2202}
2056 2203
2057void 2204void
2058ev_fork_stop (EV_P_ ev_fork *w) 2205ev_fork_stop (EV_P_ ev_fork *w)
2059{ 2206{
2060 ev_clear_pending (EV_A_ (W)w); 2207 clear_pending (EV_A_ (W)w);
2061 if (expect_false (!ev_is_active (w))) 2208 if (expect_false (!ev_is_active (w)))
2062 return; 2209 return;
2063 2210
2064 { 2211 {
2065 int active = ((W)w)->active; 2212 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines