libev/ev.c

1281 lines
26 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
*
2007-10-31 14:44:14 +00:00
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, 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 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if EV_USE_CONFIG_H
# include "config.h"
#endif
2007-10-31 14:44:14 +00:00
#include <math.h>
#include <stdlib.h>
2007-10-31 00:24:16 +00:00
#include <unistd.h>
#include <fcntl.h>
#include <signal.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>
2007-11-03 09:19:58 +00:00
#ifndef WIN32
# include <sys/wait.h>
#endif
#include <sys/time.h>
#include <time.h>
2007-11-02 11:02:23 +00:00
/**/
#ifndef EV_USE_MONOTONIC
2007-11-01 13:33:12 +00:00
# define EV_USE_MONOTONIC 1
#endif
#ifndef EV_USE_SELECT
# define EV_USE_SELECT 1
#endif
#ifndef EV_USE_POLL
# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
#endif
#ifndef EV_USE_EPOLL
# define EV_USE_EPOLL 0
#endif
#ifndef EV_USE_KQUEUE
# define EV_USE_KQUEUE 0
#endif
2007-11-02 11:02:23 +00:00
#ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 1
#endif
/**/
#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
/**/
2007-10-30 23:10:33 +00:00
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
2007-11-02 11:02:23 +00:00
#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
2007-11-01 09:05:33 +00:00
#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
2007-11-02 11:02:23 +00:00
/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
#include "ev.h"
2007-11-02 11:02:23 +00:00
#if __GNUC__ >= 3
# define expect(expr,value) __builtin_expect ((expr),(value))
# define inline inline
#else
# define expect(expr,value) (expr)
# define inline static
#endif
#define expect_false(expr) expect ((expr) != 0, 0)
#define expect_true(expr) expect ((expr) != 0, 1)
2007-11-02 20:05:05 +00:00
#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
#define ABSPRI(w) ((w)->priority - EV_MINPRI)
typedef struct ev_watcher *W;
typedef struct ev_watcher_list *WL;
2007-10-31 09:23:17 +00:00
typedef struct ev_watcher_time *WT;
2007-11-02 11:02:23 +00:00
static ev_tstamp now_floor, now, diff; /* monotonic clock */
ev_tstamp ev_now;
int ev_method;
static int have_monotonic; /* runtime */
static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
2007-10-30 23:54:38 +00:00
static void (*method_modify)(int fd, int oev, int nev);
static void (*method_poll)(ev_tstamp timeout);
2007-10-31 00:32:33 +00:00
/*****************************************************************************/
ev_tstamp
ev_time (void)
{
#if EV_USE_REALTIME
struct timespec ts;
clock_gettime (CLOCK_REALTIME, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
#else
struct timeval tv;
gettimeofday (&tv, 0);
return tv.tv_sec + tv.tv_usec * 1e-6;
#endif
}
static ev_tstamp
get_clock (void)
{
#if EV_USE_MONOTONIC
2007-11-02 11:02:23 +00:00
if (expect_true (have_monotonic))
{
struct timespec ts;
clock_gettime (CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
#endif
return ev_time ();
}
#define array_roundsize(base,n) ((n) | 4 & ~3)
#define array_needsize(base,cur,cnt,init) \
2007-11-02 11:02:23 +00:00
if (expect_false ((cnt) > cur)) \
{ \
2007-10-31 20:10:17 +00:00
int newcnt = cur; \
do \
{ \
newcnt = array_roundsize (base, newcnt << 1); \
2007-10-31 20:10:17 +00:00
} \
while ((cnt) > newcnt); \
\
base = realloc (base, sizeof (*base) * (newcnt)); \
init (base + cur, newcnt - cur); \
cur = newcnt; \
}
2007-10-31 00:32:33 +00:00
/*****************************************************************************/
typedef struct
{
struct ev_io *head;
2007-11-01 11:11:22 +00:00
unsigned char events;
unsigned char reify;
} ANFD;
static ANFD *anfds;
static int anfdmax;
static void
anfds_init (ANFD *base, int count)
{
while (count--)
{
2007-10-31 22:16:36 +00:00
base->head = 0;
base->events = EV_NONE;
2007-11-01 11:11:22 +00:00
base->reify = 0;
++base;
}
}
typedef struct
{
W w;
int events;
} ANPENDING;
2007-11-02 20:05:05 +00:00
static ANPENDING *pendings [NUMPRI];
static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
static void
event (W w, int events)
{
2007-11-01 09:21:51 +00:00
if (w->pending)
{
2007-11-02 20:05:05 +00:00
pendings [ABSPRI (w)][w->pending - 1].events |= events;
2007-11-01 09:21:51 +00:00
return;
}
2007-11-02 20:05:05 +00:00
w->pending = ++pendingcnt [ABSPRI (w)];
array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
pendings [ABSPRI (w)][w->pending - 1].w = w;
pendings [ABSPRI (w)][w->pending - 1].events = events;
}
2007-10-31 22:16:36 +00:00
static void
queue_events (W *events, int eventcnt, int type)
{
int i;
for (i = 0; i < eventcnt; ++i)
event (events [i], type);
}
static void
fd_event (int fd, int events)
{
ANFD *anfd = anfds + fd;
struct ev_io *w;
for (w = anfd->head; w; w = w->next)
{
int ev = w->events & events;
if (ev)
event ((W)w, ev);
}
}
2007-10-31 22:16:36 +00:00
/*****************************************************************************/
static int *fdchanges;
static int fdchangemax, fdchangecnt;
static void
2007-10-31 22:16:36 +00:00
fd_reify (void)
{
int i;
2007-10-31 22:16:36 +00:00
for (i = 0; i < fdchangecnt; ++i)
{
int fd = fdchanges [i];
ANFD *anfd = anfds + fd;
struct ev_io *w;
int events = 0;
for (w = anfd->head; w; w = w->next)
events |= w->events;
2007-11-01 11:11:22 +00:00
anfd->reify = 0;
2007-10-31 22:16:36 +00:00
if (anfd->events != events)
{
method_modify (fd, anfd->events, events);
anfd->events = events;
}
}
fdchangecnt = 0;
}
static void
fd_change (int fd)
{
2007-11-01 11:11:22 +00:00
if (anfds [fd].reify || fdchangecnt < 0)
2007-10-31 22:16:36 +00:00
return;
2007-11-01 11:11:22 +00:00
anfds [fd].reify = 1;
2007-10-31 22:16:36 +00:00
++fdchangecnt;
array_needsize (fdchanges, fdchangemax, fdchangecnt, );
fdchanges [fdchangecnt - 1] = fd;
}
static void
fd_kill (int fd)
{
struct ev_io *w;
printf ("killing fd %d\n", fd);//D
while ((w = anfds [fd].head))
{
ev_io_stop (w);
event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
}
}
2007-10-31 17:55:55 +00:00
/* called on EBADF to verify fds */
static void
fd_ebadf (void)
2007-10-31 17:55:55 +00:00
{
int fd;
for (fd = 0; fd < anfdmax; ++fd)
2007-10-31 22:16:36 +00:00
if (anfds [fd].events)
2007-10-31 17:55:55 +00:00
if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
fd_kill (fd);
}
/* called on ENOMEM in select/poll to kill some fds and retry */
static void
fd_enomem (void)
{
int fd = anfdmax;
while (fd--)
if (anfds [fd].events)
{
close (fd);
fd_kill (fd);
return;
}
2007-10-31 17:55:55 +00:00
}
2007-10-31 00:32:33 +00:00
/*****************************************************************************/
2007-10-31 09:23:17 +00:00
static struct ev_timer **timers;
static int timermax, timercnt;
2007-10-30 23:10:33 +00:00
2007-10-31 09:23:17 +00:00
static struct ev_periodic **periodics;
static int periodicmax, periodiccnt;
static void
2007-10-31 09:23:17 +00:00
upheap (WT *timers, int k)
{
2007-10-31 09:23:17 +00:00
WT w = timers [k];
while (k && timers [k >> 1]->at > w->at)
{
timers [k] = timers [k >> 1];
timers [k]->active = k + 1;
k >>= 1;
}
timers [k] = w;
timers [k]->active = k + 1;
}
static void
2007-10-31 09:23:17 +00:00
downheap (WT *timers, int N, int k)
{
2007-10-31 09:23:17 +00:00
WT w = timers [k];
2007-10-30 23:10:33 +00:00
while (k < (N >> 1))
{
int j = k << 1;
2007-10-30 23:10:33 +00:00
if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
++j;
if (w->at <= timers [j]->at)
break;
timers [k] = timers [j];
2007-10-30 21:42:12 +00:00
timers [k]->active = k + 1;
k = j;
}
timers [k] = w;
timers [k]->active = k + 1;
}
2007-10-31 00:32:33 +00:00
/*****************************************************************************/
2007-10-31 00:24:16 +00:00
typedef struct
{
struct ev_signal *head;
2007-11-01 11:43:11 +00:00
sig_atomic_t volatile gotsig;
2007-10-31 00:24:16 +00:00
} ANSIG;
static ANSIG *signals;
2007-10-30 23:10:33 +00:00
static int signalmax;
2007-10-31 00:24:16 +00:00
static int sigpipe [2];
2007-11-01 11:43:11 +00:00
static sig_atomic_t volatile gotsig;
2007-10-31 00:24:16 +00:00
static struct ev_io sigev;
static void
2007-10-31 00:24:16 +00:00
signals_init (ANSIG *base, int count)
{
while (count--)
2007-10-31 00:24:16 +00:00
{
base->head = 0;
base->gotsig = 0;
2007-11-01 11:11:22 +00:00
2007-10-31 00:24:16 +00:00
++base;
}
}
static void
sighandler (int signum)
{
signals [signum - 1].gotsig = 1;
if (!gotsig)
{
gotsig = 1;
2007-11-01 11:43:11 +00:00
write (sigpipe [1], &signum, 1);
2007-10-31 00:24:16 +00:00
}
}
static void
sigcb (struct ev_io *iow, int revents)
{
struct ev_signal *w;
2007-11-01 15:21:13 +00:00
int signum;
2007-10-31 00:24:16 +00:00
read (sigpipe [0], &revents, 1);
2007-11-01 11:43:11 +00:00
gotsig = 0;
2007-10-31 00:24:16 +00:00
2007-11-01 15:21:13 +00:00
for (signum = signalmax; signum--; )
if (signals [signum].gotsig)
2007-10-31 00:24:16 +00:00
{
2007-11-01 15:21:13 +00:00
signals [signum].gotsig = 0;
2007-10-31 00:24:16 +00:00
2007-11-01 15:21:13 +00:00
for (w = signals [signum].head; w; w = w->next)
event ((W)w, EV_SIGNAL);
2007-10-31 00:24:16 +00:00
}
}
static void
siginit (void)
{
2007-11-03 09:19:58 +00:00
#ifndef WIN32
2007-10-31 00:24:16 +00:00
fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
/* rather than sort out wether we really need nb, set it */
fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
2007-11-03 09:19:58 +00:00
#endif
2007-10-31 00:24:16 +00:00
2007-11-01 06:48:49 +00:00
ev_io_set (&sigev, sigpipe [0], EV_READ);
ev_io_start (&sigev);
}
2007-10-31 00:32:33 +00:00
/*****************************************************************************/
static struct ev_idle **idles;
static int idlemax, idlecnt;
2007-10-31 18:28:00 +00:00
static struct ev_prepare **prepares;
static int preparemax, preparecnt;
static struct ev_check **checks;
static int checkmax, checkcnt;
/*****************************************************************************/
2007-10-31 19:07:43 +00:00
static struct ev_child *childs [PID_HASHSIZE];
static struct ev_signal childev;
2007-11-03 09:19:58 +00:00
#ifndef WIN32
2007-10-31 19:07:43 +00:00
#ifndef WCONTINUED
# define WCONTINUED 0
#endif
static void
childcb (struct ev_signal *sw, int revents)
{
struct ev_child *w;
int pid, status;
while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
2007-11-01 17:17:32 +00:00
if (w->pid == pid || !w->pid)
2007-10-31 19:07:43 +00:00
{
w->status = status;
event ((W)w, EV_CHILD);
}
}
2007-11-03 09:19:58 +00:00
#endif
2007-10-31 19:07:43 +00:00
/*****************************************************************************/
#if EV_USE_KQUEUE
# include "ev_kqueue.c"
#endif
#if EV_USE_EPOLL
# include "ev_epoll.c"
#endif
#if EV_USE_POLL
# include "ev_poll.c"
#endif
#if EV_USE_SELECT
# include "ev_select.c"
#endif
2007-10-31 20:46:44 +00:00
int
ev_version_major (void)
{
return EV_VERSION_MAJOR;
}
int
ev_version_minor (void)
{
return EV_VERSION_MINOR;
}
/* return true if we are running with elevated privileges and ignore env variables */
static int
enable_secure ()
{
return getuid () != geteuid ()
|| getgid () != getegid ();
}
int ev_init (int methods)
{
2007-10-31 20:10:17 +00:00
if (!ev_method)
{
#if EV_USE_MONOTONIC
2007-10-31 20:10:17 +00:00
{
struct timespec ts;
if (!clock_gettime (CLOCK_MONOTONIC, &ts))
have_monotonic = 1;
}
#endif
2007-11-02 11:02:23 +00:00
ev_now = ev_time ();
now = get_clock ();
now_floor = now;
diff = ev_now - now;
2007-10-31 20:10:17 +00:00
if (pipe (sigpipe))
return 0;
2007-10-31 00:24:16 +00:00
if (methods == EVMETHOD_AUTO)
if (!enable_secure () && getenv ("LIBEV_METHODS"))
methods = atoi (getenv ("LIBEV_METHODS"));
else
methods = EVMETHOD_ANY;
ev_method = 0;
#if EV_USE_KQUEUE
if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
#endif
#if EV_USE_EPOLL
if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
#endif
#if EV_USE_POLL
if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
#endif
#if EV_USE_SELECT
if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
#endif
2007-10-31 20:10:17 +00:00
if (ev_method)
{
2007-11-01 06:48:49 +00:00
ev_watcher_init (&sigev, sigcb);
2007-10-31 20:10:17 +00:00
siginit ();
2007-10-31 19:07:43 +00:00
2007-11-03 09:19:58 +00:00
#ifndef WIN32
2007-11-01 06:48:49 +00:00
ev_signal_init (&childev, childcb, SIGCHLD);
ev_signal_start (&childev);
2007-11-03 09:19:58 +00:00
#endif
2007-10-31 20:10:17 +00:00
}
2007-10-31 00:24:16 +00:00
}
return ev_method;
}
2007-10-31 00:32:33 +00:00
/*****************************************************************************/
2007-10-31 20:46:44 +00:00
void
2007-11-01 11:55:54 +00:00
ev_fork_prepare (void)
{
2007-10-31 07:40:49 +00:00
/* nop */
}
2007-10-31 20:46:44 +00:00
void
2007-11-01 11:55:54 +00:00
ev_fork_parent (void)
{
2007-10-31 07:40:49 +00:00
/* nop */
}
2007-10-31 20:46:44 +00:00
void
2007-11-01 11:55:54 +00:00
ev_fork_child (void)
{
#if EV_USE_EPOLL
2007-10-30 23:54:38 +00:00
if (ev_method == EVMETHOD_EPOLL)
epoll_postfork_child ();
#endif
2007-10-31 00:24:16 +00:00
2007-11-01 06:48:49 +00:00
ev_io_stop (&sigev);
2007-10-31 00:24:16 +00:00
close (sigpipe [0]);
close (sigpipe [1]);
pipe (sigpipe);
siginit ();
}
2007-10-31 00:32:33 +00:00
/*****************************************************************************/
static void
2007-10-31 20:46:44 +00:00
call_pending (void)
{
2007-11-02 20:05:05 +00:00
int pri;
2007-11-02 20:05:05 +00:00
for (pri = NUMPRI; pri--; )
while (pendingcnt [pri])
{
ANPENDING *p = pendings [pri] + --pendingcnt [pri];
if (p->w)
{
p->w->pending = 0;
p->w->cb (p->w, p->events);
}
}
}
static void
2007-10-31 20:46:44 +00:00
timers_reify (void)
{
2007-10-30 23:10:33 +00:00
while (timercnt && timers [0]->at <= now)
{
struct ev_timer *w = timers [0];
2007-10-30 23:10:33 +00:00
/* first reschedule or stop timer */
if (w->repeat)
{
2007-11-01 11:11:22 +00:00
assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
2007-10-31 09:23:17 +00:00
w->at = now + w->repeat;
downheap ((WT *)timers, timercnt, 0);
}
else
2007-11-01 06:48:49 +00:00
ev_timer_stop (w); /* nonrepeating: stop timer */
event ((W)w, EV_TIMEOUT);
2007-10-31 09:23:17 +00:00
}
}
static void
2007-10-31 20:46:44 +00:00
periodics_reify (void)
2007-10-31 09:23:17 +00:00
{
while (periodiccnt && periodics [0]->at <= ev_now)
{
struct ev_periodic *w = periodics [0];
/* first reschedule or stop timer */
if (w->interval)
2007-10-30 23:10:33 +00:00
{
2007-10-31 09:23:17 +00:00
w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
2007-11-01 11:11:22 +00:00
assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
2007-10-31 09:23:17 +00:00
downheap ((WT *)periodics, periodiccnt, 0);
2007-10-30 23:10:33 +00:00
}
2007-10-31 09:23:17 +00:00
else
2007-11-01 06:48:49 +00:00
ev_periodic_stop (w); /* nonrepeating: stop timer */
2007-11-01 11:11:22 +00:00
event ((W)w, EV_PERIODIC);
}
}
2007-10-31 09:23:17 +00:00
static void
2007-10-31 10:50:05 +00:00
periodics_reschedule (ev_tstamp diff)
2007-10-31 09:23:17 +00:00
{
int i;
2007-10-31 10:50:05 +00:00
/* adjust periodics after time jump */
2007-10-31 09:23:17 +00:00
for (i = 0; i < periodiccnt; ++i)
{
struct ev_periodic *w = periodics [i];
if (w->interval)
{
ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
if (fabs (diff) >= 1e-4)
{
2007-11-01 06:48:49 +00:00
ev_periodic_stop (w);
ev_periodic_start (w);
2007-10-31 09:23:17 +00:00
i = 0; /* restart loop, inefficient, but time jumps should be rare */
}
}
}
}
2007-11-02 11:02:23 +00:00
static int
time_update_monotonic (void)
{
now = get_clock ();
if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
{
ev_now = now + diff;
return 0;
}
else
{
now_floor = now;
ev_now = ev_time ();
return 1;
}
}
2007-10-30 23:10:33 +00:00
static void
2007-10-31 20:46:44 +00:00
time_update (void)
2007-10-30 23:10:33 +00:00
{
int i;
2007-10-31 09:23:17 +00:00
2007-11-02 11:02:23 +00:00
#if EV_USE_MONOTONIC
if (expect_true (have_monotonic))
2007-10-30 23:10:33 +00:00
{
2007-11-02 11:02:23 +00:00
if (time_update_monotonic ())
2007-10-30 23:10:33 +00:00
{
2007-11-02 11:02:23 +00:00
ev_tstamp odiff = diff;
2007-10-30 23:10:33 +00:00
2007-11-02 11:02:23 +00:00
for (i = 4; --i; ) /* loop a few times, before making important decisions */
{
diff = ev_now - now;
2007-10-30 23:10:33 +00:00
2007-11-02 11:02:23 +00:00
if (fabs (odiff - diff) < MIN_TIMEJUMP)
return; /* all is well */
2007-10-30 23:10:33 +00:00
2007-11-02 11:02:23 +00:00
ev_now = ev_time ();
now = get_clock ();
now_floor = now;
}
periodics_reschedule (diff - odiff);
/* no timer adjustment, as the monotonic clock doesn't jump */
}
2007-10-30 23:10:33 +00:00
}
else
2007-11-02 11:02:23 +00:00
#endif
2007-10-30 23:10:33 +00:00
{
2007-11-02 11:02:23 +00:00
ev_now = ev_time ();
if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
2007-10-31 10:50:05 +00:00
{
periodics_reschedule (ev_now - now);
/* adjust timers. this is easy, as the offset is the same for all */
for (i = 0; i < timercnt; ++i)
timers [i]->at += diff;
}
2007-10-30 23:10:33 +00:00
now = ev_now;
}
}
int ev_loop_done;
2007-10-30 23:10:33 +00:00
void ev_loop (int flags)
{
double block;
2007-10-31 21:50:15 +00:00
ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
do
{
2007-10-31 18:28:00 +00:00
/* queue check watchers (and execute them) */
2007-11-02 11:02:23 +00:00
if (expect_false (preparecnt))
2007-10-31 18:28:00 +00:00
{
queue_events ((W *)prepares, preparecnt, EV_PREPARE);
call_pending ();
}
/* update fd-related kernel structures */
2007-10-30 23:54:38 +00:00
fd_reify ();
/* calculate blocking time */
2007-10-31 09:23:17 +00:00
2007-10-31 18:37:38 +00:00
/* we only need this for !monotonic clockor timers, but as we basically
always have timers, we just calculate it always */
2007-11-02 11:02:23 +00:00
#if EV_USE_MONOTONIC
if (expect_true (have_monotonic))
time_update_monotonic ();
else
#endif
{
ev_now = ev_time ();
now = ev_now;
}
2007-10-31 09:23:17 +00:00
if (flags & EVLOOP_NONBLOCK || idlecnt)
block = 0.;
else
{
2007-10-30 23:10:33 +00:00
block = MAX_BLOCKTIME;
2007-10-31 09:23:17 +00:00
if (timercnt)
2007-10-30 23:10:33 +00:00
{
2007-11-02 11:02:23 +00:00
ev_tstamp to = timers [0]->at - now + method_fudge;
2007-10-30 23:10:33 +00:00