        -:    0:Source:/home/dsg/apr_trunk/util-misc/apr_queue.c
        -:    0:Graph:./util-misc/.libs/apr_queue.gcno
        -:    0:Data:./util-misc/.libs/apr_queue.gcda
        -:    0:Runs:346
        -:    1:/* Licensed to the Apache Software Foundation (ASF) under one or more
        -:    2: * contributor license agreements.  See the NOTICE file distributed with
        -:    3: * this work for additional information regarding copyright ownership.
        -:    4: * The ASF licenses this file to You under the Apache License, Version 2.0
        -:    5: * (the "License"); you may not use this file except in compliance with
        -:    6: * the License.  You may obtain a copy of the License at
        -:    7: *
        -:    8: *     http://www.apache.org/licenses/LICENSE-2.0
        -:    9: *
        -:   10: * Unless required by applicable law or agreed to in writing, software
        -:   11: * distributed under the License is distributed on an "AS IS" BASIS,
        -:   12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        -:   13: * See the License for the specific language governing permissions and
        -:   14: * limitations under the License.
        -:   15: */
        -:   16:
        -:   17:#include "apr.h"
        -:   18:
        -:   19:#if APR_HAVE_STDIO_H
        -:   20:#include <stdio.h>
        -:   21:#endif
        -:   22:#if APR_HAVE_STDLIB_H
        -:   23:#include <stdlib.h>
        -:   24:#endif
        -:   25:#if APR_HAVE_UNISTD_H
        -:   26:#include <unistd.h>
        -:   27:#endif
        -:   28:
        -:   29:#include "apu.h"
        -:   30:#include "apr_portable.h"
        -:   31:#include "apr_thread_mutex.h"
        -:   32:#include "apr_thread_cond.h"
        -:   33:#include "apr_errno.h"
        -:   34:#include "apr_queue.h"
        -:   35:
        -:   36:#if APR_HAS_THREADS
        -:   37:/*
        -:   38: * define this to get debug messages
        -:   39: *
        -:   40:#define QUEUE_DEBUG
        -:   41: */
        -:   42:
        -:   43:struct apr_queue_t {
        -:   44:    void              **data;
        -:   45:    unsigned int        nelts; /**< # elements */
        -:   46:    unsigned int        in;    /**< next empty location */
        -:   47:    unsigned int        out;   /**< next filled location */
        -:   48:    unsigned int        bounds;/**< max size of queue */
        -:   49:    unsigned int        full_waiters;
        -:   50:    unsigned int        empty_waiters;
        -:   51:    apr_thread_mutex_t *one_big_mutex;
        -:   52:    apr_thread_cond_t  *not_empty;
        -:   53:    apr_thread_cond_t  *not_full;
        -:   54:    int                 terminated;
        -:   55:};
        -:   56:
        -:   57:#ifdef QUEUE_DEBUG
        -:   58:static void Q_DBG(char*msg, apr_queue_t *q) {
        -:   59:    fprintf(stderr, "%ld\t#%d in %d out %d\t%s\n",
        -:   60:                    apr_os_thread_current(),
        -:   61:                    q->nelts, q->in, q->out,
        -:   62:                    msg
        -:   63:                    );
        -:   64:}
        -:   65:#else
        -:   66:#define Q_DBG(x,y)
        -:   67:#endif
        -:   68:
        -:   69:/**
        -:   70: * Detects when the apr_queue_t is full. This utility function is expected
        -:   71: * to be called from within critical sections, and is not threadsafe.
        -:   72: */
        -:   73:#define apr_queue_full(queue) ((queue)->nelts == (queue)->bounds)
        -:   74:
        -:   75:/**
        -:   76: * Detects when the apr_queue_t is empty. This utility function is expected
        -:   77: * to be called from within critical sections, and is not threadsafe.
        -:   78: */
        -:   79:#define apr_queue_empty(queue) ((queue)->nelts == 0)
        -:   80:
        -:   81:/**
        -:   82: * Callback routine that is called to destroy this
        -:   83: * apr_queue_t when its pool is destroyed.
        -:   84: */
        4:   85:static apr_status_t queue_destroy(void *data)
        -:   86:{
        4:   87:    apr_queue_t *queue = data;
        -:   88:
        -:   89:    /* Ignore errors here, we can't do anything about them anyway. */
        -:   90:
        4:   91:    apr_thread_cond_destroy(queue->not_empty);
        4:   92:    apr_thread_cond_destroy(queue->not_full);
        4:   93:    apr_thread_mutex_destroy(queue->one_big_mutex);
        -:   94:
        4:   95:    return APR_SUCCESS;
        -:   96:}
        -:   97:
        -:   98:/**
        -:   99: * Initialize the apr_queue_t.
        -:  100: */
        4:  101:APR_DECLARE(apr_status_t) apr_queue_create(apr_queue_t **q,
        -:  102:                                           unsigned int queue_capacity,
        -:  103:                                           apr_pool_t *a)
        -:  104:{
        -:  105:    apr_status_t rv;
        -:  106:    apr_queue_t *queue;
        4:  107:    queue = apr_palloc(a, sizeof(apr_queue_t));
        4:  108:    *q = queue;
        -:  109:
        -:  110:    /* nested doesn't work ;( */
        4:  111:    rv = apr_thread_mutex_create(&queue->one_big_mutex,
        -:  112:                                 APR_THREAD_MUTEX_UNNESTED,
        -:  113:                                 a);
        4:  114:    if (rv != APR_SUCCESS) {
    #####:  115:        return rv;
        -:  116:    }
        -:  117:
        4:  118:    rv = apr_thread_cond_create(&queue->not_empty, a);
        4:  119:    if (rv != APR_SUCCESS) {
    #####:  120:        return rv;
        -:  121:    }
        -:  122:
        4:  123:    rv = apr_thread_cond_create(&queue->not_full, a);
        4:  124:    if (rv != APR_SUCCESS) {
    #####:  125:        return rv;
        -:  126:    }
        -:  127:
        -:  128:    /* Set all the data in the queue to NULL */
        4:  129:    queue->data = apr_pcalloc(a, queue_capacity * sizeof(void*));
        4:  130:    queue->bounds = queue_capacity;
        4:  131:    queue->nelts = 0;
        4:  132:    queue->in = 0;
        4:  133:    queue->out = 0;
        4:  134:    queue->terminated = 0;
        4:  135:    queue->full_waiters = 0;
        4:  136:    queue->empty_waiters = 0;
        -:  137:
        4:  138:    apr_pool_cleanup_register(a, queue, queue_destroy, apr_pool_cleanup_null);
        -:  139:
        4:  140:    return APR_SUCCESS;
        -:  141:}
        -:  142:
        -:  143:/**
        -:  144: * Push new data onto the queue. Blocks if the queue is full. Once
        -:  145: * the push operation has completed, it signals other threads waiting
        -:  146: * in apr_queue_pop() that they may continue consuming sockets.
        -:  147: */
      183:  148:static apr_status_t queue_push(apr_queue_t *queue, void *data,
        -:  149:                               apr_interval_time_t timeout)
        -:  150:{
        -:  151:    apr_status_t rv;
        -:  152:
      183:  153:    if (queue->terminated) {
        8:  154:        return APR_EOF; /* no more elements ever again */
        -:  155:    }
        -:  156:
      175:  157:    rv = apr_thread_mutex_lock(queue->one_big_mutex);
      179:  158:    if (rv != APR_SUCCESS) {
    #####:  159:        return rv;
        -:  160:    }
        -:  161:
      179:  162:    if (apr_queue_full(queue)) {
        4:  163:        if (!timeout) {
        2:  164:            apr_thread_mutex_unlock(queue->one_big_mutex);
        2:  165:            return APR_EAGAIN;
        -:  166:        }
        2:  167:        if (!queue->terminated) {
        2:  168:            queue->full_waiters++;
        2:  169:            if (timeout > 0) {
        2:  170:                rv = apr_thread_cond_timedwait(queue->not_full,
        -:  171:                                               queue->one_big_mutex,
        -:  172:                                               timeout);
        -:  173:            }
        -:  174:            else {
    #####:  175:                rv = apr_thread_cond_wait(queue->not_full,
        -:  176:                                          queue->one_big_mutex);
        -:  177:            }
        2:  178:            queue->full_waiters--;
        2:  179:            if (rv != APR_SUCCESS) {
        2:  180:                apr_thread_mutex_unlock(queue->one_big_mutex);
        2:  181:                return rv;
        -:  182:            }
        -:  183:        }
        -:  184:        /* If we wake up and it's still empty, then we were interrupted */
    #####:  185:        if (apr_queue_full(queue)) {
        -:  186:            Q_DBG("queue full (intr)", queue);
    #####:  187:            rv = apr_thread_mutex_unlock(queue->one_big_mutex);
    #####:  188:            if (rv != APR_SUCCESS) {
    #####:  189:                return rv;
        -:  190:            }
    #####:  191:            if (queue->terminated) {
    #####:  192:                return APR_EOF; /* no more elements ever again */
        -:  193:            }
        -:  194:            else {
    #####:  195:                return APR_EINTR;
        -:  196:            }
        -:  197:        }
        -:  198:    }
        -:  199:
      175:  200:    queue->data[queue->in] = data;
      175:  201:    queue->in++;
      175:  202:    if (queue->in >= queue->bounds)
        2:  203:        queue->in -= queue->bounds;
      175:  204:    queue->nelts++;
        -:  205:
      175:  206:    if (queue->empty_waiters) {
        -:  207:        Q_DBG("sig !empty", queue);
        2:  208:        rv = apr_thread_cond_signal(queue->not_empty);
        2:  209:        if (rv != APR_SUCCESS) {
    #####:  210:            apr_thread_mutex_unlock(queue->one_big_mutex);
    #####:  211:            return rv;
        -:  212:        }
        -:  213:    }
        -:  214:
      175:  215:    rv = apr_thread_mutex_unlock(queue->one_big_mutex);
      175:  216:    return rv;
        -:  217:}
        -:  218:
      168:  219:APR_DECLARE(apr_status_t) apr_queue_push(apr_queue_t *queue, void *data)
        -:  220:{
      168:  221:    return queue_push(queue, data, -1);
        -:  222:}
        -:  223:
        -:  224:/**
        -:  225: * Push new data onto the queue. If the queue is full, return APR_EAGAIN. If
        -:  226: * the push operation completes successfully, it signals other threads
        -:  227: * waiting in apr_queue_pop() that they may continue consuming sockets.
        -:  228: */
        8:  229:APR_DECLARE(apr_status_t) apr_queue_trypush(apr_queue_t *queue, void *data)
        -:  230:{
        8:  231:    return queue_push(queue, data, 0);
        -:  232:}
        -:  233:
        6:  234:APR_DECLARE(apr_status_t) apr_queue_timedpush(apr_queue_t *queue, void *data,
        -:  235:                                              apr_interval_time_t timeout)
        -:  236:{
        6:  237:    return queue_push(queue, data, timeout);
        -:  238:}
        -:  239:
        -:  240:/**
        -:  241: * not thread safe
        -:  242: */
    #####:  243:APR_DECLARE(unsigned int) apr_queue_size(apr_queue_t *queue) {
    #####:  244:    return queue->nelts;
        -:  245:}
        -:  246:
        -:  247:/**
        -:  248: * Retrieves the next item from the queue. If there are no
        -:  249: * items available, it will either return APR_EAGAIN (timeout = 0),
        -:  250: * or block until one becomes available (infinitely with timeout < 0,
        -:  251: * otherwise until the given timeout expires). Once retrieved, the
        -:  252: * item is placed into the address specified by 'data'.
        -:  253: */
      118:  254:static apr_status_t queue_pop(apr_queue_t *queue, void **data,
        -:  255:                              apr_interval_time_t timeout)
        -:  256:{
        -:  257:    apr_status_t rv;
        -:  258:
      118:  259:    if (queue->terminated) {
        6:  260:        return APR_EOF; /* no more elements ever again */
        -:  261:    }
        -:  262:
      112:  263:    rv = apr_thread_mutex_lock(queue->one_big_mutex);
      114:  264:    if (rv != APR_SUCCESS) {
    #####:  265:        return rv;
        -:  266:    }
        -:  267:
        -:  268:    /* Keep waiting until we wake up and find that the queue is not empty. */
      114:  269:    if (apr_queue_empty(queue)) {
        6:  270:        if (!timeout) {
        2:  271:            apr_thread_mutex_unlock(queue->one_big_mutex);
        2:  272:            return APR_EAGAIN;
        -:  273:        }
        4:  274:        if (!queue->terminated) {
        4:  275:            queue->empty_waiters++;
        4:  276:            if (timeout > 0) {
        2:  277:                rv = apr_thread_cond_timedwait(queue->not_empty,
        -:  278:                                               queue->one_big_mutex,
        -:  279:                                               timeout);
        -:  280:            }
        -:  281:            else {
        2:  282:                rv = apr_thread_cond_wait(queue->not_empty,
        -:  283:                                          queue->one_big_mutex);
        -:  284:            }
        4:  285:            queue->empty_waiters--;
        4:  286:            if (rv != APR_SUCCESS) {
        2:  287:                apr_thread_mutex_unlock(queue->one_big_mutex);
        2:  288:                return rv;
        -:  289:            }
        -:  290:        }
        -:  291:        /* If we wake up and it's still empty, then we were interrupted */
        2:  292:        if (apr_queue_empty(queue)) {
        -:  293:            Q_DBG("queue empty (intr)", queue);
    #####:  294:            rv = apr_thread_mutex_unlock(queue->one_big_mutex);
    #####:  295:            if (rv != APR_SUCCESS) {
    #####:  296:                return rv;
        -:  297:            }
    #####:  298:            if (queue->terminated) {
    #####:  299:                return APR_EOF; /* no more elements ever again */
        -:  300:            }
        -:  301:            else {
    #####:  302:                return APR_EINTR;
        -:  303:            }
        -:  304:        }
        -:  305:    }
        -:  306:
      110:  307:    *data = queue->data[queue->out];
      110:  308:    queue->nelts--;
        -:  309:
      110:  310:    queue->out++;
      110:  311:    if (queue->out >= queue->bounds)
        2:  312:        queue->out -= queue->bounds;
      110:  313:    if (queue->full_waiters) {
        -:  314:        Q_DBG("signal !full", queue);
    #####:  315:        rv = apr_thread_cond_signal(queue->not_full);
    #####:  316:        if (rv != APR_SUCCESS) {
    #####:  317:            apr_thread_mutex_unlock(queue->one_big_mutex);
    #####:  318:            return rv;
        -:  319:        }
        -:  320:    }
        -:  321:
      110:  322:    rv = apr_thread_mutex_unlock(queue->one_big_mutex);
      110:  323:    return rv;
        -:  324:}
        -:  325:
      104:  326:APR_DECLARE(apr_status_t) apr_queue_pop(apr_queue_t *queue, void **data)
        -:  327:{
      104:  328:    return queue_pop(queue, data, -1);
        -:  329:}
        -:  330:
        8:  331:APR_DECLARE(apr_status_t) apr_queue_trypop(apr_queue_t *queue, void **data)
        -:  332:{
        8:  333:    return queue_pop(queue, data, 0);
        -:  334:}
        -:  335:
        6:  336:APR_DECLARE(apr_status_t) apr_queue_timedpop(apr_queue_t *queue, void **data,
        -:  337:                                             apr_interval_time_t timeout)
        -:  338:{
        6:  339:    return queue_pop(queue, data, timeout);
        -:  340:}
        -:  341:
        4:  342:APR_DECLARE(apr_status_t) apr_queue_interrupt_all(apr_queue_t *queue)
        -:  343:{
        -:  344:    apr_status_t rv;
        -:  345:    Q_DBG("intr all", queue);
        4:  346:    if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
    #####:  347:        return rv;
        -:  348:    }
        4:  349:    apr_thread_cond_broadcast(queue->not_empty);
        4:  350:    apr_thread_cond_broadcast(queue->not_full);
        -:  351:
        4:  352:    if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
    #####:  353:        return rv;
        -:  354:    }
        -:  355:
        4:  356:    return APR_SUCCESS;
        -:  357:}
        -:  358:
        4:  359:APR_DECLARE(apr_status_t) apr_queue_term(apr_queue_t *queue)
        -:  360:{
        -:  361:    apr_status_t rv;
        -:  362:
        4:  363:    if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
    #####:  364:        return rv;
        -:  365:    }
        -:  366:
        -:  367:    /* we must hold one_big_mutex when setting this... otherwise,
        -:  368:     * we could end up setting it and waking everybody up just after a
        -:  369:     * would-be popper checks it but right before they block
        -:  370:     */
        4:  371:    queue->terminated = 1;
        4:  372:    if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
    #####:  373:        return rv;
        -:  374:    }
        4:  375:    return apr_queue_interrupt_all(queue);
        -:  376:}
        -:  377:
        -:  378:#endif /* APR_HAS_THREADS */
