2 * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
4 * gsttaskpool.c: Pool for streaming threads
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: Pool of GStreamer streaming threads
25 * @see_also: #GstTask, #GstPad
27 * This object provides an abstraction for creating threads. The default
28 * implementation uses a regular GThreadPool to start tasks.
30 * Subclasses can be made to create custom threads.
32 * Last reviewed on 2009-04-23 (0.10.24)
35 #include "gst_private.h"
38 #include "gsttaskpool.h"
40 GST_DEBUG_CATEGORY_STATIC (taskpool_debug);
41 #define GST_CAT_DEFAULT (taskpool_debug)
43 static void gst_task_pool_class_init (GstTaskPoolClass * klass);
44 static void gst_task_pool_init (GstTaskPool * pool);
45 static void gst_task_pool_finalize (GObject * object);
49 GST_DEBUG_CATEGORY_INIT (taskpool_debug, "taskpool", 0, "Thread pool"); \
52 G_DEFINE_TYPE_WITH_CODE (GstTaskPool, gst_task_pool, GST_TYPE_OBJECT, _do_init);
55 default_prepare (GstTaskPool * pool, GFunc func, gpointer user_data,
58 GST_OBJECT_LOCK (pool);
59 pool->pool = g_thread_pool_new ((GFunc) func, user_data, -1, FALSE, NULL);
60 GST_OBJECT_UNLOCK (pool);
64 default_cleanup (GstTaskPool * pool)
66 GST_OBJECT_LOCK (pool);
68 /* Shut down all the threads, we still process the ones scheduled
69 * because the unref happens in the thread function.
70 * Also wait for currently running ones to finish. */
71 g_thread_pool_free (pool->pool, FALSE, TRUE);
74 GST_OBJECT_UNLOCK (pool);
78 default_push (GstTaskPool * pool, gpointer data, GError ** error)
80 GST_OBJECT_LOCK (pool);
82 g_thread_pool_push (pool->pool, data, error);
83 GST_OBJECT_UNLOCK (pool);
89 default_join (GstTaskPool * pool, gpointer id)
91 /* does nothing, we can't join for threads from the threadpool */
95 gst_task_pool_class_init (GstTaskPoolClass * klass)
97 GObjectClass *gobject_class;
98 GstTaskPoolClass *gsttaskpool_class;
100 gobject_class = (GObjectClass *) klass;
101 gsttaskpool_class = (GstTaskPoolClass *) klass;
103 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_task_pool_finalize);
105 gsttaskpool_class->prepare = default_prepare;
106 gsttaskpool_class->cleanup = default_cleanup;
107 gsttaskpool_class->push = default_push;
108 gsttaskpool_class->join = default_join;
112 gst_task_pool_init (GstTaskPool * pool)
117 gst_task_pool_finalize (GObject * object)
119 GstTaskPool *pool = GST_TASK_POOL (object);
121 GST_DEBUG ("taskpool %p finalize", pool);
123 G_OBJECT_CLASS (gst_task_pool_parent_class)->finalize (object);
129 * Create a new default task pool. The default task pool will use a regular
130 * GThreadPool for threads.
132 * Returns: a new #GstTaskPool. gst_object_unref() after usage.
135 gst_task_pool_new (void)
139 pool = g_object_new (GST_TYPE_TASK_POOL, NULL);
145 gst_task_pool_set_func (GstTaskPool * pool, GFunc func, gpointer user_data)
147 g_return_if_fail (GST_IS_TASK_POOL (pool));
150 pool->user_data = user_data;
155 * gst_task_pool_prepare:
156 * @pool: a #GstTaskPool
157 * @error: an error return location
159 * Prepare the taskpool for accepting gst_task_pool_push() operations.
164 gst_task_pool_prepare (GstTaskPool * pool, GError ** error)
166 GstTaskPoolClass *klass;
168 g_return_if_fail (GST_IS_TASK_POOL (pool));
170 klass = GST_TASK_POOL_GET_CLASS (pool);
173 klass->prepare (pool, pool->func, pool->user_data, error);
177 * gst_task_pool_cleanup:
178 * @pool: a #GstTaskPool
180 * Wait for all tasks to be stopped. This is mainly used internally
181 * to ensure proper cleanup of internal data structures in test suites.
186 gst_task_pool_cleanup (GstTaskPool * pool)
188 GstTaskPoolClass *klass;
190 g_return_if_fail (GST_IS_TASK_POOL (pool));
192 klass = GST_TASK_POOL_GET_CLASS (pool);
195 klass->cleanup (pool);
199 * gst_task_pool_push:
200 * @pool: a #GstTaskPool
201 * @data: data to pass to the thread function
202 * @error: return location for an error
204 * Start the execution of a new thread from @pool.
206 * Returns: a pointer that should be used for the gst_task_pool_join
207 * function. This pointer can be NULL, you must check @error to detect
211 gst_task_pool_push (GstTaskPool * pool, gpointer data, GError ** error)
213 GstTaskPoolClass *klass;
215 g_return_val_if_fail (GST_IS_TASK_POOL (pool), NULL);
217 klass = GST_TASK_POOL_GET_CLASS (pool);
219 if (klass->push == NULL)
222 return klass->push (pool, data, error);
227 g_warning ("pushing tasks on pool %p is not supported", pool);
233 * gst_task_pool_join:
234 * @pool: a #GstTaskPool
237 * Join a task and/or return it to the pool. @id is the id obtained from
238 * gst_task_pool_push().
241 gst_task_pool_join (GstTaskPool * pool, gpointer id)
243 GstTaskPoolClass *klass;
245 g_return_if_fail (GST_IS_TASK_POOL (pool));
247 klass = GST_TASK_POOL_GET_CLASS (pool);
250 klass->join (pool, id);