1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "apr_portable.h"
21 #include "apr_arch_file_io.h"
22 #include "apr_arch_networkio.h"
23 #include "apr_arch_poll_private.h"
24 #include "apr_arch_inherit.h"
28 static apr_int16_t get_kqueue_revent(apr_int16_t event, apr_int16_t flags)
32 if (event == EVFILT_READ)
34 else if (event == EVFILT_WRITE)
38 /* APR_POLLPRI, APR_POLLERR, and APR_POLLNVAL are not handled by this
40 * TODO: See if EV_ERROR + certain system errors in the returned data field
41 * should map to APR_POLLNVAL.
46 struct apr_pollset_private_t
51 struct kevent *ke_set;
52 apr_pollfd_t *result_set;
54 /* A thread mutex to protect operations on the rings */
55 apr_thread_mutex_t *ring_lock;
57 /* A ring containing all of the pollfd_t that are active */
58 APR_RING_HEAD(pfd_query_ring_t, pfd_elem_t) query_ring;
59 /* A ring of pollfd_t that have been used, and then _remove'd */
60 APR_RING_HEAD(pfd_free_ring_t, pfd_elem_t) free_ring;
61 /* A ring of pollfd_t where rings that have been _remove'd but
62 might still be inside a _poll */
63 APR_RING_HEAD(pfd_dead_ring_t, pfd_elem_t) dead_ring;
66 static apr_status_t impl_pollset_cleanup(apr_pollset_t *pollset)
68 close(pollset->p->kqueue_fd);
72 static apr_status_t impl_pollset_create(apr_pollset_t *pollset,
78 pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t));
80 if (flags & APR_POLLSET_THREADSAFE &&
81 ((rv = apr_thread_mutex_create(&pollset->p->ring_lock,
82 APR_THREAD_MUTEX_DEFAULT,
83 p)) != APR_SUCCESS)) {
88 if (flags & APR_POLLSET_THREADSAFE) {
94 /* POLLIN and POLLOUT are represented in different returned
95 * events, so we need 2 entries per descriptor in the result set,
96 * both for what is returned by kevent() and what is returned to
97 * the caller of apr_pollset_poll() (since it doesn't spend the
98 * CPU to coalesce separate APR_POLLIN and APR_POLLOUT events
99 * for the same descriptor)
101 pollset->p->setsize = 2 * size;
104 (struct kevent *) apr_palloc(p, pollset->p->setsize * sizeof(struct kevent));
106 memset(pollset->p->ke_set, 0, pollset->p->setsize * sizeof(struct kevent));
108 pollset->p->kqueue_fd = kqueue();
110 if (pollset->p->kqueue_fd == -1) {
112 return apr_get_netos_error();
118 if ((flags = fcntl(pollset->p->kqueue_fd, F_GETFD)) == -1) {
120 close(pollset->p->kqueue_fd);
126 if (fcntl(pollset->p->kqueue_fd, F_SETFD, flags) == -1) {
128 close(pollset->p->kqueue_fd);
134 pollset->p->result_set = apr_palloc(p, pollset->p->setsize * sizeof(apr_pollfd_t));
136 APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link);
137 APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link);
138 APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link);
143 static apr_status_t impl_pollset_add(apr_pollset_t *pollset,
144 const apr_pollfd_t *descriptor)
148 apr_status_t rv = APR_SUCCESS;
150 pollset_lock_rings();
152 if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) {
153 elem = APR_RING_FIRST(&(pollset->p->free_ring));
154 APR_RING_REMOVE(elem, link);
157 elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t));
158 APR_RING_ELEM_INIT(elem, link);
160 elem->pfd = *descriptor;
162 if (descriptor->desc_type == APR_POLL_SOCKET) {
163 fd = descriptor->desc.s->socketdes;
166 fd = descriptor->desc.f->filedes;
169 if (descriptor->reqevents & APR_POLLIN) {
170 EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, elem);
172 if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0,
174 rv = apr_get_netos_error();
178 if (descriptor->reqevents & APR_POLLOUT && rv == APR_SUCCESS) {
179 EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, elem);
181 if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0,
183 rv = apr_get_netos_error();
187 if (rv == APR_SUCCESS) {
188 APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link);
191 APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link);
194 pollset_unlock_rings();
199 static apr_status_t impl_pollset_remove(apr_pollset_t *pollset,
200 const apr_pollfd_t *descriptor)
206 pollset_lock_rings();
208 if (descriptor->desc_type == APR_POLL_SOCKET) {
209 fd = descriptor->desc.s->socketdes;
212 fd = descriptor->desc.f->filedes;
215 rv = APR_NOTFOUND; /* unless at least one of the specified conditions is */
216 if (descriptor->reqevents & APR_POLLIN) {
217 EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
219 if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0,
225 if (descriptor->reqevents & APR_POLLOUT) {
226 EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
228 if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0,
234 for (ep = APR_RING_FIRST(&(pollset->p->query_ring));
235 ep != APR_RING_SENTINEL(&(pollset->p->query_ring),
237 ep = APR_RING_NEXT(ep, link)) {
239 if (descriptor->desc.s == ep->pfd.desc.s) {
240 APR_RING_REMOVE(ep, link);
241 APR_RING_INSERT_TAIL(&(pollset->p->dead_ring),
242 ep, pfd_elem_t, link);
247 pollset_unlock_rings();
252 static apr_status_t impl_pollset_poll(apr_pollset_t *pollset,
253 apr_interval_time_t timeout,
255 const apr_pollfd_t **descriptors)
258 struct timespec tv, *tvptr;
259 apr_status_t rv = APR_SUCCESS;
266 tv.tv_sec = (long) apr_time_sec(timeout);
267 tv.tv_nsec = (long) apr_time_usec(timeout) * 1000;
271 ret = kevent(pollset->p->kqueue_fd, NULL, 0, pollset->p->ke_set,
272 pollset->p->setsize, tvptr);
275 rv = apr_get_netos_error();
281 for (i = 0, j = 0; i < ret; i++) {
282 fd = (((pfd_elem_t*)(pollset->p->ke_set[i].udata))->pfd);
283 if ((pollset->flags & APR_POLLSET_WAKEABLE) &&
284 fd.desc_type == APR_POLL_FILE &&
285 fd.desc.f == pollset->wakeup_pipe[0]) {
286 apr_pollset_drain_wakeup_pipe(pollset);
290 pollset->p->result_set[j] = fd;
291 pollset->p->result_set[j].rtnevents =
292 get_kqueue_revent(pollset->p->ke_set[i].filter,
293 pollset->p->ke_set[i].flags);
297 if ((*num = j)) { /* any event besides wakeup pipe? */
300 *descriptors = pollset->p->result_set;
306 pollset_lock_rings();
308 /* Shift all PFDs in the Dead Ring to the Free Ring */
309 APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring),
312 pollset_unlock_rings();
317 static apr_pollset_provider_t impl = {
322 impl_pollset_cleanup,
326 apr_pollset_provider_t *apr_pollset_provider_kqueue = &impl;
328 static apr_status_t cb_cleanup(void *b_)
330 apr_pollcb_t *pollcb = (apr_pollcb_t *) b_;
335 static apr_status_t impl_pollcb_create(apr_pollcb_t *pollcb,
344 return apr_get_netos_error();
351 if ((flags = fcntl(fd, F_GETFD)) == -1) {
359 if (fcntl(fd, F_SETFD, flags) == -1) {
368 pollcb->pollset.ke = (struct kevent *)apr_pcalloc(p, 2 * size * sizeof(struct kevent));
369 apr_pool_cleanup_register(p, pollcb, cb_cleanup, apr_pool_cleanup_null);
374 static apr_status_t impl_pollcb_add(apr_pollcb_t *pollcb,
375 apr_pollfd_t *descriptor)
379 apr_status_t rv = APR_SUCCESS;
381 if (descriptor->desc_type == APR_POLL_SOCKET) {
382 fd = descriptor->desc.s->socketdes;
385 fd = descriptor->desc.f->filedes;
388 if (descriptor->reqevents & APR_POLLIN) {
389 EV_SET(&ev, fd, EVFILT_READ, EV_ADD, 0, 0, descriptor);
391 if (kevent(pollcb->fd, &ev, 1, NULL, 0, NULL) == -1) {
392 rv = apr_get_netos_error();
396 if (descriptor->reqevents & APR_POLLOUT && rv == APR_SUCCESS) {
397 EV_SET(&ev, fd, EVFILT_WRITE, EV_ADD, 0, 0, descriptor);
399 if (kevent(pollcb->fd, &ev, 1, NULL, 0, NULL) == -1) {
400 rv = apr_get_netos_error();
407 static apr_status_t impl_pollcb_remove(apr_pollcb_t *pollcb,
408 apr_pollfd_t *descriptor)
414 if (descriptor->desc_type == APR_POLL_SOCKET) {
415 fd = descriptor->desc.s->socketdes;
418 fd = descriptor->desc.f->filedes;
421 rv = APR_NOTFOUND; /* unless at least one of the specified conditions is */
422 if (descriptor->reqevents & APR_POLLIN) {
423 EV_SET(&ev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
425 if (kevent(pollcb->fd, &ev, 1, NULL, 0, NULL) != -1) {
430 if (descriptor->reqevents & APR_POLLOUT) {
431 EV_SET(&ev, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
433 if (kevent(pollcb->fd, &ev, 1, NULL, 0, NULL) != -1) {
442 static apr_status_t impl_pollcb_poll(apr_pollcb_t *pollcb,
443 apr_interval_time_t timeout,
444 apr_pollcb_cb_t func,
448 struct timespec tv, *tvptr;
449 apr_status_t rv = APR_SUCCESS;
455 tv.tv_sec = (long) apr_time_sec(timeout);
456 tv.tv_nsec = (long) apr_time_usec(timeout) * 1000;
460 ret = kevent(pollcb->fd, NULL, 0, pollcb->pollset.ke, 2 * pollcb->nalloc,
464 rv = apr_get_netos_error();
470 for (i = 0; i < ret; i++) {
471 apr_pollfd_t *pollfd = (apr_pollfd_t *)(pollcb->pollset.ke[i].udata);
473 pollfd->rtnevents = get_kqueue_revent(pollcb->pollset.ke[i].filter,
474 pollcb->pollset.ke[i].flags);
476 rv = func(baton, pollfd);
487 static apr_pollcb_provider_t impl_cb = {
495 apr_pollcb_provider_t *apr_pollcb_provider_kqueue = &impl_cb;
497 #endif /* HAVE_KQUEUE */