1 /* $NetBSD: event-internal.h,v 1.2 2013/04/11 16:56:41 christos Exp $ */
3 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef _EVENT_INTERNAL_H_
29 #define _EVENT_INTERNAL_H_
35 #include "event2/event-config.h"
37 #include <sys/queue.h>
38 #include "event2/event_struct.h"
39 #include "minheap-internal.h"
40 #include "evsignal-internal.h"
41 #include "mm-internal.h"
42 #include "defer-internal.h"
44 /* map union members back */
46 /* mutually exclusive */
47 #define ev_signal_next _ev.ev_signal.ev_signal_next
48 #define ev_io_next _ev.ev_io.ev_io_next
49 #define ev_io_timeout _ev.ev_io.ev_timeout
51 /* used only by signals */
52 #define ev_ncalls _ev.ev_signal.ev_ncalls
53 #define ev_pncalls _ev.ev_signal.ev_pncalls
55 /* Possible values for ev_closure in struct event. */
56 #define EV_CLOSURE_NONE 0
57 #define EV_CLOSURE_SIGNAL 1
58 #define EV_CLOSURE_PERSIST 2
60 /** Structure to define the backend of a given event_base. */
62 /** The name of this backend. */
64 /** Function to set up an event_base to use this backend. It should
65 * create a new structure holding whatever information is needed to
66 * run the backend, and return it. The returned pointer will get
67 * stored by event_init into the event_base.evbase field. On failure,
68 * this function should return NULL. */
69 void *(*init
)(struct event_base
*);
70 /** Enable reading/writing on a given fd or signal. 'events' will be
71 * the events that we're trying to enable: one or more of EV_READ,
72 * EV_WRITE, EV_SIGNAL, and EV_ET. 'old' will be those events that
73 * were enabled on this fd previously. 'fdinfo' will be a structure
74 * associated with the fd by the evmap; its size is defined by the
75 * fdinfo field below. It will be set to 0 the first time the fd is
76 * added. The function should return 0 on success and -1 on error.
78 int (*add
)(struct event_base
*, evutil_socket_t fd
, short old
, short events
, void *fdinfo
);
79 /** As "add", except 'events' contains the events we mean to disable. */
80 int (*del
)(struct event_base
*, evutil_socket_t fd
, short old
, short events
, void *fdinfo
);
81 /** Function to implement the core of an event loop. It must see which
82 added events are ready, and cause event_active to be called for each
83 active event (usually via event_io_active or such). It should
84 return 0 on success and -1 on error.
86 int (*dispatch
)(struct event_base
*, struct timeval
*);
87 /** Function to clean up and free our data from the event_base. */
88 void (*dealloc
)(struct event_base
*);
89 /** Flag: set if we need to reinitialize the event base after we fork.
92 /** Bit-array of supported event_method_features that this backend can
94 enum event_method_feature features
;
95 /** Length of the extra information we should record for each fd that
96 has one or more active events. This information is recorded
97 as part of the evmap entry for each fd, and passed as an argument
98 to the add and del functions above.
104 /* If we're on win32, then file descriptors are not nice low densely packed
105 integers. Instead, they are pointer-like windows handles, and we want to
106 use a hashtable instead of an array to map fds to events.
111 /* #define HT_CACHE_HASH_VALS */
114 #include "ht-internal.h"
115 struct event_map_entry
;
116 HT_HEAD(event_io_map
, event_map_entry
);
118 #define event_io_map event_signal_map
121 /* Used to map signal numbers to a list of events. If EVMAP_USE_HT is not
122 defined, this structure is also used as event_io_map, which maps fds to a
125 struct event_signal_map
{
126 /* An array of evmap_io * or of evmap_signal *; empty entries are
129 /* The number of entries available in entries */
133 /* A list of events waiting on a given 'common' timeout value. Ordinarily,
134 * events waiting for a timeout wait on a minheap. Sometimes, however, a
135 * queue can be faster.
137 struct common_timeout_list
{
138 /* List of events currently waiting in the queue. */
139 struct event_list events
;
140 /* 'magic' timeval used to indicate the duration of events in this
142 struct timeval duration
;
143 /* Event that triggers whenever one of the events in the queue is
144 * ready to activate */
145 struct event timeout_event
;
146 /* The event_base that this timeout list is part of */
147 struct event_base
*base
;
150 /** Mask used to get the real tv_usec value from a common timeout. */
151 #define COMMON_TIMEOUT_MICROSECONDS_MASK 0x000fffff
155 /* List of 'changes' since the last call to eventop.dispatch. Only maintained
156 * if the backend is using changesets. */
157 struct event_changelist
{
158 struct event_change
*changes
;
163 #ifndef _EVENT_DISABLE_DEBUG_MODE
164 /* Global internal flag: set to one if debug mode is on. */
165 extern int _event_debug_mode_on
;
166 #define EVENT_DEBUG_MODE_IS_ON() (_event_debug_mode_on)
168 #define EVENT_DEBUG_MODE_IS_ON() (0)
172 /** Function pointers and other data to describe this event_base's
174 const struct eventop
*evsel
;
175 /** Pointer to backend-specific data. */
178 /** List of changes to tell backend about at next dispatch. Only used
179 * by the O(1) backends. */
180 struct event_changelist changelist
;
182 /** Function pointers used to describe the backend that this event_base
183 * uses for signals */
184 const struct eventop
*evsigsel
;
185 /** Data to implement the common signal handelr code. */
186 struct evsig_info sig
;
188 /** Number of virtual events */
189 int virtual_event_count
;
190 /** Number of total events added to this event_base */
192 /** Number of total events active in this event_base */
193 int event_count_active
;
195 /** Set if we should terminate the loop once we're done processing
198 /** Set if we should terminate the loop immediately */
200 /** Set if we should start a new instance of the loop immediately. */
203 /** The currently running priority of events */
204 int event_running_priority
;
206 /** Set if we're running the event_base_loop function, to prevent
207 * reentrant invocation. */
210 /* Active event management. */
211 /** An array of nactivequeues queues for active events (ones that
212 * have triggered, and whose callbacks need to be called). Low
213 * priority numbers are more important, and stall higher ones.
215 struct event_list
*activequeues
;
216 /** The length of the activequeues array */
219 /* common timeout logic */
221 /** An array of common_timeout_list* for all of the common timeout
223 struct common_timeout_list
**common_timeout_queues
;
224 /** The number of entries used in common_timeout_queues */
225 int n_common_timeouts
;
226 /** The total size of common_timeout_queues. */
227 int n_common_timeouts_allocated
;
229 /** List of defered_cb that are active. We run these after the active
231 struct deferred_cb_queue defer_queue
;
233 /** Mapping from file descriptors to enabled (added) events */
234 struct event_io_map io
;
236 /** Mapping from signal numbers to enabled (added) events. */
237 struct event_signal_map sigmap
;
239 /** All events that have been enabled (added) in this event_base */
240 struct event_list eventqueue
;
242 /** Stored timeval; used to detect when time is running backwards. */
243 struct timeval event_tv
;
245 /** Priority queue of events with timeouts. */
246 struct min_heap timeheap
;
248 /** Stored timeval: used to avoid calling gettimeofday/clock_gettime
250 struct timeval tv_cache
;
252 #if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
253 /** Difference between internal time (maybe from clock_gettime) and
255 struct timeval tv_clock_diff
;
256 /** Second in which we last updated tv_clock_diff, in monotonic time. */
257 time_t last_updated_clock_diff
;
260 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
261 /* threading support */
262 /** The thread currently running the event_loop for this base */
263 unsigned long th_owner_id
;
264 /** A lock to prevent conflicting accesses to this event_base */
266 /** The event whose callback is executing right now */
267 struct event
*current_event
;
268 /** A condition that gets signalled when we're done processing an
269 * event with waiters on it. */
270 void *current_event_cond
;
271 /** Number of threads blocking on current_event_cond. */
272 int current_event_waiters
;
276 /** IOCP support structure, if IOCP is enabled. */
277 struct event_iocp_port
*iocp
;
280 /** Flags that this base was configured with */
281 enum event_base_config_flag flags
;
283 /* Notify main thread to wake up break, etc. */
284 /** True if the base already has a pending notify, and we don't need
285 * to add any more. */
286 int is_notify_pending
;
287 /** A socketpair used by some th_notify functions to wake up the main
289 evutil_socket_t th_notify_fd
[2];
290 /** An event used by some th_notify functions to wake up the main
292 struct event th_notify
;
293 /** A function used to wake up the main thread from another thread. */
294 int (*th_notify_fn
)(struct event_base
*base
);
297 struct event_config_entry
{
298 TAILQ_ENTRY(event_config_entry
) next
;
300 const char *avoid_method
;
303 /** Internal structure: describes the configuration we want for an event_base
304 * that we're about to allocate. */
305 struct event_config
{
306 TAILQ_HEAD(event_configq
, event_config_entry
) entries
;
309 enum event_method_feature require_features
;
310 enum event_base_config_flag flags
;
313 /* Internal use only: Functions that might be missing from <sys/queue.h> */
314 #if defined(_EVENT_HAVE_SYS_QUEUE_H) && !defined(_EVENT_HAVE_TAILQFOREACH)
316 #define TAILQ_FIRST(head) ((head)->tqh_first)
319 #define TAILQ_END(head) NULL
322 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
325 #ifndef TAILQ_FOREACH
326 #define TAILQ_FOREACH(var, head, field) \
327 for ((var) = TAILQ_FIRST(head); \
328 (var) != TAILQ_END(head); \
329 (var) = TAILQ_NEXT(var, field))
332 #ifndef TAILQ_INSERT_BEFORE
333 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
334 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
335 (elm)->field.tqe_next = (listelm); \
336 *(listelm)->field.tqe_prev = (elm); \
337 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
338 } while (/*CONSTCOND*/0)
340 #endif /* TAILQ_FOREACH */
342 #define N_ACTIVE_CALLBACKS(base) \
343 ((base)->event_count_active + (base)->defer_queue.active_count)
345 int _evsig_set_handler(struct event_base
*base
, int evsignal
,
347 int _evsig_restore_handler(struct event_base
*base
, int evsignal
);
350 void event_active_nolock(struct event
*ev
, int res
, short count
);
352 /* FIXME document. */
353 void event_base_add_virtual(struct event_base
*base
);
354 void event_base_del_virtual(struct event_base
*base
);
356 /** For debugging: unless assertions are disabled, verify the referential
357 integrity of the internal data structures of 'base'. This operation can
360 Returns on success; aborts on failure.
362 void event_base_assert_ok(struct event_base
*base
);
368 #endif /* _EVENT_INTERNAL_H_ */