xref: /freebsd/contrib/unbound/util/mini_event.h (revision c0caa2e24e9a8c64aa01e4265e8b989ba74b9702)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * mini-event.h - micro implementation of libevent api, using select() only.
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  * This file implements part of the event(3) libevent api.
39b7579f77SDag-Erling Smørgrav  * The back end is only select. Max number of fds is limited.
40b7579f77SDag-Erling Smørgrav  * Max number of signals is limited, one handler per signal only.
41b7579f77SDag-Erling Smørgrav  * And one handler per fd.
42b7579f77SDag-Erling Smørgrav  *
43b7579f77SDag-Erling Smørgrav  * Although limited to select() and a max (1024) open fds, it
44b7579f77SDag-Erling Smørgrav  * is efficient:
45b7579f77SDag-Erling Smørgrav  * o dispatch call caches fd_sets to use.
46b7579f77SDag-Erling Smørgrav  * o handler calling takes time ~ to the number of fds.
47b7579f77SDag-Erling Smørgrav  * o timeouts are stored in a redblack tree, sorted, so take log(n).
48b7579f77SDag-Erling Smørgrav  * Timeouts are only accurate to the second (no subsecond accuracy).
49b7579f77SDag-Erling Smørgrav  * To avoid cpu hogging, fractional timeouts are rounded up to a whole second.
50b7579f77SDag-Erling Smørgrav  */
51b7579f77SDag-Erling Smørgrav 
52b7579f77SDag-Erling Smørgrav #ifndef MINI_EVENT_H
53b7579f77SDag-Erling Smørgrav #define MINI_EVENT_H
54b7579f77SDag-Erling Smørgrav 
55b7579f77SDag-Erling Smørgrav #if defined(USE_MINI_EVENT) && !defined(USE_WINSOCK)
56b7579f77SDag-Erling Smørgrav 
57*c0caa2e2SCy Schubert #ifdef	HAVE_SYS_SELECT_H
58*c0caa2e2SCy Schubert /* for fd_set on OpenBSD */
59*c0caa2e2SCy Schubert #include <sys/select.h>
60*c0caa2e2SCy Schubert #endif
6125039b37SCy Schubert #include <sys/time.h>
6225039b37SCy Schubert 
63b7579f77SDag-Erling Smørgrav #ifndef HAVE_EVENT_BASE_FREE
64b7579f77SDag-Erling Smørgrav #define HAVE_EVENT_BASE_FREE
65b7579f77SDag-Erling Smørgrav #endif
66b7579f77SDag-Erling Smørgrav 
67ff825849SDag-Erling Smørgrav /* redefine to use our own namespace so that on platforms where
68ff825849SDag-Erling Smørgrav  * linkers crosslink library-private symbols with other symbols, it works */
69ff825849SDag-Erling Smørgrav #define event_init minievent_init
70ff825849SDag-Erling Smørgrav #define event_get_version minievent_get_version
71ff825849SDag-Erling Smørgrav #define event_get_method minievent_get_method
72ff825849SDag-Erling Smørgrav #define event_base_dispatch minievent_base_dispatch
73ff825849SDag-Erling Smørgrav #define event_base_loopexit minievent_base_loopexit
74ff825849SDag-Erling Smørgrav #define event_base_free minievent_base_free
75ff825849SDag-Erling Smørgrav #define event_set minievent_set
76ff825849SDag-Erling Smørgrav #define event_base_set minievent_base_set
77ff825849SDag-Erling Smørgrav #define event_add minievent_add
78ff825849SDag-Erling Smørgrav #define event_del minievent_del
79ff825849SDag-Erling Smørgrav #define signal_add minisignal_add
80ff825849SDag-Erling Smørgrav #define signal_del minisignal_del
81ff825849SDag-Erling Smørgrav 
82b7579f77SDag-Erling Smørgrav /** event timeout */
83b7579f77SDag-Erling Smørgrav #define EV_TIMEOUT	0x01
84b7579f77SDag-Erling Smørgrav /** event fd readable */
85b7579f77SDag-Erling Smørgrav #define EV_READ		0x02
86b7579f77SDag-Erling Smørgrav /** event fd writable */
87b7579f77SDag-Erling Smørgrav #define EV_WRITE	0x04
88b7579f77SDag-Erling Smørgrav /** event signal */
89b7579f77SDag-Erling Smørgrav #define EV_SIGNAL	0x08
90b7579f77SDag-Erling Smørgrav /** event must persist */
91b7579f77SDag-Erling Smørgrav #define EV_PERSIST	0x10
92b7579f77SDag-Erling Smørgrav 
93b7579f77SDag-Erling Smørgrav /* needs our redblack tree */
94b7579f77SDag-Erling Smørgrav #include "rbtree.h"
95b7579f77SDag-Erling Smørgrav 
96b7579f77SDag-Erling Smørgrav /** max number of file descriptors to support */
97b7579f77SDag-Erling Smørgrav #define MAX_FDS 1024
98b7579f77SDag-Erling Smørgrav /** max number of signals to support */
99b7579f77SDag-Erling Smørgrav #define MAX_SIG 32
100b7579f77SDag-Erling Smørgrav 
101b7579f77SDag-Erling Smørgrav /** event base */
102b7579f77SDag-Erling Smørgrav struct event_base
103b7579f77SDag-Erling Smørgrav {
104b7579f77SDag-Erling Smørgrav 	/** sorted by timeout (absolute), ptr */
1053005e0a3SDag-Erling Smørgrav 	rbtree_type* times;
106b7579f77SDag-Erling Smørgrav 	/** array of 0 - maxfd of ptr to event for it */
107b7579f77SDag-Erling Smørgrav 	struct event** fds;
108b7579f77SDag-Erling Smørgrav 	/** max fd in use */
109b7579f77SDag-Erling Smørgrav 	int maxfd;
110b7579f77SDag-Erling Smørgrav 	/** capacity - size of the fds array */
111b7579f77SDag-Erling Smørgrav 	int capfd;
112b7579f77SDag-Erling Smørgrav 	/* fdset for read write, for fds ready, and added */
113b7579f77SDag-Erling Smørgrav 	fd_set
114b7579f77SDag-Erling Smørgrav 		/** fds for reading */
115b7579f77SDag-Erling Smørgrav 		reads,
116b7579f77SDag-Erling Smørgrav 		/** fds for writing */
117b7579f77SDag-Erling Smørgrav 		writes,
118b7579f77SDag-Erling Smørgrav 		/** fds determined ready for use */
119b7579f77SDag-Erling Smørgrav 		ready,
120b7579f77SDag-Erling Smørgrav 		/** ready plus newly added events. */
121b7579f77SDag-Erling Smørgrav 		content;
122b7579f77SDag-Erling Smørgrav 	/** array of 0 - maxsig of ptr to event for it */
123b7579f77SDag-Erling Smørgrav 	struct event** signals;
124b7579f77SDag-Erling Smørgrav 	/** if we need to exit */
125b7579f77SDag-Erling Smørgrav 	int need_to_exit;
126b7579f77SDag-Erling Smørgrav 	/** where to store time in seconds */
12717d15b25SDag-Erling Smørgrav 	time_t* time_secs;
128b7579f77SDag-Erling Smørgrav 	/** where to store time in microseconds */
129b7579f77SDag-Erling Smørgrav 	struct timeval* time_tv;
130b7579f77SDag-Erling Smørgrav };
131b7579f77SDag-Erling Smørgrav 
132b7579f77SDag-Erling Smørgrav /**
133b7579f77SDag-Erling Smørgrav  * Event structure. Has some of the event elements.
134b7579f77SDag-Erling Smørgrav  */
135b7579f77SDag-Erling Smørgrav struct event {
136b7579f77SDag-Erling Smørgrav 	/** node in timeout rbtree */
1373005e0a3SDag-Erling Smørgrav 	rbnode_type node;
138b7579f77SDag-Erling Smørgrav 	/** is event already added */
139b7579f77SDag-Erling Smørgrav 	int added;
140b7579f77SDag-Erling Smørgrav 
141b7579f77SDag-Erling Smørgrav 	/** event base it belongs to */
142b7579f77SDag-Erling Smørgrav 	struct event_base *ev_base;
143b7579f77SDag-Erling Smørgrav 	/** fd to poll or -1 for timeouts. signal number for sigs. */
144b7579f77SDag-Erling Smørgrav 	int ev_fd;
145b7579f77SDag-Erling Smørgrav 	/** what events this event is interested in, see EV_.. above. */
146b7579f77SDag-Erling Smørgrav 	short ev_events;
147b7579f77SDag-Erling Smørgrav 	/** timeout value */
148b7579f77SDag-Erling Smørgrav 	struct timeval ev_timeout;
149b7579f77SDag-Erling Smørgrav 
150b7579f77SDag-Erling Smørgrav 	/** callback to call: fd, eventbits, userarg */
151b7579f77SDag-Erling Smørgrav 	void (*ev_callback)(int, short, void *arg);
152b7579f77SDag-Erling Smørgrav 	/** callback user arg */
153b7579f77SDag-Erling Smørgrav 	void *ev_arg;
154b7579f77SDag-Erling Smørgrav };
155b7579f77SDag-Erling Smørgrav 
156b7579f77SDag-Erling Smørgrav /* function prototypes (some are as they appear in event.h) */
157b7579f77SDag-Erling Smørgrav /** create event base */
15817d15b25SDag-Erling Smørgrav void *event_init(time_t* time_secs, struct timeval* time_tv);
159b7579f77SDag-Erling Smørgrav /** get version */
160b7579f77SDag-Erling Smørgrav const char *event_get_version(void);
161b7579f77SDag-Erling Smørgrav /** get polling method, select */
162b7579f77SDag-Erling Smørgrav const char *event_get_method(void);
163b7579f77SDag-Erling Smørgrav /** run select in a loop */
164b7579f77SDag-Erling Smørgrav int event_base_dispatch(struct event_base *);
165b7579f77SDag-Erling Smørgrav /** exit that loop */
166b7579f77SDag-Erling Smørgrav int event_base_loopexit(struct event_base *, struct timeval *);
167b7579f77SDag-Erling Smørgrav /** free event base. Free events yourself */
168b7579f77SDag-Erling Smørgrav void event_base_free(struct event_base *);
169b7579f77SDag-Erling Smørgrav /** set content of event */
170b7579f77SDag-Erling Smørgrav void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
171b7579f77SDag-Erling Smørgrav /** add event to a base. You *must* call this for every event. */
172b7579f77SDag-Erling Smørgrav int event_base_set(struct event_base *, struct event *);
173b7579f77SDag-Erling Smørgrav /** add event to make it active. You may not change it with event_set anymore */
174b7579f77SDag-Erling Smørgrav int event_add(struct event *, struct timeval *);
175b7579f77SDag-Erling Smørgrav /** remove event. You may change it again */
176b7579f77SDag-Erling Smørgrav int event_del(struct event *);
177b7579f77SDag-Erling Smørgrav 
178b7579f77SDag-Erling Smørgrav /** add a timer */
179b7579f77SDag-Erling Smørgrav #define evtimer_add(ev, tv)             event_add(ev, tv)
180b7579f77SDag-Erling Smørgrav /** remove a timer */
181b7579f77SDag-Erling Smørgrav #define evtimer_del(ev)                 event_del(ev)
182b7579f77SDag-Erling Smørgrav 
183b7579f77SDag-Erling Smørgrav /* uses different implementation. Cannot mix fd/timeouts and signals inside
184b7579f77SDag-Erling Smørgrav  * the same struct event. create several event structs for that.  */
185b7579f77SDag-Erling Smørgrav /** install signal handler */
186b7579f77SDag-Erling Smørgrav int signal_add(struct event *, struct timeval *);
187b7579f77SDag-Erling Smørgrav /** set signal event contents */
188b7579f77SDag-Erling Smørgrav #define signal_set(ev, x, cb, arg)      \
189b7579f77SDag-Erling Smørgrav         event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
190b7579f77SDag-Erling Smørgrav /** remove signal handler */
191b7579f77SDag-Erling Smørgrav int signal_del(struct event *);
192b7579f77SDag-Erling Smørgrav 
193b7579f77SDag-Erling Smørgrav #endif /* USE_MINI_EVENT and not USE_WINSOCK */
194b7579f77SDag-Erling Smørgrav 
195b7579f77SDag-Erling Smørgrav /** compare events in tree, based on timevalue, ptr for uniqueness */
196b7579f77SDag-Erling Smørgrav int mini_ev_cmp(const void* a, const void* b);
197b7579f77SDag-Erling Smørgrav 
198b7579f77SDag-Erling Smørgrav #endif /* MINI_EVENT_H */
199