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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * 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 #ifndef HAVE_EVENT_BASE_FREE
58 #define HAVE_EVENT_BASE_FREE
62 #define EV_TIMEOUT 0x01
63 /** event fd readable */
65 /** event fd writable */
68 #define EV_SIGNAL 0x08
69 /** event must persist */
70 #define EV_PERSIST 0x10
72 /* needs our redblack tree */
75 /** max number of file descriptors to support */
77 /** max number of signals to support */
83 /** sorted by timeout (absolute), ptr */
85 /** array of 0 - maxfd of ptr to event for it */
89 /** capacity - size of the fds array */
91 /* fdset for read write, for fds ready, and added */
93 /** fds for reading */
95 /** fds for writing */
97 /** fds determined ready for use */
99 /** ready plus newly added events. */
101 /** array of 0 - maxsig of ptr to event for it */
102 struct event** signals;
103 /** if we need to exit */
105 /** where to store time in seconds */
107 /** where to store time in microseconds */
108 struct timeval* time_tv;
112 * Event structure. Has some of the event elements.
115 /** node in timeout rbtree */
117 /** is event already added */
120 /** event base it belongs to */
121 struct event_base *ev_base;
122 /** fd to poll or -1 for timeouts. signal number for sigs. */
124 /** what events this event is interested in, see EV_.. above. */
127 struct timeval ev_timeout;
129 /** callback to call: fd, eventbits, userarg */
130 void (*ev_callback)(int, short, void *arg);
131 /** callback user arg */
135 /* function prototypes (some are as they appear in event.h) */
136 /** create event base */
137 void *event_init(uint32_t* time_secs, struct timeval* time_tv);
139 const char *event_get_version(void);
140 /** get polling method, select */
141 const char *event_get_method(void);
142 /** run select in a loop */
143 int event_base_dispatch(struct event_base *);
144 /** exit that loop */
145 int event_base_loopexit(struct event_base *, struct timeval *);
146 /** free event base. Free events yourself */
147 void event_base_free(struct event_base *);
148 /** set content of event */
149 void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
150 /** add event to a base. You *must* call this for every event. */
151 int event_base_set(struct event_base *, struct event *);
152 /** add event to make it active. You may not change it with event_set anymore */
153 int event_add(struct event *, struct timeval *);
154 /** remove event. You may change it again */
155 int event_del(struct event *);
158 #define evtimer_add(ev, tv) event_add(ev, tv)
159 /** remove a timer */
160 #define evtimer_del(ev) event_del(ev)
162 /* uses different implementation. Cannot mix fd/timeouts and signals inside
163 * the same struct event. create several event structs for that. */
164 /** install signal handler */
165 int signal_add(struct event *, struct timeval *);
166 /** set signal event contents */
167 #define signal_set(ev, x, cb, arg) \
168 event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
169 /** remove signal handler */
170 int signal_del(struct event *);
172 #endif /* USE_MINI_EVENT and not USE_WINSOCK */
174 /** compare events in tree, based on timevalue, ptr for uniqueness */
175 int mini_ev_cmp(const void* a, const void* b);
177 #endif /* MINI_EVENT_H */