Browse Source

prepare for multiple bases

master
Marc Alexander Lehmann 14 years ago
parent
commit
1cb1e18a7a
7 changed files with 369 additions and 324 deletions
  1. +240
    -176
      ev.c
  2. +38
    -24
      ev.h
  3. +10
    -13
      ev_epoll.c
  4. +29
    -33
      ev_kqueue.c
  5. +9
    -12
      ev_poll.c
  6. +6
    -8
      ev_select.c
  7. +37
    -58
      event.c

+ 240
- 176
ev.c
File diff suppressed because it is too large
View File


+ 38
- 24
ev.h View File

@ -44,6 +44,11 @@ typedef double ev_tstamp;
# define EV_MAXPRI +2
#endif
#define EV_P void
#define EV_P_
#define EV_A
#define EV_A_
/* eventmask, revents, events... */
#define EV_UNDEF -1 /* guaranteed to be invalid */
#define EV_NONE 0x00
@ -82,7 +87,7 @@ typedef double ev_tstamp;
int pending; /* private */ \
int priority; /* ro */ \
EV_COMMON; /* rw */ \
void (*cb)(struct type *, int revents); /* rw */ /* gets invoked with an eventmask */
void (*cb)(EV_P_ struct type *, int revents); /* rw */ /* gets invoked with an eventmask */
#define EV_WATCHER_LIST(type) \
EV_WATCHER (type); \
@ -187,8 +192,8 @@ struct ev_child
#define EVMETHOD_PORT 32 /* NYI */
#define EVMETHOD_ANY ~0 /* any method, do not consult env */
#if EV_PROTOTYPES
extern int ev_method;
int ev_init (int methods); /* returns ev_method */
int ev_method (EV_P);
int ev_init (EV_P_ int methods); /* returns ev_method */
int ev_version_major (void);
int ev_version_minor (void);
@ -197,19 +202,28 @@ void ev_fork_prepare (void);
void ev_fork_parent (void);
void ev_fork_child (void);
extern ev_tstamp ev_now; /* time w.r.t. timers and the eventloop, updated after each poll */
ev_tstamp ev_time (void);
#endif
#define EVLOOP_NONBLOCK 1 /* do not block/wait */
#define EVLOOP_ONESHOT 2 /* block *once* only */
#if EV_PROTOTYPES
void ev_loop (int flags);
extern int ev_loop_done; /* set to 1 to break out of event loop, set to 2 to break out of all event loops */
void ev_loop (EV_P_ int flags);
void ev_unloop (EV_P_ int status); /* set to 1 to break out of event loop, set to 2 to break out of all event loops */
ev_tstamp ev_now (EV_P); /* time w.r.t. timers and the eventloop, updated after each poll */
/*
* ref/unref can be used to add or remove a refcount on the mainloop. every watcher
* keeps one reference. if you have a long-runing watcher you never unregister that
* should not keep ev_loop from running, unref() after starting, and ref() before stopping.
*/
void ev_ref (EV_P);
void ev_unref (EV_P);
/* convinience function, wait for a single event, without registering an event watcher */
/* if timeout is < 0, do wait indefinitely */
void ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg);
void ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg);
#endif
/* these may evaluate ev multiple times, and the other arguments at most once */
@ -240,30 +254,30 @@ void ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, voi
/* stopping (enabling, adding) a watcher does nothing if it is already running */
/* stopping (disabling, deleting) a watcher does nothing unless its already running */
#if EV_PROTOTYPES
void ev_io_start (struct ev_io *w);
void ev_io_stop (struct ev_io *w);
void ev_io_start (EV_P_ struct ev_io *w);
void ev_io_stop (EV_P_ struct ev_io *w);
void ev_timer_start (struct ev_timer *w);
void ev_timer_stop (struct ev_timer *w);
void ev_timer_again (struct ev_timer *w); /* stops if active and no repeat, restarts if active and repeating, starts if inactive and repeating */
void ev_timer_start (EV_P_ struct ev_timer *w);
void ev_timer_stop (EV_P_ struct ev_timer *w);
void ev_timer_again (EV_P_ struct ev_timer *w); /* stops if active and no repeat, restarts if active and repeating, starts if inactive and repeating */
void ev_periodic_start (struct ev_periodic *w);
void ev_periodic_stop (struct ev_periodic *w);
void ev_periodic_start (EV_P_ struct ev_periodic *w);
void ev_periodic_stop (EV_P_ struct ev_periodic *w);
void ev_signal_start (struct ev_signal *w);
void ev_signal_stop (struct ev_signal *w);
void ev_signal_start (EV_P_ struct ev_signal *w);
void ev_signal_stop (EV_P_ struct ev_signal *w);
void ev_idle_start (struct ev_idle *w);
void ev_idle_stop (struct ev_idle *w);
void ev_idle_start (EV_P_ struct ev_idle *w);
void ev_idle_stop (EV_P_ struct ev_idle *w);
void ev_prepare_start (struct ev_prepare *w);
void ev_prepare_stop (struct ev_prepare *w);
void ev_prepare_start (EV_P_ struct ev_prepare *w);
void ev_prepare_stop (EV_P_ struct ev_prepare *w);
void ev_check_start (struct ev_check *w);
void ev_check_stop (struct ev_check *w);
void ev_check_start (EV_P_ struct ev_check *w);
void ev_check_stop (EV_P_ struct ev_check *w);
void ev_child_start (struct ev_child *w);
void ev_child_stop (struct ev_child *w);
void ev_child_start (EV_P_ struct ev_child *w);
void ev_child_stop (EV_P_ struct ev_child *w);
#endif
#ifdef __cplusplus


+ 10
- 13
ev_epoll.c View File

@ -31,10 +31,8 @@
#include <sys/epoll.h>
static int epoll_fd = -1;
static void
epoll_modify (int fd, int oev, int nev)
epoll_modify (EV_P_ int fd, int oev, int nev)
{
int mode = nev ? oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD : EPOLL_CTL_DEL;
@ -48,7 +46,7 @@ epoll_modify (int fd, int oev, int nev)
}
static void
epoll_postfork_child (void)
epoll_postfork_child (EV_P)
{
int fd;
@ -58,14 +56,11 @@ epoll_postfork_child (void)
/* re-register interest in fds */
for (fd = 0; fd < anfdmax; ++fd)
if (anfds [fd].events)//D
epoll_modify (fd, EV_NONE, anfds [fd].events);
epoll_modify (EV_A_ fd, EV_NONE, anfds [fd].events);
}
static struct epoll_event *events;
static int eventmax;
static void
epoll_poll (ev_tstamp timeout)
epoll_poll (EV_P_ ev_tstamp timeout)
{
int eventcnt = epoll_wait (epoll_fd, events, eventmax, ceil (timeout * 1000.));
int i;
@ -75,6 +70,7 @@ epoll_poll (ev_tstamp timeout)
for (i = 0; i < eventcnt; ++i)
fd_event (
EV_A_
events [i].data.u64,
(events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
| (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0)
@ -89,22 +85,23 @@ epoll_poll (ev_tstamp timeout)
}
}
static void
epoll_init (int flags)
static int
epoll_init (EV_P_ int flags)
{
epoll_fd = epoll_create (256);
if (epoll_fd < 0)
return;
return 0;
fcntl (epoll_fd, F_SETFD, FD_CLOEXEC);
ev_method = EVMETHOD_EPOLL;
method_fudge = 1e-3; /* needed to compensate for epoll returning early */
method_modify = epoll_modify;
method_poll = epoll_poll;
eventmax = 64; /* intiial number of events receivable per poll */
events = malloc (sizeof (struct epoll_event) * eventmax);
return EVMETHOD_EPOLL;
}

+ 29
- 33
ev_kqueue.c View File

@ -34,20 +34,14 @@
#include <string.h>
#include <errno.h>
static int kq_fd;
static struct kevent *kq_changes;
static int kq_changemax, kq_changecnt;
static struct kevent *kq_events;
static int kq_eventmax;
static void
kqueue_change (int fd, int filter, int flags, int fflags)
kqueue_change (EV_P_ int fd, int filter, int flags, int fflags)
{
struct kevent *ke;
array_needsize (kq_changes, kq_changemax, ++kq_changecnt, );
array_needsize (kqueue_changes, kqueue_changemax, ++kqueue_changecnt, );
ke = &kq_changes [kq_changecnt - 1];
ke = &kqueue_changes [kqueue_changecnt - 1];
memset (ke, 0, sizeof (struct kevent));
ke->ident = fd;
ke->filter = filter;
@ -60,7 +54,7 @@ kqueue_change (int fd, int filter, int flags, int fflags)
#endif
static void
kqueue_modify (int fd, int oev, int nev)
kqueue_modify (EV_P_ int fd, int oev, int nev)
{
if ((oev ^ nev) & EV_READ)
{
@ -80,22 +74,22 @@ kqueue_modify (int fd, int oev, int nev)
}
static void
kqueue_poll (ev_tstamp timeout)
kqueue_poll (EV_P_ ev_tstamp timeout)
{
int res, i;
struct timespec ts;
ts.tv_sec = (time_t)timeout;
ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9;
res = kevent (kq_fd, kq_changes, kq_changecnt, kq_events, kq_eventmax, &ts);
kq_changecnt = 0;
res = kevent (kqueue_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts);
kqueue_changecnt = 0;
if (res < 0)
return;
for (i = 0; i < res; ++i)
{
if (kq_events [i].flags & EV_ERROR)
if (kqueue_events [i].flags & EV_ERROR)
{
/*
* Error messages that can happen, when a delete fails.
@ -109,34 +103,35 @@ kqueue_poll (ev_tstamp timeout)
* an event we are still processing. In that case
* the data field is set to ENOENT.
*/
if (kq_events [i].data == EBADF)
fd_kill (kq_events [i].ident);
if (kqueue_events [i].data == EBADF)
fd_kill (EV_A_ kqueue_events [i].ident);
}
else
fd_event (
kq_events [i].ident,
kq_events [i].filter == EVFILT_READ ? EV_READ
: kq_events [i].filter == EVFILT_WRITE ? EV_WRITE
EV_A_
kqueue_events [i].ident,
kqueue_events [i].filter == EVFILT_READ ? EV_READ
: kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE
: 0
);
}
if (expect_false (res == kq_eventmax))
if (expect_false (res == kqueue_eventmax))
{
free (kq_events);
kq_eventmax = array_roundsize (kq_events, kq_eventmax << 1);
kq_events = malloc (sizeof (struct kevent) * kq_eventmax);
free (kqueue_events);
kqueue_eventmax = array_roundsize (kqueue_events, kqueue_eventmax << 1);
kqueue_events = malloc (sizeof (struct kevent) * kqueue_eventmax);
}
}
static void
kqueue_init (int flags)
static int
kqueue_init (EV_P_ int flags)
{
struct kevent ch, ev;
/* Initalize the kernel queue */
if ((kq_fd = kqueue ()) < 0)
return;
if ((kqueue_fd = kqueue ()) < 0)
return 0;
/* Check for Mac OS X kqueue bug. */
ch.ident = -1;
@ -148,21 +143,22 @@ kqueue_init (int flags)
* stick an error in ev. If kqueue is broken, then
* kevent will fail.
*/
if (kevent (kq_fd, &ch, 1, &ev, 1, 0) != 1
if (kevent (kqueue_fd, &ch, 1, &ev, 1, 0) != 1
|| ev.ident != -1
|| ev.flags != EV_ERROR)
{
/* detected broken kqueue */
close (kq_fd);
return;
close (kqueue_fd);
return 0;
}
ev_method = EVMETHOD_KQUEUE;
method_fudge = 1e-3; /* needed to compensate for kevent returning early */
method_modify = kqueue_modify;
method_poll = kqueue_poll;
kq_eventmax = 64; /* intiial number of events receivable per poll */
kq_events = malloc (sizeof (struct kevent) * kq_eventmax);
kqueue_eventmax = 64; /* intiial number of events receivable per poll */
kqueue_events = malloc (sizeof (struct kevent) * kqueue_eventmax);
return EVMETHOD_KQUEUE;
}

+ 9
- 12
ev_poll.c View File

@ -31,11 +31,6 @@
#include <poll.h>
static struct pollfd *polls;
static int pollmax, pollcnt;
static int *pollidxs; /* maps fds into structure indices */
static int pollidxmax;
static void
pollidx_init (int *base, int count)
{
@ -44,7 +39,7 @@ pollidx_init (int *base, int count)
}
static void
poll_modify (int fd, int oev, int nev)
poll_modify (EV_P_ int fd, int oev, int nev)
{
int idx;
array_needsize (pollidxs, pollidxmax, fd + 1, pollidx_init);
@ -74,7 +69,7 @@ poll_modify (int fd, int oev, int nev)
}
static void
poll_poll (ev_tstamp timeout)
poll_poll (EV_P_ ev_tstamp timeout)
{
int res = poll (polls, pollcnt, ceil (timeout * 1000.));
@ -84,6 +79,7 @@ poll_poll (ev_tstamp timeout)
for (i = 0; i < pollcnt; ++i)
fd_event (
EV_A_
polls [i].fd,
(polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
| (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
@ -92,17 +88,18 @@ poll_poll (ev_tstamp timeout)
else if (res < 0)
{
if (errno == EBADF)
fd_ebadf ();
fd_ebadf (EV_A);
else if (errno == ENOMEM)
fd_enomem ();
fd_enomem (EV_A);
}
}
static void
poll_init (int flags)
static int
poll_init (EV_P_ int flags)
{
ev_method = EVMETHOD_POLL;
method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */
method_modify = poll_modify;
method_poll = poll_poll;
return EVMETHOD_POLL;
}

+ 6
- 8
ev_select.c View File

@ -42,11 +42,8 @@
#include <string.h>
#include <inttypes.h>
static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo;
static int vec_max;
static void
select_modify (int fd, int oev, int nev)
select_modify (EV_P_ int fd, int oev, int nev)
{
int offs = fd >> 3;
int mask = 1 << (fd & 7);
@ -75,7 +72,7 @@ select_modify (int fd, int oev, int nev)
}
static void
select_poll (ev_tstamp timeout)
select_poll (EV_P_ ev_tstamp timeout)
{
struct timeval tv;
int res;
@ -124,12 +121,13 @@ select_poll (ev_tstamp timeout)
}
}
static void
select_init (int flags)
static int
select_init (EV_P_ int flags)
{
ev_method = EVMETHOD_SELECT;
method_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */
method_modify = select_modify;
method_poll = select_poll;
return EVMETHOD_SELECT;
}

+ 37
- 58
event.c View File

@ -35,13 +35,14 @@
#include "event.h"
#define dLOOPev
#define dLOOPbase
struct event_base
{
int dummy;
};
static int x_actives;
static struct event_base x_base, *x_cur;
static void
@ -110,50 +111,33 @@ int event_loopexit (struct timeval *tv)
static void
x_cb (struct event *ev, int revents)
{
if (ev->ev_events & EV_SIGNAL)
{
/* sig */
if (ev_is_active (&ev->iosig.sig))
{
ev_signal_stop (&ev->iosig.sig);
--x_actives;
}
}
else
{
/* io */
if (!(ev->ev_events & EV_PERSIST) && ev_is_active (&ev->iosig.io))
{
ev_io_stop (&ev->iosig.io);
--x_actives;
}
}
revents &= EV_READ | EV_WRITE | EV_TIMEOUT | EV_SIGNAL;
if (revents & EV_TIMEOUT)
--x_actives;
ev->ev_res = revents;
ev->ev_callback (ev->ev_fd, revents, ev->ev_arg);
}
static void
x_cb_io (struct ev_io *w, int revents)
x_cb_sig (EV_P_ struct ev_signal *w, int revents)
{
x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.io)), revents);
x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents);
}
static void
x_cb_to (struct ev_timer *w, int revents)
x_cb_io (EV_P_ struct ev_io *w, int revents)
{
x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
struct event *ev = (struct event *)(((char *)w) - offsetof (struct event, iosig.io));
if (!(ev->ev_events & EV_PERSIST) && ev_is_active (w))
ev_io_stop (w);
x_cb (ev, revents);
}
static void
x_cb_sig (struct ev_signal *w, int revents)
x_cb_to (EV_P_ struct ev_timer *w, int revents)
{
x_cb ((struct event *)(((char *)w) - offsetof (struct event, iosig.sig)), revents);
x_cb ((struct event *)(((char *)w) - offsetof (struct event, to)), revents);
}
void event_set (struct event *ev, int fd, short events, void (*cb)(int, short, void *), void *arg)
@ -181,27 +165,26 @@ int event_once (int fd, short events, void (*cb)(int, short, void *), void *arg,
int event_add (struct event *ev, struct timeval *tv)
{
dLOOPev;
/* disable all watchers */
event_del (ev);
if (ev->ev_events & EV_SIGNAL)
{
ev_signal_set (&ev->iosig.sig, ev->ev_fd);
ev_signal_start (&ev->iosig.sig);
++x_actives;
ev_signal_start (EV_A_ &ev->iosig.sig);
}
else if (ev->ev_events & (EV_READ | EV_WRITE))
{
ev_io_set (&ev->iosig.io, ev->ev_fd, ev->ev_events & (EV_READ | EV_WRITE));
ev_io_start (&ev->iosig.io);
++x_actives;
ev_io_start (EV_A_ &ev->iosig.io);
}
if (tv)
{
ev_timer_set (&ev->to, tv_get (tv), 0.);
ev_timer_start (&ev->to);
++x_actives;
ev_timer_start (EV_A_ &ev->to);
}
return 0;
@ -209,36 +192,31 @@ int event_add (struct event *ev, struct timeval *tv)
int event_del (struct event *ev)
{
dLOOPev;
if (ev->ev_events & EV_SIGNAL)
{
/* sig */
if (ev_is_active (&ev->iosig.sig))
{
ev_signal_stop (&ev->iosig.sig);
--x_actives;
}
ev_signal_stop (EV_A_ &ev->iosig.sig);
}
else
{
/* io */
if (ev_is_active (&ev->iosig.io))
{
ev_io_stop (&ev->iosig.io);
--x_actives;
}
ev_io_stop (EV_A_ &ev->iosig.io);
}
if (ev_is_active (&ev->to))
{
ev_timer_stop (&ev->to);
--x_actives;
}
ev_timer_stop (EV_A_ &ev->to);
return 0;
}
int event_pending (struct event *ev, short events, struct timeval *tv)
{
dLOOPev;
short revents = 0;
if (ev->ev_events & EV_SIGNAL)
@ -259,7 +237,7 @@ int event_pending (struct event *ev, short events, struct timeval *tv)
revents |= EV_TIMEOUT;
if (tv)
tv_set (tv, ev_now); /* not sure if this is right :) */
tv_set (tv, ev_now (EV_A)); /* not sure if this is right :) */
}
return events & revents;
@ -286,11 +264,8 @@ int event_base_set (struct event_base *base, struct event *ev)
int event_base_loop (struct event_base *base, int flags)
{
do
{
ev_loop (flags | EVLOOP_ONESHOT);
}
while (!(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) && x_actives && !ev_loop_done);
dLOOPbase;
ev_loop (EV_A_ flags | EVLOOP_ONESHOT);
return 0;
}
@ -301,16 +276,17 @@ int event_base_dispatch (struct event_base *base)
}
static void
x_loopexit_cb (int revents, void *arg)
x_loopexit_cb (EV_P_ int revents, void *arg)
{
ev_loop_done = 2;
ev_unloop (EV_A_ 2);
}
int event_base_loopexit (struct event_base *base, struct timeval *tv)
{
dLOOPbase;
ev_tstamp after = tv_get (tv);
ev_once (-1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
ev_once (EV_A_ -1, 0, after >= 0. ? after : 0., x_loopexit_cb, (void *)base);
return -1;
}
@ -333,6 +309,7 @@ x_once_cb (int revents, void *arg)
int event_base_once (struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg, struct timeval *tv)
{
dLOOPbase;
struct x_once *once = malloc (sizeof (struct x_once));
if (!once)
@ -342,13 +319,15 @@ int event_base_once (struct event_base *base, int fd, short events, void (*cb)(i
once->cb = cb;
once->arg = arg;
ev_once (fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
ev_once (EV_A_ fd, events & (EV_READ | EV_WRITE), tv_get (tv), x_once_cb, (void *)once);
return 0;
}
int event_base_priority_init (struct event_base *base, int npri)
{
dLOOPbase;
return 0;
}

Loading…
Cancel
Save