2 * mini-event.h - micro implementation of libevent api, using select() only.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * This file implements part of the event(3) libevent api.
39 * The back end is only select. Max number of fds is limited.
40 * Max number of signals is limited, one handler per signal only.
41 * And one handler per fd.
43 * Although limited to select() and a max (1024) open fds, it
45 * o dispatch call caches fd_sets to use.
46 * o handler calling takes time ~ to the number of fds.
47 * o timeouts are stored in a redblack tree, sorted, so take log(n).
48 * Timeouts are only accurate to the second (no subsecond accuracy).
49 * To avoid cpu hogging, fractional timeouts are rounded up to a whole second.
55 #if defined(USE_MINI_EVENT) && !defined(USE_WINSOCK)
57 #ifdef HAVE_SYS_SELECT_H
58 /* for fd_set on OpenBSD */
59 #include <sys/select.h>
63 #ifndef HAVE_EVENT_BASE_FREE
64 #define HAVE_EVENT_BASE_FREE
67 /* redefine to use our own namespace so that on platforms where
68 * linkers crosslink library-private symbols with other symbols, it works */
69 #define event_init minievent_init
70 #define event_get_version minievent_get_version
71 #define event_get_method minievent_get_method
72 #define event_base_dispatch minievent_base_dispatch
73 #define event_base_loopexit minievent_base_loopexit
74 #define event_base_free minievent_base_free
75 #define event_set minievent_set
76 #define event_base_set minievent_base_set
77 #define event_add minievent_add
78 #define event_del minievent_del
79 #define signal_add minisignal_add
80 #define signal_del minisignal_del
83 #define EV_TIMEOUT 0x01
84 /** event fd readable */
86 /** event fd writable */
89 #define EV_SIGNAL 0x08
90 /** event must persist */
91 #define EV_PERSIST 0x10
93 /* needs our redblack tree */
96 /** max number of file descriptors to support */
98 /** max number of signals to support */
104 /** sorted by timeout (absolute), ptr */
106 /** array of 0 - maxfd of ptr to event for it */
110 /** capacity - size of the fds array */
112 /* fdset for read write, for fds ready, and added */
114 /** fds for reading */
116 /** fds for writing */
118 /** fds determined ready for use */
120 /** ready plus newly added events. */
122 /** array of 0 - maxsig of ptr to event for it */
123 struct event** signals;
124 /** if we need to exit */
126 /** where to store time in seconds */
128 /** where to store time in microseconds */
129 struct timeval* time_tv;
133 * Event structure. Has some of the event elements.
136 /** node in timeout rbtree */
138 /** is event already added */
141 /** event base it belongs to */
142 struct event_base *ev_base;
143 /** fd to poll or -1 for timeouts. signal number for sigs. */
145 /** what events this event is interested in, see EV_.. above. */
148 struct timeval ev_timeout;
150 /** callback to call: fd, eventbits, userarg */
151 void (*ev_callback)(int, short, void *arg);
152 /** callback user arg */
156 /* function prototypes (some are as they appear in event.h) */
157 /** create event base */
158 void *event_init(time_t* time_secs, struct timeval* time_tv);
160 const char *event_get_version(void);
161 /** get polling method, select */
162 const char *event_get_method(void);
163 /** run select in a loop */
164 int event_base_dispatch(struct event_base *);
165 /** exit that loop */
166 int event_base_loopexit(struct event_base *, struct timeval *);
167 /** free event base. Free events yourself */
168 void event_base_free(struct event_base *);
169 /** set content of event */
170 void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
171 /** add event to a base. You *must* call this for every event. */
172 int event_base_set(struct event_base *, struct event *);
173 /** add event to make it active. You may not change it with event_set anymore */
174 int event_add(struct event *, struct timeval *);
175 /** remove event. You may change it again */
176 int event_del(struct event *);
179 #define evtimer_add(ev, tv) event_add(ev, tv)
180 /** remove a timer */
181 #define evtimer_del(ev) event_del(ev)
183 /* uses different implementation. Cannot mix fd/timeouts and signals inside
184 * the same struct event. create several event structs for that. */
185 /** install signal handler */
186 int signal_add(struct event *, struct timeval *);
187 /** set signal event contents */
188 #define signal_set(ev, x, cb, arg) \
189 event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
190 /** remove signal handler */
191 int signal_del(struct event *);
193 #endif /* USE_MINI_EVENT and not USE_WINSOCK */
195 /** compare events in tree, based on timevalue, ptr for uniqueness */
196 int mini_ev_cmp(const void* a, const void* b);
198 #endif /* MINI_EVENT_H */