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

Comparing libev/ev.c (file contents):
Revision 1.384 by root, Wed Jul 20 00:58:45 2011 UTC vs.
Revision 1.418 by root, Mon Apr 2 23:14:41 2012 UTC

1/* 1/*
2 * libev event processing core, watcher management 2 * libev event processing core, watcher management
3 * 3 *
4 * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
59# endif 59# endif
60# ifndef EV_USE_MONOTONIC 60# ifndef EV_USE_MONOTONIC
61# define EV_USE_MONOTONIC 1 61# define EV_USE_MONOTONIC 1
62# endif 62# endif
63# endif 63# endif
64# elif !defined(EV_USE_CLOCK_SYSCALL) 64# elif !defined EV_USE_CLOCK_SYSCALL
65# define EV_USE_CLOCK_SYSCALL 0 65# define EV_USE_CLOCK_SYSCALL 0
66# endif 66# endif
67 67
68# if HAVE_CLOCK_GETTIME 68# if HAVE_CLOCK_GETTIME
69# ifndef EV_USE_MONOTONIC 69# ifndef EV_USE_MONOTONIC
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
188EV_CPP(extern "C" {) 188#if EV_NO_THREADS
189# undef EV_NO_SMP
190# define EV_NO_SMP 1
191# undef ECB_NO_THREADS
192# define ECB_NO_THREADS 1
193#endif
194#if EV_NO_SMP
195# undef EV_NO_SMP
196# define ECB_NO_SMP 1
197#endif
189 198
190#ifndef _WIN32 199#ifndef _WIN32
191# include <sys/time.h> 200# include <sys/time.h>
192# include <sys/wait.h> 201# include <sys/wait.h>
193# include <unistd.h> 202# include <unistd.h>
210#define _DARWIN_UNLIMITED_SELECT 1 219#define _DARWIN_UNLIMITED_SELECT 1
211 220
212/* this block tries to deduce configuration from header-defined symbols and defaults */ 221/* this block tries to deduce configuration from header-defined symbols and defaults */
213 222
214/* try to deduce the maximum number of signals on this platform */ 223/* try to deduce the maximum number of signals on this platform */
215#if defined (EV_NSIG) 224#if defined EV_NSIG
216/* use what's provided */ 225/* use what's provided */
217#elif defined (NSIG) 226#elif defined NSIG
218# define EV_NSIG (NSIG) 227# define EV_NSIG (NSIG)
219#elif defined(_NSIG) 228#elif defined _NSIG
220# define EV_NSIG (_NSIG) 229# define EV_NSIG (_NSIG)
221#elif defined (SIGMAX) 230#elif defined SIGMAX
222# define EV_NSIG (SIGMAX+1) 231# define EV_NSIG (SIGMAX+1)
223#elif defined (SIG_MAX) 232#elif defined SIG_MAX
224# define EV_NSIG (SIG_MAX+1) 233# define EV_NSIG (SIG_MAX+1)
225#elif defined (_SIG_MAX) 234#elif defined _SIG_MAX
226# define EV_NSIG (_SIG_MAX+1) 235# define EV_NSIG (_SIG_MAX+1)
227#elif defined (MAXSIG) 236#elif defined MAXSIG
228# define EV_NSIG (MAXSIG+1) 237# define EV_NSIG (MAXSIG+1)
229#elif defined (MAX_SIG) 238#elif defined MAX_SIG
230# define EV_NSIG (MAX_SIG+1) 239# define EV_NSIG (MAX_SIG+1)
231#elif defined (SIGARRAYSIZE) 240#elif defined SIGARRAYSIZE
232# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ 241# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
233#elif defined (_sys_nsig) 242#elif defined _sys_nsig
234# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ 243# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
235#else 244#else
236# error "unable to find value for NSIG, please report" 245# error "unable to find value for NSIG, please report"
237/* to make it compile regardless, just remove the above line, */ 246/* to make it compile regardless, just remove the above line, */
238/* but consider reporting it, too! :) */ 247/* but consider reporting it, too! :) */
250# define EV_USE_CLOCK_SYSCALL 0 259# define EV_USE_CLOCK_SYSCALL 0
251# endif 260# endif
252#endif 261#endif
253 262
254#ifndef EV_USE_MONOTONIC 263#ifndef EV_USE_MONOTONIC
255# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 264# if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
256# define EV_USE_MONOTONIC EV_FEATURE_OS 265# define EV_USE_MONOTONIC EV_FEATURE_OS
257# else 266# else
258# define EV_USE_MONOTONIC 0 267# define EV_USE_MONOTONIC 0
259# endif 268# endif
260#endif 269#endif
386# define EV_USE_INOTIFY 0 395# define EV_USE_INOTIFY 0
387#endif 396#endif
388 397
389#if !EV_USE_NANOSLEEP 398#if !EV_USE_NANOSLEEP
390/* hp-ux has it in sys/time.h, which we unconditionally include above */ 399/* hp-ux has it in sys/time.h, which we unconditionally include above */
391# if !defined(_WIN32) && !defined(__hpux) 400# if !defined _WIN32 && !defined __hpux
392# include <sys/select.h> 401# include <sys/select.h>
393# endif 402# endif
394#endif 403#endif
395 404
396#if EV_USE_INOTIFY 405#if EV_USE_INOTIFY
464#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 473#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
465 474
466#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) 475#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) 476#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
468 477
469/* the following are taken from libecb */ 478/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
470/* ecb.h start */ 479/* ECB.H BEGIN */
480/*
481 * libecb - http://software.schmorp.de/pkg/libecb
482 *
483 * Copyright (©) 2009-2012 Marc Alexander Lehmann <libecb@schmorp.de>
484 * Copyright (©) 2011 Emanuele Giaquinta
485 * All rights reserved.
486 *
487 * Redistribution and use in source and binary forms, with or without modifica-
488 * tion, are permitted provided that the following conditions are met:
489 *
490 * 1. Redistributions of source code must retain the above copyright notice,
491 * this list of conditions and the following disclaimer.
492 *
493 * 2. Redistributions in binary form must reproduce the above copyright
494 * notice, this list of conditions and the following disclaimer in the
495 * documentation and/or other materials provided with the distribution.
496 *
497 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
498 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
499 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
500 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
501 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
502 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
503 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
504 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
505 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
506 * OF THE POSSIBILITY OF SUCH DAMAGE.
507 */
508
509#ifndef ECB_H
510#define ECB_H
511
512#ifdef _WIN32
513 typedef signed char int8_t;
514 typedef unsigned char uint8_t;
515 typedef signed short int16_t;
516 typedef unsigned short uint16_t;
517 typedef signed int int32_t;
518 typedef unsigned int uint32_t;
519 #if __GNUC__
520 typedef signed long long int64_t;
521 typedef unsigned long long uint64_t;
522 #else /* _MSC_VER || __BORLANDC__ */
523 typedef signed __int64 int64_t;
524 typedef unsigned __int64 uint64_t;
525 #endif
526#else
527 #include <inttypes.h>
528#endif
471 529
472/* many compilers define _GNUC_ to some versions but then only implement 530/* many compilers define _GNUC_ to some versions but then only implement
473 * what their idiot authors think are the "more important" extensions, 531 * what their idiot authors think are the "more important" extensions,
474 * causing enourmous grief in return for some better fake benchmark numbers. 532 * causing enormous grief in return for some better fake benchmark numbers.
475 * or so. 533 * or so.
476 * we try to detect these and simply assume they are not gcc - if they have 534 * 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. 535 * an issue with that they should have done it right in the first place.
478 */ 536 */
479#ifndef ECB_GCC_VERSION 537#ifndef ECB_GCC_VERSION
480 #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__) 538 #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 539 #define ECB_GCC_VERSION(major,minor) 0
482 #else 540 #else
483 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 541 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
484 #endif 542 #endif
485#endif 543#endif
544
545/*****************************************************************************/
546
547/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
548/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
549
550#if ECB_NO_THREADS
551# define ECB_NO_SMP 1
552#endif
553
554#if ECB_NO_THREADS || ECB_NO_SMP
555 #define ECB_MEMORY_FENCE do { } while (0)
556#endif
557
558#ifndef ECB_MEMORY_FENCE
559 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
560 #if __i386 || __i386__
561 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
562 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */
563 #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */
564 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__
565 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
566 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory")
567 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */
568 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
569 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
570 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
571 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__
572 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
573 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
574 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__
575 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
576 #elif __sparc || __sparc__
577 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory")
578 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
579 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
580 #elif defined __s390__ || defined __s390x__
581 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
582 #elif defined __mips__
583 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
584 #endif
585 #endif
586#endif
587
588#ifndef ECB_MEMORY_FENCE
589 #if ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
590 #define ECB_MEMORY_FENCE __sync_synchronize ()
591 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
592 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
593 #elif _MSC_VER >= 1400 /* VC++ 2005 */
594 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
595 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
596 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
597 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
598 #elif defined _WIN32
599 #include <WinNT.h>
600 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
601 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
602 #include <mbarrier.h>
603 #define ECB_MEMORY_FENCE __machine_rw_barrier ()
604 #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier ()
605 #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier ()
606 #elif __xlC__
607 #define ECB_MEMORY_FENCE __sync ()
608 #endif
609#endif
610
611#ifndef ECB_MEMORY_FENCE
612 #if !ECB_AVOID_PTHREADS
613 /*
614 * if you get undefined symbol references to pthread_mutex_lock,
615 * or failure to find pthread.h, then you should implement
616 * the ECB_MEMORY_FENCE operations for your cpu/compiler
617 * OR provide pthread.h and link against the posix thread library
618 * of your system.
619 */
620 #include <pthread.h>
621 #define ECB_NEEDS_PTHREADS 1
622 #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1
623
624 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
625 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
626 #endif
627#endif
628
629#if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
630 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
631#endif
632
633#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
634 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
635#endif
636
637/*****************************************************************************/
638
639#define ECB_C99 (__STDC_VERSION__ >= 199901L)
486 640
487#if __cplusplus 641#if __cplusplus
488 #define ecb_inline static inline 642 #define ecb_inline static inline
489#elif ECB_GCC_VERSION(2,5) 643#elif ECB_GCC_VERSION(2,5)
490 #define ecb_inline static __inline__ 644 #define ecb_inline static __inline__
492 #define ecb_inline static inline 646 #define ecb_inline static inline
493#else 647#else
494 #define ecb_inline static 648 #define ecb_inline static
495#endif 649#endif
496 650
497#ifndef ECB_MEMORY_FENCE
498 #if ECB_GCC_VERSION(2,5) 651#if ECB_GCC_VERSION(3,3)
499 #if __x86 652 #define ecb_restrict __restrict__
500 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") 653#elif ECB_C99
501 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 654 #define ecb_restrict restrict
502 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */ 655#else
503 #elif __amd64 656 #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 657#endif
509#endif
510 658
511#ifndef ECB_MEMORY_FENCE 659typedef 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 660
527#ifndef ECB_MEMORY_FENCE 661#define ECB_CONCAT_(a, b) a ## b
528 #include <pthread.h> 662#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
663#define ECB_STRINGIFY_(a) # a
664#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
529 665
530 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; 666#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 667
536#if ECB_GCC_VERSION(3,1) 668#if ECB_GCC_VERSION(3,1)
537 #define ecb_attribute(attrlist) __attribute__(attrlist) 669 #define ecb_attribute(attrlist) __attribute__(attrlist)
538 #define ecb_is_constant(expr) __builtin_constant_p (expr) 670 #define ecb_is_constant(expr) __builtin_constant_p (expr)
539 #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) 671 #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
543 #define ecb_is_constant(expr) 0 675 #define ecb_is_constant(expr) 0
544 #define ecb_expect(expr,value) (expr) 676 #define ecb_expect(expr,value) (expr)
545 #define ecb_prefetch(addr,rw,locality) 677 #define ecb_prefetch(addr,rw,locality)
546#endif 678#endif
547 679
680/* no emulation for ecb_decltype */
681#if ECB_GCC_VERSION(4,5)
682 #define ecb_decltype(x) __decltype(x)
683#elif ECB_GCC_VERSION(3,0)
684 #define ecb_decltype(x) __typeof(x)
685#endif
686
548#define ecb_noinline ecb_attribute ((__noinline__)) 687#define ecb_noinline ecb_attribute ((__noinline__))
549#define ecb_noreturn ecb_attribute ((__noreturn__)) 688#define ecb_noreturn ecb_attribute ((__noreturn__))
550#define ecb_unused ecb_attribute ((__unused__)) 689#define ecb_unused ecb_attribute ((__unused__))
551#define ecb_const ecb_attribute ((__const__)) 690#define ecb_const ecb_attribute ((__const__))
552#define ecb_pure ecb_attribute ((__pure__)) 691#define ecb_pure ecb_attribute ((__pure__))
564/* put around conditional expressions if you are very sure that the */ 703/* put around conditional expressions if you are very sure that the */
565/* expression is mostly true or mostly false. note that these return */ 704/* expression is mostly true or mostly false. note that these return */
566/* booleans, not the expression. */ 705/* booleans, not the expression. */
567#define ecb_expect_false(expr) ecb_expect (!!(expr), 0) 706#define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
568#define ecb_expect_true(expr) ecb_expect (!!(expr), 1) 707#define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
569/* ecb.h end */ 708/* for compatibility to the rest of the world */
709#define ecb_likely(expr) ecb_expect_true (expr)
710#define ecb_unlikely(expr) ecb_expect_false (expr)
711
712/* count trailing zero bits and count # of one bits */
713#if ECB_GCC_VERSION(3,4)
714 /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */
715 #define ecb_ld32(x) (__builtin_clz (x) ^ 31)
716 #define ecb_ld64(x) (__builtin_clzll (x) ^ 63)
717 #define ecb_ctz32(x) __builtin_ctz (x)
718 #define ecb_ctz64(x) __builtin_ctzll (x)
719 #define ecb_popcount32(x) __builtin_popcount (x)
720 /* no popcountll */
721#else
722 ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const;
723 ecb_function_ int
724 ecb_ctz32 (uint32_t x)
725 {
726 int r = 0;
727
728 x &= ~x + 1; /* this isolates the lowest bit */
729
730#if ECB_branchless_on_i386
731 r += !!(x & 0xaaaaaaaa) << 0;
732 r += !!(x & 0xcccccccc) << 1;
733 r += !!(x & 0xf0f0f0f0) << 2;
734 r += !!(x & 0xff00ff00) << 3;
735 r += !!(x & 0xffff0000) << 4;
736#else
737 if (x & 0xaaaaaaaa) r += 1;
738 if (x & 0xcccccccc) r += 2;
739 if (x & 0xf0f0f0f0) r += 4;
740 if (x & 0xff00ff00) r += 8;
741 if (x & 0xffff0000) r += 16;
742#endif
743
744 return r;
745 }
746
747 ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const;
748 ecb_function_ int
749 ecb_ctz64 (uint64_t x)
750 {
751 int shift = x & 0xffffffffU ? 0 : 32;
752 return ecb_ctz32 (x >> shift) + shift;
753 }
754
755 ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const;
756 ecb_function_ int
757 ecb_popcount32 (uint32_t x)
758 {
759 x -= (x >> 1) & 0x55555555;
760 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
761 x = ((x >> 4) + x) & 0x0f0f0f0f;
762 x *= 0x01010101;
763
764 return x >> 24;
765 }
766
767 ecb_function_ int ecb_ld32 (uint32_t x) ecb_const;
768 ecb_function_ int ecb_ld32 (uint32_t x)
769 {
770 int r = 0;
771
772 if (x >> 16) { x >>= 16; r += 16; }
773 if (x >> 8) { x >>= 8; r += 8; }
774 if (x >> 4) { x >>= 4; r += 4; }
775 if (x >> 2) { x >>= 2; r += 2; }
776 if (x >> 1) { r += 1; }
777
778 return r;
779 }
780
781 ecb_function_ int ecb_ld64 (uint64_t x) ecb_const;
782 ecb_function_ int ecb_ld64 (uint64_t x)
783 {
784 int r = 0;
785
786 if (x >> 32) { x >>= 32; r += 32; }
787
788 return r + ecb_ld32 (x);
789 }
790#endif
791
792ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const;
793ecb_function_ uint8_t ecb_bitrev8 (uint8_t x)
794{
795 return ( (x * 0x0802U & 0x22110U)
796 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
797}
798
799ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) ecb_const;
800ecb_function_ uint16_t ecb_bitrev16 (uint16_t x)
801{
802 x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1);
803 x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2);
804 x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4);
805 x = ( x >> 8 ) | ( x << 8);
806
807 return x;
808}
809
810ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) ecb_const;
811ecb_function_ uint32_t ecb_bitrev32 (uint32_t x)
812{
813 x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
814 x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
815 x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
816 x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
817 x = ( x >> 16 ) | ( x << 16);
818
819 return x;
820}
821
822/* popcount64 is only available on 64 bit cpus as gcc builtin */
823/* so for this version we are lazy */
824ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const;
825ecb_function_ int
826ecb_popcount64 (uint64_t x)
827{
828 return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
829}
830
831ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const;
832ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const;
833ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const;
834ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const;
835ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const;
836ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
837ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const;
838ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const;
839
840ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); }
841ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); }
842ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); }
843ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); }
844ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
845ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
846ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
847ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
848
849#if ECB_GCC_VERSION(4,3)
850 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
851 #define ecb_bswap32(x) __builtin_bswap32 (x)
852 #define ecb_bswap64(x) __builtin_bswap64 (x)
853#else
854 ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const;
855 ecb_function_ uint16_t
856 ecb_bswap16 (uint16_t x)
857 {
858 return ecb_rotl16 (x, 8);
859 }
860
861 ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const;
862 ecb_function_ uint32_t
863 ecb_bswap32 (uint32_t x)
864 {
865 return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16);
866 }
867
868 ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const;
869 ecb_function_ uint64_t
870 ecb_bswap64 (uint64_t x)
871 {
872 return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32);
873 }
874#endif
875
876#if ECB_GCC_VERSION(4,5)
877 #define ecb_unreachable() __builtin_unreachable ()
878#else
879 /* this seems to work fine, but gcc always emits a warning for it :/ */
880 ecb_inline void ecb_unreachable (void) ecb_noreturn;
881 ecb_inline void ecb_unreachable (void) { }
882#endif
883
884/* try to tell the compiler that some condition is definitely true */
885#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
886
887ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
888ecb_inline unsigned char
889ecb_byteorder_helper (void)
890{
891 const uint32_t u = 0x11223344;
892 return *(unsigned char *)&u;
893}
894
895ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
896ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
897ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
898ecb_inline ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }
899
900#if ECB_GCC_VERSION(3,0) || ECB_C99
901 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
902#else
903 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
904#endif
905
906#if __cplusplus
907 template<typename T>
908 static inline T ecb_div_rd (T val, T div)
909 {
910 return val < 0 ? - ((-val + div - 1) / div) : (val ) / div;
911 }
912 template<typename T>
913 static inline T ecb_div_ru (T val, T div)
914 {
915 return val < 0 ? - ((-val ) / div) : (val + div - 1) / div;
916 }
917#else
918 #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div))
919 #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div))
920#endif
921
922#if ecb_cplusplus_does_not_suck
923 /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */
924 template<typename T, int N>
925 static inline int ecb_array_length (const T (&arr)[N])
926 {
927 return N;
928 }
929#else
930 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
931#endif
932
933#endif
934
935/* ECB.H END */
936
937#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
938/* if your architecture doesn't need memory fences, e.g. because it is
939 * single-cpu/core, or if you use libev in a project that doesn't use libev
940 * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling
941 * libev, in which cases the memory fences become nops.
942 * alternatively, you can remove this #error and link against libpthread,
943 * which will then provide the memory fences.
944 */
945# error "memory fences not defined for your architecture, please report"
946#endif
947
948#ifndef ECB_MEMORY_FENCE
949# define ECB_MEMORY_FENCE do { } while (0)
950# define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
951# define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
952#endif
570 953
571#define expect_false(cond) ecb_expect_false (cond) 954#define expect_false(cond) ecb_expect_false (cond)
572#define expect_true(cond) ecb_expect_true (cond) 955#define expect_true(cond) ecb_expect_true (cond)
573#define noinline ecb_noinline 956#define noinline ecb_noinline
574 957
870 #undef VAR 1253 #undef VAR
871 }; 1254 };
872 #include "ev_wrap.h" 1255 #include "ev_wrap.h"
873 1256
874 static struct ev_loop default_loop_struct; 1257 static struct ev_loop default_loop_struct;
875 struct ev_loop *ev_default_loop_ptr; 1258 EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */
876 1259
877#else 1260#else
878 1261
879 ev_tstamp ev_rt_now; 1262 EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */
880 #define VAR(name,decl) static decl; 1263 #define VAR(name,decl) static decl;
881 #include "ev_vars.h" 1264 #include "ev_vars.h"
882 #undef VAR 1265 #undef VAR
883 1266
884 static int ev_default_loop_ptr; 1267 static int ev_default_loop_ptr;
949#if EV_USE_NANOSLEEP 1332#if EV_USE_NANOSLEEP
950 struct timespec ts; 1333 struct timespec ts;
951 1334
952 EV_TS_SET (ts, delay); 1335 EV_TS_SET (ts, delay);
953 nanosleep (&ts, 0); 1336 nanosleep (&ts, 0);
954#elif defined(_WIN32) 1337#elif defined _WIN32
955 Sleep ((unsigned long)(delay * 1e3)); 1338 Sleep ((unsigned long)(delay * 1e3));
956#else 1339#else
957 struct timeval tv; 1340 struct timeval tv;
958 1341
959 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 1342 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
978 1361
979 do 1362 do
980 ncur <<= 1; 1363 ncur <<= 1;
981 while (cnt > ncur); 1364 while (cnt > ncur);
982 1365
983 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ 1366 /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */
984 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) 1367 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
985 { 1368 {
986 ncur *= elem; 1369 ncur *= elem;
987 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); 1370 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
988 ncur = ncur - sizeof (void *) * 4; 1371 ncur = ncur - sizeof (void *) * 4;
1474 1857
1475 if (pipe_write_wanted) 1858 if (pipe_write_wanted)
1476 { 1859 {
1477 int old_errno; 1860 int old_errno;
1478 1861
1479 pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ 1862 pipe_write_skipped = 0; /* just an optimisation, no fence needed */
1480 1863
1481 old_errno = errno; /* save errno because write will clobber it */ 1864 old_errno = errno; /* save errno because write will clobber it */
1482 1865
1483#if EV_USE_EVENTFD 1866#if EV_USE_EVENTFD
1484 if (evfd >= 0) 1867 if (evfd >= 0)
1492 /* win32 people keep sending patches that change this write() to send() */ 1875 /* win32 people keep sending patches that change this write() to send() */
1493 /* and then run away. but send() is wrong, it wants a socket handle on win32 */ 1876 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1494 /* so when you think this write should be a send instead, please find out */ 1877 /* so when you think this write should be a send instead, please find out */
1495 /* where your send() is from - it's definitely not the microsoft send, and */ 1878 /* where your send() is from - it's definitely not the microsoft send, and */
1496 /* tell me. thank you. */ 1879 /* tell me. thank you. */
1880 /* it might be that your problem is that your environment needs EV_USE_WSASOCKET */
1881 /* check the ev documentation on how to use this flag */
1497 write (evpipe [1], &(evpipe [1]), 1); 1882 write (evpipe [1], &(evpipe [1]), 1);
1498 } 1883 }
1499 1884
1500 errno = old_errno; 1885 errno = old_errno;
1501 } 1886 }
2545 2930
2546 mn_now = ev_rt_now; 2931 mn_now = ev_rt_now;
2547 } 2932 }
2548} 2933}
2549 2934
2550void 2935int
2551ev_run (EV_P_ int flags) 2936ev_run (EV_P_ int flags)
2552{ 2937{
2553#if EV_FEATURE_API 2938#if EV_FEATURE_API
2554 ++loop_depth; 2939 ++loop_depth;
2555#endif 2940#endif
2616 time_update (EV_A_ 1e100); 3001 time_update (EV_A_ 1e100);
2617 3002
2618 /* from now on, we want a pipe-wake-up */ 3003 /* from now on, we want a pipe-wake-up */
2619 pipe_write_wanted = 1; 3004 pipe_write_wanted = 1;
2620 3005
2621 ECB_MEMORY_FENCE; /* amke sure pipe_write_wanted is visible before we check for potential skips */ 3006 ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */
2622 3007
2623 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) 3008 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
2624 { 3009 {
2625 waittime = MAX_BLOCKTIME; 3010 waittime = MAX_BLOCKTIME;
2626 3011
2668#endif 3053#endif
2669 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ 3054 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2670 backend_poll (EV_A_ waittime); 3055 backend_poll (EV_A_ waittime);
2671 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 3056 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2672 3057
2673 pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ 3058 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
2674 3059
2675 if (pipe_write_skipped) 3060 if (pipe_write_skipped)
2676 { 3061 {
2677 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); 3062 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
2678 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3063 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
2712 loop_done = EVBREAK_CANCEL; 3097 loop_done = EVBREAK_CANCEL;
2713 3098
2714#if EV_FEATURE_API 3099#if EV_FEATURE_API
2715 --loop_depth; 3100 --loop_depth;
2716#endif 3101#endif
3102
3103 return activecnt;
2717} 3104}
2718 3105
2719void 3106void
2720ev_break (EV_P_ int how) 3107ev_break (EV_P_ int how)
2721{ 3108{
2936 3323
2937void noinline 3324void noinline
2938ev_timer_again (EV_P_ ev_timer *w) 3325ev_timer_again (EV_P_ ev_timer *w)
2939{ 3326{
2940 EV_FREQUENT_CHECK; 3327 EV_FREQUENT_CHECK;
3328
3329 clear_pending (EV_A_ (W)w);
2941 3330
2942 if (ev_is_active (w)) 3331 if (ev_is_active (w))
2943 { 3332 {
2944 if (w->repeat) 3333 if (w->repeat)
2945 { 3334 {
3354} 3743}
3355 3744
3356inline_size int 3745inline_size int
3357infy_newfd (void) 3746infy_newfd (void)
3358{ 3747{
3359#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) 3748#if defined IN_CLOEXEC && defined IN_NONBLOCK
3360 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); 3749 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3361 if (fd >= 0) 3750 if (fd >= 0)
3362 return fd; 3751 return fd;
3363#endif 3752#endif
3364 return inotify_init (); 3753 return inotify_init ();
4028 cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); 4417 cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i]));
4029#endif 4418#endif
4030 4419
4031#if EV_IDLE_ENABLE 4420#if EV_IDLE_ENABLE
4032 if (types & EV_IDLE) 4421 if (types & EV_IDLE)
4033 for (j = NUMPRI; i--; ) 4422 for (j = NUMPRI; j--; )
4034 for (i = idlecnt [j]; i--; ) 4423 for (i = idlecnt [j]; i--; )
4035 cb (EV_A_ EV_IDLE, idles [j][i]); 4424 cb (EV_A_ EV_IDLE, idles [j][i]);
4036#endif 4425#endif
4037 4426
4038#if EV_FORK_ENABLE 4427#if EV_FORK_ENABLE
4091 4480
4092#if EV_MULTIPLICITY 4481#if EV_MULTIPLICITY
4093 #include "ev_wrap.h" 4482 #include "ev_wrap.h"
4094#endif 4483#endif
4095 4484
4096EV_CPP(})
4097

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines