From ccd1c8f16026a4640c7a4699384626a4fc6bbc19 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Wed, 27 Jan 2010 17:50:02 +0100 Subject: [PATCH] kqueue() BSD fixes --- BSDmakefile | 8 ++++++++ src/network/kqueue.c | 34 ++++++++++++++++++++++++++++++++-- src/system.h | 2 -- 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 BSDmakefile diff --git a/BSDmakefile b/BSDmakefile new file mode 100644 index 0000000..a27bafd --- /dev/null +++ b/BSDmakefile @@ -0,0 +1,8 @@ + +GMAKE=gmake + +all: + @${GMAKE} ${.TARGETS} + +${.TARGETS}: all + diff --git a/src/network/kqueue.c b/src/network/kqueue.c index 899dfbc..86042e7 100644 --- a/src/network/kqueue.c +++ b/src/network/kqueue.c @@ -38,7 +38,7 @@ struct net_backend int kqfd; size_t num; size_t max; - struct net_connection_epoll** conns; + struct net_connection_kqueue** conns; struct kevent** changes; size_t nchanges; struct kevent events[KQUEUE_EVBUFFER]; @@ -93,7 +93,33 @@ void net_backend_shutdown() */ int net_backend_process() { + int n; + struct timespec tspec = { 0, }; + size_t secs = timeout_queue_get_next_timeout(&g_backend->timeout_queue, g_backend->now); + tspec.tv_sec = secs; + int res = kevent(g_backend->kqfd, *g_backend->changes, g_backend->nchanges, g_backend->events, KQUEUE_EVBUFFER, &tspec); + g_backend->nchanges = 0; + g_backend->now = time(0); + timeout_queue_process(&g_backend->timeout_queue, g_backend->now); + + if (res == -1) + { + LOG_WARN("kevent returned -1"); + return 0; + } + + for (n = 0; n < res; n++) + { + struct net_connection_kqueue* con = (struct net_connection_kqueue*) g_backend->events[n].udata; + int ev = 0; + if (g_backend->events[n].filter & EVFILT_READ) ev |= NET_EVENT_READ; + if (g_backend->events[n].filter & EVFILT_WRITE) ev |= NET_EVENT_WRITE; + net_con_callback((struct net_connection*) con, ev); + } + + net_cleanup_process(g_backend->cleaner); + return 1; } struct timeout_queue* net_backend_get_timeout_queue() @@ -130,6 +156,8 @@ void net_con_initialize(struct net_connection* con_, int sd, net_connection_cb c EV_SET(&con->ev, sd, filter, EV_ADD, 0, 0, con); + g_backend->changes[g_backend->nchanges++] = &con->ev; + g_backend->conns[sd] = con; g_backend->num++; } @@ -153,6 +181,7 @@ void net_con_update(struct net_connection* con_, int events) return; EV_SET(&con->ev, con->sd, filter, EV_ADD, 0, 0, con); + g_backend->changes[g_backend->nchanges++] = &con->ev; } void net_con_close(struct net_connection* con_) @@ -169,7 +198,8 @@ void net_con_close(struct net_connection* con_) net_con_clear_timeout(con_); - EV_SET(&con->ev, con->sd, 0, EV_DELETE, 0, 0, 0); + /* No need to remove it from the kqueue filter, the kqueue man page says + it is automatically removed when the descriptor is closed. */ net_close(con->sd); con->sd = -1; diff --git a/src/system.h b/src/system.h index 45236f1..879241b 100644 --- a/src/system.h +++ b/src/system.h @@ -108,13 +108,11 @@ #include #endif -/* #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) #define USE_KQUEUE #define HAVE_BACKEND #include #endif -*/ #ifndef HAVE_BACKEND #define USE_SELECT