6 * SPDX-License-Identifier: BSD-2-Clause
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
35 * Hack to provide libevent (see devel/libevent port) like API.
36 * Should be removed if FreeBSD ever decides to import libevent into base.
39 #include <sys/select.h>
41 #include <sys/queue.h>
49 #define L2CAP_SOCKET_CHECKED
52 #define __event_link(ev) \
54 TAILQ_INSERT_TAIL(&pending, ev, next); \
55 ev->flags |= EV_PENDING; \
58 static void tv_add(struct timeval *, struct timeval const *);
59 static void tv_sub(struct timeval *, struct timeval const *);
60 static int tv_cmp(struct timeval const *, struct timeval const *);
61 static int __event_dispatch(void);
62 static void __event_add_current(struct event *);
63 static void __event_del_current(struct event *);
66 static TAILQ_HEAD(, event) pending;
67 static TAILQ_HEAD(, event) current;
78 while (__event_dispatch() == 0)
85 __event_dispatch(void)
90 struct timeval now, timeout, t;
97 gettimeofday(&now, NULL);
99 timeout.tv_sec = 10; /* arbitrary */
102 TAILQ_INIT(¤t);
108 event_log_debug("%s: building fd set...", __func__);
110 while (!TAILQ_EMPTY(&pending)) {
111 ev = TAILQ_FIRST(&pending);
114 if (ev->flags & EV_HAS_TIMEOUT) {
115 if (tv_cmp(&now, &ev->expire) >= 0)
116 t.tv_sec = t.tv_usec = 0;
122 if (tv_cmp(&t, &timeout) < 0)
127 if (ev->flags & EV_READ) {
129 nfd = (nfd > ev->fd) ? nfd : ev->fd;
132 if (ev->flags & EV_WRITE) {
134 nfd = (nfd > ev->fd) ? nfd : ev->fd;
138 __event_add_current(ev);
141 event_log_debug("%s: waiting for events...", __func__);
143 nfd = select(nfd + 1, &r, &w, NULL, &timeout);
148 * Process current pending
151 event_log_debug("%s: processing events...", __func__);
153 gettimeofday(&now, NULL);
155 while (!TAILQ_EMPTY(¤t)) {
156 ev = TAILQ_FIRST(¤t);
157 __event_del_current(ev);
159 /* check if fd is ready for reading/writing */
160 if (nfd > 0 && ev->fd >= 0) {
161 if (FD_ISSET(ev->fd, &r) || FD_ISSET(ev->fd, &w)) {
162 if (ev->flags & EV_PERSIST) {
163 if (ev->flags & EV_HAS_TIMEOUT)
164 event_add(ev, &ev->timeout);
171 event_log_debug("%s: calling %p(%d, %p), " \
172 "ev=%p", __func__, ev->cb, ev->fd,
176 (ev->flags & (EV_READ|EV_WRITE)),
183 /* if event has no timeout - just requeue */
184 if ((ev->flags & EV_HAS_TIMEOUT) == 0) {
189 /* check if event has expired */
190 if (tv_cmp(&now, &ev->expire) >= 0) {
191 if (ev->flags & EV_PERSIST)
192 event_add(ev, &ev->timeout);
194 event_log_debug("%s: calling %p(%d, %p), ev=%p",
195 __func__, ev->cb, ev->fd, ev->cbarg, ev);
198 (ev->flags & (EV_READ|EV_WRITE)),
204 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
212 __event_set(struct event *ev, int fd, short flags,
213 void (*cb)(int, short, void *), void *cbarg)
222 __event_add(struct event *ev, const struct timeval *timeout)
224 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
226 if (timeout != NULL) {
227 gettimeofday(&ev->expire, NULL);
228 tv_add(&ev->expire, timeout);
229 ev->timeout = *timeout;
230 ev->flags |= EV_HAS_TIMEOUT;
232 ev->flags &= ~EV_HAS_TIMEOUT;
240 __event_del(struct event *ev)
242 assert((ev->flags & EV_CURRENT) == 0);
244 if ((ev->flags & EV_PENDING) != 0) {
245 TAILQ_REMOVE(&pending, ev, next);
246 ev->flags &= ~EV_PENDING;
253 __event_add_current(struct event *ev)
255 assert((ev->flags & (EV_PENDING|EV_CURRENT)) == 0);
257 TAILQ_INSERT_TAIL(¤t, ev, next);
258 ev->flags |= EV_CURRENT;
262 __event_del_current(struct event *ev)
264 assert((ev->flags & (EV_CURRENT|EV_PENDING)) == EV_CURRENT);
266 TAILQ_REMOVE(¤t, ev, next);
267 ev->flags &= ~EV_CURRENT;
271 tv_add(struct timeval *a, struct timeval const *b)
273 a->tv_sec += b->tv_sec;
274 a->tv_usec += b->tv_usec;
276 if(a->tv_usec >= 1000000) {
277 a->tv_usec -= 1000000;
283 tv_sub(struct timeval *a, struct timeval const *b)
285 if (a->tv_usec < b->tv_usec) {
286 a->tv_usec += 1000000;
290 a->tv_usec -= b->tv_usec;
291 a->tv_sec -= b->tv_sec;
295 tv_cmp(struct timeval const *a, struct timeval const *b)
297 if (a->tv_sec > b->tv_sec)
300 if (a->tv_sec < b->tv_sec)
303 if (a->tv_usec > b->tv_usec)
306 if (a->tv_usec < b->tv_usec)