ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.390 by root, Thu Aug 4 11:58:02 2011 UTC vs.
Revision 1.391 by root, Thu Aug 4 13:57:16 2011 UTC

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/* the following are taken from libecb */ 469/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
470/* ecb.h start */ 470/* ECB.H BEGIN */
471/*
472 * libecb - http://software.schmorp.de/pkg/libecb
473 *
474 * Copyright (©) 2009-2011 Marc Alexander Lehmann <libecb@schmorp.de>
475 * Copyright (©) 2011 Emanuele Giaquinta
476 * All rights reserved.
477 *
478 * Redistribution and use in source and binary forms, with or without modifica-
479 * tion, are permitted provided that the following conditions are met:
480 *
481 * 1. Redistributions of source code must retain the above copyright notice,
482 * this list of conditions and the following disclaimer.
483 *
484 * 2. Redistributions in binary form must reproduce the above copyright
485 * notice, this list of conditions and the following disclaimer in the
486 * documentation and/or other materials provided with the distribution.
487 *
488 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
489 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
490 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
491 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
492 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
493 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
494 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
495 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
496 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
497 * OF THE POSSIBILITY OF SUCH DAMAGE.
498 */
499
500#ifndef ECB_H
501#define ECB_H
502
503#ifdef _WIN32
504 typedef signed char int8_t;
505 typedef unsigned char uint8_t;
506 typedef signed short int16_t;
507 typedef unsigned short uint16_t;
508 typedef signed int int32_t;
509 typedef unsigned int uint32_t;
510 #if __GNUC__
511 typedef signed long long int64_t;
512 typedef unsigned long long uint64_t;
513 #else /* _MSC_VER || __BORLANDC__ */
514 typedef signed __int64 int64_t;
515 typedef unsigned __int64 uint64_t;
516 #endif
517#else
518 #include <inttypes.h>
519#endif
471 520
472/* many compilers define _GNUC_ to some versions but then only implement 521/* many compilers define _GNUC_ to some versions but then only implement
473 * what their idiot authors think are the "more important" extensions, 522 * what their idiot authors think are the "more important" extensions,
474 * causing enourmous grief in return for some better fake benchmark numbers. 523 * causing enormous grief in return for some better fake benchmark numbers.
475 * or so. 524 * or so.
476 * we try to detect these and simply assume they are not gcc - if they have 525 * 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. 526 * an issue with that they should have done it right in the first place.
478 */ 527 */
479#ifndef ECB_GCC_VERSION 528#ifndef ECB_GCC_VERSION
482 #else 531 #else
483 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 532 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
484 #endif 533 #endif
485#endif 534#endif
486 535
487#if __cplusplus 536/*****************************************************************************/
488 #define ecb_inline static inline 537
489#elif ECB_GCC_VERSION(2,5) 538/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
490 #define ecb_inline static __inline__ 539/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
491#elif ECB_C99 540
492 #define ecb_inline static inline 541#if ECB_NO_THREADS || ECB_NO_SMP
493#else 542 #define ECB_MEMORY_FENCE do { } while (0)
494 #define ecb_inline static 543 #define ECB_MEMORY_FENCE_ACQUIRE do { } while (0)
544 #define ECB_MEMORY_FENCE_RELEASE do { } while (0)
495#endif 545#endif
496 546
497#ifndef ECB_MEMORY_FENCE 547#ifndef ECB_MEMORY_FENCE
498 #if ECB_GCC_VERSION(2,5) 548 #if ECB_GCC_VERSION(2,5)
499 #if __x86 549 #if __x86
500 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") 550 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
501 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 551 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */
502 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */ 552 #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */
503 #elif __amd64 553 #elif __amd64
504 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") 554 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
505 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") 555 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory")
506 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") 556 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */
507 #endif 557 #endif
508 #endif 558 #endif
509#endif 559#endif
510 560
511#ifndef ECB_MEMORY_FENCE 561#ifndef ECB_MEMORY_FENCE
518 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 568 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
519 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ 569 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
520 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 570 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
521 #elif defined(_WIN32) 571 #elif defined(_WIN32)
522 #include <WinNT.h> 572 #include <WinNT.h>
523 #define ECB_MEMORY_FENCE MemoryBarrier () 573 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
524 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 574 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
525 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 575 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
526 #endif 576 #endif
527#endif 577#endif
528 578
529#ifndef ECB_MEMORY_FENCE 579#ifndef ECB_MEMORY_FENCE
580 /*
581 * if you get undefined symbol references to pthread_mutex_lock,
582 * or failure to find pthread.h, then you should implement
583 * the ECB_MEMORY_FENCE operations for your cpu/compiler
584 * OR provide pthread.h and link against the posix thread library
585 * of your system.
586 */
530 #include <pthread.h> 587 #include <pthread.h>
588 #define ECB_NEEDS_PTHREADS 1
589 #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1
531 590
532 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; 591 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) 592 #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 593 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
535 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 594 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
536#endif 595#endif
596
597/*****************************************************************************/
598
599#define ECB_C99 (__STDC_VERSION__ >= 199901L)
600
601#if __cplusplus
602 #define ecb_inline static inline
603#elif ECB_GCC_VERSION(2,5)
604 #define ecb_inline static __inline__
605#elif ECB_C99
606 #define ecb_inline static inline
607#else
608 #define ecb_inline static
609#endif
610
611#if ECB_GCC_VERSION(3,3)
612 #define ecb_restrict __restrict__
613#elif ECB_C99
614 #define ecb_restrict restrict
615#else
616 #define ecb_restrict
617#endif
618
619typedef int ecb_bool;
620
621#define ECB_CONCAT_(a, b) a ## b
622#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
623#define ECB_STRINGIFY_(a) # a
624#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
625
626#define ecb_function_ ecb_inline
537 627
538#if ECB_GCC_VERSION(3,1) 628#if ECB_GCC_VERSION(3,1)
539 #define ecb_attribute(attrlist) __attribute__(attrlist) 629 #define ecb_attribute(attrlist) __attribute__(attrlist)
540 #define ecb_is_constant(expr) __builtin_constant_p (expr) 630 #define ecb_is_constant(expr) __builtin_constant_p (expr)
541 #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) 631 #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
545 #define ecb_is_constant(expr) 0 635 #define ecb_is_constant(expr) 0
546 #define ecb_expect(expr,value) (expr) 636 #define ecb_expect(expr,value) (expr)
547 #define ecb_prefetch(addr,rw,locality) 637 #define ecb_prefetch(addr,rw,locality)
548#endif 638#endif
549 639
640/* no emulation for ecb_decltype */
641#if ECB_GCC_VERSION(4,5)
642 #define ecb_decltype(x) __decltype(x)
643#elif ECB_GCC_VERSION(3,0)
644 #define ecb_decltype(x) __typeof(x)
645#endif
646
550#define ecb_noinline ecb_attribute ((__noinline__)) 647#define ecb_noinline ecb_attribute ((__noinline__))
551#define ecb_noreturn ecb_attribute ((__noreturn__)) 648#define ecb_noreturn ecb_attribute ((__noreturn__))
552#define ecb_unused ecb_attribute ((__unused__)) 649#define ecb_unused ecb_attribute ((__unused__))
553#define ecb_const ecb_attribute ((__const__)) 650#define ecb_const ecb_attribute ((__const__))
554#define ecb_pure ecb_attribute ((__pure__)) 651#define ecb_pure ecb_attribute ((__pure__))
566/* put around conditional expressions if you are very sure that the */ 663/* put around conditional expressions if you are very sure that the */
567/* expression is mostly true or mostly false. note that these return */ 664/* expression is mostly true or mostly false. note that these return */
568/* booleans, not the expression. */ 665/* booleans, not the expression. */
569#define ecb_expect_false(expr) ecb_expect (!!(expr), 0) 666#define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
570#define ecb_expect_true(expr) ecb_expect (!!(expr), 1) 667#define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
571/* ecb.h end */ 668/* for compatibility to the rest of the world */
669#define ecb_likely(expr) ecb_expect_true (expr)
670#define ecb_unlikely(expr) ecb_expect_false (expr)
671
672/* count trailing zero bits and count # of one bits */
673#if ECB_GCC_VERSION(3,4)
674 /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */
675 #define ecb_ld32(x) (__builtin_clz (x) ^ 31)
676 #define ecb_ld64(x) (__builtin_clzll (x) ^ 63)
677 #define ecb_ctz32(x) __builtin_ctz (x)
678 #define ecb_ctz64(x) __builtin_ctzll (x)
679 #define ecb_popcount32(x) __builtin_popcount (x)
680 /* no popcountll */
681#else
682 ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const;
683 ecb_function_ int
684 ecb_ctz32 (uint32_t x)
685 {
686 int r = 0;
687
688 x &= ~x + 1; /* this isolates the lowest bit */
689
690#if ECB_branchless_on_i386
691 r += !!(x & 0xaaaaaaaa) << 0;
692 r += !!(x & 0xcccccccc) << 1;
693 r += !!(x & 0xf0f0f0f0) << 2;
694 r += !!(x & 0xff00ff00) << 3;
695 r += !!(x & 0xffff0000) << 4;
696#else
697 if (x & 0xaaaaaaaa) r += 1;
698 if (x & 0xcccccccc) r += 2;
699 if (x & 0xf0f0f0f0) r += 4;
700 if (x & 0xff00ff00) r += 8;
701 if (x & 0xffff0000) r += 16;
702#endif
703
704 return r;
705 }
706
707 ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const;
708 ecb_function_ int
709 ecb_ctz64 (uint64_t x)
710 {
711 int shift = x & 0xffffffffU ? 0 : 32;
712 return ecb_ctz32 (x >> shift) + shift;
713 }
714
715 ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const;
716 ecb_function_ int
717 ecb_popcount32 (uint32_t x)
718 {
719 x -= (x >> 1) & 0x55555555;
720 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
721 x = ((x >> 4) + x) & 0x0f0f0f0f;
722 x *= 0x01010101;
723
724 return x >> 24;
725 }
726
727 ecb_function_ int ecb_ld32 (uint32_t x) ecb_const;
728 ecb_function_ int ecb_ld32 (uint32_t x)
729 {
730 int r = 0;
731
732 if (x >> 16) { x >>= 16; r += 16; }
733 if (x >> 8) { x >>= 8; r += 8; }
734 if (x >> 4) { x >>= 4; r += 4; }
735 if (x >> 2) { x >>= 2; r += 2; }
736 if (x >> 1) { r += 1; }
737
738 return r;
739 }
740
741 ecb_function_ int ecb_ld64 (uint64_t x) ecb_const;
742 ecb_function_ int ecb_ld64 (uint64_t x)
743 {
744 int r = 0;
745
746 if (x >> 32) { x >>= 32; r += 32; }
747
748 return r + ecb_ld32 (x);
749 }
750#endif
751
752/* popcount64 is only available on 64 bit cpus as gcc builtin */
753/* so for this version we are lazy */
754ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const;
755ecb_function_ int
756ecb_popcount64 (uint64_t x)
757{
758 return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
759}
760
761ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const;
762ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const;
763ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const;
764ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const;
765ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const;
766ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
767ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const;
768ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const;
769
770ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); }
771ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); }
772ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); }
773ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); }
774ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
775ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
776ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
777ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
778
779#if ECB_GCC_VERSION(4,3)
780 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
781 #define ecb_bswap32(x) __builtin_bswap32 (x)
782 #define ecb_bswap64(x) __builtin_bswap64 (x)
783#else
784 ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const;
785 ecb_function_ uint16_t
786 ecb_bswap16 (uint16_t x)
787 {
788 return ecb_rotl16 (x, 8);
789 }
790
791 ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const;
792 ecb_function_ uint32_t
793 ecb_bswap32 (uint32_t x)
794 {
795 return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16);
796 }
797
798 ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const;
799 ecb_function_ uint64_t
800 ecb_bswap64 (uint64_t x)
801 {
802 return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32);
803 }
804#endif
805
806#if ECB_GCC_VERSION(4,5)
807 #define ecb_unreachable() __builtin_unreachable ()
808#else
809 /* this seems to work fine, but gcc always emits a warning for it :/ */
810 ecb_function_ void ecb_unreachable (void) ecb_noreturn;
811 ecb_function_ void ecb_unreachable (void) { }
812#endif
813
814/* try to tell the compiler that some condition is definitely true */
815#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
816
817ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const;
818ecb_function_ unsigned char
819ecb_byteorder_helper (void)
820{
821 const uint32_t u = 0x11223344;
822 return *(unsigned char *)&u;
823}
824
825ecb_function_ ecb_bool ecb_big_endian (void) ecb_const;
826ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
827ecb_function_ ecb_bool ecb_little_endian (void) ecb_const;
828ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }
829
830#if ECB_GCC_VERSION(3,0) || ECB_C99
831 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
832#else
833 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
834#endif
835
836#if ecb_cplusplus_does_not_suck
837 /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */
838 template<typename T, int N>
839 static inline int ecb_array_length (const T (&arr)[N])
840 {
841 return N;
842 }
843#else
844 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
845#endif
846
847#endif
848
849/* ECB.H END */
572 850
573#define expect_false(cond) ecb_expect_false (cond) 851#define expect_false(cond) ecb_expect_false (cond)
574#define expect_true(cond) ecb_expect_true (cond) 852#define expect_true(cond) ecb_expect_true (cond)
575#define noinline ecb_noinline 853#define noinline ecb_noinline
576 854

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines