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

Comparing libev/ev.c (file contents):
Revision 1.154 by root, Wed Nov 28 11:53:37 2007 UTC vs.
Revision 1.169 by root, Sat Dec 8 14:27:39 2007 UTC

222#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 222#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
223/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 223/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
224 224
225#if __GNUC__ >= 3 225#if __GNUC__ >= 3
226# define expect(expr,value) __builtin_expect ((expr),(value)) 226# define expect(expr,value) __builtin_expect ((expr),(value))
227# define inline_size static inline /* inline for codesize */
228# if EV_MINIMAL
229# define noinline __attribute__ ((noinline)) 227# define noinline __attribute__ ((noinline))
230# define inline_speed static noinline
231# else
232# define noinline
233# define inline_speed static inline
234# endif
235#else 228#else
236# define expect(expr,value) (expr) 229# define expect(expr,value) (expr)
237# define inline_speed static
238# define inline_size static
239# define noinline 230# define noinline
231# if __STDC_VERSION__ < 199901L
232# define inline
233# endif
240#endif 234#endif
241 235
242#define expect_false(expr) expect ((expr) != 0, 0) 236#define expect_false(expr) expect ((expr) != 0, 0)
243#define expect_true(expr) expect ((expr) != 0, 1) 237#define expect_true(expr) expect ((expr) != 0, 1)
238#define inline_size static inline
239
240#if EV_MINIMAL
241# define inline_speed static noinline
242#else
243# define inline_speed static inline
244#endif
244 245
245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 246#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
246#define ABSPRI(w) ((w)->priority - EV_MINPRI) 247#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
247 248
248#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 249#define EMPTY /* required for microsofts broken pseudo-c compiler */
249#define EMPTY2(a,b) /* used to suppress some warnings */ 250#define EMPTY2(a,b) /* used to suppress some warnings */
250 251
251typedef ev_watcher *W; 252typedef ev_watcher *W;
252typedef ev_watcher_list *WL; 253typedef ev_watcher_list *WL;
253typedef ev_watcher_time *WT; 254typedef ev_watcher_time *WT;
281 perror (msg); 282 perror (msg);
282 abort (); 283 abort ();
283 } 284 }
284} 285}
285 286
286static void *(*alloc)(void *ptr, size_t size) = realloc; 287static void *(*alloc)(void *ptr, long size);
287 288
288void 289void
289ev_set_allocator (void *(*cb)(void *ptr, size_t size)) 290ev_set_allocator (void *(*cb)(void *ptr, long size))
290{ 291{
291 alloc = cb; 292 alloc = cb;
292} 293}
293 294
294inline_speed void * 295inline_speed void *
295ev_realloc (void *ptr, size_t size) 296ev_realloc (void *ptr, long size)
296{ 297{
297 ptr = alloc (ptr, size); 298 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
298 299
299 if (!ptr && size) 300 if (!ptr && size)
300 { 301 {
301 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", (long)size); 302 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
302 abort (); 303 abort ();
303 } 304 }
304 305
305 return ptr; 306 return ptr;
306} 307}
324{ 325{
325 W w; 326 W w;
326 int events; 327 int events;
327} ANPENDING; 328} ANPENDING;
328 329
330#if EV_USE_INOTIFY
329typedef struct 331typedef struct
330{ 332{
331#if EV_USE_INOTIFY
332 WL head; 333 WL head;
333#endif
334} ANFS; 334} ANFS;
335#endif
335 336
336#if EV_MULTIPLICITY 337#if EV_MULTIPLICITY
337 338
338 struct ev_loop 339 struct ev_loop
339 { 340 {
396{ 397{
397 return ev_rt_now; 398 return ev_rt_now;
398} 399}
399#endif 400#endif
400 401
401#define array_roundsize(type,n) (((n) | 4) & ~3) 402int inline_size
403array_nextsize (int elem, int cur, int cnt)
404{
405 int ncur = cur + 1;
406
407 do
408 ncur <<= 1;
409 while (cnt > ncur);
410
411 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
412 if (elem * ncur > 4096)
413 {
414 ncur *= elem;
415 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
416 ncur = ncur - sizeof (void *) * 4;
417 ncur /= elem;
418 }
419
420 return ncur;
421}
422
423inline_speed void *
424array_realloc (int elem, void *base, int *cur, int cnt)
425{
426 *cur = array_nextsize (elem, *cur, cnt);
427 return ev_realloc (base, elem * *cur);
428}
402 429
403#define array_needsize(type,base,cur,cnt,init) \ 430#define array_needsize(type,base,cur,cnt,init) \
404 if (expect_false ((cnt) > cur)) \ 431 if (expect_false ((cnt) > (cur))) \
405 { \ 432 { \
406 int newcnt = cur; \ 433 int ocur_ = (cur); \
407 do \ 434 (base) = (type *)array_realloc \
408 { \ 435 (sizeof (type), (base), &(cur), (cnt)); \
409 newcnt = array_roundsize (type, newcnt << 1); \ 436 init ((base) + (ocur_), (cur) - ocur_); \
410 } \
411 while ((cnt) > newcnt); \
412 \
413 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
414 init (base + cur, newcnt - cur); \
415 cur = newcnt; \
416 } 437 }
417 438
439#if 0
418#define array_slim(type,stem) \ 440#define array_slim(type,stem) \
419 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 441 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
420 { \ 442 { \
421 stem ## max = array_roundsize (stem ## cnt >> 1); \ 443 stem ## max = array_roundsize (stem ## cnt >> 1); \
422 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 444 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
423 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 445 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
424 } 446 }
447#endif
425 448
426#define array_free(stem, idx) \ 449#define array_free(stem, idx) \
427 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 450 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
428 451
429/*****************************************************************************/ 452/*****************************************************************************/
485} 508}
486 509
487void 510void
488ev_feed_fd_event (EV_P_ int fd, int revents) 511ev_feed_fd_event (EV_P_ int fd, int revents)
489{ 512{
513 if (fd >= 0 && fd < anfdmax)
490 fd_event (EV_A_ fd, revents); 514 fd_event (EV_A_ fd, revents);
491} 515}
492 516
493void inline_size 517void inline_size
494fd_reify (EV_P) 518fd_reify (EV_P)
495{ 519{
589static void noinline 613static void noinline
590fd_rearm_all (EV_P) 614fd_rearm_all (EV_P)
591{ 615{
592 int fd; 616 int fd;
593 617
594 /* this should be highly optimised to not do anything but set a flag */
595 for (fd = 0; fd < anfdmax; ++fd) 618 for (fd = 0; fd < anfdmax; ++fd)
596 if (anfds [fd].events) 619 if (anfds [fd].events)
597 { 620 {
598 anfds [fd].events = 0; 621 anfds [fd].events = 0;
599 fd_change (EV_A_ fd); 622 fd_change (EV_A_ fd);
765 ev_child *w; 788 ev_child *w;
766 789
767 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 790 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
768 if (w->pid == pid || !w->pid) 791 if (w->pid == pid || !w->pid)
769 { 792 {
770 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 793 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
771 w->rpid = pid; 794 w->rpid = pid;
772 w->rstatus = status; 795 w->rstatus = status;
773 ev_feed_event (EV_A_ (W)w, EV_CHILD); 796 ev_feed_event (EV_A_ (W)w, EV_CHILD);
774 } 797 }
775} 798}
776 799
777#ifndef WCONTINUED 800#ifndef WCONTINUED
887ev_backend (EV_P) 910ev_backend (EV_P)
888{ 911{
889 return backend; 912 return backend;
890} 913}
891 914
915unsigned int
916ev_loop_count (EV_P)
917{
918 return loop_count;
919}
920
892static void noinline 921static void noinline
893loop_init (EV_P_ unsigned int flags) 922loop_init (EV_P_ unsigned int flags)
894{ 923{
895 if (!backend) 924 if (!backend)
896 { 925 {
905 ev_rt_now = ev_time (); 934 ev_rt_now = ev_time ();
906 mn_now = get_clock (); 935 mn_now = get_clock ();
907 now_floor = mn_now; 936 now_floor = mn_now;
908 rtmn_diff = ev_rt_now - mn_now; 937 rtmn_diff = ev_rt_now - mn_now;
909 938
939 /* pid check not overridable via env */
940#ifndef _WIN32
941 if (flags & EVFLAG_FORKCHECK)
942 curpid = getpid ();
943#endif
944
910 if (!(flags & EVFLAG_NOENV) 945 if (!(flags & EVFLAG_NOENV)
911 && !enable_secure () 946 && !enable_secure ()
912 && getenv ("LIBEV_FLAGS")) 947 && getenv ("LIBEV_FLAGS"))
913 flags = atoi (getenv ("LIBEV_FLAGS")); 948 flags = atoi (getenv ("LIBEV_FLAGS"));
914 949
970#if EV_USE_SELECT 1005#if EV_USE_SELECT
971 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1006 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
972#endif 1007#endif
973 1008
974 for (i = NUMPRI; i--; ) 1009 for (i = NUMPRI; i--; )
1010 {
975 array_free (pending, [i]); 1011 array_free (pending, [i]);
1012#if EV_IDLE_ENABLE
1013 array_free (idle, [i]);
1014#endif
1015 }
976 1016
977 /* have to use the microsoft-never-gets-it-right macro */ 1017 /* have to use the microsoft-never-gets-it-right macro */
978 array_free (fdchange, EMPTY0); 1018 array_free (fdchange, EMPTY);
979 array_free (timer, EMPTY0); 1019 array_free (timer, EMPTY);
980#if EV_PERIODIC_ENABLE 1020#if EV_PERIODIC_ENABLE
981 array_free (periodic, EMPTY0); 1021 array_free (periodic, EMPTY);
982#endif 1022#endif
983 array_free (idle, EMPTY0);
984 array_free (prepare, EMPTY0); 1023 array_free (prepare, EMPTY);
985 array_free (check, EMPTY0); 1024 array_free (check, EMPTY);
986 1025
987 backend = 0; 1026 backend = 0;
988} 1027}
989 1028
990void inline_size infy_fork (EV_P); 1029void inline_size infy_fork (EV_P);
1126 postfork = 1; 1165 postfork = 1;
1127} 1166}
1128 1167
1129/*****************************************************************************/ 1168/*****************************************************************************/
1130 1169
1131int inline_size 1170void
1132any_pending (EV_P) 1171ev_invoke (EV_P_ void *w, int revents)
1133{ 1172{
1134 int pri; 1173 EV_CB_INVOKE ((W)w, revents);
1135
1136 for (pri = NUMPRI; pri--; )
1137 if (pendingcnt [pri])
1138 return 1;
1139
1140 return 0;
1141} 1174}
1142 1175
1143void inline_speed 1176void inline_speed
1144call_pending (EV_P) 1177call_pending (EV_P)
1145{ 1178{
1237 for (i = periodiccnt >> 1; i--; ) 1270 for (i = periodiccnt >> 1; i--; )
1238 downheap ((WT *)periodics, periodiccnt, i); 1271 downheap ((WT *)periodics, periodiccnt, i);
1239} 1272}
1240#endif 1273#endif
1241 1274
1275#if EV_IDLE_ENABLE
1276void inline_size
1277idle_reify (EV_P)
1278{
1279 if (expect_false (idleall))
1280 {
1281 int pri;
1282
1283 for (pri = NUMPRI; pri--; )
1284 {
1285 if (pendingcnt [pri])
1286 break;
1287
1288 if (idlecnt [pri])
1289 {
1290 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1291 break;
1292 }
1293 }
1294 }
1295}
1296#endif
1297
1242int inline_size 1298int inline_size
1243time_update_monotonic (EV_P) 1299time_update_monotonic (EV_P)
1244{ 1300{
1245 mn_now = get_clock (); 1301 mn_now = get_clock ();
1246 1302
1270 ev_tstamp odiff = rtmn_diff; 1326 ev_tstamp odiff = rtmn_diff;
1271 1327
1272 /* loop a few times, before making important decisions. 1328 /* loop a few times, before making important decisions.
1273 * on the choice of "4": one iteration isn't enough, 1329 * on the choice of "4": one iteration isn't enough,
1274 * in case we get preempted during the calls to 1330 * in case we get preempted during the calls to
1275 * ev_time and get_clock. a second call is almost guarenteed 1331 * ev_time and get_clock. a second call is almost guaranteed
1276 * to succeed in that case, though. and looping a few more times 1332 * 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 1333 * doesn't hurt either as we only do this on time-jumps or
1278 * in the unlikely event of getting preempted here. 1334 * in the unlikely event of having been preempted here.
1279 */ 1335 */
1280 for (i = 4; --i; ) 1336 for (i = 4; --i; )
1281 { 1337 {
1282 rtmn_diff = ev_rt_now - mn_now; 1338 rtmn_diff = ev_rt_now - mn_now;
1283 1339
1305 { 1361 {
1306#if EV_PERIODIC_ENABLE 1362#if EV_PERIODIC_ENABLE
1307 periodics_reschedule (EV_A); 1363 periodics_reschedule (EV_A);
1308#endif 1364#endif
1309 1365
1310 /* adjust timers. this is easy, as the offset is the same for all */ 1366 /* adjust timers. this is easy, as the offset is the same for all of them */
1311 for (i = 0; i < timercnt; ++i) 1367 for (i = 0; i < timercnt; ++i)
1312 ((WT)timers [i])->at += ev_rt_now - mn_now; 1368 ((WT)timers [i])->at += ev_rt_now - mn_now;
1313 } 1369 }
1314 1370
1315 mn_now = ev_rt_now; 1371 mn_now = ev_rt_now;
1335{ 1391{
1336 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1392 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1337 ? EVUNLOOP_ONE 1393 ? EVUNLOOP_ONE
1338 : EVUNLOOP_CANCEL; 1394 : EVUNLOOP_CANCEL;
1339 1395
1340 while (activecnt) 1396 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1397
1398 do
1341 { 1399 {
1342 /* we might have forked, so reify kernel state if necessary */ 1400#ifndef _WIN32
1401 if (expect_false (curpid)) /* penalise the forking check even more */
1402 if (expect_false (getpid () != curpid))
1403 {
1404 curpid = getpid ();
1405 postfork = 1;
1406 }
1407#endif
1408
1343 #if EV_FORK_ENABLE 1409#if EV_FORK_ENABLE
1410 /* we might have forked, so queue fork handlers */
1344 if (expect_false (postfork)) 1411 if (expect_false (postfork))
1345 if (forkcnt) 1412 if (forkcnt)
1346 { 1413 {
1347 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1414 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1348 call_pending (EV_A); 1415 call_pending (EV_A);
1349 } 1416 }
1350 #endif 1417#endif
1351 1418
1352 /* queue check watchers (and execute them) */ 1419 /* queue check watchers (and execute them) */
1353 if (expect_false (preparecnt)) 1420 if (expect_false (preparecnt))
1354 { 1421 {
1355 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1422 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1356 call_pending (EV_A); 1423 call_pending (EV_A);
1357 } 1424 }
1358 1425
1426 if (expect_false (!activecnt))
1427 break;
1428
1359 /* we might have forked, so reify kernel state if necessary */ 1429 /* we might have forked, so reify kernel state if necessary */
1360 if (expect_false (postfork)) 1430 if (expect_false (postfork))
1361 loop_fork (EV_A); 1431 loop_fork (EV_A);
1362 1432
1363 /* update fd-related kernel structures */ 1433 /* update fd-related kernel structures */
1364 fd_reify (EV_A); 1434 fd_reify (EV_A);
1365 1435
1366 /* calculate blocking time */ 1436 /* calculate blocking time */
1367 { 1437 {
1368 double block; 1438 ev_tstamp block;
1369 1439
1370 if (flags & EVLOOP_NONBLOCK || idlecnt) 1440 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1371 block = 0.; /* do not block at all */ 1441 block = 0.; /* do not block at all */
1372 else 1442 else
1373 { 1443 {
1374 /* update time to cancel out callback processing overhead */ 1444 /* update time to cancel out callback processing overhead */
1375#if EV_USE_MONOTONIC 1445#if EV_USE_MONOTONIC
1399#endif 1469#endif
1400 1470
1401 if (expect_false (block < 0.)) block = 0.; 1471 if (expect_false (block < 0.)) block = 0.;
1402 } 1472 }
1403 1473
1474 ++loop_count;
1404 backend_poll (EV_A_ block); 1475 backend_poll (EV_A_ block);
1405 } 1476 }
1406 1477
1407 /* update ev_rt_now, do magic */ 1478 /* update ev_rt_now, do magic */
1408 time_update (EV_A); 1479 time_update (EV_A);
1411 timers_reify (EV_A); /* relative timers called last */ 1482 timers_reify (EV_A); /* relative timers called last */
1412#if EV_PERIODIC_ENABLE 1483#if EV_PERIODIC_ENABLE
1413 periodics_reify (EV_A); /* absolute timers called first */ 1484 periodics_reify (EV_A); /* absolute timers called first */
1414#endif 1485#endif
1415 1486
1487#if EV_IDLE_ENABLE
1416 /* queue idle watchers unless other events are pending */ 1488 /* queue idle watchers unless other events are pending */
1417 if (idlecnt && !any_pending (EV_A)) 1489 idle_reify (EV_A);
1418 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1490#endif
1419 1491
1420 /* queue check watchers, to be executed first */ 1492 /* queue check watchers, to be executed first */
1421 if (expect_false (checkcnt)) 1493 if (expect_false (checkcnt))
1422 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1494 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1423 1495
1424 call_pending (EV_A); 1496 call_pending (EV_A);
1425 1497
1426 if (expect_false (loop_done))
1427 break;
1428 } 1498 }
1499 while (expect_true (activecnt && !loop_done));
1429 1500
1430 if (loop_done == EVUNLOOP_ONE) 1501 if (loop_done == EVUNLOOP_ONE)
1431 loop_done = EVUNLOOP_CANCEL; 1502 loop_done = EVUNLOOP_CANCEL;
1432} 1503}
1433 1504
1460 head = &(*head)->next; 1531 head = &(*head)->next;
1461 } 1532 }
1462} 1533}
1463 1534
1464void inline_speed 1535void inline_speed
1465ev_clear_pending (EV_P_ W w) 1536clear_pending (EV_P_ W w)
1466{ 1537{
1467 if (w->pending) 1538 if (w->pending)
1468 { 1539 {
1469 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1540 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1470 w->pending = 0; 1541 w->pending = 0;
1471 } 1542 }
1472} 1543}
1473 1544
1545int
1546ev_clear_pending (EV_P_ void *w)
1547{
1548 W w_ = (W)w;
1549 int pending = w_->pending;
1550
1551 if (!pending)
1552 return 0;
1553
1554 w_->pending = 0;
1555 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1556 p->w = 0;
1557
1558 return p->events;
1559}
1560
1561void inline_size
1562pri_adjust (EV_P_ W w)
1563{
1564 int pri = w->priority;
1565 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1566 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1567 w->priority = pri;
1568}
1569
1474void inline_speed 1570void inline_speed
1475ev_start (EV_P_ W w, int active) 1571ev_start (EV_P_ W w, int active)
1476{ 1572{
1477 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1573 pri_adjust (EV_A_ w);
1478 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1479
1480 w->active = active; 1574 w->active = active;
1481 ev_ref (EV_A); 1575 ev_ref (EV_A);
1482} 1576}
1483 1577
1484void inline_size 1578void inline_size
1508} 1602}
1509 1603
1510void 1604void
1511ev_io_stop (EV_P_ ev_io *w) 1605ev_io_stop (EV_P_ ev_io *w)
1512{ 1606{
1513 ev_clear_pending (EV_A_ (W)w); 1607 clear_pending (EV_A_ (W)w);
1514 if (expect_false (!ev_is_active (w))) 1608 if (expect_false (!ev_is_active (w)))
1515 return; 1609 return;
1516 1610
1517 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1611 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1518 1612
1541} 1635}
1542 1636
1543void 1637void
1544ev_timer_stop (EV_P_ ev_timer *w) 1638ev_timer_stop (EV_P_ ev_timer *w)
1545{ 1639{
1546 ev_clear_pending (EV_A_ (W)w); 1640 clear_pending (EV_A_ (W)w);
1547 if (expect_false (!ev_is_active (w))) 1641 if (expect_false (!ev_is_active (w)))
1548 return; 1642 return;
1549 1643
1550 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1644 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1551 1645
1609} 1703}
1610 1704
1611void 1705void
1612ev_periodic_stop (EV_P_ ev_periodic *w) 1706ev_periodic_stop (EV_P_ ev_periodic *w)
1613{ 1707{
1614 ev_clear_pending (EV_A_ (W)w); 1708 clear_pending (EV_A_ (W)w);
1615 if (expect_false (!ev_is_active (w))) 1709 if (expect_false (!ev_is_active (w)))
1616 return; 1710 return;
1617 1711
1618 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1712 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1619 1713
1673} 1767}
1674 1768
1675void 1769void
1676ev_signal_stop (EV_P_ ev_signal *w) 1770ev_signal_stop (EV_P_ ev_signal *w)
1677{ 1771{
1678 ev_clear_pending (EV_A_ (W)w); 1772 clear_pending (EV_A_ (W)w);
1679 if (expect_false (!ev_is_active (w))) 1773 if (expect_false (!ev_is_active (w)))
1680 return; 1774 return;
1681 1775
1682 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1776 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1683 ev_stop (EV_A_ (W)w); 1777 ev_stop (EV_A_ (W)w);
1700} 1794}
1701 1795
1702void 1796void
1703ev_child_stop (EV_P_ ev_child *w) 1797ev_child_stop (EV_P_ ev_child *w)
1704{ 1798{
1705 ev_clear_pending (EV_A_ (W)w); 1799 clear_pending (EV_A_ (W)w);
1706 if (expect_false (!ev_is_active (w))) 1800 if (expect_false (!ev_is_active (w)))
1707 return; 1801 return;
1708 1802
1709 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1803 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1710 ev_stop (EV_A_ (W)w); 1804 ev_stop (EV_A_ (W)w);
1718# endif 1812# endif
1719 1813
1720#define DEF_STAT_INTERVAL 5.0074891 1814#define DEF_STAT_INTERVAL 5.0074891
1721#define MIN_STAT_INTERVAL 0.1074891 1815#define MIN_STAT_INTERVAL 0.1074891
1722 1816
1723void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); 1817static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
1724 1818
1725#if EV_USE_INOTIFY 1819#if EV_USE_INOTIFY
1726# define EV_INOTIFY_BUFSIZE 8192 1820# define EV_INOTIFY_BUFSIZE 8192
1727 1821
1728static void noinline 1822static void noinline
1879 w->attr.st_nlink = 0; 1973 w->attr.st_nlink = 0;
1880 else if (!w->attr.st_nlink) 1974 else if (!w->attr.st_nlink)
1881 w->attr.st_nlink = 1; 1975 w->attr.st_nlink = 1;
1882} 1976}
1883 1977
1884void noinline 1978static void noinline
1885stat_timer_cb (EV_P_ ev_timer *w_, int revents) 1979stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1886{ 1980{
1887 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 1981 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1888 1982
1889 /* we copy this here each the time so that */ 1983 /* we copy this here each the time so that */
1890 /* prev has the old value when the callback gets invoked */ 1984 /* prev has the old value when the callback gets invoked */
1891 w->prev = w->attr; 1985 w->prev = w->attr;
1892 ev_stat_stat (EV_A_ w); 1986 ev_stat_stat (EV_A_ w);
1893 1987
1894 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) 1988 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
1989 if (
1990 w->prev.st_dev != w->attr.st_dev
1991 || w->prev.st_ino != w->attr.st_ino
1992 || w->prev.st_mode != w->attr.st_mode
1993 || w->prev.st_nlink != w->attr.st_nlink
1994 || w->prev.st_uid != w->attr.st_uid
1995 || w->prev.st_gid != w->attr.st_gid
1996 || w->prev.st_rdev != w->attr.st_rdev
1997 || w->prev.st_size != w->attr.st_size
1998 || w->prev.st_atime != w->attr.st_atime
1999 || w->prev.st_mtime != w->attr.st_mtime
2000 || w->prev.st_ctime != w->attr.st_ctime
1895 { 2001 ) {
1896 #if EV_USE_INOTIFY 2002 #if EV_USE_INOTIFY
1897 infy_del (EV_A_ w); 2003 infy_del (EV_A_ w);
1898 infy_add (EV_A_ w); 2004 infy_add (EV_A_ w);
1899 ev_stat_stat (EV_A_ w); /* avoid race... */ 2005 ev_stat_stat (EV_A_ w); /* avoid race... */
1900 #endif 2006 #endif
1934} 2040}
1935 2041
1936void 2042void
1937ev_stat_stop (EV_P_ ev_stat *w) 2043ev_stat_stop (EV_P_ ev_stat *w)
1938{ 2044{
1939 ev_clear_pending (EV_A_ (W)w); 2045 clear_pending (EV_A_ (W)w);
1940 if (expect_false (!ev_is_active (w))) 2046 if (expect_false (!ev_is_active (w)))
1941 return; 2047 return;
1942 2048
1943#if EV_USE_INOTIFY 2049#if EV_USE_INOTIFY
1944 infy_del (EV_A_ w); 2050 infy_del (EV_A_ w);
1947 2053
1948 ev_stop (EV_A_ (W)w); 2054 ev_stop (EV_A_ (W)w);
1949} 2055}
1950#endif 2056#endif
1951 2057
2058#if EV_IDLE_ENABLE
1952void 2059void
1953ev_idle_start (EV_P_ ev_idle *w) 2060ev_idle_start (EV_P_ ev_idle *w)
1954{ 2061{
1955 if (expect_false (ev_is_active (w))) 2062 if (expect_false (ev_is_active (w)))
1956 return; 2063 return;
1957 2064
2065 pri_adjust (EV_A_ (W)w);
2066
2067 {
2068 int active = ++idlecnt [ABSPRI (w)];
2069
2070 ++idleall;
1958 ev_start (EV_A_ (W)w, ++idlecnt); 2071 ev_start (EV_A_ (W)w, active);
2072
1959 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2073 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1960 idles [idlecnt - 1] = w; 2074 idles [ABSPRI (w)][active - 1] = w;
2075 }
1961} 2076}
1962 2077
1963void 2078void
1964ev_idle_stop (EV_P_ ev_idle *w) 2079ev_idle_stop (EV_P_ ev_idle *w)
1965{ 2080{
1966 ev_clear_pending (EV_A_ (W)w); 2081 clear_pending (EV_A_ (W)w);
1967 if (expect_false (!ev_is_active (w))) 2082 if (expect_false (!ev_is_active (w)))
1968 return; 2083 return;
1969 2084
1970 { 2085 {
1971 int active = ((W)w)->active; 2086 int active = ((W)w)->active;
1972 idles [active - 1] = idles [--idlecnt]; 2087
2088 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
1973 ((W)idles [active - 1])->active = active; 2089 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2090
2091 ev_stop (EV_A_ (W)w);
2092 --idleall;
1974 } 2093 }
1975
1976 ev_stop (EV_A_ (W)w);
1977} 2094}
2095#endif
1978 2096
1979void 2097void
1980ev_prepare_start (EV_P_ ev_prepare *w) 2098ev_prepare_start (EV_P_ ev_prepare *w)
1981{ 2099{
1982 if (expect_false (ev_is_active (w))) 2100 if (expect_false (ev_is_active (w)))
1988} 2106}
1989 2107
1990void 2108void
1991ev_prepare_stop (EV_P_ ev_prepare *w) 2109ev_prepare_stop (EV_P_ ev_prepare *w)
1992{ 2110{
1993 ev_clear_pending (EV_A_ (W)w); 2111 clear_pending (EV_A_ (W)w);
1994 if (expect_false (!ev_is_active (w))) 2112 if (expect_false (!ev_is_active (w)))
1995 return; 2113 return;
1996 2114
1997 { 2115 {
1998 int active = ((W)w)->active; 2116 int active = ((W)w)->active;
2015} 2133}
2016 2134
2017void 2135void
2018ev_check_stop (EV_P_ ev_check *w) 2136ev_check_stop (EV_P_ ev_check *w)
2019{ 2137{
2020 ev_clear_pending (EV_A_ (W)w); 2138 clear_pending (EV_A_ (W)w);
2021 if (expect_false (!ev_is_active (w))) 2139 if (expect_false (!ev_is_active (w)))
2022 return; 2140 return;
2023 2141
2024 { 2142 {
2025 int active = ((W)w)->active; 2143 int active = ((W)w)->active;
2067} 2185}
2068 2186
2069void 2187void
2070ev_embed_stop (EV_P_ ev_embed *w) 2188ev_embed_stop (EV_P_ ev_embed *w)
2071{ 2189{
2072 ev_clear_pending (EV_A_ (W)w); 2190 clear_pending (EV_A_ (W)w);
2073 if (expect_false (!ev_is_active (w))) 2191 if (expect_false (!ev_is_active (w)))
2074 return; 2192 return;
2075 2193
2076 ev_io_stop (EV_A_ &w->io); 2194 ev_io_stop (EV_A_ &w->io);
2077 2195
2092} 2210}
2093 2211
2094void 2212void
2095ev_fork_stop (EV_P_ ev_fork *w) 2213ev_fork_stop (EV_P_ ev_fork *w)
2096{ 2214{
2097 ev_clear_pending (EV_A_ (W)w); 2215 clear_pending (EV_A_ (W)w);
2098 if (expect_false (!ev_is_active (w))) 2216 if (expect_false (!ev_is_active (w)))
2099 return; 2217 return;
2100 2218
2101 { 2219 {
2102 int active = ((W)w)->active; 2220 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines