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 | EV_CPP(extern "C" {) |
188 | #if EV_NO_THREADS |
|
|
189 | # undef EV_NO_SMP |
|
|
190 | # define EV_NO_SMP 1 |
|
|
191 | # undef ECB_NO_THREADS |
|
|
192 | # define ECB_NO_THREADS 1 |
|
|
193 | #endif |
|
|
194 | #if EV_NO_SMP |
|
|
195 | # undef EV_NO_SMP |
|
|
196 | # define ECB_NO_SMP 1 |
|
|
197 | #endif |
189 | |
198 | |
190 | #ifndef _WIN32 |
199 | #ifndef _WIN32 |
191 | # include <sys/time.h> |
200 | # include <sys/time.h> |
192 | # include <sys/wait.h> |
201 | # include <sys/wait.h> |
193 | # include <unistd.h> |
202 | # include <unistd.h> |
194 | #else |
203 | #else |
195 | # include <io.h> |
204 | # include <io.h> |
196 | # define WIN32_LEAN_AND_MEAN |
205 | # define WIN32_LEAN_AND_MEAN |
197 | # include <windows.h> |
206 | # include <windows.h> |
|
|
207 | # include <winsock2.h> |
198 | # ifndef EV_SELECT_IS_WINSOCKET |
208 | # ifndef EV_SELECT_IS_WINSOCKET |
199 | # define EV_SELECT_IS_WINSOCKET 1 |
209 | # define EV_SELECT_IS_WINSOCKET 1 |
200 | # endif |
210 | # endif |
201 | # undef EV_AVOID_STDIO |
211 | # undef EV_AVOID_STDIO |
202 | #endif |
212 | #endif |
… | |
… | |
210 | #define _DARWIN_UNLIMITED_SELECT 1 |
220 | #define _DARWIN_UNLIMITED_SELECT 1 |
211 | |
221 | |
212 | /* 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 */ |
213 | |
223 | |
214 | /* try to deduce the maximum number of signals on this platform */ |
224 | /* try to deduce the maximum number of signals on this platform */ |
215 | #if defined (EV_NSIG) |
225 | #if defined EV_NSIG |
216 | /* use what's provided */ |
226 | /* use what's provided */ |
217 | #elif defined (NSIG) |
227 | #elif defined NSIG |
218 | # define EV_NSIG (NSIG) |
228 | # define EV_NSIG (NSIG) |
219 | #elif defined(_NSIG) |
229 | #elif defined _NSIG |
220 | # define EV_NSIG (_NSIG) |
230 | # define EV_NSIG (_NSIG) |
221 | #elif defined (SIGMAX) |
231 | #elif defined SIGMAX |
222 | # define EV_NSIG (SIGMAX+1) |
232 | # define EV_NSIG (SIGMAX+1) |
223 | #elif defined (SIG_MAX) |
233 | #elif defined SIG_MAX |
224 | # define EV_NSIG (SIG_MAX+1) |
234 | # define EV_NSIG (SIG_MAX+1) |
225 | #elif defined (_SIG_MAX) |
235 | #elif defined _SIG_MAX |
226 | # define EV_NSIG (_SIG_MAX+1) |
236 | # define EV_NSIG (_SIG_MAX+1) |
227 | #elif defined (MAXSIG) |
237 | #elif defined MAXSIG |
228 | # define EV_NSIG (MAXSIG+1) |
238 | # define EV_NSIG (MAXSIG+1) |
229 | #elif defined (MAX_SIG) |
239 | #elif defined MAX_SIG |
230 | # define EV_NSIG (MAX_SIG+1) |
240 | # define EV_NSIG (MAX_SIG+1) |
231 | #elif defined (SIGARRAYSIZE) |
241 | #elif defined SIGARRAYSIZE |
232 | # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ |
242 | # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ |
233 | #elif defined (_sys_nsig) |
243 | #elif defined _sys_nsig |
234 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
244 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
235 | #else |
245 | #else |
236 | # error "unable to find value for NSIG, please report" |
246 | # error "unable to find value for NSIG, please report" |
237 | /* to make it compile regardless, just remove the above line, */ |
247 | /* to make it compile regardless, just remove the above line, */ |
238 | /* but consider reporting it, too! :) */ |
248 | /* but consider reporting it, too! :) */ |
… | |
… | |
250 | # define EV_USE_CLOCK_SYSCALL 0 |
260 | # define EV_USE_CLOCK_SYSCALL 0 |
251 | # endif |
261 | # endif |
252 | #endif |
262 | #endif |
253 | |
263 | |
254 | #ifndef EV_USE_MONOTONIC |
264 | #ifndef EV_USE_MONOTONIC |
255 | # if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 |
265 | # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0 |
256 | # define EV_USE_MONOTONIC EV_FEATURE_OS |
266 | # define EV_USE_MONOTONIC EV_FEATURE_OS |
257 | # else |
267 | # else |
258 | # define EV_USE_MONOTONIC 0 |
268 | # define EV_USE_MONOTONIC 0 |
259 | # endif |
269 | # endif |
260 | #endif |
270 | #endif |
… | |
… | |
350 | #endif |
360 | #endif |
351 | |
361 | |
352 | /* 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, */ |
353 | /* which makes programs even slower. might work on other unices, too. */ |
363 | /* which makes programs even slower. might work on other unices, too. */ |
354 | #if EV_USE_CLOCK_SYSCALL |
364 | #if EV_USE_CLOCK_SYSCALL |
355 | # include <syscall.h> |
365 | # include <sys/syscall.h> |
356 | # ifdef SYS_clock_gettime |
366 | # ifdef SYS_clock_gettime |
357 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
367 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
358 | # undef EV_USE_MONOTONIC |
368 | # undef EV_USE_MONOTONIC |
359 | # define EV_USE_MONOTONIC 1 |
369 | # define EV_USE_MONOTONIC 1 |
360 | # else |
370 | # else |
… | |
… | |
386 | # define EV_USE_INOTIFY 0 |
396 | # define EV_USE_INOTIFY 0 |
387 | #endif |
397 | #endif |
388 | |
398 | |
389 | #if !EV_USE_NANOSLEEP |
399 | #if !EV_USE_NANOSLEEP |
390 | /* 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 */ |
391 | # if !defined(_WIN32) && !defined(__hpux) |
401 | # if !defined _WIN32 && !defined __hpux |
392 | # include <sys/select.h> |
402 | # include <sys/select.h> |
393 | # endif |
403 | # endif |
394 | #endif |
404 | #endif |
395 | |
405 | |
396 | #if EV_USE_INOTIFY |
406 | #if EV_USE_INOTIFY |
… | |
… | |
469 | /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ |
479 | /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ |
470 | /* ECB.H BEGIN */ |
480 | /* ECB.H BEGIN */ |
471 | /* |
481 | /* |
472 | * libecb - http://software.schmorp.de/pkg/libecb |
482 | * libecb - http://software.schmorp.de/pkg/libecb |
473 | * |
483 | * |
474 | * Copyright (©) 2009-2011 Marc Alexander Lehmann <libecb@schmorp.de> |
484 | * Copyright (©) 2009-2012 Marc Alexander Lehmann <libecb@schmorp.de> |
475 | * Copyright (©) 2011 Emanuele Giaquinta |
485 | * Copyright (©) 2011 Emanuele Giaquinta |
476 | * All rights reserved. |
486 | * All rights reserved. |
477 | * |
487 | * |
478 | * Redistribution and use in source and binary forms, with or without modifica- |
488 | * Redistribution and use in source and binary forms, with or without modifica- |
479 | * tion, are permitted provided that the following conditions are met: |
489 | * tion, are permitted provided that the following conditions are met: |
… | |
… | |
524 | * or so. |
534 | * or so. |
525 | * we try to detect these and simply assume they are not gcc - if they have |
535 | * we try to detect these and simply assume they are not gcc - if they have |
526 | * an issue with that they should have done it right in the first place. |
536 | * an issue with that they should have done it right in the first place. |
527 | */ |
537 | */ |
528 | #ifndef ECB_GCC_VERSION |
538 | #ifndef ECB_GCC_VERSION |
529 | #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__) |
539 | #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__ |
530 | #define ECB_GCC_VERSION(major,minor) 0 |
540 | #define ECB_GCC_VERSION(major,minor) 0 |
531 | #else |
541 | #else |
532 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
542 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
533 | #endif |
543 | #endif |
534 | #endif |
544 | #endif |
… | |
… | |
536 | /*****************************************************************************/ |
546 | /*****************************************************************************/ |
537 | |
547 | |
538 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
548 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
539 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
549 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
540 | |
550 | |
|
|
551 | #if ECB_NO_THREADS |
|
|
552 | # define ECB_NO_SMP 1 |
|
|
553 | #endif |
|
|
554 | |
541 | #if ECB_NO_THREADS || ECB_NO_SMP |
555 | #if ECB_NO_THREADS || ECB_NO_SMP |
542 | #define ECB_MEMORY_FENCE do { } while (0) |
556 | #define ECB_MEMORY_FENCE do { } while (0) |
543 | #endif |
557 | #endif |
544 | |
558 | |
545 | #ifndef ECB_MEMORY_FENCE |
559 | #ifndef ECB_MEMORY_FENCE |
546 | #if ECB_GCC_VERSION(2,5) |
560 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
547 | #if __x86 |
561 | #if __i386 || __i386__ |
548 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
562 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
549 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
563 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
550 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
564 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
551 | #elif __amd64 |
565 | #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ |
552 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
566 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
553 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
567 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
554 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ |
568 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ |
555 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
569 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
556 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
570 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
557 | #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ |
571 | #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ |
558 | || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) |
572 | || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ |
559 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,4" : : "r" (0) : "memory") |
573 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
560 | #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ |
574 | #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ |
561 | || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) |
575 | || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__ |
562 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dsb" : : : "memory") |
576 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") |
|
|
577 | #elif __sparc || __sparc__ |
|
|
578 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory") |
|
|
579 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") |
|
|
580 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") |
|
|
581 | #elif defined __s390__ || defined __s390x__ |
|
|
582 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") |
|
|
583 | #elif defined __mips__ |
|
|
584 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
|
|
585 | #elif defined __alpha__ |
|
|
586 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory") |
563 | #endif |
587 | #endif |
564 | #endif |
588 | #endif |
565 | #endif |
589 | #endif |
566 | |
590 | |
567 | #ifndef ECB_MEMORY_FENCE |
591 | #ifndef ECB_MEMORY_FENCE |
568 | #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) |
592 | #if ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
569 | #define ECB_MEMORY_FENCE __sync_synchronize () |
593 | #define ECB_MEMORY_FENCE __sync_synchronize () |
570 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
594 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
571 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
595 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
572 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
596 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
573 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
597 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
574 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
598 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
575 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
599 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
576 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
600 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
577 | #elif defined(_WIN32) |
601 | #elif defined _WIN32 |
578 | #include <WinNT.h> |
602 | #include <WinNT.h> |
579 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
603 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
|
|
604 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
|
|
605 | #include <mbarrier.h> |
|
|
606 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
|
|
607 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () |
|
|
608 | #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () |
|
|
609 | #elif __xlC__ |
|
|
610 | #define ECB_MEMORY_FENCE __sync () |
580 | #endif |
611 | #endif |
581 | #endif |
612 | #endif |
582 | |
613 | |
583 | #ifndef ECB_MEMORY_FENCE |
614 | #ifndef ECB_MEMORY_FENCE |
584 | #if !ECB_AVOID_PTHREADS |
615 | #if !ECB_AVOID_PTHREADS |
… | |
… | |
596 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
627 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
597 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
628 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
598 | #endif |
629 | #endif |
599 | #endif |
630 | #endif |
600 | |
631 | |
601 | #if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) |
632 | #if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE |
602 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
633 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
603 | #endif |
634 | #endif |
604 | |
635 | |
605 | #if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) |
636 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
606 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
637 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
607 | #endif |
638 | #endif |
608 | |
639 | |
609 | /*****************************************************************************/ |
640 | /*****************************************************************************/ |
610 | |
641 | |
… | |
… | |
759 | |
790 | |
760 | return r + ecb_ld32 (x); |
791 | return r + ecb_ld32 (x); |
761 | } |
792 | } |
762 | #endif |
793 | #endif |
763 | |
794 | |
|
|
795 | ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const; |
|
|
796 | ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) |
|
|
797 | { |
|
|
798 | return ( (x * 0x0802U & 0x22110U) |
|
|
799 | | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; |
|
|
800 | } |
|
|
801 | |
|
|
802 | ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) ecb_const; |
|
|
803 | ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) |
|
|
804 | { |
|
|
805 | x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1); |
|
|
806 | x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2); |
|
|
807 | x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4); |
|
|
808 | x = ( x >> 8 ) | ( x << 8); |
|
|
809 | |
|
|
810 | return x; |
|
|
811 | } |
|
|
812 | |
|
|
813 | ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) ecb_const; |
|
|
814 | ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) |
|
|
815 | { |
|
|
816 | x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1); |
|
|
817 | x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2); |
|
|
818 | x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4); |
|
|
819 | x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8); |
|
|
820 | x = ( x >> 16 ) | ( x << 16); |
|
|
821 | |
|
|
822 | return x; |
|
|
823 | } |
|
|
824 | |
764 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
825 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
765 | /* so for this version we are lazy */ |
826 | /* so for this version we are lazy */ |
766 | ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; |
827 | ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; |
767 | ecb_function_ int |
828 | ecb_function_ int |
768 | ecb_popcount64 (uint64_t x) |
829 | ecb_popcount64 (uint64_t x) |
… | |
… | |
817 | |
878 | |
818 | #if ECB_GCC_VERSION(4,5) |
879 | #if ECB_GCC_VERSION(4,5) |
819 | #define ecb_unreachable() __builtin_unreachable () |
880 | #define ecb_unreachable() __builtin_unreachable () |
820 | #else |
881 | #else |
821 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
882 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
822 | ecb_function_ void ecb_unreachable (void) ecb_noreturn; |
883 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
823 | ecb_function_ void ecb_unreachable (void) { } |
884 | ecb_inline void ecb_unreachable (void) { } |
824 | #endif |
885 | #endif |
825 | |
886 | |
826 | /* try to tell the compiler that some condition is definitely true */ |
887 | /* try to tell the compiler that some condition is definitely true */ |
827 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
888 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
828 | |
889 | |
829 | ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; |
890 | ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; |
830 | ecb_function_ unsigned char |
891 | ecb_inline unsigned char |
831 | ecb_byteorder_helper (void) |
892 | ecb_byteorder_helper (void) |
832 | { |
893 | { |
833 | const uint32_t u = 0x11223344; |
894 | const uint32_t u = 0x11223344; |
834 | return *(unsigned char *)&u; |
895 | return *(unsigned char *)&u; |
835 | } |
896 | } |
836 | |
897 | |
837 | ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; |
898 | ecb_inline ecb_bool ecb_big_endian (void) ecb_const; |
838 | ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
899 | ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
839 | ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; |
900 | ecb_inline ecb_bool ecb_little_endian (void) ecb_const; |
840 | ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
901 | ecb_inline ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
841 | |
902 | |
842 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
903 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
843 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
904 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
844 | #else |
905 | #else |
845 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
906 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
|
|
907 | #endif |
|
|
908 | |
|
|
909 | #if __cplusplus |
|
|
910 | template<typename T> |
|
|
911 | static inline T ecb_div_rd (T val, T div) |
|
|
912 | { |
|
|
913 | return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; |
|
|
914 | } |
|
|
915 | template<typename T> |
|
|
916 | static inline T ecb_div_ru (T val, T div) |
|
|
917 | { |
|
|
918 | return val < 0 ? - ((-val ) / div) : (val + div - 1) / div; |
|
|
919 | } |
|
|
920 | #else |
|
|
921 | #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) |
|
|
922 | #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) |
846 | #endif |
923 | #endif |
847 | |
924 | |
848 | #if ecb_cplusplus_does_not_suck |
925 | #if ecb_cplusplus_does_not_suck |
849 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
926 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
850 | template<typename T, int N> |
927 | template<typename T, int N> |
… | |
… | |
859 | #endif |
936 | #endif |
860 | |
937 | |
861 | /* ECB.H END */ |
938 | /* ECB.H END */ |
862 | |
939 | |
863 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
940 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
|
|
941 | /* if your architecture doesn't need memory fences, e.g. because it is |
|
|
942 | * single-cpu/core, or if you use libev in a project that doesn't use libev |
|
|
943 | * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling |
|
|
944 | * libev, in which cases the memory fences become nops. |
|
|
945 | * alternatively, you can remove this #error and link against libpthread, |
|
|
946 | * which will then provide the memory fences. |
|
|
947 | */ |
|
|
948 | # error "memory fences not defined for your architecture, please report" |
|
|
949 | #endif |
|
|
950 | |
864 | # undef ECB_MEMORY_FENCE |
951 | #ifndef ECB_MEMORY_FENCE |
865 | # undef ECB_MEMORY_FENCE_ACQUIRE |
952 | # define ECB_MEMORY_FENCE do { } while (0) |
866 | # undef ECB_MEMORY_FENCE_RELEASE |
953 | # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
954 | # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
867 | #endif |
955 | #endif |
868 | |
956 | |
869 | #define expect_false(cond) ecb_expect_false (cond) |
957 | #define expect_false(cond) ecb_expect_false (cond) |
870 | #define expect_true(cond) ecb_expect_true (cond) |
958 | #define expect_true(cond) ecb_expect_true (cond) |
871 | #define noinline ecb_noinline |
959 | #define noinline ecb_noinline |
… | |
… | |
1018 | { |
1106 | { |
1019 | write (STDERR_FILENO, msg, strlen (msg)); |
1107 | write (STDERR_FILENO, msg, strlen (msg)); |
1020 | } |
1108 | } |
1021 | #endif |
1109 | #endif |
1022 | |
1110 | |
1023 | static void (*syserr_cb)(const char *msg); |
1111 | static void (*syserr_cb)(const char *msg) EV_THROW; |
1024 | |
1112 | |
1025 | void ecb_cold |
1113 | void ecb_cold |
1026 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
1114 | ev_set_syserr_cb (void (*cb)(const char *msg)) EV_THROW |
1027 | { |
1115 | { |
1028 | syserr_cb = cb; |
1116 | syserr_cb = cb; |
1029 | } |
1117 | } |
1030 | |
1118 | |
1031 | static void noinline ecb_cold |
1119 | static void noinline ecb_cold |
… | |
… | |
1067 | free (ptr); |
1155 | free (ptr); |
1068 | return 0; |
1156 | return 0; |
1069 | #endif |
1157 | #endif |
1070 | } |
1158 | } |
1071 | |
1159 | |
1072 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
1160 | static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; |
1073 | |
1161 | |
1074 | void ecb_cold |
1162 | void ecb_cold |
1075 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
1163 | ev_set_allocator (void *(*cb)(void *ptr, long size)) EV_THROW |
1076 | { |
1164 | { |
1077 | alloc = cb; |
1165 | alloc = cb; |
1078 | } |
1166 | } |
1079 | |
1167 | |
1080 | inline_speed void * |
1168 | inline_speed void * |
… | |
… | |
1168 | #undef VAR |
1256 | #undef VAR |
1169 | }; |
1257 | }; |
1170 | #include "ev_wrap.h" |
1258 | #include "ev_wrap.h" |
1171 | |
1259 | |
1172 | static struct ev_loop default_loop_struct; |
1260 | static struct ev_loop default_loop_struct; |
1173 | struct ev_loop *ev_default_loop_ptr; |
1261 | EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ |
1174 | |
1262 | |
1175 | #else |
1263 | #else |
1176 | |
1264 | |
1177 | ev_tstamp ev_rt_now; |
1265 | EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */ |
1178 | #define VAR(name,decl) static decl; |
1266 | #define VAR(name,decl) static decl; |
1179 | #include "ev_vars.h" |
1267 | #include "ev_vars.h" |
1180 | #undef VAR |
1268 | #undef VAR |
1181 | |
1269 | |
1182 | static int ev_default_loop_ptr; |
1270 | static int ev_default_loop_ptr; |
… | |
… | |
1197 | |
1285 | |
1198 | /*****************************************************************************/ |
1286 | /*****************************************************************************/ |
1199 | |
1287 | |
1200 | #ifndef EV_HAVE_EV_TIME |
1288 | #ifndef EV_HAVE_EV_TIME |
1201 | ev_tstamp |
1289 | ev_tstamp |
1202 | ev_time (void) |
1290 | ev_time (void) EV_THROW |
1203 | { |
1291 | { |
1204 | #if EV_USE_REALTIME |
1292 | #if EV_USE_REALTIME |
1205 | if (expect_true (have_realtime)) |
1293 | if (expect_true (have_realtime)) |
1206 | { |
1294 | { |
1207 | struct timespec ts; |
1295 | struct timespec ts; |
… | |
… | |
1231 | return ev_time (); |
1319 | return ev_time (); |
1232 | } |
1320 | } |
1233 | |
1321 | |
1234 | #if EV_MULTIPLICITY |
1322 | #if EV_MULTIPLICITY |
1235 | ev_tstamp |
1323 | ev_tstamp |
1236 | ev_now (EV_P) |
1324 | ev_now (EV_P) EV_THROW |
1237 | { |
1325 | { |
1238 | return ev_rt_now; |
1326 | return ev_rt_now; |
1239 | } |
1327 | } |
1240 | #endif |
1328 | #endif |
1241 | |
1329 | |
1242 | void |
1330 | void |
1243 | ev_sleep (ev_tstamp delay) |
1331 | ev_sleep (ev_tstamp delay) EV_THROW |
1244 | { |
1332 | { |
1245 | if (delay > 0.) |
1333 | if (delay > 0.) |
1246 | { |
1334 | { |
1247 | #if EV_USE_NANOSLEEP |
1335 | #if EV_USE_NANOSLEEP |
1248 | struct timespec ts; |
1336 | struct timespec ts; |
1249 | |
1337 | |
1250 | EV_TS_SET (ts, delay); |
1338 | EV_TS_SET (ts, delay); |
1251 | nanosleep (&ts, 0); |
1339 | nanosleep (&ts, 0); |
1252 | #elif defined(_WIN32) |
1340 | #elif defined _WIN32 |
1253 | Sleep ((unsigned long)(delay * 1e3)); |
1341 | Sleep ((unsigned long)(delay * 1e3)); |
1254 | #else |
1342 | #else |
1255 | struct timeval tv; |
1343 | struct timeval tv; |
1256 | |
1344 | |
1257 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
1345 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
… | |
… | |
1276 | |
1364 | |
1277 | do |
1365 | do |
1278 | ncur <<= 1; |
1366 | ncur <<= 1; |
1279 | while (cnt > ncur); |
1367 | while (cnt > ncur); |
1280 | |
1368 | |
1281 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ |
1369 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */ |
1282 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
1370 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
1283 | { |
1371 | { |
1284 | ncur *= elem; |
1372 | ncur *= elem; |
1285 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
1373 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
1286 | ncur = ncur - sizeof (void *) * 4; |
1374 | ncur = ncur - sizeof (void *) * 4; |
… | |
… | |
1329 | pendingcb (EV_P_ ev_prepare *w, int revents) |
1417 | pendingcb (EV_P_ ev_prepare *w, int revents) |
1330 | { |
1418 | { |
1331 | } |
1419 | } |
1332 | |
1420 | |
1333 | void noinline |
1421 | void noinline |
1334 | ev_feed_event (EV_P_ void *w, int revents) |
1422 | ev_feed_event (EV_P_ void *w, int revents) EV_THROW |
1335 | { |
1423 | { |
1336 | W w_ = (W)w; |
1424 | W w_ = (W)w; |
1337 | int pri = ABSPRI (w_); |
1425 | int pri = ABSPRI (w_); |
1338 | |
1426 | |
1339 | if (expect_false (w_->pending)) |
1427 | if (expect_false (w_->pending)) |
… | |
… | |
1343 | w_->pending = ++pendingcnt [pri]; |
1431 | w_->pending = ++pendingcnt [pri]; |
1344 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
1432 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
1345 | pendings [pri][w_->pending - 1].w = w_; |
1433 | pendings [pri][w_->pending - 1].w = w_; |
1346 | pendings [pri][w_->pending - 1].events = revents; |
1434 | pendings [pri][w_->pending - 1].events = revents; |
1347 | } |
1435 | } |
|
|
1436 | |
|
|
1437 | pendingpri = NUMPRI - 1; |
1348 | } |
1438 | } |
1349 | |
1439 | |
1350 | inline_speed void |
1440 | inline_speed void |
1351 | feed_reverse (EV_P_ W w) |
1441 | feed_reverse (EV_P_ W w) |
1352 | { |
1442 | { |
… | |
… | |
1398 | if (expect_true (!anfd->reify)) |
1488 | if (expect_true (!anfd->reify)) |
1399 | fd_event_nocheck (EV_A_ fd, revents); |
1489 | fd_event_nocheck (EV_A_ fd, revents); |
1400 | } |
1490 | } |
1401 | |
1491 | |
1402 | void |
1492 | void |
1403 | ev_feed_fd_event (EV_P_ int fd, int revents) |
1493 | ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW |
1404 | { |
1494 | { |
1405 | if (fd >= 0 && fd < anfdmax) |
1495 | if (fd >= 0 && fd < anfdmax) |
1406 | fd_event_nocheck (EV_A_ fd, revents); |
1496 | fd_event_nocheck (EV_A_ fd, revents); |
1407 | } |
1497 | } |
1408 | |
1498 | |
… | |
… | |
1757 | } |
1847 | } |
1758 | |
1848 | |
1759 | inline_speed void |
1849 | inline_speed void |
1760 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1850 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1761 | { |
1851 | { |
|
|
1852 | ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */ |
|
|
1853 | |
1762 | if (expect_true (*flag)) |
1854 | if (expect_true (*flag)) |
1763 | return; |
1855 | return; |
1764 | |
1856 | |
1765 | *flag = 1; |
1857 | *flag = 1; |
1766 | |
1858 | |
… | |
… | |
1785 | write (evfd, &counter, sizeof (uint64_t)); |
1877 | write (evfd, &counter, sizeof (uint64_t)); |
1786 | } |
1878 | } |
1787 | else |
1879 | else |
1788 | #endif |
1880 | #endif |
1789 | { |
1881 | { |
1790 | /* win32 people keep sending patches that change this write() to send() */ |
1882 | #ifdef _WIN32 |
1791 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
1883 | WSABUF buf; |
1792 | /* so when you think this write should be a send instead, please find out */ |
1884 | DWORD sent; |
1793 | /* where your send() is from - it's definitely not the microsoft send, and */ |
1885 | buf.buf = &buf; |
1794 | /* tell me. thank you. */ |
1886 | buf.len = 1; |
|
|
1887 | WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0); |
|
|
1888 | #else |
1795 | write (evpipe [1], &(evpipe [1]), 1); |
1889 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
1890 | #endif |
1796 | } |
1891 | } |
1797 | |
1892 | |
1798 | errno = old_errno; |
1893 | errno = old_errno; |
1799 | } |
1894 | } |
1800 | } |
1895 | } |
… | |
… | |
1815 | read (evfd, &counter, sizeof (uint64_t)); |
1910 | read (evfd, &counter, sizeof (uint64_t)); |
1816 | } |
1911 | } |
1817 | else |
1912 | else |
1818 | #endif |
1913 | #endif |
1819 | { |
1914 | { |
1820 | char dummy; |
1915 | char dummy[4]; |
1821 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1916 | #ifdef _WIN32 |
|
|
1917 | WSABUF buf; |
|
|
1918 | DWORD recvd; |
|
|
1919 | buf.buf = dummy; |
|
|
1920 | buf.len = sizeof (dummy); |
|
|
1921 | WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, 0, 0, 0); |
|
|
1922 | #else |
1822 | read (evpipe [0], &dummy, 1); |
1923 | read (evpipe [0], &dummy, sizeof (dummy)); |
|
|
1924 | #endif |
1823 | } |
1925 | } |
1824 | } |
1926 | } |
1825 | |
1927 | |
1826 | pipe_write_skipped = 0; |
1928 | pipe_write_skipped = 0; |
|
|
1929 | |
|
|
1930 | ECB_MEMORY_FENCE; /* push out skipped, acquire flags */ |
1827 | |
1931 | |
1828 | #if EV_SIGNAL_ENABLE |
1932 | #if EV_SIGNAL_ENABLE |
1829 | if (sig_pending) |
1933 | if (sig_pending) |
1830 | { |
1934 | { |
1831 | sig_pending = 0; |
1935 | sig_pending = 0; |
|
|
1936 | |
|
|
1937 | ECB_MEMORY_FENCE_RELEASE; |
1832 | |
1938 | |
1833 | for (i = EV_NSIG - 1; i--; ) |
1939 | for (i = EV_NSIG - 1; i--; ) |
1834 | if (expect_false (signals [i].pending)) |
1940 | if (expect_false (signals [i].pending)) |
1835 | ev_feed_signal_event (EV_A_ i + 1); |
1941 | ev_feed_signal_event (EV_A_ i + 1); |
1836 | } |
1942 | } |
… | |
… | |
1838 | |
1944 | |
1839 | #if EV_ASYNC_ENABLE |
1945 | #if EV_ASYNC_ENABLE |
1840 | if (async_pending) |
1946 | if (async_pending) |
1841 | { |
1947 | { |
1842 | async_pending = 0; |
1948 | async_pending = 0; |
|
|
1949 | |
|
|
1950 | ECB_MEMORY_FENCE_RELEASE; |
1843 | |
1951 | |
1844 | for (i = asynccnt; i--; ) |
1952 | for (i = asynccnt; i--; ) |
1845 | if (asyncs [i]->sent) |
1953 | if (asyncs [i]->sent) |
1846 | { |
1954 | { |
1847 | asyncs [i]->sent = 0; |
1955 | asyncs [i]->sent = 0; |
… | |
… | |
1852 | } |
1960 | } |
1853 | |
1961 | |
1854 | /*****************************************************************************/ |
1962 | /*****************************************************************************/ |
1855 | |
1963 | |
1856 | void |
1964 | void |
1857 | ev_feed_signal (int signum) |
1965 | ev_feed_signal (int signum) EV_THROW |
1858 | { |
1966 | { |
1859 | #if EV_MULTIPLICITY |
1967 | #if EV_MULTIPLICITY |
1860 | EV_P = signals [signum - 1].loop; |
1968 | EV_P = signals [signum - 1].loop; |
1861 | |
1969 | |
1862 | if (!EV_A) |
1970 | if (!EV_A) |
… | |
… | |
1879 | |
1987 | |
1880 | ev_feed_signal (signum); |
1988 | ev_feed_signal (signum); |
1881 | } |
1989 | } |
1882 | |
1990 | |
1883 | void noinline |
1991 | void noinline |
1884 | ev_feed_signal_event (EV_P_ int signum) |
1992 | ev_feed_signal_event (EV_P_ int signum) EV_THROW |
1885 | { |
1993 | { |
1886 | WL w; |
1994 | WL w; |
1887 | |
1995 | |
1888 | if (expect_false (signum <= 0 || signum > EV_NSIG)) |
1996 | if (expect_false (signum <= 0 || signum > EV_NSIG)) |
1889 | return; |
1997 | return; |
… | |
… | |
2005 | #if EV_USE_SELECT |
2113 | #if EV_USE_SELECT |
2006 | # include "ev_select.c" |
2114 | # include "ev_select.c" |
2007 | #endif |
2115 | #endif |
2008 | |
2116 | |
2009 | int ecb_cold |
2117 | int ecb_cold |
2010 | ev_version_major (void) |
2118 | ev_version_major (void) EV_THROW |
2011 | { |
2119 | { |
2012 | return EV_VERSION_MAJOR; |
2120 | return EV_VERSION_MAJOR; |
2013 | } |
2121 | } |
2014 | |
2122 | |
2015 | int ecb_cold |
2123 | int ecb_cold |
2016 | ev_version_minor (void) |
2124 | ev_version_minor (void) EV_THROW |
2017 | { |
2125 | { |
2018 | return EV_VERSION_MINOR; |
2126 | return EV_VERSION_MINOR; |
2019 | } |
2127 | } |
2020 | |
2128 | |
2021 | /* return true if we are running with elevated privileges and should ignore env variables */ |
2129 | /* return true if we are running with elevated privileges and should ignore env variables */ |
… | |
… | |
2029 | || getgid () != getegid (); |
2137 | || getgid () != getegid (); |
2030 | #endif |
2138 | #endif |
2031 | } |
2139 | } |
2032 | |
2140 | |
2033 | unsigned int ecb_cold |
2141 | unsigned int ecb_cold |
2034 | ev_supported_backends (void) |
2142 | ev_supported_backends (void) EV_THROW |
2035 | { |
2143 | { |
2036 | unsigned int flags = 0; |
2144 | unsigned int flags = 0; |
2037 | |
2145 | |
2038 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
2146 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
2039 | if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; |
2147 | if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; |
… | |
… | |
2043 | |
2151 | |
2044 | return flags; |
2152 | return flags; |
2045 | } |
2153 | } |
2046 | |
2154 | |
2047 | unsigned int ecb_cold |
2155 | unsigned int ecb_cold |
2048 | ev_recommended_backends (void) |
2156 | ev_recommended_backends (void) EV_THROW |
2049 | { |
2157 | { |
2050 | unsigned int flags = ev_supported_backends (); |
2158 | unsigned int flags = ev_supported_backends (); |
2051 | |
2159 | |
2052 | #ifndef __NetBSD__ |
2160 | #ifndef __NetBSD__ |
2053 | /* kqueue is borked on everything but netbsd apparently */ |
2161 | /* kqueue is borked on everything but netbsd apparently */ |
… | |
… | |
2065 | |
2173 | |
2066 | return flags; |
2174 | return flags; |
2067 | } |
2175 | } |
2068 | |
2176 | |
2069 | unsigned int ecb_cold |
2177 | unsigned int ecb_cold |
2070 | ev_embeddable_backends (void) |
2178 | ev_embeddable_backends (void) EV_THROW |
2071 | { |
2179 | { |
2072 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
2180 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
2073 | |
2181 | |
2074 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
2182 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
2075 | if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ |
2183 | if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ |
… | |
… | |
2077 | |
2185 | |
2078 | return flags; |
2186 | return flags; |
2079 | } |
2187 | } |
2080 | |
2188 | |
2081 | unsigned int |
2189 | unsigned int |
2082 | ev_backend (EV_P) |
2190 | ev_backend (EV_P) EV_THROW |
2083 | { |
2191 | { |
2084 | return backend; |
2192 | return backend; |
2085 | } |
2193 | } |
2086 | |
2194 | |
2087 | #if EV_FEATURE_API |
2195 | #if EV_FEATURE_API |
2088 | unsigned int |
2196 | unsigned int |
2089 | ev_iteration (EV_P) |
2197 | ev_iteration (EV_P) EV_THROW |
2090 | { |
2198 | { |
2091 | return loop_count; |
2199 | return loop_count; |
2092 | } |
2200 | } |
2093 | |
2201 | |
2094 | unsigned int |
2202 | unsigned int |
2095 | ev_depth (EV_P) |
2203 | ev_depth (EV_P) EV_THROW |
2096 | { |
2204 | { |
2097 | return loop_depth; |
2205 | return loop_depth; |
2098 | } |
2206 | } |
2099 | |
2207 | |
2100 | void |
2208 | void |
2101 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
2209 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW |
2102 | { |
2210 | { |
2103 | io_blocktime = interval; |
2211 | io_blocktime = interval; |
2104 | } |
2212 | } |
2105 | |
2213 | |
2106 | void |
2214 | void |
2107 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
2215 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW |
2108 | { |
2216 | { |
2109 | timeout_blocktime = interval; |
2217 | timeout_blocktime = interval; |
2110 | } |
2218 | } |
2111 | |
2219 | |
2112 | void |
2220 | void |
2113 | ev_set_userdata (EV_P_ void *data) |
2221 | ev_set_userdata (EV_P_ void *data) EV_THROW |
2114 | { |
2222 | { |
2115 | userdata = data; |
2223 | userdata = data; |
2116 | } |
2224 | } |
2117 | |
2225 | |
2118 | void * |
2226 | void * |
2119 | ev_userdata (EV_P) |
2227 | ev_userdata (EV_P) EV_THROW |
2120 | { |
2228 | { |
2121 | return userdata; |
2229 | return userdata; |
2122 | } |
2230 | } |
2123 | |
2231 | |
2124 | void |
2232 | void |
2125 | ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
2233 | ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) EV_THROW |
2126 | { |
2234 | { |
2127 | invoke_cb = invoke_pending_cb; |
2235 | invoke_cb = invoke_pending_cb; |
2128 | } |
2236 | } |
2129 | |
2237 | |
2130 | void |
2238 | void |
2131 | ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
2239 | ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW |
2132 | { |
2240 | { |
2133 | release_cb = release; |
2241 | release_cb = release; |
2134 | acquire_cb = acquire; |
2242 | acquire_cb = acquire; |
2135 | } |
2243 | } |
2136 | #endif |
2244 | #endif |
2137 | |
2245 | |
2138 | /* initialise a loop structure, must be zero-initialised */ |
2246 | /* initialise a loop structure, must be zero-initialised */ |
2139 | static void noinline ecb_cold |
2247 | static void noinline ecb_cold |
2140 | loop_init (EV_P_ unsigned int flags) |
2248 | loop_init (EV_P_ unsigned int flags) EV_THROW |
2141 | { |
2249 | { |
2142 | if (!backend) |
2250 | if (!backend) |
2143 | { |
2251 | { |
2144 | origflags = flags; |
2252 | origflags = flags; |
2145 | |
2253 | |
… | |
… | |
2398 | } |
2506 | } |
2399 | |
2507 | |
2400 | #if EV_MULTIPLICITY |
2508 | #if EV_MULTIPLICITY |
2401 | |
2509 | |
2402 | struct ev_loop * ecb_cold |
2510 | struct ev_loop * ecb_cold |
2403 | ev_loop_new (unsigned int flags) |
2511 | ev_loop_new (unsigned int flags) EV_THROW |
2404 | { |
2512 | { |
2405 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2513 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2406 | |
2514 | |
2407 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2515 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2408 | loop_init (EV_A_ flags); |
2516 | loop_init (EV_A_ flags); |
… | |
… | |
2452 | } |
2560 | } |
2453 | #endif |
2561 | #endif |
2454 | |
2562 | |
2455 | #if EV_FEATURE_API |
2563 | #if EV_FEATURE_API |
2456 | void ecb_cold |
2564 | void ecb_cold |
2457 | ev_verify (EV_P) |
2565 | ev_verify (EV_P) EV_THROW |
2458 | { |
2566 | { |
2459 | #if EV_VERIFY |
2567 | #if EV_VERIFY |
2460 | int i; |
2568 | int i, j; |
2461 | WL w; |
2569 | WL w, w2; |
2462 | |
2570 | |
2463 | assert (activecnt >= -1); |
2571 | assert (activecnt >= -1); |
2464 | |
2572 | |
2465 | assert (fdchangemax >= fdchangecnt); |
2573 | assert (fdchangemax >= fdchangecnt); |
2466 | for (i = 0; i < fdchangecnt; ++i) |
2574 | for (i = 0; i < fdchangecnt; ++i) |
2467 | assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); |
2575 | assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); |
2468 | |
2576 | |
2469 | assert (anfdmax >= 0); |
2577 | assert (anfdmax >= 0); |
2470 | for (i = 0; i < anfdmax; ++i) |
2578 | for (i = j = 0; i < anfdmax; ++i) |
2471 | for (w = anfds [i].head; w; w = w->next) |
2579 | for (w = w2 = anfds [i].head; w; w = w->next) |
2472 | { |
2580 | { |
2473 | verify_watcher (EV_A_ (W)w); |
2581 | verify_watcher (EV_A_ (W)w); |
|
|
2582 | |
|
|
2583 | if (j++ & 1) |
|
|
2584 | { |
|
|
2585 | assert (("libev: io watcher list contains a loop", w != w2)); |
|
|
2586 | w2 = w2->next; |
|
|
2587 | } |
|
|
2588 | |
2474 | assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); |
2589 | assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); |
2475 | assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); |
2590 | assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); |
2476 | } |
2591 | } |
2477 | |
2592 | |
2478 | assert (timermax >= timercnt); |
2593 | assert (timermax >= timercnt); |
… | |
… | |
2531 | #if EV_MULTIPLICITY |
2646 | #if EV_MULTIPLICITY |
2532 | struct ev_loop * ecb_cold |
2647 | struct ev_loop * ecb_cold |
2533 | #else |
2648 | #else |
2534 | int |
2649 | int |
2535 | #endif |
2650 | #endif |
2536 | ev_default_loop (unsigned int flags) |
2651 | ev_default_loop (unsigned int flags) EV_THROW |
2537 | { |
2652 | { |
2538 | if (!ev_default_loop_ptr) |
2653 | if (!ev_default_loop_ptr) |
2539 | { |
2654 | { |
2540 | #if EV_MULTIPLICITY |
2655 | #if EV_MULTIPLICITY |
2541 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
2656 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
… | |
… | |
2560 | |
2675 | |
2561 | return ev_default_loop_ptr; |
2676 | return ev_default_loop_ptr; |
2562 | } |
2677 | } |
2563 | |
2678 | |
2564 | void |
2679 | void |
2565 | ev_loop_fork (EV_P) |
2680 | ev_loop_fork (EV_P) EV_THROW |
2566 | { |
2681 | { |
2567 | postfork = 1; /* must be in line with ev_default_fork */ |
2682 | postfork = 1; /* must be in line with ev_default_fork */ |
2568 | } |
2683 | } |
2569 | |
2684 | |
2570 | /*****************************************************************************/ |
2685 | /*****************************************************************************/ |
… | |
… | |
2574 | { |
2689 | { |
2575 | EV_CB_INVOKE ((W)w, revents); |
2690 | EV_CB_INVOKE ((W)w, revents); |
2576 | } |
2691 | } |
2577 | |
2692 | |
2578 | unsigned int |
2693 | unsigned int |
2579 | ev_pending_count (EV_P) |
2694 | ev_pending_count (EV_P) EV_THROW |
2580 | { |
2695 | { |
2581 | int pri; |
2696 | int pri; |
2582 | unsigned int count = 0; |
2697 | unsigned int count = 0; |
2583 | |
2698 | |
2584 | for (pri = NUMPRI; pri--; ) |
2699 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
2588 | } |
2703 | } |
2589 | |
2704 | |
2590 | void noinline |
2705 | void noinline |
2591 | ev_invoke_pending (EV_P) |
2706 | ev_invoke_pending (EV_P) |
2592 | { |
2707 | { |
2593 | int pri; |
2708 | for (pendingpri = NUMPRI; pendingpri--; ) /* pendingpri is modified during the loop */ |
2594 | |
|
|
2595 | for (pri = NUMPRI; pri--; ) |
|
|
2596 | while (pendingcnt [pri]) |
2709 | while (pendingcnt [pendingpri]) |
2597 | { |
2710 | { |
2598 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
2711 | ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; |
2599 | |
2712 | |
2600 | p->w->pending = 0; |
2713 | p->w->pending = 0; |
2601 | EV_CB_INVOKE (p->w, p->events); |
2714 | EV_CB_INVOKE (p->w, p->events); |
2602 | EV_FREQUENT_CHECK; |
2715 | EV_FREQUENT_CHECK; |
2603 | } |
2716 | } |
… | |
… | |
2843 | |
2956 | |
2844 | mn_now = ev_rt_now; |
2957 | mn_now = ev_rt_now; |
2845 | } |
2958 | } |
2846 | } |
2959 | } |
2847 | |
2960 | |
2848 | void |
2961 | int |
2849 | ev_run (EV_P_ int flags) |
2962 | ev_run (EV_P_ int flags) |
2850 | { |
2963 | { |
2851 | #if EV_FEATURE_API |
2964 | #if EV_FEATURE_API |
2852 | ++loop_depth; |
2965 | ++loop_depth; |
2853 | #endif |
2966 | #endif |
… | |
… | |
2966 | #endif |
3079 | #endif |
2967 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
3080 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2968 | backend_poll (EV_A_ waittime); |
3081 | backend_poll (EV_A_ waittime); |
2969 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
3082 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2970 | |
3083 | |
2971 | pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ |
3084 | pipe_write_wanted = 0; /* just an optimisation, no fence needed */ |
2972 | |
3085 | |
2973 | if (pipe_write_skipped) |
3086 | if (pipe_write_skipped) |
2974 | { |
3087 | { |
2975 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
3088 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
2976 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
3089 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
… | |
… | |
3010 | loop_done = EVBREAK_CANCEL; |
3123 | loop_done = EVBREAK_CANCEL; |
3011 | |
3124 | |
3012 | #if EV_FEATURE_API |
3125 | #if EV_FEATURE_API |
3013 | --loop_depth; |
3126 | --loop_depth; |
3014 | #endif |
3127 | #endif |
|
|
3128 | |
|
|
3129 | return activecnt; |
3015 | } |
3130 | } |
3016 | |
3131 | |
3017 | void |
3132 | void |
3018 | ev_break (EV_P_ int how) |
3133 | ev_break (EV_P_ int how) EV_THROW |
3019 | { |
3134 | { |
3020 | loop_done = how; |
3135 | loop_done = how; |
3021 | } |
3136 | } |
3022 | |
3137 | |
3023 | void |
3138 | void |
3024 | ev_ref (EV_P) |
3139 | ev_ref (EV_P) EV_THROW |
3025 | { |
3140 | { |
3026 | ++activecnt; |
3141 | ++activecnt; |
3027 | } |
3142 | } |
3028 | |
3143 | |
3029 | void |
3144 | void |
3030 | ev_unref (EV_P) |
3145 | ev_unref (EV_P) EV_THROW |
3031 | { |
3146 | { |
3032 | --activecnt; |
3147 | --activecnt; |
3033 | } |
3148 | } |
3034 | |
3149 | |
3035 | void |
3150 | void |
3036 | ev_now_update (EV_P) |
3151 | ev_now_update (EV_P) EV_THROW |
3037 | { |
3152 | { |
3038 | time_update (EV_A_ 1e100); |
3153 | time_update (EV_A_ 1e100); |
3039 | } |
3154 | } |
3040 | |
3155 | |
3041 | void |
3156 | void |
3042 | ev_suspend (EV_P) |
3157 | ev_suspend (EV_P) EV_THROW |
3043 | { |
3158 | { |
3044 | ev_now_update (EV_A); |
3159 | ev_now_update (EV_A); |
3045 | } |
3160 | } |
3046 | |
3161 | |
3047 | void |
3162 | void |
3048 | ev_resume (EV_P) |
3163 | ev_resume (EV_P) EV_THROW |
3049 | { |
3164 | { |
3050 | ev_tstamp mn_prev = mn_now; |
3165 | ev_tstamp mn_prev = mn_now; |
3051 | |
3166 | |
3052 | ev_now_update (EV_A); |
3167 | ev_now_update (EV_A); |
3053 | timers_reschedule (EV_A_ mn_now - mn_prev); |
3168 | timers_reschedule (EV_A_ mn_now - mn_prev); |
… | |
… | |
3092 | w->pending = 0; |
3207 | w->pending = 0; |
3093 | } |
3208 | } |
3094 | } |
3209 | } |
3095 | |
3210 | |
3096 | int |
3211 | int |
3097 | ev_clear_pending (EV_P_ void *w) |
3212 | ev_clear_pending (EV_P_ void *w) EV_THROW |
3098 | { |
3213 | { |
3099 | W w_ = (W)w; |
3214 | W w_ = (W)w; |
3100 | int pending = w_->pending; |
3215 | int pending = w_->pending; |
3101 | |
3216 | |
3102 | if (expect_true (pending)) |
3217 | if (expect_true (pending)) |
… | |
… | |
3135 | } |
3250 | } |
3136 | |
3251 | |
3137 | /*****************************************************************************/ |
3252 | /*****************************************************************************/ |
3138 | |
3253 | |
3139 | void noinline |
3254 | void noinline |
3140 | ev_io_start (EV_P_ ev_io *w) |
3255 | ev_io_start (EV_P_ ev_io *w) EV_THROW |
3141 | { |
3256 | { |
3142 | int fd = w->fd; |
3257 | int fd = w->fd; |
3143 | |
3258 | |
3144 | if (expect_false (ev_is_active (w))) |
3259 | if (expect_false (ev_is_active (w))) |
3145 | return; |
3260 | return; |
… | |
… | |
3151 | |
3266 | |
3152 | ev_start (EV_A_ (W)w, 1); |
3267 | ev_start (EV_A_ (W)w, 1); |
3153 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
3268 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
3154 | wlist_add (&anfds[fd].head, (WL)w); |
3269 | wlist_add (&anfds[fd].head, (WL)w); |
3155 | |
3270 | |
|
|
3271 | /* common bug, apparently */ |
|
|
3272 | assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w)); |
|
|
3273 | |
3156 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
3274 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
3157 | w->events &= ~EV__IOFDSET; |
3275 | w->events &= ~EV__IOFDSET; |
3158 | |
3276 | |
3159 | EV_FREQUENT_CHECK; |
3277 | EV_FREQUENT_CHECK; |
3160 | } |
3278 | } |
3161 | |
3279 | |
3162 | void noinline |
3280 | void noinline |
3163 | ev_io_stop (EV_P_ ev_io *w) |
3281 | ev_io_stop (EV_P_ ev_io *w) EV_THROW |
3164 | { |
3282 | { |
3165 | clear_pending (EV_A_ (W)w); |
3283 | clear_pending (EV_A_ (W)w); |
3166 | if (expect_false (!ev_is_active (w))) |
3284 | if (expect_false (!ev_is_active (w))) |
3167 | return; |
3285 | return; |
3168 | |
3286 | |
… | |
… | |
3177 | |
3295 | |
3178 | EV_FREQUENT_CHECK; |
3296 | EV_FREQUENT_CHECK; |
3179 | } |
3297 | } |
3180 | |
3298 | |
3181 | void noinline |
3299 | void noinline |
3182 | ev_timer_start (EV_P_ ev_timer *w) |
3300 | ev_timer_start (EV_P_ ev_timer *w) EV_THROW |
3183 | { |
3301 | { |
3184 | if (expect_false (ev_is_active (w))) |
3302 | if (expect_false (ev_is_active (w))) |
3185 | return; |
3303 | return; |
3186 | |
3304 | |
3187 | ev_at (w) += mn_now; |
3305 | ev_at (w) += mn_now; |
… | |
… | |
3201 | |
3319 | |
3202 | /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ |
3320 | /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ |
3203 | } |
3321 | } |
3204 | |
3322 | |
3205 | void noinline |
3323 | void noinline |
3206 | ev_timer_stop (EV_P_ ev_timer *w) |
3324 | ev_timer_stop (EV_P_ ev_timer *w) EV_THROW |
3207 | { |
3325 | { |
3208 | clear_pending (EV_A_ (W)w); |
3326 | clear_pending (EV_A_ (W)w); |
3209 | if (expect_false (!ev_is_active (w))) |
3327 | if (expect_false (!ev_is_active (w))) |
3210 | return; |
3328 | return; |
3211 | |
3329 | |
… | |
… | |
3231 | |
3349 | |
3232 | EV_FREQUENT_CHECK; |
3350 | EV_FREQUENT_CHECK; |
3233 | } |
3351 | } |
3234 | |
3352 | |
3235 | void noinline |
3353 | void noinline |
3236 | ev_timer_again (EV_P_ ev_timer *w) |
3354 | ev_timer_again (EV_P_ ev_timer *w) EV_THROW |
3237 | { |
3355 | { |
3238 | EV_FREQUENT_CHECK; |
3356 | EV_FREQUENT_CHECK; |
|
|
3357 | |
|
|
3358 | clear_pending (EV_A_ (W)w); |
3239 | |
3359 | |
3240 | if (ev_is_active (w)) |
3360 | if (ev_is_active (w)) |
3241 | { |
3361 | { |
3242 | if (w->repeat) |
3362 | if (w->repeat) |
3243 | { |
3363 | { |
… | |
… | |
3256 | |
3376 | |
3257 | EV_FREQUENT_CHECK; |
3377 | EV_FREQUENT_CHECK; |
3258 | } |
3378 | } |
3259 | |
3379 | |
3260 | ev_tstamp |
3380 | ev_tstamp |
3261 | ev_timer_remaining (EV_P_ ev_timer *w) |
3381 | ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW |
3262 | { |
3382 | { |
3263 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
3383 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
3264 | } |
3384 | } |
3265 | |
3385 | |
3266 | #if EV_PERIODIC_ENABLE |
3386 | #if EV_PERIODIC_ENABLE |
3267 | void noinline |
3387 | void noinline |
3268 | ev_periodic_start (EV_P_ ev_periodic *w) |
3388 | ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW |
3269 | { |
3389 | { |
3270 | if (expect_false (ev_is_active (w))) |
3390 | if (expect_false (ev_is_active (w))) |
3271 | return; |
3391 | return; |
3272 | |
3392 | |
3273 | if (w->reschedule_cb) |
3393 | if (w->reschedule_cb) |
… | |
… | |
3293 | |
3413 | |
3294 | /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ |
3414 | /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ |
3295 | } |
3415 | } |
3296 | |
3416 | |
3297 | void noinline |
3417 | void noinline |
3298 | ev_periodic_stop (EV_P_ ev_periodic *w) |
3418 | ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW |
3299 | { |
3419 | { |
3300 | clear_pending (EV_A_ (W)w); |
3420 | clear_pending (EV_A_ (W)w); |
3301 | if (expect_false (!ev_is_active (w))) |
3421 | if (expect_false (!ev_is_active (w))) |
3302 | return; |
3422 | return; |
3303 | |
3423 | |
… | |
… | |
3321 | |
3441 | |
3322 | EV_FREQUENT_CHECK; |
3442 | EV_FREQUENT_CHECK; |
3323 | } |
3443 | } |
3324 | |
3444 | |
3325 | void noinline |
3445 | void noinline |
3326 | ev_periodic_again (EV_P_ ev_periodic *w) |
3446 | ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW |
3327 | { |
3447 | { |
3328 | /* TODO: use adjustheap and recalculation */ |
3448 | /* TODO: use adjustheap and recalculation */ |
3329 | ev_periodic_stop (EV_A_ w); |
3449 | ev_periodic_stop (EV_A_ w); |
3330 | ev_periodic_start (EV_A_ w); |
3450 | ev_periodic_start (EV_A_ w); |
3331 | } |
3451 | } |
… | |
… | |
3336 | #endif |
3456 | #endif |
3337 | |
3457 | |
3338 | #if EV_SIGNAL_ENABLE |
3458 | #if EV_SIGNAL_ENABLE |
3339 | |
3459 | |
3340 | void noinline |
3460 | void noinline |
3341 | ev_signal_start (EV_P_ ev_signal *w) |
3461 | ev_signal_start (EV_P_ ev_signal *w) EV_THROW |
3342 | { |
3462 | { |
3343 | if (expect_false (ev_is_active (w))) |
3463 | if (expect_false (ev_is_active (w))) |
3344 | return; |
3464 | return; |
3345 | |
3465 | |
3346 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); |
3466 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); |
… | |
… | |
3417 | |
3537 | |
3418 | EV_FREQUENT_CHECK; |
3538 | EV_FREQUENT_CHECK; |
3419 | } |
3539 | } |
3420 | |
3540 | |
3421 | void noinline |
3541 | void noinline |
3422 | ev_signal_stop (EV_P_ ev_signal *w) |
3542 | ev_signal_stop (EV_P_ ev_signal *w) EV_THROW |
3423 | { |
3543 | { |
3424 | clear_pending (EV_A_ (W)w); |
3544 | clear_pending (EV_A_ (W)w); |
3425 | if (expect_false (!ev_is_active (w))) |
3545 | if (expect_false (!ev_is_active (w))) |
3426 | return; |
3546 | return; |
3427 | |
3547 | |
… | |
… | |
3458 | #endif |
3578 | #endif |
3459 | |
3579 | |
3460 | #if EV_CHILD_ENABLE |
3580 | #if EV_CHILD_ENABLE |
3461 | |
3581 | |
3462 | void |
3582 | void |
3463 | ev_child_start (EV_P_ ev_child *w) |
3583 | ev_child_start (EV_P_ ev_child *w) EV_THROW |
3464 | { |
3584 | { |
3465 | #if EV_MULTIPLICITY |
3585 | #if EV_MULTIPLICITY |
3466 | assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
3586 | assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
3467 | #endif |
3587 | #endif |
3468 | if (expect_false (ev_is_active (w))) |
3588 | if (expect_false (ev_is_active (w))) |
… | |
… | |
3475 | |
3595 | |
3476 | EV_FREQUENT_CHECK; |
3596 | EV_FREQUENT_CHECK; |
3477 | } |
3597 | } |
3478 | |
3598 | |
3479 | void |
3599 | void |
3480 | ev_child_stop (EV_P_ ev_child *w) |
3600 | ev_child_stop (EV_P_ ev_child *w) EV_THROW |
3481 | { |
3601 | { |
3482 | clear_pending (EV_A_ (W)w); |
3602 | clear_pending (EV_A_ (W)w); |
3483 | if (expect_false (!ev_is_active (w))) |
3603 | if (expect_false (!ev_is_active (w))) |
3484 | return; |
3604 | return; |
3485 | |
3605 | |
… | |
… | |
3652 | } |
3772 | } |
3653 | |
3773 | |
3654 | inline_size int |
3774 | inline_size int |
3655 | infy_newfd (void) |
3775 | infy_newfd (void) |
3656 | { |
3776 | { |
3657 | #if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) |
3777 | #if defined IN_CLOEXEC && defined IN_NONBLOCK |
3658 | int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); |
3778 | int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); |
3659 | if (fd >= 0) |
3779 | if (fd >= 0) |
3660 | return fd; |
3780 | return fd; |
3661 | #endif |
3781 | #endif |
3662 | return inotify_init (); |
3782 | return inotify_init (); |
… | |
… | |
3737 | #else |
3857 | #else |
3738 | # define EV_LSTAT(p,b) lstat (p, b) |
3858 | # define EV_LSTAT(p,b) lstat (p, b) |
3739 | #endif |
3859 | #endif |
3740 | |
3860 | |
3741 | void |
3861 | void |
3742 | ev_stat_stat (EV_P_ ev_stat *w) |
3862 | ev_stat_stat (EV_P_ ev_stat *w) EV_THROW |
3743 | { |
3863 | { |
3744 | if (lstat (w->path, &w->attr) < 0) |
3864 | if (lstat (w->path, &w->attr) < 0) |
3745 | w->attr.st_nlink = 0; |
3865 | w->attr.st_nlink = 0; |
3746 | else if (!w->attr.st_nlink) |
3866 | else if (!w->attr.st_nlink) |
3747 | w->attr.st_nlink = 1; |
3867 | w->attr.st_nlink = 1; |
… | |
… | |
3786 | ev_feed_event (EV_A_ w, EV_STAT); |
3906 | ev_feed_event (EV_A_ w, EV_STAT); |
3787 | } |
3907 | } |
3788 | } |
3908 | } |
3789 | |
3909 | |
3790 | void |
3910 | void |
3791 | ev_stat_start (EV_P_ ev_stat *w) |
3911 | ev_stat_start (EV_P_ ev_stat *w) EV_THROW |
3792 | { |
3912 | { |
3793 | if (expect_false (ev_is_active (w))) |
3913 | if (expect_false (ev_is_active (w))) |
3794 | return; |
3914 | return; |
3795 | |
3915 | |
3796 | ev_stat_stat (EV_A_ w); |
3916 | ev_stat_stat (EV_A_ w); |
… | |
… | |
3817 | |
3937 | |
3818 | EV_FREQUENT_CHECK; |
3938 | EV_FREQUENT_CHECK; |
3819 | } |
3939 | } |
3820 | |
3940 | |
3821 | void |
3941 | void |
3822 | ev_stat_stop (EV_P_ ev_stat *w) |
3942 | ev_stat_stop (EV_P_ ev_stat *w) EV_THROW |
3823 | { |
3943 | { |
3824 | clear_pending (EV_A_ (W)w); |
3944 | clear_pending (EV_A_ (W)w); |
3825 | if (expect_false (!ev_is_active (w))) |
3945 | if (expect_false (!ev_is_active (w))) |
3826 | return; |
3946 | return; |
3827 | |
3947 | |
… | |
… | |
3843 | } |
3963 | } |
3844 | #endif |
3964 | #endif |
3845 | |
3965 | |
3846 | #if EV_IDLE_ENABLE |
3966 | #if EV_IDLE_ENABLE |
3847 | void |
3967 | void |
3848 | ev_idle_start (EV_P_ ev_idle *w) |
3968 | ev_idle_start (EV_P_ ev_idle *w) EV_THROW |
3849 | { |
3969 | { |
3850 | if (expect_false (ev_is_active (w))) |
3970 | if (expect_false (ev_is_active (w))) |
3851 | return; |
3971 | return; |
3852 | |
3972 | |
3853 | pri_adjust (EV_A_ (W)w); |
3973 | pri_adjust (EV_A_ (W)w); |
… | |
… | |
3866 | |
3986 | |
3867 | EV_FREQUENT_CHECK; |
3987 | EV_FREQUENT_CHECK; |
3868 | } |
3988 | } |
3869 | |
3989 | |
3870 | void |
3990 | void |
3871 | ev_idle_stop (EV_P_ ev_idle *w) |
3991 | ev_idle_stop (EV_P_ ev_idle *w) EV_THROW |
3872 | { |
3992 | { |
3873 | clear_pending (EV_A_ (W)w); |
3993 | clear_pending (EV_A_ (W)w); |
3874 | if (expect_false (!ev_is_active (w))) |
3994 | if (expect_false (!ev_is_active (w))) |
3875 | return; |
3995 | return; |
3876 | |
3996 | |
… | |
… | |
3890 | } |
4010 | } |
3891 | #endif |
4011 | #endif |
3892 | |
4012 | |
3893 | #if EV_PREPARE_ENABLE |
4013 | #if EV_PREPARE_ENABLE |
3894 | void |
4014 | void |
3895 | ev_prepare_start (EV_P_ ev_prepare *w) |
4015 | ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW |
3896 | { |
4016 | { |
3897 | if (expect_false (ev_is_active (w))) |
4017 | if (expect_false (ev_is_active (w))) |
3898 | return; |
4018 | return; |
3899 | |
4019 | |
3900 | EV_FREQUENT_CHECK; |
4020 | EV_FREQUENT_CHECK; |
… | |
… | |
3905 | |
4025 | |
3906 | EV_FREQUENT_CHECK; |
4026 | EV_FREQUENT_CHECK; |
3907 | } |
4027 | } |
3908 | |
4028 | |
3909 | void |
4029 | void |
3910 | ev_prepare_stop (EV_P_ ev_prepare *w) |
4030 | ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW |
3911 | { |
4031 | { |
3912 | clear_pending (EV_A_ (W)w); |
4032 | clear_pending (EV_A_ (W)w); |
3913 | if (expect_false (!ev_is_active (w))) |
4033 | if (expect_false (!ev_is_active (w))) |
3914 | return; |
4034 | return; |
3915 | |
4035 | |
… | |
… | |
3928 | } |
4048 | } |
3929 | #endif |
4049 | #endif |
3930 | |
4050 | |
3931 | #if EV_CHECK_ENABLE |
4051 | #if EV_CHECK_ENABLE |
3932 | void |
4052 | void |
3933 | ev_check_start (EV_P_ ev_check *w) |
4053 | ev_check_start (EV_P_ ev_check *w) EV_THROW |
3934 | { |
4054 | { |
3935 | if (expect_false (ev_is_active (w))) |
4055 | if (expect_false (ev_is_active (w))) |
3936 | return; |
4056 | return; |
3937 | |
4057 | |
3938 | EV_FREQUENT_CHECK; |
4058 | EV_FREQUENT_CHECK; |
… | |
… | |
3943 | |
4063 | |
3944 | EV_FREQUENT_CHECK; |
4064 | EV_FREQUENT_CHECK; |
3945 | } |
4065 | } |
3946 | |
4066 | |
3947 | void |
4067 | void |
3948 | ev_check_stop (EV_P_ ev_check *w) |
4068 | ev_check_stop (EV_P_ ev_check *w) EV_THROW |
3949 | { |
4069 | { |
3950 | clear_pending (EV_A_ (W)w); |
4070 | clear_pending (EV_A_ (W)w); |
3951 | if (expect_false (!ev_is_active (w))) |
4071 | if (expect_false (!ev_is_active (w))) |
3952 | return; |
4072 | return; |
3953 | |
4073 | |
… | |
… | |
3966 | } |
4086 | } |
3967 | #endif |
4087 | #endif |
3968 | |
4088 | |
3969 | #if EV_EMBED_ENABLE |
4089 | #if EV_EMBED_ENABLE |
3970 | void noinline |
4090 | void noinline |
3971 | ev_embed_sweep (EV_P_ ev_embed *w) |
4091 | ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW |
3972 | { |
4092 | { |
3973 | ev_run (w->other, EVRUN_NOWAIT); |
4093 | ev_run (w->other, EVRUN_NOWAIT); |
3974 | } |
4094 | } |
3975 | |
4095 | |
3976 | static void |
4096 | static void |
… | |
… | |
4024 | ev_idle_stop (EV_A_ idle); |
4144 | ev_idle_stop (EV_A_ idle); |
4025 | } |
4145 | } |
4026 | #endif |
4146 | #endif |
4027 | |
4147 | |
4028 | void |
4148 | void |
4029 | ev_embed_start (EV_P_ ev_embed *w) |
4149 | ev_embed_start (EV_P_ ev_embed *w) EV_THROW |
4030 | { |
4150 | { |
4031 | if (expect_false (ev_is_active (w))) |
4151 | if (expect_false (ev_is_active (w))) |
4032 | return; |
4152 | return; |
4033 | |
4153 | |
4034 | { |
4154 | { |
… | |
… | |
4055 | |
4175 | |
4056 | EV_FREQUENT_CHECK; |
4176 | EV_FREQUENT_CHECK; |
4057 | } |
4177 | } |
4058 | |
4178 | |
4059 | void |
4179 | void |
4060 | ev_embed_stop (EV_P_ ev_embed *w) |
4180 | ev_embed_stop (EV_P_ ev_embed *w) EV_THROW |
4061 | { |
4181 | { |
4062 | clear_pending (EV_A_ (W)w); |
4182 | clear_pending (EV_A_ (W)w); |
4063 | if (expect_false (!ev_is_active (w))) |
4183 | if (expect_false (!ev_is_active (w))) |
4064 | return; |
4184 | return; |
4065 | |
4185 | |
… | |
… | |
4075 | } |
4195 | } |
4076 | #endif |
4196 | #endif |
4077 | |
4197 | |
4078 | #if EV_FORK_ENABLE |
4198 | #if EV_FORK_ENABLE |
4079 | void |
4199 | void |
4080 | ev_fork_start (EV_P_ ev_fork *w) |
4200 | ev_fork_start (EV_P_ ev_fork *w) EV_THROW |
4081 | { |
4201 | { |
4082 | if (expect_false (ev_is_active (w))) |
4202 | if (expect_false (ev_is_active (w))) |
4083 | return; |
4203 | return; |
4084 | |
4204 | |
4085 | EV_FREQUENT_CHECK; |
4205 | EV_FREQUENT_CHECK; |
… | |
… | |
4090 | |
4210 | |
4091 | EV_FREQUENT_CHECK; |
4211 | EV_FREQUENT_CHECK; |
4092 | } |
4212 | } |
4093 | |
4213 | |
4094 | void |
4214 | void |
4095 | ev_fork_stop (EV_P_ ev_fork *w) |
4215 | ev_fork_stop (EV_P_ ev_fork *w) EV_THROW |
4096 | { |
4216 | { |
4097 | clear_pending (EV_A_ (W)w); |
4217 | clear_pending (EV_A_ (W)w); |
4098 | if (expect_false (!ev_is_active (w))) |
4218 | if (expect_false (!ev_is_active (w))) |
4099 | return; |
4219 | return; |
4100 | |
4220 | |
… | |
… | |
4113 | } |
4233 | } |
4114 | #endif |
4234 | #endif |
4115 | |
4235 | |
4116 | #if EV_CLEANUP_ENABLE |
4236 | #if EV_CLEANUP_ENABLE |
4117 | void |
4237 | void |
4118 | ev_cleanup_start (EV_P_ ev_cleanup *w) |
4238 | ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW |
4119 | { |
4239 | { |
4120 | if (expect_false (ev_is_active (w))) |
4240 | if (expect_false (ev_is_active (w))) |
4121 | return; |
4241 | return; |
4122 | |
4242 | |
4123 | EV_FREQUENT_CHECK; |
4243 | EV_FREQUENT_CHECK; |
… | |
… | |
4130 | ev_unref (EV_A); |
4250 | ev_unref (EV_A); |
4131 | EV_FREQUENT_CHECK; |
4251 | EV_FREQUENT_CHECK; |
4132 | } |
4252 | } |
4133 | |
4253 | |
4134 | void |
4254 | void |
4135 | ev_cleanup_stop (EV_P_ ev_cleanup *w) |
4255 | ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW |
4136 | { |
4256 | { |
4137 | clear_pending (EV_A_ (W)w); |
4257 | clear_pending (EV_A_ (W)w); |
4138 | if (expect_false (!ev_is_active (w))) |
4258 | if (expect_false (!ev_is_active (w))) |
4139 | return; |
4259 | return; |
4140 | |
4260 | |
… | |
… | |
4154 | } |
4274 | } |
4155 | #endif |
4275 | #endif |
4156 | |
4276 | |
4157 | #if EV_ASYNC_ENABLE |
4277 | #if EV_ASYNC_ENABLE |
4158 | void |
4278 | void |
4159 | ev_async_start (EV_P_ ev_async *w) |
4279 | ev_async_start (EV_P_ ev_async *w) EV_THROW |
4160 | { |
4280 | { |
4161 | if (expect_false (ev_is_active (w))) |
4281 | if (expect_false (ev_is_active (w))) |
4162 | return; |
4282 | return; |
4163 | |
4283 | |
4164 | w->sent = 0; |
4284 | w->sent = 0; |
… | |
… | |
4173 | |
4293 | |
4174 | EV_FREQUENT_CHECK; |
4294 | EV_FREQUENT_CHECK; |
4175 | } |
4295 | } |
4176 | |
4296 | |
4177 | void |
4297 | void |
4178 | ev_async_stop (EV_P_ ev_async *w) |
4298 | ev_async_stop (EV_P_ ev_async *w) EV_THROW |
4179 | { |
4299 | { |
4180 | clear_pending (EV_A_ (W)w); |
4300 | clear_pending (EV_A_ (W)w); |
4181 | if (expect_false (!ev_is_active (w))) |
4301 | if (expect_false (!ev_is_active (w))) |
4182 | return; |
4302 | return; |
4183 | |
4303 | |
… | |
… | |
4194 | |
4314 | |
4195 | EV_FREQUENT_CHECK; |
4315 | EV_FREQUENT_CHECK; |
4196 | } |
4316 | } |
4197 | |
4317 | |
4198 | void |
4318 | void |
4199 | ev_async_send (EV_P_ ev_async *w) |
4319 | ev_async_send (EV_P_ ev_async *w) EV_THROW |
4200 | { |
4320 | { |
4201 | w->sent = 1; |
4321 | w->sent = 1; |
4202 | evpipe_write (EV_A_ &async_pending); |
4322 | evpipe_write (EV_A_ &async_pending); |
4203 | } |
4323 | } |
4204 | #endif |
4324 | #endif |
… | |
… | |
4241 | |
4361 | |
4242 | once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); |
4362 | once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); |
4243 | } |
4363 | } |
4244 | |
4364 | |
4245 | void |
4365 | void |
4246 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
4366 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW |
4247 | { |
4367 | { |
4248 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
4368 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
4249 | |
4369 | |
4250 | if (expect_false (!once)) |
4370 | if (expect_false (!once)) |
4251 | { |
4371 | { |
… | |
… | |
4273 | |
4393 | |
4274 | /*****************************************************************************/ |
4394 | /*****************************************************************************/ |
4275 | |
4395 | |
4276 | #if EV_WALK_ENABLE |
4396 | #if EV_WALK_ENABLE |
4277 | void ecb_cold |
4397 | void ecb_cold |
4278 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
4398 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW |
4279 | { |
4399 | { |
4280 | int i, j; |
4400 | int i, j; |
4281 | ev_watcher_list *wl, *wn; |
4401 | ev_watcher_list *wl, *wn; |
4282 | |
4402 | |
4283 | if (types & (EV_IO | EV_EMBED)) |
4403 | if (types & (EV_IO | EV_EMBED)) |
… | |
… | |
4389 | |
4509 | |
4390 | #if EV_MULTIPLICITY |
4510 | #if EV_MULTIPLICITY |
4391 | #include "ev_wrap.h" |
4511 | #include "ev_wrap.h" |
4392 | #endif |
4512 | #endif |
4393 | |
4513 | |
4394 | EV_CPP(}) |
|
|
4395 | |
|
|