Blame gst/gsttaskpool.c

Packit f546b1
/* GStreamer
Packit f546b1
 * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
Packit f546b1
 *
Packit f546b1
 * gsttaskpool.c: Pool for streaming threads
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
/**
Packit f546b1
 * SECTION:gsttaskpool
Packit f546b1
 * @title: GstTaskPool
Packit f546b1
 * @short_description: Pool of GStreamer streaming threads
Packit f546b1
 * @see_also: #GstTask, #GstPad
Packit f546b1
 *
Packit f546b1
 * This object provides an abstraction for creating threads. The default
Packit f546b1
 * implementation uses a regular GThreadPool to start tasks.
Packit f546b1
 *
Packit f546b1
 * Subclasses can be made to create custom threads.
Packit f546b1
 */
Packit f546b1
Packit f546b1
#include "gst_private.h"
Packit f546b1
Packit f546b1
#include "gstinfo.h"
Packit f546b1
#include "gsttaskpool.h"
Packit f546b1
Packit f546b1
GST_DEBUG_CATEGORY_STATIC (taskpool_debug);
Packit f546b1
#define GST_CAT_DEFAULT (taskpool_debug)
Packit f546b1
Packit f546b1
#ifndef GST_DISABLE_GST_DEBUG
Packit f546b1
static void gst_task_pool_finalize (GObject * object);
Packit f546b1
#endif
Packit f546b1
Packit f546b1
#define _do_init \
Packit f546b1
{ \
Packit f546b1
  GST_DEBUG_CATEGORY_INIT (taskpool_debug, "taskpool", 0, "Thread pool"); \
Packit f546b1
}
Packit f546b1
Packit f546b1
G_DEFINE_TYPE_WITH_CODE (GstTaskPool, gst_task_pool, GST_TYPE_OBJECT, _do_init);
Packit f546b1
Packit f546b1
typedef struct
Packit f546b1
{
Packit f546b1
  GstTaskPoolFunction func;
Packit f546b1
  gpointer user_data;
Packit f546b1
} TaskData;
Packit f546b1
Packit f546b1
static void
Packit f546b1
default_func (TaskData * tdata, GstTaskPool * pool)
Packit f546b1
{
Packit f546b1
  GstTaskPoolFunction func;
Packit f546b1
  gpointer user_data;
Packit f546b1
Packit f546b1
  func = tdata->func;
Packit f546b1
  user_data = tdata->user_data;
Packit f546b1
  g_slice_free (TaskData, tdata);
Packit f546b1
Packit f546b1
  func (user_data);
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
default_prepare (GstTaskPool * pool, GError ** error)
Packit f546b1
{
Packit f546b1
  GST_OBJECT_LOCK (pool);
Packit f546b1
  pool->pool = g_thread_pool_new ((GFunc) default_func, pool, -1, FALSE, NULL);
Packit f546b1
  GST_OBJECT_UNLOCK (pool);
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
default_cleanup (GstTaskPool * pool)
Packit f546b1
{
Packit f546b1
  GST_OBJECT_LOCK (pool);
Packit f546b1
  if (pool->pool) {
Packit f546b1
    /* Shut down all the threads, we still process the ones scheduled
Packit f546b1
     * because the unref happens in the thread function.
Packit f546b1
     * Also wait for currently running ones to finish. */
Packit f546b1
    g_thread_pool_free (pool->pool, FALSE, TRUE);
Packit f546b1
    pool->pool = NULL;
Packit f546b1
  }
Packit f546b1
  GST_OBJECT_UNLOCK (pool);
Packit f546b1
}
Packit f546b1
Packit f546b1
static gpointer
Packit f546b1
default_push (GstTaskPool * pool, GstTaskPoolFunction func,
Packit f546b1
    gpointer user_data, GError ** error)
Packit f546b1
{
Packit f546b1
  TaskData *tdata;
Packit f546b1
Packit f546b1
  tdata = g_slice_new (TaskData);
Packit f546b1
  tdata->func = func;
Packit f546b1
  tdata->user_data = user_data;
Packit f546b1
Packit f546b1
  GST_OBJECT_LOCK (pool);
Packit f546b1
  if (pool->pool)
Packit f546b1
    g_thread_pool_push (pool->pool, tdata, error);
Packit f546b1
  else {
Packit f546b1
    g_slice_free (TaskData, tdata);
Packit f546b1
  }
Packit f546b1
  GST_OBJECT_UNLOCK (pool);
Packit f546b1
Packit f546b1
  return NULL;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
default_join (GstTaskPool * pool, gpointer id)
Packit f546b1
{
Packit f546b1
  /* we do nothing here, we can't join from the pools */
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_task_pool_class_init (GstTaskPoolClass * klass)
Packit f546b1
{
Packit f546b1
  GObjectClass *gobject_class;
Packit f546b1
  GstTaskPoolClass *gsttaskpool_class;
Packit f546b1
Packit f546b1
  gobject_class = (GObjectClass *) klass;
Packit f546b1
  gsttaskpool_class = (GstTaskPoolClass *) klass;
Packit f546b1
Packit f546b1
#ifndef GST_DISABLE_GST_DEBUG
Packit f546b1
  gobject_class->finalize = gst_task_pool_finalize;
Packit f546b1
#endif
Packit f546b1
Packit f546b1
  gsttaskpool_class->prepare = default_prepare;
Packit f546b1
  gsttaskpool_class->cleanup = default_cleanup;
Packit f546b1
  gsttaskpool_class->push = default_push;
Packit f546b1
  gsttaskpool_class->join = default_join;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_task_pool_init (GstTaskPool * pool)
Packit f546b1
{
Packit f546b1
}
Packit f546b1
Packit f546b1
#ifndef GST_DISABLE_GST_DEBUG
Packit f546b1
static void
Packit f546b1
gst_task_pool_finalize (GObject * object)
Packit f546b1
{
Packit f546b1
  GST_DEBUG ("taskpool %p finalize", object);
Packit f546b1
Packit f546b1
  G_OBJECT_CLASS (gst_task_pool_parent_class)->finalize (object);
Packit f546b1
}
Packit f546b1
#endif
Packit f546b1
/**
Packit f546b1
 * gst_task_pool_new:
Packit f546b1
 *
Packit f546b1
 * Create a new default task pool. The default task pool will use a regular
Packit f546b1
 * GThreadPool for threads.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer full): a new #GstTaskPool. gst_object_unref() after usage.
Packit f546b1
 */
Packit f546b1
GstTaskPool *
Packit f546b1
gst_task_pool_new (void)
Packit f546b1
{
Packit f546b1
  GstTaskPool *pool;
Packit f546b1
Packit f546b1
  pool = g_object_new (GST_TYPE_TASK_POOL, NULL);
Packit f546b1
Packit f546b1
  /* clear floating flag */
Packit f546b1
  gst_object_ref_sink (pool);
Packit f546b1
Packit f546b1
  return pool;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_task_pool_prepare:
Packit f546b1
 * @pool: a #GstTaskPool
Packit f546b1
 * @error: an error return location
Packit f546b1
 *
Packit f546b1
 * Prepare the taskpool for accepting gst_task_pool_push() operations.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_task_pool_prepare (GstTaskPool * pool, GError ** error)
Packit f546b1
{
Packit f546b1
  GstTaskPoolClass *klass;
Packit f546b1
Packit f546b1
  g_return_if_fail (GST_IS_TASK_POOL (pool));
Packit f546b1
Packit f546b1
  klass = GST_TASK_POOL_GET_CLASS (pool);
Packit f546b1
Packit f546b1
  if (klass->prepare)
Packit f546b1
    klass->prepare (pool, error);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_task_pool_cleanup:
Packit f546b1
 * @pool: a #GstTaskPool
Packit f546b1
 *
Packit f546b1
 * Wait for all tasks to be stopped. This is mainly used internally
Packit f546b1
 * to ensure proper cleanup of internal data structures in test suites.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_task_pool_cleanup (GstTaskPool * pool)
Packit f546b1
{
Packit f546b1
  GstTaskPoolClass *klass;
Packit f546b1
Packit f546b1
  g_return_if_fail (GST_IS_TASK_POOL (pool));
Packit f546b1
Packit f546b1
  klass = GST_TASK_POOL_GET_CLASS (pool);
Packit f546b1
Packit f546b1
  if (klass->cleanup)
Packit f546b1
    klass->cleanup (pool);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_task_pool_push:
Packit f546b1
 * @pool: a #GstTaskPool
Packit f546b1
 * @func: (scope async): the function to call
Packit f546b1
 * @user_data: (closure): data to pass to @func
Packit f546b1
 * @error: return location for an error
Packit f546b1
 *
Packit f546b1
 * Start the execution of a new thread from @pool.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer none) (nullable): a pointer that should be used
Packit f546b1
 * for the gst_task_pool_join function. This pointer can be %NULL, you
Packit f546b1
 * must check @error to detect errors.
Packit f546b1
 */
Packit f546b1
gpointer
Packit f546b1
gst_task_pool_push (GstTaskPool * pool, GstTaskPoolFunction func,
Packit f546b1
    gpointer user_data, GError ** error)
Packit f546b1
{
Packit f546b1
  GstTaskPoolClass *klass;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (GST_IS_TASK_POOL (pool), NULL);
Packit f546b1
Packit f546b1
  klass = GST_TASK_POOL_GET_CLASS (pool);
Packit f546b1
Packit f546b1
  if (klass->push == NULL)
Packit f546b1
    goto not_supported;
Packit f546b1
Packit f546b1
  return klass->push (pool, func, user_data, error);
Packit f546b1
Packit f546b1
  /* ERRORS */
Packit f546b1
not_supported:
Packit f546b1
  {
Packit f546b1
    g_warning ("pushing tasks on pool %p is not supported", pool);
Packit f546b1
    return NULL;
Packit f546b1
  }
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_task_pool_join:
Packit f546b1
 * @pool: a #GstTaskPool
Packit f546b1
 * @id: the id
Packit f546b1
 *
Packit f546b1
 * Join a task and/or return it to the pool. @id is the id obtained from
Packit f546b1
 * gst_task_pool_push().
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_task_pool_join (GstTaskPool * pool, gpointer id)
Packit f546b1
{
Packit f546b1
  GstTaskPoolClass *klass;
Packit f546b1
Packit f546b1
  g_return_if_fail (GST_IS_TASK_POOL (pool));
Packit f546b1
Packit f546b1
  klass = GST_TASK_POOL_GET_CLASS (pool);
Packit f546b1
Packit f546b1
  if (klass->join)
Packit f546b1
    klass->join (pool, id);
Packit f546b1
}