2009-04-23 13:44:13 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
*
|
|
|
|
* gsttaskpool.c: Pool for streaming threads
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2009-04-23 13:44:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gsttaskpool
|
2017-01-16 14:26:16 +00:00
|
|
|
* @title: GstTaskPool
|
2009-04-23 13:44:13 +00:00
|
|
|
* @short_description: Pool of GStreamer streaming threads
|
|
|
|
* @see_also: #GstTask, #GstPad
|
|
|
|
*
|
|
|
|
* This object provides an abstraction for creating threads. The default
|
|
|
|
* implementation uses a regular GThreadPool to start tasks.
|
|
|
|
*
|
|
|
|
* Subclasses can be made to create custom threads.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gst_private.h"
|
|
|
|
|
|
|
|
#include "gstinfo.h"
|
|
|
|
#include "gsttaskpool.h"
|
2019-02-28 15:48:57 +00:00
|
|
|
#include "gsterror.h"
|
2009-04-23 13:44:13 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (taskpool_debug);
|
|
|
|
#define GST_CAT_DEFAULT (taskpool_debug)
|
|
|
|
|
2010-09-13 06:50:53 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2009-04-23 13:44:13 +00:00
|
|
|
static void gst_task_pool_finalize (GObject * object);
|
2010-09-13 06:50:53 +00:00
|
|
|
#endif
|
2009-04-23 13:44:13 +00:00
|
|
|
|
|
|
|
#define _do_init \
|
|
|
|
{ \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (taskpool_debug, "taskpool", 0, "Thread pool"); \
|
|
|
|
}
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstTaskPool, gst_task_pool, GST_TYPE_OBJECT, _do_init);
|
|
|
|
|
2009-04-24 10:35:08 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstTaskPoolFunction func;
|
|
|
|
gpointer user_data;
|
|
|
|
} TaskData;
|
|
|
|
|
2009-04-23 13:44:13 +00:00
|
|
|
static void
|
2009-04-24 10:35:08 +00:00
|
|
|
default_func (TaskData * tdata, GstTaskPool * pool)
|
|
|
|
{
|
|
|
|
GstTaskPoolFunction func;
|
|
|
|
gpointer user_data;
|
|
|
|
|
|
|
|
func = tdata->func;
|
|
|
|
user_data = tdata->user_data;
|
|
|
|
g_slice_free (TaskData, tdata);
|
|
|
|
|
|
|
|
func (user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
default_prepare (GstTaskPool * pool, GError ** error)
|
2009-04-23 13:44:13 +00:00
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (pool);
|
2019-02-28 15:48:57 +00:00
|
|
|
pool->pool = g_thread_pool_new ((GFunc) default_func, pool, -1, FALSE, error);
|
2009-04-23 13:44:13 +00:00
|
|
|
GST_OBJECT_UNLOCK (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
default_cleanup (GstTaskPool * pool)
|
|
|
|
{
|
2019-03-08 15:19:29 +00:00
|
|
|
GThreadPool *pool_;
|
|
|
|
|
2009-04-23 13:44:13 +00:00
|
|
|
GST_OBJECT_LOCK (pool);
|
2019-03-08 15:19:29 +00:00
|
|
|
pool_ = pool->pool;
|
|
|
|
pool->pool = NULL;
|
|
|
|
GST_OBJECT_UNLOCK (pool);
|
|
|
|
|
|
|
|
if (pool_) {
|
2009-04-23 13:44:13 +00:00
|
|
|
/* Shut down all the threads, we still process the ones scheduled
|
|
|
|
* because the unref happens in the thread function.
|
|
|
|
* Also wait for currently running ones to finish. */
|
2019-03-08 15:19:29 +00:00
|
|
|
g_thread_pool_free (pool_, FALSE, TRUE);
|
2009-04-23 13:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-23 14:00:56 +00:00
|
|
|
static gpointer
|
2009-04-24 10:35:08 +00:00
|
|
|
default_push (GstTaskPool * pool, GstTaskPoolFunction func,
|
|
|
|
gpointer user_data, GError ** error)
|
2009-04-23 13:44:13 +00:00
|
|
|
{
|
2009-04-24 10:35:08 +00:00
|
|
|
TaskData *tdata;
|
|
|
|
|
|
|
|
tdata = g_slice_new (TaskData);
|
|
|
|
tdata->func = func;
|
|
|
|
tdata->user_data = user_data;
|
|
|
|
|
2009-04-23 13:44:13 +00:00
|
|
|
GST_OBJECT_LOCK (pool);
|
|
|
|
if (pool->pool)
|
2009-04-24 10:35:08 +00:00
|
|
|
g_thread_pool_push (pool->pool, tdata, error);
|
|
|
|
else {
|
|
|
|
g_slice_free (TaskData, tdata);
|
2019-02-28 15:48:57 +00:00
|
|
|
g_set_error_literal (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
|
|
|
|
"No thread pool");
|
|
|
|
|
2009-04-24 10:35:08 +00:00
|
|
|
}
|
2009-04-23 13:44:13 +00:00
|
|
|
GST_OBJECT_UNLOCK (pool);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-04-23 14:00:56 +00:00
|
|
|
default_join (GstTaskPool * pool, gpointer id)
|
2009-04-23 13:44:13 +00:00
|
|
|
{
|
2009-04-24 10:35:08 +00:00
|
|
|
/* we do nothing here, we can't join from the pools */
|
2009-04-23 13:44:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:41:31 +00:00
|
|
|
static void
|
|
|
|
default_dispose_handle (GstTaskPool * pool, gpointer id)
|
|
|
|
{
|
|
|
|
/* we do nothing here, the default handle is NULL */
|
|
|
|
}
|
|
|
|
|
2009-04-23 13:44:13 +00:00
|
|
|
static void
|
|
|
|
gst_task_pool_class_init (GstTaskPoolClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstTaskPoolClass *gsttaskpool_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gsttaskpool_class = (GstTaskPoolClass *) klass;
|
|
|
|
|
2010-09-13 06:50:53 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2009-10-28 00:29:30 +00:00
|
|
|
gobject_class->finalize = gst_task_pool_finalize;
|
2010-09-13 06:50:53 +00:00
|
|
|
#endif
|
2009-04-23 13:44:13 +00:00
|
|
|
|
|
|
|
gsttaskpool_class->prepare = default_prepare;
|
|
|
|
gsttaskpool_class->cleanup = default_cleanup;
|
|
|
|
gsttaskpool_class->push = default_push;
|
|
|
|
gsttaskpool_class->join = default_join;
|
2020-11-03 01:41:31 +00:00
|
|
|
gsttaskpool_class->dispose_handle = default_dispose_handle;
|
2009-04-23 13:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_task_pool_init (GstTaskPool * pool)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-13 06:50:53 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2009-04-23 13:44:13 +00:00
|
|
|
static void
|
|
|
|
gst_task_pool_finalize (GObject * object)
|
|
|
|
{
|
2009-06-19 12:42:45 +00:00
|
|
|
GST_DEBUG ("taskpool %p finalize", object);
|
2009-04-23 13:44:13 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_task_pool_parent_class)->finalize (object);
|
|
|
|
}
|
2010-09-13 06:50:53 +00:00
|
|
|
#endif
|
2009-04-23 13:44:13 +00:00
|
|
|
/**
|
|
|
|
* gst_task_pool_new:
|
|
|
|
*
|
|
|
|
* Create a new default task pool. The default task pool will use a regular
|
|
|
|
* GThreadPool for threads.
|
|
|
|
*
|
2010-12-07 18:35:04 +00:00
|
|
|
* Returns: (transfer full): a new #GstTaskPool. gst_object_unref() after usage.
|
2009-04-23 13:44:13 +00:00
|
|
|
*/
|
|
|
|
GstTaskPool *
|
|
|
|
gst_task_pool_new (void)
|
|
|
|
{
|
|
|
|
GstTaskPool *pool;
|
|
|
|
|
2017-04-04 16:53:39 +00:00
|
|
|
pool = g_object_new (GST_TYPE_TASK_POOL, NULL);
|
2009-04-23 13:44:13 +00:00
|
|
|
|
2017-05-15 11:32:48 +00:00
|
|
|
/* clear floating flag */
|
|
|
|
gst_object_ref_sink (pool);
|
|
|
|
|
2009-04-23 13:44:13 +00:00
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_task_pool_prepare:
|
|
|
|
* @pool: a #GstTaskPool
|
|
|
|
* @error: an error return location
|
|
|
|
*
|
|
|
|
* Prepare the taskpool for accepting gst_task_pool_push() operations.
|
|
|
|
*
|
|
|
|
* MT safe.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_task_pool_prepare (GstTaskPool * pool, GError ** error)
|
|
|
|
{
|
|
|
|
GstTaskPoolClass *klass;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_TASK_POOL (pool));
|
|
|
|
|
|
|
|
klass = GST_TASK_POOL_GET_CLASS (pool);
|
|
|
|
|
|
|
|
if (klass->prepare)
|
2009-04-24 10:35:08 +00:00
|
|
|
klass->prepare (pool, error);
|
2009-04-23 13:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_task_pool_cleanup:
|
|
|
|
* @pool: a #GstTaskPool
|
|
|
|
*
|
|
|
|
* Wait for all tasks to be stopped. This is mainly used internally
|
|
|
|
* to ensure proper cleanup of internal data structures in test suites.
|
|
|
|
*
|
|
|
|
* MT safe.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_task_pool_cleanup (GstTaskPool * pool)
|
|
|
|
{
|
|
|
|
GstTaskPoolClass *klass;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_TASK_POOL (pool));
|
|
|
|
|
|
|
|
klass = GST_TASK_POOL_GET_CLASS (pool);
|
|
|
|
|
|
|
|
if (klass->cleanup)
|
|
|
|
klass->cleanup (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_task_pool_push:
|
|
|
|
* @pool: a #GstTaskPool
|
2012-06-20 00:37:59 +00:00
|
|
|
* @func: (scope async): the function to call
|
2010-12-07 18:35:04 +00:00
|
|
|
* @user_data: (closure): data to pass to @func
|
2009-04-23 13:44:13 +00:00
|
|
|
* @error: return location for an error
|
|
|
|
*
|
|
|
|
* Start the execution of a new thread from @pool.
|
|
|
|
*
|
2020-11-03 01:33:32 +00:00
|
|
|
* Returns: (transfer full) (nullable): a pointer that should be used
|
2014-06-11 22:21:34 +00:00
|
|
|
* for the gst_task_pool_join function. This pointer can be %NULL, you
|
2020-11-03 01:41:31 +00:00
|
|
|
* must check @error to detect errors. If the pointer is not %NULL and
|
|
|
|
* gst_task_pool_join() is not used, call gst_task_pool_dispose_handle()
|
|
|
|
* instead.
|
2009-04-23 13:44:13 +00:00
|
|
|
*/
|
2009-04-23 14:00:56 +00:00
|
|
|
gpointer
|
2009-04-24 10:35:08 +00:00
|
|
|
gst_task_pool_push (GstTaskPool * pool, GstTaskPoolFunction func,
|
|
|
|
gpointer user_data, GError ** error)
|
2009-04-23 13:44:13 +00:00
|
|
|
{
|
|
|
|
GstTaskPoolClass *klass;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_TASK_POOL (pool), NULL);
|
|
|
|
|
|
|
|
klass = GST_TASK_POOL_GET_CLASS (pool);
|
|
|
|
|
|
|
|
if (klass->push == NULL)
|
|
|
|
goto not_supported;
|
|
|
|
|
2009-04-24 10:35:08 +00:00
|
|
|
return klass->push (pool, func, user_data, error);
|
2009-04-23 13:44:13 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
not_supported:
|
|
|
|
{
|
|
|
|
g_warning ("pushing tasks on pool %p is not supported", pool);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_task_pool_join:
|
|
|
|
* @pool: a #GstTaskPool
|
2020-11-03 01:39:37 +00:00
|
|
|
* @id: (transfer full) (nullable): the id
|
2009-04-23 13:44:13 +00:00
|
|
|
*
|
2017-01-16 14:26:16 +00:00
|
|
|
* Join a task and/or return it to the pool. @id is the id obtained from
|
2020-11-03 01:39:37 +00:00
|
|
|
* gst_task_pool_push(). The default implementation does nothing, as the
|
|
|
|
* default push() implementation always returns %NULL.
|
2009-04-23 13:44:13 +00:00
|
|
|
*/
|
|
|
|
void
|
2009-04-23 14:00:56 +00:00
|
|
|
gst_task_pool_join (GstTaskPool * pool, gpointer id)
|
2009-04-23 13:44:13 +00:00
|
|
|
{
|
|
|
|
GstTaskPoolClass *klass;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_TASK_POOL (pool));
|
|
|
|
|
|
|
|
klass = GST_TASK_POOL_GET_CLASS (pool);
|
|
|
|
|
|
|
|
if (klass->join)
|
2009-04-23 14:00:56 +00:00
|
|
|
klass->join (pool, id);
|
2009-04-23 13:44:13 +00:00
|
|
|
}
|
2020-11-03 01:41:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_task_pool_dispose_handle:
|
|
|
|
* @pool: a #GstTaskPool
|
|
|
|
* @id: (transfer full) (nullable): the id
|
|
|
|
*
|
|
|
|
* Dispose of the handle returned by gst_task_pool_push(). This does
|
|
|
|
* not need to be called with the default implementation as the default
|
|
|
|
* push() implementation always returns %NULL. This does not need to be
|
|
|
|
* called either when calling gst_task_pool_join().
|
|
|
|
*
|
|
|
|
* Since: 1.20
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_task_pool_dispose_handle (GstTaskPool * pool, gpointer id)
|
|
|
|
{
|
|
|
|
GstTaskPoolClass *klass;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_TASK_POOL (pool));
|
|
|
|
|
|
|
|
klass = GST_TASK_POOL_GET_CLASS (pool);
|
|
|
|
|
|
|
|
if (klass->dispose_handle)
|
|
|
|
klass->dispose_handle (pool, id);
|
|
|
|
}
|