6 * Copyright (c) 2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Hack to provide libevent (see devel/libevent port) like API.
35 * Should be removed if FreeBSD ever decides to import libevent into base.
38 #include <sys/select.h>
40 #include <sys/queue.h>
50 #define __event_link(ev) \
52 TAILQ_INSERT_TAIL(&pending, ev, next); \
53 ev->flags |= EV_PENDING; \
56 static void tv_add(struct timeval *, struct timeval const *);
57 static void tv_sub(struct timeval *, struct timeval const *);
58 static int tv_cmp(struct timeval const *, struct timeval const *);
59 static int __event_dispatch(void);
60 static void __event_add_current(struct event *);
61 static void __event_del_current(struct event *);
64 static TAILQ_HEAD(, event) pending;
65 static TAILQ_HEAD(, event) current;
76 while (__event_dispatch() == 0)
83 __event_dispatch(void)
88 struct timeval now, timeout, t;
95 gettimeofday(&now, NULL);
97 timeout.tv_sec = 10; /* arbitrary */
100 TAILQ_INIT(¤t);
106 event_log_debug("%s: building fd set...", __func__);
108 while (!TAILQ_EMPTY(&pending)) {
109 ev = TAILQ_FIRST(&pending);
112 if (ev->flags & EV_HAS_TIMEOUT) {
113 if (tv_cmp(&now, &ev->expire) >= 0)
114 t.tv_sec = t.tv_usec = 0;
120 if (tv_cmp(&t, &timeout) < 0)
125 if (ev->flags & EV_READ) {
127 nfd = (nfd > ev->fd) ? nfd : ev->fd;
130 if (ev->flags & EV_WRITE) {
132 nfd = (nfd > ev->fd) ? nfd : ev->fd;
136 __event_add_current(ev);
139 event_log_debug("%s: waiting for events...", __func__);
141 nfd = select(nfd + 1, &r, &w, NULL, &timeout);
146 * Process current pending
149 event_log_debug("%s: processing events...", __func__);
151 gettimeofday(&now, NULL);
153 while (!TAILQ_EMPTY(¤t)) {
154 ev = TAILQ_FIRST(¤t);
155 __event_del_current(ev);
157 /* check if fd is ready for reading/writing */
158 if (nfd > 0 && ev->fd >= 0) {
159 if (FD_ISSET(ev->fd, &r) || FD_ISSET(ev->fd, &w)) {
160 if (ev->flags & EV_PERSIST) {
161 if (ev->flags & EV_HAS_TIMEOUT)
162 event_add(ev, &ev->timeout);
169 event_log_debug("%s: calling %p(%d, %p), " \
170 "ev=%p", __func__, ev->cb, ev->fd,
174 (ev->flags & (EV_READ|EV_WRITE)),
181 /* if event has no timeout - just requeue */
182 if ((ev->flags & EV_HAS_TIMEOUT) == 0) {
187 /* check if event has expired */
188 if (tv_cmp(&now, &ev->expire) >= 0) {
189 if (ev->flags & EV_PERSIST)
190 event_add(ev, &ev->timeout);
192 event_log_debug("%s: calling %p(%d, %p), ev=%p",
193 __func__, ev->cb, ev->fd, ev->cbarg, ev);
196 (ev->flags & (EV_READ|EV_WRITE)),
202 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
210 __event_set(struct event *ev, int fd, short flags,
211 void (*cb)(int, short, void *), void *cbarg)
220 __event_add(struct event *ev, const struct timeval *timeout)
222 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
224 if (timeout != NULL) {
225 gettimeofday(&ev->expire, NULL);
226 tv_add(&ev->expire, timeout);
227 ev->timeout = *timeout;
228 ev->flags |= EV_HAS_TIMEOUT;
230 ev->flags &= ~EV_HAS_TIMEOUT;
238 __event_del(struct event *ev)
240 assert((ev->flags & EV_CURRENT) == 0);
242 if ((ev->flags & EV_PENDING) != 0) {
243 TAILQ_REMOVE(&pending, ev, next);
244 ev->flags &= ~EV_PENDING;
251 __event_add_current(struct event *ev)
253 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
255 TAILQ_INSERT_TAIL(¤t, ev, next);
256 ev->flags |= EV_CURRENT;
260 __event_del_current(struct event *ev)
262 assert((ev->flags & (EV_CURRENT|EV_PENDING)) == EV_CURRENT);
264 TAILQ_REMOVE(¤t, ev, next);
265 ev->flags &= ~EV_CURRENT;
269 tv_add(struct timeval *a, struct timeval const *b)
271 a->tv_sec += b->tv_sec;
272 a->tv_usec += b->tv_usec;
274 if(a->tv_usec >= 1000000) {
275 a->tv_usec -= 1000000;
281 tv_sub(struct timeval *a, struct timeval const *b)
283 if (a->tv_usec < b->tv_usec) {
284 a->tv_usec += 1000000;
288 a->tv_usec -= b->tv_usec;
289 a->tv_sec -= b->tv_sec;
293 tv_cmp(struct timeval const *a, struct timeval const *b)
295 if (a->tv_sec > b->tv_sec)
298 if (a->tv_sec < b->tv_sec)
301 if (a->tv_usec > b->tv_usec)
304 if (a->tv_usec < b->tv_usec)