… | |
… | |
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 | } |
… | |
… | |
910 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
964 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
911 | |
965 | |
912 | #define array_needsize(type,base,cur,cnt,init) \ |
966 | #define array_needsize(type,base,cur,cnt,init) \ |
913 | if (expect_false ((cnt) > (cur))) \ |
967 | if (expect_false ((cnt) > (cur))) \ |
914 | { \ |
968 | { \ |
915 | int ocur_ = (cur); \ |
969 | int ecb_unused ocur_ = (cur); \ |
916 | (base) = (type *)array_realloc \ |
970 | (base) = (type *)array_realloc \ |
917 | (sizeof (type), (base), &(cur), (cnt)); \ |
971 | (sizeof (type), (base), &(cur), (cnt)); \ |
918 | init ((base) + (ocur_), (cur) - ocur_); \ |
972 | init ((base) + (ocur_), (cur) - ocur_); \ |
919 | } |
973 | } |
920 | |
974 | |
… | |
… | |
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 |
… | |
… | |
1363 | ev_io_start (EV_A_ &pipe_w); |
1417 | ev_io_start (EV_A_ &pipe_w); |
1364 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1418 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1365 | } |
1419 | } |
1366 | } |
1420 | } |
1367 | |
1421 | |
1368 | inline_size void |
1422 | inline_speed 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; |
|
|
1434 | |
|
|
1435 | pipe_write_skipped = 0; |
|
|
1436 | |
|
|
1437 | old_errno = errno; /* save errno because write will clobber it */ |
|
|
1438 | |
1378 | #if EV_USE_EVENTFD |
1439 | #if EV_USE_EVENTFD |
1379 | if (evfd >= 0) |
1440 | if (evfd >= 0) |
1380 | { |
1441 | { |
1381 | uint64_t counter = 1; |
1442 | uint64_t counter = 1; |
1382 | 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; |
1383 | } |
1457 | } |
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 | } |
1458 | } |
1395 | } |
1459 | } |
1396 | |
1460 | |
1397 | /* called whenever the libev signal pipe */ |
1461 | /* called whenever the libev signal pipe */ |
1398 | /* got some events (signal, async) */ |
1462 | /* got some events (signal, async) */ |
1399 | static void |
1463 | static void |
1400 | pipecb (EV_P_ ev_io *iow, int revents) |
1464 | pipecb (EV_P_ ev_io *iow, int revents) |
1401 | { |
1465 | { |
1402 | int i; |
1466 | int i; |
1403 | |
1467 | |
|
|
1468 | if (revents & EV_READ) |
|
|
1469 | { |
1404 | #if EV_USE_EVENTFD |
1470 | #if EV_USE_EVENTFD |
1405 | if (evfd >= 0) |
1471 | if (evfd >= 0) |
1406 | { |
1472 | { |
1407 | uint64_t counter; |
1473 | uint64_t counter; |
1408 | read (evfd, &counter, sizeof (uint64_t)); |
1474 | read (evfd, &counter, sizeof (uint64_t)); |
1409 | } |
1475 | } |
1410 | else |
1476 | else |
1411 | #endif |
1477 | #endif |
1412 | { |
1478 | { |
1413 | char dummy; |
1479 | char dummy; |
1414 | /* 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 */ |
1415 | read (evpipe [0], &dummy, 1); |
1481 | read (evpipe [0], &dummy, 1); |
|
|
1482 | } |
1416 | } |
1483 | } |
|
|
1484 | |
|
|
1485 | pipe_write_skipped = 0; |
1417 | |
1486 | |
1418 | #if EV_SIGNAL_ENABLE |
1487 | #if EV_SIGNAL_ENABLE |
1419 | if (sig_pending) |
1488 | if (sig_pending) |
1420 | { |
1489 | { |
1421 | sig_pending = 0; |
1490 | sig_pending = 0; |
… | |
… | |
1450 | EV_P = signals [signum - 1].loop; |
1519 | EV_P = signals [signum - 1].loop; |
1451 | |
1520 | |
1452 | if (!EV_A) |
1521 | if (!EV_A) |
1453 | return; |
1522 | return; |
1454 | #endif |
1523 | #endif |
|
|
1524 | |
|
|
1525 | if (!ev_active (&pipe_w)) |
|
|
1526 | return; |
1455 | |
1527 | |
1456 | signals [signum - 1].pending = 1; |
1528 | signals [signum - 1].pending = 1; |
1457 | evpipe_write (EV_A_ &sig_pending); |
1529 | evpipe_write (EV_A_ &sig_pending); |
1458 | } |
1530 | } |
1459 | |
1531 | |
… | |
… | |
1591 | #endif |
1663 | #endif |
1592 | #if EV_USE_SELECT |
1664 | #if EV_USE_SELECT |
1593 | # include "ev_select.c" |
1665 | # include "ev_select.c" |
1594 | #endif |
1666 | #endif |
1595 | |
1667 | |
1596 | int |
1668 | int ecb_cold |
1597 | ev_version_major (void) |
1669 | ev_version_major (void) |
1598 | { |
1670 | { |
1599 | return EV_VERSION_MAJOR; |
1671 | return EV_VERSION_MAJOR; |
1600 | } |
1672 | } |
1601 | |
1673 | |
1602 | int |
1674 | int ecb_cold |
1603 | ev_version_minor (void) |
1675 | ev_version_minor (void) |
1604 | { |
1676 | { |
1605 | return EV_VERSION_MINOR; |
1677 | return EV_VERSION_MINOR; |
1606 | } |
1678 | } |
1607 | |
1679 | |
1608 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1680 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1609 | int inline_size |
1681 | int inline_size ecb_cold |
1610 | enable_secure (void) |
1682 | enable_secure (void) |
1611 | { |
1683 | { |
1612 | #ifdef _WIN32 |
1684 | #ifdef _WIN32 |
1613 | return 0; |
1685 | return 0; |
1614 | #else |
1686 | #else |
1615 | return getuid () != geteuid () |
1687 | return getuid () != geteuid () |
1616 | || getgid () != getegid (); |
1688 | || getgid () != getegid (); |
1617 | #endif |
1689 | #endif |
1618 | } |
1690 | } |
1619 | |
1691 | |
1620 | unsigned int |
1692 | unsigned int ecb_cold |
1621 | ev_supported_backends (void) |
1693 | ev_supported_backends (void) |
1622 | { |
1694 | { |
1623 | unsigned int flags = 0; |
1695 | unsigned int flags = 0; |
1624 | |
1696 | |
1625 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
1697 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
… | |
… | |
1629 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1701 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1630 | |
1702 | |
1631 | return flags; |
1703 | return flags; |
1632 | } |
1704 | } |
1633 | |
1705 | |
1634 | unsigned int |
1706 | unsigned int ecb_cold |
1635 | ev_recommended_backends (void) |
1707 | ev_recommended_backends (void) |
1636 | { |
1708 | { |
1637 | unsigned int flags = ev_supported_backends (); |
1709 | unsigned int flags = ev_supported_backends (); |
1638 | |
1710 | |
1639 | #ifndef __NetBSD__ |
1711 | #ifndef __NetBSD__ |
… | |
… | |
1651 | #endif |
1723 | #endif |
1652 | |
1724 | |
1653 | return flags; |
1725 | return flags; |
1654 | } |
1726 | } |
1655 | |
1727 | |
1656 | unsigned int |
1728 | unsigned int ecb_cold |
1657 | ev_embeddable_backends (void) |
1729 | ev_embeddable_backends (void) |
1658 | { |
1730 | { |
1659 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1731 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1660 | |
1732 | |
1661 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
1733 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
… | |
… | |
1706 | ev_userdata (EV_P) |
1778 | ev_userdata (EV_P) |
1707 | { |
1779 | { |
1708 | return userdata; |
1780 | return userdata; |
1709 | } |
1781 | } |
1710 | |
1782 | |
|
|
1783 | void |
1711 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1784 | ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1712 | { |
1785 | { |
1713 | invoke_cb = invoke_pending_cb; |
1786 | invoke_cb = invoke_pending_cb; |
1714 | } |
1787 | } |
1715 | |
1788 | |
|
|
1789 | void |
1716 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1790 | ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1717 | { |
1791 | { |
1718 | release_cb = release; |
1792 | release_cb = release; |
1719 | acquire_cb = acquire; |
1793 | acquire_cb = acquire; |
1720 | } |
1794 | } |
1721 | #endif |
1795 | #endif |
1722 | |
1796 | |
1723 | /* initialise a loop structure, must be zero-initialised */ |
1797 | /* initialise a loop structure, must be zero-initialised */ |
1724 | static void noinline |
1798 | static void noinline ecb_cold |
1725 | loop_init (EV_P_ unsigned int flags) |
1799 | loop_init (EV_P_ unsigned int flags) |
1726 | { |
1800 | { |
1727 | if (!backend) |
1801 | if (!backend) |
1728 | { |
1802 | { |
1729 | origflags = flags; |
1803 | origflags = flags; |
… | |
… | |
1757 | if (!(flags & EVFLAG_NOENV) |
1831 | if (!(flags & EVFLAG_NOENV) |
1758 | && !enable_secure () |
1832 | && !enable_secure () |
1759 | && getenv ("LIBEV_FLAGS")) |
1833 | && getenv ("LIBEV_FLAGS")) |
1760 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1834 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1761 | |
1835 | |
1762 | ev_rt_now = ev_time (); |
1836 | ev_rt_now = ev_time (); |
1763 | mn_now = get_clock (); |
1837 | mn_now = get_clock (); |
1764 | now_floor = mn_now; |
1838 | now_floor = mn_now; |
1765 | rtmn_diff = ev_rt_now - mn_now; |
1839 | rtmn_diff = ev_rt_now - mn_now; |
1766 | #if EV_FEATURE_API |
1840 | #if EV_FEATURE_API |
1767 | invoke_cb = ev_invoke_pending; |
1841 | invoke_cb = ev_invoke_pending; |
1768 | #endif |
1842 | #endif |
1769 | |
1843 | |
1770 | io_blocktime = 0.; |
1844 | io_blocktime = 0.; |
1771 | timeout_blocktime = 0.; |
1845 | timeout_blocktime = 0.; |
1772 | backend = 0; |
1846 | backend = 0; |
1773 | backend_fd = -1; |
1847 | backend_fd = -1; |
1774 | sig_pending = 0; |
1848 | sig_pending = 0; |
1775 | #if EV_ASYNC_ENABLE |
1849 | #if EV_ASYNC_ENABLE |
1776 | async_pending = 0; |
1850 | async_pending = 0; |
1777 | #endif |
1851 | #endif |
|
|
1852 | pipe_write_skipped = 0; |
|
|
1853 | pipe_write_wanted = 0; |
1778 | #if EV_USE_INOTIFY |
1854 | #if EV_USE_INOTIFY |
1779 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1855 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1780 | #endif |
1856 | #endif |
1781 | #if EV_USE_SIGNALFD |
1857 | #if EV_USE_SIGNALFD |
1782 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1858 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1783 | #endif |
1859 | #endif |
1784 | |
1860 | |
1785 | if (!(flags & EVBACKEND_MASK)) |
1861 | if (!(flags & EVBACKEND_MASK)) |
1786 | flags |= ev_recommended_backends (); |
1862 | flags |= ev_recommended_backends (); |
1787 | |
1863 | |
… | |
… | |
1812 | #endif |
1888 | #endif |
1813 | } |
1889 | } |
1814 | } |
1890 | } |
1815 | |
1891 | |
1816 | /* free up a loop structure */ |
1892 | /* free up a loop structure */ |
1817 | void |
1893 | void ecb_cold |
1818 | ev_loop_destroy (EV_P) |
1894 | ev_loop_destroy (EV_P) |
1819 | { |
1895 | { |
1820 | int i; |
1896 | int i; |
1821 | |
1897 | |
1822 | #if EV_MULTIPLICITY |
1898 | #if EV_MULTIPLICITY |
… | |
… | |
1952 | infy_fork (EV_A); |
2028 | infy_fork (EV_A); |
1953 | #endif |
2029 | #endif |
1954 | |
2030 | |
1955 | if (ev_is_active (&pipe_w)) |
2031 | if (ev_is_active (&pipe_w)) |
1956 | { |
2032 | { |
1957 | /* this "locks" the handlers against writing to the pipe */ |
2033 | /* 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 | |
2034 | |
1964 | ev_ref (EV_A); |
2035 | ev_ref (EV_A); |
1965 | ev_io_stop (EV_A_ &pipe_w); |
2036 | ev_io_stop (EV_A_ &pipe_w); |
1966 | |
2037 | |
1967 | #if EV_USE_EVENTFD |
2038 | #if EV_USE_EVENTFD |
… | |
… | |
1985 | postfork = 0; |
2056 | postfork = 0; |
1986 | } |
2057 | } |
1987 | |
2058 | |
1988 | #if EV_MULTIPLICITY |
2059 | #if EV_MULTIPLICITY |
1989 | |
2060 | |
1990 | struct ev_loop * |
2061 | struct ev_loop * ecb_cold |
1991 | ev_loop_new (unsigned int flags) |
2062 | ev_loop_new (unsigned int flags) |
1992 | { |
2063 | { |
1993 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2064 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1994 | |
2065 | |
1995 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2066 | memset (EV_A, 0, sizeof (struct ev_loop)); |
… | |
… | |
2003 | } |
2074 | } |
2004 | |
2075 | |
2005 | #endif /* multiplicity */ |
2076 | #endif /* multiplicity */ |
2006 | |
2077 | |
2007 | #if EV_VERIFY |
2078 | #if EV_VERIFY |
2008 | static void noinline |
2079 | static void noinline ecb_cold |
2009 | verify_watcher (EV_P_ W w) |
2080 | verify_watcher (EV_P_ W w) |
2010 | { |
2081 | { |
2011 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2082 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2012 | |
2083 | |
2013 | if (w->pending) |
2084 | if (w->pending) |
2014 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2085 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2015 | } |
2086 | } |
2016 | |
2087 | |
2017 | static void noinline |
2088 | static void noinline ecb_cold |
2018 | verify_heap (EV_P_ ANHE *heap, int N) |
2089 | verify_heap (EV_P_ ANHE *heap, int N) |
2019 | { |
2090 | { |
2020 | int i; |
2091 | int i; |
2021 | |
2092 | |
2022 | for (i = HEAP0; i < N + HEAP0; ++i) |
2093 | for (i = HEAP0; i < N + HEAP0; ++i) |
… | |
… | |
2027 | |
2098 | |
2028 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2099 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2029 | } |
2100 | } |
2030 | } |
2101 | } |
2031 | |
2102 | |
2032 | static void noinline |
2103 | static void noinline ecb_cold |
2033 | array_verify (EV_P_ W *ws, int cnt) |
2104 | array_verify (EV_P_ W *ws, int cnt) |
2034 | { |
2105 | { |
2035 | while (cnt--) |
2106 | while (cnt--) |
2036 | { |
2107 | { |
2037 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
2108 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
… | |
… | |
2039 | } |
2110 | } |
2040 | } |
2111 | } |
2041 | #endif |
2112 | #endif |
2042 | |
2113 | |
2043 | #if EV_FEATURE_API |
2114 | #if EV_FEATURE_API |
2044 | void |
2115 | void ecb_cold |
2045 | ev_verify (EV_P) |
2116 | ev_verify (EV_P) |
2046 | { |
2117 | { |
2047 | #if EV_VERIFY |
2118 | #if EV_VERIFY |
2048 | int i; |
2119 | int i; |
2049 | WL w; |
2120 | WL w; |
… | |
… | |
2115 | #endif |
2186 | #endif |
2116 | } |
2187 | } |
2117 | #endif |
2188 | #endif |
2118 | |
2189 | |
2119 | #if EV_MULTIPLICITY |
2190 | #if EV_MULTIPLICITY |
2120 | struct ev_loop * |
2191 | struct ev_loop * ecb_cold |
2121 | #else |
2192 | #else |
2122 | int |
2193 | int |
2123 | #endif |
2194 | #endif |
2124 | ev_default_loop (unsigned int flags) |
2195 | ev_default_loop (unsigned int flags) |
2125 | { |
2196 | { |
… | |
… | |
2324 | } |
2395 | } |
2325 | } |
2396 | } |
2326 | |
2397 | |
2327 | /* simply recalculate all periodics */ |
2398 | /* simply recalculate all periodics */ |
2328 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2399 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2329 | static void noinline |
2400 | static void noinline ecb_cold |
2330 | periodics_reschedule (EV_P) |
2401 | periodics_reschedule (EV_P) |
2331 | { |
2402 | { |
2332 | int i; |
2403 | int i; |
2333 | |
2404 | |
2334 | /* adjust periodics after time jump */ |
2405 | /* adjust periodics after time jump */ |
… | |
… | |
2347 | reheap (periodics, periodiccnt); |
2418 | reheap (periodics, periodiccnt); |
2348 | } |
2419 | } |
2349 | #endif |
2420 | #endif |
2350 | |
2421 | |
2351 | /* adjust all timers by a given offset */ |
2422 | /* adjust all timers by a given offset */ |
2352 | static void noinline |
2423 | static void noinline ecb_cold |
2353 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2424 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2354 | { |
2425 | { |
2355 | int i; |
2426 | int i; |
2356 | |
2427 | |
2357 | for (i = 0; i < timercnt; ++i) |
2428 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
2499 | ev_tstamp prev_mn_now = mn_now; |
2570 | ev_tstamp prev_mn_now = mn_now; |
2500 | |
2571 | |
2501 | /* update time to cancel out callback processing overhead */ |
2572 | /* update time to cancel out callback processing overhead */ |
2502 | time_update (EV_A_ 1e100); |
2573 | time_update (EV_A_ 1e100); |
2503 | |
2574 | |
|
|
2575 | /* from now on, we want a pipe-wake-up */ |
|
|
2576 | pipe_write_wanted = 1; |
|
|
2577 | |
2504 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) |
2578 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2505 | { |
2579 | { |
2506 | waittime = MAX_BLOCKTIME; |
2580 | waittime = MAX_BLOCKTIME; |
2507 | |
2581 | |
2508 | if (timercnt) |
2582 | if (timercnt) |
2509 | { |
2583 | { |
2510 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_mintime; |
2584 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
2511 | if (waittime > to) waittime = to; |
2585 | if (waittime > to) waittime = to; |
2512 | } |
2586 | } |
2513 | |
2587 | |
2514 | #if EV_PERIODIC_ENABLE |
2588 | #if EV_PERIODIC_ENABLE |
2515 | if (periodiccnt) |
2589 | if (periodiccnt) |
2516 | { |
2590 | { |
2517 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_mintime; |
2591 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now; |
2518 | if (waittime > to) waittime = to; |
2592 | if (waittime > to) waittime = to; |
2519 | } |
2593 | } |
2520 | #endif |
2594 | #endif |
2521 | |
2595 | |
2522 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2596 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2523 | if (expect_false (waittime < timeout_blocktime)) |
2597 | if (expect_false (waittime < timeout_blocktime)) |
2524 | waittime = timeout_blocktime; |
2598 | waittime = timeout_blocktime; |
|
|
2599 | |
|
|
2600 | /* at this point, we NEED to wait, so we have to ensure */ |
|
|
2601 | /* to pass a minimum nonzero value to the backend */ |
|
|
2602 | if (expect_false (waittime < backend_mintime)) |
|
|
2603 | waittime = backend_mintime; |
2525 | |
2604 | |
2526 | /* extra check because io_blocktime is commonly 0 */ |
2605 | /* extra check because io_blocktime is commonly 0 */ |
2527 | if (expect_false (io_blocktime)) |
2606 | if (expect_false (io_blocktime)) |
2528 | { |
2607 | { |
2529 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
2608 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
… | |
… | |
2543 | ++loop_count; |
2622 | ++loop_count; |
2544 | #endif |
2623 | #endif |
2545 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2624 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2546 | backend_poll (EV_A_ waittime); |
2625 | backend_poll (EV_A_ waittime); |
2547 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2626 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
|
|
2627 | |
|
|
2628 | pipe_write_wanted = 0; |
|
|
2629 | |
|
|
2630 | if (pipe_write_skipped) |
|
|
2631 | { |
|
|
2632 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
|
|
2633 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
|
|
2634 | } |
|
|
2635 | |
2548 | |
2636 | |
2549 | /* update ev_rt_now, do magic */ |
2637 | /* update ev_rt_now, do magic */ |
2550 | time_update (EV_A_ waittime + sleeptime); |
2638 | time_update (EV_A_ waittime + sleeptime); |
2551 | } |
2639 | } |
2552 | |
2640 | |
… | |
… | |
3206 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3294 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3207 | ofs += sizeof (struct inotify_event) + ev->len; |
3295 | ofs += sizeof (struct inotify_event) + ev->len; |
3208 | } |
3296 | } |
3209 | } |
3297 | } |
3210 | |
3298 | |
3211 | inline_size void |
3299 | inline_size void ecb_cold |
3212 | ev_check_2625 (EV_P) |
3300 | ev_check_2625 (EV_P) |
3213 | { |
3301 | { |
3214 | /* kernels < 2.6.25 are borked |
3302 | /* kernels < 2.6.25 are borked |
3215 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3303 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3216 | */ |
3304 | */ |
… | |
… | |
3841 | } |
3929 | } |
3842 | |
3930 | |
3843 | /*****************************************************************************/ |
3931 | /*****************************************************************************/ |
3844 | |
3932 | |
3845 | #if EV_WALK_ENABLE |
3933 | #if EV_WALK_ENABLE |
3846 | void |
3934 | void ecb_cold |
3847 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3935 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3848 | { |
3936 | { |
3849 | int i, j; |
3937 | int i, j; |
3850 | ev_watcher_list *wl, *wn; |
3938 | ev_watcher_list *wl, *wn; |
3851 | |
3939 | |