1 /* 2 * mini-event.h - micro implementation of libevent api, using select() only. 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 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. 18 * 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. 22 * 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. 34 */ 35 36 /** 37 * \file 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. 42 * 43 * Although limited to select() and a max (1024) open fds, it 44 * is efficient: 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. 50 */ 51 52 #ifndef MINI_EVENT_H 53 #define MINI_EVENT_H 54 struct region; 55 56 #if defined(USE_MINI_EVENT) && !defined(USE_WINSOCK) 57 58 #ifdef HAVE_SYS_SELECT_H 59 /* for fd_set on OpenBSD */ 60 #include <sys/select.h> 61 #endif 62 63 #ifndef HAVE_EVENT_BASE_FREE 64 #define HAVE_EVENT_BASE_FREE 65 #endif 66 67 /** event timeout */ 68 #define EV_TIMEOUT 0x01 69 /** event fd readable */ 70 #define EV_READ 0x02 71 /** event fd writable */ 72 #define EV_WRITE 0x04 73 /** event signal */ 74 #define EV_SIGNAL 0x08 75 /** event must persist */ 76 #define EV_PERSIST 0x10 77 78 /* needs our redblack tree */ 79 #include "rbtree.h" 80 81 /** max number of file descriptors to support */ 82 #define MAX_FDS 1024 83 /** max number of signals to support */ 84 #define MAX_SIG 32 85 86 /** event base */ 87 struct event_base 88 { 89 /** sorted by timeout (absolute), ptr */ 90 rbtree_type* times; 91 /** array of 0 - maxfd of ptr to event for it */ 92 struct event** fds; 93 /** max fd in use */ 94 int maxfd; 95 /** capacity - size of the fds array */ 96 int capfd; 97 /* fdset for read write, for fds ready, and added */ 98 fd_set 99 /** fds for reading */ 100 reads, 101 /** fds for writing */ 102 writes, 103 /** fds determined ready for use */ 104 ready, 105 /** ready plus newly added events. */ 106 content; 107 /** array of 0 - maxsig of ptr to event for it */ 108 struct event** signals; 109 /** if we need to exit */ 110 int need_to_exit; 111 /** where to store time in seconds */ 112 time_t* time_secs; 113 /** where to store time in microseconds */ 114 struct timeval* time_tv; 115 /** region for allocation */ 116 struct region* region; 117 }; 118 119 /** 120 * Event structure. Has some of the event elements. 121 */ 122 struct event { 123 /** node in timeout rbtree */ 124 rbnode_type node; 125 /** is event already added */ 126 int added; 127 128 /** event base it belongs to */ 129 struct event_base *ev_base; 130 /** fd to poll or -1 for timeouts. signal number for sigs. */ 131 int ev_fd; 132 /** what events this event is interested in, see EV_.. above. */ 133 short ev_flags; 134 /** timeout value */ 135 struct timeval ev_timeout; 136 137 /** callback to call: fd, eventbits, userarg */ 138 void (*ev_callback)(int, short, void *arg); 139 /** callback user arg */ 140 void *ev_arg; 141 }; 142 143 /* function prototypes (some are as they appear in event.h) */ 144 /** create event base */ 145 void *event_init(time_t* time_secs, struct timeval* time_tv); 146 /** get version */ 147 const char *event_get_version(void); 148 /** get polling method, select */ 149 const char *event_get_method(void); 150 /** run select in a loop */ 151 int event_base_dispatch(struct event_base *); 152 /** exit that loop */ 153 int event_base_loopexit(struct event_base *, struct timeval *); 154 /** exit loop */ 155 int event_base_loopbreak(struct event_base *); 156 /** run select once */ 157 #define EVLOOP_ONCE 1 158 int event_base_loop(struct event_base* base, int flags); 159 /** free event base. Free events yourself */ 160 void event_base_free(struct event_base *); 161 /** set content of event */ 162 void event_set(struct event *, int, short, void (*)(int, short, void *), void *); 163 /** add event to a base. You *must* call this for every event. */ 164 int event_base_set(struct event_base *, struct event *); 165 /** add event to make it active. You may not change it with event_set anymore */ 166 int event_add(struct event *, struct timeval *); 167 /** remove event. You may change it again */ 168 int event_del(struct event *); 169 170 /** add a timer */ 171 #define evtimer_add(ev, tv) event_add(ev, tv) 172 /** remove a timer */ 173 #define evtimer_del(ev) event_del(ev) 174 175 /* uses different implementation. Cannot mix fd/timeouts and signals inside 176 * the same struct event. create several event structs for that. */ 177 /** install signal handler */ 178 int signal_add(struct event *, struct timeval *); 179 /** set signal event contents */ 180 #define signal_set(ev, x, cb, arg) \ 181 event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg) 182 /** remove signal handler */ 183 int signal_del(struct event *); 184 185 #endif /* USE_MINI_EVENT and not USE_WINSOCK */ 186 187 /** compare events in tree, based on timevalue, ptr for uniqueness */ 188 int mini_ev_cmp(const void* a, const void* b); 189 190 #endif /* MINI_EVENT_H */ 191