From 8a39364cd53e4e8cbf6f06eced62726c0b8c1b84 Mon Sep 17 00:00:00 2001 From: Yann Ylavic Date: Wed, 26 Jan 2022 21:16:08 +0000 Subject: poll: Implement APR_POLLSET_NOCOPY for kqueue. Like with epoll, it allows to be lockless if the lifetime of the pollfd(s) is garanteed by the user. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1897518 13f79535-47bb-0310-9956-ffa450edef68 --- poll/unix/epoll.c | 2 +- poll/unix/kqueue.c | 127 +++++++++++++++---------- poll/unix/port.c | 273 ++++++++++++++++++++++++++++++----------------------- 3 files changed, 234 insertions(+), 168 deletions(-) (limited to 'poll') diff --git a/poll/unix/epoll.c b/poll/unix/epoll.c index c00a1094d..89c41a404 100644 --- a/poll/unix/epoll.c +++ b/poll/unix/epoll.c @@ -123,7 +123,7 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, } #endif - pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t)); + pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); #if APR_HAS_THREADS if ((flags & APR_POLLSET_THREADSAFE) && !(flags & APR_POLLSET_NOCOPY) && diff --git a/poll/unix/kqueue.c b/poll/unix/kqueue.c index e8a1ef95b..aa4251f1b 100644 --- a/poll/unix/kqueue.c +++ b/poll/unix/kqueue.c @@ -75,9 +75,12 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, apr_uint32_t flags) { apr_status_t rv; - pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t)); + + pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); + #if APR_HAS_THREADS if (flags & APR_POLLSET_THREADSAFE && + !(flags & APR_POLLSET_NOCOPY) && ((rv = apr_thread_mutex_create(&pollset->p->ring_lock, APR_THREAD_MUTEX_DEFAULT, p)) != APR_SUCCESS)) { @@ -99,14 +102,9 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, * for the same descriptor) */ pollset->p->setsize = 2 * size; - - pollset->p->ke_set = - (struct kevent *) apr_palloc(p, pollset->p->setsize * sizeof(struct kevent)); - - memset(pollset->p->ke_set, 0, pollset->p->setsize * sizeof(struct kevent)); + pollset->p->ke_set = apr_pcalloc(p, pollset->p->setsize * sizeof(struct kevent)); pollset->p->kqueue_fd = kqueue(); - if (pollset->p->kqueue_fd == -1) { pollset->p = NULL; return apr_get_netos_error(); @@ -133,9 +131,11 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, pollset->p->result_set = apr_palloc(p, pollset->p->setsize * sizeof(apr_pollfd_t)); - APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); + if (!(flags & APR_POLLSET_NOCOPY)) { + APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); + } return APR_SUCCESS; } @@ -144,20 +144,22 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, const apr_pollfd_t *descriptor) { apr_os_sock_t fd; - pfd_elem_t *elem; + pfd_elem_t *elem = NULL; apr_status_t rv = APR_SUCCESS; - pollset_lock_rings(); + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + pollset_lock_rings(); - if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) { - elem = APR_RING_FIRST(&(pollset->p->free_ring)); - APR_RING_REMOVE(elem, link); - } - else { - elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t)); - APR_RING_ELEM_INIT(elem, link); + if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) { + elem = APR_RING_FIRST(&(pollset->p->free_ring)); + APR_RING_REMOVE(elem, link); + } + else { + elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t)); + APR_RING_ELEM_INIT(elem, link); + } + elem->pfd = *descriptor; } - elem->pfd = *descriptor; if (descriptor->desc_type == APR_POLL_SOCKET) { fd = descriptor->desc.s->socketdes; @@ -167,7 +169,14 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, } if (descriptor->reqevents & APR_POLLIN) { - EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, elem); + if (pollset->flags & APR_POLLSET_NOCOPY) { + EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, + descriptor); + } + else { + EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, + elem); + } if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0, NULL) == -1) { @@ -176,7 +185,14 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, } if (descriptor->reqevents & APR_POLLOUT && rv == APR_SUCCESS) { - EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, elem); + if (pollset->flags & APR_POLLSET_NOCOPY) { + EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, + descriptor); + } + else { + EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, + elem); + } if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0, NULL) == -1) { @@ -184,14 +200,16 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, } } - if (rv == APR_SUCCESS) { - APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link); - } - else { - APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link); - } + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + if (rv == APR_SUCCESS) { + APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link); + } + else { + APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link); + } - pollset_unlock_rings(); + pollset_unlock_rings(); + } return rv; } @@ -199,12 +217,9 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, const apr_pollfd_t *descriptor) { - pfd_elem_t *ep; apr_status_t rv; apr_os_sock_t fd; - pollset_lock_rings(); - if (descriptor->desc_type == APR_POLL_SOCKET) { fd = descriptor->desc.s->socketdes; } @@ -231,20 +246,26 @@ static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, } } - for (ep = APR_RING_FIRST(&(pollset->p->query_ring)); - ep != APR_RING_SENTINEL(&(pollset->p->query_ring), - pfd_elem_t, link); - ep = APR_RING_NEXT(ep, link)) { + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + pfd_elem_t *ep; + + pollset_lock_rings(); - if (descriptor->desc.s == ep->pfd.desc.s) { - APR_RING_REMOVE(ep, link); - APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), - ep, pfd_elem_t, link); - break; + for (ep = APR_RING_FIRST(&(pollset->p->query_ring)); + ep != APR_RING_SENTINEL(&(pollset->p->query_ring), + pfd_elem_t, link); + ep = APR_RING_NEXT(ep, link)) { + + if (descriptor->desc.s == ep->pfd.desc.s) { + APR_RING_REMOVE(ep, link); + APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), + ep, pfd_elem_t, link); + break; + } } - } - pollset_unlock_rings(); + pollset_unlock_rings(); + } return rv; } @@ -282,7 +303,13 @@ static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, const apr_pollfd_t *fd; for (i = 0, j = 0; i < ret; i++) { - fd = &((pfd_elem_t *)pollset->p->ke_set[i].udata)->pfd; + if (pollset->flags & APR_POLLSET_NOCOPY) { + fd = (apr_pollfd_t *)pollset->p->ke_set[i].udata; + } + else { + fd = &((pfd_elem_t *)pollset->p->ke_set[i].udata)->pfd; + } + if ((pollset->flags & APR_POLLSET_WAKEABLE) && fd->desc_type == APR_POLL_FILE && fd->desc.f == pollset->wakeup_pipe[0]) { @@ -305,13 +332,15 @@ static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, } } - pollset_lock_rings(); + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + pollset_lock_rings(); - /* Shift all PFDs in the Dead Ring to the Free Ring */ - APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), - pfd_elem_t, link); + /* Shift all PFDs in the Dead Ring to the Free Ring */ + APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), + pfd_elem_t, link); - pollset_unlock_rings(); + pollset_unlock_rings(); + } return rv; } diff --git a/poll/unix/port.c b/poll/unix/port.c index 638a8cba7..f5c7a01c4 100644 --- a/poll/unix/port.c +++ b/poll/unix/port.c @@ -159,7 +159,7 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, apr_uint32_t flags) { apr_status_t rv = APR_SUCCESS; - pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t)); + pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); #if APR_HAS_THREADS if (flags & APR_POLLSET_THREADSAFE && ((rv = apr_thread_mutex_create(&pollset->p->ring_lock, @@ -206,10 +206,12 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t)); - APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->add_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->add_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); + } return rv; } @@ -222,19 +224,6 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, int res; apr_status_t rv = APR_SUCCESS; - pollset_lock_rings(); - - if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) { - elem = APR_RING_FIRST(&(pollset->p->free_ring)); - APR_RING_REMOVE(elem, link); - } - else { - elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t)); - APR_RING_ELEM_INIT(elem, link); - elem->on_query_ring = 0; - } - elem->pfd = *descriptor; - if (descriptor->desc_type == APR_POLL_SOCKET) { fd = descriptor->desc.s->socketdes; } @@ -242,27 +231,52 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, fd = descriptor->desc.f->filedes; } - /* If another thread is polling, notify the kernel immediately; otherwise, - * wait until the next call to apr_pollset_poll(). - */ - if (apr_atomic_read32(&pollset->p->waiting)) { + if (pollset->flags & APR_POLLSET_NOCOPY) { res = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, fd, - get_event(descriptor->reqevents), (void *)elem); + get_event(descriptor->reqevents), (void *)descriptor); if (res < 0) { rv = apr_get_netos_error(); - APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link); + } + } + else { + pollset_lock_rings(); + + if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) { + elem = APR_RING_FIRST(&(pollset->p->free_ring)); + APR_RING_REMOVE(elem, link); } else { - elem->on_query_ring = 1; - APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link); + elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t)); + APR_RING_ELEM_INIT(elem, link); + elem->on_query_ring = 0; + } + elem->pfd = *descriptor; + + /* If another thread is polling, notify the kernel immediately; otherwise, + * wait until the next call to apr_pollset_poll(). + */ + if (apr_atomic_read32(&pollset->p->waiting)) { + res = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, fd, + get_event(descriptor->reqevents), (void *)elem); + + if (res < 0) { + rv = apr_get_netos_error(); + APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, + pfd_elem_t, link); + } + else { + elem->on_query_ring = 1; + APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, + pfd_elem_t, link); + } + } + else { + APR_RING_INSERT_TAIL(&(pollset->p->add_ring), elem, pfd_elem_t, link); } - } - else { - APR_RING_INSERT_TAIL(&(pollset->p->add_ring), elem, pfd_elem_t, link); - } - pollset_unlock_rings(); + pollset_unlock_rings(); + } return rv; } @@ -275,9 +289,7 @@ static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, apr_status_t rv = APR_SUCCESS; int res; int err = 0; - int found; - - pollset_lock_rings(); + int found = 0; if (descriptor->desc_type == APR_POLL_SOCKET) { fd = descriptor->desc.s->socketdes; @@ -286,34 +298,36 @@ static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, fd = descriptor->desc.f->filedes; } - /* Search the add ring first. This ring is often shorter, - * and it often contains the descriptor being removed. - * (For the common scenario where apr_pollset_poll() - * returns activity for the descriptor and the descriptor - * is then removed from the pollset, it will have just - * been moved to the add ring by apr_pollset_poll().) - * - * If it is on the add ring, it isn't associated with the - * event port yet/anymore. - */ - found = 0; - for (ep = APR_RING_FIRST(&(pollset->p->add_ring)); - ep != APR_RING_SENTINEL(&(pollset->p->add_ring), - pfd_elem_t, link); - ep = APR_RING_NEXT(ep, link)) { - - if (descriptor->desc.s == ep->pfd.desc.s) { - found = 1; - APR_RING_REMOVE(ep, link); - APR_RING_INSERT_TAIL(&(pollset->p->free_ring), - ep, pfd_elem_t, link); - break; + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + pollset_lock_rings(); + + /* Search the add ring first. This ring is often shorter, + * and it often contains the descriptor being removed. + * (For the common scenario where apr_pollset_poll() + * returns activity for the descriptor and the descriptor + * is then removed from the pollset, it will have just + * been moved to the add ring by apr_pollset_poll().) + * + * If it is on the add ring, it isn't associated with the + * event port yet/anymore. + */ + for (ep = APR_RING_FIRST(&(pollset->p->add_ring)); + ep != APR_RING_SENTINEL(&(pollset->p->add_ring), + pfd_elem_t, link); + ep = APR_RING_NEXT(ep, link)) { + + if (descriptor->desc.s == ep->pfd.desc.s) { + found = 1; + APR_RING_REMOVE(ep, link); + APR_RING_INSERT_TAIL(&(pollset->p->free_ring), + ep, pfd_elem_t, link); + break; + } } } if (!found) { res = port_dissociate(pollset->p->port_fd, PORT_SOURCE_FD, fd); - if (res < 0) { /* The expected case for this failure is that another * thread's call to port_getn() returned this fd and @@ -325,25 +339,29 @@ static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, rv = APR_NOTFOUND; } - for (ep = APR_RING_FIRST(&(pollset->p->query_ring)); - ep != APR_RING_SENTINEL(&(pollset->p->query_ring), - pfd_elem_t, link); - ep = APR_RING_NEXT(ep, link)) { - - if (descriptor->desc.s == ep->pfd.desc.s) { - APR_RING_REMOVE(ep, link); - ep->on_query_ring = 0; - APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), - ep, pfd_elem_t, link); - if (ENOENT == err) { - rv = APR_SUCCESS; + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + for (ep = APR_RING_FIRST(&(pollset->p->query_ring)); + ep != APR_RING_SENTINEL(&(pollset->p->query_ring), + pfd_elem_t, link); + ep = APR_RING_NEXT(ep, link)) { + + if (descriptor->desc.s == ep->pfd.desc.s) { + APR_RING_REMOVE(ep, link); + ep->on_query_ring = 0; + APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), + ep, pfd_elem_t, link); + if (ENOENT == err) { + rv = APR_SUCCESS; + } + break; } - break; } + pollset_unlock_rings(); } } - - pollset_unlock_rings(); + else if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + pollset_unlock_rings(); + } return rv; } @@ -363,73 +381,89 @@ static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, *num = 0; nget = 1; - pollset_lock_rings(); + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + pollset_lock_rings(); - apr_atomic_inc32(&pollset->p->waiting); + apr_atomic_inc32(&pollset->p->waiting); - while (!APR_RING_EMPTY(&(pollset->p->add_ring), pfd_elem_t, link)) { - ep = APR_RING_FIRST(&(pollset->p->add_ring)); - APR_RING_REMOVE(ep, link); + while (!APR_RING_EMPTY(&(pollset->p->add_ring), pfd_elem_t, link)) { + ep = APR_RING_FIRST(&(pollset->p->add_ring)); + APR_RING_REMOVE(ep, link); - if (ep->pfd.desc_type == APR_POLL_SOCKET) { - fd = ep->pfd.desc.s->socketdes; - } - else { - fd = ep->pfd.desc.f->filedes; - } + if (ep->pfd.desc_type == APR_POLL_SOCKET) { + fd = ep->pfd.desc.s->socketdes; + } + else { + fd = ep->pfd.desc.f->filedes; + } - ret = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, - fd, get_event(ep->pfd.reqevents), ep); - if (ret < 0) { - rv = apr_get_netos_error(); - APR_RING_INSERT_TAIL(&(pollset->p->free_ring), ep, pfd_elem_t, link); - break; - } + ret = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, + fd, get_event(ep->pfd.reqevents), ep); + if (ret < 0) { + rv = apr_get_netos_error(); + APR_RING_INSERT_TAIL(&(pollset->p->free_ring), ep, pfd_elem_t, link); + break; + } - ep->on_query_ring = 1; - APR_RING_INSERT_TAIL(&(pollset->p->query_ring), ep, pfd_elem_t, link); - } + ep->on_query_ring = 1; + APR_RING_INSERT_TAIL(&(pollset->p->query_ring), ep, pfd_elem_t, link); + } - pollset_unlock_rings(); + pollset_unlock_rings(); - if (rv != APR_SUCCESS) { - apr_atomic_dec32(&pollset->p->waiting); - return rv; + if (rv != APR_SUCCESS) { + apr_atomic_dec32(&pollset->p->waiting); + return rv; + } } rv = call_port_getn(pollset->p->port_fd, pollset->p->port_set, pollset->nalloc, &nget, timeout); - /* decrease the waiting ASAP to reduce the window for calling - port_associate within apr_pollset_add() */ - apr_atomic_dec32(&pollset->p->waiting); + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + /* decrease the waiting ASAP to reduce the window for calling + port_associate within apr_pollset_add() */ + apr_atomic_dec32(&pollset->p->waiting); - pollset_lock_rings(); + pollset_lock_rings(); + } for (i = 0, j = 0; i < nget; i++) { - ep = (pfd_elem_t *)pollset->p->port_set[i].portev_user; + const apr_poll_fd_t *pfd; + + if (pollset->flags & APR_POLLSET_NOCOPY) { + pfd = (apr_pollfd_t *)pollset->p->port_set[i].portev_user; + } + else { + ep = (pfd_elem_t *)pollset->p->port_set[i].portev_user; + pfd = &ep->pfd; + } + if ((pollset->flags & APR_POLLSET_WAKEABLE) && - ep->pfd.desc_type == APR_POLL_FILE && - ep->pfd.desc.f == pollset->wakeup_pipe[0]) { + pfd->desc_type == APR_POLL_FILE && + pfd->desc.f == pollset->wakeup_pipe[0]) { apr_poll_drain_wakeup_pipe(&pollset->wakeup_set, pollset->wakeup_pipe); rv = APR_EINTR; } else { - pollset->p->result_set[j] = ep->pfd; + pollset->p->result_set[j] = *pfd; pollset->p->result_set[j].rtnevents = get_revent(pollset->p->port_set[i].portev_events); j++; } - /* If the ring element is still on the query ring, move it - * to the add ring for re-association with the event port - * later. (It may have already been moved to the dead ring - * by a call to pollset_remove on another thread.) - */ - if (ep->on_query_ring) { - APR_RING_REMOVE(ep, link); - ep->on_query_ring = 0; - APR_RING_INSERT_TAIL(&(pollset->p->add_ring), ep, - pfd_elem_t, link); + + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + /* If the ring element is still on the query ring, move it + * to the add ring for re-association with the event port + * later. (It may have already been moved to the dead ring + * by a call to pollset_remove on another thread.) + */ + if (ep->on_query_ring) { + APR_RING_REMOVE(ep, link); + ep->on_query_ring = 0; + APR_RING_INSERT_TAIL(&(pollset->p->add_ring), ep, + pfd_elem_t, link); + } } } if ((*num = j)) { /* any event besides wakeup pipe? */ @@ -439,10 +473,13 @@ static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, } } - /* Shift all PFDs in the Dead Ring to the Free Ring */ - APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), pfd_elem_t, link); + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + /* Shift all PFDs in the Dead Ring to the Free Ring */ + APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), + pfd_elem_t, link); - pollset_unlock_rings(); + pollset_unlock_rings(); + } return rv; } -- cgit v1.2.1 From cda93a2e16d4272edc2d9967fb6e8339ec1d7749 Mon Sep 17 00:00:00 2001 From: Yann Ylavic Date: Wed, 26 Jan 2022 21:17:36 +0000 Subject: Revert r1897518 (spurious changes). git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1897519 13f79535-47bb-0310-9956-ffa450edef68 --- poll/unix/epoll.c | 2 +- poll/unix/kqueue.c | 127 ++++++++++--------------- poll/unix/port.c | 273 +++++++++++++++++++++++------------------------------ 3 files changed, 168 insertions(+), 234 deletions(-) (limited to 'poll') diff --git a/poll/unix/epoll.c b/poll/unix/epoll.c index 89c41a404..c00a1094d 100644 --- a/poll/unix/epoll.c +++ b/poll/unix/epoll.c @@ -123,7 +123,7 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, } #endif - pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); + pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t)); #if APR_HAS_THREADS if ((flags & APR_POLLSET_THREADSAFE) && !(flags & APR_POLLSET_NOCOPY) && diff --git a/poll/unix/kqueue.c b/poll/unix/kqueue.c index aa4251f1b..e8a1ef95b 100644 --- a/poll/unix/kqueue.c +++ b/poll/unix/kqueue.c @@ -75,12 +75,9 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, apr_uint32_t flags) { apr_status_t rv; - - pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); - + pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t)); #if APR_HAS_THREADS if (flags & APR_POLLSET_THREADSAFE && - !(flags & APR_POLLSET_NOCOPY) && ((rv = apr_thread_mutex_create(&pollset->p->ring_lock, APR_THREAD_MUTEX_DEFAULT, p)) != APR_SUCCESS)) { @@ -102,9 +99,14 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, * for the same descriptor) */ pollset->p->setsize = 2 * size; - pollset->p->ke_set = apr_pcalloc(p, pollset->p->setsize * sizeof(struct kevent)); + + pollset->p->ke_set = + (struct kevent *) apr_palloc(p, pollset->p->setsize * sizeof(struct kevent)); + + memset(pollset->p->ke_set, 0, pollset->p->setsize * sizeof(struct kevent)); pollset->p->kqueue_fd = kqueue(); + if (pollset->p->kqueue_fd == -1) { pollset->p = NULL; return apr_get_netos_error(); @@ -131,11 +133,9 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, pollset->p->result_set = apr_palloc(p, pollset->p->setsize * sizeof(apr_pollfd_t)); - if (!(flags & APR_POLLSET_NOCOPY)) { - APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); - } + APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); return APR_SUCCESS; } @@ -144,22 +144,20 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, const apr_pollfd_t *descriptor) { apr_os_sock_t fd; - pfd_elem_t *elem = NULL; + pfd_elem_t *elem; apr_status_t rv = APR_SUCCESS; - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - pollset_lock_rings(); + pollset_lock_rings(); - if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) { - elem = APR_RING_FIRST(&(pollset->p->free_ring)); - APR_RING_REMOVE(elem, link); - } - else { - elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t)); - APR_RING_ELEM_INIT(elem, link); - } - elem->pfd = *descriptor; + if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) { + elem = APR_RING_FIRST(&(pollset->p->free_ring)); + APR_RING_REMOVE(elem, link); } + else { + elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t)); + APR_RING_ELEM_INIT(elem, link); + } + elem->pfd = *descriptor; if (descriptor->desc_type == APR_POLL_SOCKET) { fd = descriptor->desc.s->socketdes; @@ -169,14 +167,7 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, } if (descriptor->reqevents & APR_POLLIN) { - if (pollset->flags & APR_POLLSET_NOCOPY) { - EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, - descriptor); - } - else { - EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, - elem); - } + EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, elem); if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0, NULL) == -1) { @@ -185,14 +176,7 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, } if (descriptor->reqevents & APR_POLLOUT && rv == APR_SUCCESS) { - if (pollset->flags & APR_POLLSET_NOCOPY) { - EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, - descriptor); - } - else { - EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, - elem); - } + EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, elem); if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0, NULL) == -1) { @@ -200,26 +184,27 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, } } - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - if (rv == APR_SUCCESS) { - APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link); - } - else { - APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link); - } - - pollset_unlock_rings(); + if (rv == APR_SUCCESS) { + APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link); + } + else { + APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link); } + pollset_unlock_rings(); + return rv; } static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, const apr_pollfd_t *descriptor) { + pfd_elem_t *ep; apr_status_t rv; apr_os_sock_t fd; + pollset_lock_rings(); + if (descriptor->desc_type == APR_POLL_SOCKET) { fd = descriptor->desc.s->socketdes; } @@ -246,27 +231,21 @@ static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, } } - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - pfd_elem_t *ep; - - pollset_lock_rings(); + for (ep = APR_RING_FIRST(&(pollset->p->query_ring)); + ep != APR_RING_SENTINEL(&(pollset->p->query_ring), + pfd_elem_t, link); + ep = APR_RING_NEXT(ep, link)) { - for (ep = APR_RING_FIRST(&(pollset->p->query_ring)); - ep != APR_RING_SENTINEL(&(pollset->p->query_ring), - pfd_elem_t, link); - ep = APR_RING_NEXT(ep, link)) { - - if (descriptor->desc.s == ep->pfd.desc.s) { - APR_RING_REMOVE(ep, link); - APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), - ep, pfd_elem_t, link); - break; - } + if (descriptor->desc.s == ep->pfd.desc.s) { + APR_RING_REMOVE(ep, link); + APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), + ep, pfd_elem_t, link); + break; } - - pollset_unlock_rings(); } + pollset_unlock_rings(); + return rv; } @@ -303,13 +282,7 @@ static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, const apr_pollfd_t *fd; for (i = 0, j = 0; i < ret; i++) { - if (pollset->flags & APR_POLLSET_NOCOPY) { - fd = (apr_pollfd_t *)pollset->p->ke_set[i].udata; - } - else { - fd = &((pfd_elem_t *)pollset->p->ke_set[i].udata)->pfd; - } - + fd = &((pfd_elem_t *)pollset->p->ke_set[i].udata)->pfd; if ((pollset->flags & APR_POLLSET_WAKEABLE) && fd->desc_type == APR_POLL_FILE && fd->desc.f == pollset->wakeup_pipe[0]) { @@ -332,15 +305,13 @@ static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, } } - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - pollset_lock_rings(); + pollset_lock_rings(); - /* Shift all PFDs in the Dead Ring to the Free Ring */ - APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), - pfd_elem_t, link); + /* Shift all PFDs in the Dead Ring to the Free Ring */ + APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), + pfd_elem_t, link); - pollset_unlock_rings(); - } + pollset_unlock_rings(); return rv; } diff --git a/poll/unix/port.c b/poll/unix/port.c index f5c7a01c4..638a8cba7 100644 --- a/poll/unix/port.c +++ b/poll/unix/port.c @@ -159,7 +159,7 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, apr_uint32_t flags) { apr_status_t rv = APR_SUCCESS; - pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); + pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t)); #if APR_HAS_THREADS if (flags & APR_POLLSET_THREADSAFE && ((rv = apr_thread_mutex_create(&pollset->p->ring_lock, @@ -206,12 +206,10 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t)); - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->add_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); - } + APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->add_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); return rv; } @@ -224,6 +222,19 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, int res; apr_status_t rv = APR_SUCCESS; + pollset_lock_rings(); + + if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) { + elem = APR_RING_FIRST(&(pollset->p->free_ring)); + APR_RING_REMOVE(elem, link); + } + else { + elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t)); + APR_RING_ELEM_INIT(elem, link); + elem->on_query_ring = 0; + } + elem->pfd = *descriptor; + if (descriptor->desc_type == APR_POLL_SOCKET) { fd = descriptor->desc.s->socketdes; } @@ -231,53 +242,28 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, fd = descriptor->desc.f->filedes; } - if (pollset->flags & APR_POLLSET_NOCOPY) { + /* If another thread is polling, notify the kernel immediately; otherwise, + * wait until the next call to apr_pollset_poll(). + */ + if (apr_atomic_read32(&pollset->p->waiting)) { res = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, fd, - get_event(descriptor->reqevents), (void *)descriptor); + get_event(descriptor->reqevents), (void *)elem); if (res < 0) { rv = apr_get_netos_error(); - } - } - else { - pollset_lock_rings(); - - if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) { - elem = APR_RING_FIRST(&(pollset->p->free_ring)); - APR_RING_REMOVE(elem, link); - } - else { - elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t)); - APR_RING_ELEM_INIT(elem, link); - elem->on_query_ring = 0; - } - elem->pfd = *descriptor; - - /* If another thread is polling, notify the kernel immediately; otherwise, - * wait until the next call to apr_pollset_poll(). - */ - if (apr_atomic_read32(&pollset->p->waiting)) { - res = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, fd, - get_event(descriptor->reqevents), (void *)elem); - - if (res < 0) { - rv = apr_get_netos_error(); - APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, - pfd_elem_t, link); - } - else { - elem->on_query_ring = 1; - APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, - pfd_elem_t, link); - } + APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link); } else { - APR_RING_INSERT_TAIL(&(pollset->p->add_ring), elem, pfd_elem_t, link); + elem->on_query_ring = 1; + APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link); } - - pollset_unlock_rings(); + } + else { + APR_RING_INSERT_TAIL(&(pollset->p->add_ring), elem, pfd_elem_t, link); } + pollset_unlock_rings(); + return rv; } @@ -289,7 +275,9 @@ static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, apr_status_t rv = APR_SUCCESS; int res; int err = 0; - int found = 0; + int found; + + pollset_lock_rings(); if (descriptor->desc_type == APR_POLL_SOCKET) { fd = descriptor->desc.s->socketdes; @@ -298,36 +286,34 @@ static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, fd = descriptor->desc.f->filedes; } - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - pollset_lock_rings(); - - /* Search the add ring first. This ring is often shorter, - * and it often contains the descriptor being removed. - * (For the common scenario where apr_pollset_poll() - * returns activity for the descriptor and the descriptor - * is then removed from the pollset, it will have just - * been moved to the add ring by apr_pollset_poll().) - * - * If it is on the add ring, it isn't associated with the - * event port yet/anymore. - */ - for (ep = APR_RING_FIRST(&(pollset->p->add_ring)); - ep != APR_RING_SENTINEL(&(pollset->p->add_ring), - pfd_elem_t, link); - ep = APR_RING_NEXT(ep, link)) { - - if (descriptor->desc.s == ep->pfd.desc.s) { - found = 1; - APR_RING_REMOVE(ep, link); - APR_RING_INSERT_TAIL(&(pollset->p->free_ring), - ep, pfd_elem_t, link); - break; - } + /* Search the add ring first. This ring is often shorter, + * and it often contains the descriptor being removed. + * (For the common scenario where apr_pollset_poll() + * returns activity for the descriptor and the descriptor + * is then removed from the pollset, it will have just + * been moved to the add ring by apr_pollset_poll().) + * + * If it is on the add ring, it isn't associated with the + * event port yet/anymore. + */ + found = 0; + for (ep = APR_RING_FIRST(&(pollset->p->add_ring)); + ep != APR_RING_SENTINEL(&(pollset->p->add_ring), + pfd_elem_t, link); + ep = APR_RING_NEXT(ep, link)) { + + if (descriptor->desc.s == ep->pfd.desc.s) { + found = 1; + APR_RING_REMOVE(ep, link); + APR_RING_INSERT_TAIL(&(pollset->p->free_ring), + ep, pfd_elem_t, link); + break; } } if (!found) { res = port_dissociate(pollset->p->port_fd, PORT_SOURCE_FD, fd); + if (res < 0) { /* The expected case for this failure is that another * thread's call to port_getn() returned this fd and @@ -339,29 +325,25 @@ static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, rv = APR_NOTFOUND; } - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - for (ep = APR_RING_FIRST(&(pollset->p->query_ring)); - ep != APR_RING_SENTINEL(&(pollset->p->query_ring), - pfd_elem_t, link); - ep = APR_RING_NEXT(ep, link)) { - - if (descriptor->desc.s == ep->pfd.desc.s) { - APR_RING_REMOVE(ep, link); - ep->on_query_ring = 0; - APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), - ep, pfd_elem_t, link); - if (ENOENT == err) { - rv = APR_SUCCESS; - } - break; + for (ep = APR_RING_FIRST(&(pollset->p->query_ring)); + ep != APR_RING_SENTINEL(&(pollset->p->query_ring), + pfd_elem_t, link); + ep = APR_RING_NEXT(ep, link)) { + + if (descriptor->desc.s == ep->pfd.desc.s) { + APR_RING_REMOVE(ep, link); + ep->on_query_ring = 0; + APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), + ep, pfd_elem_t, link); + if (ENOENT == err) { + rv = APR_SUCCESS; } + break; } - pollset_unlock_rings(); } } - else if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - pollset_unlock_rings(); - } + + pollset_unlock_rings(); return rv; } @@ -381,89 +363,73 @@ static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, *num = 0; nget = 1; - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - pollset_lock_rings(); + pollset_lock_rings(); - apr_atomic_inc32(&pollset->p->waiting); + apr_atomic_inc32(&pollset->p->waiting); - while (!APR_RING_EMPTY(&(pollset->p->add_ring), pfd_elem_t, link)) { - ep = APR_RING_FIRST(&(pollset->p->add_ring)); - APR_RING_REMOVE(ep, link); - - if (ep->pfd.desc_type == APR_POLL_SOCKET) { - fd = ep->pfd.desc.s->socketdes; - } - else { - fd = ep->pfd.desc.f->filedes; - } + while (!APR_RING_EMPTY(&(pollset->p->add_ring), pfd_elem_t, link)) { + ep = APR_RING_FIRST(&(pollset->p->add_ring)); + APR_RING_REMOVE(ep, link); - ret = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, - fd, get_event(ep->pfd.reqevents), ep); - if (ret < 0) { - rv = apr_get_netos_error(); - APR_RING_INSERT_TAIL(&(pollset->p->free_ring), ep, pfd_elem_t, link); - break; - } + if (ep->pfd.desc_type == APR_POLL_SOCKET) { + fd = ep->pfd.desc.s->socketdes; + } + else { + fd = ep->pfd.desc.f->filedes; + } - ep->on_query_ring = 1; - APR_RING_INSERT_TAIL(&(pollset->p->query_ring), ep, pfd_elem_t, link); + ret = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, + fd, get_event(ep->pfd.reqevents), ep); + if (ret < 0) { + rv = apr_get_netos_error(); + APR_RING_INSERT_TAIL(&(pollset->p->free_ring), ep, pfd_elem_t, link); + break; } - pollset_unlock_rings(); + ep->on_query_ring = 1; + APR_RING_INSERT_TAIL(&(pollset->p->query_ring), ep, pfd_elem_t, link); + } - if (rv != APR_SUCCESS) { - apr_atomic_dec32(&pollset->p->waiting); - return rv; - } + pollset_unlock_rings(); + + if (rv != APR_SUCCESS) { + apr_atomic_dec32(&pollset->p->waiting); + return rv; } rv = call_port_getn(pollset->p->port_fd, pollset->p->port_set, pollset->nalloc, &nget, timeout); - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - /* decrease the waiting ASAP to reduce the window for calling - port_associate within apr_pollset_add() */ - apr_atomic_dec32(&pollset->p->waiting); + /* decrease the waiting ASAP to reduce the window for calling + port_associate within apr_pollset_add() */ + apr_atomic_dec32(&pollset->p->waiting); - pollset_lock_rings(); - } + pollset_lock_rings(); for (i = 0, j = 0; i < nget; i++) { - const apr_poll_fd_t *pfd; - - if (pollset->flags & APR_POLLSET_NOCOPY) { - pfd = (apr_pollfd_t *)pollset->p->port_set[i].portev_user; - } - else { - ep = (pfd_elem_t *)pollset->p->port_set[i].portev_user; - pfd = &ep->pfd; - } - + ep = (pfd_elem_t *)pollset->p->port_set[i].portev_user; if ((pollset->flags & APR_POLLSET_WAKEABLE) && - pfd->desc_type == APR_POLL_FILE && - pfd->desc.f == pollset->wakeup_pipe[0]) { + ep->pfd.desc_type == APR_POLL_FILE && + ep->pfd.desc.f == pollset->wakeup_pipe[0]) { apr_poll_drain_wakeup_pipe(&pollset->wakeup_set, pollset->wakeup_pipe); rv = APR_EINTR; } else { - pollset->p->result_set[j] = *pfd; + pollset->p->result_set[j] = ep->pfd; pollset->p->result_set[j].rtnevents = get_revent(pollset->p->port_set[i].portev_events); j++; } - - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - /* If the ring element is still on the query ring, move it - * to the add ring for re-association with the event port - * later. (It may have already been moved to the dead ring - * by a call to pollset_remove on another thread.) - */ - if (ep->on_query_ring) { - APR_RING_REMOVE(ep, link); - ep->on_query_ring = 0; - APR_RING_INSERT_TAIL(&(pollset->p->add_ring), ep, - pfd_elem_t, link); - } + /* If the ring element is still on the query ring, move it + * to the add ring for re-association with the event port + * later. (It may have already been moved to the dead ring + * by a call to pollset_remove on another thread.) + */ + if (ep->on_query_ring) { + APR_RING_REMOVE(ep, link); + ep->on_query_ring = 0; + APR_RING_INSERT_TAIL(&(pollset->p->add_ring), ep, + pfd_elem_t, link); } } if ((*num = j)) { /* any event besides wakeup pipe? */ @@ -473,13 +439,10 @@ static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, } } - if (!(pollset->flags & APR_POLLSET_NOCOPY)) { - /* Shift all PFDs in the Dead Ring to the Free Ring */ - APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), - pfd_elem_t, link); + /* Shift all PFDs in the Dead Ring to the Free Ring */ + APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), pfd_elem_t, link); - pollset_unlock_rings(); - } + pollset_unlock_rings(); return rv; } -- cgit v1.2.1 From 9155323a563d055a226a4e6cfc1f2fb9f8d2abf0 Mon Sep 17 00:00:00 2001 From: Yann Ylavic Date: Wed, 26 Jan 2022 21:20:18 +0000 Subject: poll: Implement APR_POLLSET_NOCOPY for kqueue. Like with epoll, it allows to be lockless if the lifetime of the pollfd(s) is garanteed by the user. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1897520 13f79535-47bb-0310-9956-ffa450edef68 --- poll/unix/epoll.c | 2 +- poll/unix/kqueue.c | 127 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 79 insertions(+), 50 deletions(-) (limited to 'poll') diff --git a/poll/unix/epoll.c b/poll/unix/epoll.c index c00a1094d..89c41a404 100644 --- a/poll/unix/epoll.c +++ b/poll/unix/epoll.c @@ -123,7 +123,7 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, } #endif - pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t)); + pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); #if APR_HAS_THREADS if ((flags & APR_POLLSET_THREADSAFE) && !(flags & APR_POLLSET_NOCOPY) && diff --git a/poll/unix/kqueue.c b/poll/unix/kqueue.c index e8a1ef95b..aa4251f1b 100644 --- a/poll/unix/kqueue.c +++ b/poll/unix/kqueue.c @@ -75,9 +75,12 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, apr_uint32_t flags) { apr_status_t rv; - pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t)); + + pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); + #if APR_HAS_THREADS if (flags & APR_POLLSET_THREADSAFE && + !(flags & APR_POLLSET_NOCOPY) && ((rv = apr_thread_mutex_create(&pollset->p->ring_lock, APR_THREAD_MUTEX_DEFAULT, p)) != APR_SUCCESS)) { @@ -99,14 +102,9 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, * for the same descriptor) */ pollset->p->setsize = 2 * size; - - pollset->p->ke_set = - (struct kevent *) apr_palloc(p, pollset->p->setsize * sizeof(struct kevent)); - - memset(pollset->p->ke_set, 0, pollset->p->setsize * sizeof(struct kevent)); + pollset->p->ke_set = apr_pcalloc(p, pollset->p->setsize * sizeof(struct kevent)); pollset->p->kqueue_fd = kqueue(); - if (pollset->p->kqueue_fd == -1) { pollset->p = NULL; return apr_get_netos_error(); @@ -133,9 +131,11 @@ static apr_status_t impl_pollset_create(apr_pollset_t *pollset, pollset->p->result_set = apr_palloc(p, pollset->p->setsize * sizeof(apr_pollfd_t)); - APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); - APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); + if (!(flags & APR_POLLSET_NOCOPY)) { + APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); + APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); + } return APR_SUCCESS; } @@ -144,20 +144,22 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, const apr_pollfd_t *descriptor) { apr_os_sock_t fd; - pfd_elem_t *elem; + pfd_elem_t *elem = NULL; apr_status_t rv = APR_SUCCESS; - pollset_lock_rings(); + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + pollset_lock_rings(); - if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) { - elem = APR_RING_FIRST(&(pollset->p->free_ring)); - APR_RING_REMOVE(elem, link); - } - else { - elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t)); - APR_RING_ELEM_INIT(elem, link); + if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) { + elem = APR_RING_FIRST(&(pollset->p->free_ring)); + APR_RING_REMOVE(elem, link); + } + else { + elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t)); + APR_RING_ELEM_INIT(elem, link); + } + elem->pfd = *descriptor; } - elem->pfd = *descriptor; if (descriptor->desc_type == APR_POLL_SOCKET) { fd = descriptor->desc.s->socketdes; @@ -167,7 +169,14 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, } if (descriptor->reqevents & APR_POLLIN) { - EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, elem); + if (pollset->flags & APR_POLLSET_NOCOPY) { + EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, + descriptor); + } + else { + EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, + elem); + } if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0, NULL) == -1) { @@ -176,7 +185,14 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, } if (descriptor->reqevents & APR_POLLOUT && rv == APR_SUCCESS) { - EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, elem); + if (pollset->flags & APR_POLLSET_NOCOPY) { + EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, + descriptor); + } + else { + EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, + elem); + } if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0, NULL) == -1) { @@ -184,14 +200,16 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, } } - if (rv == APR_SUCCESS) { - APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link); - } - else { - APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link); - } + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + if (rv == APR_SUCCESS) { + APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link); + } + else { + APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link); + } - pollset_unlock_rings(); + pollset_unlock_rings(); + } return rv; } @@ -199,12 +217,9 @@ static apr_status_t impl_pollset_add(apr_pollset_t *pollset, static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, const apr_pollfd_t *descriptor) { - pfd_elem_t *ep; apr_status_t rv; apr_os_sock_t fd; - pollset_lock_rings(); - if (descriptor->desc_type == APR_POLL_SOCKET) { fd = descriptor->desc.s->socketdes; } @@ -231,20 +246,26 @@ static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, } } - for (ep = APR_RING_FIRST(&(pollset->p->query_ring)); - ep != APR_RING_SENTINEL(&(pollset->p->query_ring), - pfd_elem_t, link); - ep = APR_RING_NEXT(ep, link)) { + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + pfd_elem_t *ep; + + pollset_lock_rings(); - if (descriptor->desc.s == ep->pfd.desc.s) { - APR_RING_REMOVE(ep, link); - APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), - ep, pfd_elem_t, link); - break; + for (ep = APR_RING_FIRST(&(pollset->p->query_ring)); + ep != APR_RING_SENTINEL(&(pollset->p->query_ring), + pfd_elem_t, link); + ep = APR_RING_NEXT(ep, link)) { + + if (descriptor->desc.s == ep->pfd.desc.s) { + APR_RING_REMOVE(ep, link); + APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), + ep, pfd_elem_t, link); + break; + } } - } - pollset_unlock_rings(); + pollset_unlock_rings(); + } return rv; } @@ -282,7 +303,13 @@ static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, const apr_pollfd_t *fd; for (i = 0, j = 0; i < ret; i++) { - fd = &((pfd_elem_t *)pollset->p->ke_set[i].udata)->pfd; + if (pollset->flags & APR_POLLSET_NOCOPY) { + fd = (apr_pollfd_t *)pollset->p->ke_set[i].udata; + } + else { + fd = &((pfd_elem_t *)pollset->p->ke_set[i].udata)->pfd; + } + if ((pollset->flags & APR_POLLSET_WAKEABLE) && fd->desc_type == APR_POLL_FILE && fd->desc.f == pollset->wakeup_pipe[0]) { @@ -305,13 +332,15 @@ static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, } } - pollset_lock_rings(); + if (!(pollset->flags & APR_POLLSET_NOCOPY)) { + pollset_lock_rings(); - /* Shift all PFDs in the Dead Ring to the Free Ring */ - APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), - pfd_elem_t, link); + /* Shift all PFDs in the Dead Ring to the Free Ring */ + APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), + pfd_elem_t, link); - pollset_unlock_rings(); + pollset_unlock_rings(); + } return rv; } -- cgit v1.2.1 From df4d82325af245e6d56cfd82b6b3587dd9ca8ae9 Mon Sep 17 00:00:00 2001 From: Yann Ylavic Date: Wed, 26 Jan 2022 22:53:33 +0000 Subject: poll: Provide APR_POLLEXCL for exclusive wake up on systems that support it. epoll has EPOLLEXCLUSIVE, start with that. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1897521 13f79535-47bb-0310-9956-ffa450edef68 --- poll/unix/epoll.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'poll') diff --git a/poll/unix/epoll.c b/poll/unix/epoll.c index 89c41a404..340f43db8 100644 --- a/poll/unix/epoll.c +++ b/poll/unix/epoll.c @@ -35,6 +35,10 @@ static apr_int16_t get_epoll_event(apr_int16_t event) rv |= EPOLLPRI; if (event & APR_POLLOUT) rv |= EPOLLOUT; +#ifdef EPOLLEXCLUSIVE + if (event & APR_POLLEXCL) + rv |= EPOLLEXCLUSIVE; +#endif /* APR_POLLNVAL is not handled by epoll. EPOLLERR and EPOLLHUP are return-only */ return rv; -- cgit v1.2.1 From ed69b77ae5419f23034d0b95fb37ab992da601fd Mon Sep 17 00:00:00 2001 From: Yann Ylavic Date: Thu, 27 Jan 2022 13:50:16 +0000 Subject: poll: Follow up to r1897521: struct epoll_event's events field is unsigned int. EPOLLEXCLUSIVE is 1u << 28 so it doesn't fit in an int16_t, use unsigned for the native epoll events type. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1897548 13f79535-47bb-0310-9956-ffa450edef68 --- poll/unix/epoll.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'poll') diff --git a/poll/unix/epoll.c b/poll/unix/epoll.c index 340f43db8..7e811241d 100644 --- a/poll/unix/epoll.c +++ b/poll/unix/epoll.c @@ -25,9 +25,9 @@ #if defined(HAVE_EPOLL) -static apr_int16_t get_epoll_event(apr_int16_t event) +static unsigned get_epoll_event(apr_int16_t event) { - apr_int16_t rv = 0; + unsigned rv = 0; if (event & APR_POLLIN) rv |= EPOLLIN; @@ -44,7 +44,7 @@ static apr_int16_t get_epoll_event(apr_int16_t event) return rv; } -static apr_int16_t get_epoll_revent(apr_int16_t event) +static apr_int16_t get_epoll_revent(unsigned event) { apr_int16_t rv = 0; -- cgit v1.2.1