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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 * @short_description: Pool of GStreamer streaming threads
26 * @see_also: #GstTask, #GstPad
28 * This object provides an abstraction for creating threads. The default
29 * implementation uses a regular GThreadPool to start tasks.
31 * Subclasses can be made to create custom threads.
34 #include "gst_private.h"
37 #include "gsttaskpool.h"
40 GST_DEBUG_CATEGORY_STATIC (taskpool_debug);
41 #define GST_CAT_DEFAULT (taskpool_debug)
43 #ifndef GST_DISABLE_GST_DEBUG
44 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);
56 GstTaskPoolFunction func;
61 default_func (TaskData * tdata, GstTaskPool * pool)
63 GstTaskPoolFunction func;
67 user_data = tdata->user_data;
68 g_slice_free (TaskData, tdata);
74 default_prepare (GstTaskPool * pool, GError ** error)
76 GST_OBJECT_LOCK (pool);
77 pool->pool = g_thread_pool_new ((GFunc) default_func, pool, -1, FALSE, error);
78 GST_OBJECT_UNLOCK (pool);
82 default_cleanup (GstTaskPool * pool)
86 GST_OBJECT_LOCK (pool);
89 GST_OBJECT_UNLOCK (pool);
92 /* Shut down all the threads, we still process the ones scheduled
93 * because the unref happens in the thread function.
94 * Also wait for currently running ones to finish. */
95 g_thread_pool_free (pool_, FALSE, TRUE);
100 default_push (GstTaskPool * pool, GstTaskPoolFunction func,
101 gpointer user_data, GError ** error)
105 tdata = g_slice_new (TaskData);
107 tdata->user_data = user_data;
109 GST_OBJECT_LOCK (pool);
111 g_thread_pool_push (pool->pool, tdata, error);
113 g_slice_free (TaskData, tdata);
114 g_set_error_literal (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
118 GST_OBJECT_UNLOCK (pool);
124 default_join (GstTaskPool * pool, gpointer id)
126 /* we do nothing here, we can't join from the pools */
130 default_dispose_handle (GstTaskPool * pool, gpointer id)
132 /* we do nothing here, the default handle is NULL */
136 gst_task_pool_class_init (GstTaskPoolClass * klass)
138 GObjectClass *gobject_class;
139 GstTaskPoolClass *gsttaskpool_class;
141 gobject_class = (GObjectClass *) klass;
142 gsttaskpool_class = (GstTaskPoolClass *) klass;
144 #ifndef GST_DISABLE_GST_DEBUG
145 gobject_class->finalize = gst_task_pool_finalize;
148 gsttaskpool_class->prepare = default_prepare;
149 gsttaskpool_class->cleanup = default_cleanup;
150 gsttaskpool_class->push = default_push;
151 gsttaskpool_class->join = default_join;
152 gsttaskpool_class->dispose_handle = default_dispose_handle;
156 gst_task_pool_init (GstTaskPool * pool)
160 #ifndef GST_DISABLE_GST_DEBUG
162 gst_task_pool_finalize (GObject * object)
164 GST_DEBUG ("taskpool %p finalize", object);
166 G_OBJECT_CLASS (gst_task_pool_parent_class)->finalize (object);
172 * Create a new default task pool. The default task pool will use a regular
173 * GThreadPool for threads.
175 * Returns: (transfer full): a new #GstTaskPool. gst_object_unref() after usage.
178 gst_task_pool_new (void)
182 pool = g_object_new (GST_TYPE_TASK_POOL, NULL);
184 /* clear floating flag */
185 gst_object_ref_sink (pool);
191 * gst_task_pool_prepare:
192 * @pool: a #GstTaskPool
193 * @error: an error return location
195 * Prepare the taskpool for accepting gst_task_pool_push() operations.
200 gst_task_pool_prepare (GstTaskPool * pool, GError ** error)
202 GstTaskPoolClass *klass;
204 g_return_if_fail (GST_IS_TASK_POOL (pool));
206 klass = GST_TASK_POOL_GET_CLASS (pool);
209 klass->prepare (pool, error);
213 * gst_task_pool_cleanup:
214 * @pool: a #GstTaskPool
216 * Wait for all tasks to be stopped. This is mainly used internally
217 * to ensure proper cleanup of internal data structures in test suites.
222 gst_task_pool_cleanup (GstTaskPool * pool)
224 GstTaskPoolClass *klass;
226 g_return_if_fail (GST_IS_TASK_POOL (pool));
228 klass = GST_TASK_POOL_GET_CLASS (pool);
231 klass->cleanup (pool);
235 * gst_task_pool_push:
236 * @pool: a #GstTaskPool
237 * @func: (scope async): the function to call
238 * @user_data: (closure): data to pass to @func
239 * @error: return location for an error
241 * Start the execution of a new thread from @pool.
243 * Returns: (transfer full) (nullable): a pointer that should be used
244 * for the gst_task_pool_join function. This pointer can be %NULL, you
245 * must check @error to detect errors. If the pointer is not %NULL and
246 * gst_task_pool_join() is not used, call gst_task_pool_dispose_handle()
250 gst_task_pool_push (GstTaskPool * pool, GstTaskPoolFunction func,
251 gpointer user_data, GError ** error)
253 GstTaskPoolClass *klass;
255 g_return_val_if_fail (GST_IS_TASK_POOL (pool), NULL);
257 klass = GST_TASK_POOL_GET_CLASS (pool);
259 if (klass->push == NULL)
262 return klass->push (pool, func, user_data, error);
267 g_warning ("pushing tasks on pool %p is not supported", pool);
273 * gst_task_pool_join:
274 * @pool: a #GstTaskPool
275 * @id: (transfer full) (nullable): the id
277 * Join a task and/or return it to the pool. @id is the id obtained from
278 * gst_task_pool_push(). The default implementation does nothing, as the
279 * default #GstTaskPoolClass::push implementation always returns %NULL.
281 * This method should only be called with the same @pool instance that provided
285 gst_task_pool_join (GstTaskPool * pool, gpointer id)
287 GstTaskPoolClass *klass;
289 g_return_if_fail (GST_IS_TASK_POOL (pool));
291 klass = GST_TASK_POOL_GET_CLASS (pool);
294 klass->join (pool, id);
298 * gst_task_pool_dispose_handle:
299 * @pool: a #GstTaskPool
300 * @id: (transfer full) (nullable): the id
302 * Dispose of the handle returned by gst_task_pool_push(). This does
303 * not need to be called with the default implementation as the default
304 * #GstTaskPoolClass::push implementation always returns %NULL. This does not need to be
305 * called either when calling gst_task_pool_join(), but should be called
306 * when joining is not necessary, but gst_task_pool_push() returned a
309 * This method should only be called with the same @pool instance that provided
315 gst_task_pool_dispose_handle (GstTaskPool * pool, gpointer id)
317 GstTaskPoolClass *klass;
319 g_return_if_fail (GST_IS_TASK_POOL (pool));
321 klass = GST_TASK_POOL_GET_CLASS (pool);
323 if (klass->dispose_handle)
324 klass->dispose_handle (pool, id);
331 GstTaskPoolFunction func;
338 static SharedTaskData *
339 shared_task_data_ref (SharedTaskData * tdata)
341 g_atomic_int_add (&tdata->refcount, 1);
347 shared_task_data_unref (SharedTaskData * tdata)
349 if (g_atomic_int_dec_and_test (&tdata->refcount)) {
350 g_mutex_clear (&tdata->done_lock);
351 g_cond_clear (&tdata->done_cond);
352 g_slice_free (SharedTaskData, tdata);
356 struct _GstSharedTaskPoolPrivate
361 #define GST_SHARED_TASK_POOL_CAST(pool) ((GstSharedTaskPool*)(pool))
363 G_DEFINE_TYPE_WITH_PRIVATE (GstSharedTaskPool, gst_shared_task_pool,
367 shared_func (SharedTaskData * tdata, GstTaskPool * pool)
369 tdata->func (tdata->user_data);
371 g_mutex_lock (&tdata->done_lock);
373 g_cond_signal (&tdata->done_cond);
374 g_mutex_unlock (&tdata->done_lock);
376 shared_task_data_unref (tdata);
380 shared_push (GstTaskPool * pool, GstTaskPoolFunction func,
381 gpointer user_data, GError ** error)
383 SharedTaskData *ret = NULL;
385 GST_OBJECT_LOCK (pool);
388 GST_OBJECT_UNLOCK (pool);
392 ret = g_slice_new (SharedTaskData);
396 ret->user_data = user_data;
397 g_atomic_int_set (&ret->refcount, 1);
398 g_cond_init (&ret->done_cond);
399 g_mutex_init (&ret->done_lock);
401 g_thread_pool_push (pool->pool, shared_task_data_ref (ret), error);
403 GST_OBJECT_UNLOCK (pool);
410 shared_join (GstTaskPool * pool, gpointer id)
412 SharedTaskData *tdata;
417 tdata = (SharedTaskData *) id;
419 g_mutex_lock (&tdata->done_lock);
420 while (!tdata->done) {
421 g_cond_wait (&tdata->done_cond, &tdata->done_lock);
423 g_mutex_unlock (&tdata->done_lock);
425 shared_task_data_unref (tdata);
429 shared_dispose_handle (GstTaskPool * pool, gpointer id)
431 SharedTaskData *tdata;
436 tdata = (SharedTaskData *) id;
439 shared_task_data_unref (tdata);
443 shared_prepare (GstTaskPool * pool, GError ** error)
445 GstSharedTaskPool *shared_pool = GST_SHARED_TASK_POOL_CAST (pool);
447 GST_OBJECT_LOCK (pool);
449 g_thread_pool_new ((GFunc) shared_func, pool,
450 shared_pool->priv->max_threads, FALSE, error);
451 GST_OBJECT_UNLOCK (pool);
455 gst_shared_task_pool_class_init (GstSharedTaskPoolClass * klass)
457 GstTaskPoolClass *taskpoolclass = GST_TASK_POOL_CLASS (klass);
459 taskpoolclass->prepare = shared_prepare;
460 taskpoolclass->push = shared_push;
461 taskpoolclass->join = shared_join;
462 taskpoolclass->dispose_handle = shared_dispose_handle;
466 gst_shared_task_pool_init (GstSharedTaskPool * pool)
468 GstSharedTaskPoolPrivate *priv;
470 priv = pool->priv = gst_shared_task_pool_get_instance_private (pool);
471 priv->max_threads = 1;
475 * gst_shared_task_pool_set_max_threads:
476 * @pool: a #GstSharedTaskPool
477 * @max_threads: Maximum number of threads to spawn.
479 * Update the maximal number of threads the @pool may spawn. When
480 * the maximal number of threads is reduced, existing threads are not
481 * immediately shut down, see g_thread_pool_set_max_threads().
483 * Setting @max_threads to 0 effectively freezes the pool.
488 gst_shared_task_pool_set_max_threads (GstSharedTaskPool * pool,
491 GstTaskPool *taskpool;
493 g_return_if_fail (GST_IS_SHARED_TASK_POOL (pool));
495 taskpool = GST_TASK_POOL (pool);
497 GST_OBJECT_LOCK (pool);
499 g_thread_pool_set_max_threads (taskpool->pool, max_threads, NULL);
500 pool->priv->max_threads = max_threads;
501 GST_OBJECT_UNLOCK (pool);
505 * gst_shared_task_pool_get_max_threads:
506 * @pool: a #GstSharedTaskPool
508 * Returns: the maximum number of threads @pool is configured to spawn
512 gst_shared_task_pool_get_max_threads (GstSharedTaskPool * pool)
516 g_return_val_if_fail (GST_IS_SHARED_TASK_POOL (pool), 0);
518 GST_OBJECT_LOCK (pool);
519 ret = pool->priv->max_threads;
520 GST_OBJECT_UNLOCK (pool);
526 * gst_shared_task_pool_new:
528 * Create a new shared task pool. The shared task pool will queue tasks on
529 * a maximum number of threads, 1 by default.
531 * Do not use a #GstSharedTaskPool to manage potentially inter-dependent tasks such
532 * as pad tasks, as having one task waiting on another to return before returning
533 * would cause obvious deadlocks if they happen to share the same thread.
535 * Returns: (transfer full): a new #GstSharedTaskPool. gst_object_unref() after usage.
539 gst_shared_task_pool_new (void)
543 pool = g_object_new (GST_TYPE_SHARED_TASK_POOL, NULL);
545 /* clear floating flag */
546 gst_object_ref_sink (pool);