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 |
… | |
… | |
219 | #define _DARWIN_UNLIMITED_SELECT 1 |
219 | #define _DARWIN_UNLIMITED_SELECT 1 |
220 | |
220 | |
221 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
221 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
222 | |
222 | |
223 | /* try to deduce the maximum number of signals on this platform */ |
223 | /* try to deduce the maximum number of signals on this platform */ |
224 | #if defined (EV_NSIG) |
224 | #if defined EV_NSIG |
225 | /* use what's provided */ |
225 | /* use what's provided */ |
226 | #elif defined (NSIG) |
226 | #elif defined NSIG |
227 | # define EV_NSIG (NSIG) |
227 | # define EV_NSIG (NSIG) |
228 | #elif defined(_NSIG) |
228 | #elif defined _NSIG |
229 | # define EV_NSIG (_NSIG) |
229 | # define EV_NSIG (_NSIG) |
230 | #elif defined (SIGMAX) |
230 | #elif defined SIGMAX |
231 | # define EV_NSIG (SIGMAX+1) |
231 | # define EV_NSIG (SIGMAX+1) |
232 | #elif defined (SIG_MAX) |
232 | #elif defined SIG_MAX |
233 | # define EV_NSIG (SIG_MAX+1) |
233 | # define EV_NSIG (SIG_MAX+1) |
234 | #elif defined (_SIG_MAX) |
234 | #elif defined _SIG_MAX |
235 | # define EV_NSIG (_SIG_MAX+1) |
235 | # define EV_NSIG (_SIG_MAX+1) |
236 | #elif defined (MAXSIG) |
236 | #elif defined MAXSIG |
237 | # define EV_NSIG (MAXSIG+1) |
237 | # define EV_NSIG (MAXSIG+1) |
238 | #elif defined (MAX_SIG) |
238 | #elif defined MAX_SIG |
239 | # define EV_NSIG (MAX_SIG+1) |
239 | # define EV_NSIG (MAX_SIG+1) |
240 | #elif defined (SIGARRAYSIZE) |
240 | #elif defined SIGARRAYSIZE |
241 | # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ |
241 | # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ |
242 | #elif defined (_sys_nsig) |
242 | #elif defined _sys_nsig |
243 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
243 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
244 | #else |
244 | #else |
245 | # error "unable to find value for NSIG, please report" |
245 | # error "unable to find value for NSIG, please report" |
246 | /* to make it compile regardless, just remove the above line, */ |
246 | /* to make it compile regardless, just remove the above line, */ |
247 | /* but consider reporting it, too! :) */ |
247 | /* but consider reporting it, too! :) */ |
… | |
… | |
259 | # define EV_USE_CLOCK_SYSCALL 0 |
259 | # define EV_USE_CLOCK_SYSCALL 0 |
260 | # endif |
260 | # endif |
261 | #endif |
261 | #endif |
262 | |
262 | |
263 | #ifndef EV_USE_MONOTONIC |
263 | #ifndef EV_USE_MONOTONIC |
264 | # if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 |
264 | # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0 |
265 | # define EV_USE_MONOTONIC EV_FEATURE_OS |
265 | # define EV_USE_MONOTONIC EV_FEATURE_OS |
266 | # else |
266 | # else |
267 | # define EV_USE_MONOTONIC 0 |
267 | # define EV_USE_MONOTONIC 0 |
268 | # endif |
268 | # endif |
269 | #endif |
269 | #endif |
… | |
… | |
395 | # define EV_USE_INOTIFY 0 |
395 | # define EV_USE_INOTIFY 0 |
396 | #endif |
396 | #endif |
397 | |
397 | |
398 | #if !EV_USE_NANOSLEEP |
398 | #if !EV_USE_NANOSLEEP |
399 | /* hp-ux has it in sys/time.h, which we unconditionally include above */ |
399 | /* hp-ux has it in sys/time.h, which we unconditionally include above */ |
400 | # if !defined(_WIN32) && !defined(__hpux) |
400 | # if !defined _WIN32 && !defined __hpux |
401 | # include <sys/select.h> |
401 | # include <sys/select.h> |
402 | # endif |
402 | # endif |
403 | #endif |
403 | #endif |
404 | |
404 | |
405 | #if EV_USE_INOTIFY |
405 | #if EV_USE_INOTIFY |
… | |
… | |
533 | * or so. |
533 | * or so. |
534 | * we try to detect these and simply assume they are not gcc - if they have |
534 | * we try to detect these and simply assume they are not gcc - if they have |
535 | * an issue with that they should have done it right in the first place. |
535 | * an issue with that they should have done it right in the first place. |
536 | */ |
536 | */ |
537 | #ifndef ECB_GCC_VERSION |
537 | #ifndef ECB_GCC_VERSION |
538 | #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__) |
538 | #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__ |
539 | #define ECB_GCC_VERSION(major,minor) 0 |
539 | #define ECB_GCC_VERSION(major,minor) 0 |
540 | #else |
540 | #else |
541 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
541 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
542 | #endif |
542 | #endif |
543 | #endif |
543 | #endif |
… | |
… | |
554 | #if ECB_NO_THREADS || ECB_NO_SMP |
554 | #if ECB_NO_THREADS || ECB_NO_SMP |
555 | #define ECB_MEMORY_FENCE do { } while (0) |
555 | #define ECB_MEMORY_FENCE do { } while (0) |
556 | #endif |
556 | #endif |
557 | |
557 | |
558 | #ifndef ECB_MEMORY_FENCE |
558 | #ifndef ECB_MEMORY_FENCE |
559 | #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
559 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
560 | #if __i386 || __i386__ |
560 | #if __i386 || __i386__ |
561 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
561 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
562 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
562 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
563 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
563 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
564 | #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ |
564 | #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ |
565 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
565 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
566 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
566 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
567 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ |
567 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ |
568 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
568 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
569 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
569 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
570 | #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ |
570 | #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ |
571 | || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) |
571 | || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ |
572 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
572 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
573 | #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ |
573 | #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ |
574 | || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) |
574 | || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__ |
575 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") |
575 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") |
576 | #elif __sparc || __sparc__ |
576 | #elif __sparc || __sparc__ |
577 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory") |
577 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory") |
578 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") |
578 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") |
579 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") |
579 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") |
580 | #elif defined(__s390__) || defined(__s390x__) |
580 | #elif defined __s390__ || defined __s390x__ |
581 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") |
581 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") |
582 | #elif defined(__mips__) |
582 | #elif defined __mips__ |
583 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
583 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
|
|
584 | #elif defined __alpha__ |
|
|
585 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory") |
584 | #endif |
586 | #endif |
585 | #endif |
587 | #endif |
586 | #endif |
588 | #endif |
587 | |
589 | |
588 | #ifndef ECB_MEMORY_FENCE |
590 | #ifndef ECB_MEMORY_FENCE |
589 | #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) |
591 | #if ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
590 | #define ECB_MEMORY_FENCE __sync_synchronize () |
592 | #define ECB_MEMORY_FENCE __sync_synchronize () |
591 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
593 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
592 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
594 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
593 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
595 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
594 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
596 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
595 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
597 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
596 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
598 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
597 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
599 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
598 | #elif defined(_WIN32) |
600 | #elif defined _WIN32 |
599 | #include <WinNT.h> |
601 | #include <WinNT.h> |
600 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
602 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
601 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
603 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
602 | #include <mbarrier.h> |
604 | #include <mbarrier.h> |
603 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
605 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
604 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () |
606 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () |
605 | #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () |
607 | #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () |
|
|
608 | #elif __xlC__ |
|
|
609 | #define ECB_MEMORY_FENCE __sync () |
606 | #endif |
610 | #endif |
607 | #endif |
611 | #endif |
608 | |
612 | |
609 | #ifndef ECB_MEMORY_FENCE |
613 | #ifndef ECB_MEMORY_FENCE |
610 | #if !ECB_AVOID_PTHREADS |
614 | #if !ECB_AVOID_PTHREADS |
… | |
… | |
622 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
626 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
623 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
627 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
624 | #endif |
628 | #endif |
625 | #endif |
629 | #endif |
626 | |
630 | |
627 | #if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) |
631 | #if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE |
628 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
632 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
629 | #endif |
633 | #endif |
630 | |
634 | |
631 | #if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) |
635 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
632 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
636 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
633 | #endif |
637 | #endif |
634 | |
638 | |
635 | /*****************************************************************************/ |
639 | /*****************************************************************************/ |
636 | |
640 | |
… | |
… | |
1330 | #if EV_USE_NANOSLEEP |
1334 | #if EV_USE_NANOSLEEP |
1331 | struct timespec ts; |
1335 | struct timespec ts; |
1332 | |
1336 | |
1333 | EV_TS_SET (ts, delay); |
1337 | EV_TS_SET (ts, delay); |
1334 | nanosleep (&ts, 0); |
1338 | nanosleep (&ts, 0); |
1335 | #elif defined(_WIN32) |
1339 | #elif defined _WIN32 |
1336 | Sleep ((unsigned long)(delay * 1e3)); |
1340 | Sleep ((unsigned long)(delay * 1e3)); |
1337 | #else |
1341 | #else |
1338 | struct timeval tv; |
1342 | struct timeval tv; |
1339 | |
1343 | |
1340 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
1344 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
… | |
… | |
2928 | |
2932 | |
2929 | mn_now = ev_rt_now; |
2933 | mn_now = ev_rt_now; |
2930 | } |
2934 | } |
2931 | } |
2935 | } |
2932 | |
2936 | |
2933 | void |
2937 | int |
2934 | ev_run (EV_P_ int flags) |
2938 | ev_run (EV_P_ int flags) |
2935 | { |
2939 | { |
2936 | #if EV_FEATURE_API |
2940 | #if EV_FEATURE_API |
2937 | ++loop_depth; |
2941 | ++loop_depth; |
2938 | #endif |
2942 | #endif |
… | |
… | |
3095 | loop_done = EVBREAK_CANCEL; |
3099 | loop_done = EVBREAK_CANCEL; |
3096 | |
3100 | |
3097 | #if EV_FEATURE_API |
3101 | #if EV_FEATURE_API |
3098 | --loop_depth; |
3102 | --loop_depth; |
3099 | #endif |
3103 | #endif |
|
|
3104 | |
|
|
3105 | return activecnt; |
3100 | } |
3106 | } |
3101 | |
3107 | |
3102 | void |
3108 | void |
3103 | ev_break (EV_P_ int how) |
3109 | ev_break (EV_P_ int how) |
3104 | { |
3110 | { |
… | |
… | |
3739 | } |
3745 | } |
3740 | |
3746 | |
3741 | inline_size int |
3747 | inline_size int |
3742 | infy_newfd (void) |
3748 | infy_newfd (void) |
3743 | { |
3749 | { |
3744 | #if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) |
3750 | #if defined IN_CLOEXEC && defined IN_NONBLOCK |
3745 | int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); |
3751 | int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); |
3746 | if (fd >= 0) |
3752 | if (fd >= 0) |
3747 | return fd; |
3753 | return fd; |
3748 | #endif |
3754 | #endif |
3749 | return inotify_init (); |
3755 | return inotify_init (); |