Skip to content

Commit

Permalink
Restructure the event backends so that they do not need to keep track…
Browse files Browse the repository at this point in the history
… of events themselves, as a side effect multiple events can use the same fd or signal.

svn:r972
  • Loading branch information
provos committed Dec 23, 2008
1 parent 97cebce commit 02b2b4d
Show file tree
Hide file tree
Showing 20 changed files with 780 additions and 857 deletions.
3 changes: 2 additions & 1 deletion ChangeLog
Expand Up @@ -132,7 +132,8 @@ Changes in current version:
o Fix a typo in setting the global event base; reported by lance.
o Set the 0x20 bit on outgoing alphabetic characters in DNS requests randomly, and insist on a match in replies. This helps resist DNS poisoning attacks.
o Make the http connection close detection work properly with bufferevents and fix a potential memory leak associated with it.

o Restructure the event backends so that they do not need to keep track of events themselves, as a side effect multiple events can use the same fd or signal.

Changes in 1.4.0:
o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr.
o demote most http warnings to debug messages
Expand Down
2 changes: 1 addition & 1 deletion Makefile.am
Expand Up @@ -82,7 +82,7 @@ event-config.h: config.h
echo "#endif" >> $@

CORE_SRC = event.c buffer.c evbuffer-internal.h bufferevent.c \
bufferevent-internal.h \
bufferevent-internal.h evmap.c evmap.h \
log.c evutil.c $(SYS_SRC)
EXTRA_SRC = event_tagging.c http.c evhttp.h http-internal.h evdns.c \
evdns.h evrpc.c evrpc.h evrpc-internal.h mm-internal.h \
Expand Down
129 changes: 27 additions & 102 deletions WIN32-Code/win32.c
Expand Up @@ -48,39 +48,26 @@
#include "log.h"
#include "event.h"
#include "event-internal.h"
#include "evmap.h"

#define XFREE(ptr) do { if (ptr) mm_free(ptr); } while(0)

extern struct event_list timequeue;
extern struct event_list addqueue;
#if 0
extern struct event_list signalqueue;
#endif

struct win_fd_set {
u_int fd_count;
SOCKET fd_array[1];
};

int evsigcaught[NSIG];
volatile sig_atomic_t signal_caught = 0;
/* MSDN says this is required to handle SIGFPE */
volatile double SIGFPE_REQ = 0.0f;

#if 0
static void signal_handler(int sig);

void signal_process(void);
int signal_recalc(void);
#endif

struct event_entry {
RB_ENTRY(event_entry) node;
SOCKET sock;
int read_pos;
int write_pos;
struct event *read_event;
struct event *write_event;
};

static int
Expand Down Expand Up @@ -108,10 +95,10 @@ RB_PROTOTYPE(event_map, event_entry, node, compare);
RB_GENERATE(event_map, event_entry, node, compare);

void *win32_init (struct event_base *);
int win32_insert (void *, struct event *);
int win32_del (void *, struct event *);
int win32_dispatch (struct event_base *base, void *, struct timeval *);
void win32_dealloc (struct event_base *, void *);
int win32_insert(struct event_base *, evutil_socket_t, short old short events);
int win32_del(struct event_base *, evutil_socket_t, short old, short events);
int win32_dispatch (struct event_base *base, struct timeval *);
void win32_dealloc (struct event_base *);

struct eventop win32ops = {
"win32",
Expand Down Expand Up @@ -268,55 +255,44 @@ win32_init(struct event_base *_base)
}

int
win32_insert(void *op, struct event *ev)
win32_insert(struct event_base *base, evutil_socket_t fd,
short old, short events)
{
struct win32op *win32op = op;
struct win32op *win32op = base->evbase;
struct event_entry *ent;

if (ev->ev_events & EV_SIGNAL) {
return (evsignal_add(ev));
}
if (!(ev->ev_events & (EV_READ|EV_WRITE)))
if (!(events & (EV_READ|EV_WRITE)))
return (0);
ent = get_event_entry(win32op, ev->ev_fd, 1);
ent = get_event_entry(win32op, fd, 1);
if (!ent)
return (-1); /* out of memory */

event_debug(("%s: adding event for %d", __func__, (int)ev->ev_fd));
if (ev->ev_events & EV_READ) {
event_debug(("%s: adding event for %d", __func__, (int)fd));
if (events & EV_READ) {
if (do_fd_set(win32op, ent, 1)<0)
return (-1);
ent->read_event = ev;
}
if (ev->ev_events & EV_WRITE) {
if (events & EV_WRITE) {
if (do_fd_set(win32op, ent, 0)<0)
return (-1);
ent->write_event = ev;
}
return (0);
}

int
win32_del(void *op, struct event *ev)
win32_del(struct event_base *base, evutil_socket_t fd, short old, short events)
{
struct win32op *win32op = op;
struct win32op *win32op = base->evbase;
struct event_entry *ent;

if (ev->ev_events & EV_SIGNAL)
return (evsignal_del(ev));

if (!(ent = get_event_entry(win32op, ev->ev_fd, 0)))
if (!(ent = get_event_entry(win32op, fd, 0)))
return (-1);
event_debug(("%s: Removing event for %d", __func__, ev->ev_fd));
if (ev == ent->read_event) {
event_debug(("%s: Removing event for %d", __func__, fd));
if (events & EV_READ)
do_fd_clear(win32op, ent, 1);
ent->read_event = NULL;
}
if (ev == ent->write_event) {
if (events & EV_WRITE)
do_fd_clear(win32op, ent, 0);
ent->write_event = NULL;
}
if (!ent->read_event && !ent->write_event) {
if ((events & (EV_WRITE|EV_READ) == (EV_WRITE|EV_READ)) {
RB_REMOVE(event_map, &win32op->event_root, ent);
mm_free(ent);
}
Expand All @@ -343,10 +319,9 @@ fd_set_copy(struct win_fd_set *out, const struct win_fd_set *in)
*/

int
win32_dispatch(struct event_base *base, void *op,
struct timeval *tv)
win32_dispatch(struct event_base *base, struct timeval *tv)
{
struct win32op *win32op = op;
struct win32op *win32op = base->evbase;
int res = 0;
int i;
int fd_count;
Expand Down Expand Up @@ -383,34 +358,26 @@ win32_dispatch(struct event_base *base, void *op,
for (i=0; i<win32op->readset_out->fd_count; ++i) {
struct event_entry *ent;
SOCKET s = win32op->readset_out->fd_array[i];
if ((ent = get_event_entry(win32op, s, 0)) && ent->read_event)
event_active(ent->read_event, EV_READ, 1);
evmap_io_active(base, s, EV_READ);
}
for (i=0; i<win32op->exset_out->fd_count; ++i) {
struct event_entry *ent;
SOCKET s = win32op->exset_out->fd_array[i];
if ((ent = get_event_entry(win32op, s, 0)) && ent->read_event)
event_active(ent->read_event, EV_READ, 1);
evmap_io_active(base, s, EV_READ, 1);
}
for (i=0; i<win32op->writeset_out->fd_count; ++i) {
struct event_entry *ent;
SOCKET s = win32op->writeset_out->fd_array[i];
if ((ent = get_event_entry(win32op, s, 0)) && ent->write_event)
event_active(ent->write_event, EV_WRITE, 1);
evmap_io_active(base, s, EV_WRITE, 1);
}

#if 0
if (signal_recalc() == -1)
return (-1);
#endif

return (0);
}

void
win32_dealloc(struct event_base *_base, void *arg)
win32_dealloc(struct event_base *_base)
{
struct win32op *win32op = arg;
struct win32op *win32op = _base->evbase;

evsignal_dealloc(_base);
if (win32op->readset_in)
Expand All @@ -428,45 +395,3 @@ win32_dealloc(struct event_base *_base, void *arg)
memset(win32op, 0, sizeof(win32op));
mm_free(win32op);
}

#if 0
static void
signal_handler(int sig)
{
evsigcaught[sig]++;
signal_caught = 1;
}

int
signal_recalc(void)
{
struct event *ev;

/* Reinstall our signal handler. */
TAILQ_FOREACH(ev, &signalqueue, ev_signal_next) {
if((int)signal(EVENT_SIGNAL(ev), signal_handler) == -1)
return (-1);
}
return (0);
}

void
signal_process(void)
{
struct event *ev;
short ncalls;

TAILQ_FOREACH(ev, &signalqueue, ev_signal_next) {
ncalls = evsigcaught[EVENT_SIGNAL(ev)];
if (ncalls) {
if (!(ev->ev_events & EV_PERSIST))
event_del(ev);
event_active(ev, EV_SIGNAL, ncalls);
}
}

memset(evsigcaught, 0, sizeof(evsigcaught));
signal_caught = 0;
}
#endif

0 comments on commit 02b2b4d

Please sign in to comment.