Blame gst/gstatomicqueue.c

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