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 or with the gst_task_set_state() function.
47 * A #GstTask will repeatedly call the #GstTaskFunction with the user data
48 * that was provided when creating the task with gst_task_create(). While calling
49 * the function it will acquire the provided lock. The provided lock is released
50 * when the task pauses or stops.
52 * Stopping a task with gst_task_stop() will not immediately make sure the task is
53 * not running anymore. Use gst_task_join() to make sure the task is completely
54 * stopped and the thread is stopped.
56 * After creating a #GstTask, use gst_object_unref() to free its resources. This can
57 * only be done it the task is not running anymore.
59 * Task functions can send a #GstMessage to send out-of-band data to the
60 * application. The application can receive messages from the #GstBus in its
63 * For debugging perposes, the task will configure its object name as the thread
64 * name on Linux. Please note that the object name should be configured before the
65 * task is started; changing the object name after the task has been started, has
66 * no effect on the thread name.
68 * Last reviewed on 2010-03-15 (0.10.29)
71 #include "gst_private.h"
76 #ifdef HAVE_SYS_PRCTL_H
77 #include <sys/prctl.h>
80 GST_DEBUG_CATEGORY_STATIC (task_debug);
81 #define GST_CAT_DEFAULT (task_debug)
83 #define GST_TASK_GET_PRIVATE(obj) \
84 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_TASK, GstTaskPrivate))
86 struct _GstTaskPrivate
88 /* callbacks for managing the thread of this task */
89 GstTaskThreadCallbacks thr_callbacks;
90 gpointer thr_user_data;
91 GDestroyNotify thr_notify;
94 GThreadPriority priority;
99 /* remember the pool and id that is currently running. */
101 GstTaskPool *pool_id;
104 static void gst_task_finalize (GObject * object);
106 static void gst_task_func (GstTask * task);
108 static GStaticMutex pool_lock = G_STATIC_MUTEX_INIT;
112 GST_DEBUG_CATEGORY_INIT (task_debug, "task", 0, "Processing tasks"); \
115 G_DEFINE_TYPE_WITH_CODE (GstTask, gst_task, GST_TYPE_OBJECT, _do_init);
118 init_klass_pool (GstTaskClass * klass)
120 g_static_mutex_lock (&pool_lock);
122 gst_task_pool_cleanup (klass->pool);
123 gst_object_unref (klass->pool);
125 klass->pool = gst_task_pool_new ();
126 gst_task_pool_prepare (klass->pool, NULL);
127 g_static_mutex_unlock (&pool_lock);
131 gst_task_class_init (GstTaskClass * klass)
133 GObjectClass *gobject_class;
135 gobject_class = (GObjectClass *) klass;
137 g_type_class_add_private (klass, sizeof (GstTaskPrivate));
139 gobject_class->finalize = gst_task_finalize;
141 init_klass_pool (klass);
145 gst_task_init (GstTask * task)
149 klass = GST_TASK_GET_CLASS (task);
151 task->priv = GST_TASK_GET_PRIVATE (task);
152 task->running = FALSE;
153 task->abidata.ABI.thread = NULL;
155 task->cond = g_cond_new ();
156 task->state = GST_TASK_STOPPED;
157 task->priv->prio_set = FALSE;
159 /* use the default klass pool for this task, users can
160 * override this later */
161 g_static_mutex_lock (&pool_lock);
162 task->priv->pool = gst_object_ref (klass->pool);
163 g_static_mutex_unlock (&pool_lock);
167 gst_task_finalize (GObject * object)
169 GstTask *task = GST_TASK (object);
170 GstTaskPrivate *priv = task->priv;
172 GST_DEBUG ("task %p finalize", task);
174 if (priv->thr_notify)
175 priv->thr_notify (priv->thr_user_data);
176 priv->thr_notify = NULL;
177 priv->thr_user_data = NULL;
179 gst_object_unref (priv->pool);
181 /* task thread cannot be running here since it holds a ref
182 * to the task so that the finalize could not have happened */
183 g_cond_free (task->cond);
186 G_OBJECT_CLASS (gst_task_parent_class)->finalize (object);
189 /* should be called with the object LOCK */
191 gst_task_configure_name (GstTask * task)
193 #ifdef HAVE_SYS_PRCTL_H
195 gchar thread_name[17] = { 0, };
197 name = GST_OBJECT_NAME (task);
199 /* set the thread name to something easily identifiable */
200 if (!snprintf (thread_name, 17, "%s", GST_STR_NULL (name))) {
201 GST_DEBUG_OBJECT (task, "Could not create thread name for '%s'", name);
203 GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", thread_name);
204 if (prctl (PR_SET_NAME, (unsigned long int) thread_name))
205 GST_DEBUG_OBJECT (task, "Failed to set thread name");
211 gst_task_func (GstTask * task)
213 GStaticRecMutex *lock;
215 GstTaskPrivate *priv;
219 tself = g_thread_self ();
221 GST_DEBUG ("Entering task %p, thread %p", task, tself);
223 /* we have to grab the lock to get the mutex. We also
224 * mark our state running so that nobody can mess with
226 GST_OBJECT_LOCK (task);
227 if (task->state == GST_TASK_STOPPED)
229 lock = GST_TASK_GET_LOCK (task);
230 if (G_UNLIKELY (lock == NULL))
232 task->abidata.ABI.thread = tself;
233 /* only update the priority when it was changed */
235 g_thread_set_priority (tself, priv->priority);
236 GST_OBJECT_UNLOCK (task);
238 /* fire the enter_thread callback when we need to */
239 if (priv->thr_callbacks.enter_thread)
240 priv->thr_callbacks.enter_thread (task, tself, priv->thr_user_data);
242 /* locking order is TASK_LOCK, LOCK */
243 g_static_rec_mutex_lock (lock);
244 GST_OBJECT_LOCK (task);
245 /* configure the thread name now */
246 gst_task_configure_name (task);
248 while (G_LIKELY (task->state != GST_TASK_STOPPED)) {
249 while (G_UNLIKELY (task->state == GST_TASK_PAUSED)) {
252 t = g_static_rec_mutex_unlock_full (lock);
254 g_warning ("wrong STREAM_LOCK count %d", t);
256 GST_TASK_SIGNAL (task);
257 GST_TASK_WAIT (task);
258 GST_OBJECT_UNLOCK (task);
259 /* locking order.. */
261 g_static_rec_mutex_lock_full (lock, t);
263 GST_OBJECT_LOCK (task);
264 if (G_UNLIKELY (task->state == GST_TASK_STOPPED))
267 GST_OBJECT_UNLOCK (task);
269 task->func (task->data);
271 GST_OBJECT_LOCK (task);
274 GST_OBJECT_UNLOCK (task);
275 g_static_rec_mutex_unlock (lock);
277 GST_OBJECT_LOCK (task);
278 task->abidata.ABI.thread = NULL;
281 if (priv->thr_callbacks.leave_thread) {
282 /* fire the leave_thread callback when we need to. We need to do this before
283 * we signal the task and with the task lock released. */
284 GST_OBJECT_UNLOCK (task);
285 priv->thr_callbacks.leave_thread (task, tself, priv->thr_user_data);
286 GST_OBJECT_LOCK (task);
288 /* restore normal priority when releasing back into the pool, we will not
289 * touch the priority when a custom callback has been installed. */
290 g_thread_set_priority (tself, G_THREAD_PRIORITY_NORMAL);
292 /* now we allow messing with the lock again by setting the running flag to
293 * FALSE. Together with the SIGNAL this is the sign for the _join() to
295 * Note that we still have not dropped the final ref on the task. We could
296 * check here if there is a pending join() going on and drop the last ref
297 * before releasing the lock as we can be sure that a ref is held by the
298 * caller of the join(). */
299 task->running = FALSE;
300 GST_TASK_SIGNAL (task);
301 GST_OBJECT_UNLOCK (task);
303 GST_DEBUG ("Exit task %p, thread %p", task, g_thread_self ());
305 gst_object_unref (task);
310 g_warning ("starting task without a lock");
316 * gst_task_cleanup_all:
318 * Wait for all tasks to be stopped. This is mainly used internally
319 * to ensure proper cleanup of internal data structures in test suites.
324 gst_task_cleanup_all (void)
328 if ((klass = g_type_class_peek (GST_TYPE_TASK))) {
329 init_klass_pool (klass);
335 * @func: The #GstTaskFunction to use
336 * @data: User data to pass to @func
338 * Create a new Task that will repeatedly call the provided @func
339 * with @data as a parameter. Typically the task will run in
342 * The function cannot be changed after the task has been created. You
343 * must create a new #GstTask to change the function.
345 * This function will not yet create and start a thread. Use gst_task_start() or
346 * gst_task_pause() to create and start the GThread.
348 * Before the task can be used, a #GStaticRecMutex must be configured using the
349 * gst_task_set_lock() function. This lock will always be acquired while
352 * Returns: A new #GstTask.
357 gst_task_create (GstTaskFunction func, gpointer data)
361 task = g_object_newv (GST_TYPE_TASK, 0, NULL);
365 GST_DEBUG ("Created task %p", task);
372 * @task: The #GstTask to use
373 * @mutex: The #GMutex to use
375 * Set the mutex used by the task. The mutex will be acquired before
376 * calling the #GstTaskFunction.
378 * This function has to be called before calling gst_task_pause() or
384 gst_task_set_lock (GstTask * task, GStaticRecMutex * mutex)
386 GST_OBJECT_LOCK (task);
387 if (G_UNLIKELY (task->running))
389 GST_TASK_GET_LOCK (task) = mutex;
390 GST_OBJECT_UNLOCK (task);
397 GST_OBJECT_UNLOCK (task);
398 g_warning ("cannot call set_lock on a running task");
403 * gst_task_set_priority:
405 * @priority: a new priority for @task
407 * Changes the priority of @task to @priority.
409 * Note: try not to depend on task priorities.
416 gst_task_set_priority (GstTask * task, GThreadPriority priority)
418 GstTaskPrivate *priv;
421 g_return_if_fail (GST_IS_TASK (task));
425 GST_OBJECT_LOCK (task);
426 priv->prio_set = TRUE;
427 priv->priority = priority;
428 thread = task->abidata.ABI.thread;
429 if (thread != NULL) {
430 /* if this task already has a thread, we can configure the priority right
431 * away, else we do that when we assign a thread to the task. */
432 g_thread_set_priority (thread, priority);
434 GST_OBJECT_UNLOCK (task);
441 * Get the #GstTaskPool that this task will use for its streaming
446 * Returns: the #GstTaskPool used by @task. gst_object_unref()
452 gst_task_get_pool (GstTask * task)
455 GstTaskPrivate *priv;
457 g_return_val_if_fail (GST_IS_TASK (task), NULL);
461 GST_OBJECT_LOCK (task);
462 result = gst_object_ref (priv->pool);
463 GST_OBJECT_UNLOCK (task);
471 * @pool: a #GstTaskPool
473 * Set @pool as the new GstTaskPool for @task. Any new streaming threads that
474 * will be created by @task will now use @pool.
481 gst_task_set_pool (GstTask * task, GstTaskPool * pool)
484 GstTaskPrivate *priv;
486 g_return_if_fail (GST_IS_TASK (task));
487 g_return_if_fail (GST_IS_TASK_POOL (pool));
491 GST_OBJECT_LOCK (task);
492 if (priv->pool != pool) {
494 priv->pool = gst_object_ref (pool);
497 GST_OBJECT_UNLOCK (task);
500 gst_object_unref (old);
505 * gst_task_set_thread_callbacks:
506 * @task: The #GstTask to use
507 * @callbacks: a #GstTaskThreadCallbacks pointer
508 * @user_data: user data passed to the callbacks
509 * @notify: called when @user_data is no longer referenced
511 * Set callbacks which will be executed when a new thread is needed, the thread
512 * function is entered and left and when the thread is joined.
514 * By default a thread for @task will be created from a default thread pool.
516 * Objects can use custom GThreads or can perform additional configuration of
517 * the threads (such as changing the thread priority) by installing callbacks.
524 gst_task_set_thread_callbacks (GstTask * task,
525 GstTaskThreadCallbacks * callbacks, gpointer user_data,
526 GDestroyNotify notify)
528 GDestroyNotify old_notify;
530 g_return_if_fail (task != NULL);
531 g_return_if_fail (GST_IS_TASK (task));
532 g_return_if_fail (callbacks != NULL);
534 GST_OBJECT_LOCK (task);
535 old_notify = task->priv->thr_notify;
540 old_data = task->priv->thr_user_data;
542 task->priv->thr_user_data = NULL;
543 task->priv->thr_notify = NULL;
544 GST_OBJECT_UNLOCK (task);
546 old_notify (old_data);
548 GST_OBJECT_LOCK (task);
550 task->priv->thr_callbacks = *callbacks;
551 task->priv->thr_user_data = user_data;
552 task->priv->thr_notify = notify;
553 GST_OBJECT_UNLOCK (task);
557 * gst_task_get_state:
558 * @task: The #GstTask to query
560 * Get the current state of the task.
562 * Returns: The #GstTaskState of the task
567 gst_task_get_state (GstTask * task)
571 g_return_val_if_fail (GST_IS_TASK (task), GST_TASK_STOPPED);
573 GST_OBJECT_LOCK (task);
574 result = task->state;
575 GST_OBJECT_UNLOCK (task);
580 /* make sure the task is running and start a thread if it's not.
581 * This function must be called with the task LOCK. */
583 start_task (GstTask * task)
586 GError *error = NULL;
587 GstTaskPrivate *priv;
591 /* new task, We ref before so that it remains alive while
592 * the thread is running. */
593 gst_object_ref (task);
594 /* mark task as running so that a join will wait until we schedule
595 * and exit the task function. */
596 task->running = TRUE;
598 /* push on the thread pool, we remember the original pool because the user
599 * could change it later on and then we join to the wrong pool. */
600 priv->pool_id = gst_object_ref (priv->pool);
602 gst_task_pool_push (priv->pool_id, (GstTaskPoolFunction) gst_task_func,
606 g_warning ("failed to create thread: %s", error->message);
607 g_error_free (error);
615 * gst_task_set_state:
617 * @state: the new task state
619 * Sets the state of @task to @state.
621 * The @task must have a lock associated with it using
622 * gst_task_set_lock() when going to GST_TASK_STARTED or GST_TASK_PAUSED or
623 * this function will return %FALSE.
627 * Returns: %TRUE if the state could be changed.
632 gst_task_set_state (GstTask * task, GstTaskState state)
637 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
639 GST_DEBUG_OBJECT (task, "Changing task %p to state %d", task, state);
641 GST_OBJECT_LOCK (task);
642 if (state != GST_TASK_STOPPED)
643 if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
646 /* if the state changed, do our thing */
651 case GST_TASK_STOPPED:
652 /* If the task already has a thread scheduled we don't have to do
654 if (G_UNLIKELY (!task->running))
655 res = start_task (task);
657 case GST_TASK_PAUSED:
658 /* when we are paused, signal to go to the new state */
659 GST_TASK_SIGNAL (task);
661 case GST_TASK_STARTED:
662 /* if we were started, we'll go to the new state after the next
667 GST_OBJECT_UNLOCK (task);
674 GST_WARNING_OBJECT (task, "state %d set on task without a lock", state);
675 GST_OBJECT_UNLOCK (task);
676 g_warning ("task without a lock can't be set to state %d", state);
683 * @task: The #GstTask to start
685 * Starts @task. The @task must have a lock associated with it using
686 * gst_task_set_lock() or this function will return %FALSE.
688 * Returns: %TRUE if the task could be started.
693 gst_task_start (GstTask * task)
695 return gst_task_set_state (task, GST_TASK_STARTED);
700 * @task: The #GstTask to stop
702 * Stops @task. This method merely schedules the task to stop and
703 * will not wait for the task to have completely stopped. Use
704 * gst_task_join() to stop and wait for completion.
706 * Returns: %TRUE if the task could be stopped.
711 gst_task_stop (GstTask * task)
713 return gst_task_set_state (task, GST_TASK_STOPPED);
718 * @task: The #GstTask to pause
720 * Pauses @task. This method can also be called on a task in the
721 * stopped state, in which case a thread will be started and will remain
722 * in the paused state. This function does not wait for the task to complete
725 * Returns: %TRUE if the task could be paused.
730 gst_task_pause (GstTask * task)
732 return gst_task_set_state (task, GST_TASK_PAUSED);
737 * @task: The #GstTask to join
739 * Joins @task. After this call, it is safe to unref the task
740 * and clean up the lock set with gst_task_set_lock().
742 * The task will automatically be stopped with this call.
744 * This function cannot be called from within a task function as this
745 * would cause a deadlock. The function will detect this and print a
748 * Returns: %TRUE if the task could be joined.
753 gst_task_join (GstTask * task)
756 GstTaskPrivate *priv;
758 GstTaskPool *pool = NULL;
762 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
764 tself = g_thread_self ();
766 GST_DEBUG_OBJECT (task, "Joining task %p, thread %p", task, tself);
768 /* we don't use a real thread join here because we are using
770 GST_OBJECT_LOCK (task);
771 if (G_UNLIKELY (tself == task->abidata.ABI.thread))
773 task->state = GST_TASK_STOPPED;
774 /* signal the state change for when it was blocked in PAUSED. */
775 GST_TASK_SIGNAL (task);
776 /* we set the running flag when pushing the task on the thread pool.
777 * This means that the task function might not be called when we try
778 * to join it here. */
779 while (G_LIKELY (task->running))
780 GST_TASK_WAIT (task);
781 /* clean the thread */
782 task->abidata.ABI.thread = NULL;
783 /* get the id and pool to join */
784 pool = priv->pool_id;
786 priv->pool_id = NULL;
788 GST_OBJECT_UNLOCK (task);
792 gst_task_pool_join (pool, id);
793 gst_object_unref (pool);
796 GST_DEBUG_OBJECT (task, "Joined task %p", task);
803 GST_WARNING_OBJECT (task, "trying to join task from its thread");
804 GST_OBJECT_UNLOCK (task);
805 g_warning ("\nTrying to join task %p from its thread would deadlock.\n"
806 "You cannot change the state of an element from its streaming\n"
807 "thread. Use g_idle_add() or post a GstMessage on the bus to\n"
808 "schedule the state change from the main thread.\n", task);