2 * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
3 * 2011 Wim Taymans <wim.taymans@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library 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.
23 #include "gst_private.h"
28 #include "gstatomicqueue.h"
31 * SECTION:gstatomicqueue
32 * @title: GstAtomicQueue
33 * @short_description: An atomic queue implementation
35 * The #GstAtomicQueue object implements a queue that can be used from multiple
36 * threads without performing any blocking operations.
41 /* By default the queue uses 2 * sizeof(gpointer) * clp2 (max_items) of
42 * memory. clp2(x) is the next power of two >= than x.
44 * The queue can operate in low memory mode, in which it consumes almost
45 * half the memory at the expense of extra overhead in the readers. This
46 * is disabled by default because even without LOW_MEM mode, the memory
47 * consumption is still lower than a plain GList.
51 typedef struct _GstAQueueMem GstAQueueMem;
75 new_queue_mem (guint size, gint pos)
79 mem = g_new (GstAQueueMem, 1);
81 /* we keep the size as a mask for performance */
82 mem->size = clp2 (MAX (size, 16)) - 1;
83 mem->array = g_new0 (gpointer, mem->size + 1);
93 free_queue_mem (GstAQueueMem * mem)
99 struct _GstAtomicQueue
101 volatile gint refcount;
105 GstAQueueMem *head_mem;
106 GstAQueueMem *tail_mem;
107 GstAQueueMem *free_list;
111 add_to_free_list (GstAtomicQueue * queue, GstAQueueMem * mem)
114 mem->free = g_atomic_pointer_get (&queue->free_list);
115 } while (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
116 queue->free_list, mem->free, mem));
120 clear_free_list (GstAtomicQueue * queue)
122 GstAQueueMem *free_list;
124 /* take the free list and replace with NULL */
126 free_list = g_atomic_pointer_get (&queue->free_list);
127 if (free_list == NULL)
129 } while (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
130 queue->free_list, free_list, NULL));
133 GstAQueueMem *next = free_list->free;
135 free_queue_mem (free_list);
142 * gst_atomic_queue_new:
143 * @initial_size: initial queue size
145 * Create a new atomic queue instance. @initial_size will be rounded up to the
146 * nearest power of 2 and used as the initial size of the queue.
148 * Returns: a new #GstAtomicQueue
153 gst_atomic_queue_new (guint initial_size)
155 GstAtomicQueue *queue;
157 queue = g_new (GstAtomicQueue, 1);
161 queue->num_readers = 0;
163 queue->head_mem = queue->tail_mem = new_queue_mem (initial_size, 0);
164 queue->free_list = NULL;
170 * gst_atomic_queue_ref:
171 * @queue: a #GstAtomicQueue
173 * Increase the refcount of @queue.
178 gst_atomic_queue_ref (GstAtomicQueue * queue)
180 g_return_if_fail (queue != NULL);
182 g_atomic_int_inc (&queue->refcount);
186 gst_atomic_queue_free (GstAtomicQueue * queue)
188 free_queue_mem (queue->head_mem);
189 if (queue->head_mem != queue->tail_mem)
190 free_queue_mem (queue->tail_mem);
191 clear_free_list (queue);
196 * gst_atomic_queue_unref:
197 * @queue: a #GstAtomicQueue
199 * Unref @queue and free the memory when the refcount reaches 0.
204 gst_atomic_queue_unref (GstAtomicQueue * queue)
206 g_return_if_fail (queue != NULL);
208 if (g_atomic_int_dec_and_test (&queue->refcount))
209 gst_atomic_queue_free (queue);
213 * gst_atomic_queue_peek:
214 * @queue: a #GstAtomicQueue
216 * Peek the head element of the queue without removing it from the queue.
218 * Returns: the head element of @queue or NULL when the queue is empty.
223 gst_atomic_queue_peek (GstAtomicQueue * queue)
225 GstAQueueMem *head_mem;
226 gint head, tail, size;
228 g_return_val_if_fail (queue != NULL, NULL);
233 head_mem = g_atomic_pointer_get (&queue->head_mem);
235 head = g_atomic_int_get (&head_mem->head);
236 tail = g_atomic_int_get (&head_mem->tail);
237 size = head_mem->size;
239 /* when we are not empty, we can continue */
240 if (G_LIKELY (head != tail))
243 /* else array empty, try to take next */
244 next = g_atomic_pointer_get (&head_mem->next);
248 /* now we try to move the next array as the head memory. If we fail to do that,
249 * some other reader managed to do it first and we retry */
250 if (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
251 queue->head_mem, head_mem, next))
254 /* when we managed to swing the head pointer the old head is now
255 * useless and we add it to the freelist. We can't free the memory yet
256 * because we first need to make sure no reader is accessing it anymore. */
257 add_to_free_list (queue, head_mem);
260 return head_mem->array[head & size];
264 * gst_atomic_queue_pop:
265 * @queue: a #GstAtomicQueue
267 * Get the head element of the queue.
269 * Returns: the head element of @queue or NULL when the queue is empty.
274 gst_atomic_queue_pop (GstAtomicQueue * queue)
277 GstAQueueMem *head_mem;
278 gint head, tail, size;
280 g_return_val_if_fail (queue != NULL, NULL);
283 g_atomic_int_inc (&queue->num_readers);
290 head_mem = g_atomic_pointer_get (&queue->head_mem);
292 head = g_atomic_int_get (&head_mem->head);
293 tail = g_atomic_int_get (&head_mem->tail);
294 size = head_mem->size;
296 /* when we are not empty, we can continue */
297 if (G_LIKELY (head != tail))
300 /* else array empty, try to take next */
301 next = g_atomic_pointer_get (&head_mem->next);
305 /* now we try to move the next array as the head memory. If we fail to do that,
306 * some other reader managed to do it first and we retry */
307 if (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
308 queue->head_mem, head_mem, next))
311 /* when we managed to swing the head pointer the old head is now
312 * useless and we add it to the freelist. We can't free the memory yet
313 * because we first need to make sure no reader is accessing it anymore. */
314 add_to_free_list (queue, head_mem);
317 ret = head_mem->array[head & size];
318 } while (!g_atomic_int_compare_and_exchange (&head_mem->head, head,
322 /* decrement number of readers, when we reach 0 readers we can be sure that
323 * none is accessing the memory in the free list and we can try to clean up */
324 if (g_atomic_int_dec_and_test (&queue->num_readers))
325 clear_free_list (queue);
332 * gst_atomic_queue_push:
333 * @queue: a #GstAtomicQueue
336 * Append @data to the tail of the queue.
341 gst_atomic_queue_push (GstAtomicQueue * queue, gpointer data)
343 GstAQueueMem *tail_mem;
344 gint head, tail, size;
346 g_return_if_fail (queue != NULL);
352 tail_mem = g_atomic_pointer_get (&queue->tail_mem);
353 head = g_atomic_int_get (&tail_mem->head);
354 tail = g_atomic_int_get (&tail_mem->tail);
355 size = tail_mem->size;
357 /* we're not full, continue */
358 if (tail - head <= size)
361 /* else we need to grow the array, we store a mask so we have to add 1 */
362 mem = new_queue_mem ((size << 1) + 1, tail);
364 /* try to make our new array visible to other writers */
365 if (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
366 queue->tail_mem, tail_mem, mem)) {
367 /* we tried to swap the new writer array but something changed. This is
368 * because some other writer beat us to it, we free our memory and try
370 free_queue_mem (mem);
373 /* make sure that readers can find our new array as well. The one who
374 * manages to swap the pointer is the only one who can set the next
375 * pointer to the new array */
376 g_atomic_pointer_set (&tail_mem->next, mem);
378 } while (!g_atomic_int_compare_and_exchange (&tail_mem->tail, tail,
381 tail_mem->array[tail & size] = data;
385 * gst_atomic_queue_length:
386 * @queue: a #GstAtomicQueue
388 * Get the amount of items in the queue.
390 * Returns: the number of elements in the queue.
395 gst_atomic_queue_length (GstAtomicQueue * queue)
397 GstAQueueMem *head_mem, *tail_mem;
400 g_return_val_if_fail (queue != NULL, 0);
403 g_atomic_int_inc (&queue->num_readers);
406 head_mem = g_atomic_pointer_get (&queue->head_mem);
407 head = g_atomic_int_get (&head_mem->head);
409 tail_mem = g_atomic_pointer_get (&queue->tail_mem);
410 tail = g_atomic_int_get (&tail_mem->tail);
413 if (g_atomic_int_dec_and_test (&queue->num_readers))
414 clear_free_list (queue);