Skip to content

Commit

Permalink
more the signal base into the event base; this removes global state a…
Browse files Browse the repository at this point in the history
…nd makes signals

work better with threading; from Wouter Wijngaards
small fixes for kqueue and style by me


svn:r351
  • Loading branch information
provos committed Mar 10, 2007
1 parent a968da7 commit 41b7cbc
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 124 deletions.
8 changes: 4 additions & 4 deletions WIN32-Code/win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ struct win32op {
struct event **events;
};

void *win32_init (void);
void *win32_init (struct event_base *);
int win32_insert (void *, struct event *);
int win32_del (void *, struct event *);
int win32_recalc (struct event_base *base, void *, int);
int win32_dispatch (struct event_base *base, void *, struct timeval *);
void win32_dealloc (void *);
void win32_dealloc (struct event_base *, void *);

struct eventop win32ops = {
"win32",
Expand Down Expand Up @@ -167,7 +167,7 @@ do_fd_clear(struct win32op *op, SOCKET s, int read)

#define NEVENT 64
void *
win32_init(void)
win32_init(struct event_base *)
{
struct win32op *winop;
size_t size;
Expand Down Expand Up @@ -376,7 +376,7 @@ win32_dispatch(struct event_base *base, void *op,
}

void
win32_dealloc(void *arg)
win32_dealloc(struct event_base *, void *arg)
{
struct win32op *win32op = arg;

Expand Down
20 changes: 10 additions & 10 deletions devpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
#include "evsignal.h"
#include "log.h"

extern volatile sig_atomic_t evsignal_caught;

/* due to limitations in the devpoll interface, we need to keep track of
* all file descriptors outself.
*/
Expand All @@ -70,12 +68,12 @@ struct devpollop {
int nchanges;
};

void *devpoll_init (void);
void *devpoll_init (struct event_base *);
int devpoll_add (void *, struct event *);
int devpoll_del (void *, struct event *);
int devpoll_recalc (struct event_base *, void *, int);
int devpoll_dispatch (struct event_base *, void *, struct timeval *);
void devpoll_dealloc (void *);
void devpoll_dealloc (struct event_base *, void *);

struct eventop devpollops = {
"devpoll",
Expand Down Expand Up @@ -126,7 +124,7 @@ devpoll_queue(struct devpollop *devpollop, int fd, int events) {
}

void *
devpoll_init(void)
devpoll_init(struct event_base *base)
{
int dpfd, nfiles = NEVENT;
struct rlimit rl;
Expand Down Expand Up @@ -179,7 +177,7 @@ devpoll_init(void)
return (NULL);
}

evsignal_init();
evsignal_init(base);

return (devpollop);
}
Expand Down Expand Up @@ -237,10 +235,11 @@ devpoll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
return (-1);
}

evsignal_process();
evsignal_process(base);
return (0);
} else if (evsignal_caught)
evsignal_process();
} else if (base->sig.evsignal_caught) {
evsignal_process(base);
}

event_debug(("%s: devpoll_wait reports %d", __func__, res));

Expand Down Expand Up @@ -398,10 +397,11 @@ devpoll_del(void *arg, struct event *ev)
}

void
devpoll_dealloc(void *arg)
devpoll_dealloc(struct event_base *base, void *arg)
{
struct devpollop *devpollop = arg;

evsignal_dealloc(base);
if (devpollop->fds)
free(devpollop->fds);
if (devpollop->events)
Expand Down
22 changes: 12 additions & 10 deletions epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <stdint.h>
#include <sys/types.h>
#include <sys/tree.h>
#include <sys/resource.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
Expand All @@ -49,11 +50,10 @@
#endif

#include "event.h"
#include "event-internal.h"
#include "evsignal.h"
#include "log.h"

extern volatile sig_atomic_t evsignal_caught;

/* due to limitations in the epoll interface, we need to keep track of
* all file descriptors outself.
*/
Expand All @@ -70,12 +70,12 @@ struct epollop {
int epfd;
};

void *epoll_init (void);
void *epoll_init (struct event_base *);
int epoll_add (void *, struct event *);
int epoll_del (void *, struct event *);
int epoll_recalc (struct event_base *, void *, int);
int epoll_dispatch (struct event_base *, void *, struct timeval *);
void epoll_dealloc (void *);
void epoll_dealloc (struct event_base *, void *);

struct eventop epollops = {
"epoll",
Expand All @@ -99,7 +99,7 @@ struct eventop epollops = {
#define NEVENT 32000

void *
epoll_init(void)
epoll_init(struct event_base *base)
{
int epfd, nfiles = NEVENT;
struct rlimit rl;
Expand Down Expand Up @@ -149,7 +149,7 @@ epoll_init(void)
}
epollop->nfds = nfiles;

evsignal_init();
evsignal_init(base);

return (epollop);
}
Expand Down Expand Up @@ -198,10 +198,11 @@ epoll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
return (-1);
}

evsignal_process();
evsignal_process(base);
return (0);
} else if (evsignal_caught)
evsignal_process();
} else if (base->sig.evsignal_caught) {
evsignal_process(base);
}

event_debug(("%s: epoll_wait reports %d", __func__, res));

Expand Down Expand Up @@ -346,10 +347,11 @@ epoll_del(void *arg, struct event *ev)
}

void
epoll_dealloc(void *arg)
epoll_dealloc(struct event_base *base, void *arg)
{
struct epollop *epollop = arg;

evsignal_dealloc(base);
if (epollop->fds)
free(epollop->fds);
if (epollop->events)
Expand Down
5 changes: 5 additions & 0 deletions event-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
extern "C" {
#endif

#include "evsignal.h"

struct event_base {
const struct eventop *evsel;
void *evbase;
Expand All @@ -43,6 +45,9 @@ struct event_base {
struct event_list **activequeues;
int nactivequeues;

/* signal handling info */
struct evsignal_info sig;

struct event_list eventqueue;
struct timeval event_tv;

Expand Down
3 changes: 3 additions & 0 deletions event.3
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
.Nm event_add ,
.Nm event_del ,
.Nm event_once ,
.Nm event_base_once ,
.Nm event_pending ,
.Nm event_initialized ,
.Nm event_priority_init ,
Expand Down Expand Up @@ -107,6 +108,8 @@
.Ft int
.Fn "event_once" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
.Ft int
.Fn "event_base_once" "struct event_base *base" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
.Ft int
.Fn "event_pending" "struct event *ev" "short event" "struct timeval *tv"
.Ft int
.Fn "event_initialized" "struct event *ev"
Expand Down
67 changes: 41 additions & 26 deletions event.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ const struct eventop *eventops[] = {
};

/* Global state */
struct event_list signalqueue;

struct event_base *current_base = NULL;
extern struct event_base *evsignal_base;

/* Handle signals - This is a deprecated interface */
int (*event_sigcb)(void); /* Signal callback when gotsig is set */
Expand Down Expand Up @@ -174,36 +173,40 @@ void *
event_init(void)
{
int i;
struct event_base *base;

if ((current_base = calloc(1, sizeof(struct event_base))) == NULL)
if ((base = calloc(1, sizeof(struct event_base))) == NULL)
event_err(1, "%s: calloc");

event_sigcb = NULL;
event_gotsig = 0;
gettime(&current_base->event_tv);
gettime(&base->event_tv);

RB_INIT(&current_base->timetree);
TAILQ_INIT(&current_base->eventqueue);
TAILQ_INIT(&signalqueue);
RB_INIT(&base->timetree);
TAILQ_INIT(&base->eventqueue);
TAILQ_INIT(&base->sig.signalqueue);
base->sig.ev_signal_pair[0] = -1;
base->sig.ev_signal_pair[1] = -1;

current_base->evbase = NULL;
for (i = 0; eventops[i] && !current_base->evbase; i++) {
current_base->evsel = eventops[i];
base->evbase = NULL;
for (i = 0; eventops[i] && !base->evbase; i++) {
base->evsel = eventops[i];

current_base->evbase = current_base->evsel->init();
base->evbase = base->evsel->init(base);
}

if (current_base->evbase == NULL)
if (base->evbase == NULL)
event_errx(1, "%s: no event mechanism available", __func__);

if (getenv("EVENT_SHOW_METHOD"))
event_msgx("libevent using: %s\n",
current_base->evsel->name);
base->evsel->name);

/* allocate a single active event queue */
event_base_priority_init(current_base, 1);
event_base_priority_init(base, 1);

return (current_base);
current_base = base;
return (base);
}

void
Expand All @@ -217,7 +220,8 @@ event_base_free(struct event_base *base)
current_base = NULL;

assert(base);
assert(TAILQ_EMPTY(&base->eventqueue));
if (base->evsel->dealloc != NULL)
base->evsel->dealloc(base, base->evbase);
for (i=0; i < base->nactivequeues; ++i)
assert(TAILQ_EMPTY(base->activequeues[i]));

Expand All @@ -227,8 +231,7 @@ event_base_free(struct event_base *base)
free(base->activequeues[i]);
free(base->activequeues);

if (base->evsel->dealloc != NULL)
base->evsel->dealloc(base->evbase);
assert(TAILQ_EMPTY(&base->eventqueue));

free(base);
}
Expand Down Expand Up @@ -343,7 +346,6 @@ event_loopexit_cb(int fd, short what, void *arg)
}

/* not thread safe */

int
event_loopexit(struct timeval *tv)
{
Expand All @@ -354,7 +356,7 @@ event_loopexit(struct timeval *tv)
int
event_base_loopexit(struct event_base *event_base, struct timeval *tv)
{
return (event_once(-1, EV_TIMEOUT, event_loopexit_cb,
return (event_base_once(event_base, -1, EV_TIMEOUT, event_loopexit_cb,
event_base, tv));
}

Expand All @@ -374,6 +376,8 @@ event_base_loop(struct event_base *base, int flags)
struct timeval tv;
int res, done;

if(!TAILQ_EMPTY(&base->sig.signalqueue))
evsignal_base = base;
done = 0;
while (!done) {
/* Calculate the initial events that we are waiting for */
Expand Down Expand Up @@ -422,6 +426,7 @@ event_base_loop(struct event_base *base, int flags)

res = evsel->dispatch(base, evbase, &tv);


if (res == -1)
return (-1);

Expand Down Expand Up @@ -459,11 +464,18 @@ event_once_cb(int fd, short events, void *arg)
free(eonce);
}

/* Schedules an event once */

/* not threadsafe, event scheduled once. */
int
event_once(int fd, short events,
void (*callback)(int, short, void *), void *arg, struct timeval *tv)
{
return event_base_once(current_base, fd, events, callback, arg, tv);
}

/* Schedules an event once */
int
event_base_once(struct event_base *base, int fd, short events,
void (*callback)(int, short, void *), void *arg, struct timeval *tv)
{
struct event_once *eonce;
struct timeval etv;
Expand Down Expand Up @@ -496,7 +508,9 @@ event_once(int fd, short events,
return (-1);
}

res = event_add(&eonce->ev, tv);
res = event_base_set(base, &eonce->ev);
if (res == 0)
res = event_add(&eonce->ev, tv);
if (res != 0) {
free(eonce);
return (res);
Expand All @@ -521,7 +535,8 @@ event_set(struct event *ev, int fd, short events,
ev->ev_pncalls = NULL;

/* by default, we put new events into the middle priority */
ev->ev_pri = current_base->nactivequeues/2;
if(current_base)
ev->ev_pri = current_base->nactivequeues/2;
}

int
Expand Down Expand Up @@ -801,7 +816,7 @@ event_queue_remove(struct event_base *base, struct event *ev, int queue)
ev, ev_active_next);
break;
case EVLIST_SIGNAL:
TAILQ_REMOVE(&signalqueue, ev, ev_signal_next);
TAILQ_REMOVE(&base->sig.signalqueue, ev, ev_signal_next);
break;
case EVLIST_TIMEOUT:
RB_REMOVE(event_tree, &base->timetree, ev);
Expand Down Expand Up @@ -843,7 +858,7 @@ event_queue_insert(struct event_base *base, struct event *ev, int queue)
ev,ev_active_next);
break;
case EVLIST_SIGNAL:
TAILQ_INSERT_TAIL(&signalqueue, ev, ev_signal_next);
TAILQ_INSERT_TAIL(&base->sig.signalqueue, ev, ev_signal_next);
break;
case EVLIST_TIMEOUT: {
struct event *tmp = RB_INSERT(event_tree, &base->timetree, ev);
Expand Down
Loading

0 comments on commit 41b7cbc

Please sign in to comment.