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"
29 #include "glib-compat-private.h"
32 * SECTION:gstatomicqueue
33 * @title: GstAtomicQueue
34 * @short_description: An atomic queue implementation
36 * The #GstAtomicQueue object implements a queue that can be used from multiple
37 * threads without performing any blocking operations.
42 /* By default the queue uses 2 * sizeof(gpointer) * clp2 (max_items) of
43 * memory. clp2(x) is the next power of two >= than x.
45 * The queue can operate in low memory mode, in which it consumes almost
46 * half the memory at the expense of extra overhead in the readers. This
47 * is disabled by default because even without LOW_MEM mode, the memory
48 * consumption is still lower than a plain GList.
52 typedef struct _GstAQueueMem GstAQueueMem;
76 new_queue_mem (guint size, gint pos)
80 mem = g_new (GstAQueueMem, 1);
82 /* we keep the size as a mask for performance */
83 mem->size = clp2 (MAX (size, 16)) - 1;
84 mem->array = g_new0 (gpointer, mem->size + 1);
94 free_queue_mem (GstAQueueMem * mem)
100 struct _GstAtomicQueue
102 volatile gint refcount;
106 GstAQueueMem *head_mem;
107 GstAQueueMem *tail_mem;
108 GstAQueueMem *free_list;
112 add_to_free_list (GstAtomicQueue * queue, GstAQueueMem * mem)
115 mem->free = g_atomic_pointer_get (&queue->free_list);
116 } while (!g_atomic_pointer_compare_and_exchange (&queue->free_list,
121 clear_free_list (GstAtomicQueue * queue)
123 GstAQueueMem *free_list;
125 /* take the free list and replace with NULL */
127 free_list = g_atomic_pointer_get (&queue->free_list);
128 if (free_list == NULL)
130 } while (!g_atomic_pointer_compare_and_exchange (&queue->free_list, free_list,
134 GstAQueueMem *next = free_list->free;
136 free_queue_mem (free_list);
143 * gst_atomic_queue_new:
144 * @initial_size: initial queue size
146 * Create a new atomic queue instance. @initial_size will be rounded up to the
147 * nearest power of 2 and used as the initial size of the queue.
149 * Returns: a new #GstAtomicQueue
154 gst_atomic_queue_new (guint initial_size)
156 GstAtomicQueue *queue;
158 queue = g_new (GstAtomicQueue, 1);
162 queue->num_readers = 0;
164 queue->head_mem = queue->tail_mem = new_queue_mem (initial_size, 0);
165 queue->free_list = NULL;
171 * gst_atomic_queue_ref:
172 * @queue: a #GstAtomicQueue
174 * Increase the refcount of @queue.
179 gst_atomic_queue_ref (GstAtomicQueue * queue)
181 g_return_if_fail (queue != NULL);
183 g_atomic_int_inc (&queue->refcount);
187 gst_atomic_queue_free (GstAtomicQueue * queue)
189 free_queue_mem (queue->head_mem);
190 if (queue->head_mem != queue->tail_mem)
191 free_queue_mem (queue->tail_mem);
192 clear_free_list (queue);
197 * gst_atomic_queue_unref:
198 * @queue: a #GstAtomicQueue
200 * Unref @queue and free the memory when the refcount reaches 0.
205 gst_atomic_queue_unref (GstAtomicQueue * queue)
207 g_return_if_fail (queue != NULL);
209 if (g_atomic_int_dec_and_test (&queue->refcount))
210 gst_atomic_queue_free (queue);
214 * gst_atomic_queue_peek:
215 * @queue: a #GstAtomicQueue
217 * Peek the head element of the queue without removing it from the queue.
219 * Returns: the head element of @queue or NULL when the queue is empty.
224 gst_atomic_queue_peek (GstAtomicQueue * queue)
226 GstAQueueMem *head_mem;
227 gint head, tail, size;
229 g_return_val_if_fail (queue != NULL, NULL);
234 head_mem = g_atomic_pointer_get (&queue->head_mem);
236 head = g_atomic_int_get (&head_mem->head);
237 tail = g_atomic_int_get (&head_mem->tail);
238 size = head_mem->size;
240 /* when we are not empty, we can continue */
241 if (G_LIKELY (head != tail))
244 /* else array empty, try to take next */
245 next = g_atomic_pointer_get (&head_mem->next);
249 /* now we try to move the next array as the head memory. If we fail to do that,
250 * some other reader managed to do it first and we retry */
251 if (!g_atomic_pointer_compare_and_exchange (&queue->head_mem, head_mem,
255 /* when we managed to swing the head pointer the old head is now
256 * useless and we add it to the freelist. We can't free the memory yet
257 * because we first need to make sure no reader is accessing it anymore. */
258 add_to_free_list (queue, head_mem);
261 return head_mem->array[head & size];
265 * gst_atomic_queue_pop:
266 * @queue: a #GstAtomicQueue
268 * Get the head element of the queue.
270 * Returns: the head element of @queue or NULL when the queue is empty.
275 gst_atomic_queue_pop (GstAtomicQueue * queue)
278 GstAQueueMem *head_mem;
279 gint head, tail, size;
281 g_return_val_if_fail (queue != NULL, NULL);
284 g_atomic_int_inc (&queue->num_readers);
291 head_mem = g_atomic_pointer_get (&queue->head_mem);
293 head = g_atomic_int_get (&head_mem->head);
294 tail = g_atomic_int_get (&head_mem->tail);
295 size = head_mem->size;
297 /* when we are not empty, we can continue */
298 if (G_LIKELY (head != tail))
301 /* else array empty, try to take next */
302 next = g_atomic_pointer_get (&head_mem->next);
306 /* now we try to move the next array as the head memory. If we fail to do that,
307 * some other reader managed to do it first and we retry */
308 if (!g_atomic_pointer_compare_and_exchange (&queue->head_mem, head_mem,
312 /* when we managed to swing the head pointer the old head is now
313 * useless and we add it to the freelist. We can't free the memory yet
314 * because we first need to make sure no reader is accessing it anymore. */
315 add_to_free_list (queue, head_mem);
318 ret = head_mem->array[head & size];
319 } while (!g_atomic_int_compare_and_exchange (&head_mem->head, head,
323 /* decrement number of readers, when we reach 0 readers we can be sure that
324 * none is accessing the memory in the free list and we can try to clean up */
325 if (g_atomic_int_dec_and_test (&queue->num_readers))
326 clear_free_list (queue);
333 * gst_atomic_queue_push:
334 * @queue: a #GstAtomicQueue
337 * Append @data to the tail of the queue.
342 gst_atomic_queue_push (GstAtomicQueue * queue, gpointer data)
344 GstAQueueMem *tail_mem;
345 gint head, tail, size;
347 g_return_if_fail (queue != NULL);
353 tail_mem = g_atomic_pointer_get (&queue->tail_mem);
354 head = g_atomic_int_get (&tail_mem->head);
355 tail = g_atomic_int_get (&tail_mem->tail);
356 size = tail_mem->size;
358 /* we're not full, continue */
359 if (tail - head <= size)
362 /* else we need to grow the array, we store a mask so we have to add 1 */
363 mem = new_queue_mem ((size << 1) + 1, tail);
365 /* try to make our new array visible to other writers */
366 if (!g_atomic_pointer_compare_and_exchange (&queue->tail_mem, tail_mem,
368 /* we tried to swap the new writer array but something changed. This is
369 * because some other writer beat us to it, we free our memory and try
371 free_queue_mem (mem);
374 /* make sure that readers can find our new array as well. The one who
375 * manages to swap the pointer is the only one who can set the next
376 * pointer to the new array */
377 g_atomic_pointer_set (&tail_mem->next, mem);
379 } while (!g_atomic_int_compare_and_exchange (&tail_mem->tail, tail,
382 tail_mem->array[tail & size] = data;
386 * gst_atomic_queue_length:
387 * @queue: a #GstAtomicQueue
389 * Get the amount of items in the queue.
391 * Returns: the number of elements in the queue.
396 gst_atomic_queue_length (GstAtomicQueue * queue)
398 GstAQueueMem *head_mem, *tail_mem;
401 g_return_val_if_fail (queue != NULL, 0);
404 g_atomic_int_inc (&queue->num_readers);
407 head_mem = g_atomic_pointer_get (&queue->head_mem);
408 head = g_atomic_int_get (&head_mem->head);
410 tail_mem = g_atomic_pointer_get (&queue->tail_mem);
411 tail = g_atomic_int_get (&tail_mem->tail);
414 if (g_atomic_int_dec_and_test (&queue->num_readers))
415 clear_free_list (queue);