… | |
… | |
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 | |
251 | typedef ev_watcher *W; |
251 | typedef ev_watcher *W; |
252 | typedef ev_watcher_list *WL; |
252 | typedef ev_watcher_list *WL; |
253 | typedef ev_watcher_time *WT; |
253 | typedef ev_watcher_time *WT; |
… | |
… | |
281 | perror (msg); |
281 | perror (msg); |
282 | abort (); |
282 | abort (); |
283 | } |
283 | } |
284 | } |
284 | } |
285 | |
285 | |
286 | static void *(*alloc)(void *ptr, size_t size) = realloc; |
286 | static void *(*alloc)(void *ptr, long size); |
287 | |
287 | |
288 | void |
288 | void |
289 | ev_set_allocator (void *(*cb)(void *ptr, size_t size)) |
289 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
290 | { |
290 | { |
291 | alloc = cb; |
291 | alloc = cb; |
292 | } |
292 | } |
293 | |
293 | |
294 | inline_speed void * |
294 | inline_speed void * |
295 | ev_realloc (void *ptr, size_t size) |
295 | ev_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 |
329 | typedef struct |
330 | typedef 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) |
401 | int inline_size |
|
|
402 | array_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 | |
|
|
422 | inline_speed void * |
|
|
423 | array_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 | /*****************************************************************************/ |
… | |
… | |
589 | static void noinline |
611 | static void noinline |
590 | fd_rearm_all (EV_P) |
612 | fd_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 |
… | |
… | |
887 | ev_backend (EV_P) |
908 | ev_backend (EV_P) |
888 | { |
909 | { |
889 | return backend; |
910 | return backend; |
890 | } |
911 | } |
891 | |
912 | |
|
|
913 | unsigned int |
|
|
914 | ev_loop_count (EV_P) |
|
|
915 | { |
|
|
916 | return loop_count; |
|
|
917 | } |
|
|
918 | |
892 | static void noinline |
919 | static void noinline |
893 | loop_init (EV_P_ unsigned int flags) |
920 | loop_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 | |
|
|
1027 | void inline_size infy_fork (EV_P); |
989 | |
1028 | |
990 | void inline_size |
1029 | void inline_size |
991 | loop_fork (EV_P) |
1030 | loop_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 | |
|
|
1126 | int inline_size |
|
|
1127 | any_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 | |
1138 | void inline_speed |
1168 | void inline_speed |
1139 | call_pending (EV_P) |
1169 | call_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 |
|
|
1268 | void inline_size |
|
|
1269 | idle_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 | |
1237 | int inline_size |
1290 | int inline_size |
1238 | time_update_monotonic (EV_P) |
1291 | time_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 | |
1459 | void inline_speed |
1527 | void inline_speed |
1460 | ev_clear_pending (EV_P_ W w) |
1528 | clear_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 | |
|
|
1537 | int |
|
|
1538 | ev_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 | |
|
|
1553 | void inline_size |
|
|
1554 | pri_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 | |
1469 | void inline_speed |
1562 | void inline_speed |
1470 | ev_start (EV_P_ W w, int active) |
1563 | ev_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 | |
1479 | void inline_size |
1570 | void inline_size |
… | |
… | |
1503 | } |
1594 | } |
1504 | |
1595 | |
1505 | void |
1596 | void |
1506 | ev_io_stop (EV_P_ ev_io *w) |
1597 | ev_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 | |
1538 | void |
1629 | void |
1539 | ev_timer_stop (EV_P_ ev_timer *w) |
1630 | ev_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 | |
1606 | void |
1697 | void |
1607 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1698 | ev_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 | |
1670 | void |
1761 | void |
1671 | ev_signal_stop (EV_P_ ev_signal *w) |
1762 | ev_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 | |
1697 | void |
1788 | void |
1698 | ev_child_stop (EV_P_ ev_child *w) |
1789 | ev_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 | |
1718 | void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); |
1809 | static 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 ((PATH_MAX + sizeof (struct inotify_event)) + 2048) |
1812 | # define EV_INOTIFY_BUFSIZE 8192 |
1722 | |
1813 | |
1723 | static void noinline |
1814 | static void noinline |
1724 | infy_add (EV_P_ ev_stat *w) |
1815 | infy_add (EV_P_ ev_stat *w) |
1725 | { |
1816 | { |
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); |
1817 | 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) |
1819 | if (w->wd < 0) |
1729 | { |
1820 | { |
1730 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
1821 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
1731 | |
1822 | |
1732 | /* monitor some parent directory for speedup hints */ |
1823 | /* monitor some parent directory for speedup hints */ |
1733 | if (errno == ENOENT || errno == EACCES) |
1824 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
1734 | { |
1825 | { |
1735 | char path [PATH_MAX]; |
1826 | char path [4096]; |
1736 | strcpy (path, w->path); |
1827 | strcpy (path, w->path); |
1737 | |
1828 | |
1738 | do |
1829 | do |
1739 | { |
1830 | { |
1740 | int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF |
1831 | int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF |
… | |
… | |
1744 | |
1835 | |
1745 | if (!pend) |
1836 | if (!pend) |
1746 | break; /* whoops, no '/', complain to your admin */ |
1837 | break; /* whoops, no '/', complain to your admin */ |
1747 | |
1838 | |
1748 | *pend = 0; |
1839 | *pend = 0; |
1749 | w->wd = inotify_add_watch (fs_fd, path, IN_DELETE_SELF | IN_CREATE | IN_MOVED_TO | IN_MASK_ADD); |
1840 | w->wd = inotify_add_watch (fs_fd, path, mask); |
1750 | } |
1841 | } |
1751 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
1842 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
1752 | } |
1843 | } |
1753 | } |
1844 | } |
1754 | else |
1845 | else |
… | |
… | |
1759 | } |
1850 | } |
1760 | |
1851 | |
1761 | static void noinline |
1852 | static void noinline |
1762 | infy_del (EV_P_ ev_stat *w) |
1853 | infy_del (EV_P_ ev_stat *w) |
1763 | { |
1854 | { |
1764 | WL w_; |
|
|
1765 | int slot; |
1855 | int slot; |
1766 | int wd = w->wd; |
1856 | int wd = w->wd; |
1767 | |
1857 | |
1768 | if (wd < 0) |
1858 | if (wd < 0) |
1769 | return; |
1859 | return; |
… | |
… | |
1798 | { |
1888 | { |
1799 | w->wd = -1; |
1889 | w->wd = -1; |
1800 | infy_add (EV_A_ w); /* re-add, no matter what */ |
1890 | infy_add (EV_A_ w); /* re-add, no matter what */ |
1801 | } |
1891 | } |
1802 | |
1892 | |
1803 | stat_timer_cb (EV_P_ &w->timer, 0); |
1893 | stat_timer_cb (EV_A_ &w->timer, 0); |
1804 | } |
1894 | } |
1805 | } |
1895 | } |
1806 | } |
1896 | } |
1807 | } |
1897 | } |
1808 | |
1898 | |
… | |
… | |
1832 | ev_set_priority (&fs_w, EV_MAXPRI); |
1922 | ev_set_priority (&fs_w, EV_MAXPRI); |
1833 | ev_io_start (EV_A_ &fs_w); |
1923 | ev_io_start (EV_A_ &fs_w); |
1834 | } |
1924 | } |
1835 | } |
1925 | } |
1836 | |
1926 | |
|
|
1927 | void inline_size |
|
|
1928 | infy_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 | |
1837 | #endif |
1959 | #endif |
1838 | |
1960 | |
1839 | void |
1961 | void |
1840 | ev_stat_stat (EV_P_ ev_stat *w) |
1962 | ev_stat_stat (EV_P_ ev_stat *w) |
1841 | { |
1963 | { |
… | |
… | |
1843 | w->attr.st_nlink = 0; |
1965 | w->attr.st_nlink = 0; |
1844 | else if (!w->attr.st_nlink) |
1966 | else if (!w->attr.st_nlink) |
1845 | w->attr.st_nlink = 1; |
1967 | w->attr.st_nlink = 1; |
1846 | } |
1968 | } |
1847 | |
1969 | |
1848 | void noinline |
1970 | static void noinline |
1849 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
1971 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
1850 | { |
1972 | { |
1851 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
1973 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
1852 | |
1974 | |
1853 | /* we copy this here each the time so that */ |
1975 | /* we copy this here each the time so that */ |
1854 | /* prev has the old value when the callback gets invoked */ |
1976 | /* prev has the old value when the callback gets invoked */ |
1855 | w->prev = w->attr; |
1977 | w->prev = w->attr; |
1856 | ev_stat_stat (EV_A_ w); |
1978 | ev_stat_stat (EV_A_ w); |
1857 | |
1979 | |
1858 | 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 |
1859 | { |
1993 | ) { |
1860 | #if EV_USE_INOTIFY |
1994 | #if EV_USE_INOTIFY |
1861 | infy_del (EV_A_ w); |
1995 | infy_del (EV_A_ w); |
1862 | infy_add (EV_A_ w); |
1996 | infy_add (EV_A_ w); |
1863 | ev_stat_stat (EV_A_ w); /* avoid race... */ |
1997 | ev_stat_stat (EV_A_ w); /* avoid race... */ |
1864 | #endif |
1998 | #endif |
… | |
… | |
1898 | } |
2032 | } |
1899 | |
2033 | |
1900 | void |
2034 | void |
1901 | ev_stat_stop (EV_P_ ev_stat *w) |
2035 | ev_stat_stop (EV_P_ ev_stat *w) |
1902 | { |
2036 | { |
1903 | ev_clear_pending (EV_A_ (W)w); |
2037 | clear_pending (EV_A_ (W)w); |
1904 | if (expect_false (!ev_is_active (w))) |
2038 | if (expect_false (!ev_is_active (w))) |
1905 | return; |
2039 | return; |
1906 | |
2040 | |
1907 | #if EV_USE_INOTIFY |
2041 | #if EV_USE_INOTIFY |
1908 | infy_del (EV_A_ w); |
2042 | infy_del (EV_A_ w); |
… | |
… | |
1911 | |
2045 | |
1912 | ev_stop (EV_A_ (W)w); |
2046 | ev_stop (EV_A_ (W)w); |
1913 | } |
2047 | } |
1914 | #endif |
2048 | #endif |
1915 | |
2049 | |
|
|
2050 | #if EV_IDLE_ENABLE |
1916 | void |
2051 | void |
1917 | ev_idle_start (EV_P_ ev_idle *w) |
2052 | ev_idle_start (EV_P_ ev_idle *w) |
1918 | { |
2053 | { |
1919 | if (expect_false (ev_is_active (w))) |
2054 | if (expect_false (ev_is_active (w))) |
1920 | return; |
2055 | return; |
1921 | |
2056 | |
|
|
2057 | pri_adjust (EV_A_ (W)w); |
|
|
2058 | |
|
|
2059 | { |
|
|
2060 | int active = ++idlecnt [ABSPRI (w)]; |
|
|
2061 | |
|
|
2062 | ++idleall; |
1922 | ev_start (EV_A_ (W)w, ++idlecnt); |
2063 | ev_start (EV_A_ (W)w, active); |
|
|
2064 | |
1923 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
2065 | array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2); |
1924 | idles [idlecnt - 1] = w; |
2066 | idles [ABSPRI (w)][active - 1] = w; |
|
|
2067 | } |
1925 | } |
2068 | } |
1926 | |
2069 | |
1927 | void |
2070 | void |
1928 | ev_idle_stop (EV_P_ ev_idle *w) |
2071 | ev_idle_stop (EV_P_ ev_idle *w) |
1929 | { |
2072 | { |
1930 | ev_clear_pending (EV_A_ (W)w); |
2073 | clear_pending (EV_A_ (W)w); |
1931 | if (expect_false (!ev_is_active (w))) |
2074 | if (expect_false (!ev_is_active (w))) |
1932 | return; |
2075 | return; |
1933 | |
2076 | |
1934 | { |
2077 | { |
1935 | int active = ((W)w)->active; |
2078 | int active = ((W)w)->active; |
1936 | idles [active - 1] = idles [--idlecnt]; |
2079 | |
|
|
2080 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
1937 | ((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; |
1938 | } |
2085 | } |
1939 | |
|
|
1940 | ev_stop (EV_A_ (W)w); |
|
|
1941 | } |
2086 | } |
|
|
2087 | #endif |
1942 | |
2088 | |
1943 | void |
2089 | void |
1944 | ev_prepare_start (EV_P_ ev_prepare *w) |
2090 | ev_prepare_start (EV_P_ ev_prepare *w) |
1945 | { |
2091 | { |
1946 | if (expect_false (ev_is_active (w))) |
2092 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1952 | } |
2098 | } |
1953 | |
2099 | |
1954 | void |
2100 | void |
1955 | ev_prepare_stop (EV_P_ ev_prepare *w) |
2101 | ev_prepare_stop (EV_P_ ev_prepare *w) |
1956 | { |
2102 | { |
1957 | ev_clear_pending (EV_A_ (W)w); |
2103 | clear_pending (EV_A_ (W)w); |
1958 | if (expect_false (!ev_is_active (w))) |
2104 | if (expect_false (!ev_is_active (w))) |
1959 | return; |
2105 | return; |
1960 | |
2106 | |
1961 | { |
2107 | { |
1962 | int active = ((W)w)->active; |
2108 | int active = ((W)w)->active; |
… | |
… | |
1979 | } |
2125 | } |
1980 | |
2126 | |
1981 | void |
2127 | void |
1982 | ev_check_stop (EV_P_ ev_check *w) |
2128 | ev_check_stop (EV_P_ ev_check *w) |
1983 | { |
2129 | { |
1984 | ev_clear_pending (EV_A_ (W)w); |
2130 | clear_pending (EV_A_ (W)w); |
1985 | if (expect_false (!ev_is_active (w))) |
2131 | if (expect_false (!ev_is_active (w))) |
1986 | return; |
2132 | return; |
1987 | |
2133 | |
1988 | { |
2134 | { |
1989 | int active = ((W)w)->active; |
2135 | int active = ((W)w)->active; |
… | |
… | |
2031 | } |
2177 | } |
2032 | |
2178 | |
2033 | void |
2179 | void |
2034 | ev_embed_stop (EV_P_ ev_embed *w) |
2180 | ev_embed_stop (EV_P_ ev_embed *w) |
2035 | { |
2181 | { |
2036 | ev_clear_pending (EV_A_ (W)w); |
2182 | clear_pending (EV_A_ (W)w); |
2037 | if (expect_false (!ev_is_active (w))) |
2183 | if (expect_false (!ev_is_active (w))) |
2038 | return; |
2184 | return; |
2039 | |
2185 | |
2040 | ev_io_stop (EV_A_ &w->io); |
2186 | ev_io_stop (EV_A_ &w->io); |
2041 | |
2187 | |
… | |
… | |
2056 | } |
2202 | } |
2057 | |
2203 | |
2058 | void |
2204 | void |
2059 | ev_fork_stop (EV_P_ ev_fork *w) |
2205 | ev_fork_stop (EV_P_ ev_fork *w) |
2060 | { |
2206 | { |
2061 | ev_clear_pending (EV_A_ (W)w); |
2207 | clear_pending (EV_A_ (W)w); |
2062 | if (expect_false (!ev_is_active (w))) |
2208 | if (expect_false (!ev_is_active (w))) |
2063 | return; |
2209 | return; |
2064 | |
2210 | |
2065 | { |
2211 | { |
2066 | int active = ((W)w)->active; |
2212 | int active = ((W)w)->active; |