… | |
… | |
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 | |
… | |
… | |
897 | } |
951 | } |
898 | |
952 | |
899 | return ncur; |
953 | return ncur; |
900 | } |
954 | } |
901 | |
955 | |
902 | static noinline void * |
956 | static void * noinline ecb_cold |
903 | array_realloc (int elem, void *base, int *cur, int cnt) |
957 | array_realloc (int elem, void *base, int *cur, int cnt) |
904 | { |
958 | { |
905 | *cur = array_nextsize (elem, *cur, cnt); |
959 | *cur = array_nextsize (elem, *cur, cnt); |
906 | return ev_realloc (base, elem * *cur); |
960 | return ev_realloc (base, elem * *cur); |
907 | } |
961 | } |
… | |
… | |
1090 | fdchanges [fdchangecnt - 1] = fd; |
1144 | fdchanges [fdchangecnt - 1] = fd; |
1091 | } |
1145 | } |
1092 | } |
1146 | } |
1093 | |
1147 | |
1094 | /* 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 */ |
1095 | inline_speed void |
1149 | inline_speed void ecb_cold |
1096 | fd_kill (EV_P_ int fd) |
1150 | fd_kill (EV_P_ int fd) |
1097 | { |
1151 | { |
1098 | ev_io *w; |
1152 | ev_io *w; |
1099 | |
1153 | |
1100 | while ((w = (ev_io *)anfds [fd].head)) |
1154 | while ((w = (ev_io *)anfds [fd].head)) |
… | |
… | |
1103 | 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); |
1104 | } |
1158 | } |
1105 | } |
1159 | } |
1106 | |
1160 | |
1107 | /* check whether the given fd is actually valid, for error recovery */ |
1161 | /* check whether the given fd is actually valid, for error recovery */ |
1108 | inline_size int |
1162 | inline_size int ecb_cold |
1109 | fd_valid (int fd) |
1163 | fd_valid (int fd) |
1110 | { |
1164 | { |
1111 | #ifdef _WIN32 |
1165 | #ifdef _WIN32 |
1112 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1166 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1113 | #else |
1167 | #else |
1114 | return fcntl (fd, F_GETFD) != -1; |
1168 | return fcntl (fd, F_GETFD) != -1; |
1115 | #endif |
1169 | #endif |
1116 | } |
1170 | } |
1117 | |
1171 | |
1118 | /* called on EBADF to verify fds */ |
1172 | /* called on EBADF to verify fds */ |
1119 | static void noinline |
1173 | static void noinline ecb_cold |
1120 | fd_ebadf (EV_P) |
1174 | fd_ebadf (EV_P) |
1121 | { |
1175 | { |
1122 | int fd; |
1176 | int fd; |
1123 | |
1177 | |
1124 | for (fd = 0; fd < anfdmax; ++fd) |
1178 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
1126 | if (!fd_valid (fd) && errno == EBADF) |
1180 | if (!fd_valid (fd) && errno == EBADF) |
1127 | fd_kill (EV_A_ fd); |
1181 | fd_kill (EV_A_ fd); |
1128 | } |
1182 | } |
1129 | |
1183 | |
1130 | /* 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 */ |
1131 | static void noinline |
1185 | static void noinline ecb_cold |
1132 | fd_enomem (EV_P) |
1186 | fd_enomem (EV_P) |
1133 | { |
1187 | { |
1134 | int fd; |
1188 | int fd; |
1135 | |
1189 | |
1136 | for (fd = anfdmax; fd--; ) |
1190 | for (fd = anfdmax; fd--; ) |
… | |
… | |
1331 | |
1385 | |
1332 | /*****************************************************************************/ |
1386 | /*****************************************************************************/ |
1333 | |
1387 | |
1334 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1388 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1335 | |
1389 | |
1336 | static void noinline |
1390 | static void noinline ecb_cold |
1337 | evpipe_init (EV_P) |
1391 | evpipe_init (EV_P) |
1338 | { |
1392 | { |
1339 | if (!ev_is_active (&pipe_w)) |
1393 | if (!ev_is_active (&pipe_w)) |
1340 | { |
1394 | { |
1341 | # if EV_USE_EVENTFD |
1395 | # if EV_USE_EVENTFD |
… | |
… | |
1368 | inline_size void |
1422 | inline_size void |
1369 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1423 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1370 | { |
1424 | { |
1371 | if (!*flag) |
1425 | if (!*flag) |
1372 | { |
1426 | { |
1373 | int old_errno = errno; /* save errno because write might clobber it */ |
|
|
1374 | char dummy; |
|
|
1375 | |
|
|
1376 | *flag = 1; |
1427 | *flag = 1; |
1377 | |
1428 | |
|
|
1429 | pipe_write_skipped = 1; |
|
|
1430 | |
|
|
1431 | if (pipe_write_wanted) |
|
|
1432 | { |
|
|
1433 | int old_errno = errno; /* save errno because write will clobber it */ |
|
|
1434 | char dummy; |
|
|
1435 | |
|
|
1436 | pipe_write_skipped = 0; |
|
|
1437 | |
1378 | #if EV_USE_EVENTFD |
1438 | #if EV_USE_EVENTFD |
1379 | if (evfd >= 0) |
1439 | if (evfd >= 0) |
1380 | { |
1440 | { |
1381 | uint64_t counter = 1; |
1441 | uint64_t counter = 1; |
1382 | write (evfd, &counter, sizeof (uint64_t)); |
1442 | write (evfd, &counter, sizeof (uint64_t)); |
|
|
1443 | } |
|
|
1444 | else |
|
|
1445 | #endif |
|
|
1446 | { |
|
|
1447 | /* win32 people keep sending patches that change this write() to send() */ |
|
|
1448 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
|
|
1449 | /* so when you think this write should be a send instead, please find out */ |
|
|
1450 | /* where your send() is from - it's definitely not the microsoft send, and */ |
|
|
1451 | /* tell me. thank you. */ |
|
|
1452 | write (evpipe [1], &dummy, 1); |
|
|
1453 | } |
|
|
1454 | |
|
|
1455 | errno = old_errno; |
1383 | } |
1456 | } |
1384 | else |
|
|
1385 | #endif |
|
|
1386 | /* win32 people keep sending patches that change this write() to send() */ |
|
|
1387 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
|
|
1388 | /* so when you think this write should be a send instead, please find out */ |
|
|
1389 | /* where your send() is from - it's definitely not the microsoft send, and */ |
|
|
1390 | /* tell me. thank you. */ |
|
|
1391 | write (evpipe [1], &dummy, 1); |
|
|
1392 | |
|
|
1393 | errno = old_errno; |
|
|
1394 | } |
1457 | } |
1395 | } |
1458 | } |
1396 | |
1459 | |
1397 | /* called whenever the libev signal pipe */ |
1460 | /* called whenever the libev signal pipe */ |
1398 | /* got some events (signal, async) */ |
1461 | /* got some events (signal, async) */ |
1399 | static void |
1462 | static void |
1400 | pipecb (EV_P_ ev_io *iow, int revents) |
1463 | pipecb (EV_P_ ev_io *iow, int revents) |
1401 | { |
1464 | { |
1402 | int i; |
1465 | int i; |
1403 | |
1466 | |
|
|
1467 | if (revents & EV_READ) |
|
|
1468 | { |
1404 | #if EV_USE_EVENTFD |
1469 | #if EV_USE_EVENTFD |
1405 | if (evfd >= 0) |
1470 | if (evfd >= 0) |
1406 | { |
1471 | { |
1407 | uint64_t counter; |
1472 | uint64_t counter; |
1408 | read (evfd, &counter, sizeof (uint64_t)); |
1473 | read (evfd, &counter, sizeof (uint64_t)); |
1409 | } |
1474 | } |
1410 | else |
1475 | else |
1411 | #endif |
1476 | #endif |
1412 | { |
1477 | { |
1413 | char dummy; |
1478 | char dummy; |
1414 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1479 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1415 | read (evpipe [0], &dummy, 1); |
1480 | read (evpipe [0], &dummy, 1); |
|
|
1481 | } |
1416 | } |
1482 | } |
|
|
1483 | |
|
|
1484 | pipe_write_skipped = 0; |
1417 | |
1485 | |
1418 | #if EV_SIGNAL_ENABLE |
1486 | #if EV_SIGNAL_ENABLE |
1419 | if (sig_pending) |
1487 | if (sig_pending) |
1420 | { |
1488 | { |
1421 | sig_pending = 0; |
1489 | sig_pending = 0; |
… | |
… | |
1450 | EV_P = signals [signum - 1].loop; |
1518 | EV_P = signals [signum - 1].loop; |
1451 | |
1519 | |
1452 | if (!EV_A) |
1520 | if (!EV_A) |
1453 | return; |
1521 | return; |
1454 | #endif |
1522 | #endif |
|
|
1523 | |
|
|
1524 | evpipe_init (EV_A); |
1455 | |
1525 | |
1456 | signals [signum - 1].pending = 1; |
1526 | signals [signum - 1].pending = 1; |
1457 | evpipe_write (EV_A_ &sig_pending); |
1527 | evpipe_write (EV_A_ &sig_pending); |
1458 | } |
1528 | } |
1459 | |
1529 | |
… | |
… | |
1591 | #endif |
1661 | #endif |
1592 | #if EV_USE_SELECT |
1662 | #if EV_USE_SELECT |
1593 | # include "ev_select.c" |
1663 | # include "ev_select.c" |
1594 | #endif |
1664 | #endif |
1595 | |
1665 | |
1596 | int |
1666 | int ecb_cold |
1597 | ev_version_major (void) |
1667 | ev_version_major (void) |
1598 | { |
1668 | { |
1599 | return EV_VERSION_MAJOR; |
1669 | return EV_VERSION_MAJOR; |
1600 | } |
1670 | } |
1601 | |
1671 | |
1602 | int |
1672 | int ecb_cold |
1603 | ev_version_minor (void) |
1673 | ev_version_minor (void) |
1604 | { |
1674 | { |
1605 | return EV_VERSION_MINOR; |
1675 | return EV_VERSION_MINOR; |
1606 | } |
1676 | } |
1607 | |
1677 | |
1608 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1678 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1609 | int inline_size |
1679 | int inline_size ecb_cold |
1610 | enable_secure (void) |
1680 | enable_secure (void) |
1611 | { |
1681 | { |
1612 | #ifdef _WIN32 |
1682 | #ifdef _WIN32 |
1613 | return 0; |
1683 | return 0; |
1614 | #else |
1684 | #else |
1615 | return getuid () != geteuid () |
1685 | return getuid () != geteuid () |
1616 | || getgid () != getegid (); |
1686 | || getgid () != getegid (); |
1617 | #endif |
1687 | #endif |
1618 | } |
1688 | } |
1619 | |
1689 | |
1620 | unsigned int |
1690 | unsigned int ecb_cold |
1621 | ev_supported_backends (void) |
1691 | ev_supported_backends (void) |
1622 | { |
1692 | { |
1623 | unsigned int flags = 0; |
1693 | unsigned int flags = 0; |
1624 | |
1694 | |
1625 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
1695 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
… | |
… | |
1629 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1699 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1630 | |
1700 | |
1631 | return flags; |
1701 | return flags; |
1632 | } |
1702 | } |
1633 | |
1703 | |
1634 | unsigned int |
1704 | unsigned int ecb_cold |
1635 | ev_recommended_backends (void) |
1705 | ev_recommended_backends (void) |
1636 | { |
1706 | { |
1637 | unsigned int flags = ev_supported_backends (); |
1707 | unsigned int flags = ev_supported_backends (); |
1638 | |
1708 | |
1639 | #ifndef __NetBSD__ |
1709 | #ifndef __NetBSD__ |
… | |
… | |
1651 | #endif |
1721 | #endif |
1652 | |
1722 | |
1653 | return flags; |
1723 | return flags; |
1654 | } |
1724 | } |
1655 | |
1725 | |
1656 | unsigned int |
1726 | unsigned int ecb_cold |
1657 | ev_embeddable_backends (void) |
1727 | ev_embeddable_backends (void) |
1658 | { |
1728 | { |
1659 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1729 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1660 | |
1730 | |
1661 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
1731 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
… | |
… | |
1706 | ev_userdata (EV_P) |
1776 | ev_userdata (EV_P) |
1707 | { |
1777 | { |
1708 | return userdata; |
1778 | return userdata; |
1709 | } |
1779 | } |
1710 | |
1780 | |
|
|
1781 | void |
1711 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1782 | ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1712 | { |
1783 | { |
1713 | invoke_cb = invoke_pending_cb; |
1784 | invoke_cb = invoke_pending_cb; |
1714 | } |
1785 | } |
1715 | |
1786 | |
|
|
1787 | void |
1716 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1788 | ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1717 | { |
1789 | { |
1718 | release_cb = release; |
1790 | release_cb = release; |
1719 | acquire_cb = acquire; |
1791 | acquire_cb = acquire; |
1720 | } |
1792 | } |
1721 | #endif |
1793 | #endif |
1722 | |
1794 | |
1723 | /* initialise a loop structure, must be zero-initialised */ |
1795 | /* initialise a loop structure, must be zero-initialised */ |
1724 | static void noinline |
1796 | static void noinline ecb_cold |
1725 | loop_init (EV_P_ unsigned int flags) |
1797 | loop_init (EV_P_ unsigned int flags) |
1726 | { |
1798 | { |
1727 | if (!backend) |
1799 | if (!backend) |
1728 | { |
1800 | { |
1729 | origflags = flags; |
1801 | origflags = flags; |
… | |
… | |
1757 | if (!(flags & EVFLAG_NOENV) |
1829 | if (!(flags & EVFLAG_NOENV) |
1758 | && !enable_secure () |
1830 | && !enable_secure () |
1759 | && getenv ("LIBEV_FLAGS")) |
1831 | && getenv ("LIBEV_FLAGS")) |
1760 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1832 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1761 | |
1833 | |
1762 | ev_rt_now = ev_time (); |
1834 | ev_rt_now = ev_time (); |
1763 | mn_now = get_clock (); |
1835 | mn_now = get_clock (); |
1764 | now_floor = mn_now; |
1836 | now_floor = mn_now; |
1765 | rtmn_diff = ev_rt_now - mn_now; |
1837 | rtmn_diff = ev_rt_now - mn_now; |
1766 | #if EV_FEATURE_API |
1838 | #if EV_FEATURE_API |
1767 | invoke_cb = ev_invoke_pending; |
1839 | invoke_cb = ev_invoke_pending; |
1768 | #endif |
1840 | #endif |
1769 | |
1841 | |
1770 | io_blocktime = 0.; |
1842 | io_blocktime = 0.; |
1771 | timeout_blocktime = 0.; |
1843 | timeout_blocktime = 0.; |
1772 | backend = 0; |
1844 | backend = 0; |
1773 | backend_fd = -1; |
1845 | backend_fd = -1; |
1774 | sig_pending = 0; |
1846 | sig_pending = 0; |
1775 | #if EV_ASYNC_ENABLE |
1847 | #if EV_ASYNC_ENABLE |
1776 | async_pending = 0; |
1848 | async_pending = 0; |
1777 | #endif |
1849 | #endif |
|
|
1850 | pipe_write_skipped = 0; |
|
|
1851 | pipe_write_wanted = 0; |
1778 | #if EV_USE_INOTIFY |
1852 | #if EV_USE_INOTIFY |
1779 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1853 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1780 | #endif |
1854 | #endif |
1781 | #if EV_USE_SIGNALFD |
1855 | #if EV_USE_SIGNALFD |
1782 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1856 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1783 | #endif |
1857 | #endif |
1784 | |
1858 | |
1785 | if (!(flags & EVBACKEND_MASK)) |
1859 | if (!(flags & EVBACKEND_MASK)) |
1786 | flags |= ev_recommended_backends (); |
1860 | flags |= ev_recommended_backends (); |
1787 | |
1861 | |
… | |
… | |
1812 | #endif |
1886 | #endif |
1813 | } |
1887 | } |
1814 | } |
1888 | } |
1815 | |
1889 | |
1816 | /* free up a loop structure */ |
1890 | /* free up a loop structure */ |
1817 | void |
1891 | void ecb_cold |
1818 | ev_loop_destroy (EV_P) |
1892 | ev_loop_destroy (EV_P) |
1819 | { |
1893 | { |
1820 | int i; |
1894 | int i; |
1821 | |
1895 | |
1822 | #if EV_MULTIPLICITY |
1896 | #if EV_MULTIPLICITY |
… | |
… | |
1952 | infy_fork (EV_A); |
2026 | infy_fork (EV_A); |
1953 | #endif |
2027 | #endif |
1954 | |
2028 | |
1955 | if (ev_is_active (&pipe_w)) |
2029 | if (ev_is_active (&pipe_w)) |
1956 | { |
2030 | { |
1957 | /* this "locks" the handlers against writing to the pipe */ |
2031 | /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ |
1958 | /* while we modify the fd vars */ |
|
|
1959 | sig_pending = 1; |
|
|
1960 | #if EV_ASYNC_ENABLE |
|
|
1961 | async_pending = 1; |
|
|
1962 | #endif |
|
|
1963 | |
2032 | |
1964 | ev_ref (EV_A); |
2033 | ev_ref (EV_A); |
1965 | ev_io_stop (EV_A_ &pipe_w); |
2034 | ev_io_stop (EV_A_ &pipe_w); |
1966 | |
2035 | |
1967 | #if EV_USE_EVENTFD |
2036 | #if EV_USE_EVENTFD |
… | |
… | |
1985 | postfork = 0; |
2054 | postfork = 0; |
1986 | } |
2055 | } |
1987 | |
2056 | |
1988 | #if EV_MULTIPLICITY |
2057 | #if EV_MULTIPLICITY |
1989 | |
2058 | |
1990 | struct ev_loop * |
2059 | struct ev_loop * ecb_cold |
1991 | ev_loop_new (unsigned int flags) |
2060 | ev_loop_new (unsigned int flags) |
1992 | { |
2061 | { |
1993 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2062 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1994 | |
2063 | |
1995 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2064 | memset (EV_A, 0, sizeof (struct ev_loop)); |
… | |
… | |
2003 | } |
2072 | } |
2004 | |
2073 | |
2005 | #endif /* multiplicity */ |
2074 | #endif /* multiplicity */ |
2006 | |
2075 | |
2007 | #if EV_VERIFY |
2076 | #if EV_VERIFY |
2008 | static void noinline |
2077 | static void noinline ecb_cold |
2009 | verify_watcher (EV_P_ W w) |
2078 | verify_watcher (EV_P_ W w) |
2010 | { |
2079 | { |
2011 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2080 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2012 | |
2081 | |
2013 | if (w->pending) |
2082 | if (w->pending) |
2014 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2083 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2015 | } |
2084 | } |
2016 | |
2085 | |
2017 | static void noinline |
2086 | static void noinline ecb_cold |
2018 | verify_heap (EV_P_ ANHE *heap, int N) |
2087 | verify_heap (EV_P_ ANHE *heap, int N) |
2019 | { |
2088 | { |
2020 | int i; |
2089 | int i; |
2021 | |
2090 | |
2022 | for (i = HEAP0; i < N + HEAP0; ++i) |
2091 | for (i = HEAP0; i < N + HEAP0; ++i) |
… | |
… | |
2027 | |
2096 | |
2028 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2097 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2029 | } |
2098 | } |
2030 | } |
2099 | } |
2031 | |
2100 | |
2032 | static void noinline |
2101 | static void noinline ecb_cold |
2033 | array_verify (EV_P_ W *ws, int cnt) |
2102 | array_verify (EV_P_ W *ws, int cnt) |
2034 | { |
2103 | { |
2035 | while (cnt--) |
2104 | while (cnt--) |
2036 | { |
2105 | { |
2037 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
2106 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
… | |
… | |
2039 | } |
2108 | } |
2040 | } |
2109 | } |
2041 | #endif |
2110 | #endif |
2042 | |
2111 | |
2043 | #if EV_FEATURE_API |
2112 | #if EV_FEATURE_API |
2044 | void |
2113 | void ecb_cold |
2045 | ev_verify (EV_P) |
2114 | ev_verify (EV_P) |
2046 | { |
2115 | { |
2047 | #if EV_VERIFY |
2116 | #if EV_VERIFY |
2048 | int i; |
2117 | int i; |
2049 | WL w; |
2118 | WL w; |
… | |
… | |
2115 | #endif |
2184 | #endif |
2116 | } |
2185 | } |
2117 | #endif |
2186 | #endif |
2118 | |
2187 | |
2119 | #if EV_MULTIPLICITY |
2188 | #if EV_MULTIPLICITY |
2120 | struct ev_loop * |
2189 | struct ev_loop * ecb_cold |
2121 | #else |
2190 | #else |
2122 | int |
2191 | int |
2123 | #endif |
2192 | #endif |
2124 | ev_default_loop (unsigned int flags) |
2193 | ev_default_loop (unsigned int flags) |
2125 | { |
2194 | { |
… | |
… | |
2324 | } |
2393 | } |
2325 | } |
2394 | } |
2326 | |
2395 | |
2327 | /* simply recalculate all periodics */ |
2396 | /* simply recalculate all periodics */ |
2328 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2397 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2329 | static void noinline |
2398 | static void noinline ecb_cold |
2330 | periodics_reschedule (EV_P) |
2399 | periodics_reschedule (EV_P) |
2331 | { |
2400 | { |
2332 | int i; |
2401 | int i; |
2333 | |
2402 | |
2334 | /* adjust periodics after time jump */ |
2403 | /* adjust periodics after time jump */ |
… | |
… | |
2347 | reheap (periodics, periodiccnt); |
2416 | reheap (periodics, periodiccnt); |
2348 | } |
2417 | } |
2349 | #endif |
2418 | #endif |
2350 | |
2419 | |
2351 | /* adjust all timers by a given offset */ |
2420 | /* adjust all timers by a given offset */ |
2352 | static void noinline |
2421 | static void noinline ecb_cold |
2353 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2422 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2354 | { |
2423 | { |
2355 | int i; |
2424 | int i; |
2356 | |
2425 | |
2357 | for (i = 0; i < timercnt; ++i) |
2426 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
2499 | ev_tstamp prev_mn_now = mn_now; |
2568 | ev_tstamp prev_mn_now = mn_now; |
2500 | |
2569 | |
2501 | /* update time to cancel out callback processing overhead */ |
2570 | /* update time to cancel out callback processing overhead */ |
2502 | time_update (EV_A_ 1e100); |
2571 | time_update (EV_A_ 1e100); |
2503 | |
2572 | |
|
|
2573 | /* from now on, we want a pipe-wake-up */ |
|
|
2574 | pipe_write_wanted = 1; |
|
|
2575 | |
2504 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) |
2576 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2505 | { |
2577 | { |
2506 | waittime = MAX_BLOCKTIME; |
2578 | waittime = MAX_BLOCKTIME; |
2507 | |
2579 | |
2508 | if (timercnt) |
2580 | if (timercnt) |
2509 | { |
2581 | { |
2510 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_mintime; |
2582 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
2511 | if (waittime > to) waittime = to; |
2583 | if (waittime > to) waittime = to; |
2512 | } |
2584 | } |
2513 | |
2585 | |
2514 | #if EV_PERIODIC_ENABLE |
2586 | #if EV_PERIODIC_ENABLE |
2515 | if (periodiccnt) |
2587 | if (periodiccnt) |
2516 | { |
2588 | { |
2517 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_mintime; |
2589 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now; |
2518 | if (waittime > to) waittime = to; |
2590 | if (waittime > to) waittime = to; |
2519 | } |
2591 | } |
2520 | #endif |
2592 | #endif |
2521 | |
2593 | |
2522 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2594 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2523 | if (expect_false (waittime < timeout_blocktime)) |
2595 | if (expect_false (waittime < timeout_blocktime)) |
2524 | waittime = timeout_blocktime; |
2596 | waittime = timeout_blocktime; |
|
|
2597 | |
|
|
2598 | /* at this point, we NEED to wait, so we have to ensure */ |
|
|
2599 | /* to pass a minimum nonzero value to the backend */ |
|
|
2600 | if (expect_false (waittime < backend_mintime)) |
|
|
2601 | waittime = backend_mintime; |
2525 | |
2602 | |
2526 | /* extra check because io_blocktime is commonly 0 */ |
2603 | /* extra check because io_blocktime is commonly 0 */ |
2527 | if (expect_false (io_blocktime)) |
2604 | if (expect_false (io_blocktime)) |
2528 | { |
2605 | { |
2529 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
2606 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
… | |
… | |
2543 | ++loop_count; |
2620 | ++loop_count; |
2544 | #endif |
2621 | #endif |
2545 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2622 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2546 | backend_poll (EV_A_ waittime); |
2623 | backend_poll (EV_A_ waittime); |
2547 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2624 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
|
|
2625 | |
|
|
2626 | pipe_write_wanted = 0; |
|
|
2627 | |
|
|
2628 | if (pipe_write_skipped) |
|
|
2629 | { |
|
|
2630 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
|
|
2631 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
|
|
2632 | } |
|
|
2633 | |
2548 | |
2634 | |
2549 | /* update ev_rt_now, do magic */ |
2635 | /* update ev_rt_now, do magic */ |
2550 | time_update (EV_A_ waittime + sleeptime); |
2636 | time_update (EV_A_ waittime + sleeptime); |
2551 | } |
2637 | } |
2552 | |
2638 | |
… | |
… | |
3206 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3292 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3207 | ofs += sizeof (struct inotify_event) + ev->len; |
3293 | ofs += sizeof (struct inotify_event) + ev->len; |
3208 | } |
3294 | } |
3209 | } |
3295 | } |
3210 | |
3296 | |
3211 | inline_size void |
3297 | inline_size void ecb_cold |
3212 | ev_check_2625 (EV_P) |
3298 | ev_check_2625 (EV_P) |
3213 | { |
3299 | { |
3214 | /* kernels < 2.6.25 are borked |
3300 | /* kernels < 2.6.25 are borked |
3215 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3301 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3216 | */ |
3302 | */ |
… | |
… | |
3841 | } |
3927 | } |
3842 | |
3928 | |
3843 | /*****************************************************************************/ |
3929 | /*****************************************************************************/ |
3844 | |
3930 | |
3845 | #if EV_WALK_ENABLE |
3931 | #if EV_WALK_ENABLE |
3846 | void |
3932 | void ecb_cold |
3847 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3933 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3848 | { |
3934 | { |
3849 | int i, j; |
3935 | int i, j; |
3850 | ev_watcher_list *wl, *wn; |
3936 | ev_watcher_list *wl, *wn; |
3851 | |
3937 | |