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 | #define ECB_MEMORY_FENCE_ACQUIRE do { } while (0) |
|
|
544 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) |
|
|
545 | #endif |
557 | #endif |
546 | |
558 | |
547 | #ifndef ECB_MEMORY_FENCE |
559 | #ifndef ECB_MEMORY_FENCE |
548 | #if ECB_GCC_VERSION(2,5) |
560 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
549 | #if __x86 |
561 | #if __i386 || __i386__ |
550 | #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") |
551 | #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 */ |
552 | #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 */ |
553 | #elif __amd64 |
565 | #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ |
554 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
566 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
555 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
567 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
556 | #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 */ |
|
|
569 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
|
|
570 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
|
|
571 | #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ |
|
|
572 | || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ |
|
|
573 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
|
|
574 | #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ |
|
|
575 | || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__ |
|
|
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") |
557 | #endif |
587 | #endif |
558 | #endif |
588 | #endif |
559 | #endif |
589 | #endif |
560 | |
590 | |
561 | #ifndef ECB_MEMORY_FENCE |
591 | #ifndef ECB_MEMORY_FENCE |
562 | #if ECB_GCC_VERSION(4,4) |
592 | #if ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
563 | #define ECB_MEMORY_FENCE __sync_synchronize () |
593 | #define ECB_MEMORY_FENCE __sync_synchronize () |
564 | #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); }) */ |
565 | #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 ); }) */ |
566 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
596 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
567 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
597 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
568 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
598 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
569 | #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 */ |
570 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
600 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
571 | #elif defined(_WIN32) |
601 | #elif defined _WIN32 |
572 | #include <WinNT.h> |
602 | #include <WinNT.h> |
573 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
603 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
574 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
604 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
575 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
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 () |
576 | #endif |
611 | #endif |
577 | #endif |
612 | #endif |
578 | |
613 | |
579 | #ifndef ECB_MEMORY_FENCE |
614 | #ifndef ECB_MEMORY_FENCE |
|
|
615 | #if !ECB_AVOID_PTHREADS |
580 | /* |
616 | /* |
581 | * if you get undefined symbol references to pthread_mutex_lock, |
617 | * if you get undefined symbol references to pthread_mutex_lock, |
582 | * or failure to find pthread.h, then you should implement |
618 | * or failure to find pthread.h, then you should implement |
583 | * the ECB_MEMORY_FENCE operations for your cpu/compiler |
619 | * the ECB_MEMORY_FENCE operations for your cpu/compiler |
584 | * OR provide pthread.h and link against the posix thread library |
620 | * OR provide pthread.h and link against the posix thread library |
585 | * of your system. |
621 | * of your system. |
586 | */ |
622 | */ |
587 | #include <pthread.h> |
623 | #include <pthread.h> |
588 | #define ECB_NEEDS_PTHREADS 1 |
624 | #define ECB_NEEDS_PTHREADS 1 |
589 | #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 |
625 | #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 |
590 | |
626 | |
591 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
627 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
592 | #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) |
|
|
629 | #endif |
|
|
630 | #endif |
|
|
631 | |
|
|
632 | #if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE |
593 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
633 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
634 | #endif |
|
|
635 | |
|
|
636 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
594 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
637 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
595 | #endif |
638 | #endif |
596 | |
639 | |
597 | /*****************************************************************************/ |
640 | /*****************************************************************************/ |
598 | |
641 | |
… | |
… | |
747 | |
790 | |
748 | return r + ecb_ld32 (x); |
791 | return r + ecb_ld32 (x); |
749 | } |
792 | } |
750 | #endif |
793 | #endif |
751 | |
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 | |
752 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
825 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
753 | /* so for this version we are lazy */ |
826 | /* so for this version we are lazy */ |
754 | ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; |
827 | ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; |
755 | ecb_function_ int |
828 | ecb_function_ int |
756 | ecb_popcount64 (uint64_t x) |
829 | ecb_popcount64 (uint64_t x) |
… | |
… | |
805 | |
878 | |
806 | #if ECB_GCC_VERSION(4,5) |
879 | #if ECB_GCC_VERSION(4,5) |
807 | #define ecb_unreachable() __builtin_unreachable () |
880 | #define ecb_unreachable() __builtin_unreachable () |
808 | #else |
881 | #else |
809 | /* 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 :/ */ |
810 | ecb_function_ void ecb_unreachable (void) ecb_noreturn; |
883 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
811 | ecb_function_ void ecb_unreachable (void) { } |
884 | ecb_inline void ecb_unreachable (void) { } |
812 | #endif |
885 | #endif |
813 | |
886 | |
814 | /* try to tell the compiler that some condition is definitely true */ |
887 | /* try to tell the compiler that some condition is definitely true */ |
815 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
888 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
816 | |
889 | |
817 | ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; |
890 | ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; |
818 | ecb_function_ unsigned char |
891 | ecb_inline unsigned char |
819 | ecb_byteorder_helper (void) |
892 | ecb_byteorder_helper (void) |
820 | { |
893 | { |
821 | const uint32_t u = 0x11223344; |
894 | const uint32_t u = 0x11223344; |
822 | return *(unsigned char *)&u; |
895 | return *(unsigned char *)&u; |
823 | } |
896 | } |
824 | |
897 | |
825 | ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; |
898 | ecb_inline ecb_bool ecb_big_endian (void) ecb_const; |
826 | 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; } |
827 | ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; |
900 | ecb_inline ecb_bool ecb_little_endian (void) ecb_const; |
828 | 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; } |
829 | |
902 | |
830 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
903 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
831 | #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)) |
832 | #else |
905 | #else |
833 | #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)) |
834 | #endif |
923 | #endif |
835 | |
924 | |
836 | #if ecb_cplusplus_does_not_suck |
925 | #if ecb_cplusplus_does_not_suck |
837 | /* 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) */ |
838 | template<typename T, int N> |
927 | template<typename T, int N> |
… | |
… | |
845 | #endif |
934 | #endif |
846 | |
935 | |
847 | #endif |
936 | #endif |
848 | |
937 | |
849 | /* ECB.H END */ |
938 | /* ECB.H END */ |
|
|
939 | |
|
|
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 | |
|
|
951 | #ifndef ECB_MEMORY_FENCE |
|
|
952 | # define ECB_MEMORY_FENCE do { } while (0) |
|
|
953 | # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
954 | # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
955 | #endif |
850 | |
956 | |
851 | #define expect_false(cond) ecb_expect_false (cond) |
957 | #define expect_false(cond) ecb_expect_false (cond) |
852 | #define expect_true(cond) ecb_expect_true (cond) |
958 | #define expect_true(cond) ecb_expect_true (cond) |
853 | #define noinline ecb_noinline |
959 | #define noinline ecb_noinline |
854 | |
960 | |
… | |
… | |
1000 | { |
1106 | { |
1001 | write (STDERR_FILENO, msg, strlen (msg)); |
1107 | write (STDERR_FILENO, msg, strlen (msg)); |
1002 | } |
1108 | } |
1003 | #endif |
1109 | #endif |
1004 | |
1110 | |
1005 | static void (*syserr_cb)(const char *msg); |
1111 | static void (*syserr_cb)(const char *msg) EV_THROW; |
1006 | |
1112 | |
1007 | void ecb_cold |
1113 | void ecb_cold |
1008 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
1114 | ev_set_syserr_cb (void (*cb)(const char *msg)) EV_THROW |
1009 | { |
1115 | { |
1010 | syserr_cb = cb; |
1116 | syserr_cb = cb; |
1011 | } |
1117 | } |
1012 | |
1118 | |
1013 | static void noinline ecb_cold |
1119 | static void noinline ecb_cold |
… | |
… | |
1049 | free (ptr); |
1155 | free (ptr); |
1050 | return 0; |
1156 | return 0; |
1051 | #endif |
1157 | #endif |
1052 | } |
1158 | } |
1053 | |
1159 | |
1054 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
1160 | static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; |
1055 | |
1161 | |
1056 | void ecb_cold |
1162 | void ecb_cold |
1057 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
1163 | ev_set_allocator (void *(*cb)(void *ptr, long size)) EV_THROW |
1058 | { |
1164 | { |
1059 | alloc = cb; |
1165 | alloc = cb; |
1060 | } |
1166 | } |
1061 | |
1167 | |
1062 | inline_speed void * |
1168 | inline_speed void * |
… | |
… | |
1150 | #undef VAR |
1256 | #undef VAR |
1151 | }; |
1257 | }; |
1152 | #include "ev_wrap.h" |
1258 | #include "ev_wrap.h" |
1153 | |
1259 | |
1154 | static struct ev_loop default_loop_struct; |
1260 | static struct ev_loop default_loop_struct; |
1155 | 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 */ |
1156 | |
1262 | |
1157 | #else |
1263 | #else |
1158 | |
1264 | |
1159 | 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 */ |
1160 | #define VAR(name,decl) static decl; |
1266 | #define VAR(name,decl) static decl; |
1161 | #include "ev_vars.h" |
1267 | #include "ev_vars.h" |
1162 | #undef VAR |
1268 | #undef VAR |
1163 | |
1269 | |
1164 | static int ev_default_loop_ptr; |
1270 | static int ev_default_loop_ptr; |
… | |
… | |
1179 | |
1285 | |
1180 | /*****************************************************************************/ |
1286 | /*****************************************************************************/ |
1181 | |
1287 | |
1182 | #ifndef EV_HAVE_EV_TIME |
1288 | #ifndef EV_HAVE_EV_TIME |
1183 | ev_tstamp |
1289 | ev_tstamp |
1184 | ev_time (void) |
1290 | ev_time (void) EV_THROW |
1185 | { |
1291 | { |
1186 | #if EV_USE_REALTIME |
1292 | #if EV_USE_REALTIME |
1187 | if (expect_true (have_realtime)) |
1293 | if (expect_true (have_realtime)) |
1188 | { |
1294 | { |
1189 | struct timespec ts; |
1295 | struct timespec ts; |
… | |
… | |
1213 | return ev_time (); |
1319 | return ev_time (); |
1214 | } |
1320 | } |
1215 | |
1321 | |
1216 | #if EV_MULTIPLICITY |
1322 | #if EV_MULTIPLICITY |
1217 | ev_tstamp |
1323 | ev_tstamp |
1218 | ev_now (EV_P) |
1324 | ev_now (EV_P) EV_THROW |
1219 | { |
1325 | { |
1220 | return ev_rt_now; |
1326 | return ev_rt_now; |
1221 | } |
1327 | } |
1222 | #endif |
1328 | #endif |
1223 | |
1329 | |
1224 | void |
1330 | void |
1225 | ev_sleep (ev_tstamp delay) |
1331 | ev_sleep (ev_tstamp delay) EV_THROW |
1226 | { |
1332 | { |
1227 | if (delay > 0.) |
1333 | if (delay > 0.) |
1228 | { |
1334 | { |
1229 | #if EV_USE_NANOSLEEP |
1335 | #if EV_USE_NANOSLEEP |
1230 | struct timespec ts; |
1336 | struct timespec ts; |
1231 | |
1337 | |
1232 | EV_TS_SET (ts, delay); |
1338 | EV_TS_SET (ts, delay); |
1233 | nanosleep (&ts, 0); |
1339 | nanosleep (&ts, 0); |
1234 | #elif defined(_WIN32) |
1340 | #elif defined _WIN32 |
1235 | Sleep ((unsigned long)(delay * 1e3)); |
1341 | Sleep ((unsigned long)(delay * 1e3)); |
1236 | #else |
1342 | #else |
1237 | struct timeval tv; |
1343 | struct timeval tv; |
1238 | |
1344 | |
1239 | /* 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 */ |
… | |
… | |
1258 | |
1364 | |
1259 | do |
1365 | do |
1260 | ncur <<= 1; |
1366 | ncur <<= 1; |
1261 | while (cnt > ncur); |
1367 | while (cnt > ncur); |
1262 | |
1368 | |
1263 | /* 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 */ |
1264 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
1370 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
1265 | { |
1371 | { |
1266 | ncur *= elem; |
1372 | ncur *= elem; |
1267 | 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); |
1268 | ncur = ncur - sizeof (void *) * 4; |
1374 | ncur = ncur - sizeof (void *) * 4; |
… | |
… | |
1311 | pendingcb (EV_P_ ev_prepare *w, int revents) |
1417 | pendingcb (EV_P_ ev_prepare *w, int revents) |
1312 | { |
1418 | { |
1313 | } |
1419 | } |
1314 | |
1420 | |
1315 | void noinline |
1421 | void noinline |
1316 | ev_feed_event (EV_P_ void *w, int revents) |
1422 | ev_feed_event (EV_P_ void *w, int revents) EV_THROW |
1317 | { |
1423 | { |
1318 | W w_ = (W)w; |
1424 | W w_ = (W)w; |
1319 | int pri = ABSPRI (w_); |
1425 | int pri = ABSPRI (w_); |
1320 | |
1426 | |
1321 | if (expect_false (w_->pending)) |
1427 | if (expect_false (w_->pending)) |
… | |
… | |
1325 | w_->pending = ++pendingcnt [pri]; |
1431 | w_->pending = ++pendingcnt [pri]; |
1326 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
1432 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
1327 | pendings [pri][w_->pending - 1].w = w_; |
1433 | pendings [pri][w_->pending - 1].w = w_; |
1328 | pendings [pri][w_->pending - 1].events = revents; |
1434 | pendings [pri][w_->pending - 1].events = revents; |
1329 | } |
1435 | } |
|
|
1436 | |
|
|
1437 | pendingpri = NUMPRI - 1; |
1330 | } |
1438 | } |
1331 | |
1439 | |
1332 | inline_speed void |
1440 | inline_speed void |
1333 | feed_reverse (EV_P_ W w) |
1441 | feed_reverse (EV_P_ W w) |
1334 | { |
1442 | { |
… | |
… | |
1380 | if (expect_true (!anfd->reify)) |
1488 | if (expect_true (!anfd->reify)) |
1381 | fd_event_nocheck (EV_A_ fd, revents); |
1489 | fd_event_nocheck (EV_A_ fd, revents); |
1382 | } |
1490 | } |
1383 | |
1491 | |
1384 | void |
1492 | void |
1385 | ev_feed_fd_event (EV_P_ int fd, int revents) |
1493 | ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW |
1386 | { |
1494 | { |
1387 | if (fd >= 0 && fd < anfdmax) |
1495 | if (fd >= 0 && fd < anfdmax) |
1388 | fd_event_nocheck (EV_A_ fd, revents); |
1496 | fd_event_nocheck (EV_A_ fd, revents); |
1389 | } |
1497 | } |
1390 | |
1498 | |
… | |
… | |
1739 | } |
1847 | } |
1740 | |
1848 | |
1741 | inline_speed void |
1849 | inline_speed void |
1742 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1850 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1743 | { |
1851 | { |
|
|
1852 | ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */ |
|
|
1853 | |
1744 | if (expect_true (*flag)) |
1854 | if (expect_true (*flag)) |
1745 | return; |
1855 | return; |
1746 | |
1856 | |
1747 | *flag = 1; |
1857 | *flag = 1; |
1748 | |
1858 | |
… | |
… | |
1754 | |
1864 | |
1755 | if (pipe_write_wanted) |
1865 | if (pipe_write_wanted) |
1756 | { |
1866 | { |
1757 | int old_errno; |
1867 | int old_errno; |
1758 | |
1868 | |
1759 | pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ |
1869 | pipe_write_skipped = 0; /* just an optimisation, no fence needed */ |
1760 | |
1870 | |
1761 | old_errno = errno; /* save errno because write will clobber it */ |
1871 | old_errno = errno; /* save errno because write will clobber it */ |
1762 | |
1872 | |
1763 | #if EV_USE_EVENTFD |
1873 | #if EV_USE_EVENTFD |
1764 | if (evfd >= 0) |
1874 | if (evfd >= 0) |
… | |
… | |
1767 | write (evfd, &counter, sizeof (uint64_t)); |
1877 | write (evfd, &counter, sizeof (uint64_t)); |
1768 | } |
1878 | } |
1769 | else |
1879 | else |
1770 | #endif |
1880 | #endif |
1771 | { |
1881 | { |
1772 | /* win32 people keep sending patches that change this write() to send() */ |
1882 | #ifdef _WIN32 |
1773 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
1883 | WSABUF buf; |
1774 | /* so when you think this write should be a send instead, please find out */ |
1884 | DWORD sent; |
1775 | /* where your send() is from - it's definitely not the microsoft send, and */ |
1885 | buf.buf = &buf; |
1776 | /* 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 |
1777 | write (evpipe [1], &(evpipe [1]), 1); |
1889 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
1890 | #endif |
1778 | } |
1891 | } |
1779 | |
1892 | |
1780 | errno = old_errno; |
1893 | errno = old_errno; |
1781 | } |
1894 | } |
1782 | } |
1895 | } |
… | |
… | |
1797 | read (evfd, &counter, sizeof (uint64_t)); |
1910 | read (evfd, &counter, sizeof (uint64_t)); |
1798 | } |
1911 | } |
1799 | else |
1912 | else |
1800 | #endif |
1913 | #endif |
1801 | { |
1914 | { |
1802 | char dummy; |
1915 | char dummy[4]; |
1803 | /* 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 |
1804 | read (evpipe [0], &dummy, 1); |
1923 | read (evpipe [0], &dummy, sizeof (dummy)); |
|
|
1924 | #endif |
1805 | } |
1925 | } |
1806 | } |
1926 | } |
1807 | |
1927 | |
1808 | pipe_write_skipped = 0; |
1928 | pipe_write_skipped = 0; |
|
|
1929 | |
|
|
1930 | ECB_MEMORY_FENCE; /* push out skipped, acquire flags */ |
1809 | |
1931 | |
1810 | #if EV_SIGNAL_ENABLE |
1932 | #if EV_SIGNAL_ENABLE |
1811 | if (sig_pending) |
1933 | if (sig_pending) |
1812 | { |
1934 | { |
1813 | sig_pending = 0; |
1935 | sig_pending = 0; |
|
|
1936 | |
|
|
1937 | ECB_MEMORY_FENCE_RELEASE; |
1814 | |
1938 | |
1815 | for (i = EV_NSIG - 1; i--; ) |
1939 | for (i = EV_NSIG - 1; i--; ) |
1816 | if (expect_false (signals [i].pending)) |
1940 | if (expect_false (signals [i].pending)) |
1817 | ev_feed_signal_event (EV_A_ i + 1); |
1941 | ev_feed_signal_event (EV_A_ i + 1); |
1818 | } |
1942 | } |
… | |
… | |
1820 | |
1944 | |
1821 | #if EV_ASYNC_ENABLE |
1945 | #if EV_ASYNC_ENABLE |
1822 | if (async_pending) |
1946 | if (async_pending) |
1823 | { |
1947 | { |
1824 | async_pending = 0; |
1948 | async_pending = 0; |
|
|
1949 | |
|
|
1950 | ECB_MEMORY_FENCE_RELEASE; |
1825 | |
1951 | |
1826 | for (i = asynccnt; i--; ) |
1952 | for (i = asynccnt; i--; ) |
1827 | if (asyncs [i]->sent) |
1953 | if (asyncs [i]->sent) |
1828 | { |
1954 | { |
1829 | asyncs [i]->sent = 0; |
1955 | asyncs [i]->sent = 0; |
… | |
… | |
1834 | } |
1960 | } |
1835 | |
1961 | |
1836 | /*****************************************************************************/ |
1962 | /*****************************************************************************/ |
1837 | |
1963 | |
1838 | void |
1964 | void |
1839 | ev_feed_signal (int signum) |
1965 | ev_feed_signal (int signum) EV_THROW |
1840 | { |
1966 | { |
1841 | #if EV_MULTIPLICITY |
1967 | #if EV_MULTIPLICITY |
1842 | EV_P = signals [signum - 1].loop; |
1968 | EV_P = signals [signum - 1].loop; |
1843 | |
1969 | |
1844 | if (!EV_A) |
1970 | if (!EV_A) |
… | |
… | |
1861 | |
1987 | |
1862 | ev_feed_signal (signum); |
1988 | ev_feed_signal (signum); |
1863 | } |
1989 | } |
1864 | |
1990 | |
1865 | void noinline |
1991 | void noinline |
1866 | ev_feed_signal_event (EV_P_ int signum) |
1992 | ev_feed_signal_event (EV_P_ int signum) EV_THROW |
1867 | { |
1993 | { |
1868 | WL w; |
1994 | WL w; |
1869 | |
1995 | |
1870 | if (expect_false (signum <= 0 || signum > EV_NSIG)) |
1996 | if (expect_false (signum <= 0 || signum > EV_NSIG)) |
1871 | return; |
1997 | return; |
… | |
… | |
1987 | #if EV_USE_SELECT |
2113 | #if EV_USE_SELECT |
1988 | # include "ev_select.c" |
2114 | # include "ev_select.c" |
1989 | #endif |
2115 | #endif |
1990 | |
2116 | |
1991 | int ecb_cold |
2117 | int ecb_cold |
1992 | ev_version_major (void) |
2118 | ev_version_major (void) EV_THROW |
1993 | { |
2119 | { |
1994 | return EV_VERSION_MAJOR; |
2120 | return EV_VERSION_MAJOR; |
1995 | } |
2121 | } |
1996 | |
2122 | |
1997 | int ecb_cold |
2123 | int ecb_cold |
1998 | ev_version_minor (void) |
2124 | ev_version_minor (void) EV_THROW |
1999 | { |
2125 | { |
2000 | return EV_VERSION_MINOR; |
2126 | return EV_VERSION_MINOR; |
2001 | } |
2127 | } |
2002 | |
2128 | |
2003 | /* 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 */ |
… | |
… | |
2011 | || getgid () != getegid (); |
2137 | || getgid () != getegid (); |
2012 | #endif |
2138 | #endif |
2013 | } |
2139 | } |
2014 | |
2140 | |
2015 | unsigned int ecb_cold |
2141 | unsigned int ecb_cold |
2016 | ev_supported_backends (void) |
2142 | ev_supported_backends (void) EV_THROW |
2017 | { |
2143 | { |
2018 | unsigned int flags = 0; |
2144 | unsigned int flags = 0; |
2019 | |
2145 | |
2020 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
2146 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
2021 | if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; |
2147 | if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; |
… | |
… | |
2025 | |
2151 | |
2026 | return flags; |
2152 | return flags; |
2027 | } |
2153 | } |
2028 | |
2154 | |
2029 | unsigned int ecb_cold |
2155 | unsigned int ecb_cold |
2030 | ev_recommended_backends (void) |
2156 | ev_recommended_backends (void) EV_THROW |
2031 | { |
2157 | { |
2032 | unsigned int flags = ev_supported_backends (); |
2158 | unsigned int flags = ev_supported_backends (); |
2033 | |
2159 | |
2034 | #ifndef __NetBSD__ |
2160 | #ifndef __NetBSD__ |
2035 | /* kqueue is borked on everything but netbsd apparently */ |
2161 | /* kqueue is borked on everything but netbsd apparently */ |
… | |
… | |
2047 | |
2173 | |
2048 | return flags; |
2174 | return flags; |
2049 | } |
2175 | } |
2050 | |
2176 | |
2051 | unsigned int ecb_cold |
2177 | unsigned int ecb_cold |
2052 | ev_embeddable_backends (void) |
2178 | ev_embeddable_backends (void) EV_THROW |
2053 | { |
2179 | { |
2054 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
2180 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
2055 | |
2181 | |
2056 | /* 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 */ |
2057 | 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 */ |
… | |
… | |
2059 | |
2185 | |
2060 | return flags; |
2186 | return flags; |
2061 | } |
2187 | } |
2062 | |
2188 | |
2063 | unsigned int |
2189 | unsigned int |
2064 | ev_backend (EV_P) |
2190 | ev_backend (EV_P) EV_THROW |
2065 | { |
2191 | { |
2066 | return backend; |
2192 | return backend; |
2067 | } |
2193 | } |
2068 | |
2194 | |
2069 | #if EV_FEATURE_API |
2195 | #if EV_FEATURE_API |
2070 | unsigned int |
2196 | unsigned int |
2071 | ev_iteration (EV_P) |
2197 | ev_iteration (EV_P) EV_THROW |
2072 | { |
2198 | { |
2073 | return loop_count; |
2199 | return loop_count; |
2074 | } |
2200 | } |
2075 | |
2201 | |
2076 | unsigned int |
2202 | unsigned int |
2077 | ev_depth (EV_P) |
2203 | ev_depth (EV_P) EV_THROW |
2078 | { |
2204 | { |
2079 | return loop_depth; |
2205 | return loop_depth; |
2080 | } |
2206 | } |
2081 | |
2207 | |
2082 | void |
2208 | void |
2083 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
2209 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW |
2084 | { |
2210 | { |
2085 | io_blocktime = interval; |
2211 | io_blocktime = interval; |
2086 | } |
2212 | } |
2087 | |
2213 | |
2088 | void |
2214 | void |
2089 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
2215 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW |
2090 | { |
2216 | { |
2091 | timeout_blocktime = interval; |
2217 | timeout_blocktime = interval; |
2092 | } |
2218 | } |
2093 | |
2219 | |
2094 | void |
2220 | void |
2095 | ev_set_userdata (EV_P_ void *data) |
2221 | ev_set_userdata (EV_P_ void *data) EV_THROW |
2096 | { |
2222 | { |
2097 | userdata = data; |
2223 | userdata = data; |
2098 | } |
2224 | } |
2099 | |
2225 | |
2100 | void * |
2226 | void * |
2101 | ev_userdata (EV_P) |
2227 | ev_userdata (EV_P) EV_THROW |
2102 | { |
2228 | { |
2103 | return userdata; |
2229 | return userdata; |
2104 | } |
2230 | } |
2105 | |
2231 | |
2106 | void |
2232 | void |
2107 | 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 |
2108 | { |
2234 | { |
2109 | invoke_cb = invoke_pending_cb; |
2235 | invoke_cb = invoke_pending_cb; |
2110 | } |
2236 | } |
2111 | |
2237 | |
2112 | void |
2238 | void |
2113 | 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 |
2114 | { |
2240 | { |
2115 | release_cb = release; |
2241 | release_cb = release; |
2116 | acquire_cb = acquire; |
2242 | acquire_cb = acquire; |
2117 | } |
2243 | } |
2118 | #endif |
2244 | #endif |
2119 | |
2245 | |
2120 | /* initialise a loop structure, must be zero-initialised */ |
2246 | /* initialise a loop structure, must be zero-initialised */ |
2121 | static void noinline ecb_cold |
2247 | static void noinline ecb_cold |
2122 | loop_init (EV_P_ unsigned int flags) |
2248 | loop_init (EV_P_ unsigned int flags) EV_THROW |
2123 | { |
2249 | { |
2124 | if (!backend) |
2250 | if (!backend) |
2125 | { |
2251 | { |
2126 | origflags = flags; |
2252 | origflags = flags; |
2127 | |
2253 | |
… | |
… | |
2380 | } |
2506 | } |
2381 | |
2507 | |
2382 | #if EV_MULTIPLICITY |
2508 | #if EV_MULTIPLICITY |
2383 | |
2509 | |
2384 | struct ev_loop * ecb_cold |
2510 | struct ev_loop * ecb_cold |
2385 | ev_loop_new (unsigned int flags) |
2511 | ev_loop_new (unsigned int flags) EV_THROW |
2386 | { |
2512 | { |
2387 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2513 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2388 | |
2514 | |
2389 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2515 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2390 | loop_init (EV_A_ flags); |
2516 | loop_init (EV_A_ flags); |
… | |
… | |
2434 | } |
2560 | } |
2435 | #endif |
2561 | #endif |
2436 | |
2562 | |
2437 | #if EV_FEATURE_API |
2563 | #if EV_FEATURE_API |
2438 | void ecb_cold |
2564 | void ecb_cold |
2439 | ev_verify (EV_P) |
2565 | ev_verify (EV_P) EV_THROW |
2440 | { |
2566 | { |
2441 | #if EV_VERIFY |
2567 | #if EV_VERIFY |
2442 | int i; |
2568 | int i; |
2443 | WL w; |
2569 | WL w, w2; |
2444 | |
2570 | |
2445 | assert (activecnt >= -1); |
2571 | assert (activecnt >= -1); |
2446 | |
2572 | |
2447 | assert (fdchangemax >= fdchangecnt); |
2573 | assert (fdchangemax >= fdchangecnt); |
2448 | for (i = 0; i < fdchangecnt; ++i) |
2574 | for (i = 0; i < fdchangecnt; ++i) |
2449 | assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); |
2575 | assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); |
2450 | |
2576 | |
2451 | assert (anfdmax >= 0); |
2577 | assert (anfdmax >= 0); |
2452 | for (i = 0; i < anfdmax; ++i) |
2578 | for (i = 0; i < anfdmax; ++i) |
|
|
2579 | { |
|
|
2580 | int j = 0; |
|
|
2581 | |
2453 | for (w = anfds [i].head; w; w = w->next) |
2582 | for (w = w2 = anfds [i].head; w; w = w->next) |
2454 | { |
2583 | { |
2455 | verify_watcher (EV_A_ (W)w); |
2584 | verify_watcher (EV_A_ (W)w); |
|
|
2585 | |
|
|
2586 | if (j++ & 1) |
|
|
2587 | { |
|
|
2588 | assert (("libev: io watcher list contains a loop", w != w2)); |
|
|
2589 | w2 = w2->next; |
|
|
2590 | } |
|
|
2591 | |
2456 | assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); |
2592 | assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); |
2457 | assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); |
2593 | assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); |
2458 | } |
2594 | } |
|
|
2595 | } |
2459 | |
2596 | |
2460 | assert (timermax >= timercnt); |
2597 | assert (timermax >= timercnt); |
2461 | verify_heap (EV_A_ timers, timercnt); |
2598 | verify_heap (EV_A_ timers, timercnt); |
2462 | |
2599 | |
2463 | #if EV_PERIODIC_ENABLE |
2600 | #if EV_PERIODIC_ENABLE |
… | |
… | |
2513 | #if EV_MULTIPLICITY |
2650 | #if EV_MULTIPLICITY |
2514 | struct ev_loop * ecb_cold |
2651 | struct ev_loop * ecb_cold |
2515 | #else |
2652 | #else |
2516 | int |
2653 | int |
2517 | #endif |
2654 | #endif |
2518 | ev_default_loop (unsigned int flags) |
2655 | ev_default_loop (unsigned int flags) EV_THROW |
2519 | { |
2656 | { |
2520 | if (!ev_default_loop_ptr) |
2657 | if (!ev_default_loop_ptr) |
2521 | { |
2658 | { |
2522 | #if EV_MULTIPLICITY |
2659 | #if EV_MULTIPLICITY |
2523 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
2660 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
… | |
… | |
2542 | |
2679 | |
2543 | return ev_default_loop_ptr; |
2680 | return ev_default_loop_ptr; |
2544 | } |
2681 | } |
2545 | |
2682 | |
2546 | void |
2683 | void |
2547 | ev_loop_fork (EV_P) |
2684 | ev_loop_fork (EV_P) EV_THROW |
2548 | { |
2685 | { |
2549 | postfork = 1; /* must be in line with ev_default_fork */ |
2686 | postfork = 1; /* must be in line with ev_default_fork */ |
2550 | } |
2687 | } |
2551 | |
2688 | |
2552 | /*****************************************************************************/ |
2689 | /*****************************************************************************/ |
… | |
… | |
2556 | { |
2693 | { |
2557 | EV_CB_INVOKE ((W)w, revents); |
2694 | EV_CB_INVOKE ((W)w, revents); |
2558 | } |
2695 | } |
2559 | |
2696 | |
2560 | unsigned int |
2697 | unsigned int |
2561 | ev_pending_count (EV_P) |
2698 | ev_pending_count (EV_P) EV_THROW |
2562 | { |
2699 | { |
2563 | int pri; |
2700 | int pri; |
2564 | unsigned int count = 0; |
2701 | unsigned int count = 0; |
2565 | |
2702 | |
2566 | for (pri = NUMPRI; pri--; ) |
2703 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
2570 | } |
2707 | } |
2571 | |
2708 | |
2572 | void noinline |
2709 | void noinline |
2573 | ev_invoke_pending (EV_P) |
2710 | ev_invoke_pending (EV_P) |
2574 | { |
2711 | { |
2575 | int pri; |
2712 | for (pendingpri = NUMPRI; pendingpri--; ) /* pendingpri is modified during the loop */ |
2576 | |
|
|
2577 | for (pri = NUMPRI; pri--; ) |
|
|
2578 | while (pendingcnt [pri]) |
2713 | while (pendingcnt [pendingpri]) |
2579 | { |
2714 | { |
2580 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
2715 | ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; |
2581 | |
2716 | |
2582 | p->w->pending = 0; |
2717 | p->w->pending = 0; |
2583 | EV_CB_INVOKE (p->w, p->events); |
2718 | EV_CB_INVOKE (p->w, p->events); |
2584 | EV_FREQUENT_CHECK; |
2719 | EV_FREQUENT_CHECK; |
2585 | } |
2720 | } |
… | |
… | |
2825 | |
2960 | |
2826 | mn_now = ev_rt_now; |
2961 | mn_now = ev_rt_now; |
2827 | } |
2962 | } |
2828 | } |
2963 | } |
2829 | |
2964 | |
2830 | void |
2965 | int |
2831 | ev_run (EV_P_ int flags) |
2966 | ev_run (EV_P_ int flags) |
2832 | { |
2967 | { |
2833 | #if EV_FEATURE_API |
2968 | #if EV_FEATURE_API |
2834 | ++loop_depth; |
2969 | ++loop_depth; |
2835 | #endif |
2970 | #endif |
… | |
… | |
2948 | #endif |
3083 | #endif |
2949 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
3084 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2950 | backend_poll (EV_A_ waittime); |
3085 | backend_poll (EV_A_ waittime); |
2951 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
3086 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2952 | |
3087 | |
2953 | pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ |
3088 | pipe_write_wanted = 0; /* just an optimisation, no fence needed */ |
2954 | |
3089 | |
2955 | if (pipe_write_skipped) |
3090 | if (pipe_write_skipped) |
2956 | { |
3091 | { |
2957 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
3092 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
2958 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
3093 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
… | |
… | |
2992 | loop_done = EVBREAK_CANCEL; |
3127 | loop_done = EVBREAK_CANCEL; |
2993 | |
3128 | |
2994 | #if EV_FEATURE_API |
3129 | #if EV_FEATURE_API |
2995 | --loop_depth; |
3130 | --loop_depth; |
2996 | #endif |
3131 | #endif |
|
|
3132 | |
|
|
3133 | return activecnt; |
2997 | } |
3134 | } |
2998 | |
3135 | |
2999 | void |
3136 | void |
3000 | ev_break (EV_P_ int how) |
3137 | ev_break (EV_P_ int how) EV_THROW |
3001 | { |
3138 | { |
3002 | loop_done = how; |
3139 | loop_done = how; |
3003 | } |
3140 | } |
3004 | |
3141 | |
3005 | void |
3142 | void |
3006 | ev_ref (EV_P) |
3143 | ev_ref (EV_P) EV_THROW |
3007 | { |
3144 | { |
3008 | ++activecnt; |
3145 | ++activecnt; |
3009 | } |
3146 | } |
3010 | |
3147 | |
3011 | void |
3148 | void |
3012 | ev_unref (EV_P) |
3149 | ev_unref (EV_P) EV_THROW |
3013 | { |
3150 | { |
3014 | --activecnt; |
3151 | --activecnt; |
3015 | } |
3152 | } |
3016 | |
3153 | |
3017 | void |
3154 | void |
3018 | ev_now_update (EV_P) |
3155 | ev_now_update (EV_P) EV_THROW |
3019 | { |
3156 | { |
3020 | time_update (EV_A_ 1e100); |
3157 | time_update (EV_A_ 1e100); |
3021 | } |
3158 | } |
3022 | |
3159 | |
3023 | void |
3160 | void |
3024 | ev_suspend (EV_P) |
3161 | ev_suspend (EV_P) EV_THROW |
3025 | { |
3162 | { |
3026 | ev_now_update (EV_A); |
3163 | ev_now_update (EV_A); |
3027 | } |
3164 | } |
3028 | |
3165 | |
3029 | void |
3166 | void |
3030 | ev_resume (EV_P) |
3167 | ev_resume (EV_P) EV_THROW |
3031 | { |
3168 | { |
3032 | ev_tstamp mn_prev = mn_now; |
3169 | ev_tstamp mn_prev = mn_now; |
3033 | |
3170 | |
3034 | ev_now_update (EV_A); |
3171 | ev_now_update (EV_A); |
3035 | timers_reschedule (EV_A_ mn_now - mn_prev); |
3172 | timers_reschedule (EV_A_ mn_now - mn_prev); |
… | |
… | |
3074 | w->pending = 0; |
3211 | w->pending = 0; |
3075 | } |
3212 | } |
3076 | } |
3213 | } |
3077 | |
3214 | |
3078 | int |
3215 | int |
3079 | ev_clear_pending (EV_P_ void *w) |
3216 | ev_clear_pending (EV_P_ void *w) EV_THROW |
3080 | { |
3217 | { |
3081 | W w_ = (W)w; |
3218 | W w_ = (W)w; |
3082 | int pending = w_->pending; |
3219 | int pending = w_->pending; |
3083 | |
3220 | |
3084 | if (expect_true (pending)) |
3221 | if (expect_true (pending)) |
… | |
… | |
3117 | } |
3254 | } |
3118 | |
3255 | |
3119 | /*****************************************************************************/ |
3256 | /*****************************************************************************/ |
3120 | |
3257 | |
3121 | void noinline |
3258 | void noinline |
3122 | ev_io_start (EV_P_ ev_io *w) |
3259 | ev_io_start (EV_P_ ev_io *w) EV_THROW |
3123 | { |
3260 | { |
3124 | int fd = w->fd; |
3261 | int fd = w->fd; |
3125 | |
3262 | |
3126 | if (expect_false (ev_is_active (w))) |
3263 | if (expect_false (ev_is_active (w))) |
3127 | return; |
3264 | return; |
… | |
… | |
3133 | |
3270 | |
3134 | ev_start (EV_A_ (W)w, 1); |
3271 | ev_start (EV_A_ (W)w, 1); |
3135 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
3272 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
3136 | wlist_add (&anfds[fd].head, (WL)w); |
3273 | wlist_add (&anfds[fd].head, (WL)w); |
3137 | |
3274 | |
|
|
3275 | /* common bug, apparently */ |
|
|
3276 | assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w)); |
|
|
3277 | |
3138 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
3278 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
3139 | w->events &= ~EV__IOFDSET; |
3279 | w->events &= ~EV__IOFDSET; |
3140 | |
3280 | |
3141 | EV_FREQUENT_CHECK; |
3281 | EV_FREQUENT_CHECK; |
3142 | } |
3282 | } |
3143 | |
3283 | |
3144 | void noinline |
3284 | void noinline |
3145 | ev_io_stop (EV_P_ ev_io *w) |
3285 | ev_io_stop (EV_P_ ev_io *w) EV_THROW |
3146 | { |
3286 | { |
3147 | clear_pending (EV_A_ (W)w); |
3287 | clear_pending (EV_A_ (W)w); |
3148 | if (expect_false (!ev_is_active (w))) |
3288 | if (expect_false (!ev_is_active (w))) |
3149 | return; |
3289 | return; |
3150 | |
3290 | |
… | |
… | |
3159 | |
3299 | |
3160 | EV_FREQUENT_CHECK; |
3300 | EV_FREQUENT_CHECK; |
3161 | } |
3301 | } |
3162 | |
3302 | |
3163 | void noinline |
3303 | void noinline |
3164 | ev_timer_start (EV_P_ ev_timer *w) |
3304 | ev_timer_start (EV_P_ ev_timer *w) EV_THROW |
3165 | { |
3305 | { |
3166 | if (expect_false (ev_is_active (w))) |
3306 | if (expect_false (ev_is_active (w))) |
3167 | return; |
3307 | return; |
3168 | |
3308 | |
3169 | ev_at (w) += mn_now; |
3309 | ev_at (w) += mn_now; |
… | |
… | |
3183 | |
3323 | |
3184 | /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ |
3324 | /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ |
3185 | } |
3325 | } |
3186 | |
3326 | |
3187 | void noinline |
3327 | void noinline |
3188 | ev_timer_stop (EV_P_ ev_timer *w) |
3328 | ev_timer_stop (EV_P_ ev_timer *w) EV_THROW |
3189 | { |
3329 | { |
3190 | clear_pending (EV_A_ (W)w); |
3330 | clear_pending (EV_A_ (W)w); |
3191 | if (expect_false (!ev_is_active (w))) |
3331 | if (expect_false (!ev_is_active (w))) |
3192 | return; |
3332 | return; |
3193 | |
3333 | |
… | |
… | |
3213 | |
3353 | |
3214 | EV_FREQUENT_CHECK; |
3354 | EV_FREQUENT_CHECK; |
3215 | } |
3355 | } |
3216 | |
3356 | |
3217 | void noinline |
3357 | void noinline |
3218 | ev_timer_again (EV_P_ ev_timer *w) |
3358 | ev_timer_again (EV_P_ ev_timer *w) EV_THROW |
3219 | { |
3359 | { |
3220 | EV_FREQUENT_CHECK; |
3360 | EV_FREQUENT_CHECK; |
|
|
3361 | |
|
|
3362 | clear_pending (EV_A_ (W)w); |
3221 | |
3363 | |
3222 | if (ev_is_active (w)) |
3364 | if (ev_is_active (w)) |
3223 | { |
3365 | { |
3224 | if (w->repeat) |
3366 | if (w->repeat) |
3225 | { |
3367 | { |
… | |
… | |
3238 | |
3380 | |
3239 | EV_FREQUENT_CHECK; |
3381 | EV_FREQUENT_CHECK; |
3240 | } |
3382 | } |
3241 | |
3383 | |
3242 | ev_tstamp |
3384 | ev_tstamp |
3243 | ev_timer_remaining (EV_P_ ev_timer *w) |
3385 | ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW |
3244 | { |
3386 | { |
3245 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
3387 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
3246 | } |
3388 | } |
3247 | |
3389 | |
3248 | #if EV_PERIODIC_ENABLE |
3390 | #if EV_PERIODIC_ENABLE |
3249 | void noinline |
3391 | void noinline |
3250 | ev_periodic_start (EV_P_ ev_periodic *w) |
3392 | ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW |
3251 | { |
3393 | { |
3252 | if (expect_false (ev_is_active (w))) |
3394 | if (expect_false (ev_is_active (w))) |
3253 | return; |
3395 | return; |
3254 | |
3396 | |
3255 | if (w->reschedule_cb) |
3397 | if (w->reschedule_cb) |
… | |
… | |
3275 | |
3417 | |
3276 | /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ |
3418 | /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ |
3277 | } |
3419 | } |
3278 | |
3420 | |
3279 | void noinline |
3421 | void noinline |
3280 | ev_periodic_stop (EV_P_ ev_periodic *w) |
3422 | ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW |
3281 | { |
3423 | { |
3282 | clear_pending (EV_A_ (W)w); |
3424 | clear_pending (EV_A_ (W)w); |
3283 | if (expect_false (!ev_is_active (w))) |
3425 | if (expect_false (!ev_is_active (w))) |
3284 | return; |
3426 | return; |
3285 | |
3427 | |
… | |
… | |
3303 | |
3445 | |
3304 | EV_FREQUENT_CHECK; |
3446 | EV_FREQUENT_CHECK; |
3305 | } |
3447 | } |
3306 | |
3448 | |
3307 | void noinline |
3449 | void noinline |
3308 | ev_periodic_again (EV_P_ ev_periodic *w) |
3450 | ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW |
3309 | { |
3451 | { |
3310 | /* TODO: use adjustheap and recalculation */ |
3452 | /* TODO: use adjustheap and recalculation */ |
3311 | ev_periodic_stop (EV_A_ w); |
3453 | ev_periodic_stop (EV_A_ w); |
3312 | ev_periodic_start (EV_A_ w); |
3454 | ev_periodic_start (EV_A_ w); |
3313 | } |
3455 | } |
… | |
… | |
3318 | #endif |
3460 | #endif |
3319 | |
3461 | |
3320 | #if EV_SIGNAL_ENABLE |
3462 | #if EV_SIGNAL_ENABLE |
3321 | |
3463 | |
3322 | void noinline |
3464 | void noinline |
3323 | ev_signal_start (EV_P_ ev_signal *w) |
3465 | ev_signal_start (EV_P_ ev_signal *w) EV_THROW |
3324 | { |
3466 | { |
3325 | if (expect_false (ev_is_active (w))) |
3467 | if (expect_false (ev_is_active (w))) |
3326 | return; |
3468 | return; |
3327 | |
3469 | |
3328 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); |
3470 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); |
… | |
… | |
3399 | |
3541 | |
3400 | EV_FREQUENT_CHECK; |
3542 | EV_FREQUENT_CHECK; |
3401 | } |
3543 | } |
3402 | |
3544 | |
3403 | void noinline |
3545 | void noinline |
3404 | ev_signal_stop (EV_P_ ev_signal *w) |
3546 | ev_signal_stop (EV_P_ ev_signal *w) EV_THROW |
3405 | { |
3547 | { |
3406 | clear_pending (EV_A_ (W)w); |
3548 | clear_pending (EV_A_ (W)w); |
3407 | if (expect_false (!ev_is_active (w))) |
3549 | if (expect_false (!ev_is_active (w))) |
3408 | return; |
3550 | return; |
3409 | |
3551 | |
… | |
… | |
3440 | #endif |
3582 | #endif |
3441 | |
3583 | |
3442 | #if EV_CHILD_ENABLE |
3584 | #if EV_CHILD_ENABLE |
3443 | |
3585 | |
3444 | void |
3586 | void |
3445 | ev_child_start (EV_P_ ev_child *w) |
3587 | ev_child_start (EV_P_ ev_child *w) EV_THROW |
3446 | { |
3588 | { |
3447 | #if EV_MULTIPLICITY |
3589 | #if EV_MULTIPLICITY |
3448 | assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
3590 | assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
3449 | #endif |
3591 | #endif |
3450 | if (expect_false (ev_is_active (w))) |
3592 | if (expect_false (ev_is_active (w))) |
… | |
… | |
3457 | |
3599 | |
3458 | EV_FREQUENT_CHECK; |
3600 | EV_FREQUENT_CHECK; |
3459 | } |
3601 | } |
3460 | |
3602 | |
3461 | void |
3603 | void |
3462 | ev_child_stop (EV_P_ ev_child *w) |
3604 | ev_child_stop (EV_P_ ev_child *w) EV_THROW |
3463 | { |
3605 | { |
3464 | clear_pending (EV_A_ (W)w); |
3606 | clear_pending (EV_A_ (W)w); |
3465 | if (expect_false (!ev_is_active (w))) |
3607 | if (expect_false (!ev_is_active (w))) |
3466 | return; |
3608 | return; |
3467 | |
3609 | |
… | |
… | |
3634 | } |
3776 | } |
3635 | |
3777 | |
3636 | inline_size int |
3778 | inline_size int |
3637 | infy_newfd (void) |
3779 | infy_newfd (void) |
3638 | { |
3780 | { |
3639 | #if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) |
3781 | #if defined IN_CLOEXEC && defined IN_NONBLOCK |
3640 | int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); |
3782 | int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); |
3641 | if (fd >= 0) |
3783 | if (fd >= 0) |
3642 | return fd; |
3784 | return fd; |
3643 | #endif |
3785 | #endif |
3644 | return inotify_init (); |
3786 | return inotify_init (); |
… | |
… | |
3719 | #else |
3861 | #else |
3720 | # define EV_LSTAT(p,b) lstat (p, b) |
3862 | # define EV_LSTAT(p,b) lstat (p, b) |
3721 | #endif |
3863 | #endif |
3722 | |
3864 | |
3723 | void |
3865 | void |
3724 | ev_stat_stat (EV_P_ ev_stat *w) |
3866 | ev_stat_stat (EV_P_ ev_stat *w) EV_THROW |
3725 | { |
3867 | { |
3726 | if (lstat (w->path, &w->attr) < 0) |
3868 | if (lstat (w->path, &w->attr) < 0) |
3727 | w->attr.st_nlink = 0; |
3869 | w->attr.st_nlink = 0; |
3728 | else if (!w->attr.st_nlink) |
3870 | else if (!w->attr.st_nlink) |
3729 | w->attr.st_nlink = 1; |
3871 | w->attr.st_nlink = 1; |
… | |
… | |
3768 | ev_feed_event (EV_A_ w, EV_STAT); |
3910 | ev_feed_event (EV_A_ w, EV_STAT); |
3769 | } |
3911 | } |
3770 | } |
3912 | } |
3771 | |
3913 | |
3772 | void |
3914 | void |
3773 | ev_stat_start (EV_P_ ev_stat *w) |
3915 | ev_stat_start (EV_P_ ev_stat *w) EV_THROW |
3774 | { |
3916 | { |
3775 | if (expect_false (ev_is_active (w))) |
3917 | if (expect_false (ev_is_active (w))) |
3776 | return; |
3918 | return; |
3777 | |
3919 | |
3778 | ev_stat_stat (EV_A_ w); |
3920 | ev_stat_stat (EV_A_ w); |
… | |
… | |
3799 | |
3941 | |
3800 | EV_FREQUENT_CHECK; |
3942 | EV_FREQUENT_CHECK; |
3801 | } |
3943 | } |
3802 | |
3944 | |
3803 | void |
3945 | void |
3804 | ev_stat_stop (EV_P_ ev_stat *w) |
3946 | ev_stat_stop (EV_P_ ev_stat *w) EV_THROW |
3805 | { |
3947 | { |
3806 | clear_pending (EV_A_ (W)w); |
3948 | clear_pending (EV_A_ (W)w); |
3807 | if (expect_false (!ev_is_active (w))) |
3949 | if (expect_false (!ev_is_active (w))) |
3808 | return; |
3950 | return; |
3809 | |
3951 | |
… | |
… | |
3825 | } |
3967 | } |
3826 | #endif |
3968 | #endif |
3827 | |
3969 | |
3828 | #if EV_IDLE_ENABLE |
3970 | #if EV_IDLE_ENABLE |
3829 | void |
3971 | void |
3830 | ev_idle_start (EV_P_ ev_idle *w) |
3972 | ev_idle_start (EV_P_ ev_idle *w) EV_THROW |
3831 | { |
3973 | { |
3832 | if (expect_false (ev_is_active (w))) |
3974 | if (expect_false (ev_is_active (w))) |
3833 | return; |
3975 | return; |
3834 | |
3976 | |
3835 | pri_adjust (EV_A_ (W)w); |
3977 | pri_adjust (EV_A_ (W)w); |
… | |
… | |
3848 | |
3990 | |
3849 | EV_FREQUENT_CHECK; |
3991 | EV_FREQUENT_CHECK; |
3850 | } |
3992 | } |
3851 | |
3993 | |
3852 | void |
3994 | void |
3853 | ev_idle_stop (EV_P_ ev_idle *w) |
3995 | ev_idle_stop (EV_P_ ev_idle *w) EV_THROW |
3854 | { |
3996 | { |
3855 | clear_pending (EV_A_ (W)w); |
3997 | clear_pending (EV_A_ (W)w); |
3856 | if (expect_false (!ev_is_active (w))) |
3998 | if (expect_false (!ev_is_active (w))) |
3857 | return; |
3999 | return; |
3858 | |
4000 | |
… | |
… | |
3872 | } |
4014 | } |
3873 | #endif |
4015 | #endif |
3874 | |
4016 | |
3875 | #if EV_PREPARE_ENABLE |
4017 | #if EV_PREPARE_ENABLE |
3876 | void |
4018 | void |
3877 | ev_prepare_start (EV_P_ ev_prepare *w) |
4019 | ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW |
3878 | { |
4020 | { |
3879 | if (expect_false (ev_is_active (w))) |
4021 | if (expect_false (ev_is_active (w))) |
3880 | return; |
4022 | return; |
3881 | |
4023 | |
3882 | EV_FREQUENT_CHECK; |
4024 | EV_FREQUENT_CHECK; |
… | |
… | |
3887 | |
4029 | |
3888 | EV_FREQUENT_CHECK; |
4030 | EV_FREQUENT_CHECK; |
3889 | } |
4031 | } |
3890 | |
4032 | |
3891 | void |
4033 | void |
3892 | ev_prepare_stop (EV_P_ ev_prepare *w) |
4034 | ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW |
3893 | { |
4035 | { |
3894 | clear_pending (EV_A_ (W)w); |
4036 | clear_pending (EV_A_ (W)w); |
3895 | if (expect_false (!ev_is_active (w))) |
4037 | if (expect_false (!ev_is_active (w))) |
3896 | return; |
4038 | return; |
3897 | |
4039 | |
… | |
… | |
3910 | } |
4052 | } |
3911 | #endif |
4053 | #endif |
3912 | |
4054 | |
3913 | #if EV_CHECK_ENABLE |
4055 | #if EV_CHECK_ENABLE |
3914 | void |
4056 | void |
3915 | ev_check_start (EV_P_ ev_check *w) |
4057 | ev_check_start (EV_P_ ev_check *w) EV_THROW |
3916 | { |
4058 | { |
3917 | if (expect_false (ev_is_active (w))) |
4059 | if (expect_false (ev_is_active (w))) |
3918 | return; |
4060 | return; |
3919 | |
4061 | |
3920 | EV_FREQUENT_CHECK; |
4062 | EV_FREQUENT_CHECK; |
… | |
… | |
3925 | |
4067 | |
3926 | EV_FREQUENT_CHECK; |
4068 | EV_FREQUENT_CHECK; |
3927 | } |
4069 | } |
3928 | |
4070 | |
3929 | void |
4071 | void |
3930 | ev_check_stop (EV_P_ ev_check *w) |
4072 | ev_check_stop (EV_P_ ev_check *w) EV_THROW |
3931 | { |
4073 | { |
3932 | clear_pending (EV_A_ (W)w); |
4074 | clear_pending (EV_A_ (W)w); |
3933 | if (expect_false (!ev_is_active (w))) |
4075 | if (expect_false (!ev_is_active (w))) |
3934 | return; |
4076 | return; |
3935 | |
4077 | |
… | |
… | |
3948 | } |
4090 | } |
3949 | #endif |
4091 | #endif |
3950 | |
4092 | |
3951 | #if EV_EMBED_ENABLE |
4093 | #if EV_EMBED_ENABLE |
3952 | void noinline |
4094 | void noinline |
3953 | ev_embed_sweep (EV_P_ ev_embed *w) |
4095 | ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW |
3954 | { |
4096 | { |
3955 | ev_run (w->other, EVRUN_NOWAIT); |
4097 | ev_run (w->other, EVRUN_NOWAIT); |
3956 | } |
4098 | } |
3957 | |
4099 | |
3958 | static void |
4100 | static void |
… | |
… | |
4006 | ev_idle_stop (EV_A_ idle); |
4148 | ev_idle_stop (EV_A_ idle); |
4007 | } |
4149 | } |
4008 | #endif |
4150 | #endif |
4009 | |
4151 | |
4010 | void |
4152 | void |
4011 | ev_embed_start (EV_P_ ev_embed *w) |
4153 | ev_embed_start (EV_P_ ev_embed *w) EV_THROW |
4012 | { |
4154 | { |
4013 | if (expect_false (ev_is_active (w))) |
4155 | if (expect_false (ev_is_active (w))) |
4014 | return; |
4156 | return; |
4015 | |
4157 | |
4016 | { |
4158 | { |
… | |
… | |
4037 | |
4179 | |
4038 | EV_FREQUENT_CHECK; |
4180 | EV_FREQUENT_CHECK; |
4039 | } |
4181 | } |
4040 | |
4182 | |
4041 | void |
4183 | void |
4042 | ev_embed_stop (EV_P_ ev_embed *w) |
4184 | ev_embed_stop (EV_P_ ev_embed *w) EV_THROW |
4043 | { |
4185 | { |
4044 | clear_pending (EV_A_ (W)w); |
4186 | clear_pending (EV_A_ (W)w); |
4045 | if (expect_false (!ev_is_active (w))) |
4187 | if (expect_false (!ev_is_active (w))) |
4046 | return; |
4188 | return; |
4047 | |
4189 | |
… | |
… | |
4057 | } |
4199 | } |
4058 | #endif |
4200 | #endif |
4059 | |
4201 | |
4060 | #if EV_FORK_ENABLE |
4202 | #if EV_FORK_ENABLE |
4061 | void |
4203 | void |
4062 | ev_fork_start (EV_P_ ev_fork *w) |
4204 | ev_fork_start (EV_P_ ev_fork *w) EV_THROW |
4063 | { |
4205 | { |
4064 | if (expect_false (ev_is_active (w))) |
4206 | if (expect_false (ev_is_active (w))) |
4065 | return; |
4207 | return; |
4066 | |
4208 | |
4067 | EV_FREQUENT_CHECK; |
4209 | EV_FREQUENT_CHECK; |
… | |
… | |
4072 | |
4214 | |
4073 | EV_FREQUENT_CHECK; |
4215 | EV_FREQUENT_CHECK; |
4074 | } |
4216 | } |
4075 | |
4217 | |
4076 | void |
4218 | void |
4077 | ev_fork_stop (EV_P_ ev_fork *w) |
4219 | ev_fork_stop (EV_P_ ev_fork *w) EV_THROW |
4078 | { |
4220 | { |
4079 | clear_pending (EV_A_ (W)w); |
4221 | clear_pending (EV_A_ (W)w); |
4080 | if (expect_false (!ev_is_active (w))) |
4222 | if (expect_false (!ev_is_active (w))) |
4081 | return; |
4223 | return; |
4082 | |
4224 | |
… | |
… | |
4095 | } |
4237 | } |
4096 | #endif |
4238 | #endif |
4097 | |
4239 | |
4098 | #if EV_CLEANUP_ENABLE |
4240 | #if EV_CLEANUP_ENABLE |
4099 | void |
4241 | void |
4100 | ev_cleanup_start (EV_P_ ev_cleanup *w) |
4242 | ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW |
4101 | { |
4243 | { |
4102 | if (expect_false (ev_is_active (w))) |
4244 | if (expect_false (ev_is_active (w))) |
4103 | return; |
4245 | return; |
4104 | |
4246 | |
4105 | EV_FREQUENT_CHECK; |
4247 | EV_FREQUENT_CHECK; |
… | |
… | |
4112 | ev_unref (EV_A); |
4254 | ev_unref (EV_A); |
4113 | EV_FREQUENT_CHECK; |
4255 | EV_FREQUENT_CHECK; |
4114 | } |
4256 | } |
4115 | |
4257 | |
4116 | void |
4258 | void |
4117 | ev_cleanup_stop (EV_P_ ev_cleanup *w) |
4259 | ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW |
4118 | { |
4260 | { |
4119 | clear_pending (EV_A_ (W)w); |
4261 | clear_pending (EV_A_ (W)w); |
4120 | if (expect_false (!ev_is_active (w))) |
4262 | if (expect_false (!ev_is_active (w))) |
4121 | return; |
4263 | return; |
4122 | |
4264 | |
… | |
… | |
4136 | } |
4278 | } |
4137 | #endif |
4279 | #endif |
4138 | |
4280 | |
4139 | #if EV_ASYNC_ENABLE |
4281 | #if EV_ASYNC_ENABLE |
4140 | void |
4282 | void |
4141 | ev_async_start (EV_P_ ev_async *w) |
4283 | ev_async_start (EV_P_ ev_async *w) EV_THROW |
4142 | { |
4284 | { |
4143 | if (expect_false (ev_is_active (w))) |
4285 | if (expect_false (ev_is_active (w))) |
4144 | return; |
4286 | return; |
4145 | |
4287 | |
4146 | w->sent = 0; |
4288 | w->sent = 0; |
… | |
… | |
4155 | |
4297 | |
4156 | EV_FREQUENT_CHECK; |
4298 | EV_FREQUENT_CHECK; |
4157 | } |
4299 | } |
4158 | |
4300 | |
4159 | void |
4301 | void |
4160 | ev_async_stop (EV_P_ ev_async *w) |
4302 | ev_async_stop (EV_P_ ev_async *w) EV_THROW |
4161 | { |
4303 | { |
4162 | clear_pending (EV_A_ (W)w); |
4304 | clear_pending (EV_A_ (W)w); |
4163 | if (expect_false (!ev_is_active (w))) |
4305 | if (expect_false (!ev_is_active (w))) |
4164 | return; |
4306 | return; |
4165 | |
4307 | |
… | |
… | |
4176 | |
4318 | |
4177 | EV_FREQUENT_CHECK; |
4319 | EV_FREQUENT_CHECK; |
4178 | } |
4320 | } |
4179 | |
4321 | |
4180 | void |
4322 | void |
4181 | ev_async_send (EV_P_ ev_async *w) |
4323 | ev_async_send (EV_P_ ev_async *w) EV_THROW |
4182 | { |
4324 | { |
4183 | w->sent = 1; |
4325 | w->sent = 1; |
4184 | evpipe_write (EV_A_ &async_pending); |
4326 | evpipe_write (EV_A_ &async_pending); |
4185 | } |
4327 | } |
4186 | #endif |
4328 | #endif |
… | |
… | |
4223 | |
4365 | |
4224 | once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); |
4366 | once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); |
4225 | } |
4367 | } |
4226 | |
4368 | |
4227 | void |
4369 | void |
4228 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
4370 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW |
4229 | { |
4371 | { |
4230 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
4372 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
4231 | |
4373 | |
4232 | if (expect_false (!once)) |
4374 | if (expect_false (!once)) |
4233 | { |
4375 | { |
… | |
… | |
4255 | |
4397 | |
4256 | /*****************************************************************************/ |
4398 | /*****************************************************************************/ |
4257 | |
4399 | |
4258 | #if EV_WALK_ENABLE |
4400 | #if EV_WALK_ENABLE |
4259 | void ecb_cold |
4401 | void ecb_cold |
4260 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
4402 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW |
4261 | { |
4403 | { |
4262 | int i, j; |
4404 | int i, j; |
4263 | ev_watcher_list *wl, *wn; |
4405 | ev_watcher_list *wl, *wn; |
4264 | |
4406 | |
4265 | if (types & (EV_IO | EV_EMBED)) |
4407 | if (types & (EV_IO | EV_EMBED)) |
… | |
… | |
4371 | |
4513 | |
4372 | #if EV_MULTIPLICITY |
4514 | #if EV_MULTIPLICITY |
4373 | #include "ev_wrap.h" |
4515 | #include "ev_wrap.h" |
4374 | #endif |
4516 | #endif |
4375 | |
4517 | |
4376 | EV_CPP(}) |
|
|
4377 | |
|
|