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.172 by root, Sun Dec 9 02:27:44 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
423static noinline 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/*****************************************************************************/
430 453
431void noinline 454void noinline
432ev_feed_event (EV_P_ void *w, int revents) 455ev_feed_event (EV_P_ void *w, int revents)
433{ 456{
434 W w_ = (W)w; 457 W w_ = (W)w;
458 int pri = ABSPRI (w_);
435 459
436 if (expect_false (w_->pending)) 460 if (expect_false (w_->pending))
461 pendings [pri][w_->pending - 1].events |= revents;
462 else
437 { 463 {
464 w_->pending = ++pendingcnt [pri];
465 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
466 pendings [pri][w_->pending - 1].w = w_;
438 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 467 pendings [pri][w_->pending - 1].events = revents;
439 return;
440 } 468 }
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} 469}
447 470
448void inline_size 471void inline_size
449queue_events (EV_P_ W *events, int eventcnt, int type) 472queue_events (EV_P_ W *events, int eventcnt, int type)
450{ 473{
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);
726 for (signum = signalmax; signum--; ) 749 for (signum = signalmax; signum--; )
727 if (signals [signum].gotsig) 750 if (signals [signum].gotsig)
728 ev_feed_signal_event (EV_A_ signum + 1); 751 ev_feed_signal_event (EV_A_ signum + 1);
729} 752}
730 753
731void inline_size 754void inline_speed
732fd_intern (int fd) 755fd_intern (int fd)
733{ 756{
734#ifdef _WIN32 757#ifdef _WIN32
735 int arg = 1; 758 int arg = 1;
736 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 759 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
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}
1028
1029void inline_size infy_fork (EV_P);
989 1030
990void inline_size 1031void inline_size
991loop_fork (EV_P) 1032loop_fork (EV_P)
992{ 1033{
993#if EV_USE_PORT 1034#if EV_USE_PORT
996#if EV_USE_KQUEUE 1037#if EV_USE_KQUEUE
997 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 1038 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
998#endif 1039#endif
999#if EV_USE_EPOLL 1040#if EV_USE_EPOLL
1000 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 1041 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
1042#endif
1043#if EV_USE_INOTIFY
1044 infy_fork (EV_A);
1001#endif 1045#endif
1002 1046
1003 if (ev_is_active (&sigev)) 1047 if (ev_is_active (&sigev))
1004 { 1048 {
1005 /* default loop */ 1049 /* default loop */
1121 postfork = 1; 1165 postfork = 1;
1122} 1166}
1123 1167
1124/*****************************************************************************/ 1168/*****************************************************************************/
1125 1169
1126int inline_size 1170void
1127any_pending (EV_P) 1171ev_invoke (EV_P_ void *w, int revents)
1128{ 1172{
1129 int pri; 1173 EV_CB_INVOKE ((W)w, revents);
1130
1131 for (pri = NUMPRI; pri--; )
1132 if (pendingcnt [pri])
1133 return 1;
1134
1135 return 0;
1136} 1174}
1137 1175
1138void inline_speed 1176void inline_speed
1139call_pending (EV_P) 1177call_pending (EV_P)
1140{ 1178{
1232 for (i = periodiccnt >> 1; i--; ) 1270 for (i = periodiccnt >> 1; i--; )
1233 downheap ((WT *)periodics, periodiccnt, i); 1271 downheap ((WT *)periodics, periodiccnt, i);
1234} 1272}
1235#endif 1273#endif
1236 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
1237int inline_size 1298int inline_size
1238time_update_monotonic (EV_P) 1299time_update_monotonic (EV_P)
1239{ 1300{
1240 mn_now = get_clock (); 1301 mn_now = get_clock ();
1241 1302
1265 ev_tstamp odiff = rtmn_diff; 1326 ev_tstamp odiff = rtmn_diff;
1266 1327
1267 /* loop a few times, before making important decisions. 1328 /* loop a few times, before making important decisions.
1268 * on the choice of "4": one iteration isn't enough, 1329 * on the choice of "4": one iteration isn't enough,
1269 * in case we get preempted during the calls to 1330 * in case we get preempted during the calls to
1270 * ev_time and get_clock. a second call is almost guarenteed 1331 * ev_time and get_clock. a second call is almost guaranteed
1271 * 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
1272 * 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
1273 * in the unlikely event of getting preempted here. 1334 * in the unlikely event of having been preempted here.
1274 */ 1335 */
1275 for (i = 4; --i; ) 1336 for (i = 4; --i; )
1276 { 1337 {
1277 rtmn_diff = ev_rt_now - mn_now; 1338 rtmn_diff = ev_rt_now - mn_now;
1278 1339
1300 { 1361 {
1301#if EV_PERIODIC_ENABLE 1362#if EV_PERIODIC_ENABLE
1302 periodics_reschedule (EV_A); 1363 periodics_reschedule (EV_A);
1303#endif 1364#endif
1304 1365
1305 /* 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 */
1306 for (i = 0; i < timercnt; ++i) 1367 for (i = 0; i < timercnt; ++i)
1307 ((WT)timers [i])->at += ev_rt_now - mn_now; 1368 ((WT)timers [i])->at += ev_rt_now - mn_now;
1308 } 1369 }
1309 1370
1310 mn_now = ev_rt_now; 1371 mn_now = ev_rt_now;
1330{ 1391{
1331 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1392 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1332 ? EVUNLOOP_ONE 1393 ? EVUNLOOP_ONE
1333 : EVUNLOOP_CANCEL; 1394 : EVUNLOOP_CANCEL;
1334 1395
1335 while (activecnt) 1396 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1397
1398 do
1336 { 1399 {
1337 /* 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
1338 #if EV_FORK_ENABLE 1409#if EV_FORK_ENABLE
1410 /* we might have forked, so queue fork handlers */
1339 if (expect_false (postfork)) 1411 if (expect_false (postfork))
1340 if (forkcnt) 1412 if (forkcnt)
1341 { 1413 {
1342 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1414 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1343 call_pending (EV_A); 1415 call_pending (EV_A);
1344 } 1416 }
1345 #endif 1417#endif
1346 1418
1347 /* queue check watchers (and execute them) */ 1419 /* queue prepare watchers (and execute them) */
1348 if (expect_false (preparecnt)) 1420 if (expect_false (preparecnt))
1349 { 1421 {
1350 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1422 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1351 call_pending (EV_A); 1423 call_pending (EV_A);
1352 } 1424 }
1353 1425
1426 if (expect_false (!activecnt))
1427 break;
1428
1354 /* we might have forked, so reify kernel state if necessary */ 1429 /* we might have forked, so reify kernel state if necessary */
1355 if (expect_false (postfork)) 1430 if (expect_false (postfork))
1356 loop_fork (EV_A); 1431 loop_fork (EV_A);
1357 1432
1358 /* update fd-related kernel structures */ 1433 /* update fd-related kernel structures */
1359 fd_reify (EV_A); 1434 fd_reify (EV_A);
1360 1435
1361 /* calculate blocking time */ 1436 /* calculate blocking time */
1362 { 1437 {
1363 double block; 1438 ev_tstamp block;
1364 1439
1365 if (flags & EVLOOP_NONBLOCK || idlecnt) 1440 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1366 block = 0.; /* do not block at all */ 1441 block = 0.; /* do not block at all */
1367 else 1442 else
1368 { 1443 {
1369 /* update time to cancel out callback processing overhead */ 1444 /* update time to cancel out callback processing overhead */
1370#if EV_USE_MONOTONIC 1445#if EV_USE_MONOTONIC
1394#endif 1469#endif
1395 1470
1396 if (expect_false (block < 0.)) block = 0.; 1471 if (expect_false (block < 0.)) block = 0.;
1397 } 1472 }
1398 1473
1474 ++loop_count;
1399 backend_poll (EV_A_ block); 1475 backend_poll (EV_A_ block);
1400 } 1476 }
1401 1477
1402 /* update ev_rt_now, do magic */ 1478 /* update ev_rt_now, do magic */
1403 time_update (EV_A); 1479 time_update (EV_A);
1406 timers_reify (EV_A); /* relative timers called last */ 1482 timers_reify (EV_A); /* relative timers called last */
1407#if EV_PERIODIC_ENABLE 1483#if EV_PERIODIC_ENABLE
1408 periodics_reify (EV_A); /* absolute timers called first */ 1484 periodics_reify (EV_A); /* absolute timers called first */
1409#endif 1485#endif
1410 1486
1487#if EV_IDLE_ENABLE
1411 /* queue idle watchers unless other events are pending */ 1488 /* queue idle watchers unless other events are pending */
1412 if (idlecnt && !any_pending (EV_A)) 1489 idle_reify (EV_A);
1413 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1490#endif
1414 1491
1415 /* queue check watchers, to be executed first */ 1492 /* queue check watchers, to be executed first */
1416 if (expect_false (checkcnt)) 1493 if (expect_false (checkcnt))
1417 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1494 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1418 1495
1419 call_pending (EV_A); 1496 call_pending (EV_A);
1420 1497
1421 if (expect_false (loop_done))
1422 break;
1423 } 1498 }
1499 while (expect_true (activecnt && !loop_done));
1424 1500
1425 if (loop_done == EVUNLOOP_ONE) 1501 if (loop_done == EVUNLOOP_ONE)
1426 loop_done = EVUNLOOP_CANCEL; 1502 loop_done = EVUNLOOP_CANCEL;
1427} 1503}
1428 1504
1455 head = &(*head)->next; 1531 head = &(*head)->next;
1456 } 1532 }
1457} 1533}
1458 1534
1459void inline_speed 1535void inline_speed
1460ev_clear_pending (EV_P_ W w) 1536clear_pending (EV_P_ W w)
1461{ 1537{
1462 if (w->pending) 1538 if (w->pending)
1463 { 1539 {
1464 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1540 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1465 w->pending = 0; 1541 w->pending = 0;
1466 } 1542 }
1467} 1543}
1468 1544
1545int
1546ev_clear_pending (EV_P_ void *w)
1547{
1548 W w_ = (W)w;
1549 int pending = w_->pending;
1550
1551 if (expect_true (pending))
1552 {
1553 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1554 w_->pending = 0;
1555 p->w = 0;
1556 return p->events;
1557 }
1558 else
1559 return 0;
1560}
1561
1562void inline_size
1563pri_adjust (EV_P_ W w)
1564{
1565 int pri = w->priority;
1566 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1567 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1568 w->priority = pri;
1569}
1570
1469void inline_speed 1571void inline_speed
1470ev_start (EV_P_ W w, int active) 1572ev_start (EV_P_ W w, int active)
1471{ 1573{
1472 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1574 pri_adjust (EV_A_ w);
1473 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1474
1475 w->active = active; 1575 w->active = active;
1476 ev_ref (EV_A); 1576 ev_ref (EV_A);
1477} 1577}
1478 1578
1479void inline_size 1579void inline_size
1483 w->active = 0; 1583 w->active = 0;
1484} 1584}
1485 1585
1486/*****************************************************************************/ 1586/*****************************************************************************/
1487 1587
1488void 1588void noinline
1489ev_io_start (EV_P_ ev_io *w) 1589ev_io_start (EV_P_ ev_io *w)
1490{ 1590{
1491 int fd = w->fd; 1591 int fd = w->fd;
1492 1592
1493 if (expect_false (ev_is_active (w))) 1593 if (expect_false (ev_is_active (w)))
1500 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1600 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1501 1601
1502 fd_change (EV_A_ fd); 1602 fd_change (EV_A_ fd);
1503} 1603}
1504 1604
1505void 1605void noinline
1506ev_io_stop (EV_P_ ev_io *w) 1606ev_io_stop (EV_P_ ev_io *w)
1507{ 1607{
1508 ev_clear_pending (EV_A_ (W)w); 1608 clear_pending (EV_A_ (W)w);
1509 if (expect_false (!ev_is_active (w))) 1609 if (expect_false (!ev_is_active (w)))
1510 return; 1610 return;
1511 1611
1512 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1612 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1513 1613
1515 ev_stop (EV_A_ (W)w); 1615 ev_stop (EV_A_ (W)w);
1516 1616
1517 fd_change (EV_A_ w->fd); 1617 fd_change (EV_A_ w->fd);
1518} 1618}
1519 1619
1520void 1620void noinline
1521ev_timer_start (EV_P_ ev_timer *w) 1621ev_timer_start (EV_P_ ev_timer *w)
1522{ 1622{
1523 if (expect_false (ev_is_active (w))) 1623 if (expect_false (ev_is_active (w)))
1524 return; 1624 return;
1525 1625
1533 upheap ((WT *)timers, timercnt - 1); 1633 upheap ((WT *)timers, timercnt - 1);
1534 1634
1535 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1635 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1536} 1636}
1537 1637
1538void 1638void noinline
1539ev_timer_stop (EV_P_ ev_timer *w) 1639ev_timer_stop (EV_P_ ev_timer *w)
1540{ 1640{
1541 ev_clear_pending (EV_A_ (W)w); 1641 clear_pending (EV_A_ (W)w);
1542 if (expect_false (!ev_is_active (w))) 1642 if (expect_false (!ev_is_active (w)))
1543 return; 1643 return;
1544 1644
1545 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1645 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1546 1646
1557 ((WT)w)->at -= mn_now; 1657 ((WT)w)->at -= mn_now;
1558 1658
1559 ev_stop (EV_A_ (W)w); 1659 ev_stop (EV_A_ (W)w);
1560} 1660}
1561 1661
1562void 1662void noinline
1563ev_timer_again (EV_P_ ev_timer *w) 1663ev_timer_again (EV_P_ ev_timer *w)
1564{ 1664{
1565 if (ev_is_active (w)) 1665 if (ev_is_active (w))
1566 { 1666 {
1567 if (w->repeat) 1667 if (w->repeat)
1578 ev_timer_start (EV_A_ w); 1678 ev_timer_start (EV_A_ w);
1579 } 1679 }
1580} 1680}
1581 1681
1582#if EV_PERIODIC_ENABLE 1682#if EV_PERIODIC_ENABLE
1583void 1683void noinline
1584ev_periodic_start (EV_P_ ev_periodic *w) 1684ev_periodic_start (EV_P_ ev_periodic *w)
1585{ 1685{
1586 if (expect_false (ev_is_active (w))) 1686 if (expect_false (ev_is_active (w)))
1587 return; 1687 return;
1588 1688
1601 upheap ((WT *)periodics, periodiccnt - 1); 1701 upheap ((WT *)periodics, periodiccnt - 1);
1602 1702
1603 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1703 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1604} 1704}
1605 1705
1606void 1706void noinline
1607ev_periodic_stop (EV_P_ ev_periodic *w) 1707ev_periodic_stop (EV_P_ ev_periodic *w)
1608{ 1708{
1609 ev_clear_pending (EV_A_ (W)w); 1709 clear_pending (EV_A_ (W)w);
1610 if (expect_false (!ev_is_active (w))) 1710 if (expect_false (!ev_is_active (w)))
1611 return; 1711 return;
1612 1712
1613 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1713 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1614 1714
1623 } 1723 }
1624 1724
1625 ev_stop (EV_A_ (W)w); 1725 ev_stop (EV_A_ (W)w);
1626} 1726}
1627 1727
1628void 1728void noinline
1629ev_periodic_again (EV_P_ ev_periodic *w) 1729ev_periodic_again (EV_P_ ev_periodic *w)
1630{ 1730{
1631 /* TODO: use adjustheap and recalculation */ 1731 /* TODO: use adjustheap and recalculation */
1632 ev_periodic_stop (EV_A_ w); 1732 ev_periodic_stop (EV_A_ w);
1633 ev_periodic_start (EV_A_ w); 1733 ev_periodic_start (EV_A_ w);
1636 1736
1637#ifndef SA_RESTART 1737#ifndef SA_RESTART
1638# define SA_RESTART 0 1738# define SA_RESTART 0
1639#endif 1739#endif
1640 1740
1641void 1741void noinline
1642ev_signal_start (EV_P_ ev_signal *w) 1742ev_signal_start (EV_P_ ev_signal *w)
1643{ 1743{
1644#if EV_MULTIPLICITY 1744#if EV_MULTIPLICITY
1645 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1745 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1646#endif 1746#endif
1665 sigaction (w->signum, &sa, 0); 1765 sigaction (w->signum, &sa, 0);
1666#endif 1766#endif
1667 } 1767 }
1668} 1768}
1669 1769
1670void 1770void noinline
1671ev_signal_stop (EV_P_ ev_signal *w) 1771ev_signal_stop (EV_P_ ev_signal *w)
1672{ 1772{
1673 ev_clear_pending (EV_A_ (W)w); 1773 clear_pending (EV_A_ (W)w);
1674 if (expect_false (!ev_is_active (w))) 1774 if (expect_false (!ev_is_active (w)))
1675 return; 1775 return;
1676 1776
1677 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1777 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1678 ev_stop (EV_A_ (W)w); 1778 ev_stop (EV_A_ (W)w);
1695} 1795}
1696 1796
1697void 1797void
1698ev_child_stop (EV_P_ ev_child *w) 1798ev_child_stop (EV_P_ ev_child *w)
1699{ 1799{
1700 ev_clear_pending (EV_A_ (W)w); 1800 clear_pending (EV_A_ (W)w);
1701 if (expect_false (!ev_is_active (w))) 1801 if (expect_false (!ev_is_active (w)))
1702 return; 1802 return;
1703 1803
1704 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1804 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1705 ev_stop (EV_A_ (W)w); 1805 ev_stop (EV_A_ (W)w);
1713# endif 1813# endif
1714 1814
1715#define DEF_STAT_INTERVAL 5.0074891 1815#define DEF_STAT_INTERVAL 5.0074891
1716#define MIN_STAT_INTERVAL 0.1074891 1816#define MIN_STAT_INTERVAL 0.1074891
1717 1817
1718void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); 1818static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
1719 1819
1720#if EV_USE_INOTIFY 1820#if EV_USE_INOTIFY
1721# define EV_INOTIFY_BUFSIZE 8192 1821# define EV_INOTIFY_BUFSIZE 8192
1722 1822
1723static void noinline 1823static void noinline
1831 ev_set_priority (&fs_w, EV_MAXPRI); 1931 ev_set_priority (&fs_w, EV_MAXPRI);
1832 ev_io_start (EV_A_ &fs_w); 1932 ev_io_start (EV_A_ &fs_w);
1833 } 1933 }
1834} 1934}
1835 1935
1936void inline_size
1937infy_fork (EV_P)
1938{
1939 int slot;
1940
1941 if (fs_fd < 0)
1942 return;
1943
1944 close (fs_fd);
1945 fs_fd = inotify_init ();
1946
1947 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1948 {
1949 WL w_ = fs_hash [slot].head;
1950 fs_hash [slot].head = 0;
1951
1952 while (w_)
1953 {
1954 ev_stat *w = (ev_stat *)w_;
1955 w_ = w_->next; /* lets us add this watcher */
1956
1957 w->wd = -1;
1958
1959 if (fs_fd >= 0)
1960 infy_add (EV_A_ w); /* re-add, no matter what */
1961 else
1962 ev_timer_start (EV_A_ &w->timer);
1963 }
1964
1965 }
1966}
1967
1836#endif 1968#endif
1837 1969
1838void 1970void
1839ev_stat_stat (EV_P_ ev_stat *w) 1971ev_stat_stat (EV_P_ ev_stat *w)
1840{ 1972{
1842 w->attr.st_nlink = 0; 1974 w->attr.st_nlink = 0;
1843 else if (!w->attr.st_nlink) 1975 else if (!w->attr.st_nlink)
1844 w->attr.st_nlink = 1; 1976 w->attr.st_nlink = 1;
1845} 1977}
1846 1978
1847void noinline 1979static void noinline
1848stat_timer_cb (EV_P_ ev_timer *w_, int revents) 1980stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1849{ 1981{
1850 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 1982 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1851 1983
1852 /* we copy this here each the time so that */ 1984 /* we copy this here each the time so that */
1853 /* prev has the old value when the callback gets invoked */ 1985 /* prev has the old value when the callback gets invoked */
1854 w->prev = w->attr; 1986 w->prev = w->attr;
1855 ev_stat_stat (EV_A_ w); 1987 ev_stat_stat (EV_A_ w);
1856 1988
1857 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) 1989 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
1990 if (
1991 w->prev.st_dev != w->attr.st_dev
1992 || w->prev.st_ino != w->attr.st_ino
1993 || w->prev.st_mode != w->attr.st_mode
1994 || w->prev.st_nlink != w->attr.st_nlink
1995 || w->prev.st_uid != w->attr.st_uid
1996 || w->prev.st_gid != w->attr.st_gid
1997 || w->prev.st_rdev != w->attr.st_rdev
1998 || w->prev.st_size != w->attr.st_size
1999 || w->prev.st_atime != w->attr.st_atime
2000 || w->prev.st_mtime != w->attr.st_mtime
2001 || w->prev.st_ctime != w->attr.st_ctime
1858 { 2002 ) {
1859 #if EV_USE_INOTIFY 2003 #if EV_USE_INOTIFY
1860 infy_del (EV_A_ w); 2004 infy_del (EV_A_ w);
1861 infy_add (EV_A_ w); 2005 infy_add (EV_A_ w);
1862 ev_stat_stat (EV_A_ w); /* avoid race... */ 2006 ev_stat_stat (EV_A_ w); /* avoid race... */
1863 #endif 2007 #endif
1897} 2041}
1898 2042
1899void 2043void
1900ev_stat_stop (EV_P_ ev_stat *w) 2044ev_stat_stop (EV_P_ ev_stat *w)
1901{ 2045{
1902 ev_clear_pending (EV_A_ (W)w); 2046 clear_pending (EV_A_ (W)w);
1903 if (expect_false (!ev_is_active (w))) 2047 if (expect_false (!ev_is_active (w)))
1904 return; 2048 return;
1905 2049
1906#if EV_USE_INOTIFY 2050#if EV_USE_INOTIFY
1907 infy_del (EV_A_ w); 2051 infy_del (EV_A_ w);
1910 2054
1911 ev_stop (EV_A_ (W)w); 2055 ev_stop (EV_A_ (W)w);
1912} 2056}
1913#endif 2057#endif
1914 2058
2059#if EV_IDLE_ENABLE
1915void 2060void
1916ev_idle_start (EV_P_ ev_idle *w) 2061ev_idle_start (EV_P_ ev_idle *w)
1917{ 2062{
1918 if (expect_false (ev_is_active (w))) 2063 if (expect_false (ev_is_active (w)))
1919 return; 2064 return;
1920 2065
2066 pri_adjust (EV_A_ (W)w);
2067
2068 {
2069 int active = ++idlecnt [ABSPRI (w)];
2070
2071 ++idleall;
1921 ev_start (EV_A_ (W)w, ++idlecnt); 2072 ev_start (EV_A_ (W)w, active);
2073
1922 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2074 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1923 idles [idlecnt - 1] = w; 2075 idles [ABSPRI (w)][active - 1] = w;
2076 }
1924} 2077}
1925 2078
1926void 2079void
1927ev_idle_stop (EV_P_ ev_idle *w) 2080ev_idle_stop (EV_P_ ev_idle *w)
1928{ 2081{
1929 ev_clear_pending (EV_A_ (W)w); 2082 clear_pending (EV_A_ (W)w);
1930 if (expect_false (!ev_is_active (w))) 2083 if (expect_false (!ev_is_active (w)))
1931 return; 2084 return;
1932 2085
1933 { 2086 {
1934 int active = ((W)w)->active; 2087 int active = ((W)w)->active;
1935 idles [active - 1] = idles [--idlecnt]; 2088
2089 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
1936 ((W)idles [active - 1])->active = active; 2090 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2091
2092 ev_stop (EV_A_ (W)w);
2093 --idleall;
1937 } 2094 }
1938
1939 ev_stop (EV_A_ (W)w);
1940} 2095}
2096#endif
1941 2097
1942void 2098void
1943ev_prepare_start (EV_P_ ev_prepare *w) 2099ev_prepare_start (EV_P_ ev_prepare *w)
1944{ 2100{
1945 if (expect_false (ev_is_active (w))) 2101 if (expect_false (ev_is_active (w)))
1951} 2107}
1952 2108
1953void 2109void
1954ev_prepare_stop (EV_P_ ev_prepare *w) 2110ev_prepare_stop (EV_P_ ev_prepare *w)
1955{ 2111{
1956 ev_clear_pending (EV_A_ (W)w); 2112 clear_pending (EV_A_ (W)w);
1957 if (expect_false (!ev_is_active (w))) 2113 if (expect_false (!ev_is_active (w)))
1958 return; 2114 return;
1959 2115
1960 { 2116 {
1961 int active = ((W)w)->active; 2117 int active = ((W)w)->active;
1978} 2134}
1979 2135
1980void 2136void
1981ev_check_stop (EV_P_ ev_check *w) 2137ev_check_stop (EV_P_ ev_check *w)
1982{ 2138{
1983 ev_clear_pending (EV_A_ (W)w); 2139 clear_pending (EV_A_ (W)w);
1984 if (expect_false (!ev_is_active (w))) 2140 if (expect_false (!ev_is_active (w)))
1985 return; 2141 return;
1986 2142
1987 { 2143 {
1988 int active = ((W)w)->active; 2144 int active = ((W)w)->active;
2030} 2186}
2031 2187
2032void 2188void
2033ev_embed_stop (EV_P_ ev_embed *w) 2189ev_embed_stop (EV_P_ ev_embed *w)
2034{ 2190{
2035 ev_clear_pending (EV_A_ (W)w); 2191 clear_pending (EV_A_ (W)w);
2036 if (expect_false (!ev_is_active (w))) 2192 if (expect_false (!ev_is_active (w)))
2037 return; 2193 return;
2038 2194
2039 ev_io_stop (EV_A_ &w->io); 2195 ev_io_stop (EV_A_ &w->io);
2040 2196
2055} 2211}
2056 2212
2057void 2213void
2058ev_fork_stop (EV_P_ ev_fork *w) 2214ev_fork_stop (EV_P_ ev_fork *w)
2059{ 2215{
2060 ev_clear_pending (EV_A_ (W)w); 2216 clear_pending (EV_A_ (W)w);
2061 if (expect_false (!ev_is_active (w))) 2217 if (expect_false (!ev_is_active (w)))
2062 return; 2218 return;
2063 2219
2064 { 2220 {
2065 int active = ((W)w)->active; 2221 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines