2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
5 * gsttask.c: Streaming tasks
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Abstraction of GStreamer streaming threads.
26 * @see_also: #GstElement, #GstPad
28 * #GstTask is used by #GstElement and #GstPad to provide the data passing
29 * threads in a #GstPipeline.
31 * A #GstPad will typically start a #GstTask to push or pull data to/from the
32 * peer pads. Most source elements start a #GstTask to push data. In some cases
33 * a demuxer element can start a #GstTask to pull data from a peer element. This
34 * is typically done when the demuxer can perform random access on the upstream
35 * peer element for improved performance.
37 * Although convenience functions exist on #GstPad to start/pause/stop tasks, it
38 * might sometimes be needed to create a #GstTask manually if it is not related to
41 * Before the #GstTask can be run, it needs a #GStaticRecMutex that can be set with
42 * gst_task_set_lock().
44 * The task can be started, paused and stopped with gst_task_start(), gst_task_pause()
45 * and gst_task_stop() respectively.
47 * A #GstTask will repeadedly call the #GstTaskFunction with the user data
48 * that was provided when creating the task with gst_task_create(). Before calling
49 * the function it will acquire the provided lock.
51 * Stopping a task with gst_task_stop() will not immediatly make sure the task is
52 * not running anymore. Use gst_task_join() to make sure the task is completely
53 * stopped and the thread is stopped.
55 * After creating a #GstTask, use gst_object_unref() to free its resources. This can
56 * only be done it the task is not running anymore.
58 * Last reviewed on 2006-02-13 (0.10.4)
61 #include "gst_private.h"
66 GST_DEBUG_CATEGORY_STATIC (task_debug);
67 #define GST_CAT_DEFAULT (task_debug)
69 static void gst_task_class_init (GstTaskClass * klass);
70 static void gst_task_init (GstTask * task);
71 static void gst_task_finalize (GObject * object);
73 static void gst_task_func (GstTask * task, GstTaskClass * tclass);
75 static GstObjectClass *parent_class = NULL;
77 static GStaticMutex pool_lock = G_STATIC_MUTEX_INIT;
80 gst_task_get_type (void)
82 static GType _gst_task_type = 0;
84 if (G_UNLIKELY (_gst_task_type == 0)) {
85 static const GTypeInfo task_info = {
86 sizeof (GstTaskClass),
89 (GClassInitFunc) gst_task_class_init,
94 (GInstanceInitFunc) gst_task_init,
99 g_type_register_static (GST_TYPE_OBJECT, "GstTask", &task_info, 0);
101 GST_DEBUG_CATEGORY_INIT (task_debug, "task", 0, "Processing tasks");
103 return _gst_task_type;
107 gst_task_class_init (GstTaskClass * klass)
109 GObjectClass *gobject_class;
111 gobject_class = (GObjectClass *) klass;
113 parent_class = g_type_class_peek_parent (klass);
115 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_task_finalize);
117 klass->pool = g_thread_pool_new (
118 (GFunc) gst_task_func, klass, -1, FALSE, NULL);
122 gst_task_init (GstTask * task)
124 task->running = FALSE;
125 task->abidata.ABI.thread = NULL;
127 task->cond = g_cond_new ();
128 task->state = GST_TASK_STOPPED;
132 gst_task_finalize (GObject * object)
134 GstTask *task = GST_TASK (object);
136 GST_DEBUG ("task %p finalize", task);
138 /* task thread cannot be running here since it holds a ref
139 * to the task so that the finalize could not have happened */
140 g_cond_free (task->cond);
143 G_OBJECT_CLASS (parent_class)->finalize (object);
147 gst_task_func (GstTask * task, GstTaskClass * tclass)
149 GStaticRecMutex *lock;
152 tself = g_thread_self ();
154 GST_DEBUG ("Entering task %p, thread %p", task, tself);
156 /* we have to grab the lock to get the mutex. We also
157 * mark our state running so that nobody can mess with
159 GST_OBJECT_LOCK (task);
160 if (task->state == GST_TASK_STOPPED)
162 lock = GST_TASK_GET_LOCK (task);
163 if (G_UNLIKELY (lock == NULL))
165 task->running = TRUE;
166 task->abidata.ABI.thread = tself;
167 GST_OBJECT_UNLOCK (task);
169 /* locking order is TASK_LOCK, LOCK */
170 g_static_rec_mutex_lock (lock);
171 GST_OBJECT_LOCK (task);
172 while (G_LIKELY (task->state != GST_TASK_STOPPED)) {
173 while (G_UNLIKELY (task->state == GST_TASK_PAUSED)) {
176 t = g_static_rec_mutex_unlock_full (lock);
178 g_warning ("wrong STREAM_LOCK count %d", t);
180 GST_TASK_SIGNAL (task);
181 GST_TASK_WAIT (task);
182 GST_OBJECT_UNLOCK (task);
183 /* locking order.. */
185 g_static_rec_mutex_lock_full (lock, t);
187 GST_OBJECT_LOCK (task);
188 if (G_UNLIKELY (task->state == GST_TASK_STOPPED))
191 GST_OBJECT_UNLOCK (task);
193 task->func (task->data);
195 GST_OBJECT_LOCK (task);
198 GST_OBJECT_UNLOCK (task);
199 g_static_rec_mutex_unlock (lock);
201 /* now we allow messing with the lock again */
202 GST_OBJECT_LOCK (task);
203 task->running = FALSE;
204 task->abidata.ABI.thread = NULL;
206 GST_TASK_SIGNAL (task);
207 GST_OBJECT_UNLOCK (task);
209 GST_DEBUG ("Exit task %p, thread %p", task, g_thread_self ());
211 gst_object_unref (task);
216 g_warning ("starting task without a lock");
222 * gst_task_cleanup_all:
224 * Wait for all tasks to be stopped. This is mainly used internally
225 * to ensure proper cleanup of internal datastructures in testsuites.
230 gst_task_cleanup_all (void)
234 if ((klass = g_type_class_peek (GST_TYPE_TASK))) {
235 g_static_mutex_lock (&pool_lock);
237 /* Shut down all the threads, we still process the ones scheduled
238 * because the unref happens in the thread function.
239 * Also wait for currently running ones to finish. */
240 g_thread_pool_free (klass->pool, FALSE, TRUE);
241 /* create new pool, so we can still do something after this
243 klass->pool = g_thread_pool_new (
244 (GFunc) gst_task_func, klass, -1, FALSE, NULL);
246 g_static_mutex_unlock (&pool_lock);
252 * @func: The #GstTaskFunction to use
253 * @data: User data to pass to @func
255 * Create a new Task that will repeadedly call the provided @func
256 * with @data as a parameter. Typically the task will run in
259 * Returns: A new #GstTask.
264 gst_task_create (GstTaskFunction func, gpointer data)
268 task = g_object_new (GST_TYPE_TASK, NULL);
272 GST_DEBUG ("Created task %p", task);
279 * @task: The #GstTask to use
280 * @mutex: The GMutex to use
282 * Set the mutex used by the task. The mutex will be acquired before
283 * calling the #GstTaskFunction.
285 * This function has to be called before calling gst_task_pause() or
291 gst_task_set_lock (GstTask * task, GStaticRecMutex * mutex)
293 GST_OBJECT_LOCK (task);
296 GST_TASK_GET_LOCK (task) = mutex;
297 GST_OBJECT_UNLOCK (task);
304 GST_OBJECT_UNLOCK (task);
305 g_warning ("cannot call set_lock on a running task");
311 * gst_task_get_state:
312 * @task: The #GstTask to query
314 * Get the current state of the task.
316 * Returns: The #GstTaskState of the task
321 gst_task_get_state (GstTask * task)
325 g_return_val_if_fail (GST_IS_TASK (task), GST_TASK_STOPPED);
327 GST_OBJECT_LOCK (task);
328 result = task->state;
329 GST_OBJECT_UNLOCK (task);
336 * @task: The #GstTask to start
338 * Starts @task. The @task must have a lock associated with it using
339 * gst_task_set_lock() or thsi function will return FALSE.
341 * Returns: TRUE if the task could be started.
346 gst_task_start (GstTask * task)
350 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
352 GST_DEBUG_OBJECT (task, "Starting task %p", task);
354 GST_OBJECT_LOCK (task);
355 if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
359 task->state = GST_TASK_STARTED;
361 case GST_TASK_STOPPED:
363 GstTaskClass *tclass;
365 tclass = GST_TASK_GET_CLASS (task);
367 /* new task, push on threadpool. We ref before so
368 * that it remains alive while on the threadpool. */
369 gst_object_ref (task);
370 g_static_mutex_lock (&pool_lock);
371 g_thread_pool_push (tclass->pool, task, NULL);
372 g_static_mutex_unlock (&pool_lock);
375 case GST_TASK_PAUSED:
376 /* PAUSE to PLAY, signal */
377 GST_TASK_SIGNAL (task);
379 case GST_TASK_STARTED:
383 GST_OBJECT_UNLOCK (task);
390 GST_WARNING_OBJECT (task, "starting task without a lock");
391 GST_OBJECT_UNLOCK (task);
392 g_warning ("starting task without a lock");
399 * @task: The #GstTask to stop
401 * Stops @task. This method merely schedules the task to stop and
402 * will not wait for the task to have completely stopped. Use
403 * gst_task_join() to stop and wait for completion.
405 * Returns: TRUE if the task could be stopped.
410 gst_task_stop (GstTask * task)
412 GstTaskClass *tclass;
415 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
417 tclass = GST_TASK_GET_CLASS (task);
419 GST_DEBUG_OBJECT (task, "Stopping task %p", task);
421 GST_OBJECT_LOCK (task);
423 task->state = GST_TASK_STOPPED;
425 case GST_TASK_STOPPED:
427 case GST_TASK_PAUSED:
428 GST_TASK_SIGNAL (task);
430 case GST_TASK_STARTED:
433 GST_OBJECT_UNLOCK (task);
440 * @task: The #GstTask to pause
442 * Pauses @task. This method can also be called on a task in the
443 * stopped state, in which case a thread will be started and will remain
444 * in the paused state. This function does not wait for the task to complete
447 * Returns: TRUE if the task could be paused.
452 gst_task_pause (GstTask * task)
456 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
458 GST_DEBUG_OBJECT (task, "Pausing task %p", task);
460 GST_OBJECT_LOCK (task);
461 if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
465 task->state = GST_TASK_PAUSED;
467 case GST_TASK_STOPPED:
469 GstTaskClass *tclass;
471 tclass = GST_TASK_GET_CLASS (task);
473 gst_object_ref (task);
474 g_static_mutex_lock (&pool_lock);
475 g_thread_pool_push (tclass->pool, task, NULL);
476 g_static_mutex_unlock (&pool_lock);
479 case GST_TASK_PAUSED:
481 case GST_TASK_STARTED:
484 GST_OBJECT_UNLOCK (task);
491 GST_WARNING_OBJECT (task, "pausing task without a lock");
492 GST_OBJECT_UNLOCK (task);
493 g_warning ("pausing task without a lock");
500 * @task: The #GstTask to join
502 * Joins @task. After this call, it is safe to unref the task
503 * and clean up the lock set with gst_task_set_lock().
505 * The task will automatically be stopped with this call.
507 * This function cannot be called from within a task function as this
508 * will cause a deadlock.
510 * Returns: TRUE if the task could be joined.
515 gst_task_join (GstTask * task)
519 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
521 tself = g_thread_self ();
523 GST_DEBUG_OBJECT (task, "Joining task %p, thread %p", task, tself);
525 GST_OBJECT_LOCK (task);
526 if (tself == task->abidata.ABI.thread)
528 task->state = GST_TASK_STOPPED;
529 GST_TASK_SIGNAL (task);
530 while (task->running)
531 GST_TASK_WAIT (task);
532 GST_OBJECT_UNLOCK (task);
534 GST_DEBUG_OBJECT (task, "Joined task %p", task);
541 GST_WARNING_OBJECT (task, "trying to join task from its thread");
542 GST_OBJECT_UNLOCK (task);
543 g_warning ("trying to join task %p from its thread would deadlock", task);