mirror of
https://github.com/apache/httpd.git
synced 2026-01-13 21:42:17 +03:00
(the old code assumed that apr_atomic_t and apr_uint32_t were interchangeable). Also, add more detailed comments on how one of the synchronization functions works. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@101340 13f79535-47bb-0310-9956-ffa450edef68
426 lines
14 KiB
C
426 lines
14 KiB
C
/* ====================================================================
|
|
* The Apache Software License, Version 1.1
|
|
*
|
|
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
|
|
* reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in
|
|
* the documentation and/or other materials provided with the
|
|
* distribution.
|
|
*
|
|
* 3. The end-user documentation included with the redistribution,
|
|
* if any, must include the following acknowledgment:
|
|
* "This product includes software developed by the
|
|
* Apache Software Foundation (http://www.apache.org/)."
|
|
* Alternately, this acknowledgment may appear in the software itself,
|
|
* if and wherever such third-party acknowledgments normally appear.
|
|
*
|
|
* 4. The names "Apache" and "Apache Software Foundation" must
|
|
* not be used to endorse or promote products derived from this
|
|
* software without prior written permission. For written
|
|
* permission, please contact apache@apache.org.
|
|
*
|
|
* 5. Products derived from this software may not be called "Apache",
|
|
* nor may "Apache" appear in their name, without prior written
|
|
* permission of the Apache Software Foundation.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
* ====================================================================
|
|
*
|
|
* This software consists of voluntary contributions made by many
|
|
* individuals on behalf of the Apache Software Foundation. For more
|
|
* information on the Apache Software Foundation, please see
|
|
* <http://www.apache.org/>.
|
|
*
|
|
* Portions of this software are based upon public domain software
|
|
* originally written at the National Center for Supercomputing Applications,
|
|
* University of Illinois, Urbana-Champaign.
|
|
*/
|
|
|
|
#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((volatile 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_palloc(pool, sizeof(*qi));
|
|
memset(qi, 0, 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 (;;) {
|
|
new_recycle->next = queue_info->recycled_pools;
|
|
if (apr_atomic_casptr((volatile void**)&(queue_info->recycled_pools),
|
|
new_recycle, new_recycle->next) ==
|
|
new_recycle->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.
|
|
* - 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.
|
|
*/
|
|
if (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 */
|
|
for (;;) {
|
|
struct recycled_pool *first_pool = queue_info->recycled_pools;
|
|
if (first_pool == NULL) {
|
|
break;
|
|
}
|
|
if (apr_atomic_casptr((volatile 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;
|
|
|
|
/* 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. Blocks if the queue is full. Once
|
|
* the push operation has completed, it signals other threads waiting
|
|
* in ap_queue_pop() that they may continue consuming sockets.
|
|
*/
|
|
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->nelts];
|
|
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->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;
|
|
}
|
|
|
|
apr_status_t ap_queue_interrupt_all(fd_queue_t *queue)
|
|
{
|
|
apr_status_t rv;
|
|
|
|
if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
|
|
return rv;
|
|
}
|
|
apr_thread_cond_broadcast(queue->not_empty);
|
|
return apr_thread_mutex_unlock(queue->one_big_mutex);
|
|
}
|
|
|
|
apr_status_t ap_queue_term(fd_queue_t *queue)
|
|
{
|
|
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
|
|
*/
|
|
queue->terminated = 1;
|
|
if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
|
|
return rv;
|
|
}
|
|
return ap_queue_interrupt_all(queue);
|
|
}
|