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.
38 guint waiting_threads;
43 GCompareDataFunc func;
50 * Creates a new asynchronous queue with the initial reference count of 1.
52 * Return value: the new #GAsyncQueue.
55 g_async_queue_new (void)
57 GAsyncQueue* retval = g_new (GAsyncQueue, 1);
58 retval->mutex = g_mutex_new ();
60 retval->queue = g_queue_new ();
61 retval->waiting_threads = 0;
62 retval->ref_count = 1;
68 * @queue: a #GAsyncQueue.
70 * Increases the reference count of the asynchronous @queue by 1. You
71 * do not need to hold the lock to call this function.
73 * Returns: the @queue that was passed in (since 2.6)
76 g_async_queue_ref (GAsyncQueue *queue)
78 g_return_val_if_fail (queue, NULL);
79 g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
81 g_atomic_int_inc (&queue->ref_count);
87 * g_async_queue_ref_unlocked:
88 * @queue: a #GAsyncQueue.
90 * Increases the reference count of the asynchronous @queue by 1.
92 * @Deprecated: Since 2.8, reference counting is done atomically
93 * so g_async_queue_ref() can be used regardless of the @queue's
97 g_async_queue_ref_unlocked (GAsyncQueue *queue)
99 g_return_if_fail (queue);
100 g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
102 g_atomic_int_inc (&queue->ref_count);
106 * g_async_queue_unref_and_unlock:
107 * @queue: a #GAsyncQueue.
109 * Decreases the reference count of the asynchronous @queue by 1 and
110 * releases the lock. This function must be called while holding the
111 * @queue's lock. If the reference count went to 0, the @queue will be
112 * destroyed and the memory allocated will be freed.
114 * @Deprecated: Since 2.8, reference counting is done atomically
115 * so g_async_queue_unref() can be used regardless of the @queue's
119 g_async_queue_unref_and_unlock (GAsyncQueue *queue)
121 g_return_if_fail (queue);
122 g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
124 g_mutex_unlock (queue->mutex);
125 g_async_queue_unref (queue);
129 * g_async_queue_unref:
130 * @queue: a #GAsyncQueue.
132 * Decreases the reference count of the asynchronous @queue by 1. If
133 * the reference count went to 0, the @queue will be destroyed and the
134 * memory allocated will be freed. So you are not allowed to use the
135 * @queue afterwards, as it might have disappeared. You do not need to
136 * hold the lock to call this function.
139 g_async_queue_unref (GAsyncQueue *queue)
141 g_return_if_fail (queue);
142 g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
144 if (g_atomic_int_dec_and_test (&queue->ref_count))
146 g_return_if_fail (queue->waiting_threads == 0);
147 g_mutex_free (queue->mutex);
149 g_cond_free (queue->cond);
150 g_queue_free (queue->queue);
156 * g_async_queue_lock:
157 * @queue: a #GAsyncQueue.
159 * Acquires the @queue's lock. After that you can only call the
160 * <function>g_async_queue_*_unlocked()</function> function variants on that
161 * @queue. Otherwise it will deadlock.
164 g_async_queue_lock (GAsyncQueue *queue)
166 g_return_if_fail (queue);
167 g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
169 g_mutex_lock (queue->mutex);
173 * g_async_queue_unlock:
174 * @queue: a #GAsyncQueue.
176 * Releases the queue's lock.
179 g_async_queue_unlock (GAsyncQueue *queue)
181 g_return_if_fail (queue);
182 g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
184 g_mutex_unlock (queue->mutex);
188 * g_async_queue_push:
189 * @queue: a #GAsyncQueue.
190 * @data: @data to push into the @queue.
192 * Pushes the @data into the @queue. @data must not be %NULL.
195 g_async_queue_push (GAsyncQueue* queue, gpointer data)
197 g_return_if_fail (queue);
198 g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
199 g_return_if_fail (data);
201 g_mutex_lock (queue->mutex);
202 g_async_queue_push_unlocked (queue, data);
203 g_mutex_unlock (queue->mutex);
207 * g_async_queue_push_unlocked:
208 * @queue: a #GAsyncQueue.
209 * @data: @data to push into the @queue.
211 * Pushes the @data into the @queue. @data must not be %NULL. This
212 * function must be called while holding the @queue's lock.
215 g_async_queue_push_unlocked (GAsyncQueue* queue, gpointer data)
217 g_return_if_fail (queue);
218 g_return_if_fail (g_atomic_int_get (&queue->ref_count) > 0);
219 g_return_if_fail (data);
221 g_queue_push_head (queue->queue, data);
222 if (queue->waiting_threads > 0)
223 g_cond_signal (queue->cond);
227 * g_async_queue_push_sorted:
228 * @queue: a #GAsyncQueue
229 * @data: the @data to push into the @queue
230 * @func: the #GCompareDataFunc is used to sort @queue. This function
231 * is passed two elements of the @queue. The function should return
232 * 0 if they are equal, a negative value if the first element
233 * should be higher in the @queue or a positive value if the first
234 * element should be lower in the @queue than the second element.
235 * @user_data: user data passed to @func.
237 * Inserts @data into @queue using @func to determine the new
240 * This function requires that the @queue is sorted before pushing on
243 * This function will lock @queue before it sorts the queue and unlock
244 * it when it is finished.
246 * For an example of @func see g_async_queue_sort().
251 g_async_queue_push_sorted (GAsyncQueue *queue,
253 GCompareDataFunc func,
256 g_return_if_fail (queue != NULL);
258 g_mutex_lock (queue->mutex);
259 g_async_queue_push_sorted_unlocked (queue, data, func, user_data);
260 g_mutex_unlock (queue->mutex);
264 g_async_queue_invert_compare (gpointer v1,
268 return -sd->func (v1, v2, sd->user_data);
272 * g_async_queue_push_sorted_unlocked:
273 * @queue: a #GAsyncQueue
274 * @data: the @data to push into the @queue
275 * @func: the #GCompareDataFunc is used to sort @queue. This function
276 * is passed two elements of the @queue. The function should return
277 * 0 if they are equal, a negative value if the first element
278 * should be higher in the @queue or a positive value if the first
279 * element should be lower in the @queue than the second element.
280 * @user_data: user data passed to @func.
282 * Inserts @data into @queue using @func to determine the new
285 * This function requires that the @queue is sorted before pushing on
288 * This function is called while holding the @queue's lock.
290 * For an example of @func see g_async_queue_sort().
295 g_async_queue_push_sorted_unlocked (GAsyncQueue *queue,
297 GCompareDataFunc func,
302 g_return_if_fail (queue != NULL);
305 sd.user_data = user_data;
307 g_queue_insert_sorted (queue->queue,
309 (GCompareDataFunc)g_async_queue_invert_compare,
311 if (queue->waiting_threads > 0)
312 g_cond_signal (queue->cond);
316 g_async_queue_pop_intern_unlocked (GAsyncQueue* queue, gboolean try,
321 if (!g_queue_peek_tail (queue->queue))
327 queue->cond = g_cond_new ();
331 queue->waiting_threads++;
332 while (!g_queue_peek_tail (queue->queue))
333 g_cond_wait(queue->cond, queue->mutex);
334 queue->waiting_threads--;
338 queue->waiting_threads++;
339 while (!g_queue_peek_tail (queue->queue))
340 if (!g_cond_timed_wait (queue->cond, queue->mutex, end_time))
342 queue->waiting_threads--;
343 if (!g_queue_peek_tail (queue->queue))
348 retval = g_queue_pop_tail (queue->queue);
357 * @queue: a #GAsyncQueue.
359 * Pops data from the @queue. This function blocks until data become
362 * Return value: data from the queue.
365 g_async_queue_pop (GAsyncQueue* queue)
369 g_return_val_if_fail (queue, NULL);
370 g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
372 g_mutex_lock (queue->mutex);
373 retval = g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
374 g_mutex_unlock (queue->mutex);
380 * g_async_queue_pop_unlocked:
381 * @queue: a #GAsyncQueue.
383 * Pops data from the @queue. This function blocks until data become
384 * available. This function must be called while holding the @queue's
387 * Return value: data from the queue.
390 g_async_queue_pop_unlocked (GAsyncQueue* queue)
392 g_return_val_if_fail (queue, NULL);
393 g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
395 return g_async_queue_pop_intern_unlocked (queue, FALSE, NULL);
399 * g_async_queue_try_pop:
400 * @queue: a #GAsyncQueue.
402 * Tries to pop data from the @queue. If no data is available, %NULL is
405 * Return value: data from the queue or %NULL, when no data is
406 * available immediately.
409 g_async_queue_try_pop (GAsyncQueue* queue)
413 g_return_val_if_fail (queue, NULL);
414 g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
416 g_mutex_lock (queue->mutex);
417 retval = g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
418 g_mutex_unlock (queue->mutex);
424 * g_async_queue_try_pop_unlocked:
425 * @queue: a #GAsyncQueue.
427 * Tries to pop data from the @queue. If no data is available, %NULL is
428 * returned. This function must be called while holding the @queue's
431 * Return value: data from the queue or %NULL, when no data is
432 * available immediately.
435 g_async_queue_try_pop_unlocked (GAsyncQueue* queue)
437 g_return_val_if_fail (queue, NULL);
438 g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
440 return g_async_queue_pop_intern_unlocked (queue, TRUE, NULL);
444 * g_async_queue_timed_pop:
445 * @queue: a #GAsyncQueue.
446 * @end_time: a #GTimeVal, determining the final time.
448 * Pops data from the @queue. If no data is received before @end_time,
451 * To easily calculate @end_time a combination of g_get_current_time()
452 * and g_time_val_add() can be used.
454 * Return value: data from the queue or %NULL, when no data is
455 * received before @end_time.
458 g_async_queue_timed_pop (GAsyncQueue* queue, GTimeVal *end_time)
462 g_return_val_if_fail (queue, NULL);
463 g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
465 g_mutex_lock (queue->mutex);
466 retval = g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
467 g_mutex_unlock (queue->mutex);
473 * g_async_queue_timed_pop_unlocked:
474 * @queue: a #GAsyncQueue.
475 * @end_time: a #GTimeVal, determining the final time.
477 * Pops data from the @queue. If no data is received before @end_time,
478 * %NULL is returned. This function must be called while holding the
481 * To easily calculate @end_time a combination of g_get_current_time()
482 * and g_time_val_add() can be used.
484 * Return value: data from the queue or %NULL, when no data is
485 * received before @end_time.
488 g_async_queue_timed_pop_unlocked (GAsyncQueue* queue, GTimeVal *end_time)
490 g_return_val_if_fail (queue, NULL);
491 g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, NULL);
493 return g_async_queue_pop_intern_unlocked (queue, FALSE, end_time);
497 * g_async_queue_length:
498 * @queue: a #GAsyncQueue.
500 * Returns the length of the queue, negative values mean waiting
501 * threads, positive values mean available entries in the
502 * @queue. Actually this function returns the number of data items in
503 * the queue minus the number of waiting threads. Thus a return value
504 * of 0 could mean 'n' entries in the queue and 'n' thread waiting.
505 * That can happen due to locking of the queue or due to
508 * Return value: the length of the @queue.
511 g_async_queue_length (GAsyncQueue* queue)
515 g_return_val_if_fail (queue, 0);
516 g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, 0);
518 g_mutex_lock (queue->mutex);
519 retval = queue->queue->length - queue->waiting_threads;
520 g_mutex_unlock (queue->mutex);
526 * g_async_queue_length_unlocked:
527 * @queue: a #GAsyncQueue.
529 * Returns the length of the queue, negative values mean waiting
530 * threads, positive values mean available entries in the
531 * @queue. Actually this function returns the number of data items in
532 * the queue minus the number of waiting threads. Thus a return value
533 * of 0 could mean 'n' entries in the queue and 'n' thread waiting.
534 * That can happen due to locking of the queue or due to
535 * scheduling. This function must be called while holding the @queue's
538 * Return value: the length of the @queue.
541 g_async_queue_length_unlocked (GAsyncQueue* queue)
543 g_return_val_if_fail (queue, 0);
544 g_return_val_if_fail (g_atomic_int_get (&queue->ref_count) > 0, 0);
546 return queue->queue->length - queue->waiting_threads;
550 * g_async_queue_sort:
551 * @queue: a #GAsyncQueue
552 * @func: the #GCompareDataFunc is used to sort @queue. This
553 * function is passed two elements of the @queue. The function
554 * should return 0 if they are equal, a negative value if the
555 * first element should be higher in the @queue or a positive
556 * value if the first element should be lower in the @queue than
557 * the second element.
558 * @user_data: user data passed to @func
560 * Sorts @queue using @func.
562 * This function will lock @queue before it sorts the queue and unlock
563 * it when it is finished.
565 * If you were sorting a list of priority numbers to make sure the
566 * lowest priority would be at the top of the queue, you could use:
567 * <informalexample><programlisting>
571 * id1 = GPOINTER_TO_INT (element1);
572 * id2 = GPOINTER_TO_INT (element2);
574 * return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
575 * </programlisting></informalexample>
580 g_async_queue_sort (GAsyncQueue *queue,
581 GCompareDataFunc func,
584 g_return_if_fail (queue != NULL);
585 g_return_if_fail (func != NULL);
587 g_mutex_lock (queue->mutex);
588 g_async_queue_sort_unlocked (queue, func, user_data);
589 g_mutex_unlock (queue->mutex);
593 * g_async_queue_sort_unlocked:
594 * @queue: a #GAsyncQueue
595 * @func: the #GCompareDataFunc is used to sort @queue. This
596 * function is passed two elements of the @queue. The function
597 * should return 0 if they are equal, a negative value if the
598 * first element should be higher in the @queue or a positive
599 * value if the first element should be lower in the @queue than
600 * the second element.
601 * @user_data: user data passed to @func
603 * Sorts @queue using @func.
605 * This function is called while holding the @queue's lock.
610 g_async_queue_sort_unlocked (GAsyncQueue *queue,
611 GCompareDataFunc func,
616 g_return_if_fail (queue != NULL);
617 g_return_if_fail (func != NULL);
620 sd.user_data = user_data;
622 g_queue_sort (queue->queue,
623 (GCompareDataFunc)g_async_queue_invert_compare,
627 #define __G_ASYNCQUEUE_C__
628 #include "galiasdef.c"