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

Comparing libev/ev.c (file contents):
Revision 1.392 by root, Thu Aug 4 14:37:49 2011 UTC vs.
Revision 1.424 by root, Tue May 1 22:01:40 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
350#endif 359#endif
351 360
352/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ 361/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
353/* which makes programs even slower. might work on other unices, too. */ 362/* which makes programs even slower. might work on other unices, too. */
354#if EV_USE_CLOCK_SYSCALL 363#if EV_USE_CLOCK_SYSCALL
355# include <syscall.h> 364# include <sys/syscall.h>
356# ifdef SYS_clock_gettime 365# ifdef SYS_clock_gettime
357# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 366# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
358# undef EV_USE_MONOTONIC 367# undef EV_USE_MONOTONIC
359# define EV_USE_MONOTONIC 1 368# define EV_USE_MONOTONIC 1
360# else 369# else
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
469/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ 478/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
470/* ECB.H BEGIN */ 479/* ECB.H BEGIN */
471/* 480/*
472 * libecb - http://software.schmorp.de/pkg/libecb 481 * libecb - http://software.schmorp.de/pkg/libecb
473 * 482 *
474 * Copyright (©) 2009-2011 Marc Alexander Lehmann <libecb@schmorp.de> 483 * Copyright (©) 2009-2012 Marc Alexander Lehmann <libecb@schmorp.de>
475 * Copyright (©) 2011 Emanuele Giaquinta 484 * Copyright (©) 2011 Emanuele Giaquinta
476 * All rights reserved. 485 * All rights reserved.
477 * 486 *
478 * Redistribution and use in source and binary forms, with or without modifica- 487 * Redistribution and use in source and binary forms, with or without modifica-
479 * tion, are permitted provided that the following conditions are met: 488 * tion, are permitted provided that the following conditions are met:
524 * or so. 533 * or so.
525 * 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
526 * 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.
527 */ 536 */
528#ifndef ECB_GCC_VERSION 537#ifndef ECB_GCC_VERSION
529 #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__
530 #define ECB_GCC_VERSION(major,minor) 0 539 #define ECB_GCC_VERSION(major,minor) 0
531 #else 540 #else
532 #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)))
533 #endif 542 #endif
534#endif 543#endif
536/*****************************************************************************/ 545/*****************************************************************************/
537 546
538/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ 547/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
539/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ 548/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
540 549
550#if ECB_NO_THREADS
551# define ECB_NO_SMP 1
552#endif
553
541#if ECB_NO_THREADS || ECB_NO_SMP 554#if ECB_NO_THREADS || ECB_NO_SMP
542 #define ECB_MEMORY_FENCE do { } while (0) 555 #define ECB_MEMORY_FENCE do { } while (0)
543 #define ECB_MEMORY_FENCE_ACQUIRE do { } while (0)
544 #define ECB_MEMORY_FENCE_RELEASE do { } while (0)
545#endif 556#endif
546 557
547#ifndef ECB_MEMORY_FENCE 558#ifndef ECB_MEMORY_FENCE
548 #if ECB_GCC_VERSION(2,5) 559 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
549 #if __x86 560 #if __i386 || __i386__
550 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") 561 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
551 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ 562 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */
552 #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ 563 #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */
553 #elif __amd64 564 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__
554 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") 565 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
555 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") 566 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory")
556 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ 567 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */
557 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ 568 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
558 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 569 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
559 #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ 570 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
560 || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) \ 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")
561 || defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ 573 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
562 || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) 574 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__
563 #define ECB_MEMORY_FENCE \ 575 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
564 do { \ 576 #elif __sparc || __sparc__
565 int null = 0; \ 577 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory")
566 __asm__ __volatile__ ("mcr p15,0,%0,c6,c10,5", : "=&r" (null) : : "memory"); \ 578 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
567 while (0) 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 #elif defined __alpha__
585 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
568 #endif 586 #endif
569 #endif 587 #endif
570#endif 588#endif
571 589
572#ifndef ECB_MEMORY_FENCE 590#ifndef ECB_MEMORY_FENCE
573 #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) 591 #if ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
574 #define ECB_MEMORY_FENCE __sync_synchronize () 592 #define ECB_MEMORY_FENCE __sync_synchronize ()
575 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ 593 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
576 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ 594 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
577 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 595 #elif _MSC_VER >= 1400 /* VC++ 2005 */
578 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 596 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
579 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 597 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
580 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ 598 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
581 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 599 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
582 #elif defined(_WIN32) 600 #elif defined _WIN32
583 #include <WinNT.h> 601 #include <WinNT.h>
584 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ 602 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
603 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
604 #include <mbarrier.h>
605 #define ECB_MEMORY_FENCE __machine_rw_barrier ()
606 #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier ()
607 #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier ()
608 #elif __xlC__
609 #define ECB_MEMORY_FENCE __sync ()
585 #endif 610 #endif
586#endif 611#endif
587 612
588#ifndef ECB_MEMORY_FENCE 613#ifndef ECB_MEMORY_FENCE
589 #if !ECB_AVOID_PTHREADS 614 #if !ECB_AVOID_PTHREADS
601 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; 626 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
602 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) 627 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
603 #endif 628 #endif
604#endif 629#endif
605 630
606#if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) 631#if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
607 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 632 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
608#endif 633#endif
609 634
610#if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) 635#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
611 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 636 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
612#endif 637#endif
613 638
614/*****************************************************************************/ 639/*****************************************************************************/
615 640
764 789
765 return r + ecb_ld32 (x); 790 return r + ecb_ld32 (x);
766 } 791 }
767#endif 792#endif
768 793
794ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const;
795ecb_function_ uint8_t ecb_bitrev8 (uint8_t x)
796{
797 return ( (x * 0x0802U & 0x22110U)
798 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
799}
800
801ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) ecb_const;
802ecb_function_ uint16_t ecb_bitrev16 (uint16_t x)
803{
804 x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1);
805 x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2);
806 x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4);
807 x = ( x >> 8 ) | ( x << 8);
808
809 return x;
810}
811
812ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) ecb_const;
813ecb_function_ uint32_t ecb_bitrev32 (uint32_t x)
814{
815 x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
816 x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
817 x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
818 x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
819 x = ( x >> 16 ) | ( x << 16);
820
821 return x;
822}
823
769/* popcount64 is only available on 64 bit cpus as gcc builtin */ 824/* popcount64 is only available on 64 bit cpus as gcc builtin */
770/* so for this version we are lazy */ 825/* so for this version we are lazy */
771ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; 826ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const;
772ecb_function_ int 827ecb_function_ int
773ecb_popcount64 (uint64_t x) 828ecb_popcount64 (uint64_t x)
822 877
823#if ECB_GCC_VERSION(4,5) 878#if ECB_GCC_VERSION(4,5)
824 #define ecb_unreachable() __builtin_unreachable () 879 #define ecb_unreachable() __builtin_unreachable ()
825#else 880#else
826 /* this seems to work fine, but gcc always emits a warning for it :/ */ 881 /* this seems to work fine, but gcc always emits a warning for it :/ */
827 ecb_function_ void ecb_unreachable (void) ecb_noreturn; 882 ecb_inline void ecb_unreachable (void) ecb_noreturn;
828 ecb_function_ void ecb_unreachable (void) { } 883 ecb_inline void ecb_unreachable (void) { }
829#endif 884#endif
830 885
831/* try to tell the compiler that some condition is definitely true */ 886/* try to tell the compiler that some condition is definitely true */
832#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 887#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
833 888
834ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; 889ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
835ecb_function_ unsigned char 890ecb_inline unsigned char
836ecb_byteorder_helper (void) 891ecb_byteorder_helper (void)
837{ 892{
838 const uint32_t u = 0x11223344; 893 const uint32_t u = 0x11223344;
839 return *(unsigned char *)&u; 894 return *(unsigned char *)&u;
840} 895}
841 896
842ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; 897ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
843ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } 898ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
844ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; 899ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
845ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } 900ecb_inline ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }
846 901
847#if ECB_GCC_VERSION(3,0) || ECB_C99 902#if ECB_GCC_VERSION(3,0) || ECB_C99
848 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) 903 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
849#else 904#else
850 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) 905 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
906#endif
907
908#if __cplusplus
909 template<typename T>
910 static inline T ecb_div_rd (T val, T div)
911 {
912 return val < 0 ? - ((-val + div - 1) / div) : (val ) / div;
913 }
914 template<typename T>
915 static inline T ecb_div_ru (T val, T div)
916 {
917 return val < 0 ? - ((-val ) / div) : (val + div - 1) / div;
918 }
919#else
920 #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div))
921 #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div))
851#endif 922#endif
852 923
853#if ecb_cplusplus_does_not_suck 924#if ecb_cplusplus_does_not_suck
854 /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ 925 /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */
855 template<typename T, int N> 926 template<typename T, int N>
864#endif 935#endif
865 936
866/* ECB.H END */ 937/* ECB.H END */
867 938
868#if ECB_MEMORY_FENCE_NEEDS_PTHREADS 939#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
940/* if your architecture doesn't need memory fences, e.g. because it is
941 * single-cpu/core, or if you use libev in a project that doesn't use libev
942 * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling
943 * libev, in which cases the memory fences become nops.
944 * alternatively, you can remove this #error and link against libpthread,
945 * which will then provide the memory fences.
946 */
947# error "memory fences not defined for your architecture, please report"
948#endif
949
869# undef ECB_MEMORY_FENCE 950#ifndef ECB_MEMORY_FENCE
870# undef ECB_MEMORY_FENCE_ACQUIRE 951# define ECB_MEMORY_FENCE do { } while (0)
871# undef ECB_MEMORY_FENCE_RELEASE 952# define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
953# define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
872#endif 954#endif
873 955
874#define expect_false(cond) ecb_expect_false (cond) 956#define expect_false(cond) ecb_expect_false (cond)
875#define expect_true(cond) ecb_expect_true (cond) 957#define expect_true(cond) ecb_expect_true (cond)
876#define noinline ecb_noinline 958#define noinline ecb_noinline
1023{ 1105{
1024 write (STDERR_FILENO, msg, strlen (msg)); 1106 write (STDERR_FILENO, msg, strlen (msg));
1025} 1107}
1026#endif 1108#endif
1027 1109
1028static void (*syserr_cb)(const char *msg); 1110static void (*syserr_cb)(const char *msg) EV_THROW;
1029 1111
1030void ecb_cold 1112void ecb_cold
1031ev_set_syserr_cb (void (*cb)(const char *msg)) 1113ev_set_syserr_cb (void (*cb)(const char *msg)) EV_THROW
1032{ 1114{
1033 syserr_cb = cb; 1115 syserr_cb = cb;
1034} 1116}
1035 1117
1036static void noinline ecb_cold 1118static void noinline ecb_cold
1072 free (ptr); 1154 free (ptr);
1073 return 0; 1155 return 0;
1074#endif 1156#endif
1075} 1157}
1076 1158
1077static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 1159static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1078 1160
1079void ecb_cold 1161void ecb_cold
1080ev_set_allocator (void *(*cb)(void *ptr, long size)) 1162ev_set_allocator (void *(*cb)(void *ptr, long size)) EV_THROW
1081{ 1163{
1082 alloc = cb; 1164 alloc = cb;
1083} 1165}
1084 1166
1085inline_speed void * 1167inline_speed void *
1173 #undef VAR 1255 #undef VAR
1174 }; 1256 };
1175 #include "ev_wrap.h" 1257 #include "ev_wrap.h"
1176 1258
1177 static struct ev_loop default_loop_struct; 1259 static struct ev_loop default_loop_struct;
1178 struct ev_loop *ev_default_loop_ptr; 1260 EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */
1179 1261
1180#else 1262#else
1181 1263
1182 ev_tstamp ev_rt_now; 1264 EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */
1183 #define VAR(name,decl) static decl; 1265 #define VAR(name,decl) static decl;
1184 #include "ev_vars.h" 1266 #include "ev_vars.h"
1185 #undef VAR 1267 #undef VAR
1186 1268
1187 static int ev_default_loop_ptr; 1269 static int ev_default_loop_ptr;
1202 1284
1203/*****************************************************************************/ 1285/*****************************************************************************/
1204 1286
1205#ifndef EV_HAVE_EV_TIME 1287#ifndef EV_HAVE_EV_TIME
1206ev_tstamp 1288ev_tstamp
1207ev_time (void) 1289ev_time (void) EV_THROW
1208{ 1290{
1209#if EV_USE_REALTIME 1291#if EV_USE_REALTIME
1210 if (expect_true (have_realtime)) 1292 if (expect_true (have_realtime))
1211 { 1293 {
1212 struct timespec ts; 1294 struct timespec ts;
1236 return ev_time (); 1318 return ev_time ();
1237} 1319}
1238 1320
1239#if EV_MULTIPLICITY 1321#if EV_MULTIPLICITY
1240ev_tstamp 1322ev_tstamp
1241ev_now (EV_P) 1323ev_now (EV_P) EV_THROW
1242{ 1324{
1243 return ev_rt_now; 1325 return ev_rt_now;
1244} 1326}
1245#endif 1327#endif
1246 1328
1247void 1329void
1248ev_sleep (ev_tstamp delay) 1330ev_sleep (ev_tstamp delay) EV_THROW
1249{ 1331{
1250 if (delay > 0.) 1332 if (delay > 0.)
1251 { 1333 {
1252#if EV_USE_NANOSLEEP 1334#if EV_USE_NANOSLEEP
1253 struct timespec ts; 1335 struct timespec ts;
1254 1336
1255 EV_TS_SET (ts, delay); 1337 EV_TS_SET (ts, delay);
1256 nanosleep (&ts, 0); 1338 nanosleep (&ts, 0);
1257#elif defined(_WIN32) 1339#elif defined _WIN32
1258 Sleep ((unsigned long)(delay * 1e3)); 1340 Sleep ((unsigned long)(delay * 1e3));
1259#else 1341#else
1260 struct timeval tv; 1342 struct timeval tv;
1261 1343
1262 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 1344 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
1281 1363
1282 do 1364 do
1283 ncur <<= 1; 1365 ncur <<= 1;
1284 while (cnt > ncur); 1366 while (cnt > ncur);
1285 1367
1286 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ 1368 /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */
1287 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) 1369 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
1288 { 1370 {
1289 ncur *= elem; 1371 ncur *= elem;
1290 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); 1372 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
1291 ncur = ncur - sizeof (void *) * 4; 1373 ncur = ncur - sizeof (void *) * 4;
1334pendingcb (EV_P_ ev_prepare *w, int revents) 1416pendingcb (EV_P_ ev_prepare *w, int revents)
1335{ 1417{
1336} 1418}
1337 1419
1338void noinline 1420void noinline
1339ev_feed_event (EV_P_ void *w, int revents) 1421ev_feed_event (EV_P_ void *w, int revents) EV_THROW
1340{ 1422{
1341 W w_ = (W)w; 1423 W w_ = (W)w;
1342 int pri = ABSPRI (w_); 1424 int pri = ABSPRI (w_);
1343 1425
1344 if (expect_false (w_->pending)) 1426 if (expect_false (w_->pending))
1403 if (expect_true (!anfd->reify)) 1485 if (expect_true (!anfd->reify))
1404 fd_event_nocheck (EV_A_ fd, revents); 1486 fd_event_nocheck (EV_A_ fd, revents);
1405} 1487}
1406 1488
1407void 1489void
1408ev_feed_fd_event (EV_P_ int fd, int revents) 1490ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW
1409{ 1491{
1410 if (fd >= 0 && fd < anfdmax) 1492 if (fd >= 0 && fd < anfdmax)
1411 fd_event_nocheck (EV_A_ fd, revents); 1493 fd_event_nocheck (EV_A_ fd, revents);
1412} 1494}
1413 1495
1762} 1844}
1763 1845
1764inline_speed void 1846inline_speed void
1765evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1847evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1766{ 1848{
1849 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
1850
1767 if (expect_true (*flag)) 1851 if (expect_true (*flag))
1768 return; 1852 return;
1769 1853
1770 *flag = 1; 1854 *flag = 1;
1771 1855
1795 /* win32 people keep sending patches that change this write() to send() */ 1879 /* win32 people keep sending patches that change this write() to send() */
1796 /* and then run away. but send() is wrong, it wants a socket handle on win32 */ 1880 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1797 /* so when you think this write should be a send instead, please find out */ 1881 /* so when you think this write should be a send instead, please find out */
1798 /* where your send() is from - it's definitely not the microsoft send, and */ 1882 /* where your send() is from - it's definitely not the microsoft send, and */
1799 /* tell me. thank you. */ 1883 /* tell me. thank you. */
1884 /* it might be that your problem is that your environment needs EV_USE_WSASOCKET */
1885 /* check the ev documentation on how to use this flag */
1800 write (evpipe [1], &(evpipe [1]), 1); 1886 write (evpipe [1], &(evpipe [1]), 1);
1801 } 1887 }
1802 1888
1803 errno = old_errno; 1889 errno = old_errno;
1804 } 1890 }
1828 } 1914 }
1829 } 1915 }
1830 1916
1831 pipe_write_skipped = 0; 1917 pipe_write_skipped = 0;
1832 1918
1919 ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
1920
1833#if EV_SIGNAL_ENABLE 1921#if EV_SIGNAL_ENABLE
1834 if (sig_pending) 1922 if (sig_pending)
1835 { 1923 {
1836 sig_pending = 0; 1924 sig_pending = 0;
1925
1926 ECB_MEMORY_FENCE_RELEASE;
1837 1927
1838 for (i = EV_NSIG - 1; i--; ) 1928 for (i = EV_NSIG - 1; i--; )
1839 if (expect_false (signals [i].pending)) 1929 if (expect_false (signals [i].pending))
1840 ev_feed_signal_event (EV_A_ i + 1); 1930 ev_feed_signal_event (EV_A_ i + 1);
1841 } 1931 }
1843 1933
1844#if EV_ASYNC_ENABLE 1934#if EV_ASYNC_ENABLE
1845 if (async_pending) 1935 if (async_pending)
1846 { 1936 {
1847 async_pending = 0; 1937 async_pending = 0;
1938
1939 ECB_MEMORY_FENCE_RELEASE;
1848 1940
1849 for (i = asynccnt; i--; ) 1941 for (i = asynccnt; i--; )
1850 if (asyncs [i]->sent) 1942 if (asyncs [i]->sent)
1851 { 1943 {
1852 asyncs [i]->sent = 0; 1944 asyncs [i]->sent = 0;
1857} 1949}
1858 1950
1859/*****************************************************************************/ 1951/*****************************************************************************/
1860 1952
1861void 1953void
1862ev_feed_signal (int signum) 1954ev_feed_signal (int signum) EV_THROW
1863{ 1955{
1864#if EV_MULTIPLICITY 1956#if EV_MULTIPLICITY
1865 EV_P = signals [signum - 1].loop; 1957 EV_P = signals [signum - 1].loop;
1866 1958
1867 if (!EV_A) 1959 if (!EV_A)
1884 1976
1885 ev_feed_signal (signum); 1977 ev_feed_signal (signum);
1886} 1978}
1887 1979
1888void noinline 1980void noinline
1889ev_feed_signal_event (EV_P_ int signum) 1981ev_feed_signal_event (EV_P_ int signum) EV_THROW
1890{ 1982{
1891 WL w; 1983 WL w;
1892 1984
1893 if (expect_false (signum <= 0 || signum > EV_NSIG)) 1985 if (expect_false (signum <= 0 || signum > EV_NSIG))
1894 return; 1986 return;
2010#if EV_USE_SELECT 2102#if EV_USE_SELECT
2011# include "ev_select.c" 2103# include "ev_select.c"
2012#endif 2104#endif
2013 2105
2014int ecb_cold 2106int ecb_cold
2015ev_version_major (void) 2107ev_version_major (void) EV_THROW
2016{ 2108{
2017 return EV_VERSION_MAJOR; 2109 return EV_VERSION_MAJOR;
2018} 2110}
2019 2111
2020int ecb_cold 2112int ecb_cold
2021ev_version_minor (void) 2113ev_version_minor (void) EV_THROW
2022{ 2114{
2023 return EV_VERSION_MINOR; 2115 return EV_VERSION_MINOR;
2024} 2116}
2025 2117
2026/* return true if we are running with elevated privileges and should ignore env variables */ 2118/* return true if we are running with elevated privileges and should ignore env variables */
2034 || getgid () != getegid (); 2126 || getgid () != getegid ();
2035#endif 2127#endif
2036} 2128}
2037 2129
2038unsigned int ecb_cold 2130unsigned int ecb_cold
2039ev_supported_backends (void) 2131ev_supported_backends (void) EV_THROW
2040{ 2132{
2041 unsigned int flags = 0; 2133 unsigned int flags = 0;
2042 2134
2043 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2135 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2044 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2136 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
2048 2140
2049 return flags; 2141 return flags;
2050} 2142}
2051 2143
2052unsigned int ecb_cold 2144unsigned int ecb_cold
2053ev_recommended_backends (void) 2145ev_recommended_backends (void) EV_THROW
2054{ 2146{
2055 unsigned int flags = ev_supported_backends (); 2147 unsigned int flags = ev_supported_backends ();
2056 2148
2057#ifndef __NetBSD__ 2149#ifndef __NetBSD__
2058 /* kqueue is borked on everything but netbsd apparently */ 2150 /* kqueue is borked on everything but netbsd apparently */
2070 2162
2071 return flags; 2163 return flags;
2072} 2164}
2073 2165
2074unsigned int ecb_cold 2166unsigned int ecb_cold
2075ev_embeddable_backends (void) 2167ev_embeddable_backends (void) EV_THROW
2076{ 2168{
2077 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 2169 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2078 2170
2079 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 2171 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
2080 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ 2172 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2082 2174
2083 return flags; 2175 return flags;
2084} 2176}
2085 2177
2086unsigned int 2178unsigned int
2087ev_backend (EV_P) 2179ev_backend (EV_P) EV_THROW
2088{ 2180{
2089 return backend; 2181 return backend;
2090} 2182}
2091 2183
2092#if EV_FEATURE_API 2184#if EV_FEATURE_API
2093unsigned int 2185unsigned int
2094ev_iteration (EV_P) 2186ev_iteration (EV_P) EV_THROW
2095{ 2187{
2096 return loop_count; 2188 return loop_count;
2097} 2189}
2098 2190
2099unsigned int 2191unsigned int
2100ev_depth (EV_P) 2192ev_depth (EV_P) EV_THROW
2101{ 2193{
2102 return loop_depth; 2194 return loop_depth;
2103} 2195}
2104 2196
2105void 2197void
2106ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 2198ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2107{ 2199{
2108 io_blocktime = interval; 2200 io_blocktime = interval;
2109} 2201}
2110 2202
2111void 2203void
2112ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 2204ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2113{ 2205{
2114 timeout_blocktime = interval; 2206 timeout_blocktime = interval;
2115} 2207}
2116 2208
2117void 2209void
2118ev_set_userdata (EV_P_ void *data) 2210ev_set_userdata (EV_P_ void *data) EV_THROW
2119{ 2211{
2120 userdata = data; 2212 userdata = data;
2121} 2213}
2122 2214
2123void * 2215void *
2124ev_userdata (EV_P) 2216ev_userdata (EV_P) EV_THROW
2125{ 2217{
2126 return userdata; 2218 return userdata;
2127} 2219}
2128 2220
2129void 2221void
2130ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 2222ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) EV_THROW
2131{ 2223{
2132 invoke_cb = invoke_pending_cb; 2224 invoke_cb = invoke_pending_cb;
2133} 2225}
2134 2226
2135void 2227void
2136ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) 2228ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW
2137{ 2229{
2138 release_cb = release; 2230 release_cb = release;
2139 acquire_cb = acquire; 2231 acquire_cb = acquire;
2140} 2232}
2141#endif 2233#endif
2142 2234
2143/* initialise a loop structure, must be zero-initialised */ 2235/* initialise a loop structure, must be zero-initialised */
2144static void noinline ecb_cold 2236static void noinline ecb_cold
2145loop_init (EV_P_ unsigned int flags) 2237loop_init (EV_P_ unsigned int flags) EV_THROW
2146{ 2238{
2147 if (!backend) 2239 if (!backend)
2148 { 2240 {
2149 origflags = flags; 2241 origflags = flags;
2150 2242
2403} 2495}
2404 2496
2405#if EV_MULTIPLICITY 2497#if EV_MULTIPLICITY
2406 2498
2407struct ev_loop * ecb_cold 2499struct ev_loop * ecb_cold
2408ev_loop_new (unsigned int flags) 2500ev_loop_new (unsigned int flags) EV_THROW
2409{ 2501{
2410 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2502 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2411 2503
2412 memset (EV_A, 0, sizeof (struct ev_loop)); 2504 memset (EV_A, 0, sizeof (struct ev_loop));
2413 loop_init (EV_A_ flags); 2505 loop_init (EV_A_ flags);
2457} 2549}
2458#endif 2550#endif
2459 2551
2460#if EV_FEATURE_API 2552#if EV_FEATURE_API
2461void ecb_cold 2553void ecb_cold
2462ev_verify (EV_P) 2554ev_verify (EV_P) EV_THROW
2463{ 2555{
2464#if EV_VERIFY 2556#if EV_VERIFY
2465 int i; 2557 int i;
2466 WL w; 2558 WL w;
2467 2559
2536#if EV_MULTIPLICITY 2628#if EV_MULTIPLICITY
2537struct ev_loop * ecb_cold 2629struct ev_loop * ecb_cold
2538#else 2630#else
2539int 2631int
2540#endif 2632#endif
2541ev_default_loop (unsigned int flags) 2633ev_default_loop (unsigned int flags) EV_THROW
2542{ 2634{
2543 if (!ev_default_loop_ptr) 2635 if (!ev_default_loop_ptr)
2544 { 2636 {
2545#if EV_MULTIPLICITY 2637#if EV_MULTIPLICITY
2546 EV_P = ev_default_loop_ptr = &default_loop_struct; 2638 EV_P = ev_default_loop_ptr = &default_loop_struct;
2565 2657
2566 return ev_default_loop_ptr; 2658 return ev_default_loop_ptr;
2567} 2659}
2568 2660
2569void 2661void
2570ev_loop_fork (EV_P) 2662ev_loop_fork (EV_P) EV_THROW
2571{ 2663{
2572 postfork = 1; /* must be in line with ev_default_fork */ 2664 postfork = 1; /* must be in line with ev_default_fork */
2573} 2665}
2574 2666
2575/*****************************************************************************/ 2667/*****************************************************************************/
2579{ 2671{
2580 EV_CB_INVOKE ((W)w, revents); 2672 EV_CB_INVOKE ((W)w, revents);
2581} 2673}
2582 2674
2583unsigned int 2675unsigned int
2584ev_pending_count (EV_P) 2676ev_pending_count (EV_P) EV_THROW
2585{ 2677{
2586 int pri; 2678 int pri;
2587 unsigned int count = 0; 2679 unsigned int count = 0;
2588 2680
2589 for (pri = NUMPRI; pri--; ) 2681 for (pri = NUMPRI; pri--; )
2848 2940
2849 mn_now = ev_rt_now; 2941 mn_now = ev_rt_now;
2850 } 2942 }
2851} 2943}
2852 2944
2853void 2945int
2854ev_run (EV_P_ int flags) 2946ev_run (EV_P_ int flags)
2855{ 2947{
2856#if EV_FEATURE_API 2948#if EV_FEATURE_API
2857 ++loop_depth; 2949 ++loop_depth;
2858#endif 2950#endif
2971#endif 3063#endif
2972 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ 3064 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2973 backend_poll (EV_A_ waittime); 3065 backend_poll (EV_A_ waittime);
2974 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 3066 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2975 3067
2976 pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ 3068 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
2977 3069
2978 if (pipe_write_skipped) 3070 if (pipe_write_skipped)
2979 { 3071 {
2980 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); 3072 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
2981 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3073 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3015 loop_done = EVBREAK_CANCEL; 3107 loop_done = EVBREAK_CANCEL;
3016 3108
3017#if EV_FEATURE_API 3109#if EV_FEATURE_API
3018 --loop_depth; 3110 --loop_depth;
3019#endif 3111#endif
3112
3113 return activecnt;
3020} 3114}
3021 3115
3022void 3116void
3023ev_break (EV_P_ int how) 3117ev_break (EV_P_ int how) EV_THROW
3024{ 3118{
3025 loop_done = how; 3119 loop_done = how;
3026} 3120}
3027 3121
3028void 3122void
3029ev_ref (EV_P) 3123ev_ref (EV_P) EV_THROW
3030{ 3124{
3031 ++activecnt; 3125 ++activecnt;
3032} 3126}
3033 3127
3034void 3128void
3035ev_unref (EV_P) 3129ev_unref (EV_P) EV_THROW
3036{ 3130{
3037 --activecnt; 3131 --activecnt;
3038} 3132}
3039 3133
3040void 3134void
3041ev_now_update (EV_P) 3135ev_now_update (EV_P) EV_THROW
3042{ 3136{
3043 time_update (EV_A_ 1e100); 3137 time_update (EV_A_ 1e100);
3044} 3138}
3045 3139
3046void 3140void
3047ev_suspend (EV_P) 3141ev_suspend (EV_P) EV_THROW
3048{ 3142{
3049 ev_now_update (EV_A); 3143 ev_now_update (EV_A);
3050} 3144}
3051 3145
3052void 3146void
3053ev_resume (EV_P) 3147ev_resume (EV_P) EV_THROW
3054{ 3148{
3055 ev_tstamp mn_prev = mn_now; 3149 ev_tstamp mn_prev = mn_now;
3056 3150
3057 ev_now_update (EV_A); 3151 ev_now_update (EV_A);
3058 timers_reschedule (EV_A_ mn_now - mn_prev); 3152 timers_reschedule (EV_A_ mn_now - mn_prev);
3097 w->pending = 0; 3191 w->pending = 0;
3098 } 3192 }
3099} 3193}
3100 3194
3101int 3195int
3102ev_clear_pending (EV_P_ void *w) 3196ev_clear_pending (EV_P_ void *w) EV_THROW
3103{ 3197{
3104 W w_ = (W)w; 3198 W w_ = (W)w;
3105 int pending = w_->pending; 3199 int pending = w_->pending;
3106 3200
3107 if (expect_true (pending)) 3201 if (expect_true (pending))
3140} 3234}
3141 3235
3142/*****************************************************************************/ 3236/*****************************************************************************/
3143 3237
3144void noinline 3238void noinline
3145ev_io_start (EV_P_ ev_io *w) 3239ev_io_start (EV_P_ ev_io *w) EV_THROW
3146{ 3240{
3147 int fd = w->fd; 3241 int fd = w->fd;
3148 3242
3149 if (expect_false (ev_is_active (w))) 3243 if (expect_false (ev_is_active (w)))
3150 return; 3244 return;
3163 3257
3164 EV_FREQUENT_CHECK; 3258 EV_FREQUENT_CHECK;
3165} 3259}
3166 3260
3167void noinline 3261void noinline
3168ev_io_stop (EV_P_ ev_io *w) 3262ev_io_stop (EV_P_ ev_io *w) EV_THROW
3169{ 3263{
3170 clear_pending (EV_A_ (W)w); 3264 clear_pending (EV_A_ (W)w);
3171 if (expect_false (!ev_is_active (w))) 3265 if (expect_false (!ev_is_active (w)))
3172 return; 3266 return;
3173 3267
3182 3276
3183 EV_FREQUENT_CHECK; 3277 EV_FREQUENT_CHECK;
3184} 3278}
3185 3279
3186void noinline 3280void noinline
3187ev_timer_start (EV_P_ ev_timer *w) 3281ev_timer_start (EV_P_ ev_timer *w) EV_THROW
3188{ 3282{
3189 if (expect_false (ev_is_active (w))) 3283 if (expect_false (ev_is_active (w)))
3190 return; 3284 return;
3191 3285
3192 ev_at (w) += mn_now; 3286 ev_at (w) += mn_now;
3206 3300
3207 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ 3301 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
3208} 3302}
3209 3303
3210void noinline 3304void noinline
3211ev_timer_stop (EV_P_ ev_timer *w) 3305ev_timer_stop (EV_P_ ev_timer *w) EV_THROW
3212{ 3306{
3213 clear_pending (EV_A_ (W)w); 3307 clear_pending (EV_A_ (W)w);
3214 if (expect_false (!ev_is_active (w))) 3308 if (expect_false (!ev_is_active (w)))
3215 return; 3309 return;
3216 3310
3236 3330
3237 EV_FREQUENT_CHECK; 3331 EV_FREQUENT_CHECK;
3238} 3332}
3239 3333
3240void noinline 3334void noinline
3241ev_timer_again (EV_P_ ev_timer *w) 3335ev_timer_again (EV_P_ ev_timer *w) EV_THROW
3242{ 3336{
3243 EV_FREQUENT_CHECK; 3337 EV_FREQUENT_CHECK;
3338
3339 clear_pending (EV_A_ (W)w);
3244 3340
3245 if (ev_is_active (w)) 3341 if (ev_is_active (w))
3246 { 3342 {
3247 if (w->repeat) 3343 if (w->repeat)
3248 { 3344 {
3261 3357
3262 EV_FREQUENT_CHECK; 3358 EV_FREQUENT_CHECK;
3263} 3359}
3264 3360
3265ev_tstamp 3361ev_tstamp
3266ev_timer_remaining (EV_P_ ev_timer *w) 3362ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW
3267{ 3363{
3268 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 3364 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
3269} 3365}
3270 3366
3271#if EV_PERIODIC_ENABLE 3367#if EV_PERIODIC_ENABLE
3272void noinline 3368void noinline
3273ev_periodic_start (EV_P_ ev_periodic *w) 3369ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW
3274{ 3370{
3275 if (expect_false (ev_is_active (w))) 3371 if (expect_false (ev_is_active (w)))
3276 return; 3372 return;
3277 3373
3278 if (w->reschedule_cb) 3374 if (w->reschedule_cb)
3298 3394
3299 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ 3395 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
3300} 3396}
3301 3397
3302void noinline 3398void noinline
3303ev_periodic_stop (EV_P_ ev_periodic *w) 3399ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW
3304{ 3400{
3305 clear_pending (EV_A_ (W)w); 3401 clear_pending (EV_A_ (W)w);
3306 if (expect_false (!ev_is_active (w))) 3402 if (expect_false (!ev_is_active (w)))
3307 return; 3403 return;
3308 3404
3326 3422
3327 EV_FREQUENT_CHECK; 3423 EV_FREQUENT_CHECK;
3328} 3424}
3329 3425
3330void noinline 3426void noinline
3331ev_periodic_again (EV_P_ ev_periodic *w) 3427ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW
3332{ 3428{
3333 /* TODO: use adjustheap and recalculation */ 3429 /* TODO: use adjustheap and recalculation */
3334 ev_periodic_stop (EV_A_ w); 3430 ev_periodic_stop (EV_A_ w);
3335 ev_periodic_start (EV_A_ w); 3431 ev_periodic_start (EV_A_ w);
3336} 3432}
3341#endif 3437#endif
3342 3438
3343#if EV_SIGNAL_ENABLE 3439#if EV_SIGNAL_ENABLE
3344 3440
3345void noinline 3441void noinline
3346ev_signal_start (EV_P_ ev_signal *w) 3442ev_signal_start (EV_P_ ev_signal *w) EV_THROW
3347{ 3443{
3348 if (expect_false (ev_is_active (w))) 3444 if (expect_false (ev_is_active (w)))
3349 return; 3445 return;
3350 3446
3351 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 3447 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
3422 3518
3423 EV_FREQUENT_CHECK; 3519 EV_FREQUENT_CHECK;
3424} 3520}
3425 3521
3426void noinline 3522void noinline
3427ev_signal_stop (EV_P_ ev_signal *w) 3523ev_signal_stop (EV_P_ ev_signal *w) EV_THROW
3428{ 3524{
3429 clear_pending (EV_A_ (W)w); 3525 clear_pending (EV_A_ (W)w);
3430 if (expect_false (!ev_is_active (w))) 3526 if (expect_false (!ev_is_active (w)))
3431 return; 3527 return;
3432 3528
3463#endif 3559#endif
3464 3560
3465#if EV_CHILD_ENABLE 3561#if EV_CHILD_ENABLE
3466 3562
3467void 3563void
3468ev_child_start (EV_P_ ev_child *w) 3564ev_child_start (EV_P_ ev_child *w) EV_THROW
3469{ 3565{
3470#if EV_MULTIPLICITY 3566#if EV_MULTIPLICITY
3471 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 3567 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
3472#endif 3568#endif
3473 if (expect_false (ev_is_active (w))) 3569 if (expect_false (ev_is_active (w)))
3480 3576
3481 EV_FREQUENT_CHECK; 3577 EV_FREQUENT_CHECK;
3482} 3578}
3483 3579
3484void 3580void
3485ev_child_stop (EV_P_ ev_child *w) 3581ev_child_stop (EV_P_ ev_child *w) EV_THROW
3486{ 3582{
3487 clear_pending (EV_A_ (W)w); 3583 clear_pending (EV_A_ (W)w);
3488 if (expect_false (!ev_is_active (w))) 3584 if (expect_false (!ev_is_active (w)))
3489 return; 3585 return;
3490 3586
3657} 3753}
3658 3754
3659inline_size int 3755inline_size int
3660infy_newfd (void) 3756infy_newfd (void)
3661{ 3757{
3662#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) 3758#if defined IN_CLOEXEC && defined IN_NONBLOCK
3663 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); 3759 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3664 if (fd >= 0) 3760 if (fd >= 0)
3665 return fd; 3761 return fd;
3666#endif 3762#endif
3667 return inotify_init (); 3763 return inotify_init ();
3742#else 3838#else
3743# define EV_LSTAT(p,b) lstat (p, b) 3839# define EV_LSTAT(p,b) lstat (p, b)
3744#endif 3840#endif
3745 3841
3746void 3842void
3747ev_stat_stat (EV_P_ ev_stat *w) 3843ev_stat_stat (EV_P_ ev_stat *w) EV_THROW
3748{ 3844{
3749 if (lstat (w->path, &w->attr) < 0) 3845 if (lstat (w->path, &w->attr) < 0)
3750 w->attr.st_nlink = 0; 3846 w->attr.st_nlink = 0;
3751 else if (!w->attr.st_nlink) 3847 else if (!w->attr.st_nlink)
3752 w->attr.st_nlink = 1; 3848 w->attr.st_nlink = 1;
3791 ev_feed_event (EV_A_ w, EV_STAT); 3887 ev_feed_event (EV_A_ w, EV_STAT);
3792 } 3888 }
3793} 3889}
3794 3890
3795void 3891void
3796ev_stat_start (EV_P_ ev_stat *w) 3892ev_stat_start (EV_P_ ev_stat *w) EV_THROW
3797{ 3893{
3798 if (expect_false (ev_is_active (w))) 3894 if (expect_false (ev_is_active (w)))
3799 return; 3895 return;
3800 3896
3801 ev_stat_stat (EV_A_ w); 3897 ev_stat_stat (EV_A_ w);
3822 3918
3823 EV_FREQUENT_CHECK; 3919 EV_FREQUENT_CHECK;
3824} 3920}
3825 3921
3826void 3922void
3827ev_stat_stop (EV_P_ ev_stat *w) 3923ev_stat_stop (EV_P_ ev_stat *w) EV_THROW
3828{ 3924{
3829 clear_pending (EV_A_ (W)w); 3925 clear_pending (EV_A_ (W)w);
3830 if (expect_false (!ev_is_active (w))) 3926 if (expect_false (!ev_is_active (w)))
3831 return; 3927 return;
3832 3928
3848} 3944}
3849#endif 3945#endif
3850 3946
3851#if EV_IDLE_ENABLE 3947#if EV_IDLE_ENABLE
3852void 3948void
3853ev_idle_start (EV_P_ ev_idle *w) 3949ev_idle_start (EV_P_ ev_idle *w) EV_THROW
3854{ 3950{
3855 if (expect_false (ev_is_active (w))) 3951 if (expect_false (ev_is_active (w)))
3856 return; 3952 return;
3857 3953
3858 pri_adjust (EV_A_ (W)w); 3954 pri_adjust (EV_A_ (W)w);
3871 3967
3872 EV_FREQUENT_CHECK; 3968 EV_FREQUENT_CHECK;
3873} 3969}
3874 3970
3875void 3971void
3876ev_idle_stop (EV_P_ ev_idle *w) 3972ev_idle_stop (EV_P_ ev_idle *w) EV_THROW
3877{ 3973{
3878 clear_pending (EV_A_ (W)w); 3974 clear_pending (EV_A_ (W)w);
3879 if (expect_false (!ev_is_active (w))) 3975 if (expect_false (!ev_is_active (w)))
3880 return; 3976 return;
3881 3977
3895} 3991}
3896#endif 3992#endif
3897 3993
3898#if EV_PREPARE_ENABLE 3994#if EV_PREPARE_ENABLE
3899void 3995void
3900ev_prepare_start (EV_P_ ev_prepare *w) 3996ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW
3901{ 3997{
3902 if (expect_false (ev_is_active (w))) 3998 if (expect_false (ev_is_active (w)))
3903 return; 3999 return;
3904 4000
3905 EV_FREQUENT_CHECK; 4001 EV_FREQUENT_CHECK;
3910 4006
3911 EV_FREQUENT_CHECK; 4007 EV_FREQUENT_CHECK;
3912} 4008}
3913 4009
3914void 4010void
3915ev_prepare_stop (EV_P_ ev_prepare *w) 4011ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW
3916{ 4012{
3917 clear_pending (EV_A_ (W)w); 4013 clear_pending (EV_A_ (W)w);
3918 if (expect_false (!ev_is_active (w))) 4014 if (expect_false (!ev_is_active (w)))
3919 return; 4015 return;
3920 4016
3933} 4029}
3934#endif 4030#endif
3935 4031
3936#if EV_CHECK_ENABLE 4032#if EV_CHECK_ENABLE
3937void 4033void
3938ev_check_start (EV_P_ ev_check *w) 4034ev_check_start (EV_P_ ev_check *w) EV_THROW
3939{ 4035{
3940 if (expect_false (ev_is_active (w))) 4036 if (expect_false (ev_is_active (w)))
3941 return; 4037 return;
3942 4038
3943 EV_FREQUENT_CHECK; 4039 EV_FREQUENT_CHECK;
3948 4044
3949 EV_FREQUENT_CHECK; 4045 EV_FREQUENT_CHECK;
3950} 4046}
3951 4047
3952void 4048void
3953ev_check_stop (EV_P_ ev_check *w) 4049ev_check_stop (EV_P_ ev_check *w) EV_THROW
3954{ 4050{
3955 clear_pending (EV_A_ (W)w); 4051 clear_pending (EV_A_ (W)w);
3956 if (expect_false (!ev_is_active (w))) 4052 if (expect_false (!ev_is_active (w)))
3957 return; 4053 return;
3958 4054
3971} 4067}
3972#endif 4068#endif
3973 4069
3974#if EV_EMBED_ENABLE 4070#if EV_EMBED_ENABLE
3975void noinline 4071void noinline
3976ev_embed_sweep (EV_P_ ev_embed *w) 4072ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW
3977{ 4073{
3978 ev_run (w->other, EVRUN_NOWAIT); 4074 ev_run (w->other, EVRUN_NOWAIT);
3979} 4075}
3980 4076
3981static void 4077static void
4029 ev_idle_stop (EV_A_ idle); 4125 ev_idle_stop (EV_A_ idle);
4030} 4126}
4031#endif 4127#endif
4032 4128
4033void 4129void
4034ev_embed_start (EV_P_ ev_embed *w) 4130ev_embed_start (EV_P_ ev_embed *w) EV_THROW
4035{ 4131{
4036 if (expect_false (ev_is_active (w))) 4132 if (expect_false (ev_is_active (w)))
4037 return; 4133 return;
4038 4134
4039 { 4135 {
4060 4156
4061 EV_FREQUENT_CHECK; 4157 EV_FREQUENT_CHECK;
4062} 4158}
4063 4159
4064void 4160void
4065ev_embed_stop (EV_P_ ev_embed *w) 4161ev_embed_stop (EV_P_ ev_embed *w) EV_THROW
4066{ 4162{
4067 clear_pending (EV_A_ (W)w); 4163 clear_pending (EV_A_ (W)w);
4068 if (expect_false (!ev_is_active (w))) 4164 if (expect_false (!ev_is_active (w)))
4069 return; 4165 return;
4070 4166
4080} 4176}
4081#endif 4177#endif
4082 4178
4083#if EV_FORK_ENABLE 4179#if EV_FORK_ENABLE
4084void 4180void
4085ev_fork_start (EV_P_ ev_fork *w) 4181ev_fork_start (EV_P_ ev_fork *w) EV_THROW
4086{ 4182{
4087 if (expect_false (ev_is_active (w))) 4183 if (expect_false (ev_is_active (w)))
4088 return; 4184 return;
4089 4185
4090 EV_FREQUENT_CHECK; 4186 EV_FREQUENT_CHECK;
4095 4191
4096 EV_FREQUENT_CHECK; 4192 EV_FREQUENT_CHECK;
4097} 4193}
4098 4194
4099void 4195void
4100ev_fork_stop (EV_P_ ev_fork *w) 4196ev_fork_stop (EV_P_ ev_fork *w) EV_THROW
4101{ 4197{
4102 clear_pending (EV_A_ (W)w); 4198 clear_pending (EV_A_ (W)w);
4103 if (expect_false (!ev_is_active (w))) 4199 if (expect_false (!ev_is_active (w)))
4104 return; 4200 return;
4105 4201
4118} 4214}
4119#endif 4215#endif
4120 4216
4121#if EV_CLEANUP_ENABLE 4217#if EV_CLEANUP_ENABLE
4122void 4218void
4123ev_cleanup_start (EV_P_ ev_cleanup *w) 4219ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW
4124{ 4220{
4125 if (expect_false (ev_is_active (w))) 4221 if (expect_false (ev_is_active (w)))
4126 return; 4222 return;
4127 4223
4128 EV_FREQUENT_CHECK; 4224 EV_FREQUENT_CHECK;
4135 ev_unref (EV_A); 4231 ev_unref (EV_A);
4136 EV_FREQUENT_CHECK; 4232 EV_FREQUENT_CHECK;
4137} 4233}
4138 4234
4139void 4235void
4140ev_cleanup_stop (EV_P_ ev_cleanup *w) 4236ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW
4141{ 4237{
4142 clear_pending (EV_A_ (W)w); 4238 clear_pending (EV_A_ (W)w);
4143 if (expect_false (!ev_is_active (w))) 4239 if (expect_false (!ev_is_active (w)))
4144 return; 4240 return;
4145 4241
4159} 4255}
4160#endif 4256#endif
4161 4257
4162#if EV_ASYNC_ENABLE 4258#if EV_ASYNC_ENABLE
4163void 4259void
4164ev_async_start (EV_P_ ev_async *w) 4260ev_async_start (EV_P_ ev_async *w) EV_THROW
4165{ 4261{
4166 if (expect_false (ev_is_active (w))) 4262 if (expect_false (ev_is_active (w)))
4167 return; 4263 return;
4168 4264
4169 w->sent = 0; 4265 w->sent = 0;
4178 4274
4179 EV_FREQUENT_CHECK; 4275 EV_FREQUENT_CHECK;
4180} 4276}
4181 4277
4182void 4278void
4183ev_async_stop (EV_P_ ev_async *w) 4279ev_async_stop (EV_P_ ev_async *w) EV_THROW
4184{ 4280{
4185 clear_pending (EV_A_ (W)w); 4281 clear_pending (EV_A_ (W)w);
4186 if (expect_false (!ev_is_active (w))) 4282 if (expect_false (!ev_is_active (w)))
4187 return; 4283 return;
4188 4284
4199 4295
4200 EV_FREQUENT_CHECK; 4296 EV_FREQUENT_CHECK;
4201} 4297}
4202 4298
4203void 4299void
4204ev_async_send (EV_P_ ev_async *w) 4300ev_async_send (EV_P_ ev_async *w) EV_THROW
4205{ 4301{
4206 w->sent = 1; 4302 w->sent = 1;
4207 evpipe_write (EV_A_ &async_pending); 4303 evpipe_write (EV_A_ &async_pending);
4208} 4304}
4209#endif 4305#endif
4246 4342
4247 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); 4343 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
4248} 4344}
4249 4345
4250void 4346void
4251ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 4347ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW
4252{ 4348{
4253 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 4349 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
4254 4350
4255 if (expect_false (!once)) 4351 if (expect_false (!once))
4256 { 4352 {
4278 4374
4279/*****************************************************************************/ 4375/*****************************************************************************/
4280 4376
4281#if EV_WALK_ENABLE 4377#if EV_WALK_ENABLE
4282void ecb_cold 4378void ecb_cold
4283ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 4379ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW
4284{ 4380{
4285 int i, j; 4381 int i, j;
4286 ev_watcher_list *wl, *wn; 4382 ev_watcher_list *wl, *wn;
4287 4383
4288 if (types & (EV_IO | EV_EMBED)) 4384 if (types & (EV_IO | EV_EMBED))
4394 4490
4395#if EV_MULTIPLICITY 4491#if EV_MULTIPLICITY
4396 #include "ev_wrap.h" 4492 #include "ev_wrap.h"
4397#endif 4493#endif
4398 4494
4399EV_CPP(})
4400

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines