OpenMPI 0.1.1

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

event-internal.h
1 /*
2 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
15 *
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.
26 */
27 #ifndef _EVENT_INTERNAL_H_
28 #define _EVENT_INTERNAL_H_
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #include "event2/event-config.h"
35 #include <time.h>
36 #include <sys/queue.h>
37 #include "event2/event_struct.h"
38 #include "minheap-internal.h"
39 #include "evsignal-internal.h"
40 #include "mm-internal.h"
41 #include "defer-internal.h"
42
43 /* map union members back */
44
45 /* mutually exclusive */
46 #define ev_signal_next _ev.ev_signal.ev_signal_next
47 #define ev_io_next _ev.ev_io.ev_io_next
48 #define ev_io_timeout _ev.ev_io.ev_timeout
49
50 /* used only by signals */
51 #define ev_ncalls _ev.ev_signal.ev_ncalls
52 #define ev_pncalls _ev.ev_signal.ev_pncalls
53
54 /* Possible values for ev_closure in struct event. */
55 #define EV_CLOSURE_NONE 0
56 #define EV_CLOSURE_SIGNAL 1
57 #define EV_CLOSURE_PERSIST 2
58
59 /** Structure to define the backend of a given event_base. */
60 struct eventop {
61 /** The name of this backend. */
62 const char *name;
63 /** Function to set up an event_base to use this backend. It should
64 * create a new structure holding whatever information is needed to
65 * run the backend, and return it. The returned pointer will get
66 * stored by event_init into the event_base.evbase field. On failure,
67 * this function should return NULL. */
68 void *(*init)(struct event_base *);
69 /** Enable reading/writing on a given fd or signal. 'events' will be
70 * the events that we're trying to enable: one or more of EV_READ,
71 * EV_WRITE, EV_SIGNAL, and EV_ET. 'old' will be those events that
72 * were enabled on this fd previously. 'fdinfo' will be a structure
73 * associated with the fd by the evmap; its size is defined by the
74 * fdinfo field below. It will be set to 0 the first time the fd is
75 * added. The function should return 0 on success and -1 on error.
76 */
77 int (*add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
78 /** As "add", except 'events' contains the events we mean to disable. */
79 int (*del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
80 /** Function to implement the core of an event loop. It must see which
81 added events are ready, and cause event_active to be called for each
82 active event (usually via event_io_active or such). It should
83 return 0 on success and -1 on error.
84 */
85 int (*dispatch)(struct event_base *, struct timeval *);
86 /** Function to clean up and free our data from the event_base. */
87 void (*dealloc)(struct event_base *);
88 /** Flag: set if we need to reinitialize the event base after we fork.
89 */
90 int need_reinit;
91 /** Bit-array of supported event_method_features that this backend can
92 * provide. */
93 enum event_method_feature features;
94 /** Length of the extra information we should record for each fd that
95 has one or more active events. This information is recorded
96 as part of the evmap entry for each fd, and passed as an argument
97 to the add and del functions above.
98 */
99 size_t fdinfo_len;
100 };
101
102 #ifdef WIN32
103 /* If we're on win32, then file descriptors are not nice low densely packed
104 integers. Instead, they are pointer-like windows handles, and we want to
105 use a hashtable instead of an array to map fds to events.
106 */
107 #define EVMAP_USE_HT
108 #endif
109
110 /* #define HT_CACHE_HASH_VALS */
111
112 #ifdef EVMAP_USE_HT
113 #include "ht-internal.h"
114 struct event_map_entry;
115 HT_HEAD(event_io_map, event_map_entry);
116 #else
117 #define event_io_map event_signal_map
118 #endif
119
120 /* Used to map signal numbers to a list of events. If EVMAP_USE_HT is not
121 defined, this structure is also used as event_io_map, which maps fds to a
122 list of events.
123 */
124 struct event_signal_map {
125 /* An array of evmap_io * or of evmap_signal *; empty entries are
126 * set to NULL. */
127 void **entries;
128 /* The number of entries available in entries */
129 int nentries;
130 };
131
132 /* A list of events waiting on a given 'common' timeout value. Ordinarily,
133 * events waiting for a timeout wait on a minheap. Sometimes, however, a
134 * queue can be faster.
135 **/
136 struct common_timeout_list {
137 /* List of events currently waiting in the queue. */
138 struct event_list events;
139 /* 'magic' timeval used to indicate the duration of events in this
140 * queue. */
141 struct timeval duration;
142 /* Event that triggers whenever one of the events in the queue is
143 * ready to activate */
144 struct event timeout_event;
145 /* The event_base that this timeout list is part of */
146 struct event_base *base;
147 };
148
149 struct event_change;
150
151 /* List of 'changes' since the last call to eventop.dispatch. Only maintained
152 * if the backend is using changesets. */
153 struct event_changelist {
154 struct event_change *changes;
155 int n_changes;
156 int changes_size;
157 };
158
159 #ifndef _EVENT_DISABLE_DEBUG_MODE
160 /* Global internal flag: set to one if debug mode is on. */
161 extern int _event_debug_mode_on;
162 #define EVENT_DEBUG_MODE_IS_ON() (_event_debug_mode_on)
163 #else
164 #define EVENT_DEBUG_MODE_IS_ON() (0)
165 #endif
166
167 struct event_base {
168 /** Function pointers and other data to describe this event_base's
169 * backend. */
170 const struct eventop *evsel;
171 /** Pointer to backend-specific data. */
172 void *evbase;
173
174 /** List of changes to tell backend about at next dispatch. Only used
175 * by the O(1) backends. */
176 struct event_changelist changelist;
177
178 /** Function pointers used to describe the backend that this event_base
179 * uses for signals */
180 const struct eventop *evsigsel;
181 /** Data to implement the common signal handelr code. */
182 struct evsig_info sig;
183
184 /** Number of virtual events */
185 int virtual_event_count;
186 /** Number of total events added to this event_base */
187 int event_count;
188 /** Number of total events active in this event_base */
189 int event_count_active;
190
191 /** Set if we should terminate the loop once we're done processing
192 * events. */
193 int event_gotterm;
194 /** Set if we should terminate the loop immediately */
195 int event_break;
196
197 /** Set if we're running the event_base_loop function, to prevent
198 * reentrant invocation. */
199 int running_loop;
200
201 /* Active event management. */
202 /** An array of nactivequeues queues for active events (ones that
203 * have triggered, and whose callbacks need to be called). Low
204 * priority numbers are more important, and stall higher ones.
205 */
206 struct event_list *activequeues;
207 /** The length of the activequeues array */
208 int nactivequeues;
209
210 /* common timeout logic */
211
212 /** An array of common_timeout_list* for all of the common timeout
213 * values we know. */
214 struct common_timeout_list **common_timeout_queues;
215 /** The number of entries used in common_timeout_queues */
216 int n_common_timeouts;
217 /** The total size of common_timeout_queues. */
218 int n_common_timeouts_allocated;
219
220 /** List of defered_cb that are active. We run these after the active
221 * events. */
222 struct deferred_cb_queue defer_queue;
223
224 /** Mapping from file descriptors to enabled (added) events */
225 struct event_io_map io;
226
227 /** Mapping from signal numbers to enabled (added) events. */
228 struct event_signal_map sigmap;
229
230 /** All events that have been enabled (added) in this event_base */
231 struct event_list eventqueue;
232
233 /** Stored timeval; used to detect when time is running backwards. */
234 struct timeval event_tv;
235
236 /** Priority queue of events with timeouts. */
237 struct min_heap timeheap;
238
239 /** Stored timeval: used to avoid calling gettimeofday/clock_gettime
240 * too often. */
241 struct timeval tv_cache;
242
243 #if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
244 /** Difference between internal time (maybe from clock_gettime) and
245 * gettimeofday. */
246 struct timeval tv_clock_diff;
247 /** Second in which we last updated tv_clock_diff, in monotonic time. */
248 time_t last_updated_clock_diff;
249 #endif
250
251 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
252 /* threading support */
253 /** The thread currently running the event_loop for this base */
254 unsigned long th_owner_id;
255 /** A lock to prevent conflicting accesses to this event_base */
256 void *th_base_lock;
257 /** The event whose callback is executing right now */
258 struct event *current_event;
259 /** A condition that gets signalled when we're done processing an
260 * event with waiters on it. */
261 void *current_event_cond;
262 /** Number of threads blocking on current_event_cond. */
263 int current_event_waiters;
264 #endif
265
266 #ifdef WIN32
267 /** IOCP support structure, if IOCP is enabled. */
268 struct event_iocp_port *iocp;
269 #endif
270
271 /** Flags that this base was configured with */
272 enum event_base_config_flag flags;
273
274 /* Notify main thread to wake up break, etc. */
275 /** True if the base already has a pending notify, and we don't need
276 * to add any more. */
277 int is_notify_pending;
278 /** A socketpair used by some th_notify functions to wake up the main
279 * thread. */
280 evutil_socket_t th_notify_fd[2];
281 /** An event used by some th_notify functions to wake up the main
282 * thread. */
283 struct event th_notify;
284 /** A function used to wake up the main thread from another thread. */
285 int (*th_notify_fn)(struct event_base *base);
286 };
287
288 struct event_config_entry {
289 TAILQ_ENTRY(event_config_entry) next;
290
291 const char *avoid_method;
292 };
293
294 /** Internal structure: describes the configuration we want for an event_base
295 * that we're about to allocate. */
296 struct event_config {
297 TAILQ_HEAD(event_configq, event_config_entry) entries;
298
299 int n_cpus_hint;
300 enum event_method_feature require_features;
301 enum event_base_config_flag flags;
302 };
303
304 /* Internal use only: Functions that might be missing from <sys/queue.h> */
305 #if defined(_EVENT_HAVE_SYS_QUEUE_H) && !defined(_EVENT_HAVE_TAILQFOREACH)
306 #ifndef TAILQ_FIRST
307 #define TAILQ_FIRST(head) ((head)->tqh_first)
308 #endif
309 #ifndef TAILQ_END
310 #define TAILQ_END(head) NULL
311 #endif
312 #ifndef TAILQ_NEXT
313 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
314 #endif
315
316 #ifndef TAILQ_FOREACH
317 #define TAILQ_FOREACH(var, head, field) \
318 for ((var) = TAILQ_FIRST(head); \
319 (var) != TAILQ_END(head); \
320 (var) = TAILQ_NEXT(var, field))
321 #endif
322
323 #ifndef TAILQ_INSERT_BEFORE
324 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
325 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
326 (elm)->field.tqe_next = (listelm); \
327 *(listelm)->field.tqe_prev = (elm); \
328 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
329 } while (0)
330 #endif
331 #endif /* TAILQ_FOREACH */
332
333 #define N_ACTIVE_CALLBACKS(base) \
334 ((base)->event_count_active + (base)->defer_queue.active_count)
335
336 int _evsig_set_handler(struct event_base *base, int evsignal,
337 void (*fn)(int));
338 int _evsig_restore_handler(struct event_base *base, int evsignal);
339
340
341 void event_active_nolock(struct event *ev, int res, short count);
342
343 /* FIXME document. */
344 void event_base_add_virtual(struct event_base *base);
345 void event_base_del_virtual(struct event_base *base);
346
347 #ifdef __cplusplus
348 }
349 #endif
350
351 #endif /* _EVENT_INTERNAL_H_ */
352
event_base::event_count
int event_count
Number of total events added to this event_base.
Definition: event-internal.h:187
event_base::current_event_cond
void * current_event_cond
A condition that gets signalled when we're done processing an event with waiters on it...
Definition: event-internal.h:261
event_base::sigmap
struct event_signal_map sigmap
Mapping from signal numbers to enabled (added) events.
Definition: event-internal.h:228
event_base::timeheap
struct min_heap timeheap
Priority queue of events with timeouts.
Definition: event-internal.h:237
event_config
Internal structure: describes the configuration we want for an event_base that we're about to allocat...
Definition: event-internal.h:296
event_base::tv_cache
struct timeval tv_cache
Stored timeval: used to avoid calling gettimeofday/clock_gettime too often.
Definition: event-internal.h:241
event_base::event_count_active
int event_count_active
Number of total events active in this event_base.
Definition: event-internal.h:189
event_base::event_break
int event_break
Set if we should terminate the loop immediately.
Definition: event-internal.h:195
event_base::changelist
struct event_changelist changelist
List of changes to tell backend about at next dispatch.
Definition: event-internal.h:176
event
Structure to represent a single event.
Definition: event_struct.h:87
evsig_info
Definition: evsignal-internal.h:39
event_base::n_common_timeouts_allocated
int n_common_timeouts_allocated
The total size of common_timeout_queues.
Definition: event-internal.h:218
event_base::event_gotterm
int event_gotterm
Set if we should terminate the loop once we're done processing events.
Definition: event-internal.h:193
eventop::dealloc
void(* dealloc)(struct event_base *)
Function to clean up and free our data from the event_base.
Definition: event-internal.h:87
eventop
Structure to define the backend of a given event_base.
Definition: event-internal.h:60
event_signal_map
Definition: event-internal.h:124
event_base::io
struct event_io_map io
Mapping from file descriptors to enabled (added) events.
Definition: event-internal.h:225
event_base::current_event_waiters
int current_event_waiters
Number of threads blocking on current_event_cond.
Definition: event-internal.h:263
event_base::th_base_lock
void * th_base_lock
A lock to prevent conflicting accesses to this event_base.
Definition: event-internal.h:256
event_base::current_event
struct event * current_event
The event whose callback is executing right now.
Definition: event-internal.h:258
evutil_socket_t
#define evutil_socket_t
A type wide enough to hold the output of "socket()" or "accept()".
Definition: util.h:278
event_base::nactivequeues
int nactivequeues
The length of the activequeues array.
Definition: event-internal.h:208
event_base::is_notify_pending
int is_notify_pending
True if the base already has a pending notify, and we don't need to add any more. ...
Definition: event-internal.h:277
eventop::del
int(* del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo)
As "add", except 'events' contains the events we mean to disable.
Definition: event-internal.h:79
event_base::running_loop
int running_loop
Set if we're running the event_base_loop function, to prevent reentrant invocation.
Definition: event-internal.h:199
event_base::flags
enum event_base_config_flag flags
Flags that this base was configured with.
Definition: event-internal.h:272
event_base::activequeues
struct event_list * activequeues
An array of nactivequeues queues for active events (ones that have triggered, and whose callbacks nee...
Definition: event-internal.h:206
event_base::n_common_timeouts
int n_common_timeouts
The number of entries used in common_timeout_queues.
Definition: event-internal.h:216
event_struct.h
Structures used by event.h.
event_base::virtual_event_count
int virtual_event_count
Number of virtual events.
Definition: event-internal.h:185
event_change
Represents a.
Definition: changelist-internal.h:53
event_base::th_notify
struct event th_notify
An event used by some th_notify functions to wake up the main thread.
Definition: event-internal.h:283
event_base_config_flag
event_base_config_flag
A flag passed to event_config_set_flag().
Definition: event.h:447
event_changelist
Definition: event-internal.h:153
event_base::evbase
void * evbase
Pointer to backend-specific data.
Definition: event-internal.h:172
event_base::evsel
const struct eventop * evsel
Function pointers and other data to describe this event_base's backend.
Definition: event-internal.h:170
event_base::evsigsel
const struct eventop * evsigsel
Function pointers used to describe the backend that this event_base uses for signals.
Definition: event-internal.h:180
common_timeout_list
Definition: event-internal.h:136
event_base::th_owner_id
unsigned long th_owner_id
The thread currently running the event_loop for this base.
Definition: event-internal.h:254
event_config_entry
Definition: event-internal.h:288
event_base::common_timeout_queues
struct common_timeout_list ** common_timeout_queues
An array of common_timeout_list* for all of the common timeout values we know.
Definition: event-internal.h:214
event_base::event_tv
struct timeval event_tv
Stored timeval; used to detect when time is running backwards.
Definition: event-internal.h:234
eventop::need_reinit
int need_reinit
Flag: set if we need to reinitialize the event base after we fork.
Definition: event-internal.h:90
eventop::name
const char * name
The name of this backend.
Definition: event-internal.h:62
event_base::th_notify_fn
int(* th_notify_fn)(struct event_base *base)
A function used to wake up the main thread from another thread.
Definition: event-internal.h:285
eventop::features
enum event_method_feature features
Bit-array of supported event_method_features that this backend can provide.
Definition: event-internal.h:93
eventop::dispatch
int(* dispatch)(struct event_base *, struct timeval *)
Function to implement the core of an event loop.
Definition: event-internal.h:85
event_base::defer_queue
struct deferred_cb_queue defer_queue
List of defered_cb that are active.
Definition: event-internal.h:222
event_base::sig
struct evsig_info sig
Data to implement the common signal handelr code.
Definition: event-internal.h:182
eventop::add
int(* add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo)
Enable reading/writing on a given fd or signal.
Definition: event-internal.h:77
event_base::eventqueue
struct event_list eventqueue
All events that have been enabled (added) in this event_base.
Definition: event-internal.h:231
event_base::th_notify_fd
evutil_socket_t th_notify_fd[2]
A socketpair used by some th_notify functions to wake up the main thread.
Definition: event-internal.h:280
eventop::fdinfo_len
size_t fdinfo_len
Length of the extra information we should record for each fd that has one or more active events...
Definition: event-internal.h:99
event_base
Definition: event-internal.h:167
event_method_feature
event_method_feature
A flag used to describe which features an event_base (must) provide.
Definition: event.h:426
deferred_cb_queue
A deferred_cb_queue is a list of deferred_cb that we can add to and run.
Definition: defer-internal.h:54
min_heap
Definition: minheap-internal.h:38