… | |
… | |
464 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
464 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
465 | |
465 | |
466 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
466 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
467 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
467 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
468 | |
468 | |
469 | #if __GNUC__ >= 4 |
469 | /* the following are taken from libecb */ |
470 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
470 | /* ecb.h start */ |
471 | # define noinline __attribute__ ((noinline)) |
471 | |
|
|
472 | /* many compilers define _GNUC_ to some versions but then only implement |
|
|
473 | * what their idiot authors think are the "more important" extensions, |
|
|
474 | * causing enourmous grief in return for some better fake benchmark numbers. |
|
|
475 | * or so. |
|
|
476 | * we try to detect these and simply assume they are not gcc - if they have |
|
|
477 | * an issue with that they should have done it right in the first place. |
|
|
478 | */ |
|
|
479 | #ifndef ECB_GCC_VERSION |
|
|
480 | #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__) |
|
|
481 | #define ECB_GCC_VERSION(major,minor) 0 |
|
|
482 | #else |
|
|
483 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
|
|
484 | #endif |
|
|
485 | #endif |
|
|
486 | |
|
|
487 | #if __cplusplus |
|
|
488 | #define ecb_inline static inline |
|
|
489 | #elif ECB_GCC_VERSION(2,5) |
|
|
490 | #define ecb_inline static __inline__ |
|
|
491 | #elif ECB_C99 |
|
|
492 | #define ecb_inline static inline |
472 | #else |
493 | #else |
473 | # define expect(expr,value) (expr) |
494 | #define ecb_inline static |
474 | # define noinline |
|
|
475 | # if __STDC_VERSION__ < 199901L && __GNUC__ < 2 |
|
|
476 | # define inline |
|
|
477 | # endif |
495 | #endif |
478 | #endif |
|
|
479 | |
496 | |
|
|
497 | #if ECB_GCC_VERSION(3,1) |
|
|
498 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
|
|
499 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
|
|
500 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
501 | #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
|
|
502 | #else |
|
|
503 | #define ecb_attribute(attrlist) |
|
|
504 | #define ecb_is_constant(expr) 0 |
|
|
505 | #define ecb_expect(expr,value) (expr) |
|
|
506 | #define ecb_prefetch(addr,rw,locality) |
|
|
507 | #endif |
|
|
508 | |
|
|
509 | #define ecb_noinline ecb_attribute ((__noinline__)) |
|
|
510 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
|
|
511 | #define ecb_unused ecb_attribute ((__unused__)) |
|
|
512 | #define ecb_const ecb_attribute ((__const__)) |
|
|
513 | #define ecb_pure ecb_attribute ((__pure__)) |
|
|
514 | |
|
|
515 | #if ECB_GCC_VERSION(4,3) |
|
|
516 | #define ecb_artificial ecb_attribute ((__artificial__)) |
|
|
517 | #define ecb_hot ecb_attribute ((__hot__)) |
|
|
518 | #define ecb_cold ecb_attribute ((__cold__)) |
|
|
519 | #else |
|
|
520 | #define ecb_artificial |
|
|
521 | #define ecb_hot |
|
|
522 | #define ecb_cold |
|
|
523 | #endif |
|
|
524 | |
|
|
525 | /* put around conditional expressions if you are very sure that the */ |
|
|
526 | /* expression is mostly true or mostly false. note that these return */ |
|
|
527 | /* booleans, not the expression. */ |
480 | #define expect_false(expr) expect ((expr) != 0, 0) |
528 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
481 | #define expect_true(expr) expect ((expr) != 0, 1) |
529 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
|
|
530 | /* ecb.h end */ |
|
|
531 | |
|
|
532 | #define expect_false(cond) ecb_expect_false (cond) |
|
|
533 | #define expect_true(cond) ecb_expect_true (cond) |
|
|
534 | #define noinline ecb_noinline |
|
|
535 | |
482 | #define inline_size static inline |
536 | #define inline_size ecb_inline |
483 | |
537 | |
484 | #if EV_FEATURE_CODE |
538 | #if EV_FEATURE_CODE |
485 | # define inline_speed static inline |
539 | # define inline_speed ecb_inline |
486 | #else |
540 | #else |
487 | # define inline_speed static noinline |
541 | # define inline_speed static noinline |
488 | #endif |
542 | #endif |
489 | |
543 | |
490 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
544 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
… | |
… | |
581 | |
635 | |
582 | #ifdef __linux |
636 | #ifdef __linux |
583 | # include <sys/utsname.h> |
637 | # include <sys/utsname.h> |
584 | #endif |
638 | #endif |
585 | |
639 | |
586 | static unsigned int noinline |
640 | static unsigned int noinline ecb_cold |
587 | ev_linux_version (void) |
641 | ev_linux_version (void) |
588 | { |
642 | { |
589 | #ifdef __linux |
643 | #ifdef __linux |
590 | unsigned int v = 0; |
644 | unsigned int v = 0; |
591 | struct utsname buf; |
645 | struct utsname buf; |
… | |
… | |
620 | } |
674 | } |
621 | |
675 | |
622 | /*****************************************************************************/ |
676 | /*****************************************************************************/ |
623 | |
677 | |
624 | #if EV_AVOID_STDIO |
678 | #if EV_AVOID_STDIO |
625 | static void noinline |
679 | static void noinline ecb_cold |
626 | ev_printerr (const char *msg) |
680 | ev_printerr (const char *msg) |
627 | { |
681 | { |
628 | write (STDERR_FILENO, msg, strlen (msg)); |
682 | write (STDERR_FILENO, msg, strlen (msg)); |
629 | } |
683 | } |
630 | #endif |
684 | #endif |
631 | |
685 | |
632 | static void (*syserr_cb)(const char *msg); |
686 | static void (*syserr_cb)(const char *msg); |
633 | |
687 | |
634 | void |
688 | void ecb_cold |
635 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
689 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
636 | { |
690 | { |
637 | syserr_cb = cb; |
691 | syserr_cb = cb; |
638 | } |
692 | } |
639 | |
693 | |
640 | static void noinline |
694 | static void noinline ecb_cold |
641 | ev_syserr (const char *msg) |
695 | ev_syserr (const char *msg) |
642 | { |
696 | { |
643 | if (!msg) |
697 | if (!msg) |
644 | msg = "(libev) system error"; |
698 | msg = "(libev) system error"; |
645 | |
699 | |
… | |
… | |
678 | #endif |
732 | #endif |
679 | } |
733 | } |
680 | |
734 | |
681 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
735 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
682 | |
736 | |
683 | void |
737 | void ecb_cold |
684 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
738 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
685 | { |
739 | { |
686 | alloc = cb; |
740 | alloc = cb; |
687 | } |
741 | } |
688 | |
742 | |
… | |
… | |
870 | select (0, 0, 0, 0, &tv); |
924 | select (0, 0, 0, 0, &tv); |
871 | #endif |
925 | #endif |
872 | } |
926 | } |
873 | } |
927 | } |
874 | |
928 | |
875 | inline_speed int |
|
|
876 | ev_timeout_to_ms (ev_tstamp timeout) |
|
|
877 | { |
|
|
878 | int ms = timeout * 1000. + .999999; |
|
|
879 | |
|
|
880 | return expect_true (ms) ? ms : timeout < 1e-6 ? 0 : 1; |
|
|
881 | } |
|
|
882 | |
|
|
883 | /*****************************************************************************/ |
929 | /*****************************************************************************/ |
884 | |
930 | |
885 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
931 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
886 | |
932 | |
887 | /* find a suitable new size for the given array, */ |
933 | /* find a suitable new size for the given array, */ |
… | |
… | |
905 | } |
951 | } |
906 | |
952 | |
907 | return ncur; |
953 | return ncur; |
908 | } |
954 | } |
909 | |
955 | |
910 | static noinline void * |
956 | static void * noinline ecb_cold |
911 | array_realloc (int elem, void *base, int *cur, int cnt) |
957 | array_realloc (int elem, void *base, int *cur, int cnt) |
912 | { |
958 | { |
913 | *cur = array_nextsize (elem, *cur, cnt); |
959 | *cur = array_nextsize (elem, *cur, cnt); |
914 | return ev_realloc (base, elem * *cur); |
960 | return ev_realloc (base, elem * *cur); |
915 | } |
961 | } |
… | |
… | |
1098 | fdchanges [fdchangecnt - 1] = fd; |
1144 | fdchanges [fdchangecnt - 1] = fd; |
1099 | } |
1145 | } |
1100 | } |
1146 | } |
1101 | |
1147 | |
1102 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
1148 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
1103 | inline_speed void |
1149 | inline_speed void ecb_cold |
1104 | fd_kill (EV_P_ int fd) |
1150 | fd_kill (EV_P_ int fd) |
1105 | { |
1151 | { |
1106 | ev_io *w; |
1152 | ev_io *w; |
1107 | |
1153 | |
1108 | while ((w = (ev_io *)anfds [fd].head)) |
1154 | while ((w = (ev_io *)anfds [fd].head)) |
… | |
… | |
1111 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1157 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1112 | } |
1158 | } |
1113 | } |
1159 | } |
1114 | |
1160 | |
1115 | /* check whether the given fd is actually valid, for error recovery */ |
1161 | /* check whether the given fd is actually valid, for error recovery */ |
1116 | inline_size int |
1162 | inline_size int ecb_cold |
1117 | fd_valid (int fd) |
1163 | fd_valid (int fd) |
1118 | { |
1164 | { |
1119 | #ifdef _WIN32 |
1165 | #ifdef _WIN32 |
1120 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1166 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1121 | #else |
1167 | #else |
1122 | return fcntl (fd, F_GETFD) != -1; |
1168 | return fcntl (fd, F_GETFD) != -1; |
1123 | #endif |
1169 | #endif |
1124 | } |
1170 | } |
1125 | |
1171 | |
1126 | /* called on EBADF to verify fds */ |
1172 | /* called on EBADF to verify fds */ |
1127 | static void noinline |
1173 | static void noinline ecb_cold |
1128 | fd_ebadf (EV_P) |
1174 | fd_ebadf (EV_P) |
1129 | { |
1175 | { |
1130 | int fd; |
1176 | int fd; |
1131 | |
1177 | |
1132 | for (fd = 0; fd < anfdmax; ++fd) |
1178 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
1134 | if (!fd_valid (fd) && errno == EBADF) |
1180 | if (!fd_valid (fd) && errno == EBADF) |
1135 | fd_kill (EV_A_ fd); |
1181 | fd_kill (EV_A_ fd); |
1136 | } |
1182 | } |
1137 | |
1183 | |
1138 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
1184 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
1139 | static void noinline |
1185 | static void noinline ecb_cold |
1140 | fd_enomem (EV_P) |
1186 | fd_enomem (EV_P) |
1141 | { |
1187 | { |
1142 | int fd; |
1188 | int fd; |
1143 | |
1189 | |
1144 | for (fd = anfdmax; fd--; ) |
1190 | for (fd = anfdmax; fd--; ) |
… | |
… | |
1339 | |
1385 | |
1340 | /*****************************************************************************/ |
1386 | /*****************************************************************************/ |
1341 | |
1387 | |
1342 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1388 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1343 | |
1389 | |
1344 | static void noinline |
1390 | static void noinline ecb_cold |
1345 | evpipe_init (EV_P) |
1391 | evpipe_init (EV_P) |
1346 | { |
1392 | { |
1347 | if (!ev_is_active (&pipe_w)) |
1393 | if (!ev_is_active (&pipe_w)) |
1348 | { |
1394 | { |
1349 | # if EV_USE_EVENTFD |
1395 | # if EV_USE_EVENTFD |
… | |
… | |
1371 | ev_io_start (EV_A_ &pipe_w); |
1417 | ev_io_start (EV_A_ &pipe_w); |
1372 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1418 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1373 | } |
1419 | } |
1374 | } |
1420 | } |
1375 | |
1421 | |
1376 | inline_size void |
1422 | inline_speed void |
1377 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1423 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1378 | { |
1424 | { |
1379 | if (!*flag) |
1425 | if (!*flag) |
1380 | { |
1426 | { |
1381 | int old_errno = errno; /* save errno because write might clobber it */ |
|
|
1382 | char dummy; |
|
|
1383 | |
|
|
1384 | *flag = 1; |
1427 | *flag = 1; |
1385 | |
1428 | |
|
|
1429 | pipe_write_skipped = 1; |
|
|
1430 | |
|
|
1431 | if (pipe_write_wanted) |
|
|
1432 | { |
|
|
1433 | int old_errno; |
|
|
1434 | |
|
|
1435 | pipe_write_skipped = 0; |
|
|
1436 | |
|
|
1437 | old_errno = errno; /* save errno because write will clobber it */ |
|
|
1438 | |
1386 | #if EV_USE_EVENTFD |
1439 | #if EV_USE_EVENTFD |
1387 | if (evfd >= 0) |
1440 | if (evfd >= 0) |
1388 | { |
1441 | { |
1389 | uint64_t counter = 1; |
1442 | uint64_t counter = 1; |
1390 | write (evfd, &counter, sizeof (uint64_t)); |
1443 | write (evfd, &counter, sizeof (uint64_t)); |
|
|
1444 | } |
|
|
1445 | else |
|
|
1446 | #endif |
|
|
1447 | { |
|
|
1448 | /* win32 people keep sending patches that change this write() to send() */ |
|
|
1449 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
|
|
1450 | /* so when you think this write should be a send instead, please find out */ |
|
|
1451 | /* where your send() is from - it's definitely not the microsoft send, and */ |
|
|
1452 | /* tell me. thank you. */ |
|
|
1453 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
1454 | } |
|
|
1455 | |
|
|
1456 | errno = old_errno; |
1391 | } |
1457 | } |
1392 | else |
|
|
1393 | #endif |
|
|
1394 | /* win32 people keep sending patches that change this write() to send() */ |
|
|
1395 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
|
|
1396 | /* so when you think this write should be a send instead, please find out */ |
|
|
1397 | /* where your send() is from - it's definitely not the microsoft send, and */ |
|
|
1398 | /* tell me. thank you. */ |
|
|
1399 | write (evpipe [1], &dummy, 1); |
|
|
1400 | |
|
|
1401 | errno = old_errno; |
|
|
1402 | } |
1458 | } |
1403 | } |
1459 | } |
1404 | |
1460 | |
1405 | /* called whenever the libev signal pipe */ |
1461 | /* called whenever the libev signal pipe */ |
1406 | /* got some events (signal, async) */ |
1462 | /* got some events (signal, async) */ |
1407 | static void |
1463 | static void |
1408 | pipecb (EV_P_ ev_io *iow, int revents) |
1464 | pipecb (EV_P_ ev_io *iow, int revents) |
1409 | { |
1465 | { |
1410 | int i; |
1466 | int i; |
1411 | |
1467 | |
|
|
1468 | if (revents & EV_READ) |
|
|
1469 | { |
1412 | #if EV_USE_EVENTFD |
1470 | #if EV_USE_EVENTFD |
1413 | if (evfd >= 0) |
1471 | if (evfd >= 0) |
1414 | { |
1472 | { |
1415 | uint64_t counter; |
1473 | uint64_t counter; |
1416 | read (evfd, &counter, sizeof (uint64_t)); |
1474 | read (evfd, &counter, sizeof (uint64_t)); |
1417 | } |
1475 | } |
1418 | else |
1476 | else |
1419 | #endif |
1477 | #endif |
1420 | { |
1478 | { |
1421 | char dummy; |
1479 | char dummy; |
1422 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1480 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1423 | read (evpipe [0], &dummy, 1); |
1481 | read (evpipe [0], &dummy, 1); |
|
|
1482 | } |
1424 | } |
1483 | } |
|
|
1484 | |
|
|
1485 | pipe_write_skipped = 0; |
1425 | |
1486 | |
1426 | #if EV_SIGNAL_ENABLE |
1487 | #if EV_SIGNAL_ENABLE |
1427 | if (sig_pending) |
1488 | if (sig_pending) |
1428 | { |
1489 | { |
1429 | sig_pending = 0; |
1490 | sig_pending = 0; |
… | |
… | |
1458 | EV_P = signals [signum - 1].loop; |
1519 | EV_P = signals [signum - 1].loop; |
1459 | |
1520 | |
1460 | if (!EV_A) |
1521 | if (!EV_A) |
1461 | return; |
1522 | return; |
1462 | #endif |
1523 | #endif |
|
|
1524 | |
|
|
1525 | evpipe_init (EV_A); |
1463 | |
1526 | |
1464 | signals [signum - 1].pending = 1; |
1527 | signals [signum - 1].pending = 1; |
1465 | evpipe_write (EV_A_ &sig_pending); |
1528 | evpipe_write (EV_A_ &sig_pending); |
1466 | } |
1529 | } |
1467 | |
1530 | |
… | |
… | |
1599 | #endif |
1662 | #endif |
1600 | #if EV_USE_SELECT |
1663 | #if EV_USE_SELECT |
1601 | # include "ev_select.c" |
1664 | # include "ev_select.c" |
1602 | #endif |
1665 | #endif |
1603 | |
1666 | |
1604 | int |
1667 | int ecb_cold |
1605 | ev_version_major (void) |
1668 | ev_version_major (void) |
1606 | { |
1669 | { |
1607 | return EV_VERSION_MAJOR; |
1670 | return EV_VERSION_MAJOR; |
1608 | } |
1671 | } |
1609 | |
1672 | |
1610 | int |
1673 | int ecb_cold |
1611 | ev_version_minor (void) |
1674 | ev_version_minor (void) |
1612 | { |
1675 | { |
1613 | return EV_VERSION_MINOR; |
1676 | return EV_VERSION_MINOR; |
1614 | } |
1677 | } |
1615 | |
1678 | |
1616 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1679 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1617 | int inline_size |
1680 | int inline_size ecb_cold |
1618 | enable_secure (void) |
1681 | enable_secure (void) |
1619 | { |
1682 | { |
1620 | #ifdef _WIN32 |
1683 | #ifdef _WIN32 |
1621 | return 0; |
1684 | return 0; |
1622 | #else |
1685 | #else |
1623 | return getuid () != geteuid () |
1686 | return getuid () != geteuid () |
1624 | || getgid () != getegid (); |
1687 | || getgid () != getegid (); |
1625 | #endif |
1688 | #endif |
1626 | } |
1689 | } |
1627 | |
1690 | |
1628 | unsigned int |
1691 | unsigned int ecb_cold |
1629 | ev_supported_backends (void) |
1692 | ev_supported_backends (void) |
1630 | { |
1693 | { |
1631 | unsigned int flags = 0; |
1694 | unsigned int flags = 0; |
1632 | |
1695 | |
1633 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
1696 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
… | |
… | |
1637 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1700 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1638 | |
1701 | |
1639 | return flags; |
1702 | return flags; |
1640 | } |
1703 | } |
1641 | |
1704 | |
1642 | unsigned int |
1705 | unsigned int ecb_cold |
1643 | ev_recommended_backends (void) |
1706 | ev_recommended_backends (void) |
1644 | { |
1707 | { |
1645 | unsigned int flags = ev_supported_backends (); |
1708 | unsigned int flags = ev_supported_backends (); |
1646 | |
1709 | |
1647 | #ifndef __NetBSD__ |
1710 | #ifndef __NetBSD__ |
… | |
… | |
1659 | #endif |
1722 | #endif |
1660 | |
1723 | |
1661 | return flags; |
1724 | return flags; |
1662 | } |
1725 | } |
1663 | |
1726 | |
1664 | unsigned int |
1727 | unsigned int ecb_cold |
1665 | ev_embeddable_backends (void) |
1728 | ev_embeddable_backends (void) |
1666 | { |
1729 | { |
1667 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1730 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1668 | |
1731 | |
1669 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
1732 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
… | |
… | |
1714 | ev_userdata (EV_P) |
1777 | ev_userdata (EV_P) |
1715 | { |
1778 | { |
1716 | return userdata; |
1779 | return userdata; |
1717 | } |
1780 | } |
1718 | |
1781 | |
|
|
1782 | void |
1719 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1783 | ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1720 | { |
1784 | { |
1721 | invoke_cb = invoke_pending_cb; |
1785 | invoke_cb = invoke_pending_cb; |
1722 | } |
1786 | } |
1723 | |
1787 | |
|
|
1788 | void |
1724 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1789 | ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1725 | { |
1790 | { |
1726 | release_cb = release; |
1791 | release_cb = release; |
1727 | acquire_cb = acquire; |
1792 | acquire_cb = acquire; |
1728 | } |
1793 | } |
1729 | #endif |
1794 | #endif |
1730 | |
1795 | |
1731 | /* initialise a loop structure, must be zero-initialised */ |
1796 | /* initialise a loop structure, must be zero-initialised */ |
1732 | static void noinline |
1797 | static void noinline ecb_cold |
1733 | loop_init (EV_P_ unsigned int flags) |
1798 | loop_init (EV_P_ unsigned int flags) |
1734 | { |
1799 | { |
1735 | if (!backend) |
1800 | if (!backend) |
1736 | { |
1801 | { |
1737 | origflags = flags; |
1802 | origflags = flags; |
… | |
… | |
1765 | if (!(flags & EVFLAG_NOENV) |
1830 | if (!(flags & EVFLAG_NOENV) |
1766 | && !enable_secure () |
1831 | && !enable_secure () |
1767 | && getenv ("LIBEV_FLAGS")) |
1832 | && getenv ("LIBEV_FLAGS")) |
1768 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1833 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1769 | |
1834 | |
1770 | ev_rt_now = ev_time (); |
1835 | ev_rt_now = ev_time (); |
1771 | mn_now = get_clock (); |
1836 | mn_now = get_clock (); |
1772 | now_floor = mn_now; |
1837 | now_floor = mn_now; |
1773 | rtmn_diff = ev_rt_now - mn_now; |
1838 | rtmn_diff = ev_rt_now - mn_now; |
1774 | #if EV_FEATURE_API |
1839 | #if EV_FEATURE_API |
1775 | invoke_cb = ev_invoke_pending; |
1840 | invoke_cb = ev_invoke_pending; |
1776 | #endif |
1841 | #endif |
1777 | |
1842 | |
1778 | io_blocktime = 0.; |
1843 | io_blocktime = 0.; |
1779 | timeout_blocktime = 0.; |
1844 | timeout_blocktime = 0.; |
1780 | backend = 0; |
1845 | backend = 0; |
1781 | backend_fd = -1; |
1846 | backend_fd = -1; |
1782 | sig_pending = 0; |
1847 | sig_pending = 0; |
1783 | #if EV_ASYNC_ENABLE |
1848 | #if EV_ASYNC_ENABLE |
1784 | async_pending = 0; |
1849 | async_pending = 0; |
1785 | #endif |
1850 | #endif |
|
|
1851 | pipe_write_skipped = 0; |
|
|
1852 | pipe_write_wanted = 0; |
1786 | #if EV_USE_INOTIFY |
1853 | #if EV_USE_INOTIFY |
1787 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1854 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1788 | #endif |
1855 | #endif |
1789 | #if EV_USE_SIGNALFD |
1856 | #if EV_USE_SIGNALFD |
1790 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1857 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1791 | #endif |
1858 | #endif |
1792 | |
1859 | |
1793 | if (!(flags & EVBACKEND_MASK)) |
1860 | if (!(flags & EVBACKEND_MASK)) |
1794 | flags |= ev_recommended_backends (); |
1861 | flags |= ev_recommended_backends (); |
1795 | |
1862 | |
… | |
… | |
1820 | #endif |
1887 | #endif |
1821 | } |
1888 | } |
1822 | } |
1889 | } |
1823 | |
1890 | |
1824 | /* free up a loop structure */ |
1891 | /* free up a loop structure */ |
1825 | void |
1892 | void ecb_cold |
1826 | ev_loop_destroy (EV_P) |
1893 | ev_loop_destroy (EV_P) |
1827 | { |
1894 | { |
1828 | int i; |
1895 | int i; |
1829 | |
1896 | |
1830 | #if EV_MULTIPLICITY |
1897 | #if EV_MULTIPLICITY |
… | |
… | |
1960 | infy_fork (EV_A); |
2027 | infy_fork (EV_A); |
1961 | #endif |
2028 | #endif |
1962 | |
2029 | |
1963 | if (ev_is_active (&pipe_w)) |
2030 | if (ev_is_active (&pipe_w)) |
1964 | { |
2031 | { |
1965 | /* this "locks" the handlers against writing to the pipe */ |
2032 | /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ |
1966 | /* while we modify the fd vars */ |
|
|
1967 | sig_pending = 1; |
|
|
1968 | #if EV_ASYNC_ENABLE |
|
|
1969 | async_pending = 1; |
|
|
1970 | #endif |
|
|
1971 | |
2033 | |
1972 | ev_ref (EV_A); |
2034 | ev_ref (EV_A); |
1973 | ev_io_stop (EV_A_ &pipe_w); |
2035 | ev_io_stop (EV_A_ &pipe_w); |
1974 | |
2036 | |
1975 | #if EV_USE_EVENTFD |
2037 | #if EV_USE_EVENTFD |
… | |
… | |
1993 | postfork = 0; |
2055 | postfork = 0; |
1994 | } |
2056 | } |
1995 | |
2057 | |
1996 | #if EV_MULTIPLICITY |
2058 | #if EV_MULTIPLICITY |
1997 | |
2059 | |
1998 | struct ev_loop * |
2060 | struct ev_loop * ecb_cold |
1999 | ev_loop_new (unsigned int flags) |
2061 | ev_loop_new (unsigned int flags) |
2000 | { |
2062 | { |
2001 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2063 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2002 | |
2064 | |
2003 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2065 | memset (EV_A, 0, sizeof (struct ev_loop)); |
… | |
… | |
2011 | } |
2073 | } |
2012 | |
2074 | |
2013 | #endif /* multiplicity */ |
2075 | #endif /* multiplicity */ |
2014 | |
2076 | |
2015 | #if EV_VERIFY |
2077 | #if EV_VERIFY |
2016 | static void noinline |
2078 | static void noinline ecb_cold |
2017 | verify_watcher (EV_P_ W w) |
2079 | verify_watcher (EV_P_ W w) |
2018 | { |
2080 | { |
2019 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2081 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2020 | |
2082 | |
2021 | if (w->pending) |
2083 | if (w->pending) |
2022 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2084 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2023 | } |
2085 | } |
2024 | |
2086 | |
2025 | static void noinline |
2087 | static void noinline ecb_cold |
2026 | verify_heap (EV_P_ ANHE *heap, int N) |
2088 | verify_heap (EV_P_ ANHE *heap, int N) |
2027 | { |
2089 | { |
2028 | int i; |
2090 | int i; |
2029 | |
2091 | |
2030 | for (i = HEAP0; i < N + HEAP0; ++i) |
2092 | for (i = HEAP0; i < N + HEAP0; ++i) |
… | |
… | |
2035 | |
2097 | |
2036 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2098 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2037 | } |
2099 | } |
2038 | } |
2100 | } |
2039 | |
2101 | |
2040 | static void noinline |
2102 | static void noinline ecb_cold |
2041 | array_verify (EV_P_ W *ws, int cnt) |
2103 | array_verify (EV_P_ W *ws, int cnt) |
2042 | { |
2104 | { |
2043 | while (cnt--) |
2105 | while (cnt--) |
2044 | { |
2106 | { |
2045 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
2107 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
… | |
… | |
2047 | } |
2109 | } |
2048 | } |
2110 | } |
2049 | #endif |
2111 | #endif |
2050 | |
2112 | |
2051 | #if EV_FEATURE_API |
2113 | #if EV_FEATURE_API |
2052 | void |
2114 | void ecb_cold |
2053 | ev_verify (EV_P) |
2115 | ev_verify (EV_P) |
2054 | { |
2116 | { |
2055 | #if EV_VERIFY |
2117 | #if EV_VERIFY |
2056 | int i; |
2118 | int i; |
2057 | WL w; |
2119 | WL w; |
… | |
… | |
2123 | #endif |
2185 | #endif |
2124 | } |
2186 | } |
2125 | #endif |
2187 | #endif |
2126 | |
2188 | |
2127 | #if EV_MULTIPLICITY |
2189 | #if EV_MULTIPLICITY |
2128 | struct ev_loop * |
2190 | struct ev_loop * ecb_cold |
2129 | #else |
2191 | #else |
2130 | int |
2192 | int |
2131 | #endif |
2193 | #endif |
2132 | ev_default_loop (unsigned int flags) |
2194 | ev_default_loop (unsigned int flags) |
2133 | { |
2195 | { |
… | |
… | |
2332 | } |
2394 | } |
2333 | } |
2395 | } |
2334 | |
2396 | |
2335 | /* simply recalculate all periodics */ |
2397 | /* simply recalculate all periodics */ |
2336 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2398 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2337 | static void noinline |
2399 | static void noinline ecb_cold |
2338 | periodics_reschedule (EV_P) |
2400 | periodics_reschedule (EV_P) |
2339 | { |
2401 | { |
2340 | int i; |
2402 | int i; |
2341 | |
2403 | |
2342 | /* adjust periodics after time jump */ |
2404 | /* adjust periodics after time jump */ |
… | |
… | |
2355 | reheap (periodics, periodiccnt); |
2417 | reheap (periodics, periodiccnt); |
2356 | } |
2418 | } |
2357 | #endif |
2419 | #endif |
2358 | |
2420 | |
2359 | /* adjust all timers by a given offset */ |
2421 | /* adjust all timers by a given offset */ |
2360 | static void noinline |
2422 | static void noinline ecb_cold |
2361 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2423 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2362 | { |
2424 | { |
2363 | int i; |
2425 | int i; |
2364 | |
2426 | |
2365 | for (i = 0; i < timercnt; ++i) |
2427 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
2507 | ev_tstamp prev_mn_now = mn_now; |
2569 | ev_tstamp prev_mn_now = mn_now; |
2508 | |
2570 | |
2509 | /* update time to cancel out callback processing overhead */ |
2571 | /* update time to cancel out callback processing overhead */ |
2510 | time_update (EV_A_ 1e100); |
2572 | time_update (EV_A_ 1e100); |
2511 | |
2573 | |
|
|
2574 | /* from now on, we want a pipe-wake-up */ |
|
|
2575 | pipe_write_wanted = 1; |
|
|
2576 | |
2512 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) |
2577 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2513 | { |
2578 | { |
2514 | waittime = MAX_BLOCKTIME; |
2579 | waittime = MAX_BLOCKTIME; |
2515 | |
2580 | |
2516 | if (timercnt) |
2581 | if (timercnt) |
2517 | { |
2582 | { |
2518 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2583 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
2519 | if (waittime > to) waittime = to; |
2584 | if (waittime > to) waittime = to; |
2520 | } |
2585 | } |
2521 | |
2586 | |
2522 | #if EV_PERIODIC_ENABLE |
2587 | #if EV_PERIODIC_ENABLE |
2523 | if (periodiccnt) |
2588 | if (periodiccnt) |
2524 | { |
2589 | { |
2525 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2590 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now; |
2526 | if (waittime > to) waittime = to; |
2591 | if (waittime > to) waittime = to; |
2527 | } |
2592 | } |
2528 | #endif |
2593 | #endif |
2529 | |
2594 | |
2530 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2595 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2531 | if (expect_false (waittime < timeout_blocktime)) |
2596 | if (expect_false (waittime < timeout_blocktime)) |
2532 | waittime = timeout_blocktime; |
2597 | waittime = timeout_blocktime; |
|
|
2598 | |
|
|
2599 | /* at this point, we NEED to wait, so we have to ensure */ |
|
|
2600 | /* to pass a minimum nonzero value to the backend */ |
|
|
2601 | if (expect_false (waittime < backend_mintime)) |
|
|
2602 | waittime = backend_mintime; |
2533 | |
2603 | |
2534 | /* extra check because io_blocktime is commonly 0 */ |
2604 | /* extra check because io_blocktime is commonly 0 */ |
2535 | if (expect_false (io_blocktime)) |
2605 | if (expect_false (io_blocktime)) |
2536 | { |
2606 | { |
2537 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
2607 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
2538 | |
2608 | |
2539 | if (sleeptime > waittime - backend_fudge) |
2609 | if (sleeptime > waittime - backend_mintime) |
2540 | sleeptime = waittime - backend_fudge; |
2610 | sleeptime = waittime - backend_mintime; |
2541 | |
2611 | |
2542 | if (expect_true (sleeptime > 0.)) |
2612 | if (expect_true (sleeptime > 0.)) |
2543 | { |
2613 | { |
2544 | ev_sleep (sleeptime); |
2614 | ev_sleep (sleeptime); |
2545 | waittime -= sleeptime; |
2615 | waittime -= sleeptime; |
… | |
… | |
2551 | ++loop_count; |
2621 | ++loop_count; |
2552 | #endif |
2622 | #endif |
2553 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2623 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2554 | backend_poll (EV_A_ waittime); |
2624 | backend_poll (EV_A_ waittime); |
2555 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2625 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
|
|
2626 | |
|
|
2627 | pipe_write_wanted = 0; |
|
|
2628 | |
|
|
2629 | if (pipe_write_skipped) |
|
|
2630 | { |
|
|
2631 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
|
|
2632 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
|
|
2633 | } |
|
|
2634 | |
2556 | |
2635 | |
2557 | /* update ev_rt_now, do magic */ |
2636 | /* update ev_rt_now, do magic */ |
2558 | time_update (EV_A_ waittime + sleeptime); |
2637 | time_update (EV_A_ waittime + sleeptime); |
2559 | } |
2638 | } |
2560 | |
2639 | |
… | |
… | |
3214 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3293 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3215 | ofs += sizeof (struct inotify_event) + ev->len; |
3294 | ofs += sizeof (struct inotify_event) + ev->len; |
3216 | } |
3295 | } |
3217 | } |
3296 | } |
3218 | |
3297 | |
3219 | inline_size void |
3298 | inline_size void ecb_cold |
3220 | ev_check_2625 (EV_P) |
3299 | ev_check_2625 (EV_P) |
3221 | { |
3300 | { |
3222 | /* kernels < 2.6.25 are borked |
3301 | /* kernels < 2.6.25 are borked |
3223 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3302 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3224 | */ |
3303 | */ |
… | |
… | |
3849 | } |
3928 | } |
3850 | |
3929 | |
3851 | /*****************************************************************************/ |
3930 | /*****************************************************************************/ |
3852 | |
3931 | |
3853 | #if EV_WALK_ENABLE |
3932 | #if EV_WALK_ENABLE |
3854 | void |
3933 | void ecb_cold |
3855 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3934 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3856 | { |
3935 | { |
3857 | int i, j; |
3936 | int i, j; |
3858 | ev_watcher_list *wl, *wn; |
3937 | ev_watcher_list *wl, *wn; |
3859 | |
3938 | |