From ac941cc4b20251e27465958626cf0cb8e47a1c21 Mon Sep 17 00:00:00 2001 From: mheily Date: Wed, 22 Jan 2014 04:30:45 +0000 Subject: [PATCH] kqlite: add initial timerfd support git-svn-id: svn://svn.code.sf.net/p/libkqueue/code/trunk@665 fb4e3144-bc1c-4b72-a658-5bcd248dd7f7 --- kqlite/kqlite.c | 27 +++++++++++++++++---------- kqlite/test-lite.c | 17 ++++++++++++++++- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/kqlite/kqlite.c b/kqlite/kqlite.c index c3b1d30..f3f8306 100644 --- a/kqlite/kqlite.c +++ b/kqlite/kqlite.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -66,6 +67,7 @@ struct kqueue { int epfd; /* epoll */ int inofd; /* inotify */ int sigfd; /* signalfd */ + int timefd; /* timerfd */ int readfd, writefd; /* epoll descriptors for EVFILT_READ & EVFILT_WRITE */ sigset_t sigmask; /* All of the active knotes for each filter. The index in the array matches @@ -128,7 +130,7 @@ kq_init(void) struct kqueue *kq; #if defined(USE_KQUEUE) - if ((kq = malloc(sizeof(*kq))) == NULL) + if ((kq = calloc(1, sizeof(*kq))) == NULL) return (NULL); kq->kqfd = kqueue(); @@ -155,14 +157,14 @@ kq_init(void) /* Initialize all the event descriptors */ sigemptyset(&kq->sigmask); - kq->sigfd = kq->inofd = kq->epfd = kq->readfd = kq->writefd = -1; kq->sigfd = signalfd(-1, &kq->sigmask, 0); kq->inofd = inotify_init(); kq->epfd = epoll_create(10); kq->readfd = epoll_create(10); kq->writefd = epoll_create(10); + kq->timefd = timerfd_create(CLOCK_MONOTONIC, 0); if (kq->sigfd < 0 || kq->inofd < 0 || kq->epfd < 0 - || kq->readfd < 0 || kq->writefd < 0) + || kq->readfd < 0 || kq->writefd < 0 || kq->timefd < 0) goto errout; /* Add the signalfd descriptor to the epollset */ @@ -202,12 +204,7 @@ kq_init(void) return (kq); errout: - free(kq); - if (kq->epfd >= 0) close(kq->epfd); - if (kq->readfd >= 0) close(kq->readfd); - if (kq->writefd >= 0) close(kq->writefd); - if (kq->sigfd >= 0) close(kq->sigfd); - //FIXME: something like: if (kq->wfd[EVFILT_SIGNAL] >= 0) free(kq->epfd); + kq_free(kq); return (NULL); #endif } @@ -219,11 +216,21 @@ kq_free(kqueue_t kq) close(kq.kqfd); #elif defined(USE_EPOLL) + close(kq->sigfd); + close(kq->inofd); close(kq->epfd); + close(kq->readfd); + close(kq->writefd); + close(kq->timefd); + //FIXME: need to free each individual knote for (int i = 0; i < EVFILT_SYSCOUNT; i++) utarray_free(kq->knote[i]); - //FIXME: there are a more things to do + +# ifdef KQ_THREADSAFE + pthread_mutex_destroy(&kq->kq_mtx); +# endif + #endif free(kq); } diff --git a/kqlite/test-lite.c b/kqlite/test-lite.c index 8e705a2..d261195 100644 --- a/kqlite/test-lite.c +++ b/kqlite/test-lite.c @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2013 Mark Heily + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + #include "./lite.h" #include @@ -8,7 +24,6 @@ #include #include - void test_evfilt_write(kqueue_t kq) { struct kevent kev; int sockfd[2];