… | |
… | |
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 |
|
|
496 | |
|
|
497 | #ifndef ECB_MEMORY_FENCE |
|
|
498 | #if ECB_GCC_VERSION(2,5) |
|
|
499 | #if __x86 |
|
|
500 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
|
|
501 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
502 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */ |
|
|
503 | #elif __amd64 |
|
|
504 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
|
|
505 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
|
|
506 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") |
|
|
507 | #endif |
478 | #endif |
508 | #endif |
|
|
509 | #endif |
479 | |
510 | |
|
|
511 | #ifndef ECB_MEMORY_FENCE |
|
|
512 | #if ECB_GCC_VERSION(4,4) |
|
|
513 | #define ECB_MEMORY_FENCE __sync_synchronize () |
|
|
514 | #define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) |
|
|
515 | #define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) |
|
|
516 | #elif _MSC_VER >= 1400 |
|
|
517 | #define ECB_MEMORY_FENCE do { } while (0) |
|
|
518 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
519 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
520 | #elif defined(_WIN32) && defined(MemoryBarrier) |
|
|
521 | #define ECB_MEMORY_FENCE MemoryBarrier () |
|
|
522 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
523 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
524 | #endif |
|
|
525 | #endif |
|
|
526 | |
|
|
527 | #ifndef ECB_MEMORY_FENCE |
|
|
528 | #include <pthread.h> |
|
|
529 | |
|
|
530 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
|
|
531 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
|
|
532 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
533 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
534 | #endif |
|
|
535 | |
|
|
536 | #if ECB_GCC_VERSION(3,1) |
|
|
537 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
|
|
538 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
|
|
539 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
540 | #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
|
|
541 | #else |
|
|
542 | #define ecb_attribute(attrlist) |
|
|
543 | #define ecb_is_constant(expr) 0 |
|
|
544 | #define ecb_expect(expr,value) (expr) |
|
|
545 | #define ecb_prefetch(addr,rw,locality) |
|
|
546 | #endif |
|
|
547 | |
|
|
548 | #define ecb_noinline ecb_attribute ((__noinline__)) |
|
|
549 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
|
|
550 | #define ecb_unused ecb_attribute ((__unused__)) |
|
|
551 | #define ecb_const ecb_attribute ((__const__)) |
|
|
552 | #define ecb_pure ecb_attribute ((__pure__)) |
|
|
553 | |
|
|
554 | #if ECB_GCC_VERSION(4,3) |
|
|
555 | #define ecb_artificial ecb_attribute ((__artificial__)) |
|
|
556 | #define ecb_hot ecb_attribute ((__hot__)) |
|
|
557 | #define ecb_cold ecb_attribute ((__cold__)) |
|
|
558 | #else |
|
|
559 | #define ecb_artificial |
|
|
560 | #define ecb_hot |
|
|
561 | #define ecb_cold |
|
|
562 | #endif |
|
|
563 | |
|
|
564 | /* put around conditional expressions if you are very sure that the */ |
|
|
565 | /* expression is mostly true or mostly false. note that these return */ |
|
|
566 | /* booleans, not the expression. */ |
480 | #define expect_false(expr) expect ((expr) != 0, 0) |
567 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
481 | #define expect_true(expr) expect ((expr) != 0, 1) |
568 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
|
|
569 | /* ecb.h end */ |
|
|
570 | |
|
|
571 | #define expect_false(cond) ecb_expect_false (cond) |
|
|
572 | #define expect_true(cond) ecb_expect_true (cond) |
|
|
573 | #define noinline ecb_noinline |
|
|
574 | |
482 | #define inline_size static inline |
575 | #define inline_size ecb_inline |
483 | |
576 | |
484 | #if EV_FEATURE_CODE |
577 | #if EV_FEATURE_CODE |
485 | # define inline_speed static inline |
578 | # define inline_speed ecb_inline |
486 | #else |
579 | #else |
487 | # define inline_speed static noinline |
580 | # define inline_speed static noinline |
488 | #endif |
581 | #endif |
489 | |
582 | |
490 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
583 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
… | |
… | |
581 | |
674 | |
582 | #ifdef __linux |
675 | #ifdef __linux |
583 | # include <sys/utsname.h> |
676 | # include <sys/utsname.h> |
584 | #endif |
677 | #endif |
585 | |
678 | |
586 | static unsigned int noinline |
679 | static unsigned int noinline ecb_cold |
587 | ev_linux_version (void) |
680 | ev_linux_version (void) |
588 | { |
681 | { |
589 | #ifdef __linux |
682 | #ifdef __linux |
590 | unsigned int v = 0; |
683 | unsigned int v = 0; |
591 | struct utsname buf; |
684 | struct utsname buf; |
… | |
… | |
620 | } |
713 | } |
621 | |
714 | |
622 | /*****************************************************************************/ |
715 | /*****************************************************************************/ |
623 | |
716 | |
624 | #if EV_AVOID_STDIO |
717 | #if EV_AVOID_STDIO |
625 | static void noinline |
718 | static void noinline ecb_cold |
626 | ev_printerr (const char *msg) |
719 | ev_printerr (const char *msg) |
627 | { |
720 | { |
628 | write (STDERR_FILENO, msg, strlen (msg)); |
721 | write (STDERR_FILENO, msg, strlen (msg)); |
629 | } |
722 | } |
630 | #endif |
723 | #endif |
631 | |
724 | |
632 | static void (*syserr_cb)(const char *msg); |
725 | static void (*syserr_cb)(const char *msg); |
633 | |
726 | |
634 | void |
727 | void ecb_cold |
635 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
728 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
636 | { |
729 | { |
637 | syserr_cb = cb; |
730 | syserr_cb = cb; |
638 | } |
731 | } |
639 | |
732 | |
640 | static void noinline |
733 | static void noinline ecb_cold |
641 | ev_syserr (const char *msg) |
734 | ev_syserr (const char *msg) |
642 | { |
735 | { |
643 | if (!msg) |
736 | if (!msg) |
644 | msg = "(libev) system error"; |
737 | msg = "(libev) system error"; |
645 | |
738 | |
… | |
… | |
678 | #endif |
771 | #endif |
679 | } |
772 | } |
680 | |
773 | |
681 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
774 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
682 | |
775 | |
683 | void |
776 | void ecb_cold |
684 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
777 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
685 | { |
778 | { |
686 | alloc = cb; |
779 | alloc = cb; |
687 | } |
780 | } |
688 | |
781 | |
… | |
… | |
870 | select (0, 0, 0, 0, &tv); |
963 | select (0, 0, 0, 0, &tv); |
871 | #endif |
964 | #endif |
872 | } |
965 | } |
873 | } |
966 | } |
874 | |
967 | |
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 | /*****************************************************************************/ |
968 | /*****************************************************************************/ |
884 | |
969 | |
885 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
970 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
886 | |
971 | |
887 | /* find a suitable new size for the given array, */ |
972 | /* find a suitable new size for the given array, */ |
… | |
… | |
905 | } |
990 | } |
906 | |
991 | |
907 | return ncur; |
992 | return ncur; |
908 | } |
993 | } |
909 | |
994 | |
910 | static noinline void * |
995 | static void * noinline ecb_cold |
911 | array_realloc (int elem, void *base, int *cur, int cnt) |
996 | array_realloc (int elem, void *base, int *cur, int cnt) |
912 | { |
997 | { |
913 | *cur = array_nextsize (elem, *cur, cnt); |
998 | *cur = array_nextsize (elem, *cur, cnt); |
914 | return ev_realloc (base, elem * *cur); |
999 | return ev_realloc (base, elem * *cur); |
915 | } |
1000 | } |
… | |
… | |
918 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
1003 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
919 | |
1004 | |
920 | #define array_needsize(type,base,cur,cnt,init) \ |
1005 | #define array_needsize(type,base,cur,cnt,init) \ |
921 | if (expect_false ((cnt) > (cur))) \ |
1006 | if (expect_false ((cnt) > (cur))) \ |
922 | { \ |
1007 | { \ |
923 | int ocur_ = (cur); \ |
1008 | int ecb_unused ocur_ = (cur); \ |
924 | (base) = (type *)array_realloc \ |
1009 | (base) = (type *)array_realloc \ |
925 | (sizeof (type), (base), &(cur), (cnt)); \ |
1010 | (sizeof (type), (base), &(cur), (cnt)); \ |
926 | init ((base) + (ocur_), (cur) - ocur_); \ |
1011 | init ((base) + (ocur_), (cur) - ocur_); \ |
927 | } |
1012 | } |
928 | |
1013 | |
… | |
… | |
1098 | fdchanges [fdchangecnt - 1] = fd; |
1183 | fdchanges [fdchangecnt - 1] = fd; |
1099 | } |
1184 | } |
1100 | } |
1185 | } |
1101 | |
1186 | |
1102 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
1187 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
1103 | inline_speed void |
1188 | inline_speed void ecb_cold |
1104 | fd_kill (EV_P_ int fd) |
1189 | fd_kill (EV_P_ int fd) |
1105 | { |
1190 | { |
1106 | ev_io *w; |
1191 | ev_io *w; |
1107 | |
1192 | |
1108 | while ((w = (ev_io *)anfds [fd].head)) |
1193 | while ((w = (ev_io *)anfds [fd].head)) |
… | |
… | |
1111 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1196 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1112 | } |
1197 | } |
1113 | } |
1198 | } |
1114 | |
1199 | |
1115 | /* check whether the given fd is actually valid, for error recovery */ |
1200 | /* check whether the given fd is actually valid, for error recovery */ |
1116 | inline_size int |
1201 | inline_size int ecb_cold |
1117 | fd_valid (int fd) |
1202 | fd_valid (int fd) |
1118 | { |
1203 | { |
1119 | #ifdef _WIN32 |
1204 | #ifdef _WIN32 |
1120 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1205 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1121 | #else |
1206 | #else |
1122 | return fcntl (fd, F_GETFD) != -1; |
1207 | return fcntl (fd, F_GETFD) != -1; |
1123 | #endif |
1208 | #endif |
1124 | } |
1209 | } |
1125 | |
1210 | |
1126 | /* called on EBADF to verify fds */ |
1211 | /* called on EBADF to verify fds */ |
1127 | static void noinline |
1212 | static void noinline ecb_cold |
1128 | fd_ebadf (EV_P) |
1213 | fd_ebadf (EV_P) |
1129 | { |
1214 | { |
1130 | int fd; |
1215 | int fd; |
1131 | |
1216 | |
1132 | for (fd = 0; fd < anfdmax; ++fd) |
1217 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
1134 | if (!fd_valid (fd) && errno == EBADF) |
1219 | if (!fd_valid (fd) && errno == EBADF) |
1135 | fd_kill (EV_A_ fd); |
1220 | fd_kill (EV_A_ fd); |
1136 | } |
1221 | } |
1137 | |
1222 | |
1138 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
1223 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
1139 | static void noinline |
1224 | static void noinline ecb_cold |
1140 | fd_enomem (EV_P) |
1225 | fd_enomem (EV_P) |
1141 | { |
1226 | { |
1142 | int fd; |
1227 | int fd; |
1143 | |
1228 | |
1144 | for (fd = anfdmax; fd--; ) |
1229 | for (fd = anfdmax; fd--; ) |
… | |
… | |
1339 | |
1424 | |
1340 | /*****************************************************************************/ |
1425 | /*****************************************************************************/ |
1341 | |
1426 | |
1342 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1427 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1343 | |
1428 | |
1344 | static void noinline |
1429 | static void noinline ecb_cold |
1345 | evpipe_init (EV_P) |
1430 | evpipe_init (EV_P) |
1346 | { |
1431 | { |
1347 | if (!ev_is_active (&pipe_w)) |
1432 | if (!ev_is_active (&pipe_w)) |
1348 | { |
1433 | { |
1349 | # if EV_USE_EVENTFD |
1434 | # if EV_USE_EVENTFD |
… | |
… | |
1371 | ev_io_start (EV_A_ &pipe_w); |
1456 | ev_io_start (EV_A_ &pipe_w); |
1372 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1457 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1373 | } |
1458 | } |
1374 | } |
1459 | } |
1375 | |
1460 | |
1376 | inline_size void |
1461 | inline_speed void |
1377 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1462 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1378 | { |
1463 | { |
1379 | if (!*flag) |
1464 | if (expect_true (*flag)) |
|
|
1465 | return; |
|
|
1466 | |
|
|
1467 | *flag = 1; |
|
|
1468 | |
|
|
1469 | ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ |
|
|
1470 | |
|
|
1471 | pipe_write_skipped = 1; |
|
|
1472 | |
|
|
1473 | ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */ |
|
|
1474 | |
|
|
1475 | if (pipe_write_wanted) |
1380 | { |
1476 | { |
|
|
1477 | int old_errno; |
|
|
1478 | |
|
|
1479 | pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ |
|
|
1480 | |
1381 | int old_errno = errno; /* save errno because write might clobber it */ |
1481 | old_errno = errno; /* save errno because write will clobber it */ |
1382 | char dummy; |
|
|
1383 | |
|
|
1384 | *flag = 1; |
|
|
1385 | |
1482 | |
1386 | #if EV_USE_EVENTFD |
1483 | #if EV_USE_EVENTFD |
1387 | if (evfd >= 0) |
1484 | if (evfd >= 0) |
1388 | { |
1485 | { |
1389 | uint64_t counter = 1; |
1486 | uint64_t counter = 1; |
1390 | write (evfd, &counter, sizeof (uint64_t)); |
1487 | write (evfd, &counter, sizeof (uint64_t)); |
1391 | } |
1488 | } |
1392 | else |
1489 | else |
1393 | #endif |
1490 | #endif |
|
|
1491 | { |
1394 | /* win32 people keep sending patches that change this write() to send() */ |
1492 | /* 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 */ |
1493 | /* 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 */ |
1494 | /* 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 */ |
1495 | /* where your send() is from - it's definitely not the microsoft send, and */ |
1398 | /* tell me. thank you. */ |
1496 | /* tell me. thank you. */ |
1399 | write (evpipe [1], &dummy, 1); |
1497 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
1498 | } |
1400 | |
1499 | |
1401 | errno = old_errno; |
1500 | errno = old_errno; |
1402 | } |
1501 | } |
1403 | } |
1502 | } |
1404 | |
1503 | |
… | |
… | |
1407 | static void |
1506 | static void |
1408 | pipecb (EV_P_ ev_io *iow, int revents) |
1507 | pipecb (EV_P_ ev_io *iow, int revents) |
1409 | { |
1508 | { |
1410 | int i; |
1509 | int i; |
1411 | |
1510 | |
|
|
1511 | if (revents & EV_READ) |
|
|
1512 | { |
1412 | #if EV_USE_EVENTFD |
1513 | #if EV_USE_EVENTFD |
1413 | if (evfd >= 0) |
1514 | if (evfd >= 0) |
1414 | { |
1515 | { |
1415 | uint64_t counter; |
1516 | uint64_t counter; |
1416 | read (evfd, &counter, sizeof (uint64_t)); |
1517 | read (evfd, &counter, sizeof (uint64_t)); |
1417 | } |
1518 | } |
1418 | else |
1519 | else |
1419 | #endif |
1520 | #endif |
1420 | { |
1521 | { |
1421 | char dummy; |
1522 | char dummy; |
1422 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1523 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1423 | read (evpipe [0], &dummy, 1); |
1524 | read (evpipe [0], &dummy, 1); |
|
|
1525 | } |
1424 | } |
1526 | } |
|
|
1527 | |
|
|
1528 | pipe_write_skipped = 0; |
1425 | |
1529 | |
1426 | #if EV_SIGNAL_ENABLE |
1530 | #if EV_SIGNAL_ENABLE |
1427 | if (sig_pending) |
1531 | if (sig_pending) |
1428 | { |
1532 | { |
1429 | sig_pending = 0; |
1533 | sig_pending = 0; |
… | |
… | |
1458 | EV_P = signals [signum - 1].loop; |
1562 | EV_P = signals [signum - 1].loop; |
1459 | |
1563 | |
1460 | if (!EV_A) |
1564 | if (!EV_A) |
1461 | return; |
1565 | return; |
1462 | #endif |
1566 | #endif |
|
|
1567 | |
|
|
1568 | if (!ev_active (&pipe_w)) |
|
|
1569 | return; |
1463 | |
1570 | |
1464 | signals [signum - 1].pending = 1; |
1571 | signals [signum - 1].pending = 1; |
1465 | evpipe_write (EV_A_ &sig_pending); |
1572 | evpipe_write (EV_A_ &sig_pending); |
1466 | } |
1573 | } |
1467 | |
1574 | |
… | |
… | |
1599 | #endif |
1706 | #endif |
1600 | #if EV_USE_SELECT |
1707 | #if EV_USE_SELECT |
1601 | # include "ev_select.c" |
1708 | # include "ev_select.c" |
1602 | #endif |
1709 | #endif |
1603 | |
1710 | |
1604 | int |
1711 | int ecb_cold |
1605 | ev_version_major (void) |
1712 | ev_version_major (void) |
1606 | { |
1713 | { |
1607 | return EV_VERSION_MAJOR; |
1714 | return EV_VERSION_MAJOR; |
1608 | } |
1715 | } |
1609 | |
1716 | |
1610 | int |
1717 | int ecb_cold |
1611 | ev_version_minor (void) |
1718 | ev_version_minor (void) |
1612 | { |
1719 | { |
1613 | return EV_VERSION_MINOR; |
1720 | return EV_VERSION_MINOR; |
1614 | } |
1721 | } |
1615 | |
1722 | |
1616 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1723 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1617 | int inline_size |
1724 | int inline_size ecb_cold |
1618 | enable_secure (void) |
1725 | enable_secure (void) |
1619 | { |
1726 | { |
1620 | #ifdef _WIN32 |
1727 | #ifdef _WIN32 |
1621 | return 0; |
1728 | return 0; |
1622 | #else |
1729 | #else |
1623 | return getuid () != geteuid () |
1730 | return getuid () != geteuid () |
1624 | || getgid () != getegid (); |
1731 | || getgid () != getegid (); |
1625 | #endif |
1732 | #endif |
1626 | } |
1733 | } |
1627 | |
1734 | |
1628 | unsigned int |
1735 | unsigned int ecb_cold |
1629 | ev_supported_backends (void) |
1736 | ev_supported_backends (void) |
1630 | { |
1737 | { |
1631 | unsigned int flags = 0; |
1738 | unsigned int flags = 0; |
1632 | |
1739 | |
1633 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
1740 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
… | |
… | |
1637 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1744 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1638 | |
1745 | |
1639 | return flags; |
1746 | return flags; |
1640 | } |
1747 | } |
1641 | |
1748 | |
1642 | unsigned int |
1749 | unsigned int ecb_cold |
1643 | ev_recommended_backends (void) |
1750 | ev_recommended_backends (void) |
1644 | { |
1751 | { |
1645 | unsigned int flags = ev_supported_backends (); |
1752 | unsigned int flags = ev_supported_backends (); |
1646 | |
1753 | |
1647 | #ifndef __NetBSD__ |
1754 | #ifndef __NetBSD__ |
… | |
… | |
1659 | #endif |
1766 | #endif |
1660 | |
1767 | |
1661 | return flags; |
1768 | return flags; |
1662 | } |
1769 | } |
1663 | |
1770 | |
1664 | unsigned int |
1771 | unsigned int ecb_cold |
1665 | ev_embeddable_backends (void) |
1772 | ev_embeddable_backends (void) |
1666 | { |
1773 | { |
1667 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1774 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1668 | |
1775 | |
1669 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
1776 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
… | |
… | |
1714 | ev_userdata (EV_P) |
1821 | ev_userdata (EV_P) |
1715 | { |
1822 | { |
1716 | return userdata; |
1823 | return userdata; |
1717 | } |
1824 | } |
1718 | |
1825 | |
|
|
1826 | void |
1719 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1827 | ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1720 | { |
1828 | { |
1721 | invoke_cb = invoke_pending_cb; |
1829 | invoke_cb = invoke_pending_cb; |
1722 | } |
1830 | } |
1723 | |
1831 | |
|
|
1832 | void |
1724 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1833 | ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1725 | { |
1834 | { |
1726 | release_cb = release; |
1835 | release_cb = release; |
1727 | acquire_cb = acquire; |
1836 | acquire_cb = acquire; |
1728 | } |
1837 | } |
1729 | #endif |
1838 | #endif |
1730 | |
1839 | |
1731 | /* initialise a loop structure, must be zero-initialised */ |
1840 | /* initialise a loop structure, must be zero-initialised */ |
1732 | static void noinline |
1841 | static void noinline ecb_cold |
1733 | loop_init (EV_P_ unsigned int flags) |
1842 | loop_init (EV_P_ unsigned int flags) |
1734 | { |
1843 | { |
1735 | if (!backend) |
1844 | if (!backend) |
1736 | { |
1845 | { |
1737 | origflags = flags; |
1846 | origflags = flags; |
… | |
… | |
1765 | if (!(flags & EVFLAG_NOENV) |
1874 | if (!(flags & EVFLAG_NOENV) |
1766 | && !enable_secure () |
1875 | && !enable_secure () |
1767 | && getenv ("LIBEV_FLAGS")) |
1876 | && getenv ("LIBEV_FLAGS")) |
1768 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1877 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1769 | |
1878 | |
1770 | ev_rt_now = ev_time (); |
1879 | ev_rt_now = ev_time (); |
1771 | mn_now = get_clock (); |
1880 | mn_now = get_clock (); |
1772 | now_floor = mn_now; |
1881 | now_floor = mn_now; |
1773 | rtmn_diff = ev_rt_now - mn_now; |
1882 | rtmn_diff = ev_rt_now - mn_now; |
1774 | #if EV_FEATURE_API |
1883 | #if EV_FEATURE_API |
1775 | invoke_cb = ev_invoke_pending; |
1884 | invoke_cb = ev_invoke_pending; |
1776 | #endif |
1885 | #endif |
1777 | |
1886 | |
1778 | io_blocktime = 0.; |
1887 | io_blocktime = 0.; |
1779 | timeout_blocktime = 0.; |
1888 | timeout_blocktime = 0.; |
1780 | backend = 0; |
1889 | backend = 0; |
1781 | backend_fd = -1; |
1890 | backend_fd = -1; |
1782 | sig_pending = 0; |
1891 | sig_pending = 0; |
1783 | #if EV_ASYNC_ENABLE |
1892 | #if EV_ASYNC_ENABLE |
1784 | async_pending = 0; |
1893 | async_pending = 0; |
1785 | #endif |
1894 | #endif |
|
|
1895 | pipe_write_skipped = 0; |
|
|
1896 | pipe_write_wanted = 0; |
1786 | #if EV_USE_INOTIFY |
1897 | #if EV_USE_INOTIFY |
1787 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1898 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1788 | #endif |
1899 | #endif |
1789 | #if EV_USE_SIGNALFD |
1900 | #if EV_USE_SIGNALFD |
1790 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1901 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1791 | #endif |
1902 | #endif |
1792 | |
1903 | |
1793 | if (!(flags & EVBACKEND_MASK)) |
1904 | if (!(flags & EVBACKEND_MASK)) |
1794 | flags |= ev_recommended_backends (); |
1905 | flags |= ev_recommended_backends (); |
1795 | |
1906 | |
… | |
… | |
1820 | #endif |
1931 | #endif |
1821 | } |
1932 | } |
1822 | } |
1933 | } |
1823 | |
1934 | |
1824 | /* free up a loop structure */ |
1935 | /* free up a loop structure */ |
1825 | void |
1936 | void ecb_cold |
1826 | ev_loop_destroy (EV_P) |
1937 | ev_loop_destroy (EV_P) |
1827 | { |
1938 | { |
1828 | int i; |
1939 | int i; |
1829 | |
1940 | |
1830 | #if EV_MULTIPLICITY |
1941 | #if EV_MULTIPLICITY |
… | |
… | |
1960 | infy_fork (EV_A); |
2071 | infy_fork (EV_A); |
1961 | #endif |
2072 | #endif |
1962 | |
2073 | |
1963 | if (ev_is_active (&pipe_w)) |
2074 | if (ev_is_active (&pipe_w)) |
1964 | { |
2075 | { |
1965 | /* this "locks" the handlers against writing to the pipe */ |
2076 | /* 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 | |
2077 | |
1972 | ev_ref (EV_A); |
2078 | ev_ref (EV_A); |
1973 | ev_io_stop (EV_A_ &pipe_w); |
2079 | ev_io_stop (EV_A_ &pipe_w); |
1974 | |
2080 | |
1975 | #if EV_USE_EVENTFD |
2081 | #if EV_USE_EVENTFD |
… | |
… | |
1993 | postfork = 0; |
2099 | postfork = 0; |
1994 | } |
2100 | } |
1995 | |
2101 | |
1996 | #if EV_MULTIPLICITY |
2102 | #if EV_MULTIPLICITY |
1997 | |
2103 | |
1998 | struct ev_loop * |
2104 | struct ev_loop * ecb_cold |
1999 | ev_loop_new (unsigned int flags) |
2105 | ev_loop_new (unsigned int flags) |
2000 | { |
2106 | { |
2001 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2107 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2002 | |
2108 | |
2003 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2109 | memset (EV_A, 0, sizeof (struct ev_loop)); |
… | |
… | |
2011 | } |
2117 | } |
2012 | |
2118 | |
2013 | #endif /* multiplicity */ |
2119 | #endif /* multiplicity */ |
2014 | |
2120 | |
2015 | #if EV_VERIFY |
2121 | #if EV_VERIFY |
2016 | static void noinline |
2122 | static void noinline ecb_cold |
2017 | verify_watcher (EV_P_ W w) |
2123 | verify_watcher (EV_P_ W w) |
2018 | { |
2124 | { |
2019 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2125 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2020 | |
2126 | |
2021 | if (w->pending) |
2127 | if (w->pending) |
2022 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2128 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2023 | } |
2129 | } |
2024 | |
2130 | |
2025 | static void noinline |
2131 | static void noinline ecb_cold |
2026 | verify_heap (EV_P_ ANHE *heap, int N) |
2132 | verify_heap (EV_P_ ANHE *heap, int N) |
2027 | { |
2133 | { |
2028 | int i; |
2134 | int i; |
2029 | |
2135 | |
2030 | for (i = HEAP0; i < N + HEAP0; ++i) |
2136 | for (i = HEAP0; i < N + HEAP0; ++i) |
… | |
… | |
2035 | |
2141 | |
2036 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2142 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2037 | } |
2143 | } |
2038 | } |
2144 | } |
2039 | |
2145 | |
2040 | static void noinline |
2146 | static void noinline ecb_cold |
2041 | array_verify (EV_P_ W *ws, int cnt) |
2147 | array_verify (EV_P_ W *ws, int cnt) |
2042 | { |
2148 | { |
2043 | while (cnt--) |
2149 | while (cnt--) |
2044 | { |
2150 | { |
2045 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
2151 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
… | |
… | |
2047 | } |
2153 | } |
2048 | } |
2154 | } |
2049 | #endif |
2155 | #endif |
2050 | |
2156 | |
2051 | #if EV_FEATURE_API |
2157 | #if EV_FEATURE_API |
2052 | void |
2158 | void ecb_cold |
2053 | ev_verify (EV_P) |
2159 | ev_verify (EV_P) |
2054 | { |
2160 | { |
2055 | #if EV_VERIFY |
2161 | #if EV_VERIFY |
2056 | int i; |
2162 | int i; |
2057 | WL w; |
2163 | WL w; |
… | |
… | |
2123 | #endif |
2229 | #endif |
2124 | } |
2230 | } |
2125 | #endif |
2231 | #endif |
2126 | |
2232 | |
2127 | #if EV_MULTIPLICITY |
2233 | #if EV_MULTIPLICITY |
2128 | struct ev_loop * |
2234 | struct ev_loop * ecb_cold |
2129 | #else |
2235 | #else |
2130 | int |
2236 | int |
2131 | #endif |
2237 | #endif |
2132 | ev_default_loop (unsigned int flags) |
2238 | ev_default_loop (unsigned int flags) |
2133 | { |
2239 | { |
… | |
… | |
2332 | } |
2438 | } |
2333 | } |
2439 | } |
2334 | |
2440 | |
2335 | /* simply recalculate all periodics */ |
2441 | /* simply recalculate all periodics */ |
2336 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2442 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2337 | static void noinline |
2443 | static void noinline ecb_cold |
2338 | periodics_reschedule (EV_P) |
2444 | periodics_reschedule (EV_P) |
2339 | { |
2445 | { |
2340 | int i; |
2446 | int i; |
2341 | |
2447 | |
2342 | /* adjust periodics after time jump */ |
2448 | /* adjust periodics after time jump */ |
… | |
… | |
2355 | reheap (periodics, periodiccnt); |
2461 | reheap (periodics, periodiccnt); |
2356 | } |
2462 | } |
2357 | #endif |
2463 | #endif |
2358 | |
2464 | |
2359 | /* adjust all timers by a given offset */ |
2465 | /* adjust all timers by a given offset */ |
2360 | static void noinline |
2466 | static void noinline ecb_cold |
2361 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2467 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2362 | { |
2468 | { |
2363 | int i; |
2469 | int i; |
2364 | |
2470 | |
2365 | for (i = 0; i < timercnt; ++i) |
2471 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
2507 | ev_tstamp prev_mn_now = mn_now; |
2613 | ev_tstamp prev_mn_now = mn_now; |
2508 | |
2614 | |
2509 | /* update time to cancel out callback processing overhead */ |
2615 | /* update time to cancel out callback processing overhead */ |
2510 | time_update (EV_A_ 1e100); |
2616 | time_update (EV_A_ 1e100); |
2511 | |
2617 | |
|
|
2618 | /* from now on, we want a pipe-wake-up */ |
|
|
2619 | pipe_write_wanted = 1; |
|
|
2620 | |
|
|
2621 | ECB_MEMORY_FENCE; /* amke sure pipe_write_wanted is visible before we check for potential skips */ |
|
|
2622 | |
2512 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) |
2623 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2513 | { |
2624 | { |
2514 | waittime = MAX_BLOCKTIME; |
2625 | waittime = MAX_BLOCKTIME; |
2515 | |
2626 | |
2516 | if (timercnt) |
2627 | if (timercnt) |
2517 | { |
2628 | { |
2518 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2629 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
2519 | if (waittime > to) waittime = to; |
2630 | if (waittime > to) waittime = to; |
2520 | } |
2631 | } |
2521 | |
2632 | |
2522 | #if EV_PERIODIC_ENABLE |
2633 | #if EV_PERIODIC_ENABLE |
2523 | if (periodiccnt) |
2634 | if (periodiccnt) |
2524 | { |
2635 | { |
2525 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2636 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now; |
2526 | if (waittime > to) waittime = to; |
2637 | if (waittime > to) waittime = to; |
2527 | } |
2638 | } |
2528 | #endif |
2639 | #endif |
2529 | |
2640 | |
2530 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2641 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2531 | if (expect_false (waittime < timeout_blocktime)) |
2642 | if (expect_false (waittime < timeout_blocktime)) |
2532 | waittime = timeout_blocktime; |
2643 | waittime = timeout_blocktime; |
|
|
2644 | |
|
|
2645 | /* at this point, we NEED to wait, so we have to ensure */ |
|
|
2646 | /* to pass a minimum nonzero value to the backend */ |
|
|
2647 | if (expect_false (waittime < backend_mintime)) |
|
|
2648 | waittime = backend_mintime; |
2533 | |
2649 | |
2534 | /* extra check because io_blocktime is commonly 0 */ |
2650 | /* extra check because io_blocktime is commonly 0 */ |
2535 | if (expect_false (io_blocktime)) |
2651 | if (expect_false (io_blocktime)) |
2536 | { |
2652 | { |
2537 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
2653 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
2538 | |
2654 | |
2539 | if (sleeptime > waittime - backend_fudge) |
2655 | if (sleeptime > waittime - backend_mintime) |
2540 | sleeptime = waittime - backend_fudge; |
2656 | sleeptime = waittime - backend_mintime; |
2541 | |
2657 | |
2542 | if (expect_true (sleeptime > 0.)) |
2658 | if (expect_true (sleeptime > 0.)) |
2543 | { |
2659 | { |
2544 | ev_sleep (sleeptime); |
2660 | ev_sleep (sleeptime); |
2545 | waittime -= sleeptime; |
2661 | waittime -= sleeptime; |
… | |
… | |
2551 | ++loop_count; |
2667 | ++loop_count; |
2552 | #endif |
2668 | #endif |
2553 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2669 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2554 | backend_poll (EV_A_ waittime); |
2670 | backend_poll (EV_A_ waittime); |
2555 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2671 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
|
|
2672 | |
|
|
2673 | pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ |
|
|
2674 | |
|
|
2675 | if (pipe_write_skipped) |
|
|
2676 | { |
|
|
2677 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
|
|
2678 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
|
|
2679 | } |
|
|
2680 | |
2556 | |
2681 | |
2557 | /* update ev_rt_now, do magic */ |
2682 | /* update ev_rt_now, do magic */ |
2558 | time_update (EV_A_ waittime + sleeptime); |
2683 | time_update (EV_A_ waittime + sleeptime); |
2559 | } |
2684 | } |
2560 | |
2685 | |
… | |
… | |
3214 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3339 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3215 | ofs += sizeof (struct inotify_event) + ev->len; |
3340 | ofs += sizeof (struct inotify_event) + ev->len; |
3216 | } |
3341 | } |
3217 | } |
3342 | } |
3218 | |
3343 | |
3219 | inline_size void |
3344 | inline_size void ecb_cold |
3220 | ev_check_2625 (EV_P) |
3345 | ev_check_2625 (EV_P) |
3221 | { |
3346 | { |
3222 | /* kernels < 2.6.25 are borked |
3347 | /* kernels < 2.6.25 are borked |
3223 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3348 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3224 | */ |
3349 | */ |
… | |
… | |
3849 | } |
3974 | } |
3850 | |
3975 | |
3851 | /*****************************************************************************/ |
3976 | /*****************************************************************************/ |
3852 | |
3977 | |
3853 | #if EV_WALK_ENABLE |
3978 | #if EV_WALK_ENABLE |
3854 | void |
3979 | void ecb_cold |
3855 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3980 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3856 | { |
3981 | { |
3857 | int i, j; |
3982 | int i, j; |
3858 | ev_watcher_list *wl, *wn; |
3983 | ev_watcher_list *wl, *wn; |
3859 | |
3984 | |