… | |
… | |
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-2011 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__) |
|
|
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 | #endif |
|
|
562 | #endif |
|
|
563 | #endif |
|
|
564 | |
|
|
565 | #ifndef ECB_MEMORY_FENCE |
|
|
566 | #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) |
|
|
567 | #define ECB_MEMORY_FENCE __sync_synchronize () |
|
|
568 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
|
|
569 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
|
|
570 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
|
|
571 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
|
|
572 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
|
|
573 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
|
|
574 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
|
|
575 | #elif defined(_WIN32) |
|
|
576 | #include <WinNT.h> |
|
|
577 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
|
|
578 | #endif |
|
|
579 | #endif |
|
|
580 | |
|
|
581 | #ifndef ECB_MEMORY_FENCE |
|
|
582 | #if !ECB_AVOID_PTHREADS |
|
|
583 | /* |
|
|
584 | * if you get undefined symbol references to pthread_mutex_lock, |
|
|
585 | * or failure to find pthread.h, then you should implement |
|
|
586 | * the ECB_MEMORY_FENCE operations for your cpu/compiler |
|
|
587 | * OR provide pthread.h and link against the posix thread library |
|
|
588 | * of your system. |
|
|
589 | */ |
|
|
590 | #include <pthread.h> |
|
|
591 | #define ECB_NEEDS_PTHREADS 1 |
|
|
592 | #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 |
|
|
593 | |
|
|
594 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
|
|
595 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
|
|
596 | #endif |
|
|
597 | #endif |
|
|
598 | |
|
|
599 | #if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) |
|
|
600 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
601 | #endif |
|
|
602 | |
|
|
603 | #if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) |
|
|
604 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
605 | #endif |
|
|
606 | |
|
|
607 | /*****************************************************************************/ |
|
|
608 | |
|
|
609 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
|
|
610 | |
487 | #if __cplusplus |
611 | #if __cplusplus |
488 | #define ecb_inline static inline |
612 | #define ecb_inline static inline |
489 | #elif ECB_GCC_VERSION(2,5) |
613 | #elif ECB_GCC_VERSION(2,5) |
490 | #define ecb_inline static __inline__ |
614 | #define ecb_inline static __inline__ |
491 | #elif ECB_C99 |
615 | #elif ECB_C99 |
492 | #define ecb_inline static inline |
616 | #define ecb_inline static inline |
493 | #else |
617 | #else |
494 | #define ecb_inline static |
618 | #define ecb_inline static |
495 | #endif |
619 | #endif |
496 | |
620 | |
497 | #ifndef ECB_MEMORY_FENCE |
|
|
498 | #if ECB_GCC_VERSION(2,5) |
621 | #if ECB_GCC_VERSION(3,3) |
499 | #if __x86 |
622 | #define ecb_restrict __restrict__ |
500 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
623 | #elif ECB_C99 |
501 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
624 | #define ecb_restrict restrict |
502 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */ |
625 | #else |
503 | #elif __amd64 |
626 | #define ecb_restrict |
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 |
|
|
508 | #endif |
627 | #endif |
509 | #endif |
|
|
510 | |
628 | |
511 | #ifndef ECB_MEMORY_FENCE |
629 | typedef int ecb_bool; |
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 | |
630 | |
527 | #ifndef ECB_MEMORY_FENCE |
631 | #define ECB_CONCAT_(a, b) a ## b |
528 | #include <pthread.h> |
632 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
|
|
633 | #define ECB_STRINGIFY_(a) # a |
|
|
634 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
529 | |
635 | |
530 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
636 | #define ecb_function_ ecb_inline |
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 | |
637 | |
536 | #if ECB_GCC_VERSION(3,1) |
638 | #if ECB_GCC_VERSION(3,1) |
537 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
639 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
538 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
640 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
539 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
641 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
… | |
… | |
543 | #define ecb_is_constant(expr) 0 |
645 | #define ecb_is_constant(expr) 0 |
544 | #define ecb_expect(expr,value) (expr) |
646 | #define ecb_expect(expr,value) (expr) |
545 | #define ecb_prefetch(addr,rw,locality) |
647 | #define ecb_prefetch(addr,rw,locality) |
546 | #endif |
648 | #endif |
547 | |
649 | |
|
|
650 | /* no emulation for ecb_decltype */ |
|
|
651 | #if ECB_GCC_VERSION(4,5) |
|
|
652 | #define ecb_decltype(x) __decltype(x) |
|
|
653 | #elif ECB_GCC_VERSION(3,0) |
|
|
654 | #define ecb_decltype(x) __typeof(x) |
|
|
655 | #endif |
|
|
656 | |
548 | #define ecb_noinline ecb_attribute ((__noinline__)) |
657 | #define ecb_noinline ecb_attribute ((__noinline__)) |
549 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
658 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
550 | #define ecb_unused ecb_attribute ((__unused__)) |
659 | #define ecb_unused ecb_attribute ((__unused__)) |
551 | #define ecb_const ecb_attribute ((__const__)) |
660 | #define ecb_const ecb_attribute ((__const__)) |
552 | #define ecb_pure ecb_attribute ((__pure__)) |
661 | #define ecb_pure ecb_attribute ((__pure__)) |
… | |
… | |
564 | /* put around conditional expressions if you are very sure that the */ |
673 | /* put around conditional expressions if you are very sure that the */ |
565 | /* expression is mostly true or mostly false. note that these return */ |
674 | /* expression is mostly true or mostly false. note that these return */ |
566 | /* booleans, not the expression. */ |
675 | /* booleans, not the expression. */ |
567 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
676 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
568 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
677 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
569 | /* ecb.h end */ |
678 | /* for compatibility to the rest of the world */ |
|
|
679 | #define ecb_likely(expr) ecb_expect_true (expr) |
|
|
680 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
|
|
681 | |
|
|
682 | /* count trailing zero bits and count # of one bits */ |
|
|
683 | #if ECB_GCC_VERSION(3,4) |
|
|
684 | /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ |
|
|
685 | #define ecb_ld32(x) (__builtin_clz (x) ^ 31) |
|
|
686 | #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) |
|
|
687 | #define ecb_ctz32(x) __builtin_ctz (x) |
|
|
688 | #define ecb_ctz64(x) __builtin_ctzll (x) |
|
|
689 | #define ecb_popcount32(x) __builtin_popcount (x) |
|
|
690 | /* no popcountll */ |
|
|
691 | #else |
|
|
692 | ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const; |
|
|
693 | ecb_function_ int |
|
|
694 | ecb_ctz32 (uint32_t x) |
|
|
695 | { |
|
|
696 | int r = 0; |
|
|
697 | |
|
|
698 | x &= ~x + 1; /* this isolates the lowest bit */ |
|
|
699 | |
|
|
700 | #if ECB_branchless_on_i386 |
|
|
701 | r += !!(x & 0xaaaaaaaa) << 0; |
|
|
702 | r += !!(x & 0xcccccccc) << 1; |
|
|
703 | r += !!(x & 0xf0f0f0f0) << 2; |
|
|
704 | r += !!(x & 0xff00ff00) << 3; |
|
|
705 | r += !!(x & 0xffff0000) << 4; |
|
|
706 | #else |
|
|
707 | if (x & 0xaaaaaaaa) r += 1; |
|
|
708 | if (x & 0xcccccccc) r += 2; |
|
|
709 | if (x & 0xf0f0f0f0) r += 4; |
|
|
710 | if (x & 0xff00ff00) r += 8; |
|
|
711 | if (x & 0xffff0000) r += 16; |
|
|
712 | #endif |
|
|
713 | |
|
|
714 | return r; |
|
|
715 | } |
|
|
716 | |
|
|
717 | ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const; |
|
|
718 | ecb_function_ int |
|
|
719 | ecb_ctz64 (uint64_t x) |
|
|
720 | { |
|
|
721 | int shift = x & 0xffffffffU ? 0 : 32; |
|
|
722 | return ecb_ctz32 (x >> shift) + shift; |
|
|
723 | } |
|
|
724 | |
|
|
725 | ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const; |
|
|
726 | ecb_function_ int |
|
|
727 | ecb_popcount32 (uint32_t x) |
|
|
728 | { |
|
|
729 | x -= (x >> 1) & 0x55555555; |
|
|
730 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
|
|
731 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
|
|
732 | x *= 0x01010101; |
|
|
733 | |
|
|
734 | return x >> 24; |
|
|
735 | } |
|
|
736 | |
|
|
737 | ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; |
|
|
738 | ecb_function_ int ecb_ld32 (uint32_t x) |
|
|
739 | { |
|
|
740 | int r = 0; |
|
|
741 | |
|
|
742 | if (x >> 16) { x >>= 16; r += 16; } |
|
|
743 | if (x >> 8) { x >>= 8; r += 8; } |
|
|
744 | if (x >> 4) { x >>= 4; r += 4; } |
|
|
745 | if (x >> 2) { x >>= 2; r += 2; } |
|
|
746 | if (x >> 1) { r += 1; } |
|
|
747 | |
|
|
748 | return r; |
|
|
749 | } |
|
|
750 | |
|
|
751 | ecb_function_ int ecb_ld64 (uint64_t x) ecb_const; |
|
|
752 | ecb_function_ int ecb_ld64 (uint64_t x) |
|
|
753 | { |
|
|
754 | int r = 0; |
|
|
755 | |
|
|
756 | if (x >> 32) { x >>= 32; r += 32; } |
|
|
757 | |
|
|
758 | return r + ecb_ld32 (x); |
|
|
759 | } |
|
|
760 | #endif |
|
|
761 | |
|
|
762 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
|
|
763 | /* so for this version we are lazy */ |
|
|
764 | ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; |
|
|
765 | ecb_function_ int |
|
|
766 | ecb_popcount64 (uint64_t x) |
|
|
767 | { |
|
|
768 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
|
|
769 | } |
|
|
770 | |
|
|
771 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const; |
|
|
772 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const; |
|
|
773 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const; |
|
|
774 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const; |
|
|
775 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
|
|
776 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
|
|
777 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const; |
|
|
778 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const; |
|
|
779 | |
|
|
780 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } |
|
|
781 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } |
|
|
782 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } |
|
|
783 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } |
|
|
784 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
|
|
785 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
|
|
786 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
|
|
787 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
|
|
788 | |
|
|
789 | #if ECB_GCC_VERSION(4,3) |
|
|
790 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
|
|
791 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
|
|
792 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
|
|
793 | #else |
|
|
794 | ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; |
|
|
795 | ecb_function_ uint16_t |
|
|
796 | ecb_bswap16 (uint16_t x) |
|
|
797 | { |
|
|
798 | return ecb_rotl16 (x, 8); |
|
|
799 | } |
|
|
800 | |
|
|
801 | ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
|
|
802 | ecb_function_ uint32_t |
|
|
803 | ecb_bswap32 (uint32_t x) |
|
|
804 | { |
|
|
805 | return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); |
|
|
806 | } |
|
|
807 | |
|
|
808 | ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const; |
|
|
809 | ecb_function_ uint64_t |
|
|
810 | ecb_bswap64 (uint64_t x) |
|
|
811 | { |
|
|
812 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
|
|
813 | } |
|
|
814 | #endif |
|
|
815 | |
|
|
816 | #if ECB_GCC_VERSION(4,5) |
|
|
817 | #define ecb_unreachable() __builtin_unreachable () |
|
|
818 | #else |
|
|
819 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
|
|
820 | ecb_function_ void ecb_unreachable (void) ecb_noreturn; |
|
|
821 | ecb_function_ void ecb_unreachable (void) { } |
|
|
822 | #endif |
|
|
823 | |
|
|
824 | /* try to tell the compiler that some condition is definitely true */ |
|
|
825 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
|
|
826 | |
|
|
827 | ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; |
|
|
828 | ecb_function_ unsigned char |
|
|
829 | ecb_byteorder_helper (void) |
|
|
830 | { |
|
|
831 | const uint32_t u = 0x11223344; |
|
|
832 | return *(unsigned char *)&u; |
|
|
833 | } |
|
|
834 | |
|
|
835 | ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; |
|
|
836 | ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
|
|
837 | ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; |
|
|
838 | ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
|
|
839 | |
|
|
840 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
|
|
841 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
|
|
842 | #else |
|
|
843 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
|
|
844 | #endif |
|
|
845 | |
|
|
846 | #if __cplusplus |
|
|
847 | template<typename T> |
|
|
848 | static inline T ecb_div_rd (T val, T div) |
|
|
849 | { |
|
|
850 | return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; |
|
|
851 | } |
|
|
852 | template<typename T> |
|
|
853 | static inline T ecb_div_ru (T val, T div) |
|
|
854 | { |
|
|
855 | return val < 0 ? - ((-val ) / div) : (val + div - 1) / div; |
|
|
856 | } |
|
|
857 | #else |
|
|
858 | #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) |
|
|
859 | #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) |
|
|
860 | #endif |
|
|
861 | |
|
|
862 | #if ecb_cplusplus_does_not_suck |
|
|
863 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
|
|
864 | template<typename T, int N> |
|
|
865 | static inline int ecb_array_length (const T (&arr)[N]) |
|
|
866 | { |
|
|
867 | return N; |
|
|
868 | } |
|
|
869 | #else |
|
|
870 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
|
|
871 | #endif |
|
|
872 | |
|
|
873 | #endif |
|
|
874 | |
|
|
875 | /* ECB.H END */ |
|
|
876 | |
|
|
877 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
|
|
878 | /* if your architecture doesn't need memory fences, e.g. because it is |
|
|
879 | * single-cpu/core, or if you use libev in a project that doesn't use libev |
|
|
880 | * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling |
|
|
881 | * libev, in which casess the memory fences become nops. |
|
|
882 | * alternatively, you can remove this #error and link against libpthread, |
|
|
883 | * which will then provide the memory fences. |
|
|
884 | */ |
|
|
885 | # error "memory fences not defined for your architecture, please report" |
|
|
886 | #endif |
|
|
887 | |
|
|
888 | #ifndef ECB_MEMORY_FENCE |
|
|
889 | # define ECB_MEMORY_FENCE do { } while (0) |
|
|
890 | # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
891 | # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
892 | #endif |
570 | |
893 | |
571 | #define expect_false(cond) ecb_expect_false (cond) |
894 | #define expect_false(cond) ecb_expect_false (cond) |
572 | #define expect_true(cond) ecb_expect_true (cond) |
895 | #define expect_true(cond) ecb_expect_true (cond) |
573 | #define noinline ecb_noinline |
896 | #define noinline ecb_noinline |
574 | |
897 | |
… | |
… | |
978 | |
1301 | |
979 | do |
1302 | do |
980 | ncur <<= 1; |
1303 | ncur <<= 1; |
981 | while (cnt > ncur); |
1304 | while (cnt > ncur); |
982 | |
1305 | |
983 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ |
1306 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */ |
984 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
1307 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
985 | { |
1308 | { |
986 | ncur *= elem; |
1309 | ncur *= elem; |
987 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
1310 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
988 | ncur = ncur - sizeof (void *) * 4; |
1311 | ncur = ncur - sizeof (void *) * 4; |
… | |
… | |
1474 | |
1797 | |
1475 | if (pipe_write_wanted) |
1798 | if (pipe_write_wanted) |
1476 | { |
1799 | { |
1477 | int old_errno; |
1800 | int old_errno; |
1478 | |
1801 | |
1479 | pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ |
1802 | pipe_write_skipped = 0; /* just an optimisation, no fence needed */ |
1480 | |
1803 | |
1481 | old_errno = errno; /* save errno because write will clobber it */ |
1804 | old_errno = errno; /* save errno because write will clobber it */ |
1482 | |
1805 | |
1483 | #if EV_USE_EVENTFD |
1806 | #if EV_USE_EVENTFD |
1484 | if (evfd >= 0) |
1807 | if (evfd >= 0) |
… | |
… | |
2616 | time_update (EV_A_ 1e100); |
2939 | time_update (EV_A_ 1e100); |
2617 | |
2940 | |
2618 | /* from now on, we want a pipe-wake-up */ |
2941 | /* from now on, we want a pipe-wake-up */ |
2619 | pipe_write_wanted = 1; |
2942 | pipe_write_wanted = 1; |
2620 | |
2943 | |
2621 | ECB_MEMORY_FENCE; /* amke sure pipe_write_wanted is visible before we check for potential skips */ |
2944 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
2622 | |
2945 | |
2623 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2946 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2624 | { |
2947 | { |
2625 | waittime = MAX_BLOCKTIME; |
2948 | waittime = MAX_BLOCKTIME; |
2626 | |
2949 | |
… | |
… | |
4028 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
4351 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
4029 | #endif |
4352 | #endif |
4030 | |
4353 | |
4031 | #if EV_IDLE_ENABLE |
4354 | #if EV_IDLE_ENABLE |
4032 | if (types & EV_IDLE) |
4355 | if (types & EV_IDLE) |
4033 | for (j = NUMPRI; i--; ) |
4356 | for (j = NUMPRI; j--; ) |
4034 | for (i = idlecnt [j]; i--; ) |
4357 | for (i = idlecnt [j]; i--; ) |
4035 | cb (EV_A_ EV_IDLE, idles [j][i]); |
4358 | cb (EV_A_ EV_IDLE, idles [j][i]); |
4036 | #endif |
4359 | #endif |
4037 | |
4360 | |
4038 | #if EV_FORK_ENABLE |
4361 | #if EV_FORK_ENABLE |
… | |
… | |
4091 | |
4414 | |
4092 | #if EV_MULTIPLICITY |
4415 | #if EV_MULTIPLICITY |
4093 | #include "ev_wrap.h" |
4416 | #include "ev_wrap.h" |
4094 | #endif |
4417 | #endif |
4095 | |
4418 | |
4096 | EV_CPP(}) |
|
|
4097 | |
|
|