2002-04-09 15:14:06 +00:00
|
|
|
/*
|
2009-01-27 22:34:36 +00:00
|
|
|
* Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
|
|
|
|
* Copyright (c) 2007-2009 Niels Provos and Nick Mathewson
|
2002-04-09 15:14:06 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2003-10-04 23:27:26 +00:00
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2002-04-09 15:14:06 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2005-12-17 20:25:22 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2009-01-27 22:30:46 +00:00
|
|
|
#include "event-config.h"
|
2005-12-17 20:25:22 +00:00
|
|
|
#endif
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2003-06-12 23:33:19 +00:00
|
|
|
#ifdef WIN32
|
2008-05-12 01:03:36 +00:00
|
|
|
#include <winsock2.h>
|
2003-06-12 23:33:19 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#undef WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <sys/types.h>
|
2008-05-15 03:14:48 +00:00
|
|
|
#ifndef WIN32
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_SYS_TIME_H
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <sys/time.h>
|
2009-01-27 21:10:31 +00:00
|
|
|
#else
|
2002-10-07 00:47:34 +00:00
|
|
|
#include <sys/_time.h>
|
|
|
|
#endif
|
2008-05-15 03:14:48 +00:00
|
|
|
#endif
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <sys/queue.h>
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_SYS_SOCKET_H
|
2008-03-02 21:18:33 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_UNISTD_H
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <unistd.h>
|
2003-06-12 23:33:19 +00:00
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_SYS_EVENTFD_H
|
2009-01-19 20:37:24 +00:00
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#endif
|
2008-05-29 01:39:43 +00:00
|
|
|
#include <ctype.h>
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <errno.h>
|
2006-03-28 04:16:14 +00:00
|
|
|
#include <signal.h>
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2007-07-30 22:41:00 +00:00
|
|
|
#include <time.h>
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2008-04-16 20:01:51 +00:00
|
|
|
#include "event2/event.h"
|
|
|
|
#include "event2/event_struct.h"
|
|
|
|
#include "event2/event_compat.h"
|
2004-11-25 09:50:18 +00:00
|
|
|
#include "event-internal.h"
|
2009-04-10 14:22:33 +00:00
|
|
|
#include "defer-internal.h"
|
2008-03-02 21:18:33 +00:00
|
|
|
#include "evthread-internal.h"
|
|
|
|
#include "event2/thread.h"
|
2008-04-16 20:01:51 +00:00
|
|
|
#include "event2/util.h"
|
2009-01-13 20:26:37 +00:00
|
|
|
#include "log-internal.h"
|
|
|
|
#include "evmap-internal.h"
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_EVENT_PORTS
|
2006-07-15 02:55:57 +00:00
|
|
|
extern const struct eventop evportops;
|
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_SELECT
|
2003-09-25 03:26:53 +00:00
|
|
|
extern const struct eventop selectops;
|
2002-04-09 15:14:06 +00:00
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_POLL
|
2003-09-25 03:26:53 +00:00
|
|
|
extern const struct eventop pollops;
|
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_EPOLL
|
2003-09-25 03:26:53 +00:00
|
|
|
extern const struct eventop epollops;
|
2003-03-07 23:20:36 +00:00
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_WORKING_KQUEUE
|
2003-09-25 03:26:53 +00:00
|
|
|
extern const struct eventop kqops;
|
2002-04-09 15:14:06 +00:00
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_DEVPOLL
|
2004-07-30 04:57:21 +00:00
|
|
|
extern const struct eventop devpollops;
|
|
|
|
#endif
|
2003-06-12 23:33:19 +00:00
|
|
|
#ifdef WIN32
|
2003-09-25 03:26:53 +00:00
|
|
|
extern const struct eventop win32ops;
|
2003-06-12 23:33:19 +00:00
|
|
|
#endif
|
2002-04-09 15:14:06 +00:00
|
|
|
|
|
|
|
/* In order of preference */
|
2008-12-13 06:11:12 +00:00
|
|
|
static const struct eventop *eventops[] = {
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_EVENT_PORTS
|
2006-07-15 02:55:57 +00:00
|
|
|
&evportops,
|
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_WORKING_KQUEUE
|
2002-04-09 15:14:06 +00:00
|
|
|
&kqops,
|
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_EPOLL
|
2003-03-07 23:20:36 +00:00
|
|
|
&epollops,
|
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_DEVPOLL
|
2004-07-30 04:57:21 +00:00
|
|
|
&devpollops,
|
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_POLL
|
2003-02-28 22:38:30 +00:00
|
|
|
&pollops,
|
|
|
|
#endif
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_SELECT
|
2002-04-09 15:14:06 +00:00
|
|
|
&selectops,
|
2003-06-12 23:33:19 +00:00
|
|
|
#endif
|
|
|
|
#ifdef WIN32
|
|
|
|
&win32ops,
|
2002-04-09 15:14:06 +00:00
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2004-05-24 00:19:52 +00:00
|
|
|
/* Global state */
|
2004-11-25 09:50:18 +00:00
|
|
|
struct event_base *current_base = NULL;
|
2008-12-23 22:23:37 +00:00
|
|
|
extern struct event_base *evsig_base;
|
2007-07-30 22:41:00 +00:00
|
|
|
static int use_monotonic;
|
2002-04-09 15:14:06 +00:00
|
|
|
|
|
|
|
/* Prototypes */
|
2008-05-17 02:14:17 +00:00
|
|
|
static inline int event_add_internal(struct event *ev,
|
|
|
|
const struct timeval *tv);
|
2008-03-02 21:18:33 +00:00
|
|
|
static inline int event_del_internal(struct event *ev);
|
|
|
|
static inline void event_active_internal(struct event *ev, int res,short count);
|
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
static void event_queue_insert(struct event_base *, struct event *, int);
|
|
|
|
static void event_queue_remove(struct event_base *, struct event *, int);
|
|
|
|
static int event_haveevents(struct event_base *);
|
2004-03-22 21:46:45 +00:00
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
static void event_process_active(struct event_base *);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-07-30 22:41:00 +00:00
|
|
|
static int timeout_next(struct event_base *, struct timeval **);
|
2004-11-25 09:50:18 +00:00
|
|
|
static void timeout_process(struct event_base *);
|
|
|
|
static void timeout_correct(struct event_base *, struct timeval *);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2008-05-03 21:37:33 +00:00
|
|
|
static void event_signal_closure(struct event_base *, struct event *ev);
|
2009-01-22 02:33:38 +00:00
|
|
|
static void event_persist_closure(struct event_base *, struct event *ev);
|
2008-05-03 21:37:33 +00:00
|
|
|
|
2009-07-21 19:20:25 +00:00
|
|
|
static int evthread_notify_base(struct event_base *base);
|
2009-01-19 01:34:14 +00:00
|
|
|
|
2007-07-30 22:41:00 +00:00
|
|
|
static void
|
|
|
|
detect_monotonic(void)
|
|
|
|
{
|
2009-01-27 22:30:46 +00:00
|
|
|
#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
2007-07-30 22:41:00 +00:00
|
|
|
struct timespec ts;
|
2009-09-11 18:21:57 +00:00
|
|
|
static int use_monotonic_initialized = 0;
|
|
|
|
|
|
|
|
if (use_monotonic_initialized)
|
|
|
|
return;
|
2007-07-30 22:41:00 +00:00
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
|
|
|
|
use_monotonic = 1;
|
2009-09-11 18:21:57 +00:00
|
|
|
|
|
|
|
use_monotonic_initialized = 1;
|
2007-07-30 22:41:00 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-03-28 04:33:41 +00:00
|
|
|
static int
|
2008-05-03 18:23:44 +00:00
|
|
|
gettime(struct event_base *base, struct timeval *tp)
|
2006-03-28 04:33:41 +00:00
|
|
|
{
|
2008-05-03 18:23:44 +00:00
|
|
|
if (base->tv_cache.tv_sec) {
|
|
|
|
*tp = base->tv_cache;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-01-27 22:30:46 +00:00
|
|
|
#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
2007-07-30 22:41:00 +00:00
|
|
|
if (use_monotonic) {
|
2008-05-04 17:23:20 +00:00
|
|
|
struct timespec ts;
|
|
|
|
|
2007-07-30 22:41:00 +00:00
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
tp->tv_sec = ts.tv_sec;
|
|
|
|
tp->tv_usec = ts.tv_nsec / 1000;
|
|
|
|
return (0);
|
|
|
|
}
|
2006-03-28 04:33:41 +00:00
|
|
|
#endif
|
|
|
|
|
2008-05-08 14:06:33 +00:00
|
|
|
return (evutil_gettimeofday(tp, NULL));
|
2006-03-28 04:33:41 +00:00
|
|
|
}
|
|
|
|
|
2007-11-07 01:48:44 +00:00
|
|
|
struct event_base *
|
2002-04-09 15:14:06 +00:00
|
|
|
event_init(void)
|
2007-11-14 17:52:21 +00:00
|
|
|
{
|
|
|
|
struct event_base *base = event_base_new();
|
|
|
|
|
|
|
|
if (base != NULL)
|
|
|
|
current_base = base;
|
|
|
|
|
|
|
|
return (base);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct event_base *
|
|
|
|
event_base_new(void)
|
2008-05-08 05:56:20 +00:00
|
|
|
{
|
|
|
|
return (event_base_new_with_config(NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
event_config_is_avoided_method(struct event_config *cfg, const char *method)
|
|
|
|
{
|
|
|
|
struct event_config_entry *entry;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(entry, &cfg->entries, next) {
|
|
|
|
if (entry->avoid_method != NULL &&
|
|
|
|
strcmp(entry->avoid_method, method) == 0)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-05-29 01:39:43 +00:00
|
|
|
static int
|
|
|
|
event_is_method_disabled(const char *name)
|
|
|
|
{
|
|
|
|
char environment[64];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
evutil_snprintf(environment, sizeof(environment), "EVENT_NO%s", name);
|
|
|
|
for (i = 8; environment[i] != '\0'; ++i)
|
|
|
|
environment[i] = toupper(environment[i]);
|
|
|
|
return (getenv(environment) != NULL);
|
2008-05-31 14:37:31 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 18:48:22 +00:00
|
|
|
int
|
2008-05-31 14:37:31 +00:00
|
|
|
event_base_get_features(struct event_base *base)
|
|
|
|
{
|
|
|
|
return base->evsel->features;
|
2008-05-29 01:39:43 +00:00
|
|
|
}
|
|
|
|
|
2009-07-26 01:29:39 +00:00
|
|
|
void
|
|
|
|
event_deferred_cb_queue_init(struct deferred_cb_queue *cb)
|
|
|
|
{
|
|
|
|
memset(cb, 0, sizeof(struct deferred_cb_queue));
|
|
|
|
TAILQ_INIT(&cb->deferred_cb_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
notify_base_cbq_callback(struct deferred_cb_queue *cb, void *baseptr)
|
|
|
|
{
|
|
|
|
struct event_base *base = baseptr;
|
|
|
|
if (!EVBASE_IN_THREAD(base))
|
|
|
|
evthread_notify_base(base);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct deferred_cb_queue *
|
|
|
|
event_base_get_deferred_cb_queue(struct event_base *base)
|
|
|
|
{
|
|
|
|
return base ? &base->defer_queue : NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-08 05:56:20 +00:00
|
|
|
struct event_base *
|
|
|
|
event_base_new_with_config(struct event_config *cfg)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
|
|
|
int i;
|
2007-03-10 06:37:53 +00:00
|
|
|
struct event_base *base;
|
2009-04-22 19:41:23 +00:00
|
|
|
int should_check_environment;
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2008-04-25 01:18:08 +00:00
|
|
|
if ((base = mm_calloc(1, sizeof(struct event_base))) == NULL)
|
2007-12-06 18:12:56 +00:00
|
|
|
event_err(1, "%s: calloc", __func__);
|
2004-11-25 09:50:18 +00:00
|
|
|
|
2007-07-30 22:41:00 +00:00
|
|
|
detect_monotonic();
|
2008-05-03 18:23:44 +00:00
|
|
|
gettime(base, &base->event_tv);
|
2008-05-31 14:37:31 +00:00
|
|
|
|
2007-11-03 18:04:53 +00:00
|
|
|
min_heap_ctor(&base->timeheap);
|
2007-03-10 06:37:53 +00:00
|
|
|
TAILQ_INIT(&base->eventqueue);
|
|
|
|
base->sig.ev_signal_pair[0] = -1;
|
|
|
|
base->sig.ev_signal_pair[1] = -1;
|
2008-05-31 14:37:31 +00:00
|
|
|
|
2009-07-26 01:29:39 +00:00
|
|
|
event_deferred_cb_queue_init(&base->defer_queue);
|
|
|
|
base->defer_queue.notify_fn = notify_base_cbq_callback;
|
|
|
|
base->defer_queue.notify_arg = base;
|
|
|
|
|
2009-01-14 18:38:03 +00:00
|
|
|
evmap_io_initmap(&base->io);
|
|
|
|
evmap_signal_initmap(&base->sigmap);
|
|
|
|
|
2007-03-10 06:37:53 +00:00
|
|
|
base->evbase = NULL;
|
2009-04-22 19:41:23 +00:00
|
|
|
|
|
|
|
should_check_environment =
|
2009-04-28 19:08:36 +00:00
|
|
|
!(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV));
|
2009-04-22 19:41:23 +00:00
|
|
|
|
2007-03-10 06:37:53 +00:00
|
|
|
for (i = 0; eventops[i] && !base->evbase; i++) {
|
2008-05-08 05:56:20 +00:00
|
|
|
if (cfg != NULL) {
|
|
|
|
/* determine if this backend should be avoided */
|
|
|
|
if (event_config_is_avoided_method(cfg,
|
|
|
|
eventops[i]->name))
|
|
|
|
continue;
|
2008-05-31 14:37:31 +00:00
|
|
|
if ((eventops[i]->features & cfg->require_features)
|
2008-12-23 16:37:01 +00:00
|
|
|
!= cfg->require_features)
|
2008-05-31 14:37:31 +00:00
|
|
|
continue;
|
2008-05-08 05:56:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-29 01:39:43 +00:00
|
|
|
/* also obey the environment variables */
|
2009-04-22 19:41:23 +00:00
|
|
|
if (should_check_environment &&
|
|
|
|
event_is_method_disabled(eventops[i]->name))
|
2008-05-29 01:39:43 +00:00
|
|
|
continue;
|
|
|
|
|
2007-03-10 06:37:53 +00:00
|
|
|
base->evsel = eventops[i];
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-03-10 06:37:53 +00:00
|
|
|
base->evbase = base->evsel->init(base);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2008-05-31 14:37:31 +00:00
|
|
|
if (base->evbase == NULL) {
|
|
|
|
if (cfg == NULL)
|
2008-12-23 16:37:01 +00:00
|
|
|
event_errx(1, "%s: no event mechanism available",
|
|
|
|
__func__);
|
|
|
|
else {
|
2008-05-31 14:37:31 +00:00
|
|
|
event_base_free(base);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2003-03-01 19:46:27 +00:00
|
|
|
|
2008-05-31 14:37:31 +00:00
|
|
|
if (getenv("EVENT_SHOW_METHOD"))
|
2008-05-29 01:39:43 +00:00
|
|
|
event_msgx("libevent using: %s", base->evsel->name);
|
2004-09-19 21:08:09 +00:00
|
|
|
|
|
|
|
/* allocate a single active event queue */
|
2007-03-10 06:37:53 +00:00
|
|
|
event_base_priority_init(base, 1);
|
2004-11-25 09:50:18 +00:00
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
/* prepare for threading */
|
|
|
|
base->th_notify_fd[0] = -1;
|
|
|
|
base->th_notify_fd[1] = -1;
|
|
|
|
|
2009-02-12 22:19:54 +00:00
|
|
|
if (!cfg || !(cfg->flags & EVENT_BASE_FLAG_NOLOCK)) {
|
|
|
|
int r;
|
|
|
|
EVTHREAD_ALLOC_LOCK(base->th_base_lock);
|
2009-07-26 01:29:39 +00:00
|
|
|
base->defer_queue.lock = base->th_base_lock;
|
2009-07-14 16:54:48 +00:00
|
|
|
EVTHREAD_ALLOC_LOCK(base->current_event_lock);
|
2009-02-12 22:19:54 +00:00
|
|
|
r = evthread_make_base_notifiable(base);
|
|
|
|
if (r<0) {
|
|
|
|
event_base_free(base);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 06:37:53 +00:00
|
|
|
return (base);
|
2004-09-19 21:08:09 +00:00
|
|
|
}
|
|
|
|
|
2006-03-28 04:40:54 +00:00
|
|
|
void
|
|
|
|
event_base_free(struct event_base *base)
|
|
|
|
{
|
2007-11-07 05:02:21 +00:00
|
|
|
int i, n_deleted=0;
|
|
|
|
struct event *ev;
|
2006-03-28 04:40:54 +00:00
|
|
|
|
|
|
|
if (base == NULL && current_base)
|
|
|
|
base = current_base;
|
2007-11-07 05:02:21 +00:00
|
|
|
if (base == current_base)
|
2006-03-28 04:40:54 +00:00
|
|
|
current_base = NULL;
|
|
|
|
|
2007-11-03 18:04:53 +00:00
|
|
|
/* XXX(niels) - check for internal events first */
|
2006-03-28 04:40:54 +00:00
|
|
|
assert(base);
|
2008-03-02 21:18:33 +00:00
|
|
|
|
|
|
|
/* threading fds if we have them */
|
|
|
|
if (base->th_notify_fd[0] != -1) {
|
|
|
|
event_del(&base->th_notify);
|
2008-05-15 03:19:05 +00:00
|
|
|
EVUTIL_CLOSESOCKET(base->th_notify_fd[0]);
|
|
|
|
EVUTIL_CLOSESOCKET(base->th_notify_fd[1]);
|
2009-01-19 20:37:24 +00:00
|
|
|
base->th_notify_fd[0] = -1;
|
|
|
|
base->th_notify_fd[1] = -1;
|
2008-03-02 21:18:33 +00:00
|
|
|
}
|
|
|
|
|
2007-11-07 05:02:21 +00:00
|
|
|
/* Delete all non-internal events. */
|
|
|
|
for (ev = TAILQ_FIRST(&base->eventqueue); ev; ) {
|
|
|
|
struct event *next = TAILQ_NEXT(ev, ev_next);
|
|
|
|
if (!(ev->ev_flags & EVLIST_INTERNAL)) {
|
|
|
|
event_del(ev);
|
|
|
|
++n_deleted;
|
|
|
|
}
|
|
|
|
ev = next;
|
|
|
|
}
|
2008-01-26 07:29:57 +00:00
|
|
|
while ((ev = min_heap_top(&base->timeheap)) != NULL) {
|
|
|
|
event_del(ev);
|
|
|
|
++n_deleted;
|
|
|
|
}
|
|
|
|
|
2008-07-25 00:19:15 +00:00
|
|
|
for (i = 0; i < base->nactivequeues; ++i) {
|
|
|
|
for (ev = TAILQ_FIRST(base->activequeues[i]); ev; ) {
|
|
|
|
struct event *next = TAILQ_NEXT(ev, ev_active_next);
|
|
|
|
if (!(ev->ev_flags & EVLIST_INTERNAL)) {
|
|
|
|
event_del(ev);
|
|
|
|
++n_deleted;
|
|
|
|
}
|
|
|
|
ev = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-07 05:02:21 +00:00
|
|
|
if (n_deleted)
|
|
|
|
event_debug(("%s: %d events were still set in base",
|
2008-07-25 00:19:15 +00:00
|
|
|
__func__, n_deleted));
|
2007-11-07 05:02:21 +00:00
|
|
|
|
2008-09-26 13:36:15 +00:00
|
|
|
if (base->evsel != NULL && base->evsel->dealloc != NULL)
|
2008-12-23 16:37:01 +00:00
|
|
|
base->evsel->dealloc(base);
|
2007-11-07 05:02:21 +00:00
|
|
|
|
2008-01-26 07:29:57 +00:00
|
|
|
for (i = 0; i < base->nactivequeues; ++i)
|
2006-03-28 04:40:54 +00:00
|
|
|
assert(TAILQ_EMPTY(base->activequeues[i]));
|
2008-01-26 07:29:57 +00:00
|
|
|
|
2007-11-03 18:04:53 +00:00
|
|
|
assert(min_heap_empty(&base->timeheap));
|
2007-11-13 03:32:10 +00:00
|
|
|
min_heap_dtor(&base->timeheap);
|
2006-03-28 04:40:54 +00:00
|
|
|
|
|
|
|
for (i = 0; i < base->nactivequeues; ++i)
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(base->activequeues[i]);
|
|
|
|
mm_free(base->activequeues);
|
2006-03-28 04:40:54 +00:00
|
|
|
|
2007-03-10 06:37:53 +00:00
|
|
|
assert(TAILQ_EMPTY(&base->eventqueue));
|
2006-03-28 04:40:54 +00:00
|
|
|
|
2009-01-09 13:42:21 +00:00
|
|
|
evmap_io_clear(&base->io);
|
|
|
|
evmap_signal_clear(&base->sigmap);
|
2008-12-23 16:37:01 +00:00
|
|
|
|
2009-05-22 14:31:07 +00:00
|
|
|
EVTHREAD_FREE_LOCK(base->th_base_lock);
|
2009-07-14 16:54:48 +00:00
|
|
|
EVTHREAD_FREE_LOCK(base->current_event_lock);
|
2009-05-22 14:31:07 +00:00
|
|
|
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(base);
|
2006-03-28 04:40:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-25 06:57:59 +00:00
|
|
|
/* reinitialized the event base after a fork */
|
|
|
|
int
|
|
|
|
event_reinit(struct event_base *base)
|
|
|
|
{
|
|
|
|
const struct eventop *evsel = base->evsel;
|
|
|
|
int res = 0;
|
|
|
|
struct event *ev;
|
|
|
|
|
|
|
|
/* check if this event mechanism requires reinit */
|
|
|
|
if (!evsel->need_reinit)
|
|
|
|
return (0);
|
|
|
|
|
2008-07-25 00:19:15 +00:00
|
|
|
/* prevent internal delete */
|
2008-07-25 00:48:30 +00:00
|
|
|
if (base->sig.ev_signal_added) {
|
2009-01-19 23:40:11 +00:00
|
|
|
/* we cannot call event_del here because the base has
|
|
|
|
* not been reinitialized yet. */
|
2008-07-25 00:48:30 +00:00
|
|
|
event_queue_remove(base, &base->sig.ev_signal,
|
|
|
|
EVLIST_INSERTED);
|
2009-01-19 23:40:11 +00:00
|
|
|
if (base->sig.ev_signal.ev_flags & EVLIST_ACTIVE)
|
|
|
|
event_queue_remove(base, &base->sig.ev_signal,
|
|
|
|
EVLIST_ACTIVE);
|
2008-07-25 00:19:15 +00:00
|
|
|
base->sig.ev_signal_added = 0;
|
2008-07-25 00:48:30 +00:00
|
|
|
}
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2007-11-25 07:11:59 +00:00
|
|
|
if (base->evsel->dealloc != NULL)
|
2008-12-23 16:37:01 +00:00
|
|
|
base->evsel->dealloc(base);
|
|
|
|
base->evbase = evsel->init(base);
|
2007-11-25 06:57:59 +00:00
|
|
|
if (base->evbase == NULL)
|
|
|
|
event_errx(1, "%s: could not reinitialize event mechanism",
|
|
|
|
__func__);
|
|
|
|
|
2009-01-09 13:42:21 +00:00
|
|
|
evmap_io_clear(&base->io);
|
|
|
|
evmap_signal_clear(&base->sigmap);
|
2008-12-23 16:37:01 +00:00
|
|
|
|
2007-11-25 06:57:59 +00:00
|
|
|
TAILQ_FOREACH(ev, &base->eventqueue, ev_next) {
|
2008-12-23 16:37:01 +00:00
|
|
|
if (ev->ev_events & (EV_READ|EV_WRITE)) {
|
|
|
|
if (evmap_io_add(base, ev->ev_fd, ev) == -1)
|
|
|
|
res = -1;
|
|
|
|
} else if (ev->ev_events & EV_SIGNAL) {
|
|
|
|
if (evmap_signal_add(base, ev->ev_fd, ev) == -1)
|
|
|
|
res = -1;
|
|
|
|
}
|
2007-11-25 06:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
|
2008-05-08 05:33:15 +00:00
|
|
|
const char **
|
2008-05-08 22:51:39 +00:00
|
|
|
event_get_supported_methods(void)
|
2008-05-08 05:33:15 +00:00
|
|
|
{
|
2009-07-28 05:09:06 +00:00
|
|
|
static const char **methods = NULL;
|
2008-05-08 05:33:15 +00:00
|
|
|
const struct eventop **method;
|
|
|
|
const char **tmp;
|
|
|
|
int i = 0, k;
|
|
|
|
|
|
|
|
/* count all methods */
|
2008-05-29 01:39:43 +00:00
|
|
|
for (method = &eventops[0]; *method != NULL; ++method) {
|
2008-05-08 05:33:15 +00:00
|
|
|
++i;
|
2008-05-29 01:39:43 +00:00
|
|
|
}
|
2008-05-08 05:33:15 +00:00
|
|
|
|
|
|
|
/* allocate one more than we need for the NULL pointer */
|
|
|
|
tmp = mm_malloc((i + 1) * sizeof(char *));
|
|
|
|
if (tmp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/* populate the array with the supported methods */
|
2008-05-29 01:39:43 +00:00
|
|
|
for (k = 0, i = 0; eventops[k] != NULL; ++k) {
|
|
|
|
tmp[i++] = eventops[k]->name;
|
|
|
|
}
|
2008-05-08 05:33:15 +00:00
|
|
|
tmp[i] = NULL;
|
|
|
|
|
2008-05-29 01:39:43 +00:00
|
|
|
if (methods != NULL)
|
2009-04-30 23:49:15 +00:00
|
|
|
mm_free((char**)methods);
|
2008-05-29 01:39:43 +00:00
|
|
|
|
2008-05-08 05:33:15 +00:00
|
|
|
methods = tmp;
|
2008-05-31 14:37:31 +00:00
|
|
|
|
2008-05-08 05:33:15 +00:00
|
|
|
return (methods);
|
|
|
|
}
|
|
|
|
|
2008-05-08 05:56:20 +00:00
|
|
|
struct event_config *
|
|
|
|
event_config_new(void)
|
|
|
|
{
|
2009-04-23 21:34:37 +00:00
|
|
|
struct event_config *cfg = mm_calloc(1, sizeof(*cfg));
|
2008-05-08 05:56:20 +00:00
|
|
|
|
|
|
|
if (cfg == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
TAILQ_INIT(&cfg->entries);
|
2008-05-31 14:37:31 +00:00
|
|
|
|
2008-05-08 05:56:20 +00:00
|
|
|
return (cfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
event_config_entry_free(struct event_config_entry *entry)
|
|
|
|
{
|
|
|
|
if (entry->avoid_method != NULL)
|
|
|
|
mm_free((char *)entry->avoid_method);
|
|
|
|
mm_free(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
event_config_free(struct event_config *cfg)
|
|
|
|
{
|
|
|
|
struct event_config_entry *entry;
|
|
|
|
|
|
|
|
while ((entry = TAILQ_FIRST(&cfg->entries)) != NULL) {
|
|
|
|
TAILQ_REMOVE(&cfg->entries, entry, next);
|
|
|
|
event_config_entry_free(entry);
|
|
|
|
}
|
2008-12-19 21:02:36 +00:00
|
|
|
mm_free(cfg);
|
2008-05-08 05:56:20 +00:00
|
|
|
}
|
|
|
|
|
2009-04-28 19:08:17 +00:00
|
|
|
|
|
|
|
int
|
2009-10-21 18:48:22 +00:00
|
|
|
event_config_set_flag(struct event_config *cfg, int flag)
|
2009-04-28 19:08:17 +00:00
|
|
|
{
|
|
|
|
if (!cfg)
|
|
|
|
return -1;
|
|
|
|
cfg->flags |= flag;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-08 05:56:20 +00:00
|
|
|
int
|
|
|
|
event_config_avoid_method(struct event_config *cfg, const char *method)
|
|
|
|
{
|
|
|
|
struct event_config_entry *entry = mm_malloc(sizeof(*entry));
|
|
|
|
if (entry == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if ((entry->avoid_method = mm_strdup(method)) == NULL) {
|
|
|
|
mm_free(entry);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&cfg->entries, entry, next);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-05-31 14:37:31 +00:00
|
|
|
int
|
|
|
|
event_config_require_features(struct event_config *cfg,
|
2009-10-21 18:48:22 +00:00
|
|
|
int features)
|
2008-05-31 14:37:31 +00:00
|
|
|
{
|
|
|
|
if (!cfg)
|
|
|
|
return (-1);
|
|
|
|
cfg->require_features = features;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-09-19 21:08:09 +00:00
|
|
|
int
|
2004-12-01 20:04:54 +00:00
|
|
|
event_priority_init(int npriorities)
|
|
|
|
{
|
|
|
|
return event_base_priority_init(current_base, npriorities);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
event_base_priority_init(struct event_base *base, int npriorities)
|
2004-09-19 21:08:09 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2009-07-26 01:29:39 +00:00
|
|
|
if (N_ACTIVE_CALLBACKS(base) || npriorities < 1
|
2009-05-15 18:44:44 +00:00
|
|
|
|| npriorities >= EVENT_MAX_PRIORITIES)
|
2004-09-19 21:08:09 +00:00
|
|
|
return (-1);
|
|
|
|
|
2009-01-27 16:29:48 +00:00
|
|
|
if (npriorities == base->nactivequeues)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (base->nactivequeues) {
|
2004-11-25 09:50:18 +00:00
|
|
|
for (i = 0; i < base->nactivequeues; ++i) {
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(base->activequeues[i]);
|
2004-09-19 21:08:09 +00:00
|
|
|
}
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(base->activequeues);
|
2004-09-19 21:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate our priority queues */
|
2004-11-25 09:50:18 +00:00
|
|
|
base->nactivequeues = npriorities;
|
2008-04-25 01:18:08 +00:00
|
|
|
base->activequeues = (struct event_list **)mm_calloc(
|
2007-11-25 17:14:19 +00:00
|
|
|
base->nactivequeues,
|
2007-12-24 22:49:30 +00:00
|
|
|
npriorities * sizeof(struct event_list *));
|
2004-11-25 09:50:18 +00:00
|
|
|
if (base->activequeues == NULL)
|
2005-03-29 07:03:10 +00:00
|
|
|
event_err(1, "%s: calloc", __func__);
|
2004-09-19 21:08:09 +00:00
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
for (i = 0; i < base->nactivequeues; ++i) {
|
2008-04-25 01:18:08 +00:00
|
|
|
base->activequeues[i] = mm_malloc(sizeof(struct event_list));
|
2004-11-25 09:50:18 +00:00
|
|
|
if (base->activequeues[i] == NULL)
|
2005-03-29 07:03:10 +00:00
|
|
|
event_err(1, "%s: malloc", __func__);
|
2004-11-25 09:50:18 +00:00
|
|
|
TAILQ_INIT(base->activequeues[i]);
|
2004-09-19 21:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2009-07-21 19:20:25 +00:00
|
|
|
static int
|
2004-11-25 09:50:18 +00:00
|
|
|
event_haveevents(struct event_base *base)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2009-10-21 03:54:00 +00:00
|
|
|
/* Caller must hold th_base_lock */
|
2004-11-25 09:50:18 +00:00
|
|
|
return (base->event_count > 0);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2009-01-22 02:33:38 +00:00
|
|
|
static void
|
|
|
|
event_persist_closure(struct event_base *base, struct event *ev)
|
|
|
|
{
|
|
|
|
/* reschedule the persistent event if we have a timeout */
|
|
|
|
if (ev->ev_io_timeout.tv_sec || ev->ev_io_timeout.tv_usec)
|
|
|
|
event_add(ev, &ev->ev_io_timeout);
|
|
|
|
(*ev->ev_callback)((int)ev->ev_fd, ev->ev_res, ev->ev_arg);
|
|
|
|
}
|
|
|
|
|
2008-05-03 21:37:33 +00:00
|
|
|
static void
|
|
|
|
event_signal_closure(struct event_base *base, struct event *ev)
|
|
|
|
{
|
|
|
|
short ncalls;
|
|
|
|
|
|
|
|
/* Allows deletes to work */
|
|
|
|
ncalls = ev->ev_ncalls;
|
|
|
|
ev->ev_pncalls = &ncalls;
|
|
|
|
while (ncalls) {
|
|
|
|
ncalls--;
|
|
|
|
ev->ev_ncalls = ncalls;
|
|
|
|
(*ev->ev_callback)((int)ev->ev_fd, ev->ev_res, ev->ev_arg);
|
2009-05-03 18:56:08 +00:00
|
|
|
if (base->event_break)
|
2008-05-03 21:37:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-19 21:08:09 +00:00
|
|
|
/*
|
2009-04-10 14:21:53 +00:00
|
|
|
Helper for event_process_active to process all the events in a single queue,
|
|
|
|
releasing the lock as we go. This function requires that the lock be held
|
|
|
|
when it's invoked. Returns -1 if we get a signal or an event_break that
|
|
|
|
means we should stop processing any active events now. Otherwise returns
|
|
|
|
the number of non-internal events that we processed.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
event_process_active_single_queue(struct event_base *base,
|
|
|
|
struct event_list *activeq)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2007-12-24 22:49:30 +00:00
|
|
|
struct event *ev;
|
2009-04-10 14:21:53 +00:00
|
|
|
int count = 0;
|
2004-09-19 21:08:09 +00:00
|
|
|
|
2007-01-27 04:06:52 +00:00
|
|
|
assert(activeq != NULL);
|
|
|
|
|
2004-09-19 21:08:09 +00:00
|
|
|
for (ev = TAILQ_FIRST(activeq); ev; ev = TAILQ_FIRST(activeq)) {
|
2007-12-24 22:49:30 +00:00
|
|
|
if (ev->ev_events & EV_PERSIST)
|
|
|
|
event_queue_remove(base, ev, EVLIST_ACTIVE);
|
|
|
|
else
|
2008-03-02 21:18:33 +00:00
|
|
|
event_del_internal(ev);
|
2009-04-10 14:21:53 +00:00
|
|
|
if (!(ev->ev_flags & EVLIST_INTERNAL))
|
|
|
|
++count;
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2008-06-24 22:41:43 +00:00
|
|
|
event_debug((
|
|
|
|
"event_process_active: event: %p, %s%scall %p",
|
2009-01-27 21:10:31 +00:00
|
|
|
ev,
|
2008-06-24 22:41:43 +00:00
|
|
|
ev->ev_res & EV_READ ? "EV_READ " : " ",
|
|
|
|
ev->ev_res & EV_WRITE ? "EV_WRITE " : " ",
|
|
|
|
ev->ev_callback));
|
|
|
|
|
2009-07-14 16:54:48 +00:00
|
|
|
base->current_event = ev;
|
|
|
|
|
|
|
|
EVBASE_ACQUIRE_LOCK(base, EVTHREAD_WRITE, current_event_lock);
|
|
|
|
|
2009-02-12 22:19:54 +00:00
|
|
|
EVBASE_RELEASE_LOCK(base,
|
2008-03-10 03:17:20 +00:00
|
|
|
EVTHREAD_WRITE, th_base_lock);
|
2008-03-02 21:18:33 +00:00
|
|
|
|
2009-05-15 18:44:44 +00:00
|
|
|
switch (ev->ev_closure) {
|
|
|
|
case EV_CLOSURE_SIGNAL:
|
|
|
|
event_signal_closure(base, ev);
|
|
|
|
break;
|
|
|
|
case EV_CLOSURE_PERSIST:
|
|
|
|
event_persist_closure(base, ev);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case EV_CLOSURE_NONE:
|
2008-05-03 21:37:33 +00:00
|
|
|
(*ev->ev_callback)(
|
|
|
|
(int)ev->ev_fd, ev->ev_res, ev->ev_arg);
|
2009-05-15 18:44:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-14 16:54:48 +00:00
|
|
|
EVBASE_RELEASE_LOCK(base, EVTHREAD_WRITE, current_event_lock);
|
|
|
|
EVBASE_ACQUIRE_LOCK(base, EVTHREAD_WRITE, th_base_lock);
|
|
|
|
base->current_event = NULL;
|
|
|
|
|
2009-05-03 18:56:08 +00:00
|
|
|
if (base->event_break)
|
2009-04-10 14:21:53 +00:00
|
|
|
return -1;
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
2009-04-10 14:21:53 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2009-04-10 14:22:33 +00:00
|
|
|
static int
|
2009-07-26 01:29:39 +00:00
|
|
|
event_process_deferred_callbacks(struct deferred_cb_queue *queue, int *breakptr)
|
2009-04-10 14:22:33 +00:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
struct deferred_cb *cb;
|
|
|
|
|
2009-07-26 01:29:39 +00:00
|
|
|
while ((cb = TAILQ_FIRST(&queue->deferred_cb_list))) {
|
2009-04-10 14:22:33 +00:00
|
|
|
cb->queued = 0;
|
2009-07-26 01:29:39 +00:00
|
|
|
TAILQ_REMOVE(&queue->deferred_cb_list, cb, cb_next);
|
|
|
|
--queue->active_count;
|
|
|
|
UNLOCK_DEFERRED_QUEUE(queue);
|
2009-04-10 14:22:33 +00:00
|
|
|
|
|
|
|
cb->cb(cb, cb->arg);
|
|
|
|
++count;
|
2009-07-26 01:29:39 +00:00
|
|
|
if (*breakptr)
|
2009-04-10 14:22:33 +00:00
|
|
|
return -1;
|
|
|
|
|
2009-07-26 01:29:39 +00:00
|
|
|
LOCK_DEFERRED_QUEUE(queue);
|
2009-04-10 14:22:33 +00:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2009-04-10 14:21:53 +00:00
|
|
|
/*
|
|
|
|
* Active events are stored in priority queues. Lower priorities are always
|
|
|
|
* process before higher priorities. Low priority events can starve high
|
|
|
|
* priority ones.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
event_process_active(struct event_base *base)
|
|
|
|
{
|
2009-10-21 03:54:00 +00:00
|
|
|
/* Caller must hold th_base_lock */
|
2009-04-10 14:21:53 +00:00
|
|
|
struct event_list *activeq = NULL;
|
|
|
|
int i, c;
|
|
|
|
|
|
|
|
for (i = 0; i < base->nactivequeues; ++i) {
|
|
|
|
if (TAILQ_FIRST(base->activequeues[i]) != NULL) {
|
|
|
|
activeq = base->activequeues[i];
|
|
|
|
c = event_process_active_single_queue(base, activeq);
|
|
|
|
if (c < 0)
|
2009-10-21 03:54:00 +00:00
|
|
|
return;
|
2009-04-10 14:21:53 +00:00
|
|
|
else if (c > 0)
|
|
|
|
break; /* Processed a real event; do not
|
|
|
|
* consider lower-priority events */
|
|
|
|
/* If we get here, all of the events we processed
|
|
|
|
* were internal. Continue. */
|
|
|
|
}
|
|
|
|
}
|
2008-03-02 21:18:33 +00:00
|
|
|
|
2009-07-26 01:29:39 +00:00
|
|
|
event_process_deferred_callbacks(&base->defer_queue,&base->event_break);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2004-03-22 21:46:45 +00:00
|
|
|
/*
|
2009-10-16 13:19:57 +00:00
|
|
|
* Wait continuously for events. We exit only if no events are left.
|
2004-03-22 21:46:45 +00:00
|
|
|
*/
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
int
|
|
|
|
event_dispatch(void)
|
|
|
|
{
|
|
|
|
return (event_loop(0));
|
|
|
|
}
|
|
|
|
|
2004-12-01 20:04:54 +00:00
|
|
|
int
|
|
|
|
event_base_dispatch(struct event_base *event_base)
|
|
|
|
{
|
|
|
|
return (event_base_loop(event_base, 0));
|
|
|
|
}
|
|
|
|
|
2008-02-16 06:09:39 +00:00
|
|
|
const char *
|
|
|
|
event_base_get_method(struct event_base *base)
|
|
|
|
{
|
|
|
|
assert(base);
|
|
|
|
return (base->evsel->name);
|
|
|
|
}
|
|
|
|
|
2004-03-22 21:46:45 +00:00
|
|
|
static void
|
2007-11-25 21:53:06 +00:00
|
|
|
event_loopexit_cb(evutil_socket_t fd, short what, void *arg)
|
2004-03-22 21:46:45 +00:00
|
|
|
{
|
2004-11-25 09:50:18 +00:00
|
|
|
struct event_base *base = arg;
|
|
|
|
base->event_gotterm = 1;
|
2004-03-22 21:46:45 +00:00
|
|
|
}
|
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
/* not thread safe */
|
2004-03-22 21:46:45 +00:00
|
|
|
int
|
2008-05-17 02:14:17 +00:00
|
|
|
event_loopexit(const struct timeval *tv)
|
2004-03-22 21:46:45 +00:00
|
|
|
{
|
2004-11-25 09:50:18 +00:00
|
|
|
return (event_once(-1, EV_TIMEOUT, event_loopexit_cb,
|
|
|
|
current_base, tv));
|
2004-03-22 21:46:45 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 08:43:55 +00:00
|
|
|
int
|
2008-05-17 02:14:17 +00:00
|
|
|
event_base_loopexit(struct event_base *event_base, const struct timeval *tv)
|
2005-04-02 08:43:55 +00:00
|
|
|
{
|
2007-03-10 06:37:53 +00:00
|
|
|
return (event_base_once(event_base, -1, EV_TIMEOUT, event_loopexit_cb,
|
2005-04-02 08:43:55 +00:00
|
|
|
event_base, tv));
|
|
|
|
}
|
|
|
|
|
2007-11-17 22:21:42 +00:00
|
|
|
int
|
|
|
|
event_loopbreak(void)
|
|
|
|
{
|
|
|
|
return (event_base_loopbreak(current_base));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
event_base_loopbreak(struct event_base *event_base)
|
|
|
|
{
|
|
|
|
if (event_base == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2009-02-12 22:19:54 +00:00
|
|
|
EVBASE_ACQUIRE_LOCK(event_base, EVTHREAD_WRITE, th_base_lock);
|
2009-01-19 20:22:47 +00:00
|
|
|
event_base->event_break = 1;
|
2009-02-12 22:19:54 +00:00
|
|
|
EVBASE_RELEASE_LOCK(event_base, EVTHREAD_WRITE, th_base_lock);
|
2009-01-19 20:22:47 +00:00
|
|
|
|
2009-02-12 22:19:54 +00:00
|
|
|
if (!EVBASE_IN_THREAD(event_base)) {
|
2009-01-19 20:22:47 +00:00
|
|
|
return evthread_notify_base(event_base);
|
2009-01-19 01:34:14 +00:00
|
|
|
} else {
|
|
|
|
return (0);
|
|
|
|
}
|
2007-11-17 22:21:42 +00:00
|
|
|
}
|
|
|
|
|
2009-10-01 15:29:08 +00:00
|
|
|
int
|
|
|
|
event_base_got_break(struct event_base *event_base)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
EVBASE_ACQUIRE_LOCK(event_base, EVTHREAD_READ, th_base_lock);
|
|
|
|
res = event_base->event_break;
|
|
|
|
EVBASE_RELEASE_LOCK(event_base, EVTHREAD_READ, th_base_lock);
|
|
|
|
return res;
|
|
|
|
}
|
2007-11-17 22:21:42 +00:00
|
|
|
|
2009-10-01 15:29:08 +00:00
|
|
|
int
|
|
|
|
event_base_got_exit(struct event_base *event_base)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
EVBASE_ACQUIRE_LOCK(event_base, EVTHREAD_READ, th_base_lock);
|
|
|
|
res = event_base->event_gotterm;
|
|
|
|
EVBASE_RELEASE_LOCK(event_base, EVTHREAD_READ, th_base_lock);
|
|
|
|
return res;
|
|
|
|
}
|
2007-11-17 22:21:42 +00:00
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
/* not thread safe */
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
int
|
|
|
|
event_loop(int flags)
|
|
|
|
{
|
2004-12-01 20:04:54 +00:00
|
|
|
return event_base_loop(current_base, flags);
|
2004-11-25 09:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-12-01 20:04:54 +00:00
|
|
|
event_base_loop(struct event_base *base, int flags)
|
2004-11-25 09:50:18 +00:00
|
|
|
{
|
|
|
|
const struct eventop *evsel = base->evsel;
|
2002-04-09 15:14:06 +00:00
|
|
|
struct timeval tv;
|
2007-07-30 22:41:00 +00:00
|
|
|
struct timeval *tv_p;
|
2002-04-09 15:14:06 +00:00
|
|
|
int res, done;
|
|
|
|
|
2009-10-21 03:54:00 +00:00
|
|
|
/* Grab the lock. We will release it inside evsel.dispatch, and again
|
|
|
|
* as we invoke user callbacks. */
|
|
|
|
EVBASE_ACQUIRE_LOCK(base, EVTHREAD_WRITE, th_base_lock);
|
|
|
|
|
2008-10-30 19:38:31 +00:00
|
|
|
/* clear time cache */
|
|
|
|
base->tv_cache.tv_sec = 0;
|
|
|
|
|
2008-11-27 19:57:48 +00:00
|
|
|
if (base->sig.ev_signal_added)
|
2008-12-23 22:23:37 +00:00
|
|
|
evsig_base = base;
|
2002-04-09 15:14:06 +00:00
|
|
|
done = 0;
|
2009-02-12 22:19:54 +00:00
|
|
|
|
|
|
|
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
|
|
|
|
base->th_owner_id = EVTHREAD_GET_ID();
|
|
|
|
#endif
|
|
|
|
|
2009-10-01 15:29:08 +00:00
|
|
|
base->event_gotterm = base->event_break = 0;
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
while (!done) {
|
2004-03-22 21:46:45 +00:00
|
|
|
/* Terminate the loop if we have been asked to */
|
2004-11-25 09:50:18 +00:00
|
|
|
if (base->event_gotterm) {
|
2004-03-23 03:43:53 +00:00
|
|
|
break;
|
2004-03-22 21:46:45 +00:00
|
|
|
}
|
|
|
|
|
2007-11-17 22:21:42 +00:00
|
|
|
if (base->event_break) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-07-30 22:41:00 +00:00
|
|
|
timeout_correct(base, &tv);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-07-30 22:41:00 +00:00
|
|
|
tv_p = &tv;
|
2009-07-26 01:29:39 +00:00
|
|
|
if (!N_ACTIVE_CALLBACKS(base) && !(flags & EVLOOP_NONBLOCK)) {
|
2007-07-30 22:41:00 +00:00
|
|
|
timeout_next(base, &tv_p);
|
|
|
|
} else {
|
2009-01-27 21:10:31 +00:00
|
|
|
/*
|
2007-07-30 22:41:00 +00:00
|
|
|
* if we have active events, we just poll new events
|
|
|
|
* without waiting.
|
|
|
|
*/
|
2007-11-07 06:01:57 +00:00
|
|
|
evutil_timerclear(&tv);
|
2007-07-30 22:41:00 +00:00
|
|
|
}
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
/* If we have no events, we just exit */
|
2009-07-26 01:29:39 +00:00
|
|
|
if (!event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) {
|
2005-04-02 08:43:55 +00:00
|
|
|
event_debug(("%s: no events registered.", __func__));
|
2002-04-09 15:14:06 +00:00
|
|
|
return (1);
|
2005-04-02 08:43:55 +00:00
|
|
|
}
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2008-05-03 18:23:44 +00:00
|
|
|
/* update last old time */
|
|
|
|
gettime(base, &base->event_tv);
|
|
|
|
|
|
|
|
/* clear time cache */
|
|
|
|
base->tv_cache.tv_sec = 0;
|
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
res = evsel->dispatch(base, tv_p);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
|
|
|
if (res == -1)
|
|
|
|
return (-1);
|
2008-05-03 18:23:44 +00:00
|
|
|
gettime(base, &base->tv_cache);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
timeout_process(base);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2009-07-26 01:29:39 +00:00
|
|
|
if (N_ACTIVE_CALLBACKS(base)) {
|
2004-11-25 09:50:18 +00:00
|
|
|
event_process_active(base);
|
|
|
|
if (!base->event_count_active && (flags & EVLOOP_ONCE))
|
2002-04-09 15:14:06 +00:00
|
|
|
done = 1;
|
|
|
|
} else if (flags & EVLOOP_NONBLOCK)
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
|
2008-11-27 19:27:33 +00:00
|
|
|
/* clear time cache */
|
|
|
|
base->tv_cache.tv_sec = 0;
|
|
|
|
|
2009-10-21 03:54:00 +00:00
|
|
|
EVBASE_RELEASE_LOCK(base, EVTHREAD_WRITE, th_base_lock);
|
|
|
|
|
2005-04-02 08:43:55 +00:00
|
|
|
event_debug(("%s: asked to terminate loop.", __func__));
|
2002-04-09 15:14:06 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-02-22 21:17:23 +00:00
|
|
|
/* Sets up an event for processing once */
|
|
|
|
|
|
|
|
struct event_once {
|
|
|
|
struct event ev;
|
|
|
|
|
2007-11-25 21:53:06 +00:00
|
|
|
void (*cb)(evutil_socket_t, short, void *);
|
2004-02-22 21:17:23 +00:00
|
|
|
void *arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* One-time callback, it deletes itself */
|
|
|
|
|
|
|
|
static void
|
2007-11-25 21:53:06 +00:00
|
|
|
event_once_cb(evutil_socket_t fd, short events, void *arg)
|
2004-02-22 21:17:23 +00:00
|
|
|
{
|
|
|
|
struct event_once *eonce = arg;
|
|
|
|
|
|
|
|
(*eonce->cb)(fd, events, eonce->arg);
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(eonce);
|
2004-02-22 21:17:23 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 06:37:53 +00:00
|
|
|
/* not threadsafe, event scheduled once. */
|
2004-02-22 21:17:23 +00:00
|
|
|
int
|
2007-11-25 21:53:06 +00:00
|
|
|
event_once(evutil_socket_t fd, short events,
|
|
|
|
void (*callback)(evutil_socket_t, short, void *),
|
2008-05-17 02:14:17 +00:00
|
|
|
void *arg, const struct timeval *tv)
|
2007-03-10 06:37:53 +00:00
|
|
|
{
|
|
|
|
return event_base_once(current_base, fd, events, callback, arg, tv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Schedules an event once */
|
|
|
|
int
|
2007-11-25 21:53:06 +00:00
|
|
|
event_base_once(struct event_base *base, evutil_socket_t fd, short events,
|
|
|
|
void (*callback)(evutil_socket_t, short, void *),
|
2008-05-17 02:14:17 +00:00
|
|
|
void *arg, const struct timeval *tv)
|
2004-02-22 21:17:23 +00:00
|
|
|
{
|
|
|
|
struct event_once *eonce;
|
|
|
|
struct timeval etv;
|
2008-05-03 22:14:44 +00:00
|
|
|
int res = 0;
|
2004-02-22 21:17:23 +00:00
|
|
|
|
2009-04-29 20:48:28 +00:00
|
|
|
/* We cannot support signals that just fire once, or persistent
|
|
|
|
* events. */
|
|
|
|
if (events & (EV_SIGNAL|EV_PERSIST))
|
2004-04-04 02:20:21 +00:00
|
|
|
return (-1);
|
|
|
|
|
2008-04-25 01:18:08 +00:00
|
|
|
if ((eonce = mm_calloc(1, sizeof(struct event_once))) == NULL)
|
2004-02-22 21:17:23 +00:00
|
|
|
return (-1);
|
|
|
|
|
2005-03-12 02:30:32 +00:00
|
|
|
eonce->cb = callback;
|
|
|
|
eonce->arg = arg;
|
|
|
|
|
2004-02-22 21:17:23 +00:00
|
|
|
if (events == EV_TIMEOUT) {
|
|
|
|
if (tv == NULL) {
|
2007-11-07 06:01:57 +00:00
|
|
|
evutil_timerclear(&etv);
|
2004-02-22 21:17:23 +00:00
|
|
|
tv = &etv;
|
|
|
|
}
|
|
|
|
|
2008-05-03 22:14:44 +00:00
|
|
|
evtimer_assign(&eonce->ev, base, event_once_cb, eonce);
|
2004-02-22 21:17:23 +00:00
|
|
|
} else if (events & (EV_READ|EV_WRITE)) {
|
|
|
|
events &= EV_READ|EV_WRITE;
|
|
|
|
|
2008-05-03 22:14:44 +00:00
|
|
|
event_assign(&eonce->ev, base, fd, events, event_once_cb, eonce);
|
2004-02-22 21:17:23 +00:00
|
|
|
} else {
|
|
|
|
/* Bad event combination */
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(eonce);
|
2004-02-22 21:17:23 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2007-03-10 06:37:53 +00:00
|
|
|
if (res == 0)
|
|
|
|
res = event_add(&eonce->ev, tv);
|
2006-10-28 03:20:22 +00:00
|
|
|
if (res != 0) {
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(eonce);
|
2006-10-28 03:20:22 +00:00
|
|
|
return (res);
|
|
|
|
}
|
2004-02-22 21:17:23 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
void
|
2007-11-25 21:53:06 +00:00
|
|
|
event_set(struct event *ev, evutil_socket_t fd, short events,
|
|
|
|
void (*callback)(evutil_socket_t, short, void *), void *arg)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2004-11-25 09:50:18 +00:00
|
|
|
/* Take the current base - caller needs to set the real base later */
|
|
|
|
ev->ev_base = current_base;
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
ev->ev_callback = callback;
|
|
|
|
ev->ev_arg = arg;
|
|
|
|
ev->ev_fd = fd;
|
|
|
|
ev->ev_events = events;
|
2007-12-24 22:49:30 +00:00
|
|
|
ev->ev_res = 0;
|
|
|
|
ev->ev_flags = EVLIST_INIT;
|
2002-04-17 02:07:31 +00:00
|
|
|
ev->ev_ncalls = 0;
|
|
|
|
ev->ev_pncalls = NULL;
|
2004-09-19 21:08:09 +00:00
|
|
|
|
2008-05-03 21:37:33 +00:00
|
|
|
if (events & EV_SIGNAL) {
|
|
|
|
if ((events & (EV_READ|EV_WRITE)) != 0)
|
|
|
|
event_errx(1, "%s: EV_SIGNAL incompatible use",
|
|
|
|
__func__);
|
2009-05-15 18:44:44 +00:00
|
|
|
ev->ev_closure = EV_CLOSURE_SIGNAL;
|
2008-05-03 21:37:33 +00:00
|
|
|
} else {
|
2009-01-22 02:33:38 +00:00
|
|
|
if (events & EV_PERSIST) {
|
|
|
|
timerclear(&ev->ev_io_timeout);
|
2009-05-15 18:44:44 +00:00
|
|
|
ev->ev_closure = EV_CLOSURE_PERSIST;
|
2009-01-22 02:33:38 +00:00
|
|
|
} else {
|
2009-05-15 18:44:44 +00:00
|
|
|
ev->ev_closure = EV_CLOSURE_NONE;
|
2009-01-22 02:33:38 +00:00
|
|
|
}
|
2008-05-03 21:37:33 +00:00
|
|
|
}
|
|
|
|
|
2007-11-03 18:04:53 +00:00
|
|
|
min_heap_elem_init(ev);
|
|
|
|
|
2004-09-19 21:08:09 +00:00
|
|
|
/* by default, we put new events into the middle priority */
|
2008-03-02 21:18:33 +00:00
|
|
|
if (current_base)
|
2007-12-24 22:49:30 +00:00
|
|
|
ev->ev_pri = current_base->nactivequeues/2;
|
2004-11-25 09:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
event_base_set(struct event_base *base, struct event *ev)
|
|
|
|
{
|
|
|
|
/* Only innocent events may be assigned to a different base */
|
2007-12-24 22:49:30 +00:00
|
|
|
if (ev->ev_flags != EVLIST_INIT)
|
2004-11-25 09:50:18 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
ev->ev_base = base;
|
2007-12-24 22:49:30 +00:00
|
|
|
ev->ev_pri = base->nactivequeues/2;
|
2004-11-25 09:50:18 +00:00
|
|
|
|
|
|
|
return (0);
|
2004-09-19 21:08:09 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 22:14:44 +00:00
|
|
|
void
|
2008-04-25 01:18:18 +00:00
|
|
|
event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg)
|
|
|
|
{
|
|
|
|
event_set(ev, fd, events, cb, arg);
|
2008-05-03 22:14:44 +00:00
|
|
|
if (base != NULL)
|
|
|
|
assert(event_base_set(base, ev) == 0);
|
2008-04-25 01:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct event *
|
|
|
|
event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg)
|
|
|
|
{
|
|
|
|
struct event *ev;
|
|
|
|
ev = mm_malloc(sizeof(struct event));
|
2008-05-03 22:14:44 +00:00
|
|
|
if (ev == NULL)
|
|
|
|
return (NULL);
|
|
|
|
event_assign(ev, base, fd, events, cb, arg);
|
|
|
|
|
|
|
|
return (ev);
|
2008-04-25 01:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
event_free(struct event *ev)
|
|
|
|
{
|
2008-04-25 01:29:15 +00:00
|
|
|
/* make sure that this event won't be coming back to haunt us. */
|
2008-04-25 01:18:18 +00:00
|
|
|
event_del(ev);
|
|
|
|
mm_free(ev);
|
|
|
|
}
|
|
|
|
|
2004-09-19 21:08:09 +00:00
|
|
|
/*
|
|
|
|
* Set's the priority of an event - if an event is already scheduled
|
|
|
|
* changing the priority is going to fail.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
event_priority_set(struct event *ev, int pri)
|
|
|
|
{
|
2007-12-24 22:49:30 +00:00
|
|
|
if (ev->ev_flags & EVLIST_ACTIVE)
|
|
|
|
return (-1);
|
|
|
|
if (pri < 0 || pri >= ev->ev_base->nactivequeues)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
ev->ev_pri = pri;
|
|
|
|
|
|
|
|
return (0);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks if a specific event is pending or scheduled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
event_pending(struct event *ev, short event, struct timeval *tv)
|
|
|
|
{
|
2006-03-28 04:33:41 +00:00
|
|
|
struct timeval now, res;
|
2002-04-09 15:14:06 +00:00
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (ev->ev_flags & EVLIST_INSERTED)
|
2008-07-11 15:49:04 +00:00
|
|
|
flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL));
|
2007-12-24 22:49:30 +00:00
|
|
|
if (ev->ev_flags & EVLIST_ACTIVE)
|
|
|
|
flags |= ev->ev_res;
|
2002-04-09 15:14:06 +00:00
|
|
|
if (ev->ev_flags & EVLIST_TIMEOUT)
|
|
|
|
flags |= EV_TIMEOUT;
|
|
|
|
|
2004-04-04 02:20:21 +00:00
|
|
|
event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_SIGNAL);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
|
|
|
/* See if there is a timeout that we should report */
|
2006-03-28 04:33:41 +00:00
|
|
|
if (tv != NULL && (flags & event & EV_TIMEOUT)) {
|
2008-05-03 18:23:44 +00:00
|
|
|
gettime(ev->ev_base, &now);
|
2007-11-07 06:01:57 +00:00
|
|
|
evutil_timersub(&ev->ev_timeout, &now, &res);
|
2006-03-28 16:57:19 +00:00
|
|
|
/* correctly remap to real time */
|
2008-05-08 14:06:33 +00:00
|
|
|
evutil_gettimeofday(&now, NULL);
|
2007-11-07 06:01:57 +00:00
|
|
|
evutil_timeradd(&now, &res, tv);
|
2006-03-28 04:33:41 +00:00
|
|
|
}
|
2002-04-09 15:14:06 +00:00
|
|
|
|
|
|
|
return (flags & event);
|
|
|
|
}
|
|
|
|
|
2008-05-05 15:45:30 +00:00
|
|
|
int
|
|
|
|
_event_initialized(struct event *ev, int need_fd)
|
|
|
|
{
|
|
|
|
if (!(ev->ev_flags & EVLIST_INIT))
|
|
|
|
return 0;
|
|
|
|
#ifdef WIN32
|
|
|
|
/* XXX Is this actually a sensible thing to check? -NM */
|
2008-05-12 01:03:36 +00:00
|
|
|
if (need_fd && (ev)->ev_fd == (evutil_socket_t)INVALID_HANDLE_VALUE)
|
2008-05-05 15:45:30 +00:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
evutil_socket_t
|
|
|
|
event_get_fd(struct event *ev)
|
|
|
|
{
|
|
|
|
return ev->ev_fd;
|
|
|
|
}
|
|
|
|
|
2009-05-05 01:09:03 +00:00
|
|
|
struct event_base *
|
|
|
|
event_get_base(struct event *ev)
|
|
|
|
{
|
|
|
|
return ev->ev_base;
|
|
|
|
}
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
int
|
2008-05-17 02:14:17 +00:00
|
|
|
event_add(struct event *ev, const struct timeval *tv)
|
2008-03-02 21:18:33 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2009-02-12 22:19:54 +00:00
|
|
|
EVBASE_ACQUIRE_LOCK(ev->ev_base, EVTHREAD_WRITE, th_base_lock);
|
2008-05-05 15:45:30 +00:00
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
res = event_add_internal(ev, tv);
|
|
|
|
|
2009-02-12 22:19:54 +00:00
|
|
|
EVBASE_RELEASE_LOCK(ev->ev_base, EVTHREAD_WRITE, th_base_lock);
|
2008-03-02 21:18:33 +00:00
|
|
|
|
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
|
2009-01-19 01:34:14 +00:00
|
|
|
static int
|
2009-01-19 20:37:24 +00:00
|
|
|
evthread_notify_base_default(struct event_base *base)
|
2009-01-19 01:34:14 +00:00
|
|
|
{
|
|
|
|
char buf[1];
|
|
|
|
int r;
|
2009-01-19 20:22:47 +00:00
|
|
|
buf[0] = (char) 0;
|
2009-01-19 19:46:03 +00:00
|
|
|
#ifdef WIN32
|
2009-01-19 01:34:14 +00:00
|
|
|
r = send(base->th_notify_fd[1], buf, 1, 0);
|
2009-01-19 19:46:03 +00:00
|
|
|
#else
|
|
|
|
r = write(base->th_notify_fd[1], buf, 1);
|
|
|
|
#endif
|
2009-01-19 01:34:14 +00:00
|
|
|
return (r < 0) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2009-01-27 22:30:46 +00:00
|
|
|
#if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H)
|
2009-01-19 20:37:24 +00:00
|
|
|
static int
|
|
|
|
evthread_notify_base_eventfd(struct event_base *base)
|
|
|
|
{
|
|
|
|
ev_uint64_t msg = 1;
|
|
|
|
int r;
|
|
|
|
do {
|
|
|
|
r = write(base->th_notify_fd[0], (void*) &msg, sizeof(msg));
|
|
|
|
} while (r < 0 && errno == EAGAIN);
|
|
|
|
|
|
|
|
return (r < 0) ? -1 : 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
evthread_notify_base(struct event_base *base)
|
|
|
|
{
|
|
|
|
if (!base->th_notify_fn)
|
|
|
|
return -1;
|
|
|
|
return base->th_notify_fn(base);
|
|
|
|
}
|
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
static inline int
|
2008-05-17 02:14:17 +00:00
|
|
|
event_add_internal(struct event *ev, const struct timeval *tv)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2004-11-25 09:50:18 +00:00
|
|
|
struct event_base *base = ev->ev_base;
|
2008-03-02 21:18:33 +00:00
|
|
|
int res = 0;
|
2009-10-02 03:03:58 +00:00
|
|
|
int notify = 0;
|
2004-11-25 09:50:18 +00:00
|
|
|
|
2005-03-29 07:03:10 +00:00
|
|
|
event_debug((
|
2002-04-09 15:14:06 +00:00
|
|
|
"event_add: event: %p, %s%s%scall %p",
|
|
|
|
ev,
|
|
|
|
ev->ev_events & EV_READ ? "EV_READ " : " ",
|
|
|
|
ev->ev_events & EV_WRITE ? "EV_WRITE " : " ",
|
|
|
|
tv ? "EV_TIMEOUT " : " ",
|
|
|
|
ev->ev_callback));
|
|
|
|
|
|
|
|
assert(!(ev->ev_flags & ~EVLIST_ALL));
|
|
|
|
|
2008-07-25 01:29:54 +00:00
|
|
|
/*
|
|
|
|
* prepare for timeout insertion further below, if we get a
|
|
|
|
* failure on any step, we should not change any state.
|
|
|
|
*/
|
|
|
|
if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) {
|
|
|
|
if (min_heap_reserve(&base->timeheap,
|
|
|
|
1 + min_heap_size(&base->timeheap)) == -1)
|
|
|
|
return (-1); /* ENOMEM == errno */
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL)) &&
|
|
|
|
!(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE))) {
|
2008-12-23 16:37:01 +00:00
|
|
|
if (ev->ev_events & (EV_READ|EV_WRITE))
|
|
|
|
res = evmap_io_add(base, ev->ev_fd, ev);
|
|
|
|
else if (ev->ev_events & EV_SIGNAL)
|
|
|
|
res = evmap_signal_add(base, ev->ev_fd, ev);
|
2008-07-25 01:29:54 +00:00
|
|
|
if (res != -1)
|
|
|
|
event_queue_insert(base, ev, EVLIST_INSERTED);
|
2009-10-02 03:03:58 +00:00
|
|
|
if (res == 1) {
|
|
|
|
/* evmap says we need to notify the main thread. */
|
|
|
|
notify = 1;
|
|
|
|
res = 0;
|
|
|
|
}
|
2008-07-25 01:29:54 +00:00
|
|
|
}
|
|
|
|
|
2009-01-27 21:10:31 +00:00
|
|
|
/*
|
2009-10-16 13:19:57 +00:00
|
|
|
* we should change the timeout state only if the previous event
|
2008-07-25 01:29:54 +00:00
|
|
|
* addition succeeded.
|
|
|
|
*/
|
|
|
|
if (res != -1 && tv != NULL) {
|
2002-04-09 15:14:06 +00:00
|
|
|
struct timeval now;
|
|
|
|
|
2009-01-22 02:33:38 +00:00
|
|
|
/*
|
|
|
|
* for persistent timeout events, we remember the
|
|
|
|
* timeout value and re-add the event.
|
|
|
|
*/
|
2009-05-15 18:44:44 +00:00
|
|
|
if (ev->ev_closure == EV_CLOSURE_PERSIST)
|
2009-01-22 02:33:38 +00:00
|
|
|
ev->ev_io_timeout = *tv;
|
|
|
|
|
2009-01-27 21:10:31 +00:00
|
|
|
/*
|
2008-07-25 01:29:54 +00:00
|
|
|
* we already reserved memory above for the case where we
|
2009-10-16 13:19:57 +00:00
|
|
|
* are not replacing an existing timeout.
|
2008-07-25 01:29:54 +00:00
|
|
|
*/
|
2009-10-02 03:03:58 +00:00
|
|
|
if (ev->ev_flags & EVLIST_TIMEOUT) {
|
|
|
|
if (min_heap_elt_is_top(ev))
|
|
|
|
notify = 1;
|
2004-11-25 09:50:18 +00:00
|
|
|
event_queue_remove(base, ev, EVLIST_TIMEOUT);
|
2009-10-02 03:03:58 +00:00
|
|
|
}
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2003-04-10 19:14:03 +00:00
|
|
|
/* Check if it is active due to a timeout. Rescheduling
|
|
|
|
* this timeout before the callback can be executed
|
|
|
|
* removes it from the active list. */
|
2007-12-24 22:49:30 +00:00
|
|
|
if ((ev->ev_flags & EVLIST_ACTIVE) &&
|
|
|
|
(ev->ev_res & EV_TIMEOUT)) {
|
2008-07-11 15:49:04 +00:00
|
|
|
if (ev->ev_events & EV_SIGNAL) {
|
2008-05-03 21:37:33 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
2003-04-10 19:14:03 +00:00
|
|
|
}
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2007-12-24 22:49:30 +00:00
|
|
|
event_queue_remove(base, ev, EVLIST_ACTIVE);
|
2003-04-10 19:14:03 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:23:44 +00:00
|
|
|
gettime(base, &now);
|
2007-11-07 06:01:57 +00:00
|
|
|
evutil_timeradd(&now, tv, &ev->ev_timeout);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2005-03-29 07:03:10 +00:00
|
|
|
event_debug((
|
2002-04-09 15:14:06 +00:00
|
|
|
"event_add: timeout in %d seconds, call %p",
|
2007-12-06 19:20:24 +00:00
|
|
|
(int)tv->tv_sec, ev->ev_callback));
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
event_queue_insert(base, ev, EVLIST_TIMEOUT);
|
2009-10-02 03:03:58 +00:00
|
|
|
if (min_heap_elt_is_top(ev)) {
|
|
|
|
/* The earliest timeout is now earlier than it was
|
|
|
|
* before: we will need to tell the main thread to
|
|
|
|
* wake up earlier than it would otherwise. */
|
|
|
|
notify = 1;
|
|
|
|
}
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
/* if we are not in the right thread, we need to wake up the loop */
|
2009-10-02 03:03:58 +00:00
|
|
|
if (res != -1 && notify && !EVBASE_IN_THREAD(base))
|
2009-01-19 20:22:47 +00:00
|
|
|
evthread_notify_base(base);
|
2008-03-02 21:18:33 +00:00
|
|
|
|
|
|
|
return (res);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
event_del(struct event *ev)
|
2008-03-02 21:18:33 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2009-02-12 22:19:54 +00:00
|
|
|
EVBASE_ACQUIRE_LOCK(ev->ev_base, EVTHREAD_WRITE, th_base_lock);
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
res = event_del_internal(ev);
|
|
|
|
|
2009-02-12 22:19:54 +00:00
|
|
|
EVBASE_RELEASE_LOCK(ev->ev_base, EVTHREAD_WRITE, th_base_lock);
|
2008-03-02 21:18:33 +00:00
|
|
|
|
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
|
2009-07-14 16:54:48 +00:00
|
|
|
/* Helper for event_del: always called with th_base_lock held. */
|
2008-03-02 21:18:33 +00:00
|
|
|
static inline int
|
|
|
|
event_del_internal(struct event *ev)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2004-12-14 03:36:12 +00:00
|
|
|
struct event_base *base;
|
2009-10-02 03:03:58 +00:00
|
|
|
int res = 0, notify = 0;
|
2009-07-14 16:54:48 +00:00
|
|
|
int need_cur_lock;
|
2004-11-25 09:50:18 +00:00
|
|
|
|
2005-03-29 07:03:10 +00:00
|
|
|
event_debug(("event_del: %p, callback %p",
|
2002-04-09 15:14:06 +00:00
|
|
|
ev, ev->ev_callback));
|
|
|
|
|
2004-12-14 03:36:12 +00:00
|
|
|
/* An event without a base has not been added */
|
|
|
|
if (ev->ev_base == NULL)
|
2005-02-22 15:47:53 +00:00
|
|
|
return (-1);
|
2004-12-14 03:36:12 +00:00
|
|
|
|
2009-07-14 16:54:48 +00:00
|
|
|
/* If the main thread is currently executing this event's callback,
|
|
|
|
* and we are not the main thread, then we want to wait until the
|
|
|
|
* callback is done before we start removing the event. That way,
|
|
|
|
* when this function returns, it will be safe to free the
|
|
|
|
* user-supplied argument. */
|
2004-12-14 03:36:12 +00:00
|
|
|
base = ev->ev_base;
|
2009-07-14 16:54:48 +00:00
|
|
|
need_cur_lock = (base->current_event == ev);
|
|
|
|
if (need_cur_lock)
|
|
|
|
EVBASE_ACQUIRE_LOCK(base, EVTHREAD_WRITE, current_event_lock);
|
2004-12-14 03:36:12 +00:00
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
assert(!(ev->ev_flags & ~EVLIST_ALL));
|
|
|
|
|
2002-04-10 03:15:19 +00:00
|
|
|
/* See if we are just active executing this event in a loop */
|
2008-07-11 15:49:04 +00:00
|
|
|
if (ev->ev_events & EV_SIGNAL) {
|
2008-05-03 21:37:33 +00:00
|
|
|
if (ev->ev_ncalls && ev->ev_pncalls) {
|
|
|
|
/* Abort loop */
|
|
|
|
*ev->ev_pncalls = 0;
|
|
|
|
}
|
2002-04-10 03:15:19 +00:00
|
|
|
}
|
|
|
|
|
2009-10-02 03:03:58 +00:00
|
|
|
if (ev->ev_flags & EVLIST_TIMEOUT) {
|
|
|
|
/* NOTE: We never need to notify the main thread because of a
|
|
|
|
* deleted timeout event: all that could happen if we don't is
|
|
|
|
* that the dispatch loop might wake up too early. But the
|
|
|
|
* point of notifying the main thread _is_ to wake up the
|
|
|
|
* dispatch loop early anyway, so we wouldn't gain anything by
|
|
|
|
* doing it.
|
|
|
|
*/
|
2004-11-25 09:50:18 +00:00
|
|
|
event_queue_remove(base, ev, EVLIST_TIMEOUT);
|
2009-10-02 03:03:58 +00:00
|
|
|
}
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-12-24 22:49:30 +00:00
|
|
|
if (ev->ev_flags & EVLIST_ACTIVE)
|
|
|
|
event_queue_remove(base, ev, EVLIST_ACTIVE);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
|
|
|
if (ev->ev_flags & EVLIST_INSERTED) {
|
2004-11-25 09:50:18 +00:00
|
|
|
event_queue_remove(base, ev, EVLIST_INSERTED);
|
2008-12-23 16:37:01 +00:00
|
|
|
if (ev->ev_events & (EV_READ|EV_WRITE))
|
|
|
|
res = evmap_io_del(base, ev->ev_fd, ev);
|
|
|
|
else
|
|
|
|
res = evmap_signal_del(base, ev->ev_fd, ev);
|
2009-10-02 03:03:58 +00:00
|
|
|
if (res == 1) {
|
|
|
|
/* evmap says we need to notify the main thread. */
|
|
|
|
notify = 1;
|
|
|
|
res = 0;
|
|
|
|
}
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
/* if we are not in the right thread, we need to wake up the loop */
|
2009-10-02 03:03:58 +00:00
|
|
|
if (res != -1 && notify && !EVBASE_IN_THREAD(base))
|
2009-01-19 20:22:47 +00:00
|
|
|
evthread_notify_base(base);
|
2009-01-19 01:34:14 +00:00
|
|
|
|
2009-07-14 16:54:48 +00:00
|
|
|
if (need_cur_lock)
|
|
|
|
EVBASE_RELEASE_LOCK(base, EVTHREAD_WRITE, current_event_lock);
|
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
return (res);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-04-10 02:10:47 +00:00
|
|
|
event_active(struct event *ev, int res, short ncalls)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2009-02-12 22:19:54 +00:00
|
|
|
EVBASE_ACQUIRE_LOCK(ev->ev_base, EVTHREAD_WRITE, th_base_lock);
|
2008-03-02 21:18:33 +00:00
|
|
|
|
|
|
|
event_active_internal(ev, res, ncalls);
|
2009-01-19 01:34:14 +00:00
|
|
|
|
2009-02-12 22:19:54 +00:00
|
|
|
EVBASE_RELEASE_LOCK(ev->ev_base, EVTHREAD_WRITE, th_base_lock);
|
2008-03-02 21:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
event_active_internal(struct event *ev, int res, short ncalls)
|
|
|
|
{
|
|
|
|
struct event_base *base;
|
|
|
|
|
2007-12-24 22:49:30 +00:00
|
|
|
/* We get different kinds of events, add them together */
|
|
|
|
if (ev->ev_flags & EVLIST_ACTIVE) {
|
|
|
|
ev->ev_res |= res;
|
2002-06-11 18:38:37 +00:00
|
|
|
return;
|
2007-12-24 22:49:30 +00:00
|
|
|
}
|
2002-06-11 18:38:37 +00:00
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
base = ev->ev_base;
|
|
|
|
|
2007-12-24 22:49:30 +00:00
|
|
|
ev->ev_res = res;
|
2008-05-03 21:37:33 +00:00
|
|
|
|
2008-07-11 15:49:04 +00:00
|
|
|
if (ev->ev_events & EV_SIGNAL) {
|
2008-05-03 21:37:33 +00:00
|
|
|
ev->ev_ncalls = ncalls;
|
|
|
|
ev->ev_pncalls = NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
event_queue_insert(base, ev, EVLIST_ACTIVE);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 14:22:33 +00:00
|
|
|
void
|
|
|
|
event_deferred_cb_init(struct deferred_cb *cb, deferred_cb_fn fn, void *arg)
|
|
|
|
{
|
|
|
|
memset(cb, 0, sizeof(struct deferred_cb));
|
|
|
|
cb->cb = fn;
|
|
|
|
cb->arg = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-26 01:29:39 +00:00
|
|
|
event_deferred_cb_cancel(struct deferred_cb_queue *queue,
|
|
|
|
struct deferred_cb *cb)
|
2009-04-10 14:22:33 +00:00
|
|
|
{
|
2009-07-26 01:29:39 +00:00
|
|
|
if (!queue) {
|
|
|
|
if (current_base)
|
|
|
|
queue = ¤t_base->defer_queue;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2009-04-10 14:22:33 +00:00
|
|
|
|
2009-07-26 01:29:39 +00:00
|
|
|
LOCK_DEFERRED_QUEUE(queue);
|
2009-04-10 14:22:33 +00:00
|
|
|
if (cb->queued) {
|
2009-07-26 01:29:39 +00:00
|
|
|
TAILQ_REMOVE(&queue->deferred_cb_list, cb, cb_next);
|
|
|
|
--queue->active_count;
|
2009-04-10 14:22:33 +00:00
|
|
|
cb->queued = 0;
|
|
|
|
}
|
2009-07-26 01:29:39 +00:00
|
|
|
UNLOCK_DEFERRED_QUEUE(queue);
|
2009-04-10 14:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-26 01:29:39 +00:00
|
|
|
event_deferred_cb_schedule(struct deferred_cb_queue *queue,
|
|
|
|
struct deferred_cb *cb)
|
2009-04-10 14:22:33 +00:00
|
|
|
{
|
2009-07-26 01:29:39 +00:00
|
|
|
if (!queue) {
|
|
|
|
if (current_base)
|
|
|
|
queue = ¤t_base->defer_queue;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCK_DEFERRED_QUEUE(queue);
|
2009-04-10 14:58:15 +00:00
|
|
|
if (!cb->queued) {
|
|
|
|
cb->queued = 1;
|
2009-07-26 01:29:39 +00:00
|
|
|
TAILQ_INSERT_TAIL(&queue->deferred_cb_list, cb, cb_next);
|
|
|
|
++queue->active_count;
|
|
|
|
if (queue->notify_fn)
|
|
|
|
queue->notify_fn(queue, queue->notify_arg);
|
2009-04-10 14:58:15 +00:00
|
|
|
}
|
2009-07-26 01:29:39 +00:00
|
|
|
UNLOCK_DEFERRED_QUEUE(queue);
|
2009-04-10 14:22:33 +00:00
|
|
|
}
|
|
|
|
|
2007-07-30 22:41:00 +00:00
|
|
|
static int
|
|
|
|
timeout_next(struct event_base *base, struct timeval **tv_p)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2009-10-21 03:54:00 +00:00
|
|
|
/* Caller must hold th_base_lock */
|
2002-04-09 15:14:06 +00:00
|
|
|
struct timeval now;
|
|
|
|
struct event *ev;
|
2007-07-30 22:41:00 +00:00
|
|
|
struct timeval *tv = *tv_p;
|
2008-03-02 21:18:33 +00:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
ev = min_heap_top(&base->timeheap);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
if (ev == NULL) {
|
2007-07-30 22:41:00 +00:00
|
|
|
/* if no time-based events are active wait for I/O */
|
|
|
|
*tv_p = NULL;
|
2008-03-02 21:18:33 +00:00
|
|
|
goto out;
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:23:44 +00:00
|
|
|
if (gettime(base, &now) == -1) {
|
2008-03-02 21:18:33 +00:00
|
|
|
res = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-11-07 06:01:57 +00:00
|
|
|
if (evutil_timercmp(&ev->ev_timeout, &now, <=)) {
|
|
|
|
evutil_timerclear(tv);
|
2008-03-02 21:18:33 +00:00
|
|
|
goto out;
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2007-11-07 06:01:57 +00:00
|
|
|
evutil_timersub(&ev->ev_timeout, &now, tv);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2004-02-22 21:17:23 +00:00
|
|
|
assert(tv->tv_sec >= 0);
|
|
|
|
assert(tv->tv_usec >= 0);
|
2007-12-06 19:20:24 +00:00
|
|
|
event_debug(("timeout_next: in %d seconds", (int)tv->tv_sec));
|
2008-03-02 21:18:33 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
return (res);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2007-07-30 22:41:00 +00:00
|
|
|
/*
|
|
|
|
* Determines if the time is running backwards by comparing the current
|
|
|
|
* time against the last time we checked. Not needed when using clock
|
|
|
|
* monotonic.
|
|
|
|
*/
|
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
static void
|
2007-07-30 22:41:00 +00:00
|
|
|
timeout_correct(struct event_base *base, struct timeval *tv)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2009-10-21 03:54:00 +00:00
|
|
|
/* Caller must hold th_base_lock. */
|
2007-11-03 18:04:53 +00:00
|
|
|
struct event **pev;
|
|
|
|
unsigned int size;
|
2007-07-30 22:41:00 +00:00
|
|
|
struct timeval off;
|
|
|
|
|
|
|
|
if (use_monotonic)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Check if time is running backwards */
|
2008-05-03 18:23:44 +00:00
|
|
|
gettime(base, tv);
|
2008-03-02 21:18:33 +00:00
|
|
|
|
2007-11-07 06:01:57 +00:00
|
|
|
if (evutil_timercmp(tv, &base->event_tv, >=)) {
|
2007-07-30 22:41:00 +00:00
|
|
|
base->event_tv = *tv;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_debug(("%s: time is running backwards, corrected",
|
|
|
|
__func__));
|
2007-11-07 06:01:57 +00:00
|
|
|
evutil_timersub(&base->event_tv, tv, &off);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2004-06-11 04:39:11 +00:00
|
|
|
/*
|
|
|
|
* We can modify the key element of the node without destroying
|
2009-10-16 13:19:57 +00:00
|
|
|
* the key, because we apply it to all in the right order.
|
2002-04-09 15:14:06 +00:00
|
|
|
*/
|
2007-11-03 18:04:53 +00:00
|
|
|
pev = base->timeheap.p;
|
|
|
|
size = base->timeheap.n;
|
|
|
|
for (; size-- > 0; ++pev) {
|
2007-11-07 07:33:36 +00:00
|
|
|
struct timeval *ev_tv = &(**pev).ev_timeout;
|
|
|
|
evutil_timersub(ev_tv, &off, ev_tv);
|
2007-11-03 18:04:53 +00:00
|
|
|
}
|
2009-07-17 18:59:22 +00:00
|
|
|
/* Now remember what the new time turned out to be. */
|
|
|
|
base->event_tv = *tv;
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2009-04-08 16:57:38 +00:00
|
|
|
static void
|
2004-11-25 09:50:18 +00:00
|
|
|
timeout_process(struct event_base *base)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2009-10-21 03:54:00 +00:00
|
|
|
/* Caller must hold lock. */
|
2002-04-09 15:14:06 +00:00
|
|
|
struct timeval now;
|
2007-11-03 18:04:53 +00:00
|
|
|
struct event *ev;
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
if (min_heap_empty(&base->timeheap)) {
|
2007-09-20 18:27:01 +00:00
|
|
|
return;
|
2008-03-02 21:18:33 +00:00
|
|
|
}
|
2007-09-20 18:27:01 +00:00
|
|
|
|
2008-05-03 18:23:44 +00:00
|
|
|
gettime(base, &now);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-11-03 18:04:53 +00:00
|
|
|
while ((ev = min_heap_top(&base->timeheap))) {
|
2007-11-07 06:01:57 +00:00
|
|
|
if (evutil_timercmp(&ev->ev_timeout, &now, >))
|
2002-04-09 15:14:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* delete this event from the I/O queues */
|
2008-03-02 21:18:33 +00:00
|
|
|
event_del_internal(ev);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2005-03-29 07:03:10 +00:00
|
|
|
event_debug(("timeout_process: call %p",
|
2002-04-09 15:14:06 +00:00
|
|
|
ev->ev_callback));
|
2008-03-02 21:18:33 +00:00
|
|
|
event_active_internal(ev, EV_TIMEOUT, 1);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-21 19:20:25 +00:00
|
|
|
static void
|
2004-11-25 09:50:18 +00:00
|
|
|
event_queue_remove(struct event_base *base, struct event *ev, int queue)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
|
|
|
if (!(ev->ev_flags & queue))
|
2005-03-29 07:03:10 +00:00
|
|
|
event_errx(1, "%s: %p(fd %d) not on queue %x", __func__,
|
|
|
|
ev, ev->ev_fd, queue);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-11-12 02:31:07 +00:00
|
|
|
if (~ev->ev_flags & EVLIST_INTERNAL)
|
2004-11-25 09:50:18 +00:00
|
|
|
base->event_count--;
|
2004-05-24 00:19:52 +00:00
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
ev->ev_flags &= ~queue;
|
|
|
|
switch (queue) {
|
2008-05-03 18:23:44 +00:00
|
|
|
case EVLIST_INSERTED:
|
|
|
|
TAILQ_REMOVE(&base->eventqueue, ev, ev_next);
|
|
|
|
break;
|
2007-12-24 22:49:30 +00:00
|
|
|
case EVLIST_ACTIVE:
|
|
|
|
base->event_count_active--;
|
|
|
|
TAILQ_REMOVE(base->activequeues[ev->ev_pri],
|
|
|
|
ev, ev_active_next);
|
|
|
|
break;
|
2002-04-09 15:14:06 +00:00
|
|
|
case EVLIST_TIMEOUT:
|
2007-11-03 18:04:53 +00:00
|
|
|
min_heap_erase(&base->timeheap, ev);
|
2002-04-09 15:14:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
2005-03-29 07:03:10 +00:00
|
|
|
event_errx(1, "%s: unknown queue %x", __func__, queue);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-21 19:20:25 +00:00
|
|
|
static void
|
2004-11-25 09:50:18 +00:00
|
|
|
event_queue_insert(struct event_base *base, struct event *ev, int queue)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2007-12-24 22:49:30 +00:00
|
|
|
if (ev->ev_flags & queue) {
|
|
|
|
/* Double insertion is possible for active events */
|
|
|
|
if (queue & EVLIST_ACTIVE)
|
|
|
|
return;
|
|
|
|
|
2005-03-29 07:03:10 +00:00
|
|
|
event_errx(1, "%s: %p(fd %d) already on queue %x", __func__,
|
|
|
|
ev, ev->ev_fd, queue);
|
2007-12-24 22:49:30 +00:00
|
|
|
}
|
2004-09-19 21:08:09 +00:00
|
|
|
|
2007-11-12 02:31:07 +00:00
|
|
|
if (~ev->ev_flags & EVLIST_INTERNAL)
|
2004-11-25 09:50:18 +00:00
|
|
|
base->event_count++;
|
2004-05-24 00:19:52 +00:00
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
ev->ev_flags |= queue;
|
|
|
|
switch (queue) {
|
2008-05-03 18:23:44 +00:00
|
|
|
case EVLIST_INSERTED:
|
|
|
|
TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next);
|
|
|
|
break;
|
2007-12-24 22:49:30 +00:00
|
|
|
case EVLIST_ACTIVE:
|
|
|
|
base->event_count_active++;
|
|
|
|
TAILQ_INSERT_TAIL(base->activequeues[ev->ev_pri],
|
|
|
|
ev,ev_active_next);
|
|
|
|
break;
|
2004-03-27 17:42:49 +00:00
|
|
|
case EVLIST_TIMEOUT: {
|
2007-11-03 18:04:53 +00:00
|
|
|
min_heap_push(&base->timeheap, ev);
|
2002-04-09 15:14:06 +00:00
|
|
|
break;
|
2004-03-27 17:42:49 +00:00
|
|
|
}
|
2002-04-09 15:14:06 +00:00
|
|
|
default:
|
2005-03-29 07:03:10 +00:00
|
|
|
event_errx(1, "%s: unknown queue %x", __func__, queue);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-25 05:28:57 +00:00
|
|
|
|
|
|
|
/* Functions for debugging */
|
|
|
|
|
|
|
|
const char *
|
|
|
|
event_get_version(void)
|
|
|
|
{
|
2009-01-27 22:30:46 +00:00
|
|
|
return (_EVENT_VERSION);
|
2005-02-25 05:28:57 +00:00
|
|
|
}
|
|
|
|
|
2009-04-17 17:22:32 +00:00
|
|
|
ev_uint32_t
|
|
|
|
event_get_version_number(void)
|
|
|
|
{
|
|
|
|
return (_EVENT_NUMERIC_VERSION);
|
|
|
|
}
|
|
|
|
|
2009-01-27 21:10:31 +00:00
|
|
|
/*
|
2005-02-25 05:28:57 +00:00
|
|
|
* No thread-safe interface needed - the information should be the same
|
|
|
|
* for all threads.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char *
|
|
|
|
event_get_method(void)
|
|
|
|
{
|
|
|
|
return (current_base->evsel->name);
|
|
|
|
}
|
2007-11-25 17:14:19 +00:00
|
|
|
|
2009-04-17 06:56:57 +00:00
|
|
|
#ifndef _EVENT_DISABLE_MM_REPLACEMENT
|
2008-04-25 01:18:08 +00:00
|
|
|
static void *(*_mm_malloc_fn)(size_t sz) = NULL;
|
|
|
|
static void *(*_mm_realloc_fn)(void *p, size_t sz) = NULL;
|
|
|
|
static void (*_mm_free_fn)(void *p) = NULL;
|
2007-11-25 17:14:19 +00:00
|
|
|
|
|
|
|
void *
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_malloc(size_t sz)
|
2007-11-25 17:14:19 +00:00
|
|
|
{
|
2008-04-25 01:18:08 +00:00
|
|
|
if (_mm_malloc_fn)
|
|
|
|
return _mm_malloc_fn(sz);
|
2007-11-25 17:14:19 +00:00
|
|
|
else
|
|
|
|
return malloc(sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_calloc(size_t count, size_t size)
|
2007-11-25 17:14:19 +00:00
|
|
|
{
|
2008-04-25 01:18:08 +00:00
|
|
|
if (_mm_malloc_fn) {
|
2007-11-25 17:14:19 +00:00
|
|
|
size_t sz = count * size;
|
2008-04-25 01:18:08 +00:00
|
|
|
void *p = _mm_malloc_fn(sz);
|
2007-11-25 17:14:19 +00:00
|
|
|
if (p)
|
|
|
|
memset(p, 0, sz);
|
|
|
|
return p;
|
|
|
|
} else
|
|
|
|
return calloc(count, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_strdup(const char *str)
|
2007-11-25 17:14:19 +00:00
|
|
|
{
|
2008-04-25 01:18:08 +00:00
|
|
|
if (_mm_malloc_fn) {
|
2007-11-25 17:14:19 +00:00
|
|
|
size_t ln = strlen(str);
|
2008-04-25 01:18:08 +00:00
|
|
|
void *p = _mm_malloc_fn(ln+1);
|
2007-11-25 17:14:19 +00:00
|
|
|
if (p)
|
|
|
|
memcpy(p, str, ln+1);
|
|
|
|
return p;
|
|
|
|
} else
|
|
|
|
#ifdef WIN32
|
|
|
|
return _strdup(str);
|
|
|
|
#else
|
|
|
|
return strdup(str);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_realloc(void *ptr, size_t sz)
|
2007-11-25 17:14:19 +00:00
|
|
|
{
|
2008-04-25 01:18:08 +00:00
|
|
|
if (_mm_realloc_fn)
|
|
|
|
return _mm_realloc_fn(ptr, sz);
|
2007-11-25 17:14:19 +00:00
|
|
|
else
|
|
|
|
return realloc(ptr, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(void *ptr)
|
2007-11-25 17:14:19 +00:00
|
|
|
{
|
2009-04-13 18:32:24 +00:00
|
|
|
if (_mm_free_fn)
|
2008-04-25 01:18:08 +00:00
|
|
|
_mm_free_fn(ptr);
|
2007-11-25 17:14:19 +00:00
|
|
|
else
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
event_set_mem_functions(void *(*malloc_fn)(size_t sz),
|
|
|
|
void *(*realloc_fn)(void *ptr, size_t sz),
|
|
|
|
void (*free_fn)(void *ptr))
|
|
|
|
{
|
2008-04-25 01:18:08 +00:00
|
|
|
_mm_malloc_fn = malloc_fn;
|
|
|
|
_mm_realloc_fn = realloc_fn;
|
|
|
|
_mm_free_fn = free_fn;
|
2007-11-25 17:14:19 +00:00
|
|
|
}
|
2009-04-17 06:56:57 +00:00
|
|
|
#endif
|
2009-02-12 22:19:54 +00:00
|
|
|
|
2009-07-17 21:47:45 +00:00
|
|
|
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
|
2008-03-02 21:18:33 +00:00
|
|
|
/* support for threading */
|
2009-02-12 22:19:54 +00:00
|
|
|
void (*_evthread_locking_fn)(int mode, void *lock) = NULL;
|
|
|
|
unsigned long (*_evthread_id_fn)(void) = NULL;
|
|
|
|
void *(*_evthread_lock_alloc_fn)(void) = NULL;
|
|
|
|
void (*_evthread_lock_free_fn)(void *) = NULL;
|
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
void
|
2009-02-12 22:19:54 +00:00
|
|
|
evthread_set_locking_callback(void (*locking_fn)(int mode, void *lock))
|
2008-03-02 21:18:33 +00:00
|
|
|
{
|
2009-02-12 22:19:54 +00:00
|
|
|
_evthread_locking_fn = locking_fn;
|
2008-03-02 21:18:33 +00:00
|
|
|
}
|
2009-07-17 21:47:45 +00:00
|
|
|
#endif
|
2008-03-02 21:18:33 +00:00
|
|
|
|
2009-01-27 22:30:46 +00:00
|
|
|
#if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H)
|
2008-03-02 21:18:33 +00:00
|
|
|
static void
|
2009-01-19 20:37:24 +00:00
|
|
|
evthread_notify_drain_eventfd(int fd, short what, void *arg)
|
2008-03-02 21:18:33 +00:00
|
|
|
{
|
2009-01-19 20:37:24 +00:00
|
|
|
ev_uint64_t msg;
|
|
|
|
|
|
|
|
read(fd, (void*) &msg, sizeof(msg));
|
|
|
|
}
|
|
|
|
#endif
|
2009-01-19 01:34:14 +00:00
|
|
|
|
2009-01-19 20:37:24 +00:00
|
|
|
static void
|
|
|
|
evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg)
|
|
|
|
{
|
|
|
|
unsigned char buf[128];
|
2009-01-19 19:46:03 +00:00
|
|
|
#ifdef WIN32
|
2009-01-19 20:22:47 +00:00
|
|
|
while (recv(fd, (char*)buf, sizeof(buf), 0) > 0)
|
|
|
|
;
|
2009-01-19 19:46:03 +00:00
|
|
|
#else
|
2009-01-19 20:22:47 +00:00
|
|
|
while (read(fd, (char*)buf, sizeof(buf)) > 0)
|
|
|
|
;
|
2009-01-19 19:46:03 +00:00
|
|
|
#endif
|
2008-03-02 21:18:33 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 21:47:45 +00:00
|
|
|
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
|
2008-03-02 21:18:33 +00:00
|
|
|
void
|
2009-02-12 22:19:54 +00:00
|
|
|
evthread_set_id_callback(unsigned long (*id_fn)(void))
|
2008-03-02 21:18:33 +00:00
|
|
|
{
|
2009-02-12 22:19:54 +00:00
|
|
|
_evthread_id_fn = id_fn;
|
2009-01-19 01:34:14 +00:00
|
|
|
}
|
2009-07-17 21:47:45 +00:00
|
|
|
#endif
|
2009-01-19 01:34:14 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
evthread_make_base_notifiable(struct event_base *base)
|
|
|
|
{
|
2009-01-19 20:37:24 +00:00
|
|
|
void (*cb)(evutil_socket_t, short, void *) = evthread_notify_drain_default;
|
|
|
|
int (*notify)(struct event_base *) = evthread_notify_base_default;
|
|
|
|
|
2009-01-19 01:34:14 +00:00
|
|
|
if (!base)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (base->th_notify_fd[0] >= 0)
|
|
|
|
return 0;
|
|
|
|
|
2009-01-27 22:30:46 +00:00
|
|
|
#if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H)
|
2009-01-19 20:37:24 +00:00
|
|
|
base->th_notify_fd[0] = eventfd(0, 0);
|
|
|
|
if (base->th_notify_fd[0] >= 0) {
|
|
|
|
notify = evthread_notify_base_eventfd;
|
|
|
|
cb = evthread_notify_drain_eventfd;
|
|
|
|
} else
|
|
|
|
#endif
|
2009-01-19 19:46:03 +00:00
|
|
|
#if defined(_EVENT_HAVE_PIPE)
|
2009-01-19 20:37:24 +00:00
|
|
|
{
|
|
|
|
if ((base->evsel->features & EV_FEATURE_FDS)) {
|
|
|
|
if (pipe(base->th_notify_fd) < 0)
|
|
|
|
event_warn("%s: pipe", __func__);
|
|
|
|
}
|
2009-01-19 01:34:14 +00:00
|
|
|
}
|
|
|
|
if (base->th_notify_fd[0] < 0)
|
2009-02-12 22:19:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#define LOCAL_SOCKETPAIR_AF AF_INET
|
|
|
|
#else
|
|
|
|
#define LOCAL_SOCKETPAIR_AF AF_UNIX
|
2009-01-19 01:34:14 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0,
|
2009-02-12 22:19:54 +00:00
|
|
|
base->th_notify_fd) == -1) {
|
2009-01-19 01:34:14 +00:00
|
|
|
event_sock_warn(-1, "%s: socketpair", __func__);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
2008-03-02 21:18:33 +00:00
|
|
|
|
|
|
|
evutil_make_socket_nonblocking(base->th_notify_fd[0]);
|
2009-01-19 01:34:14 +00:00
|
|
|
|
2009-01-19 20:37:24 +00:00
|
|
|
base->th_notify_fn = notify;
|
|
|
|
|
2009-07-14 18:50:06 +00:00
|
|
|
/*
|
|
|
|
This can't be right, can it? We want writes to this socket to
|
|
|
|
just succeed.
|
|
|
|
evutil_make_socket_nonblocking(base->th_notify_fd[1]);
|
|
|
|
*/
|
2008-03-02 21:18:33 +00:00
|
|
|
|
|
|
|
/* prepare an event that we can use for wakeup */
|
2009-01-22 17:48:55 +00:00
|
|
|
event_assign(&base->th_notify, base, base->th_notify_fd[0],
|
|
|
|
EV_READ|EV_PERSIST, cb, base);
|
2008-05-02 16:28:25 +00:00
|
|
|
|
2008-03-02 21:18:33 +00:00
|
|
|
/* we need to mark this as internal event */
|
|
|
|
base->th_notify.ev_flags |= EVLIST_INTERNAL;
|
|
|
|
|
2009-07-17 18:38:14 +00:00
|
|
|
return event_add(&base->th_notify, NULL);
|
2008-03-02 21:18:33 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 21:47:45 +00:00
|
|
|
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
|
2008-03-10 03:17:20 +00:00
|
|
|
void
|
2009-02-12 22:19:54 +00:00
|
|
|
evthread_set_lock_create_callbacks(void *(*alloc_fn)(void),
|
|
|
|
void (*free_fn)(void *))
|
2008-03-02 21:18:33 +00:00
|
|
|
{
|
2009-02-12 22:19:54 +00:00
|
|
|
_evthread_lock_alloc_fn = alloc_fn;
|
|
|
|
_evthread_lock_free_fn = free_fn;
|
2008-03-02 21:18:33 +00:00
|
|
|
}
|
2009-07-17 21:47:45 +00:00
|
|
|
#endif
|
2008-05-15 03:49:03 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
event_base_dump_events(struct event_base *base, FILE *output)
|
|
|
|
{
|
|
|
|
struct event *e;
|
|
|
|
int i;
|
|
|
|
fprintf(output, "Inserted events:\n");
|
|
|
|
TAILQ_FOREACH(e, &base->eventqueue, ev_next) {
|
|
|
|
fprintf(output, " %p [fd %ld]%s%s%s%s%s\n",
|
|
|
|
(void*)e, (long)e->ev_fd,
|
|
|
|
(e->ev_events&EV_READ)?" Read":"",
|
|
|
|
(e->ev_events&EV_WRITE)?" Write":"",
|
|
|
|
(e->ev_events&EV_SIGNAL)?" Signal":"",
|
|
|
|
(e->ev_events&EV_TIMEOUT)?" Timeout":"",
|
|
|
|
(e->ev_events&EV_PERSIST)?" Persist":"");
|
|
|
|
|
|
|
|
}
|
|
|
|
for (i = 0; i < base->nactivequeues; ++i) {
|
|
|
|
if (TAILQ_EMPTY(base->activequeues[i]))
|
|
|
|
continue;
|
|
|
|
fprintf(output, "Active events [priority %d]:\n", i);
|
|
|
|
TAILQ_FOREACH(e, &base->eventqueue, ev_next) {
|
|
|
|
fprintf(output, " %p [fd %ld]%s%s%s%s\n",
|
|
|
|
(void*)e, (long)e->ev_fd,
|
|
|
|
(e->ev_res&EV_READ)?" Read active":"",
|
|
|
|
(e->ev_res&EV_WRITE)?" Write active":"",
|
|
|
|
(e->ev_res&EV_SIGNAL)?" Signal active":"",
|
|
|
|
(e->ev_res&EV_TIMEOUT)?" Timeout active":"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|