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

Comparing libev/ev.c (file contents):
Revision 1.345 by sf-exg, Sat Jul 31 22:33:26 2010 UTC vs.
Revision 1.453 by root, Thu Feb 28 00:33:25 2013 UTC

1/* 1/*
2 * libev event processing core, watcher management 2 * libev event processing core, watcher management
3 * 3 *
4 * Copyright (c) 2007,2008,2009,2010 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 *
10 * 1. Redistributions of source code must retain the above copyright notice, 10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer. 11 * this list of conditions and the following disclaimer.
12 * 12 *
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * and other provisions required by the GPL. If you do not delete the 35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under 36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL. 37 * either the BSD or the GPL.
38 */ 38 */
39 39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* this big block deduces configuration from config.h */ 40/* this big block deduces configuration from config.h */
45#ifndef EV_STANDALONE 41#ifndef EV_STANDALONE
46# ifdef EV_CONFIG_H 42# ifdef EV_CONFIG_H
47# include EV_CONFIG_H 43# include EV_CONFIG_H
48# else 44# else
49# include "config.h" 45# include "config.h"
50# endif 46# endif
47
48#if HAVE_FLOOR
49# ifndef EV_USE_FLOOR
50# define EV_USE_FLOOR 1
51# endif
52#endif
51 53
52# if HAVE_CLOCK_SYSCALL 54# if HAVE_CLOCK_SYSCALL
53# ifndef EV_USE_CLOCK_SYSCALL 55# ifndef EV_USE_CLOCK_SYSCALL
54# define EV_USE_CLOCK_SYSCALL 1 56# define EV_USE_CLOCK_SYSCALL 1
55# ifndef EV_USE_REALTIME 57# ifndef EV_USE_REALTIME
57# endif 59# endif
58# ifndef EV_USE_MONOTONIC 60# ifndef EV_USE_MONOTONIC
59# define EV_USE_MONOTONIC 1 61# define EV_USE_MONOTONIC 1
60# endif 62# endif
61# endif 63# endif
62# elif !defined(EV_USE_CLOCK_SYSCALL) 64# elif !defined EV_USE_CLOCK_SYSCALL
63# define EV_USE_CLOCK_SYSCALL 0 65# define EV_USE_CLOCK_SYSCALL 0
64# endif 66# endif
65 67
66# if HAVE_CLOCK_GETTIME 68# if HAVE_CLOCK_GETTIME
67# ifndef EV_USE_MONOTONIC 69# ifndef EV_USE_MONOTONIC
160# define EV_USE_EVENTFD 0 162# define EV_USE_EVENTFD 0
161# endif 163# endif
162 164
163#endif 165#endif
164 166
165#include <math.h>
166#include <stdlib.h> 167#include <stdlib.h>
167#include <string.h> 168#include <string.h>
168#include <fcntl.h> 169#include <fcntl.h>
169#include <stddef.h> 170#include <stddef.h>
170 171
180 181
181#ifdef EV_H 182#ifdef EV_H
182# include EV_H 183# include EV_H
183#else 184#else
184# include "ev.h" 185# include "ev.h"
186#endif
187
188#if EV_NO_THREADS
189# undef EV_NO_SMP
190# define EV_NO_SMP 1
191# undef ECB_NO_THREADS
192# define ECB_NO_THREADS 1
193#endif
194#if EV_NO_SMP
195# undef EV_NO_SMP
196# define ECB_NO_SMP 1
185#endif 197#endif
186 198
187#ifndef _WIN32 199#ifndef _WIN32
188# include <sys/time.h> 200# include <sys/time.h>
189# include <sys/wait.h> 201# include <sys/wait.h>
190# include <unistd.h> 202# include <unistd.h>
191#else 203#else
192# include <io.h> 204# include <io.h>
193# define WIN32_LEAN_AND_MEAN 205# define WIN32_LEAN_AND_MEAN
206# include <winsock2.h>
194# include <windows.h> 207# include <windows.h>
195# ifndef EV_SELECT_IS_WINSOCKET 208# ifndef EV_SELECT_IS_WINSOCKET
196# define EV_SELECT_IS_WINSOCKET 1 209# define EV_SELECT_IS_WINSOCKET 1
197# endif 210# endif
198# undef EV_AVOID_STDIO 211# undef EV_AVOID_STDIO
207#define _DARWIN_UNLIMITED_SELECT 1 220#define _DARWIN_UNLIMITED_SELECT 1
208 221
209/* 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 */
210 223
211/* try to deduce the maximum number of signals on this platform */ 224/* try to deduce the maximum number of signals on this platform */
212#if defined (EV_NSIG) 225#if defined EV_NSIG
213/* use what's provided */ 226/* use what's provided */
214#elif defined (NSIG) 227#elif defined NSIG
215# define EV_NSIG (NSIG) 228# define EV_NSIG (NSIG)
216#elif defined(_NSIG) 229#elif defined _NSIG
217# define EV_NSIG (_NSIG) 230# define EV_NSIG (_NSIG)
218#elif defined (SIGMAX) 231#elif defined SIGMAX
219# define EV_NSIG (SIGMAX+1) 232# define EV_NSIG (SIGMAX+1)
220#elif defined (SIG_MAX) 233#elif defined SIG_MAX
221# define EV_NSIG (SIG_MAX+1) 234# define EV_NSIG (SIG_MAX+1)
222#elif defined (_SIG_MAX) 235#elif defined _SIG_MAX
223# define EV_NSIG (_SIG_MAX+1) 236# define EV_NSIG (_SIG_MAX+1)
224#elif defined (MAXSIG) 237#elif defined MAXSIG
225# define EV_NSIG (MAXSIG+1) 238# define EV_NSIG (MAXSIG+1)
226#elif defined (MAX_SIG) 239#elif defined MAX_SIG
227# define EV_NSIG (MAX_SIG+1) 240# define EV_NSIG (MAX_SIG+1)
228#elif defined (SIGARRAYSIZE) 241#elif defined SIGARRAYSIZE
229# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ 242# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
230#elif defined (_sys_nsig) 243#elif defined _sys_nsig
231# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ 244# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
232#else 245#else
233# error "unable to find value for NSIG, please report" 246# error "unable to find value for NSIG, please report"
234/* to make it compile regardless, just remove the above line, */ 247/* to make it compile regardless, just remove the above line, */
235/* but consider reporting it, too! :) */ 248/* but consider reporting it, too! :) */
236# define EV_NSIG 65 249# define EV_NSIG 65
237#endif 250#endif
238 251
252#ifndef EV_USE_FLOOR
253# define EV_USE_FLOOR 0
254#endif
255
239#ifndef EV_USE_CLOCK_SYSCALL 256#ifndef EV_USE_CLOCK_SYSCALL
240# if __linux && __GLIBC__ >= 2 257# if __linux && __GLIBC__ >= 2
241# define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS 258# define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
242# else 259# else
243# define EV_USE_CLOCK_SYSCALL 0 260# define EV_USE_CLOCK_SYSCALL 0
244# endif 261# endif
245#endif 262#endif
246 263
247#ifndef EV_USE_MONOTONIC 264#ifndef EV_USE_MONOTONIC
248# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 265# if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
249# define EV_USE_MONOTONIC EV_FEATURE_OS 266# define EV_USE_MONOTONIC EV_FEATURE_OS
250# else 267# else
251# define EV_USE_MONOTONIC 0 268# define EV_USE_MONOTONIC 0
252# endif 269# endif
253#endif 270#endif
340 357
341#ifndef EV_HEAP_CACHE_AT 358#ifndef EV_HEAP_CACHE_AT
342# define EV_HEAP_CACHE_AT EV_FEATURE_DATA 359# define EV_HEAP_CACHE_AT EV_FEATURE_DATA
343#endif 360#endif
344 361
362#ifdef ANDROID
363/* supposedly, android doesn't typedef fd_mask */
364# undef EV_USE_SELECT
365# define EV_USE_SELECT 0
366/* supposedly, we need to include syscall.h, not sys/syscall.h, so just disable */
367# undef EV_USE_CLOCK_SYSCALL
368# define EV_USE_CLOCK_SYSCALL 0
369#endif
370
371/* aix's poll.h seems to cause lots of trouble */
372#ifdef _AIX
373/* AIX has a completely broken poll.h header */
374# undef EV_USE_POLL
375# define EV_USE_POLL 0
376#endif
377
345/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ 378/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
346/* which makes programs even slower. might work on other unices, too. */ 379/* which makes programs even slower. might work on other unices, too. */
347#if EV_USE_CLOCK_SYSCALL 380#if EV_USE_CLOCK_SYSCALL
348# include <syscall.h> 381# include <sys/syscall.h>
349# ifdef SYS_clock_gettime 382# ifdef SYS_clock_gettime
350# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 383# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
351# undef EV_USE_MONOTONIC 384# undef EV_USE_MONOTONIC
352# define EV_USE_MONOTONIC 1 385# define EV_USE_MONOTONIC 1
353# else 386# else
356# endif 389# endif
357#endif 390#endif
358 391
359/* this block fixes any misconfiguration where we know we run into trouble otherwise */ 392/* this block fixes any misconfiguration where we know we run into trouble otherwise */
360 393
361#ifdef _AIX
362/* AIX has a completely broken poll.h header */
363# undef EV_USE_POLL
364# define EV_USE_POLL 0
365#endif
366
367#ifndef CLOCK_MONOTONIC 394#ifndef CLOCK_MONOTONIC
368# undef EV_USE_MONOTONIC 395# undef EV_USE_MONOTONIC
369# define EV_USE_MONOTONIC 0 396# define EV_USE_MONOTONIC 0
370#endif 397#endif
371 398
378# undef EV_USE_INOTIFY 405# undef EV_USE_INOTIFY
379# define EV_USE_INOTIFY 0 406# define EV_USE_INOTIFY 0
380#endif 407#endif
381 408
382#if !EV_USE_NANOSLEEP 409#if !EV_USE_NANOSLEEP
383# ifndef _WIN32 410/* hp-ux has it in sys/time.h, which we unconditionally include above */
411# if !defined _WIN32 && !defined __hpux
384# include <sys/select.h> 412# include <sys/select.h>
385# endif 413# endif
386#endif 414#endif
387 415
388#if EV_USE_INOTIFY 416#if EV_USE_INOTIFY
389# include <sys/utsname.h>
390# include <sys/statfs.h> 417# include <sys/statfs.h>
391# include <sys/inotify.h> 418# include <sys/inotify.h>
392/* some very old inotify.h headers don't have IN_DONT_FOLLOW */ 419/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
393# ifndef IN_DONT_FOLLOW 420# ifndef IN_DONT_FOLLOW
394# undef EV_USE_INOTIFY 421# undef EV_USE_INOTIFY
395# define EV_USE_INOTIFY 0 422# define EV_USE_INOTIFY 0
396# endif 423# endif
397#endif
398
399#if EV_SELECT_IS_WINSOCKET
400# include <winsock.h>
401#endif 424#endif
402 425
403#if EV_USE_EVENTFD 426#if EV_USE_EVENTFD
404/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 427/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
405# include <stdint.h> 428# include <stdint.h>
411# define EFD_CLOEXEC O_CLOEXEC 434# define EFD_CLOEXEC O_CLOEXEC
412# else 435# else
413# define EFD_CLOEXEC 02000000 436# define EFD_CLOEXEC 02000000
414# endif 437# endif
415# endif 438# endif
416# ifdef __cplusplus
417extern "C" {
418# endif
419int (eventfd) (unsigned int initval, int flags); 439EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
420# ifdef __cplusplus
421}
422# endif
423#endif 440#endif
424 441
425#if EV_USE_SIGNALFD 442#if EV_USE_SIGNALFD
426/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 443/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
427# include <stdint.h> 444# include <stdint.h>
433# define SFD_CLOEXEC O_CLOEXEC 450# define SFD_CLOEXEC O_CLOEXEC
434# else 451# else
435# define SFD_CLOEXEC 02000000 452# define SFD_CLOEXEC 02000000
436# endif 453# endif
437# endif 454# endif
438# ifdef __cplusplus
439extern "C" {
440# endif
441int signalfd (int fd, const sigset_t *mask, int flags); 455EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
442 456
443struct signalfd_siginfo 457struct signalfd_siginfo
444{ 458{
445 uint32_t ssi_signo; 459 uint32_t ssi_signo;
446 char pad[128 - sizeof (uint32_t)]; 460 char pad[128 - sizeof (uint32_t)];
447}; 461};
448# ifdef __cplusplus
449}
450# endif 462#endif
451#endif
452
453 463
454/**/ 464/**/
455 465
456#if EV_VERIFY >= 3 466#if EV_VERIFY >= 3
457# define EV_FREQUENT_CHECK ev_verify (EV_A) 467# define EV_FREQUENT_CHECK ev_verify (EV_A)
458#else 468#else
459# define EV_FREQUENT_CHECK do { } while (0) 469# define EV_FREQUENT_CHECK do { } while (0)
460#endif 470#endif
461 471
462/* 472/*
463 * This is used to avoid floating point rounding problems. 473 * This is used to work around floating point rounding problems.
464 * It is added to ev_rt_now when scheduling periodics
465 * to ensure progress, time-wise, even when rounding
466 * errors are against us.
467 * This value is good at least till the year 4000. 474 * This value is good at least till the year 4000.
468 * Better solutions welcome.
469 */ 475 */
470#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ 476#define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */
477/*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */
471 478
472#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 479#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
473#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 480#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
474 481
482#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
483#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
484
485/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
486/* ECB.H BEGIN */
487/*
488 * libecb - http://software.schmorp.de/pkg/libecb
489 *
490 * Copyright (©) 2009-2012 Marc Alexander Lehmann <libecb@schmorp.de>
491 * Copyright (©) 2011 Emanuele Giaquinta
492 * All rights reserved.
493 *
494 * Redistribution and use in source and binary forms, with or without modifica-
495 * tion, are permitted provided that the following conditions are met:
496 *
497 * 1. Redistributions of source code must retain the above copyright notice,
498 * this list of conditions and the following disclaimer.
499 *
500 * 2. Redistributions in binary form must reproduce the above copyright
501 * notice, this list of conditions and the following disclaimer in the
502 * documentation and/or other materials provided with the distribution.
503 *
504 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
505 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
506 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
507 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
508 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
509 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
510 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
511 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
512 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
513 * OF THE POSSIBILITY OF SUCH DAMAGE.
514 */
515
516#ifndef ECB_H
517#define ECB_H
518
519/* 16 bits major, 16 bits minor */
520#define ECB_VERSION 0x00010002
521
522#ifdef _WIN32
523 typedef signed char int8_t;
524 typedef unsigned char uint8_t;
525 typedef signed short int16_t;
526 typedef unsigned short uint16_t;
527 typedef signed int int32_t;
528 typedef unsigned int uint32_t;
475#if __GNUC__ >= 4 529 #if __GNUC__
476# define expect(expr,value) __builtin_expect ((expr),(value)) 530 typedef signed long long int64_t;
477# define noinline __attribute__ ((noinline)) 531 typedef unsigned long long uint64_t;
532 #else /* _MSC_VER || __BORLANDC__ */
533 typedef signed __int64 int64_t;
534 typedef unsigned __int64 uint64_t;
535 #endif
536 #ifdef _WIN64
537 #define ECB_PTRSIZE 8
538 typedef uint64_t uintptr_t;
539 typedef int64_t intptr_t;
540 #else
541 #define ECB_PTRSIZE 4
542 typedef uint32_t uintptr_t;
543 typedef int32_t intptr_t;
544 #endif
478#else 545#else
479# define expect(expr,value) (expr) 546 #include <inttypes.h>
480# define noinline 547 #if UINTMAX_MAX > 0xffffffffU
481# if __STDC_VERSION__ < 199901L && __GNUC__ < 2 548 #define ECB_PTRSIZE 8
482# define inline 549 #else
550 #define ECB_PTRSIZE 4
551 #endif
483# endif 552#endif
553
554/* many compilers define _GNUC_ to some versions but then only implement
555 * what their idiot authors think are the "more important" extensions,
556 * causing enormous grief in return for some better fake benchmark numbers.
557 * or so.
558 * we try to detect these and simply assume they are not gcc - if they have
559 * an issue with that they should have done it right in the first place.
560 */
561#ifndef ECB_GCC_VERSION
562 #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__
563 #define ECB_GCC_VERSION(major,minor) 0
564 #else
565 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
484#endif 566 #endif
567#endif
485 568
569#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */
570#define ECB_C99 (__STDC_VERSION__ >= 199901L)
571#define ECB_C11 (__STDC_VERSION__ >= 201112L)
572#define ECB_CPP (__cplusplus+0)
573#define ECB_CPP11 (__cplusplus >= 201103L)
574
575#if ECB_CPP
576 #define ECB_EXTERN_C extern "C"
577 #define ECB_EXTERN_C_BEG ECB_EXTERN_C {
578 #define ECB_EXTERN_C_END }
579#else
580 #define ECB_EXTERN_C extern
581 #define ECB_EXTERN_C_BEG
582 #define ECB_EXTERN_C_END
583#endif
584
585/*****************************************************************************/
586
587/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
588/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
589
590#if ECB_NO_THREADS
591 #define ECB_NO_SMP 1
592#endif
593
594#if ECB_NO_SMP
595 #define ECB_MEMORY_FENCE do { } while (0)
596#endif
597
598#ifndef ECB_MEMORY_FENCE
599 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
600 #if __i386 || __i386__
601 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
602 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
603 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
604 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__
605 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
606 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
607 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
608 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
609 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
610 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
611 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__
612 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
613 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
614 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__
615 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
616 #elif __sparc || __sparc__
617 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
618 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
619 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
620 #elif defined __s390__ || defined __s390x__
621 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
622 #elif defined __mips__
623 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
624 #elif defined __alpha__
625 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
626 #elif defined __hppa__
627 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
628 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
629 #elif defined __ia64__
630 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory")
631 #endif
632 #endif
633#endif
634
635#ifndef ECB_MEMORY_FENCE
636 #if ECB_GCC_VERSION(4,7)
637 /* see comment below (stdatomic.h) about the C11 memory model. */
638 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
639
640 /* The __has_feature syntax from clang is so misdesigned that we cannot use it
641 * without risking compile time errors with other compilers. We *could*
642 * define our own ecb_clang_has_feature, but I just can't be bothered to work
643 * around this shit time and again.
644 * #elif defined __clang && __has_feature (cxx_atomic)
645 * // see comment below (stdatomic.h) about the C11 memory model.
646 * #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
647 */
648
649 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
650 #define ECB_MEMORY_FENCE __sync_synchronize ()
651 #elif _MSC_VER >= 1400 /* VC++ 2005 */
652 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
653 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
654 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
655 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
656 #elif defined _WIN32
657 #include <WinNT.h>
658 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
659 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
660 #include <mbarrier.h>
661 #define ECB_MEMORY_FENCE __machine_rw_barrier ()
662 #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier ()
663 #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier ()
664 #elif __xlC__
665 #define ECB_MEMORY_FENCE __sync ()
666 #endif
667#endif
668
669#ifndef ECB_MEMORY_FENCE
670 #if ECB_C11 && !defined __STDC_NO_ATOMICS__
671 /* we assume that these memory fences work on all variables/all memory accesses, */
672 /* not just C11 atomics and atomic accesses */
673 #include <stdatomic.h>
674 /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */
675 /* any fence other than seq_cst, which isn't very efficient for us. */
676 /* Why that is, we don't know - either the C11 memory model is quite useless */
677 /* for most usages, or gcc and clang have a bug */
678 /* I *currently* lean towards the latter, and inefficiently implement */
679 /* all three of ecb's fences as a seq_cst fence */
680 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
681 #endif
682#endif
683
684#ifndef ECB_MEMORY_FENCE
685 #if !ECB_AVOID_PTHREADS
686 /*
687 * if you get undefined symbol references to pthread_mutex_lock,
688 * or failure to find pthread.h, then you should implement
689 * the ECB_MEMORY_FENCE operations for your cpu/compiler
690 * OR provide pthread.h and link against the posix thread library
691 * of your system.
692 */
693 #include <pthread.h>
694 #define ECB_NEEDS_PTHREADS 1
695 #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1
696
697 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
698 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
699 #endif
700#endif
701
702#if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
703 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
704#endif
705
706#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
707 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
708#endif
709
710/*****************************************************************************/
711
712#if __cplusplus
713 #define ecb_inline static inline
714#elif ECB_GCC_VERSION(2,5)
715 #define ecb_inline static __inline__
716#elif ECB_C99
717 #define ecb_inline static inline
718#else
719 #define ecb_inline static
720#endif
721
722#if ECB_GCC_VERSION(3,3)
723 #define ecb_restrict __restrict__
724#elif ECB_C99
725 #define ecb_restrict restrict
726#else
727 #define ecb_restrict
728#endif
729
730typedef int ecb_bool;
731
732#define ECB_CONCAT_(a, b) a ## b
733#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
734#define ECB_STRINGIFY_(a) # a
735#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
736
737#define ecb_function_ ecb_inline
738
739#if ECB_GCC_VERSION(3,1)
740 #define ecb_attribute(attrlist) __attribute__(attrlist)
741 #define ecb_is_constant(expr) __builtin_constant_p (expr)
742 #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
743 #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
744#else
745 #define ecb_attribute(attrlist)
746 #define ecb_is_constant(expr) 0
747 #define ecb_expect(expr,value) (expr)
748 #define ecb_prefetch(addr,rw,locality)
749#endif
750
751/* no emulation for ecb_decltype */
752#if ECB_GCC_VERSION(4,5)
753 #define ecb_decltype(x) __decltype(x)
754#elif ECB_GCC_VERSION(3,0)
755 #define ecb_decltype(x) __typeof(x)
756#endif
757
758#define ecb_noinline ecb_attribute ((__noinline__))
759#define ecb_unused ecb_attribute ((__unused__))
760#define ecb_const ecb_attribute ((__const__))
761#define ecb_pure ecb_attribute ((__pure__))
762
763#if ECB_C11
764 #define ecb_noreturn _Noreturn
765#else
766 #define ecb_noreturn ecb_attribute ((__noreturn__))
767#endif
768
769#if ECB_GCC_VERSION(4,3)
770 #define ecb_artificial ecb_attribute ((__artificial__))
771 #define ecb_hot ecb_attribute ((__hot__))
772 #define ecb_cold ecb_attribute ((__cold__))
773#else
774 #define ecb_artificial
775 #define ecb_hot
776 #define ecb_cold
777#endif
778
779/* put around conditional expressions if you are very sure that the */
780/* expression is mostly true or mostly false. note that these return */
781/* booleans, not the expression. */
486#define expect_false(expr) expect ((expr) != 0, 0) 782#define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
487#define expect_true(expr) expect ((expr) != 0, 1) 783#define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
784/* for compatibility to the rest of the world */
785#define ecb_likely(expr) ecb_expect_true (expr)
786#define ecb_unlikely(expr) ecb_expect_false (expr)
787
788/* count trailing zero bits and count # of one bits */
789#if ECB_GCC_VERSION(3,4)
790 /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */
791 #define ecb_ld32(x) (__builtin_clz (x) ^ 31)
792 #define ecb_ld64(x) (__builtin_clzll (x) ^ 63)
793 #define ecb_ctz32(x) __builtin_ctz (x)
794 #define ecb_ctz64(x) __builtin_ctzll (x)
795 #define ecb_popcount32(x) __builtin_popcount (x)
796 /* no popcountll */
797#else
798 ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const;
799 ecb_function_ int
800 ecb_ctz32 (uint32_t x)
801 {
802 int r = 0;
803
804 x &= ~x + 1; /* this isolates the lowest bit */
805
806#if ECB_branchless_on_i386
807 r += !!(x & 0xaaaaaaaa) << 0;
808 r += !!(x & 0xcccccccc) << 1;
809 r += !!(x & 0xf0f0f0f0) << 2;
810 r += !!(x & 0xff00ff00) << 3;
811 r += !!(x & 0xffff0000) << 4;
812#else
813 if (x & 0xaaaaaaaa) r += 1;
814 if (x & 0xcccccccc) r += 2;
815 if (x & 0xf0f0f0f0) r += 4;
816 if (x & 0xff00ff00) r += 8;
817 if (x & 0xffff0000) r += 16;
818#endif
819
820 return r;
821 }
822
823 ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const;
824 ecb_function_ int
825 ecb_ctz64 (uint64_t x)
826 {
827 int shift = x & 0xffffffffU ? 0 : 32;
828 return ecb_ctz32 (x >> shift) + shift;
829 }
830
831 ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const;
832 ecb_function_ int
833 ecb_popcount32 (uint32_t x)
834 {
835 x -= (x >> 1) & 0x55555555;
836 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
837 x = ((x >> 4) + x) & 0x0f0f0f0f;
838 x *= 0x01010101;
839
840 return x >> 24;
841 }
842
843 ecb_function_ int ecb_ld32 (uint32_t x) ecb_const;
844 ecb_function_ int ecb_ld32 (uint32_t x)
845 {
846 int r = 0;
847
848 if (x >> 16) { x >>= 16; r += 16; }
849 if (x >> 8) { x >>= 8; r += 8; }
850 if (x >> 4) { x >>= 4; r += 4; }
851 if (x >> 2) { x >>= 2; r += 2; }
852 if (x >> 1) { r += 1; }
853
854 return r;
855 }
856
857 ecb_function_ int ecb_ld64 (uint64_t x) ecb_const;
858 ecb_function_ int ecb_ld64 (uint64_t x)
859 {
860 int r = 0;
861
862 if (x >> 32) { x >>= 32; r += 32; }
863
864 return r + ecb_ld32 (x);
865 }
866#endif
867
868ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) ecb_const;
869ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
870ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) ecb_const;
871ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); }
872
873ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const;
874ecb_function_ uint8_t ecb_bitrev8 (uint8_t x)
875{
876 return ( (x * 0x0802U & 0x22110U)
877 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
878}
879
880ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) ecb_const;
881ecb_function_ uint16_t ecb_bitrev16 (uint16_t x)
882{
883 x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1);
884 x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2);
885 x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4);
886 x = ( x >> 8 ) | ( x << 8);
887
888 return x;
889}
890
891ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) ecb_const;
892ecb_function_ uint32_t ecb_bitrev32 (uint32_t x)
893{
894 x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
895 x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
896 x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
897 x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
898 x = ( x >> 16 ) | ( x << 16);
899
900 return x;
901}
902
903/* popcount64 is only available on 64 bit cpus as gcc builtin */
904/* so for this version we are lazy */
905ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const;
906ecb_function_ int
907ecb_popcount64 (uint64_t x)
908{
909 return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
910}
911
912ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const;
913ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const;
914ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const;
915ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const;
916ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const;
917ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
918ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const;
919ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const;
920
921ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); }
922ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); }
923ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); }
924ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); }
925ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
926ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
927ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
928ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
929
930#if ECB_GCC_VERSION(4,3)
931 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
932 #define ecb_bswap32(x) __builtin_bswap32 (x)
933 #define ecb_bswap64(x) __builtin_bswap64 (x)
934#else
935 ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const;
936 ecb_function_ uint16_t
937 ecb_bswap16 (uint16_t x)
938 {
939 return ecb_rotl16 (x, 8);
940 }
941
942 ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const;
943 ecb_function_ uint32_t
944 ecb_bswap32 (uint32_t x)
945 {
946 return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16);
947 }
948
949 ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const;
950 ecb_function_ uint64_t
951 ecb_bswap64 (uint64_t x)
952 {
953 return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32);
954 }
955#endif
956
957#if ECB_GCC_VERSION(4,5)
958 #define ecb_unreachable() __builtin_unreachable ()
959#else
960 /* this seems to work fine, but gcc always emits a warning for it :/ */
961 ecb_inline void ecb_unreachable (void) ecb_noreturn;
962 ecb_inline void ecb_unreachable (void) { }
963#endif
964
965/* try to tell the compiler that some condition is definitely true */
966#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
967
968ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
969ecb_inline unsigned char
970ecb_byteorder_helper (void)
971{
972 /* the union code still generates code under pressure in gcc, */
973 /* but less than using pointers, and always seems to */
974 /* successfully return a constant. */
975 /* the reason why we have this horrible preprocessor mess */
976 /* is to avoid it in all cases, at least on common architectures */
977 /* or when using a recent enough gcc version (>= 4.6) */
978#if __i386 || __i386__ || _M_X86 || __amd64 || __amd64__ || _M_X64
979 return 0x44;
980#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
981 return 0x44;
982#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
983 return 0x11;
984#else
985 union
986 {
987 uint32_t i;
988 uint8_t c;
989 } u = { 0x11223344 };
990 return u.c;
991#endif
992}
993
994ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
995ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
996ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
997ecb_inline ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }
998
999#if ECB_GCC_VERSION(3,0) || ECB_C99
1000 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
1001#else
1002 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
1003#endif
1004
1005#if __cplusplus
1006 template<typename T>
1007 static inline T ecb_div_rd (T val, T div)
1008 {
1009 return val < 0 ? - ((-val + div - 1) / div) : (val ) / div;
1010 }
1011 template<typename T>
1012 static inline T ecb_div_ru (T val, T div)
1013 {
1014 return val < 0 ? - ((-val ) / div) : (val + div - 1) / div;
1015 }
1016#else
1017 #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div))
1018 #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div))
1019#endif
1020
1021#if ecb_cplusplus_does_not_suck
1022 /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */
1023 template<typename T, int N>
1024 static inline int ecb_array_length (const T (&arr)[N])
1025 {
1026 return N;
1027 }
1028#else
1029 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
1030#endif
1031
1032/*******************************************************************************/
1033/* floating point stuff, can be disabled by defining ECB_NO_LIBM */
1034
1035/* basically, everything uses "ieee pure-endian" floating point numbers */
1036/* the only noteworthy exception is ancient armle, which uses order 43218765 */
1037#if 0 \
1038 || __i386 || __i386__ \
1039 || __amd64 || __amd64__ || __x86_64 || __x86_64__ \
1040 || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \
1041 || defined __arm__ && defined __ARM_EABI__ \
1042 || defined __s390__ || defined __s390x__ \
1043 || defined __mips__ \
1044 || defined __alpha__ \
1045 || defined __hppa__ \
1046 || defined __ia64__ \
1047 || defined _M_IX86 || defined _M_AMD64 || defined _M_IA64
1048 #define ECB_STDFP 1
1049 #include <string.h> /* for memcpy */
1050#else
1051 #define ECB_STDFP 0
1052 #include <math.h> /* for frexp*, ldexp* */
1053#endif
1054
1055#ifndef ECB_NO_LIBM
1056
1057 /* convert a float to ieee single/binary32 */
1058 ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const;
1059 ecb_function_ uint32_t
1060 ecb_float_to_binary32 (float x)
1061 {
1062 uint32_t r;
1063
1064 #if ECB_STDFP
1065 memcpy (&r, &x, 4);
1066 #else
1067 /* slow emulation, works for anything but -0 */
1068 uint32_t m;
1069 int e;
1070
1071 if (x == 0e0f ) return 0x00000000U;
1072 if (x > +3.40282346638528860e+38f) return 0x7f800000U;
1073 if (x < -3.40282346638528860e+38f) return 0xff800000U;
1074 if (x != x ) return 0x7fbfffffU;
1075
1076 m = frexpf (x, &e) * 0x1000000U;
1077
1078 r = m & 0x80000000U;
1079
1080 if (r)
1081 m = -m;
1082
1083 if (e <= -126)
1084 {
1085 m &= 0xffffffU;
1086 m >>= (-125 - e);
1087 e = -126;
1088 }
1089
1090 r |= (e + 126) << 23;
1091 r |= m & 0x7fffffU;
1092 #endif
1093
1094 return r;
1095 }
1096
1097 /* converts an ieee single/binary32 to a float */
1098 ecb_function_ float ecb_binary32_to_float (uint32_t x) ecb_const;
1099 ecb_function_ float
1100 ecb_binary32_to_float (uint32_t x)
1101 {
1102 float r;
1103
1104 #if ECB_STDFP
1105 memcpy (&r, &x, 4);
1106 #else
1107 /* emulation, only works for normals and subnormals and +0 */
1108 int neg = x >> 31;
1109 int e = (x >> 23) & 0xffU;
1110
1111 x &= 0x7fffffU;
1112
1113 if (e)
1114 x |= 0x800000U;
1115 else
1116 e = 1;
1117
1118 /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */
1119 r = ldexpf (x * (0.5f / 0x800000U), e - 126);
1120
1121 r = neg ? -r : r;
1122 #endif
1123
1124 return r;
1125 }
1126
1127 /* convert a double to ieee double/binary64 */
1128 ecb_function_ uint64_t ecb_double_to_binary64 (double x) ecb_const;
1129 ecb_function_ uint64_t
1130 ecb_double_to_binary64 (double x)
1131 {
1132 uint64_t r;
1133
1134 #if ECB_STDFP
1135 memcpy (&r, &x, 8);
1136 #else
1137 /* slow emulation, works for anything but -0 */
1138 uint64_t m;
1139 int e;
1140
1141 if (x == 0e0 ) return 0x0000000000000000U;
1142 if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U;
1143 if (x < -1.79769313486231470e+308) return 0xfff0000000000000U;
1144 if (x != x ) return 0X7ff7ffffffffffffU;
1145
1146 m = frexp (x, &e) * 0x20000000000000U;
1147
1148 r = m & 0x8000000000000000;;
1149
1150 if (r)
1151 m = -m;
1152
1153 if (e <= -1022)
1154 {
1155 m &= 0x1fffffffffffffU;
1156 m >>= (-1021 - e);
1157 e = -1022;
1158 }
1159
1160 r |= ((uint64_t)(e + 1022)) << 52;
1161 r |= m & 0xfffffffffffffU;
1162 #endif
1163
1164 return r;
1165 }
1166
1167 /* converts an ieee double/binary64 to a double */
1168 ecb_function_ double ecb_binary64_to_double (uint64_t x) ecb_const;
1169 ecb_function_ double
1170 ecb_binary64_to_double (uint64_t x)
1171 {
1172 double r;
1173
1174 #if ECB_STDFP
1175 memcpy (&r, &x, 8);
1176 #else
1177 /* emulation, only works for normals and subnormals and +0 */
1178 int neg = x >> 63;
1179 int e = (x >> 52) & 0x7ffU;
1180
1181 x &= 0xfffffffffffffU;
1182
1183 if (e)
1184 x |= 0x10000000000000U;
1185 else
1186 e = 1;
1187
1188 /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */
1189 r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022);
1190
1191 r = neg ? -r : r;
1192 #endif
1193
1194 return r;
1195 }
1196
1197#endif
1198
1199#endif
1200
1201/* ECB.H END */
1202
1203#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
1204/* if your architecture doesn't need memory fences, e.g. because it is
1205 * single-cpu/core, or if you use libev in a project that doesn't use libev
1206 * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling
1207 * libev, in which cases the memory fences become nops.
1208 * alternatively, you can remove this #error and link against libpthread,
1209 * which will then provide the memory fences.
1210 */
1211# error "memory fences not defined for your architecture, please report"
1212#endif
1213
1214#ifndef ECB_MEMORY_FENCE
1215# define ECB_MEMORY_FENCE do { } while (0)
1216# define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
1217# define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
1218#endif
1219
1220#define expect_false(cond) ecb_expect_false (cond)
1221#define expect_true(cond) ecb_expect_true (cond)
1222#define noinline ecb_noinline
1223
488#define inline_size static inline 1224#define inline_size ecb_inline
489 1225
490#if EV_FEATURE_CODE 1226#if EV_FEATURE_CODE
491# define inline_speed static inline 1227# define inline_speed ecb_inline
492#else 1228#else
493# define inline_speed static noinline 1229# define inline_speed static noinline
494#endif 1230#endif
495 1231
496#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 1232#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
535# include "ev_win32.c" 1271# include "ev_win32.c"
536#endif 1272#endif
537 1273
538/*****************************************************************************/ 1274/*****************************************************************************/
539 1275
1276/* define a suitable floor function (only used by periodics atm) */
1277
1278#if EV_USE_FLOOR
1279# include <math.h>
1280# define ev_floor(v) floor (v)
1281#else
1282
1283#include <float.h>
1284
1285/* a floor() replacement function, should be independent of ev_tstamp type */
1286static ev_tstamp noinline
1287ev_floor (ev_tstamp v)
1288{
1289 /* the choice of shift factor is not terribly important */
1290#if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */
1291 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.;
1292#else
1293 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.;
1294#endif
1295
1296 /* argument too large for an unsigned long? */
1297 if (expect_false (v >= shift))
1298 {
1299 ev_tstamp f;
1300
1301 if (v == v - 1.)
1302 return v; /* very large number */
1303
1304 f = shift * ev_floor (v * (1. / shift));
1305 return f + ev_floor (v - f);
1306 }
1307
1308 /* special treatment for negative args? */
1309 if (expect_false (v < 0.))
1310 {
1311 ev_tstamp f = -ev_floor (-v);
1312
1313 return f - (f == v ? 0 : 1);
1314 }
1315
1316 /* fits into an unsigned long */
1317 return (unsigned long)v;
1318}
1319
1320#endif
1321
1322/*****************************************************************************/
1323
1324#ifdef __linux
1325# include <sys/utsname.h>
1326#endif
1327
1328static unsigned int noinline ecb_cold
1329ev_linux_version (void)
1330{
1331#ifdef __linux
1332 unsigned int v = 0;
1333 struct utsname buf;
1334 int i;
1335 char *p = buf.release;
1336
1337 if (uname (&buf))
1338 return 0;
1339
1340 for (i = 3+1; --i; )
1341 {
1342 unsigned int c = 0;
1343
1344 for (;;)
1345 {
1346 if (*p >= '0' && *p <= '9')
1347 c = c * 10 + *p++ - '0';
1348 else
1349 {
1350 p += *p == '.';
1351 break;
1352 }
1353 }
1354
1355 v = (v << 8) | c;
1356 }
1357
1358 return v;
1359#else
1360 return 0;
1361#endif
1362}
1363
1364/*****************************************************************************/
1365
540#if EV_AVOID_STDIO 1366#if EV_AVOID_STDIO
541static void noinline 1367static void noinline ecb_cold
542ev_printerr (const char *msg) 1368ev_printerr (const char *msg)
543{ 1369{
544 write (STDERR_FILENO, msg, strlen (msg)); 1370 write (STDERR_FILENO, msg, strlen (msg));
545} 1371}
546#endif 1372#endif
547 1373
548static void (*syserr_cb)(const char *msg); 1374static void (*syserr_cb)(const char *msg) EV_THROW;
549 1375
550void 1376void ecb_cold
551ev_set_syserr_cb (void (*cb)(const char *msg)) 1377ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW
552{ 1378{
553 syserr_cb = cb; 1379 syserr_cb = cb;
554} 1380}
555 1381
556static void noinline 1382static void noinline ecb_cold
557ev_syserr (const char *msg) 1383ev_syserr (const char *msg)
558{ 1384{
559 if (!msg) 1385 if (!msg)
560 msg = "(libev) system error"; 1386 msg = "(libev) system error";
561 1387
562 if (syserr_cb) 1388 if (syserr_cb)
563 syserr_cb (msg); 1389 syserr_cb (msg);
564 else 1390 else
565 { 1391 {
566#if EV_AVOID_STDIO 1392#if EV_AVOID_STDIO
567 const char *err = strerror (errno);
568
569 ev_printerr (msg); 1393 ev_printerr (msg);
570 ev_printerr (": "); 1394 ev_printerr (": ");
571 ev_printerr (err); 1395 ev_printerr (strerror (errno));
572 ev_printerr ("\n"); 1396 ev_printerr ("\n");
573#else 1397#else
574 perror (msg); 1398 perror (msg);
575#endif 1399#endif
576 abort (); 1400 abort ();
577 } 1401 }
578} 1402}
579 1403
580static void * 1404static void *
581ev_realloc_emul (void *ptr, long size) 1405ev_realloc_emul (void *ptr, long size) EV_THROW
582{ 1406{
583#if __GLIBC__
584 return realloc (ptr, size);
585#else
586 /* some systems, notably openbsd and darwin, fail to properly 1407 /* some systems, notably openbsd and darwin, fail to properly
587 * implement realloc (x, 0) (as required by both ansi c-89 and 1408 * implement realloc (x, 0) (as required by both ansi c-89 and
588 * the single unix specification, so work around them here. 1409 * the single unix specification, so work around them here.
1410 * recently, also (at least) fedora and debian started breaking it,
1411 * despite documenting it otherwise.
589 */ 1412 */
590 1413
591 if (size) 1414 if (size)
592 return realloc (ptr, size); 1415 return realloc (ptr, size);
593 1416
594 free (ptr); 1417 free (ptr);
595 return 0; 1418 return 0;
596#endif
597} 1419}
598 1420
599static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 1421static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
600 1422
601void 1423void ecb_cold
602ev_set_allocator (void *(*cb)(void *ptr, long size)) 1424ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW
603{ 1425{
604 alloc = cb; 1426 alloc = cb;
605} 1427}
606 1428
607inline_speed void * 1429inline_speed void *
610 ptr = alloc (ptr, size); 1432 ptr = alloc (ptr, size);
611 1433
612 if (!ptr && size) 1434 if (!ptr && size)
613 { 1435 {
614#if EV_AVOID_STDIO 1436#if EV_AVOID_STDIO
615 ev_printerr ("libev: memory allocation failed, aborting.\n"); 1437 ev_printerr ("(libev) memory allocation failed, aborting.\n");
616#else 1438#else
617 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 1439 fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size);
618#endif 1440#endif
619 abort (); 1441 abort ();
620 } 1442 }
621 1443
622 return ptr; 1444 return ptr;
639 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 1461 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
640 unsigned char unused; 1462 unsigned char unused;
641#if EV_USE_EPOLL 1463#if EV_USE_EPOLL
642 unsigned int egen; /* generation counter to counter epoll bugs */ 1464 unsigned int egen; /* generation counter to counter epoll bugs */
643#endif 1465#endif
644#if EV_SELECT_IS_WINSOCKET 1466#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
645 SOCKET handle; 1467 SOCKET handle;
1468#endif
1469#if EV_USE_IOCP
1470 OVERLAPPED or, ow;
646#endif 1471#endif
647} ANFD; 1472} ANFD;
648 1473
649/* stores the pending event set for a given watcher */ 1474/* stores the pending event set for a given watcher */
650typedef struct 1475typedef struct
692 #undef VAR 1517 #undef VAR
693 }; 1518 };
694 #include "ev_wrap.h" 1519 #include "ev_wrap.h"
695 1520
696 static struct ev_loop default_loop_struct; 1521 static struct ev_loop default_loop_struct;
697 struct ev_loop *ev_default_loop_ptr; 1522 EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */
698 1523
699#else 1524#else
700 1525
701 ev_tstamp ev_rt_now; 1526 EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */
702 #define VAR(name,decl) static decl; 1527 #define VAR(name,decl) static decl;
703 #include "ev_vars.h" 1528 #include "ev_vars.h"
704 #undef VAR 1529 #undef VAR
705 1530
706 static int ev_default_loop_ptr; 1531 static int ev_default_loop_ptr;
715# define EV_RELEASE_CB (void)0 1540# define EV_RELEASE_CB (void)0
716# define EV_ACQUIRE_CB (void)0 1541# define EV_ACQUIRE_CB (void)0
717# define EV_INVOKE_PENDING ev_invoke_pending (EV_A) 1542# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
718#endif 1543#endif
719 1544
720#define EVUNLOOP_RECURSE 0x80 1545#define EVBREAK_RECURSE 0x80
721 1546
722/*****************************************************************************/ 1547/*****************************************************************************/
723 1548
724#ifndef EV_HAVE_EV_TIME 1549#ifndef EV_HAVE_EV_TIME
725ev_tstamp 1550ev_tstamp
726ev_time (void) 1551ev_time (void) EV_THROW
727{ 1552{
728#if EV_USE_REALTIME 1553#if EV_USE_REALTIME
729 if (expect_true (have_realtime)) 1554 if (expect_true (have_realtime))
730 { 1555 {
731 struct timespec ts; 1556 struct timespec ts;
755 return ev_time (); 1580 return ev_time ();
756} 1581}
757 1582
758#if EV_MULTIPLICITY 1583#if EV_MULTIPLICITY
759ev_tstamp 1584ev_tstamp
760ev_now (EV_P) 1585ev_now (EV_P) EV_THROW
761{ 1586{
762 return ev_rt_now; 1587 return ev_rt_now;
763} 1588}
764#endif 1589#endif
765 1590
766void 1591void
767ev_sleep (ev_tstamp delay) 1592ev_sleep (ev_tstamp delay) EV_THROW
768{ 1593{
769 if (delay > 0.) 1594 if (delay > 0.)
770 { 1595 {
771#if EV_USE_NANOSLEEP 1596#if EV_USE_NANOSLEEP
772 struct timespec ts; 1597 struct timespec ts;
773 1598
774 ts.tv_sec = (time_t)delay; 1599 EV_TS_SET (ts, delay);
775 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
776
777 nanosleep (&ts, 0); 1600 nanosleep (&ts, 0);
778#elif defined(_WIN32) 1601#elif defined _WIN32
779 Sleep ((unsigned long)(delay * 1e3)); 1602 Sleep ((unsigned long)(delay * 1e3));
780#else 1603#else
781 struct timeval tv; 1604 struct timeval tv;
782 1605
783 tv.tv_sec = (time_t)delay;
784 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
785
786 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 1606 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
787 /* something not guaranteed by newer posix versions, but guaranteed */ 1607 /* something not guaranteed by newer posix versions, but guaranteed */
788 /* by older ones */ 1608 /* by older ones */
1609 EV_TV_SET (tv, delay);
789 select (0, 0, 0, 0, &tv); 1610 select (0, 0, 0, 0, &tv);
790#endif 1611#endif
791 } 1612 }
792} 1613}
793 1614
804 1625
805 do 1626 do
806 ncur <<= 1; 1627 ncur <<= 1;
807 while (cnt > ncur); 1628 while (cnt > ncur);
808 1629
809 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ 1630 /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */
810 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) 1631 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
811 { 1632 {
812 ncur *= elem; 1633 ncur *= elem;
813 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); 1634 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
814 ncur = ncur - sizeof (void *) * 4; 1635 ncur = ncur - sizeof (void *) * 4;
816 } 1637 }
817 1638
818 return ncur; 1639 return ncur;
819} 1640}
820 1641
821static noinline void * 1642static void * noinline ecb_cold
822array_realloc (int elem, void *base, int *cur, int cnt) 1643array_realloc (int elem, void *base, int *cur, int cnt)
823{ 1644{
824 *cur = array_nextsize (elem, *cur, cnt); 1645 *cur = array_nextsize (elem, *cur, cnt);
825 return ev_realloc (base, elem * *cur); 1646 return ev_realloc (base, elem * *cur);
826} 1647}
829 memset ((void *)(base), 0, sizeof (*(base)) * (count)) 1650 memset ((void *)(base), 0, sizeof (*(base)) * (count))
830 1651
831#define array_needsize(type,base,cur,cnt,init) \ 1652#define array_needsize(type,base,cur,cnt,init) \
832 if (expect_false ((cnt) > (cur))) \ 1653 if (expect_false ((cnt) > (cur))) \
833 { \ 1654 { \
834 int ocur_ = (cur); \ 1655 int ecb_unused ocur_ = (cur); \
835 (base) = (type *)array_realloc \ 1656 (base) = (type *)array_realloc \
836 (sizeof (type), (base), &(cur), (cnt)); \ 1657 (sizeof (type), (base), &(cur), (cnt)); \
837 init ((base) + (ocur_), (cur) - ocur_); \ 1658 init ((base) + (ocur_), (cur) - ocur_); \
838 } 1659 }
839 1660
857pendingcb (EV_P_ ev_prepare *w, int revents) 1678pendingcb (EV_P_ ev_prepare *w, int revents)
858{ 1679{
859} 1680}
860 1681
861void noinline 1682void noinline
862ev_feed_event (EV_P_ void *w, int revents) 1683ev_feed_event (EV_P_ void *w, int revents) EV_THROW
863{ 1684{
864 W w_ = (W)w; 1685 W w_ = (W)w;
865 int pri = ABSPRI (w_); 1686 int pri = ABSPRI (w_);
866 1687
867 if (expect_false (w_->pending)) 1688 if (expect_false (w_->pending))
871 w_->pending = ++pendingcnt [pri]; 1692 w_->pending = ++pendingcnt [pri];
872 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); 1693 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
873 pendings [pri][w_->pending - 1].w = w_; 1694 pendings [pri][w_->pending - 1].w = w_;
874 pendings [pri][w_->pending - 1].events = revents; 1695 pendings [pri][w_->pending - 1].events = revents;
875 } 1696 }
1697
1698 pendingpri = NUMPRI - 1;
876} 1699}
877 1700
878inline_speed void 1701inline_speed void
879feed_reverse (EV_P_ W w) 1702feed_reverse (EV_P_ W w)
880{ 1703{
926 if (expect_true (!anfd->reify)) 1749 if (expect_true (!anfd->reify))
927 fd_event_nocheck (EV_A_ fd, revents); 1750 fd_event_nocheck (EV_A_ fd, revents);
928} 1751}
929 1752
930void 1753void
931ev_feed_fd_event (EV_P_ int fd, int revents) 1754ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW
932{ 1755{
933 if (fd >= 0 && fd < anfdmax) 1756 if (fd >= 0 && fd < anfdmax)
934 fd_event_nocheck (EV_A_ fd, revents); 1757 fd_event_nocheck (EV_A_ fd, revents);
935} 1758}
936 1759
939inline_size void 1762inline_size void
940fd_reify (EV_P) 1763fd_reify (EV_P)
941{ 1764{
942 int i; 1765 int i;
943 1766
1767#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
1768 for (i = 0; i < fdchangecnt; ++i)
1769 {
1770 int fd = fdchanges [i];
1771 ANFD *anfd = anfds + fd;
1772
1773 if (anfd->reify & EV__IOFDSET && anfd->head)
1774 {
1775 SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd);
1776
1777 if (handle != anfd->handle)
1778 {
1779 unsigned long arg;
1780
1781 assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0));
1782
1783 /* handle changed, but fd didn't - we need to do it in two steps */
1784 backend_modify (EV_A_ fd, anfd->events, 0);
1785 anfd->events = 0;
1786 anfd->handle = handle;
1787 }
1788 }
1789 }
1790#endif
1791
944 for (i = 0; i < fdchangecnt; ++i) 1792 for (i = 0; i < fdchangecnt; ++i)
945 { 1793 {
946 int fd = fdchanges [i]; 1794 int fd = fdchanges [i];
947 ANFD *anfd = anfds + fd; 1795 ANFD *anfd = anfds + fd;
948 ev_io *w; 1796 ev_io *w;
949 1797
950 unsigned char events = 0; 1798 unsigned char o_events = anfd->events;
1799 unsigned char o_reify = anfd->reify;
951 1800
952 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 1801 anfd->reify = 0;
953 events |= (unsigned char)w->events;
954 1802
955#if EV_SELECT_IS_WINSOCKET 1803 /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
956 if (events)
957 { 1804 {
958 unsigned long arg; 1805 anfd->events = 0;
959 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 1806
960 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); 1807 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
1808 anfd->events |= (unsigned char)w->events;
1809
1810 if (o_events != anfd->events)
1811 o_reify = EV__IOFDSET; /* actually |= */
961 } 1812 }
962#endif
963 1813
964 { 1814 if (o_reify & EV__IOFDSET)
965 unsigned char o_events = anfd->events;
966 unsigned char o_reify = anfd->reify;
967
968 anfd->reify = 0;
969 anfd->events = events;
970
971 if (o_events != events || o_reify & EV__IOFDSET)
972 backend_modify (EV_A_ fd, o_events, events); 1815 backend_modify (EV_A_ fd, o_events, anfd->events);
973 }
974 } 1816 }
975 1817
976 fdchangecnt = 0; 1818 fdchangecnt = 0;
977} 1819}
978 1820
990 fdchanges [fdchangecnt - 1] = fd; 1832 fdchanges [fdchangecnt - 1] = fd;
991 } 1833 }
992} 1834}
993 1835
994/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ 1836/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
995inline_speed void 1837inline_speed void ecb_cold
996fd_kill (EV_P_ int fd) 1838fd_kill (EV_P_ int fd)
997{ 1839{
998 ev_io *w; 1840 ev_io *w;
999 1841
1000 while ((w = (ev_io *)anfds [fd].head)) 1842 while ((w = (ev_io *)anfds [fd].head))
1003 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 1845 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
1004 } 1846 }
1005} 1847}
1006 1848
1007/* check whether the given fd is actually valid, for error recovery */ 1849/* check whether the given fd is actually valid, for error recovery */
1008inline_size int 1850inline_size int ecb_cold
1009fd_valid (int fd) 1851fd_valid (int fd)
1010{ 1852{
1011#ifdef _WIN32 1853#ifdef _WIN32
1012 return EV_FD_TO_WIN32_HANDLE (fd) != -1; 1854 return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1013#else 1855#else
1014 return fcntl (fd, F_GETFD) != -1; 1856 return fcntl (fd, F_GETFD) != -1;
1015#endif 1857#endif
1016} 1858}
1017 1859
1018/* called on EBADF to verify fds */ 1860/* called on EBADF to verify fds */
1019static void noinline 1861static void noinline ecb_cold
1020fd_ebadf (EV_P) 1862fd_ebadf (EV_P)
1021{ 1863{
1022 int fd; 1864 int fd;
1023 1865
1024 for (fd = 0; fd < anfdmax; ++fd) 1866 for (fd = 0; fd < anfdmax; ++fd)
1026 if (!fd_valid (fd) && errno == EBADF) 1868 if (!fd_valid (fd) && errno == EBADF)
1027 fd_kill (EV_A_ fd); 1869 fd_kill (EV_A_ fd);
1028} 1870}
1029 1871
1030/* called on ENOMEM in select/poll to kill some fds and retry */ 1872/* called on ENOMEM in select/poll to kill some fds and retry */
1031static void noinline 1873static void noinline ecb_cold
1032fd_enomem (EV_P) 1874fd_enomem (EV_P)
1033{ 1875{
1034 int fd; 1876 int fd;
1035 1877
1036 for (fd = anfdmax; fd--; ) 1878 for (fd = anfdmax; fd--; )
1231 2073
1232/*****************************************************************************/ 2074/*****************************************************************************/
1233 2075
1234#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE 2076#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1235 2077
1236static void noinline 2078static void noinline ecb_cold
1237evpipe_init (EV_P) 2079evpipe_init (EV_P)
1238{ 2080{
1239 if (!ev_is_active (&pipe_w)) 2081 if (!ev_is_active (&pipe_w))
1240 { 2082 {
2083 int fds [2];
2084
1241# if EV_USE_EVENTFD 2085# if EV_USE_EVENTFD
2086 fds [0] = -1;
1242 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 2087 fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1243 if (evfd < 0 && errno == EINVAL) 2088 if (fds [1] < 0 && errno == EINVAL)
1244 evfd = eventfd (0, 0); 2089 fds [1] = eventfd (0, 0);
1245 2090
1246 if (evfd >= 0) 2091 if (fds [1] < 0)
2092# endif
1247 { 2093 {
2094 while (pipe (fds))
2095 ev_syserr ("(libev) error creating signal/async pipe");
2096
2097 fd_intern (fds [0]);
2098 }
2099
2100 fd_intern (fds [1]);
2101
1248 evpipe [0] = -1; 2102 evpipe [0] = fds [0];
1249 fd_intern (evfd); /* doing it twice doesn't hurt */ 2103
1250 ev_io_set (&pipe_w, evfd, EV_READ); 2104 if (evpipe [1] < 0)
2105 evpipe [1] = fds [1]; /* first call, set write fd */
2106 else
2107 {
2108 /* on subsequent calls, do not change evpipe [1] */
2109 /* so that evpipe_write can always rely on its value. */
2110 /* this branch does not do anything sensible on windows, */
2111 /* so must not be executed on windows */
2112
2113 dup2 (fds [1], evpipe [1]);
2114 close (fds [1]);
2115 }
2116
2117 ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ);
2118 ev_io_start (EV_A_ &pipe_w);
2119 ev_unref (EV_A); /* watcher should not keep loop alive */
2120 }
2121}
2122
2123inline_speed void
2124evpipe_write (EV_P_ EV_ATOMIC_T *flag)
2125{
2126 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
2127
2128 if (expect_true (*flag))
2129 return;
2130
2131 *flag = 1;
2132 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
2133
2134 pipe_write_skipped = 1;
2135
2136 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
2137
2138 if (pipe_write_wanted)
2139 {
2140 int old_errno;
2141
2142 pipe_write_skipped = 0;
2143 ECB_MEMORY_FENCE_RELEASE;
2144
2145 old_errno = errno; /* save errno because write will clobber it */
2146
2147#if EV_USE_EVENTFD
2148 if (evpipe [0] < 0)
2149 {
2150 uint64_t counter = 1;
2151 write (evpipe [1], &counter, sizeof (uint64_t));
1251 } 2152 }
1252 else 2153 else
1253# endif 2154#endif
1254 { 2155 {
1255 while (pipe (evpipe)) 2156#ifdef _WIN32
1256 ev_syserr ("(libev) error creating signal/async pipe"); 2157 WSABUF buf;
1257 2158 DWORD sent;
1258 fd_intern (evpipe [0]); 2159 buf.buf = &buf;
1259 fd_intern (evpipe [1]); 2160 buf.len = 1;
1260 ev_io_set (&pipe_w, evpipe [0], EV_READ); 2161 WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0);
2162#else
2163 write (evpipe [1], &(evpipe [1]), 1);
2164#endif
1261 } 2165 }
1262
1263 ev_io_start (EV_A_ &pipe_w);
1264 ev_unref (EV_A); /* watcher should not keep loop alive */
1265 }
1266}
1267
1268inline_size void
1269evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1270{
1271 if (!*flag)
1272 {
1273 int old_errno = errno; /* save errno because write might clobber it */
1274 char dummy;
1275
1276 *flag = 1;
1277
1278#if EV_USE_EVENTFD
1279 if (evfd >= 0)
1280 {
1281 uint64_t counter = 1;
1282 write (evfd, &counter, sizeof (uint64_t));
1283 }
1284 else
1285#endif
1286 write (evpipe [1], &dummy, 1);
1287 2166
1288 errno = old_errno; 2167 errno = old_errno;
1289 } 2168 }
1290} 2169}
1291 2170
1294static void 2173static void
1295pipecb (EV_P_ ev_io *iow, int revents) 2174pipecb (EV_P_ ev_io *iow, int revents)
1296{ 2175{
1297 int i; 2176 int i;
1298 2177
2178 if (revents & EV_READ)
2179 {
1299#if EV_USE_EVENTFD 2180#if EV_USE_EVENTFD
1300 if (evfd >= 0) 2181 if (evpipe [0] < 0)
1301 { 2182 {
1302 uint64_t counter; 2183 uint64_t counter;
1303 read (evfd, &counter, sizeof (uint64_t)); 2184 read (evpipe [1], &counter, sizeof (uint64_t));
1304 } 2185 }
1305 else 2186 else
1306#endif 2187#endif
1307 { 2188 {
1308 char dummy; 2189 char dummy[4];
2190#ifdef _WIN32
2191 WSABUF buf;
2192 DWORD recvd;
2193 DWORD flags = 0;
2194 buf.buf = dummy;
2195 buf.len = sizeof (dummy);
2196 WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0);
2197#else
1309 read (evpipe [0], &dummy, 1); 2198 read (evpipe [0], &dummy, sizeof (dummy));
2199#endif
2200 }
1310 } 2201 }
1311 2202
2203 pipe_write_skipped = 0;
2204
2205 ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
2206
2207#if EV_SIGNAL_ENABLE
1312 if (sig_pending) 2208 if (sig_pending)
1313 { 2209 {
1314 sig_pending = 0; 2210 sig_pending = 0;
2211
2212 ECB_MEMORY_FENCE;
1315 2213
1316 for (i = EV_NSIG - 1; i--; ) 2214 for (i = EV_NSIG - 1; i--; )
1317 if (expect_false (signals [i].pending)) 2215 if (expect_false (signals [i].pending))
1318 ev_feed_signal_event (EV_A_ i + 1); 2216 ev_feed_signal_event (EV_A_ i + 1);
1319 } 2217 }
2218#endif
1320 2219
1321#if EV_ASYNC_ENABLE 2220#if EV_ASYNC_ENABLE
1322 if (async_pending) 2221 if (async_pending)
1323 { 2222 {
1324 async_pending = 0; 2223 async_pending = 0;
2224
2225 ECB_MEMORY_FENCE;
1325 2226
1326 for (i = asynccnt; i--; ) 2227 for (i = asynccnt; i--; )
1327 if (asyncs [i]->sent) 2228 if (asyncs [i]->sent)
1328 { 2229 {
1329 asyncs [i]->sent = 0; 2230 asyncs [i]->sent = 0;
2231 ECB_MEMORY_FENCE_RELEASE;
1330 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); 2232 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
1331 } 2233 }
1332 } 2234 }
1333#endif 2235#endif
1334} 2236}
1335 2237
1336/*****************************************************************************/ 2238/*****************************************************************************/
1337 2239
2240void
2241ev_feed_signal (int signum) EV_THROW
2242{
2243#if EV_MULTIPLICITY
2244 EV_P;
2245 ECB_MEMORY_FENCE_ACQUIRE;
2246 EV_A = signals [signum - 1].loop;
2247
2248 if (!EV_A)
2249 return;
2250#endif
2251
2252 signals [signum - 1].pending = 1;
2253 evpipe_write (EV_A_ &sig_pending);
2254}
2255
1338static void 2256static void
1339ev_sighandler (int signum) 2257ev_sighandler (int signum)
1340{ 2258{
1341#if EV_MULTIPLICITY
1342 EV_P = signals [signum - 1].loop;
1343#endif
1344
1345#ifdef _WIN32 2259#ifdef _WIN32
1346 signal (signum, ev_sighandler); 2260 signal (signum, ev_sighandler);
1347#endif 2261#endif
1348 2262
1349 signals [signum - 1].pending = 1; 2263 ev_feed_signal (signum);
1350 evpipe_write (EV_A_ &sig_pending);
1351} 2264}
1352 2265
1353void noinline 2266void noinline
1354ev_feed_signal_event (EV_P_ int signum) 2267ev_feed_signal_event (EV_P_ int signum) EV_THROW
1355{ 2268{
1356 WL w; 2269 WL w;
1357 2270
1358 if (expect_false (signum <= 0 || signum > EV_NSIG)) 2271 if (expect_false (signum <= 0 || signum >= EV_NSIG))
1359 return; 2272 return;
1360 2273
1361 --signum; 2274 --signum;
1362 2275
1363#if EV_MULTIPLICITY 2276#if EV_MULTIPLICITY
1367 if (expect_false (signals [signum].loop != EV_A)) 2280 if (expect_false (signals [signum].loop != EV_A))
1368 return; 2281 return;
1369#endif 2282#endif
1370 2283
1371 signals [signum].pending = 0; 2284 signals [signum].pending = 0;
2285 ECB_MEMORY_FENCE_RELEASE;
1372 2286
1373 for (w = signals [signum].head; w; w = w->next) 2287 for (w = signals [signum].head; w; w = w->next)
1374 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 2288 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1375} 2289}
1376 2290
1455 2369
1456#endif 2370#endif
1457 2371
1458/*****************************************************************************/ 2372/*****************************************************************************/
1459 2373
2374#if EV_USE_IOCP
2375# include "ev_iocp.c"
2376#endif
1460#if EV_USE_PORT 2377#if EV_USE_PORT
1461# include "ev_port.c" 2378# include "ev_port.c"
1462#endif 2379#endif
1463#if EV_USE_KQUEUE 2380#if EV_USE_KQUEUE
1464# include "ev_kqueue.c" 2381# include "ev_kqueue.c"
1471#endif 2388#endif
1472#if EV_USE_SELECT 2389#if EV_USE_SELECT
1473# include "ev_select.c" 2390# include "ev_select.c"
1474#endif 2391#endif
1475 2392
1476int 2393int ecb_cold
1477ev_version_major (void) 2394ev_version_major (void) EV_THROW
1478{ 2395{
1479 return EV_VERSION_MAJOR; 2396 return EV_VERSION_MAJOR;
1480} 2397}
1481 2398
1482int 2399int ecb_cold
1483ev_version_minor (void) 2400ev_version_minor (void) EV_THROW
1484{ 2401{
1485 return EV_VERSION_MINOR; 2402 return EV_VERSION_MINOR;
1486} 2403}
1487 2404
1488/* return true if we are running with elevated privileges and should ignore env variables */ 2405/* return true if we are running with elevated privileges and should ignore env variables */
1489int inline_size 2406int inline_size ecb_cold
1490enable_secure (void) 2407enable_secure (void)
1491{ 2408{
1492#ifdef _WIN32 2409#ifdef _WIN32
1493 return 0; 2410 return 0;
1494#else 2411#else
1495 return getuid () != geteuid () 2412 return getuid () != geteuid ()
1496 || getgid () != getegid (); 2413 || getgid () != getegid ();
1497#endif 2414#endif
1498} 2415}
1499 2416
1500unsigned int 2417unsigned int ecb_cold
1501ev_supported_backends (void) 2418ev_supported_backends (void) EV_THROW
1502{ 2419{
1503 unsigned int flags = 0; 2420 unsigned int flags = 0;
1504 2421
1505 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2422 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
1506 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2423 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
1509 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; 2426 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
1510 2427
1511 return flags; 2428 return flags;
1512} 2429}
1513 2430
1514unsigned int 2431unsigned int ecb_cold
1515ev_recommended_backends (void) 2432ev_recommended_backends (void) EV_THROW
1516{ 2433{
1517 unsigned int flags = ev_supported_backends (); 2434 unsigned int flags = ev_supported_backends ();
1518 2435
1519#ifndef __NetBSD__ 2436#ifndef __NetBSD__
1520 /* kqueue is borked on everything but netbsd apparently */ 2437 /* kqueue is borked on everything but netbsd apparently */
1531#endif 2448#endif
1532 2449
1533 return flags; 2450 return flags;
1534} 2451}
1535 2452
2453unsigned int ecb_cold
2454ev_embeddable_backends (void) EV_THROW
2455{
2456 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2457
2458 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
2459 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2460 flags &= ~EVBACKEND_EPOLL;
2461
2462 return flags;
2463}
2464
1536unsigned int 2465unsigned int
1537ev_embeddable_backends (void)
1538{
1539 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
1540
1541 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
1542 /* please fix it and tell me how to detect the fix */
1543 flags &= ~EVBACKEND_EPOLL;
1544
1545 return flags;
1546}
1547
1548unsigned int
1549ev_backend (EV_P) 2466ev_backend (EV_P) EV_THROW
1550{ 2467{
1551 return backend; 2468 return backend;
1552} 2469}
1553 2470
1554#if EV_FEATURE_API 2471#if EV_FEATURE_API
1555unsigned int 2472unsigned int
1556ev_iteration (EV_P) 2473ev_iteration (EV_P) EV_THROW
1557{ 2474{
1558 return loop_count; 2475 return loop_count;
1559} 2476}
1560 2477
1561unsigned int 2478unsigned int
1562ev_depth (EV_P) 2479ev_depth (EV_P) EV_THROW
1563{ 2480{
1564 return loop_depth; 2481 return loop_depth;
1565} 2482}
1566 2483
1567void 2484void
1568ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 2485ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
1569{ 2486{
1570 io_blocktime = interval; 2487 io_blocktime = interval;
1571} 2488}
1572 2489
1573void 2490void
1574ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 2491ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
1575{ 2492{
1576 timeout_blocktime = interval; 2493 timeout_blocktime = interval;
1577} 2494}
1578 2495
1579void 2496void
1580ev_set_userdata (EV_P_ void *data) 2497ev_set_userdata (EV_P_ void *data) EV_THROW
1581{ 2498{
1582 userdata = data; 2499 userdata = data;
1583} 2500}
1584 2501
1585void * 2502void *
1586ev_userdata (EV_P) 2503ev_userdata (EV_P) EV_THROW
1587{ 2504{
1588 return userdata; 2505 return userdata;
1589} 2506}
1590 2507
2508void
1591void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 2509ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) EV_THROW
1592{ 2510{
1593 invoke_cb = invoke_pending_cb; 2511 invoke_cb = invoke_pending_cb;
1594} 2512}
1595 2513
2514void
1596void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) 2515ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW
1597{ 2516{
1598 release_cb = release; 2517 release_cb = release;
1599 acquire_cb = acquire; 2518 acquire_cb = acquire;
1600} 2519}
1601#endif 2520#endif
1602 2521
1603/* initialise a loop structure, must be zero-initialised */ 2522/* initialise a loop structure, must be zero-initialised */
1604static void noinline 2523static void noinline ecb_cold
1605loop_init (EV_P_ unsigned int flags) 2524loop_init (EV_P_ unsigned int flags) EV_THROW
1606{ 2525{
1607 if (!backend) 2526 if (!backend)
1608 { 2527 {
2528 origflags = flags;
2529
1609#if EV_USE_REALTIME 2530#if EV_USE_REALTIME
1610 if (!have_realtime) 2531 if (!have_realtime)
1611 { 2532 {
1612 struct timespec ts; 2533 struct timespec ts;
1613 2534
1635 if (!(flags & EVFLAG_NOENV) 2556 if (!(flags & EVFLAG_NOENV)
1636 && !enable_secure () 2557 && !enable_secure ()
1637 && getenv ("LIBEV_FLAGS")) 2558 && getenv ("LIBEV_FLAGS"))
1638 flags = atoi (getenv ("LIBEV_FLAGS")); 2559 flags = atoi (getenv ("LIBEV_FLAGS"));
1639 2560
1640 ev_rt_now = ev_time (); 2561 ev_rt_now = ev_time ();
1641 mn_now = get_clock (); 2562 mn_now = get_clock ();
1642 now_floor = mn_now; 2563 now_floor = mn_now;
1643 rtmn_diff = ev_rt_now - mn_now; 2564 rtmn_diff = ev_rt_now - mn_now;
1644#if EV_FEATURE_API 2565#if EV_FEATURE_API
1645 invoke_cb = ev_invoke_pending; 2566 invoke_cb = ev_invoke_pending;
1646#endif 2567#endif
1647 2568
1648 io_blocktime = 0.; 2569 io_blocktime = 0.;
1649 timeout_blocktime = 0.; 2570 timeout_blocktime = 0.;
1650 backend = 0; 2571 backend = 0;
1651 backend_fd = -1; 2572 backend_fd = -1;
1652 sig_pending = 0; 2573 sig_pending = 0;
1653#if EV_ASYNC_ENABLE 2574#if EV_ASYNC_ENABLE
1654 async_pending = 0; 2575 async_pending = 0;
1655#endif 2576#endif
2577 pipe_write_skipped = 0;
2578 pipe_write_wanted = 0;
2579 evpipe [0] = -1;
2580 evpipe [1] = -1;
1656#if EV_USE_INOTIFY 2581#if EV_USE_INOTIFY
1657 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 2582 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1658#endif 2583#endif
1659#if EV_USE_SIGNALFD 2584#if EV_USE_SIGNALFD
1660 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 2585 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
1661#endif 2586#endif
1662 2587
1663 if (!(flags & 0x0000ffffU)) 2588 if (!(flags & EVBACKEND_MASK))
1664 flags |= ev_recommended_backends (); 2589 flags |= ev_recommended_backends ();
1665 2590
2591#if EV_USE_IOCP
2592 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
2593#endif
1666#if EV_USE_PORT 2594#if EV_USE_PORT
1667 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 2595 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1668#endif 2596#endif
1669#if EV_USE_KQUEUE 2597#if EV_USE_KQUEUE
1670 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); 2598 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
1687#endif 2615#endif
1688 } 2616 }
1689} 2617}
1690 2618
1691/* free up a loop structure */ 2619/* free up a loop structure */
1692static void noinline 2620void ecb_cold
1693loop_destroy (EV_P) 2621ev_loop_destroy (EV_P)
1694{ 2622{
1695 int i; 2623 int i;
2624
2625#if EV_MULTIPLICITY
2626 /* mimic free (0) */
2627 if (!EV_A)
2628 return;
2629#endif
2630
2631#if EV_CLEANUP_ENABLE
2632 /* queue cleanup watchers (and execute them) */
2633 if (expect_false (cleanupcnt))
2634 {
2635 queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP);
2636 EV_INVOKE_PENDING;
2637 }
2638#endif
2639
2640#if EV_CHILD_ENABLE
2641 if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
2642 {
2643 ev_ref (EV_A); /* child watcher */
2644 ev_signal_stop (EV_A_ &childev);
2645 }
2646#endif
1696 2647
1697 if (ev_is_active (&pipe_w)) 2648 if (ev_is_active (&pipe_w))
1698 { 2649 {
1699 /*ev_ref (EV_A);*/ 2650 /*ev_ref (EV_A);*/
1700 /*ev_io_stop (EV_A_ &pipe_w);*/ 2651 /*ev_io_stop (EV_A_ &pipe_w);*/
1701 2652
1702#if EV_USE_EVENTFD
1703 if (evfd >= 0)
1704 close (evfd);
1705#endif
1706
1707 if (evpipe [0] >= 0)
1708 {
1709 EV_WIN32_CLOSE_FD (evpipe [0]); 2653 if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]);
1710 EV_WIN32_CLOSE_FD (evpipe [1]); 2654 if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]);
1711 }
1712 } 2655 }
1713 2656
1714#if EV_USE_SIGNALFD 2657#if EV_USE_SIGNALFD
1715 if (ev_is_active (&sigfd_w)) 2658 if (ev_is_active (&sigfd_w))
1716 close (sigfd); 2659 close (sigfd);
1722#endif 2665#endif
1723 2666
1724 if (backend_fd >= 0) 2667 if (backend_fd >= 0)
1725 close (backend_fd); 2668 close (backend_fd);
1726 2669
2670#if EV_USE_IOCP
2671 if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
2672#endif
1727#if EV_USE_PORT 2673#if EV_USE_PORT
1728 if (backend == EVBACKEND_PORT ) port_destroy (EV_A); 2674 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
1729#endif 2675#endif
1730#if EV_USE_KQUEUE 2676#if EV_USE_KQUEUE
1731 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); 2677 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
1758 array_free (periodic, EMPTY); 2704 array_free (periodic, EMPTY);
1759#endif 2705#endif
1760#if EV_FORK_ENABLE 2706#if EV_FORK_ENABLE
1761 array_free (fork, EMPTY); 2707 array_free (fork, EMPTY);
1762#endif 2708#endif
2709#if EV_CLEANUP_ENABLE
2710 array_free (cleanup, EMPTY);
2711#endif
1763 array_free (prepare, EMPTY); 2712 array_free (prepare, EMPTY);
1764 array_free (check, EMPTY); 2713 array_free (check, EMPTY);
1765#if EV_ASYNC_ENABLE 2714#if EV_ASYNC_ENABLE
1766 array_free (async, EMPTY); 2715 array_free (async, EMPTY);
1767#endif 2716#endif
1768 2717
1769 backend = 0; 2718 backend = 0;
2719
2720#if EV_MULTIPLICITY
2721 if (ev_is_default_loop (EV_A))
2722#endif
2723 ev_default_loop_ptr = 0;
2724#if EV_MULTIPLICITY
2725 else
2726 ev_free (EV_A);
2727#endif
1770} 2728}
1771 2729
1772#if EV_USE_INOTIFY 2730#if EV_USE_INOTIFY
1773inline_size void infy_fork (EV_P); 2731inline_size void infy_fork (EV_P);
1774#endif 2732#endif
1787#endif 2745#endif
1788#if EV_USE_INOTIFY 2746#if EV_USE_INOTIFY
1789 infy_fork (EV_A); 2747 infy_fork (EV_A);
1790#endif 2748#endif
1791 2749
2750#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1792 if (ev_is_active (&pipe_w)) 2751 if (ev_is_active (&pipe_w))
1793 { 2752 {
1794 /* this "locks" the handlers against writing to the pipe */ 2753 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
1795 /* while we modify the fd vars */
1796 sig_pending = 1;
1797#if EV_ASYNC_ENABLE
1798 async_pending = 1;
1799#endif
1800 2754
1801 ev_ref (EV_A); 2755 ev_ref (EV_A);
1802 ev_io_stop (EV_A_ &pipe_w); 2756 ev_io_stop (EV_A_ &pipe_w);
1803 2757
1804#if EV_USE_EVENTFD
1805 if (evfd >= 0)
1806 close (evfd);
1807#endif
1808
1809 if (evpipe [0] >= 0) 2758 if (evpipe [0] >= 0)
1810 {
1811 EV_WIN32_CLOSE_FD (evpipe [0]); 2759 EV_WIN32_CLOSE_FD (evpipe [0]);
1812 EV_WIN32_CLOSE_FD (evpipe [1]);
1813 }
1814 2760
1815#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1816 evpipe_init (EV_A); 2761 evpipe_init (EV_A);
1817 /* now iterate over everything, in case we missed something */ 2762 /* iterate over everything, in case we missed something before */
1818 pipecb (EV_A_ &pipe_w, EV_READ); 2763 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
1819#endif
1820 } 2764 }
2765#endif
1821 2766
1822 postfork = 0; 2767 postfork = 0;
1823} 2768}
1824 2769
1825#if EV_MULTIPLICITY 2770#if EV_MULTIPLICITY
1826 2771
1827struct ev_loop * 2772struct ev_loop * ecb_cold
1828ev_loop_new (unsigned int flags) 2773ev_loop_new (unsigned int flags) EV_THROW
1829{ 2774{
1830 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2775 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1831 2776
1832 memset (EV_A, 0, sizeof (struct ev_loop)); 2777 memset (EV_A, 0, sizeof (struct ev_loop));
1833 loop_init (EV_A_ flags); 2778 loop_init (EV_A_ flags);
1834 2779
1835 if (ev_backend (EV_A)) 2780 if (ev_backend (EV_A))
1836 return EV_A; 2781 return EV_A;
1837 2782
2783 ev_free (EV_A);
1838 return 0; 2784 return 0;
1839} 2785}
1840 2786
1841void
1842ev_loop_destroy (EV_P)
1843{
1844 loop_destroy (EV_A);
1845 ev_free (loop);
1846}
1847
1848void
1849ev_loop_fork (EV_P)
1850{
1851 postfork = 1; /* must be in line with ev_default_fork */
1852}
1853#endif /* multiplicity */ 2787#endif /* multiplicity */
1854 2788
1855#if EV_VERIFY 2789#if EV_VERIFY
1856static void noinline 2790static void noinline ecb_cold
1857verify_watcher (EV_P_ W w) 2791verify_watcher (EV_P_ W w)
1858{ 2792{
1859 assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); 2793 assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
1860 2794
1861 if (w->pending) 2795 if (w->pending)
1862 assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); 2796 assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
1863} 2797}
1864 2798
1865static void noinline 2799static void noinline ecb_cold
1866verify_heap (EV_P_ ANHE *heap, int N) 2800verify_heap (EV_P_ ANHE *heap, int N)
1867{ 2801{
1868 int i; 2802 int i;
1869 2803
1870 for (i = HEAP0; i < N + HEAP0; ++i) 2804 for (i = HEAP0; i < N + HEAP0; ++i)
1875 2809
1876 verify_watcher (EV_A_ (W)ANHE_w (heap [i])); 2810 verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
1877 } 2811 }
1878} 2812}
1879 2813
1880static void noinline 2814static void noinline ecb_cold
1881array_verify (EV_P_ W *ws, int cnt) 2815array_verify (EV_P_ W *ws, int cnt)
1882{ 2816{
1883 while (cnt--) 2817 while (cnt--)
1884 { 2818 {
1885 assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); 2819 assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1));
1887 } 2821 }
1888} 2822}
1889#endif 2823#endif
1890 2824
1891#if EV_FEATURE_API 2825#if EV_FEATURE_API
1892void 2826void ecb_cold
1893ev_verify (EV_P) 2827ev_verify (EV_P) EV_THROW
1894{ 2828{
1895#if EV_VERIFY 2829#if EV_VERIFY
1896 int i; 2830 int i;
1897 WL w; 2831 WL w, w2;
1898 2832
1899 assert (activecnt >= -1); 2833 assert (activecnt >= -1);
1900 2834
1901 assert (fdchangemax >= fdchangecnt); 2835 assert (fdchangemax >= fdchangecnt);
1902 for (i = 0; i < fdchangecnt; ++i) 2836 for (i = 0; i < fdchangecnt; ++i)
1903 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); 2837 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
1904 2838
1905 assert (anfdmax >= 0); 2839 assert (anfdmax >= 0);
1906 for (i = 0; i < anfdmax; ++i) 2840 for (i = 0; i < anfdmax; ++i)
2841 {
2842 int j = 0;
2843
1907 for (w = anfds [i].head; w; w = w->next) 2844 for (w = w2 = anfds [i].head; w; w = w->next)
1908 { 2845 {
1909 verify_watcher (EV_A_ (W)w); 2846 verify_watcher (EV_A_ (W)w);
2847
2848 if (j++ & 1)
2849 {
2850 assert (("libev: io watcher list contains a loop", w != w2));
2851 w2 = w2->next;
2852 }
2853
1910 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); 2854 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
1911 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); 2855 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
1912 } 2856 }
2857 }
1913 2858
1914 assert (timermax >= timercnt); 2859 assert (timermax >= timercnt);
1915 verify_heap (EV_A_ timers, timercnt); 2860 verify_heap (EV_A_ timers, timercnt);
1916 2861
1917#if EV_PERIODIC_ENABLE 2862#if EV_PERIODIC_ENABLE
1932#if EV_FORK_ENABLE 2877#if EV_FORK_ENABLE
1933 assert (forkmax >= forkcnt); 2878 assert (forkmax >= forkcnt);
1934 array_verify (EV_A_ (W *)forks, forkcnt); 2879 array_verify (EV_A_ (W *)forks, forkcnt);
1935#endif 2880#endif
1936 2881
2882#if EV_CLEANUP_ENABLE
2883 assert (cleanupmax >= cleanupcnt);
2884 array_verify (EV_A_ (W *)cleanups, cleanupcnt);
2885#endif
2886
1937#if EV_ASYNC_ENABLE 2887#if EV_ASYNC_ENABLE
1938 assert (asyncmax >= asynccnt); 2888 assert (asyncmax >= asynccnt);
1939 array_verify (EV_A_ (W *)asyncs, asynccnt); 2889 array_verify (EV_A_ (W *)asyncs, asynccnt);
1940#endif 2890#endif
1941 2891
1958#endif 2908#endif
1959} 2909}
1960#endif 2910#endif
1961 2911
1962#if EV_MULTIPLICITY 2912#if EV_MULTIPLICITY
1963struct ev_loop * 2913struct ev_loop * ecb_cold
1964ev_default_loop_init (unsigned int flags)
1965#else 2914#else
1966int 2915int
2916#endif
1967ev_default_loop (unsigned int flags) 2917ev_default_loop (unsigned int flags) EV_THROW
1968#endif
1969{ 2918{
1970 if (!ev_default_loop_ptr) 2919 if (!ev_default_loop_ptr)
1971 { 2920 {
1972#if EV_MULTIPLICITY 2921#if EV_MULTIPLICITY
1973 EV_P = ev_default_loop_ptr = &default_loop_struct; 2922 EV_P = ev_default_loop_ptr = &default_loop_struct;
1992 2941
1993 return ev_default_loop_ptr; 2942 return ev_default_loop_ptr;
1994} 2943}
1995 2944
1996void 2945void
1997ev_default_destroy (void) 2946ev_loop_fork (EV_P) EV_THROW
1998{ 2947{
1999#if EV_MULTIPLICITY 2948 postfork = 1;
2000 EV_P = ev_default_loop_ptr;
2001#endif
2002
2003 ev_default_loop_ptr = 0;
2004
2005#if EV_CHILD_ENABLE
2006 ev_ref (EV_A); /* child watcher */
2007 ev_signal_stop (EV_A_ &childev);
2008#endif
2009
2010 loop_destroy (EV_A);
2011}
2012
2013void
2014ev_default_fork (void)
2015{
2016#if EV_MULTIPLICITY
2017 EV_P = ev_default_loop_ptr;
2018#endif
2019
2020 postfork = 1; /* must be in line with ev_loop_fork */
2021} 2949}
2022 2950
2023/*****************************************************************************/ 2951/*****************************************************************************/
2024 2952
2025void 2953void
2027{ 2955{
2028 EV_CB_INVOKE ((W)w, revents); 2956 EV_CB_INVOKE ((W)w, revents);
2029} 2957}
2030 2958
2031unsigned int 2959unsigned int
2032ev_pending_count (EV_P) 2960ev_pending_count (EV_P) EV_THROW
2033{ 2961{
2034 int pri; 2962 int pri;
2035 unsigned int count = 0; 2963 unsigned int count = 0;
2036 2964
2037 for (pri = NUMPRI; pri--; ) 2965 for (pri = NUMPRI; pri--; )
2041} 2969}
2042 2970
2043void noinline 2971void noinline
2044ev_invoke_pending (EV_P) 2972ev_invoke_pending (EV_P)
2045{ 2973{
2046 int pri; 2974 pendingpri = NUMPRI;
2047 2975
2048 for (pri = NUMPRI; pri--; ) 2976 while (pendingpri) /* pendingpri possibly gets modified in the inner loop */
2977 {
2978 --pendingpri;
2979
2049 while (pendingcnt [pri]) 2980 while (pendingcnt [pendingpri])
2050 { 2981 {
2051 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 2982 ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
2052 2983
2053 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
2054 /* ^ this is no longer true, as pending_w could be here */
2055
2056 p->w->pending = 0; 2984 p->w->pending = 0;
2057 EV_CB_INVOKE (p->w, p->events); 2985 EV_CB_INVOKE (p->w, p->events);
2058 EV_FREQUENT_CHECK; 2986 EV_FREQUENT_CHECK;
2059 } 2987 }
2988 }
2060} 2989}
2061 2990
2062#if EV_IDLE_ENABLE 2991#if EV_IDLE_ENABLE
2063/* make idle watchers pending. this handles the "call-idle */ 2992/* make idle watchers pending. this handles the "call-idle */
2064/* only when higher priorities are idle" logic */ 2993/* only when higher priorities are idle" logic */
2121 feed_reverse_done (EV_A_ EV_TIMER); 3050 feed_reverse_done (EV_A_ EV_TIMER);
2122 } 3051 }
2123} 3052}
2124 3053
2125#if EV_PERIODIC_ENABLE 3054#if EV_PERIODIC_ENABLE
3055
3056static void noinline
3057periodic_recalc (EV_P_ ev_periodic *w)
3058{
3059 ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL;
3060 ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval);
3061
3062 /* the above almost always errs on the low side */
3063 while (at <= ev_rt_now)
3064 {
3065 ev_tstamp nat = at + w->interval;
3066
3067 /* when resolution fails us, we use ev_rt_now */
3068 if (expect_false (nat == at))
3069 {
3070 at = ev_rt_now;
3071 break;
3072 }
3073
3074 at = nat;
3075 }
3076
3077 ev_at (w) = at;
3078}
3079
2126/* make periodics pending */ 3080/* make periodics pending */
2127inline_size void 3081inline_size void
2128periodics_reify (EV_P) 3082periodics_reify (EV_P)
2129{ 3083{
2130 EV_FREQUENT_CHECK; 3084 EV_FREQUENT_CHECK;
2131 3085
2132 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 3086 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
2133 { 3087 {
2134 int feed_count = 0;
2135
2136 do 3088 do
2137 { 3089 {
2138 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 3090 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
2139 3091
2140 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ 3092 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
2149 ANHE_at_cache (periodics [HEAP0]); 3101 ANHE_at_cache (periodics [HEAP0]);
2150 downheap (periodics, periodiccnt, HEAP0); 3102 downheap (periodics, periodiccnt, HEAP0);
2151 } 3103 }
2152 else if (w->interval) 3104 else if (w->interval)
2153 { 3105 {
2154 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 3106 periodic_recalc (EV_A_ w);
2155 /* if next trigger time is not sufficiently in the future, put it there */
2156 /* this might happen because of floating point inexactness */
2157 if (ev_at (w) - ev_rt_now < TIME_EPSILON)
2158 {
2159 ev_at (w) += w->interval;
2160
2161 /* if interval is unreasonably low we might still have a time in the past */
2162 /* so correct this. this will make the periodic very inexact, but the user */
2163 /* has effectively asked to get triggered more often than possible */
2164 if (ev_at (w) < ev_rt_now)
2165 ev_at (w) = ev_rt_now;
2166 }
2167
2168 ANHE_at_cache (periodics [HEAP0]); 3107 ANHE_at_cache (periodics [HEAP0]);
2169 downheap (periodics, periodiccnt, HEAP0); 3108 downheap (periodics, periodiccnt, HEAP0);
2170 } 3109 }
2171 else 3110 else
2172 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 3111 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
2180 } 3119 }
2181} 3120}
2182 3121
2183/* simply recalculate all periodics */ 3122/* simply recalculate all periodics */
2184/* TODO: maybe ensure that at least one event happens when jumping forward? */ 3123/* TODO: maybe ensure that at least one event happens when jumping forward? */
2185static void noinline 3124static void noinline ecb_cold
2186periodics_reschedule (EV_P) 3125periodics_reschedule (EV_P)
2187{ 3126{
2188 int i; 3127 int i;
2189 3128
2190 /* adjust periodics after time jump */ 3129 /* adjust periodics after time jump */
2193 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); 3132 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
2194 3133
2195 if (w->reschedule_cb) 3134 if (w->reschedule_cb)
2196 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 3135 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
2197 else if (w->interval) 3136 else if (w->interval)
2198 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 3137 periodic_recalc (EV_A_ w);
2199 3138
2200 ANHE_at_cache (periodics [i]); 3139 ANHE_at_cache (periodics [i]);
2201 } 3140 }
2202 3141
2203 reheap (periodics, periodiccnt); 3142 reheap (periodics, periodiccnt);
2204} 3143}
2205#endif 3144#endif
2206 3145
2207/* adjust all timers by a given offset */ 3146/* adjust all timers by a given offset */
2208static void noinline 3147static void noinline ecb_cold
2209timers_reschedule (EV_P_ ev_tstamp adjust) 3148timers_reschedule (EV_P_ ev_tstamp adjust)
2210{ 3149{
2211 int i; 3150 int i;
2212 3151
2213 for (i = 0; i < timercnt; ++i) 3152 for (i = 0; i < timercnt; ++i)
2250 * doesn't hurt either as we only do this on time-jumps or 3189 * doesn't hurt either as we only do this on time-jumps or
2251 * in the unlikely event of having been preempted here. 3190 * in the unlikely event of having been preempted here.
2252 */ 3191 */
2253 for (i = 4; --i; ) 3192 for (i = 4; --i; )
2254 { 3193 {
3194 ev_tstamp diff;
2255 rtmn_diff = ev_rt_now - mn_now; 3195 rtmn_diff = ev_rt_now - mn_now;
2256 3196
3197 diff = odiff - rtmn_diff;
3198
2257 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)) 3199 if (expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP))
2258 return; /* all is well */ 3200 return; /* all is well */
2259 3201
2260 ev_rt_now = ev_time (); 3202 ev_rt_now = ev_time ();
2261 mn_now = get_clock (); 3203 mn_now = get_clock ();
2262 now_floor = mn_now; 3204 now_floor = mn_now;
2284 3226
2285 mn_now = ev_rt_now; 3227 mn_now = ev_rt_now;
2286 } 3228 }
2287} 3229}
2288 3230
2289void 3231int
2290ev_loop (EV_P_ int flags) 3232ev_run (EV_P_ int flags)
2291{ 3233{
2292#if EV_FEATURE_API 3234#if EV_FEATURE_API
2293 ++loop_depth; 3235 ++loop_depth;
2294#endif 3236#endif
2295 3237
2296 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); 3238 assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE));
2297 3239
2298 loop_done = EVUNLOOP_CANCEL; 3240 loop_done = EVBREAK_CANCEL;
2299 3241
2300 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ 3242 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2301 3243
2302 do 3244 do
2303 { 3245 {
2346 /* calculate blocking time */ 3288 /* calculate blocking time */
2347 { 3289 {
2348 ev_tstamp waittime = 0.; 3290 ev_tstamp waittime = 0.;
2349 ev_tstamp sleeptime = 0.; 3291 ev_tstamp sleeptime = 0.;
2350 3292
3293 /* remember old timestamp for io_blocktime calculation */
3294 ev_tstamp prev_mn_now = mn_now;
3295
3296 /* update time to cancel out callback processing overhead */
3297 time_update (EV_A_ 1e100);
3298
3299 /* from now on, we want a pipe-wake-up */
3300 pipe_write_wanted = 1;
3301
3302 ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */
3303
2351 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 3304 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
2352 { 3305 {
2353 /* remember old timestamp for io_blocktime calculation */
2354 ev_tstamp prev_mn_now = mn_now;
2355
2356 /* update time to cancel out callback processing overhead */
2357 time_update (EV_A_ 1e100);
2358
2359 waittime = MAX_BLOCKTIME; 3306 waittime = MAX_BLOCKTIME;
2360 3307
2361 if (timercnt) 3308 if (timercnt)
2362 { 3309 {
2363 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 3310 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
2364 if (waittime > to) waittime = to; 3311 if (waittime > to) waittime = to;
2365 } 3312 }
2366 3313
2367#if EV_PERIODIC_ENABLE 3314#if EV_PERIODIC_ENABLE
2368 if (periodiccnt) 3315 if (periodiccnt)
2369 { 3316 {
2370 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 3317 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now;
2371 if (waittime > to) waittime = to; 3318 if (waittime > to) waittime = to;
2372 } 3319 }
2373#endif 3320#endif
2374 3321
2375 /* don't let timeouts decrease the waittime below timeout_blocktime */ 3322 /* don't let timeouts decrease the waittime below timeout_blocktime */
2376 if (expect_false (waittime < timeout_blocktime)) 3323 if (expect_false (waittime < timeout_blocktime))
2377 waittime = timeout_blocktime; 3324 waittime = timeout_blocktime;
3325
3326 /* at this point, we NEED to wait, so we have to ensure */
3327 /* to pass a minimum nonzero value to the backend */
3328 if (expect_false (waittime < backend_mintime))
3329 waittime = backend_mintime;
2378 3330
2379 /* extra check because io_blocktime is commonly 0 */ 3331 /* extra check because io_blocktime is commonly 0 */
2380 if (expect_false (io_blocktime)) 3332 if (expect_false (io_blocktime))
2381 { 3333 {
2382 sleeptime = io_blocktime - (mn_now - prev_mn_now); 3334 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2383 3335
2384 if (sleeptime > waittime - backend_fudge) 3336 if (sleeptime > waittime - backend_mintime)
2385 sleeptime = waittime - backend_fudge; 3337 sleeptime = waittime - backend_mintime;
2386 3338
2387 if (expect_true (sleeptime > 0.)) 3339 if (expect_true (sleeptime > 0.))
2388 { 3340 {
2389 ev_sleep (sleeptime); 3341 ev_sleep (sleeptime);
2390 waittime -= sleeptime; 3342 waittime -= sleeptime;
2393 } 3345 }
2394 3346
2395#if EV_FEATURE_API 3347#if EV_FEATURE_API
2396 ++loop_count; 3348 ++loop_count;
2397#endif 3349#endif
2398 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ 3350 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2399 backend_poll (EV_A_ waittime); 3351 backend_poll (EV_A_ waittime);
2400 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ 3352 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
3353
3354 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
3355
3356 ECB_MEMORY_FENCE_ACQUIRE;
3357 if (pipe_write_skipped)
3358 {
3359 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
3360 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3361 }
3362
2401 3363
2402 /* update ev_rt_now, do magic */ 3364 /* update ev_rt_now, do magic */
2403 time_update (EV_A_ waittime + sleeptime); 3365 time_update (EV_A_ waittime + sleeptime);
2404 } 3366 }
2405 3367
2423 EV_INVOKE_PENDING; 3385 EV_INVOKE_PENDING;
2424 } 3386 }
2425 while (expect_true ( 3387 while (expect_true (
2426 activecnt 3388 activecnt
2427 && !loop_done 3389 && !loop_done
2428 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 3390 && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
2429 )); 3391 ));
2430 3392
2431 if (loop_done == EVUNLOOP_ONE) 3393 if (loop_done == EVBREAK_ONE)
2432 loop_done = EVUNLOOP_CANCEL; 3394 loop_done = EVBREAK_CANCEL;
2433 3395
2434#if EV_FEATURE_API 3396#if EV_FEATURE_API
2435 --loop_depth; 3397 --loop_depth;
2436#endif 3398#endif
3399
3400 return activecnt;
2437} 3401}
2438 3402
2439void 3403void
2440ev_unloop (EV_P_ int how) 3404ev_break (EV_P_ int how) EV_THROW
2441{ 3405{
2442 loop_done = how; 3406 loop_done = how;
2443} 3407}
2444 3408
2445void 3409void
2446ev_ref (EV_P) 3410ev_ref (EV_P) EV_THROW
2447{ 3411{
2448 ++activecnt; 3412 ++activecnt;
2449} 3413}
2450 3414
2451void 3415void
2452ev_unref (EV_P) 3416ev_unref (EV_P) EV_THROW
2453{ 3417{
2454 --activecnt; 3418 --activecnt;
2455} 3419}
2456 3420
2457void 3421void
2458ev_now_update (EV_P) 3422ev_now_update (EV_P) EV_THROW
2459{ 3423{
2460 time_update (EV_A_ 1e100); 3424 time_update (EV_A_ 1e100);
2461} 3425}
2462 3426
2463void 3427void
2464ev_suspend (EV_P) 3428ev_suspend (EV_P) EV_THROW
2465{ 3429{
2466 ev_now_update (EV_A); 3430 ev_now_update (EV_A);
2467} 3431}
2468 3432
2469void 3433void
2470ev_resume (EV_P) 3434ev_resume (EV_P) EV_THROW
2471{ 3435{
2472 ev_tstamp mn_prev = mn_now; 3436 ev_tstamp mn_prev = mn_now;
2473 3437
2474 ev_now_update (EV_A); 3438 ev_now_update (EV_A);
2475 timers_reschedule (EV_A_ mn_now - mn_prev); 3439 timers_reschedule (EV_A_ mn_now - mn_prev);
2514 w->pending = 0; 3478 w->pending = 0;
2515 } 3479 }
2516} 3480}
2517 3481
2518int 3482int
2519ev_clear_pending (EV_P_ void *w) 3483ev_clear_pending (EV_P_ void *w) EV_THROW
2520{ 3484{
2521 W w_ = (W)w; 3485 W w_ = (W)w;
2522 int pending = w_->pending; 3486 int pending = w_->pending;
2523 3487
2524 if (expect_true (pending)) 3488 if (expect_true (pending))
2557} 3521}
2558 3522
2559/*****************************************************************************/ 3523/*****************************************************************************/
2560 3524
2561void noinline 3525void noinline
2562ev_io_start (EV_P_ ev_io *w) 3526ev_io_start (EV_P_ ev_io *w) EV_THROW
2563{ 3527{
2564 int fd = w->fd; 3528 int fd = w->fd;
2565 3529
2566 if (expect_false (ev_is_active (w))) 3530 if (expect_false (ev_is_active (w)))
2567 return; 3531 return;
2573 3537
2574 ev_start (EV_A_ (W)w, 1); 3538 ev_start (EV_A_ (W)w, 1);
2575 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 3539 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2576 wlist_add (&anfds[fd].head, (WL)w); 3540 wlist_add (&anfds[fd].head, (WL)w);
2577 3541
3542 /* common bug, apparently */
3543 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3544
2578 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); 3545 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2579 w->events &= ~EV__IOFDSET; 3546 w->events &= ~EV__IOFDSET;
2580 3547
2581 EV_FREQUENT_CHECK; 3548 EV_FREQUENT_CHECK;
2582} 3549}
2583 3550
2584void noinline 3551void noinline
2585ev_io_stop (EV_P_ ev_io *w) 3552ev_io_stop (EV_P_ ev_io *w) EV_THROW
2586{ 3553{
2587 clear_pending (EV_A_ (W)w); 3554 clear_pending (EV_A_ (W)w);
2588 if (expect_false (!ev_is_active (w))) 3555 if (expect_false (!ev_is_active (w)))
2589 return; 3556 return;
2590 3557
2593 EV_FREQUENT_CHECK; 3560 EV_FREQUENT_CHECK;
2594 3561
2595 wlist_del (&anfds[w->fd].head, (WL)w); 3562 wlist_del (&anfds[w->fd].head, (WL)w);
2596 ev_stop (EV_A_ (W)w); 3563 ev_stop (EV_A_ (W)w);
2597 3564
2598 fd_change (EV_A_ w->fd, 1); 3565 fd_change (EV_A_ w->fd, EV_ANFD_REIFY);
2599 3566
2600 EV_FREQUENT_CHECK; 3567 EV_FREQUENT_CHECK;
2601} 3568}
2602 3569
2603void noinline 3570void noinline
2604ev_timer_start (EV_P_ ev_timer *w) 3571ev_timer_start (EV_P_ ev_timer *w) EV_THROW
2605{ 3572{
2606 if (expect_false (ev_is_active (w))) 3573 if (expect_false (ev_is_active (w)))
2607 return; 3574 return;
2608 3575
2609 ev_at (w) += mn_now; 3576 ev_at (w) += mn_now;
2623 3590
2624 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ 3591 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
2625} 3592}
2626 3593
2627void noinline 3594void noinline
2628ev_timer_stop (EV_P_ ev_timer *w) 3595ev_timer_stop (EV_P_ ev_timer *w) EV_THROW
2629{ 3596{
2630 clear_pending (EV_A_ (W)w); 3597 clear_pending (EV_A_ (W)w);
2631 if (expect_false (!ev_is_active (w))) 3598 if (expect_false (!ev_is_active (w)))
2632 return; 3599 return;
2633 3600
2653 3620
2654 EV_FREQUENT_CHECK; 3621 EV_FREQUENT_CHECK;
2655} 3622}
2656 3623
2657void noinline 3624void noinline
2658ev_timer_again (EV_P_ ev_timer *w) 3625ev_timer_again (EV_P_ ev_timer *w) EV_THROW
2659{ 3626{
2660 EV_FREQUENT_CHECK; 3627 EV_FREQUENT_CHECK;
3628
3629 clear_pending (EV_A_ (W)w);
2661 3630
2662 if (ev_is_active (w)) 3631 if (ev_is_active (w))
2663 { 3632 {
2664 if (w->repeat) 3633 if (w->repeat)
2665 { 3634 {
2678 3647
2679 EV_FREQUENT_CHECK; 3648 EV_FREQUENT_CHECK;
2680} 3649}
2681 3650
2682ev_tstamp 3651ev_tstamp
2683ev_timer_remaining (EV_P_ ev_timer *w) 3652ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW
2684{ 3653{
2685 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 3654 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
2686} 3655}
2687 3656
2688#if EV_PERIODIC_ENABLE 3657#if EV_PERIODIC_ENABLE
2689void noinline 3658void noinline
2690ev_periodic_start (EV_P_ ev_periodic *w) 3659ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW
2691{ 3660{
2692 if (expect_false (ev_is_active (w))) 3661 if (expect_false (ev_is_active (w)))
2693 return; 3662 return;
2694 3663
2695 if (w->reschedule_cb) 3664 if (w->reschedule_cb)
2696 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 3665 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
2697 else if (w->interval) 3666 else if (w->interval)
2698 { 3667 {
2699 assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); 3668 assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.));
2700 /* this formula differs from the one in periodic_reify because we do not always round up */ 3669 periodic_recalc (EV_A_ w);
2701 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
2702 } 3670 }
2703 else 3671 else
2704 ev_at (w) = w->offset; 3672 ev_at (w) = w->offset;
2705 3673
2706 EV_FREQUENT_CHECK; 3674 EV_FREQUENT_CHECK;
2716 3684
2717 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ 3685 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
2718} 3686}
2719 3687
2720void noinline 3688void noinline
2721ev_periodic_stop (EV_P_ ev_periodic *w) 3689ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW
2722{ 3690{
2723 clear_pending (EV_A_ (W)w); 3691 clear_pending (EV_A_ (W)w);
2724 if (expect_false (!ev_is_active (w))) 3692 if (expect_false (!ev_is_active (w)))
2725 return; 3693 return;
2726 3694
2744 3712
2745 EV_FREQUENT_CHECK; 3713 EV_FREQUENT_CHECK;
2746} 3714}
2747 3715
2748void noinline 3716void noinline
2749ev_periodic_again (EV_P_ ev_periodic *w) 3717ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW
2750{ 3718{
2751 /* TODO: use adjustheap and recalculation */ 3719 /* TODO: use adjustheap and recalculation */
2752 ev_periodic_stop (EV_A_ w); 3720 ev_periodic_stop (EV_A_ w);
2753 ev_periodic_start (EV_A_ w); 3721 ev_periodic_start (EV_A_ w);
2754} 3722}
2759#endif 3727#endif
2760 3728
2761#if EV_SIGNAL_ENABLE 3729#if EV_SIGNAL_ENABLE
2762 3730
2763void noinline 3731void noinline
2764ev_signal_start (EV_P_ ev_signal *w) 3732ev_signal_start (EV_P_ ev_signal *w) EV_THROW
2765{ 3733{
2766 if (expect_false (ev_is_active (w))) 3734 if (expect_false (ev_is_active (w)))
2767 return; 3735 return;
2768 3736
2769 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 3737 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
2771#if EV_MULTIPLICITY 3739#if EV_MULTIPLICITY
2772 assert (("libev: a signal must not be attached to two different loops", 3740 assert (("libev: a signal must not be attached to two different loops",
2773 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); 3741 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
2774 3742
2775 signals [w->signum - 1].loop = EV_A; 3743 signals [w->signum - 1].loop = EV_A;
3744 ECB_MEMORY_FENCE_RELEASE;
2776#endif 3745#endif
2777 3746
2778 EV_FREQUENT_CHECK; 3747 EV_FREQUENT_CHECK;
2779 3748
2780#if EV_USE_SIGNALFD 3749#if EV_USE_SIGNALFD
2827 sa.sa_handler = ev_sighandler; 3796 sa.sa_handler = ev_sighandler;
2828 sigfillset (&sa.sa_mask); 3797 sigfillset (&sa.sa_mask);
2829 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 3798 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2830 sigaction (w->signum, &sa, 0); 3799 sigaction (w->signum, &sa, 0);
2831 3800
3801 if (origflags & EVFLAG_NOSIGMASK)
3802 {
2832 sigemptyset (&sa.sa_mask); 3803 sigemptyset (&sa.sa_mask);
2833 sigaddset (&sa.sa_mask, w->signum); 3804 sigaddset (&sa.sa_mask, w->signum);
2834 sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); 3805 sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
3806 }
2835#endif 3807#endif
2836 } 3808 }
2837 3809
2838 EV_FREQUENT_CHECK; 3810 EV_FREQUENT_CHECK;
2839} 3811}
2840 3812
2841void noinline 3813void noinline
2842ev_signal_stop (EV_P_ ev_signal *w) 3814ev_signal_stop (EV_P_ ev_signal *w) EV_THROW
2843{ 3815{
2844 clear_pending (EV_A_ (W)w); 3816 clear_pending (EV_A_ (W)w);
2845 if (expect_false (!ev_is_active (w))) 3817 if (expect_false (!ev_is_active (w)))
2846 return; 3818 return;
2847 3819
2878#endif 3850#endif
2879 3851
2880#if EV_CHILD_ENABLE 3852#if EV_CHILD_ENABLE
2881 3853
2882void 3854void
2883ev_child_start (EV_P_ ev_child *w) 3855ev_child_start (EV_P_ ev_child *w) EV_THROW
2884{ 3856{
2885#if EV_MULTIPLICITY 3857#if EV_MULTIPLICITY
2886 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 3858 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2887#endif 3859#endif
2888 if (expect_false (ev_is_active (w))) 3860 if (expect_false (ev_is_active (w)))
2895 3867
2896 EV_FREQUENT_CHECK; 3868 EV_FREQUENT_CHECK;
2897} 3869}
2898 3870
2899void 3871void
2900ev_child_stop (EV_P_ ev_child *w) 3872ev_child_stop (EV_P_ ev_child *w) EV_THROW
2901{ 3873{
2902 clear_pending (EV_A_ (W)w); 3874 clear_pending (EV_A_ (W)w);
2903 if (expect_false (!ev_is_active (w))) 3875 if (expect_false (!ev_is_active (w)))
2904 return; 3876 return;
2905 3877
2932# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX) 3904# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
2933 3905
2934static void noinline 3906static void noinline
2935infy_add (EV_P_ ev_stat *w) 3907infy_add (EV_P_ ev_stat *w)
2936{ 3908{
2937 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); 3909 w->wd = inotify_add_watch (fs_fd, w->path,
3910 IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY
3911 | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO
3912 | IN_DONT_FOLLOW | IN_MASK_ADD);
2938 3913
2939 if (w->wd >= 0) 3914 if (w->wd >= 0)
2940 { 3915 {
2941 struct statfs sfs; 3916 struct statfs sfs;
2942 3917
2946 3921
2947 if (!fs_2625) 3922 if (!fs_2625)
2948 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; 3923 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
2949 else if (!statfs (w->path, &sfs) 3924 else if (!statfs (w->path, &sfs)
2950 && (sfs.f_type == 0x1373 /* devfs */ 3925 && (sfs.f_type == 0x1373 /* devfs */
3926 || sfs.f_type == 0x4006 /* fat */
3927 || sfs.f_type == 0x4d44 /* msdos */
2951 || sfs.f_type == 0xEF53 /* ext2/3 */ 3928 || sfs.f_type == 0xEF53 /* ext2/3 */
3929 || sfs.f_type == 0x72b6 /* jffs2 */
3930 || sfs.f_type == 0x858458f6 /* ramfs */
3931 || sfs.f_type == 0x5346544e /* ntfs */
2952 || sfs.f_type == 0x3153464a /* jfs */ 3932 || sfs.f_type == 0x3153464a /* jfs */
3933 || sfs.f_type == 0x9123683e /* btrfs */
2953 || sfs.f_type == 0x52654973 /* reiser3 */ 3934 || sfs.f_type == 0x52654973 /* reiser3 */
2954 || sfs.f_type == 0x01021994 /* tempfs */ 3935 || sfs.f_type == 0x01021994 /* tmpfs */
2955 || sfs.f_type == 0x58465342 /* xfs */)) 3936 || sfs.f_type == 0x58465342 /* xfs */))
2956 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */ 3937 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
2957 else 3938 else
2958 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */ 3939 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
2959 } 3940 }
2980 if (!pend || pend == path) 3961 if (!pend || pend == path)
2981 break; 3962 break;
2982 3963
2983 *pend = 0; 3964 *pend = 0;
2984 w->wd = inotify_add_watch (fs_fd, path, mask); 3965 w->wd = inotify_add_watch (fs_fd, path, mask);
2985 } 3966 }
2986 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 3967 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
2987 } 3968 }
2988 } 3969 }
2989 3970
2990 if (w->wd >= 0) 3971 if (w->wd >= 0)
3057 infy_wd (EV_A_ ev->wd, ev->wd, ev); 4038 infy_wd (EV_A_ ev->wd, ev->wd, ev);
3058 ofs += sizeof (struct inotify_event) + ev->len; 4039 ofs += sizeof (struct inotify_event) + ev->len;
3059 } 4040 }
3060} 4041}
3061 4042
3062inline_size unsigned int
3063ev_linux_version (void)
3064{
3065 struct utsname buf;
3066 unsigned int v;
3067 int i;
3068 char *p = buf.release;
3069
3070 if (uname (&buf))
3071 return 0;
3072
3073 for (i = 3+1; --i; )
3074 {
3075 unsigned int c = 0;
3076
3077 for (;;)
3078 {
3079 if (*p >= '0' && *p <= '9')
3080 c = c * 10 + *p++ - '0';
3081 else
3082 {
3083 p += *p == '.';
3084 break;
3085 }
3086 }
3087
3088 v = (v << 8) | c;
3089 }
3090
3091 return v;
3092}
3093
3094inline_size void 4043inline_size void ecb_cold
3095ev_check_2625 (EV_P) 4044ev_check_2625 (EV_P)
3096{ 4045{
3097 /* kernels < 2.6.25 are borked 4046 /* kernels < 2.6.25 are borked
3098 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html 4047 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
3099 */ 4048 */
3104} 4053}
3105 4054
3106inline_size int 4055inline_size int
3107infy_newfd (void) 4056infy_newfd (void)
3108{ 4057{
3109#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK) 4058#if defined IN_CLOEXEC && defined IN_NONBLOCK
3110 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); 4059 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3111 if (fd >= 0) 4060 if (fd >= 0)
3112 return fd; 4061 return fd;
3113#endif 4062#endif
3114 return inotify_init (); 4063 return inotify_init ();
3189#else 4138#else
3190# define EV_LSTAT(p,b) lstat (p, b) 4139# define EV_LSTAT(p,b) lstat (p, b)
3191#endif 4140#endif
3192 4141
3193void 4142void
3194ev_stat_stat (EV_P_ ev_stat *w) 4143ev_stat_stat (EV_P_ ev_stat *w) EV_THROW
3195{ 4144{
3196 if (lstat (w->path, &w->attr) < 0) 4145 if (lstat (w->path, &w->attr) < 0)
3197 w->attr.st_nlink = 0; 4146 w->attr.st_nlink = 0;
3198 else if (!w->attr.st_nlink) 4147 else if (!w->attr.st_nlink)
3199 w->attr.st_nlink = 1; 4148 w->attr.st_nlink = 1;
3238 ev_feed_event (EV_A_ w, EV_STAT); 4187 ev_feed_event (EV_A_ w, EV_STAT);
3239 } 4188 }
3240} 4189}
3241 4190
3242void 4191void
3243ev_stat_start (EV_P_ ev_stat *w) 4192ev_stat_start (EV_P_ ev_stat *w) EV_THROW
3244{ 4193{
3245 if (expect_false (ev_is_active (w))) 4194 if (expect_false (ev_is_active (w)))
3246 return; 4195 return;
3247 4196
3248 ev_stat_stat (EV_A_ w); 4197 ev_stat_stat (EV_A_ w);
3269 4218
3270 EV_FREQUENT_CHECK; 4219 EV_FREQUENT_CHECK;
3271} 4220}
3272 4221
3273void 4222void
3274ev_stat_stop (EV_P_ ev_stat *w) 4223ev_stat_stop (EV_P_ ev_stat *w) EV_THROW
3275{ 4224{
3276 clear_pending (EV_A_ (W)w); 4225 clear_pending (EV_A_ (W)w);
3277 if (expect_false (!ev_is_active (w))) 4226 if (expect_false (!ev_is_active (w)))
3278 return; 4227 return;
3279 4228
3295} 4244}
3296#endif 4245#endif
3297 4246
3298#if EV_IDLE_ENABLE 4247#if EV_IDLE_ENABLE
3299void 4248void
3300ev_idle_start (EV_P_ ev_idle *w) 4249ev_idle_start (EV_P_ ev_idle *w) EV_THROW
3301{ 4250{
3302 if (expect_false (ev_is_active (w))) 4251 if (expect_false (ev_is_active (w)))
3303 return; 4252 return;
3304 4253
3305 pri_adjust (EV_A_ (W)w); 4254 pri_adjust (EV_A_ (W)w);
3318 4267
3319 EV_FREQUENT_CHECK; 4268 EV_FREQUENT_CHECK;
3320} 4269}
3321 4270
3322void 4271void
3323ev_idle_stop (EV_P_ ev_idle *w) 4272ev_idle_stop (EV_P_ ev_idle *w) EV_THROW
3324{ 4273{
3325 clear_pending (EV_A_ (W)w); 4274 clear_pending (EV_A_ (W)w);
3326 if (expect_false (!ev_is_active (w))) 4275 if (expect_false (!ev_is_active (w)))
3327 return; 4276 return;
3328 4277
3342} 4291}
3343#endif 4292#endif
3344 4293
3345#if EV_PREPARE_ENABLE 4294#if EV_PREPARE_ENABLE
3346void 4295void
3347ev_prepare_start (EV_P_ ev_prepare *w) 4296ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW
3348{ 4297{
3349 if (expect_false (ev_is_active (w))) 4298 if (expect_false (ev_is_active (w)))
3350 return; 4299 return;
3351 4300
3352 EV_FREQUENT_CHECK; 4301 EV_FREQUENT_CHECK;
3357 4306
3358 EV_FREQUENT_CHECK; 4307 EV_FREQUENT_CHECK;
3359} 4308}
3360 4309
3361void 4310void
3362ev_prepare_stop (EV_P_ ev_prepare *w) 4311ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW
3363{ 4312{
3364 clear_pending (EV_A_ (W)w); 4313 clear_pending (EV_A_ (W)w);
3365 if (expect_false (!ev_is_active (w))) 4314 if (expect_false (!ev_is_active (w)))
3366 return; 4315 return;
3367 4316
3380} 4329}
3381#endif 4330#endif
3382 4331
3383#if EV_CHECK_ENABLE 4332#if EV_CHECK_ENABLE
3384void 4333void
3385ev_check_start (EV_P_ ev_check *w) 4334ev_check_start (EV_P_ ev_check *w) EV_THROW
3386{ 4335{
3387 if (expect_false (ev_is_active (w))) 4336 if (expect_false (ev_is_active (w)))
3388 return; 4337 return;
3389 4338
3390 EV_FREQUENT_CHECK; 4339 EV_FREQUENT_CHECK;
3395 4344
3396 EV_FREQUENT_CHECK; 4345 EV_FREQUENT_CHECK;
3397} 4346}
3398 4347
3399void 4348void
3400ev_check_stop (EV_P_ ev_check *w) 4349ev_check_stop (EV_P_ ev_check *w) EV_THROW
3401{ 4350{
3402 clear_pending (EV_A_ (W)w); 4351 clear_pending (EV_A_ (W)w);
3403 if (expect_false (!ev_is_active (w))) 4352 if (expect_false (!ev_is_active (w)))
3404 return; 4353 return;
3405 4354
3418} 4367}
3419#endif 4368#endif
3420 4369
3421#if EV_EMBED_ENABLE 4370#if EV_EMBED_ENABLE
3422void noinline 4371void noinline
3423ev_embed_sweep (EV_P_ ev_embed *w) 4372ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW
3424{ 4373{
3425 ev_loop (w->other, EVLOOP_NONBLOCK); 4374 ev_run (w->other, EVRUN_NOWAIT);
3426} 4375}
3427 4376
3428static void 4377static void
3429embed_io_cb (EV_P_ ev_io *io, int revents) 4378embed_io_cb (EV_P_ ev_io *io, int revents)
3430{ 4379{
3431 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); 4380 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
3432 4381
3433 if (ev_cb (w)) 4382 if (ev_cb (w))
3434 ev_feed_event (EV_A_ (W)w, EV_EMBED); 4383 ev_feed_event (EV_A_ (W)w, EV_EMBED);
3435 else 4384 else
3436 ev_loop (w->other, EVLOOP_NONBLOCK); 4385 ev_run (w->other, EVRUN_NOWAIT);
3437} 4386}
3438 4387
3439static void 4388static void
3440embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 4389embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3441{ 4390{
3445 EV_P = w->other; 4394 EV_P = w->other;
3446 4395
3447 while (fdchangecnt) 4396 while (fdchangecnt)
3448 { 4397 {
3449 fd_reify (EV_A); 4398 fd_reify (EV_A);
3450 ev_loop (EV_A_ EVLOOP_NONBLOCK); 4399 ev_run (EV_A_ EVRUN_NOWAIT);
3451 } 4400 }
3452 } 4401 }
3453} 4402}
3454 4403
3455static void 4404static void
3461 4410
3462 { 4411 {
3463 EV_P = w->other; 4412 EV_P = w->other;
3464 4413
3465 ev_loop_fork (EV_A); 4414 ev_loop_fork (EV_A);
3466 ev_loop (EV_A_ EVLOOP_NONBLOCK); 4415 ev_run (EV_A_ EVRUN_NOWAIT);
3467 } 4416 }
3468 4417
3469 ev_embed_start (EV_A_ w); 4418 ev_embed_start (EV_A_ w);
3470} 4419}
3471 4420
3476 ev_idle_stop (EV_A_ idle); 4425 ev_idle_stop (EV_A_ idle);
3477} 4426}
3478#endif 4427#endif
3479 4428
3480void 4429void
3481ev_embed_start (EV_P_ ev_embed *w) 4430ev_embed_start (EV_P_ ev_embed *w) EV_THROW
3482{ 4431{
3483 if (expect_false (ev_is_active (w))) 4432 if (expect_false (ev_is_active (w)))
3484 return; 4433 return;
3485 4434
3486 { 4435 {
3507 4456
3508 EV_FREQUENT_CHECK; 4457 EV_FREQUENT_CHECK;
3509} 4458}
3510 4459
3511void 4460void
3512ev_embed_stop (EV_P_ ev_embed *w) 4461ev_embed_stop (EV_P_ ev_embed *w) EV_THROW
3513{ 4462{
3514 clear_pending (EV_A_ (W)w); 4463 clear_pending (EV_A_ (W)w);
3515 if (expect_false (!ev_is_active (w))) 4464 if (expect_false (!ev_is_active (w)))
3516 return; 4465 return;
3517 4466
3527} 4476}
3528#endif 4477#endif
3529 4478
3530#if EV_FORK_ENABLE 4479#if EV_FORK_ENABLE
3531void 4480void
3532ev_fork_start (EV_P_ ev_fork *w) 4481ev_fork_start (EV_P_ ev_fork *w) EV_THROW
3533{ 4482{
3534 if (expect_false (ev_is_active (w))) 4483 if (expect_false (ev_is_active (w)))
3535 return; 4484 return;
3536 4485
3537 EV_FREQUENT_CHECK; 4486 EV_FREQUENT_CHECK;
3542 4491
3543 EV_FREQUENT_CHECK; 4492 EV_FREQUENT_CHECK;
3544} 4493}
3545 4494
3546void 4495void
3547ev_fork_stop (EV_P_ ev_fork *w) 4496ev_fork_stop (EV_P_ ev_fork *w) EV_THROW
3548{ 4497{
3549 clear_pending (EV_A_ (W)w); 4498 clear_pending (EV_A_ (W)w);
3550 if (expect_false (!ev_is_active (w))) 4499 if (expect_false (!ev_is_active (w)))
3551 return; 4500 return;
3552 4501
3563 4512
3564 EV_FREQUENT_CHECK; 4513 EV_FREQUENT_CHECK;
3565} 4514}
3566#endif 4515#endif
3567 4516
4517#if EV_CLEANUP_ENABLE
4518void
4519ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW
4520{
4521 if (expect_false (ev_is_active (w)))
4522 return;
4523
4524 EV_FREQUENT_CHECK;
4525
4526 ev_start (EV_A_ (W)w, ++cleanupcnt);
4527 array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2);
4528 cleanups [cleanupcnt - 1] = w;
4529
4530 /* cleanup watchers should never keep a refcount on the loop */
4531 ev_unref (EV_A);
4532 EV_FREQUENT_CHECK;
4533}
4534
4535void
4536ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW
4537{
4538 clear_pending (EV_A_ (W)w);
4539 if (expect_false (!ev_is_active (w)))
4540 return;
4541
4542 EV_FREQUENT_CHECK;
4543 ev_ref (EV_A);
4544
4545 {
4546 int active = ev_active (w);
4547
4548 cleanups [active - 1] = cleanups [--cleanupcnt];
4549 ev_active (cleanups [active - 1]) = active;
4550 }
4551
4552 ev_stop (EV_A_ (W)w);
4553
4554 EV_FREQUENT_CHECK;
4555}
4556#endif
4557
3568#if EV_ASYNC_ENABLE 4558#if EV_ASYNC_ENABLE
3569void 4559void
3570ev_async_start (EV_P_ ev_async *w) 4560ev_async_start (EV_P_ ev_async *w) EV_THROW
3571{ 4561{
3572 if (expect_false (ev_is_active (w))) 4562 if (expect_false (ev_is_active (w)))
3573 return; 4563 return;
4564
4565 w->sent = 0;
3574 4566
3575 evpipe_init (EV_A); 4567 evpipe_init (EV_A);
3576 4568
3577 EV_FREQUENT_CHECK; 4569 EV_FREQUENT_CHECK;
3578 4570
3582 4574
3583 EV_FREQUENT_CHECK; 4575 EV_FREQUENT_CHECK;
3584} 4576}
3585 4577
3586void 4578void
3587ev_async_stop (EV_P_ ev_async *w) 4579ev_async_stop (EV_P_ ev_async *w) EV_THROW
3588{ 4580{
3589 clear_pending (EV_A_ (W)w); 4581 clear_pending (EV_A_ (W)w);
3590 if (expect_false (!ev_is_active (w))) 4582 if (expect_false (!ev_is_active (w)))
3591 return; 4583 return;
3592 4584
3603 4595
3604 EV_FREQUENT_CHECK; 4596 EV_FREQUENT_CHECK;
3605} 4597}
3606 4598
3607void 4599void
3608ev_async_send (EV_P_ ev_async *w) 4600ev_async_send (EV_P_ ev_async *w) EV_THROW
3609{ 4601{
3610 w->sent = 1; 4602 w->sent = 1;
3611 evpipe_write (EV_A_ &async_pending); 4603 evpipe_write (EV_A_ &async_pending);
3612} 4604}
3613#endif 4605#endif
3650 4642
3651 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); 4643 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
3652} 4644}
3653 4645
3654void 4646void
3655ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 4647ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW
3656{ 4648{
3657 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 4649 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
3658 4650
3659 if (expect_false (!once)) 4651 if (expect_false (!once))
3660 { 4652 {
3681} 4673}
3682 4674
3683/*****************************************************************************/ 4675/*****************************************************************************/
3684 4676
3685#if EV_WALK_ENABLE 4677#if EV_WALK_ENABLE
3686void 4678void ecb_cold
3687ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 4679ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW
3688{ 4680{
3689 int i, j; 4681 int i, j;
3690 ev_watcher_list *wl, *wn; 4682 ev_watcher_list *wl, *wn;
3691 4683
3692 if (types & (EV_IO | EV_EMBED)) 4684 if (types & (EV_IO | EV_EMBED))
3735 cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); 4727 cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i]));
3736#endif 4728#endif
3737 4729
3738#if EV_IDLE_ENABLE 4730#if EV_IDLE_ENABLE
3739 if (types & EV_IDLE) 4731 if (types & EV_IDLE)
3740 for (j = NUMPRI; i--; ) 4732 for (j = NUMPRI; j--; )
3741 for (i = idlecnt [j]; i--; ) 4733 for (i = idlecnt [j]; i--; )
3742 cb (EV_A_ EV_IDLE, idles [j][i]); 4734 cb (EV_A_ EV_IDLE, idles [j][i]);
3743#endif 4735#endif
3744 4736
3745#if EV_FORK_ENABLE 4737#if EV_FORK_ENABLE
3798 4790
3799#if EV_MULTIPLICITY 4791#if EV_MULTIPLICITY
3800 #include "ev_wrap.h" 4792 #include "ev_wrap.h"
3801#endif 4793#endif
3802 4794
3803#ifdef __cplusplus
3804}
3805#endif
3806

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines