summaryrefslogtreecommitdiff
path: root/poll
diff options
context:
space:
mode:
authorJustin Erenkrantz <jerenkrantz@apache.org>2004-07-06 03:38:06 +0000
committerJustin Erenkrantz <jerenkrantz@apache.org>2004-07-06 03:38:06 +0000
commitb0a6eebf6c9f97e5b69e92d014d3e7bee0bf535c (patch)
treeee0d1f82e772eeebcb35be3d8facf31cc71ddeea /poll
parentf5c52a8c3df49896ccc149380f334b8279ce8456 (diff)
downloadapr-b0a6eebf6c9f97e5b69e92d014d3e7bee0bf535c.tar.gz
Add support for KQueue and sys_epoll to apr_pollset.
(Justin tweaked the cleanup code from Paul's last posted patch.) Submitted by: Paul Querna Reviewed by: Justin Erenkrantz, Joe Orton git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@65261 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poll')
-rw-r--r--poll/unix/poll.c367
1 files changed, 355 insertions, 12 deletions
diff --git a/poll/unix/poll.c b/poll/unix/poll.c
index c792d0a65..4e36c3225 100644
--- a/poll/unix/poll.c
+++ b/poll/unix/poll.c
@@ -26,12 +26,77 @@
#include <sys/poll.h>
#endif
+#ifdef HAVE_KQUEUE
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+#endif
+
+#ifdef HAVE_EPOLL
+#include <sys/epoll.h>
+#endif
#ifdef NETWARE
#define HAS_SOCKETS(dt) (dt == APR_POLL_SOCKET) ? 1 : 0
#define HAS_PIPES(dt) (dt == APR_POLL_FILE) ? 1 : 0
#endif
+#ifdef HAVE_KQUEUE
+static apr_int16_t get_kqueue_revent(apr_int16_t event, apr_int16_t flags)
+{
+ apr_int16_t rv = 0;
+
+ if (event & EVFILT_READ)
+ rv |= APR_POLLIN;
+ if (event & EVFILT_WRITE)
+ rv |= APR_POLLOUT;
+ if (flags & EV_ERROR || flags & EV_EOF)
+ rv |= APR_POLLERR;
+
+ return rv;
+}
+
+#endif
+
+#ifdef HAVE_EPOLL
+static apr_int16_t get_epoll_event(apr_int16_t event)
+{
+ apr_int16_t rv = 0;
+
+ if (event & APR_POLLIN)
+ rv |= EPOLLIN;
+ if (event & APR_POLLPRI)
+ rv |= EPOLLPRI;
+ if (event & APR_POLLOUT)
+ rv |= EPOLLOUT;
+ if (event & APR_POLLERR)
+ rv |= EPOLLERR;
+ if (event & APR_POLLHUP)
+ rv |= EPOLLHUP;
+ /* APR_POLLNVAL is not handled by epoll. */
+
+ return rv;
+}
+
+static apr_int16_t get_epoll_revent(apr_int16_t event)
+{
+ apr_int16_t rv = 0;
+
+ if (event & EPOLLIN)
+ rv |= APR_POLLIN;
+ if (event & EPOLLPRI)
+ rv |= APR_POLLPRI;
+ if (event & EPOLLOUT)
+ rv |= APR_POLLOUT;
+ if (event & EPOLLERR)
+ rv |= APR_POLLERR;
+ if (event & EPOLLHUP)
+ rv |= APR_POLLHUP;
+ /* APR_POLLNVAL is not handled by epoll. */
+
+ return rv;
+}
+#endif
#ifdef HAVE_POLL /* We can just use poll to do our socket polling. */
@@ -284,9 +349,18 @@ APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, int num, apr_int32_t *n
#endif
struct apr_pollset_t {
+ apr_pool_t *pool;
+
apr_uint32_t nelts;
apr_uint32_t nalloc;
-#ifdef HAVE_POLL
+#ifdef HAVE_KQUEUE
+ int kqueue_fd;
+ struct kevent kevent;
+ struct kevent *ke_set;
+#elif defined(HAVE_EPOLL)
+ int epoll_fd;
+ struct epoll_event *pollset;
+#elif defined(HAVE_POLL)
struct pollfd *pollset;
#else
fd_set readset, writeset, exceptset;
@@ -300,12 +374,23 @@ struct apr_pollset_t {
#endif
};
+static apr_status_t backend_cleanup(void *p_)
+{
+ apr_pollset_t *pollset = (apr_pollset_t *)p_;
+#ifdef HAVE_KQUEUE
+ close(pollset->kqueue_fd);
+#elif defined(HAVE_EPOLL)
+ close(pollset->epoll_fd);
+#endif
+ return APR_SUCCESS;
+}
+
APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
apr_uint32_t size,
apr_pool_t *p,
apr_uint32_t flags)
{
-#if !defined(HAVE_POLL) && defined(FD_SETSIZE)
+#if !defined(HAVE_KQUEUE) && !defined(HAVE_EPOLL) && !defined(HAVE_POLL) && defined(FD_SETSIZE)
if (size > FD_SETSIZE) {
*pollset = NULL;
return APR_EINVAL;
@@ -314,7 +399,22 @@ APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
*pollset = apr_palloc(p, sizeof(**pollset));
(*pollset)->nelts = 0;
(*pollset)->nalloc = size;
-#ifdef HAVE_POLL
+ (*pollset)->pool = p;
+#ifdef HAVE_KQUEUE
+ (*pollset)->ke_set = (struct kevent*)apr_palloc(p, size * sizeof(struct kevent));
+ memset((*pollset)->ke_set, 0, size * sizeof(struct kevent));
+ (*pollset)->kqueue_fd = kqueue();
+ if ((*pollset)->kqueue_fd == -1) {
+ return APR_ENOMEM;
+ }
+ apr_pool_cleanup_register(p, (void*)(*pollset), backend_cleanup,
+ apr_pool_cleanup_null);
+#elif defined(HAVE_EPOLL)
+ (*pollset)->epoll_fd = epoll_create(size);
+ (*pollset)->pollset = apr_palloc(p, size * sizeof(struct epoll_event));
+ apr_pool_cleanup_register(p, (void*)(*pollset), backend_cleanup,
+ apr_pool_cleanup_null);
+#elif defined(HAVE_POLL)
(*pollset)->pollset = apr_palloc(p, size * sizeof(struct pollfd));
#else
FD_ZERO(&((*pollset)->readset));
@@ -333,25 +433,71 @@ APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset)
{
- /* A no-op function for now. If we later implement /dev/poll
- * support, we'll need to close the /dev/poll fd here
- */
- return APR_SUCCESS;
+ return apr_pool_cleanup_run(pollset->pool, pollset, backend_cleanup);
}
APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
const apr_pollfd_t *descriptor)
{
-#ifndef HAVE_POLL
+#ifdef HAVE_KQUEUE
+ apr_os_sock_t fd;
+#elif defined(HAVE_EPOLL)
+ struct epoll_event ev;
+ int ret = -1;
+#else
+#if !defined(HAVE_POLL)
apr_os_sock_t fd;
#endif
+#endif
if (pollset->nelts == pollset->nalloc) {
return APR_ENOMEM;
}
pollset->query_set[pollset->nelts] = *descriptor;
-#ifdef HAVE_POLL
+
+#ifdef HAVE_KQUEUE
+ if (descriptor->desc_type == APR_POLL_SOCKET) {
+ fd = descriptor->desc.s->socketdes;
+ }
+ else {
+ fd = descriptor->desc.f->filedes;
+ }
+
+ if (descriptor->reqevents & APR_POLLIN) {
+ EV_SET(&pollset->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
+
+ if (kevent(pollset->kqueue_fd, &pollset->kevent, 1, NULL, 0,
+ NULL) == -1) {
+ return APR_ENOMEM;
+ }
+ }
+
+ if (descriptor->reqevents & APR_POLLOUT) {
+ EV_SET(&pollset->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
+
+ if (kevent(pollset->kqueue_fd, &pollset->kevent, 1, NULL, 0,
+ NULL) == -1) {
+ return APR_ENOMEM;
+ }
+ }
+
+#elif defined(HAVE_EPOLL)
+ ev.events = get_epoll_event(descriptor->reqevents);
+ if (descriptor->desc_type == APR_POLL_SOCKET) {
+ ev.data.fd = descriptor->desc.s->socketdes;
+ ret = epoll_ctl(pollset->epoll_fd, EPOLL_CTL_ADD,
+ descriptor->desc.s->socketdes, &ev);
+ }
+ else {
+ ev.data.fd = descriptor->desc.f->filedes;
+ ret = epoll_ctl(pollset->epoll_fd, EPOLL_CTL_ADD,
+ descriptor->desc.f->filedes, &ev);
+ }
+ if (0 != ret) {
+ return APR_EBADF;
+ }
+#elif defined(HAVE_POLL)
if (descriptor->desc_type == APR_POLL_SOCKET) {
pollset->pollset[pollset->nelts].fd = descriptor->desc.s->socketdes;
@@ -420,11 +566,97 @@ APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
const apr_pollfd_t *descriptor)
{
apr_uint32_t i;
-#ifndef HAVE_POLL
+#ifdef HAVE_KQUEUE
+ apr_os_sock_t fd;
+#elif defined(HAVE_EPOLL)
+ struct epoll_event ev;
+ int ret = -1;
+#elif defined(HAVE_POLL)
apr_os_sock_t fd;
#endif
-#ifdef HAVE_POLL
+#ifdef HAVE_KQUEUE
+ for (i = 0; i < pollset->nelts; i++) {
+ if (descriptor->desc.s == pollset->query_set[i].desc.s) {
+ /* Found an instance of the fd: remove this and any other copies */
+ apr_uint32_t dst = i;
+ apr_uint32_t old_nelts = pollset->nelts;
+ pollset->nelts--;
+ for (i++; i < old_nelts; i++) {
+ if (descriptor->desc.s == pollset->query_set[i].desc.s) {
+ pollset->nelts--;
+ }
+ else {
+ pollset->query_set[dst] = pollset->query_set[i];
+ dst++;
+ }
+ }
+
+ if (descriptor->desc_type == APR_POLL_SOCKET) {
+ fd = descriptor->desc.s->socketdes;
+ }
+ else {
+ fd = descriptor->desc.f->filedes;
+ }
+
+ if (descriptor->reqevents & APR_POLLIN) {
+ EV_SET(&pollset->kevent, fd,
+ EVFILT_READ, EV_DELETE, 0, 0, NULL);
+
+ if (kevent(pollset->kqueue_fd, &pollset->kevent, 1, NULL, 0,
+ NULL) == -1) {
+ return APR_EBADF;
+ }
+ }
+
+ if (descriptor->reqevents & APR_POLLOUT) {
+ EV_SET(&pollset->kevent, fd,
+ EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
+
+ if (kevent(pollset->kqueue_fd, &pollset->kevent, 1, NULL, 0,
+ NULL) == -1) {
+ return APR_EBADF;
+ }
+ }
+
+ return APR_SUCCESS;
+ }
+ }
+#elif defined(HAVE_EPOLL)
+ for (i = 0; i < pollset->nelts; i++) {
+ if (descriptor->desc.s == pollset->query_set[i].desc.s) {
+ /* Found an instance of the fd: remove this and any other copies */
+ apr_uint32_t dst = i;
+ apr_uint32_t old_nelts = pollset->nelts;
+ pollset->nelts--;
+ for (i++; i < old_nelts; i++) {
+ if (descriptor->desc.s == pollset->query_set[i].desc.s) {
+ pollset->nelts--;
+ }
+ else {
+ pollset->query_set[dst] = pollset->query_set[i];
+ dst++;
+ }
+ }
+ ev.events = get_epoll_event(descriptor->reqevents);
+ if (descriptor->desc_type == APR_POLL_SOCKET) {
+ ev.data.fd = descriptor->desc.s->socketdes;
+ ret = epoll_ctl(pollset->epoll_fd, EPOLL_CTL_DEL,
+ descriptor->desc.s->socketdes, &ev);
+ }
+ else {
+ ev.data.fd = descriptor->desc.f->filedes;
+ ret = epoll_ctl(pollset->epoll_fd, EPOLL_CTL_DEL,
+ descriptor->desc.f->filedes, &ev);
+ }
+ if (ret < 0) {
+ return APR_EBADF;
+ }
+
+ return APR_SUCCESS;
+ }
+ }
+#elif defined(HAVE_POLL)
for (i = 0; i < pollset->nelts; i++) {
if (descriptor->desc.s == pollset->query_set[i].desc.s) {
/* Found an instance of the fd: remove this and any other copies */
@@ -485,8 +717,119 @@ APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
return APR_NOTFOUND;
}
+#ifdef HAVE_KQUEUE
+APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
+ apr_interval_time_t timeout,
+ apr_int32_t *num,
+ const apr_pollfd_t **descriptors)
+{
+ int rv;
+ apr_uint32_t i, j, r = 0;
+ struct timespec tv, *tvptr;
+
+ if (timeout < 0) {
+ tvptr = NULL;
+ }
+ else {
+ tv.tv_sec = (long)apr_time_sec(timeout);
+ tv.tv_nsec = (long)apr_time_msec(timeout);
+ tvptr = &tv;
+ }
+
+ rv = kevent(pollset->kqueue_fd, NULL, 0, pollset->ke_set, pollset->nelts,
+ tvptr);
+ (*num) = rv;
+ if (rv < 0) {
+ return apr_get_netos_error();
+ }
+ if (rv == 0) {
+ return APR_TIMEUP;
+ }
+
+ /* TODO: Is there a better way to re-associate our data? */
+ for (i = 0; i < pollset->nelts; i++) {
+ apr_os_sock_t fd;
+ if (pollset->query_set[i].desc_type == APR_POLL_SOCKET) {
+ fd = pollset->query_set[i].desc.s->socketdes;
+ }
+ else {
+ fd = pollset->query_set[i].desc.f->filedes;
+ }
+ for (j = 0; j < rv; j++) {
+ if (pollset->ke_set[j].ident == fd ) {
+ pollset->result_set[r] = pollset->query_set[i];
+ pollset->result_set[r].rtnevents =
+ get_kqueue_revent(pollset->ke_set[j].filter,
+ pollset->ke_set[j].flags);
+ r++;
+ }
+ }
+ }
+
+ (*num) = r;
+
+ if (descriptors) {
+ *descriptors = pollset->result_set;
+ }
+
+ return APR_SUCCESS;
+}
+
+#elif defined(HAVE_EPOLL)
+
+APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
+ apr_interval_time_t timeout,
+ apr_int32_t *num,
+ const apr_pollfd_t **descriptors)
+{
+ int rv;
+ apr_uint32_t i, j, k;
+
+ if (timeout > 0) {
+ timeout /= 1000;
+ }
-#ifdef HAVE_POLL
+ rv = epoll_wait(pollset->epoll_fd, pollset->pollset, pollset->nelts,
+ timeout);
+ (*num) = rv;
+ if (rv < 0) {
+ return apr_get_netos_error();
+ }
+ if (rv == 0) {
+ return APR_TIMEUP;
+ }
+ j = 0;
+ for (i = 0; i < pollset->nelts; i++) {
+ if (pollset->pollset[i].events != 0) {
+ /* TODO: Is there a better way to re-associate our data? */
+ for (k = 0; k < pollset->nelts; k++) {
+ if (pollset->query_set[k].desc_type == APR_POLL_SOCKET &&
+ pollset->query_set[k].desc.s->socketdes ==
+ pollset->pollset[i].data.fd) {
+ pollset->result_set[j] = pollset->query_set[k];
+ pollset->result_set[j].rtnevents =
+ get_epoll_revent(pollset->pollset[i].events);
+ j++;
+ break;
+ }
+ else if (pollset->query_set[k].desc_type == APR_POLL_FILE
+ && pollset->query_set[k].desc.f->filedes ==
+ pollset->pollset[i].data.fd) {
+ pollset->result_set[j] = pollset->query_set[k];
+ pollset->result_set[j].rtnevents =
+ get_epoll_revent(pollset->pollset[i].events);
+ j++;
+ break;
+ }
+ }
+ }
+ }
+ if (descriptors) {
+ *descriptors = pollset->result_set;
+ }
+ return APR_SUCCESS;
+}
+#elif defined(HAVE_POLL)
APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
apr_interval_time_t timeout,
apr_int32_t *num,