2004-11-25 09:50:18 +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
|
2004-11-25 09:50:18 +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.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
2009-07-17 18:38:38 +00:00
|
|
|
*
|
2004-11-25 09:50:18 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#ifndef _EVENT_INTERNAL_H_
|
|
|
|
#define _EVENT_INTERNAL_H_
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2009-01-27 22:30:46 +00:00
|
|
|
#include "event-config.h"
|
2009-05-05 16:52:37 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include "event2/event_struct.h"
|
2009-01-13 20:26:37 +00:00
|
|
|
#include "minheap-internal.h"
|
|
|
|
#include "evsignal-internal.h"
|
2007-11-25 17:14:19 +00:00
|
|
|
#include "mm-internal.h"
|
2009-07-26 01:29:39 +00:00
|
|
|
#include "defer-internal.h"
|
2007-03-10 06:37:53 +00:00
|
|
|
|
2008-05-03 21:37:33 +00:00
|
|
|
/* map union members back */
|
|
|
|
|
|
|
|
/* mutually exclusive */
|
|
|
|
#define ev_signal_next _ev.ev_signal.ev_signal_next
|
2009-01-22 02:33:38 +00:00
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
#define ev_io_next _ev.ev_io.ev_io_next
|
2009-01-22 02:33:38 +00:00
|
|
|
#define ev_io_timeout _ev.ev_io.ev_timeout
|
2008-05-03 21:37:33 +00:00
|
|
|
|
|
|
|
/* used only by signals */
|
|
|
|
#define ev_ncalls _ev.ev_signal.ev_ncalls
|
|
|
|
#define ev_pncalls _ev.ev_signal.ev_pncalls
|
|
|
|
|
2009-05-15 18:44:44 +00:00
|
|
|
/* Possible event closures. */
|
|
|
|
#define EV_CLOSURE_NONE 0
|
|
|
|
#define EV_CLOSURE_SIGNAL 1
|
|
|
|
#define EV_CLOSURE_PERSIST 2
|
|
|
|
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Structure to define the backend of a given event_base. */
|
2007-11-25 06:57:59 +00:00
|
|
|
struct eventop {
|
2009-04-17 06:55:08 +00:00
|
|
|
/** The name of this backend. */
|
2007-11-25 06:57:59 +00:00
|
|
|
const char *name;
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Set up an event_base to use this backend.*/
|
2007-11-25 06:57:59 +00:00
|
|
|
void *(*init)(struct event_base *);
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Enable reading/writing on a given fd. */
|
2009-01-14 20:52:32 +00:00
|
|
|
int (*add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Disable reading/writing on a given fd. */
|
2009-01-14 20:52:32 +00:00
|
|
|
int (*del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Function to implement the core of an event loop. It must see which
|
|
|
|
added events are ready, and cause event_active to be called for each
|
|
|
|
active event (usually via event_io_active or such).
|
|
|
|
*/
|
2008-12-23 16:37:01 +00:00
|
|
|
int (*dispatch)(struct event_base *, struct timeval *);
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Function to clean up and free our data from the event_base. */
|
2008-12-23 16:37:01 +00:00
|
|
|
void (*dealloc)(struct event_base *);
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Set if we need to reinitialize the event base after we fork. */
|
2007-11-25 06:57:59 +00:00
|
|
|
int need_reinit;
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Bit-array of supported event_method_features */
|
2008-05-31 14:37:31 +00:00
|
|
|
enum event_method_feature features;
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Length of extra information we should record for each fd that
|
|
|
|
has one or more active events.
|
|
|
|
*/
|
2009-01-14 20:52:32 +00:00
|
|
|
size_t fdinfo_len;
|
2007-11-25 06:57:59 +00:00
|
|
|
};
|
|
|
|
|
2009-01-09 13:42:21 +00:00
|
|
|
#ifdef WIN32
|
2009-04-17 06:55:08 +00:00
|
|
|
/* If we're on win32, then file descriptors are not nice low densely packed
|
|
|
|
integers. Instead, they are pointer-like windows handles, and we want to
|
|
|
|
use a hashtable instead of an array to map fds to events.
|
|
|
|
*/
|
2009-01-09 13:42:21 +00:00
|
|
|
#define EVMAP_USE_HT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EVMAP_USE_HT
|
|
|
|
#include "ht-internal.h"
|
|
|
|
struct event_map_entry;
|
|
|
|
HT_HEAD(event_io_map, event_map_entry);
|
|
|
|
#else
|
|
|
|
#define event_io_map event_signal_map
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Used to map signal numbers to a list of events. If EVMAP_USE_HT is not
|
|
|
|
defined, this is also used as event_io_map, to map fds to a list of events.
|
|
|
|
*/
|
|
|
|
struct event_signal_map {
|
2008-12-25 09:22:13 +00:00
|
|
|
void **entries;
|
2008-12-23 16:37:01 +00:00
|
|
|
int nentries;
|
|
|
|
};
|
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
struct event_base {
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Function pointers and other data to describe this event_base's
|
|
|
|
* backend. */
|
2004-11-25 09:50:18 +00:00
|
|
|
const struct eventop *evsel;
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Pointer to backend-specific data. */
|
2004-11-25 09:50:18 +00:00
|
|
|
void *evbase;
|
2008-12-23 16:37:01 +00:00
|
|
|
|
|
|
|
/* signal handling info */
|
|
|
|
const struct eventop *evsigsel;
|
|
|
|
void *evsigbase;
|
2008-12-23 22:23:37 +00:00
|
|
|
struct evsig_info sig;
|
2008-12-23 16:37:01 +00:00
|
|
|
|
2009-04-17 06:55:08 +00:00
|
|
|
int event_count; /**< counts number of total events */
|
|
|
|
int event_count_active; /**< counts number of active events */
|
2004-11-25 09:50:18 +00:00
|
|
|
|
2009-04-17 06:55:08 +00:00
|
|
|
int event_gotterm; /**< Set to terminate loop once done
|
|
|
|
* processing events. */
|
|
|
|
int event_break; /**< Set to exit loop immediately */
|
2004-11-25 09:50:18 +00:00
|
|
|
|
2009-04-17 06:55:08 +00:00
|
|
|
/* Active event management. */
|
|
|
|
/** An array of nactivequeues queues for active events (ones that
|
|
|
|
* have triggered, and whose callbacks need to be called). Low
|
|
|
|
* priority numbers are more important, and stall higher ones.
|
|
|
|
*/
|
2007-12-24 22:49:30 +00:00
|
|
|
struct event_list **activequeues;
|
2004-11-25 09:50:18 +00:00
|
|
|
int nactivequeues;
|
|
|
|
|
2009-07-14 16:54:48 +00:00
|
|
|
/** The event whose callback is executing right now */
|
|
|
|
struct event *current_event;
|
|
|
|
|
2009-07-26 01:29:39 +00:00
|
|
|
struct deferred_cb_queue defer_queue;
|
2009-04-10 14:22:33 +00:00
|
|
|
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Mapping from file descriptors to enabled events */
|
2009-01-09 13:42:21 +00:00
|
|
|
struct event_io_map io;
|
2008-12-23 16:37:01 +00:00
|
|
|
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Mapping from signal numbers to enabled events. */
|
2009-01-09 13:42:21 +00:00
|
|
|
struct event_signal_map sigmap;
|
2007-03-10 06:37:53 +00:00
|
|
|
|
2009-04-17 06:55:08 +00:00
|
|
|
/** All events that have been enabled (added) in this event_base */
|
2004-11-25 09:50:18 +00:00
|
|
|
struct event_list eventqueue;
|
2009-04-17 06:55:08 +00:00
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
struct timeval event_tv;
|
|
|
|
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Priority queue of events with timeouts. */
|
2007-11-03 18:04:53 +00:00
|
|
|
struct min_heap timeheap;
|
2008-03-02 21:18:33 +00:00
|
|
|
|
2008-05-03 18:23:44 +00:00
|
|
|
struct timeval tv_cache;
|
2009-01-19 20:37:24 +00:00
|
|
|
|
2009-02-12 22:19:54 +00:00
|
|
|
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
|
2008-03-02 21:18:33 +00:00
|
|
|
/* threading support */
|
2009-04-17 06:55:08 +00:00
|
|
|
/** The thread currently running the event_loop for this base */
|
2008-03-02 21:18:33 +00:00
|
|
|
unsigned long th_owner_id;
|
2009-04-17 06:55:08 +00:00
|
|
|
/** A lock to prevent conflicting accesses to this event_base */
|
2008-03-10 03:17:20 +00:00
|
|
|
void *th_base_lock;
|
2009-07-14 16:54:48 +00:00
|
|
|
/** A lock to prevent event_del from deleting an event while its
|
|
|
|
* callback is executing. */
|
|
|
|
void *current_event_lock;
|
2009-02-12 22:19:54 +00:00
|
|
|
#endif
|
2009-01-19 20:37:24 +00:00
|
|
|
|
2009-05-05 14:18:14 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
struct event_iocp_port *iocp;
|
|
|
|
#endif
|
|
|
|
|
2009-01-19 20:37:24 +00:00
|
|
|
/* Notify main thread to wake up break, etc. */
|
2008-03-02 21:18:33 +00:00
|
|
|
int th_notify_fd[2];
|
|
|
|
struct event th_notify;
|
2009-01-19 20:37:24 +00:00
|
|
|
int (*th_notify_fn)(struct event_base *base);
|
2004-11-25 09:50:18 +00:00
|
|
|
};
|
|
|
|
|
2008-05-08 05:56:20 +00:00
|
|
|
struct event_config_entry {
|
|
|
|
TAILQ_ENTRY(event_config_entry) (next);
|
|
|
|
|
|
|
|
const char *avoid_method;
|
|
|
|
};
|
|
|
|
|
2009-04-17 06:55:08 +00:00
|
|
|
/** Internal structure: describes the configuration we want for an event_base
|
|
|
|
* that we're about to allocate. */
|
2008-05-08 05:56:20 +00:00
|
|
|
struct event_config {
|
|
|
|
TAILQ_HEAD(event_configq, event_config_entry) entries;
|
2008-05-31 14:37:31 +00:00
|
|
|
|
|
|
|
enum event_method_feature require_features;
|
2009-02-12 22:19:54 +00:00
|
|
|
enum event_base_config_flag flags;
|
2008-05-08 05:56:20 +00:00
|
|
|
};
|
|
|
|
|
2007-11-07 06:01:57 +00:00
|
|
|
/* Internal use only: Functions that might be missing from <sys/queue.h> */
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifndef _EVENT_HAVE_TAILQFOREACH
|
2007-11-07 06:01:57 +00:00
|
|
|
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
|
|
|
#define TAILQ_END(head) NULL
|
|
|
|
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
|
|
|
|
#define TAILQ_FOREACH(var, head, field) \
|
|
|
|
for((var) = TAILQ_FIRST(head); \
|
|
|
|
(var) != TAILQ_END(head); \
|
|
|
|
(var) = TAILQ_NEXT(var, field))
|
|
|
|
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
|
|
|
|
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
|
|
|
|
(elm)->field.tqe_next = (listelm); \
|
|
|
|
*(listelm)->field.tqe_prev = (elm); \
|
|
|
|
(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
|
|
|
|
} while (0)
|
|
|
|
#endif /* TAILQ_FOREACH */
|
|
|
|
|
2009-07-26 01:29:39 +00:00
|
|
|
#define N_ACTIVE_CALLBACKS(base) \
|
|
|
|
((base)->event_count_active + (base)->defer_queue.active_count)
|
|
|
|
|
2008-12-23 22:23:37 +00:00
|
|
|
int _evsig_set_handler(struct event_base *base, int evsignal,
|
2007-11-25 17:15:28 +00:00
|
|
|
void (*fn)(int));
|
2008-12-23 22:23:37 +00:00
|
|
|
int _evsig_restore_handler(struct event_base *base, int evsignal);
|
2007-11-25 17:15:28 +00:00
|
|
|
|
2009-10-27 05:16:32 +00:00
|
|
|
void event_active_nolock(struct event *ev, int res, short count);
|
|
|
|
|
2004-11-25 09:50:18 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* _EVENT_INTERNAL_H_ */
|
2009-02-12 22:19:54 +00:00
|
|
|
|