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

Comparing libev/ev.c (file contents):
Revision 1.398 by root, Sun Sep 25 21:27:35 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#endif 556#endif
544 557
545#ifndef ECB_MEMORY_FENCE 558#ifndef ECB_MEMORY_FENCE
546 #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || defined(__clang__) 559 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
547 #if __i386__ 560 #if __i386 || __i386__
548 #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")
549 #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 */
550 #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 */
551 #elif __amd64 564 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__
552 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") 565 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
553 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") 566 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory")
554 #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 */
555 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ 568 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
556 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 569 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
557 #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ 570 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
558 || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) 571 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__
559 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") 572 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
560 #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ 573 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
561 || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) 574 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__
562 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") 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")
563 #endif 586 #endif
564 #endif 587 #endif
565#endif 588#endif
566 589
567#ifndef ECB_MEMORY_FENCE 590#ifndef ECB_MEMORY_FENCE
568 #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) 591 #if ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
569 #define ECB_MEMORY_FENCE __sync_synchronize () 592 #define ECB_MEMORY_FENCE __sync_synchronize ()
570 /*#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); }) */
571 /*#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 ); }) */
572 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 595 #elif _MSC_VER >= 1400 /* VC++ 2005 */
573 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 596 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
574 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 597 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
575 #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 */
576 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 599 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
577 #elif defined(_WIN32) 600 #elif defined _WIN32
578 #include <WinNT.h> 601 #include <WinNT.h>
579 #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 ()
580 #endif 610 #endif
581#endif 611#endif
582 612
583#ifndef ECB_MEMORY_FENCE 613#ifndef ECB_MEMORY_FENCE
584 #if !ECB_AVOID_PTHREADS 614 #if !ECB_AVOID_PTHREADS
596 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; 626 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
597 #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)
598 #endif 628 #endif
599#endif 629#endif
600 630
601#if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) 631#if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
602 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 632 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
603#endif 633#endif
604 634
605#if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) 635#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
606 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 636 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
607#endif 637#endif
608 638
609/*****************************************************************************/ 639/*****************************************************************************/
610 640
759 789
760 return r + ecb_ld32 (x); 790 return r + ecb_ld32 (x);
761 } 791 }
762#endif 792#endif
763 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
764/* popcount64 is only available on 64 bit cpus as gcc builtin */ 824/* popcount64 is only available on 64 bit cpus as gcc builtin */
765/* so for this version we are lazy */ 825/* so for this version we are lazy */
766ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; 826ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const;
767ecb_function_ int 827ecb_function_ int
768ecb_popcount64 (uint64_t x) 828ecb_popcount64 (uint64_t x)
817 877
818#if ECB_GCC_VERSION(4,5) 878#if ECB_GCC_VERSION(4,5)
819 #define ecb_unreachable() __builtin_unreachable () 879 #define ecb_unreachable() __builtin_unreachable ()
820#else 880#else
821 /* 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 :/ */
822 ecb_function_ void ecb_unreachable (void) ecb_noreturn; 882 ecb_inline void ecb_unreachable (void) ecb_noreturn;
823 ecb_function_ void ecb_unreachable (void) { } 883 ecb_inline void ecb_unreachable (void) { }
824#endif 884#endif
825 885
826/* try to tell the compiler that some condition is definitely true */ 886/* try to tell the compiler that some condition is definitely true */
827#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 887#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
828 888
829ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; 889ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
830ecb_function_ unsigned char 890ecb_inline unsigned char
831ecb_byteorder_helper (void) 891ecb_byteorder_helper (void)
832{ 892{
833 const uint32_t u = 0x11223344; 893 const uint32_t u = 0x11223344;
834 return *(unsigned char *)&u; 894 return *(unsigned char *)&u;
835} 895}
836 896
837ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; 897ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
838ecb_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; }
839ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; 899ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
840ecb_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; }
841 901
842#if ECB_GCC_VERSION(3,0) || ECB_C99 902#if ECB_GCC_VERSION(3,0) || ECB_C99
843 #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))
844#else 904#else
845 #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)))
878 938
879#if ECB_MEMORY_FENCE_NEEDS_PTHREADS 939#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
880/* if your architecture doesn't need memory fences, e.g. because it is 940/* if your architecture doesn't need memory fences, e.g. because it is
881 * single-cpu/core, or if you use libev in a project that doesn't use libev 941 * single-cpu/core, or if you use libev in a project that doesn't use libev
882 * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling 942 * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling
883 * libev, in which casess the memory fences become nops. 943 * libev, in which cases the memory fences become nops.
884 * alternatively, you can remove this #error and link against libpthread, 944 * alternatively, you can remove this #error and link against libpthread,
885 * which will then provide the memory fences. 945 * which will then provide the memory fences.
886 */ 946 */
887# error "memory fences not defined for your architecture, please report" 947# error "memory fences not defined for your architecture, please report"
888#endif 948#endif
1045{ 1105{
1046 write (STDERR_FILENO, msg, strlen (msg)); 1106 write (STDERR_FILENO, msg, strlen (msg));
1047} 1107}
1048#endif 1108#endif
1049 1109
1050static void (*syserr_cb)(const char *msg); 1110static void (*syserr_cb)(const char *msg) EV_THROW;
1051 1111
1052void ecb_cold 1112void ecb_cold
1053ev_set_syserr_cb (void (*cb)(const char *msg)) 1113ev_set_syserr_cb (void (*cb)(const char *msg)) EV_THROW
1054{ 1114{
1055 syserr_cb = cb; 1115 syserr_cb = cb;
1056} 1116}
1057 1117
1058static void noinline ecb_cold 1118static void noinline ecb_cold
1094 free (ptr); 1154 free (ptr);
1095 return 0; 1155 return 0;
1096#endif 1156#endif
1097} 1157}
1098 1158
1099static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 1159static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1100 1160
1101void ecb_cold 1161void ecb_cold
1102ev_set_allocator (void *(*cb)(void *ptr, long size)) 1162ev_set_allocator (void *(*cb)(void *ptr, long size)) EV_THROW
1103{ 1163{
1104 alloc = cb; 1164 alloc = cb;
1105} 1165}
1106 1166
1107inline_speed void * 1167inline_speed void *
1195 #undef VAR 1255 #undef VAR
1196 }; 1256 };
1197 #include "ev_wrap.h" 1257 #include "ev_wrap.h"
1198 1258
1199 static struct ev_loop default_loop_struct; 1259 static struct ev_loop default_loop_struct;
1200 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 */
1201 1261
1202#else 1262#else
1203 1263
1204 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 */
1205 #define VAR(name,decl) static decl; 1265 #define VAR(name,decl) static decl;
1206 #include "ev_vars.h" 1266 #include "ev_vars.h"
1207 #undef VAR 1267 #undef VAR
1208 1268
1209 static int ev_default_loop_ptr; 1269 static int ev_default_loop_ptr;
1224 1284
1225/*****************************************************************************/ 1285/*****************************************************************************/
1226 1286
1227#ifndef EV_HAVE_EV_TIME 1287#ifndef EV_HAVE_EV_TIME
1228ev_tstamp 1288ev_tstamp
1229ev_time (void) 1289ev_time (void) EV_THROW
1230{ 1290{
1231#if EV_USE_REALTIME 1291#if EV_USE_REALTIME
1232 if (expect_true (have_realtime)) 1292 if (expect_true (have_realtime))
1233 { 1293 {
1234 struct timespec ts; 1294 struct timespec ts;
1258 return ev_time (); 1318 return ev_time ();
1259} 1319}
1260 1320
1261#if EV_MULTIPLICITY 1321#if EV_MULTIPLICITY
1262ev_tstamp 1322ev_tstamp
1263ev_now (EV_P) 1323ev_now (EV_P) EV_THROW
1264{ 1324{
1265 return ev_rt_now; 1325 return ev_rt_now;
1266} 1326}
1267#endif 1327#endif
1268 1328
1269void 1329void
1270ev_sleep (ev_tstamp delay) 1330ev_sleep (ev_tstamp delay) EV_THROW
1271{ 1331{
1272 if (delay > 0.) 1332 if (delay > 0.)
1273 { 1333 {
1274#if EV_USE_NANOSLEEP 1334#if EV_USE_NANOSLEEP
1275 struct timespec ts; 1335 struct timespec ts;
1276 1336
1277 EV_TS_SET (ts, delay); 1337 EV_TS_SET (ts, delay);
1278 nanosleep (&ts, 0); 1338 nanosleep (&ts, 0);
1279#elif defined(_WIN32) 1339#elif defined _WIN32
1280 Sleep ((unsigned long)(delay * 1e3)); 1340 Sleep ((unsigned long)(delay * 1e3));
1281#else 1341#else
1282 struct timeval tv; 1342 struct timeval tv;
1283 1343
1284 /* 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 */
1303 1363
1304 do 1364 do
1305 ncur <<= 1; 1365 ncur <<= 1;
1306 while (cnt > ncur); 1366 while (cnt > ncur);
1307 1367
1308 /* 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 */
1309 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) 1369 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
1310 { 1370 {
1311 ncur *= elem; 1371 ncur *= elem;
1312 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);
1313 ncur = ncur - sizeof (void *) * 4; 1373 ncur = ncur - sizeof (void *) * 4;
1356pendingcb (EV_P_ ev_prepare *w, int revents) 1416pendingcb (EV_P_ ev_prepare *w, int revents)
1357{ 1417{
1358} 1418}
1359 1419
1360void noinline 1420void noinline
1361ev_feed_event (EV_P_ void *w, int revents) 1421ev_feed_event (EV_P_ void *w, int revents) EV_THROW
1362{ 1422{
1363 W w_ = (W)w; 1423 W w_ = (W)w;
1364 int pri = ABSPRI (w_); 1424 int pri = ABSPRI (w_);
1365 1425
1366 if (expect_false (w_->pending)) 1426 if (expect_false (w_->pending))
1425 if (expect_true (!anfd->reify)) 1485 if (expect_true (!anfd->reify))
1426 fd_event_nocheck (EV_A_ fd, revents); 1486 fd_event_nocheck (EV_A_ fd, revents);
1427} 1487}
1428 1488
1429void 1489void
1430ev_feed_fd_event (EV_P_ int fd, int revents) 1490ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW
1431{ 1491{
1432 if (fd >= 0 && fd < anfdmax) 1492 if (fd >= 0 && fd < anfdmax)
1433 fd_event_nocheck (EV_A_ fd, revents); 1493 fd_event_nocheck (EV_A_ fd, revents);
1434} 1494}
1435 1495
1784} 1844}
1785 1845
1786inline_speed void 1846inline_speed void
1787evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1847evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1788{ 1848{
1849 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
1850
1789 if (expect_true (*flag)) 1851 if (expect_true (*flag))
1790 return; 1852 return;
1791 1853
1792 *flag = 1; 1854 *flag = 1;
1793 1855
1817 /* win32 people keep sending patches that change this write() to send() */ 1879 /* win32 people keep sending patches that change this write() to send() */
1818 /* 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 */
1819 /* 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 */
1820 /* 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 */
1821 /* 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 */
1822 write (evpipe [1], &(evpipe [1]), 1); 1886 write (evpipe [1], &(evpipe [1]), 1);
1823 } 1887 }
1824 1888
1825 errno = old_errno; 1889 errno = old_errno;
1826 } 1890 }
1850 } 1914 }
1851 } 1915 }
1852 1916
1853 pipe_write_skipped = 0; 1917 pipe_write_skipped = 0;
1854 1918
1919 ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
1920
1855#if EV_SIGNAL_ENABLE 1921#if EV_SIGNAL_ENABLE
1856 if (sig_pending) 1922 if (sig_pending)
1857 { 1923 {
1858 sig_pending = 0; 1924 sig_pending = 0;
1925
1926 ECB_MEMORY_FENCE_RELEASE;
1859 1927
1860 for (i = EV_NSIG - 1; i--; ) 1928 for (i = EV_NSIG - 1; i--; )
1861 if (expect_false (signals [i].pending)) 1929 if (expect_false (signals [i].pending))
1862 ev_feed_signal_event (EV_A_ i + 1); 1930 ev_feed_signal_event (EV_A_ i + 1);
1863 } 1931 }
1865 1933
1866#if EV_ASYNC_ENABLE 1934#if EV_ASYNC_ENABLE
1867 if (async_pending) 1935 if (async_pending)
1868 { 1936 {
1869 async_pending = 0; 1937 async_pending = 0;
1938
1939 ECB_MEMORY_FENCE_RELEASE;
1870 1940
1871 for (i = asynccnt; i--; ) 1941 for (i = asynccnt; i--; )
1872 if (asyncs [i]->sent) 1942 if (asyncs [i]->sent)
1873 { 1943 {
1874 asyncs [i]->sent = 0; 1944 asyncs [i]->sent = 0;
1879} 1949}
1880 1950
1881/*****************************************************************************/ 1951/*****************************************************************************/
1882 1952
1883void 1953void
1884ev_feed_signal (int signum) 1954ev_feed_signal (int signum) EV_THROW
1885{ 1955{
1886#if EV_MULTIPLICITY 1956#if EV_MULTIPLICITY
1887 EV_P = signals [signum - 1].loop; 1957 EV_P = signals [signum - 1].loop;
1888 1958
1889 if (!EV_A) 1959 if (!EV_A)
1906 1976
1907 ev_feed_signal (signum); 1977 ev_feed_signal (signum);
1908} 1978}
1909 1979
1910void noinline 1980void noinline
1911ev_feed_signal_event (EV_P_ int signum) 1981ev_feed_signal_event (EV_P_ int signum) EV_THROW
1912{ 1982{
1913 WL w; 1983 WL w;
1914 1984
1915 if (expect_false (signum <= 0 || signum > EV_NSIG)) 1985 if (expect_false (signum <= 0 || signum > EV_NSIG))
1916 return; 1986 return;
2032#if EV_USE_SELECT 2102#if EV_USE_SELECT
2033# include "ev_select.c" 2103# include "ev_select.c"
2034#endif 2104#endif
2035 2105
2036int ecb_cold 2106int ecb_cold
2037ev_version_major (void) 2107ev_version_major (void) EV_THROW
2038{ 2108{
2039 return EV_VERSION_MAJOR; 2109 return EV_VERSION_MAJOR;
2040} 2110}
2041 2111
2042int ecb_cold 2112int ecb_cold
2043ev_version_minor (void) 2113ev_version_minor (void) EV_THROW
2044{ 2114{
2045 return EV_VERSION_MINOR; 2115 return EV_VERSION_MINOR;
2046} 2116}
2047 2117
2048/* 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 */
2056 || getgid () != getegid (); 2126 || getgid () != getegid ();
2057#endif 2127#endif
2058} 2128}
2059 2129
2060unsigned int ecb_cold 2130unsigned int ecb_cold
2061ev_supported_backends (void) 2131ev_supported_backends (void) EV_THROW
2062{ 2132{
2063 unsigned int flags = 0; 2133 unsigned int flags = 0;
2064 2134
2065 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2135 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2066 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2136 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
2070 2140
2071 return flags; 2141 return flags;
2072} 2142}
2073 2143
2074unsigned int ecb_cold 2144unsigned int ecb_cold
2075ev_recommended_backends (void) 2145ev_recommended_backends (void) EV_THROW
2076{ 2146{
2077 unsigned int flags = ev_supported_backends (); 2147 unsigned int flags = ev_supported_backends ();
2078 2148
2079#ifndef __NetBSD__ 2149#ifndef __NetBSD__
2080 /* kqueue is borked on everything but netbsd apparently */ 2150 /* kqueue is borked on everything but netbsd apparently */
2092 2162
2093 return flags; 2163 return flags;
2094} 2164}
2095 2165
2096unsigned int ecb_cold 2166unsigned int ecb_cold
2097ev_embeddable_backends (void) 2167ev_embeddable_backends (void) EV_THROW
2098{ 2168{
2099 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 2169 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2100 2170
2101 /* 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 */
2102 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 */
2104 2174
2105 return flags; 2175 return flags;
2106} 2176}
2107 2177
2108unsigned int 2178unsigned int
2109ev_backend (EV_P) 2179ev_backend (EV_P) EV_THROW
2110{ 2180{
2111 return backend; 2181 return backend;
2112} 2182}
2113 2183
2114#if EV_FEATURE_API 2184#if EV_FEATURE_API
2115unsigned int 2185unsigned int
2116ev_iteration (EV_P) 2186ev_iteration (EV_P) EV_THROW
2117{ 2187{
2118 return loop_count; 2188 return loop_count;
2119} 2189}
2120 2190
2121unsigned int 2191unsigned int
2122ev_depth (EV_P) 2192ev_depth (EV_P) EV_THROW
2123{ 2193{
2124 return loop_depth; 2194 return loop_depth;
2125} 2195}
2126 2196
2127void 2197void
2128ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 2198ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2129{ 2199{
2130 io_blocktime = interval; 2200 io_blocktime = interval;
2131} 2201}
2132 2202
2133void 2203void
2134ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 2204ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2135{ 2205{
2136 timeout_blocktime = interval; 2206 timeout_blocktime = interval;
2137} 2207}
2138 2208
2139void 2209void
2140ev_set_userdata (EV_P_ void *data) 2210ev_set_userdata (EV_P_ void *data) EV_THROW
2141{ 2211{
2142 userdata = data; 2212 userdata = data;
2143} 2213}
2144 2214
2145void * 2215void *
2146ev_userdata (EV_P) 2216ev_userdata (EV_P) EV_THROW
2147{ 2217{
2148 return userdata; 2218 return userdata;
2149} 2219}
2150 2220
2151void 2221void
2152ev_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
2153{ 2223{
2154 invoke_cb = invoke_pending_cb; 2224 invoke_cb = invoke_pending_cb;
2155} 2225}
2156 2226
2157void 2227void
2158ev_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
2159{ 2229{
2160 release_cb = release; 2230 release_cb = release;
2161 acquire_cb = acquire; 2231 acquire_cb = acquire;
2162} 2232}
2163#endif 2233#endif
2164 2234
2165/* initialise a loop structure, must be zero-initialised */ 2235/* initialise a loop structure, must be zero-initialised */
2166static void noinline ecb_cold 2236static void noinline ecb_cold
2167loop_init (EV_P_ unsigned int flags) 2237loop_init (EV_P_ unsigned int flags) EV_THROW
2168{ 2238{
2169 if (!backend) 2239 if (!backend)
2170 { 2240 {
2171 origflags = flags; 2241 origflags = flags;
2172 2242
2425} 2495}
2426 2496
2427#if EV_MULTIPLICITY 2497#if EV_MULTIPLICITY
2428 2498
2429struct ev_loop * ecb_cold 2499struct ev_loop * ecb_cold
2430ev_loop_new (unsigned int flags) 2500ev_loop_new (unsigned int flags) EV_THROW
2431{ 2501{
2432 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2502 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2433 2503
2434 memset (EV_A, 0, sizeof (struct ev_loop)); 2504 memset (EV_A, 0, sizeof (struct ev_loop));
2435 loop_init (EV_A_ flags); 2505 loop_init (EV_A_ flags);
2479} 2549}
2480#endif 2550#endif
2481 2551
2482#if EV_FEATURE_API 2552#if EV_FEATURE_API
2483void ecb_cold 2553void ecb_cold
2484ev_verify (EV_P) 2554ev_verify (EV_P) EV_THROW
2485{ 2555{
2486#if EV_VERIFY 2556#if EV_VERIFY
2487 int i; 2557 int i;
2488 WL w; 2558 WL w;
2489 2559
2558#if EV_MULTIPLICITY 2628#if EV_MULTIPLICITY
2559struct ev_loop * ecb_cold 2629struct ev_loop * ecb_cold
2560#else 2630#else
2561int 2631int
2562#endif 2632#endif
2563ev_default_loop (unsigned int flags) 2633ev_default_loop (unsigned int flags) EV_THROW
2564{ 2634{
2565 if (!ev_default_loop_ptr) 2635 if (!ev_default_loop_ptr)
2566 { 2636 {
2567#if EV_MULTIPLICITY 2637#if EV_MULTIPLICITY
2568 EV_P = ev_default_loop_ptr = &default_loop_struct; 2638 EV_P = ev_default_loop_ptr = &default_loop_struct;
2587 2657
2588 return ev_default_loop_ptr; 2658 return ev_default_loop_ptr;
2589} 2659}
2590 2660
2591void 2661void
2592ev_loop_fork (EV_P) 2662ev_loop_fork (EV_P) EV_THROW
2593{ 2663{
2594 postfork = 1; /* must be in line with ev_default_fork */ 2664 postfork = 1; /* must be in line with ev_default_fork */
2595} 2665}
2596 2666
2597/*****************************************************************************/ 2667/*****************************************************************************/
2601{ 2671{
2602 EV_CB_INVOKE ((W)w, revents); 2672 EV_CB_INVOKE ((W)w, revents);
2603} 2673}
2604 2674
2605unsigned int 2675unsigned int
2606ev_pending_count (EV_P) 2676ev_pending_count (EV_P) EV_THROW
2607{ 2677{
2608 int pri; 2678 int pri;
2609 unsigned int count = 0; 2679 unsigned int count = 0;
2610 2680
2611 for (pri = NUMPRI; pri--; ) 2681 for (pri = NUMPRI; pri--; )
2870 2940
2871 mn_now = ev_rt_now; 2941 mn_now = ev_rt_now;
2872 } 2942 }
2873} 2943}
2874 2944
2875void 2945int
2876ev_run (EV_P_ int flags) 2946ev_run (EV_P_ int flags)
2877{ 2947{
2878#if EV_FEATURE_API 2948#if EV_FEATURE_API
2879 ++loop_depth; 2949 ++loop_depth;
2880#endif 2950#endif
2993#endif 3063#endif
2994 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ 3064 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2995 backend_poll (EV_A_ waittime); 3065 backend_poll (EV_A_ waittime);
2996 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 3066 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2997 3067
2998 pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ 3068 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
2999 3069
3000 if (pipe_write_skipped) 3070 if (pipe_write_skipped)
3001 { 3071 {
3002 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)));
3003 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3073 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3037 loop_done = EVBREAK_CANCEL; 3107 loop_done = EVBREAK_CANCEL;
3038 3108
3039#if EV_FEATURE_API 3109#if EV_FEATURE_API
3040 --loop_depth; 3110 --loop_depth;
3041#endif 3111#endif
3112
3113 return activecnt;
3042} 3114}
3043 3115
3044void 3116void
3045ev_break (EV_P_ int how) 3117ev_break (EV_P_ int how) EV_THROW
3046{ 3118{
3047 loop_done = how; 3119 loop_done = how;
3048} 3120}
3049 3121
3050void 3122void
3051ev_ref (EV_P) 3123ev_ref (EV_P) EV_THROW
3052{ 3124{
3053 ++activecnt; 3125 ++activecnt;
3054} 3126}
3055 3127
3056void 3128void
3057ev_unref (EV_P) 3129ev_unref (EV_P) EV_THROW
3058{ 3130{
3059 --activecnt; 3131 --activecnt;
3060} 3132}
3061 3133
3062void 3134void
3063ev_now_update (EV_P) 3135ev_now_update (EV_P) EV_THROW
3064{ 3136{
3065 time_update (EV_A_ 1e100); 3137 time_update (EV_A_ 1e100);
3066} 3138}
3067 3139
3068void 3140void
3069ev_suspend (EV_P) 3141ev_suspend (EV_P) EV_THROW
3070{ 3142{
3071 ev_now_update (EV_A); 3143 ev_now_update (EV_A);
3072} 3144}
3073 3145
3074void 3146void
3075ev_resume (EV_P) 3147ev_resume (EV_P) EV_THROW
3076{ 3148{
3077 ev_tstamp mn_prev = mn_now; 3149 ev_tstamp mn_prev = mn_now;
3078 3150
3079 ev_now_update (EV_A); 3151 ev_now_update (EV_A);
3080 timers_reschedule (EV_A_ mn_now - mn_prev); 3152 timers_reschedule (EV_A_ mn_now - mn_prev);
3119 w->pending = 0; 3191 w->pending = 0;
3120 } 3192 }
3121} 3193}
3122 3194
3123int 3195int
3124ev_clear_pending (EV_P_ void *w) 3196ev_clear_pending (EV_P_ void *w) EV_THROW
3125{ 3197{
3126 W w_ = (W)w; 3198 W w_ = (W)w;
3127 int pending = w_->pending; 3199 int pending = w_->pending;
3128 3200
3129 if (expect_true (pending)) 3201 if (expect_true (pending))
3162} 3234}
3163 3235
3164/*****************************************************************************/ 3236/*****************************************************************************/
3165 3237
3166void noinline 3238void noinline
3167ev_io_start (EV_P_ ev_io *w) 3239ev_io_start (EV_P_ ev_io *w) EV_THROW
3168{ 3240{
3169 int fd = w->fd; 3241 int fd = w->fd;
3170 3242
3171 if (expect_false (ev_is_active (w))) 3243 if (expect_false (ev_is_active (w)))
3172 return; 3244 return;
3185 3257
3186 EV_FREQUENT_CHECK; 3258 EV_FREQUENT_CHECK;
3187} 3259}
3188 3260
3189void noinline 3261void noinline
3190ev_io_stop (EV_P_ ev_io *w) 3262ev_io_stop (EV_P_ ev_io *w) EV_THROW
3191{ 3263{
3192 clear_pending (EV_A_ (W)w); 3264 clear_pending (EV_A_ (W)w);
3193 if (expect_false (!ev_is_active (w))) 3265 if (expect_false (!ev_is_active (w)))
3194 return; 3266 return;
3195 3267
3204 3276
3205 EV_FREQUENT_CHECK; 3277 EV_FREQUENT_CHECK;
3206} 3278}
3207 3279
3208void noinline 3280void noinline
3209ev_timer_start (EV_P_ ev_timer *w) 3281ev_timer_start (EV_P_ ev_timer *w) EV_THROW
3210{ 3282{
3211 if (expect_false (ev_is_active (w))) 3283 if (expect_false (ev_is_active (w)))
3212 return; 3284 return;
3213 3285
3214 ev_at (w) += mn_now; 3286 ev_at (w) += mn_now;
3228 3300
3229 /*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));*/
3230} 3302}
3231 3303
3232void noinline 3304void noinline
3233ev_timer_stop (EV_P_ ev_timer *w) 3305ev_timer_stop (EV_P_ ev_timer *w) EV_THROW
3234{ 3306{
3235 clear_pending (EV_A_ (W)w); 3307 clear_pending (EV_A_ (W)w);
3236 if (expect_false (!ev_is_active (w))) 3308 if (expect_false (!ev_is_active (w)))
3237 return; 3309 return;
3238 3310
3258 3330
3259 EV_FREQUENT_CHECK; 3331 EV_FREQUENT_CHECK;
3260} 3332}
3261 3333
3262void noinline 3334void noinline
3263ev_timer_again (EV_P_ ev_timer *w) 3335ev_timer_again (EV_P_ ev_timer *w) EV_THROW
3264{ 3336{
3265 EV_FREQUENT_CHECK; 3337 EV_FREQUENT_CHECK;
3338
3339 clear_pending (EV_A_ (W)w);
3266 3340
3267 if (ev_is_active (w)) 3341 if (ev_is_active (w))
3268 { 3342 {
3269 if (w->repeat) 3343 if (w->repeat)
3270 { 3344 {
3283 3357
3284 EV_FREQUENT_CHECK; 3358 EV_FREQUENT_CHECK;
3285} 3359}
3286 3360
3287ev_tstamp 3361ev_tstamp
3288ev_timer_remaining (EV_P_ ev_timer *w) 3362ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW
3289{ 3363{
3290 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 3364 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
3291} 3365}
3292 3366
3293#if EV_PERIODIC_ENABLE 3367#if EV_PERIODIC_ENABLE
3294void noinline 3368void noinline
3295ev_periodic_start (EV_P_ ev_periodic *w) 3369ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW
3296{ 3370{
3297 if (expect_false (ev_is_active (w))) 3371 if (expect_false (ev_is_active (w)))
3298 return; 3372 return;
3299 3373
3300 if (w->reschedule_cb) 3374 if (w->reschedule_cb)
3320 3394
3321 /*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));*/
3322} 3396}
3323 3397
3324void noinline 3398void noinline
3325ev_periodic_stop (EV_P_ ev_periodic *w) 3399ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW
3326{ 3400{
3327 clear_pending (EV_A_ (W)w); 3401 clear_pending (EV_A_ (W)w);
3328 if (expect_false (!ev_is_active (w))) 3402 if (expect_false (!ev_is_active (w)))
3329 return; 3403 return;
3330 3404
3348 3422
3349 EV_FREQUENT_CHECK; 3423 EV_FREQUENT_CHECK;
3350} 3424}
3351 3425
3352void noinline 3426void noinline
3353ev_periodic_again (EV_P_ ev_periodic *w) 3427ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW
3354{ 3428{
3355 /* TODO: use adjustheap and recalculation */ 3429 /* TODO: use adjustheap and recalculation */
3356 ev_periodic_stop (EV_A_ w); 3430 ev_periodic_stop (EV_A_ w);
3357 ev_periodic_start (EV_A_ w); 3431 ev_periodic_start (EV_A_ w);
3358} 3432}
3363#endif 3437#endif
3364 3438
3365#if EV_SIGNAL_ENABLE 3439#if EV_SIGNAL_ENABLE
3366 3440
3367void noinline 3441void noinline
3368ev_signal_start (EV_P_ ev_signal *w) 3442ev_signal_start (EV_P_ ev_signal *w) EV_THROW
3369{ 3443{
3370 if (expect_false (ev_is_active (w))) 3444 if (expect_false (ev_is_active (w)))
3371 return; 3445 return;
3372 3446
3373 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));
3444 3518
3445 EV_FREQUENT_CHECK; 3519 EV_FREQUENT_CHECK;
3446} 3520}
3447 3521
3448void noinline 3522void noinline
3449ev_signal_stop (EV_P_ ev_signal *w) 3523ev_signal_stop (EV_P_ ev_signal *w) EV_THROW
3450{ 3524{
3451 clear_pending (EV_A_ (W)w); 3525 clear_pending (EV_A_ (W)w);
3452 if (expect_false (!ev_is_active (w))) 3526 if (expect_false (!ev_is_active (w)))
3453 return; 3527 return;
3454 3528
3485#endif 3559#endif
3486 3560
3487#if EV_CHILD_ENABLE 3561#if EV_CHILD_ENABLE
3488 3562
3489void 3563void
3490ev_child_start (EV_P_ ev_child *w) 3564ev_child_start (EV_P_ ev_child *w) EV_THROW
3491{ 3565{
3492#if EV_MULTIPLICITY 3566#if EV_MULTIPLICITY
3493 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));
3494#endif 3568#endif
3495 if (expect_false (ev_is_active (w))) 3569 if (expect_false (ev_is_active (w)))
3502 3576
3503 EV_FREQUENT_CHECK; 3577 EV_FREQUENT_CHECK;
3504} 3578}
3505 3579
3506void 3580void
3507ev_child_stop (EV_P_ ev_child *w) 3581ev_child_stop (EV_P_ ev_child *w) EV_THROW
3508{ 3582{
3509 clear_pending (EV_A_ (W)w); 3583 clear_pending (EV_A_ (W)w);
3510 if (expect_false (!ev_is_active (w))) 3584 if (expect_false (!ev_is_active (w)))
3511 return; 3585 return;
3512 3586
3679} 3753}
3680 3754
3681inline_size int 3755inline_size int
3682infy_newfd (void) 3756infy_newfd (void)
3683{ 3757{
3684#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) 3758#if defined IN_CLOEXEC && defined IN_NONBLOCK
3685 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); 3759 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3686 if (fd >= 0) 3760 if (fd >= 0)
3687 return fd; 3761 return fd;
3688#endif 3762#endif
3689 return inotify_init (); 3763 return inotify_init ();
3764#else 3838#else
3765# define EV_LSTAT(p,b) lstat (p, b) 3839# define EV_LSTAT(p,b) lstat (p, b)
3766#endif 3840#endif
3767 3841
3768void 3842void
3769ev_stat_stat (EV_P_ ev_stat *w) 3843ev_stat_stat (EV_P_ ev_stat *w) EV_THROW
3770{ 3844{
3771 if (lstat (w->path, &w->attr) < 0) 3845 if (lstat (w->path, &w->attr) < 0)
3772 w->attr.st_nlink = 0; 3846 w->attr.st_nlink = 0;
3773 else if (!w->attr.st_nlink) 3847 else if (!w->attr.st_nlink)
3774 w->attr.st_nlink = 1; 3848 w->attr.st_nlink = 1;
3813 ev_feed_event (EV_A_ w, EV_STAT); 3887 ev_feed_event (EV_A_ w, EV_STAT);
3814 } 3888 }
3815} 3889}
3816 3890
3817void 3891void
3818ev_stat_start (EV_P_ ev_stat *w) 3892ev_stat_start (EV_P_ ev_stat *w) EV_THROW
3819{ 3893{
3820 if (expect_false (ev_is_active (w))) 3894 if (expect_false (ev_is_active (w)))
3821 return; 3895 return;
3822 3896
3823 ev_stat_stat (EV_A_ w); 3897 ev_stat_stat (EV_A_ w);
3844 3918
3845 EV_FREQUENT_CHECK; 3919 EV_FREQUENT_CHECK;
3846} 3920}
3847 3921
3848void 3922void
3849ev_stat_stop (EV_P_ ev_stat *w) 3923ev_stat_stop (EV_P_ ev_stat *w) EV_THROW
3850{ 3924{
3851 clear_pending (EV_A_ (W)w); 3925 clear_pending (EV_A_ (W)w);
3852 if (expect_false (!ev_is_active (w))) 3926 if (expect_false (!ev_is_active (w)))
3853 return; 3927 return;
3854 3928
3870} 3944}
3871#endif 3945#endif
3872 3946
3873#if EV_IDLE_ENABLE 3947#if EV_IDLE_ENABLE
3874void 3948void
3875ev_idle_start (EV_P_ ev_idle *w) 3949ev_idle_start (EV_P_ ev_idle *w) EV_THROW
3876{ 3950{
3877 if (expect_false (ev_is_active (w))) 3951 if (expect_false (ev_is_active (w)))
3878 return; 3952 return;
3879 3953
3880 pri_adjust (EV_A_ (W)w); 3954 pri_adjust (EV_A_ (W)w);
3893 3967
3894 EV_FREQUENT_CHECK; 3968 EV_FREQUENT_CHECK;
3895} 3969}
3896 3970
3897void 3971void
3898ev_idle_stop (EV_P_ ev_idle *w) 3972ev_idle_stop (EV_P_ ev_idle *w) EV_THROW
3899{ 3973{
3900 clear_pending (EV_A_ (W)w); 3974 clear_pending (EV_A_ (W)w);
3901 if (expect_false (!ev_is_active (w))) 3975 if (expect_false (!ev_is_active (w)))
3902 return; 3976 return;
3903 3977
3917} 3991}
3918#endif 3992#endif
3919 3993
3920#if EV_PREPARE_ENABLE 3994#if EV_PREPARE_ENABLE
3921void 3995void
3922ev_prepare_start (EV_P_ ev_prepare *w) 3996ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW
3923{ 3997{
3924 if (expect_false (ev_is_active (w))) 3998 if (expect_false (ev_is_active (w)))
3925 return; 3999 return;
3926 4000
3927 EV_FREQUENT_CHECK; 4001 EV_FREQUENT_CHECK;
3932 4006
3933 EV_FREQUENT_CHECK; 4007 EV_FREQUENT_CHECK;
3934} 4008}
3935 4009
3936void 4010void
3937ev_prepare_stop (EV_P_ ev_prepare *w) 4011ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW
3938{ 4012{
3939 clear_pending (EV_A_ (W)w); 4013 clear_pending (EV_A_ (W)w);
3940 if (expect_false (!ev_is_active (w))) 4014 if (expect_false (!ev_is_active (w)))
3941 return; 4015 return;
3942 4016
3955} 4029}
3956#endif 4030#endif
3957 4031
3958#if EV_CHECK_ENABLE 4032#if EV_CHECK_ENABLE
3959void 4033void
3960ev_check_start (EV_P_ ev_check *w) 4034ev_check_start (EV_P_ ev_check *w) EV_THROW
3961{ 4035{
3962 if (expect_false (ev_is_active (w))) 4036 if (expect_false (ev_is_active (w)))
3963 return; 4037 return;
3964 4038
3965 EV_FREQUENT_CHECK; 4039 EV_FREQUENT_CHECK;
3970 4044
3971 EV_FREQUENT_CHECK; 4045 EV_FREQUENT_CHECK;
3972} 4046}
3973 4047
3974void 4048void
3975ev_check_stop (EV_P_ ev_check *w) 4049ev_check_stop (EV_P_ ev_check *w) EV_THROW
3976{ 4050{
3977 clear_pending (EV_A_ (W)w); 4051 clear_pending (EV_A_ (W)w);
3978 if (expect_false (!ev_is_active (w))) 4052 if (expect_false (!ev_is_active (w)))
3979 return; 4053 return;
3980 4054
3993} 4067}
3994#endif 4068#endif
3995 4069
3996#if EV_EMBED_ENABLE 4070#if EV_EMBED_ENABLE
3997void noinline 4071void noinline
3998ev_embed_sweep (EV_P_ ev_embed *w) 4072ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW
3999{ 4073{
4000 ev_run (w->other, EVRUN_NOWAIT); 4074 ev_run (w->other, EVRUN_NOWAIT);
4001} 4075}
4002 4076
4003static void 4077static void
4051 ev_idle_stop (EV_A_ idle); 4125 ev_idle_stop (EV_A_ idle);
4052} 4126}
4053#endif 4127#endif
4054 4128
4055void 4129void
4056ev_embed_start (EV_P_ ev_embed *w) 4130ev_embed_start (EV_P_ ev_embed *w) EV_THROW
4057{ 4131{
4058 if (expect_false (ev_is_active (w))) 4132 if (expect_false (ev_is_active (w)))
4059 return; 4133 return;
4060 4134
4061 { 4135 {
4082 4156
4083 EV_FREQUENT_CHECK; 4157 EV_FREQUENT_CHECK;
4084} 4158}
4085 4159
4086void 4160void
4087ev_embed_stop (EV_P_ ev_embed *w) 4161ev_embed_stop (EV_P_ ev_embed *w) EV_THROW
4088{ 4162{
4089 clear_pending (EV_A_ (W)w); 4163 clear_pending (EV_A_ (W)w);
4090 if (expect_false (!ev_is_active (w))) 4164 if (expect_false (!ev_is_active (w)))
4091 return; 4165 return;
4092 4166
4102} 4176}
4103#endif 4177#endif
4104 4178
4105#if EV_FORK_ENABLE 4179#if EV_FORK_ENABLE
4106void 4180void
4107ev_fork_start (EV_P_ ev_fork *w) 4181ev_fork_start (EV_P_ ev_fork *w) EV_THROW
4108{ 4182{
4109 if (expect_false (ev_is_active (w))) 4183 if (expect_false (ev_is_active (w)))
4110 return; 4184 return;
4111 4185
4112 EV_FREQUENT_CHECK; 4186 EV_FREQUENT_CHECK;
4117 4191
4118 EV_FREQUENT_CHECK; 4192 EV_FREQUENT_CHECK;
4119} 4193}
4120 4194
4121void 4195void
4122ev_fork_stop (EV_P_ ev_fork *w) 4196ev_fork_stop (EV_P_ ev_fork *w) EV_THROW
4123{ 4197{
4124 clear_pending (EV_A_ (W)w); 4198 clear_pending (EV_A_ (W)w);
4125 if (expect_false (!ev_is_active (w))) 4199 if (expect_false (!ev_is_active (w)))
4126 return; 4200 return;
4127 4201
4140} 4214}
4141#endif 4215#endif
4142 4216
4143#if EV_CLEANUP_ENABLE 4217#if EV_CLEANUP_ENABLE
4144void 4218void
4145ev_cleanup_start (EV_P_ ev_cleanup *w) 4219ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW
4146{ 4220{
4147 if (expect_false (ev_is_active (w))) 4221 if (expect_false (ev_is_active (w)))
4148 return; 4222 return;
4149 4223
4150 EV_FREQUENT_CHECK; 4224 EV_FREQUENT_CHECK;
4157 ev_unref (EV_A); 4231 ev_unref (EV_A);
4158 EV_FREQUENT_CHECK; 4232 EV_FREQUENT_CHECK;
4159} 4233}
4160 4234
4161void 4235void
4162ev_cleanup_stop (EV_P_ ev_cleanup *w) 4236ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW
4163{ 4237{
4164 clear_pending (EV_A_ (W)w); 4238 clear_pending (EV_A_ (W)w);
4165 if (expect_false (!ev_is_active (w))) 4239 if (expect_false (!ev_is_active (w)))
4166 return; 4240 return;
4167 4241
4181} 4255}
4182#endif 4256#endif
4183 4257
4184#if EV_ASYNC_ENABLE 4258#if EV_ASYNC_ENABLE
4185void 4259void
4186ev_async_start (EV_P_ ev_async *w) 4260ev_async_start (EV_P_ ev_async *w) EV_THROW
4187{ 4261{
4188 if (expect_false (ev_is_active (w))) 4262 if (expect_false (ev_is_active (w)))
4189 return; 4263 return;
4190 4264
4191 w->sent = 0; 4265 w->sent = 0;
4200 4274
4201 EV_FREQUENT_CHECK; 4275 EV_FREQUENT_CHECK;
4202} 4276}
4203 4277
4204void 4278void
4205ev_async_stop (EV_P_ ev_async *w) 4279ev_async_stop (EV_P_ ev_async *w) EV_THROW
4206{ 4280{
4207 clear_pending (EV_A_ (W)w); 4281 clear_pending (EV_A_ (W)w);
4208 if (expect_false (!ev_is_active (w))) 4282 if (expect_false (!ev_is_active (w)))
4209 return; 4283 return;
4210 4284
4221 4295
4222 EV_FREQUENT_CHECK; 4296 EV_FREQUENT_CHECK;
4223} 4297}
4224 4298
4225void 4299void
4226ev_async_send (EV_P_ ev_async *w) 4300ev_async_send (EV_P_ ev_async *w) EV_THROW
4227{ 4301{
4228 w->sent = 1; 4302 w->sent = 1;
4229 evpipe_write (EV_A_ &async_pending); 4303 evpipe_write (EV_A_ &async_pending);
4230} 4304}
4231#endif 4305#endif
4268 4342
4269 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));
4270} 4344}
4271 4345
4272void 4346void
4273ev_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
4274{ 4348{
4275 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));
4276 4350
4277 if (expect_false (!once)) 4351 if (expect_false (!once))
4278 { 4352 {
4300 4374
4301/*****************************************************************************/ 4375/*****************************************************************************/
4302 4376
4303#if EV_WALK_ENABLE 4377#if EV_WALK_ENABLE
4304void ecb_cold 4378void ecb_cold
4305ev_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
4306{ 4380{
4307 int i, j; 4381 int i, j;
4308 ev_watcher_list *wl, *wn; 4382 ev_watcher_list *wl, *wn;
4309 4383
4310 if (types & (EV_IO | EV_EMBED)) 4384 if (types & (EV_IO | EV_EMBED))
4416 4490
4417#if EV_MULTIPLICITY 4491#if EV_MULTIPLICITY
4418 #include "ev_wrap.h" 4492 #include "ev_wrap.h"
4419#endif 4493#endif
4420 4494
4421EV_CPP(})
4422

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines