fixed a typo in a comment.
[glib.git] / gasyncqueue.c
blob126bb1d846a9679ccbd3a0898d6841bb6d01834c
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.
24 * MT safe
27 #include "glib.h"
29 struct _GAsyncQueue
31 GMutex *mutex;
32 GCond *cond;
33 GQueue *queue;
34 guint waiting_threads;
35 guint ref_count;
38 /**
39 * g_async_queue_new:
41 * Creates a new asynchronous queue with the initial reference count of 1.
43 * Return value: the new #GAsyncQueue.
44 **/
45 GAsyncQueue*
46 g_async_queue_new ()
48 GAsyncQueue* retval = g_new (GAsyncQueue, 1);
49 retval->mutex = g_mutex_new ();
50 retval->cond = g_cond_new ();
51 retval->queue = g_queue_new ();
52 retval->waiting_threads = 0;
53 retval->ref_count = 1;
54 return retval;
57 /**
58 * g_async_queue_ref:
59 * @queue: a #GAsyncQueue.
61 * Increases the reference count of the asynchronous @queue by 1.
62 **/
63 void
64 g_async_queue_ref (GAsyncQueue *queue)
66 g_return_if_fail (queue);
67 g_return_if_fail (queue->ref_count > 0);
69 g_mutex_lock (queue->mutex);
70 queue->ref_count++;
71 g_mutex_unlock (queue->mutex);
74 /**
75 * g_async_queue_ref_unlocked:
76 * @queue: a #GAsyncQueue.
78 * Increases the reference count of the asynchronous @queue by 1. This
79 * function must be called while holding the @queue's lock.
80 **/
81 void
82 g_async_queue_ref_unlocked (GAsyncQueue *queue)
84 g_return_if_fail (queue);
85 g_return_if_fail (queue->ref_count > 0);
87 queue->ref_count++;
90 /**
91 * g_async_queue_unref_and_unlock:
92 * @queue: a #GAsyncQueue.
94 * Decreases the reference count of the asynchronous @queue by 1 and
95 * releases the lock. This function must be called while holding the
96 * @queue's lock. If the reference count went to 0, the @queue will be
97 * destroyed and the memory allocated will be freed. So you are not
98 * allowed to use the @queue afterwards, as it might have disappeared.
99 * The obvious asymmetry (it is not named
100 * g_async_queue_unref_unlocked) is because the queue can't be
101 * unlocked after dereffing it, as it might already have disappeared.
103 void
104 g_async_queue_unref_and_unlock (GAsyncQueue *queue)
106 gboolean stop;
108 g_return_if_fail (queue);
109 g_return_if_fail (queue->ref_count > 0);
111 queue->ref_count--;
112 stop = (queue->ref_count == 0);
113 g_mutex_unlock (queue->mutex);
115 if (stop)
117 g_return_if_fail (queue->waiting_threads == 0);
118 g_mutex_free (queue->mutex);
119 g_cond_free (queue->cond);
120 g_queue_free (queue->queue);
121 g_free (queue);
126 * g_async_queue_unref:
127 * @queue: a #GAsyncQueue.
129 * Decreases the reference count of the asynchronous @queue by 1. If
130 * the reference count went to 0, the @queue will be destroyed and the
131 * memory allocated will be freed. So you are not allowed to use the
132 * @queue afterwards, as it might have disappeared.
134 void
135 g_async_queue_unref (GAsyncQueue *queue)
137 g_return_if_fail (queue);
138 g_return_if_fail (queue->ref_count > 0);
140 g_mutex_lock (queue->mutex);
141 g_async_queue_unref_and_unlock (queue);
145 * g_async_queue_lock:
146 * @queue: a #GAsyncQueue.
148 * Acquire the @queue's lock. After that you can only call the
149 * g_async_queue_*_unlocked function variants on that
150 * @queue. Otherwise it will deadlock.
152 void
153 g_async_queue_lock (GAsyncQueue *queue)
155 g_return_if_fail (queue);
156 g_return_if_fail (queue->ref_count > 0);
158 g_mutex_lock (queue->mutex);
162 * g_async_queue_unlock:
163 * @queue: a #GAsyncQueue.
165 * Release the queue's lock.
167 void
168 g_async_queue_unlock (GAsyncQueue *queue)
170 g_return_if_fail (queue);
171 g_return_if_fail (queue->ref_count > 0);
173 g_mutex_unlock (queue->mutex);
177 * g_async_queue_push:
178 * @queue: a #GAsyncQueue.
179 * @data: @data to push into the @queue.
181 * Push the @data into the @queue. @data must not be #NULL.
183 void
184 g_async_queue_push (GAsyncQueue* queue, gpointer data)
186 g_return_if_fail (queue);
187 g_return_if_fail (queue->ref_count > 0);
188 g_return_if_fail (data);
190 g_mutex_lock (queue->mutex);
191 g_async_queue_push_unlocked (queue, data);
192 g_mutex_unlock (queue->mutex);
196 * g_async_queue_push_unlocked:
197 * @queue: a #GAsyncQueue.
198 * @data: @data to push into the @queue.
200 * Push the @data into the @queue. @data must not be #NULL. This
201 * function must be called while holding the @queue's lock.
203 void
204 g_async_queue_push_unlocked (GAsyncQueue* queue, gpointer data)
206 g_return_if_fail (queue);
207 g_return_if_fail (queue->ref_count > 0);
208 g_return_if_fail (data);
210 g_queue_push_head (queue->queue, data);
211 g_cond_signal (queue->cond);
214 static gpointer
215 g_async_queue_pop_intern_unlocked (GAsyncQueue* queue, gboolean try,
216 GTimeVal *end_time)
218 gpointer retval;
220 if (!g_queue_peek_tail (queue->queue))
222 if (try)
223 return NULL;
224 if (!end_time)
226 queue->waiting_threads++;
227 while (!g_queue_peek_tail (queue->queue))
228 g_cond_wait(queue->cond, queue->mutex);
229 queue->waiting_threads--;
231 else
233 queue->waiting_threads++;
234 while (!g_queue_peek_tail (queue->queue))
235 if (!g_cond_timed_wait (queue->cond, queue->mutex, end_time))
236 break;
237 queue->waiting_threads--;
238 if (!g_queue_peek_tail (queue->queue))
239 return NULL;
243 retval = g_queue_pop_tail (queue->queue);
245 g_assert (retval);
247 return retval;
251 * g_async_queue_pop:
252 * @queue: a #GAsyncQueue.
254 * Pop data from the @queue. This function blocks until data become
255 * available.
257 * Return value: data from the queue.
259 gpointer
260 g_async_queue_pop (GAsyncQueue* queue)
262 gpointer retval;
264 g_return_val_if_fail (queue, NULL);
265 g_return_val_if_fail (queue->ref_count > 0, NULL);
267 g_mutex_lock (queue->mutex);
268 retval = g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
269 g_mutex_unlock (queue->mutex);
271 return retval;
275 * g_async_queue_pop_unlocked:
276 * @queue: a #GAsyncQueue.
278 * Pop data from the @queue. This function blocks until data become
279 * available. This function must be called while holding the @queue's
280 * lock.
282 * Return value: data from the queue.
284 gpointer
285 g_async_queue_pop_unlocked (GAsyncQueue* queue)
287 g_return_val_if_fail (queue, NULL);
288 g_return_val_if_fail (queue->ref_count > 0, NULL);
290 return g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
294 * g_async_queue_try_pop:
295 * @queue: a #GAsyncQueue.
297 * Try to pop data from the @queue. If no data is available, #NULL is
298 * returned.
300 * Return value: data from the queue or #NULL, when no data is
301 * available immediately.
303 gpointer
304 g_async_queue_try_pop (GAsyncQueue* queue)
306 gpointer retval;
308 g_return_val_if_fail (queue, NULL);
309 g_return_val_if_fail (queue->ref_count > 0, NULL);
311 g_mutex_lock (queue->mutex);
312 retval = g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
313 g_mutex_unlock (queue->mutex);
315 return retval;
319 * g_async_queue_try_pop_unlocked:
320 * @queue: a #GAsyncQueue.
322 * Try to pop data from the @queue. If no data is available, #NULL is
323 * returned. This function must be called while holding the @queue's
324 * lock.
326 * Return value: data from the queue or #NULL, when no data is
327 * available immediately.
329 gpointer
330 g_async_queue_try_pop_unlocked (GAsyncQueue* queue)
332 g_return_val_if_fail (queue, NULL);
333 g_return_val_if_fail (queue->ref_count > 0, NULL);
335 return g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
339 * g_async_queue_timed_pop:
340 * @queue: a #GAsyncQueue.
341 * @end_time: a #GTimeVal, determining the final time.
343 * Pop data from the @queue. If no data is received before @end_time,
344 * #NULL is returned.
346 * Return value: data from the queue or #NULL, when no data is
347 * received before @end_time.
349 gpointer
350 g_async_queue_timed_pop (GAsyncQueue* queue, GTimeVal *end_time)
352 gpointer retval;
354 g_return_val_if_fail (queue, NULL);
355 g_return_val_if_fail (queue->ref_count > 0, NULL);
357 g_mutex_lock (queue->mutex);
358 retval = g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
359 g_mutex_unlock (queue->mutex);
361 return retval;
365 * g_async_queue_timed_pop_unlocked:
366 * @queue: a #GAsyncQueue.
367 * @end_time: a #GTimeVal, determining the final time.
369 * Pop data from the @queue. If no data is received before @end_time,
370 * #NULL is returned. This function must be called while holding the
371 * @queue's lock.
373 * Return value: data from the queue or #NULL, when no data is
374 * received before @end_time.
376 gpointer
377 g_async_queue_timed_pop_unlocked (GAsyncQueue* queue, GTimeVal *end_time)
379 g_return_val_if_fail (queue, NULL);
380 g_return_val_if_fail (queue->ref_count > 0, NULL);
382 return g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
386 * g_async_queue_length:
387 * @queue: a #GAsyncQueue.
389 * Returns the length of the queue, negative values mean waiting
390 * threads, positive values mean available entries in the
391 * @queue. Actually this function returns the number of data items in
392 * the queue minus the number of waiting threads. Thus a return value
393 * of 0 could mean 'n' entries in the queue and 'n' thread waiting.
394 * That can happen due to locking of the queue or due to
395 * scheduling.
397 * Return value: the length of the @queue.
399 gint
400 g_async_queue_length (GAsyncQueue* queue)
402 glong retval;
404 g_return_val_if_fail (queue, 0);
405 g_return_val_if_fail (queue->ref_count > 0, 0);
407 g_mutex_lock (queue->mutex);
408 retval = queue->queue->length - queue->waiting_threads;
409 g_mutex_unlock (queue->mutex);
411 return retval;
415 * g_async_queue_length_unlocked:
416 * @queue: a #GAsyncQueue.
418 * Returns the length of the queue, negative values mean waiting
419 * threads, positive values mean available entries in the
420 * @queue. Actually this function returns the number of data items in
421 * the queue minus the number of waiting threads. Thus a return value
422 * of 0 could mean 'n' entries in the queue and 'n' thread waiting.
423 * That can happen due to locking of the queue or due to
424 * scheduling. This function must be called while holding the @queue's
425 * lock.
427 * Return value: the length of the @queue.
429 gint
430 g_async_queue_length_unlocked (GAsyncQueue* queue)
432 g_return_val_if_fail (queue, 0);
433 g_return_val_if_fail (queue->ref_count > 0, 0);
435 return queue->queue->length - queue->waiting_threads;