fdqueue.c revision 6a5bdbbacf4a62adecde52b8f23ebcc4fa2a08b8
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony/* ====================================================================
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * The Apache Software License, Version 1.1
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * reserved.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * Redistribution and use in source and binary forms, with or without
d03cac4d8ba79a23cfda410d35b614b0d805ba4cnilgun * modification, are permitted provided that the following conditions
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * 1. Redistributions of source code must retain the above copyright
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * notice, this list of conditions and the following disclaimer.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * 2. Redistributions in binary form must reproduce the above copyright
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * notice, this list of conditions and the following disclaimer in
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * the documentation and/or other materials provided with the
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen * distribution.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * 3. The end-user documentation included with the redistribution,
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * if any, must include the following acknowledgment:
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * "This product includes software developed by the
3f08db06526d6901aa08c110b5bc7dde6bc39905nd * Apache Software Foundation (http://www.apache.org/)."
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * Alternately, this acknowledgment may appear in the software itself,
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * if and wherever such third-party acknowledgments normally appear.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * 4. The names "Apache" and "Apache Software Foundation" must
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * not be used to endorse or promote products derived from this
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * software without prior written permission. For written
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * permission, please contact apache@apache.org.
c3c006c28c5b03892ccaef6e4d2cbb15a13a2072rbowen * 5. Products derived from this software may not be called "Apache",
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * nor may "Apache" appear in their name, without prior written
a166215fba9f76030a979c81f95becff52f5aeefrbowen * permission of the Apache Software Foundation.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d03cac4d8ba79a23cfda410d35b614b0d805ba4cnilgun * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * SUCH DAMAGE.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * ====================================================================
a166215fba9f76030a979c81f95becff52f5aeefrbowen * This software consists of voluntary contributions made by many
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * individuals on behalf of the Apache Software Foundation. For more
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * information on the Apache Software Foundation, please see
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * Portions of this software are based upon public domain software
dfc5f686a6ac416a480f0281c9ff0a751013fcf2nilgun * originally written at the National Center for Supercomputing Applications,
a166215fba9f76030a979c81f95becff52f5aeefrbowen * University of Illinois, Urbana-Champaign.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * Detects when the fd_queue_t is full. This utility function is expected
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * to be called from within critical sections, and is not threadsafe.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony#define ap_queue_full(queue) ((queue)->tail == (queue)->bounds)
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * Detects when the fd_queue_t is empty. This utility function is expected
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * to be called from within critical sections, and is not threadsafe.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * Callback routine that is called to destroy this
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * fd_queue_t when its pool is destroyed.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony /* Ignore errors here, we can't do anything about them anyway.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * XXX: We should at least try to signal an error here, it is
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * indicative of a programmer error. -aaron */
d03cac4d8ba79a23cfda410d35b614b0d805ba4cnilgun * Initialize the fd_queue_t.
d03cac4d8ba79a23cfda410d35b614b0d805ba4cnilgunapr_status_t ap_queue_init(fd_queue_t *queue, int queue_capacity, apr_pool_t *a)
d03cac4d8ba79a23cfda410d35b614b0d805ba4cnilgun if ((rv = apr_thread_mutex_create(&queue->one_big_mutex,
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony if ((rv = apr_thread_cond_create(&queue->not_empty, a)) != APR_SUCCESS) {
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony if ((rv = apr_thread_cond_create(&queue->not_full, a)) != APR_SUCCESS) {
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony queue->data = apr_palloc(a, queue_capacity * sizeof(fd_queue_elem_t));
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony /* Set all the sockets in the queue to NULL */
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony for (i = 0; i < queue_capacity; ++i)
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony apr_pool_cleanup_register(a, queue, ap_queue_destroy, apr_pool_cleanup_null);
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * Push a new socket onto the queue. Blocks if the queue is full. Once
a166215fba9f76030a979c81f95becff52f5aeefrbowen * the push operation has completed, it signals other threads waiting
a166215fba9f76030a979c81f95becff52f5aeefrbowen * in apr_queue_pop() that they may continue consuming sockets.
a166215fba9f76030a979c81f95becff52f5aeefrbowenapr_status_t ap_queue_push(fd_queue_t *queue, apr_socket_t *sd, apr_pool_t *p,
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony apr_thread_cond_wait(queue->not_full, queue->one_big_mutex);
d03cac4d8ba79a23cfda410d35b614b0d805ba4cnilgun *recycled_pool = queue->recycled_pools[--queue->num_recycled];
d03cac4d8ba79a23cfda410d35b614b0d805ba4cnilgun if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * Retrieves the next available socket from the queue. If there are no
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * sockets available, it will block until one becomes available.
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony * Once retrieved, the socket is placed into the address specified by
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctonyapr_status_t ap_queue_pop(fd_queue_t *queue, apr_socket_t **sd, apr_pool_t **p,
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
0f6bc9e2c8229996a73fb57ec6c45841346ebcb1pctony queue->recycled_pools[queue->num_recycled++] = recycled_pool;
0d0ba3a410038e179b695446bb149cce6264e0abnd /* Keep waiting until we wake up and find that the queue is not empty. */
0d0ba3a410038e179b695446bb149cce6264e0abnd apr_thread_cond_wait(queue->not_empty, queue->one_big_mutex);
0d0ba3a410038e179b695446bb149cce6264e0abnd /* If we wake up and it's still empty, then we were interrupted */
0d0ba3a410038e179b695446bb149cce6264e0abnd if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
return rv;
return APR_SUCCESS;
return rv;
return rv;
return APR_SUCCESS;
return rv;
return rv;