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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, 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 #GRecMutex 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_new(). 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 when 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 purposes, 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.
69 #include "gst_private.h"
73 #include "glib-compat-private.h"
77 #ifdef HAVE_SYS_PRCTL_H
78 #include <sys/prctl.h>
81 #ifdef HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID
85 GST_DEBUG_CATEGORY_STATIC (task_debug);
86 #define GST_CAT_DEFAULT (task_debug)
88 #define SET_TASK_STATE(t,s) (g_atomic_int_set (&GST_TASK_STATE(t), (s)))
89 #define GET_TASK_STATE(t) ((GstTaskState) g_atomic_int_get (&GST_TASK_STATE(t)))
91 #define GST_TASK_GET_PRIVATE(obj) \
92 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_TASK, GstTaskPrivate))
94 struct _GstTaskPrivate
96 /* callbacks for managing the thread of this task */
97 GstTaskThreadFunc enter_func;
98 gpointer enter_user_data;
99 GDestroyNotify enter_notify;
101 GstTaskThreadFunc leave_func;
102 gpointer leave_user_data;
103 GDestroyNotify leave_notify;
105 /* configured pool */
108 /* remember the pool and id that is currently running. */
110 GstTaskPool *pool_id;
114 #define WIN32_LEAN_AND_MEAN
117 struct _THREADNAME_INFO
119 DWORD dwType; // must be 0x1000
120 LPCSTR szName; // pointer to name (in user addr space)
121 DWORD dwThreadID; // thread ID (-1=caller thread)
122 DWORD dwFlags; // reserved for future use, must be zero
124 typedef struct _THREADNAME_INFO THREADNAME_INFO;
127 SetThreadName (DWORD dwThreadID, LPCSTR szThreadName)
129 THREADNAME_INFO info;
130 info.dwType = 0x1000;
131 info.szName = szThreadName;
132 info.dwThreadID = dwThreadID;
136 RaiseException (0x406D1388, 0, sizeof (info) / sizeof (DWORD),
139 __except (EXCEPTION_CONTINUE_EXECUTION) {
144 static void gst_task_finalize (GObject * object);
146 static void gst_task_func (GstTask * task);
148 static GMutex pool_lock;
152 GST_DEBUG_CATEGORY_INIT (task_debug, "task", 0, "Processing tasks"); \
155 G_DEFINE_TYPE_WITH_CODE (GstTask, gst_task, GST_TYPE_OBJECT, _do_init);
158 init_klass_pool (GstTaskClass * klass)
160 g_mutex_lock (&pool_lock);
162 gst_task_pool_cleanup (klass->pool);
163 gst_object_unref (klass->pool);
165 klass->pool = gst_task_pool_new ();
166 gst_task_pool_prepare (klass->pool, NULL);
167 g_mutex_unlock (&pool_lock);
171 gst_task_class_init (GstTaskClass * klass)
173 GObjectClass *gobject_class;
175 gobject_class = (GObjectClass *) klass;
177 g_type_class_add_private (klass, sizeof (GstTaskPrivate));
179 gobject_class->finalize = gst_task_finalize;
181 init_klass_pool (klass);
185 gst_task_init (GstTask * task)
189 klass = GST_TASK_GET_CLASS (task);
191 task->priv = GST_TASK_GET_PRIVATE (task);
192 task->running = FALSE;
195 g_cond_init (&task->cond);
196 SET_TASK_STATE (task, GST_TASK_STOPPED);
198 /* use the default klass pool for this task, users can
199 * override this later */
200 g_mutex_lock (&pool_lock);
201 task->priv->pool = gst_object_ref (klass->pool);
202 g_mutex_unlock (&pool_lock);
204 /* clear floating flag */
205 gst_object_ref_sink (task);
209 gst_task_finalize (GObject * object)
211 GstTask *task = GST_TASK (object);
212 GstTaskPrivate *priv = task->priv;
214 GST_DEBUG ("task %p finalize", task);
216 if (priv->enter_notify)
217 priv->enter_notify (priv->enter_user_data);
219 if (priv->leave_notify)
220 priv->leave_notify (priv->leave_user_data);
223 task->notify (task->user_data);
225 gst_object_unref (priv->pool);
227 /* task thread cannot be running here since it holds a ref
228 * to the task so that the finalize could not have happened */
229 g_cond_clear (&task->cond);
231 G_OBJECT_CLASS (gst_task_parent_class)->finalize (object);
234 /* should be called with the object LOCK */
236 gst_task_configure_name (GstTask * task)
238 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME)
240 gchar thread_name[17] = { 0, };
242 GST_OBJECT_LOCK (task);
243 name = GST_OBJECT_NAME (task);
245 /* set the thread name to something easily identifiable */
246 if (!snprintf (thread_name, 17, "%s", GST_STR_NULL (name))) {
247 GST_DEBUG_OBJECT (task, "Could not create thread name for '%s'", name);
249 GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", thread_name);
250 if (prctl (PR_SET_NAME, (unsigned long int) thread_name, 0, 0, 0))
251 GST_DEBUG_OBJECT (task, "Failed to set thread name");
253 GST_OBJECT_UNLOCK (task);
254 #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
257 GST_OBJECT_LOCK (task);
258 name = GST_OBJECT_NAME (task);
260 /* set the thread name to something easily identifiable */
261 GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", name);
262 if (pthread_setname_np (name))
263 GST_DEBUG_OBJECT (task, "Failed to set thread name");
265 GST_OBJECT_UNLOCK (task);
266 #elif defined (_MSC_VER)
268 name = GST_OBJECT_NAME (task);
270 /* set the thread name to something easily identifiable */
271 GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", name);
272 SetThreadName (-1, name);
277 gst_task_func (GstTask * task)
281 GstTaskPrivate *priv;
285 tself = g_thread_self ();
287 GST_DEBUG ("Entering task %p, thread %p", task, tself);
289 /* we have to grab the lock to get the mutex. We also
290 * mark our state running so that nobody can mess with
292 GST_OBJECT_LOCK (task);
293 if (GET_TASK_STATE (task) == GST_TASK_STOPPED)
295 lock = GST_TASK_GET_LOCK (task);
296 if (G_UNLIKELY (lock == NULL))
298 task->thread = tself;
299 GST_OBJECT_UNLOCK (task);
301 /* fire the enter_func callback when we need to */
302 if (priv->enter_func)
303 priv->enter_func (task, tself, priv->enter_user_data);
305 /* locking order is TASK_LOCK, LOCK */
306 g_rec_mutex_lock (lock);
307 /* configure the thread name now */
308 gst_task_configure_name (task);
310 while (G_LIKELY (GET_TASK_STATE (task) != GST_TASK_STOPPED)) {
311 GST_OBJECT_LOCK (task);
312 while (G_UNLIKELY (GST_TASK_STATE (task) == GST_TASK_PAUSED)) {
313 g_rec_mutex_unlock (lock);
315 GST_TASK_SIGNAL (task);
316 GST_INFO_OBJECT (task, "Task going to paused");
317 GST_TASK_WAIT (task);
318 GST_INFO_OBJECT (task, "Task resume from paused");
319 GST_OBJECT_UNLOCK (task);
320 /* locking order.. */
321 g_rec_mutex_lock (lock);
322 GST_OBJECT_LOCK (task);
325 if (G_UNLIKELY (GET_TASK_STATE (task) == GST_TASK_STOPPED)) {
326 GST_OBJECT_UNLOCK (task);
329 GST_OBJECT_UNLOCK (task);
332 task->func (task->user_data);
335 g_rec_mutex_unlock (lock);
337 GST_OBJECT_LOCK (task);
341 if (priv->leave_func) {
342 /* fire the leave_func callback when we need to. We need to do this before
343 * we signal the task and with the task lock released. */
344 GST_OBJECT_UNLOCK (task);
345 priv->leave_func (task, tself, priv->leave_user_data);
346 GST_OBJECT_LOCK (task);
348 /* now we allow messing with the lock again by setting the running flag to
349 * %FALSE. Together with the SIGNAL this is the sign for the _join() to
351 * Note that we still have not dropped the final ref on the task. We could
352 * check here if there is a pending join() going on and drop the last ref
353 * before releasing the lock as we can be sure that a ref is held by the
354 * caller of the join(). */
355 task->running = FALSE;
356 GST_TASK_SIGNAL (task);
357 GST_OBJECT_UNLOCK (task);
359 GST_DEBUG ("Exit task %p, thread %p", task, g_thread_self ());
361 gst_object_unref (task);
366 g_warning ("starting task without a lock");
372 * gst_task_cleanup_all:
374 * Wait for all tasks to be stopped. This is mainly used internally
375 * to ensure proper cleanup of internal data structures in test suites.
380 gst_task_cleanup_all (void)
384 if ((klass = g_type_class_peek (GST_TYPE_TASK))) {
385 init_klass_pool (klass);
391 * @func: The #GstTaskFunction to use
392 * @user_data: User data to pass to @func
393 * @notify: the function to call when @user_data is no longer needed.
395 * Create a new Task that will repeatedly call the provided @func
396 * with @user_data as a parameter. Typically the task will run in
399 * The function cannot be changed after the task has been created. You
400 * must create a new #GstTask to change the function.
402 * This function will not yet create and start a thread. Use gst_task_start() or
403 * gst_task_pause() to create and start the GThread.
405 * Before the task can be used, a #GRecMutex must be configured using the
406 * gst_task_set_lock() function. This lock will always be acquired while
409 * Returns: (transfer full): A new #GstTask.
414 gst_task_new (GstTaskFunction func, gpointer user_data, GDestroyNotify notify)
418 g_return_val_if_fail (func != NULL, NULL);
420 task = g_object_newv (GST_TYPE_TASK, 0, NULL);
422 task->user_data = user_data;
423 task->notify = notify;
425 GST_DEBUG ("Created task %p", task);
432 * @task: The #GstTask to use
433 * @mutex: The #GRecMutex to use
435 * Set the mutex used by the task. The mutex will be acquired before
436 * calling the #GstTaskFunction.
438 * This function has to be called before calling gst_task_pause() or
444 gst_task_set_lock (GstTask * task, GRecMutex * mutex)
446 g_return_if_fail (GST_IS_TASK (task));
448 GST_OBJECT_LOCK (task);
449 if (G_UNLIKELY (task->running))
451 GST_INFO ("setting stream lock %p on task %p", mutex, task);
452 GST_TASK_GET_LOCK (task) = mutex;
453 GST_OBJECT_UNLOCK (task);
460 GST_OBJECT_UNLOCK (task);
461 g_warning ("cannot call set_lock on a running task");
469 * Get the #GstTaskPool that this task will use for its streaming
474 * Returns: (transfer full): the #GstTaskPool used by @task. gst_object_unref()
478 gst_task_get_pool (GstTask * task)
481 GstTaskPrivate *priv;
483 g_return_val_if_fail (GST_IS_TASK (task), NULL);
487 GST_OBJECT_LOCK (task);
488 result = gst_object_ref (priv->pool);
489 GST_OBJECT_UNLOCK (task);
497 * @pool: (transfer none): a #GstTaskPool
499 * Set @pool as the new GstTaskPool for @task. Any new streaming threads that
500 * will be created by @task will now use @pool.
505 gst_task_set_pool (GstTask * task, GstTaskPool * pool)
508 GstTaskPrivate *priv;
510 g_return_if_fail (GST_IS_TASK (task));
511 g_return_if_fail (GST_IS_TASK_POOL (pool));
515 GST_OBJECT_LOCK (task);
516 if (priv->pool != pool) {
518 priv->pool = gst_object_ref (pool);
521 GST_OBJECT_UNLOCK (task);
524 gst_object_unref (old);
528 * gst_task_set_enter_callback:
529 * @task: The #GstTask to use
530 * @enter_func: (in): a #GstTaskThreadFunc
531 * @user_data: user data passed to @enter_func
532 * @notify: called when @user_data is no longer referenced
534 * Call @enter_func when the task function of @task is entered. @user_data will
535 * be passed to @enter_func and @notify will be called when @user_data is no
539 gst_task_set_enter_callback (GstTask * task, GstTaskThreadFunc enter_func,
540 gpointer user_data, GDestroyNotify notify)
542 GDestroyNotify old_notify;
544 g_return_if_fail (task != NULL);
545 g_return_if_fail (GST_IS_TASK (task));
547 GST_OBJECT_LOCK (task);
548 if ((old_notify = task->priv->enter_notify)) {
549 gpointer old_data = task->priv->enter_user_data;
551 task->priv->enter_user_data = NULL;
552 task->priv->enter_notify = NULL;
553 GST_OBJECT_UNLOCK (task);
555 old_notify (old_data);
557 GST_OBJECT_LOCK (task);
559 task->priv->enter_func = enter_func;
560 task->priv->enter_user_data = user_data;
561 task->priv->enter_notify = notify;
562 GST_OBJECT_UNLOCK (task);
566 * gst_task_set_leave_callback:
567 * @task: The #GstTask to use
568 * @leave_func: (in): a #GstTaskThreadFunc
569 * @user_data: user data passed to @leave_func
570 * @notify: called when @user_data is no longer referenced
572 * Call @leave_func when the task function of @task is left. @user_data will
573 * be passed to @leave_func and @notify will be called when @user_data is no
577 gst_task_set_leave_callback (GstTask * task, GstTaskThreadFunc leave_func,
578 gpointer user_data, GDestroyNotify notify)
580 GDestroyNotify old_notify;
582 g_return_if_fail (task != NULL);
583 g_return_if_fail (GST_IS_TASK (task));
585 GST_OBJECT_LOCK (task);
586 if ((old_notify = task->priv->leave_notify)) {
587 gpointer old_data = task->priv->leave_user_data;
589 task->priv->leave_user_data = NULL;
590 task->priv->leave_notify = NULL;
591 GST_OBJECT_UNLOCK (task);
593 old_notify (old_data);
595 GST_OBJECT_LOCK (task);
597 task->priv->leave_func = leave_func;
598 task->priv->leave_user_data = user_data;
599 task->priv->leave_notify = notify;
600 GST_OBJECT_UNLOCK (task);
604 * gst_task_get_state:
605 * @task: The #GstTask to query
607 * Get the current state of the task.
609 * Returns: The #GstTaskState of the task
614 gst_task_get_state (GstTask * task)
618 g_return_val_if_fail (GST_IS_TASK (task), GST_TASK_STOPPED);
620 result = GET_TASK_STATE (task);
625 /* make sure the task is running and start a thread if it's not.
626 * This function must be called with the task LOCK. */
628 start_task (GstTask * task)
631 GError *error = NULL;
632 GstTaskPrivate *priv;
636 /* new task, We ref before so that it remains alive while
637 * the thread is running. */
638 gst_object_ref (task);
639 /* mark task as running so that a join will wait until we schedule
640 * and exit the task function. */
641 task->running = TRUE;
643 /* push on the thread pool, we remember the original pool because the user
644 * could change it later on and then we join to the wrong pool. */
645 priv->pool_id = gst_object_ref (priv->pool);
647 gst_task_pool_push (priv->pool_id, (GstTaskPoolFunction) gst_task_func,
651 g_warning ("failed to create thread: %s", error->message);
652 g_error_free (error);
660 * gst_task_set_state:
662 * @state: the new task state
664 * Sets the state of @task to @state.
666 * The @task must have a lock associated with it using
667 * gst_task_set_lock() when going to GST_TASK_STARTED or GST_TASK_PAUSED or
668 * this function will return %FALSE.
672 * Returns: %TRUE if the state could be changed.
675 gst_task_set_state (GstTask * task, GstTaskState state)
680 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
682 GST_DEBUG_OBJECT (task, "Changing task %p to state %d", task, state);
684 GST_OBJECT_LOCK (task);
685 if (state != GST_TASK_STOPPED)
686 if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
689 /* if the state changed, do our thing */
690 old = GET_TASK_STATE (task);
692 SET_TASK_STATE (task, state);
694 case GST_TASK_STOPPED:
695 /* If the task already has a thread scheduled we don't have to do
697 if (G_UNLIKELY (!task->running))
698 res = start_task (task);
700 case GST_TASK_PAUSED:
701 /* when we are paused, signal to go to the new state */
702 GST_TASK_SIGNAL (task);
704 case GST_TASK_STARTED:
705 /* if we were started, we'll go to the new state after the next
710 GST_OBJECT_UNLOCK (task);
717 GST_WARNING_OBJECT (task, "state %d set on task without a lock", state);
718 GST_OBJECT_UNLOCK (task);
719 g_warning ("task without a lock can't be set to state %d", state);
726 * @task: The #GstTask to start
728 * Starts @task. The @task must have a lock associated with it using
729 * gst_task_set_lock() or this function will return %FALSE.
731 * Returns: %TRUE if the task could be started.
736 gst_task_start (GstTask * task)
738 return gst_task_set_state (task, GST_TASK_STARTED);
743 * @task: The #GstTask to stop
745 * Stops @task. This method merely schedules the task to stop and
746 * will not wait for the task to have completely stopped. Use
747 * gst_task_join() to stop and wait for completion.
749 * Returns: %TRUE if the task could be stopped.
754 gst_task_stop (GstTask * task)
756 return gst_task_set_state (task, GST_TASK_STOPPED);
761 * @task: The #GstTask to pause
763 * Pauses @task. This method can also be called on a task in the
764 * stopped state, in which case a thread will be started and will remain
765 * in the paused state. This function does not wait for the task to complete
768 * Returns: %TRUE if the task could be paused.
773 gst_task_pause (GstTask * task)
775 return gst_task_set_state (task, GST_TASK_PAUSED);
780 * @task: The #GstTask to join
782 * Joins @task. After this call, it is safe to unref the task
783 * and clean up the lock set with gst_task_set_lock().
785 * The task will automatically be stopped with this call.
787 * This function cannot be called from within a task function as this
788 * would cause a deadlock. The function will detect this and print a
791 * Returns: %TRUE if the task could be joined.
796 gst_task_join (GstTask * task)
799 GstTaskPrivate *priv;
801 GstTaskPool *pool = NULL;
803 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
807 tself = g_thread_self ();
809 GST_DEBUG_OBJECT (task, "Joining task %p, thread %p", task, tself);
811 /* we don't use a real thread join here because we are using
813 GST_OBJECT_LOCK (task);
814 if (G_UNLIKELY (tself == task->thread))
816 SET_TASK_STATE (task, GST_TASK_STOPPED);
817 /* signal the state change for when it was blocked in PAUSED. */
818 GST_TASK_SIGNAL (task);
819 /* we set the running flag when pushing the task on the thread pool.
820 * This means that the task function might not be called when we try
821 * to join it here. */
822 while (G_LIKELY (task->running))
823 GST_TASK_WAIT (task);
824 /* clean the thread */
826 /* get the id and pool to join */
827 pool = priv->pool_id;
829 priv->pool_id = NULL;
831 GST_OBJECT_UNLOCK (task);
835 gst_task_pool_join (pool, id);
836 gst_object_unref (pool);
839 GST_DEBUG_OBJECT (task, "Joined task %p", task);
846 GST_WARNING_OBJECT (task, "trying to join task from its thread");
847 GST_OBJECT_UNLOCK (task);
848 g_warning ("\nTrying to join task %p from its thread would deadlock.\n"
849 "You cannot change the state of an element from its streaming\n"
850 "thread. Use g_idle_add() or post a GstMessage on the bus to\n"
851 "schedule the state change from the main thread.\n", task);