2004-03-11 22:02:01 +00:00
|
|
|
/* $OpenBSD: event.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
|
2004-02-12 15:55:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, 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.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
2004-03-17 21:37:12 +00:00
|
|
|
#include "lam_config.h"
|
2004-02-12 15:55:09 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#undef WIN32_LEAN_AND_MEAN
|
|
|
|
#include "misc.h"
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/tree.h>
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#else
|
|
|
|
#include <sys/_time.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifndef WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#ifdef USE_LOG
|
|
|
|
#include "log.h"
|
|
|
|
#else
|
|
|
|
#define LOG_DBG(x)
|
2004-03-11 22:02:01 +00:00
|
|
|
#define log_error(x) perror(x)
|
2004-02-12 15:55:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "event.h"
|
2004-03-18 21:35:28 +00:00
|
|
|
#include "include/types.h"
|
|
|
|
#include "include/constants.h"
|
2004-03-17 18:45:16 +00:00
|
|
|
#include "lfc/lam_object.h"
|
|
|
|
#include "threads/mutex.h"
|
|
|
|
#include "threads/thread.h"
|
|
|
|
#include "util/output.h"
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-04-12 15:42:44 +00:00
|
|
|
#if HAVE_SELECT
|
2004-02-12 15:55:09 +00:00
|
|
|
extern const struct lam_eventop lam_selectops;
|
|
|
|
#endif
|
2004-04-12 15:42:44 +00:00
|
|
|
#if HAVE_POLL
|
2004-02-12 15:55:09 +00:00
|
|
|
extern const struct lam_eventop lam_pollops;
|
|
|
|
#endif
|
2004-04-12 15:42:44 +00:00
|
|
|
#if HAVE_RTSIG
|
2004-02-12 15:55:09 +00:00
|
|
|
extern const struct lam_eventop lam_rtsigops;
|
|
|
|
#endif
|
2004-04-12 15:42:44 +00:00
|
|
|
#if HAVE_EPOLL
|
2004-02-12 15:55:09 +00:00
|
|
|
extern const struct lam_eventop lam_epollops;
|
|
|
|
#endif
|
2004-04-12 15:42:44 +00:00
|
|
|
#if HAVE_WORKING_KQUEUE
|
2004-02-12 15:55:09 +00:00
|
|
|
extern const struct lam_eventop lam_kqops;
|
|
|
|
#endif
|
2004-04-12 15:42:44 +00:00
|
|
|
#if WIN32
|
2004-02-12 15:55:09 +00:00
|
|
|
extern const struct lam_eventop lam_win32ops;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* In order of preference */
|
|
|
|
static const struct lam_eventop *lam_eventops[] = {
|
2004-04-12 15:42:44 +00:00
|
|
|
#if HAVE_WORKING_KQUEUE
|
2004-03-11 22:02:01 +00:00
|
|
|
&lam_kqops,
|
2004-02-12 15:55:09 +00:00
|
|
|
#endif
|
2004-04-12 15:42:44 +00:00
|
|
|
#if HAVE_EPOLL
|
2004-03-11 22:02:01 +00:00
|
|
|
&lam_epollops,
|
2004-02-12 15:55:09 +00:00
|
|
|
#endif
|
2004-04-12 15:42:44 +00:00
|
|
|
#if HAVE_RTSIG
|
2004-03-11 22:02:01 +00:00
|
|
|
&lam_rtsigops,
|
2004-02-12 15:55:09 +00:00
|
|
|
#endif
|
2004-04-12 15:42:44 +00:00
|
|
|
#if HAVE_POLL
|
2004-03-11 22:02:01 +00:00
|
|
|
&lam_pollops,
|
2004-02-12 15:55:09 +00:00
|
|
|
#endif
|
2004-04-12 15:42:44 +00:00
|
|
|
#if HAVE_SELECT
|
2004-03-11 22:02:01 +00:00
|
|
|
&lam_selectops,
|
2004-02-12 15:55:09 +00:00
|
|
|
#endif
|
2004-04-12 15:42:44 +00:00
|
|
|
#if WIN32
|
2004-03-11 22:02:01 +00:00
|
|
|
&lam_win32ops,
|
2004-02-12 15:55:09 +00:00
|
|
|
#endif
|
2004-03-11 22:02:01 +00:00
|
|
|
NULL
|
2004-02-12 15:55:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct lam_eventop *lam_evsel;
|
|
|
|
void *lam_evbase;
|
|
|
|
|
|
|
|
/* Handle signals */
|
2004-03-11 22:02:01 +00:00
|
|
|
int (*lam_event_sigcb)(void); /* Signal callback when gotsig is set */
|
|
|
|
int lam_event_gotsig; /* Set in signal handler */
|
2004-02-12 15:55:09 +00:00
|
|
|
|
|
|
|
/* Prototypes */
|
|
|
|
static void lam_event_process_active(void);
|
2004-03-11 22:02:01 +00:00
|
|
|
static void lam_timeout_correct(struct timeval *off);
|
2004-02-12 15:55:09 +00:00
|
|
|
static void lam_timeout_insert(struct lam_event *);
|
2004-05-18 21:06:11 +00:00
|
|
|
static void lam_event_queue_insert(struct lam_event *, int);
|
|
|
|
static void lam_event_queue_remove(struct lam_event *, int);
|
|
|
|
static void lam_timeout_process(void);
|
2004-03-26 05:00:29 +00:00
|
|
|
int lam_event_haveevents(void);
|
2004-02-12 15:55:09 +00:00
|
|
|
|
|
|
|
static RB_HEAD(lam_event_tree, lam_event) lam_timetree;
|
|
|
|
static struct lam_event_list lam_activequeue;
|
|
|
|
struct lam_event_list lam_signalqueue;
|
|
|
|
struct lam_event_list lam_eventqueue;
|
|
|
|
static struct timeval lam_event_tv;
|
2004-03-11 22:02:01 +00:00
|
|
|
lam_mutex_t lam_event_lock;
|
2004-04-23 01:38:41 +00:00
|
|
|
#if LAM_HAVE_THREADS
|
2004-03-16 21:56:19 +00:00
|
|
|
lam_thread_t lam_event_thread;
|
2004-04-23 01:38:41 +00:00
|
|
|
lam_event_t lam_event_pipe_event;
|
|
|
|
int lam_event_pipe[2];
|
|
|
|
int lam_event_pipe_signalled;
|
|
|
|
#endif
|
2004-02-12 15:55:09 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
compare(struct lam_event *a, struct lam_event *b)
|
|
|
|
{
|
2004-03-11 22:02:01 +00:00
|
|
|
if (timercmp(&a->ev_timeout, &b->ev_timeout, <))
|
|
|
|
return (-1);
|
|
|
|
else if (timercmp(&a->ev_timeout, &b->ev_timeout, >))
|
|
|
|
return (1);
|
|
|
|
return (0);
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
2004-03-26 05:00:29 +00:00
|
|
|
static RB_PROTOTYPE(lam_event_tree, lam_event, ev_timeout_node, compare)
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-26 05:00:29 +00:00
|
|
|
static RB_GENERATE(lam_event_tree, lam_event, ev_timeout_node, compare)
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-05-18 21:06:11 +00:00
|
|
|
static int lam_timeout_next(struct timeval *tv)
|
|
|
|
{
|
|
|
|
struct timeval dflt = LAM_TIMEOUT_DEFAULT;
|
|
|
|
struct timeval now;
|
|
|
|
struct lam_event *ev;
|
|
|
|
|
|
|
|
if ((ev = RB_MIN(lam_event_tree, &lam_timetree)) == NULL) {
|
|
|
|
*tv = dflt;
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gettimeofday(&now, NULL) == -1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (timercmp(&ev->ev_timeout, &now, <=)) {
|
|
|
|
timerclear(tv);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
timersub(&ev->ev_timeout, &now, tv);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-03-16 21:56:19 +00:00
|
|
|
/* run loop for dispatch thread */
|
|
|
|
static void* lam_event_run(lam_object_t* arg)
|
|
|
|
{
|
|
|
|
lam_event_loop(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
|
2004-04-23 01:38:41 +00:00
|
|
|
#if LAM_HAVE_THREADS
|
|
|
|
static void lam_event_pipe_handler(int sd, short flags, void* user)
|
|
|
|
{
|
|
|
|
unsigned char byte;
|
|
|
|
if(read(sd, &byte, 1) != 1) {
|
|
|
|
lam_output(0, "lam_event_pipe: read failed with: errno=%d\n", errno);
|
|
|
|
lam_event_del(&lam_event_pipe_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-03-16 21:56:19 +00:00
|
|
|
|
|
|
|
int
|
2004-02-12 15:55:09 +00:00
|
|
|
lam_event_init(void)
|
|
|
|
{
|
2004-03-16 21:56:19 +00:00
|
|
|
static int inited = false;
|
2004-04-06 16:13:17 +00:00
|
|
|
int i;
|
|
|
|
#if LAM_HAVE_THREADS
|
|
|
|
int rc;
|
|
|
|
#endif
|
2004-03-16 21:56:19 +00:00
|
|
|
|
|
|
|
if(inited)
|
|
|
|
return LAM_SUCCESS;
|
2004-03-11 22:02:01 +00:00
|
|
|
|
|
|
|
lam_event_sigcb = NULL;
|
|
|
|
lam_event_gotsig = 0;
|
|
|
|
gettimeofday(&lam_event_tv, NULL);
|
|
|
|
|
2004-03-16 21:56:19 +00:00
|
|
|
OBJ_CONSTRUCT(&lam_event_lock, lam_mutex_t);
|
2004-03-11 22:02:01 +00:00
|
|
|
RB_INIT(&lam_timetree);
|
|
|
|
TAILQ_INIT(&lam_eventqueue);
|
|
|
|
TAILQ_INIT(&lam_activequeue);
|
|
|
|
TAILQ_INIT(&lam_signalqueue);
|
|
|
|
|
|
|
|
lam_evbase = NULL;
|
|
|
|
for (i = 0; lam_eventops[i] && !lam_evbase; i++) {
|
|
|
|
lam_evsel = lam_eventops[i];
|
|
|
|
lam_evbase = lam_evsel->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lam_evbase == NULL)
|
|
|
|
errx(1, "%s: no event mechanism available", __func__);
|
|
|
|
|
2004-04-06 16:13:17 +00:00
|
|
|
#if LAM_HAVE_THREADS
|
2004-04-23 01:38:41 +00:00
|
|
|
if(pipe(lam_event_pipe) != 0) {
|
|
|
|
lam_output(0, "lam_event_init: pipe() failed with errno=%d\n", errno);
|
|
|
|
return LAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
lam_event_pipe_signalled = 1;
|
|
|
|
lam_event_set(
|
|
|
|
&lam_event_pipe_event,
|
|
|
|
lam_event_pipe[0],
|
|
|
|
LAM_EV_READ|LAM_EV_PERSIST,
|
|
|
|
lam_event_pipe_handler,
|
|
|
|
0);
|
|
|
|
lam_event_add_i(&lam_event_pipe_event, 0);
|
|
|
|
lam_event_pipe_signalled = 0;
|
|
|
|
|
2004-03-16 21:56:19 +00:00
|
|
|
/* spin up a thread to dispatch events */
|
|
|
|
OBJ_CONSTRUCT(&lam_event_thread, lam_thread_t);
|
|
|
|
lam_event_thread.t_run = lam_event_run;
|
|
|
|
if((rc = lam_thread_start(&lam_event_thread)) != LAM_SUCCESS)
|
|
|
|
return rc;
|
2004-04-06 16:13:17 +00:00
|
|
|
#endif
|
2004-02-12 15:55:09 +00:00
|
|
|
|
|
|
|
#if defined(USE_LOG) && defined(USE_DEBUG)
|
2004-03-11 22:02:01 +00:00
|
|
|
log_to(stderr);
|
|
|
|
log_debug_cmd(LOG_MISC, 80);
|
2004-02-12 15:55:09 +00:00
|
|
|
#endif
|
2004-03-16 21:56:19 +00:00
|
|
|
inited = true;
|
|
|
|
return LAM_SUCCESS;
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
2004-03-16 21:56:19 +00:00
|
|
|
int
|
2004-02-12 15:55:09 +00:00
|
|
|
lam_event_haveevents(void)
|
|
|
|
{
|
2004-03-11 22:02:01 +00:00
|
|
|
return (RB_ROOT(&lam_timetree) || TAILQ_FIRST(&lam_eventqueue) ||
|
|
|
|
TAILQ_FIRST(&lam_signalqueue) || TAILQ_FIRST(&lam_activequeue));
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lam_event_process_active(void)
|
|
|
|
{
|
2004-03-11 22:02:01 +00:00
|
|
|
struct lam_event *ev;
|
|
|
|
short ncalls;
|
|
|
|
|
|
|
|
for (ev = TAILQ_FIRST(&lam_activequeue); ev;
|
|
|
|
ev = TAILQ_FIRST(&lam_activequeue)) {
|
|
|
|
lam_event_queue_remove(ev, LAM_EVLIST_ACTIVE);
|
|
|
|
|
|
|
|
/* Allows deletes to work */
|
|
|
|
ncalls = ev->ev_ncalls;
|
|
|
|
ev->ev_pncalls = &ncalls;
|
|
|
|
while (ncalls) {
|
|
|
|
ncalls--;
|
|
|
|
ev->ev_ncalls = ncalls;
|
2004-05-18 21:06:11 +00:00
|
|
|
if(lam_using_threads()) {
|
|
|
|
lam_mutex_unlock(&lam_event_lock);
|
|
|
|
(*ev->ev_callback)((int)ev->ev_fd, ev->ev_res, ev->ev_arg);
|
|
|
|
lam_mutex_lock(&lam_event_lock);
|
|
|
|
} else {
|
|
|
|
(*ev->ev_callback)((int)ev->ev_fd, ev->ev_res, ev->ev_arg);
|
|
|
|
}
|
2004-03-11 22:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lam_event_dispatch(void)
|
|
|
|
{
|
2004-03-11 22:02:01 +00:00
|
|
|
return (lam_event_loop(0));
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lam_event_loop(int flags)
|
|
|
|
{
|
2004-03-11 22:02:01 +00:00
|
|
|
struct timeval tv;
|
|
|
|
int res, done;
|
|
|
|
|
2004-05-18 21:06:11 +00:00
|
|
|
THREAD_LOCK(&lam_event_lock);
|
2004-03-15 20:44:45 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
/* Calculate the initial events that we are waiting for */
|
2004-05-18 21:06:11 +00:00
|
|
|
if (lam_evsel->recalc && lam_evsel->recalc(lam_evbase, 0) == -1) {
|
2004-03-11 22:02:01 +00:00
|
|
|
lam_output(0, "lam_event_loop: lam_evsel->recalc() failed.");
|
2004-05-18 21:06:11 +00:00
|
|
|
THREAD_UNLOCK(&lam_event_lock);
|
2004-03-11 22:02:01 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
done = 0;
|
|
|
|
while (!done) {
|
|
|
|
while (lam_event_gotsig) {
|
|
|
|
lam_event_gotsig = 0;
|
|
|
|
if (lam_event_sigcb) {
|
|
|
|
res = (*lam_event_sigcb)();
|
|
|
|
if (res == -1) {
|
|
|
|
lam_output(0, "lam_event_loop: lam_event_sigcb() failed.");
|
|
|
|
errno = EINTR;
|
2004-05-18 21:06:11 +00:00
|
|
|
THREAD_UNLOCK(&lam_event_lock);
|
2004-03-11 22:02:01 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-18 21:06:11 +00:00
|
|
|
if (!(flags & LAM_EVLOOP_NONBLOCK)) {
|
|
|
|
static struct timeval dflt = LAM_TIMEOUT_DEFAULT;
|
|
|
|
tv = dflt;
|
|
|
|
} else
|
2004-03-11 22:02:01 +00:00
|
|
|
timerclear(&tv);
|
|
|
|
|
2004-04-23 01:38:41 +00:00
|
|
|
#if LAM_HAVE_THREADS
|
|
|
|
lam_event_pipe_signalled = 0;
|
|
|
|
#endif
|
2004-03-11 22:02:01 +00:00
|
|
|
res = lam_evsel->dispatch(lam_evbase, &tv);
|
2004-04-23 01:38:41 +00:00
|
|
|
#if LAM_HAVE_THREADS
|
|
|
|
lam_event_pipe_signalled = 1;
|
|
|
|
#endif
|
2004-03-11 22:02:01 +00:00
|
|
|
if (res == -1) {
|
|
|
|
lam_output(0, "lam_event_loop: lam_evesel->dispatch() failed.");
|
2004-05-18 21:06:11 +00:00
|
|
|
THREAD_UNLOCK(&lam_event_lock);
|
2004-03-11 22:02:01 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2004-05-18 21:06:11 +00:00
|
|
|
if(NULL != RB_MIN(lam_event_tree, &lam_timetree)) {
|
|
|
|
/* Check if time is running backwards */
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
if (timercmp(&tv, &lam_event_tv, <)) {
|
|
|
|
struct timeval off;
|
|
|
|
LOG_DBG((LOG_MISC, 10,
|
|
|
|
"%s: time is running backwards, corrected",
|
|
|
|
__func__));
|
|
|
|
|
|
|
|
timersub(&lam_event_tv, &tv, &off);
|
|
|
|
lam_timeout_correct(&off);
|
|
|
|
}
|
|
|
|
lam_event_tv = tv;
|
|
|
|
lam_timeout_process();
|
|
|
|
}
|
2004-03-11 22:02:01 +00:00
|
|
|
|
|
|
|
if (TAILQ_FIRST(&lam_activequeue)) {
|
|
|
|
lam_event_process_active();
|
|
|
|
if (flags & LAM_EVLOOP_ONCE)
|
|
|
|
done = 1;
|
2004-04-23 01:38:41 +00:00
|
|
|
} else if (flags & (LAM_EVLOOP_NONBLOCK|LAM_EVLOOP_ONCE))
|
2004-03-11 22:02:01 +00:00
|
|
|
done = 1;
|
|
|
|
|
2004-05-18 21:06:11 +00:00
|
|
|
if (lam_evsel->recalc && lam_evsel->recalc(lam_evbase, 0) == -1) {
|
2004-03-11 22:02:01 +00:00
|
|
|
lam_output(0, "lam_event_loop: lam_evesel->recalc() failed.");
|
2004-05-18 21:06:11 +00:00
|
|
|
THREAD_UNLOCK(&lam_event_lock);
|
2004-03-11 22:02:01 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
2004-05-18 21:06:11 +00:00
|
|
|
THREAD_UNLOCK(&lam_event_lock);
|
2004-03-11 22:02:01 +00:00
|
|
|
return (0);
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2004-03-11 22:02:01 +00:00
|
|
|
lam_event_add_i(struct lam_event *ev, struct timeval *tv)
|
2004-02-12 15:55:09 +00:00
|
|
|
{
|
2004-05-04 22:45:09 +00:00
|
|
|
int rc = 0;
|
2004-03-11 22:02:01 +00:00
|
|
|
LOG_DBG((LOG_MISC, 55,
|
|
|
|
"event_add: event: %p, %s%s%scall %p",
|
|
|
|
ev,
|
|
|
|
ev->ev_events & LAM_EV_READ ? "LAM_EV_READ " : " ",
|
|
|
|
ev->ev_events & LAM_EV_WRITE ? "LAM_EV_WRITE " : " ",
|
|
|
|
tv ? "LAM_EV_TIMEOUT " : " ",
|
|
|
|
ev->ev_callback));
|
|
|
|
|
|
|
|
assert(!(ev->ev_flags & ~LAM_EVLIST_ALL));
|
2004-04-23 01:38:41 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
if (tv != NULL) {
|
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
if (ev->ev_flags & LAM_EVLIST_TIMEOUT)
|
|
|
|
lam_event_queue_remove(ev, LAM_EVLIST_TIMEOUT);
|
|
|
|
|
|
|
|
/* Check if it is active due to a timeout. Rescheduling
|
|
|
|
* this timeout before the callback can be executed
|
|
|
|
* removes it from the active list. */
|
|
|
|
if ((ev->ev_flags & LAM_EVLIST_ACTIVE) &&
|
|
|
|
(ev->ev_res & LAM_EV_TIMEOUT)) {
|
|
|
|
/* See if we are just active executing this
|
|
|
|
* event in a loop
|
|
|
|
*/
|
|
|
|
if (ev->ev_ncalls && ev->ev_pncalls) {
|
|
|
|
/* Abort loop */
|
|
|
|
*ev->ev_pncalls = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lam_event_queue_remove(ev, LAM_EVLIST_ACTIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
timeradd(&now, tv, &ev->ev_timeout);
|
|
|
|
|
|
|
|
LOG_DBG((LOG_MISC, 55,
|
|
|
|
"event_add: timeout in %d seconds, call %p",
|
|
|
|
tv->tv_sec, ev->ev_callback));
|
|
|
|
|
|
|
|
lam_event_queue_insert(ev, LAM_EVLIST_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ev->ev_events & (LAM_EV_READ|LAM_EV_WRITE)) &&
|
|
|
|
!(ev->ev_flags & (LAM_EVLIST_INSERTED|LAM_EVLIST_ACTIVE))) {
|
|
|
|
lam_event_queue_insert(ev, LAM_EVLIST_INSERTED);
|
2004-05-04 22:45:09 +00:00
|
|
|
rc = (lam_evsel->add(lam_evbase, ev));
|
2004-03-11 22:02:01 +00:00
|
|
|
} else if ((ev->ev_events & LAM_EV_SIGNAL) &&
|
|
|
|
!(ev->ev_flags & LAM_EVLIST_SIGNAL)) {
|
|
|
|
lam_event_queue_insert(ev, LAM_EVLIST_SIGNAL);
|
2004-05-04 22:45:09 +00:00
|
|
|
rc = (lam_evsel->add(lam_evbase, ev));
|
2004-03-11 22:02:01 +00:00
|
|
|
}
|
2004-04-23 01:38:41 +00:00
|
|
|
|
|
|
|
#if LAM_HAVE_THREADS
|
|
|
|
if(lam_event_pipe_signalled == 0) {
|
|
|
|
unsigned char byte = 0;
|
|
|
|
if(write(lam_event_pipe[1], &byte, 1) != 1)
|
|
|
|
lam_output(0, "lam_event_add: write() to lam_event_pipe[1] failed with errno=%d\n", errno);
|
|
|
|
lam_event_pipe_signalled++;
|
|
|
|
}
|
|
|
|
#endif
|
2004-05-04 22:45:09 +00:00
|
|
|
return rc;
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
int lam_event_del_i(struct lam_event *ev)
|
|
|
|
{
|
|
|
|
LOG_DBG((LOG_MISC, 80, "event_del: %p, callback %p",
|
|
|
|
ev, ev->ev_callback));
|
|
|
|
|
|
|
|
assert(!(ev->ev_flags & ~LAM_EVLIST_ALL));
|
|
|
|
|
|
|
|
/* See if we are just active executing this event in a loop */
|
|
|
|
if (ev->ev_ncalls && ev->ev_pncalls) {
|
|
|
|
/* Abort loop */
|
|
|
|
*ev->ev_pncalls = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev->ev_flags & LAM_EVLIST_TIMEOUT)
|
|
|
|
lam_event_queue_remove(ev, LAM_EVLIST_TIMEOUT);
|
|
|
|
|
|
|
|
if (ev->ev_flags & LAM_EVLIST_ACTIVE)
|
|
|
|
lam_event_queue_remove(ev, LAM_EVLIST_ACTIVE);
|
|
|
|
|
|
|
|
if (ev->ev_flags & LAM_EVLIST_INSERTED) {
|
|
|
|
lam_event_queue_remove(ev, LAM_EVLIST_INSERTED);
|
|
|
|
return (lam_evsel->del(lam_evbase, ev));
|
|
|
|
} else if (ev->ev_flags & LAM_EVLIST_SIGNAL) {
|
|
|
|
lam_event_queue_remove(ev, LAM_EVLIST_SIGNAL);
|
|
|
|
return (lam_evsel->del(lam_evbase, ev));
|
|
|
|
}
|
2004-04-23 01:38:41 +00:00
|
|
|
|
|
|
|
#if LAM_HAVE_THREADS
|
|
|
|
if(lam_event_pipe_signalled == 0) {
|
|
|
|
unsigned char byte = 0;
|
|
|
|
if(write(lam_event_pipe[1], &byte, 1) != 1)
|
|
|
|
lam_output(0, "lam_event_add: write() to lam_event_pipe[1] failed with errno=%d\n", errno);
|
|
|
|
lam_event_pipe_signalled++;
|
|
|
|
}
|
|
|
|
#endif
|
2004-03-11 22:02:01 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
|
|
|
|
static void
|
2004-02-12 15:55:09 +00:00
|
|
|
lam_timeout_correct(struct timeval *off)
|
|
|
|
{
|
2004-03-11 22:02:01 +00:00
|
|
|
struct lam_event *ev;
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
/* We can modify the key element of the node without destroying
|
|
|
|
* the key, beause we apply it to all in the right order.
|
|
|
|
*/
|
|
|
|
RB_FOREACH(ev, lam_event_tree, &lam_timetree)
|
|
|
|
timersub(&ev->ev_timeout, off, &ev->ev_timeout);
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
|
|
|
|
static void
|
2004-02-12 15:55:09 +00:00
|
|
|
lam_timeout_process(void)
|
|
|
|
{
|
2004-03-11 22:02:01 +00:00
|
|
|
struct timeval now;
|
|
|
|
struct lam_event *ev, *next;
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
gettimeofday(&now, NULL);
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
for (ev = RB_MIN(lam_event_tree, &lam_timetree); ev; ev = next) {
|
|
|
|
if (timercmp(&ev->ev_timeout, &now, >))
|
|
|
|
break;
|
|
|
|
next = RB_NEXT(lam_event_tree, &lam_timetree, ev);
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
lam_event_queue_remove(ev, LAM_EVLIST_TIMEOUT);
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
/* delete this event from the I/O queues */
|
|
|
|
lam_event_del_i(ev);
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
LOG_DBG((LOG_MISC, 60, "timeout_process: call %p",
|
|
|
|
ev->ev_callback));
|
|
|
|
lam_event_active_i(ev, LAM_EV_TIMEOUT, 1);
|
|
|
|
}
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lam_timeout_insert(struct lam_event *ev)
|
|
|
|
{
|
2004-03-11 22:02:01 +00:00
|
|
|
struct lam_event *tmp;
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
tmp = RB_FIND(lam_event_tree, &lam_timetree, ev);
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
if (tmp != NULL) {
|
|
|
|
struct timeval tv;
|
|
|
|
struct timeval add = {0,1};
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
/* Find unique time */
|
|
|
|
tv = ev->ev_timeout;
|
|
|
|
do {
|
|
|
|
timeradd(&tv, &add, &tv);
|
|
|
|
tmp = RB_NEXT(lam_event_tree, &lam_timetree, tmp);
|
|
|
|
} while (tmp != NULL && timercmp(&tmp->ev_timeout, &tv, ==));
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
ev->ev_timeout = tv;
|
|
|
|
}
|
2004-02-12 15:55:09 +00:00
|
|
|
|
2004-03-11 22:02:01 +00:00
|
|
|
tmp = RB_INSERT(lam_event_tree, &lam_timetree, ev);
|
|
|
|
assert(tmp == NULL);
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lam_event_queue_remove(struct lam_event *ev, int queue)
|
|
|
|
{
|
2004-03-11 22:02:01 +00:00
|
|
|
if (!(ev->ev_flags & queue))
|
|
|
|
errx(1, "%s: %p(fd %d) not on queue %x", __func__,
|
|
|
|
ev, ev->ev_fd, queue);
|
|
|
|
|
|
|
|
ev->ev_flags &= ~queue;
|
|
|
|
switch (queue) {
|
|
|
|
case LAM_EVLIST_ACTIVE:
|
|
|
|
TAILQ_REMOVE(&lam_activequeue, ev, ev_active_next);
|
|
|
|
break;
|
|
|
|
case LAM_EVLIST_SIGNAL:
|
|
|
|
TAILQ_REMOVE(&lam_signalqueue, ev, ev_signal_next);
|
|
|
|
break;
|
|
|
|
case LAM_EVLIST_TIMEOUT:
|
|
|
|
RB_REMOVE(lam_event_tree, &lam_timetree, ev);
|
|
|
|
break;
|
|
|
|
case LAM_EVLIST_INSERTED:
|
|
|
|
TAILQ_REMOVE(&lam_eventqueue, ev, ev_next);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(1, "%s: unknown queue %x", __func__, queue);
|
|
|
|
}
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lam_event_queue_insert(struct lam_event *ev, int queue)
|
|
|
|
{
|
2004-03-11 22:02:01 +00:00
|
|
|
if (ev->ev_flags & queue)
|
|
|
|
errx(1, "%s: %p(fd %d) already on queue %x", __func__,
|
|
|
|
ev, ev->ev_fd, queue);
|
|
|
|
|
|
|
|
ev->ev_flags |= queue;
|
|
|
|
switch (queue) {
|
|
|
|
case LAM_EVLIST_ACTIVE:
|
|
|
|
TAILQ_INSERT_TAIL(&lam_activequeue, ev, ev_active_next);
|
|
|
|
break;
|
|
|
|
case LAM_EVLIST_SIGNAL:
|
|
|
|
TAILQ_INSERT_TAIL(&lam_signalqueue, ev, ev_signal_next);
|
|
|
|
break;
|
|
|
|
case LAM_EVLIST_TIMEOUT:
|
|
|
|
lam_timeout_insert(ev);
|
|
|
|
break;
|
|
|
|
case LAM_EVLIST_INSERTED:
|
|
|
|
TAILQ_INSERT_TAIL(&lam_eventqueue, ev, ev_next);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(1, "%s: unknown queue %x", __func__, queue);
|
|
|
|
}
|
2004-02-12 15:55:09 +00:00
|
|
|
}
|
|
|
|
|
2004-05-18 21:06:11 +00:00
|
|
|
void lam_event_active_i(struct lam_event * ev, int res, short ncalls)
|
|
|
|
{
|
|
|
|
/* We get different kinds of events, add them together */
|
|
|
|
if (ev->ev_flags & LAM_EVLIST_ACTIVE) {
|
|
|
|
ev->ev_res |= res;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ev->ev_res = res;
|
|
|
|
ev->ev_ncalls = ncalls;
|
|
|
|
ev->ev_pncalls = NULL;
|
|
|
|
lam_event_queue_insert(ev, LAM_EVLIST_ACTIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|