… | |
… | |
210 | #elif defined (_sys_nsig) |
210 | #elif defined (_sys_nsig) |
211 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
211 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
212 | #else |
212 | #else |
213 | # error "unable to find value for NSIG, please report" |
213 | # error "unable to find value for NSIG, please report" |
214 | /* to make it compile regardless, just remove the above line */ |
214 | /* to make it compile regardless, just remove the above line */ |
215 | # define EV_NSIG 64 |
215 | # define EV_NSIG 65 |
216 | #endif |
216 | #endif |
217 | |
217 | |
218 | /* Default to some arbitrary number that's big enough to get most |
|
|
219 | of the common signals. |
|
|
220 | */ |
|
|
221 | #ifndef NSIG |
|
|
222 | # define NSIG 50 |
|
|
223 | #endif |
|
|
224 | /* <-- NSIG logic from Configure */ |
|
|
225 | #ifndef EV_USE_CLOCK_SYSCALL |
218 | #ifndef EV_USE_CLOCK_SYSCALL |
226 | # if __linux && __GLIBC__ >= 2 |
219 | # if __linux && __GLIBC__ >= 2 |
227 | # define EV_USE_CLOCK_SYSCALL 1 |
220 | # define EV_USE_CLOCK_SYSCALL 1 |
228 | # else |
221 | # else |
229 | # define EV_USE_CLOCK_SYSCALL 0 |
222 | # define EV_USE_CLOCK_SYSCALL 0 |
… | |
… | |
309 | # define EV_USE_EVENTFD 0 |
302 | # define EV_USE_EVENTFD 0 |
310 | # endif |
303 | # endif |
311 | #endif |
304 | #endif |
312 | |
305 | |
313 | #ifndef EV_USE_SIGNALFD |
306 | #ifndef EV_USE_SIGNALFD |
314 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9)) |
307 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
315 | # define EV_USE_SIGNALFD 1 |
308 | # define EV_USE_SIGNALFD 1 |
316 | # else |
309 | # else |
317 | # define EV_USE_SIGNALFD 0 |
310 | # define EV_USE_SIGNALFD 0 |
318 | # endif |
311 | # endif |
319 | #endif |
312 | #endif |
… | |
… | |
393 | # include <stdint.h> |
386 | # include <stdint.h> |
394 | # ifndef EFD_NONBLOCK |
387 | # ifndef EFD_NONBLOCK |
395 | # define EFD_NONBLOCK O_NONBLOCK |
388 | # define EFD_NONBLOCK O_NONBLOCK |
396 | # endif |
389 | # endif |
397 | # ifndef EFD_CLOEXEC |
390 | # ifndef EFD_CLOEXEC |
|
|
391 | # ifdef O_CLOEXEC |
398 | # define EFD_CLOEXEC O_CLOEXEC |
392 | # define EFD_CLOEXEC O_CLOEXEC |
|
|
393 | # else |
|
|
394 | # define EFD_CLOEXEC 02000000 |
|
|
395 | # endif |
399 | # endif |
396 | # endif |
400 | # ifdef __cplusplus |
397 | # ifdef __cplusplus |
401 | extern "C" { |
398 | extern "C" { |
402 | # endif |
399 | # endif |
403 | int eventfd (unsigned int initval, int flags); |
400 | int eventfd (unsigned int initval, int flags); |
… | |
… | |
405 | } |
402 | } |
406 | # endif |
403 | # endif |
407 | #endif |
404 | #endif |
408 | |
405 | |
409 | #if EV_USE_SIGNALFD |
406 | #if EV_USE_SIGNALFD |
410 | # include <sys/signalfd.h> |
407 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
|
|
408 | # include <stdint.h> |
|
|
409 | # ifndef SFD_NONBLOCK |
|
|
410 | # define SFD_NONBLOCK O_NONBLOCK |
411 | #endif |
411 | # endif |
|
|
412 | # ifndef SFD_CLOEXEC |
|
|
413 | # ifdef O_CLOEXEC |
|
|
414 | # define SFD_CLOEXEC O_CLOEXEC |
|
|
415 | # else |
|
|
416 | # define SFD_CLOEXEC 02000000 |
|
|
417 | # endif |
|
|
418 | # endif |
|
|
419 | # ifdef __cplusplus |
|
|
420 | extern "C" { |
|
|
421 | # endif |
|
|
422 | int signalfd (int fd, const sigset_t *mask, int flags); |
|
|
423 | |
|
|
424 | struct signalfd_siginfo |
|
|
425 | { |
|
|
426 | uint32_t ssi_signo; |
|
|
427 | char pad[128 - sizeof (uint32_t)]; |
|
|
428 | }; |
|
|
429 | # ifdef __cplusplus |
|
|
430 | } |
|
|
431 | # endif |
|
|
432 | #endif |
|
|
433 | |
412 | |
434 | |
413 | /**/ |
435 | /**/ |
414 | |
436 | |
415 | #if EV_VERIFY >= 3 |
437 | #if EV_VERIFY >= 3 |
416 | # define EV_FREQUENT_CHECK ev_loop_verify (EV_A) |
438 | # define EV_FREQUENT_CHECK ev_loop_verify (EV_A) |
… | |
… | |
479 | |
501 | |
480 | #if EV_USE_MONOTONIC |
502 | #if EV_USE_MONOTONIC |
481 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
503 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
482 | #endif |
504 | #endif |
483 | |
505 | |
|
|
506 | #ifndef EV_FD_TO_WIN32_HANDLE |
|
|
507 | # define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd) |
|
|
508 | #endif |
|
|
509 | #ifndef EV_WIN32_HANDLE_TO_FD |
|
|
510 | # define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (fd, 0) |
|
|
511 | #endif |
|
|
512 | #ifndef EV_WIN32_CLOSE_FD |
|
|
513 | # define EV_WIN32_CLOSE_FD(fd) close (fd) |
|
|
514 | #endif |
|
|
515 | |
484 | #ifdef _WIN32 |
516 | #ifdef _WIN32 |
485 | # include "ev_win32.c" |
517 | # include "ev_win32.c" |
486 | #endif |
518 | #endif |
487 | |
519 | |
488 | /*****************************************************************************/ |
520 | /*****************************************************************************/ |
… | |
… | |
879 | |
911 | |
880 | #if EV_SELECT_IS_WINSOCKET |
912 | #if EV_SELECT_IS_WINSOCKET |
881 | if (events) |
913 | if (events) |
882 | { |
914 | { |
883 | unsigned long arg; |
915 | unsigned long arg; |
884 | #ifdef EV_FD_TO_WIN32_HANDLE |
|
|
885 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
916 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
886 | #else |
|
|
887 | anfd->handle = _get_osfhandle (fd); |
|
|
888 | #endif |
|
|
889 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
917 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
890 | } |
918 | } |
891 | #endif |
919 | #endif |
892 | |
920 | |
893 | { |
921 | { |
… | |
… | |
964 | |
992 | |
965 | for (fd = anfdmax; fd--; ) |
993 | for (fd = anfdmax; fd--; ) |
966 | if (anfds [fd].events) |
994 | if (anfds [fd].events) |
967 | { |
995 | { |
968 | fd_kill (EV_A_ fd); |
996 | fd_kill (EV_A_ fd); |
969 | return; |
997 | break; |
970 | } |
998 | } |
971 | } |
999 | } |
972 | |
1000 | |
973 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
1001 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
974 | static void noinline |
1002 | static void noinline |
… | |
… | |
1064 | |
1092 | |
1065 | for (;;) |
1093 | for (;;) |
1066 | { |
1094 | { |
1067 | int c = k << 1; |
1095 | int c = k << 1; |
1068 | |
1096 | |
1069 | if (c > N + HEAP0 - 1) |
1097 | if (c >= N + HEAP0) |
1070 | break; |
1098 | break; |
1071 | |
1099 | |
1072 | c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) |
1100 | c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) |
1073 | ? 1 : 0; |
1101 | ? 1 : 0; |
1074 | |
1102 | |
… | |
… | |
1110 | |
1138 | |
1111 | /* move an element suitably so it is in a correct place */ |
1139 | /* move an element suitably so it is in a correct place */ |
1112 | inline_size void |
1140 | inline_size void |
1113 | adjustheap (ANHE *heap, int N, int k) |
1141 | adjustheap (ANHE *heap, int N, int k) |
1114 | { |
1142 | { |
1115 | if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) |
1143 | if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)])) |
1116 | upheap (heap, k); |
1144 | upheap (heap, k); |
1117 | else |
1145 | else |
1118 | downheap (heap, N, k); |
1146 | downheap (heap, N, k); |
1119 | } |
1147 | } |
1120 | |
1148 | |
… | |
… | |
1133 | /*****************************************************************************/ |
1161 | /*****************************************************************************/ |
1134 | |
1162 | |
1135 | /* associate signal watchers to a signal signal */ |
1163 | /* associate signal watchers to a signal signal */ |
1136 | typedef struct |
1164 | typedef struct |
1137 | { |
1165 | { |
|
|
1166 | EV_ATOMIC_T pending; |
|
|
1167 | #if EV_MULTIPLICITY |
|
|
1168 | EV_P; |
|
|
1169 | #endif |
1138 | WL head; |
1170 | WL head; |
1139 | EV_ATOMIC_T gotsig; |
|
|
1140 | } ANSIG; |
1171 | } ANSIG; |
1141 | |
1172 | |
1142 | static ANSIG *signals; |
1173 | static ANSIG signals [EV_NSIG - 1]; |
1143 | static int signalmax; |
|
|
1144 | |
|
|
1145 | static EV_ATOMIC_T gotsig; |
|
|
1146 | |
1174 | |
1147 | /*****************************************************************************/ |
1175 | /*****************************************************************************/ |
1148 | |
1176 | |
1149 | /* used to prepare libev internal fd's */ |
1177 | /* used to prepare libev internal fd's */ |
1150 | /* this is not fork-safe */ |
1178 | /* this is not fork-safe */ |
… | |
… | |
1218 | /* called whenever the libev signal pipe */ |
1246 | /* called whenever the libev signal pipe */ |
1219 | /* got some events (signal, async) */ |
1247 | /* got some events (signal, async) */ |
1220 | static void |
1248 | static void |
1221 | pipecb (EV_P_ ev_io *iow, int revents) |
1249 | pipecb (EV_P_ ev_io *iow, int revents) |
1222 | { |
1250 | { |
|
|
1251 | int i; |
|
|
1252 | |
1223 | #if EV_USE_EVENTFD |
1253 | #if EV_USE_EVENTFD |
1224 | if (evfd >= 0) |
1254 | if (evfd >= 0) |
1225 | { |
1255 | { |
1226 | uint64_t counter; |
1256 | uint64_t counter; |
1227 | read (evfd, &counter, sizeof (uint64_t)); |
1257 | read (evfd, &counter, sizeof (uint64_t)); |
… | |
… | |
1231 | { |
1261 | { |
1232 | char dummy; |
1262 | char dummy; |
1233 | read (evpipe [0], &dummy, 1); |
1263 | read (evpipe [0], &dummy, 1); |
1234 | } |
1264 | } |
1235 | |
1265 | |
1236 | if (gotsig && ev_is_default_loop (EV_A)) |
1266 | if (sig_pending) |
1237 | { |
1267 | { |
1238 | int signum; |
1268 | sig_pending = 0; |
1239 | gotsig = 0; |
|
|
1240 | |
1269 | |
1241 | for (signum = signalmax; signum--; ) |
1270 | for (i = EV_NSIG - 1; i--; ) |
1242 | if (signals [signum].gotsig) |
1271 | if (expect_false (signals [i].pending)) |
1243 | ev_feed_signal_event (EV_A_ signum + 1); |
1272 | ev_feed_signal_event (EV_A_ i + 1); |
1244 | } |
1273 | } |
1245 | |
1274 | |
1246 | #if EV_ASYNC_ENABLE |
1275 | #if EV_ASYNC_ENABLE |
1247 | if (gotasync) |
1276 | if (async_pending) |
1248 | { |
1277 | { |
1249 | int i; |
1278 | async_pending = 0; |
1250 | gotasync = 0; |
|
|
1251 | |
1279 | |
1252 | for (i = asynccnt; i--; ) |
1280 | for (i = asynccnt; i--; ) |
1253 | if (asyncs [i]->sent) |
1281 | if (asyncs [i]->sent) |
1254 | { |
1282 | { |
1255 | asyncs [i]->sent = 0; |
1283 | asyncs [i]->sent = 0; |
… | |
… | |
1263 | |
1291 | |
1264 | static void |
1292 | static void |
1265 | ev_sighandler (int signum) |
1293 | ev_sighandler (int signum) |
1266 | { |
1294 | { |
1267 | #if EV_MULTIPLICITY |
1295 | #if EV_MULTIPLICITY |
1268 | struct ev_loop *loop = &default_loop_struct; |
1296 | EV_P = signals [signum - 1].loop; |
1269 | #endif |
1297 | #endif |
1270 | |
1298 | |
1271 | #if _WIN32 |
1299 | #if _WIN32 |
1272 | signal (signum, ev_sighandler); |
1300 | signal (signum, ev_sighandler); |
1273 | #endif |
1301 | #endif |
1274 | |
1302 | |
1275 | signals [signum - 1].gotsig = 1; |
1303 | signals [signum - 1].pending = 1; |
1276 | evpipe_write (EV_A_ &gotsig); |
1304 | evpipe_write (EV_A_ &sig_pending); |
1277 | } |
1305 | } |
1278 | |
1306 | |
1279 | void noinline |
1307 | void noinline |
1280 | ev_feed_signal_event (EV_P_ int signum) |
1308 | ev_feed_signal_event (EV_P_ int signum) |
1281 | { |
1309 | { |
1282 | WL w; |
1310 | WL w; |
1283 | |
1311 | |
|
|
1312 | if (expect_false (signum <= 0 || signum > EV_NSIG)) |
|
|
1313 | return; |
|
|
1314 | |
|
|
1315 | --signum; |
|
|
1316 | |
1284 | #if EV_MULTIPLICITY |
1317 | #if EV_MULTIPLICITY |
1285 | assert (("libev: feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); |
1318 | /* it is permissible to try to feed a signal to the wrong loop */ |
1286 | #endif |
1319 | /* or, likely more useful, feeding a signal nobody is waiting for */ |
1287 | |
1320 | |
1288 | --signum; |
1321 | if (expect_false (signals [signum].loop != EV_A)) |
1289 | |
|
|
1290 | if (signum < 0 || signum >= signalmax) |
|
|
1291 | return; |
1322 | return; |
|
|
1323 | #endif |
1292 | |
1324 | |
1293 | signals [signum].gotsig = 0; |
1325 | signals [signum].pending = 0; |
1294 | |
1326 | |
1295 | for (w = signals [signum].head; w; w = w->next) |
1327 | for (w = signals [signum].head; w; w = w->next) |
1296 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
1328 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
1297 | } |
1329 | } |
1298 | |
1330 | |
1299 | #if EV_USE_SIGNALFD |
1331 | #if EV_USE_SIGNALFD |
1300 | static void |
1332 | static void |
1301 | sigfdcb (EV_P_ ev_io *iow, int revents) |
1333 | sigfdcb (EV_P_ ev_io *iow, int revents) |
1302 | { |
1334 | { |
1303 | struct signalfd_siginfo si[4], *sip; |
1335 | struct signalfd_siginfo si[2], *sip; /* these structs are big */ |
1304 | |
1336 | |
1305 | for (;;) |
1337 | for (;;) |
1306 | { |
1338 | { |
1307 | ssize_t res = read (sigfd, si, sizeof (si)); |
1339 | ssize_t res = read (sigfd, si, sizeof (si)); |
1308 | |
1340 | |
… | |
… | |
1542 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
1574 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
1543 | have_monotonic = 1; |
1575 | have_monotonic = 1; |
1544 | } |
1576 | } |
1545 | #endif |
1577 | #endif |
1546 | |
1578 | |
|
|
1579 | /* pid check not overridable via env */ |
|
|
1580 | #ifndef _WIN32 |
|
|
1581 | if (flags & EVFLAG_FORKCHECK) |
|
|
1582 | curpid = getpid (); |
|
|
1583 | #endif |
|
|
1584 | |
|
|
1585 | if (!(flags & EVFLAG_NOENV) |
|
|
1586 | && !enable_secure () |
|
|
1587 | && getenv ("LIBEV_FLAGS")) |
|
|
1588 | flags = atoi (getenv ("LIBEV_FLAGS")); |
|
|
1589 | |
1547 | ev_rt_now = ev_time (); |
1590 | ev_rt_now = ev_time (); |
1548 | mn_now = get_clock (); |
1591 | mn_now = get_clock (); |
1549 | now_floor = mn_now; |
1592 | now_floor = mn_now; |
1550 | rtmn_diff = ev_rt_now - mn_now; |
1593 | rtmn_diff = ev_rt_now - mn_now; |
1551 | #if EV_MINIMAL < 2 |
1594 | #if EV_MINIMAL < 2 |
… | |
… | |
1554 | |
1597 | |
1555 | io_blocktime = 0.; |
1598 | io_blocktime = 0.; |
1556 | timeout_blocktime = 0.; |
1599 | timeout_blocktime = 0.; |
1557 | backend = 0; |
1600 | backend = 0; |
1558 | backend_fd = -1; |
1601 | backend_fd = -1; |
1559 | gotasync = 0; |
1602 | sig_pending = 0; |
|
|
1603 | #if EV_ASYNC_ENABLE |
|
|
1604 | async_pending = 0; |
|
|
1605 | #endif |
1560 | #if EV_USE_INOTIFY |
1606 | #if EV_USE_INOTIFY |
1561 | fs_fd = -2; |
1607 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1562 | #endif |
1608 | #endif |
1563 | #if EV_USE_SIGNALFD |
1609 | #if EV_USE_SIGNALFD |
1564 | sigfd = -2; |
1610 | sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2; |
1565 | #endif |
1611 | #endif |
1566 | |
|
|
1567 | /* pid check not overridable via env */ |
|
|
1568 | #ifndef _WIN32 |
|
|
1569 | if (flags & EVFLAG_FORKCHECK) |
|
|
1570 | curpid = getpid (); |
|
|
1571 | #endif |
|
|
1572 | |
|
|
1573 | if (!(flags & EVFLAG_NOENV) |
|
|
1574 | && !enable_secure () |
|
|
1575 | && getenv ("LIBEV_FLAGS")) |
|
|
1576 | flags = atoi (getenv ("LIBEV_FLAGS")); |
|
|
1577 | |
1612 | |
1578 | if (!(flags & 0x0000ffffU)) |
1613 | if (!(flags & 0x0000ffffU)) |
1579 | flags |= ev_recommended_backends (); |
1614 | flags |= ev_recommended_backends (); |
1580 | |
1615 | |
1581 | #if EV_USE_PORT |
1616 | #if EV_USE_PORT |
… | |
… | |
1617 | close (evfd); |
1652 | close (evfd); |
1618 | #endif |
1653 | #endif |
1619 | |
1654 | |
1620 | if (evpipe [0] >= 0) |
1655 | if (evpipe [0] >= 0) |
1621 | { |
1656 | { |
1622 | close (evpipe [0]); |
1657 | EV_WIN32_CLOSE_FD (evpipe [0]); |
1623 | close (evpipe [1]); |
1658 | EV_WIN32_CLOSE_FD (evpipe [1]); |
1624 | } |
1659 | } |
1625 | } |
1660 | } |
1626 | |
1661 | |
1627 | #if EV_USE_SIGNALFD |
1662 | #if EV_USE_SIGNALFD |
1628 | if (ev_is_active (&sigfd_w)) |
1663 | if (ev_is_active (&sigfd_w)) |
… | |
… | |
1709 | |
1744 | |
1710 | if (ev_is_active (&pipe_w)) |
1745 | if (ev_is_active (&pipe_w)) |
1711 | { |
1746 | { |
1712 | /* this "locks" the handlers against writing to the pipe */ |
1747 | /* this "locks" the handlers against writing to the pipe */ |
1713 | /* while we modify the fd vars */ |
1748 | /* while we modify the fd vars */ |
1714 | gotsig = 1; |
1749 | sig_pending = 1; |
1715 | #if EV_ASYNC_ENABLE |
1750 | #if EV_ASYNC_ENABLE |
1716 | gotasync = 1; |
1751 | async_pending = 1; |
1717 | #endif |
1752 | #endif |
1718 | |
1753 | |
1719 | ev_ref (EV_A); |
1754 | ev_ref (EV_A); |
1720 | ev_io_stop (EV_A_ &pipe_w); |
1755 | ev_io_stop (EV_A_ &pipe_w); |
1721 | |
1756 | |
… | |
… | |
1724 | close (evfd); |
1759 | close (evfd); |
1725 | #endif |
1760 | #endif |
1726 | |
1761 | |
1727 | if (evpipe [0] >= 0) |
1762 | if (evpipe [0] >= 0) |
1728 | { |
1763 | { |
1729 | close (evpipe [0]); |
1764 | EV_WIN32_CLOSE_FD (evpipe [0]); |
1730 | close (evpipe [1]); |
1765 | EV_WIN32_CLOSE_FD (evpipe [1]); |
1731 | } |
1766 | } |
1732 | |
1767 | |
1733 | evpipe_init (EV_A); |
1768 | evpipe_init (EV_A); |
1734 | /* now iterate over everything, in case we missed something */ |
1769 | /* now iterate over everything, in case we missed something */ |
1735 | pipecb (EV_A_ &pipe_w, EV_READ); |
1770 | pipecb (EV_A_ &pipe_w, EV_READ); |
… | |
… | |
1741 | #if EV_MULTIPLICITY |
1776 | #if EV_MULTIPLICITY |
1742 | |
1777 | |
1743 | struct ev_loop * |
1778 | struct ev_loop * |
1744 | ev_loop_new (unsigned int flags) |
1779 | ev_loop_new (unsigned int flags) |
1745 | { |
1780 | { |
1746 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1781 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1747 | |
1782 | |
1748 | memset (loop, 0, sizeof (struct ev_loop)); |
1783 | memset (EV_A, 0, sizeof (struct ev_loop)); |
1749 | loop_init (EV_A_ flags); |
1784 | loop_init (EV_A_ flags); |
1750 | |
1785 | |
1751 | if (ev_backend (EV_A)) |
1786 | if (ev_backend (EV_A)) |
1752 | return loop; |
1787 | return EV_A; |
1753 | |
1788 | |
1754 | return 0; |
1789 | return 0; |
1755 | } |
1790 | } |
1756 | |
1791 | |
1757 | void |
1792 | void |
… | |
… | |
1861 | assert (checkmax >= checkcnt); |
1896 | assert (checkmax >= checkcnt); |
1862 | array_verify (EV_A_ (W *)checks, checkcnt); |
1897 | array_verify (EV_A_ (W *)checks, checkcnt); |
1863 | |
1898 | |
1864 | # if 0 |
1899 | # if 0 |
1865 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1900 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1866 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1901 | for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) |
1867 | # endif |
1902 | # endif |
1868 | #endif |
1903 | #endif |
1869 | } |
1904 | } |
1870 | #endif |
1905 | #endif |
1871 | |
1906 | |
… | |
… | |
1878 | #endif |
1913 | #endif |
1879 | { |
1914 | { |
1880 | if (!ev_default_loop_ptr) |
1915 | if (!ev_default_loop_ptr) |
1881 | { |
1916 | { |
1882 | #if EV_MULTIPLICITY |
1917 | #if EV_MULTIPLICITY |
1883 | struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct; |
1918 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
1884 | #else |
1919 | #else |
1885 | ev_default_loop_ptr = 1; |
1920 | ev_default_loop_ptr = 1; |
1886 | #endif |
1921 | #endif |
1887 | |
1922 | |
1888 | loop_init (EV_A_ flags); |
1923 | loop_init (EV_A_ flags); |
… | |
… | |
1905 | |
1940 | |
1906 | void |
1941 | void |
1907 | ev_default_destroy (void) |
1942 | ev_default_destroy (void) |
1908 | { |
1943 | { |
1909 | #if EV_MULTIPLICITY |
1944 | #if EV_MULTIPLICITY |
1910 | struct ev_loop *loop = ev_default_loop_ptr; |
1945 | EV_P = ev_default_loop_ptr; |
1911 | #endif |
1946 | #endif |
1912 | |
1947 | |
1913 | ev_default_loop_ptr = 0; |
1948 | ev_default_loop_ptr = 0; |
1914 | |
1949 | |
1915 | #ifndef _WIN32 |
1950 | #ifndef _WIN32 |
… | |
… | |
1922 | |
1957 | |
1923 | void |
1958 | void |
1924 | ev_default_fork (void) |
1959 | ev_default_fork (void) |
1925 | { |
1960 | { |
1926 | #if EV_MULTIPLICITY |
1961 | #if EV_MULTIPLICITY |
1927 | struct ev_loop *loop = ev_default_loop_ptr; |
1962 | EV_P = ev_default_loop_ptr; |
1928 | #endif |
1963 | #endif |
1929 | |
1964 | |
1930 | postfork = 1; /* must be in line with ev_loop_fork */ |
1965 | postfork = 1; /* must be in line with ev_loop_fork */ |
1931 | } |
1966 | } |
1932 | |
1967 | |
… | |
… | |
2398 | inline_size void |
2433 | inline_size void |
2399 | wlist_del (WL *head, WL elem) |
2434 | wlist_del (WL *head, WL elem) |
2400 | { |
2435 | { |
2401 | while (*head) |
2436 | while (*head) |
2402 | { |
2437 | { |
2403 | if (*head == elem) |
2438 | if (expect_true (*head == elem)) |
2404 | { |
2439 | { |
2405 | *head = elem->next; |
2440 | *head = elem->next; |
2406 | return; |
2441 | break; |
2407 | } |
2442 | } |
2408 | |
2443 | |
2409 | head = &(*head)->next; |
2444 | head = &(*head)->next; |
2410 | } |
2445 | } |
2411 | } |
2446 | } |
… | |
… | |
2665 | #endif |
2700 | #endif |
2666 | |
2701 | |
2667 | void noinline |
2702 | void noinline |
2668 | ev_signal_start (EV_P_ ev_signal *w) |
2703 | ev_signal_start (EV_P_ ev_signal *w) |
2669 | { |
2704 | { |
2670 | #if EV_MULTIPLICITY |
|
|
2671 | assert (("libev: signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
|
|
2672 | #endif |
|
|
2673 | if (expect_false (ev_is_active (w))) |
2705 | if (expect_false (ev_is_active (w))) |
2674 | return; |
2706 | return; |
2675 | |
2707 | |
2676 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0)); |
2708 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); |
|
|
2709 | |
|
|
2710 | #if EV_MULTIPLICITY |
|
|
2711 | assert (("libev: a signal must not be attached to two different loops", |
|
|
2712 | !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); |
|
|
2713 | |
|
|
2714 | signals [w->signum - 1].loop = EV_A; |
|
|
2715 | #endif |
2677 | |
2716 | |
2678 | EV_FREQUENT_CHECK; |
2717 | EV_FREQUENT_CHECK; |
2679 | |
2718 | |
2680 | #if EV_USE_SIGNALFD |
2719 | #if EV_USE_SIGNALFD |
2681 | if (sigfd == -2) |
2720 | if (sigfd == -2) |
… | |
… | |
2703 | sigaddset (&sigfd_set, w->signum); |
2742 | sigaddset (&sigfd_set, w->signum); |
2704 | sigprocmask (SIG_BLOCK, &sigfd_set, 0); |
2743 | sigprocmask (SIG_BLOCK, &sigfd_set, 0); |
2705 | |
2744 | |
2706 | signalfd (sigfd, &sigfd_set, 0); |
2745 | signalfd (sigfd, &sigfd_set, 0); |
2707 | } |
2746 | } |
2708 | else |
|
|
2709 | #endif |
|
|
2710 | evpipe_init (EV_A); |
|
|
2711 | |
|
|
2712 | { |
|
|
2713 | #ifndef _WIN32 |
|
|
2714 | sigset_t full, prev; |
|
|
2715 | sigfillset (&full); |
|
|
2716 | sigprocmask (SIG_SETMASK, &full, &prev); |
|
|
2717 | #endif |
|
|
2718 | |
|
|
2719 | array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); |
|
|
2720 | |
|
|
2721 | #ifndef _WIN32 |
|
|
2722 | # if EV_USE_SIGNALFD |
|
|
2723 | if (sigfd < 0)/*TODO*/ |
|
|
2724 | # endif |
2747 | #endif |
2725 | sigdelset (&prev, w->signum); |
|
|
2726 | sigprocmask (SIG_SETMASK, &prev, 0); |
|
|
2727 | #endif |
|
|
2728 | } |
|
|
2729 | |
2748 | |
2730 | ev_start (EV_A_ (W)w, 1); |
2749 | ev_start (EV_A_ (W)w, 1); |
2731 | wlist_add (&signals [w->signum - 1].head, (WL)w); |
2750 | wlist_add (&signals [w->signum - 1].head, (WL)w); |
2732 | |
2751 | |
2733 | if (!((WL)w)->next) |
2752 | if (!((WL)w)->next) |
2734 | { |
|
|
2735 | #if _WIN32 |
|
|
2736 | signal (w->signum, ev_sighandler); |
|
|
2737 | #else |
|
|
2738 | # if EV_USE_SIGNALFD |
2753 | # if EV_USE_SIGNALFD |
2739 | if (sigfd < 0) /*TODO*/ |
2754 | if (sigfd < 0) /*TODO*/ |
2740 | # endif |
2755 | # endif |
2741 | { |
2756 | { |
|
|
2757 | # if _WIN32 |
|
|
2758 | signal (w->signum, ev_sighandler); |
|
|
2759 | # else |
2742 | struct sigaction sa = { }; |
2760 | struct sigaction sa; |
|
|
2761 | |
|
|
2762 | evpipe_init (EV_A); |
|
|
2763 | |
2743 | sa.sa_handler = ev_sighandler; |
2764 | sa.sa_handler = ev_sighandler; |
2744 | sigfillset (&sa.sa_mask); |
2765 | sigfillset (&sa.sa_mask); |
2745 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2766 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2746 | sigaction (w->signum, &sa, 0); |
2767 | sigaction (w->signum, &sa, 0); |
|
|
2768 | |
|
|
2769 | sigemptyset (&sa.sa_mask); |
|
|
2770 | sigaddset (&sa.sa_mask, w->signum); |
|
|
2771 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
|
|
2772 | #endif |
2747 | } |
2773 | } |
2748 | #endif |
|
|
2749 | } |
|
|
2750 | |
2774 | |
2751 | EV_FREQUENT_CHECK; |
2775 | EV_FREQUENT_CHECK; |
2752 | } |
2776 | } |
2753 | |
2777 | |
2754 | void noinline |
2778 | void noinline |
… | |
… | |
2762 | |
2786 | |
2763 | wlist_del (&signals [w->signum - 1].head, (WL)w); |
2787 | wlist_del (&signals [w->signum - 1].head, (WL)w); |
2764 | ev_stop (EV_A_ (W)w); |
2788 | ev_stop (EV_A_ (W)w); |
2765 | |
2789 | |
2766 | if (!signals [w->signum - 1].head) |
2790 | if (!signals [w->signum - 1].head) |
|
|
2791 | { |
|
|
2792 | #if EV_MULTIPLICITY |
|
|
2793 | signals [w->signum - 1].loop = 0; /* unattach from signal */ |
|
|
2794 | #endif |
2767 | #if EV_USE_SIGNALFD |
2795 | #if EV_USE_SIGNALFD |
2768 | if (sigfd >= 0) |
2796 | if (sigfd >= 0) |
2769 | { |
2797 | { |
2770 | sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D |
2798 | sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D |
2771 | sigdelset (&sigfd_set, w->signum); |
2799 | sigdelset (&sigfd_set, w->signum); |
2772 | signalfd (sigfd, &sigfd_set, 0); |
2800 | signalfd (sigfd, &sigfd_set, 0); |
2773 | sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D |
2801 | sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D |
2774 | /*TODO: maybe unblock signal? */ |
2802 | /*TODO: maybe unblock signal? */ |
2775 | } |
2803 | } |
2776 | else |
2804 | else |
2777 | #endif |
2805 | #endif |
2778 | signal (w->signum, SIG_DFL); |
2806 | signal (w->signum, SIG_DFL); |
|
|
2807 | } |
2779 | |
2808 | |
2780 | EV_FREQUENT_CHECK; |
2809 | EV_FREQUENT_CHECK; |
2781 | } |
2810 | } |
2782 | |
2811 | |
2783 | void |
2812 | void |
… | |
… | |
2863 | } |
2892 | } |
2864 | } |
2893 | } |
2865 | |
2894 | |
2866 | if (w->wd >= 0) |
2895 | if (w->wd >= 0) |
2867 | { |
2896 | { |
|
|
2897 | struct statfs sfs; |
|
|
2898 | |
2868 | wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); |
2899 | wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); |
2869 | |
2900 | |
2870 | /* now local changes will be tracked by inotify, but remote changes won't */ |
2901 | /* now local changes will be tracked by inotify, but remote changes won't */ |
2871 | /* unless the filesystem it known to be local, we therefore still poll */ |
2902 | /* unless the filesystem it known to be local, we therefore still poll */ |
2872 | /* also do poll on <2.6.25, but with normal frequency */ |
2903 | /* also do poll on <2.6.25, but with normal frequency */ |
2873 | struct statfs sfs; |
|
|
2874 | |
2904 | |
2875 | if (fs_2625 && !statfs (w->path, &sfs)) |
2905 | if (fs_2625 && !statfs (w->path, &sfs)) |
2876 | if (sfs.f_type == 0x1373 /* devfs */ |
2906 | if (sfs.f_type == 0x1373 /* devfs */ |
2877 | || sfs.f_type == 0xEF53 /* ext2/3 */ |
2907 | || sfs.f_type == 0xEF53 /* ext2/3 */ |
2878 | || sfs.f_type == 0x3153464a /* jfs */ |
2908 | || sfs.f_type == 0x3153464a /* jfs */ |
… | |
… | |
2967 | return; |
2997 | return; |
2968 | |
2998 | |
2969 | fs_2625 = 1; |
2999 | fs_2625 = 1; |
2970 | } |
3000 | } |
2971 | |
3001 | |
|
|
3002 | inline_size int |
|
|
3003 | infy_newfd (void) |
|
|
3004 | { |
|
|
3005 | #if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) |
|
|
3006 | int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); |
|
|
3007 | if (fd >= 0) |
|
|
3008 | return fd; |
|
|
3009 | #endif |
|
|
3010 | return inotify_init (); |
|
|
3011 | } |
|
|
3012 | |
2972 | inline_size void |
3013 | inline_size void |
2973 | infy_init (EV_P) |
3014 | infy_init (EV_P) |
2974 | { |
3015 | { |
2975 | if (fs_fd != -2) |
3016 | if (fs_fd != -2) |
2976 | return; |
3017 | return; |
2977 | |
3018 | |
2978 | fs_fd = -1; |
3019 | fs_fd = -1; |
2979 | |
3020 | |
2980 | check_2625 (EV_A); |
3021 | check_2625 (EV_A); |
2981 | |
3022 | |
2982 | fs_fd = inotify_init (); |
3023 | fs_fd = infy_newfd (); |
2983 | |
3024 | |
2984 | if (fs_fd >= 0) |
3025 | if (fs_fd >= 0) |
2985 | { |
3026 | { |
|
|
3027 | fd_intern (fs_fd); |
2986 | ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); |
3028 | ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); |
2987 | ev_set_priority (&fs_w, EV_MAXPRI); |
3029 | ev_set_priority (&fs_w, EV_MAXPRI); |
2988 | ev_io_start (EV_A_ &fs_w); |
3030 | ev_io_start (EV_A_ &fs_w); |
2989 | } |
3031 | } |
2990 | } |
3032 | } |
… | |
… | |
2995 | int slot; |
3037 | int slot; |
2996 | |
3038 | |
2997 | if (fs_fd < 0) |
3039 | if (fs_fd < 0) |
2998 | return; |
3040 | return; |
2999 | |
3041 | |
|
|
3042 | ev_io_stop (EV_A_ &fs_w); |
3000 | close (fs_fd); |
3043 | close (fs_fd); |
3001 | fs_fd = inotify_init (); |
3044 | fs_fd = infy_newfd (); |
|
|
3045 | |
|
|
3046 | if (fs_fd >= 0) |
|
|
3047 | { |
|
|
3048 | fd_intern (fs_fd); |
|
|
3049 | ev_io_set (&fs_w, fs_fd, EV_READ); |
|
|
3050 | ev_io_start (EV_A_ &fs_w); |
|
|
3051 | } |
3002 | |
3052 | |
3003 | for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) |
3053 | for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) |
3004 | { |
3054 | { |
3005 | WL w_ = fs_hash [slot].head; |
3055 | WL w_ = fs_hash [slot].head; |
3006 | fs_hash [slot].head = 0; |
3056 | fs_hash [slot].head = 0; |
… | |
… | |
3263 | embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) |
3313 | embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) |
3264 | { |
3314 | { |
3265 | ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); |
3315 | ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); |
3266 | |
3316 | |
3267 | { |
3317 | { |
3268 | struct ev_loop *loop = w->other; |
3318 | EV_P = w->other; |
3269 | |
3319 | |
3270 | while (fdchangecnt) |
3320 | while (fdchangecnt) |
3271 | { |
3321 | { |
3272 | fd_reify (EV_A); |
3322 | fd_reify (EV_A); |
3273 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
3323 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
… | |
… | |
3281 | ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); |
3331 | ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); |
3282 | |
3332 | |
3283 | ev_embed_stop (EV_A_ w); |
3333 | ev_embed_stop (EV_A_ w); |
3284 | |
3334 | |
3285 | { |
3335 | { |
3286 | struct ev_loop *loop = w->other; |
3336 | EV_P = w->other; |
3287 | |
3337 | |
3288 | ev_loop_fork (EV_A); |
3338 | ev_loop_fork (EV_A); |
3289 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
3339 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
3290 | } |
3340 | } |
3291 | |
3341 | |
… | |
… | |
3305 | { |
3355 | { |
3306 | if (expect_false (ev_is_active (w))) |
3356 | if (expect_false (ev_is_active (w))) |
3307 | return; |
3357 | return; |
3308 | |
3358 | |
3309 | { |
3359 | { |
3310 | struct ev_loop *loop = w->other; |
3360 | EV_P = w->other; |
3311 | assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
3361 | assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
3312 | ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); |
3362 | ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); |
3313 | } |
3363 | } |
3314 | |
3364 | |
3315 | EV_FREQUENT_CHECK; |
3365 | EV_FREQUENT_CHECK; |
… | |
… | |
3427 | |
3477 | |
3428 | void |
3478 | void |
3429 | ev_async_send (EV_P_ ev_async *w) |
3479 | ev_async_send (EV_P_ ev_async *w) |
3430 | { |
3480 | { |
3431 | w->sent = 1; |
3481 | w->sent = 1; |
3432 | evpipe_write (EV_A_ &gotasync); |
3482 | evpipe_write (EV_A_ &async_pending); |
3433 | } |
3483 | } |
3434 | #endif |
3484 | #endif |
3435 | |
3485 | |
3436 | /*****************************************************************************/ |
3486 | /*****************************************************************************/ |
3437 | |
3487 | |
… | |
… | |
3586 | if (types & EV_CHECK) |
3636 | if (types & EV_CHECK) |
3587 | for (i = checkcnt; i--; ) |
3637 | for (i = checkcnt; i--; ) |
3588 | cb (EV_A_ EV_CHECK, checks [i]); |
3638 | cb (EV_A_ EV_CHECK, checks [i]); |
3589 | |
3639 | |
3590 | if (types & EV_SIGNAL) |
3640 | if (types & EV_SIGNAL) |
3591 | for (i = 0; i < signalmax; ++i) |
3641 | for (i = 0; i < EV_NSIG - 1; ++i) |
3592 | for (wl = signals [i].head; wl; ) |
3642 | for (wl = signals [i].head; wl; ) |
3593 | { |
3643 | { |
3594 | wn = wl->next; |
3644 | wn = wl->next; |
3595 | cb (EV_A_ EV_SIGNAL, wl); |
3645 | cb (EV_A_ EV_SIGNAL, wl); |
3596 | wl = wn; |
3646 | wl = wn; |