6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) 2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Hack to provide libevent (see devel/libevent port) like API.
37 * Should be removed if FreeBSD ever decides to import libevent into base.
40 #include <sys/select.h>
42 #include <sys/queue.h>
50 #define L2CAP_SOCKET_CHECKED
53 #define __event_link(ev) \
55 TAILQ_INSERT_TAIL(&pending, ev, next); \
56 ev->flags |= EV_PENDING; \
59 static void tv_add(struct timeval *, struct timeval const *);
60 static void tv_sub(struct timeval *, struct timeval const *);
61 static int tv_cmp(struct timeval const *, struct timeval const *);
62 static int __event_dispatch(void);
63 static void __event_add_current(struct event *);
64 static void __event_del_current(struct event *);
67 static TAILQ_HEAD(, event) pending;
68 static TAILQ_HEAD(, event) current;
79 while (__event_dispatch() == 0)
86 __event_dispatch(void)
91 struct timeval now, timeout, t;
98 gettimeofday(&now, NULL);
100 timeout.tv_sec = 10; /* arbitrary */
103 TAILQ_INIT(¤t);
109 event_log_debug("%s: building fd set...", __func__);
111 while (!TAILQ_EMPTY(&pending)) {
112 ev = TAILQ_FIRST(&pending);
115 if (ev->flags & EV_HAS_TIMEOUT) {
116 if (tv_cmp(&now, &ev->expire) >= 0)
117 t.tv_sec = t.tv_usec = 0;
123 if (tv_cmp(&t, &timeout) < 0)
128 if (ev->flags & EV_READ) {
130 nfd = (nfd > ev->fd) ? nfd : ev->fd;
133 if (ev->flags & EV_WRITE) {
135 nfd = (nfd > ev->fd) ? nfd : ev->fd;
139 __event_add_current(ev);
142 event_log_debug("%s: waiting for events...", __func__);
144 nfd = select(nfd + 1, &r, &w, NULL, &timeout);
149 * Process current pending
152 event_log_debug("%s: processing events...", __func__);
154 gettimeofday(&now, NULL);
156 while (!TAILQ_EMPTY(¤t)) {
157 ev = TAILQ_FIRST(¤t);
158 __event_del_current(ev);
160 /* check if fd is ready for reading/writing */
161 if (nfd > 0 && ev->fd >= 0) {
162 if (FD_ISSET(ev->fd, &r) || FD_ISSET(ev->fd, &w)) {
163 if (ev->flags & EV_PERSIST) {
164 if (ev->flags & EV_HAS_TIMEOUT)
165 event_add(ev, &ev->timeout);
172 event_log_debug("%s: calling %p(%d, %p), " \
173 "ev=%p", __func__, ev->cb, ev->fd,
177 (ev->flags & (EV_READ|EV_WRITE)),
184 /* if event has no timeout - just requeue */
185 if ((ev->flags & EV_HAS_TIMEOUT) == 0) {
190 /* check if event has expired */
191 if (tv_cmp(&now, &ev->expire) >= 0) {
192 if (ev->flags & EV_PERSIST)
193 event_add(ev, &ev->timeout);
195 event_log_debug("%s: calling %p(%d, %p), ev=%p",
196 __func__, ev->cb, ev->fd, ev->cbarg, ev);
199 (ev->flags & (EV_READ|EV_WRITE)),
205 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
213 __event_set(struct event *ev, int fd, short flags,
214 void (*cb)(int, short, void *), void *cbarg)
223 __event_add(struct event *ev, const struct timeval *timeout)
225 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
227 if (timeout != NULL) {
228 gettimeofday(&ev->expire, NULL);
229 tv_add(&ev->expire, timeout);
230 ev->timeout = *timeout;
231 ev->flags |= EV_HAS_TIMEOUT;
233 ev->flags &= ~EV_HAS_TIMEOUT;
241 __event_del(struct event *ev)
243 assert((ev->flags & EV_CURRENT) == 0);
245 if ((ev->flags & EV_PENDING) != 0) {
246 TAILQ_REMOVE(&pending, ev, next);
247 ev->flags &= ~EV_PENDING;
254 __event_add_current(struct event *ev)
256 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
258 TAILQ_INSERT_TAIL(¤t, ev, next);
259 ev->flags |= EV_CURRENT;
263 __event_del_current(struct event *ev)
265 assert((ev->flags & (EV_CURRENT|EV_PENDING)) == EV_CURRENT);
267 TAILQ_REMOVE(¤t, ev, next);
268 ev->flags &= ~EV_CURRENT;
272 tv_add(struct timeval *a, struct timeval const *b)
274 a->tv_sec += b->tv_sec;
275 a->tv_usec += b->tv_usec;
277 if(a->tv_usec >= 1000000) {
278 a->tv_usec -= 1000000;
284 tv_sub(struct timeval *a, struct timeval const *b)
286 if (a->tv_usec < b->tv_usec) {
287 a->tv_usec += 1000000;
291 a->tv_usec -= b->tv_usec;
292 a->tv_sec -= b->tv_sec;
296 tv_cmp(struct timeval const *a, struct timeval const *b)
298 if (a->tv_sec > b->tv_sec)
301 if (a->tv_sec < b->tv_sec)
304 if (a->tv_usec > b->tv_usec)
307 if (a->tv_usec < b->tv_usec)