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)
195 if (fcntl(pollset->p->port_fd, F_SETFD, flags) == -1)
199 pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
201 APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link);
202 APR_RING_INIT(&pollset->p->add_ring, pfd_elem_t, link);
203 APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link);
204 APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link);
209 static apr_status_t impl_pollset_add(apr_pollset_t *pollset,
210 const apr_pollfd_t *descriptor)
215 apr_status_t rv = APR_SUCCESS;
217 pollset_lock_rings();
219 if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) {
220 elem = APR_RING_FIRST(&(pollset->p->free_ring));
221 APR_RING_REMOVE(elem, link);
224 elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t));
225 APR_RING_ELEM_INIT(elem, link);
226 elem->on_query_ring = 0;
228 elem->pfd = *descriptor;
230 if (descriptor->desc_type == APR_POLL_SOCKET) {
231 fd = descriptor->desc.s->socketdes;
234 fd = descriptor->desc.f->filedes;
237 /* If another thread is polling, notify the kernel immediately; otherwise,
238 * wait until the next call to apr_pollset_poll().
240 if (apr_atomic_read32(&pollset->p->waiting)) {
241 res = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, fd,
242 get_event(descriptor->reqevents), (void *)elem);
245 rv = apr_get_netos_error();
246 APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link);
249 elem->on_query_ring = 1;
250 APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link);
254 APR_RING_INSERT_TAIL(&(pollset->p->add_ring), elem, pfd_elem_t, link);
257 pollset_unlock_rings();
262 static apr_status_t impl_pollset_remove(apr_pollset_t *pollset,
263 const apr_pollfd_t *descriptor)
267 apr_status_t rv = APR_SUCCESS;
272 pollset_lock_rings();
274 if (descriptor->desc_type == APR_POLL_SOCKET) {
275 fd = descriptor->desc.s->socketdes;
278 fd = descriptor->desc.f->filedes;
281 /* Search the add ring first. This ring is often shorter,
282 * and it often contains the descriptor being removed.
283 * (For the common scenario where apr_pollset_poll()
284 * returns activity for the descriptor and the descriptor
285 * is then removed from the pollset, it will have just
286 * been moved to the add ring by apr_pollset_poll().)
288 * If it is on the add ring, it isn't associated with the
289 * event port yet/anymore.
292 for (ep = APR_RING_FIRST(&(pollset->p->add_ring));
293 ep != APR_RING_SENTINEL(&(pollset->p->add_ring),
295 ep = APR_RING_NEXT(ep, link)) {
297 if (descriptor->desc.s == ep->pfd.desc.s) {
299 APR_RING_REMOVE(ep, link);
300 APR_RING_INSERT_TAIL(&(pollset->p->free_ring),
301 ep, pfd_elem_t, link);
307 res = port_dissociate(pollset->p->port_fd, PORT_SOURCE_FD, fd);
310 /* The expected case for this failure is that another
311 * thread's call to port_getn() returned this fd and
312 * disassociated the fd from the event port, and
313 * impl_pollset_poll() is blocked on the ring lock,
314 * which this thread holds.
320 for (ep = APR_RING_FIRST(&(pollset->p->query_ring));
321 ep != APR_RING_SENTINEL(&(pollset->p->query_ring),
323 ep = APR_RING_NEXT(ep, link)) {
325 if (descriptor->desc.s == ep->pfd.desc.s) {
326 APR_RING_REMOVE(ep, link);
327 ep->on_query_ring = 0;
328 APR_RING_INSERT_TAIL(&(pollset->p->dead_ring),
329 ep, pfd_elem_t, link);
338 pollset_unlock_rings();
343 static apr_status_t impl_pollset_poll(apr_pollset_t *pollset,
344 apr_interval_time_t timeout,
346 const apr_pollfd_t **descriptors)
352 apr_status_t rv = APR_SUCCESS;
357 pollset_lock_rings();
359 apr_atomic_inc32(&pollset->p->waiting);
361 while (!APR_RING_EMPTY(&(pollset->p->add_ring), pfd_elem_t, link)) {
362 ep = APR_RING_FIRST(&(pollset->p->add_ring));
363 APR_RING_REMOVE(ep, link);
365 if (ep->pfd.desc_type == APR_POLL_SOCKET) {
366 fd = ep->pfd.desc.s->socketdes;
369 fd = ep->pfd.desc.f->filedes;
372 ret = port_associate(pollset->p->port_fd, PORT_SOURCE_FD,
373 fd, get_event(ep->pfd.reqevents), ep);
375 rv = apr_get_netos_error();
376 APR_RING_INSERT_TAIL(&(pollset->p->free_ring), ep, pfd_elem_t, link);
380 ep->on_query_ring = 1;
381 APR_RING_INSERT_TAIL(&(pollset->p->query_ring), ep, pfd_elem_t, link);
384 pollset_unlock_rings();
386 if (rv != APR_SUCCESS) {
387 apr_atomic_dec32(&pollset->p->waiting);
391 rv = call_port_getn(pollset->p->port_fd, pollset->p->port_set,
392 pollset->nalloc, &nget, timeout);
394 /* decrease the waiting ASAP to reduce the window for calling
395 port_associate within apr_pollset_add() */
396 apr_atomic_dec32(&pollset->p->waiting);
401 pollset_lock_rings();
403 for (i = 0, j = 0; i < nget; i++) {
404 fp = (((pfd_elem_t*)(pollset->p->port_set[i].portev_user))->pfd);
405 if ((pollset->flags & APR_POLLSET_WAKEABLE) &&
406 fp.desc_type == APR_POLL_FILE &&
407 fp.desc.f == pollset->wakeup_pipe[0]) {
408 apr_pollset_drain_wakeup_pipe(pollset);
412 pollset->p->result_set[j] = fp;
413 pollset->p->result_set[j].rtnevents =
414 get_revent(pollset->p->port_set[i].portev_events);
416 /* If the ring element is still on the query ring, move it
417 * to the add ring for re-association with the event port
418 * later. (It may have already been moved to the dead ring
419 * by a call to pollset_remove on another thread.)
421 ep = (pfd_elem_t *)pollset->p->port_set[i].portev_user;
422 if (ep->on_query_ring) {
423 APR_RING_REMOVE(ep, link);
424 ep->on_query_ring = 0;
425 APR_RING_INSERT_TAIL(&(pollset->p->add_ring), ep,
431 pollset_unlock_rings();
432 if ((*num = j)) { /* any event besides wakeup pipe? */
435 *descriptors = pollset->p->result_set;
440 pollset_lock_rings();
442 /* Shift all PFDs in the Dead Ring to the Free Ring */
443 APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), pfd_elem_t, link);
445 pollset_unlock_rings();
450 static apr_pollset_provider_t impl = {
455 impl_pollset_cleanup,
459 apr_pollset_provider_t *apr_pollset_provider_port = &impl;
461 static apr_status_t cb_cleanup(void *p_)
463 apr_pollcb_t *pollcb = (apr_pollcb_t *) p_;
468 static apr_status_t impl_pollcb_create(apr_pollcb_t *pollcb,
473 pollcb->fd = port_create();
475 if (pollcb->fd < 0) {
476 return apr_get_netos_error();
482 if ((flags = fcntl(pollcb->fd, F_GETFD)) == -1)
486 if (fcntl(pollcb->fd, F_SETFD, flags) == -1)
490 pollcb->pollset.port = apr_palloc(p, size * sizeof(port_event_t));
491 apr_pool_cleanup_register(p, pollcb, cb_cleanup, apr_pool_cleanup_null);
496 static apr_status_t impl_pollcb_add(apr_pollcb_t *pollcb,
497 apr_pollfd_t *descriptor)
501 if (descriptor->desc_type == APR_POLL_SOCKET) {
502 fd = descriptor->desc.s->socketdes;
505 fd = descriptor->desc.f->filedes;
508 ret = port_associate(pollcb->fd, PORT_SOURCE_FD, fd,
509 get_event(descriptor->reqevents), descriptor);
512 return apr_get_netos_error();
518 static apr_status_t impl_pollcb_remove(apr_pollcb_t *pollcb,
519 apr_pollfd_t *descriptor)
523 if (descriptor->desc_type == APR_POLL_SOCKET) {
524 fd = descriptor->desc.s->socketdes;
527 fd = descriptor->desc.f->filedes;
530 ret = port_dissociate(pollcb->fd, PORT_SOURCE_FD, fd);
539 static apr_status_t impl_pollcb_poll(apr_pollcb_t *pollcb,
540 apr_interval_time_t timeout,
541 apr_pollcb_cb_t func,
544 apr_pollfd_t *pollfd;
546 unsigned int i, nget = 1;
548 rv = call_port_getn(pollcb->fd, pollcb->pollset.port, pollcb->nalloc,
552 for (i = 0; i < nget; i++) {
553 pollfd = (apr_pollfd_t *)(pollcb->pollset.port[i].portev_user);
554 pollfd->rtnevents = get_revent(pollcb->pollset.port[i].portev_events);
556 rv = func(baton, pollfd);
560 rv = apr_pollcb_add(pollcb, pollfd);
567 static apr_pollcb_provider_t impl_cb = {
575 apr_pollcb_provider_t *apr_pollcb_provider_port = &impl_cb;
577 #endif /* HAVE_PORT_CREATE */