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 G_DEFINE_BOXED_TYPE (GstAtomicQueue, gst_atomic_queue,
43 (GBoxedCopyFunc) gst_atomic_queue_ref,
44 (GBoxedFreeFunc) gst_atomic_queue_unref);
46 /* By default the queue uses 2 * sizeof(gpointer) * clp2 (max_items) of
47 * memory. clp2(x) is the next power of two >= than x.
49 * The queue can operate in low memory mode, in which it consumes almost
50 * half the memory at the expense of extra overhead in the readers. This
51 * is disabled by default because even without LOW_MEM mode, the memory
52 * consumption is still lower than a plain GList.
56 typedef struct _GstAQueueMem GstAQueueMem;
63 volatile gint tail_write;
64 volatile gint tail_read;
81 new_queue_mem (guint size, gint pos)
85 mem = g_new (GstAQueueMem, 1);
87 /* we keep the size as a mask for performance */
88 mem->size = clp2 (MAX (size, 16)) - 1;
89 mem->array = g_new0 (gpointer, mem->size + 1);
91 mem->tail_write = pos;
100 free_queue_mem (GstAQueueMem * mem)
106 struct _GstAtomicQueue
108 volatile gint refcount;
112 GstAQueueMem *head_mem;
113 GstAQueueMem *tail_mem;
114 GstAQueueMem *free_list;
118 add_to_free_list (GstAtomicQueue * queue, GstAQueueMem * mem)
121 mem->free = g_atomic_pointer_get (&queue->free_list);
122 } while (!g_atomic_pointer_compare_and_exchange (&queue->free_list,
127 clear_free_list (GstAtomicQueue * queue)
129 GstAQueueMem *free_list;
131 /* take the free list and replace with NULL */
133 free_list = g_atomic_pointer_get (&queue->free_list);
134 if (free_list == NULL)
136 } while (!g_atomic_pointer_compare_and_exchange (&queue->free_list, free_list,
140 GstAQueueMem *next = free_list->free;
142 free_queue_mem (free_list);
149 * gst_atomic_queue_new:
150 * @initial_size: initial queue size
152 * Create a new atomic queue instance. @initial_size will be rounded up to the
153 * nearest power of 2 and used as the initial size of the queue.
155 * Returns: a new #GstAtomicQueue
160 gst_atomic_queue_new (guint initial_size)
162 GstAtomicQueue *queue;
164 queue = g_new (GstAtomicQueue, 1);
168 queue->num_readers = 0;
170 queue->head_mem = queue->tail_mem = new_queue_mem (initial_size, 0);
171 queue->free_list = NULL;
177 * gst_atomic_queue_ref:
178 * @queue: a #GstAtomicQueue
180 * Increase the refcount of @queue.
185 gst_atomic_queue_ref (GstAtomicQueue * queue)
187 g_return_if_fail (queue != NULL);
189 g_atomic_int_inc (&queue->refcount);
193 gst_atomic_queue_free (GstAtomicQueue * queue)
195 free_queue_mem (queue->head_mem);
196 if (queue->head_mem != queue->tail_mem)
197 free_queue_mem (queue->tail_mem);
198 clear_free_list (queue);
203 * gst_atomic_queue_unref:
204 * @queue: a #GstAtomicQueue
206 * Unref @queue and free the memory when the refcount reaches 0.
211 gst_atomic_queue_unref (GstAtomicQueue * queue)
213 g_return_if_fail (queue != NULL);
215 if (g_atomic_int_dec_and_test (&queue->refcount))
216 gst_atomic_queue_free (queue);
220 * gst_atomic_queue_peek:
221 * @queue: a #GstAtomicQueue
223 * Peek the head element of the queue without removing it from the queue.
225 * Returns: (transfer none): the head element of @queue or NULL when
226 * the queue is empty.
231 gst_atomic_queue_peek (GstAtomicQueue * queue)
233 GstAQueueMem *head_mem;
234 gint head, tail, size;
236 g_return_val_if_fail (queue != NULL, NULL);
241 head_mem = g_atomic_pointer_get (&queue->head_mem);
243 head = g_atomic_int_get (&head_mem->head);
244 tail = g_atomic_int_get (&head_mem->tail_read);
245 size = head_mem->size;
247 /* when we are not empty, we can continue */
248 if (G_LIKELY (head != tail))
251 /* else array empty, try to take next */
252 next = g_atomic_pointer_get (&head_mem->next);
256 /* now we try to move the next array as the head memory. If we fail to do that,
257 * some other reader managed to do it first and we retry */
258 if (!g_atomic_pointer_compare_and_exchange (&queue->head_mem, head_mem,
262 /* when we managed to swing the head pointer the old head is now
263 * useless and we add it to the freelist. We can't free the memory yet
264 * because we first need to make sure no reader is accessing it anymore. */
265 add_to_free_list (queue, head_mem);
268 return head_mem->array[head & size];
272 * gst_atomic_queue_pop:
273 * @queue: a #GstAtomicQueue
275 * Get the head element of the queue.
277 * Returns: (transfer full): the head element of @queue or NULL when
278 * the queue is empty.
283 gst_atomic_queue_pop (GstAtomicQueue * queue)
286 GstAQueueMem *head_mem;
287 gint head, tail, size;
289 g_return_val_if_fail (queue != NULL, NULL);
292 g_atomic_int_inc (&queue->num_readers);
299 head_mem = g_atomic_pointer_get (&queue->head_mem);
301 head = g_atomic_int_get (&head_mem->head);
302 tail = g_atomic_int_get (&head_mem->tail_read);
303 size = head_mem->size;
305 /* when we are not empty, we can continue */
310 /* else array empty, try to take next */
311 next = g_atomic_pointer_get (&head_mem->next);
315 /* now we try to move the next array as the head memory. If we fail to do that,
316 * some other reader managed to do it first and we retry */
318 (!g_atomic_pointer_compare_and_exchange (&queue->head_mem, head_mem,
322 /* when we managed to swing the head pointer the old head is now
323 * useless and we add it to the freelist. We can't free the memory yet
324 * because we first need to make sure no reader is accessing it anymore. */
325 add_to_free_list (queue, head_mem);
328 ret = head_mem->array[head & size];
330 (!g_atomic_int_compare_and_exchange (&head_mem->head, head, head + 1));
333 /* decrement number of readers, when we reach 0 readers we can be sure that
334 * none is accessing the memory in the free list and we can try to clean up */
335 if (g_atomic_int_dec_and_test (&queue->num_readers))
336 clear_free_list (queue);
343 * gst_atomic_queue_push:
344 * @queue: a #GstAtomicQueue
347 * Append @data to the tail of the queue.
352 gst_atomic_queue_push (GstAtomicQueue * queue, gpointer data)
354 GstAQueueMem *tail_mem;
355 gint head, tail, size;
357 g_return_if_fail (queue != NULL);
363 tail_mem = g_atomic_pointer_get (&queue->tail_mem);
364 head = g_atomic_int_get (&tail_mem->head);
365 tail = g_atomic_int_get (&tail_mem->tail_write);
366 size = tail_mem->size;
368 /* we're not full, continue */
370 (tail - head <= size)
373 /* else we need to grow the array, we store a mask so we have to add 1 */
374 mem = new_queue_mem ((size << 1) + 1, tail);
376 /* try to make our new array visible to other writers */
378 (!g_atomic_pointer_compare_and_exchange (&queue->tail_mem, tail_mem,
380 /* we tried to swap the new writer array but something changed. This is
381 * because some other writer beat us to it, we free our memory and try
383 free_queue_mem (mem);
386 /* make sure that readers can find our new array as well. The one who
387 * manages to swap the pointer is the only one who can set the next
388 * pointer to the new array */
389 g_atomic_pointer_set (&tail_mem->next, mem);
392 (!g_atomic_int_compare_and_exchange (&tail_mem->tail_write, tail, tail + 1));
394 tail_mem->array[tail & size] = data;
396 /* now wait until all writers have completed their write before we move the
397 * tail_read to this new item. It is possible that other writers are still
398 * updating the previous array slots and we don't want to reveal their changes
399 * before they are done. FIXME, it would be nice if we didn't have to busy
402 (!g_atomic_int_compare_and_exchange (&tail_mem->tail_read, tail, tail + 1));
406 * gst_atomic_queue_length:
407 * @queue: a #GstAtomicQueue
409 * Get the amount of items in the queue.
411 * Returns: the number of elements in the queue.
416 gst_atomic_queue_length (GstAtomicQueue * queue)
418 GstAQueueMem *head_mem, *tail_mem;
421 g_return_val_if_fail (queue != NULL, 0);
424 g_atomic_int_inc (&queue->num_readers);
427 head_mem = g_atomic_pointer_get (&queue->head_mem);
428 head = g_atomic_int_get (&head_mem->head);
430 tail_mem = g_atomic_pointer_get (&queue->tail_mem);
431 tail = g_atomic_int_get (&tail_mem->tail_read);
434 if (g_atomic_int_dec_and_test (&queue->num_readers))
435 clear_free_list (queue);