fdqueue.c revision 3a9caf3ee8db8abb70315ea0556f2030aa022b4c
/* 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
*
*
* 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
{
struct recycled_pool *next;
struct fd_queue_info_t
{
* 0 or positive: number of idle worker threads
* negative: number of threads blocked waiting
* for an idle worker
*/
int terminated;
int max_idlers;
int max_recycled_pools;
};
{
/* Clean up any pools in the recycled list */
for (;;) {
if (first_pool == NULL) {
break;
}
first_pool) == first_pool) {
}
}
return APR_SUCCESS;
}
int max_recycled_pools)
{
pool);
if (rv != APR_SUCCESS) {
return rv;
}
if (rv != APR_SUCCESS) {
return rv;
}
*queue_info = qi;
return APR_SUCCESS;
}
{
int prev_idlers;
/* Atomically increment the count of idle workers */
/* If other threads are waiting on a worker, wake one up */
if (prev_idlers < 0) {
if (rv != APR_SUCCESS) {
AP_DEBUG_ASSERT(0);
return rv;
}
if (rv != APR_SUCCESS) {
return rv;
}
if (rv != APR_SUCCESS) {
return rv;
}
}
return APR_SUCCESS;
}
{
int prev_idlers;
if (prev_idlers <= 0) {
return APR_EAGAIN;
}
return APR_SUCCESS;
}
int *had_to_block)
{
int prev_idlers;
/* Atomically decrement the idle worker count, saving the old value */
/* See TODO in ap_queue_info_set_idle() */
/* Block if there weren't any idle workers */
if (prev_idlers <= 0) {
if (rv != APR_SUCCESS) {
AP_DEBUG_ASSERT(0);
/* See TODO in ap_queue_info_set_idle() */
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 negative, the
* workers are all still busy, so it's safe to
* block on a condition variable.
* - If the idle worker count is non-negative, 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 non-negative, it's safe for this function to
* return immediately.
*
* A "negative value" (relative to zero_pt) in
* queue_info->idlers tells how many
* threads are waiting on an idle worker.
*/
*had_to_block = 1;
if (rv != APR_SUCCESS) {
AP_DEBUG_ASSERT(0);
if (rv2 != APR_SUCCESS) {
return rv2;
}
return rv;
}
}
if (rv != APR_SUCCESS) {
return rv;
}
}
if (queue_info->terminated) {
return APR_EOF;
}
else {
return APR_SUCCESS;
}
}
{
if (val < 0)
return 0;
return val;
}
{
struct recycled_pool *new_recycle;
/* 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)
return;
if (queue_info->max_recycled_pools >= 0) {
return;
}
}
sizeof (*new_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.
*/
break;
}
}
{
/* 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.
*/
*recycled_pool = NULL;
/* Atomically pop a pool from the recycled list */
for (;;) {
if (first_pool == NULL) {
break;
}
((void*) &(queue_info->recycled_pools),
if (queue_info->max_recycled_pools >= 0)
break;
}
}
}
{
if (rv != APR_SUCCESS) {
return rv;
}
}
/**
* Detects when the fd_queue_t is full. This utility function is expected
* to be called from within critical sections, and is not threadsafe.
*/
/**
* 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))
/**
* Callback routine that is called to destroy this
* fd_queue_t when its pool is destroyed.
*/
{
/* 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 */
return APR_SUCCESS;
}
/**
* Initialize the fd_queue_t.
*/
apr_pool_t * a)
{
int i;
a)) != APR_SUCCESS) {
return rv;
}
return rv;
}
/* Set all the sockets in the queue to NULL */
for (i = 0; i < queue_capacity; ++i)
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
*/
{
return rv;
}
elem->p = p;
return rv;
}
return APR_SUCCESS;
}
{
return rv;
}
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'.
*/
timer_event_t ** te_out)
{
return rv;
}
/* Keep waiting until we wake up and find that the queue is not empty. */
if (ap_queue_empty(queue)) {
if (!queue->terminated) {
}
/* If we wake up and it's still empty, then we were interrupted */
if (ap_queue_empty(queue)) {
if (rv != APR_SUCCESS) {
return rv;
}
if (queue->terminated) {
return APR_EOF; /* no more elements ever again */
}
else {
return APR_EINTR;
}
}
}
}
else {
*p = elem->p;
#ifdef AP_DEBUG
#endif /* AP_DEBUG */
}
return rv;
}
{
return rv;
}
}
{
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
*/
return rv;
}
return ap_queue_interrupt_all(queue);
}