… | |
… | |
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 /* VC++ 2005 */ |
|
|
517 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
|
|
518 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
|
|
519 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
|
|
520 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
|
|
521 | #elif defined(_WIN32) |
|
|
522 | #include <WinNT.h> |
|
|
523 | #define ECB_MEMORY_FENCE MemoryBarrier () |
|
|
524 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
525 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
526 | #endif |
|
|
527 | #endif |
|
|
528 | |
|
|
529 | #ifndef ECB_MEMORY_FENCE |
|
|
530 | #include <pthread.h> |
|
|
531 | |
|
|
532 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
|
|
533 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
|
|
534 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
535 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
536 | #endif |
|
|
537 | |
|
|
538 | #if ECB_GCC_VERSION(3,1) |
|
|
539 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
|
|
540 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
|
|
541 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
542 | #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
|
|
543 | #else |
|
|
544 | #define ecb_attribute(attrlist) |
|
|
545 | #define ecb_is_constant(expr) 0 |
|
|
546 | #define ecb_expect(expr,value) (expr) |
|
|
547 | #define ecb_prefetch(addr,rw,locality) |
|
|
548 | #endif |
|
|
549 | |
|
|
550 | #define ecb_noinline ecb_attribute ((__noinline__)) |
|
|
551 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
|
|
552 | #define ecb_unused ecb_attribute ((__unused__)) |
|
|
553 | #define ecb_const ecb_attribute ((__const__)) |
|
|
554 | #define ecb_pure ecb_attribute ((__pure__)) |
|
|
555 | |
|
|
556 | #if ECB_GCC_VERSION(4,3) |
|
|
557 | #define ecb_artificial ecb_attribute ((__artificial__)) |
|
|
558 | #define ecb_hot ecb_attribute ((__hot__)) |
|
|
559 | #define ecb_cold ecb_attribute ((__cold__)) |
|
|
560 | #else |
|
|
561 | #define ecb_artificial |
|
|
562 | #define ecb_hot |
|
|
563 | #define ecb_cold |
|
|
564 | #endif |
|
|
565 | |
|
|
566 | /* put around conditional expressions if you are very sure that the */ |
|
|
567 | /* expression is mostly true or mostly false. note that these return */ |
|
|
568 | /* booleans, not the expression. */ |
480 | #define expect_false(expr) expect ((expr) != 0, 0) |
569 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
481 | #define expect_true(expr) expect ((expr) != 0, 1) |
570 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
|
|
571 | /* ecb.h end */ |
|
|
572 | |
|
|
573 | #define expect_false(cond) ecb_expect_false (cond) |
|
|
574 | #define expect_true(cond) ecb_expect_true (cond) |
|
|
575 | #define noinline ecb_noinline |
|
|
576 | |
482 | #define inline_size static inline |
577 | #define inline_size ecb_inline |
483 | |
578 | |
484 | #if EV_FEATURE_CODE |
579 | #if EV_FEATURE_CODE |
485 | # define inline_speed static inline |
580 | # define inline_speed ecb_inline |
486 | #else |
581 | #else |
487 | # define inline_speed static noinline |
582 | # define inline_speed static noinline |
488 | #endif |
583 | #endif |
489 | |
584 | |
490 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
585 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
… | |
… | |
581 | |
676 | |
582 | #ifdef __linux |
677 | #ifdef __linux |
583 | # include <sys/utsname.h> |
678 | # include <sys/utsname.h> |
584 | #endif |
679 | #endif |
585 | |
680 | |
586 | static unsigned int noinline |
681 | static unsigned int noinline ecb_cold |
587 | ev_linux_version (void) |
682 | ev_linux_version (void) |
588 | { |
683 | { |
589 | #ifdef __linux |
684 | #ifdef __linux |
590 | unsigned int v = 0; |
685 | unsigned int v = 0; |
591 | struct utsname buf; |
686 | struct utsname buf; |
… | |
… | |
620 | } |
715 | } |
621 | |
716 | |
622 | /*****************************************************************************/ |
717 | /*****************************************************************************/ |
623 | |
718 | |
624 | #if EV_AVOID_STDIO |
719 | #if EV_AVOID_STDIO |
625 | static void noinline |
720 | static void noinline ecb_cold |
626 | ev_printerr (const char *msg) |
721 | ev_printerr (const char *msg) |
627 | { |
722 | { |
628 | write (STDERR_FILENO, msg, strlen (msg)); |
723 | write (STDERR_FILENO, msg, strlen (msg)); |
629 | } |
724 | } |
630 | #endif |
725 | #endif |
631 | |
726 | |
632 | static void (*syserr_cb)(const char *msg); |
727 | static void (*syserr_cb)(const char *msg); |
633 | |
728 | |
634 | void |
729 | void ecb_cold |
635 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
730 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
636 | { |
731 | { |
637 | syserr_cb = cb; |
732 | syserr_cb = cb; |
638 | } |
733 | } |
639 | |
734 | |
640 | static void noinline |
735 | static void noinline ecb_cold |
641 | ev_syserr (const char *msg) |
736 | ev_syserr (const char *msg) |
642 | { |
737 | { |
643 | if (!msg) |
738 | if (!msg) |
644 | msg = "(libev) system error"; |
739 | msg = "(libev) system error"; |
645 | |
740 | |
… | |
… | |
678 | #endif |
773 | #endif |
679 | } |
774 | } |
680 | |
775 | |
681 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
776 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
682 | |
777 | |
683 | void |
778 | void ecb_cold |
684 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
779 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
685 | { |
780 | { |
686 | alloc = cb; |
781 | alloc = cb; |
687 | } |
782 | } |
688 | |
783 | |
… | |
… | |
897 | } |
992 | } |
898 | |
993 | |
899 | return ncur; |
994 | return ncur; |
900 | } |
995 | } |
901 | |
996 | |
902 | static noinline void * |
997 | static void * noinline ecb_cold |
903 | array_realloc (int elem, void *base, int *cur, int cnt) |
998 | array_realloc (int elem, void *base, int *cur, int cnt) |
904 | { |
999 | { |
905 | *cur = array_nextsize (elem, *cur, cnt); |
1000 | *cur = array_nextsize (elem, *cur, cnt); |
906 | return ev_realloc (base, elem * *cur); |
1001 | return ev_realloc (base, elem * *cur); |
907 | } |
1002 | } |
… | |
… | |
910 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
1005 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
911 | |
1006 | |
912 | #define array_needsize(type,base,cur,cnt,init) \ |
1007 | #define array_needsize(type,base,cur,cnt,init) \ |
913 | if (expect_false ((cnt) > (cur))) \ |
1008 | if (expect_false ((cnt) > (cur))) \ |
914 | { \ |
1009 | { \ |
915 | int ocur_ = (cur); \ |
1010 | int ecb_unused ocur_ = (cur); \ |
916 | (base) = (type *)array_realloc \ |
1011 | (base) = (type *)array_realloc \ |
917 | (sizeof (type), (base), &(cur), (cnt)); \ |
1012 | (sizeof (type), (base), &(cur), (cnt)); \ |
918 | init ((base) + (ocur_), (cur) - ocur_); \ |
1013 | init ((base) + (ocur_), (cur) - ocur_); \ |
919 | } |
1014 | } |
920 | |
1015 | |
… | |
… | |
1090 | fdchanges [fdchangecnt - 1] = fd; |
1185 | fdchanges [fdchangecnt - 1] = fd; |
1091 | } |
1186 | } |
1092 | } |
1187 | } |
1093 | |
1188 | |
1094 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
1189 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
1095 | inline_speed void |
1190 | inline_speed void ecb_cold |
1096 | fd_kill (EV_P_ int fd) |
1191 | fd_kill (EV_P_ int fd) |
1097 | { |
1192 | { |
1098 | ev_io *w; |
1193 | ev_io *w; |
1099 | |
1194 | |
1100 | while ((w = (ev_io *)anfds [fd].head)) |
1195 | while ((w = (ev_io *)anfds [fd].head)) |
… | |
… | |
1103 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1198 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1104 | } |
1199 | } |
1105 | } |
1200 | } |
1106 | |
1201 | |
1107 | /* check whether the given fd is actually valid, for error recovery */ |
1202 | /* check whether the given fd is actually valid, for error recovery */ |
1108 | inline_size int |
1203 | inline_size int ecb_cold |
1109 | fd_valid (int fd) |
1204 | fd_valid (int fd) |
1110 | { |
1205 | { |
1111 | #ifdef _WIN32 |
1206 | #ifdef _WIN32 |
1112 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1207 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1113 | #else |
1208 | #else |
1114 | return fcntl (fd, F_GETFD) != -1; |
1209 | return fcntl (fd, F_GETFD) != -1; |
1115 | #endif |
1210 | #endif |
1116 | } |
1211 | } |
1117 | |
1212 | |
1118 | /* called on EBADF to verify fds */ |
1213 | /* called on EBADF to verify fds */ |
1119 | static void noinline |
1214 | static void noinline ecb_cold |
1120 | fd_ebadf (EV_P) |
1215 | fd_ebadf (EV_P) |
1121 | { |
1216 | { |
1122 | int fd; |
1217 | int fd; |
1123 | |
1218 | |
1124 | for (fd = 0; fd < anfdmax; ++fd) |
1219 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
1126 | if (!fd_valid (fd) && errno == EBADF) |
1221 | if (!fd_valid (fd) && errno == EBADF) |
1127 | fd_kill (EV_A_ fd); |
1222 | fd_kill (EV_A_ fd); |
1128 | } |
1223 | } |
1129 | |
1224 | |
1130 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
1225 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
1131 | static void noinline |
1226 | static void noinline ecb_cold |
1132 | fd_enomem (EV_P) |
1227 | fd_enomem (EV_P) |
1133 | { |
1228 | { |
1134 | int fd; |
1229 | int fd; |
1135 | |
1230 | |
1136 | for (fd = anfdmax; fd--; ) |
1231 | for (fd = anfdmax; fd--; ) |
… | |
… | |
1331 | |
1426 | |
1332 | /*****************************************************************************/ |
1427 | /*****************************************************************************/ |
1333 | |
1428 | |
1334 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1429 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1335 | |
1430 | |
1336 | static void noinline |
1431 | static void noinline ecb_cold |
1337 | evpipe_init (EV_P) |
1432 | evpipe_init (EV_P) |
1338 | { |
1433 | { |
1339 | if (!ev_is_active (&pipe_w)) |
1434 | if (!ev_is_active (&pipe_w)) |
1340 | { |
1435 | { |
1341 | # if EV_USE_EVENTFD |
1436 | # if EV_USE_EVENTFD |
… | |
… | |
1363 | ev_io_start (EV_A_ &pipe_w); |
1458 | ev_io_start (EV_A_ &pipe_w); |
1364 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1459 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1365 | } |
1460 | } |
1366 | } |
1461 | } |
1367 | |
1462 | |
1368 | inline_size void |
1463 | inline_speed void |
1369 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1464 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1370 | { |
1465 | { |
1371 | if (!*flag) |
1466 | if (expect_true (*flag)) |
|
|
1467 | return; |
|
|
1468 | |
|
|
1469 | *flag = 1; |
|
|
1470 | |
|
|
1471 | ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ |
|
|
1472 | |
|
|
1473 | pipe_write_skipped = 1; |
|
|
1474 | |
|
|
1475 | ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */ |
|
|
1476 | |
|
|
1477 | if (pipe_write_wanted) |
1372 | { |
1478 | { |
|
|
1479 | int old_errno; |
|
|
1480 | |
|
|
1481 | pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ |
|
|
1482 | |
1373 | int old_errno = errno; /* save errno because write might clobber it */ |
1483 | old_errno = errno; /* save errno because write will clobber it */ |
1374 | char dummy; |
|
|
1375 | |
|
|
1376 | *flag = 1; |
|
|
1377 | |
1484 | |
1378 | #if EV_USE_EVENTFD |
1485 | #if EV_USE_EVENTFD |
1379 | if (evfd >= 0) |
1486 | if (evfd >= 0) |
1380 | { |
1487 | { |
1381 | uint64_t counter = 1; |
1488 | uint64_t counter = 1; |
1382 | write (evfd, &counter, sizeof (uint64_t)); |
1489 | write (evfd, &counter, sizeof (uint64_t)); |
1383 | } |
1490 | } |
1384 | else |
1491 | else |
1385 | #endif |
1492 | #endif |
|
|
1493 | { |
1386 | /* win32 people keep sending patches that change this write() to send() */ |
1494 | /* 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 */ |
1495 | /* 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 */ |
1496 | /* 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 */ |
1497 | /* where your send() is from - it's definitely not the microsoft send, and */ |
1390 | /* tell me. thank you. */ |
1498 | /* tell me. thank you. */ |
1391 | write (evpipe [1], &dummy, 1); |
1499 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
1500 | } |
1392 | |
1501 | |
1393 | errno = old_errno; |
1502 | errno = old_errno; |
1394 | } |
1503 | } |
1395 | } |
1504 | } |
1396 | |
1505 | |
… | |
… | |
1399 | static void |
1508 | static void |
1400 | pipecb (EV_P_ ev_io *iow, int revents) |
1509 | pipecb (EV_P_ ev_io *iow, int revents) |
1401 | { |
1510 | { |
1402 | int i; |
1511 | int i; |
1403 | |
1512 | |
|
|
1513 | if (revents & EV_READ) |
|
|
1514 | { |
1404 | #if EV_USE_EVENTFD |
1515 | #if EV_USE_EVENTFD |
1405 | if (evfd >= 0) |
1516 | if (evfd >= 0) |
1406 | { |
1517 | { |
1407 | uint64_t counter; |
1518 | uint64_t counter; |
1408 | read (evfd, &counter, sizeof (uint64_t)); |
1519 | read (evfd, &counter, sizeof (uint64_t)); |
1409 | } |
1520 | } |
1410 | else |
1521 | else |
1411 | #endif |
1522 | #endif |
1412 | { |
1523 | { |
1413 | char dummy; |
1524 | char dummy; |
1414 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1525 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1415 | read (evpipe [0], &dummy, 1); |
1526 | read (evpipe [0], &dummy, 1); |
|
|
1527 | } |
1416 | } |
1528 | } |
|
|
1529 | |
|
|
1530 | pipe_write_skipped = 0; |
1417 | |
1531 | |
1418 | #if EV_SIGNAL_ENABLE |
1532 | #if EV_SIGNAL_ENABLE |
1419 | if (sig_pending) |
1533 | if (sig_pending) |
1420 | { |
1534 | { |
1421 | sig_pending = 0; |
1535 | sig_pending = 0; |
… | |
… | |
1450 | EV_P = signals [signum - 1].loop; |
1564 | EV_P = signals [signum - 1].loop; |
1451 | |
1565 | |
1452 | if (!EV_A) |
1566 | if (!EV_A) |
1453 | return; |
1567 | return; |
1454 | #endif |
1568 | #endif |
|
|
1569 | |
|
|
1570 | if (!ev_active (&pipe_w)) |
|
|
1571 | return; |
1455 | |
1572 | |
1456 | signals [signum - 1].pending = 1; |
1573 | signals [signum - 1].pending = 1; |
1457 | evpipe_write (EV_A_ &sig_pending); |
1574 | evpipe_write (EV_A_ &sig_pending); |
1458 | } |
1575 | } |
1459 | |
1576 | |
… | |
… | |
1591 | #endif |
1708 | #endif |
1592 | #if EV_USE_SELECT |
1709 | #if EV_USE_SELECT |
1593 | # include "ev_select.c" |
1710 | # include "ev_select.c" |
1594 | #endif |
1711 | #endif |
1595 | |
1712 | |
1596 | int |
1713 | int ecb_cold |
1597 | ev_version_major (void) |
1714 | ev_version_major (void) |
1598 | { |
1715 | { |
1599 | return EV_VERSION_MAJOR; |
1716 | return EV_VERSION_MAJOR; |
1600 | } |
1717 | } |
1601 | |
1718 | |
1602 | int |
1719 | int ecb_cold |
1603 | ev_version_minor (void) |
1720 | ev_version_minor (void) |
1604 | { |
1721 | { |
1605 | return EV_VERSION_MINOR; |
1722 | return EV_VERSION_MINOR; |
1606 | } |
1723 | } |
1607 | |
1724 | |
1608 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1725 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1609 | int inline_size |
1726 | int inline_size ecb_cold |
1610 | enable_secure (void) |
1727 | enable_secure (void) |
1611 | { |
1728 | { |
1612 | #ifdef _WIN32 |
1729 | #ifdef _WIN32 |
1613 | return 0; |
1730 | return 0; |
1614 | #else |
1731 | #else |
1615 | return getuid () != geteuid () |
1732 | return getuid () != geteuid () |
1616 | || getgid () != getegid (); |
1733 | || getgid () != getegid (); |
1617 | #endif |
1734 | #endif |
1618 | } |
1735 | } |
1619 | |
1736 | |
1620 | unsigned int |
1737 | unsigned int ecb_cold |
1621 | ev_supported_backends (void) |
1738 | ev_supported_backends (void) |
1622 | { |
1739 | { |
1623 | unsigned int flags = 0; |
1740 | unsigned int flags = 0; |
1624 | |
1741 | |
1625 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
1742 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
… | |
… | |
1629 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1746 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1630 | |
1747 | |
1631 | return flags; |
1748 | return flags; |
1632 | } |
1749 | } |
1633 | |
1750 | |
1634 | unsigned int |
1751 | unsigned int ecb_cold |
1635 | ev_recommended_backends (void) |
1752 | ev_recommended_backends (void) |
1636 | { |
1753 | { |
1637 | unsigned int flags = ev_supported_backends (); |
1754 | unsigned int flags = ev_supported_backends (); |
1638 | |
1755 | |
1639 | #ifndef __NetBSD__ |
1756 | #ifndef __NetBSD__ |
… | |
… | |
1651 | #endif |
1768 | #endif |
1652 | |
1769 | |
1653 | return flags; |
1770 | return flags; |
1654 | } |
1771 | } |
1655 | |
1772 | |
1656 | unsigned int |
1773 | unsigned int ecb_cold |
1657 | ev_embeddable_backends (void) |
1774 | ev_embeddable_backends (void) |
1658 | { |
1775 | { |
1659 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1776 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1660 | |
1777 | |
1661 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
1778 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
… | |
… | |
1706 | ev_userdata (EV_P) |
1823 | ev_userdata (EV_P) |
1707 | { |
1824 | { |
1708 | return userdata; |
1825 | return userdata; |
1709 | } |
1826 | } |
1710 | |
1827 | |
|
|
1828 | void |
1711 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1829 | ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1712 | { |
1830 | { |
1713 | invoke_cb = invoke_pending_cb; |
1831 | invoke_cb = invoke_pending_cb; |
1714 | } |
1832 | } |
1715 | |
1833 | |
|
|
1834 | void |
1716 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1835 | ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1717 | { |
1836 | { |
1718 | release_cb = release; |
1837 | release_cb = release; |
1719 | acquire_cb = acquire; |
1838 | acquire_cb = acquire; |
1720 | } |
1839 | } |
1721 | #endif |
1840 | #endif |
1722 | |
1841 | |
1723 | /* initialise a loop structure, must be zero-initialised */ |
1842 | /* initialise a loop structure, must be zero-initialised */ |
1724 | static void noinline |
1843 | static void noinline ecb_cold |
1725 | loop_init (EV_P_ unsigned int flags) |
1844 | loop_init (EV_P_ unsigned int flags) |
1726 | { |
1845 | { |
1727 | if (!backend) |
1846 | if (!backend) |
1728 | { |
1847 | { |
1729 | origflags = flags; |
1848 | origflags = flags; |
… | |
… | |
1757 | if (!(flags & EVFLAG_NOENV) |
1876 | if (!(flags & EVFLAG_NOENV) |
1758 | && !enable_secure () |
1877 | && !enable_secure () |
1759 | && getenv ("LIBEV_FLAGS")) |
1878 | && getenv ("LIBEV_FLAGS")) |
1760 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1879 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1761 | |
1880 | |
1762 | ev_rt_now = ev_time (); |
1881 | ev_rt_now = ev_time (); |
1763 | mn_now = get_clock (); |
1882 | mn_now = get_clock (); |
1764 | now_floor = mn_now; |
1883 | now_floor = mn_now; |
1765 | rtmn_diff = ev_rt_now - mn_now; |
1884 | rtmn_diff = ev_rt_now - mn_now; |
1766 | #if EV_FEATURE_API |
1885 | #if EV_FEATURE_API |
1767 | invoke_cb = ev_invoke_pending; |
1886 | invoke_cb = ev_invoke_pending; |
1768 | #endif |
1887 | #endif |
1769 | |
1888 | |
1770 | io_blocktime = 0.; |
1889 | io_blocktime = 0.; |
1771 | timeout_blocktime = 0.; |
1890 | timeout_blocktime = 0.; |
1772 | backend = 0; |
1891 | backend = 0; |
1773 | backend_fd = -1; |
1892 | backend_fd = -1; |
1774 | sig_pending = 0; |
1893 | sig_pending = 0; |
1775 | #if EV_ASYNC_ENABLE |
1894 | #if EV_ASYNC_ENABLE |
1776 | async_pending = 0; |
1895 | async_pending = 0; |
1777 | #endif |
1896 | #endif |
|
|
1897 | pipe_write_skipped = 0; |
|
|
1898 | pipe_write_wanted = 0; |
1778 | #if EV_USE_INOTIFY |
1899 | #if EV_USE_INOTIFY |
1779 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1900 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1780 | #endif |
1901 | #endif |
1781 | #if EV_USE_SIGNALFD |
1902 | #if EV_USE_SIGNALFD |
1782 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1903 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1783 | #endif |
1904 | #endif |
1784 | |
1905 | |
1785 | if (!(flags & EVBACKEND_MASK)) |
1906 | if (!(flags & EVBACKEND_MASK)) |
1786 | flags |= ev_recommended_backends (); |
1907 | flags |= ev_recommended_backends (); |
1787 | |
1908 | |
… | |
… | |
1812 | #endif |
1933 | #endif |
1813 | } |
1934 | } |
1814 | } |
1935 | } |
1815 | |
1936 | |
1816 | /* free up a loop structure */ |
1937 | /* free up a loop structure */ |
1817 | void |
1938 | void ecb_cold |
1818 | ev_loop_destroy (EV_P) |
1939 | ev_loop_destroy (EV_P) |
1819 | { |
1940 | { |
1820 | int i; |
1941 | int i; |
1821 | |
1942 | |
1822 | #if EV_MULTIPLICITY |
1943 | #if EV_MULTIPLICITY |
… | |
… | |
1952 | infy_fork (EV_A); |
2073 | infy_fork (EV_A); |
1953 | #endif |
2074 | #endif |
1954 | |
2075 | |
1955 | if (ev_is_active (&pipe_w)) |
2076 | if (ev_is_active (&pipe_w)) |
1956 | { |
2077 | { |
1957 | /* this "locks" the handlers against writing to the pipe */ |
2078 | /* 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 | |
2079 | |
1964 | ev_ref (EV_A); |
2080 | ev_ref (EV_A); |
1965 | ev_io_stop (EV_A_ &pipe_w); |
2081 | ev_io_stop (EV_A_ &pipe_w); |
1966 | |
2082 | |
1967 | #if EV_USE_EVENTFD |
2083 | #if EV_USE_EVENTFD |
… | |
… | |
1985 | postfork = 0; |
2101 | postfork = 0; |
1986 | } |
2102 | } |
1987 | |
2103 | |
1988 | #if EV_MULTIPLICITY |
2104 | #if EV_MULTIPLICITY |
1989 | |
2105 | |
1990 | struct ev_loop * |
2106 | struct ev_loop * ecb_cold |
1991 | ev_loop_new (unsigned int flags) |
2107 | ev_loop_new (unsigned int flags) |
1992 | { |
2108 | { |
1993 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2109 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1994 | |
2110 | |
1995 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2111 | memset (EV_A, 0, sizeof (struct ev_loop)); |
… | |
… | |
2003 | } |
2119 | } |
2004 | |
2120 | |
2005 | #endif /* multiplicity */ |
2121 | #endif /* multiplicity */ |
2006 | |
2122 | |
2007 | #if EV_VERIFY |
2123 | #if EV_VERIFY |
2008 | static void noinline |
2124 | static void noinline ecb_cold |
2009 | verify_watcher (EV_P_ W w) |
2125 | verify_watcher (EV_P_ W w) |
2010 | { |
2126 | { |
2011 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2127 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2012 | |
2128 | |
2013 | if (w->pending) |
2129 | if (w->pending) |
2014 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2130 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2015 | } |
2131 | } |
2016 | |
2132 | |
2017 | static void noinline |
2133 | static void noinline ecb_cold |
2018 | verify_heap (EV_P_ ANHE *heap, int N) |
2134 | verify_heap (EV_P_ ANHE *heap, int N) |
2019 | { |
2135 | { |
2020 | int i; |
2136 | int i; |
2021 | |
2137 | |
2022 | for (i = HEAP0; i < N + HEAP0; ++i) |
2138 | for (i = HEAP0; i < N + HEAP0; ++i) |
… | |
… | |
2027 | |
2143 | |
2028 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2144 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2029 | } |
2145 | } |
2030 | } |
2146 | } |
2031 | |
2147 | |
2032 | static void noinline |
2148 | static void noinline ecb_cold |
2033 | array_verify (EV_P_ W *ws, int cnt) |
2149 | array_verify (EV_P_ W *ws, int cnt) |
2034 | { |
2150 | { |
2035 | while (cnt--) |
2151 | while (cnt--) |
2036 | { |
2152 | { |
2037 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
2153 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
… | |
… | |
2039 | } |
2155 | } |
2040 | } |
2156 | } |
2041 | #endif |
2157 | #endif |
2042 | |
2158 | |
2043 | #if EV_FEATURE_API |
2159 | #if EV_FEATURE_API |
2044 | void |
2160 | void ecb_cold |
2045 | ev_verify (EV_P) |
2161 | ev_verify (EV_P) |
2046 | { |
2162 | { |
2047 | #if EV_VERIFY |
2163 | #if EV_VERIFY |
2048 | int i; |
2164 | int i; |
2049 | WL w; |
2165 | WL w; |
… | |
… | |
2115 | #endif |
2231 | #endif |
2116 | } |
2232 | } |
2117 | #endif |
2233 | #endif |
2118 | |
2234 | |
2119 | #if EV_MULTIPLICITY |
2235 | #if EV_MULTIPLICITY |
2120 | struct ev_loop * |
2236 | struct ev_loop * ecb_cold |
2121 | #else |
2237 | #else |
2122 | int |
2238 | int |
2123 | #endif |
2239 | #endif |
2124 | ev_default_loop (unsigned int flags) |
2240 | ev_default_loop (unsigned int flags) |
2125 | { |
2241 | { |
… | |
… | |
2324 | } |
2440 | } |
2325 | } |
2441 | } |
2326 | |
2442 | |
2327 | /* simply recalculate all periodics */ |
2443 | /* simply recalculate all periodics */ |
2328 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2444 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2329 | static void noinline |
2445 | static void noinline ecb_cold |
2330 | periodics_reschedule (EV_P) |
2446 | periodics_reschedule (EV_P) |
2331 | { |
2447 | { |
2332 | int i; |
2448 | int i; |
2333 | |
2449 | |
2334 | /* adjust periodics after time jump */ |
2450 | /* adjust periodics after time jump */ |
… | |
… | |
2347 | reheap (periodics, periodiccnt); |
2463 | reheap (periodics, periodiccnt); |
2348 | } |
2464 | } |
2349 | #endif |
2465 | #endif |
2350 | |
2466 | |
2351 | /* adjust all timers by a given offset */ |
2467 | /* adjust all timers by a given offset */ |
2352 | static void noinline |
2468 | static void noinline ecb_cold |
2353 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2469 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2354 | { |
2470 | { |
2355 | int i; |
2471 | int i; |
2356 | |
2472 | |
2357 | for (i = 0; i < timercnt; ++i) |
2473 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
2499 | ev_tstamp prev_mn_now = mn_now; |
2615 | ev_tstamp prev_mn_now = mn_now; |
2500 | |
2616 | |
2501 | /* update time to cancel out callback processing overhead */ |
2617 | /* update time to cancel out callback processing overhead */ |
2502 | time_update (EV_A_ 1e100); |
2618 | time_update (EV_A_ 1e100); |
2503 | |
2619 | |
|
|
2620 | /* from now on, we want a pipe-wake-up */ |
|
|
2621 | pipe_write_wanted = 1; |
|
|
2622 | |
|
|
2623 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
|
|
2624 | |
2504 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) |
2625 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2505 | { |
2626 | { |
2506 | waittime = MAX_BLOCKTIME; |
2627 | waittime = MAX_BLOCKTIME; |
2507 | |
2628 | |
2508 | if (timercnt) |
2629 | if (timercnt) |
2509 | { |
2630 | { |
2510 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_mintime; |
2631 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
2511 | if (waittime > to) waittime = to; |
2632 | if (waittime > to) waittime = to; |
2512 | } |
2633 | } |
2513 | |
2634 | |
2514 | #if EV_PERIODIC_ENABLE |
2635 | #if EV_PERIODIC_ENABLE |
2515 | if (periodiccnt) |
2636 | if (periodiccnt) |
2516 | { |
2637 | { |
2517 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_mintime; |
2638 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now; |
2518 | if (waittime > to) waittime = to; |
2639 | if (waittime > to) waittime = to; |
2519 | } |
2640 | } |
2520 | #endif |
2641 | #endif |
2521 | |
2642 | |
2522 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2643 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2523 | if (expect_false (waittime < timeout_blocktime)) |
2644 | if (expect_false (waittime < timeout_blocktime)) |
2524 | waittime = timeout_blocktime; |
2645 | waittime = timeout_blocktime; |
|
|
2646 | |
|
|
2647 | /* at this point, we NEED to wait, so we have to ensure */ |
|
|
2648 | /* to pass a minimum nonzero value to the backend */ |
|
|
2649 | if (expect_false (waittime < backend_mintime)) |
|
|
2650 | waittime = backend_mintime; |
2525 | |
2651 | |
2526 | /* extra check because io_blocktime is commonly 0 */ |
2652 | /* extra check because io_blocktime is commonly 0 */ |
2527 | if (expect_false (io_blocktime)) |
2653 | if (expect_false (io_blocktime)) |
2528 | { |
2654 | { |
2529 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
2655 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
… | |
… | |
2543 | ++loop_count; |
2669 | ++loop_count; |
2544 | #endif |
2670 | #endif |
2545 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2671 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2546 | backend_poll (EV_A_ waittime); |
2672 | backend_poll (EV_A_ waittime); |
2547 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2673 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
|
|
2674 | |
|
|
2675 | pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ |
|
|
2676 | |
|
|
2677 | if (pipe_write_skipped) |
|
|
2678 | { |
|
|
2679 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
|
|
2680 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
|
|
2681 | } |
|
|
2682 | |
2548 | |
2683 | |
2549 | /* update ev_rt_now, do magic */ |
2684 | /* update ev_rt_now, do magic */ |
2550 | time_update (EV_A_ waittime + sleeptime); |
2685 | time_update (EV_A_ waittime + sleeptime); |
2551 | } |
2686 | } |
2552 | |
2687 | |
… | |
… | |
3206 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3341 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3207 | ofs += sizeof (struct inotify_event) + ev->len; |
3342 | ofs += sizeof (struct inotify_event) + ev->len; |
3208 | } |
3343 | } |
3209 | } |
3344 | } |
3210 | |
3345 | |
3211 | inline_size void |
3346 | inline_size void ecb_cold |
3212 | ev_check_2625 (EV_P) |
3347 | ev_check_2625 (EV_P) |
3213 | { |
3348 | { |
3214 | /* kernels < 2.6.25 are borked |
3349 | /* kernels < 2.6.25 are borked |
3215 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3350 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3216 | */ |
3351 | */ |
… | |
… | |
3841 | } |
3976 | } |
3842 | |
3977 | |
3843 | /*****************************************************************************/ |
3978 | /*****************************************************************************/ |
3844 | |
3979 | |
3845 | #if EV_WALK_ENABLE |
3980 | #if EV_WALK_ENABLE |
3846 | void |
3981 | void ecb_cold |
3847 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3982 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3848 | { |
3983 | { |
3849 | int i, j; |
3984 | int i, j; |
3850 | ev_watcher_list *wl, *wn; |
3985 | ev_watcher_list *wl, *wn; |
3851 | |
3986 | |
… | |
… | |
3895 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
4030 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
3896 | #endif |
4031 | #endif |
3897 | |
4032 | |
3898 | #if EV_IDLE_ENABLE |
4033 | #if EV_IDLE_ENABLE |
3899 | if (types & EV_IDLE) |
4034 | if (types & EV_IDLE) |
3900 | for (j = NUMPRI; i--; ) |
4035 | for (j = NUMPRI; j--; ) |
3901 | for (i = idlecnt [j]; i--; ) |
4036 | for (i = idlecnt [j]; i--; ) |
3902 | cb (EV_A_ EV_IDLE, idles [j][i]); |
4037 | cb (EV_A_ EV_IDLE, idles [j][i]); |
3903 | #endif |
4038 | #endif |
3904 | |
4039 | |
3905 | #if EV_FORK_ENABLE |
4040 | #if EV_FORK_ENABLE |