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;
60 volatile gint tail_read;
77 new_queue_mem (guint size, gint pos)
81 mem = g_new (GstAQueueMem, 1);
83 /* we keep the size as a mask for performance */
84 mem->size = clp2 (MAX (size, 16)) - 1;
85 mem->array = g_new0 (gpointer, mem->size + 1);
96 free_queue_mem (GstAQueueMem * mem)
102 struct _GstAtomicQueue
104 volatile gint refcount;
108 GstAQueueMem *head_mem;
109 GstAQueueMem *tail_mem;
110 GstAQueueMem *free_list;
114 add_to_free_list (GstAtomicQueue * queue, GstAQueueMem * mem)
117 mem->free = g_atomic_pointer_get (&queue->free_list);
118 } while (!G_ATOMIC_POINTER_COMPARE_AND_EXCHANGE (&queue->free_list,
123 clear_free_list (GstAtomicQueue * queue)
125 GstAQueueMem *free_list;
127 /* take the free list and replace with NULL */
129 free_list = g_atomic_pointer_get (&queue->free_list);
130 if (free_list == NULL)
132 } while (!G_ATOMIC_POINTER_COMPARE_AND_EXCHANGE (&queue->free_list, free_list,
136 GstAQueueMem *next = free_list->free;
138 free_queue_mem (free_list);
145 * gst_atomic_queue_new:
146 * @initial_size: initial queue size
148 * Create a new atomic queue instance. @initial_size will be rounded up to the
149 * nearest power of 2 and used as the initial size of the queue.
151 * Returns: a new #GstAtomicQueue
156 gst_atomic_queue_new (guint initial_size)
158 GstAtomicQueue *queue;
160 queue = g_new (GstAtomicQueue, 1);
164 queue->num_readers = 0;
166 queue->head_mem = queue->tail_mem = new_queue_mem (initial_size, 0);
167 queue->free_list = NULL;
173 * gst_atomic_queue_ref:
174 * @queue: a #GstAtomicQueue
176 * Increase the refcount of @queue.
181 gst_atomic_queue_ref (GstAtomicQueue * queue)
183 g_return_if_fail (queue != NULL);
185 g_atomic_int_inc (&queue->refcount);
189 gst_atomic_queue_free (GstAtomicQueue * queue)
191 free_queue_mem (queue->head_mem);
192 if (queue->head_mem != queue->tail_mem)
193 free_queue_mem (queue->tail_mem);
194 clear_free_list (queue);
199 * gst_atomic_queue_unref:
200 * @queue: a #GstAtomicQueue
202 * Unref @queue and free the memory when the refcount reaches 0.
207 gst_atomic_queue_unref (GstAtomicQueue * queue)
209 g_return_if_fail (queue != NULL);
211 if (g_atomic_int_dec_and_test (&queue->refcount))
212 gst_atomic_queue_free (queue);
216 * gst_atomic_queue_peek:
217 * @queue: a #GstAtomicQueue
219 * Peek the head element of the queue without removing it from the queue.
221 * Returns: the head element of @queue or NULL when the queue is empty.
226 gst_atomic_queue_peek (GstAtomicQueue * queue)
228 GstAQueueMem *head_mem;
229 gint head, tail, size;
231 g_return_val_if_fail (queue != NULL, NULL);
236 head_mem = g_atomic_pointer_get (&queue->head_mem);
238 head = g_atomic_int_get (&head_mem->head);
239 tail = g_atomic_int_get (&head_mem->tail_read);
240 size = head_mem->size;
242 /* when we are not empty, we can continue */
243 if (G_LIKELY (head != tail))
246 /* else array empty, try to take next */
247 next = g_atomic_pointer_get (&head_mem->next);
251 /* now we try to move the next array as the head memory. If we fail to do that,
252 * some other reader managed to do it first and we retry */
253 if (!G_ATOMIC_POINTER_COMPARE_AND_EXCHANGE (&queue->head_mem, head_mem,
257 /* when we managed to swing the head pointer the old head is now
258 * useless and we add it to the freelist. We can't free the memory yet
259 * because we first need to make sure no reader is accessing it anymore. */
260 add_to_free_list (queue, head_mem);
263 return head_mem->array[head & size];
267 * gst_atomic_queue_pop:
268 * @queue: a #GstAtomicQueue
270 * Get the head element of the queue.
272 * Returns: the head element of @queue or NULL when the queue is empty.
277 gst_atomic_queue_pop (GstAtomicQueue * queue)
280 GstAQueueMem *head_mem;
281 gint head, tail, size;
283 g_return_val_if_fail (queue != NULL, NULL);
286 g_atomic_int_inc (&queue->num_readers);
293 head_mem = g_atomic_pointer_get (&queue->head_mem);
295 head = g_atomic_int_get (&head_mem->head);
296 tail = g_atomic_int_get (&head_mem->tail_read);
297 size = head_mem->size;
299 /* when we are not empty, we can continue */
300 if (G_LIKELY (head != tail))
303 /* else array empty, try to take next */
304 next = g_atomic_pointer_get (&head_mem->next);
308 /* now we try to move the next array as the head memory. If we fail to do that,
309 * some other reader managed to do it first and we retry */
310 if (!G_ATOMIC_POINTER_COMPARE_AND_EXCHANGE (&queue->head_mem, head_mem,
314 /* when we managed to swing the head pointer the old head is now
315 * useless and we add it to the freelist. We can't free the memory yet
316 * because we first need to make sure no reader is accessing it anymore. */
317 add_to_free_list (queue, head_mem);
320 ret = head_mem->array[head & size];
321 } while (!g_atomic_int_compare_and_exchange (&head_mem->head, head,
325 /* decrement number of readers, when we reach 0 readers we can be sure that
326 * none is accessing the memory in the free list and we can try to clean up */
327 if (g_atomic_int_dec_and_test (&queue->num_readers))
328 clear_free_list (queue);
335 * gst_atomic_queue_push:
336 * @queue: a #GstAtomicQueue
339 * Append @data to the tail of the queue.
344 gst_atomic_queue_push (GstAtomicQueue * queue, gpointer data)
346 GstAQueueMem *tail_mem;
347 gint head, tail, size;
349 g_return_if_fail (queue != NULL);
355 tail_mem = g_atomic_pointer_get (&queue->tail_mem);
356 head = g_atomic_int_get (&tail_mem->head);
357 tail = g_atomic_int_get (&tail_mem->tail);
358 size = tail_mem->size;
360 /* we're not full, continue */
361 if (tail - head <= size)
364 /* else we need to grow the array, we store a mask so we have to add 1 */
365 mem = new_queue_mem ((size << 1) + 1, tail);
367 /* try to make our new array visible to other writers */
368 if (!G_ATOMIC_POINTER_COMPARE_AND_EXCHANGE (&queue->tail_mem, tail_mem,
370 /* we tried to swap the new writer array but something changed. This is
371 * because some other writer beat us to it, we free our memory and try
373 free_queue_mem (mem);
376 /* make sure that readers can find our new array as well. The one who
377 * manages to swap the pointer is the only one who can set the next
378 * pointer to the new array */
379 g_atomic_pointer_set (&tail_mem->next, mem);
381 } while (!g_atomic_int_compare_and_exchange (&tail_mem->tail, tail,
384 tail_mem->array[tail & size] = data;
386 /* and now the readers can read */
387 g_atomic_int_inc (&tail_mem->tail_read);
391 * gst_atomic_queue_length:
392 * @queue: a #GstAtomicQueue
394 * Get the amount of items in the queue.
396 * Returns: the number of elements in the queue.
401 gst_atomic_queue_length (GstAtomicQueue * queue)
403 GstAQueueMem *head_mem, *tail_mem;
406 g_return_val_if_fail (queue != NULL, 0);
409 g_atomic_int_inc (&queue->num_readers);
412 head_mem = g_atomic_pointer_get (&queue->head_mem);
413 head = g_atomic_int_get (&head_mem->head);
415 tail_mem = g_atomic_pointer_get (&queue->tail_mem);
416 tail = g_atomic_int_get (&tail_mem->tail_read);
419 if (g_atomic_int_dec_and_test (&queue->num_readers))
420 clear_free_list (queue);