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

Comparing libev/ev.c (file contents):
Revision 1.400 by root, Sat Oct 15 09:05:03 2011 UTC vs.
Revision 1.440 by root, Tue May 29 21:37:14 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
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>
467/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ 475/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
468/* ECB.H BEGIN */ 476/* ECB.H BEGIN */
469/* 477/*
470 * libecb - http://software.schmorp.de/pkg/libecb 478 * libecb - http://software.schmorp.de/pkg/libecb
471 * 479 *
472 * Copyright (©) 2009-2011 Marc Alexander Lehmann <libecb@schmorp.de> 480 * Copyright (©) 2009-2012 Marc Alexander Lehmann <libecb@schmorp.de>
473 * Copyright (©) 2011 Emanuele Giaquinta 481 * Copyright (©) 2011 Emanuele Giaquinta
474 * All rights reserved. 482 * All rights reserved.
475 * 483 *
476 * Redistribution and use in source and binary forms, with or without modifica- 484 * Redistribution and use in source and binary forms, with or without modifica-
477 * tion, are permitted provided that the following conditions are met: 485 * tion, are permitted provided that the following conditions are met:
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 0x00010001
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
535 typedef intptr_t ptrdiff_t;
515#else 536#else
516 #include <inttypes.h> 537 #include <inttypes.h>
538 #if UINTMAX_MAX > 0xffffffffU
539 #define ECB_PTRSIZE 8
540 #else
541 #define ECB_PTRSIZE 4
542 #endif
517#endif 543#endif
518 544
519/* many compilers define _GNUC_ to some versions but then only implement 545/* many compilers define _GNUC_ to some versions but then only implement
520 * what their idiot authors think are the "more important" extensions, 546 * what their idiot authors think are the "more important" extensions,
521 * causing enormous grief in return for some better fake benchmark numbers. 547 * causing enormous grief in return for some better fake benchmark numbers.
522 * or so. 548 * or so.
523 * we try to detect these and simply assume they are not gcc - if they have 549 * 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. 550 * an issue with that they should have done it right in the first place.
525 */ 551 */
526#ifndef ECB_GCC_VERSION 552#ifndef ECB_GCC_VERSION
527 #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__) 553 #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 554 #define ECB_GCC_VERSION(major,minor) 0
529 #else 555 #else
530 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 556 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
531 #endif 557 #endif
532#endif 558#endif
533 559
560#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */
561#define ECB_C99 (__STDC_VERSION__ >= 199901L)
562#define ECB_C11 (__STDC_VERSION__ >= 201112L)
563#define ECB_CPP (__cplusplus+0)
564#define ECB_CPP98 (__cplusplus >= 199711L)
565#define ECB_CPP11 (__cplusplus >= 201103L)
566
534/*****************************************************************************/ 567/*****************************************************************************/
535 568
536/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ 569/* 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 */ 570/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
538 571
539#if ECB_NO_THREADS || ECB_NO_SMP 572#if ECB_NO_THREADS
573 #define ECB_NO_SMP 1
574#endif
575
576#if ECB_NO_SMP
540 #define ECB_MEMORY_FENCE do { } while (0) 577 #define ECB_MEMORY_FENCE do { } while (0)
541#endif 578#endif
542 579
543#ifndef ECB_MEMORY_FENCE 580#ifndef ECB_MEMORY_FENCE
544 #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || defined(__clang__) 581 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
545 #if __i386__ 582 #if __i386 || __i386__
546 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") 583 #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 */ 584 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
548 #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ 585 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
549 #elif __amd64 586 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__
550 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") 587 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
551 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") 588 #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 */ 589 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
553 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ 590 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
554 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 591 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
555 #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ 592 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
556 || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) 593 || 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") 594 #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__ ) \ 595 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
559 || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) 596 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__
560 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") 597 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
598 #elif __sparc || __sparc__
599 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
600 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
601 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
602 #elif defined __s390__ || defined __s390x__
603 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
604 #elif defined __mips__
605 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
606 #elif defined __alpha__
607 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
608 #elif defined __hppa__
609 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
610 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
611 #elif defined __ia64__
612 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory")
561 #endif 613 #endif
562 #endif 614 #endif
563#endif 615#endif
564 616
565#ifndef ECB_MEMORY_FENCE 617#ifndef ECB_MEMORY_FENCE
618 #if ECB_GCC_VERSION(4,7)
619 /* see comment below about the C11 memory model. in short - avoid */
620 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
621 #elif defined __clang && __has_feature (cxx_atomic)
622 /* see above */
623 #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
566 #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) 624 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
567 #define ECB_MEMORY_FENCE __sync_synchronize () 625 #define ECB_MEMORY_FENCE __sync_synchronize ()
568 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
569 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
570 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 626 #elif _MSC_VER >= 1400 /* VC++ 2005 */
571 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 627 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
572 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 628 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
573 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ 629 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
574 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 630 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
575 #elif defined(_WIN32) 631 #elif defined _WIN32
576 #include <WinNT.h> 632 #include <WinNT.h>
577 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ 633 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
634 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
635 #include <mbarrier.h>
636 #define ECB_MEMORY_FENCE __machine_rw_barrier ()
637 #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier ()
638 #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier ()
639 #elif __xlC__
640 #define ECB_MEMORY_FENCE __sync ()
641 #endif
642#endif
643
644#ifndef ECB_MEMORY_FENCE
645 #if ECB_C11 && !defined __STDC_NO_ATOMICS__
646 /* we assume that these memory fences work on all variables/all memory accesses, */
647 /* not just C11 atomics and atomic accesses */
648 #include <stdatomic.h>
649 /* unfortunately, the C11 memory model seems to be very limited, and unable to express */
650 /* simple barrier semantics. That means we need to take out thor's hammer. */
651 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
578 #endif 652 #endif
579#endif 653#endif
580 654
581#ifndef ECB_MEMORY_FENCE 655#ifndef ECB_MEMORY_FENCE
582 #if !ECB_AVOID_PTHREADS 656 #if !ECB_AVOID_PTHREADS
594 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; 668 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
595 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) 669 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
596 #endif 670 #endif
597#endif 671#endif
598 672
599#if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) 673#if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
600 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 674 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
601#endif 675#endif
602 676
603#if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) 677#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
604 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 678 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
605#endif 679#endif
606 680
607/*****************************************************************************/ 681/*****************************************************************************/
608
609#define ECB_C99 (__STDC_VERSION__ >= 199901L)
610 682
611#if __cplusplus 683#if __cplusplus
612 #define ecb_inline static inline 684 #define ecb_inline static inline
613#elif ECB_GCC_VERSION(2,5) 685#elif ECB_GCC_VERSION(2,5)
614 #define ecb_inline static __inline__ 686 #define ecb_inline static __inline__
653#elif ECB_GCC_VERSION(3,0) 725#elif ECB_GCC_VERSION(3,0)
654 #define ecb_decltype(x) __typeof(x) 726 #define ecb_decltype(x) __typeof(x)
655#endif 727#endif
656 728
657#define ecb_noinline ecb_attribute ((__noinline__)) 729#define ecb_noinline ecb_attribute ((__noinline__))
658#define ecb_noreturn ecb_attribute ((__noreturn__))
659#define ecb_unused ecb_attribute ((__unused__)) 730#define ecb_unused ecb_attribute ((__unused__))
660#define ecb_const ecb_attribute ((__const__)) 731#define ecb_const ecb_attribute ((__const__))
661#define ecb_pure ecb_attribute ((__pure__)) 732#define ecb_pure ecb_attribute ((__pure__))
733
734#if ECB_C11
735 #define ecb_noreturn _Noreturn
736#else
737 #define ecb_noreturn ecb_attribute ((__noreturn__))
738#endif
662 739
663#if ECB_GCC_VERSION(4,3) 740#if ECB_GCC_VERSION(4,3)
664 #define ecb_artificial ecb_attribute ((__artificial__)) 741 #define ecb_artificial ecb_attribute ((__artificial__))
665 #define ecb_hot ecb_attribute ((__hot__)) 742 #define ecb_hot ecb_attribute ((__hot__))
666 #define ecb_cold ecb_attribute ((__cold__)) 743 #define ecb_cold ecb_attribute ((__cold__))
757 834
758 return r + ecb_ld32 (x); 835 return r + ecb_ld32 (x);
759 } 836 }
760#endif 837#endif
761 838
839ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) ecb_const;
840ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
841ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) ecb_const;
842ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); }
843
844ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const;
845ecb_function_ uint8_t ecb_bitrev8 (uint8_t x)
846{
847 return ( (x * 0x0802U & 0x22110U)
848 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
849}
850
851ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) ecb_const;
852ecb_function_ uint16_t ecb_bitrev16 (uint16_t x)
853{
854 x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1);
855 x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2);
856 x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4);
857 x = ( x >> 8 ) | ( x << 8);
858
859 return x;
860}
861
862ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) ecb_const;
863ecb_function_ uint32_t ecb_bitrev32 (uint32_t x)
864{
865 x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
866 x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
867 x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
868 x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
869 x = ( x >> 16 ) | ( x << 16);
870
871 return x;
872}
873
762/* popcount64 is only available on 64 bit cpus as gcc builtin */ 874/* popcount64 is only available on 64 bit cpus as gcc builtin */
763/* so for this version we are lazy */ 875/* so for this version we are lazy */
764ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; 876ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const;
765ecb_function_ int 877ecb_function_ int
766ecb_popcount64 (uint64_t x) 878ecb_popcount64 (uint64_t x)
815 927
816#if ECB_GCC_VERSION(4,5) 928#if ECB_GCC_VERSION(4,5)
817 #define ecb_unreachable() __builtin_unreachable () 929 #define ecb_unreachable() __builtin_unreachable ()
818#else 930#else
819 /* this seems to work fine, but gcc always emits a warning for it :/ */ 931 /* this seems to work fine, but gcc always emits a warning for it :/ */
820 ecb_function_ void ecb_unreachable (void) ecb_noreturn; 932 ecb_inline void ecb_unreachable (void) ecb_noreturn;
821 ecb_function_ void ecb_unreachable (void) { } 933 ecb_inline void ecb_unreachable (void) { }
822#endif 934#endif
823 935
824/* try to tell the compiler that some condition is definitely true */ 936/* try to tell the compiler that some condition is definitely true */
825#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 937#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
826 938
827ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; 939ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
828ecb_function_ unsigned char 940ecb_inline unsigned char
829ecb_byteorder_helper (void) 941ecb_byteorder_helper (void)
830{ 942{
831 const uint32_t u = 0x11223344; 943 const uint32_t u = 0x11223344;
832 return *(unsigned char *)&u; 944 return *(unsigned char *)&u;
833} 945}
834 946
835ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; 947ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
836ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } 948ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
837ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; 949ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
838ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } 950ecb_inline ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }
839 951
840#if ECB_GCC_VERSION(3,0) || ECB_C99 952#if ECB_GCC_VERSION(3,0) || ECB_C99
841 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) 953 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
842#else 954#else
843 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) 955 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
876 988
877#if ECB_MEMORY_FENCE_NEEDS_PTHREADS 989#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
878/* if your architecture doesn't need memory fences, e.g. because it is 990/* if your architecture doesn't need memory fences, e.g. because it is
879 * single-cpu/core, or if you use libev in a project that doesn't use libev 991 * single-cpu/core, or if you use libev in a project that doesn't use libev
880 * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling 992 * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling
881 * libev, in which casess the memory fences become nops. 993 * libev, in which cases the memory fences become nops.
882 * alternatively, you can remove this #error and link against libpthread, 994 * alternatively, you can remove this #error and link against libpthread,
883 * which will then provide the memory fences. 995 * which will then provide the memory fences.
884 */ 996 */
885# error "memory fences not defined for your architecture, please report" 997# error "memory fences not defined for your architecture, please report"
886#endif 998#endif
1043{ 1155{
1044 write (STDERR_FILENO, msg, strlen (msg)); 1156 write (STDERR_FILENO, msg, strlen (msg));
1045} 1157}
1046#endif 1158#endif
1047 1159
1048static void (*syserr_cb)(const char *msg); 1160static void (*syserr_cb)(const char *msg) EV_THROW;
1049 1161
1050void ecb_cold 1162void ecb_cold
1051ev_set_syserr_cb (void (*cb)(const char *msg)) 1163ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW
1052{ 1164{
1053 syserr_cb = cb; 1165 syserr_cb = cb;
1054} 1166}
1055 1167
1056static void noinline ecb_cold 1168static void noinline ecb_cold
1074 abort (); 1186 abort ();
1075 } 1187 }
1076} 1188}
1077 1189
1078static void * 1190static void *
1079ev_realloc_emul (void *ptr, long size) 1191ev_realloc_emul (void *ptr, long size) EV_THROW
1080{ 1192{
1081#if __GLIBC__ 1193#if __GLIBC__
1082 return realloc (ptr, size); 1194 return realloc (ptr, size);
1083#else 1195#else
1084 /* some systems, notably openbsd and darwin, fail to properly 1196 /* some systems, notably openbsd and darwin, fail to properly
1092 free (ptr); 1204 free (ptr);
1093 return 0; 1205 return 0;
1094#endif 1206#endif
1095} 1207}
1096 1208
1097static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 1209static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1098 1210
1099void ecb_cold 1211void ecb_cold
1100ev_set_allocator (void *(*cb)(void *ptr, long size)) 1212ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW
1101{ 1213{
1102 alloc = cb; 1214 alloc = cb;
1103} 1215}
1104 1216
1105inline_speed void * 1217inline_speed void *
1193 #undef VAR 1305 #undef VAR
1194 }; 1306 };
1195 #include "ev_wrap.h" 1307 #include "ev_wrap.h"
1196 1308
1197 static struct ev_loop default_loop_struct; 1309 static struct ev_loop default_loop_struct;
1198 struct ev_loop *ev_default_loop_ptr; 1310 EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */
1199 1311
1200#else 1312#else
1201 1313
1202 ev_tstamp ev_rt_now; 1314 EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */
1203 #define VAR(name,decl) static decl; 1315 #define VAR(name,decl) static decl;
1204 #include "ev_vars.h" 1316 #include "ev_vars.h"
1205 #undef VAR 1317 #undef VAR
1206 1318
1207 static int ev_default_loop_ptr; 1319 static int ev_default_loop_ptr;
1222 1334
1223/*****************************************************************************/ 1335/*****************************************************************************/
1224 1336
1225#ifndef EV_HAVE_EV_TIME 1337#ifndef EV_HAVE_EV_TIME
1226ev_tstamp 1338ev_tstamp
1227ev_time (void) 1339ev_time (void) EV_THROW
1228{ 1340{
1229#if EV_USE_REALTIME 1341#if EV_USE_REALTIME
1230 if (expect_true (have_realtime)) 1342 if (expect_true (have_realtime))
1231 { 1343 {
1232 struct timespec ts; 1344 struct timespec ts;
1256 return ev_time (); 1368 return ev_time ();
1257} 1369}
1258 1370
1259#if EV_MULTIPLICITY 1371#if EV_MULTIPLICITY
1260ev_tstamp 1372ev_tstamp
1261ev_now (EV_P) 1373ev_now (EV_P) EV_THROW
1262{ 1374{
1263 return ev_rt_now; 1375 return ev_rt_now;
1264} 1376}
1265#endif 1377#endif
1266 1378
1267void 1379void
1268ev_sleep (ev_tstamp delay) 1380ev_sleep (ev_tstamp delay) EV_THROW
1269{ 1381{
1270 if (delay > 0.) 1382 if (delay > 0.)
1271 { 1383 {
1272#if EV_USE_NANOSLEEP 1384#if EV_USE_NANOSLEEP
1273 struct timespec ts; 1385 struct timespec ts;
1274 1386
1275 EV_TS_SET (ts, delay); 1387 EV_TS_SET (ts, delay);
1276 nanosleep (&ts, 0); 1388 nanosleep (&ts, 0);
1277#elif defined(_WIN32) 1389#elif defined _WIN32
1278 Sleep ((unsigned long)(delay * 1e3)); 1390 Sleep ((unsigned long)(delay * 1e3));
1279#else 1391#else
1280 struct timeval tv; 1392 struct timeval tv;
1281 1393
1282 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 1394 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
1354pendingcb (EV_P_ ev_prepare *w, int revents) 1466pendingcb (EV_P_ ev_prepare *w, int revents)
1355{ 1467{
1356} 1468}
1357 1469
1358void noinline 1470void noinline
1359ev_feed_event (EV_P_ void *w, int revents) 1471ev_feed_event (EV_P_ void *w, int revents) EV_THROW
1360{ 1472{
1361 W w_ = (W)w; 1473 W w_ = (W)w;
1362 int pri = ABSPRI (w_); 1474 int pri = ABSPRI (w_);
1363 1475
1364 if (expect_false (w_->pending)) 1476 if (expect_false (w_->pending))
1368 w_->pending = ++pendingcnt [pri]; 1480 w_->pending = ++pendingcnt [pri];
1369 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); 1481 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
1370 pendings [pri][w_->pending - 1].w = w_; 1482 pendings [pri][w_->pending - 1].w = w_;
1371 pendings [pri][w_->pending - 1].events = revents; 1483 pendings [pri][w_->pending - 1].events = revents;
1372 } 1484 }
1485
1486 pendingpri = NUMPRI - 1;
1373} 1487}
1374 1488
1375inline_speed void 1489inline_speed void
1376feed_reverse (EV_P_ W w) 1490feed_reverse (EV_P_ W w)
1377{ 1491{
1423 if (expect_true (!anfd->reify)) 1537 if (expect_true (!anfd->reify))
1424 fd_event_nocheck (EV_A_ fd, revents); 1538 fd_event_nocheck (EV_A_ fd, revents);
1425} 1539}
1426 1540
1427void 1541void
1428ev_feed_fd_event (EV_P_ int fd, int revents) 1542ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW
1429{ 1543{
1430 if (fd >= 0 && fd < anfdmax) 1544 if (fd >= 0 && fd < anfdmax)
1431 fd_event_nocheck (EV_A_ fd, revents); 1545 fd_event_nocheck (EV_A_ fd, revents);
1432} 1546}
1433 1547
1782} 1896}
1783 1897
1784inline_speed void 1898inline_speed void
1785evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1899evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1786{ 1900{
1901 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
1902
1787 if (expect_true (*flag)) 1903 if (expect_true (*flag))
1788 return; 1904 return;
1789 1905
1790 *flag = 1; 1906 *flag = 1;
1791
1792 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ 1907 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
1793 1908
1794 pipe_write_skipped = 1; 1909 pipe_write_skipped = 1;
1795 1910
1796 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */ 1911 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
1797 1912
1798 if (pipe_write_wanted) 1913 if (pipe_write_wanted)
1799 { 1914 {
1800 int old_errno; 1915 int old_errno;
1801 1916
1802 pipe_write_skipped = 0; /* just an optimisation, no fence needed */ 1917 pipe_write_skipped = 0;
1918 ECB_MEMORY_FENCE_RELEASE;
1803 1919
1804 old_errno = errno; /* save errno because write will clobber it */ 1920 old_errno = errno; /* save errno because write will clobber it */
1805 1921
1806#if EV_USE_EVENTFD 1922#if EV_USE_EVENTFD
1807 if (evfd >= 0) 1923 if (evfd >= 0)
1810 write (evfd, &counter, sizeof (uint64_t)); 1926 write (evfd, &counter, sizeof (uint64_t));
1811 } 1927 }
1812 else 1928 else
1813#endif 1929#endif
1814 { 1930 {
1815 /* win32 people keep sending patches that change this write() to send() */ 1931#ifdef _WIN32
1816 /* and then run away. but send() is wrong, it wants a socket handle on win32 */ 1932 WSABUF buf;
1817 /* so when you think this write should be a send instead, please find out */ 1933 DWORD sent;
1818 /* where your send() is from - it's definitely not the microsoft send, and */ 1934 buf.buf = &buf;
1819 /* tell me. thank you. */ 1935 buf.len = 1;
1936 WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0);
1937#else
1820 write (evpipe [1], &(evpipe [1]), 1); 1938 write (evpipe [1], &(evpipe [1]), 1);
1939#endif
1821 } 1940 }
1822 1941
1823 errno = old_errno; 1942 errno = old_errno;
1824 } 1943 }
1825} 1944}
1840 read (evfd, &counter, sizeof (uint64_t)); 1959 read (evfd, &counter, sizeof (uint64_t));
1841 } 1960 }
1842 else 1961 else
1843#endif 1962#endif
1844 { 1963 {
1845 char dummy; 1964 char dummy[4];
1846 /* see discussion in evpipe_write when you think this read should be recv in win32 */ 1965#ifdef _WIN32
1966 WSABUF buf;
1967 DWORD recvd;
1968 DWORD flags = 0;
1969 buf.buf = dummy;
1970 buf.len = sizeof (dummy);
1971 WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0);
1972#else
1847 read (evpipe [0], &dummy, 1); 1973 read (evpipe [0], &dummy, sizeof (dummy));
1974#endif
1848 } 1975 }
1849 } 1976 }
1850 1977
1851 pipe_write_skipped = 0; 1978 pipe_write_skipped = 0;
1979
1980 ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
1852 1981
1853#if EV_SIGNAL_ENABLE 1982#if EV_SIGNAL_ENABLE
1854 if (sig_pending) 1983 if (sig_pending)
1855 { 1984 {
1856 sig_pending = 0; 1985 sig_pending = 0;
1986
1987 ECB_MEMORY_FENCE;
1857 1988
1858 for (i = EV_NSIG - 1; i--; ) 1989 for (i = EV_NSIG - 1; i--; )
1859 if (expect_false (signals [i].pending)) 1990 if (expect_false (signals [i].pending))
1860 ev_feed_signal_event (EV_A_ i + 1); 1991 ev_feed_signal_event (EV_A_ i + 1);
1861 } 1992 }
1863 1994
1864#if EV_ASYNC_ENABLE 1995#if EV_ASYNC_ENABLE
1865 if (async_pending) 1996 if (async_pending)
1866 { 1997 {
1867 async_pending = 0; 1998 async_pending = 0;
1999
2000 ECB_MEMORY_FENCE;
1868 2001
1869 for (i = asynccnt; i--; ) 2002 for (i = asynccnt; i--; )
1870 if (asyncs [i]->sent) 2003 if (asyncs [i]->sent)
1871 { 2004 {
1872 asyncs [i]->sent = 0; 2005 asyncs [i]->sent = 0;
2006 ECB_MEMORY_FENCE_RELEASE;
1873 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); 2007 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
1874 } 2008 }
1875 } 2009 }
1876#endif 2010#endif
1877} 2011}
1878 2012
1879/*****************************************************************************/ 2013/*****************************************************************************/
1880 2014
1881void 2015void
1882ev_feed_signal (int signum) 2016ev_feed_signal (int signum) EV_THROW
1883{ 2017{
1884#if EV_MULTIPLICITY 2018#if EV_MULTIPLICITY
1885 EV_P = signals [signum - 1].loop; 2019 EV_P = signals [signum - 1].loop;
1886 2020
1887 if (!EV_A) 2021 if (!EV_A)
1904 2038
1905 ev_feed_signal (signum); 2039 ev_feed_signal (signum);
1906} 2040}
1907 2041
1908void noinline 2042void noinline
1909ev_feed_signal_event (EV_P_ int signum) 2043ev_feed_signal_event (EV_P_ int signum) EV_THROW
1910{ 2044{
1911 WL w; 2045 WL w;
1912 2046
1913 if (expect_false (signum <= 0 || signum > EV_NSIG)) 2047 if (expect_false (signum <= 0 || signum > EV_NSIG))
1914 return; 2048 return;
1922 if (expect_false (signals [signum].loop != EV_A)) 2056 if (expect_false (signals [signum].loop != EV_A))
1923 return; 2057 return;
1924#endif 2058#endif
1925 2059
1926 signals [signum].pending = 0; 2060 signals [signum].pending = 0;
2061 ECB_MEMORY_FENCE_RELEASE;
1927 2062
1928 for (w = signals [signum].head; w; w = w->next) 2063 for (w = signals [signum].head; w; w = w->next)
1929 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 2064 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1930} 2065}
1931 2066
2030#if EV_USE_SELECT 2165#if EV_USE_SELECT
2031# include "ev_select.c" 2166# include "ev_select.c"
2032#endif 2167#endif
2033 2168
2034int ecb_cold 2169int ecb_cold
2035ev_version_major (void) 2170ev_version_major (void) EV_THROW
2036{ 2171{
2037 return EV_VERSION_MAJOR; 2172 return EV_VERSION_MAJOR;
2038} 2173}
2039 2174
2040int ecb_cold 2175int ecb_cold
2041ev_version_minor (void) 2176ev_version_minor (void) EV_THROW
2042{ 2177{
2043 return EV_VERSION_MINOR; 2178 return EV_VERSION_MINOR;
2044} 2179}
2045 2180
2046/* return true if we are running with elevated privileges and should ignore env variables */ 2181/* return true if we are running with elevated privileges and should ignore env variables */
2054 || getgid () != getegid (); 2189 || getgid () != getegid ();
2055#endif 2190#endif
2056} 2191}
2057 2192
2058unsigned int ecb_cold 2193unsigned int ecb_cold
2059ev_supported_backends (void) 2194ev_supported_backends (void) EV_THROW
2060{ 2195{
2061 unsigned int flags = 0; 2196 unsigned int flags = 0;
2062 2197
2063 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2198 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2064 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2199 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
2068 2203
2069 return flags; 2204 return flags;
2070} 2205}
2071 2206
2072unsigned int ecb_cold 2207unsigned int ecb_cold
2073ev_recommended_backends (void) 2208ev_recommended_backends (void) EV_THROW
2074{ 2209{
2075 unsigned int flags = ev_supported_backends (); 2210 unsigned int flags = ev_supported_backends ();
2076 2211
2077#ifndef __NetBSD__ 2212#ifndef __NetBSD__
2078 /* kqueue is borked on everything but netbsd apparently */ 2213 /* kqueue is borked on everything but netbsd apparently */
2090 2225
2091 return flags; 2226 return flags;
2092} 2227}
2093 2228
2094unsigned int ecb_cold 2229unsigned int ecb_cold
2095ev_embeddable_backends (void) 2230ev_embeddable_backends (void) EV_THROW
2096{ 2231{
2097 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 2232 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2098 2233
2099 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 2234 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
2100 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ 2235 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2102 2237
2103 return flags; 2238 return flags;
2104} 2239}
2105 2240
2106unsigned int 2241unsigned int
2107ev_backend (EV_P) 2242ev_backend (EV_P) EV_THROW
2108{ 2243{
2109 return backend; 2244 return backend;
2110} 2245}
2111 2246
2112#if EV_FEATURE_API 2247#if EV_FEATURE_API
2113unsigned int 2248unsigned int
2114ev_iteration (EV_P) 2249ev_iteration (EV_P) EV_THROW
2115{ 2250{
2116 return loop_count; 2251 return loop_count;
2117} 2252}
2118 2253
2119unsigned int 2254unsigned int
2120ev_depth (EV_P) 2255ev_depth (EV_P) EV_THROW
2121{ 2256{
2122 return loop_depth; 2257 return loop_depth;
2123} 2258}
2124 2259
2125void 2260void
2126ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 2261ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2127{ 2262{
2128 io_blocktime = interval; 2263 io_blocktime = interval;
2129} 2264}
2130 2265
2131void 2266void
2132ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 2267ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2133{ 2268{
2134 timeout_blocktime = interval; 2269 timeout_blocktime = interval;
2135} 2270}
2136 2271
2137void 2272void
2138ev_set_userdata (EV_P_ void *data) 2273ev_set_userdata (EV_P_ void *data) EV_THROW
2139{ 2274{
2140 userdata = data; 2275 userdata = data;
2141} 2276}
2142 2277
2143void * 2278void *
2144ev_userdata (EV_P) 2279ev_userdata (EV_P) EV_THROW
2145{ 2280{
2146 return userdata; 2281 return userdata;
2147} 2282}
2148 2283
2149void 2284void
2150ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 2285ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) EV_THROW
2151{ 2286{
2152 invoke_cb = invoke_pending_cb; 2287 invoke_cb = invoke_pending_cb;
2153} 2288}
2154 2289
2155void 2290void
2156ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) 2291ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW
2157{ 2292{
2158 release_cb = release; 2293 release_cb = release;
2159 acquire_cb = acquire; 2294 acquire_cb = acquire;
2160} 2295}
2161#endif 2296#endif
2162 2297
2163/* initialise a loop structure, must be zero-initialised */ 2298/* initialise a loop structure, must be zero-initialised */
2164static void noinline ecb_cold 2299static void noinline ecb_cold
2165loop_init (EV_P_ unsigned int flags) 2300loop_init (EV_P_ unsigned int flags) EV_THROW
2166{ 2301{
2167 if (!backend) 2302 if (!backend)
2168 { 2303 {
2169 origflags = flags; 2304 origflags = flags;
2170 2305
2275 EV_INVOKE_PENDING; 2410 EV_INVOKE_PENDING;
2276 } 2411 }
2277#endif 2412#endif
2278 2413
2279#if EV_CHILD_ENABLE 2414#if EV_CHILD_ENABLE
2280 if (ev_is_active (&childev)) 2415 if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
2281 { 2416 {
2282 ev_ref (EV_A); /* child watcher */ 2417 ev_ref (EV_A); /* child watcher */
2283 ev_signal_stop (EV_A_ &childev); 2418 ev_signal_stop (EV_A_ &childev);
2284 } 2419 }
2285#endif 2420#endif
2423} 2558}
2424 2559
2425#if EV_MULTIPLICITY 2560#if EV_MULTIPLICITY
2426 2561
2427struct ev_loop * ecb_cold 2562struct ev_loop * ecb_cold
2428ev_loop_new (unsigned int flags) 2563ev_loop_new (unsigned int flags) EV_THROW
2429{ 2564{
2430 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2565 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2431 2566
2432 memset (EV_A, 0, sizeof (struct ev_loop)); 2567 memset (EV_A, 0, sizeof (struct ev_loop));
2433 loop_init (EV_A_ flags); 2568 loop_init (EV_A_ flags);
2477} 2612}
2478#endif 2613#endif
2479 2614
2480#if EV_FEATURE_API 2615#if EV_FEATURE_API
2481void ecb_cold 2616void ecb_cold
2482ev_verify (EV_P) 2617ev_verify (EV_P) EV_THROW
2483{ 2618{
2484#if EV_VERIFY 2619#if EV_VERIFY
2485 int i; 2620 int i;
2486 WL w; 2621 WL w, w2;
2487 2622
2488 assert (activecnt >= -1); 2623 assert (activecnt >= -1);
2489 2624
2490 assert (fdchangemax >= fdchangecnt); 2625 assert (fdchangemax >= fdchangecnt);
2491 for (i = 0; i < fdchangecnt; ++i) 2626 for (i = 0; i < fdchangecnt; ++i)
2492 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); 2627 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
2493 2628
2494 assert (anfdmax >= 0); 2629 assert (anfdmax >= 0);
2495 for (i = 0; i < anfdmax; ++i) 2630 for (i = 0; i < anfdmax; ++i)
2631 {
2632 int j = 0;
2633
2496 for (w = anfds [i].head; w; w = w->next) 2634 for (w = w2 = anfds [i].head; w; w = w->next)
2497 { 2635 {
2498 verify_watcher (EV_A_ (W)w); 2636 verify_watcher (EV_A_ (W)w);
2637
2638 if (j++ & 1)
2639 {
2640 assert (("libev: io watcher list contains a loop", w != w2));
2641 w2 = w2->next;
2642 }
2643
2499 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); 2644 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
2500 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); 2645 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
2501 } 2646 }
2647 }
2502 2648
2503 assert (timermax >= timercnt); 2649 assert (timermax >= timercnt);
2504 verify_heap (EV_A_ timers, timercnt); 2650 verify_heap (EV_A_ timers, timercnt);
2505 2651
2506#if EV_PERIODIC_ENABLE 2652#if EV_PERIODIC_ENABLE
2556#if EV_MULTIPLICITY 2702#if EV_MULTIPLICITY
2557struct ev_loop * ecb_cold 2703struct ev_loop * ecb_cold
2558#else 2704#else
2559int 2705int
2560#endif 2706#endif
2561ev_default_loop (unsigned int flags) 2707ev_default_loop (unsigned int flags) EV_THROW
2562{ 2708{
2563 if (!ev_default_loop_ptr) 2709 if (!ev_default_loop_ptr)
2564 { 2710 {
2565#if EV_MULTIPLICITY 2711#if EV_MULTIPLICITY
2566 EV_P = ev_default_loop_ptr = &default_loop_struct; 2712 EV_P = ev_default_loop_ptr = &default_loop_struct;
2585 2731
2586 return ev_default_loop_ptr; 2732 return ev_default_loop_ptr;
2587} 2733}
2588 2734
2589void 2735void
2590ev_loop_fork (EV_P) 2736ev_loop_fork (EV_P) EV_THROW
2591{ 2737{
2592 postfork = 1; /* must be in line with ev_default_fork */ 2738 postfork = 1;
2593} 2739}
2594 2740
2595/*****************************************************************************/ 2741/*****************************************************************************/
2596 2742
2597void 2743void
2599{ 2745{
2600 EV_CB_INVOKE ((W)w, revents); 2746 EV_CB_INVOKE ((W)w, revents);
2601} 2747}
2602 2748
2603unsigned int 2749unsigned int
2604ev_pending_count (EV_P) 2750ev_pending_count (EV_P) EV_THROW
2605{ 2751{
2606 int pri; 2752 int pri;
2607 unsigned int count = 0; 2753 unsigned int count = 0;
2608 2754
2609 for (pri = NUMPRI; pri--; ) 2755 for (pri = NUMPRI; pri--; )
2613} 2759}
2614 2760
2615void noinline 2761void noinline
2616ev_invoke_pending (EV_P) 2762ev_invoke_pending (EV_P)
2617{ 2763{
2618 int pri; 2764 for (pendingpri = NUMPRI; pendingpri--; ) /* pendingpri is modified during the loop */
2619
2620 for (pri = NUMPRI; pri--; )
2621 while (pendingcnt [pri]) 2765 while (pendingcnt [pendingpri])
2622 { 2766 {
2623 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 2767 ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
2624 2768
2625 p->w->pending = 0; 2769 p->w->pending = 0;
2626 EV_CB_INVOKE (p->w, p->events); 2770 EV_CB_INVOKE (p->w, p->events);
2627 EV_FREQUENT_CHECK; 2771 EV_FREQUENT_CHECK;
2628 } 2772 }
2723{ 2867{
2724 EV_FREQUENT_CHECK; 2868 EV_FREQUENT_CHECK;
2725 2869
2726 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 2870 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
2727 { 2871 {
2728 int feed_count = 0;
2729
2730 do 2872 do
2731 { 2873 {
2732 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 2874 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
2733 2875
2734 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ 2876 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
2868 3010
2869 mn_now = ev_rt_now; 3011 mn_now = ev_rt_now;
2870 } 3012 }
2871} 3013}
2872 3014
2873void 3015int
2874ev_run (EV_P_ int flags) 3016ev_run (EV_P_ int flags)
2875{ 3017{
2876#if EV_FEATURE_API 3018#if EV_FEATURE_API
2877 ++loop_depth; 3019 ++loop_depth;
2878#endif 3020#endif
2991#endif 3133#endif
2992 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ 3134 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2993 backend_poll (EV_A_ waittime); 3135 backend_poll (EV_A_ waittime);
2994 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 3136 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2995 3137
2996 pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ 3138 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
2997 3139
2998 if (pipe_write_skipped) 3140 if (pipe_write_skipped)
2999 { 3141 {
3000 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); 3142 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
3001 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3143 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3035 loop_done = EVBREAK_CANCEL; 3177 loop_done = EVBREAK_CANCEL;
3036 3178
3037#if EV_FEATURE_API 3179#if EV_FEATURE_API
3038 --loop_depth; 3180 --loop_depth;
3039#endif 3181#endif
3182
3183 return activecnt;
3040} 3184}
3041 3185
3042void 3186void
3043ev_break (EV_P_ int how) 3187ev_break (EV_P_ int how) EV_THROW
3044{ 3188{
3045 loop_done = how; 3189 loop_done = how;
3046} 3190}
3047 3191
3048void 3192void
3049ev_ref (EV_P) 3193ev_ref (EV_P) EV_THROW
3050{ 3194{
3051 ++activecnt; 3195 ++activecnt;
3052} 3196}
3053 3197
3054void 3198void
3055ev_unref (EV_P) 3199ev_unref (EV_P) EV_THROW
3056{ 3200{
3057 --activecnt; 3201 --activecnt;
3058} 3202}
3059 3203
3060void 3204void
3061ev_now_update (EV_P) 3205ev_now_update (EV_P) EV_THROW
3062{ 3206{
3063 time_update (EV_A_ 1e100); 3207 time_update (EV_A_ 1e100);
3064} 3208}
3065 3209
3066void 3210void
3067ev_suspend (EV_P) 3211ev_suspend (EV_P) EV_THROW
3068{ 3212{
3069 ev_now_update (EV_A); 3213 ev_now_update (EV_A);
3070} 3214}
3071 3215
3072void 3216void
3073ev_resume (EV_P) 3217ev_resume (EV_P) EV_THROW
3074{ 3218{
3075 ev_tstamp mn_prev = mn_now; 3219 ev_tstamp mn_prev = mn_now;
3076 3220
3077 ev_now_update (EV_A); 3221 ev_now_update (EV_A);
3078 timers_reschedule (EV_A_ mn_now - mn_prev); 3222 timers_reschedule (EV_A_ mn_now - mn_prev);
3117 w->pending = 0; 3261 w->pending = 0;
3118 } 3262 }
3119} 3263}
3120 3264
3121int 3265int
3122ev_clear_pending (EV_P_ void *w) 3266ev_clear_pending (EV_P_ void *w) EV_THROW
3123{ 3267{
3124 W w_ = (W)w; 3268 W w_ = (W)w;
3125 int pending = w_->pending; 3269 int pending = w_->pending;
3126 3270
3127 if (expect_true (pending)) 3271 if (expect_true (pending))
3160} 3304}
3161 3305
3162/*****************************************************************************/ 3306/*****************************************************************************/
3163 3307
3164void noinline 3308void noinline
3165ev_io_start (EV_P_ ev_io *w) 3309ev_io_start (EV_P_ ev_io *w) EV_THROW
3166{ 3310{
3167 int fd = w->fd; 3311 int fd = w->fd;
3168 3312
3169 if (expect_false (ev_is_active (w))) 3313 if (expect_false (ev_is_active (w)))
3170 return; 3314 return;
3176 3320
3177 ev_start (EV_A_ (W)w, 1); 3321 ev_start (EV_A_ (W)w, 1);
3178 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 3322 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
3179 wlist_add (&anfds[fd].head, (WL)w); 3323 wlist_add (&anfds[fd].head, (WL)w);
3180 3324
3325 /* common bug, apparently */
3326 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3327
3181 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); 3328 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
3182 w->events &= ~EV__IOFDSET; 3329 w->events &= ~EV__IOFDSET;
3183 3330
3184 EV_FREQUENT_CHECK; 3331 EV_FREQUENT_CHECK;
3185} 3332}
3186 3333
3187void noinline 3334void noinline
3188ev_io_stop (EV_P_ ev_io *w) 3335ev_io_stop (EV_P_ ev_io *w) EV_THROW
3189{ 3336{
3190 clear_pending (EV_A_ (W)w); 3337 clear_pending (EV_A_ (W)w);
3191 if (expect_false (!ev_is_active (w))) 3338 if (expect_false (!ev_is_active (w)))
3192 return; 3339 return;
3193 3340
3202 3349
3203 EV_FREQUENT_CHECK; 3350 EV_FREQUENT_CHECK;
3204} 3351}
3205 3352
3206void noinline 3353void noinline
3207ev_timer_start (EV_P_ ev_timer *w) 3354ev_timer_start (EV_P_ ev_timer *w) EV_THROW
3208{ 3355{
3209 if (expect_false (ev_is_active (w))) 3356 if (expect_false (ev_is_active (w)))
3210 return; 3357 return;
3211 3358
3212 ev_at (w) += mn_now; 3359 ev_at (w) += mn_now;
3226 3373
3227 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ 3374 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
3228} 3375}
3229 3376
3230void noinline 3377void noinline
3231ev_timer_stop (EV_P_ ev_timer *w) 3378ev_timer_stop (EV_P_ ev_timer *w) EV_THROW
3232{ 3379{
3233 clear_pending (EV_A_ (W)w); 3380 clear_pending (EV_A_ (W)w);
3234 if (expect_false (!ev_is_active (w))) 3381 if (expect_false (!ev_is_active (w)))
3235 return; 3382 return;
3236 3383
3256 3403
3257 EV_FREQUENT_CHECK; 3404 EV_FREQUENT_CHECK;
3258} 3405}
3259 3406
3260void noinline 3407void noinline
3261ev_timer_again (EV_P_ ev_timer *w) 3408ev_timer_again (EV_P_ ev_timer *w) EV_THROW
3262{ 3409{
3263 EV_FREQUENT_CHECK; 3410 EV_FREQUENT_CHECK;
3411
3412 clear_pending (EV_A_ (W)w);
3264 3413
3265 if (ev_is_active (w)) 3414 if (ev_is_active (w))
3266 { 3415 {
3267 if (w->repeat) 3416 if (w->repeat)
3268 { 3417 {
3281 3430
3282 EV_FREQUENT_CHECK; 3431 EV_FREQUENT_CHECK;
3283} 3432}
3284 3433
3285ev_tstamp 3434ev_tstamp
3286ev_timer_remaining (EV_P_ ev_timer *w) 3435ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW
3287{ 3436{
3288 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 3437 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
3289} 3438}
3290 3439
3291#if EV_PERIODIC_ENABLE 3440#if EV_PERIODIC_ENABLE
3292void noinline 3441void noinline
3293ev_periodic_start (EV_P_ ev_periodic *w) 3442ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW
3294{ 3443{
3295 if (expect_false (ev_is_active (w))) 3444 if (expect_false (ev_is_active (w)))
3296 return; 3445 return;
3297 3446
3298 if (w->reschedule_cb) 3447 if (w->reschedule_cb)
3318 3467
3319 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ 3468 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
3320} 3469}
3321 3470
3322void noinline 3471void noinline
3323ev_periodic_stop (EV_P_ ev_periodic *w) 3472ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW
3324{ 3473{
3325 clear_pending (EV_A_ (W)w); 3474 clear_pending (EV_A_ (W)w);
3326 if (expect_false (!ev_is_active (w))) 3475 if (expect_false (!ev_is_active (w)))
3327 return; 3476 return;
3328 3477
3346 3495
3347 EV_FREQUENT_CHECK; 3496 EV_FREQUENT_CHECK;
3348} 3497}
3349 3498
3350void noinline 3499void noinline
3351ev_periodic_again (EV_P_ ev_periodic *w) 3500ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW
3352{ 3501{
3353 /* TODO: use adjustheap and recalculation */ 3502 /* TODO: use adjustheap and recalculation */
3354 ev_periodic_stop (EV_A_ w); 3503 ev_periodic_stop (EV_A_ w);
3355 ev_periodic_start (EV_A_ w); 3504 ev_periodic_start (EV_A_ w);
3356} 3505}
3361#endif 3510#endif
3362 3511
3363#if EV_SIGNAL_ENABLE 3512#if EV_SIGNAL_ENABLE
3364 3513
3365void noinline 3514void noinline
3366ev_signal_start (EV_P_ ev_signal *w) 3515ev_signal_start (EV_P_ ev_signal *w) EV_THROW
3367{ 3516{
3368 if (expect_false (ev_is_active (w))) 3517 if (expect_false (ev_is_active (w)))
3369 return; 3518 return;
3370 3519
3371 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 3520 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
3442 3591
3443 EV_FREQUENT_CHECK; 3592 EV_FREQUENT_CHECK;
3444} 3593}
3445 3594
3446void noinline 3595void noinline
3447ev_signal_stop (EV_P_ ev_signal *w) 3596ev_signal_stop (EV_P_ ev_signal *w) EV_THROW
3448{ 3597{
3449 clear_pending (EV_A_ (W)w); 3598 clear_pending (EV_A_ (W)w);
3450 if (expect_false (!ev_is_active (w))) 3599 if (expect_false (!ev_is_active (w)))
3451 return; 3600 return;
3452 3601
3483#endif 3632#endif
3484 3633
3485#if EV_CHILD_ENABLE 3634#if EV_CHILD_ENABLE
3486 3635
3487void 3636void
3488ev_child_start (EV_P_ ev_child *w) 3637ev_child_start (EV_P_ ev_child *w) EV_THROW
3489{ 3638{
3490#if EV_MULTIPLICITY 3639#if EV_MULTIPLICITY
3491 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 3640 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
3492#endif 3641#endif
3493 if (expect_false (ev_is_active (w))) 3642 if (expect_false (ev_is_active (w)))
3500 3649
3501 EV_FREQUENT_CHECK; 3650 EV_FREQUENT_CHECK;
3502} 3651}
3503 3652
3504void 3653void
3505ev_child_stop (EV_P_ ev_child *w) 3654ev_child_stop (EV_P_ ev_child *w) EV_THROW
3506{ 3655{
3507 clear_pending (EV_A_ (W)w); 3656 clear_pending (EV_A_ (W)w);
3508 if (expect_false (!ev_is_active (w))) 3657 if (expect_false (!ev_is_active (w)))
3509 return; 3658 return;
3510 3659
3677} 3826}
3678 3827
3679inline_size int 3828inline_size int
3680infy_newfd (void) 3829infy_newfd (void)
3681{ 3830{
3682#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) 3831#if defined IN_CLOEXEC && defined IN_NONBLOCK
3683 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); 3832 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3684 if (fd >= 0) 3833 if (fd >= 0)
3685 return fd; 3834 return fd;
3686#endif 3835#endif
3687 return inotify_init (); 3836 return inotify_init ();
3762#else 3911#else
3763# define EV_LSTAT(p,b) lstat (p, b) 3912# define EV_LSTAT(p,b) lstat (p, b)
3764#endif 3913#endif
3765 3914
3766void 3915void
3767ev_stat_stat (EV_P_ ev_stat *w) 3916ev_stat_stat (EV_P_ ev_stat *w) EV_THROW
3768{ 3917{
3769 if (lstat (w->path, &w->attr) < 0) 3918 if (lstat (w->path, &w->attr) < 0)
3770 w->attr.st_nlink = 0; 3919 w->attr.st_nlink = 0;
3771 else if (!w->attr.st_nlink) 3920 else if (!w->attr.st_nlink)
3772 w->attr.st_nlink = 1; 3921 w->attr.st_nlink = 1;
3811 ev_feed_event (EV_A_ w, EV_STAT); 3960 ev_feed_event (EV_A_ w, EV_STAT);
3812 } 3961 }
3813} 3962}
3814 3963
3815void 3964void
3816ev_stat_start (EV_P_ ev_stat *w) 3965ev_stat_start (EV_P_ ev_stat *w) EV_THROW
3817{ 3966{
3818 if (expect_false (ev_is_active (w))) 3967 if (expect_false (ev_is_active (w)))
3819 return; 3968 return;
3820 3969
3821 ev_stat_stat (EV_A_ w); 3970 ev_stat_stat (EV_A_ w);
3842 3991
3843 EV_FREQUENT_CHECK; 3992 EV_FREQUENT_CHECK;
3844} 3993}
3845 3994
3846void 3995void
3847ev_stat_stop (EV_P_ ev_stat *w) 3996ev_stat_stop (EV_P_ ev_stat *w) EV_THROW
3848{ 3997{
3849 clear_pending (EV_A_ (W)w); 3998 clear_pending (EV_A_ (W)w);
3850 if (expect_false (!ev_is_active (w))) 3999 if (expect_false (!ev_is_active (w)))
3851 return; 4000 return;
3852 4001
3868} 4017}
3869#endif 4018#endif
3870 4019
3871#if EV_IDLE_ENABLE 4020#if EV_IDLE_ENABLE
3872void 4021void
3873ev_idle_start (EV_P_ ev_idle *w) 4022ev_idle_start (EV_P_ ev_idle *w) EV_THROW
3874{ 4023{
3875 if (expect_false (ev_is_active (w))) 4024 if (expect_false (ev_is_active (w)))
3876 return; 4025 return;
3877 4026
3878 pri_adjust (EV_A_ (W)w); 4027 pri_adjust (EV_A_ (W)w);
3891 4040
3892 EV_FREQUENT_CHECK; 4041 EV_FREQUENT_CHECK;
3893} 4042}
3894 4043
3895void 4044void
3896ev_idle_stop (EV_P_ ev_idle *w) 4045ev_idle_stop (EV_P_ ev_idle *w) EV_THROW
3897{ 4046{
3898 clear_pending (EV_A_ (W)w); 4047 clear_pending (EV_A_ (W)w);
3899 if (expect_false (!ev_is_active (w))) 4048 if (expect_false (!ev_is_active (w)))
3900 return; 4049 return;
3901 4050
3915} 4064}
3916#endif 4065#endif
3917 4066
3918#if EV_PREPARE_ENABLE 4067#if EV_PREPARE_ENABLE
3919void 4068void
3920ev_prepare_start (EV_P_ ev_prepare *w) 4069ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW
3921{ 4070{
3922 if (expect_false (ev_is_active (w))) 4071 if (expect_false (ev_is_active (w)))
3923 return; 4072 return;
3924 4073
3925 EV_FREQUENT_CHECK; 4074 EV_FREQUENT_CHECK;
3930 4079
3931 EV_FREQUENT_CHECK; 4080 EV_FREQUENT_CHECK;
3932} 4081}
3933 4082
3934void 4083void
3935ev_prepare_stop (EV_P_ ev_prepare *w) 4084ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW
3936{ 4085{
3937 clear_pending (EV_A_ (W)w); 4086 clear_pending (EV_A_ (W)w);
3938 if (expect_false (!ev_is_active (w))) 4087 if (expect_false (!ev_is_active (w)))
3939 return; 4088 return;
3940 4089
3953} 4102}
3954#endif 4103#endif
3955 4104
3956#if EV_CHECK_ENABLE 4105#if EV_CHECK_ENABLE
3957void 4106void
3958ev_check_start (EV_P_ ev_check *w) 4107ev_check_start (EV_P_ ev_check *w) EV_THROW
3959{ 4108{
3960 if (expect_false (ev_is_active (w))) 4109 if (expect_false (ev_is_active (w)))
3961 return; 4110 return;
3962 4111
3963 EV_FREQUENT_CHECK; 4112 EV_FREQUENT_CHECK;
3968 4117
3969 EV_FREQUENT_CHECK; 4118 EV_FREQUENT_CHECK;
3970} 4119}
3971 4120
3972void 4121void
3973ev_check_stop (EV_P_ ev_check *w) 4122ev_check_stop (EV_P_ ev_check *w) EV_THROW
3974{ 4123{
3975 clear_pending (EV_A_ (W)w); 4124 clear_pending (EV_A_ (W)w);
3976 if (expect_false (!ev_is_active (w))) 4125 if (expect_false (!ev_is_active (w)))
3977 return; 4126 return;
3978 4127
3991} 4140}
3992#endif 4141#endif
3993 4142
3994#if EV_EMBED_ENABLE 4143#if EV_EMBED_ENABLE
3995void noinline 4144void noinline
3996ev_embed_sweep (EV_P_ ev_embed *w) 4145ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW
3997{ 4146{
3998 ev_run (w->other, EVRUN_NOWAIT); 4147 ev_run (w->other, EVRUN_NOWAIT);
3999} 4148}
4000 4149
4001static void 4150static void
4049 ev_idle_stop (EV_A_ idle); 4198 ev_idle_stop (EV_A_ idle);
4050} 4199}
4051#endif 4200#endif
4052 4201
4053void 4202void
4054ev_embed_start (EV_P_ ev_embed *w) 4203ev_embed_start (EV_P_ ev_embed *w) EV_THROW
4055{ 4204{
4056 if (expect_false (ev_is_active (w))) 4205 if (expect_false (ev_is_active (w)))
4057 return; 4206 return;
4058 4207
4059 { 4208 {
4080 4229
4081 EV_FREQUENT_CHECK; 4230 EV_FREQUENT_CHECK;
4082} 4231}
4083 4232
4084void 4233void
4085ev_embed_stop (EV_P_ ev_embed *w) 4234ev_embed_stop (EV_P_ ev_embed *w) EV_THROW
4086{ 4235{
4087 clear_pending (EV_A_ (W)w); 4236 clear_pending (EV_A_ (W)w);
4088 if (expect_false (!ev_is_active (w))) 4237 if (expect_false (!ev_is_active (w)))
4089 return; 4238 return;
4090 4239
4100} 4249}
4101#endif 4250#endif
4102 4251
4103#if EV_FORK_ENABLE 4252#if EV_FORK_ENABLE
4104void 4253void
4105ev_fork_start (EV_P_ ev_fork *w) 4254ev_fork_start (EV_P_ ev_fork *w) EV_THROW
4106{ 4255{
4107 if (expect_false (ev_is_active (w))) 4256 if (expect_false (ev_is_active (w)))
4108 return; 4257 return;
4109 4258
4110 EV_FREQUENT_CHECK; 4259 EV_FREQUENT_CHECK;
4115 4264
4116 EV_FREQUENT_CHECK; 4265 EV_FREQUENT_CHECK;
4117} 4266}
4118 4267
4119void 4268void
4120ev_fork_stop (EV_P_ ev_fork *w) 4269ev_fork_stop (EV_P_ ev_fork *w) EV_THROW
4121{ 4270{
4122 clear_pending (EV_A_ (W)w); 4271 clear_pending (EV_A_ (W)w);
4123 if (expect_false (!ev_is_active (w))) 4272 if (expect_false (!ev_is_active (w)))
4124 return; 4273 return;
4125 4274
4138} 4287}
4139#endif 4288#endif
4140 4289
4141#if EV_CLEANUP_ENABLE 4290#if EV_CLEANUP_ENABLE
4142void 4291void
4143ev_cleanup_start (EV_P_ ev_cleanup *w) 4292ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW
4144{ 4293{
4145 if (expect_false (ev_is_active (w))) 4294 if (expect_false (ev_is_active (w)))
4146 return; 4295 return;
4147 4296
4148 EV_FREQUENT_CHECK; 4297 EV_FREQUENT_CHECK;
4155 ev_unref (EV_A); 4304 ev_unref (EV_A);
4156 EV_FREQUENT_CHECK; 4305 EV_FREQUENT_CHECK;
4157} 4306}
4158 4307
4159void 4308void
4160ev_cleanup_stop (EV_P_ ev_cleanup *w) 4309ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW
4161{ 4310{
4162 clear_pending (EV_A_ (W)w); 4311 clear_pending (EV_A_ (W)w);
4163 if (expect_false (!ev_is_active (w))) 4312 if (expect_false (!ev_is_active (w)))
4164 return; 4313 return;
4165 4314
4179} 4328}
4180#endif 4329#endif
4181 4330
4182#if EV_ASYNC_ENABLE 4331#if EV_ASYNC_ENABLE
4183void 4332void
4184ev_async_start (EV_P_ ev_async *w) 4333ev_async_start (EV_P_ ev_async *w) EV_THROW
4185{ 4334{
4186 if (expect_false (ev_is_active (w))) 4335 if (expect_false (ev_is_active (w)))
4187 return; 4336 return;
4188 4337
4189 w->sent = 0; 4338 w->sent = 0;
4198 4347
4199 EV_FREQUENT_CHECK; 4348 EV_FREQUENT_CHECK;
4200} 4349}
4201 4350
4202void 4351void
4203ev_async_stop (EV_P_ ev_async *w) 4352ev_async_stop (EV_P_ ev_async *w) EV_THROW
4204{ 4353{
4205 clear_pending (EV_A_ (W)w); 4354 clear_pending (EV_A_ (W)w);
4206 if (expect_false (!ev_is_active (w))) 4355 if (expect_false (!ev_is_active (w)))
4207 return; 4356 return;
4208 4357
4219 4368
4220 EV_FREQUENT_CHECK; 4369 EV_FREQUENT_CHECK;
4221} 4370}
4222 4371
4223void 4372void
4224ev_async_send (EV_P_ ev_async *w) 4373ev_async_send (EV_P_ ev_async *w) EV_THROW
4225{ 4374{
4226 w->sent = 1; 4375 w->sent = 1;
4227 evpipe_write (EV_A_ &async_pending); 4376 evpipe_write (EV_A_ &async_pending);
4228} 4377}
4229#endif 4378#endif
4266 4415
4267 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); 4416 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
4268} 4417}
4269 4418
4270void 4419void
4271ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 4420ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW
4272{ 4421{
4273 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 4422 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
4274 4423
4275 if (expect_false (!once)) 4424 if (expect_false (!once))
4276 { 4425 {
4298 4447
4299/*****************************************************************************/ 4448/*****************************************************************************/
4300 4449
4301#if EV_WALK_ENABLE 4450#if EV_WALK_ENABLE
4302void ecb_cold 4451void ecb_cold
4303ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 4452ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW
4304{ 4453{
4305 int i, j; 4454 int i, j;
4306 ev_watcher_list *wl, *wn; 4455 ev_watcher_list *wl, *wn;
4307 4456
4308 if (types & (EV_IO | EV_EMBED)) 4457 if (types & (EV_IO | EV_EMBED))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines