… | |
… | |
183 | # include EV_H |
183 | # include EV_H |
184 | #else |
184 | #else |
185 | # include "ev.h" |
185 | # include "ev.h" |
186 | #endif |
186 | #endif |
187 | |
187 | |
188 | EV_CPP(extern "C" {) |
|
|
189 | |
|
|
190 | #ifndef _WIN32 |
188 | #ifndef _WIN32 |
191 | # include <sys/time.h> |
189 | # include <sys/time.h> |
192 | # include <sys/wait.h> |
190 | # include <sys/wait.h> |
193 | # include <unistd.h> |
191 | # include <unistd.h> |
194 | #else |
192 | #else |
… | |
… | |
464 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
462 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
465 | |
463 | |
466 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
464 | #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) |
465 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
468 | |
466 | |
469 | /* the following are taken from libecb */ |
467 | /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ |
470 | /* ecb.h start */ |
468 | /* ECB.H BEGIN */ |
|
|
469 | /* |
|
|
470 | * libecb - http://software.schmorp.de/pkg/libecb |
|
|
471 | * |
|
|
472 | * Copyright (©) 2009-2012 Marc Alexander Lehmann <libecb@schmorp.de> |
|
|
473 | * Copyright (©) 2011 Emanuele Giaquinta |
|
|
474 | * All rights reserved. |
|
|
475 | * |
|
|
476 | * Redistribution and use in source and binary forms, with or without modifica- |
|
|
477 | * tion, are permitted provided that the following conditions are met: |
|
|
478 | * |
|
|
479 | * 1. Redistributions of source code must retain the above copyright notice, |
|
|
480 | * this list of conditions and the following disclaimer. |
|
|
481 | * |
|
|
482 | * 2. Redistributions in binary form must reproduce the above copyright |
|
|
483 | * notice, this list of conditions and the following disclaimer in the |
|
|
484 | * documentation and/or other materials provided with the distribution. |
|
|
485 | * |
|
|
486 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
|
487 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
|
|
488 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
|
489 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
|
|
490 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
|
491 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
|
492 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
|
493 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
|
|
494 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|
|
495 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
496 | */ |
|
|
497 | |
|
|
498 | #ifndef ECB_H |
|
|
499 | #define ECB_H |
|
|
500 | |
|
|
501 | #ifdef _WIN32 |
|
|
502 | typedef signed char int8_t; |
|
|
503 | typedef unsigned char uint8_t; |
|
|
504 | typedef signed short int16_t; |
|
|
505 | typedef unsigned short uint16_t; |
|
|
506 | typedef signed int int32_t; |
|
|
507 | typedef unsigned int uint32_t; |
|
|
508 | #if __GNUC__ |
|
|
509 | typedef signed long long int64_t; |
|
|
510 | typedef unsigned long long uint64_t; |
|
|
511 | #else /* _MSC_VER || __BORLANDC__ */ |
|
|
512 | typedef signed __int64 int64_t; |
|
|
513 | typedef unsigned __int64 uint64_t; |
|
|
514 | #endif |
|
|
515 | #else |
|
|
516 | #include <inttypes.h> |
|
|
517 | #endif |
471 | |
518 | |
472 | /* many compilers define _GNUC_ to some versions but then only implement |
519 | /* many compilers define _GNUC_ to some versions but then only implement |
473 | * what their idiot authors think are the "more important" extensions, |
520 | * what their idiot authors think are the "more important" extensions, |
474 | * causing enourmous grief in return for some better fake benchmark numbers. |
521 | * causing enormous grief in return for some better fake benchmark numbers. |
475 | * or so. |
522 | * or so. |
476 | * we try to detect these and simply assume they are not gcc - if they have |
523 | * 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. |
524 | * an issue with that they should have done it right in the first place. |
478 | */ |
525 | */ |
479 | #ifndef ECB_GCC_VERSION |
526 | #ifndef ECB_GCC_VERSION |
… | |
… | |
482 | #else |
529 | #else |
483 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
530 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
484 | #endif |
531 | #endif |
485 | #endif |
532 | #endif |
486 | |
533 | |
|
|
534 | /*****************************************************************************/ |
|
|
535 | |
|
|
536 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
|
|
537 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
|
|
538 | |
|
|
539 | #if ECB_NO_THREADS || ECB_NO_SMP |
|
|
540 | #define ECB_MEMORY_FENCE do { } while (0) |
|
|
541 | #endif |
|
|
542 | |
|
|
543 | #ifndef ECB_MEMORY_FENCE |
|
|
544 | #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || defined(__clang__) || __SUNPRO_C >= 0x5110 || __SUNPRO_xC >= 0x5110 |
|
|
545 | #if __i386__ |
|
|
546 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
|
|
547 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
|
|
548 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
|
|
549 | #elif __amd64 |
|
|
550 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
|
|
551 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
|
|
552 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ |
|
|
553 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
|
|
554 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
|
|
555 | #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ |
|
|
556 | || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) |
|
|
557 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
|
|
558 | #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ |
|
|
559 | || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) |
|
|
560 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") |
|
|
561 | #elif defined(__sparc) |
|
|
562 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #StoreLoad | #LoadLoad | #StoreStore" : : : "memory") |
|
|
563 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadLoad" : : : "memory") |
|
|
564 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #StoreStore") |
|
|
565 | #endif |
|
|
566 | #endif |
|
|
567 | #endif |
|
|
568 | |
|
|
569 | #ifndef ECB_MEMORY_FENCE |
|
|
570 | #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) |
|
|
571 | #define ECB_MEMORY_FENCE __sync_synchronize () |
|
|
572 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
|
|
573 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
|
|
574 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
|
|
575 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
|
|
576 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
|
|
577 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
|
|
578 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
|
|
579 | #elif defined(_WIN32) |
|
|
580 | #include <WinNT.h> |
|
|
581 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
|
|
582 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
|
|
583 | #include <mbarrier.h> |
|
|
584 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
|
|
585 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () |
|
|
586 | #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () |
|
|
587 | #endif |
|
|
588 | #endif |
|
|
589 | |
|
|
590 | #ifndef ECB_MEMORY_FENCE |
|
|
591 | #if !ECB_AVOID_PTHREADS |
|
|
592 | /* |
|
|
593 | * if you get undefined symbol references to pthread_mutex_lock, |
|
|
594 | * or failure to find pthread.h, then you should implement |
|
|
595 | * the ECB_MEMORY_FENCE operations for your cpu/compiler |
|
|
596 | * OR provide pthread.h and link against the posix thread library |
|
|
597 | * of your system. |
|
|
598 | */ |
|
|
599 | #include <pthread.h> |
|
|
600 | #define ECB_NEEDS_PTHREADS 1 |
|
|
601 | #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 |
|
|
602 | |
|
|
603 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
|
|
604 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
|
|
605 | #endif |
|
|
606 | #endif |
|
|
607 | |
|
|
608 | #if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) |
|
|
609 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
610 | #endif |
|
|
611 | |
|
|
612 | #if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) |
|
|
613 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
614 | #endif |
|
|
615 | |
|
|
616 | /*****************************************************************************/ |
|
|
617 | |
|
|
618 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
|
|
619 | |
487 | #if __cplusplus |
620 | #if __cplusplus |
488 | #define ecb_inline static inline |
621 | #define ecb_inline static inline |
489 | #elif ECB_GCC_VERSION(2,5) |
622 | #elif ECB_GCC_VERSION(2,5) |
490 | #define ecb_inline static __inline__ |
623 | #define ecb_inline static __inline__ |
491 | #elif ECB_C99 |
624 | #elif ECB_C99 |
492 | #define ecb_inline static inline |
625 | #define ecb_inline static inline |
493 | #else |
626 | #else |
494 | #define ecb_inline static |
627 | #define ecb_inline static |
495 | #endif |
628 | #endif |
|
|
629 | |
|
|
630 | #if ECB_GCC_VERSION(3,3) |
|
|
631 | #define ecb_restrict __restrict__ |
|
|
632 | #elif ECB_C99 |
|
|
633 | #define ecb_restrict restrict |
|
|
634 | #else |
|
|
635 | #define ecb_restrict |
|
|
636 | #endif |
|
|
637 | |
|
|
638 | typedef int ecb_bool; |
|
|
639 | |
|
|
640 | #define ECB_CONCAT_(a, b) a ## b |
|
|
641 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
|
|
642 | #define ECB_STRINGIFY_(a) # a |
|
|
643 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
|
|
644 | |
|
|
645 | #define ecb_function_ ecb_inline |
496 | |
646 | |
497 | #if ECB_GCC_VERSION(3,1) |
647 | #if ECB_GCC_VERSION(3,1) |
498 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
648 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
499 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
649 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
500 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
650 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
… | |
… | |
504 | #define ecb_is_constant(expr) 0 |
654 | #define ecb_is_constant(expr) 0 |
505 | #define ecb_expect(expr,value) (expr) |
655 | #define ecb_expect(expr,value) (expr) |
506 | #define ecb_prefetch(addr,rw,locality) |
656 | #define ecb_prefetch(addr,rw,locality) |
507 | #endif |
657 | #endif |
508 | |
658 | |
|
|
659 | /* no emulation for ecb_decltype */ |
|
|
660 | #if ECB_GCC_VERSION(4,5) |
|
|
661 | #define ecb_decltype(x) __decltype(x) |
|
|
662 | #elif ECB_GCC_VERSION(3,0) |
|
|
663 | #define ecb_decltype(x) __typeof(x) |
|
|
664 | #endif |
|
|
665 | |
509 | #define ecb_noinline ecb_attribute ((__noinline__)) |
666 | #define ecb_noinline ecb_attribute ((__noinline__)) |
510 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
667 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
511 | #define ecb_unused ecb_attribute ((__unused__)) |
668 | #define ecb_unused ecb_attribute ((__unused__)) |
512 | #define ecb_const ecb_attribute ((__const__)) |
669 | #define ecb_const ecb_attribute ((__const__)) |
513 | #define ecb_pure ecb_attribute ((__pure__)) |
670 | #define ecb_pure ecb_attribute ((__pure__)) |
… | |
… | |
525 | /* put around conditional expressions if you are very sure that the */ |
682 | /* put around conditional expressions if you are very sure that the */ |
526 | /* expression is mostly true or mostly false. note that these return */ |
683 | /* expression is mostly true or mostly false. note that these return */ |
527 | /* booleans, not the expression. */ |
684 | /* booleans, not the expression. */ |
528 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
685 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
529 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
686 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
530 | /* ecb.h end */ |
687 | /* for compatibility to the rest of the world */ |
|
|
688 | #define ecb_likely(expr) ecb_expect_true (expr) |
|
|
689 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
|
|
690 | |
|
|
691 | /* count trailing zero bits and count # of one bits */ |
|
|
692 | #if ECB_GCC_VERSION(3,4) |
|
|
693 | /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ |
|
|
694 | #define ecb_ld32(x) (__builtin_clz (x) ^ 31) |
|
|
695 | #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) |
|
|
696 | #define ecb_ctz32(x) __builtin_ctz (x) |
|
|
697 | #define ecb_ctz64(x) __builtin_ctzll (x) |
|
|
698 | #define ecb_popcount32(x) __builtin_popcount (x) |
|
|
699 | /* no popcountll */ |
|
|
700 | #else |
|
|
701 | ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const; |
|
|
702 | ecb_function_ int |
|
|
703 | ecb_ctz32 (uint32_t x) |
|
|
704 | { |
|
|
705 | int r = 0; |
|
|
706 | |
|
|
707 | x &= ~x + 1; /* this isolates the lowest bit */ |
|
|
708 | |
|
|
709 | #if ECB_branchless_on_i386 |
|
|
710 | r += !!(x & 0xaaaaaaaa) << 0; |
|
|
711 | r += !!(x & 0xcccccccc) << 1; |
|
|
712 | r += !!(x & 0xf0f0f0f0) << 2; |
|
|
713 | r += !!(x & 0xff00ff00) << 3; |
|
|
714 | r += !!(x & 0xffff0000) << 4; |
|
|
715 | #else |
|
|
716 | if (x & 0xaaaaaaaa) r += 1; |
|
|
717 | if (x & 0xcccccccc) r += 2; |
|
|
718 | if (x & 0xf0f0f0f0) r += 4; |
|
|
719 | if (x & 0xff00ff00) r += 8; |
|
|
720 | if (x & 0xffff0000) r += 16; |
|
|
721 | #endif |
|
|
722 | |
|
|
723 | return r; |
|
|
724 | } |
|
|
725 | |
|
|
726 | ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const; |
|
|
727 | ecb_function_ int |
|
|
728 | ecb_ctz64 (uint64_t x) |
|
|
729 | { |
|
|
730 | int shift = x & 0xffffffffU ? 0 : 32; |
|
|
731 | return ecb_ctz32 (x >> shift) + shift; |
|
|
732 | } |
|
|
733 | |
|
|
734 | ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const; |
|
|
735 | ecb_function_ int |
|
|
736 | ecb_popcount32 (uint32_t x) |
|
|
737 | { |
|
|
738 | x -= (x >> 1) & 0x55555555; |
|
|
739 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
|
|
740 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
|
|
741 | x *= 0x01010101; |
|
|
742 | |
|
|
743 | return x >> 24; |
|
|
744 | } |
|
|
745 | |
|
|
746 | ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; |
|
|
747 | ecb_function_ int ecb_ld32 (uint32_t x) |
|
|
748 | { |
|
|
749 | int r = 0; |
|
|
750 | |
|
|
751 | if (x >> 16) { x >>= 16; r += 16; } |
|
|
752 | if (x >> 8) { x >>= 8; r += 8; } |
|
|
753 | if (x >> 4) { x >>= 4; r += 4; } |
|
|
754 | if (x >> 2) { x >>= 2; r += 2; } |
|
|
755 | if (x >> 1) { r += 1; } |
|
|
756 | |
|
|
757 | return r; |
|
|
758 | } |
|
|
759 | |
|
|
760 | ecb_function_ int ecb_ld64 (uint64_t x) ecb_const; |
|
|
761 | ecb_function_ int ecb_ld64 (uint64_t x) |
|
|
762 | { |
|
|
763 | int r = 0; |
|
|
764 | |
|
|
765 | if (x >> 32) { x >>= 32; r += 32; } |
|
|
766 | |
|
|
767 | return r + ecb_ld32 (x); |
|
|
768 | } |
|
|
769 | #endif |
|
|
770 | |
|
|
771 | ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const; |
|
|
772 | ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) |
|
|
773 | { |
|
|
774 | return ( (x * 0x0802U & 0x22110U) |
|
|
775 | | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; |
|
|
776 | } |
|
|
777 | |
|
|
778 | ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) ecb_const; |
|
|
779 | ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) |
|
|
780 | { |
|
|
781 | x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1); |
|
|
782 | x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2); |
|
|
783 | x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4); |
|
|
784 | x = ( x >> 8 ) | ( x << 8); |
|
|
785 | |
|
|
786 | return x; |
|
|
787 | } |
|
|
788 | |
|
|
789 | ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) ecb_const; |
|
|
790 | ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) |
|
|
791 | { |
|
|
792 | x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1); |
|
|
793 | x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2); |
|
|
794 | x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4); |
|
|
795 | x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8); |
|
|
796 | x = ( x >> 16 ) | ( x << 16); |
|
|
797 | |
|
|
798 | return x; |
|
|
799 | } |
|
|
800 | |
|
|
801 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
|
|
802 | /* so for this version we are lazy */ |
|
|
803 | ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; |
|
|
804 | ecb_function_ int |
|
|
805 | ecb_popcount64 (uint64_t x) |
|
|
806 | { |
|
|
807 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
|
|
808 | } |
|
|
809 | |
|
|
810 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const; |
|
|
811 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const; |
|
|
812 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const; |
|
|
813 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const; |
|
|
814 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
|
|
815 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
|
|
816 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const; |
|
|
817 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const; |
|
|
818 | |
|
|
819 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } |
|
|
820 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } |
|
|
821 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } |
|
|
822 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } |
|
|
823 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
|
|
824 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
|
|
825 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
|
|
826 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
|
|
827 | |
|
|
828 | #if ECB_GCC_VERSION(4,3) |
|
|
829 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
|
|
830 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
|
|
831 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
|
|
832 | #else |
|
|
833 | ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; |
|
|
834 | ecb_function_ uint16_t |
|
|
835 | ecb_bswap16 (uint16_t x) |
|
|
836 | { |
|
|
837 | return ecb_rotl16 (x, 8); |
|
|
838 | } |
|
|
839 | |
|
|
840 | ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
|
|
841 | ecb_function_ uint32_t |
|
|
842 | ecb_bswap32 (uint32_t x) |
|
|
843 | { |
|
|
844 | return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); |
|
|
845 | } |
|
|
846 | |
|
|
847 | ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const; |
|
|
848 | ecb_function_ uint64_t |
|
|
849 | ecb_bswap64 (uint64_t x) |
|
|
850 | { |
|
|
851 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
|
|
852 | } |
|
|
853 | #endif |
|
|
854 | |
|
|
855 | #if ECB_GCC_VERSION(4,5) |
|
|
856 | #define ecb_unreachable() __builtin_unreachable () |
|
|
857 | #else |
|
|
858 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
|
|
859 | ecb_function_ void ecb_unreachable (void) ecb_noreturn; |
|
|
860 | ecb_function_ void ecb_unreachable (void) { } |
|
|
861 | #endif |
|
|
862 | |
|
|
863 | /* try to tell the compiler that some condition is definitely true */ |
|
|
864 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
|
|
865 | |
|
|
866 | ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; |
|
|
867 | ecb_function_ unsigned char |
|
|
868 | ecb_byteorder_helper (void) |
|
|
869 | { |
|
|
870 | const uint32_t u = 0x11223344; |
|
|
871 | return *(unsigned char *)&u; |
|
|
872 | } |
|
|
873 | |
|
|
874 | ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; |
|
|
875 | ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
|
|
876 | ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; |
|
|
877 | ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
|
|
878 | |
|
|
879 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
|
|
880 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
|
|
881 | #else |
|
|
882 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
|
|
883 | #endif |
|
|
884 | |
|
|
885 | #if __cplusplus |
|
|
886 | template<typename T> |
|
|
887 | static inline T ecb_div_rd (T val, T div) |
|
|
888 | { |
|
|
889 | return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; |
|
|
890 | } |
|
|
891 | template<typename T> |
|
|
892 | static inline T ecb_div_ru (T val, T div) |
|
|
893 | { |
|
|
894 | return val < 0 ? - ((-val ) / div) : (val + div - 1) / div; |
|
|
895 | } |
|
|
896 | #else |
|
|
897 | #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) |
|
|
898 | #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) |
|
|
899 | #endif |
|
|
900 | |
|
|
901 | #if ecb_cplusplus_does_not_suck |
|
|
902 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
|
|
903 | template<typename T, int N> |
|
|
904 | static inline int ecb_array_length (const T (&arr)[N]) |
|
|
905 | { |
|
|
906 | return N; |
|
|
907 | } |
|
|
908 | #else |
|
|
909 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
|
|
910 | #endif |
|
|
911 | |
|
|
912 | #endif |
|
|
913 | |
|
|
914 | /* ECB.H END */ |
|
|
915 | |
|
|
916 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
|
|
917 | /* if your architecture doesn't need memory fences, e.g. because it is |
|
|
918 | * single-cpu/core, or if you use libev in a project that doesn't use libev |
|
|
919 | * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling |
|
|
920 | * libev, in which cases the memory fences become nops. |
|
|
921 | * alternatively, you can remove this #error and link against libpthread, |
|
|
922 | * which will then provide the memory fences. |
|
|
923 | */ |
|
|
924 | # error "memory fences not defined for your architecture, please report" |
|
|
925 | #endif |
|
|
926 | |
|
|
927 | #ifndef ECB_MEMORY_FENCE |
|
|
928 | # define ECB_MEMORY_FENCE do { } while (0) |
|
|
929 | # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
930 | # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
931 | #endif |
531 | |
932 | |
532 | #define expect_false(cond) ecb_expect_false (cond) |
933 | #define expect_false(cond) ecb_expect_false (cond) |
533 | #define expect_true(cond) ecb_expect_true (cond) |
934 | #define expect_true(cond) ecb_expect_true (cond) |
534 | #define noinline ecb_noinline |
935 | #define noinline ecb_noinline |
535 | |
936 | |
… | |
… | |
831 | #undef VAR |
1232 | #undef VAR |
832 | }; |
1233 | }; |
833 | #include "ev_wrap.h" |
1234 | #include "ev_wrap.h" |
834 | |
1235 | |
835 | static struct ev_loop default_loop_struct; |
1236 | static struct ev_loop default_loop_struct; |
836 | struct ev_loop *ev_default_loop_ptr; |
1237 | EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ |
837 | |
1238 | |
838 | #else |
1239 | #else |
839 | |
1240 | |
840 | ev_tstamp ev_rt_now; |
1241 | EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */ |
841 | #define VAR(name,decl) static decl; |
1242 | #define VAR(name,decl) static decl; |
842 | #include "ev_vars.h" |
1243 | #include "ev_vars.h" |
843 | #undef VAR |
1244 | #undef VAR |
844 | |
1245 | |
845 | static int ev_default_loop_ptr; |
1246 | static int ev_default_loop_ptr; |
… | |
… | |
939 | |
1340 | |
940 | do |
1341 | do |
941 | ncur <<= 1; |
1342 | ncur <<= 1; |
942 | while (cnt > ncur); |
1343 | while (cnt > ncur); |
943 | |
1344 | |
944 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ |
1345 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */ |
945 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
1346 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
946 | { |
1347 | { |
947 | ncur *= elem; |
1348 | ncur *= elem; |
948 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
1349 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
949 | ncur = ncur - sizeof (void *) * 4; |
1350 | ncur = ncur - sizeof (void *) * 4; |
… | |
… | |
964 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
1365 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
965 | |
1366 | |
966 | #define array_needsize(type,base,cur,cnt,init) \ |
1367 | #define array_needsize(type,base,cur,cnt,init) \ |
967 | if (expect_false ((cnt) > (cur))) \ |
1368 | if (expect_false ((cnt) > (cur))) \ |
968 | { \ |
1369 | { \ |
969 | int ocur_ = (cur); \ |
1370 | int ecb_unused ocur_ = (cur); \ |
970 | (base) = (type *)array_realloc \ |
1371 | (base) = (type *)array_realloc \ |
971 | (sizeof (type), (base), &(cur), (cnt)); \ |
1372 | (sizeof (type), (base), &(cur), (cnt)); \ |
972 | init ((base) + (ocur_), (cur) - ocur_); \ |
1373 | init ((base) + (ocur_), (cur) - ocur_); \ |
973 | } |
1374 | } |
974 | |
1375 | |
… | |
… | |
1420 | } |
1821 | } |
1421 | |
1822 | |
1422 | inline_speed void |
1823 | inline_speed void |
1423 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1824 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1424 | { |
1825 | { |
1425 | if (!*flag) |
1826 | if (expect_true (*flag)) |
1426 | { |
1827 | return; |
|
|
1828 | |
1427 | *flag = 1; |
1829 | *flag = 1; |
1428 | |
1830 | |
|
|
1831 | ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ |
|
|
1832 | |
1429 | pipe_write_skipped = 1; |
1833 | pipe_write_skipped = 1; |
1430 | |
1834 | |
|
|
1835 | ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */ |
|
|
1836 | |
1431 | if (pipe_write_wanted) |
1837 | if (pipe_write_wanted) |
|
|
1838 | { |
|
|
1839 | int old_errno; |
|
|
1840 | |
|
|
1841 | pipe_write_skipped = 0; /* just an optimisation, no fence needed */ |
|
|
1842 | |
|
|
1843 | old_errno = errno; /* save errno because write will clobber it */ |
|
|
1844 | |
|
|
1845 | #if EV_USE_EVENTFD |
|
|
1846 | if (evfd >= 0) |
1432 | { |
1847 | { |
1433 | int old_errno; |
|
|
1434 | |
|
|
1435 | pipe_write_skipped = 0; |
|
|
1436 | |
|
|
1437 | old_errno = errno; /* save errno because write will clobber it */ |
|
|
1438 | |
|
|
1439 | #if EV_USE_EVENTFD |
|
|
1440 | if (evfd >= 0) |
|
|
1441 | { |
|
|
1442 | uint64_t counter = 1; |
1848 | uint64_t counter = 1; |
1443 | write (evfd, &counter, sizeof (uint64_t)); |
1849 | 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; |
|
|
1457 | } |
1850 | } |
|
|
1851 | else |
|
|
1852 | #endif |
|
|
1853 | { |
|
|
1854 | /* win32 people keep sending patches that change this write() to send() */ |
|
|
1855 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
|
|
1856 | /* so when you think this write should be a send instead, please find out */ |
|
|
1857 | /* where your send() is from - it's definitely not the microsoft send, and */ |
|
|
1858 | /* tell me. thank you. */ |
|
|
1859 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | errno = old_errno; |
1458 | } |
1863 | } |
1459 | } |
1864 | } |
1460 | |
1865 | |
1461 | /* called whenever the libev signal pipe */ |
1866 | /* called whenever the libev signal pipe */ |
1462 | /* got some events (signal, async) */ |
1867 | /* got some events (signal, async) */ |
… | |
… | |
1520 | |
1925 | |
1521 | if (!EV_A) |
1926 | if (!EV_A) |
1522 | return; |
1927 | return; |
1523 | #endif |
1928 | #endif |
1524 | |
1929 | |
1525 | evpipe_init (EV_A); |
1930 | if (!ev_active (&pipe_w)) |
|
|
1931 | return; |
1526 | |
1932 | |
1527 | signals [signum - 1].pending = 1; |
1933 | signals [signum - 1].pending = 1; |
1528 | evpipe_write (EV_A_ &sig_pending); |
1934 | evpipe_write (EV_A_ &sig_pending); |
1529 | } |
1935 | } |
1530 | |
1936 | |
… | |
… | |
2572 | time_update (EV_A_ 1e100); |
2978 | time_update (EV_A_ 1e100); |
2573 | |
2979 | |
2574 | /* from now on, we want a pipe-wake-up */ |
2980 | /* from now on, we want a pipe-wake-up */ |
2575 | pipe_write_wanted = 1; |
2981 | pipe_write_wanted = 1; |
2576 | |
2982 | |
|
|
2983 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
|
|
2984 | |
2577 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2985 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2578 | { |
2986 | { |
2579 | waittime = MAX_BLOCKTIME; |
2987 | waittime = MAX_BLOCKTIME; |
2580 | |
2988 | |
2581 | if (timercnt) |
2989 | if (timercnt) |
… | |
… | |
2622 | #endif |
3030 | #endif |
2623 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
3031 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2624 | backend_poll (EV_A_ waittime); |
3032 | backend_poll (EV_A_ waittime); |
2625 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
3033 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2626 | |
3034 | |
2627 | pipe_write_wanted = 0; |
3035 | pipe_write_wanted = 0; /* just an optimisation, no fence needed */ |
2628 | |
3036 | |
2629 | if (pipe_write_skipped) |
3037 | if (pipe_write_skipped) |
2630 | { |
3038 | { |
2631 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
3039 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
2632 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
3040 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
… | |
… | |
3982 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
4390 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
3983 | #endif |
4391 | #endif |
3984 | |
4392 | |
3985 | #if EV_IDLE_ENABLE |
4393 | #if EV_IDLE_ENABLE |
3986 | if (types & EV_IDLE) |
4394 | if (types & EV_IDLE) |
3987 | for (j = NUMPRI; i--; ) |
4395 | for (j = NUMPRI; j--; ) |
3988 | for (i = idlecnt [j]; i--; ) |
4396 | for (i = idlecnt [j]; i--; ) |
3989 | cb (EV_A_ EV_IDLE, idles [j][i]); |
4397 | cb (EV_A_ EV_IDLE, idles [j][i]); |
3990 | #endif |
4398 | #endif |
3991 | |
4399 | |
3992 | #if EV_FORK_ENABLE |
4400 | #if EV_FORK_ENABLE |
… | |
… | |
4045 | |
4453 | |
4046 | #if EV_MULTIPLICITY |
4454 | #if EV_MULTIPLICITY |
4047 | #include "ev_wrap.h" |
4455 | #include "ev_wrap.h" |
4048 | #endif |
4456 | #endif |
4049 | |
4457 | |
4050 | EV_CPP(}) |
|
|
4051 | |
|
|