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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
38 * define this to get debug messages
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;
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,
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.
73 #define apr_queue_full(queue) ((queue)->nelts == (queue)->bounds)
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.
79 #define apr_queue_empty(queue) ((queue)->nelts == 0)
82 * Callback routine that is called to destroy this
83 * apr_queue_t when its pool is destroyed.
85 static apr_status_t queue_destroy(void *data)
87 apr_queue_t *queue = data;
89 /* Ignore errors here, we can't do anything about them anyway. */
91 apr_thread_cond_destroy(queue->not_empty);
92 apr_thread_cond_destroy(queue->not_full);
93 apr_thread_mutex_destroy(queue->one_big_mutex);
99 * Initialize the apr_queue_t.
101 APU_DECLARE(apr_status_t) apr_queue_create(apr_queue_t **q,
102 unsigned int queue_capacity,
107 queue = apr_palloc(a, sizeof(apr_queue_t));
110 /* nested doesn't work ;( */
111 rv = apr_thread_mutex_create(&queue->one_big_mutex,
112 APR_THREAD_MUTEX_UNNESTED,
114 if (rv != APR_SUCCESS) {
118 rv = apr_thread_cond_create(&queue->not_empty, a);
119 if (rv != APR_SUCCESS) {
123 rv = apr_thread_cond_create(&queue->not_full, a);
124 if (rv != APR_SUCCESS) {
128 /* Set all the data in the queue to NULL */
129 queue->data = apr_pcalloc(a, queue_capacity * sizeof(void*));
130 queue->bounds = queue_capacity;
134 queue->terminated = 0;
135 queue->full_waiters = 0;
136 queue->empty_waiters = 0;
138 apr_pool_cleanup_register(a, queue, queue_destroy, apr_pool_cleanup_null);
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.
148 APU_DECLARE(apr_status_t) apr_queue_push(apr_queue_t *queue, void *data)
152 if (queue->terminated) {
153 return APR_EOF; /* no more elements ever again */
156 rv = apr_thread_mutex_lock(queue->one_big_mutex);
157 if (rv != APR_SUCCESS) {
161 if (apr_queue_full(queue)) {
162 if (!queue->terminated) {
163 queue->full_waiters++;
164 rv = apr_thread_cond_wait(queue->not_full, queue->one_big_mutex);
165 queue->full_waiters--;
166 if (rv != APR_SUCCESS) {
167 apr_thread_mutex_unlock(queue->one_big_mutex);
171 /* If we wake up and it's still empty, then we were interrupted */
172 if (apr_queue_full(queue)) {
173 Q_DBG("queue full (intr)", queue);
174 rv = apr_thread_mutex_unlock(queue->one_big_mutex);
175 if (rv != APR_SUCCESS) {
178 if (queue->terminated) {
179 return APR_EOF; /* no more elements ever again */
187 queue->data[queue->in] = data;
189 if (queue->in >= queue->bounds)
190 queue->in -= queue->bounds;
193 if (queue->empty_waiters) {
194 Q_DBG("sig !empty", queue);
195 rv = apr_thread_cond_signal(queue->not_empty);
196 if (rv != APR_SUCCESS) {
197 apr_thread_mutex_unlock(queue->one_big_mutex);
202 rv = apr_thread_mutex_unlock(queue->one_big_mutex);
207 * Push new data onto the queue. If the queue is full, return APR_EAGAIN. If
208 * the push operation completes successfully, it signals other threads
209 * waiting in apr_queue_pop() that they may continue consuming sockets.
211 APU_DECLARE(apr_status_t) apr_queue_trypush(apr_queue_t *queue, void *data)
215 if (queue->terminated) {
216 return APR_EOF; /* no more elements ever again */
219 rv = apr_thread_mutex_lock(queue->one_big_mutex);
220 if (rv != APR_SUCCESS) {
224 if (apr_queue_full(queue)) {
225 rv = apr_thread_mutex_unlock(queue->one_big_mutex);
229 queue->data[queue->in] = data;
231 if (queue->in >= queue->bounds)
232 queue->in -= queue->bounds;
235 if (queue->empty_waiters) {
236 Q_DBG("sig !empty", queue);
237 rv = apr_thread_cond_signal(queue->not_empty);
238 if (rv != APR_SUCCESS) {
239 apr_thread_mutex_unlock(queue->one_big_mutex);
244 rv = apr_thread_mutex_unlock(queue->one_big_mutex);
251 APU_DECLARE(unsigned int) apr_queue_size(apr_queue_t *queue) {
256 * Retrieves the next item from the queue. If there are no
257 * items available, it will block until one becomes available.
258 * Once retrieved, the item is placed into the address specified by
261 APU_DECLARE(apr_status_t) apr_queue_pop(apr_queue_t *queue, void **data)
265 if (queue->terminated) {
266 return APR_EOF; /* no more elements ever again */
269 rv = apr_thread_mutex_lock(queue->one_big_mutex);
270 if (rv != APR_SUCCESS) {
274 /* Keep waiting until we wake up and find that the queue is not empty. */
275 if (apr_queue_empty(queue)) {
276 if (!queue->terminated) {
277 queue->empty_waiters++;
278 rv = apr_thread_cond_wait(queue->not_empty, queue->one_big_mutex);
279 queue->empty_waiters--;
280 if (rv != APR_SUCCESS) {
281 apr_thread_mutex_unlock(queue->one_big_mutex);
285 /* If we wake up and it's still empty, then we were interrupted */
286 if (apr_queue_empty(queue)) {
287 Q_DBG("queue empty (intr)", queue);
288 rv = apr_thread_mutex_unlock(queue->one_big_mutex);
289 if (rv != APR_SUCCESS) {
292 if (queue->terminated) {
293 return APR_EOF; /* no more elements ever again */
301 *data = queue->data[queue->out];
305 if (queue->out >= queue->bounds)
306 queue->out -= queue->bounds;
307 if (queue->full_waiters) {
308 Q_DBG("signal !full", queue);
309 rv = apr_thread_cond_signal(queue->not_full);
310 if (rv != APR_SUCCESS) {
311 apr_thread_mutex_unlock(queue->one_big_mutex);
316 rv = apr_thread_mutex_unlock(queue->one_big_mutex);
321 * Retrieves the next item from the queue. If there are no
322 * items available, return APR_EAGAIN. Once retrieved,
323 * the item is placed into the address specified by 'data'.
325 APU_DECLARE(apr_status_t) apr_queue_trypop(apr_queue_t *queue, void **data)
329 if (queue->terminated) {
330 return APR_EOF; /* no more elements ever again */
333 rv = apr_thread_mutex_lock(queue->one_big_mutex);
334 if (rv != APR_SUCCESS) {
338 if (apr_queue_empty(queue)) {
339 rv = apr_thread_mutex_unlock(queue->one_big_mutex);
343 *data = queue->data[queue->out];
347 if (queue->out >= queue->bounds)
348 queue->out -= queue->bounds;
349 if (queue->full_waiters) {
350 Q_DBG("signal !full", queue);
351 rv = apr_thread_cond_signal(queue->not_full);
352 if (rv != APR_SUCCESS) {
353 apr_thread_mutex_unlock(queue->one_big_mutex);
358 rv = apr_thread_mutex_unlock(queue->one_big_mutex);
362 APU_DECLARE(apr_status_t) apr_queue_interrupt_all(apr_queue_t *queue)
365 Q_DBG("intr all", queue);
366 if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
369 apr_thread_cond_broadcast(queue->not_empty);
370 apr_thread_cond_broadcast(queue->not_full);
372 if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
379 APU_DECLARE(apr_status_t) apr_queue_term(apr_queue_t *queue)
383 if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
387 /* we must hold one_big_mutex when setting this... otherwise,
388 * we could end up setting it and waking everybody up just after a
389 * would-be popper checks it but right before they block
391 queue->terminated = 1;
392 if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
395 return apr_queue_interrupt_all(queue);
398 #endif /* APR_HAS_THREADS */