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

Comparing libev/ev.c (file contents):
Revision 1.391 by root, Thu Aug 4 13:57:16 2011 UTC vs.
Revision 1.422 by root, Wed Apr 18 06:09:29 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
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 */
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 #elif defined __alpha__
585 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
557 #endif 586 #endif
558 #endif 587 #endif
559#endif 588#endif
560 589
561#ifndef ECB_MEMORY_FENCE 590#ifndef ECB_MEMORY_FENCE
562 #if ECB_GCC_VERSION(4,4) 591 #if ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
563 #define ECB_MEMORY_FENCE __sync_synchronize () 592 #define ECB_MEMORY_FENCE __sync_synchronize ()
564 #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); }) */
565 #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 ); }) */
566 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 595 #elif _MSC_VER >= 1400 /* VC++ 2005 */
567 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 596 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
568 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 597 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
569 #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 */
570 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 599 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
571 #elif defined(_WIN32) 600 #elif defined _WIN32
572 #include <WinNT.h> 601 #include <WinNT.h>
573 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ 602 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
574 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 603 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
575 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 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 ()
576 #endif 610 #endif
577#endif 611#endif
578 612
579#ifndef ECB_MEMORY_FENCE 613#ifndef ECB_MEMORY_FENCE
614 #if !ECB_AVOID_PTHREADS
580 /* 615 /*
581 * if you get undefined symbol references to pthread_mutex_lock, 616 * if you get undefined symbol references to pthread_mutex_lock,
582 * or failure to find pthread.h, then you should implement 617 * or failure to find pthread.h, then you should implement
583 * the ECB_MEMORY_FENCE operations for your cpu/compiler 618 * the ECB_MEMORY_FENCE operations for your cpu/compiler
584 * OR provide pthread.h and link against the posix thread library 619 * OR provide pthread.h and link against the posix thread library
585 * of your system. 620 * of your system.
586 */ 621 */
587 #include <pthread.h> 622 #include <pthread.h>
588 #define ECB_NEEDS_PTHREADS 1 623 #define ECB_NEEDS_PTHREADS 1
589 #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 624 #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1
590 625
591 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; 626 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
592 #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)
628 #endif
629#endif
630
631#if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
593 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 632 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
633#endif
634
635#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
594 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 636 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
595#endif 637#endif
596 638
597/*****************************************************************************/ 639/*****************************************************************************/
598 640
747 789
748 return r + ecb_ld32 (x); 790 return r + ecb_ld32 (x);
749 } 791 }
750#endif 792#endif
751 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
752/* popcount64 is only available on 64 bit cpus as gcc builtin */ 824/* popcount64 is only available on 64 bit cpus as gcc builtin */
753/* so for this version we are lazy */ 825/* so for this version we are lazy */
754ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; 826ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const;
755ecb_function_ int 827ecb_function_ int
756ecb_popcount64 (uint64_t x) 828ecb_popcount64 (uint64_t x)
805 877
806#if ECB_GCC_VERSION(4,5) 878#if ECB_GCC_VERSION(4,5)
807 #define ecb_unreachable() __builtin_unreachable () 879 #define ecb_unreachable() __builtin_unreachable ()
808#else 880#else
809 /* 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 :/ */
810 ecb_function_ void ecb_unreachable (void) ecb_noreturn; 882 ecb_inline void ecb_unreachable (void) ecb_noreturn;
811 ecb_function_ void ecb_unreachable (void) { } 883 ecb_inline void ecb_unreachable (void) { }
812#endif 884#endif
813 885
814/* try to tell the compiler that some condition is definitely true */ 886/* try to tell the compiler that some condition is definitely true */
815#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 887#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
816 888
817ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; 889ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
818ecb_function_ unsigned char 890ecb_inline unsigned char
819ecb_byteorder_helper (void) 891ecb_byteorder_helper (void)
820{ 892{
821 const uint32_t u = 0x11223344; 893 const uint32_t u = 0x11223344;
822 return *(unsigned char *)&u; 894 return *(unsigned char *)&u;
823} 895}
824 896
825ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; 897ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
826ecb_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; }
827ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; 899ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
828ecb_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; }
829 901
830#if ECB_GCC_VERSION(3,0) || ECB_C99 902#if ECB_GCC_VERSION(3,0) || ECB_C99
831 #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))
832#else 904#else
833 #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))
834#endif 922#endif
835 923
836#if ecb_cplusplus_does_not_suck 924#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) */ 925 /* 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> 926 template<typename T, int N>
845#endif 933#endif
846 934
847#endif 935#endif
848 936
849/* ECB.H END */ 937/* ECB.H END */
938
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
950#ifndef ECB_MEMORY_FENCE
951# define ECB_MEMORY_FENCE do { } while (0)
952# define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
953# define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
954#endif
850 955
851#define expect_false(cond) ecb_expect_false (cond) 956#define expect_false(cond) ecb_expect_false (cond)
852#define expect_true(cond) ecb_expect_true (cond) 957#define expect_true(cond) ecb_expect_true (cond)
853#define noinline ecb_noinline 958#define noinline ecb_noinline
854 959
1000{ 1105{
1001 write (STDERR_FILENO, msg, strlen (msg)); 1106 write (STDERR_FILENO, msg, strlen (msg));
1002} 1107}
1003#endif 1108#endif
1004 1109
1005static void (*syserr_cb)(const char *msg); 1110static void (*syserr_cb)(const char *msg) EV_THROW;
1006 1111
1007void ecb_cold 1112void ecb_cold
1008ev_set_syserr_cb (void (*cb)(const char *msg)) 1113ev_set_syserr_cb (void (*cb)(const char *msg)) EV_THROW
1009{ 1114{
1010 syserr_cb = cb; 1115 syserr_cb = cb;
1011} 1116}
1012 1117
1013static void noinline ecb_cold 1118static void noinline ecb_cold
1049 free (ptr); 1154 free (ptr);
1050 return 0; 1155 return 0;
1051#endif 1156#endif
1052} 1157}
1053 1158
1054static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 1159static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1055 1160
1056void ecb_cold 1161void ecb_cold
1057ev_set_allocator (void *(*cb)(void *ptr, long size)) 1162ev_set_allocator (void *(*cb)(void *ptr, long size)) EV_THROW
1058{ 1163{
1059 alloc = cb; 1164 alloc = cb;
1060} 1165}
1061 1166
1062inline_speed void * 1167inline_speed void *
1150 #undef VAR 1255 #undef VAR
1151 }; 1256 };
1152 #include "ev_wrap.h" 1257 #include "ev_wrap.h"
1153 1258
1154 static struct ev_loop default_loop_struct; 1259 static struct ev_loop default_loop_struct;
1155 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 */
1156 1261
1157#else 1262#else
1158 1263
1159 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 */
1160 #define VAR(name,decl) static decl; 1265 #define VAR(name,decl) static decl;
1161 #include "ev_vars.h" 1266 #include "ev_vars.h"
1162 #undef VAR 1267 #undef VAR
1163 1268
1164 static int ev_default_loop_ptr; 1269 static int ev_default_loop_ptr;
1179 1284
1180/*****************************************************************************/ 1285/*****************************************************************************/
1181 1286
1182#ifndef EV_HAVE_EV_TIME 1287#ifndef EV_HAVE_EV_TIME
1183ev_tstamp 1288ev_tstamp
1184ev_time (void) 1289ev_time (void) EV_THROW
1185{ 1290{
1186#if EV_USE_REALTIME 1291#if EV_USE_REALTIME
1187 if (expect_true (have_realtime)) 1292 if (expect_true (have_realtime))
1188 { 1293 {
1189 struct timespec ts; 1294 struct timespec ts;
1213 return ev_time (); 1318 return ev_time ();
1214} 1319}
1215 1320
1216#if EV_MULTIPLICITY 1321#if EV_MULTIPLICITY
1217ev_tstamp 1322ev_tstamp
1218ev_now (EV_P) 1323ev_now (EV_P) EV_THROW
1219{ 1324{
1220 return ev_rt_now; 1325 return ev_rt_now;
1221} 1326}
1222#endif 1327#endif
1223 1328
1224void 1329void
1225ev_sleep (ev_tstamp delay) 1330ev_sleep (ev_tstamp delay) EV_THROW
1226{ 1331{
1227 if (delay > 0.) 1332 if (delay > 0.)
1228 { 1333 {
1229#if EV_USE_NANOSLEEP 1334#if EV_USE_NANOSLEEP
1230 struct timespec ts; 1335 struct timespec ts;
1231 1336
1232 EV_TS_SET (ts, delay); 1337 EV_TS_SET (ts, delay);
1233 nanosleep (&ts, 0); 1338 nanosleep (&ts, 0);
1234#elif defined(_WIN32) 1339#elif defined _WIN32
1235 Sleep ((unsigned long)(delay * 1e3)); 1340 Sleep ((unsigned long)(delay * 1e3));
1236#else 1341#else
1237 struct timeval tv; 1342 struct timeval tv;
1238 1343
1239 /* 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 */
1258 1363
1259 do 1364 do
1260 ncur <<= 1; 1365 ncur <<= 1;
1261 while (cnt > ncur); 1366 while (cnt > ncur);
1262 1367
1263 /* 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 */
1264 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) 1369 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
1265 { 1370 {
1266 ncur *= elem; 1371 ncur *= elem;
1267 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);
1268 ncur = ncur - sizeof (void *) * 4; 1373 ncur = ncur - sizeof (void *) * 4;
1311pendingcb (EV_P_ ev_prepare *w, int revents) 1416pendingcb (EV_P_ ev_prepare *w, int revents)
1312{ 1417{
1313} 1418}
1314 1419
1315void noinline 1420void noinline
1316ev_feed_event (EV_P_ void *w, int revents) 1421ev_feed_event (EV_P_ void *w, int revents) EV_THROW
1317{ 1422{
1318 W w_ = (W)w; 1423 W w_ = (W)w;
1319 int pri = ABSPRI (w_); 1424 int pri = ABSPRI (w_);
1320 1425
1321 if (expect_false (w_->pending)) 1426 if (expect_false (w_->pending))
1380 if (expect_true (!anfd->reify)) 1485 if (expect_true (!anfd->reify))
1381 fd_event_nocheck (EV_A_ fd, revents); 1486 fd_event_nocheck (EV_A_ fd, revents);
1382} 1487}
1383 1488
1384void 1489void
1385ev_feed_fd_event (EV_P_ int fd, int revents) 1490ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW
1386{ 1491{
1387 if (fd >= 0 && fd < anfdmax) 1492 if (fd >= 0 && fd < anfdmax)
1388 fd_event_nocheck (EV_A_ fd, revents); 1493 fd_event_nocheck (EV_A_ fd, revents);
1389} 1494}
1390 1495
1754 1859
1755 if (pipe_write_wanted) 1860 if (pipe_write_wanted)
1756 { 1861 {
1757 int old_errno; 1862 int old_errno;
1758 1863
1759 pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ 1864 pipe_write_skipped = 0; /* just an optimisation, no fence needed */
1760 1865
1761 old_errno = errno; /* save errno because write will clobber it */ 1866 old_errno = errno; /* save errno because write will clobber it */
1762 1867
1763#if EV_USE_EVENTFD 1868#if EV_USE_EVENTFD
1764 if (evfd >= 0) 1869 if (evfd >= 0)
1772 /* win32 people keep sending patches that change this write() to send() */ 1877 /* win32 people keep sending patches that change this write() to send() */
1773 /* and then run away. but send() is wrong, it wants a socket handle on win32 */ 1878 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1774 /* so when you think this write should be a send instead, please find out */ 1879 /* so when you think this write should be a send instead, please find out */
1775 /* where your send() is from - it's definitely not the microsoft send, and */ 1880 /* where your send() is from - it's definitely not the microsoft send, and */
1776 /* tell me. thank you. */ 1881 /* tell me. thank you. */
1882 /* it might be that your problem is that your environment needs EV_USE_WSASOCKET */
1883 /* check the ev documentation on how to use this flag */
1777 write (evpipe [1], &(evpipe [1]), 1); 1884 write (evpipe [1], &(evpipe [1]), 1);
1778 } 1885 }
1779 1886
1780 errno = old_errno; 1887 errno = old_errno;
1781 } 1888 }
1834} 1941}
1835 1942
1836/*****************************************************************************/ 1943/*****************************************************************************/
1837 1944
1838void 1945void
1839ev_feed_signal (int signum) 1946ev_feed_signal (int signum) EV_THROW
1840{ 1947{
1841#if EV_MULTIPLICITY 1948#if EV_MULTIPLICITY
1842 EV_P = signals [signum - 1].loop; 1949 EV_P = signals [signum - 1].loop;
1843 1950
1844 if (!EV_A) 1951 if (!EV_A)
1861 1968
1862 ev_feed_signal (signum); 1969 ev_feed_signal (signum);
1863} 1970}
1864 1971
1865void noinline 1972void noinline
1866ev_feed_signal_event (EV_P_ int signum) 1973ev_feed_signal_event (EV_P_ int signum) EV_THROW
1867{ 1974{
1868 WL w; 1975 WL w;
1869 1976
1870 if (expect_false (signum <= 0 || signum > EV_NSIG)) 1977 if (expect_false (signum <= 0 || signum > EV_NSIG))
1871 return; 1978 return;
1987#if EV_USE_SELECT 2094#if EV_USE_SELECT
1988# include "ev_select.c" 2095# include "ev_select.c"
1989#endif 2096#endif
1990 2097
1991int ecb_cold 2098int ecb_cold
1992ev_version_major (void) 2099ev_version_major (void) EV_THROW
1993{ 2100{
1994 return EV_VERSION_MAJOR; 2101 return EV_VERSION_MAJOR;
1995} 2102}
1996 2103
1997int ecb_cold 2104int ecb_cold
1998ev_version_minor (void) 2105ev_version_minor (void) EV_THROW
1999{ 2106{
2000 return EV_VERSION_MINOR; 2107 return EV_VERSION_MINOR;
2001} 2108}
2002 2109
2003/* return true if we are running with elevated privileges and should ignore env variables */ 2110/* return true if we are running with elevated privileges and should ignore env variables */
2011 || getgid () != getegid (); 2118 || getgid () != getegid ();
2012#endif 2119#endif
2013} 2120}
2014 2121
2015unsigned int ecb_cold 2122unsigned int ecb_cold
2016ev_supported_backends (void) 2123ev_supported_backends (void) EV_THROW
2017{ 2124{
2018 unsigned int flags = 0; 2125 unsigned int flags = 0;
2019 2126
2020 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2127 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2021 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2128 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
2025 2132
2026 return flags; 2133 return flags;
2027} 2134}
2028 2135
2029unsigned int ecb_cold 2136unsigned int ecb_cold
2030ev_recommended_backends (void) 2137ev_recommended_backends (void) EV_THROW
2031{ 2138{
2032 unsigned int flags = ev_supported_backends (); 2139 unsigned int flags = ev_supported_backends ();
2033 2140
2034#ifndef __NetBSD__ 2141#ifndef __NetBSD__
2035 /* kqueue is borked on everything but netbsd apparently */ 2142 /* kqueue is borked on everything but netbsd apparently */
2047 2154
2048 return flags; 2155 return flags;
2049} 2156}
2050 2157
2051unsigned int ecb_cold 2158unsigned int ecb_cold
2052ev_embeddable_backends (void) 2159ev_embeddable_backends (void) EV_THROW
2053{ 2160{
2054 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 2161 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2055 2162
2056 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 2163 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
2057 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ 2164 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2059 2166
2060 return flags; 2167 return flags;
2061} 2168}
2062 2169
2063unsigned int 2170unsigned int
2064ev_backend (EV_P) 2171ev_backend (EV_P) EV_THROW
2065{ 2172{
2066 return backend; 2173 return backend;
2067} 2174}
2068 2175
2069#if EV_FEATURE_API 2176#if EV_FEATURE_API
2070unsigned int 2177unsigned int
2071ev_iteration (EV_P) 2178ev_iteration (EV_P) EV_THROW
2072{ 2179{
2073 return loop_count; 2180 return loop_count;
2074} 2181}
2075 2182
2076unsigned int 2183unsigned int
2077ev_depth (EV_P) 2184ev_depth (EV_P) EV_THROW
2078{ 2185{
2079 return loop_depth; 2186 return loop_depth;
2080} 2187}
2081 2188
2082void 2189void
2083ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 2190ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2084{ 2191{
2085 io_blocktime = interval; 2192 io_blocktime = interval;
2086} 2193}
2087 2194
2088void 2195void
2089ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 2196ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2090{ 2197{
2091 timeout_blocktime = interval; 2198 timeout_blocktime = interval;
2092} 2199}
2093 2200
2094void 2201void
2095ev_set_userdata (EV_P_ void *data) 2202ev_set_userdata (EV_P_ void *data) EV_THROW
2096{ 2203{
2097 userdata = data; 2204 userdata = data;
2098} 2205}
2099 2206
2100void * 2207void *
2101ev_userdata (EV_P) 2208ev_userdata (EV_P) EV_THROW
2102{ 2209{
2103 return userdata; 2210 return userdata;
2104} 2211}
2105 2212
2106void 2213void
2107ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 2214ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) EV_THROW
2108{ 2215{
2109 invoke_cb = invoke_pending_cb; 2216 invoke_cb = invoke_pending_cb;
2110} 2217}
2111 2218
2112void 2219void
2113ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) 2220ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW
2114{ 2221{
2115 release_cb = release; 2222 release_cb = release;
2116 acquire_cb = acquire; 2223 acquire_cb = acquire;
2117} 2224}
2118#endif 2225#endif
2119 2226
2120/* initialise a loop structure, must be zero-initialised */ 2227/* initialise a loop structure, must be zero-initialised */
2121static void noinline ecb_cold 2228static void noinline ecb_cold
2122loop_init (EV_P_ unsigned int flags) 2229loop_init (EV_P_ unsigned int flags) EV_THROW
2123{ 2230{
2124 if (!backend) 2231 if (!backend)
2125 { 2232 {
2126 origflags = flags; 2233 origflags = flags;
2127 2234
2380} 2487}
2381 2488
2382#if EV_MULTIPLICITY 2489#if EV_MULTIPLICITY
2383 2490
2384struct ev_loop * ecb_cold 2491struct ev_loop * ecb_cold
2385ev_loop_new (unsigned int flags) 2492ev_loop_new (unsigned int flags) EV_THROW
2386{ 2493{
2387 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2494 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2388 2495
2389 memset (EV_A, 0, sizeof (struct ev_loop)); 2496 memset (EV_A, 0, sizeof (struct ev_loop));
2390 loop_init (EV_A_ flags); 2497 loop_init (EV_A_ flags);
2434} 2541}
2435#endif 2542#endif
2436 2543
2437#if EV_FEATURE_API 2544#if EV_FEATURE_API
2438void ecb_cold 2545void ecb_cold
2439ev_verify (EV_P) 2546ev_verify (EV_P) EV_THROW
2440{ 2547{
2441#if EV_VERIFY 2548#if EV_VERIFY
2442 int i; 2549 int i;
2443 WL w; 2550 WL w;
2444 2551
2513#if EV_MULTIPLICITY 2620#if EV_MULTIPLICITY
2514struct ev_loop * ecb_cold 2621struct ev_loop * ecb_cold
2515#else 2622#else
2516int 2623int
2517#endif 2624#endif
2518ev_default_loop (unsigned int flags) 2625ev_default_loop (unsigned int flags) EV_THROW
2519{ 2626{
2520 if (!ev_default_loop_ptr) 2627 if (!ev_default_loop_ptr)
2521 { 2628 {
2522#if EV_MULTIPLICITY 2629#if EV_MULTIPLICITY
2523 EV_P = ev_default_loop_ptr = &default_loop_struct; 2630 EV_P = ev_default_loop_ptr = &default_loop_struct;
2542 2649
2543 return ev_default_loop_ptr; 2650 return ev_default_loop_ptr;
2544} 2651}
2545 2652
2546void 2653void
2547ev_loop_fork (EV_P) 2654ev_loop_fork (EV_P) EV_THROW
2548{ 2655{
2549 postfork = 1; /* must be in line with ev_default_fork */ 2656 postfork = 1; /* must be in line with ev_default_fork */
2550} 2657}
2551 2658
2552/*****************************************************************************/ 2659/*****************************************************************************/
2556{ 2663{
2557 EV_CB_INVOKE ((W)w, revents); 2664 EV_CB_INVOKE ((W)w, revents);
2558} 2665}
2559 2666
2560unsigned int 2667unsigned int
2561ev_pending_count (EV_P) 2668ev_pending_count (EV_P) EV_THROW
2562{ 2669{
2563 int pri; 2670 int pri;
2564 unsigned int count = 0; 2671 unsigned int count = 0;
2565 2672
2566 for (pri = NUMPRI; pri--; ) 2673 for (pri = NUMPRI; pri--; )
2825 2932
2826 mn_now = ev_rt_now; 2933 mn_now = ev_rt_now;
2827 } 2934 }
2828} 2935}
2829 2936
2830void 2937int
2831ev_run (EV_P_ int flags) 2938ev_run (EV_P_ int flags)
2832{ 2939{
2833#if EV_FEATURE_API 2940#if EV_FEATURE_API
2834 ++loop_depth; 2941 ++loop_depth;
2835#endif 2942#endif
2948#endif 3055#endif
2949 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ 3056 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2950 backend_poll (EV_A_ waittime); 3057 backend_poll (EV_A_ waittime);
2951 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 3058 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2952 3059
2953 pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ 3060 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
2954 3061
2955 if (pipe_write_skipped) 3062 if (pipe_write_skipped)
2956 { 3063 {
2957 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); 3064 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
2958 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3065 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
2992 loop_done = EVBREAK_CANCEL; 3099 loop_done = EVBREAK_CANCEL;
2993 3100
2994#if EV_FEATURE_API 3101#if EV_FEATURE_API
2995 --loop_depth; 3102 --loop_depth;
2996#endif 3103#endif
3104
3105 return activecnt;
2997} 3106}
2998 3107
2999void 3108void
3000ev_break (EV_P_ int how) 3109ev_break (EV_P_ int how) EV_THROW
3001{ 3110{
3002 loop_done = how; 3111 loop_done = how;
3003} 3112}
3004 3113
3005void 3114void
3006ev_ref (EV_P) 3115ev_ref (EV_P) EV_THROW
3007{ 3116{
3008 ++activecnt; 3117 ++activecnt;
3009} 3118}
3010 3119
3011void 3120void
3012ev_unref (EV_P) 3121ev_unref (EV_P) EV_THROW
3013{ 3122{
3014 --activecnt; 3123 --activecnt;
3015} 3124}
3016 3125
3017void 3126void
3018ev_now_update (EV_P) 3127ev_now_update (EV_P) EV_THROW
3019{ 3128{
3020 time_update (EV_A_ 1e100); 3129 time_update (EV_A_ 1e100);
3021} 3130}
3022 3131
3023void 3132void
3024ev_suspend (EV_P) 3133ev_suspend (EV_P) EV_THROW
3025{ 3134{
3026 ev_now_update (EV_A); 3135 ev_now_update (EV_A);
3027} 3136}
3028 3137
3029void 3138void
3030ev_resume (EV_P) 3139ev_resume (EV_P) EV_THROW
3031{ 3140{
3032 ev_tstamp mn_prev = mn_now; 3141 ev_tstamp mn_prev = mn_now;
3033 3142
3034 ev_now_update (EV_A); 3143 ev_now_update (EV_A);
3035 timers_reschedule (EV_A_ mn_now - mn_prev); 3144 timers_reschedule (EV_A_ mn_now - mn_prev);
3074 w->pending = 0; 3183 w->pending = 0;
3075 } 3184 }
3076} 3185}
3077 3186
3078int 3187int
3079ev_clear_pending (EV_P_ void *w) 3188ev_clear_pending (EV_P_ void *w) EV_THROW
3080{ 3189{
3081 W w_ = (W)w; 3190 W w_ = (W)w;
3082 int pending = w_->pending; 3191 int pending = w_->pending;
3083 3192
3084 if (expect_true (pending)) 3193 if (expect_true (pending))
3117} 3226}
3118 3227
3119/*****************************************************************************/ 3228/*****************************************************************************/
3120 3229
3121void noinline 3230void noinline
3122ev_io_start (EV_P_ ev_io *w) 3231ev_io_start (EV_P_ ev_io *w) EV_THROW
3123{ 3232{
3124 int fd = w->fd; 3233 int fd = w->fd;
3125 3234
3126 if (expect_false (ev_is_active (w))) 3235 if (expect_false (ev_is_active (w)))
3127 return; 3236 return;
3140 3249
3141 EV_FREQUENT_CHECK; 3250 EV_FREQUENT_CHECK;
3142} 3251}
3143 3252
3144void noinline 3253void noinline
3145ev_io_stop (EV_P_ ev_io *w) 3254ev_io_stop (EV_P_ ev_io *w) EV_THROW
3146{ 3255{
3147 clear_pending (EV_A_ (W)w); 3256 clear_pending (EV_A_ (W)w);
3148 if (expect_false (!ev_is_active (w))) 3257 if (expect_false (!ev_is_active (w)))
3149 return; 3258 return;
3150 3259
3159 3268
3160 EV_FREQUENT_CHECK; 3269 EV_FREQUENT_CHECK;
3161} 3270}
3162 3271
3163void noinline 3272void noinline
3164ev_timer_start (EV_P_ ev_timer *w) 3273ev_timer_start (EV_P_ ev_timer *w) EV_THROW
3165{ 3274{
3166 if (expect_false (ev_is_active (w))) 3275 if (expect_false (ev_is_active (w)))
3167 return; 3276 return;
3168 3277
3169 ev_at (w) += mn_now; 3278 ev_at (w) += mn_now;
3183 3292
3184 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ 3293 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
3185} 3294}
3186 3295
3187void noinline 3296void noinline
3188ev_timer_stop (EV_P_ ev_timer *w) 3297ev_timer_stop (EV_P_ ev_timer *w) EV_THROW
3189{ 3298{
3190 clear_pending (EV_A_ (W)w); 3299 clear_pending (EV_A_ (W)w);
3191 if (expect_false (!ev_is_active (w))) 3300 if (expect_false (!ev_is_active (w)))
3192 return; 3301 return;
3193 3302
3213 3322
3214 EV_FREQUENT_CHECK; 3323 EV_FREQUENT_CHECK;
3215} 3324}
3216 3325
3217void noinline 3326void noinline
3218ev_timer_again (EV_P_ ev_timer *w) 3327ev_timer_again (EV_P_ ev_timer *w) EV_THROW
3219{ 3328{
3220 EV_FREQUENT_CHECK; 3329 EV_FREQUENT_CHECK;
3330
3331 clear_pending (EV_A_ (W)w);
3221 3332
3222 if (ev_is_active (w)) 3333 if (ev_is_active (w))
3223 { 3334 {
3224 if (w->repeat) 3335 if (w->repeat)
3225 { 3336 {
3238 3349
3239 EV_FREQUENT_CHECK; 3350 EV_FREQUENT_CHECK;
3240} 3351}
3241 3352
3242ev_tstamp 3353ev_tstamp
3243ev_timer_remaining (EV_P_ ev_timer *w) 3354ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW
3244{ 3355{
3245 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 3356 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
3246} 3357}
3247 3358
3248#if EV_PERIODIC_ENABLE 3359#if EV_PERIODIC_ENABLE
3249void noinline 3360void noinline
3250ev_periodic_start (EV_P_ ev_periodic *w) 3361ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW
3251{ 3362{
3252 if (expect_false (ev_is_active (w))) 3363 if (expect_false (ev_is_active (w)))
3253 return; 3364 return;
3254 3365
3255 if (w->reschedule_cb) 3366 if (w->reschedule_cb)
3275 3386
3276 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ 3387 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
3277} 3388}
3278 3389
3279void noinline 3390void noinline
3280ev_periodic_stop (EV_P_ ev_periodic *w) 3391ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW
3281{ 3392{
3282 clear_pending (EV_A_ (W)w); 3393 clear_pending (EV_A_ (W)w);
3283 if (expect_false (!ev_is_active (w))) 3394 if (expect_false (!ev_is_active (w)))
3284 return; 3395 return;
3285 3396
3303 3414
3304 EV_FREQUENT_CHECK; 3415 EV_FREQUENT_CHECK;
3305} 3416}
3306 3417
3307void noinline 3418void noinline
3308ev_periodic_again (EV_P_ ev_periodic *w) 3419ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW
3309{ 3420{
3310 /* TODO: use adjustheap and recalculation */ 3421 /* TODO: use adjustheap and recalculation */
3311 ev_periodic_stop (EV_A_ w); 3422 ev_periodic_stop (EV_A_ w);
3312 ev_periodic_start (EV_A_ w); 3423 ev_periodic_start (EV_A_ w);
3313} 3424}
3318#endif 3429#endif
3319 3430
3320#if EV_SIGNAL_ENABLE 3431#if EV_SIGNAL_ENABLE
3321 3432
3322void noinline 3433void noinline
3323ev_signal_start (EV_P_ ev_signal *w) 3434ev_signal_start (EV_P_ ev_signal *w) EV_THROW
3324{ 3435{
3325 if (expect_false (ev_is_active (w))) 3436 if (expect_false (ev_is_active (w)))
3326 return; 3437 return;
3327 3438
3328 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 3439 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
3399 3510
3400 EV_FREQUENT_CHECK; 3511 EV_FREQUENT_CHECK;
3401} 3512}
3402 3513
3403void noinline 3514void noinline
3404ev_signal_stop (EV_P_ ev_signal *w) 3515ev_signal_stop (EV_P_ ev_signal *w) EV_THROW
3405{ 3516{
3406 clear_pending (EV_A_ (W)w); 3517 clear_pending (EV_A_ (W)w);
3407 if (expect_false (!ev_is_active (w))) 3518 if (expect_false (!ev_is_active (w)))
3408 return; 3519 return;
3409 3520
3440#endif 3551#endif
3441 3552
3442#if EV_CHILD_ENABLE 3553#if EV_CHILD_ENABLE
3443 3554
3444void 3555void
3445ev_child_start (EV_P_ ev_child *w) 3556ev_child_start (EV_P_ ev_child *w) EV_THROW
3446{ 3557{
3447#if EV_MULTIPLICITY 3558#if EV_MULTIPLICITY
3448 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 3559 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
3449#endif 3560#endif
3450 if (expect_false (ev_is_active (w))) 3561 if (expect_false (ev_is_active (w)))
3457 3568
3458 EV_FREQUENT_CHECK; 3569 EV_FREQUENT_CHECK;
3459} 3570}
3460 3571
3461void 3572void
3462ev_child_stop (EV_P_ ev_child *w) 3573ev_child_stop (EV_P_ ev_child *w) EV_THROW
3463{ 3574{
3464 clear_pending (EV_A_ (W)w); 3575 clear_pending (EV_A_ (W)w);
3465 if (expect_false (!ev_is_active (w))) 3576 if (expect_false (!ev_is_active (w)))
3466 return; 3577 return;
3467 3578
3634} 3745}
3635 3746
3636inline_size int 3747inline_size int
3637infy_newfd (void) 3748infy_newfd (void)
3638{ 3749{
3639#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) 3750#if defined IN_CLOEXEC && defined IN_NONBLOCK
3640 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); 3751 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3641 if (fd >= 0) 3752 if (fd >= 0)
3642 return fd; 3753 return fd;
3643#endif 3754#endif
3644 return inotify_init (); 3755 return inotify_init ();
3719#else 3830#else
3720# define EV_LSTAT(p,b) lstat (p, b) 3831# define EV_LSTAT(p,b) lstat (p, b)
3721#endif 3832#endif
3722 3833
3723void 3834void
3724ev_stat_stat (EV_P_ ev_stat *w) 3835ev_stat_stat (EV_P_ ev_stat *w) EV_THROW
3725{ 3836{
3726 if (lstat (w->path, &w->attr) < 0) 3837 if (lstat (w->path, &w->attr) < 0)
3727 w->attr.st_nlink = 0; 3838 w->attr.st_nlink = 0;
3728 else if (!w->attr.st_nlink) 3839 else if (!w->attr.st_nlink)
3729 w->attr.st_nlink = 1; 3840 w->attr.st_nlink = 1;
3768 ev_feed_event (EV_A_ w, EV_STAT); 3879 ev_feed_event (EV_A_ w, EV_STAT);
3769 } 3880 }
3770} 3881}
3771 3882
3772void 3883void
3773ev_stat_start (EV_P_ ev_stat *w) 3884ev_stat_start (EV_P_ ev_stat *w) EV_THROW
3774{ 3885{
3775 if (expect_false (ev_is_active (w))) 3886 if (expect_false (ev_is_active (w)))
3776 return; 3887 return;
3777 3888
3778 ev_stat_stat (EV_A_ w); 3889 ev_stat_stat (EV_A_ w);
3799 3910
3800 EV_FREQUENT_CHECK; 3911 EV_FREQUENT_CHECK;
3801} 3912}
3802 3913
3803void 3914void
3804ev_stat_stop (EV_P_ ev_stat *w) 3915ev_stat_stop (EV_P_ ev_stat *w) EV_THROW
3805{ 3916{
3806 clear_pending (EV_A_ (W)w); 3917 clear_pending (EV_A_ (W)w);
3807 if (expect_false (!ev_is_active (w))) 3918 if (expect_false (!ev_is_active (w)))
3808 return; 3919 return;
3809 3920
3825} 3936}
3826#endif 3937#endif
3827 3938
3828#if EV_IDLE_ENABLE 3939#if EV_IDLE_ENABLE
3829void 3940void
3830ev_idle_start (EV_P_ ev_idle *w) 3941ev_idle_start (EV_P_ ev_idle *w) EV_THROW
3831{ 3942{
3832 if (expect_false (ev_is_active (w))) 3943 if (expect_false (ev_is_active (w)))
3833 return; 3944 return;
3834 3945
3835 pri_adjust (EV_A_ (W)w); 3946 pri_adjust (EV_A_ (W)w);
3848 3959
3849 EV_FREQUENT_CHECK; 3960 EV_FREQUENT_CHECK;
3850} 3961}
3851 3962
3852void 3963void
3853ev_idle_stop (EV_P_ ev_idle *w) 3964ev_idle_stop (EV_P_ ev_idle *w) EV_THROW
3854{ 3965{
3855 clear_pending (EV_A_ (W)w); 3966 clear_pending (EV_A_ (W)w);
3856 if (expect_false (!ev_is_active (w))) 3967 if (expect_false (!ev_is_active (w)))
3857 return; 3968 return;
3858 3969
3872} 3983}
3873#endif 3984#endif
3874 3985
3875#if EV_PREPARE_ENABLE 3986#if EV_PREPARE_ENABLE
3876void 3987void
3877ev_prepare_start (EV_P_ ev_prepare *w) 3988ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW
3878{ 3989{
3879 if (expect_false (ev_is_active (w))) 3990 if (expect_false (ev_is_active (w)))
3880 return; 3991 return;
3881 3992
3882 EV_FREQUENT_CHECK; 3993 EV_FREQUENT_CHECK;
3887 3998
3888 EV_FREQUENT_CHECK; 3999 EV_FREQUENT_CHECK;
3889} 4000}
3890 4001
3891void 4002void
3892ev_prepare_stop (EV_P_ ev_prepare *w) 4003ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW
3893{ 4004{
3894 clear_pending (EV_A_ (W)w); 4005 clear_pending (EV_A_ (W)w);
3895 if (expect_false (!ev_is_active (w))) 4006 if (expect_false (!ev_is_active (w)))
3896 return; 4007 return;
3897 4008
3910} 4021}
3911#endif 4022#endif
3912 4023
3913#if EV_CHECK_ENABLE 4024#if EV_CHECK_ENABLE
3914void 4025void
3915ev_check_start (EV_P_ ev_check *w) 4026ev_check_start (EV_P_ ev_check *w) EV_THROW
3916{ 4027{
3917 if (expect_false (ev_is_active (w))) 4028 if (expect_false (ev_is_active (w)))
3918 return; 4029 return;
3919 4030
3920 EV_FREQUENT_CHECK; 4031 EV_FREQUENT_CHECK;
3925 4036
3926 EV_FREQUENT_CHECK; 4037 EV_FREQUENT_CHECK;
3927} 4038}
3928 4039
3929void 4040void
3930ev_check_stop (EV_P_ ev_check *w) 4041ev_check_stop (EV_P_ ev_check *w) EV_THROW
3931{ 4042{
3932 clear_pending (EV_A_ (W)w); 4043 clear_pending (EV_A_ (W)w);
3933 if (expect_false (!ev_is_active (w))) 4044 if (expect_false (!ev_is_active (w)))
3934 return; 4045 return;
3935 4046
3948} 4059}
3949#endif 4060#endif
3950 4061
3951#if EV_EMBED_ENABLE 4062#if EV_EMBED_ENABLE
3952void noinline 4063void noinline
3953ev_embed_sweep (EV_P_ ev_embed *w) 4064ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW
3954{ 4065{
3955 ev_run (w->other, EVRUN_NOWAIT); 4066 ev_run (w->other, EVRUN_NOWAIT);
3956} 4067}
3957 4068
3958static void 4069static void
4006 ev_idle_stop (EV_A_ idle); 4117 ev_idle_stop (EV_A_ idle);
4007} 4118}
4008#endif 4119#endif
4009 4120
4010void 4121void
4011ev_embed_start (EV_P_ ev_embed *w) 4122ev_embed_start (EV_P_ ev_embed *w) EV_THROW
4012{ 4123{
4013 if (expect_false (ev_is_active (w))) 4124 if (expect_false (ev_is_active (w)))
4014 return; 4125 return;
4015 4126
4016 { 4127 {
4037 4148
4038 EV_FREQUENT_CHECK; 4149 EV_FREQUENT_CHECK;
4039} 4150}
4040 4151
4041void 4152void
4042ev_embed_stop (EV_P_ ev_embed *w) 4153ev_embed_stop (EV_P_ ev_embed *w) EV_THROW
4043{ 4154{
4044 clear_pending (EV_A_ (W)w); 4155 clear_pending (EV_A_ (W)w);
4045 if (expect_false (!ev_is_active (w))) 4156 if (expect_false (!ev_is_active (w)))
4046 return; 4157 return;
4047 4158
4057} 4168}
4058#endif 4169#endif
4059 4170
4060#if EV_FORK_ENABLE 4171#if EV_FORK_ENABLE
4061void 4172void
4062ev_fork_start (EV_P_ ev_fork *w) 4173ev_fork_start (EV_P_ ev_fork *w) EV_THROW
4063{ 4174{
4064 if (expect_false (ev_is_active (w))) 4175 if (expect_false (ev_is_active (w)))
4065 return; 4176 return;
4066 4177
4067 EV_FREQUENT_CHECK; 4178 EV_FREQUENT_CHECK;
4072 4183
4073 EV_FREQUENT_CHECK; 4184 EV_FREQUENT_CHECK;
4074} 4185}
4075 4186
4076void 4187void
4077ev_fork_stop (EV_P_ ev_fork *w) 4188ev_fork_stop (EV_P_ ev_fork *w) EV_THROW
4078{ 4189{
4079 clear_pending (EV_A_ (W)w); 4190 clear_pending (EV_A_ (W)w);
4080 if (expect_false (!ev_is_active (w))) 4191 if (expect_false (!ev_is_active (w)))
4081 return; 4192 return;
4082 4193
4095} 4206}
4096#endif 4207#endif
4097 4208
4098#if EV_CLEANUP_ENABLE 4209#if EV_CLEANUP_ENABLE
4099void 4210void
4100ev_cleanup_start (EV_P_ ev_cleanup *w) 4211ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW
4101{ 4212{
4102 if (expect_false (ev_is_active (w))) 4213 if (expect_false (ev_is_active (w)))
4103 return; 4214 return;
4104 4215
4105 EV_FREQUENT_CHECK; 4216 EV_FREQUENT_CHECK;
4112 ev_unref (EV_A); 4223 ev_unref (EV_A);
4113 EV_FREQUENT_CHECK; 4224 EV_FREQUENT_CHECK;
4114} 4225}
4115 4226
4116void 4227void
4117ev_cleanup_stop (EV_P_ ev_cleanup *w) 4228ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW
4118{ 4229{
4119 clear_pending (EV_A_ (W)w); 4230 clear_pending (EV_A_ (W)w);
4120 if (expect_false (!ev_is_active (w))) 4231 if (expect_false (!ev_is_active (w)))
4121 return; 4232 return;
4122 4233
4136} 4247}
4137#endif 4248#endif
4138 4249
4139#if EV_ASYNC_ENABLE 4250#if EV_ASYNC_ENABLE
4140void 4251void
4141ev_async_start (EV_P_ ev_async *w) 4252ev_async_start (EV_P_ ev_async *w) EV_THROW
4142{ 4253{
4143 if (expect_false (ev_is_active (w))) 4254 if (expect_false (ev_is_active (w)))
4144 return; 4255 return;
4145 4256
4146 w->sent = 0; 4257 w->sent = 0;
4155 4266
4156 EV_FREQUENT_CHECK; 4267 EV_FREQUENT_CHECK;
4157} 4268}
4158 4269
4159void 4270void
4160ev_async_stop (EV_P_ ev_async *w) 4271ev_async_stop (EV_P_ ev_async *w) EV_THROW
4161{ 4272{
4162 clear_pending (EV_A_ (W)w); 4273 clear_pending (EV_A_ (W)w);
4163 if (expect_false (!ev_is_active (w))) 4274 if (expect_false (!ev_is_active (w)))
4164 return; 4275 return;
4165 4276
4176 4287
4177 EV_FREQUENT_CHECK; 4288 EV_FREQUENT_CHECK;
4178} 4289}
4179 4290
4180void 4291void
4181ev_async_send (EV_P_ ev_async *w) 4292ev_async_send (EV_P_ ev_async *w) EV_THROW
4182{ 4293{
4183 w->sent = 1; 4294 w->sent = 1;
4184 evpipe_write (EV_A_ &async_pending); 4295 evpipe_write (EV_A_ &async_pending);
4185} 4296}
4186#endif 4297#endif
4223 4334
4224 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); 4335 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
4225} 4336}
4226 4337
4227void 4338void
4228ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 4339ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW
4229{ 4340{
4230 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 4341 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
4231 4342
4232 if (expect_false (!once)) 4343 if (expect_false (!once))
4233 { 4344 {
4255 4366
4256/*****************************************************************************/ 4367/*****************************************************************************/
4257 4368
4258#if EV_WALK_ENABLE 4369#if EV_WALK_ENABLE
4259void ecb_cold 4370void ecb_cold
4260ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 4371ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW
4261{ 4372{
4262 int i, j; 4373 int i, j;
4263 ev_watcher_list *wl, *wn; 4374 ev_watcher_list *wl, *wn;
4264 4375
4265 if (types & (EV_IO | EV_EMBED)) 4376 if (types & (EV_IO | EV_EMBED))
4371 4482
4372#if EV_MULTIPLICITY 4483#if EV_MULTIPLICITY
4373 #include "ev_wrap.h" 4484 #include "ev_wrap.h"
4374#endif 4485#endif
4375 4486
4376EV_CPP(})
4377

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines