OpenMPI  0.1.1
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  */
91  /** Bit-array of supported event_method_features that this backend can
92  * provide. */
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 */
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  **/
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. */
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. */
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 */
186  /** Number of total events added to this event_base */
188  /** Number of total events active in this event_base */
190 
191  /** Set if we should terminate the loop once we're done processing
192  * events. */
194  /** Set if we should terminate the loop immediately */
196 
197  /** Set if we're running the event_base_loop function, to prevent
198  * reentrant invocation. */
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 */
209 
210  /* common timeout logic */
211 
212  /** An array of common_timeout_list* for all of the common timeout
213  * values we know. */
215  /** The number of entries used in common_timeout_queues */
217  /** The total size of common_timeout_queues. */
219 
220  /** List of defered_cb that are active. We run these after the active
221  * events. */
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. */
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. */
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 */
257  /** The event whose callback is executing right now */
259  /** A condition that gets signalled when we're done processing an
260  * event with waiters on it. */
262  /** Number of threads blocking on current_event_cond. */
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 */
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. */
278  /** A socketpair used by some th_notify functions to wake up the main
279  * thread. */
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 
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 
int event_count
Number of total events added to this event_base.
Definition: event-internal.h:187
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
struct event_signal_map sigmap
Mapping from signal numbers to enabled (added) events.
Definition: event-internal.h:228
struct min_heap timeheap
Priority queue of events with timeouts.
Definition: event-internal.h:237
Internal structure: describes the configuration we want for an event_base that we're about to allocat...
Definition: event-internal.h:296
struct timeval tv_cache
Stored timeval: used to avoid calling gettimeofday/clock_gettime too often.
Definition: event-internal.h:241
int event_count_active
Number of total events active in this event_base.
Definition: event-internal.h:189
int event_break
Set if we should terminate the loop immediately.
Definition: event-internal.h:195
struct event_changelist changelist
List of changes to tell backend about at next dispatch.
Definition: event-internal.h:176
Structure to represent a single event.
Definition: event_struct.h:87
Definition: evsignal-internal.h:39
int n_common_timeouts_allocated
The total size of common_timeout_queues.
Definition: event-internal.h:218
int event_gotterm
Set if we should terminate the loop once we're done processing events.
Definition: event-internal.h:193
void(* dealloc)(struct event_base *)
Function to clean up and free our data from the event_base.
Definition: event-internal.h:87
Structure to define the backend of a given event_base.
Definition: event-internal.h:60
Definition: event-internal.h:124
struct event_io_map io
Mapping from file descriptors to enabled (added) events.
Definition: event-internal.h:225
int current_event_waiters
Number of threads blocking on current_event_cond.
Definition: event-internal.h:263
void * th_base_lock
A lock to prevent conflicting accesses to this event_base.
Definition: event-internal.h:256
struct event * current_event
The event whose callback is executing right now.
Definition: event-internal.h:258
#define evutil_socket_t
A type wide enough to hold the output of "socket()" or "accept()".
Definition: util.h:278
int nactivequeues
The length of the activequeues array.
Definition: event-internal.h:208
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
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
int running_loop
Set if we're running the event_base_loop function, to prevent reentrant invocation.
Definition: event-internal.h:199
enum event_base_config_flag flags
Flags that this base was configured with.
Definition: event-internal.h:272
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
int n_common_timeouts
The number of entries used in common_timeout_queues.
Definition: event-internal.h:216
Structures used by event.h.
int virtual_event_count
Number of virtual events.
Definition: event-internal.h:185
Represents a.
Definition: changelist-internal.h:53
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
A flag passed to event_config_set_flag().
Definition: event.h:447
Definition: event-internal.h:153
void * evbase
Pointer to backend-specific data.
Definition: event-internal.h:172
const struct eventop * evsel
Function pointers and other data to describe this event_base's backend.
Definition: event-internal.h:170
const struct eventop * evsigsel
Function pointers used to describe the backend that this event_base uses for signals.
Definition: event-internal.h:180
Definition: event-internal.h:136
unsigned long th_owner_id
The thread currently running the event_loop for this base.
Definition: event-internal.h:254
Definition: event-internal.h:288
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
struct timeval event_tv
Stored timeval; used to detect when time is running backwards.
Definition: event-internal.h:234
int need_reinit
Flag: set if we need to reinitialize the event base after we fork.
Definition: event-internal.h:90
const char * name
The name of this backend.
Definition: event-internal.h:62
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
enum event_method_feature features
Bit-array of supported event_method_features that this backend can provide.
Definition: event-internal.h:93
int(* dispatch)(struct event_base *, struct timeval *)
Function to implement the core of an event loop.
Definition: event-internal.h:85
struct deferred_cb_queue defer_queue
List of defered_cb that are active.
Definition: event-internal.h:222
struct evsig_info sig
Data to implement the common signal handelr code.
Definition: event-internal.h:182
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
struct event_list eventqueue
All events that have been enabled (added) in this event_base.
Definition: event-internal.h:231
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
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
Definition: event-internal.h:167
event_method_feature
A flag used to describe which features an event_base (must) provide.
Definition: event.h:426
A deferred_cb_queue is a list of deferred_cb that we can add to and run.
Definition: defer-internal.h:54
Definition: minheap-internal.h:38