]> git.ipfire.org Git - thirdparty/apache/httpd.git/commitdiff
Merge r1821624, r1821625, r1821626, r1821627, r1821629, r1821632, r1821635, r1821639...
authorYann Ylavic <ylavic@apache.org>
Fri, 16 Feb 2018 00:01:59 +0000 (00:01 +0000)
committerYann Ylavic <ylavic@apache.org>
Fri, 16 Feb 2018 00:01:59 +0000 (00:01 +0000)
Shared fdqueue code between MPMs event and worker into mpm_fdqueue.

This first step moves the files server/mpm/event/fdqueue.[ch] to
server/mpm_fdqueue.[ch] (untouched for now, simple svn move).

Will follow up with the necessary changes to mpm_unix.* for common code.

mpm_fdqueue: follow up to r1821624.

And now it's mpm_fdqueue, with minimal #includes.

mpm_fdqueue: follow up to r1821624.

Opacify fdqueue types.

mpm_fdqueue: follow up to r1821624.

Style, no functional change.

mpm_fdqueue: follow up to r1821624.

Rename ap_queue_info_get_idlers() to ap_queue_info_num_idlers().

mpm_fdqueue: follow up to r1821624.

Clear recycled_pools_count in ap_free_idle_pools().

mpm_fdqueue: follow up to r1821624.

Prepare mpm_worker to use common fdqueue.

mpm_worker: follow up to r1821624.

Use common [mpm_]fdqueue.

mpm_fdqueue: follow up to r1821624.

The implemention wants APR_HAS_THREADS too.

mpm_fdqueue: follow up to r1821624.

The code is not part of the API.

mpm_fdqueue: follow up to r1821624.

Fix typo in comment.

mpm_fdqueue: follow up to r1821624.

Cosmetics, 80 cols, no functional change.

mpm_worker: follow up to r1821624.

Free idle pools while stopping.

mpm_fdqueue: follow up to r1821624.

Be explicit in the naming about what's push/pop-ed.

mpm_fdqueue: follow up to r1821624.

Either error matters, simplify code.

mpm_fdqueue: follow up to r1821624.

Make the allocation and zero-ing in ap_queue_init() => ap_queue_create().

mpm_fdqueue: follow up to r1821624.

Export ap_queue_*() fonctions, so that they are accessible from MPMs, but
don't provide "mpm_fdqueue.h" in the API (include/).

mpm_fdqueue: follow up to r1821624.

80 colums.

Follow up to r1821624: CHANGES entry.

Submitted by: ylavic

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x-mpm_fdqueue@1824384 13f79535-47bb-0310-9956-ffa450edef68

CHANGES
server/Makefile.in
server/mpm/event/config3.m4
server/mpm/event/event.c
server/mpm/event/fdqueue.h [deleted file]
server/mpm/worker/config3.m4
server/mpm/worker/fdqueue.c [deleted file]
server/mpm/worker/fdqueue.h [deleted file]
server/mpm/worker/worker.c
server/mpm_fdqueue.c [moved from server/mpm/event/fdqueue.c with 75% similarity]
server/mpm_fdqueue.h [new file with mode: 0644]

diff --git a/CHANGES b/CHANGES
index 03b2456423d7434b89a35e81a3a2381c9118f4d4..938c1d5d5c9a2e5cde43d9ce29345cb7332f2701 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,9 @@
                                                          -*- coding: utf-8 -*-
 Changes with Apache 2.4.30
 
+  *) mpm_queue: Put fdqueue code in common for MPMs event and worker.
+     [Yann Ylavic]
+
   *) mod_proxy: Allow setting options to globally defined balancer from
      ProxyPass used in VirtualHost. Balancers are now merged using the new
      merge_balancers method which merges the balancers options.  [Jan Kaluza]
index 9663eec7dcad89728a61dde99618398d484ab179..1fa334467d0877e014f6b0e29ca581d1ebe79724 100644 (file)
@@ -9,7 +9,8 @@ LTLIBRARY_NAME    = libmain.la
 LTLIBRARY_SOURCES = \
        config.c log.c main.c vhost.c util.c util_fcgi.c \
        util_script.c util_md5.c util_cfgtree.c util_ebcdic.c util_time.c \
-       connection.c listen.c util_mutex.c mpm_common.c mpm_unix.c \
+       connection.c listen.c util_mutex.c \
+       mpm_common.c mpm_unix.c mpm_fdqueue.c \
        util_charset.c util_cookies.c util_debug.c util_xml.c \
        util_filter.c util_pcre.c util_regex.c exports.c \
        scoreboard.c error_bucket.c protocol.c core.c request.c provider.c \
@@ -58,6 +59,7 @@ export_files:
        ( for dir in $(EXPORT_DIRS); do \
              ls $$dir/*.h ; \
          done; \
+         echo "$(top_srcdir)/server/mpm_fdqueue.h"; \
          for dir in $(EXPORT_DIRS_APR); do \
              ls $$dir/ap[ru].h $$dir/ap[ru]_*.h 2>/dev/null; \
          done; \
index 8aa1631fabd9e27534aecaa1c5730984dc6260ad..09d362618af2c19132d7f70904204414c9f828b2 100644 (file)
@@ -2,6 +2,6 @@ dnl ## XXX - Need a more thorough check of the proper flags to use
 
 APACHE_SUBST(MOD_MPM_EVENT_LDADD)
 
-APACHE_MPM_MODULE(event, $enable_mpm_event, event.lo fdqueue.lo,[
+APACHE_MPM_MODULE(event, $enable_mpm_event, event.lo,[
     AC_CHECK_FUNCS(pthread_kill)
 ], , [\$(MOD_MPM_EVENT_LDADD)])
index 5e3186c44c3a89776b24027666aba58845c29e62..74d2596afccd3228676334bf376385529e4220b1 100644 (file)
@@ -90,7 +90,7 @@
 #include "mpm_common.h"
 #include "ap_listen.h"
 #include "scoreboard.h"
-#include "fdqueue.h"
+#include "mpm_fdqueue.h"
 #include "mpm_default.h"
 #include "http_vhost.h"
 #include "unixd.h"
@@ -212,6 +212,8 @@ static apr_pollfd_t *listener_pollfd;
  */
 static apr_pollset_t *event_pollset;
 
+typedef struct event_conn_state_t event_conn_state_t;
+
 /*
  * The chain of connections to be shutdown by a worker thread (deferred),
  * linked list updated atomically.
@@ -486,7 +488,7 @@ static void enable_listensocks(void)
                  apr_atomic_read32(&lingering_count),
                  apr_atomic_read32(&clogged_count),
                  apr_atomic_read32(&suspended_count),
-                 ap_queue_info_get_idlers(worker_queue_info));
+                 ap_queue_info_num_idlers(worker_queue_info));
     for (i = 0; i < num_listensocks; i++)
         apr_pollset_add(event_pollset, &listener_pollfd[i]);
     /*
@@ -503,7 +505,7 @@ static APR_INLINE apr_uint32_t listeners_disabled(void)
 
 static APR_INLINE int connections_above_limit(void)
 {
-    apr_uint32_t i_count = ap_queue_info_get_idlers(worker_queue_info);
+    apr_uint32_t i_count = ap_queue_info_num_idlers(worker_queue_info);
     if (i_count > 0) {
         apr_uint32_t c_count = apr_atomic_read32(&connection_count);
         apr_uint32_t l_count = apr_atomic_read32(&lingering_count);
@@ -804,7 +806,7 @@ static int start_lingering_close_blocking(event_conn_state_t *cs)
     if (ap_start_lingering_close(cs->c)) {
         notify_suspend(cs);
         apr_socket_close(csd);
-        ap_push_pool(worker_queue_info, cs->p);
+        ap_queue_info_push_pool(worker_queue_info, cs->p);
         return 0;
     }
 
@@ -847,7 +849,7 @@ static int start_lingering_close_blocking(event_conn_state_t *cs)
         ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf, APLOGNO(03092)
                      "start_lingering_close: apr_pollset_add failure");
         apr_socket_close(cs->pfd.desc.s);
-        ap_push_pool(worker_queue_info, cs->p);
+        ap_queue_info_push_pool(worker_queue_info, cs->p);
         return 0;
     }
     apr_thread_mutex_unlock(timeout_mutex);
@@ -888,7 +890,7 @@ static int stop_lingering_close(event_conn_state_t *cs)
     ap_log_error(APLOG_MARK, APLOG_TRACE4, 0, ap_server_conf,
                  "socket reached timeout in lingering-close state");
     abort_socket_nonblocking(csd);
-    ap_push_pool(worker_queue_info, cs->p);
+    ap_queue_info_push_pool(worker_queue_info, cs->p);
     if (dying)
         ap_queue_interrupt_one(worker_queue);
     return 0;
@@ -979,7 +981,7 @@ static void process_socket(apr_thread_t *thd, apr_pool_t * p, apr_socket_t * soc
         c = ap_run_create_connection(p, ap_server_conf, sock,
                                      conn_id, cs->sbh, cs->bucket_alloc);
         if (!c) {
-            ap_push_pool(worker_queue_info, p);
+            ap_queue_info_push_pool(worker_queue_info, p);
             return;
         }
         apr_atomic_inc32(&connection_count);
@@ -1143,7 +1145,7 @@ read_request:
                              "process_socket: apr_pollset_add failure for "
                              "write completion");
                 apr_socket_close(cs->pfd.desc.s);
-                ap_push_pool(worker_queue_info, cs->p);
+                ap_queue_info_push_pool(worker_queue_info, cs->p);
             }
             else {
                 apr_thread_mutex_unlock(timeout_mutex);
@@ -1192,7 +1194,7 @@ read_request:
                          "process_socket: apr_pollset_add failure for "
                          "keep alive");
             apr_socket_close(cs->pfd.desc.s);
-            ap_push_pool(worker_queue_info, cs->p);
+            ap_queue_info_push_pool(worker_queue_info, cs->p);
             return;
         }
         apr_thread_mutex_unlock(timeout_mutex);
@@ -1234,7 +1236,7 @@ static void close_listeners(int *closed)
         /* wake up the main thread */
         kill(ap_my_pid, SIGTERM);
 
-        ap_free_idle_pools(worker_queue_info);
+        ap_queue_info_free_idle_pools(worker_queue_info);
         ap_queue_interrupt_all(worker_queue);
     }
 }
@@ -1308,10 +1310,10 @@ static apr_status_t push2worker(event_conn_state_t *cs, apr_socket_t *csd,
         csd = cs->pfd.desc.s;
         ptrans = cs->p;
     }
-    rc = ap_queue_push(worker_queue, csd, cs, ptrans);
+    rc = ap_queue_push_socket(worker_queue, csd, cs, ptrans);
     if (rc != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_CRIT, rc, ap_server_conf, APLOGNO(00471)
-                     "push2worker: ap_queue_push failed");
+                     "push2worker: ap_queue_push_socket failed");
         /* trash the connection; we couldn't queue the connected
          * socket to a worker
          */
@@ -1319,7 +1321,7 @@ static apr_status_t push2worker(event_conn_state_t *cs, apr_socket_t *csd,
             abort_socket_nonblocking(csd);
         }
         if (ptrans) {
-            ap_push_pool(worker_queue_info, ptrans);
+            ap_queue_info_push_pool(worker_queue_info, ptrans);
         }
         signal_threads(ST_GRACEFUL);
     }
@@ -1482,7 +1484,7 @@ static void process_lingering_close(event_conn_state_t *cs, const apr_pollfd_t *
     rv = apr_socket_close(csd);
     AP_DEBUG_ASSERT(rv == APR_SUCCESS);
 
-    ap_push_pool(worker_queue_info, cs->p);
+    ap_queue_info_push_pool(worker_queue_info, cs->p);
     if (dying)
         ap_queue_interrupt_one(worker_queue);
 }
@@ -1810,14 +1812,14 @@ static void * APR_THREAD_FUNC listener_thread(apr_thread_t * thd, void *dummy)
                                  apr_atomic_read32(&connection_count));
                     ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, ap_server_conf,
                                  "Idle workers: %u",
-                                 ap_queue_info_get_idlers(worker_queue_info));
+                                 ap_queue_info_num_idlers(worker_queue_info));
                     workers_were_busy = 1;
                 }
                 else if (!listener_may_exit) {
                     void *csd = NULL;
                     ap_listen_rec *lr = (ap_listen_rec *) pt->baton;
                     apr_pool_t *ptrans;         /* Pool for per-transaction stuff */
-                    ap_pop_pool(&ptrans, worker_queue_info);
+                    ap_queue_info_pop_pool(worker_queue_info, &ptrans);
 
                     if (ptrans == NULL) {
                         /* create a new transaction pool for each accepted socket */
@@ -1868,7 +1870,7 @@ static void * APR_THREAD_FUNC listener_thread(apr_thread_t * thd, void *dummy)
                         }
                     }
                     else {
-                        ap_push_pool(worker_queue_info, ptrans);
+                        ap_queue_info_push_pool(worker_queue_info, ptrans);
                     }
                 }
             }               /* if:else on pt->type */
@@ -2038,7 +2040,8 @@ static void *APR_THREAD_FUNC worker_thread(apr_thread_t * thd, void *dummy)
             break;
         }
 
-        rv = ap_queue_pop_something(worker_queue, &csd, &cs, &ptrans, &te);
+        rv = ap_queue_pop_something(worker_queue, &csd, (void **)&cs,
+                                    &ptrans, &te);
 
         if (rv != APR_SUCCESS) {
             /* We get APR_EOF during a graceful shutdown once all the
@@ -2047,9 +2050,9 @@ static void *APR_THREAD_FUNC worker_thread(apr_thread_t * thd, void *dummy)
             if (APR_STATUS_IS_EOF(rv)) {
                 break;
             }
-            /* We get APR_EINTR whenever ap_queue_pop() has been interrupted
+            /* We get APR_EINTR whenever ap_queue_pop_*() has been interrupted
              * from an explicit call to ap_queue_interrupt_all(). This allows
-             * us to unblock threads stuck in ap_queue_pop() when a shutdown
+             * us to unblock threads stuck in ap_queue_pop_*() when a shutdown
              * is pending.
              *
              * If workers_may_exit is set and this is ungraceful termination/
@@ -2064,7 +2067,7 @@ static void *APR_THREAD_FUNC worker_thread(apr_thread_t * thd, void *dummy)
             /* We got some other error. */
             else if (!workers_may_exit) {
                 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
-                             APLOGNO(03099) "ap_queue_pop failed");
+                             APLOGNO(03099) "ap_queue_pop_socket failed");
             }
             continue;
         }
@@ -2183,11 +2186,10 @@ static void *APR_THREAD_FUNC start_threads(apr_thread_t * thd, void *dummy)
 
     /* We must create the fd queues before we start up the listener
      * and worker threads. */
-    worker_queue = apr_pcalloc(pchild, sizeof(*worker_queue));
-    rv = ap_queue_init(worker_queue, threads_per_child, pchild);
+    rv = ap_queue_create(&worker_queue, threads_per_child, pchild);
     if (rv != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf, APLOGNO(03100)
-                     "ap_queue_init() failed");
+                     "ap_queue_create() failed");
         clean_child_exit(APEXIT_CHILDFATAL);
     }
 
diff --git a/server/mpm/event/fdqueue.h b/server/mpm/event/fdqueue.h
deleted file mode 100644 (file)
index 37be684..0000000
+++ /dev/null
@@ -1,106 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * @file  event/fdqueue.h
- * @brief fd queue declarations
- *
- * @addtogroup APACHE_MPM_EVENT
- * @{
- */
-
-#ifndef FDQUEUE_H
-#define FDQUEUE_H
-#include "httpd.h"
-#include <stdlib.h>
-#if APR_HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#include <apr_thread_mutex.h>
-#include <apr_thread_cond.h>
-#include <sys/types.h>
-#if APR_HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-#include <apr_errno.h>
-
-#include "ap_mpm.h"
-
-typedef struct fd_queue_info_t fd_queue_info_t;
-typedef struct event_conn_state_t event_conn_state_t;
-
-apr_status_t ap_queue_info_create(fd_queue_info_t ** queue_info,
-                                  apr_pool_t * pool, int max_idlers,
-                                  int max_recycled_pools);
-apr_status_t ap_queue_info_set_idle(fd_queue_info_t * queue_info,
-                                    apr_pool_t * pool_to_recycle);
-apr_status_t ap_queue_info_try_get_idler(fd_queue_info_t * queue_info);
-apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t * queue_info,
-                                          int *had_to_block);
-apr_status_t ap_queue_info_term(fd_queue_info_t * queue_info);
-apr_uint32_t ap_queue_info_get_idlers(fd_queue_info_t * queue_info);
-void ap_free_idle_pools(fd_queue_info_t *queue_info);
-
-struct fd_queue_elem_t
-{
-    apr_socket_t *sd;
-    apr_pool_t *p;
-    event_conn_state_t *ecs;
-};
-typedef struct fd_queue_elem_t fd_queue_elem_t;
-
-typedef struct timer_event_t timer_event_t;
-
-struct timer_event_t {
-    APR_RING_ENTRY(timer_event_t) link;
-    apr_time_t when;
-    ap_mpm_callback_fn_t *cbfunc;
-    void *baton;
-};
-
-
-struct fd_queue_t
-{
-    APR_RING_HEAD(timers_t, timer_event_t) timers;
-    fd_queue_elem_t *data;
-    unsigned int nelts;
-    unsigned int bounds;
-    unsigned int in;
-    unsigned int out;
-    apr_thread_mutex_t *one_big_mutex;
-    apr_thread_cond_t *not_empty;
-    int terminated;
-};
-typedef struct fd_queue_t fd_queue_t;
-
-void ap_pop_pool(apr_pool_t ** recycled_pool, fd_queue_info_t * queue_info);
-void ap_push_pool(fd_queue_info_t * queue_info,
-                                    apr_pool_t * pool_to_recycle);
-
-apr_status_t ap_queue_init(fd_queue_t * queue, int queue_capacity,
-                           apr_pool_t * a);
-apr_status_t ap_queue_push(fd_queue_t * queue, apr_socket_t * sd,
-                           event_conn_state_t * ecs, apr_pool_t * p);
-apr_status_t ap_queue_push_timer(fd_queue_t *queue, timer_event_t *te);
-apr_status_t ap_queue_pop_something(fd_queue_t * queue, apr_socket_t ** sd,
-                                    event_conn_state_t ** ecs, apr_pool_t ** p,
-                                    timer_event_t ** te);
-apr_status_t ap_queue_interrupt_all(fd_queue_t * queue);
-apr_status_t ap_queue_interrupt_one(fd_queue_t * queue);
-apr_status_t ap_queue_term(fd_queue_t * queue);
-
-#endif /* FDQUEUE_H */
-/** @} */
index c28b73bf54c5d9e26b1d4050c95e97540beea351..6c1eb171293cc6ac40e52dfa3c6313ecd9417c86 100644 (file)
@@ -1,5 +1,5 @@
 dnl ## XXX - Need a more thorough check of the proper flags to use
 
-APACHE_MPM_MODULE(worker, $enable_mpm_worker, worker.lo fdqueue.lo,[
+APACHE_MPM_MODULE(worker, $enable_mpm_worker, worker.lo,[
     AC_CHECK_FUNCS(pthread_kill)
 ])
diff --git a/server/mpm/worker/fdqueue.c b/server/mpm/worker/fdqueue.c
deleted file mode 100644 (file)
index 803267a..0000000
+++ /dev/null
@@ -1,411 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "fdqueue.h"
-#include "apr_atomic.h"
-
-typedef struct recycled_pool {
-    apr_pool_t *pool;
-    struct recycled_pool *next;
-} recycled_pool;
-
-struct fd_queue_info_t {
-    apr_uint32_t idlers;
-    apr_thread_mutex_t *idlers_mutex;
-    apr_thread_cond_t *wait_for_idler;
-    int terminated;
-    int max_idlers;
-    recycled_pool  *recycled_pools;
-};
-
-static apr_status_t queue_info_cleanup(void *data_)
-{
-    fd_queue_info_t *qi = data_;
-    apr_thread_cond_destroy(qi->wait_for_idler);
-    apr_thread_mutex_destroy(qi->idlers_mutex);
-
-    /* Clean up any pools in the recycled list */
-    for (;;) {
-        struct recycled_pool *first_pool = qi->recycled_pools;
-        if (first_pool == NULL) {
-            break;
-        }
-        if (apr_atomic_casptr((void*)&(qi->recycled_pools), first_pool->next,
-                              first_pool) == first_pool) {
-            apr_pool_destroy(first_pool->pool);
-        }
-    }
-
-    return APR_SUCCESS;
-}
-
-apr_status_t ap_queue_info_create(fd_queue_info_t **queue_info,
-                                  apr_pool_t *pool, int max_idlers)
-{
-    apr_status_t rv;
-    fd_queue_info_t *qi;
-
-    qi = apr_pcalloc(pool, sizeof(*qi));
-
-    rv = apr_thread_mutex_create(&qi->idlers_mutex, APR_THREAD_MUTEX_DEFAULT,
-                                 pool);
-    if (rv != APR_SUCCESS) {
-        return rv;
-    }
-    rv = apr_thread_cond_create(&qi->wait_for_idler, pool);
-    if (rv != APR_SUCCESS) {
-        return rv;
-    }
-    qi->recycled_pools = NULL;
-    qi->max_idlers = max_idlers;
-    apr_pool_cleanup_register(pool, qi, queue_info_cleanup,
-                              apr_pool_cleanup_null);
-
-    *queue_info = qi;
-
-    return APR_SUCCESS;
-}
-
-apr_status_t ap_queue_info_set_idle(fd_queue_info_t *queue_info,
-                                    apr_pool_t *pool_to_recycle)
-{
-    apr_status_t rv;
-    int prev_idlers;
-
-    /* If we have been given a pool to recycle, atomically link
-     * it into the queue_info's list of recycled pools
-     */
-    if (pool_to_recycle) {
-        struct recycled_pool *new_recycle;
-        new_recycle = (struct recycled_pool *)apr_palloc(pool_to_recycle,
-                                                         sizeof(*new_recycle));
-        new_recycle->pool = pool_to_recycle;
-        for (;;) {
-            /* Save queue_info->recycled_pool in local variable next because
-             * new_recycle->next can be changed after apr_atomic_casptr
-             * function call. For gory details see PR 44402.
-             */
-            struct recycled_pool *next = queue_info->recycled_pools;
-            new_recycle->next = next;
-            if (apr_atomic_casptr((void*)&(queue_info->recycled_pools),
-                                  new_recycle, next) == next) {
-                break;
-            }
-        }
-    }
-
-    /* Atomically increment the count of idle workers */
-    for (;;) {
-        prev_idlers = queue_info->idlers;
-        if (apr_atomic_cas32(&(queue_info->idlers), prev_idlers + 1,
-                             prev_idlers) == prev_idlers) {
-            break;
-        }
-    }
-
-    /* If this thread just made the idle worker count nonzero,
-     * wake up the listener. */
-    if (prev_idlers == 0) {
-        rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
-        if (rv != APR_SUCCESS) {
-            return rv;
-        }
-        rv = apr_thread_cond_signal(queue_info->wait_for_idler);
-        if (rv != APR_SUCCESS) {
-            apr_thread_mutex_unlock(queue_info->idlers_mutex);
-            return rv;
-        }
-        rv = apr_thread_mutex_unlock(queue_info->idlers_mutex);
-        if (rv != APR_SUCCESS) {
-            return rv;
-        }
-    }
-
-    return APR_SUCCESS;
-}
-
-apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t *queue_info,
-                                          apr_pool_t **recycled_pool)
-{
-    apr_status_t rv;
-
-    *recycled_pool = NULL;
-
-    /* Block if the count of idle workers is zero */
-    if (queue_info->idlers == 0) {
-        rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
-        if (rv != APR_SUCCESS) {
-            return rv;
-        }
-        /* Re-check the idle worker count to guard against a
-         * race condition.  Now that we're in the mutex-protected
-         * region, one of two things may have happened:
-         *   - If the idle worker count is still zero, the
-         *     workers are all still busy, so it's safe to
-         *     block on a condition variable, BUT
-         *     we need to check for idle worker count again
-         *     when we are signaled since it can happen that
-         *     we are signaled by a worker thread that went idle
-         *     but received a context switch before it could
-         *     tell us. If it does signal us later once it is on
-         *     CPU again there might be no idle worker left.
-         *     See
-         *     https://issues.apache.org/bugzilla/show_bug.cgi?id=45605#c4
-         *   - If the idle worker count is nonzero, then a
-         *     worker has become idle since the first check
-         *     of queue_info->idlers above.  It's possible
-         *     that the worker has also signaled the condition
-         *     variable--and if so, the listener missed it
-         *     because it wasn't yet blocked on the condition
-         *     variable.  But if the idle worker count is
-         *     now nonzero, it's safe for this function to
-         *     return immediately.
-         */
-        while (queue_info->idlers == 0) {
-            rv = apr_thread_cond_wait(queue_info->wait_for_idler,
-                                  queue_info->idlers_mutex);
-            if (rv != APR_SUCCESS) {
-                apr_status_t rv2;
-                rv2 = apr_thread_mutex_unlock(queue_info->idlers_mutex);
-                if (rv2 != APR_SUCCESS) {
-                    return rv2;
-                }
-                return rv;
-            }
-        }
-        rv = apr_thread_mutex_unlock(queue_info->idlers_mutex);
-        if (rv != APR_SUCCESS) {
-            return rv;
-        }
-    }
-
-    /* Atomically decrement the idle worker count */
-    apr_atomic_dec32(&(queue_info->idlers));
-
-    /* Atomically pop a pool from the recycled list */
-
-    /* This function is safe only as long as it is single threaded because
-     * it reaches into the queue and accesses "next" which can change.
-     * We are OK today because it is only called from the listener thread.
-     * cas-based pushes do not have the same limitation - any number can
-     * happen concurrently with a single cas-based pop.
-     */
-
-    for (;;) {
-        struct recycled_pool *first_pool = queue_info->recycled_pools;
-        if (first_pool == NULL) {
-            break;
-        }
-        if (apr_atomic_casptr((void*)&(queue_info->recycled_pools), first_pool->next,
-                              first_pool) == first_pool) {
-            *recycled_pool = first_pool->pool;
-            break;
-        }
-    }
-
-    if (queue_info->terminated) {
-        return APR_EOF;
-    }
-    else {
-        return APR_SUCCESS;
-    }
-}
-
-apr_status_t ap_queue_info_term(fd_queue_info_t *queue_info)
-{
-    apr_status_t rv;
-    rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
-    if (rv != APR_SUCCESS) {
-        return rv;
-    }
-    queue_info->terminated = 1;
-    apr_thread_cond_broadcast(queue_info->wait_for_idler);
-    return apr_thread_mutex_unlock(queue_info->idlers_mutex);
-}
-
-/**
- * Detects when the fd_queue_t is full. This utility function is expected
- * to be called from within critical sections, and is not threadsafe.
- */
-#define ap_queue_full(queue) ((queue)->nelts == (queue)->bounds)
-
-/**
- * Detects when the fd_queue_t is empty. This utility function is expected
- * to be called from within critical sections, and is not threadsafe.
- */
-#define ap_queue_empty(queue) ((queue)->nelts == 0)
-
-/**
- * Callback routine that is called to destroy this
- * fd_queue_t when its pool is destroyed.
- */
-static apr_status_t ap_queue_destroy(void *data)
-{
-    fd_queue_t *queue = data;
-
-    /* Ignore errors here, we can't do anything about them anyway.
-     * XXX: We should at least try to signal an error here, it is
-     * indicative of a programmer error. -aaron */
-    apr_thread_cond_destroy(queue->not_empty);
-    apr_thread_mutex_destroy(queue->one_big_mutex);
-
-    return APR_SUCCESS;
-}
-
-/**
- * Initialize the fd_queue_t.
- */
-apr_status_t ap_queue_init(fd_queue_t *queue, int queue_capacity, apr_pool_t *a)
-{
-    int i;
-    apr_status_t rv;
-
-    if ((rv = apr_thread_mutex_create(&queue->one_big_mutex,
-                                      APR_THREAD_MUTEX_DEFAULT, a)) != APR_SUCCESS) {
-        return rv;
-    }
-    if ((rv = apr_thread_cond_create(&queue->not_empty, a)) != APR_SUCCESS) {
-        return rv;
-    }
-
-    queue->data = apr_palloc(a, queue_capacity * sizeof(fd_queue_elem_t));
-    queue->bounds = queue_capacity;
-    queue->nelts = 0;
-    queue->in = 0;
-    queue->out = 0;
-
-    /* Set all the sockets in the queue to NULL */
-    for (i = 0; i < queue_capacity; ++i)
-        queue->data[i].sd = NULL;
-
-    apr_pool_cleanup_register(a, queue, ap_queue_destroy, apr_pool_cleanup_null);
-
-    return APR_SUCCESS;
-}
-
-/**
- * Push a new socket onto the queue.
- *
- * precondition: ap_queue_info_wait_for_idler has already been called
- *               to reserve an idle worker thread
- */
-apr_status_t ap_queue_push(fd_queue_t *queue, apr_socket_t *sd, apr_pool_t *p)
-{
-    fd_queue_elem_t *elem;
-    apr_status_t rv;
-
-    if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
-        return rv;
-    }
-
-    AP_DEBUG_ASSERT(!queue->terminated);
-    AP_DEBUG_ASSERT(!ap_queue_full(queue));
-
-    elem = &queue->data[queue->in];
-    queue->in++;
-    if (queue->in >= queue->bounds)
-        queue->in -= queue->bounds;
-    elem->sd = sd;
-    elem->p = p;
-    queue->nelts++;
-
-    apr_thread_cond_signal(queue->not_empty);
-
-    if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
-        return rv;
-    }
-
-    return APR_SUCCESS;
-}
-
-/**
- * Retrieves the next available socket from the queue. If there are no
- * sockets available, it will block until one becomes available.
- * Once retrieved, the socket is placed into the address specified by
- * 'sd'.
- */
-apr_status_t ap_queue_pop(fd_queue_t *queue, apr_socket_t **sd, apr_pool_t **p)
-{
-    fd_queue_elem_t *elem;
-    apr_status_t rv;
-
-    if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
-        return rv;
-    }
-
-    /* Keep waiting until we wake up and find that the queue is not empty. */
-    if (ap_queue_empty(queue)) {
-        if (!queue->terminated) {
-            apr_thread_cond_wait(queue->not_empty, queue->one_big_mutex);
-        }
-        /* If we wake up and it's still empty, then we were interrupted */
-        if (ap_queue_empty(queue)) {
-            rv = apr_thread_mutex_unlock(queue->one_big_mutex);
-            if (rv != APR_SUCCESS) {
-                return rv;
-            }
-            if (queue->terminated) {
-                return APR_EOF; /* no more elements ever again */
-            }
-            else {
-                return APR_EINTR;
-            }
-        }
-    }
-
-    elem = &queue->data[queue->out];
-    queue->out++;
-    if (queue->out >= queue->bounds)
-        queue->out -= queue->bounds;
-    queue->nelts--;
-    *sd = elem->sd;
-    *p = elem->p;
-#ifdef AP_DEBUG
-    elem->sd = NULL;
-    elem->p = NULL;
-#endif /* AP_DEBUG */
-
-    rv = apr_thread_mutex_unlock(queue->one_big_mutex);
-    return rv;
-}
-
-static apr_status_t queue_interrupt_all(fd_queue_t *queue, int term)
-{
-    apr_status_t rv;
-
-    if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
-        return rv;
-    }
-    /* we must hold one_big_mutex when setting this... otherwise,
-     * we could end up setting it and waking everybody up just after a
-     * would-be popper checks it but right before they block
-     */
-    if (term) {
-        queue->terminated = 1;
-    }
-    apr_thread_cond_broadcast(queue->not_empty);
-    return apr_thread_mutex_unlock(queue->one_big_mutex);
-}
-
-apr_status_t ap_queue_interrupt_all(fd_queue_t *queue)
-{
-    return queue_interrupt_all(queue, 0);
-}
-
-apr_status_t ap_queue_term(fd_queue_t *queue)
-{
-    return queue_interrupt_all(queue, 1);
-}
diff --git a/server/mpm/worker/fdqueue.h b/server/mpm/worker/fdqueue.h
deleted file mode 100644 (file)
index 1d48a1a..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * @file  worker/fdqueue.h
- * @brief fd queue declarations
- *
- * @addtogroup APACHE_MPM_WORKER
- * @{
- */
-
-#ifndef FDQUEUE_H
-#define FDQUEUE_H
-#include "httpd.h"
-#include <stdlib.h>
-#if APR_HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#include <apr_thread_mutex.h>
-#include <apr_thread_cond.h>
-#include <sys/types.h>
-#if APR_HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-#include <apr_errno.h>
-
-typedef struct fd_queue_info_t fd_queue_info_t;
-
-apr_status_t ap_queue_info_create(fd_queue_info_t **queue_info,
-                                  apr_pool_t *pool, int max_idlers);
-apr_status_t ap_queue_info_set_idle(fd_queue_info_t *queue_info,
-                                    apr_pool_t *pool_to_recycle);
-apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t *queue_info,
-                                          apr_pool_t **recycled_pool);
-apr_status_t ap_queue_info_term(fd_queue_info_t *queue_info);
-
-struct fd_queue_elem_t {
-    apr_socket_t      *sd;
-    apr_pool_t        *p;
-};
-typedef struct fd_queue_elem_t fd_queue_elem_t;
-
-struct fd_queue_t {
-    fd_queue_elem_t    *data;
-    unsigned int       nelts;
-    unsigned int       bounds;
-    unsigned int       in;
-    unsigned int       out;
-    apr_thread_mutex_t *one_big_mutex;
-    apr_thread_cond_t  *not_empty;
-    int                 terminated;
-};
-typedef struct fd_queue_t fd_queue_t;
-
-apr_status_t ap_queue_init(fd_queue_t *queue, int queue_capacity, apr_pool_t *a);
-apr_status_t ap_queue_push(fd_queue_t *queue, apr_socket_t *sd, apr_pool_t *p);
-apr_status_t ap_queue_pop(fd_queue_t *queue, apr_socket_t **sd, apr_pool_t **p);
-apr_status_t ap_queue_interrupt_all(fd_queue_t *queue);
-apr_status_t ap_queue_term(fd_queue_t *queue);
-
-#endif /* FDQUEUE_H */
-/** @} */
index 1d513871356f1c399ba0aa178fa340097f8941fc..7804efc45728659f67d6b24be417526595db5ff5 100644 (file)
@@ -64,7 +64,7 @@
 #include "mpm_common.h"
 #include "ap_listen.h"
 #include "scoreboard.h"
-#include "fdqueue.h"
+#include "mpm_fdqueue.h"
 #include "mpm_default.h"
 #include "util_mutex.h"
 #include "unixd.h"
@@ -595,11 +595,7 @@ static void * APR_THREAD_FUNC listener_thread(apr_thread_t *thd, void * dummy)
         if (listener_may_exit) break;
 
         if (!have_idle_worker) {
-            /* the following pops a recycled ptrans pool off a stack
-             * if there is one, in addition to reserving a worker thread
-             */
-            rv = ap_queue_info_wait_for_idler(worker_queue_info,
-                                              &ptrans);
+            rv = ap_queue_info_wait_for_idler(worker_queue_info, NULL);
             if (APR_STATUS_IS_EOF(rv)) {
                 break; /* we've been signaled to die now */
             }
@@ -677,6 +673,8 @@ static void * APR_THREAD_FUNC listener_thread(apr_thread_t *thd, void * dummy)
         } /* if/else */
 
         if (!listener_may_exit) {
+            /* the following pops a recycled ptrans pool off a stack */
+            ap_queue_info_pop_pool(worker_queue_info, &ptrans);
             if (ptrans == NULL) {
                 /* we can't use a recycled transaction pool this time.
                  * create a new transaction pool */
@@ -686,8 +684,8 @@ static void * APR_THREAD_FUNC listener_thread(apr_thread_t *thd, void * dummy)
                 apr_allocator_max_free_set(allocator, ap_max_mem_free);
                 apr_pool_create_ex(&ptrans, pconf, NULL, allocator);
                 apr_allocator_owner_set(allocator, ptrans);
+                apr_pool_tag(ptrans, "transaction");
             }
-            apr_pool_tag(ptrans, "transaction");
             rv = lr->accept_func(&csd, lr, ptrans);
             /* later we trash rv and rely on csd to indicate success/failure */
             AP_DEBUG_ASSERT(rv == APR_SUCCESS || !csd);
@@ -706,14 +704,14 @@ static void * APR_THREAD_FUNC listener_thread(apr_thread_t *thd, void * dummy)
                 accept_mutex_error("unlock", rv, process_slot);
             }
             if (csd != NULL) {
-                rv = ap_queue_push(worker_queue, csd, ptrans);
+                rv = ap_queue_push_socket(worker_queue, csd, NULL, ptrans);
                 if (rv) {
                     /* trash the connection; we couldn't queue the connected
                      * socket to a worker
                      */
                     apr_socket_close(csd);
                     ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(03138)
-                                 "ap_queue_push failed");
+                                 "ap_queue_push_socket failed");
                 }
                 else {
                     have_idle_worker = 0;
@@ -739,6 +737,7 @@ static void * APR_THREAD_FUNC listener_thread(apr_thread_t *thd, void * dummy)
     }
 
     ap_close_listeners_ex(my_bucket->listeners);
+    ap_queue_info_free_idle_pools(worker_queue_info);
     ap_queue_term(worker_queue);
     dying = 1;
     ap_scoreboard_image->parent[process_slot].quiescing = 1;
@@ -802,7 +801,7 @@ worker_pop:
         if (workers_may_exit) {
             break;
         }
-        rv = ap_queue_pop(worker_queue, &csd, &ptrans);
+        rv = ap_queue_pop_socket(worker_queue, &csd, &ptrans);
 
         if (rv != APR_SUCCESS) {
             /* We get APR_EOF during a graceful shutdown once all the connections
@@ -811,9 +810,9 @@ worker_pop:
             if (APR_STATUS_IS_EOF(rv)) {
                 break;
             }
-            /* We get APR_EINTR whenever ap_queue_pop() has been interrupted
+            /* We get APR_EINTR whenever ap_queue_pop_*() has been interrupted
              * from an explicit call to ap_queue_interrupt_all(). This allows
-             * us to unblock threads stuck in ap_queue_pop() when a shutdown
+             * us to unblock threads stuck in ap_queue_pop_*() when a shutdown
              * is pending.
              *
              * If workers_may_exit is set and this is ungraceful termination/
@@ -828,7 +827,7 @@ worker_pop:
             /* We got some other error. */
             else if (!workers_may_exit) {
                 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(03139)
-                             "ap_queue_pop failed");
+                             "ap_queue_pop_socket failed");
             }
             continue;
         }
@@ -903,16 +902,15 @@ static void * APR_THREAD_FUNC start_threads(apr_thread_t *thd, void *dummy)
 
     /* We must create the fd queues before we start up the listener
      * and worker threads. */
-    worker_queue = apr_pcalloc(pchild, sizeof(*worker_queue));
-    rv = ap_queue_init(worker_queue, threads_per_child, pchild);
+    rv = ap_queue_create(&worker_queue, threads_per_child, pchild);
     if (rv != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf, APLOGNO(03140)
-                     "ap_queue_init() failed");
+                     "ap_queue_create() failed");
         clean_child_exit(APEXIT_CHILDFATAL);
     }
 
     rv = ap_queue_info_create(&worker_queue_info, pchild,
-                              threads_per_child);
+                              threads_per_child, -1);
     if (rv != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf, APLOGNO(03141)
                      "ap_queue_info_create() failed");
similarity index 75%
rename from server/mpm/event/fdqueue.c
rename to server/mpm_fdqueue.c
index 175d86a7209117736be187ee83b0ae3113db1ec4..c81245078ab509b85eb771ca75a437774860cd79 100644 (file)
  * limitations under the License.
  */
 
-#include "fdqueue.h"
-#include "apr_atomic.h"
+#include "mpm_fdqueue.h"
+
+#if APR_HAS_THREADS
+
+#include <apr_atomic.h>
 
 static const apr_uint32_t zero_pt = APR_UINT32_MAX/2;
 
@@ -41,6 +44,13 @@ struct fd_queue_info_t
     struct recycled_pool *volatile recycled_pools;
 };
 
+struct fd_queue_elem_t
+{
+    apr_socket_t *sd;
+    void *sd_baton;
+    apr_pool_t *p;
+};
+
 static apr_status_t queue_info_cleanup(void *data_)
 {
     fd_queue_info_t *qi = data_;
@@ -53,9 +63,8 @@ static apr_status_t queue_info_cleanup(void *data_)
         if (first_pool == NULL) {
             break;
         }
-        if (apr_atomic_casptr
-            ((void*) &(qi->recycled_pools), first_pool->next,
-             first_pool) == first_pool) {
+        if (apr_atomic_casptr((void *)&qi->recycled_pools, first_pool->next,
+                              first_pool) == first_pool) {
             apr_pool_destroy(first_pool->pool);
         }
     }
@@ -63,8 +72,8 @@ static apr_status_t queue_info_cleanup(void *data_)
     return APR_SUCCESS;
 }
 
-apr_status_t ap_queue_info_create(fd_queue_info_t ** queue_info,
-                                  apr_pool_t * pool, int max_idlers,
+apr_status_t ap_queue_info_create(fd_queue_info_t **queue_info,
+                                  apr_pool_t *pool, int max_idlers,
                                   int max_recycled_pools)
 {
     apr_status_t rv;
@@ -93,12 +102,12 @@ apr_status_t ap_queue_info_create(fd_queue_info_t ** queue_info,
     return APR_SUCCESS;
 }
 
-apr_status_t ap_queue_info_set_idle(fd_queue_info_t * queue_info,
-                                    apr_pool_t * pool_to_recycle)
+apr_status_t ap_queue_info_set_idle(fd_queue_info_t *queue_info,
+                                    apr_pool_t *pool_to_recycle)
 {
     apr_status_t rv;
 
-    ap_push_pool(queue_info, pool_to_recycle);
+    ap_queue_info_push_pool(queue_info, pool_to_recycle);
 
     /* If other threads are waiting on a worker, wake one up */
     if (apr_atomic_inc32(&queue_info->idlers) < zero_pt) {
@@ -121,7 +130,7 @@ apr_status_t ap_queue_info_set_idle(fd_queue_info_t * queue_info,
     return APR_SUCCESS;
 }
 
-apr_status_t ap_queue_info_try_get_idler(fd_queue_info_t * queue_info)
+apr_status_t ap_queue_info_try_get_idler(fd_queue_info_t *queue_info)
 {
     /* Don't block if there isn't any idle worker. */
     for (;;) {
@@ -136,7 +145,7 @@ apr_status_t ap_queue_info_try_get_idler(fd_queue_info_t * queue_info)
     }
 }
 
-apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t * queue_info,
+apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t *queue_info,
                                           int *had_to_block)
 {
     apr_status_t rv;
@@ -173,16 +182,14 @@ apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t * queue_info,
          *     threads are waiting on an idle worker.
          */
         if (queue_info->idlers < zero_pt) {
-            *had_to_block = 1;
+            if (had_to_block) {
+                *had_to_block = 1;
+            }
             rv = apr_thread_cond_wait(queue_info->wait_for_idler,
                                       queue_info->idlers_mutex);
             if (rv != APR_SUCCESS) {
-                apr_status_t rv2;
                 AP_DEBUG_ASSERT(0);
-                rv2 = apr_thread_mutex_unlock(queue_info->idlers_mutex);
-                if (rv2 != APR_SUCCESS) {
-                    return rv2;
-                }
+                apr_thread_mutex_unlock(queue_info->idlers_mutex);
                 return rv;
             }
         }
@@ -200,17 +207,15 @@ apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t * queue_info,
     }
 }
 
-apr_uint32_t ap_queue_info_get_idlers(fd_queue_info_t * queue_info)
+apr_uint32_t ap_queue_info_num_idlers(fd_queue_info_t *queue_info)
 {
     apr_uint32_t val;
     val = apr_atomic_read32(&queue_info->idlers);
-    if (val <= zero_pt)
-        return 0;
-    return val - zero_pt;
+    return (val > zero_pt) ? val - zero_pt : 0;
 }
 
-void ap_push_pool(fd_queue_info_t * queue_info,
-                                    apr_pool_t * pool_to_recycle)
+void ap_queue_info_push_pool(fd_queue_info_t *queue_info,
+                             apr_pool_t *pool_to_recycle)
 {
     struct recycled_pool *new_recycle;
     /* If we have been given a pool to recycle, atomically link
@@ -220,8 +225,8 @@ void ap_push_pool(fd_queue_info_t * queue_info,
         return;
 
     if (queue_info->max_recycled_pools >= 0) {
-        apr_uint32_t cnt = apr_atomic_read32(&queue_info->recycled_pools_count);
-        if (cnt >= queue_info->max_recycled_pools) {
+        apr_uint32_t n = apr_atomic_read32(&queue_info->recycled_pools_count);
+        if (n >= queue_info->max_recycled_pools) {
             apr_pool_destroy(pool_to_recycle);
             return;
         }
@@ -229,8 +234,7 @@ void ap_push_pool(fd_queue_info_t * queue_info,
     }
 
     apr_pool_clear(pool_to_recycle);
-    new_recycle = (struct recycled_pool *) apr_palloc(pool_to_recycle,
-                                                      sizeof (*new_recycle));
+    new_recycle = apr_palloc(pool_to_recycle, sizeof *new_recycle);
     new_recycle->pool = pool_to_recycle;
     for (;;) {
         /*
@@ -240,13 +244,14 @@ void ap_push_pool(fd_queue_info_t * queue_info,
          */
         struct recycled_pool *next = queue_info->recycled_pools;
         new_recycle->next = next;
-        if (apr_atomic_casptr((void*) &(queue_info->recycled_pools),
+        if (apr_atomic_casptr((void *)&queue_info->recycled_pools,
                               new_recycle, next) == next)
             break;
     }
 }
 
-void ap_pop_pool(apr_pool_t ** recycled_pool, fd_queue_info_t * queue_info)
+void ap_queue_info_pop_pool(fd_queue_info_t *queue_info,
+                            apr_pool_t **recycled_pool)
 {
     /* Atomically pop a pool from the recycled list */
 
@@ -266,9 +271,8 @@ void ap_pop_pool(apr_pool_t ** recycled_pool, fd_queue_info_t * queue_info)
         if (first_pool == NULL) {
             break;
         }
-        if (apr_atomic_casptr
-            ((void*) &(queue_info->recycled_pools),
-             first_pool->next, first_pool) == first_pool) {
+        if (apr_atomic_casptr((void *)&queue_info->recycled_pools,
+                              first_pool->next, first_pool) == first_pool) {
             *recycled_pool = first_pool->pool;
             if (queue_info->max_recycled_pools >= 0)
                 apr_atomic_dec32(&queue_info->recycled_pools_count);
@@ -277,28 +281,33 @@ void ap_pop_pool(apr_pool_t ** recycled_pool, fd_queue_info_t * queue_info)
     }
 }
 
-void ap_free_idle_pools(fd_queue_info_t *queue_info)
+void ap_queue_info_free_idle_pools(fd_queue_info_t *queue_info)
 {
     apr_pool_t *p;
 
     queue_info->max_recycled_pools = 0;
-    do {
-        ap_pop_pool(&p, queue_info);
-        if (p != NULL)
-            apr_pool_destroy(p);
-    } while (p != NULL);
+    for (;;) {
+        ap_queue_info_pop_pool(queue_info, &p);
+        if (p == NULL)
+            break;
+        apr_pool_destroy(p);
+    }
+    apr_atomic_set32(&queue_info->recycled_pools_count, 0);
 }
 
 
-apr_status_t ap_queue_info_term(fd_queue_info_t * queue_info)
+apr_status_t ap_queue_info_term(fd_queue_info_t *queue_info)
 {
     apr_status_t rv;
+
     rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
     if (rv != APR_SUCCESS) {
         return rv;
     }
+
     queue_info->terminated = 1;
     apr_thread_cond_broadcast(queue_info->wait_for_idler);
+
     return apr_thread_mutex_unlock(queue_info->idlers_mutex);
 }
 
@@ -312,7 +321,9 @@ apr_status_t ap_queue_info_term(fd_queue_info_t * queue_info)
  * Detects when the fd_queue_t is empty. This utility function is expected
  * to be called from within critical sections, and is not threadsafe.
  */
-#define ap_queue_empty(queue) ((queue)->nelts == 0 && APR_RING_EMPTY(&queue->timers ,timer_event_t, link))
+#define ap_queue_empty(queue) ((queue)->nelts == 0 && \
+                               APR_RING_EMPTY(&queue->timers, \
+                                              timer_event_t, link))
 
 /**
  * Callback routine that is called to destroy this
@@ -334,35 +345,30 @@ static apr_status_t ap_queue_destroy(void *data)
 /**
  * Initialize the fd_queue_t.
  */
-apr_status_t ap_queue_init(fd_queue_t * queue, int queue_capacity,
-                           apr_pool_t * a)
+apr_status_t ap_queue_create(fd_queue_t **pqueue, int capacity, apr_pool_t *p)
 {
-    int i;
     apr_status_t rv;
+    fd_queue_t *queue;
+
+    queue = apr_pcalloc(p, sizeof *queue);
 
     if ((rv = apr_thread_mutex_create(&queue->one_big_mutex,
                                       APR_THREAD_MUTEX_DEFAULT,
-                                      a)) != APR_SUCCESS) {
+                                      p)) != APR_SUCCESS) {
         return rv;
     }
-    if ((rv = apr_thread_cond_create(&queue->not_empty, a)) != APR_SUCCESS) {
+    if ((rv = apr_thread_cond_create(&queue->not_empty, p)) != APR_SUCCESS) {
         return rv;
     }
 
     APR_RING_INIT(&queue->timers, timer_event_t, link);
 
-    queue->data = apr_palloc(a, queue_capacity * sizeof(fd_queue_elem_t));
-    queue->bounds = queue_capacity;
-    queue->nelts = 0;
-    queue->in = 0;
-    queue->out = 0;
-
-    /* Set all the sockets in the queue to NULL */
-    for (i = 0; i < queue_capacity; ++i)
-        queue->data[i].sd = NULL;
+    queue->data = apr_pcalloc(p, capacity * sizeof(fd_queue_elem_t));
+    queue->bounds = capacity;
 
-    apr_pool_cleanup_register(a, queue, ap_queue_destroy,
+    apr_pool_cleanup_register(p, queue, ap_queue_destroy,
                               apr_pool_cleanup_null);
+    *pqueue = queue;
 
     return APR_SUCCESS;
 }
@@ -373,8 +379,9 @@ apr_status_t ap_queue_init(fd_queue_t * queue, int queue_capacity,
  * precondition: ap_queue_info_wait_for_idler has already been called
  *               to reserve an idle worker thread
  */
-apr_status_t ap_queue_push(fd_queue_t * queue, apr_socket_t * sd,
-                           event_conn_state_t * ecs, apr_pool_t * p)
+apr_status_t ap_queue_push_socket(fd_queue_t *queue,
+                                  apr_socket_t *sd, void *sd_baton,
+                                  apr_pool_t *p)
 {
     fd_queue_elem_t *elem;
     apr_status_t rv;
@@ -386,25 +393,20 @@ apr_status_t ap_queue_push(fd_queue_t * queue, apr_socket_t * sd,
     AP_DEBUG_ASSERT(!queue->terminated);
     AP_DEBUG_ASSERT(!ap_queue_full(queue));
 
-    elem = &queue->data[queue->in];
-    queue->in++;
+    elem = &queue->data[queue->in++];
     if (queue->in >= queue->bounds)
         queue->in -= queue->bounds;
     elem->sd = sd;
-    elem->ecs = ecs;
+    elem->sd_baton = sd_baton;
     elem->p = p;
     queue->nelts++;
 
     apr_thread_cond_signal(queue->not_empty);
 
-    if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
-        return rv;
-    }
-
-    return APR_SUCCESS;
+    return apr_thread_mutex_unlock(queue->one_big_mutex);
 }
 
-apr_status_t ap_queue_push_timer(fd_queue_t * queue, timer_event_t *te)
+apr_status_t ap_queue_push_timer(fd_queue_t *queue, timer_event_t *te)
 {
     apr_status_t rv;
 
@@ -418,11 +420,7 @@ apr_status_t ap_queue_push_timer(fd_queue_t * queue, timer_event_t *te)
 
     apr_thread_cond_signal(queue->not_empty);
 
-    if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
-        return rv;
-    }
-
-    return APR_SUCCESS;
+    return apr_thread_mutex_unlock(queue->one_big_mutex);
 }
 
 /**
@@ -431,11 +429,12 @@ apr_status_t ap_queue_push_timer(fd_queue_t * queue, timer_event_t *te)
  * Once retrieved, the socket is placed into the address specified by
  * 'sd'.
  */
-apr_status_t ap_queue_pop_something(fd_queue_t * queue, apr_socket_t ** sd,
-                                    event_conn_state_t ** ecs, apr_pool_t ** p,
-                                    timer_event_t ** te_out)
+apr_status_t ap_queue_pop_something(fd_queue_t *queue,
+                                    apr_socket_t **sd, void **sd_baton,
+                                    apr_pool_t **p, timer_event_t **te_out)
 {
     fd_queue_elem_t *elem;
+    timer_event_t *te;
     apr_status_t rv;
 
     if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
@@ -462,20 +461,24 @@ apr_status_t ap_queue_pop_something(fd_queue_t * queue, apr_socket_t ** sd,
         }
     }
 
-    *te_out = NULL;
-
-    if (!APR_RING_EMPTY(&queue->timers, timer_event_t, link)) {
-        *te_out = APR_RING_FIRST(&queue->timers);
-        APR_RING_REMOVE(*te_out, link);
+    te = NULL;
+    if (te_out) {
+        if (!APR_RING_EMPTY(&queue->timers, timer_event_t, link)) {
+            te = APR_RING_FIRST(&queue->timers);
+            APR_RING_REMOVE(te, link);
+        }
+        *te_out = te;
     }
-    else {
-        elem = &queue->data[queue->out];
-        queue->out++;
+    if (!te) {
+        elem = &queue->data[queue->out++];
         if (queue->out >= queue->bounds)
             queue->out -= queue->bounds;
         queue->nelts--;
+
         *sd = elem->sd;
-        *ecs = elem->ecs;
+        if (sd_baton) {
+            *sd_baton = elem->sd_baton;
+        }
         *p = elem->p;
 #ifdef AP_DEBUG
         elem->sd = NULL;
@@ -483,17 +486,17 @@ apr_status_t ap_queue_pop_something(fd_queue_t * queue, apr_socket_t ** sd,
 #endif /* AP_DEBUG */
     }
 
-    rv = apr_thread_mutex_unlock(queue->one_big_mutex);
-    return rv;
+    return apr_thread_mutex_unlock(queue->one_big_mutex);
 }
 
-static apr_status_t queue_interrupt(fd_queue_t * queue, int all, int term)
+static apr_status_t queue_interrupt(fd_queue_t *queue, int all, int term)
 {
     apr_status_t rv;
 
     if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
         return rv;
     }
+
     /* we must hold one_big_mutex when setting this... otherwise,
      * we could end up setting it and waking everybody up just after a
      * would-be popper checks it but right before they block
@@ -505,20 +508,23 @@ static apr_status_t queue_interrupt(fd_queue_t * queue, int all, int term)
         apr_thread_cond_broadcast(queue->not_empty);
     else
         apr_thread_cond_signal(queue->not_empty);
+
     return apr_thread_mutex_unlock(queue->one_big_mutex);
 }
 
-apr_status_t ap_queue_interrupt_all(fd_queue_t * queue)
+apr_status_t ap_queue_interrupt_all(fd_queue_t *queue)
 {
     return queue_interrupt(queue, 1, 0);
 }
 
-apr_status_t ap_queue_interrupt_one(fd_queue_t * queue)
+apr_status_t ap_queue_interrupt_one(fd_queue_t *queue)
 {
     return queue_interrupt(queue, 0, 0);
 }
 
-apr_status_t ap_queue_term(fd_queue_t * queue)
+apr_status_t ap_queue_term(fd_queue_t *queue)
 {
     return queue_interrupt(queue, 1, 1);
 }
+
+#endif /* APR_HAS_THREADS */
diff --git a/server/mpm_fdqueue.h b/server/mpm_fdqueue.h
new file mode 100644 (file)
index 0000000..9aeedde
--- /dev/null
@@ -0,0 +1,110 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file  server/mpm_fdqueue.h
+ * @brief fd queue declarations
+ *
+ * @addtogroup APACHE_MPM_EVENT
+ * @{
+ */
+
+#ifndef MPM_FDQUEUE_H
+#define MPM_FDQUEUE_H
+
+#include <apr.h>
+
+/* This code is not AP_DECLARE()ed/exported, and used by MPMs event/worker
+ * only (for now), not worth thinking about w/o threads either...
+ */
+#if APR_HAS_THREADS
+
+#include "ap_mpm.h"
+
+#include <apr_ring.h>
+#include <apr_pools.h>
+#include <apr_thread_mutex.h>
+#include <apr_thread_cond.h>
+#include <apr_network_io.h>
+
+struct fd_queue_info_t; /* opaque */
+struct fd_queue_elem_t; /* opaque */
+typedef struct fd_queue_info_t fd_queue_info_t;
+typedef struct fd_queue_elem_t fd_queue_elem_t;
+
+AP_DECLARE(apr_status_t) ap_queue_info_create(fd_queue_info_t **queue_info,
+                                              apr_pool_t *pool, int max_idlers,
+                                              int max_recycled_pools);
+AP_DECLARE(apr_status_t) ap_queue_info_set_idle(fd_queue_info_t *queue_info,
+                                                apr_pool_t *pool_to_recycle);
+AP_DECLARE(apr_status_t) ap_queue_info_try_get_idler(fd_queue_info_t *queue_info);
+AP_DECLARE(apr_status_t) ap_queue_info_wait_for_idler(fd_queue_info_t *queue_info,
+                                                      int *had_to_block);
+AP_DECLARE(apr_uint32_t) ap_queue_info_num_idlers(fd_queue_info_t *queue_info);
+AP_DECLARE(apr_status_t) ap_queue_info_term(fd_queue_info_t *queue_info);
+
+AP_DECLARE(void) ap_queue_info_pop_pool(fd_queue_info_t *queue_info,
+                                        apr_pool_t **recycled_pool);
+AP_DECLARE(void) ap_queue_info_push_pool(fd_queue_info_t *queue_info,
+                                         apr_pool_t *pool_to_recycle);
+AP_DECLARE(void) ap_queue_info_free_idle_pools(fd_queue_info_t *queue_info);
+
+struct timer_event_t
+{
+    APR_RING_ENTRY(timer_event_t) link;
+    apr_time_t when;
+    ap_mpm_callback_fn_t *cbfunc;
+    void *baton;
+    int canceled;
+    apr_array_header_t *remove;
+};
+typedef struct timer_event_t timer_event_t;
+
+struct fd_queue_t
+{
+    APR_RING_HEAD(timers_t, timer_event_t) timers;
+    fd_queue_elem_t *data;
+    unsigned int nelts;
+    unsigned int bounds;
+    unsigned int in;
+    unsigned int out;
+    apr_thread_mutex_t *one_big_mutex;
+    apr_thread_cond_t *not_empty;
+    int terminated;
+};
+typedef struct fd_queue_t fd_queue_t;
+
+AP_DECLARE(apr_status_t) ap_queue_create(fd_queue_t **pqueue,
+                                         int capacity, apr_pool_t *p);
+AP_DECLARE(apr_status_t) ap_queue_push_socket(fd_queue_t *queue,
+                                              apr_socket_t *sd, void *sd_baton,
+                                              apr_pool_t *p);
+AP_DECLARE(apr_status_t) ap_queue_push_timer(fd_queue_t *queue,
+                                             timer_event_t *te);
+AP_DECLARE(apr_status_t) ap_queue_pop_something(fd_queue_t *queue,
+                                                apr_socket_t **sd, void **sd_baton,
+                                                apr_pool_t **p, timer_event_t **te);
+#define                  ap_queue_pop_socket(q_, s_, p_) \
+                            ap_queue_pop_something((q_), (s_), NULL, (p_), NULL)
+
+AP_DECLARE(apr_status_t) ap_queue_interrupt_all(fd_queue_t *queue);
+AP_DECLARE(apr_status_t) ap_queue_interrupt_one(fd_queue_t *queue);
+AP_DECLARE(apr_status_t) ap_queue_term(fd_queue_t *queue);
+
+#endif /* APR_HAS_THREADS */
+
+#endif /* MPM_FDQUEUE_H */
+/** @} */