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_atomic.h"
22 #include "apr_arch_file_io.h"
23 #include "apr_arch_networkio.h"
24 #include "apr_arch_poll_private.h"
25 #include "apr_arch_inherit.h"
27 #if defined(HAVE_PORT_CREATE)
29 static apr_int16_t get_event(apr_int16_t event)
33 if (event & APR_POLLIN)
35 if (event & APR_POLLPRI)
37 if (event & APR_POLLOUT)
39 /* POLLERR, POLLHUP, and POLLNVAL aren't valid as requested events */
44 static apr_int16_t get_revent(apr_int16_t event)
65 struct apr_pollset_private_t
68 port_event_t *port_set;
69 apr_pollfd_t *result_set;
71 /* A thread mutex to protect operations on the rings */
72 apr_thread_mutex_t *ring_lock;
74 /* A ring containing all of the pollfd_t that are active */
75 APR_RING_HEAD(pfd_query_ring_t, pfd_elem_t) query_ring;
76 /* A ring containing the pollfd_t that will be added on the
77 * next call to apr_pollset_poll().
79 APR_RING_HEAD(pfd_add_ring_t, pfd_elem_t) add_ring;
80 /* A ring of pollfd_t that have been used, and then _remove'd */
81 APR_RING_HEAD(pfd_free_ring_t, pfd_elem_t) free_ring;
82 /* A ring of pollfd_t where rings that have been _remove'd but
83 might still be inside a _poll */
84 APR_RING_HEAD(pfd_dead_ring_t, pfd_elem_t) dead_ring;
85 /* number of threads in poll */
86 volatile apr_uint32_t waiting;
89 static apr_status_t call_port_getn(int port, port_event_t list[],
90 unsigned int max, unsigned int *nget,
91 apr_interval_time_t timeout)
93 struct timespec tv, *tvptr;
95 apr_status_t rv = APR_SUCCESS;
101 tv.tv_sec = (long) apr_time_sec(timeout);
102 tv.tv_nsec = (long) apr_time_usec(timeout) * 1000;
106 list[0].portev_user = (void *)-1; /* so we can double check that an
110 ret = port_getn(port, list, max, nget, tvptr);
111 /* Note: 32-bit port_getn() on Solaris 10 x86 returns large negative
112 * values instead of 0 when returning immediately.
116 rv = apr_get_netos_error();
121 if (*nget > 0 && list[0].portev_user != (void *)-1) {
122 /* This confusing API can return an event at the same time
123 * that it reports EINTR or ETIME. If that occurs, just
124 * report the event. With EINTR, nget can be > 0 without
125 * any event, so check that portev_user was filled in.
127 * (Maybe it will be simplified; see thread
128 * http://mail.opensolaris.org
129 * /pipermail/networking-discuss/2009-August/011979.html
130 * This code will still work afterwards.)
143 else if (*nget == 0) {
150 static apr_status_t impl_pollset_cleanup(apr_pollset_t *pollset)
152 close(pollset->p->port_fd);
156 static apr_status_t impl_pollset_create(apr_pollset_t *pollset,
161 apr_status_t rv = APR_SUCCESS;
162 pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t));
164 if (flags & APR_POLLSET_THREADSAFE &&
165 ((rv = apr_thread_mutex_create(&pollset->p->ring_lock,
166 APR_THREAD_MUTEX_DEFAULT,
167 p)) != APR_SUCCESS)) {
172 if (flags & APR_POLLSET_THREADSAFE) {
177 pollset->p->waiting = 0;
179 pollset->p->port_set = apr_palloc(p, size * sizeof(port_event_t));
181 pollset->p->port_fd = port_create();
183 if (pollset->p->port_fd < 0) {
185 return apr_get_netos_error();
191 if ((flags = fcntl(pollset->p->port_fd, F_GETFD)) == -1) {
193 close(pollset->p->port_fd);
199 if (fcntl(pollset->p->port_fd, F_SETFD, flags) == -1) {
201 close(pollset->p->port_fd);
207 pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
209 APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link);
210 APR_RING_INIT(&pollset->p->add_ring, pfd_elem_t, link);
211 APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link);
212 APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link);
217 static apr_status_t impl_pollset_add(apr_pollset_t *pollset,
218 const apr_pollfd_t *descriptor)
223 apr_status_t rv = APR_SUCCESS;
225 pollset_lock_rings();
227 if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) {
228 elem = APR_RING_FIRST(&(pollset->p->free_ring));
229 APR_RING_REMOVE(elem, link);
232 elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t));
233 APR_RING_ELEM_INIT(elem, link);
234 elem->on_query_ring = 0;
236 elem->pfd = *descriptor;
238 if (descriptor->desc_type == APR_POLL_SOCKET) {
239 fd = descriptor->desc.s->socketdes;
242 fd = descriptor->desc.f->filedes;
245 /* If another thread is polling, notify the kernel immediately; otherwise,
246 * wait until the next call to apr_pollset_poll().
248 if (apr_atomic_read32(&pollset->p->waiting)) {
249 res = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, fd,
250 get_event(descriptor->reqevents), (void *)elem);
253 rv = apr_get_netos_error();
254 APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link);
257 elem->on_query_ring = 1;
258 APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link);
262 APR_RING_INSERT_TAIL(&(pollset->p->add_ring), elem, pfd_elem_t, link);
265 pollset_unlock_rings();
270 static apr_status_t impl_pollset_remove(apr_pollset_t *pollset,
271 const apr_pollfd_t *descriptor)
275 apr_status_t rv = APR_SUCCESS;
280 pollset_lock_rings();
282 if (descriptor->desc_type == APR_POLL_SOCKET) {
283 fd = descriptor->desc.s->socketdes;
286 fd = descriptor->desc.f->filedes;
289 /* Search the add ring first. This ring is often shorter,
290 * and it often contains the descriptor being removed.
291 * (For the common scenario where apr_pollset_poll()
292 * returns activity for the descriptor and the descriptor
293 * is then removed from the pollset, it will have just
294 * been moved to the add ring by apr_pollset_poll().)
296 * If it is on the add ring, it isn't associated with the
297 * event port yet/anymore.
300 for (ep = APR_RING_FIRST(&(pollset->p->add_ring));
301 ep != APR_RING_SENTINEL(&(pollset->p->add_ring),
303 ep = APR_RING_NEXT(ep, link)) {
305 if (descriptor->desc.s == ep->pfd.desc.s) {
307 APR_RING_REMOVE(ep, link);
308 APR_RING_INSERT_TAIL(&(pollset->p->free_ring),
309 ep, pfd_elem_t, link);
315 res = port_dissociate(pollset->p->port_fd, PORT_SOURCE_FD, fd);
318 /* The expected case for this failure is that another
319 * thread's call to port_getn() returned this fd and
320 * disassociated the fd from the event port, and
321 * impl_pollset_poll() is blocked on the ring lock,
322 * which this thread holds.
328 for (ep = APR_RING_FIRST(&(pollset->p->query_ring));
329 ep != APR_RING_SENTINEL(&(pollset->p->query_ring),
331 ep = APR_RING_NEXT(ep, link)) {
333 if (descriptor->desc.s == ep->pfd.desc.s) {
334 APR_RING_REMOVE(ep, link);
335 ep->on_query_ring = 0;
336 APR_RING_INSERT_TAIL(&(pollset->p->dead_ring),
337 ep, pfd_elem_t, link);
346 pollset_unlock_rings();
351 static apr_status_t impl_pollset_poll(apr_pollset_t *pollset,
352 apr_interval_time_t timeout,
354 const apr_pollfd_t **descriptors)
360 apr_status_t rv = APR_SUCCESS;
365 pollset_lock_rings();
367 apr_atomic_inc32(&pollset->p->waiting);
369 while (!APR_RING_EMPTY(&(pollset->p->add_ring), pfd_elem_t, link)) {
370 ep = APR_RING_FIRST(&(pollset->p->add_ring));
371 APR_RING_REMOVE(ep, link);
373 if (ep->pfd.desc_type == APR_POLL_SOCKET) {
374 fd = ep->pfd.desc.s->socketdes;
377 fd = ep->pfd.desc.f->filedes;
380 ret = port_associate(pollset->p->port_fd, PORT_SOURCE_FD,
381 fd, get_event(ep->pfd.reqevents), ep);
383 rv = apr_get_netos_error();
384 APR_RING_INSERT_TAIL(&(pollset->p->free_ring), ep, pfd_elem_t, link);
388 ep->on_query_ring = 1;
389 APR_RING_INSERT_TAIL(&(pollset->p->query_ring), ep, pfd_elem_t, link);
392 pollset_unlock_rings();
394 if (rv != APR_SUCCESS) {
395 apr_atomic_dec32(&pollset->p->waiting);
399 rv = call_port_getn(pollset->p->port_fd, pollset->p->port_set,
400 pollset->nalloc, &nget, timeout);
402 /* decrease the waiting ASAP to reduce the window for calling
403 port_associate within apr_pollset_add() */
404 apr_atomic_dec32(&pollset->p->waiting);
409 pollset_lock_rings();
411 for (i = 0, j = 0; i < nget; i++) {
412 fp = (((pfd_elem_t*)(pollset->p->port_set[i].portev_user))->pfd);
413 if ((pollset->flags & APR_POLLSET_WAKEABLE) &&
414 fp.desc_type == APR_POLL_FILE &&
415 fp.desc.f == pollset->wakeup_pipe[0]) {
416 apr_pollset_drain_wakeup_pipe(pollset);
420 pollset->p->result_set[j] = fp;
421 pollset->p->result_set[j].rtnevents =
422 get_revent(pollset->p->port_set[i].portev_events);
424 /* If the ring element is still on the query ring, move it
425 * to the add ring for re-association with the event port
426 * later. (It may have already been moved to the dead ring
427 * by a call to pollset_remove on another thread.)
429 ep = (pfd_elem_t *)pollset->p->port_set[i].portev_user;
430 if (ep->on_query_ring) {
431 APR_RING_REMOVE(ep, link);
432 ep->on_query_ring = 0;
433 APR_RING_INSERT_TAIL(&(pollset->p->add_ring), ep,
439 pollset_unlock_rings();
440 if ((*num = j)) { /* any event besides wakeup pipe? */
443 *descriptors = pollset->p->result_set;
448 pollset_lock_rings();
450 /* Shift all PFDs in the Dead Ring to the Free Ring */
451 APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), pfd_elem_t, link);
453 pollset_unlock_rings();
458 static apr_pollset_provider_t impl = {
463 impl_pollset_cleanup,
467 apr_pollset_provider_t *apr_pollset_provider_port = &impl;
469 static apr_status_t cb_cleanup(void *p_)
471 apr_pollcb_t *pollcb = (apr_pollcb_t *) p_;
476 static apr_status_t impl_pollcb_create(apr_pollcb_t *pollcb,
481 pollcb->fd = port_create();
483 if (pollcb->fd < 0) {
484 return apr_get_netos_error();
491 if ((flags = fcntl(pollcb->fd, F_GETFD)) == -1) {
499 if (fcntl(pollcb->fd, F_SETFD, flags) == -1) {
507 pollcb->pollset.port = apr_palloc(p, size * sizeof(port_event_t));
508 apr_pool_cleanup_register(p, pollcb, cb_cleanup, apr_pool_cleanup_null);
513 static apr_status_t impl_pollcb_add(apr_pollcb_t *pollcb,
514 apr_pollfd_t *descriptor)
518 if (descriptor->desc_type == APR_POLL_SOCKET) {
519 fd = descriptor->desc.s->socketdes;
522 fd = descriptor->desc.f->filedes;
525 ret = port_associate(pollcb->fd, PORT_SOURCE_FD, fd,
526 get_event(descriptor->reqevents), descriptor);
529 return apr_get_netos_error();
535 static apr_status_t impl_pollcb_remove(apr_pollcb_t *pollcb,
536 apr_pollfd_t *descriptor)
540 if (descriptor->desc_type == APR_POLL_SOCKET) {
541 fd = descriptor->desc.s->socketdes;
544 fd = descriptor->desc.f->filedes;
547 ret = port_dissociate(pollcb->fd, PORT_SOURCE_FD, fd);
556 static apr_status_t impl_pollcb_poll(apr_pollcb_t *pollcb,
557 apr_interval_time_t timeout,
558 apr_pollcb_cb_t func,
561 apr_pollfd_t *pollfd;
563 unsigned int i, nget = 1;
565 rv = call_port_getn(pollcb->fd, pollcb->pollset.port, pollcb->nalloc,
569 for (i = 0; i < nget; i++) {
570 pollfd = (apr_pollfd_t *)(pollcb->pollset.port[i].portev_user);
571 pollfd->rtnevents = get_revent(pollcb->pollset.port[i].portev_events);
573 rv = func(baton, pollfd);
577 rv = apr_pollcb_add(pollcb, pollfd);
584 static apr_pollcb_provider_t impl_cb = {
592 apr_pollcb_provider_t *apr_pollcb_provider_port = &impl_cb;
594 #endif /* HAVE_PORT_CREATE */