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