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>
48 #define L2CAP_SOCKET_CHECKED
51 #define __event_link(ev) \
53 TAILQ_INSERT_TAIL(&pending, ev, next); \
54 ev->flags |= EV_PENDING; \
57 static void tv_add(struct timeval *, struct timeval const *);
58 static void tv_sub(struct timeval *, struct timeval const *);
59 static int tv_cmp(struct timeval const *, struct timeval const *);
60 static int __event_dispatch(void);
61 static void __event_add_current(struct event *);
62 static void __event_del_current(struct event *);
65 static TAILQ_HEAD(, event) pending;
66 static TAILQ_HEAD(, event) current;
77 while (__event_dispatch() == 0)
84 __event_dispatch(void)
89 struct timeval now, timeout, t;
96 gettimeofday(&now, NULL);
98 timeout.tv_sec = 10; /* arbitrary */
101 TAILQ_INIT(¤t);
107 event_log_debug("%s: building fd set...", __func__);
109 while (!TAILQ_EMPTY(&pending)) {
110 ev = TAILQ_FIRST(&pending);
113 if (ev->flags & EV_HAS_TIMEOUT) {
114 if (tv_cmp(&now, &ev->expire) >= 0)
115 t.tv_sec = t.tv_usec = 0;
121 if (tv_cmp(&t, &timeout) < 0)
126 if (ev->flags & EV_READ) {
128 nfd = (nfd > ev->fd) ? nfd : ev->fd;
131 if (ev->flags & EV_WRITE) {
133 nfd = (nfd > ev->fd) ? nfd : ev->fd;
137 __event_add_current(ev);
140 event_log_debug("%s: waiting for events...", __func__);
142 nfd = select(nfd + 1, &r, &w, NULL, &timeout);
147 * Process current pending
150 event_log_debug("%s: processing events...", __func__);
152 gettimeofday(&now, NULL);
154 while (!TAILQ_EMPTY(¤t)) {
155 ev = TAILQ_FIRST(¤t);
156 __event_del_current(ev);
158 /* check if fd is ready for reading/writing */
159 if (nfd > 0 && ev->fd >= 0) {
160 if (FD_ISSET(ev->fd, &r) || FD_ISSET(ev->fd, &w)) {
161 if (ev->flags & EV_PERSIST) {
162 if (ev->flags & EV_HAS_TIMEOUT)
163 event_add(ev, &ev->timeout);
170 event_log_debug("%s: calling %p(%d, %p), " \
171 "ev=%p", __func__, ev->cb, ev->fd,
175 (ev->flags & (EV_READ|EV_WRITE)),
182 /* if event has no timeout - just requeue */
183 if ((ev->flags & EV_HAS_TIMEOUT) == 0) {
188 /* check if event has expired */
189 if (tv_cmp(&now, &ev->expire) >= 0) {
190 if (ev->flags & EV_PERSIST)
191 event_add(ev, &ev->timeout);
193 event_log_debug("%s: calling %p(%d, %p), ev=%p",
194 __func__, ev->cb, ev->fd, ev->cbarg, ev);
197 (ev->flags & (EV_READ|EV_WRITE)),
203 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
211 __event_set(struct event *ev, int fd, short flags,
212 void (*cb)(int, short, void *), void *cbarg)
221 __event_add(struct event *ev, const struct timeval *timeout)
223 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
225 if (timeout != NULL) {
226 gettimeofday(&ev->expire, NULL);
227 tv_add(&ev->expire, timeout);
228 ev->timeout = *timeout;
229 ev->flags |= EV_HAS_TIMEOUT;
231 ev->flags &= ~EV_HAS_TIMEOUT;
239 __event_del(struct event *ev)
241 assert((ev->flags & EV_CURRENT) == 0);
243 if ((ev->flags & EV_PENDING) != 0) {
244 TAILQ_REMOVE(&pending, ev, next);
245 ev->flags &= ~EV_PENDING;
252 __event_add_current(struct event *ev)
254 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
256 TAILQ_INSERT_TAIL(¤t, ev, next);
257 ev->flags |= EV_CURRENT;
261 __event_del_current(struct event *ev)
263 assert((ev->flags & (EV_CURRENT|EV_PENDING)) == EV_CURRENT);
265 TAILQ_REMOVE(¤t, ev, next);
266 ev->flags &= ~EV_CURRENT;
270 tv_add(struct timeval *a, struct timeval const *b)
272 a->tv_sec += b->tv_sec;
273 a->tv_usec += b->tv_usec;
275 if(a->tv_usec >= 1000000) {
276 a->tv_usec -= 1000000;
282 tv_sub(struct timeval *a, struct timeval const *b)
284 if (a->tv_usec < b->tv_usec) {
285 a->tv_usec += 1000000;
289 a->tv_usec -= b->tv_usec;
290 a->tv_sec -= b->tv_sec;
294 tv_cmp(struct timeval const *a, struct timeval const *b)
296 if (a->tv_sec > b->tv_sec)
299 if (a->tv_sec < b->tv_sec)
302 if (a->tv_usec > b->tv_usec)
305 if (a->tv_usec < b->tv_usec)