2010-10-28 15:02:39 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
|
|
|
|
* 2011 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
*
|
|
|
|
* gstatomicqueue.c:
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
#include "gst_private.h"
|
|
|
|
|
2011-02-16 18:54:57 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-10-28 15:02:39 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
#include "gstatomicqueue.h"
|
2011-06-04 07:30:15 +00:00
|
|
|
#include "glib-compat-private.h"
|
2010-10-28 15:02:39 +00:00
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstatomicqueue
|
|
|
|
* @title: GstAtomicQueue
|
|
|
|
* @short_description: An atomic queue implementation
|
|
|
|
*
|
|
|
|
* The #GstAtomicQueue object implements a queue that can be used from multiple
|
|
|
|
* threads without performing any blocking operations.
|
|
|
|
*
|
|
|
|
* Since: 0.10.33
|
|
|
|
*/
|
|
|
|
|
2010-10-28 15:02:39 +00:00
|
|
|
/* By default the queue uses 2 * sizeof(gpointer) * clp2 (max_items) of
|
|
|
|
* memory. clp2(x) is the next power of two >= than x.
|
|
|
|
*
|
|
|
|
* The queue can operate in low memory mode, in which it consumes almost
|
|
|
|
* half the memory at the expense of extra overhead in the readers. This
|
|
|
|
* is disabled by default because even without LOW_MEM mode, the memory
|
|
|
|
* consumption is still lower than a plain GList.
|
|
|
|
*/
|
|
|
|
#undef LOW_MEM
|
|
|
|
|
|
|
|
typedef struct _GstAQueueMem GstAQueueMem;
|
|
|
|
|
|
|
|
struct _GstAQueueMem
|
|
|
|
{
|
|
|
|
gint size;
|
|
|
|
gpointer *array;
|
|
|
|
volatile gint head;
|
2012-02-24 14:24:42 +00:00
|
|
|
volatile gint tail_write;
|
2012-02-24 11:51:24 +00:00
|
|
|
volatile gint tail_read;
|
2010-10-28 15:02:39 +00:00
|
|
|
GstAQueueMem *next;
|
|
|
|
GstAQueueMem *free;
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint
|
|
|
|
clp2 (guint n)
|
|
|
|
{
|
|
|
|
guint res = 1;
|
|
|
|
|
|
|
|
while (res < n)
|
|
|
|
res <<= 1;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstAQueueMem *
|
|
|
|
new_queue_mem (guint size, gint pos)
|
|
|
|
{
|
|
|
|
GstAQueueMem *mem;
|
|
|
|
|
|
|
|
mem = g_new (GstAQueueMem, 1);
|
|
|
|
|
|
|
|
/* we keep the size as a mask for performance */
|
2011-02-16 11:48:59 +00:00
|
|
|
mem->size = clp2 (MAX (size, 16)) - 1;
|
2011-02-16 16:14:11 +00:00
|
|
|
mem->array = g_new0 (gpointer, mem->size + 1);
|
2010-10-28 15:02:39 +00:00
|
|
|
mem->head = pos;
|
2012-02-24 14:24:42 +00:00
|
|
|
mem->tail_write = pos;
|
2012-02-24 11:51:24 +00:00
|
|
|
mem->tail_read = pos;
|
2010-10-28 15:02:39 +00:00
|
|
|
mem->next = NULL;
|
|
|
|
mem->free = NULL;
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_queue_mem (GstAQueueMem * mem)
|
|
|
|
{
|
|
|
|
g_free (mem->array);
|
|
|
|
g_free (mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct _GstAtomicQueue
|
|
|
|
{
|
2011-02-16 15:19:46 +00:00
|
|
|
volatile gint refcount;
|
2010-10-28 15:02:39 +00:00
|
|
|
#ifdef LOW_MEM
|
|
|
|
gint num_readers;
|
|
|
|
#endif
|
|
|
|
GstAQueueMem *head_mem;
|
|
|
|
GstAQueueMem *tail_mem;
|
|
|
|
GstAQueueMem *free_list;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_to_free_list (GstAtomicQueue * queue, GstAQueueMem * mem)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
mem->free = g_atomic_pointer_get (&queue->free_list);
|
2012-01-22 01:25:22 +00:00
|
|
|
} while (!g_atomic_pointer_compare_and_exchange (&queue->free_list,
|
2011-06-04 07:30:15 +00:00
|
|
|
mem->free, mem));
|
2010-10-28 15:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_free_list (GstAtomicQueue * queue)
|
|
|
|
{
|
|
|
|
GstAQueueMem *free_list;
|
|
|
|
|
|
|
|
/* take the free list and replace with NULL */
|
|
|
|
do {
|
|
|
|
free_list = g_atomic_pointer_get (&queue->free_list);
|
|
|
|
if (free_list == NULL)
|
|
|
|
return;
|
2012-01-22 01:25:22 +00:00
|
|
|
} while (!g_atomic_pointer_compare_and_exchange (&queue->free_list, free_list,
|
2011-06-04 07:30:15 +00:00
|
|
|
NULL));
|
2010-10-28 15:02:39 +00:00
|
|
|
|
|
|
|
while (free_list) {
|
|
|
|
GstAQueueMem *next = free_list->free;
|
|
|
|
|
|
|
|
free_queue_mem (free_list);
|
|
|
|
|
|
|
|
free_list = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
/**
|
|
|
|
* gst_atomic_queue_new:
|
|
|
|
* @initial_size: initial queue size
|
|
|
|
*
|
|
|
|
* Create a new atomic queue instance. @initial_size will be rounded up to the
|
|
|
|
* nearest power of 2 and used as the initial size of the queue.
|
|
|
|
*
|
|
|
|
* Returns: a new #GstAtomicQueue
|
|
|
|
*
|
|
|
|
* Since: 0.10.33
|
|
|
|
*/
|
2010-10-28 15:02:39 +00:00
|
|
|
GstAtomicQueue *
|
|
|
|
gst_atomic_queue_new (guint initial_size)
|
|
|
|
{
|
|
|
|
GstAtomicQueue *queue;
|
|
|
|
|
|
|
|
queue = g_new (GstAtomicQueue, 1);
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
queue->refcount = 1;
|
2010-10-28 15:02:39 +00:00
|
|
|
#ifdef LOW_MEM
|
|
|
|
queue->num_readers = 0;
|
|
|
|
#endif
|
|
|
|
queue->head_mem = queue->tail_mem = new_queue_mem (initial_size, 0);
|
|
|
|
queue->free_list = NULL;
|
|
|
|
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
/**
|
|
|
|
* gst_atomic_queue_ref:
|
|
|
|
* @queue: a #GstAtomicQueue
|
|
|
|
*
|
|
|
|
* Increase the refcount of @queue.
|
2011-02-20 16:11:27 +00:00
|
|
|
*
|
|
|
|
* Since: 0.10.33
|
2011-02-16 15:19:46 +00:00
|
|
|
*/
|
2010-10-28 15:02:39 +00:00
|
|
|
void
|
2011-02-16 15:19:46 +00:00
|
|
|
gst_atomic_queue_ref (GstAtomicQueue * queue)
|
|
|
|
{
|
|
|
|
g_return_if_fail (queue != NULL);
|
|
|
|
|
|
|
|
g_atomic_int_inc (&queue->refcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-10-28 15:02:39 +00:00
|
|
|
gst_atomic_queue_free (GstAtomicQueue * queue)
|
|
|
|
{
|
|
|
|
free_queue_mem (queue->head_mem);
|
|
|
|
if (queue->head_mem != queue->tail_mem)
|
|
|
|
free_queue_mem (queue->tail_mem);
|
|
|
|
clear_free_list (queue);
|
|
|
|
g_free (queue);
|
|
|
|
}
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
/**
|
|
|
|
* gst_atomic_queue_unref:
|
|
|
|
* @queue: a #GstAtomicQueue
|
|
|
|
*
|
|
|
|
* Unref @queue and free the memory when the refcount reaches 0.
|
2011-02-20 16:11:27 +00:00
|
|
|
*
|
|
|
|
* Since: 0.10.33
|
2011-02-16 15:19:46 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_atomic_queue_unref (GstAtomicQueue * queue)
|
|
|
|
{
|
|
|
|
g_return_if_fail (queue != NULL);
|
|
|
|
|
|
|
|
if (g_atomic_int_dec_and_test (&queue->refcount))
|
|
|
|
gst_atomic_queue_free (queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_atomic_queue_peek:
|
|
|
|
* @queue: a #GstAtomicQueue
|
|
|
|
*
|
|
|
|
* Peek the head element of the queue without removing it from the queue.
|
|
|
|
*
|
|
|
|
* Returns: the head element of @queue or NULL when the queue is empty.
|
2011-02-20 16:11:27 +00:00
|
|
|
*
|
|
|
|
* Since: 0.10.33
|
2011-02-16 15:19:46 +00:00
|
|
|
*/
|
2010-10-28 15:02:39 +00:00
|
|
|
gpointer
|
|
|
|
gst_atomic_queue_peek (GstAtomicQueue * queue)
|
|
|
|
{
|
|
|
|
GstAQueueMem *head_mem;
|
|
|
|
gint head, tail, size;
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
g_return_val_if_fail (queue != NULL, NULL);
|
|
|
|
|
2010-10-28 15:02:39 +00:00
|
|
|
while (TRUE) {
|
|
|
|
GstAQueueMem *next;
|
|
|
|
|
|
|
|
head_mem = g_atomic_pointer_get (&queue->head_mem);
|
|
|
|
|
|
|
|
head = g_atomic_int_get (&head_mem->head);
|
2012-02-24 11:51:24 +00:00
|
|
|
tail = g_atomic_int_get (&head_mem->tail_read);
|
2010-10-28 15:02:39 +00:00
|
|
|
size = head_mem->size;
|
|
|
|
|
|
|
|
/* when we are not empty, we can continue */
|
|
|
|
if (G_LIKELY (head != tail))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* else array empty, try to take next */
|
|
|
|
next = g_atomic_pointer_get (&head_mem->next);
|
|
|
|
if (next == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* now we try to move the next array as the head memory. If we fail to do that,
|
|
|
|
* some other reader managed to do it first and we retry */
|
2012-01-22 01:25:22 +00:00
|
|
|
if (!g_atomic_pointer_compare_and_exchange (&queue->head_mem, head_mem,
|
2011-06-04 07:30:15 +00:00
|
|
|
next))
|
2010-10-28 15:02:39 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* when we managed to swing the head pointer the old head is now
|
|
|
|
* useless and we add it to the freelist. We can't free the memory yet
|
|
|
|
* because we first need to make sure no reader is accessing it anymore. */
|
|
|
|
add_to_free_list (queue, head_mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return head_mem->array[head & size];
|
|
|
|
}
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
/**
|
|
|
|
* gst_atomic_queue_pop:
|
|
|
|
* @queue: a #GstAtomicQueue
|
|
|
|
*
|
|
|
|
* Get the head element of the queue.
|
|
|
|
*
|
|
|
|
* Returns: the head element of @queue or NULL when the queue is empty.
|
2011-02-20 16:11:27 +00:00
|
|
|
*
|
|
|
|
* Since: 0.10.33
|
2011-02-16 15:19:46 +00:00
|
|
|
*/
|
2010-10-28 15:02:39 +00:00
|
|
|
gpointer
|
|
|
|
gst_atomic_queue_pop (GstAtomicQueue * queue)
|
|
|
|
{
|
|
|
|
gpointer ret;
|
|
|
|
GstAQueueMem *head_mem;
|
|
|
|
gint head, tail, size;
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
g_return_val_if_fail (queue != NULL, NULL);
|
|
|
|
|
2010-10-28 15:02:39 +00:00
|
|
|
#ifdef LOW_MEM
|
|
|
|
g_atomic_int_inc (&queue->num_readers);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
do {
|
|
|
|
while (TRUE) {
|
|
|
|
GstAQueueMem *next;
|
|
|
|
|
|
|
|
head_mem = g_atomic_pointer_get (&queue->head_mem);
|
|
|
|
|
|
|
|
head = g_atomic_int_get (&head_mem->head);
|
2012-02-24 11:51:24 +00:00
|
|
|
tail = g_atomic_int_get (&head_mem->tail_read);
|
2010-10-28 15:02:39 +00:00
|
|
|
size = head_mem->size;
|
|
|
|
|
|
|
|
/* when we are not empty, we can continue */
|
2012-02-24 14:24:42 +00:00
|
|
|
if G_LIKELY
|
|
|
|
(head != tail)
|
|
|
|
break;
|
2010-10-28 15:02:39 +00:00
|
|
|
|
|
|
|
/* else array empty, try to take next */
|
|
|
|
next = g_atomic_pointer_get (&head_mem->next);
|
|
|
|
if (next == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* now we try to move the next array as the head memory. If we fail to do that,
|
|
|
|
* some other reader managed to do it first and we retry */
|
2012-02-24 14:24:42 +00:00
|
|
|
if G_UNLIKELY
|
|
|
|
(!g_atomic_pointer_compare_and_exchange (&queue->head_mem, head_mem,
|
|
|
|
next))
|
|
|
|
continue;
|
2010-10-28 15:02:39 +00:00
|
|
|
|
|
|
|
/* when we managed to swing the head pointer the old head is now
|
|
|
|
* useless and we add it to the freelist. We can't free the memory yet
|
|
|
|
* because we first need to make sure no reader is accessing it anymore. */
|
|
|
|
add_to_free_list (queue, head_mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = head_mem->array[head & size];
|
2012-02-24 14:24:42 +00:00
|
|
|
} while G_UNLIKELY
|
|
|
|
(!g_atomic_int_compare_and_exchange (&head_mem->head, head, head + 1));
|
2010-10-28 15:02:39 +00:00
|
|
|
|
|
|
|
#ifdef LOW_MEM
|
|
|
|
/* decrement number of readers, when we reach 0 readers we can be sure that
|
|
|
|
* none is accessing the memory in the free list and we can try to clean up */
|
|
|
|
if (g_atomic_int_dec_and_test (&queue->num_readers))
|
|
|
|
clear_free_list (queue);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
/**
|
|
|
|
* gst_atomic_queue_push:
|
|
|
|
* @queue: a #GstAtomicQueue
|
|
|
|
* @data: the data
|
|
|
|
*
|
|
|
|
* Append @data to the tail of the queue.
|
2011-02-20 16:11:27 +00:00
|
|
|
*
|
|
|
|
* Since: 0.10.33
|
2011-02-16 15:19:46 +00:00
|
|
|
*/
|
2010-10-28 15:02:39 +00:00
|
|
|
void
|
|
|
|
gst_atomic_queue_push (GstAtomicQueue * queue, gpointer data)
|
|
|
|
{
|
|
|
|
GstAQueueMem *tail_mem;
|
|
|
|
gint head, tail, size;
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
g_return_if_fail (queue != NULL);
|
|
|
|
|
2010-10-28 15:02:39 +00:00
|
|
|
do {
|
|
|
|
while (TRUE) {
|
|
|
|
GstAQueueMem *mem;
|
|
|
|
|
|
|
|
tail_mem = g_atomic_pointer_get (&queue->tail_mem);
|
|
|
|
head = g_atomic_int_get (&tail_mem->head);
|
2012-02-24 14:24:42 +00:00
|
|
|
tail = g_atomic_int_get (&tail_mem->tail_write);
|
2010-10-28 15:02:39 +00:00
|
|
|
size = tail_mem->size;
|
|
|
|
|
|
|
|
/* we're not full, continue */
|
2012-02-24 14:24:42 +00:00
|
|
|
if G_LIKELY
|
|
|
|
(tail - head <= size)
|
|
|
|
break;
|
2010-10-28 15:02:39 +00:00
|
|
|
|
|
|
|
/* else we need to grow the array, we store a mask so we have to add 1 */
|
|
|
|
mem = new_queue_mem ((size << 1) + 1, tail);
|
|
|
|
|
|
|
|
/* try to make our new array visible to other writers */
|
2012-02-24 14:24:42 +00:00
|
|
|
if G_UNLIKELY
|
|
|
|
(!g_atomic_pointer_compare_and_exchange (&queue->tail_mem, tail_mem,
|
|
|
|
mem)) {
|
2010-10-28 15:02:39 +00:00
|
|
|
/* we tried to swap the new writer array but something changed. This is
|
|
|
|
* because some other writer beat us to it, we free our memory and try
|
|
|
|
* again */
|
|
|
|
free_queue_mem (mem);
|
|
|
|
continue;
|
2012-02-24 14:24:42 +00:00
|
|
|
}
|
2010-10-28 15:02:39 +00:00
|
|
|
/* make sure that readers can find our new array as well. The one who
|
|
|
|
* manages to swap the pointer is the only one who can set the next
|
|
|
|
* pointer to the new array */
|
|
|
|
g_atomic_pointer_set (&tail_mem->next, mem);
|
|
|
|
}
|
2012-02-24 14:24:42 +00:00
|
|
|
} while G_UNLIKELY
|
|
|
|
(!g_atomic_int_compare_and_exchange (&tail_mem->tail_write, tail, tail + 1));
|
2010-10-28 15:02:39 +00:00
|
|
|
|
|
|
|
tail_mem->array[tail & size] = data;
|
2012-02-24 11:51:24 +00:00
|
|
|
|
2012-02-24 14:24:42 +00:00
|
|
|
/* now wait until all writers have completed their write before we move the
|
|
|
|
* tail_read to this new item. It is possible that other writers are still
|
|
|
|
* updating the previous array slots and we don't want to reveal their changes
|
|
|
|
* before they are done. FIXME, it would be nice if we didn't have to busy
|
|
|
|
* wait here. */
|
|
|
|
while G_UNLIKELY
|
|
|
|
(!g_atomic_int_compare_and_exchange (&tail_mem->tail_read, tail, tail + 1));
|
2010-10-28 15:02:39 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
/**
|
|
|
|
* gst_atomic_queue_length:
|
|
|
|
* @queue: a #GstAtomicQueue
|
|
|
|
*
|
|
|
|
* Get the amount of items in the queue.
|
|
|
|
*
|
|
|
|
* Returns: the number of elements in the queue.
|
2011-02-20 16:11:27 +00:00
|
|
|
*
|
|
|
|
* Since: 0.10.33
|
2011-02-16 15:19:46 +00:00
|
|
|
*/
|
2010-10-28 15:02:39 +00:00
|
|
|
guint
|
|
|
|
gst_atomic_queue_length (GstAtomicQueue * queue)
|
|
|
|
{
|
|
|
|
GstAQueueMem *head_mem, *tail_mem;
|
|
|
|
gint head, tail;
|
|
|
|
|
2011-02-16 15:19:46 +00:00
|
|
|
g_return_val_if_fail (queue != NULL, 0);
|
|
|
|
|
2010-10-28 15:02:39 +00:00
|
|
|
#ifdef LOW_MEM
|
|
|
|
g_atomic_int_inc (&queue->num_readers);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
head_mem = g_atomic_pointer_get (&queue->head_mem);
|
|
|
|
head = g_atomic_int_get (&head_mem->head);
|
|
|
|
|
|
|
|
tail_mem = g_atomic_pointer_get (&queue->tail_mem);
|
2012-02-24 11:51:24 +00:00
|
|
|
tail = g_atomic_int_get (&tail_mem->tail_read);
|
2010-10-28 15:02:39 +00:00
|
|
|
|
|
|
|
#ifdef LOW_MEM
|
|
|
|
if (g_atomic_int_dec_and_test (&queue->num_readers))
|
|
|
|
clear_free_list (queue);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return tail - head;
|
|
|
|
}
|