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.
176 gst_atomic_queue_ref (GstAtomicQueue * queue)
178 g_return_if_fail (queue != NULL);
180 g_atomic_int_inc (&queue->refcount);
184 gst_atomic_queue_free (GstAtomicQueue * queue)
186 free_queue_mem (queue->head_mem);
187 if (queue->head_mem != queue->tail_mem)
188 free_queue_mem (queue->tail_mem);
189 clear_free_list (queue);
194 * gst_atomic_queue_unref:
195 * @queue: a #GstAtomicQueue
197 * Unref @queue and free the memory when the refcount reaches 0.
200 gst_atomic_queue_unref (GstAtomicQueue * queue)
202 g_return_if_fail (queue != NULL);
204 if (g_atomic_int_dec_and_test (&queue->refcount))
205 gst_atomic_queue_free (queue);
209 * gst_atomic_queue_peek:
210 * @queue: a #GstAtomicQueue
212 * Peek the head element of the queue without removing it from the queue.
214 * Returns: the head element of @queue or NULL when the queue is empty.
217 gst_atomic_queue_peek (GstAtomicQueue * queue)
219 GstAQueueMem *head_mem;
220 gint head, tail, size;
222 g_return_val_if_fail (queue != NULL, NULL);
227 head_mem = g_atomic_pointer_get (&queue->head_mem);
229 head = g_atomic_int_get (&head_mem->head);
230 tail = g_atomic_int_get (&head_mem->tail);
231 size = head_mem->size;
233 /* when we are not empty, we can continue */
234 if (G_LIKELY (head != tail))
237 /* else array empty, try to take next */
238 next = g_atomic_pointer_get (&head_mem->next);
242 /* now we try to move the next array as the head memory. If we fail to do that,
243 * some other reader managed to do it first and we retry */
244 if (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
245 queue->head_mem, head_mem, next))
248 /* when we managed to swing the head pointer the old head is now
249 * useless and we add it to the freelist. We can't free the memory yet
250 * because we first need to make sure no reader is accessing it anymore. */
251 add_to_free_list (queue, head_mem);
254 return head_mem->array[head & size];
258 * gst_atomic_queue_pop:
259 * @queue: a #GstAtomicQueue
261 * Get the head element of the queue.
263 * Returns: the head element of @queue or NULL when the queue is empty.
266 gst_atomic_queue_pop (GstAtomicQueue * queue)
269 GstAQueueMem *head_mem;
270 gint head, tail, size;
272 g_return_val_if_fail (queue != NULL, NULL);
275 g_atomic_int_inc (&queue->num_readers);
282 head_mem = g_atomic_pointer_get (&queue->head_mem);
284 head = g_atomic_int_get (&head_mem->head);
285 tail = g_atomic_int_get (&head_mem->tail);
286 size = head_mem->size;
288 /* when we are not empty, we can continue */
289 if (G_LIKELY (head != tail))
292 /* else array empty, try to take next */
293 next = g_atomic_pointer_get (&head_mem->next);
297 /* now we try to move the next array as the head memory. If we fail to do that,
298 * some other reader managed to do it first and we retry */
299 if (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
300 queue->head_mem, head_mem, next))
303 /* when we managed to swing the head pointer the old head is now
304 * useless and we add it to the freelist. We can't free the memory yet
305 * because we first need to make sure no reader is accessing it anymore. */
306 add_to_free_list (queue, head_mem);
309 ret = head_mem->array[head & size];
310 } while (!g_atomic_int_compare_and_exchange (&head_mem->head, head,
314 /* decrement number of readers, when we reach 0 readers we can be sure that
315 * none is accessing the memory in the free list and we can try to clean up */
316 if (g_atomic_int_dec_and_test (&queue->num_readers))
317 clear_free_list (queue);
324 * gst_atomic_queue_push:
325 * @queue: a #GstAtomicQueue
328 * Append @data to the tail of the queue.
331 gst_atomic_queue_push (GstAtomicQueue * queue, gpointer data)
333 GstAQueueMem *tail_mem;
334 gint head, tail, size;
336 g_return_if_fail (queue != NULL);
342 tail_mem = g_atomic_pointer_get (&queue->tail_mem);
343 head = g_atomic_int_get (&tail_mem->head);
344 tail = g_atomic_int_get (&tail_mem->tail);
345 size = tail_mem->size;
347 /* we're not full, continue */
348 if (tail - head <= size)
351 /* else we need to grow the array, we store a mask so we have to add 1 */
352 mem = new_queue_mem ((size << 1) + 1, tail);
354 /* try to make our new array visible to other writers */
355 if (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
356 queue->tail_mem, tail_mem, mem)) {
357 /* we tried to swap the new writer array but something changed. This is
358 * because some other writer beat us to it, we free our memory and try
360 free_queue_mem (mem);
363 /* make sure that readers can find our new array as well. The one who
364 * manages to swap the pointer is the only one who can set the next
365 * pointer to the new array */
366 g_atomic_pointer_set (&tail_mem->next, mem);
368 } while (!g_atomic_int_compare_and_exchange (&tail_mem->tail, tail,
371 tail_mem->array[tail & size] = data;
375 * gst_atomic_queue_length:
376 * @queue: a #GstAtomicQueue
378 * Get the amount of items in the queue.
380 * Returns: the number of elements in the queue.
383 gst_atomic_queue_length (GstAtomicQueue * queue)
385 GstAQueueMem *head_mem, *tail_mem;
388 g_return_val_if_fail (queue != NULL, 0);
391 g_atomic_int_inc (&queue->num_readers);
394 head_mem = g_atomic_pointer_get (&queue->head_mem);
395 head = g_atomic_int_get (&head_mem->head);
397 tail_mem = g_atomic_pointer_get (&queue->tail_mem);
398 tail = g_atomic_int_get (&tail_mem->tail);
401 if (g_atomic_int_dec_and_test (&queue->num_readers))
402 clear_free_list (queue);