You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by tr...@apache.org on 2009/03/01 16:45:56 UTC

svn commit: r749049 - /apr/apr/trunk/poll/unix/port.c

Author: trawick
Date: Sun Mar  1 15:45:56 2009
New Revision: 749049

URL: http://svn.apache.org/viewvc?rev=749049&view=rev
Log:
commentary, consistency, simplification, and minor fixes

impl_pollset_create():
. return the actual port_create() failure instead of APR_ENOMEM

impl_pollset_add():
. return the actual port_associate() failure instead of APR_ENOMEM

impl_pollset_poll():
. catch port_associate() failures
. don't report returned events to caller unless something popped
  besides the wakeup pipe

impl_pollcb_poll():
. fix incorrect mapping of EINTR onto APR_TIMEUP
. don't hide interesting error codes behind APR_EGENERAL

generally:
. axe redundant APR_RING_EMPTY() invocations
. don't check for EINTR explicitly, as it is handled appropriately
  by the apr_get_netos_error() invocation (IOW, EINTR == APR_EINTR here)


Modified:
    apr/apr/trunk/poll/unix/port.c

Modified: apr/apr/trunk/poll/unix/port.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/poll/unix/port.c?rev=749049&r1=749048&r2=749049&view=diff
==============================================================================
--- apr/apr/trunk/poll/unix/port.c (original)
+++ apr/apr/trunk/poll/unix/port.c Sun Mar  1 15:45:56 2009
@@ -36,6 +36,9 @@
         rv |= POLLPRI;
     if (event & APR_POLLOUT)
         rv |= POLLOUT;
+    /* TODO: Confirm that the set of return-only events is the same as with 
+     * poll(), and axe these checks:
+     */
     if (event & APR_POLLERR)
         rv |= POLLERR;
     if (event & APR_POLLHUP)
@@ -78,6 +81,9 @@
 #endif
     /* A ring containing all of the pollfd_t that are active */
     APR_RING_HEAD(pfd_query_ring_t, pfd_elem_t) query_ring;
+    /* A ring containing the pollfd_t that will be added on the
+     * next call to apr_pollset_poll().
+     */
     APR_RING_HEAD(pfd_add_ring_t, pfd_elem_t) add_ring;
     /* A ring of pollfd_t that have been used, and then _remove'd */
     APR_RING_HEAD(pfd_free_ring_t, pfd_elem_t) free_ring;
@@ -123,7 +129,7 @@
 
     if (pollset->p->port_fd < 0) {
         pollset->p = NULL;
-        return APR_ENOMEM;
+        return apr_get_netos_error();
     }
 
     {
@@ -174,12 +180,15 @@
         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)) {
         res = port_associate(pollset->p->port_fd, PORT_SOURCE_FD, fd, 
                              get_event(descriptor->reqevents), (void *)elem);
 
         if (res < 0) {
-            rv = APR_ENOMEM;
+            rv = apr_get_netos_error();
             APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link);
         }
         else {
@@ -222,39 +231,35 @@
         rv = APR_NOTFOUND;
     }
 
-    if (!APR_RING_EMPTY(&(pollset->p->query_ring), pfd_elem_t, 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);
-                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);
+            APR_RING_INSERT_TAIL(&(pollset->p->dead_ring),
+                                 ep, pfd_elem_t, link);
+            if (ENOENT == err) {
+                rv = APR_SUCCESS;
             }
+            break;
         }
     }
 
-    if (!APR_RING_EMPTY(&(pollset->p->add_ring), pfd_elem_t, link)) {
-        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) {
-                APR_RING_REMOVE(ep, link);
-                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->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) {
+            APR_RING_REMOVE(ep, link);
+            APR_RING_INSERT_TAIL(&(pollset->p->dead_ring),
+                                 ep, pfd_elem_t, link);
+            if (ENOENT == err) {
+                rv = APR_SUCCESS;
             }
+            break;
         }
     }
 
@@ -302,15 +307,23 @@
             fd = ep->pfd.desc.f->filedes;
         }
 
-        port_associate(pollset->p->port_fd, PORT_SOURCE_FD, 
-                           fd, get_event(ep->pfd.reqevents), ep);
+        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();
+            break;
+        }
 
         APR_RING_INSERT_TAIL(&(pollset->p->query_ring), ep, pfd_elem_t, link);
-
     }
 
     pollset_unlock_rings();
 
+    if (rv != APR_SUCCESS) {
+        apr_atomic_dec32(&pollset->p->waiting);
+        return rv;
+    }
+
     ret = port_getn(pollset->p->port_fd, pollset->p->port_set, pollset->nalloc,
                     &nget, tvptr);
 
@@ -321,10 +334,7 @@
 
     if (ret == -1) {
         (*num) = 0;
-        if (errno == EINTR) {
-            rv = APR_EINTR;
-        }
-        else if (errno == ETIME) {
+        if (errno == ETIME) {
             rv = APR_TIMEUP;
         }
         else {
@@ -360,17 +370,17 @@
             }
         }
         pollset_unlock_rings();
-        if ((*num = j))
+        if ((*num = j)) { /* any event besides wakeup pipe? */
             rv = APR_SUCCESS;
-        if (descriptors) {
-            *descriptors = pollset->p->result_set;
+            if (descriptors) {
+                *descriptors = pollset->p->result_set;
+            }
         }
     }
 
-
     pollset_lock_rings();
 
-    /* Shift all PFDs in the Dead Ring to be Free Ring */
+    /* 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();
@@ -491,11 +501,11 @@
                     &nget, tvptr);
 
     if (ret == -1) {
-        if (errno == ETIME || errno == EINTR) {
+        if (errno == ETIME) {
             rv = APR_TIMEUP;
         }
         else {
-            rv = APR_EGENERAL;
+            rv = apr_get_netos_error();
         }
     }
     else if (nget == 0) {