Libevent Code Doxygen document
event-internal.h
1 /*
2  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright (c) 2007-2012 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_INCLUDED_
28 #define EVENT_INTERNAL_H_INCLUDED_
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include "event2/event-config.h"
35 #include "evconfig-private.h"
36 
37 #include <time.h>
38 #include <sys/queue.h>
39 #include "event2/event_struct.h"
40 #include "minheap-internal.h"
41 #include "evsignal-internal.h"
42 #include "mm-internal.h"
43 #include "defer-internal.h"
44 
45 /* map union members back */
46 
47 /* mutually exclusive */
48 #define ev_signal_next ev_.ev_signal.ev_signal_next
49 #define ev_io_next ev_.ev_io.ev_io_next
50 #define ev_io_timeout ev_.ev_io.ev_timeout
51 
52 /* used only by signals */
53 #define ev_ncalls ev_.ev_signal.ev_ncalls
54 #define ev_pncalls ev_.ev_signal.ev_pncalls
55 
56 #define ev_pri ev_evcallback.evcb_pri
57 #define ev_flags ev_evcallback.evcb_flags
58 #define ev_closure ev_evcallback.evcb_closure
59 #define ev_callback ev_evcallback.evcb_cb_union.evcb_callback
60 #define ev_arg ev_evcallback.evcb_arg
61 
69 #define EV_CLOSURE_EVENT 0
71 #define EV_CLOSURE_EVENT_SIGNAL 1
73 #define EV_CLOSURE_EVENT_PERSIST 2
75 #define EV_CLOSURE_CB_SELF 3
77 #define EV_CLOSURE_CB_FINALIZE 4
79 #define EV_CLOSURE_EVENT_FINALIZE 5
82 #define EV_CLOSURE_EVENT_FINALIZE_FREE 6
86 struct eventop {
88  const char *name;
94  void *(*init)(struct event_base *);
103  int (*add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
105  int (*del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
111  int (*dispatch)(struct event_base *, struct timeval *);
113  void (*dealloc)(struct event_base *);
125  size_t fdinfo_len;
126 };
127 
128 #ifdef _WIN32
129 /* If we're on win32, then file descriptors are not nice low densely packed
130  integers. Instead, they are pointer-like windows handles, and we want to
131  use a hashtable instead of an array to map fds to events.
132 */
133 #define EVMAP_USE_HT
134 #endif
135 
136 /* #define HT_CACHE_HASH_VALS */
137 
138 #ifdef EVMAP_USE_HT
139 #define HT_NO_CACHE_HASH_VALUES
140 #include "ht-internal.h"
141 struct event_map_entry;
142 HT_HEAD(event_io_map, event_map_entry);
143 #else
144 #define event_io_map event_signal_map
145 #endif
146 
147 /* Used to map signal numbers to a list of events. If EVMAP_USE_HT is not
148  defined, this structure is also used as event_io_map, which maps fds to a
149  list of events.
150 */
152  /* An array of evmap_io * or of evmap_signal *; empty entries are
153  * set to NULL. */
154  void **entries;
155  /* The number of entries available in entries */
156  int nentries;
157 };
158 
159 /* A list of events waiting on a given 'common' timeout value. Ordinarily,
160  * events waiting for a timeout wait on a minheap. Sometimes, however, a
161  * queue can be faster.
162  **/
164  /* List of events currently waiting in the queue. */
165  struct event_list events;
166  /* 'magic' timeval used to indicate the duration of events in this
167  * queue. */
168  struct timeval duration;
169  /* Event that triggers whenever one of the events in the queue is
170  * ready to activate */
171  struct event timeout_event;
172  /* The event_base that this timeout list is part of */
173  struct event_base *base;
174 };
175 
177 #define COMMON_TIMEOUT_MICROSECONDS_MASK 0x000fffff
178 
179 struct event_change;
180 
181 /* List of 'changes' since the last call to eventop.dispatch. Only maintained
182  * if the backend is using changesets. */
184  struct event_change *changes;
185  int n_changes;
186  int changes_size;
187 };
188 
189 #ifndef EVENT__DISABLE_DEBUG_MODE
190 /* Global internal flag: set to one if debug mode is on. */
191 extern int event_debug_mode_on_;
192 #define EVENT_DEBUG_MODE_IS_ON() (event_debug_mode_on_)
193 #else
194 #define EVENT_DEBUG_MODE_IS_ON() (0)
195 #endif
196 
197 TAILQ_HEAD(evcallback_list, event_callback);
198 
199 /* Sets up an event for processing once */
200 struct event_once {
201  LIST_ENTRY(event_once) next_once;
202  struct event ev;
203 
204  void (*cb)(evutil_socket_t, short, void *);
205  void *arg;
206 };
207 
208 struct event_base {
211  const struct eventop *evsel;
213  void *evbase;
214 
218 
221  const struct eventop *evsigsel;
223  struct evsig_info sig;
224 
237 
245 
248 
252 
258 
259  /* Active event management. */
264  struct evcallback_list *activequeues;
269  struct evcallback_list active_later_queue;
270 
271  /* common timeout logic */
272 
280 
282  struct event_io_map io;
283 
285  struct event_signal_map sigmap;
286 
288  struct min_heap timeheap;
289 
292  struct timeval tv_cache;
293 
294  struct evutil_monotonic_timer monotonic_timer;
295 
298  struct timeval tv_clock_diff;
301 
302 #ifndef EVENT__DISABLE_THREAD_SUPPORT
303  /* threading support */
305  unsigned long th_owner_id;
313 #endif
316 
317 #ifdef _WIN32
319  struct event_iocp_port *iocp;
320 #endif
321 
324 
325  struct timeval max_dispatch_time;
326  int max_dispatch_callbacks;
327  int limit_callbacks_after_prio;
328 
329  /* Notify main thread to wake up break, etc. */
338  struct event th_notify;
340  int (*th_notify_fn)(struct event_base *base);
341 
345 
347  LIST_HEAD(once_event_list, event_once) once_events;
348 
349 };
350 
352  TAILQ_ENTRY(event_config_entry) next;
353 
354  const char *avoid_method;
355 };
356 
359 struct event_config {
360  TAILQ_HEAD(event_configq, event_config_entry) entries;
361 
362  int n_cpus_hint;
363  struct timeval max_dispatch_interval;
364  int max_dispatch_callbacks;
365  int limit_callbacks_after_prio;
366  enum event_method_feature require_features;
367  enum event_base_config_flag flags;
368 };
369 
370 /* Internal use only: Functions that might be missing from <sys/queue.h> */
371 #ifndef LIST_END
372 #define LIST_END(head) NULL
373 #endif
374 
375 #ifndef TAILQ_FIRST
376 #define TAILQ_FIRST(head) ((head)->tqh_first)
377 #endif
378 #ifndef TAILQ_END
379 #define TAILQ_END(head) NULL
380 #endif
381 #ifndef TAILQ_NEXT
382 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
383 #endif
384 
385 #ifndef TAILQ_FOREACH
386 #define TAILQ_FOREACH(var, head, field) \
387  for ((var) = TAILQ_FIRST(head); \
388  (var) != TAILQ_END(head); \
389  (var) = TAILQ_NEXT(var, field))
390 #endif
391 
392 #ifndef TAILQ_INSERT_BEFORE
393 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
394  (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
395  (elm)->field.tqe_next = (listelm); \
396  *(listelm)->field.tqe_prev = (elm); \
397  (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
398 } while (0)
399 #endif
400 
401 #define N_ACTIVE_CALLBACKS(base) \
402  ((base)->event_count_active)
403 
404 int evsig_set_handler_(struct event_base *base, int evsignal,
405  void (*fn)(int));
406 int evsig_restore_handler_(struct event_base *base, int evsignal);
407 
408 int event_add_nolock_(struct event *ev,
409  const struct timeval *tv, int tv_is_absolute);
412 #define EVENT_DEL_NOBLOCK 0
416 #define EVENT_DEL_BLOCK 1
420 #define EVENT_DEL_AUTOBLOCK 2
423 #define EVENT_DEL_EVEN_IF_FINALIZING 3
424 int event_del_nolock_(struct event *ev, int blocking);
425 int event_remove_timer_nolock_(struct event *ev);
426 
427 void event_active_nolock_(struct event *ev, int res, short count);
428 EVENT2_EXPORT_SYMBOL
429 int event_callback_activate_(struct event_base *, struct event_callback *);
430 int event_callback_activate_nolock_(struct event_base *, struct event_callback *);
431 int event_callback_cancel_(struct event_base *base,
432  struct event_callback *evcb);
433 
434 void event_callback_finalize_nolock_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *));
435 EVENT2_EXPORT_SYMBOL
436 void event_callback_finalize_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *));
437 int event_callback_finalize_many_(struct event_base *base, int n_cbs, struct event_callback **evcb, void (*cb)(struct event_callback *, void *));
438 
439 
440 EVENT2_EXPORT_SYMBOL
441 void event_active_later_(struct event *ev, int res);
442 void event_active_later_nolock_(struct event *ev, int res);
443 int event_callback_activate_later_nolock_(struct event_base *base,
444  struct event_callback *evcb);
445 int event_callback_cancel_nolock_(struct event_base *base,
446  struct event_callback *evcb, int even_if_finalizing);
447 void event_callback_init_(struct event_base *base,
448  struct event_callback *cb);
449 
450 /* FIXME document. */
451 EVENT2_EXPORT_SYMBOL
452 void event_base_add_virtual_(struct event_base *base);
453 void event_base_del_virtual_(struct event_base *base);
454 
461 EVENT2_EXPORT_SYMBOL
462 void event_base_assert_ok_(struct event_base *base);
463 void event_base_assert_ok_nolock_(struct event_base *base);
464 
465 
466 /* Helper function: Call 'fn' exactly once every inserted or active event in
467  * the event_base 'base'.
468  *
469  * If fn returns 0, continue on to the next event. Otherwise, return the same
470  * value that fn returned.
471  *
472  * Requires that 'base' be locked.
473  */
474 int event_base_foreach_event_nolock_(struct event_base *base,
475  event_base_foreach_event_cb cb, void *arg);
476 
477 /* Cleanup function to reset debug mode during shutdown.
478  *
479  * Calling this function doesn't mean it'll be possible to re-enable
480  * debug mode if any events were added.
481  */
482 void event_disable_debug_mode(void);
483 
484 #ifdef __cplusplus
485 }
486 #endif
487 
488 #endif /* EVENT_INTERNAL_H_INCLUDED_ */
int(* event_base_foreach_event_cb)(const struct event_base *, const struct event *, void *)
Definition: event.h:1578
event_base_config_flag
Definition: event.h:517
event_method_feature
Definition: event.h:489
Definition: event-internal.h:163
Definition: event-internal.h:208
enum event_base_config_flag flags
Definition: event-internal.h:323
LIST_HEAD(once_event_list, event_once) once_events
int n_common_timeouts_allocated
Definition: event-internal.h:279
int event_running_priority
Definition: event-internal.h:247
const struct eventop * evsigsel
Definition: event-internal.h:221
struct evsig_info sig
Definition: event-internal.h:223
struct event_callback * current_event
Definition: event-internal.h:315
int event_count_active_max
Definition: event-internal.h:236
struct event th_notify
Definition: event-internal.h:338
struct evcallback_list * activequeues
Definition: event-internal.h:264
int n_common_timeouts
Definition: event-internal.h:277
struct common_timeout_list ** common_timeout_queues
Definition: event-internal.h:275
int event_break
Definition: event-internal.h:242
unsigned long th_owner_id
Definition: event-internal.h:305
time_t last_updated_clock_diff
Definition: event-internal.h:300
struct timeval tv_clock_diff
Definition: event-internal.h:298
const struct eventop * evsel
Definition: event-internal.h:211
int virtual_event_count
Definition: event-internal.h:226
int is_notify_pending
Definition: event-internal.h:332
struct event_io_map io
Definition: event-internal.h:282
struct event_changelist changelist
Definition: event-internal.h:217
void * current_event_cond
Definition: event-internal.h:310
int event_count_active
Definition: event-internal.h:234
struct evcallback_list active_later_queue
Definition: event-internal.h:269
int nactivequeues
Definition: event-internal.h:266
evutil_socket_t th_notify_fd[2]
Definition: event-internal.h:335
int event_continue
Definition: event-internal.h:244
int running_loop
Definition: event-internal.h:251
void * evbase
Definition: event-internal.h:213
int event_count_max
Definition: event-internal.h:232
int event_gotterm
Definition: event-internal.h:240
int event_count
Definition: event-internal.h:230
struct event_signal_map sigmap
Definition: event-internal.h:285
int(* th_notify_fn)(struct event_base *base)
Definition: event-internal.h:340
struct timeval tv_cache
Definition: event-internal.h:292
struct min_heap timeheap
Definition: event-internal.h:288
int n_deferreds_queued
Definition: event-internal.h:257
void * th_base_lock
Definition: event-internal.h:307
int current_event_waiters
Definition: event-internal.h:312
struct evutil_weakrand_state weakrand_seed
Definition: event-internal.h:344
int virtual_event_count_max
Definition: event-internal.h:228
Definition: event_struct.h:107
Definition: changelist-internal.h:53
Definition: event-internal.h:183
Definition: event-internal.h:351
Definition: event-internal.h:359
Definition: event-internal.h:200
Definition: event-internal.h:151
Definition: event_struct.h:123
Definition: event-internal.h:86
enum event_method_feature features
Definition: event-internal.h:119
int(* del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo)
Definition: event-internal.h:105
int(* dispatch)(struct event_base *, struct timeval *)
Definition: event-internal.h:111
int need_reinit
Definition: event-internal.h:116
size_t fdinfo_len
Definition: event-internal.h:125
void(* dealloc)(struct event_base *)
Definition: event-internal.h:113
const char * name
Definition: event-internal.h:88
int(* add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo)
Definition: event-internal.h:103
Definition: evsignal-internal.h:39
Definition: time-internal.h:64
Definition: util-internal.h:315
Definition: minheap-internal.h:40
#define evutil_socket_t
Definition: util.h:310