xref: /freebsd/contrib/unbound/util/mini_event.h (revision 17d15b251108ff09e632ed4812f7c66999a6b69b)
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
24*17d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*17d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*17d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*17d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*17d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*17d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*17d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*17d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*17d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*17d15b25SDag-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 
57b7579f77SDag-Erling Smørgrav #ifndef HAVE_EVENT_BASE_FREE
58b7579f77SDag-Erling Smørgrav #define HAVE_EVENT_BASE_FREE
59b7579f77SDag-Erling Smørgrav #endif
60b7579f77SDag-Erling Smørgrav 
61b7579f77SDag-Erling Smørgrav /** event timeout */
62b7579f77SDag-Erling Smørgrav #define EV_TIMEOUT	0x01
63b7579f77SDag-Erling Smørgrav /** event fd readable */
64b7579f77SDag-Erling Smørgrav #define EV_READ		0x02
65b7579f77SDag-Erling Smørgrav /** event fd writable */
66b7579f77SDag-Erling Smørgrav #define EV_WRITE	0x04
67b7579f77SDag-Erling Smørgrav /** event signal */
68b7579f77SDag-Erling Smørgrav #define EV_SIGNAL	0x08
69b7579f77SDag-Erling Smørgrav /** event must persist */
70b7579f77SDag-Erling Smørgrav #define EV_PERSIST	0x10
71b7579f77SDag-Erling Smørgrav 
72b7579f77SDag-Erling Smørgrav /* needs our redblack tree */
73b7579f77SDag-Erling Smørgrav #include "rbtree.h"
74b7579f77SDag-Erling Smørgrav 
75b7579f77SDag-Erling Smørgrav /** max number of file descriptors to support */
76b7579f77SDag-Erling Smørgrav #define MAX_FDS 1024
77b7579f77SDag-Erling Smørgrav /** max number of signals to support */
78b7579f77SDag-Erling Smørgrav #define MAX_SIG 32
79b7579f77SDag-Erling Smørgrav 
80b7579f77SDag-Erling Smørgrav /** event base */
81b7579f77SDag-Erling Smørgrav struct event_base
82b7579f77SDag-Erling Smørgrav {
83b7579f77SDag-Erling Smørgrav 	/** sorted by timeout (absolute), ptr */
84b7579f77SDag-Erling Smørgrav 	rbtree_t* times;
85b7579f77SDag-Erling Smørgrav 	/** array of 0 - maxfd of ptr to event for it */
86b7579f77SDag-Erling Smørgrav 	struct event** fds;
87b7579f77SDag-Erling Smørgrav 	/** max fd in use */
88b7579f77SDag-Erling Smørgrav 	int maxfd;
89b7579f77SDag-Erling Smørgrav 	/** capacity - size of the fds array */
90b7579f77SDag-Erling Smørgrav 	int capfd;
91b7579f77SDag-Erling Smørgrav 	/* fdset for read write, for fds ready, and added */
92b7579f77SDag-Erling Smørgrav 	fd_set
93b7579f77SDag-Erling Smørgrav 		/** fds for reading */
94b7579f77SDag-Erling Smørgrav 		reads,
95b7579f77SDag-Erling Smørgrav 		/** fds for writing */
96b7579f77SDag-Erling Smørgrav 		writes,
97b7579f77SDag-Erling Smørgrav 		/** fds determined ready for use */
98b7579f77SDag-Erling Smørgrav 		ready,
99b7579f77SDag-Erling Smørgrav 		/** ready plus newly added events. */
100b7579f77SDag-Erling Smørgrav 		content;
101b7579f77SDag-Erling Smørgrav 	/** array of 0 - maxsig of ptr to event for it */
102b7579f77SDag-Erling Smørgrav 	struct event** signals;
103b7579f77SDag-Erling Smørgrav 	/** if we need to exit */
104b7579f77SDag-Erling Smørgrav 	int need_to_exit;
105b7579f77SDag-Erling Smørgrav 	/** where to store time in seconds */
106*17d15b25SDag-Erling Smørgrav 	time_t* time_secs;
107b7579f77SDag-Erling Smørgrav 	/** where to store time in microseconds */
108b7579f77SDag-Erling Smørgrav 	struct timeval* time_tv;
109b7579f77SDag-Erling Smørgrav };
110b7579f77SDag-Erling Smørgrav 
111b7579f77SDag-Erling Smørgrav /**
112b7579f77SDag-Erling Smørgrav  * Event structure. Has some of the event elements.
113b7579f77SDag-Erling Smørgrav  */
114b7579f77SDag-Erling Smørgrav struct event {
115b7579f77SDag-Erling Smørgrav 	/** node in timeout rbtree */
116b7579f77SDag-Erling Smørgrav 	rbnode_t node;
117b7579f77SDag-Erling Smørgrav 	/** is event already added */
118b7579f77SDag-Erling Smørgrav 	int added;
119b7579f77SDag-Erling Smørgrav 
120b7579f77SDag-Erling Smørgrav 	/** event base it belongs to */
121b7579f77SDag-Erling Smørgrav 	struct event_base *ev_base;
122b7579f77SDag-Erling Smørgrav 	/** fd to poll or -1 for timeouts. signal number for sigs. */
123b7579f77SDag-Erling Smørgrav 	int ev_fd;
124b7579f77SDag-Erling Smørgrav 	/** what events this event is interested in, see EV_.. above. */
125b7579f77SDag-Erling Smørgrav 	short ev_events;
126b7579f77SDag-Erling Smørgrav 	/** timeout value */
127b7579f77SDag-Erling Smørgrav 	struct timeval ev_timeout;
128b7579f77SDag-Erling Smørgrav 
129b7579f77SDag-Erling Smørgrav 	/** callback to call: fd, eventbits, userarg */
130b7579f77SDag-Erling Smørgrav 	void (*ev_callback)(int, short, void *arg);
131b7579f77SDag-Erling Smørgrav 	/** callback user arg */
132b7579f77SDag-Erling Smørgrav 	void *ev_arg;
133b7579f77SDag-Erling Smørgrav };
134b7579f77SDag-Erling Smørgrav 
135b7579f77SDag-Erling Smørgrav /* function prototypes (some are as they appear in event.h) */
136b7579f77SDag-Erling Smørgrav /** create event base */
137*17d15b25SDag-Erling Smørgrav void *event_init(time_t* time_secs, struct timeval* time_tv);
138b7579f77SDag-Erling Smørgrav /** get version */
139b7579f77SDag-Erling Smørgrav const char *event_get_version(void);
140b7579f77SDag-Erling Smørgrav /** get polling method, select */
141b7579f77SDag-Erling Smørgrav const char *event_get_method(void);
142b7579f77SDag-Erling Smørgrav /** run select in a loop */
143b7579f77SDag-Erling Smørgrav int event_base_dispatch(struct event_base *);
144b7579f77SDag-Erling Smørgrav /** exit that loop */
145b7579f77SDag-Erling Smørgrav int event_base_loopexit(struct event_base *, struct timeval *);
146b7579f77SDag-Erling Smørgrav /** free event base. Free events yourself */
147b7579f77SDag-Erling Smørgrav void event_base_free(struct event_base *);
148b7579f77SDag-Erling Smørgrav /** set content of event */
149b7579f77SDag-Erling Smørgrav void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
150b7579f77SDag-Erling Smørgrav /** add event to a base. You *must* call this for every event. */
151b7579f77SDag-Erling Smørgrav int event_base_set(struct event_base *, struct event *);
152b7579f77SDag-Erling Smørgrav /** add event to make it active. You may not change it with event_set anymore */
153b7579f77SDag-Erling Smørgrav int event_add(struct event *, struct timeval *);
154b7579f77SDag-Erling Smørgrav /** remove event. You may change it again */
155b7579f77SDag-Erling Smørgrav int event_del(struct event *);
156b7579f77SDag-Erling Smørgrav 
157b7579f77SDag-Erling Smørgrav /** add a timer */
158b7579f77SDag-Erling Smørgrav #define evtimer_add(ev, tv)             event_add(ev, tv)
159b7579f77SDag-Erling Smørgrav /** remove a timer */
160b7579f77SDag-Erling Smørgrav #define evtimer_del(ev)                 event_del(ev)
161b7579f77SDag-Erling Smørgrav 
162b7579f77SDag-Erling Smørgrav /* uses different implementation. Cannot mix fd/timeouts and signals inside
163b7579f77SDag-Erling Smørgrav  * the same struct event. create several event structs for that.  */
164b7579f77SDag-Erling Smørgrav /** install signal handler */
165b7579f77SDag-Erling Smørgrav int signal_add(struct event *, struct timeval *);
166b7579f77SDag-Erling Smørgrav /** set signal event contents */
167b7579f77SDag-Erling Smørgrav #define signal_set(ev, x, cb, arg)      \
168b7579f77SDag-Erling Smørgrav         event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
169b7579f77SDag-Erling Smørgrav /** remove signal handler */
170b7579f77SDag-Erling Smørgrav int signal_del(struct event *);
171b7579f77SDag-Erling Smørgrav 
172b7579f77SDag-Erling Smørgrav #endif /* USE_MINI_EVENT and not USE_WINSOCK */
173b7579f77SDag-Erling Smørgrav 
174b7579f77SDag-Erling Smørgrav /** compare events in tree, based on timevalue, ptr for uniqueness */
175b7579f77SDag-Erling Smørgrav int mini_ev_cmp(const void* a, const void* b);
176b7579f77SDag-Erling Smørgrav 
177b7579f77SDag-Erling Smørgrav #endif /* MINI_EVENT_H */
178