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

Comparing libev/ev.c (file contents):
Revision 1.409 by root, Sat Feb 4 15:17:34 2012 UTC vs.
Revision 1.451 by root, Tue Jan 22 05:18:28 2013 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
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
198
188#ifndef _WIN32 199#ifndef _WIN32
189# include <sys/time.h> 200# include <sys/time.h>
190# include <sys/wait.h> 201# include <sys/wait.h>
191# include <unistd.h> 202# include <unistd.h>
192#else 203#else
193# include <io.h> 204# include <io.h>
194# define WIN32_LEAN_AND_MEAN 205# define WIN32_LEAN_AND_MEAN
206# include <winsock2.h>
195# include <windows.h> 207# include <windows.h>
196# ifndef EV_SELECT_IS_WINSOCKET 208# ifndef EV_SELECT_IS_WINSOCKET
197# define EV_SELECT_IS_WINSOCKET 1 209# define EV_SELECT_IS_WINSOCKET 1
198# endif 210# endif
199# undef EV_AVOID_STDIO 211# undef EV_AVOID_STDIO
208#define _DARWIN_UNLIMITED_SELECT 1 220#define _DARWIN_UNLIMITED_SELECT 1
209 221
210/* this block tries to deduce configuration from header-defined symbols and defaults */ 222/* this block tries to deduce configuration from header-defined symbols and defaults */
211 223
212/* try to deduce the maximum number of signals on this platform */ 224/* try to deduce the maximum number of signals on this platform */
213#if defined (EV_NSIG) 225#if defined EV_NSIG
214/* use what's provided */ 226/* use what's provided */
215#elif defined (NSIG) 227#elif defined NSIG
216# define EV_NSIG (NSIG) 228# define EV_NSIG (NSIG)
217#elif defined(_NSIG) 229#elif defined _NSIG
218# define EV_NSIG (_NSIG) 230# define EV_NSIG (_NSIG)
219#elif defined (SIGMAX) 231#elif defined SIGMAX
220# define EV_NSIG (SIGMAX+1) 232# define EV_NSIG (SIGMAX+1)
221#elif defined (SIG_MAX) 233#elif defined SIG_MAX
222# define EV_NSIG (SIG_MAX+1) 234# define EV_NSIG (SIG_MAX+1)
223#elif defined (_SIG_MAX) 235#elif defined _SIG_MAX
224# define EV_NSIG (_SIG_MAX+1) 236# define EV_NSIG (_SIG_MAX+1)
225#elif defined (MAXSIG) 237#elif defined MAXSIG
226# define EV_NSIG (MAXSIG+1) 238# define EV_NSIG (MAXSIG+1)
227#elif defined (MAX_SIG) 239#elif defined MAX_SIG
228# define EV_NSIG (MAX_SIG+1) 240# define EV_NSIG (MAX_SIG+1)
229#elif defined (SIGARRAYSIZE) 241#elif defined SIGARRAYSIZE
230# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ 242# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
231#elif defined (_sys_nsig) 243#elif defined _sys_nsig
232# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ 244# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
233#else 245#else
234# error "unable to find value for NSIG, please report" 246# error "unable to find value for NSIG, please report"
235/* to make it compile regardless, just remove the above line, */ 247/* to make it compile regardless, just remove the above line, */
236/* but consider reporting it, too! :) */ 248/* but consider reporting it, too! :) */
248# define EV_USE_CLOCK_SYSCALL 0 260# define EV_USE_CLOCK_SYSCALL 0
249# endif 261# endif
250#endif 262#endif
251 263
252#ifndef EV_USE_MONOTONIC 264#ifndef EV_USE_MONOTONIC
253# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 265# if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
254# define EV_USE_MONOTONIC EV_FEATURE_OS 266# define EV_USE_MONOTONIC EV_FEATURE_OS
255# else 267# else
256# define EV_USE_MONOTONIC 0 268# define EV_USE_MONOTONIC 0
257# endif 269# endif
258#endif 270#endif
348#endif 360#endif
349 361
350/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ 362/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
351/* which makes programs even slower. might work on other unices, too. */ 363/* which makes programs even slower. might work on other unices, too. */
352#if EV_USE_CLOCK_SYSCALL 364#if EV_USE_CLOCK_SYSCALL
353# include <syscall.h> 365# include <sys/syscall.h>
354# ifdef SYS_clock_gettime 366# ifdef SYS_clock_gettime
355# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 367# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
356# undef EV_USE_MONOTONIC 368# undef EV_USE_MONOTONIC
357# define EV_USE_MONOTONIC 1 369# define EV_USE_MONOTONIC 1
358# else 370# else
384# define EV_USE_INOTIFY 0 396# define EV_USE_INOTIFY 0
385#endif 397#endif
386 398
387#if !EV_USE_NANOSLEEP 399#if !EV_USE_NANOSLEEP
388/* hp-ux has it in sys/time.h, which we unconditionally include above */ 400/* hp-ux has it in sys/time.h, which we unconditionally include above */
389# if !defined(_WIN32) && !defined(__hpux) 401# if !defined _WIN32 && !defined __hpux
390# include <sys/select.h> 402# include <sys/select.h>
391# endif 403# endif
392#endif 404#endif
393 405
394#if EV_USE_INOTIFY 406#if EV_USE_INOTIFY
397/* some very old inotify.h headers don't have IN_DONT_FOLLOW */ 409/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
398# ifndef IN_DONT_FOLLOW 410# ifndef IN_DONT_FOLLOW
399# undef EV_USE_INOTIFY 411# undef EV_USE_INOTIFY
400# define EV_USE_INOTIFY 0 412# define EV_USE_INOTIFY 0
401# endif 413# endif
402#endif
403
404#if EV_SELECT_IS_WINSOCKET
405# include <winsock.h>
406#endif 414#endif
407 415
408#if EV_USE_EVENTFD 416#if EV_USE_EVENTFD
409/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 417/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
410# include <stdint.h> 418# include <stdint.h>
496 */ 504 */
497 505
498#ifndef ECB_H 506#ifndef ECB_H
499#define ECB_H 507#define ECB_H
500 508
509/* 16 bits major, 16 bits minor */
510#define ECB_VERSION 0x00010002
511
501#ifdef _WIN32 512#ifdef _WIN32
502 typedef signed char int8_t; 513 typedef signed char int8_t;
503 typedef unsigned char uint8_t; 514 typedef unsigned char uint8_t;
504 typedef signed short int16_t; 515 typedef signed short int16_t;
505 typedef unsigned short uint16_t; 516 typedef unsigned short uint16_t;
510 typedef unsigned long long uint64_t; 521 typedef unsigned long long uint64_t;
511 #else /* _MSC_VER || __BORLANDC__ */ 522 #else /* _MSC_VER || __BORLANDC__ */
512 typedef signed __int64 int64_t; 523 typedef signed __int64 int64_t;
513 typedef unsigned __int64 uint64_t; 524 typedef unsigned __int64 uint64_t;
514 #endif 525 #endif
526 #ifdef _WIN64
527 #define ECB_PTRSIZE 8
528 typedef uint64_t uintptr_t;
529 typedef int64_t intptr_t;
530 #else
531 #define ECB_PTRSIZE 4
532 typedef uint32_t uintptr_t;
533 typedef int32_t intptr_t;
534 #endif
515#else 535#else
516 #include <inttypes.h> 536 #include <inttypes.h>
537 #if UINTMAX_MAX > 0xffffffffU
538 #define ECB_PTRSIZE 8
539 #else
540 #define ECB_PTRSIZE 4
541 #endif
517#endif 542#endif
518 543
519/* many compilers define _GNUC_ to some versions but then only implement 544/* many compilers define _GNUC_ to some versions but then only implement
520 * what their idiot authors think are the "more important" extensions, 545 * what their idiot authors think are the "more important" extensions,
521 * causing enormous grief in return for some better fake benchmark numbers. 546 * causing enormous grief in return for some better fake benchmark numbers.
522 * or so. 547 * or so.
523 * we try to detect these and simply assume they are not gcc - if they have 548 * we try to detect these and simply assume they are not gcc - if they have
524 * an issue with that they should have done it right in the first place. 549 * an issue with that they should have done it right in the first place.
525 */ 550 */
526#ifndef ECB_GCC_VERSION 551#ifndef ECB_GCC_VERSION
527 #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__) 552 #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__
528 #define ECB_GCC_VERSION(major,minor) 0 553 #define ECB_GCC_VERSION(major,minor) 0
529 #else 554 #else
530 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 555 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
531 #endif 556 #endif
532#endif 557#endif
533 558
559#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */
560#define ECB_C99 (__STDC_VERSION__ >= 199901L)
561#define ECB_C11 (__STDC_VERSION__ >= 201112L)
562#define ECB_CPP (__cplusplus+0)
563#define ECB_CPP11 (__cplusplus >= 201103L)
564
565#if ECB_CPP
566 #define ECB_EXTERN_C extern "C"
567 #define ECB_EXTERN_C_BEG ECB_EXTERN_C {
568 #define ECB_EXTERN_C_END }
569#else
570 #define ECB_EXTERN_C extern
571 #define ECB_EXTERN_C_BEG
572 #define ECB_EXTERN_C_END
573#endif
574
534/*****************************************************************************/ 575/*****************************************************************************/
535 576
536/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ 577/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
537/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ 578/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
538 579
539#if ECB_NO_THREADS || ECB_NO_SMP 580#if ECB_NO_THREADS
581 #define ECB_NO_SMP 1
582#endif
583
584#if ECB_NO_SMP
540 #define ECB_MEMORY_FENCE do { } while (0) 585 #define ECB_MEMORY_FENCE do { } while (0)
541#endif 586#endif
542 587
543#ifndef ECB_MEMORY_FENCE 588#ifndef ECB_MEMORY_FENCE
544 #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 589 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
545 #if __i386 || __i386__ 590 #if __i386 || __i386__
546 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") 591 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
547 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ 592 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
548 #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ 593 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
549 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ 594 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__
550 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") 595 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
551 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") 596 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
552 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ 597 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
553 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ 598 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
554 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 599 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
555 #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ 600 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
556 || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) 601 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__
557 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") 602 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
558 #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ 603 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
559 || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) 604 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__
560 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") 605 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
561 #elif __sparc || __sparc__ 606 #elif __sparc || __sparc__
562 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory") 607 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
563 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") 608 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
564 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") 609 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
565 #elif defined(__s390__) || defined(__s390x__) 610 #elif defined __s390__ || defined __s390x__
566 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") 611 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
612 #elif defined __mips__
613 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
614 #elif defined __alpha__
615 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
616 #elif defined __hppa__
617 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
618 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
619 #elif defined __ia64__
620 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory")
567 #endif 621 #endif
568 #endif 622 #endif
569#endif 623#endif
570 624
571#ifndef ECB_MEMORY_FENCE 625#ifndef ECB_MEMORY_FENCE
626 #if ECB_GCC_VERSION(4,7)
627 /* see comment below (stdatomic.h) about the C11 memory model. */
628 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
629
630 /* The __has_feature syntax from clang is so misdesigned that we cannot use it
631 * without risking compile time errors with other compilers. We *could*
632 * define our own ecb_clang_has_feature, but I just can't be bothered to work
633 * around this shit time and again.
634 * #elif defined __clang && __has_feature (cxx_atomic)
635 * // see comment below (stdatomic.h) about the C11 memory model.
636 * #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
637 */
638
572 #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) 639 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
573 #define ECB_MEMORY_FENCE __sync_synchronize () 640 #define ECB_MEMORY_FENCE __sync_synchronize ()
574 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
575 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
576 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 641 #elif _MSC_VER >= 1400 /* VC++ 2005 */
577 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 642 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
578 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 643 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
579 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ 644 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
580 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 645 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
581 #elif defined(_WIN32) 646 #elif defined _WIN32
582 #include <WinNT.h> 647 #include <WinNT.h>
583 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ 648 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
584 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 649 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
585 #include <mbarrier.h> 650 #include <mbarrier.h>
586 #define ECB_MEMORY_FENCE __machine_rw_barrier () 651 #define ECB_MEMORY_FENCE __machine_rw_barrier ()
587 #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () 652 #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier ()
588 #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () 653 #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier ()
654 #elif __xlC__
655 #define ECB_MEMORY_FENCE __sync ()
656 #endif
657#endif
658
659#ifndef ECB_MEMORY_FENCE
660 #if ECB_C11 && !defined __STDC_NO_ATOMICS__
661 /* we assume that these memory fences work on all variables/all memory accesses, */
662 /* not just C11 atomics and atomic accesses */
663 #include <stdatomic.h>
664 /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */
665 /* any fence other than seq_cst, which isn't very efficient for us. */
666 /* Why that is, we don't know - either the C11 memory model is quite useless */
667 /* for most usages, or gcc and clang have a bug */
668 /* I *currently* lean towards the latter, and inefficiently implement */
669 /* all three of ecb's fences as a seq_cst fence */
670 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
589 #endif 671 #endif
590#endif 672#endif
591 673
592#ifndef ECB_MEMORY_FENCE 674#ifndef ECB_MEMORY_FENCE
593 #if !ECB_AVOID_PTHREADS 675 #if !ECB_AVOID_PTHREADS
605 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; 687 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
606 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) 688 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
607 #endif 689 #endif
608#endif 690#endif
609 691
610#if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) 692#if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
611 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 693 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
612#endif 694#endif
613 695
614#if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) 696#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
615 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 697 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
616#endif 698#endif
617 699
618/*****************************************************************************/ 700/*****************************************************************************/
619
620#define ECB_C99 (__STDC_VERSION__ >= 199901L)
621 701
622#if __cplusplus 702#if __cplusplus
623 #define ecb_inline static inline 703 #define ecb_inline static inline
624#elif ECB_GCC_VERSION(2,5) 704#elif ECB_GCC_VERSION(2,5)
625 #define ecb_inline static __inline__ 705 #define ecb_inline static __inline__
664#elif ECB_GCC_VERSION(3,0) 744#elif ECB_GCC_VERSION(3,0)
665 #define ecb_decltype(x) __typeof(x) 745 #define ecb_decltype(x) __typeof(x)
666#endif 746#endif
667 747
668#define ecb_noinline ecb_attribute ((__noinline__)) 748#define ecb_noinline ecb_attribute ((__noinline__))
669#define ecb_noreturn ecb_attribute ((__noreturn__))
670#define ecb_unused ecb_attribute ((__unused__)) 749#define ecb_unused ecb_attribute ((__unused__))
671#define ecb_const ecb_attribute ((__const__)) 750#define ecb_const ecb_attribute ((__const__))
672#define ecb_pure ecb_attribute ((__pure__)) 751#define ecb_pure ecb_attribute ((__pure__))
752
753#if ECB_C11
754 #define ecb_noreturn _Noreturn
755#else
756 #define ecb_noreturn ecb_attribute ((__noreturn__))
757#endif
673 758
674#if ECB_GCC_VERSION(4,3) 759#if ECB_GCC_VERSION(4,3)
675 #define ecb_artificial ecb_attribute ((__artificial__)) 760 #define ecb_artificial ecb_attribute ((__artificial__))
676 #define ecb_hot ecb_attribute ((__hot__)) 761 #define ecb_hot ecb_attribute ((__hot__))
677 #define ecb_cold ecb_attribute ((__cold__)) 762 #define ecb_cold ecb_attribute ((__cold__))
768 853
769 return r + ecb_ld32 (x); 854 return r + ecb_ld32 (x);
770 } 855 }
771#endif 856#endif
772 857
858ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) ecb_const;
859ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
860ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) ecb_const;
861ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); }
862
773ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const; 863ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const;
774ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) 864ecb_function_ uint8_t ecb_bitrev8 (uint8_t x)
775{ 865{
776 return ( (x * 0x0802U & 0x22110U) 866 return ( (x * 0x0802U & 0x22110U)
777 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; 867 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
861 ecb_inline void ecb_unreachable (void) ecb_noreturn; 951 ecb_inline void ecb_unreachable (void) ecb_noreturn;
862 ecb_inline void ecb_unreachable (void) { } 952 ecb_inline void ecb_unreachable (void) { }
863#endif 953#endif
864 954
865/* try to tell the compiler that some condition is definitely true */ 955/* try to tell the compiler that some condition is definitely true */
866#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 956#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
867 957
868ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; 958ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
869ecb_inline unsigned char 959ecb_inline unsigned char
870ecb_byteorder_helper (void) 960ecb_byteorder_helper (void)
871{ 961{
872 const uint32_t u = 0x11223344; 962 /* the union code still generates code under pressure in gcc, */
873 return *(unsigned char *)&u; 963 /* but less than using pointers, and always seems to */
964 /* successfully return a constant. */
965 /* the reason why we have this horrible preprocessor mess */
966 /* is to avoid it in all cases, at least on common architectures */
967 /* or when using a recent enough gcc version (>= 4.6) */
968#if __i386 || __i386__ || _M_X86 || __amd64 || __amd64__ || _M_X64
969 return 0x44;
970#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
971 return 0x44;
972#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
973 return 0x11;
974#else
975 union
976 {
977 uint32_t i;
978 uint8_t c;
979 } u = { 0x11223344 };
980 return u.c;
981#endif
874} 982}
875 983
876ecb_inline ecb_bool ecb_big_endian (void) ecb_const; 984ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
877ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } 985ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
878ecb_inline ecb_bool ecb_little_endian (void) ecb_const; 986ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
909 } 1017 }
910#else 1018#else
911 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 1019 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
912#endif 1020#endif
913 1021
1022/*******************************************************************************/
1023/* floating point stuff, can be disabled by defining ECB_NO_LIBM */
1024
1025/* basically, everything uses "ieee pure-endian" floating point numbers */
1026/* the only noteworthy exception is ancient armle, which uses order 43218765 */
1027#if 0 \
1028 || __i386 || __i386__ \
1029 || __amd64 || __amd64__ || __x86_64 || __x86_64__ \
1030 || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \
1031 || defined __arm__ && defined __ARM_EABI__ \
1032 || defined __s390__ || defined __s390x__ \
1033 || defined __mips__ \
1034 || defined __alpha__ \
1035 || defined __hppa__ \
1036 || defined __ia64__ \
1037 || defined _M_IX86 || defined _M_AMD64 || defined _M_IA64
1038 #define ECB_STDFP 1
1039 #include <string.h> /* for memcpy */
1040#else
1041 #define ECB_STDFP 0
1042 #include <math.h> /* for frexp*, ldexp* */
1043#endif
1044
1045#ifndef ECB_NO_LIBM
1046
1047 /* convert a float to ieee single/binary32 */
1048 ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const;
1049 ecb_function_ uint32_t
1050 ecb_float_to_binary32 (float x)
1051 {
1052 uint32_t r;
1053
1054 #if ECB_STDFP
1055 memcpy (&r, &x, 4);
1056 #else
1057 /* slow emulation, works for anything but -0 */
1058 uint32_t m;
1059 int e;
1060
1061 if (x == 0e0f ) return 0x00000000U;
1062 if (x > +3.40282346638528860e+38f) return 0x7f800000U;
1063 if (x < -3.40282346638528860e+38f) return 0xff800000U;
1064 if (x != x ) return 0x7fbfffffU;
1065
1066 m = frexpf (x, &e) * 0x1000000U;
1067
1068 r = m & 0x80000000U;
1069
1070 if (r)
1071 m = -m;
1072
1073 if (e <= -126)
1074 {
1075 m &= 0xffffffU;
1076 m >>= (-125 - e);
1077 e = -126;
1078 }
1079
1080 r |= (e + 126) << 23;
1081 r |= m & 0x7fffffU;
1082 #endif
1083
1084 return r;
1085 }
1086
1087 /* converts an ieee single/binary32 to a float */
1088 ecb_function_ float ecb_binary32_to_float (uint32_t x) ecb_const;
1089 ecb_function_ float
1090 ecb_binary32_to_float (uint32_t x)
1091 {
1092 float r;
1093
1094 #if ECB_STDFP
1095 memcpy (&r, &x, 4);
1096 #else
1097 /* emulation, only works for normals and subnormals and +0 */
1098 int neg = x >> 31;
1099 int e = (x >> 23) & 0xffU;
1100
1101 x &= 0x7fffffU;
1102
1103 if (e)
1104 x |= 0x800000U;
1105 else
1106 e = 1;
1107
1108 /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */
1109 r = ldexpf (x * (0.5f / 0x800000U), e - 126);
1110
1111 r = neg ? -r : r;
1112 #endif
1113
1114 return r;
1115 }
1116
1117 /* convert a double to ieee double/binary64 */
1118 ecb_function_ uint64_t ecb_double_to_binary64 (double x) ecb_const;
1119 ecb_function_ uint64_t
1120 ecb_double_to_binary64 (double x)
1121 {
1122 uint64_t r;
1123
1124 #if ECB_STDFP
1125 memcpy (&r, &x, 8);
1126 #else
1127 /* slow emulation, works for anything but -0 */
1128 uint64_t m;
1129 int e;
1130
1131 if (x == 0e0 ) return 0x0000000000000000U;
1132 if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U;
1133 if (x < -1.79769313486231470e+308) return 0xfff0000000000000U;
1134 if (x != x ) return 0X7ff7ffffffffffffU;
1135
1136 m = frexp (x, &e) * 0x20000000000000U;
1137
1138 r = m & 0x8000000000000000;;
1139
1140 if (r)
1141 m = -m;
1142
1143 if (e <= -1022)
1144 {
1145 m &= 0x1fffffffffffffU;
1146 m >>= (-1021 - e);
1147 e = -1022;
1148 }
1149
1150 r |= ((uint64_t)(e + 1022)) << 52;
1151 r |= m & 0xfffffffffffffU;
1152 #endif
1153
1154 return r;
1155 }
1156
1157 /* converts an ieee double/binary64 to a double */
1158 ecb_function_ double ecb_binary64_to_double (uint64_t x) ecb_const;
1159 ecb_function_ double
1160 ecb_binary64_to_double (uint64_t x)
1161 {
1162 double r;
1163
1164 #if ECB_STDFP
1165 memcpy (&r, &x, 8);
1166 #else
1167 /* emulation, only works for normals and subnormals and +0 */
1168 int neg = x >> 63;
1169 int e = (x >> 52) & 0x7ffU;
1170
1171 x &= 0xfffffffffffffU;
1172
1173 if (e)
1174 x |= 0x10000000000000U;
1175 else
1176 e = 1;
1177
1178 /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */
1179 r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022);
1180
1181 r = neg ? -r : r;
1182 #endif
1183
1184 return r;
1185 }
1186
1187#endif
1188
914#endif 1189#endif
915 1190
916/* ECB.H END */ 1191/* ECB.H END */
917 1192
918#if ECB_MEMORY_FENCE_NEEDS_PTHREADS 1193#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
1084{ 1359{
1085 write (STDERR_FILENO, msg, strlen (msg)); 1360 write (STDERR_FILENO, msg, strlen (msg));
1086} 1361}
1087#endif 1362#endif
1088 1363
1089static void (*syserr_cb)(const char *msg); 1364static void (*syserr_cb)(const char *msg) EV_THROW;
1090 1365
1091void ecb_cold 1366void ecb_cold
1092ev_set_syserr_cb (void (*cb)(const char *msg)) 1367ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW
1093{ 1368{
1094 syserr_cb = cb; 1369 syserr_cb = cb;
1095} 1370}
1096 1371
1097static void noinline ecb_cold 1372static void noinline ecb_cold
1115 abort (); 1390 abort ();
1116 } 1391 }
1117} 1392}
1118 1393
1119static void * 1394static void *
1120ev_realloc_emul (void *ptr, long size) 1395ev_realloc_emul (void *ptr, long size) EV_THROW
1121{ 1396{
1122#if __GLIBC__
1123 return realloc (ptr, size);
1124#else
1125 /* some systems, notably openbsd and darwin, fail to properly 1397 /* some systems, notably openbsd and darwin, fail to properly
1126 * implement realloc (x, 0) (as required by both ansi c-89 and 1398 * implement realloc (x, 0) (as required by both ansi c-89 and
1127 * the single unix specification, so work around them here. 1399 * the single unix specification, so work around them here.
1400 * recently, also (at least) fedora and debian started breaking it,
1401 * despite documenting it otherwise.
1128 */ 1402 */
1129 1403
1130 if (size) 1404 if (size)
1131 return realloc (ptr, size); 1405 return realloc (ptr, size);
1132 1406
1133 free (ptr); 1407 free (ptr);
1134 return 0; 1408 return 0;
1135#endif
1136} 1409}
1137 1410
1138static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 1411static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1139 1412
1140void ecb_cold 1413void ecb_cold
1141ev_set_allocator (void *(*cb)(void *ptr, long size)) 1414ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW
1142{ 1415{
1143 alloc = cb; 1416 alloc = cb;
1144} 1417}
1145 1418
1146inline_speed void * 1419inline_speed void *
1263 1536
1264/*****************************************************************************/ 1537/*****************************************************************************/
1265 1538
1266#ifndef EV_HAVE_EV_TIME 1539#ifndef EV_HAVE_EV_TIME
1267ev_tstamp 1540ev_tstamp
1268ev_time (void) 1541ev_time (void) EV_THROW
1269{ 1542{
1270#if EV_USE_REALTIME 1543#if EV_USE_REALTIME
1271 if (expect_true (have_realtime)) 1544 if (expect_true (have_realtime))
1272 { 1545 {
1273 struct timespec ts; 1546 struct timespec ts;
1297 return ev_time (); 1570 return ev_time ();
1298} 1571}
1299 1572
1300#if EV_MULTIPLICITY 1573#if EV_MULTIPLICITY
1301ev_tstamp 1574ev_tstamp
1302ev_now (EV_P) 1575ev_now (EV_P) EV_THROW
1303{ 1576{
1304 return ev_rt_now; 1577 return ev_rt_now;
1305} 1578}
1306#endif 1579#endif
1307 1580
1308void 1581void
1309ev_sleep (ev_tstamp delay) 1582ev_sleep (ev_tstamp delay) EV_THROW
1310{ 1583{
1311 if (delay > 0.) 1584 if (delay > 0.)
1312 { 1585 {
1313#if EV_USE_NANOSLEEP 1586#if EV_USE_NANOSLEEP
1314 struct timespec ts; 1587 struct timespec ts;
1315 1588
1316 EV_TS_SET (ts, delay); 1589 EV_TS_SET (ts, delay);
1317 nanosleep (&ts, 0); 1590 nanosleep (&ts, 0);
1318#elif defined(_WIN32) 1591#elif defined _WIN32
1319 Sleep ((unsigned long)(delay * 1e3)); 1592 Sleep ((unsigned long)(delay * 1e3));
1320#else 1593#else
1321 struct timeval tv; 1594 struct timeval tv;
1322 1595
1323 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 1596 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
1395pendingcb (EV_P_ ev_prepare *w, int revents) 1668pendingcb (EV_P_ ev_prepare *w, int revents)
1396{ 1669{
1397} 1670}
1398 1671
1399void noinline 1672void noinline
1400ev_feed_event (EV_P_ void *w, int revents) 1673ev_feed_event (EV_P_ void *w, int revents) EV_THROW
1401{ 1674{
1402 W w_ = (W)w; 1675 W w_ = (W)w;
1403 int pri = ABSPRI (w_); 1676 int pri = ABSPRI (w_);
1404 1677
1405 if (expect_false (w_->pending)) 1678 if (expect_false (w_->pending))
1409 w_->pending = ++pendingcnt [pri]; 1682 w_->pending = ++pendingcnt [pri];
1410 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); 1683 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
1411 pendings [pri][w_->pending - 1].w = w_; 1684 pendings [pri][w_->pending - 1].w = w_;
1412 pendings [pri][w_->pending - 1].events = revents; 1685 pendings [pri][w_->pending - 1].events = revents;
1413 } 1686 }
1687
1688 pendingpri = NUMPRI - 1;
1414} 1689}
1415 1690
1416inline_speed void 1691inline_speed void
1417feed_reverse (EV_P_ W w) 1692feed_reverse (EV_P_ W w)
1418{ 1693{
1464 if (expect_true (!anfd->reify)) 1739 if (expect_true (!anfd->reify))
1465 fd_event_nocheck (EV_A_ fd, revents); 1740 fd_event_nocheck (EV_A_ fd, revents);
1466} 1741}
1467 1742
1468void 1743void
1469ev_feed_fd_event (EV_P_ int fd, int revents) 1744ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW
1470{ 1745{
1471 if (fd >= 0 && fd < anfdmax) 1746 if (fd >= 0 && fd < anfdmax)
1472 fd_event_nocheck (EV_A_ fd, revents); 1747 fd_event_nocheck (EV_A_ fd, revents);
1473} 1748}
1474 1749
1793static void noinline ecb_cold 2068static void noinline ecb_cold
1794evpipe_init (EV_P) 2069evpipe_init (EV_P)
1795{ 2070{
1796 if (!ev_is_active (&pipe_w)) 2071 if (!ev_is_active (&pipe_w))
1797 { 2072 {
2073 int fds [2];
2074
1798# if EV_USE_EVENTFD 2075# if EV_USE_EVENTFD
2076 fds [0] = -1;
1799 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 2077 fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1800 if (evfd < 0 && errno == EINVAL) 2078 if (fds [1] < 0 && errno == EINVAL)
1801 evfd = eventfd (0, 0); 2079 fds [1] = eventfd (0, 0);
1802 2080
1803 if (evfd >= 0) 2081 if (fds [1] < 0)
2082# endif
1804 { 2083 {
2084 while (pipe (fds))
2085 ev_syserr ("(libev) error creating signal/async pipe");
2086
2087 fd_intern (fds [0]);
2088 }
2089
2090 fd_intern (fds [1]);
2091
1805 evpipe [0] = -1; 2092 evpipe [0] = fds [0];
1806 fd_intern (evfd); /* doing it twice doesn't hurt */ 2093
1807 ev_io_set (&pipe_w, evfd, EV_READ); 2094 if (evpipe [1] < 0)
2095 evpipe [1] = fds [1]; /* first call, set write fd */
2096 else
2097 {
2098 /* on subsequent calls, do not change evpipe [1] */
2099 /* so that evpipe_write can always rely on its value. */
2100 /* this branch does not do anything sensible on windows, */
2101 /* so must not be executed on windows */
2102
2103 dup2 (fds [1], evpipe [1]);
2104 close (fds [1]);
2105 }
2106
2107 ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ);
2108 ev_io_start (EV_A_ &pipe_w);
2109 ev_unref (EV_A); /* watcher should not keep loop alive */
2110 }
2111}
2112
2113inline_speed void
2114evpipe_write (EV_P_ EV_ATOMIC_T *flag)
2115{
2116 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
2117
2118 if (expect_true (*flag))
2119 return;
2120
2121 *flag = 1;
2122 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
2123
2124 pipe_write_skipped = 1;
2125
2126 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
2127
2128 if (pipe_write_wanted)
2129 {
2130 int old_errno;
2131
2132 pipe_write_skipped = 0;
2133 ECB_MEMORY_FENCE_RELEASE;
2134
2135 old_errno = errno; /* save errno because write will clobber it */
2136
2137#if EV_USE_EVENTFD
2138 if (evpipe [0] < 0)
2139 {
2140 uint64_t counter = 1;
2141 write (evpipe [1], &counter, sizeof (uint64_t));
1808 } 2142 }
1809 else 2143 else
1810# endif 2144#endif
1811 { 2145 {
1812 while (pipe (evpipe)) 2146#ifdef _WIN32
1813 ev_syserr ("(libev) error creating signal/async pipe"); 2147 WSABUF buf;
1814 2148 DWORD sent;
1815 fd_intern (evpipe [0]); 2149 buf.buf = &buf;
1816 fd_intern (evpipe [1]); 2150 buf.len = 1;
1817 ev_io_set (&pipe_w, evpipe [0], EV_READ); 2151 WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0);
1818 } 2152#else
1819
1820 ev_io_start (EV_A_ &pipe_w);
1821 ev_unref (EV_A); /* watcher should not keep loop alive */
1822 }
1823}
1824
1825inline_speed void
1826evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1827{
1828 if (expect_true (*flag))
1829 return;
1830
1831 *flag = 1;
1832
1833 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
1834
1835 pipe_write_skipped = 1;
1836
1837 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
1838
1839 if (pipe_write_wanted)
1840 {
1841 int old_errno;
1842
1843 pipe_write_skipped = 0; /* just an optimisation, no fence needed */
1844
1845 old_errno = errno; /* save errno because write will clobber it */
1846
1847#if EV_USE_EVENTFD
1848 if (evfd >= 0)
1849 {
1850 uint64_t counter = 1;
1851 write (evfd, &counter, sizeof (uint64_t));
1852 }
1853 else
1854#endif
1855 {
1856 /* win32 people keep sending patches that change this write() to send() */
1857 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1858 /* so when you think this write should be a send instead, please find out */
1859 /* where your send() is from - it's definitely not the microsoft send, and */
1860 /* tell me. thank you. */
1861 write (evpipe [1], &(evpipe [1]), 1); 2153 write (evpipe [1], &(evpipe [1]), 1);
2154#endif
1862 } 2155 }
1863 2156
1864 errno = old_errno; 2157 errno = old_errno;
1865 } 2158 }
1866} 2159}
1873 int i; 2166 int i;
1874 2167
1875 if (revents & EV_READ) 2168 if (revents & EV_READ)
1876 { 2169 {
1877#if EV_USE_EVENTFD 2170#if EV_USE_EVENTFD
1878 if (evfd >= 0) 2171 if (evpipe [0] < 0)
1879 { 2172 {
1880 uint64_t counter; 2173 uint64_t counter;
1881 read (evfd, &counter, sizeof (uint64_t)); 2174 read (evpipe [1], &counter, sizeof (uint64_t));
1882 } 2175 }
1883 else 2176 else
1884#endif 2177#endif
1885 { 2178 {
1886 char dummy; 2179 char dummy[4];
1887 /* see discussion in evpipe_write when you think this read should be recv in win32 */ 2180#ifdef _WIN32
2181 WSABUF buf;
2182 DWORD recvd;
2183 DWORD flags = 0;
2184 buf.buf = dummy;
2185 buf.len = sizeof (dummy);
2186 WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0);
2187#else
1888 read (evpipe [0], &dummy, 1); 2188 read (evpipe [0], &dummy, sizeof (dummy));
2189#endif
1889 } 2190 }
1890 } 2191 }
1891 2192
1892 pipe_write_skipped = 0; 2193 pipe_write_skipped = 0;
2194
2195 ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
1893 2196
1894#if EV_SIGNAL_ENABLE 2197#if EV_SIGNAL_ENABLE
1895 if (sig_pending) 2198 if (sig_pending)
1896 { 2199 {
1897 sig_pending = 0; 2200 sig_pending = 0;
2201
2202 ECB_MEMORY_FENCE;
1898 2203
1899 for (i = EV_NSIG - 1; i--; ) 2204 for (i = EV_NSIG - 1; i--; )
1900 if (expect_false (signals [i].pending)) 2205 if (expect_false (signals [i].pending))
1901 ev_feed_signal_event (EV_A_ i + 1); 2206 ev_feed_signal_event (EV_A_ i + 1);
1902 } 2207 }
1904 2209
1905#if EV_ASYNC_ENABLE 2210#if EV_ASYNC_ENABLE
1906 if (async_pending) 2211 if (async_pending)
1907 { 2212 {
1908 async_pending = 0; 2213 async_pending = 0;
2214
2215 ECB_MEMORY_FENCE;
1909 2216
1910 for (i = asynccnt; i--; ) 2217 for (i = asynccnt; i--; )
1911 if (asyncs [i]->sent) 2218 if (asyncs [i]->sent)
1912 { 2219 {
1913 asyncs [i]->sent = 0; 2220 asyncs [i]->sent = 0;
2221 ECB_MEMORY_FENCE_RELEASE;
1914 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); 2222 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
1915 } 2223 }
1916 } 2224 }
1917#endif 2225#endif
1918} 2226}
1919 2227
1920/*****************************************************************************/ 2228/*****************************************************************************/
1921 2229
1922void 2230void
1923ev_feed_signal (int signum) 2231ev_feed_signal (int signum) EV_THROW
1924{ 2232{
1925#if EV_MULTIPLICITY 2233#if EV_MULTIPLICITY
2234 ECB_MEMORY_FENCE_ACQUIRE;
1926 EV_P = signals [signum - 1].loop; 2235 EV_P = signals [signum - 1].loop;
1927 2236
1928 if (!EV_A) 2237 if (!EV_A)
1929 return; 2238 return;
1930#endif 2239#endif
1931 2240
1932 if (!ev_active (&pipe_w))
1933 return;
1934
1935 signals [signum - 1].pending = 1; 2241 signals [signum - 1].pending = 1;
1936 evpipe_write (EV_A_ &sig_pending); 2242 evpipe_write (EV_A_ &sig_pending);
1937} 2243}
1938 2244
1939static void 2245static void
1945 2251
1946 ev_feed_signal (signum); 2252 ev_feed_signal (signum);
1947} 2253}
1948 2254
1949void noinline 2255void noinline
1950ev_feed_signal_event (EV_P_ int signum) 2256ev_feed_signal_event (EV_P_ int signum) EV_THROW
1951{ 2257{
1952 WL w; 2258 WL w;
1953 2259
1954 if (expect_false (signum <= 0 || signum > EV_NSIG)) 2260 if (expect_false (signum <= 0 || signum >= EV_NSIG))
1955 return; 2261 return;
1956 2262
1957 --signum; 2263 --signum;
1958 2264
1959#if EV_MULTIPLICITY 2265#if EV_MULTIPLICITY
1963 if (expect_false (signals [signum].loop != EV_A)) 2269 if (expect_false (signals [signum].loop != EV_A))
1964 return; 2270 return;
1965#endif 2271#endif
1966 2272
1967 signals [signum].pending = 0; 2273 signals [signum].pending = 0;
2274 ECB_MEMORY_FENCE_RELEASE;
1968 2275
1969 for (w = signals [signum].head; w; w = w->next) 2276 for (w = signals [signum].head; w; w = w->next)
1970 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 2277 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1971} 2278}
1972 2279
2071#if EV_USE_SELECT 2378#if EV_USE_SELECT
2072# include "ev_select.c" 2379# include "ev_select.c"
2073#endif 2380#endif
2074 2381
2075int ecb_cold 2382int ecb_cold
2076ev_version_major (void) 2383ev_version_major (void) EV_THROW
2077{ 2384{
2078 return EV_VERSION_MAJOR; 2385 return EV_VERSION_MAJOR;
2079} 2386}
2080 2387
2081int ecb_cold 2388int ecb_cold
2082ev_version_minor (void) 2389ev_version_minor (void) EV_THROW
2083{ 2390{
2084 return EV_VERSION_MINOR; 2391 return EV_VERSION_MINOR;
2085} 2392}
2086 2393
2087/* return true if we are running with elevated privileges and should ignore env variables */ 2394/* return true if we are running with elevated privileges and should ignore env variables */
2095 || getgid () != getegid (); 2402 || getgid () != getegid ();
2096#endif 2403#endif
2097} 2404}
2098 2405
2099unsigned int ecb_cold 2406unsigned int ecb_cold
2100ev_supported_backends (void) 2407ev_supported_backends (void) EV_THROW
2101{ 2408{
2102 unsigned int flags = 0; 2409 unsigned int flags = 0;
2103 2410
2104 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2411 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2105 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2412 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
2109 2416
2110 return flags; 2417 return flags;
2111} 2418}
2112 2419
2113unsigned int ecb_cold 2420unsigned int ecb_cold
2114ev_recommended_backends (void) 2421ev_recommended_backends (void) EV_THROW
2115{ 2422{
2116 unsigned int flags = ev_supported_backends (); 2423 unsigned int flags = ev_supported_backends ();
2117 2424
2118#ifndef __NetBSD__ 2425#ifndef __NetBSD__
2119 /* kqueue is borked on everything but netbsd apparently */ 2426 /* kqueue is borked on everything but netbsd apparently */
2131 2438
2132 return flags; 2439 return flags;
2133} 2440}
2134 2441
2135unsigned int ecb_cold 2442unsigned int ecb_cold
2136ev_embeddable_backends (void) 2443ev_embeddable_backends (void) EV_THROW
2137{ 2444{
2138 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 2445 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2139 2446
2140 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 2447 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
2141 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ 2448 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2143 2450
2144 return flags; 2451 return flags;
2145} 2452}
2146 2453
2147unsigned int 2454unsigned int
2148ev_backend (EV_P) 2455ev_backend (EV_P) EV_THROW
2149{ 2456{
2150 return backend; 2457 return backend;
2151} 2458}
2152 2459
2153#if EV_FEATURE_API 2460#if EV_FEATURE_API
2154unsigned int 2461unsigned int
2155ev_iteration (EV_P) 2462ev_iteration (EV_P) EV_THROW
2156{ 2463{
2157 return loop_count; 2464 return loop_count;
2158} 2465}
2159 2466
2160unsigned int 2467unsigned int
2161ev_depth (EV_P) 2468ev_depth (EV_P) EV_THROW
2162{ 2469{
2163 return loop_depth; 2470 return loop_depth;
2164} 2471}
2165 2472
2166void 2473void
2167ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 2474ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2168{ 2475{
2169 io_blocktime = interval; 2476 io_blocktime = interval;
2170} 2477}
2171 2478
2172void 2479void
2173ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 2480ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2174{ 2481{
2175 timeout_blocktime = interval; 2482 timeout_blocktime = interval;
2176} 2483}
2177 2484
2178void 2485void
2179ev_set_userdata (EV_P_ void *data) 2486ev_set_userdata (EV_P_ void *data) EV_THROW
2180{ 2487{
2181 userdata = data; 2488 userdata = data;
2182} 2489}
2183 2490
2184void * 2491void *
2185ev_userdata (EV_P) 2492ev_userdata (EV_P) EV_THROW
2186{ 2493{
2187 return userdata; 2494 return userdata;
2188} 2495}
2189 2496
2190void 2497void
2191ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 2498ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) EV_THROW
2192{ 2499{
2193 invoke_cb = invoke_pending_cb; 2500 invoke_cb = invoke_pending_cb;
2194} 2501}
2195 2502
2196void 2503void
2197ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) 2504ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW
2198{ 2505{
2199 release_cb = release; 2506 release_cb = release;
2200 acquire_cb = acquire; 2507 acquire_cb = acquire;
2201} 2508}
2202#endif 2509#endif
2203 2510
2204/* initialise a loop structure, must be zero-initialised */ 2511/* initialise a loop structure, must be zero-initialised */
2205static void noinline ecb_cold 2512static void noinline ecb_cold
2206loop_init (EV_P_ unsigned int flags) 2513loop_init (EV_P_ unsigned int flags) EV_THROW
2207{ 2514{
2208 if (!backend) 2515 if (!backend)
2209 { 2516 {
2210 origflags = flags; 2517 origflags = flags;
2211 2518
2256#if EV_ASYNC_ENABLE 2563#if EV_ASYNC_ENABLE
2257 async_pending = 0; 2564 async_pending = 0;
2258#endif 2565#endif
2259 pipe_write_skipped = 0; 2566 pipe_write_skipped = 0;
2260 pipe_write_wanted = 0; 2567 pipe_write_wanted = 0;
2568 evpipe [0] = -1;
2569 evpipe [1] = -1;
2261#if EV_USE_INOTIFY 2570#if EV_USE_INOTIFY
2262 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 2571 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
2263#endif 2572#endif
2264#if EV_USE_SIGNALFD 2573#if EV_USE_SIGNALFD
2265 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 2574 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
2316 EV_INVOKE_PENDING; 2625 EV_INVOKE_PENDING;
2317 } 2626 }
2318#endif 2627#endif
2319 2628
2320#if EV_CHILD_ENABLE 2629#if EV_CHILD_ENABLE
2321 if (ev_is_active (&childev)) 2630 if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
2322 { 2631 {
2323 ev_ref (EV_A); /* child watcher */ 2632 ev_ref (EV_A); /* child watcher */
2324 ev_signal_stop (EV_A_ &childev); 2633 ev_signal_stop (EV_A_ &childev);
2325 } 2634 }
2326#endif 2635#endif
2328 if (ev_is_active (&pipe_w)) 2637 if (ev_is_active (&pipe_w))
2329 { 2638 {
2330 /*ev_ref (EV_A);*/ 2639 /*ev_ref (EV_A);*/
2331 /*ev_io_stop (EV_A_ &pipe_w);*/ 2640 /*ev_io_stop (EV_A_ &pipe_w);*/
2332 2641
2333#if EV_USE_EVENTFD
2334 if (evfd >= 0)
2335 close (evfd);
2336#endif
2337
2338 if (evpipe [0] >= 0)
2339 {
2340 EV_WIN32_CLOSE_FD (evpipe [0]); 2642 if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]);
2341 EV_WIN32_CLOSE_FD (evpipe [1]); 2643 if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]);
2342 }
2343 } 2644 }
2344 2645
2345#if EV_USE_SIGNALFD 2646#if EV_USE_SIGNALFD
2346 if (ev_is_active (&sigfd_w)) 2647 if (ev_is_active (&sigfd_w))
2347 close (sigfd); 2648 close (sigfd);
2433#endif 2734#endif
2434#if EV_USE_INOTIFY 2735#if EV_USE_INOTIFY
2435 infy_fork (EV_A); 2736 infy_fork (EV_A);
2436#endif 2737#endif
2437 2738
2739#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2438 if (ev_is_active (&pipe_w)) 2740 if (ev_is_active (&pipe_w))
2439 { 2741 {
2440 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ 2742 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
2441 2743
2442 ev_ref (EV_A); 2744 ev_ref (EV_A);
2443 ev_io_stop (EV_A_ &pipe_w); 2745 ev_io_stop (EV_A_ &pipe_w);
2444 2746
2445#if EV_USE_EVENTFD
2446 if (evfd >= 0)
2447 close (evfd);
2448#endif
2449
2450 if (evpipe [0] >= 0) 2747 if (evpipe [0] >= 0)
2451 {
2452 EV_WIN32_CLOSE_FD (evpipe [0]); 2748 EV_WIN32_CLOSE_FD (evpipe [0]);
2453 EV_WIN32_CLOSE_FD (evpipe [1]);
2454 }
2455 2749
2456#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2457 evpipe_init (EV_A); 2750 evpipe_init (EV_A);
2458 /* now iterate over everything, in case we missed something */ 2751 /* iterate over everything, in case we missed something before */
2459 pipecb (EV_A_ &pipe_w, EV_READ); 2752 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
2460#endif
2461 } 2753 }
2754#endif
2462 2755
2463 postfork = 0; 2756 postfork = 0;
2464} 2757}
2465 2758
2466#if EV_MULTIPLICITY 2759#if EV_MULTIPLICITY
2467 2760
2468struct ev_loop * ecb_cold 2761struct ev_loop * ecb_cold
2469ev_loop_new (unsigned int flags) 2762ev_loop_new (unsigned int flags) EV_THROW
2470{ 2763{
2471 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2764 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2472 2765
2473 memset (EV_A, 0, sizeof (struct ev_loop)); 2766 memset (EV_A, 0, sizeof (struct ev_loop));
2474 loop_init (EV_A_ flags); 2767 loop_init (EV_A_ flags);
2518} 2811}
2519#endif 2812#endif
2520 2813
2521#if EV_FEATURE_API 2814#if EV_FEATURE_API
2522void ecb_cold 2815void ecb_cold
2523ev_verify (EV_P) 2816ev_verify (EV_P) EV_THROW
2524{ 2817{
2525#if EV_VERIFY 2818#if EV_VERIFY
2526 int i; 2819 int i;
2527 WL w; 2820 WL w, w2;
2528 2821
2529 assert (activecnt >= -1); 2822 assert (activecnt >= -1);
2530 2823
2531 assert (fdchangemax >= fdchangecnt); 2824 assert (fdchangemax >= fdchangecnt);
2532 for (i = 0; i < fdchangecnt; ++i) 2825 for (i = 0; i < fdchangecnt; ++i)
2533 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); 2826 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
2534 2827
2535 assert (anfdmax >= 0); 2828 assert (anfdmax >= 0);
2536 for (i = 0; i < anfdmax; ++i) 2829 for (i = 0; i < anfdmax; ++i)
2830 {
2831 int j = 0;
2832
2537 for (w = anfds [i].head; w; w = w->next) 2833 for (w = w2 = anfds [i].head; w; w = w->next)
2538 { 2834 {
2539 verify_watcher (EV_A_ (W)w); 2835 verify_watcher (EV_A_ (W)w);
2836
2837 if (j++ & 1)
2838 {
2839 assert (("libev: io watcher list contains a loop", w != w2));
2840 w2 = w2->next;
2841 }
2842
2540 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); 2843 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
2541 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); 2844 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
2542 } 2845 }
2846 }
2543 2847
2544 assert (timermax >= timercnt); 2848 assert (timermax >= timercnt);
2545 verify_heap (EV_A_ timers, timercnt); 2849 verify_heap (EV_A_ timers, timercnt);
2546 2850
2547#if EV_PERIODIC_ENABLE 2851#if EV_PERIODIC_ENABLE
2597#if EV_MULTIPLICITY 2901#if EV_MULTIPLICITY
2598struct ev_loop * ecb_cold 2902struct ev_loop * ecb_cold
2599#else 2903#else
2600int 2904int
2601#endif 2905#endif
2602ev_default_loop (unsigned int flags) 2906ev_default_loop (unsigned int flags) EV_THROW
2603{ 2907{
2604 if (!ev_default_loop_ptr) 2908 if (!ev_default_loop_ptr)
2605 { 2909 {
2606#if EV_MULTIPLICITY 2910#if EV_MULTIPLICITY
2607 EV_P = ev_default_loop_ptr = &default_loop_struct; 2911 EV_P = ev_default_loop_ptr = &default_loop_struct;
2626 2930
2627 return ev_default_loop_ptr; 2931 return ev_default_loop_ptr;
2628} 2932}
2629 2933
2630void 2934void
2631ev_loop_fork (EV_P) 2935ev_loop_fork (EV_P) EV_THROW
2632{ 2936{
2633 postfork = 1; /* must be in line with ev_default_fork */ 2937 postfork = 1;
2634} 2938}
2635 2939
2636/*****************************************************************************/ 2940/*****************************************************************************/
2637 2941
2638void 2942void
2640{ 2944{
2641 EV_CB_INVOKE ((W)w, revents); 2945 EV_CB_INVOKE ((W)w, revents);
2642} 2946}
2643 2947
2644unsigned int 2948unsigned int
2645ev_pending_count (EV_P) 2949ev_pending_count (EV_P) EV_THROW
2646{ 2950{
2647 int pri; 2951 int pri;
2648 unsigned int count = 0; 2952 unsigned int count = 0;
2649 2953
2650 for (pri = NUMPRI; pri--; ) 2954 for (pri = NUMPRI; pri--; )
2654} 2958}
2655 2959
2656void noinline 2960void noinline
2657ev_invoke_pending (EV_P) 2961ev_invoke_pending (EV_P)
2658{ 2962{
2659 int pri; 2963 pendingpri = NUMPRI;
2660 2964
2661 for (pri = NUMPRI; pri--; ) 2965 while (pendingpri) /* pendingpri possibly gets modified in the inner loop */
2966 {
2967 --pendingpri;
2968
2662 while (pendingcnt [pri]) 2969 while (pendingcnt [pendingpri])
2663 { 2970 {
2664 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 2971 ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
2665 2972
2666 p->w->pending = 0; 2973 p->w->pending = 0;
2667 EV_CB_INVOKE (p->w, p->events); 2974 EV_CB_INVOKE (p->w, p->events);
2668 EV_FREQUENT_CHECK; 2975 EV_FREQUENT_CHECK;
2669 } 2976 }
2977 }
2670} 2978}
2671 2979
2672#if EV_IDLE_ENABLE 2980#if EV_IDLE_ENABLE
2673/* make idle watchers pending. this handles the "call-idle */ 2981/* make idle watchers pending. this handles the "call-idle */
2674/* only when higher priorities are idle" logic */ 2982/* only when higher priorities are idle" logic */
2764{ 3072{
2765 EV_FREQUENT_CHECK; 3073 EV_FREQUENT_CHECK;
2766 3074
2767 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 3075 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
2768 { 3076 {
2769 int feed_count = 0;
2770
2771 do 3077 do
2772 { 3078 {
2773 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 3079 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
2774 3080
2775 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ 3081 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
2909 3215
2910 mn_now = ev_rt_now; 3216 mn_now = ev_rt_now;
2911 } 3217 }
2912} 3218}
2913 3219
2914void 3220int
2915ev_run (EV_P_ int flags) 3221ev_run (EV_P_ int flags)
2916{ 3222{
2917#if EV_FEATURE_API 3223#if EV_FEATURE_API
2918 ++loop_depth; 3224 ++loop_depth;
2919#endif 3225#endif
3034 backend_poll (EV_A_ waittime); 3340 backend_poll (EV_A_ waittime);
3035 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 3341 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
3036 3342
3037 pipe_write_wanted = 0; /* just an optimisation, no fence needed */ 3343 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
3038 3344
3345 ECB_MEMORY_FENCE_ACQUIRE;
3039 if (pipe_write_skipped) 3346 if (pipe_write_skipped)
3040 { 3347 {
3041 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); 3348 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
3042 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3349 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3043 } 3350 }
3076 loop_done = EVBREAK_CANCEL; 3383 loop_done = EVBREAK_CANCEL;
3077 3384
3078#if EV_FEATURE_API 3385#if EV_FEATURE_API
3079 --loop_depth; 3386 --loop_depth;
3080#endif 3387#endif
3388
3389 return activecnt;
3081} 3390}
3082 3391
3083void 3392void
3084ev_break (EV_P_ int how) 3393ev_break (EV_P_ int how) EV_THROW
3085{ 3394{
3086 loop_done = how; 3395 loop_done = how;
3087} 3396}
3088 3397
3089void 3398void
3090ev_ref (EV_P) 3399ev_ref (EV_P) EV_THROW
3091{ 3400{
3092 ++activecnt; 3401 ++activecnt;
3093} 3402}
3094 3403
3095void 3404void
3096ev_unref (EV_P) 3405ev_unref (EV_P) EV_THROW
3097{ 3406{
3098 --activecnt; 3407 --activecnt;
3099} 3408}
3100 3409
3101void 3410void
3102ev_now_update (EV_P) 3411ev_now_update (EV_P) EV_THROW
3103{ 3412{
3104 time_update (EV_A_ 1e100); 3413 time_update (EV_A_ 1e100);
3105} 3414}
3106 3415
3107void 3416void
3108ev_suspend (EV_P) 3417ev_suspend (EV_P) EV_THROW
3109{ 3418{
3110 ev_now_update (EV_A); 3419 ev_now_update (EV_A);
3111} 3420}
3112 3421
3113void 3422void
3114ev_resume (EV_P) 3423ev_resume (EV_P) EV_THROW
3115{ 3424{
3116 ev_tstamp mn_prev = mn_now; 3425 ev_tstamp mn_prev = mn_now;
3117 3426
3118 ev_now_update (EV_A); 3427 ev_now_update (EV_A);
3119 timers_reschedule (EV_A_ mn_now - mn_prev); 3428 timers_reschedule (EV_A_ mn_now - mn_prev);
3158 w->pending = 0; 3467 w->pending = 0;
3159 } 3468 }
3160} 3469}
3161 3470
3162int 3471int
3163ev_clear_pending (EV_P_ void *w) 3472ev_clear_pending (EV_P_ void *w) EV_THROW
3164{ 3473{
3165 W w_ = (W)w; 3474 W w_ = (W)w;
3166 int pending = w_->pending; 3475 int pending = w_->pending;
3167 3476
3168 if (expect_true (pending)) 3477 if (expect_true (pending))
3201} 3510}
3202 3511
3203/*****************************************************************************/ 3512/*****************************************************************************/
3204 3513
3205void noinline 3514void noinline
3206ev_io_start (EV_P_ ev_io *w) 3515ev_io_start (EV_P_ ev_io *w) EV_THROW
3207{ 3516{
3208 int fd = w->fd; 3517 int fd = w->fd;
3209 3518
3210 if (expect_false (ev_is_active (w))) 3519 if (expect_false (ev_is_active (w)))
3211 return; 3520 return;
3217 3526
3218 ev_start (EV_A_ (W)w, 1); 3527 ev_start (EV_A_ (W)w, 1);
3219 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 3528 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
3220 wlist_add (&anfds[fd].head, (WL)w); 3529 wlist_add (&anfds[fd].head, (WL)w);
3221 3530
3531 /* common bug, apparently */
3532 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3533
3222 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); 3534 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
3223 w->events &= ~EV__IOFDSET; 3535 w->events &= ~EV__IOFDSET;
3224 3536
3225 EV_FREQUENT_CHECK; 3537 EV_FREQUENT_CHECK;
3226} 3538}
3227 3539
3228void noinline 3540void noinline
3229ev_io_stop (EV_P_ ev_io *w) 3541ev_io_stop (EV_P_ ev_io *w) EV_THROW
3230{ 3542{
3231 clear_pending (EV_A_ (W)w); 3543 clear_pending (EV_A_ (W)w);
3232 if (expect_false (!ev_is_active (w))) 3544 if (expect_false (!ev_is_active (w)))
3233 return; 3545 return;
3234 3546
3243 3555
3244 EV_FREQUENT_CHECK; 3556 EV_FREQUENT_CHECK;
3245} 3557}
3246 3558
3247void noinline 3559void noinline
3248ev_timer_start (EV_P_ ev_timer *w) 3560ev_timer_start (EV_P_ ev_timer *w) EV_THROW
3249{ 3561{
3250 if (expect_false (ev_is_active (w))) 3562 if (expect_false (ev_is_active (w)))
3251 return; 3563 return;
3252 3564
3253 ev_at (w) += mn_now; 3565 ev_at (w) += mn_now;
3267 3579
3268 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ 3580 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
3269} 3581}
3270 3582
3271void noinline 3583void noinline
3272ev_timer_stop (EV_P_ ev_timer *w) 3584ev_timer_stop (EV_P_ ev_timer *w) EV_THROW
3273{ 3585{
3274 clear_pending (EV_A_ (W)w); 3586 clear_pending (EV_A_ (W)w);
3275 if (expect_false (!ev_is_active (w))) 3587 if (expect_false (!ev_is_active (w)))
3276 return; 3588 return;
3277 3589
3297 3609
3298 EV_FREQUENT_CHECK; 3610 EV_FREQUENT_CHECK;
3299} 3611}
3300 3612
3301void noinline 3613void noinline
3302ev_timer_again (EV_P_ ev_timer *w) 3614ev_timer_again (EV_P_ ev_timer *w) EV_THROW
3303{ 3615{
3304 EV_FREQUENT_CHECK; 3616 EV_FREQUENT_CHECK;
3305 3617
3306 clear_pending (EV_A_ (W)w); 3618 clear_pending (EV_A_ (W)w);
3307 3619
3324 3636
3325 EV_FREQUENT_CHECK; 3637 EV_FREQUENT_CHECK;
3326} 3638}
3327 3639
3328ev_tstamp 3640ev_tstamp
3329ev_timer_remaining (EV_P_ ev_timer *w) 3641ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW
3330{ 3642{
3331 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 3643 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
3332} 3644}
3333 3645
3334#if EV_PERIODIC_ENABLE 3646#if EV_PERIODIC_ENABLE
3335void noinline 3647void noinline
3336ev_periodic_start (EV_P_ ev_periodic *w) 3648ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW
3337{ 3649{
3338 if (expect_false (ev_is_active (w))) 3650 if (expect_false (ev_is_active (w)))
3339 return; 3651 return;
3340 3652
3341 if (w->reschedule_cb) 3653 if (w->reschedule_cb)
3361 3673
3362 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ 3674 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
3363} 3675}
3364 3676
3365void noinline 3677void noinline
3366ev_periodic_stop (EV_P_ ev_periodic *w) 3678ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW
3367{ 3679{
3368 clear_pending (EV_A_ (W)w); 3680 clear_pending (EV_A_ (W)w);
3369 if (expect_false (!ev_is_active (w))) 3681 if (expect_false (!ev_is_active (w)))
3370 return; 3682 return;
3371 3683
3389 3701
3390 EV_FREQUENT_CHECK; 3702 EV_FREQUENT_CHECK;
3391} 3703}
3392 3704
3393void noinline 3705void noinline
3394ev_periodic_again (EV_P_ ev_periodic *w) 3706ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW
3395{ 3707{
3396 /* TODO: use adjustheap and recalculation */ 3708 /* TODO: use adjustheap and recalculation */
3397 ev_periodic_stop (EV_A_ w); 3709 ev_periodic_stop (EV_A_ w);
3398 ev_periodic_start (EV_A_ w); 3710 ev_periodic_start (EV_A_ w);
3399} 3711}
3404#endif 3716#endif
3405 3717
3406#if EV_SIGNAL_ENABLE 3718#if EV_SIGNAL_ENABLE
3407 3719
3408void noinline 3720void noinline
3409ev_signal_start (EV_P_ ev_signal *w) 3721ev_signal_start (EV_P_ ev_signal *w) EV_THROW
3410{ 3722{
3411 if (expect_false (ev_is_active (w))) 3723 if (expect_false (ev_is_active (w)))
3412 return; 3724 return;
3413 3725
3414 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 3726 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
3416#if EV_MULTIPLICITY 3728#if EV_MULTIPLICITY
3417 assert (("libev: a signal must not be attached to two different loops", 3729 assert (("libev: a signal must not be attached to two different loops",
3418 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); 3730 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
3419 3731
3420 signals [w->signum - 1].loop = EV_A; 3732 signals [w->signum - 1].loop = EV_A;
3733 ECB_MEMORY_FENCE_RELEASE;
3421#endif 3734#endif
3422 3735
3423 EV_FREQUENT_CHECK; 3736 EV_FREQUENT_CHECK;
3424 3737
3425#if EV_USE_SIGNALFD 3738#if EV_USE_SIGNALFD
3485 3798
3486 EV_FREQUENT_CHECK; 3799 EV_FREQUENT_CHECK;
3487} 3800}
3488 3801
3489void noinline 3802void noinline
3490ev_signal_stop (EV_P_ ev_signal *w) 3803ev_signal_stop (EV_P_ ev_signal *w) EV_THROW
3491{ 3804{
3492 clear_pending (EV_A_ (W)w); 3805 clear_pending (EV_A_ (W)w);
3493 if (expect_false (!ev_is_active (w))) 3806 if (expect_false (!ev_is_active (w)))
3494 return; 3807 return;
3495 3808
3526#endif 3839#endif
3527 3840
3528#if EV_CHILD_ENABLE 3841#if EV_CHILD_ENABLE
3529 3842
3530void 3843void
3531ev_child_start (EV_P_ ev_child *w) 3844ev_child_start (EV_P_ ev_child *w) EV_THROW
3532{ 3845{
3533#if EV_MULTIPLICITY 3846#if EV_MULTIPLICITY
3534 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 3847 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
3535#endif 3848#endif
3536 if (expect_false (ev_is_active (w))) 3849 if (expect_false (ev_is_active (w)))
3543 3856
3544 EV_FREQUENT_CHECK; 3857 EV_FREQUENT_CHECK;
3545} 3858}
3546 3859
3547void 3860void
3548ev_child_stop (EV_P_ ev_child *w) 3861ev_child_stop (EV_P_ ev_child *w) EV_THROW
3549{ 3862{
3550 clear_pending (EV_A_ (W)w); 3863 clear_pending (EV_A_ (W)w);
3551 if (expect_false (!ev_is_active (w))) 3864 if (expect_false (!ev_is_active (w)))
3552 return; 3865 return;
3553 3866
3580# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX) 3893# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
3581 3894
3582static void noinline 3895static void noinline
3583infy_add (EV_P_ ev_stat *w) 3896infy_add (EV_P_ ev_stat *w)
3584{ 3897{
3585 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); 3898 w->wd = inotify_add_watch (fs_fd, w->path,
3899 IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY
3900 | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO
3901 | IN_DONT_FOLLOW | IN_MASK_ADD);
3586 3902
3587 if (w->wd >= 0) 3903 if (w->wd >= 0)
3588 { 3904 {
3589 struct statfs sfs; 3905 struct statfs sfs;
3590 3906
3594 3910
3595 if (!fs_2625) 3911 if (!fs_2625)
3596 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; 3912 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3597 else if (!statfs (w->path, &sfs) 3913 else if (!statfs (w->path, &sfs)
3598 && (sfs.f_type == 0x1373 /* devfs */ 3914 && (sfs.f_type == 0x1373 /* devfs */
3915 || sfs.f_type == 0x4006 /* fat */
3916 || sfs.f_type == 0x4d44 /* msdos */
3599 || sfs.f_type == 0xEF53 /* ext2/3 */ 3917 || sfs.f_type == 0xEF53 /* ext2/3 */
3918 || sfs.f_type == 0x72b6 /* jffs2 */
3919 || sfs.f_type == 0x858458f6 /* ramfs */
3920 || sfs.f_type == 0x5346544e /* ntfs */
3600 || sfs.f_type == 0x3153464a /* jfs */ 3921 || sfs.f_type == 0x3153464a /* jfs */
3922 || sfs.f_type == 0x9123683e /* btrfs */
3601 || sfs.f_type == 0x52654973 /* reiser3 */ 3923 || sfs.f_type == 0x52654973 /* reiser3 */
3602 || sfs.f_type == 0x01021994 /* tempfs */ 3924 || sfs.f_type == 0x01021994 /* tmpfs */
3603 || sfs.f_type == 0x58465342 /* xfs */)) 3925 || sfs.f_type == 0x58465342 /* xfs */))
3604 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */ 3926 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
3605 else 3927 else
3606 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */ 3928 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
3607 } 3929 }
3720} 4042}
3721 4043
3722inline_size int 4044inline_size int
3723infy_newfd (void) 4045infy_newfd (void)
3724{ 4046{
3725#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) 4047#if defined IN_CLOEXEC && defined IN_NONBLOCK
3726 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); 4048 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3727 if (fd >= 0) 4049 if (fd >= 0)
3728 return fd; 4050 return fd;
3729#endif 4051#endif
3730 return inotify_init (); 4052 return inotify_init ();
3805#else 4127#else
3806# define EV_LSTAT(p,b) lstat (p, b) 4128# define EV_LSTAT(p,b) lstat (p, b)
3807#endif 4129#endif
3808 4130
3809void 4131void
3810ev_stat_stat (EV_P_ ev_stat *w) 4132ev_stat_stat (EV_P_ ev_stat *w) EV_THROW
3811{ 4133{
3812 if (lstat (w->path, &w->attr) < 0) 4134 if (lstat (w->path, &w->attr) < 0)
3813 w->attr.st_nlink = 0; 4135 w->attr.st_nlink = 0;
3814 else if (!w->attr.st_nlink) 4136 else if (!w->attr.st_nlink)
3815 w->attr.st_nlink = 1; 4137 w->attr.st_nlink = 1;
3854 ev_feed_event (EV_A_ w, EV_STAT); 4176 ev_feed_event (EV_A_ w, EV_STAT);
3855 } 4177 }
3856} 4178}
3857 4179
3858void 4180void
3859ev_stat_start (EV_P_ ev_stat *w) 4181ev_stat_start (EV_P_ ev_stat *w) EV_THROW
3860{ 4182{
3861 if (expect_false (ev_is_active (w))) 4183 if (expect_false (ev_is_active (w)))
3862 return; 4184 return;
3863 4185
3864 ev_stat_stat (EV_A_ w); 4186 ev_stat_stat (EV_A_ w);
3885 4207
3886 EV_FREQUENT_CHECK; 4208 EV_FREQUENT_CHECK;
3887} 4209}
3888 4210
3889void 4211void
3890ev_stat_stop (EV_P_ ev_stat *w) 4212ev_stat_stop (EV_P_ ev_stat *w) EV_THROW
3891{ 4213{
3892 clear_pending (EV_A_ (W)w); 4214 clear_pending (EV_A_ (W)w);
3893 if (expect_false (!ev_is_active (w))) 4215 if (expect_false (!ev_is_active (w)))
3894 return; 4216 return;
3895 4217
3911} 4233}
3912#endif 4234#endif
3913 4235
3914#if EV_IDLE_ENABLE 4236#if EV_IDLE_ENABLE
3915void 4237void
3916ev_idle_start (EV_P_ ev_idle *w) 4238ev_idle_start (EV_P_ ev_idle *w) EV_THROW
3917{ 4239{
3918 if (expect_false (ev_is_active (w))) 4240 if (expect_false (ev_is_active (w)))
3919 return; 4241 return;
3920 4242
3921 pri_adjust (EV_A_ (W)w); 4243 pri_adjust (EV_A_ (W)w);
3934 4256
3935 EV_FREQUENT_CHECK; 4257 EV_FREQUENT_CHECK;
3936} 4258}
3937 4259
3938void 4260void
3939ev_idle_stop (EV_P_ ev_idle *w) 4261ev_idle_stop (EV_P_ ev_idle *w) EV_THROW
3940{ 4262{
3941 clear_pending (EV_A_ (W)w); 4263 clear_pending (EV_A_ (W)w);
3942 if (expect_false (!ev_is_active (w))) 4264 if (expect_false (!ev_is_active (w)))
3943 return; 4265 return;
3944 4266
3958} 4280}
3959#endif 4281#endif
3960 4282
3961#if EV_PREPARE_ENABLE 4283#if EV_PREPARE_ENABLE
3962void 4284void
3963ev_prepare_start (EV_P_ ev_prepare *w) 4285ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW
3964{ 4286{
3965 if (expect_false (ev_is_active (w))) 4287 if (expect_false (ev_is_active (w)))
3966 return; 4288 return;
3967 4289
3968 EV_FREQUENT_CHECK; 4290 EV_FREQUENT_CHECK;
3973 4295
3974 EV_FREQUENT_CHECK; 4296 EV_FREQUENT_CHECK;
3975} 4297}
3976 4298
3977void 4299void
3978ev_prepare_stop (EV_P_ ev_prepare *w) 4300ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW
3979{ 4301{
3980 clear_pending (EV_A_ (W)w); 4302 clear_pending (EV_A_ (W)w);
3981 if (expect_false (!ev_is_active (w))) 4303 if (expect_false (!ev_is_active (w)))
3982 return; 4304 return;
3983 4305
3996} 4318}
3997#endif 4319#endif
3998 4320
3999#if EV_CHECK_ENABLE 4321#if EV_CHECK_ENABLE
4000void 4322void
4001ev_check_start (EV_P_ ev_check *w) 4323ev_check_start (EV_P_ ev_check *w) EV_THROW
4002{ 4324{
4003 if (expect_false (ev_is_active (w))) 4325 if (expect_false (ev_is_active (w)))
4004 return; 4326 return;
4005 4327
4006 EV_FREQUENT_CHECK; 4328 EV_FREQUENT_CHECK;
4011 4333
4012 EV_FREQUENT_CHECK; 4334 EV_FREQUENT_CHECK;
4013} 4335}
4014 4336
4015void 4337void
4016ev_check_stop (EV_P_ ev_check *w) 4338ev_check_stop (EV_P_ ev_check *w) EV_THROW
4017{ 4339{
4018 clear_pending (EV_A_ (W)w); 4340 clear_pending (EV_A_ (W)w);
4019 if (expect_false (!ev_is_active (w))) 4341 if (expect_false (!ev_is_active (w)))
4020 return; 4342 return;
4021 4343
4034} 4356}
4035#endif 4357#endif
4036 4358
4037#if EV_EMBED_ENABLE 4359#if EV_EMBED_ENABLE
4038void noinline 4360void noinline
4039ev_embed_sweep (EV_P_ ev_embed *w) 4361ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW
4040{ 4362{
4041 ev_run (w->other, EVRUN_NOWAIT); 4363 ev_run (w->other, EVRUN_NOWAIT);
4042} 4364}
4043 4365
4044static void 4366static void
4092 ev_idle_stop (EV_A_ idle); 4414 ev_idle_stop (EV_A_ idle);
4093} 4415}
4094#endif 4416#endif
4095 4417
4096void 4418void
4097ev_embed_start (EV_P_ ev_embed *w) 4419ev_embed_start (EV_P_ ev_embed *w) EV_THROW
4098{ 4420{
4099 if (expect_false (ev_is_active (w))) 4421 if (expect_false (ev_is_active (w)))
4100 return; 4422 return;
4101 4423
4102 { 4424 {
4123 4445
4124 EV_FREQUENT_CHECK; 4446 EV_FREQUENT_CHECK;
4125} 4447}
4126 4448
4127void 4449void
4128ev_embed_stop (EV_P_ ev_embed *w) 4450ev_embed_stop (EV_P_ ev_embed *w) EV_THROW
4129{ 4451{
4130 clear_pending (EV_A_ (W)w); 4452 clear_pending (EV_A_ (W)w);
4131 if (expect_false (!ev_is_active (w))) 4453 if (expect_false (!ev_is_active (w)))
4132 return; 4454 return;
4133 4455
4143} 4465}
4144#endif 4466#endif
4145 4467
4146#if EV_FORK_ENABLE 4468#if EV_FORK_ENABLE
4147void 4469void
4148ev_fork_start (EV_P_ ev_fork *w) 4470ev_fork_start (EV_P_ ev_fork *w) EV_THROW
4149{ 4471{
4150 if (expect_false (ev_is_active (w))) 4472 if (expect_false (ev_is_active (w)))
4151 return; 4473 return;
4152 4474
4153 EV_FREQUENT_CHECK; 4475 EV_FREQUENT_CHECK;
4158 4480
4159 EV_FREQUENT_CHECK; 4481 EV_FREQUENT_CHECK;
4160} 4482}
4161 4483
4162void 4484void
4163ev_fork_stop (EV_P_ ev_fork *w) 4485ev_fork_stop (EV_P_ ev_fork *w) EV_THROW
4164{ 4486{
4165 clear_pending (EV_A_ (W)w); 4487 clear_pending (EV_A_ (W)w);
4166 if (expect_false (!ev_is_active (w))) 4488 if (expect_false (!ev_is_active (w)))
4167 return; 4489 return;
4168 4490
4181} 4503}
4182#endif 4504#endif
4183 4505
4184#if EV_CLEANUP_ENABLE 4506#if EV_CLEANUP_ENABLE
4185void 4507void
4186ev_cleanup_start (EV_P_ ev_cleanup *w) 4508ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW
4187{ 4509{
4188 if (expect_false (ev_is_active (w))) 4510 if (expect_false (ev_is_active (w)))
4189 return; 4511 return;
4190 4512
4191 EV_FREQUENT_CHECK; 4513 EV_FREQUENT_CHECK;
4198 ev_unref (EV_A); 4520 ev_unref (EV_A);
4199 EV_FREQUENT_CHECK; 4521 EV_FREQUENT_CHECK;
4200} 4522}
4201 4523
4202void 4524void
4203ev_cleanup_stop (EV_P_ ev_cleanup *w) 4525ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW
4204{ 4526{
4205 clear_pending (EV_A_ (W)w); 4527 clear_pending (EV_A_ (W)w);
4206 if (expect_false (!ev_is_active (w))) 4528 if (expect_false (!ev_is_active (w)))
4207 return; 4529 return;
4208 4530
4222} 4544}
4223#endif 4545#endif
4224 4546
4225#if EV_ASYNC_ENABLE 4547#if EV_ASYNC_ENABLE
4226void 4548void
4227ev_async_start (EV_P_ ev_async *w) 4549ev_async_start (EV_P_ ev_async *w) EV_THROW
4228{ 4550{
4229 if (expect_false (ev_is_active (w))) 4551 if (expect_false (ev_is_active (w)))
4230 return; 4552 return;
4231 4553
4232 w->sent = 0; 4554 w->sent = 0;
4241 4563
4242 EV_FREQUENT_CHECK; 4564 EV_FREQUENT_CHECK;
4243} 4565}
4244 4566
4245void 4567void
4246ev_async_stop (EV_P_ ev_async *w) 4568ev_async_stop (EV_P_ ev_async *w) EV_THROW
4247{ 4569{
4248 clear_pending (EV_A_ (W)w); 4570 clear_pending (EV_A_ (W)w);
4249 if (expect_false (!ev_is_active (w))) 4571 if (expect_false (!ev_is_active (w)))
4250 return; 4572 return;
4251 4573
4262 4584
4263 EV_FREQUENT_CHECK; 4585 EV_FREQUENT_CHECK;
4264} 4586}
4265 4587
4266void 4588void
4267ev_async_send (EV_P_ ev_async *w) 4589ev_async_send (EV_P_ ev_async *w) EV_THROW
4268{ 4590{
4269 w->sent = 1; 4591 w->sent = 1;
4270 evpipe_write (EV_A_ &async_pending); 4592 evpipe_write (EV_A_ &async_pending);
4271} 4593}
4272#endif 4594#endif
4309 4631
4310 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); 4632 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
4311} 4633}
4312 4634
4313void 4635void
4314ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 4636ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW
4315{ 4637{
4316 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 4638 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
4317 4639
4318 if (expect_false (!once)) 4640 if (expect_false (!once))
4319 { 4641 {
4341 4663
4342/*****************************************************************************/ 4664/*****************************************************************************/
4343 4665
4344#if EV_WALK_ENABLE 4666#if EV_WALK_ENABLE
4345void ecb_cold 4667void ecb_cold
4346ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 4668ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW
4347{ 4669{
4348 int i, j; 4670 int i, j;
4349 ev_watcher_list *wl, *wn; 4671 ev_watcher_list *wl, *wn;
4350 4672
4351 if (types & (EV_IO | EV_EMBED)) 4673 if (types & (EV_IO | EV_EMBED))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines