libev/ev.c

5144 lines
126 KiB
C
Raw Normal View History

2007-10-31 14:44:14 +00:00
/*
2007-11-01 13:11:11 +00:00
* libev event processing core, watcher management
*
2014-01-16 11:51:05 +00:00
* Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann <libev@schmorp.de>
2007-10-31 14:44:14 +00:00
* All rights reserved.
*
2007-12-25 07:05:45 +00:00
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
2011-02-16 08:02:49 +00:00
*
2007-12-25 07:05:45 +00:00
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
2011-02-16 08:02:49 +00:00
*
2007-12-25 07:05:45 +00:00
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
2011-02-16 08:02:49 +00:00
*
2007-12-25 07:05:45 +00:00
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
2007-10-31 14:44:14 +00:00
*
2007-12-25 07:05:45 +00:00
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License ("GPL") version 2 or any later version,
* in which case the provisions of the GPL are applicable instead of
* the above. If you wish to allow the use of your version of this file
* only under the terms of the GPL and not to allow others to use your
* version of this file under the BSD license, indicate your decision
* by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file under
* either the BSD or the GPL.
2007-10-31 14:44:14 +00:00
*/
2007-11-10 03:36:15 +00:00
2008-04-06 09:53:16 +00:00
/* this big block deduces configuration from config.h */
2007-11-04 18:15:16 +00:00
#ifndef EV_STANDALONE
2007-11-23 11:32:21 +00:00
# ifdef EV_CONFIG_H
# include EV_CONFIG_H
# else
# include "config.h"
# endif
2007-11-04 18:29:44 +00:00
2014-09-05 16:21:19 +00:00
# if HAVE_FLOOR
# ifndef EV_USE_FLOOR
# define EV_USE_FLOOR 1
# endif
2011-02-20 02:56:23 +00:00
# endif
2008-11-20 00:35:10 +00:00
# if HAVE_CLOCK_SYSCALL
# ifndef EV_USE_CLOCK_SYSCALL
# define EV_USE_CLOCK_SYSCALL 1
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# endif
# endif
2012-04-02 20:12:16 +00:00
# elif !defined EV_USE_CLOCK_SYSCALL
2009-06-29 04:41:34 +00:00
# define EV_USE_CLOCK_SYSCALL 0
2008-11-20 00:35:10 +00:00
# endif
2007-11-04 18:29:44 +00:00
# if HAVE_CLOCK_GETTIME
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# endif
# ifndef EV_USE_REALTIME
2009-02-06 20:17:43 +00:00
# define EV_USE_REALTIME 0
# endif
2007-11-18 01:25:23 +00:00
# else
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
# endif
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
2007-11-04 18:29:44 +00:00
# endif
2010-04-02 21:03:45 +00:00
# if HAVE_NANOSLEEP
# ifndef EV_USE_NANOSLEEP
# define EV_USE_NANOSLEEP EV_FEATURE_OS
2007-12-22 05:47:56 +00:00
# endif
2010-04-02 21:03:45 +00:00
# else
# undef EV_USE_NANOSLEEP
# define EV_USE_NANOSLEEP 0
2007-12-22 05:47:56 +00:00
# endif
2010-04-02 21:03:45 +00:00
# if HAVE_SELECT && HAVE_SYS_SELECT_H
# ifndef EV_USE_SELECT
2010-03-16 00:43:22 +00:00
# define EV_USE_SELECT EV_FEATURE_BACKENDS
2007-11-18 02:17:57 +00:00
# endif
2010-04-02 21:03:45 +00:00
# else
# undef EV_USE_SELECT
# define EV_USE_SELECT 0
2007-11-04 18:29:44 +00:00
# endif
2010-04-02 21:03:45 +00:00
# if HAVE_POLL && HAVE_POLL_H
# ifndef EV_USE_POLL
2010-03-16 00:43:22 +00:00
# define EV_USE_POLL EV_FEATURE_BACKENDS
2007-11-18 02:17:57 +00:00
# endif
2010-04-02 21:03:45 +00:00
# else
# undef EV_USE_POLL
# define EV_USE_POLL 0
2007-11-04 18:29:44 +00:00
# endif
2007-11-18 02:17:57 +00:00
2010-04-02 21:03:45 +00:00
# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
# ifndef EV_USE_EPOLL
2010-03-16 00:43:22 +00:00
# define EV_USE_EPOLL EV_FEATURE_BACKENDS
2007-11-18 02:17:57 +00:00
# endif
2010-04-02 21:03:45 +00:00
# else
# undef EV_USE_EPOLL
# define EV_USE_EPOLL 0
2007-11-04 18:29:44 +00:00
# endif
2007-11-18 02:17:57 +00:00
2010-04-02 21:03:45 +00:00
# if HAVE_KQUEUE && HAVE_SYS_EVENT_H
# ifndef EV_USE_KQUEUE
2010-03-16 00:43:22 +00:00
# define EV_USE_KQUEUE EV_FEATURE_BACKENDS
2007-11-18 02:17:57 +00:00
# endif
2010-04-02 21:03:45 +00:00
# else
# undef EV_USE_KQUEUE
# define EV_USE_KQUEUE 0
2007-11-04 18:29:44 +00:00
# endif
2007-11-18 02:17:57 +00:00
2010-04-02 21:03:45 +00:00
# if HAVE_PORT_H && HAVE_PORT_CREATE
# ifndef EV_USE_PORT
2010-03-16 00:43:22 +00:00
# define EV_USE_PORT EV_FEATURE_BACKENDS
2007-11-18 02:17:57 +00:00
# endif
2010-04-02 21:03:45 +00:00
# else
# undef EV_USE_PORT
# define EV_USE_PORT 0
2007-11-16 01:33:53 +00:00
# endif
2010-04-02 21:03:45 +00:00
# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
# ifndef EV_USE_INOTIFY
2010-03-16 00:43:22 +00:00
# define EV_USE_INOTIFY EV_FEATURE_OS
# endif
2010-04-02 21:03:45 +00:00
# else
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
# endif
2010-04-02 21:03:45 +00:00
# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
# ifndef EV_USE_SIGNALFD
2010-03-16 00:43:22 +00:00
# define EV_USE_SIGNALFD EV_FEATURE_OS
2009-07-19 01:36:34 +00:00
# endif
2010-04-02 21:03:45 +00:00
# else
# undef EV_USE_SIGNALFD
# define EV_USE_SIGNALFD 0
2009-07-19 01:36:34 +00:00
# endif
2010-04-02 21:03:45 +00:00
# if HAVE_EVENTFD
# ifndef EV_USE_EVENTFD
2010-03-16 00:43:22 +00:00
# define EV_USE_EVENTFD EV_FEATURE_OS
2008-04-06 09:53:16 +00:00
# endif
2010-04-02 21:03:45 +00:00
# else
# undef EV_USE_EVENTFD
# define EV_USE_EVENTFD 0
2008-04-06 09:53:16 +00:00
# endif
2008-05-22 02:44:57 +00:00
#endif
2007-10-31 14:44:14 +00:00
#include <stdlib.h>
2009-11-18 10:25:22 +00:00
#include <string.h>
2007-10-31 00:24:16 +00:00
#include <fcntl.h>
2007-10-31 13:57:34 +00:00
#include <stddef.h>
#include <stdio.h>
2007-10-30 23:10:33 +00:00
#include <assert.h>
#include <errno.h>
2007-10-31 19:07:43 +00:00
#include <sys/types.h>
#include <time.h>
2010-01-26 04:19:37 +00:00
#include <limits.h>
2007-11-06 16:09:37 +00:00
#include <signal.h>
#ifdef EV_H
# include EV_H
#else
# include "ev.h"
#endif
2012-02-04 17:57:55 +00:00
#if EV_NO_THREADS
# undef EV_NO_SMP
# define EV_NO_SMP 1
# undef ECB_NO_THREADS
# define ECB_NO_THREADS 1
#endif
#if EV_NO_SMP
# undef EV_NO_SMP
# define ECB_NO_SMP 1
#endif
#ifndef _WIN32
# include <sys/time.h>
2007-11-03 09:19:58 +00:00
# include <sys/wait.h>
# include <unistd.h>
#else
2008-06-19 06:53:49 +00:00
# include <io.h>
# define WIN32_LEAN_AND_MEAN
2012-05-08 15:44:09 +00:00
# include <winsock2.h>
2012-05-09 16:50:23 +00:00
# include <windows.h>
# ifndef EV_SELECT_IS_WINSOCKET
# define EV_SELECT_IS_WINSOCKET 1
# endif
2010-03-09 08:55:03 +00:00
# undef EV_AVOID_STDIO
2007-11-03 09:19:58 +00:00
#endif
2010-07-09 20:55:14 +00:00
/* OS X, in its infinite idiocy, actually HARDCODES
* a limit of 1024 into their select. Where people have brains,
* OS X engineers apparently have a vacuum. Or maybe they were
* ordered to have a vacuum, or they do anything for money.
* This might help. Or not.
*/
#define _DARWIN_UNLIMITED_SELECT 1
2008-04-06 09:53:16 +00:00
/* this block tries to deduce configuration from header-defined symbols and defaults */
2007-11-02 11:02:23 +00:00
2009-07-19 03:49:04 +00:00
/* try to deduce the maximum number of signals on this platform */
2012-04-02 20:12:16 +00:00
#if defined EV_NSIG
2009-07-19 03:49:04 +00:00
/* use what's provided */
2012-04-02 20:12:16 +00:00
#elif defined NSIG
2009-07-19 03:49:04 +00:00
# define EV_NSIG (NSIG)
2012-04-02 20:12:16 +00:00
#elif defined _NSIG
2009-07-19 03:49:04 +00:00
# define EV_NSIG (_NSIG)
2012-04-02 20:12:16 +00:00
#elif defined SIGMAX
2009-07-19 03:49:04 +00:00
# define EV_NSIG (SIGMAX+1)
2012-04-02 20:12:16 +00:00
#elif defined SIG_MAX
2009-07-19 03:49:04 +00:00
# define EV_NSIG (SIG_MAX+1)
2012-04-02 20:12:16 +00:00
#elif defined _SIG_MAX
2009-07-19 03:49:04 +00:00
# define EV_NSIG (_SIG_MAX+1)
2012-04-02 20:12:16 +00:00
#elif defined MAXSIG
2009-07-19 03:49:04 +00:00
# define EV_NSIG (MAXSIG+1)
2012-04-02 20:12:16 +00:00
#elif defined MAX_SIG
2009-07-19 03:49:04 +00:00
# define EV_NSIG (MAX_SIG+1)
2012-04-02 20:12:16 +00:00
#elif defined SIGARRAYSIZE
# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
2012-04-02 20:12:16 +00:00
#elif defined _sys_nsig
2009-07-19 03:49:04 +00:00
# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
#else
2013-10-29 12:13:37 +00:00
# define EV_NSIG (8 * sizeof (sigset_t) + 1)
2009-07-19 03:49:04 +00:00
#endif
2011-02-20 02:56:23 +00:00
#ifndef EV_USE_FLOOR
# define EV_USE_FLOOR 0
#endif
2008-11-20 00:35:10 +00:00
#ifndef EV_USE_CLOCK_SYSCALL
2013-10-29 12:53:38 +00:00
# if __linux && __GLIBC__ == 2 && __GLIBC_MINOR__ < 17
2010-03-16 00:20:16 +00:00
# define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
2008-11-20 00:35:10 +00:00
# else
# define EV_USE_CLOCK_SYSCALL 0
# endif
#endif
2014-09-07 13:44:21 +00:00
#if !(_POSIX_TIMERS > 0)
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
# endif
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
#endif
#ifndef EV_USE_MONOTONIC
2012-04-02 20:12:16 +00:00
# if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
2010-03-16 00:20:16 +00:00
# define EV_USE_MONOTONIC EV_FEATURE_OS
2008-05-31 03:13:26 +00:00
# else
# define EV_USE_MONOTONIC 0
# endif
2007-11-01 13:33:12 +00:00
#endif
2007-11-16 01:33:53 +00:00
#ifndef EV_USE_REALTIME
2009-02-06 20:17:43 +00:00
# define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL
2007-11-16 01:33:53 +00:00
#endif
2007-12-22 05:47:56 +00:00
#ifndef EV_USE_NANOSLEEP
2008-05-31 03:13:26 +00:00
# if _POSIX_C_SOURCE >= 199309L
2010-03-16 00:20:16 +00:00
# define EV_USE_NANOSLEEP EV_FEATURE_OS
2008-05-31 03:13:26 +00:00
# else
# define EV_USE_NANOSLEEP 0
# endif
2007-12-22 05:47:56 +00:00
#endif
#ifndef EV_USE_SELECT
2010-03-16 00:20:16 +00:00
# define EV_USE_SELECT EV_FEATURE_BACKENDS
#endif
2007-11-04 18:15:16 +00:00
#ifndef EV_USE_POLL
2007-11-12 00:39:45 +00:00
# ifdef _WIN32
# define EV_USE_POLL 0
# else
2010-03-16 00:20:16 +00:00
# define EV_USE_POLL EV_FEATURE_BACKENDS
2007-11-12 00:39:45 +00:00
# endif
#endif
#ifndef EV_USE_EPOLL
2008-04-06 09:53:16 +00:00
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
2010-03-16 00:20:16 +00:00
# define EV_USE_EPOLL EV_FEATURE_BACKENDS
2008-04-06 09:53:16 +00:00
# else
# define EV_USE_EPOLL 0
# endif
#endif
#ifndef EV_USE_KQUEUE
# define EV_USE_KQUEUE 0
#endif
2007-11-16 01:33:53 +00:00
#ifndef EV_USE_PORT
# define EV_USE_PORT 0
2007-11-02 11:02:23 +00:00
#endif
#ifndef EV_USE_INOTIFY
2008-04-06 09:53:16 +00:00
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
2010-03-16 00:20:16 +00:00
# define EV_USE_INOTIFY EV_FEATURE_OS
2008-04-06 09:53:16 +00:00
# else
# define EV_USE_INOTIFY 0
# endif
#endif
2007-11-27 19:23:31 +00:00
#ifndef EV_PID_HASHSIZE
2010-03-16 00:20:16 +00:00
# define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1
2007-11-27 19:23:31 +00:00
#endif
#ifndef EV_INOTIFY_HASHSIZE
2010-03-16 00:20:16 +00:00
# define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1
#endif
2008-04-06 09:53:16 +00:00
#ifndef EV_USE_EVENTFD
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
2010-03-16 00:20:16 +00:00
# define EV_USE_EVENTFD EV_FEATURE_OS
2008-04-06 09:53:16 +00:00
# else
# define EV_USE_EVENTFD 0
# endif
#endif
2009-07-19 01:36:34 +00:00
#ifndef EV_USE_SIGNALFD
2009-08-26 17:31:20 +00:00
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
2010-03-16 00:20:16 +00:00
# define EV_USE_SIGNALFD EV_FEATURE_OS
2009-07-19 01:36:34 +00:00
# else
# define EV_USE_SIGNALFD 0
# endif
#endif
2008-05-21 23:30:52 +00:00
#if 0 /* debugging */
2008-05-22 02:44:57 +00:00
# define EV_VERIFY 3
2008-05-21 23:30:52 +00:00
# define EV_USE_4HEAP 1
# define EV_HEAP_CACHE_AT 1
#endif
2008-05-22 02:44:57 +00:00
#ifndef EV_VERIFY
2010-03-16 00:20:16 +00:00
# define EV_VERIFY (EV_FEATURE_API ? 1 : 0)
2008-05-22 02:44:57 +00:00
#endif
2008-05-09 15:52:13 +00:00
#ifndef EV_USE_4HEAP
2010-03-16 00:20:16 +00:00
# define EV_USE_4HEAP EV_FEATURE_DATA
2008-05-09 15:52:13 +00:00
#endif
#ifndef EV_HEAP_CACHE_AT
2010-03-16 00:20:16 +00:00
# define EV_HEAP_CACHE_AT EV_FEATURE_DATA
2008-05-09 15:52:13 +00:00
#endif
2017-06-01 20:25:50 +00:00
#ifdef __ANDROID__
/* supposedly, android doesn't typedef fd_mask */
# undef EV_USE_SELECT
# define EV_USE_SELECT 0
/* supposedly, we need to include syscall.h, not sys/syscall.h, so just disable */
# undef EV_USE_CLOCK_SYSCALL
# define EV_USE_CLOCK_SYSCALL 0
#endif
/* aix's poll.h seems to cause lots of trouble */
#ifdef _AIX
/* AIX has a completely broken poll.h header */
# undef EV_USE_POLL
# define EV_USE_POLL 0
#endif
2009-06-29 04:41:34 +00:00
/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
/* which makes programs even slower. might work on other unices, too. */
#if EV_USE_CLOCK_SYSCALL
2012-04-22 10:14:20 +00:00
# include <sys/syscall.h>
2009-06-29 04:41:34 +00:00
# ifdef SYS_clock_gettime
# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
# undef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# else
# undef EV_USE_CLOCK_SYSCALL
# define EV_USE_CLOCK_SYSCALL 0
# endif
#endif
2008-04-06 09:53:16 +00:00
/* this block fixes any misconfiguration where we know we run into trouble otherwise */
2007-11-02 11:02:23 +00:00
#ifndef CLOCK_MONOTONIC
# undef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
#endif
2007-11-01 09:05:33 +00:00
#ifndef CLOCK_REALTIME
2007-11-02 11:02:23 +00:00
# undef EV_USE_REALTIME
2007-11-01 09:05:33 +00:00
# define EV_USE_REALTIME 0
#endif
2007-11-02 11:02:23 +00:00
#if !EV_STAT_ENABLE
2007-12-14 18:22:30 +00:00
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
#endif
2007-12-22 05:47:56 +00:00
#if !EV_USE_NANOSLEEP
2011-01-30 19:05:40 +00:00
/* hp-ux has it in sys/time.h, which we unconditionally include above */
2012-04-02 20:12:16 +00:00
# if !defined _WIN32 && !defined __hpux
2007-12-22 05:47:56 +00:00
# include <sys/select.h>
# endif
#endif
#if EV_USE_INOTIFY
2008-11-03 14:27:06 +00:00
# include <sys/statfs.h>
# include <sys/inotify.h>
2008-10-01 18:50:03 +00:00
/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
# ifndef IN_DONT_FOLLOW
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
# endif
#endif
2008-04-06 09:53:16 +00:00
#if EV_USE_EVENTFD
/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
2008-04-06 12:44:49 +00:00
# include <stdint.h>
2009-07-19 01:36:34 +00:00
# ifndef EFD_NONBLOCK
# define EFD_NONBLOCK O_NONBLOCK
# endif
# ifndef EFD_CLOEXEC
2009-07-29 09:36:05 +00:00
# ifdef O_CLOEXEC
# define EFD_CLOEXEC O_CLOEXEC
# else
# define EFD_CLOEXEC 02000000
# endif
2009-07-19 01:36:34 +00:00
# endif
2010-10-22 09:24:11 +00:00
EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
2008-04-06 09:53:16 +00:00
#endif
2009-07-19 01:36:34 +00:00
#if EV_USE_SIGNALFD
2009-08-26 17:31:20 +00:00
/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
# include <stdint.h>
# ifndef SFD_NONBLOCK
# define SFD_NONBLOCK O_NONBLOCK
# endif
# ifndef SFD_CLOEXEC
# ifdef O_CLOEXEC
# define SFD_CLOEXEC O_CLOEXEC
# else
# define SFD_CLOEXEC 02000000
# endif
# endif
2010-10-22 09:24:11 +00:00
EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
2009-08-26 17:31:20 +00:00
struct signalfd_siginfo
{
uint32_t ssi_signo;
char pad[128 - sizeof (uint32_t)];
};
2009-07-19 01:36:34 +00:00
#endif
2007-11-02 11:02:23 +00:00
/**/
2008-05-22 02:44:57 +00:00
#if EV_VERIFY >= 3
2010-03-16 20:39:29 +00:00
# define EV_FREQUENT_CHECK ev_verify (EV_A)
2008-05-21 23:25:21 +00:00
#else
# define EV_FREQUENT_CHECK do { } while (0)
#endif
2007-12-11 04:31:55 +00:00
/*
2011-02-20 02:56:23 +00:00
* This is used to work around floating point rounding problems.
2007-12-11 15:06:50 +00:00
* This value is good at least till the year 4000.
2007-12-11 04:31:55 +00:00
*/
2011-02-20 02:56:23 +00:00
#define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */
/*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */
2007-12-11 04:31:55 +00:00
2007-10-30 23:10:33 +00:00
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
2007-11-16 01:54:25 +00:00
#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
2010-10-15 22:44:41 +00:00
#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
2010-10-15 22:48:25 +00:00
#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
2010-10-15 22:44:41 +00:00
2011-08-04 13:57:16 +00:00
/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
/* ECB.H BEGIN */
/*
* libecb - http://software.schmorp.de/pkg/libecb
*
2015-02-11 19:20:21 +00:00
* Copyright (©) 2009-2015 Marc Alexander Lehmann <libecb@schmorp.de>
2011-08-04 13:57:16 +00:00
* Copyright (©) 2011 Emanuele Giaquinta
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
2014-05-16 15:15:39 +00:00
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License ("GPL") version 2 or any later version,
* in which case the provisions of the GPL are applicable instead of
* the above. If you wish to allow the use of your version of this file
* only under the terms of the GPL and not to allow others to use your
* version of this file under the BSD license, indicate your decision
* by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file under
* either the BSD or the GPL.
2011-08-04 13:57:16 +00:00
*/
#ifndef ECB_H
#define ECB_H
2012-05-29 21:03:22 +00:00
/* 16 bits major, 16 bits minor */
2015-12-20 01:31:17 +00:00
#define ECB_VERSION 0x00010005
2012-05-29 21:03:22 +00:00
2011-08-04 13:57:16 +00:00
#ifdef _WIN32
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;
#if __GNUC__
typedef signed long long int64_t;
typedef unsigned long long uint64_t;
#else /* _MSC_VER || __BORLANDC__ */
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
#endif
2012-05-29 21:03:22 +00:00
#ifdef _WIN64
#define ECB_PTRSIZE 8
typedef uint64_t uintptr_t;
typedef int64_t intptr_t;
#else
#define ECB_PTRSIZE 4
typedef uint32_t uintptr_t;
typedef int32_t intptr_t;
#endif
2011-08-04 13:57:16 +00:00
#else
#include <inttypes.h>
2015-12-20 01:31:17 +00:00
#if (defined INTPTR_MAX ? INTPTR_MAX : ULONG_MAX) > 0xffffffffU
2012-05-29 21:03:22 +00:00
#define ECB_PTRSIZE 8
#else
#define ECB_PTRSIZE 4
#endif
2011-08-04 13:57:16 +00:00
#endif
2011-06-19 17:55:13 +00:00
2015-04-01 06:57:41 +00:00
#define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__)
#define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64)
2013-03-01 11:13:22 +00:00
/* work around x32 idiocy by defining proper macros */
2015-04-01 06:57:41 +00:00
#if ECB_GCC_AMD64 || ECB_MSVC_AMD64
2013-10-27 16:26:07 +00:00
#if _ILP32
2013-03-01 11:13:22 +00:00
#define ECB_AMD64_X32 1
#else
#define ECB_AMD64 1
#endif
#endif
2011-06-19 17:55:13 +00:00
/* many compilers define _GNUC_ to some versions but then only implement
* what their idiot authors think are the "more important" extensions,
2011-08-04 13:57:16 +00:00
* causing enormous grief in return for some better fake benchmark numbers.
2011-06-19 17:55:13 +00:00
* or so.
* we try to detect these and simply assume they are not gcc - if they have
* an issue with that they should have done it right in the first place.
*/
2015-02-11 19:20:21 +00:00
#if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__
#define ECB_GCC_VERSION(major,minor) 0
#else
#define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
#endif
#define ECB_CLANG_VERSION(major,minor) (__clang_major__ > (major) || (__clang_major__ == (major) && __clang_minor__ >= (minor)))
#if __clang__ && defined __has_builtin
#define ECB_CLANG_BUILTIN(x) __has_builtin (x)
#else
#define ECB_CLANG_BUILTIN(x) 0
#endif
#if __clang__ && defined __has_extension
#define ECB_CLANG_EXTENSION(x) __has_extension (x)
#else
#define ECB_CLANG_EXTENSION(x) 0
2011-06-19 17:55:13 +00:00
#endif
2012-05-29 21:03:22 +00:00
#define ECB_CPP (__cplusplus+0)
#define ECB_CPP11 (__cplusplus >= 201103L)
2014-03-21 16:41:04 +00:00
#if ECB_CPP
#define ECB_C 0
#define ECB_STDC_VERSION 0
#else
#define ECB_C 1
#define ECB_STDC_VERSION __STDC_VERSION__
#endif
#define ECB_C99 (ECB_STDC_VERSION >= 199901L)
#define ECB_C11 (ECB_STDC_VERSION >= 201112L)
2012-10-08 15:43:35 +00:00
#if ECB_CPP
#define ECB_EXTERN_C extern "C"
#define ECB_EXTERN_C_BEG ECB_EXTERN_C {
#define ECB_EXTERN_C_END }
#else
#define ECB_EXTERN_C extern
#define ECB_EXTERN_C_BEG
#define ECB_EXTERN_C_END
#endif
2011-08-04 13:57:16 +00:00
/*****************************************************************************/
/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
2012-02-04 17:57:55 +00:00
#if ECB_NO_THREADS
2012-05-29 21:06:11 +00:00
#define ECB_NO_SMP 1
2012-02-04 17:57:55 +00:00
#endif
2012-05-29 21:03:22 +00:00
#if ECB_NO_SMP
2011-08-04 14:47:48 +00:00
#define ECB_MEMORY_FENCE do { } while (0)
2007-11-02 11:02:23 +00:00
#endif
2015-05-01 17:23:34 +00:00
/* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */
#if __xlC__ && ECB_CPP
#include <builtins.h>
#endif
2015-12-20 01:31:17 +00:00
#if 1400 <= _MSC_VER
#include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */
#endif
2011-07-20 00:40:14 +00:00
#ifndef ECB_MEMORY_FENCE
2012-04-02 20:22:30 +00:00
#if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
2012-01-18 12:44:32 +00:00
#if __i386 || __i386__
2011-07-20 00:40:14 +00:00
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
2012-05-29 21:03:22 +00:00
#define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
#define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
2015-04-01 06:57:41 +00:00
#elif ECB_GCC_AMD64
2012-05-29 21:03:22 +00:00
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
#define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
#define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
2011-08-04 14:37:49 +00:00
#elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
2012-05-29 21:03:22 +00:00
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
2015-12-20 01:31:17 +00:00
#elif defined __ARM_ARCH_2__ \
|| defined __ARM_ARCH_3__ || defined __ARM_ARCH_3M__ \
|| defined __ARM_ARCH_4__ || defined __ARM_ARCH_4T__ \
|| defined __ARM_ARCH_5__ || defined __ARM_ARCH_5E__ \
|| defined __ARM_ARCH_5T__ || defined __ARM_ARCH_5TE__ \
|| defined __ARM_ARCH_5TEJ__
/* should not need any, unless running old code on newer cpu - arm doesn't support that */
2012-04-02 20:22:30 +00:00
#elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
2015-12-20 01:31:17 +00:00
|| defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ \
|| defined __ARM_ARCH_6T2__
2012-03-30 17:43:55 +00:00
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
2012-04-02 20:22:30 +00:00
#elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
2015-12-20 01:31:17 +00:00
|| defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__
2012-05-29 21:03:22 +00:00
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
2014-03-21 16:41:04 +00:00
#elif __aarch64__
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory")
2015-08-09 00:13:28 +00:00
#elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8)
2012-05-29 21:03:22 +00:00
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
#define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
#define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
2012-04-02 20:22:30 +00:00
#elif defined __s390__ || defined __s390x__
2012-01-27 22:28:49 +00:00
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
2012-04-02 20:22:30 +00:00
#elif defined __mips__
2013-10-27 16:26:07 +00:00
/* GNU/Linux emulates sync on mips1 architectures, so we force its use */
2013-07-04 22:32:23 +00:00
/* anybody else who still uses mips1 is supposed to send in their version, with detection code. */
#define ECB_MEMORY_FENCE __asm__ __volatile__ (".set mips2; sync; .set mips0" : : : "memory")
2012-04-12 04:10:15 +00:00
#elif defined __alpha__
2012-05-29 21:03:22 +00:00
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
#elif defined __hppa__
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
#define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
#elif defined __ia64__
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory")
2013-09-05 18:45:29 +00:00
#elif defined __m68k__
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
#elif defined __m88k__
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("tb1 0,%%r0,128" : : : "memory")
#elif defined __sh__
#define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
2011-07-20 00:40:14 +00:00
#endif
#endif
#endif
#ifndef ECB_MEMORY_FENCE
2012-05-29 21:03:22 +00:00
#if ECB_GCC_VERSION(4,7)
2012-05-31 15:47:59 +00:00
/* see comment below (stdatomic.h) about the C11 memory model. */
2012-05-29 21:03:22 +00:00
#define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
2014-03-21 16:41:04 +00:00
#define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE)
#define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE)
2012-10-08 15:43:35 +00:00
2015-02-11 19:20:21 +00:00
#elif ECB_CLANG_EXTENSION(c_atomic)
/* see comment below (stdatomic.h) about the C11 memory model. */
#define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
#define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE)
#define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE)
2012-10-08 15:43:35 +00:00
2012-05-29 21:03:22 +00:00
#elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__