1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GAsyncQueue: asynchronous queue implementation, based on Gqueue.
5 * Copyright (C) 2000 Sebastian Wilhelmi; University of Karlsruhe
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
37 guint waiting_threads;
44 * Creates a new asynchronous queue with the initial reference count of 1.
46 * Return value: the new #GAsyncQueue.
51 GAsyncQueue* retval = g_new (GAsyncQueue, 1);
52 retval->mutex = g_mutex_new ();
54 retval->queue = g_queue_new ();
55 retval->waiting_threads = 0;
56 retval->ref_count = 1;
62 * @queue: a #GAsyncQueue.
64 * Increases the reference count of the asynchronous @queue by 1.
67 g_async_queue_ref (GAsyncQueue *queue)
69 g_return_if_fail (queue);
70 g_return_if_fail (queue->ref_count > 0);
72 g_mutex_lock (queue->mutex);
74 g_mutex_unlock (queue->mutex);
78 * g_async_queue_ref_unlocked:
79 * @queue: a #GAsyncQueue.
81 * Increases the reference count of the asynchronous @queue by 1. This
82 * function must be called while holding the @queue's lock.
85 g_async_queue_ref_unlocked (GAsyncQueue *queue)
87 g_return_if_fail (queue);
88 g_return_if_fail (queue->ref_count > 0);
94 * g_async_queue_unref_and_unlock:
95 * @queue: a #GAsyncQueue.
97 * Decreases the reference count of the asynchronous @queue by 1 and
98 * releases the lock. This function must be called while holding the
99 * @queue's lock. If the reference count went to 0, the @queue will be
100 * destroyed and the memory allocated will be freed. So you are not
101 * allowed to use the @queue afterwards, as it might have disappeared.
102 * The obvious asymmetry (it is not named
103 * g_async_queue_unref_unlocked(<!-- -->)) is because the queue can't be
104 * unlocked after unreffing it, as it might already have disappeared.
107 g_async_queue_unref_and_unlock (GAsyncQueue *queue)
111 g_return_if_fail (queue);
112 g_return_if_fail (queue->ref_count > 0);
115 stop = (queue->ref_count == 0);
116 g_mutex_unlock (queue->mutex);
120 g_return_if_fail (queue->waiting_threads == 0);
121 g_mutex_free (queue->mutex);
123 g_cond_free (queue->cond);
124 g_queue_free (queue->queue);
130 * g_async_queue_unref:
131 * @queue: a #GAsyncQueue.
133 * Decreases the reference count of the asynchronous @queue by 1. If
134 * the reference count went to 0, the @queue will be destroyed and the
135 * memory allocated will be freed. So you are not allowed to use the
136 * @queue afterwards, as it might have disappeared.
139 g_async_queue_unref (GAsyncQueue *queue)
141 g_return_if_fail (queue);
142 g_return_if_fail (queue->ref_count > 0);
144 g_mutex_lock (queue->mutex);
145 g_async_queue_unref_and_unlock (queue);
149 * g_async_queue_lock:
150 * @queue: a #GAsyncQueue.
152 * Acquires the @queue's lock. After that you can only call the
153 * <function>g_async_queue_*_unlocked()</function> function variants on that
154 * @queue. Otherwise it will deadlock.
157 g_async_queue_lock (GAsyncQueue *queue)
159 g_return_if_fail (queue);
160 g_return_if_fail (queue->ref_count > 0);
162 g_mutex_lock (queue->mutex);
166 * g_async_queue_unlock:
167 * @queue: a #GAsyncQueue.
169 * Releases the queue's lock.
172 g_async_queue_unlock (GAsyncQueue *queue)
174 g_return_if_fail (queue);
175 g_return_if_fail (queue->ref_count > 0);
177 g_mutex_unlock (queue->mutex);
181 * g_async_queue_push:
182 * @queue: a #GAsyncQueue.
183 * @data: @data to push into the @queue.
185 * Pushes the @data into the @queue. @data must not be %NULL.
188 g_async_queue_push (GAsyncQueue* queue, gpointer data)
190 g_return_if_fail (queue);
191 g_return_if_fail (queue->ref_count > 0);
192 g_return_if_fail (data);
194 g_mutex_lock (queue->mutex);
195 g_async_queue_push_unlocked (queue, data);
196 g_mutex_unlock (queue->mutex);
200 * g_async_queue_push_unlocked:
201 * @queue: a #GAsyncQueue.
202 * @data: @data to push into the @queue.
204 * Pushes the @data into the @queue. @data must not be %NULL. This
205 * function must be called while holding the @queue's lock.
208 g_async_queue_push_unlocked (GAsyncQueue* queue, gpointer data)
210 g_return_if_fail (queue);
211 g_return_if_fail (queue->ref_count > 0);
212 g_return_if_fail (data);
214 g_queue_push_head (queue->queue, data);
215 if (queue->waiting_threads > 0)
216 g_cond_signal (queue->cond);
220 g_async_queue_pop_intern_unlocked (GAsyncQueue* queue, gboolean try,
225 if (!g_queue_peek_tail (queue->queue))
231 queue->cond = g_cond_new ();
235 queue->waiting_threads++;
236 while (!g_queue_peek_tail (queue->queue))
237 g_cond_wait(queue->cond, queue->mutex);
238 queue->waiting_threads--;
242 queue->waiting_threads++;
243 while (!g_queue_peek_tail (queue->queue))
244 if (!g_cond_timed_wait (queue->cond, queue->mutex, end_time))
246 queue->waiting_threads--;
247 if (!g_queue_peek_tail (queue->queue))
252 retval = g_queue_pop_tail (queue->queue);
261 * @queue: a #GAsyncQueue.
263 * Pops data from the @queue. This function blocks until data become
266 * Return value: data from the queue.
269 g_async_queue_pop (GAsyncQueue* queue)
273 g_return_val_if_fail (queue, NULL);
274 g_return_val_if_fail (queue->ref_count > 0, NULL);
276 g_mutex_lock (queue->mutex);
277 retval = g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
278 g_mutex_unlock (queue->mutex);
284 * g_async_queue_pop_unlocked:
285 * @queue: a #GAsyncQueue.
287 * Pops data from the @queue. This function blocks until data become
288 * available. This function must be called while holding the @queue's
291 * Return value: data from the queue.
294 g_async_queue_pop_unlocked (GAsyncQueue* queue)
296 g_return_val_if_fail (queue, NULL);
297 g_return_val_if_fail (queue->ref_count > 0, NULL);
299 return g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
303 * g_async_queue_try_pop:
304 * @queue: a #GAsyncQueue.
306 * Tries to pop data from the @queue. If no data is available, %NULL is
309 * Return value: data from the queue or %NULL, when no data is
310 * available immediately.
313 g_async_queue_try_pop (GAsyncQueue* queue)
317 g_return_val_if_fail (queue, NULL);
318 g_return_val_if_fail (queue->ref_count > 0, NULL);
320 g_mutex_lock (queue->mutex);
321 retval = g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
322 g_mutex_unlock (queue->mutex);
328 * g_async_queue_try_pop_unlocked:
329 * @queue: a #GAsyncQueue.
331 * Tries to pop data from the @queue. If no data is available, %NULL is
332 * returned. This function must be called while holding the @queue's
335 * Return value: data from the queue or %NULL, when no data is
336 * available immediately.
339 g_async_queue_try_pop_unlocked (GAsyncQueue* queue)
341 g_return_val_if_fail (queue, NULL);
342 g_return_val_if_fail (queue->ref_count > 0, NULL);
344 return g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
348 * g_async_queue_timed_pop:
349 * @queue: a #GAsyncQueue.
350 * @end_time: a #GTimeVal, determining the final time.
352 * Pops data from the @queue. If no data is received before @end_time,
355 * To easily calculate @end_time a combination of g_get_current_time()
356 * and g_time_val_add() can be used.
358 * Return value: data from the queue or %NULL, when no data is
359 * received before @end_time.
362 g_async_queue_timed_pop (GAsyncQueue* queue, GTimeVal *end_time)
366 g_return_val_if_fail (queue, NULL);
367 g_return_val_if_fail (queue->ref_count > 0, NULL);
369 g_mutex_lock (queue->mutex);
370 retval = g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
371 g_mutex_unlock (queue->mutex);
377 * g_async_queue_timed_pop_unlocked:
378 * @queue: a #GAsyncQueue.
379 * @end_time: a #GTimeVal, determining the final time.
381 * Pops data from the @queue. If no data is received before @end_time,
382 * %NULL is returned. This function must be called while holding the
385 * To easily calculate @end_time a combination of g_get_current_time()
386 * and g_time_val_add() can be used.
388 * Return value: data from the queue or %NULL, when no data is
389 * received before @end_time.
392 g_async_queue_timed_pop_unlocked (GAsyncQueue* queue, GTimeVal *end_time)
394 g_return_val_if_fail (queue, NULL);
395 g_return_val_if_fail (queue->ref_count > 0, NULL);
397 return g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
401 * g_async_queue_length:
402 * @queue: a #GAsyncQueue.
404 * Returns the length of the queue, negative values mean waiting
405 * threads, positive values mean available entries in the
406 * @queue. Actually this function returns the number of data items in
407 * the queue minus the number of waiting threads. Thus a return value
408 * of 0 could mean 'n' entries in the queue and 'n' thread waiting.
409 * That can happen due to locking of the queue or due to
412 * Return value: the length of the @queue.
415 g_async_queue_length (GAsyncQueue* queue)
419 g_return_val_if_fail (queue, 0);
420 g_return_val_if_fail (queue->ref_count > 0, 0);
422 g_mutex_lock (queue->mutex);
423 retval = queue->queue->length - queue->waiting_threads;
424 g_mutex_unlock (queue->mutex);
430 * g_async_queue_length_unlocked:
431 * @queue: a #GAsyncQueue.
433 * Returns the length of the queue, negative values mean waiting
434 * threads, positive values mean available entries in the
435 * @queue. Actually this function returns the number of data items in
436 * the queue minus the number of waiting threads. Thus a return value
437 * of 0 could mean 'n' entries in the queue and 'n' thread waiting.
438 * That can happen due to locking of the queue or due to
439 * scheduling. This function must be called while holding the @queue's
442 * Return value: the length of the @queue.
445 g_async_queue_length_unlocked (GAsyncQueue* queue)
447 g_return_val_if_fail (queue, 0);
448 g_return_val_if_fail (queue->ref_count > 0, 0);
450 return queue->queue->length - queue->waiting_threads;