2 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef _EVENT_INTERNAL_H_
28 #define _EVENT_INTERNAL_H_
34 #include "event2/event-config.h"
35 #include <sys/queue.h>
36 #include "event2/event_struct.h"
37 #include "minheap-internal.h"
38 #include "evsignal-internal.h"
39 #include "mm-internal.h"
40 #include "defer-internal.h"
42 /* map union members back */
44 /* mutually exclusive */
45 #define ev_signal_next _ev.ev_signal.ev_signal_next
46 #define ev_io_next _ev.ev_io.ev_io_next
47 #define ev_io_timeout _ev.ev_io.ev_timeout
49 /* used only by signals */
50 #define ev_ncalls _ev.ev_signal.ev_ncalls
51 #define ev_pncalls _ev.ev_signal.ev_pncalls
53 /* Possible values for ev_closure in struct event. */
54 #define EV_CLOSURE_NONE 0
55 #define EV_CLOSURE_SIGNAL 1
56 #define EV_CLOSURE_PERSIST 2
58 /** Structure to define the backend of a given event_base. */
60 /** The name of this backend. */
62 /** Function to set up an event_base to use this backend. It should
63 * create a new structure holding whatever information is needed to
64 * run the backend, and return it. The returned pointer will get
65 * stored by event_init into the event_base.evbase field. On failure,
66 * this function should return NULL. */
67 void *(*init)(struct event_base *);
68 /** Enable reading/writing on a given fd or signal. 'events' will be
69 * the events that we're trying to enable: one or more of EV_READ,
70 * EV_WRITE, EV_SIGNAL, and EV_ET. 'old' will be those events that
71 * were enabled on this fd previously. 'fdinfo' will be a structure
72 * associated with the fd by the evmap; its size is defined by the
73 * fdinfo field below. It will be set to 0 the first time the fd is
74 * added. The function should return 0 on success and -1 on error.
76 int (*add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
77 /** As "add", except 'events' contains the events we mean to disable. */
78 int (*del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
79 /** Function to implement the core of an event loop. It must see which
80 added events are ready, and cause event_active to be called for each
81 active event (usually via event_io_active or such). It should
82 return 0 on success and -1 on error.
84 int (*dispatch)(struct event_base *, struct timeval *);
85 /** Function to clean up and free our data from the event_base. */
86 void (*dealloc)(struct event_base *);
87 /** Flag: set if we need to reinitialize the event base after we fork.
90 /** Bit-array of supported event_method_features that this backend can
92 enum event_method_feature features;
93 /** Length of the extra information we should record for each fd that
94 has one or more active events. This information is recorded
95 as part of the evmap entry for each fd, and passed as an argument
96 to the add and del functions above.
102 /* If we're on win32, then file descriptors are not nice low densely packed
103 integers. Instead, they are pointer-like windows handles, and we want to
104 use a hashtable instead of an array to map fds to events.
109 /* #define HT_CACHE_HASH_VALS */
112 #include "ht-internal.h"
113 struct event_map_entry;
114 HT_HEAD(event_io_map, event_map_entry);
116 #define event_io_map event_signal_map
119 /* Used to map signal numbers to a list of events. If EVMAP_USE_HT is not
120 defined, this structure is also used as event_io_map, which maps fds to a
123 struct event_signal_map {
124 /* An array of evmap_io * or of evmap_signal *; empty entries are
127 /* The number of entries available in entries */
131 /* A list of events waiting on a given 'common' timeout value. Ordinarily,
132 * events waiting for a timeout wait on a minheap. Sometimes, however, a
133 * queue can be faster.
135 struct common_timeout_list {
136 /* List of events currently waiting in the queue. */
137 struct event_list events;
138 /* 'magic' timeval used to indicate the duration of events in this
140 struct timeval duration;
141 /* Event that triggers whenever one of the events in the queue is
142 * ready to activate */
143 struct event timeout_event;
144 /* The event_base that this timeout list is part of */
145 struct event_base *base;
150 /* List of 'changes' since the last call to eventop.dispatch. Only maintained
151 * if the backend is using changesets. */
152 struct event_changelist {
153 struct event_change *changes;
158 #ifndef _EVENT_DISABLE_DEBUG_MODE
159 /* Global internal flag: set to one if debug mode is on. */
160 extern int _event_debug_mode_on;
161 #define EVENT_DEBUG_MODE_IS_ON() (_event_debug_mode_on)
163 #define EVENT_DEBUG_MODE_IS_ON() (0)
167 /** Function pointers and other data to describe this event_base's
169 const struct eventop *evsel;
170 /** Pointer to backend-specific data. */
173 /** List of changes to tell backend about at next dispatch. Only used
174 * by the O(1) backends. */
175 struct event_changelist changelist;
177 /** Function pointers used to describe the backend that this event_base
178 * uses for signals */
179 const struct eventop *evsigsel;
180 /** Data to implement the common signal handelr code. */
181 struct evsig_info sig;
183 /** Number of virtual events */
184 int virtual_event_count;
185 /** Number of total events added to this event_base */
187 /** Number of total events active in this event_base */
188 int event_count_active;
190 /** Set if we should terminate the loop once we're done processing
193 /** Set if we should terminate the loop immediately */
196 /** Set if we're running the event_base_loop function, to prevent
197 * reentrant invocation. */
200 /* Active event management. */
201 /** An array of nactivequeues queues for active events (ones that
202 * have triggered, and whose callbacks need to be called). Low
203 * priority numbers are more important, and stall higher ones.
205 struct event_list *activequeues;
206 /** The length of the activequeues array */
209 /* common timeout logic */
211 /** An array of common_timeout_list* for all of the common timeout
213 struct common_timeout_list **common_timeout_queues;
214 /** The number of entries used in common_timeout_queues */
215 int n_common_timeouts;
216 /** The total size of common_timeout_queues. */
217 int n_common_timeouts_allocated;
219 /** List of defered_cb that are active. We run these after the active
221 struct deferred_cb_queue defer_queue;
223 /** Mapping from file descriptors to enabled (added) events */
224 struct event_io_map io;
226 /** Mapping from signal numbers to enabled (added) events. */
227 struct event_signal_map sigmap;
229 /** All events that have been enabled (added) in this event_base */
230 struct event_list eventqueue;
232 /** Stored timeval; used to detect when time is running backwards. */
233 struct timeval event_tv;
235 /** Priority queue of events with timeouts. */
236 struct min_heap timeheap;
238 /** Stored timeval: used to avoid calling gettimeofday too often. */
239 struct timeval tv_cache;
241 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
242 /* threading support */
243 /** The thread currently running the event_loop for this base */
244 unsigned long th_owner_id;
245 /** A lock to prevent conflicting accesses to this event_base */
247 /** The event whose callback is executing right now */
248 struct event *current_event;
249 /** A condition that gets signalled when we're done processing an
250 * event with waiters on it. */
251 void *current_event_cond;
252 /** Number of threads blocking on current_event_cond. */
253 int current_event_waiters;
257 /** IOCP support structure, if IOCP is enabled. */
258 struct event_iocp_port *iocp;
261 /** Flags that this base was configured with */
262 enum event_base_config_flag flags;
264 /* Notify main thread to wake up break, etc. */
265 /** True if the base already has a pending notify, and we don't need
266 * to add any more. */
267 int is_notify_pending;
268 /** A socketpair used by some th_notify functions to wake up the main
270 evutil_socket_t th_notify_fd[2];
271 /** An event used by some th_notify functions to wake up the main
273 struct event th_notify;
274 /** A function used to wake up the main thread from another thread. */
275 int (*th_notify_fn)(struct event_base *base);
278 struct event_config_entry {
279 TAILQ_ENTRY(event_config_entry) next;
281 const char *avoid_method;
284 /** Internal structure: describes the configuration we want for an event_base
285 * that we're about to allocate. */
286 struct event_config {
287 TAILQ_HEAD(event_configq, event_config_entry) entries;
290 enum event_method_feature require_features;
291 enum event_base_config_flag flags;
294 /* Internal use only: Functions that might be missing from <sys/queue.h> */
295 #if defined(_EVENT_HAVE_SYS_QUEUE_H) && !defined(_EVENT_HAVE_TAILQFOREACH)
297 #define TAILQ_FIRST(head) ((head)->tqh_first)
300 #define TAILQ_END(head) NULL
303 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
306 #define TAILQ_FOREACH(var, head, field) \
307 for ((var) = TAILQ_FIRST(head); \
308 (var) != TAILQ_END(head); \
309 (var) = TAILQ_NEXT(var, field))
311 #ifndef TAILQ_INSERT_BEFORE
312 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
313 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
314 (elm)->field.tqe_next = (listelm); \
315 *(listelm)->field.tqe_prev = (elm); \
316 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
319 #endif /* TAILQ_FOREACH */
321 #define N_ACTIVE_CALLBACKS(base) \
322 ((base)->event_count_active + (base)->defer_queue.active_count)
324 int _evsig_set_handler(struct event_base *base, int evsignal,
326 int _evsig_restore_handler(struct event_base *base, int evsignal);
328 void event_active_nolock(struct event *ev, int res, short count);
330 /* FIXME document. */
331 void event_base_add_virtual(struct event_base *base);
332 void event_base_del_virtual(struct event_base *base);
338 #endif /* _EVENT_INTERNAL_H_ */