/*1* Copyright (c) 2000-2007 Niels Provos <[email protected]>2* Copyright (c) 2007-2012 Niels Provos and Nick Mathewson3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. The name of the author may not be used to endorse or promote products13* derived from this software without specific prior written permission.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT20* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,21* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY22* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF24* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/26#ifndef EVENT_INTERNAL_H_INCLUDED_27#define EVENT_INTERNAL_H_INCLUDED_2829#ifdef __cplusplus30extern "C" {31#endif3233#include "event2/event-config.h"34#include "evconfig-private.h"3536#include <time.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"4344/* map union members back */4546/* mutually exclusive */47#define ev_signal_next ev_.ev_signal.ev_signal_next48#define ev_io_next ev_.ev_io.ev_io_next49#define ev_io_timeout ev_.ev_io.ev_timeout5051/* used only by signals */52#define ev_ncalls ev_.ev_signal.ev_ncalls53#define ev_pncalls ev_.ev_signal.ev_pncalls5455#define ev_pri ev_evcallback.evcb_pri56#define ev_flags ev_evcallback.evcb_flags57#define ev_closure ev_evcallback.evcb_closure58#define ev_callback ev_evcallback.evcb_cb_union.evcb_callback59#define ev_arg ev_evcallback.evcb_arg6061/** @name Event closure codes6263Possible values for evcb_closure in struct event_callback6465@{66*/67/** A regular event. Uses the evcb_callback callback */68#define EV_CLOSURE_EVENT 069/** A signal event. Uses the evcb_callback callback */70#define EV_CLOSURE_EVENT_SIGNAL 171/** A persistent non-signal event. Uses the evcb_callback callback */72#define EV_CLOSURE_EVENT_PERSIST 273/** A simple callback. Uses the evcb_selfcb callback. */74#define EV_CLOSURE_CB_SELF 375/** A finalizing callback. Uses the evcb_cbfinalize callback. */76#define EV_CLOSURE_CB_FINALIZE 477/** A finalizing event. Uses the evcb_evfinalize callback. */78#define EV_CLOSURE_EVENT_FINALIZE 579/** A finalizing event that should get freed after. Uses the evcb_evfinalize80* callback. */81#define EV_CLOSURE_EVENT_FINALIZE_FREE 682/** @} */8384/** Structure to define the backend of a given event_base. */85struct eventop {86/** The name of this backend. */87const char *name;88/** Function to set up an event_base to use this backend. It should89* create a new structure holding whatever information is needed to90* run the backend, and return it. The returned pointer will get91* stored by event_init into the event_base.evbase field. On failure,92* this function should return NULL. */93void *(*init)(struct event_base *);94/** Enable reading/writing on a given fd or signal. 'events' will be95* the events that we're trying to enable: one or more of EV_READ,96* EV_WRITE, EV_SIGNAL, and EV_ET. 'old' will be those events that97* were enabled on this fd previously. 'fdinfo' will be a structure98* associated with the fd by the evmap; its size is defined by the99* fdinfo field below. It will be set to 0 the first time the fd is100* added. The function should return 0 on success and -1 on error.101*/102int (*add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);103/** As "add", except 'events' contains the events we mean to disable. */104int (*del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);105/** Function to implement the core of an event loop. It must see which106added events are ready, and cause event_active to be called for each107active event (usually via event_io_active or such). It should108return 0 on success and -1 on error.109*/110int (*dispatch)(struct event_base *, struct timeval *);111/** Function to clean up and free our data from the event_base. */112void (*dealloc)(struct event_base *);113/** Flag: set if we need to reinitialize the event base after we fork.114*/115int need_reinit;116/** Bit-array of supported event_method_features that this backend can117* provide. */118enum event_method_feature features;119/** Length of the extra information we should record for each fd that120has one or more active events. This information is recorded121as part of the evmap entry for each fd, and passed as an argument122to the add and del functions above.123*/124size_t fdinfo_len;125};126127#ifdef _WIN32128/* If we're on win32, then file descriptors are not nice low densely packed129integers. Instead, they are pointer-like windows handles, and we want to130use a hashtable instead of an array to map fds to events.131*/132#define EVMAP_USE_HT133#endif134135/* #define HT_CACHE_HASH_VALS */136137#ifdef EVMAP_USE_HT138#define HT_NO_CACHE_HASH_VALUES139#include "ht-internal.h"140struct event_map_entry;141HT_HEAD(event_io_map, event_map_entry);142#else143#define event_io_map event_signal_map144#endif145146/* Used to map signal numbers to a list of events. If EVMAP_USE_HT is not147defined, this structure is also used as event_io_map, which maps fds to a148list of events.149*/150struct event_signal_map {151/* An array of evmap_io * or of evmap_signal *; empty entries are152* set to NULL. */153void **entries;154/* The number of entries available in entries */155int nentries;156};157158/* A list of events waiting on a given 'common' timeout value. Ordinarily,159* events waiting for a timeout wait on a minheap. Sometimes, however, a160* queue can be faster.161**/162struct common_timeout_list {163/* List of events currently waiting in the queue. */164struct event_list events;165/* 'magic' timeval used to indicate the duration of events in this166* queue. */167struct timeval duration;168/* Event that triggers whenever one of the events in the queue is169* ready to activate */170struct event timeout_event;171/* The event_base that this timeout list is part of */172struct event_base *base;173};174175/** Mask used to get the real tv_usec value from a common timeout. */176#define COMMON_TIMEOUT_MICROSECONDS_MASK 0x000fffff177178struct event_change;179180/* List of 'changes' since the last call to eventop.dispatch. Only maintained181* if the backend is using changesets. */182struct event_changelist {183struct event_change *changes;184int n_changes;185int changes_size;186};187188#ifndef EVENT__DISABLE_DEBUG_MODE189/* Global internal flag: set to one if debug mode is on. */190extern int event_debug_mode_on_;191#define EVENT_DEBUG_MODE_IS_ON() (event_debug_mode_on_)192#else193#define EVENT_DEBUG_MODE_IS_ON() (0)194#endif195196TAILQ_HEAD(evcallback_list, event_callback);197198/* Sets up an event for processing once */199struct event_once {200LIST_ENTRY(event_once) next_once;201struct event ev;202203void (*cb)(evutil_socket_t, short, void *);204void *arg;205};206207struct event_base {208/** Function pointers and other data to describe this event_base's209* backend. */210const struct eventop *evsel;211/** Pointer to backend-specific data. */212void *evbase;213214/** List of changes to tell backend about at next dispatch. Only used215* by the O(1) backends. */216struct event_changelist changelist;217218/** Function pointers used to describe the backend that this event_base219* uses for signals */220const struct eventop *evsigsel;221/** Data to implement the common signal handler code. */222struct evsig_info sig;223224/** Number of virtual events */225int virtual_event_count;226/** Maximum number of virtual events active */227int virtual_event_count_max;228/** Number of total events added to this event_base */229int event_count;230/** Maximum number of total events added to this event_base */231int event_count_max;232/** Number of total events active in this event_base */233int event_count_active;234/** Maximum number of total events active in this event_base */235int event_count_active_max;236237/** Set if we should terminate the loop once we're done processing238* events. */239int event_gotterm;240/** Set if we should terminate the loop immediately */241int event_break;242/** Set if we should start a new instance of the loop immediately. */243int event_continue;244245/** The currently running priority of events */246int event_running_priority;247248/** Set if we're running the event_base_loop function, to prevent249* reentrant invocation. */250int running_loop;251252/** Set to the number of deferred_cbs we've made 'active' in the253* loop. This is a hack to prevent starvation; it would be smarter254* to just use event_config_set_max_dispatch_interval's max_callbacks255* feature */256int n_deferreds_queued;257258/* Active event management. */259/** An array of nactivequeues queues for active event_callbacks (ones260* that have triggered, and whose callbacks need to be called). Low261* priority numbers are more important, and stall higher ones.262*/263struct evcallback_list *activequeues;264/** The length of the activequeues array */265int nactivequeues;266/** A list of event_callbacks that should become active the next time267* we process events, but not this time. */268struct evcallback_list active_later_queue;269270/* common timeout logic */271272/** An array of common_timeout_list* for all of the common timeout273* values we know. */274struct common_timeout_list **common_timeout_queues;275/** The number of entries used in common_timeout_queues */276int n_common_timeouts;277/** The total size of common_timeout_queues. */278int n_common_timeouts_allocated;279280/** Mapping from file descriptors to enabled (added) events */281struct event_io_map io;282283/** Mapping from signal numbers to enabled (added) events. */284struct event_signal_map sigmap;285286/** Priority queue of events with timeouts. */287struct min_heap timeheap;288289/** Stored timeval: used to avoid calling gettimeofday/clock_gettime290* too often. */291struct timeval tv_cache;292293struct evutil_monotonic_timer monotonic_timer;294295/** Difference between internal time (maybe from clock_gettime) and296* gettimeofday. */297struct timeval tv_clock_diff;298/** Second in which we last updated tv_clock_diff, in monotonic time. */299time_t last_updated_clock_diff;300301#ifndef EVENT__DISABLE_THREAD_SUPPORT302/* threading support */303/** The thread currently running the event_loop for this base */304unsigned long th_owner_id;305/** A lock to prevent conflicting accesses to this event_base */306void *th_base_lock;307/** A condition that gets signalled when we're done processing an308* event with waiters on it. */309void *current_event_cond;310/** Number of threads blocking on current_event_cond. */311int current_event_waiters;312#endif313/** The event whose callback is executing right now */314struct event_callback *current_event;315316#ifdef _WIN32317/** IOCP support structure, if IOCP is enabled. */318struct event_iocp_port *iocp;319#endif320321/** Flags that this base was configured with */322enum event_base_config_flag flags;323324struct timeval max_dispatch_time;325int max_dispatch_callbacks;326int limit_callbacks_after_prio;327328/* Notify main thread to wake up break, etc. */329/** True if the base already has a pending notify, and we don't need330* to add any more. */331int is_notify_pending;332/** A socketpair used by some th_notify functions to wake up the main333* thread. */334evutil_socket_t th_notify_fd[2];335/** An event used by some th_notify functions to wake up the main336* thread. */337struct event th_notify;338/** A function used to wake up the main thread from another thread. */339int (*th_notify_fn)(struct event_base *base);340341/** Saved seed for weak random number generator. Some backends use342* this to produce fairness among sockets. Protected by th_base_lock. */343struct evutil_weakrand_state weakrand_seed;344345/** List of event_onces that have not yet fired. */346LIST_HEAD(once_event_list, event_once) once_events;347348};349350struct event_config_entry {351TAILQ_ENTRY(event_config_entry) next;352353const char *avoid_method;354};355356/** Internal structure: describes the configuration we want for an event_base357* that we're about to allocate. */358struct event_config {359TAILQ_HEAD(event_configq, event_config_entry) entries;360361int n_cpus_hint;362struct timeval max_dispatch_interval;363int max_dispatch_callbacks;364int limit_callbacks_after_prio;365enum event_method_feature require_features;366enum event_base_config_flag flags;367};368369/* Internal use only: Functions that might be missing from <sys/queue.h> */370#ifndef LIST_END371#define LIST_END(head) NULL372#endif373374#ifndef TAILQ_FIRST375#define TAILQ_FIRST(head) ((head)->tqh_first)376#endif377#ifndef TAILQ_END378#define TAILQ_END(head) NULL379#endif380#ifndef TAILQ_NEXT381#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)382#endif383384#ifndef TAILQ_FOREACH385#define TAILQ_FOREACH(var, head, field) \386for ((var) = TAILQ_FIRST(head); \387(var) != TAILQ_END(head); \388(var) = TAILQ_NEXT(var, field))389#endif390391#ifndef TAILQ_INSERT_BEFORE392#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \393(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \394(elm)->field.tqe_next = (listelm); \395*(listelm)->field.tqe_prev = (elm); \396(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \397} while (0)398#endif399400#define N_ACTIVE_CALLBACKS(base) \401((base)->event_count_active)402403int evsig_set_handler_(struct event_base *base, int evsignal,404void (*fn)(int));405int evsig_restore_handler_(struct event_base *base, int evsignal);406407int event_add_nolock_(struct event *ev,408const struct timeval *tv, int tv_is_absolute);409/** Argument for event_del_nolock_. Tells event_del not to block on the event410* if it's running in another thread. */411#define EVENT_DEL_NOBLOCK 0412/** Argument for event_del_nolock_. Tells event_del to block on the event413* if it's running in another thread, regardless of its value for EV_FINALIZE414*/415#define EVENT_DEL_BLOCK 1416/** Argument for event_del_nolock_. Tells event_del to block on the event417* if it is running in another thread and it doesn't have EV_FINALIZE set.418*/419#define EVENT_DEL_AUTOBLOCK 2420/** Argument for event_del_nolock_. Tells event_del to proceed even if the421* event is set up for finalization rather for regular use.*/422#define EVENT_DEL_EVEN_IF_FINALIZING 3423int event_del_nolock_(struct event *ev, int blocking);424int event_remove_timer_nolock_(struct event *ev);425426void event_active_nolock_(struct event *ev, int res, short count);427EVENT2_EXPORT_SYMBOL428int event_callback_activate_(struct event_base *, struct event_callback *);429int event_callback_activate_nolock_(struct event_base *, struct event_callback *);430int event_callback_cancel_(struct event_base *base,431struct event_callback *evcb);432433void event_callback_finalize_nolock_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *));434EVENT2_EXPORT_SYMBOL435void event_callback_finalize_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *));436int event_callback_finalize_many_(struct event_base *base, int n_cbs, struct event_callback **evcb, void (*cb)(struct event_callback *, void *));437438439EVENT2_EXPORT_SYMBOL440void event_active_later_(struct event *ev, int res);441void event_active_later_nolock_(struct event *ev, int res);442int event_callback_activate_later_nolock_(struct event_base *base,443struct event_callback *evcb);444int event_callback_cancel_nolock_(struct event_base *base,445struct event_callback *evcb, int even_if_finalizing);446void event_callback_init_(struct event_base *base,447struct event_callback *cb);448449/* FIXME document. */450EVENT2_EXPORT_SYMBOL451void event_base_add_virtual_(struct event_base *base);452void event_base_del_virtual_(struct event_base *base);453454/** For debugging: unless assertions are disabled, verify the referential455integrity of the internal data structures of 'base'. This operation can456be expensive.457458Returns on success; aborts on failure.459*/460EVENT2_EXPORT_SYMBOL461void event_base_assert_ok_(struct event_base *base);462void event_base_assert_ok_nolock_(struct event_base *base);463464465/* Helper function: Call 'fn' exactly once every inserted or active event in466* the event_base 'base'.467*468* If fn returns 0, continue on to the next event. Otherwise, return the same469* value that fn returned.470*471* Requires that 'base' be locked.472*/473int event_base_foreach_event_nolock_(struct event_base *base,474event_base_foreach_event_cb cb, void *arg);475476/* Cleanup function to reset debug mode during shutdown.477*478* Calling this function doesn't mean it'll be possible to re-enable479* debug mode if any events were added.480*/481void event_disable_debug_mode(void);482483#ifdef __cplusplus484}485#endif486487#endif /* EVENT_INTERNAL_H_INCLUDED_ */488489490