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_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.
68 * Last reviewed on 2012-03-29 (0.11.3)
71 #include "gst_private.h"
75 #include "glib-compat-private.h"
79 #ifdef HAVE_SYS_PRCTL_H
80 #include <sys/prctl.h>
83 GST_DEBUG_CATEGORY_STATIC (task_debug);
84 #define GST_CAT_DEFAULT (task_debug)
86 #define SET_TASK_STATE(t,s) (g_atomic_int_set (&GST_TASK_STATE(t), (s)))
87 #define GET_TASK_STATE(t) ((GstTaskState) g_atomic_int_get (&GST_TASK_STATE(t)))
89 #define GST_TASK_GET_PRIVATE(obj) \
90 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_TASK, GstTaskPrivate))
92 struct _GstTaskPrivate
94 /* callbacks for managing the thread of this task */
95 GstTaskThreadCallbacks thr_callbacks;
96 gpointer thr_user_data;
97 GDestroyNotify thr_notify;
102 /* remember the pool and id that is currently running. */
104 GstTaskPool *pool_id;
110 struct _THREADNAME_INFO
112 DWORD dwType; // must be 0x1000
113 LPCSTR szName; // pointer to name (in user addr space)
114 DWORD dwThreadID; // thread ID (-1=caller thread)
115 DWORD dwFlags; // reserved for future use, must be zero
117 typedef struct _THREADNAME_INFO THREADNAME_INFO;
120 SetThreadName (DWORD dwThreadID, LPCSTR szThreadName)
122 THREADNAME_INFO info;
123 info.dwType = 0x1000;
124 info.szName = szThreadName;
125 info.dwThreadID = dwThreadID;
129 RaiseException (0x406D1388, 0, sizeof (info) / sizeof (DWORD),
132 __except (EXCEPTION_CONTINUE_EXECUTION) {
137 static void gst_task_finalize (GObject * object);
139 static void gst_task_func (GstTask * task);
141 static GMutex pool_lock;
145 GST_DEBUG_CATEGORY_INIT (task_debug, "task", 0, "Processing tasks"); \
148 G_DEFINE_TYPE_WITH_CODE (GstTask, gst_task, GST_TYPE_OBJECT, _do_init);
151 init_klass_pool (GstTaskClass * klass)
153 g_mutex_lock (&pool_lock);
155 gst_task_pool_cleanup (klass->pool);
156 gst_object_unref (klass->pool);
158 klass->pool = gst_task_pool_new ();
159 gst_task_pool_prepare (klass->pool, NULL);
160 g_mutex_unlock (&pool_lock);
164 gst_task_class_init (GstTaskClass * klass)
166 GObjectClass *gobject_class;
168 gobject_class = (GObjectClass *) klass;
170 g_type_class_add_private (klass, sizeof (GstTaskPrivate));
172 gobject_class->finalize = gst_task_finalize;
174 init_klass_pool (klass);
178 gst_task_init (GstTask * task)
182 klass = GST_TASK_GET_CLASS (task);
184 task->priv = GST_TASK_GET_PRIVATE (task);
185 task->running = FALSE;
188 g_cond_init (&task->cond);
189 SET_TASK_STATE (task, GST_TASK_STOPPED);
191 /* use the default klass pool for this task, users can
192 * override this later */
193 g_mutex_lock (&pool_lock);
194 task->priv->pool = gst_object_ref (klass->pool);
195 g_mutex_unlock (&pool_lock);
199 gst_task_finalize (GObject * object)
201 GstTask *task = GST_TASK (object);
202 GstTaskPrivate *priv = task->priv;
204 GST_DEBUG ("task %p finalize", task);
206 if (priv->thr_notify)
207 priv->thr_notify (priv->thr_user_data);
208 priv->thr_notify = NULL;
209 priv->thr_user_data = NULL;
211 gst_object_unref (priv->pool);
213 /* task thread cannot be running here since it holds a ref
214 * to the task so that the finalize could not have happened */
215 g_cond_clear (&task->cond);
217 G_OBJECT_CLASS (gst_task_parent_class)->finalize (object);
220 /* should be called with the object LOCK */
222 gst_task_configure_name (GstTask * task)
224 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME)
226 gchar thread_name[17] = { 0, };
228 GST_OBJECT_LOCK (task);
229 name = GST_OBJECT_NAME (task);
231 /* set the thread name to something easily identifiable */
232 if (!snprintf (thread_name, 17, "%s", GST_STR_NULL (name))) {
233 GST_DEBUG_OBJECT (task, "Could not create thread name for '%s'", name);
235 GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", thread_name);
236 if (prctl (PR_SET_NAME, (unsigned long int) thread_name, 0, 0, 0))
237 GST_DEBUG_OBJECT (task, "Failed to set thread name");
239 GST_OBJECT_UNLOCK (task);
243 name = GST_OBJECT_NAME (task);
245 /* set the thread name to something easily identifiable */
246 GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", name);
247 SetThreadName (-1, name);
252 gst_task_func (GstTask * task)
256 GstTaskPrivate *priv;
260 tself = g_thread_self ();
262 GST_DEBUG ("Entering task %p, thread %p", task, tself);
264 /* we have to grab the lock to get the mutex. We also
265 * mark our state running so that nobody can mess with
267 GST_OBJECT_LOCK (task);
268 if (GET_TASK_STATE (task) == GST_TASK_STOPPED)
270 lock = GST_TASK_GET_LOCK (task);
271 if (G_UNLIKELY (lock == NULL))
273 task->thread = tself;
274 GST_OBJECT_UNLOCK (task);
276 /* fire the enter_thread callback when we need to */
277 if (priv->thr_callbacks.enter_thread)
278 priv->thr_callbacks.enter_thread (task, tself, priv->thr_user_data);
280 /* locking order is TASK_LOCK, LOCK */
281 g_rec_mutex_lock (lock);
282 /* configure the thread name now */
283 gst_task_configure_name (task);
285 while (G_LIKELY (GET_TASK_STATE (task) != GST_TASK_STOPPED)) {
286 if (G_UNLIKELY (GET_TASK_STATE (task) == GST_TASK_PAUSED)) {
287 GST_OBJECT_LOCK (task);
288 while (G_UNLIKELY (GST_TASK_STATE (task) == GST_TASK_PAUSED)) {
289 g_rec_mutex_unlock (lock);
291 GST_TASK_SIGNAL (task);
292 GST_INFO_OBJECT (task, "Task going to paused");
293 GST_TASK_WAIT (task);
294 GST_INFO_OBJECT (task, "Task resume from paused");
295 GST_OBJECT_UNLOCK (task);
296 /* locking order.. */
297 g_rec_mutex_lock (lock);
299 GST_OBJECT_LOCK (task);
300 if (G_UNLIKELY (GET_TASK_STATE (task) == GST_TASK_STOPPED)) {
301 GST_OBJECT_UNLOCK (task);
305 GST_OBJECT_UNLOCK (task);
308 task->func (task->data);
311 g_rec_mutex_unlock (lock);
313 GST_OBJECT_LOCK (task);
317 if (priv->thr_callbacks.leave_thread) {
318 /* fire the leave_thread callback when we need to. We need to do this before
319 * we signal the task and with the task lock released. */
320 GST_OBJECT_UNLOCK (task);
321 priv->thr_callbacks.leave_thread (task, tself, priv->thr_user_data);
322 GST_OBJECT_LOCK (task);
324 /* now we allow messing with the lock again by setting the running flag to
325 * FALSE. Together with the SIGNAL this is the sign for the _join() to
327 * Note that we still have not dropped the final ref on the task. We could
328 * check here if there is a pending join() going on and drop the last ref
329 * before releasing the lock as we can be sure that a ref is held by the
330 * caller of the join(). */
331 task->running = FALSE;
332 GST_TASK_SIGNAL (task);
333 GST_OBJECT_UNLOCK (task);
335 GST_DEBUG ("Exit task %p, thread %p", task, g_thread_self ());
337 gst_object_unref (task);
342 g_warning ("starting task without a lock");
348 * gst_task_cleanup_all:
350 * Wait for all tasks to be stopped. This is mainly used internally
351 * to ensure proper cleanup of internal data structures in test suites.
356 gst_task_cleanup_all (void)
360 if ((klass = g_type_class_peek (GST_TYPE_TASK))) {
361 init_klass_pool (klass);
367 * @func: The #GstTaskFunction to use
368 * @data: (closure): User data to pass to @func
370 * Create a new Task that will repeatedly call the provided @func
371 * with @data as a parameter. Typically the task will run in
374 * The function cannot be changed after the task has been created. You
375 * must create a new #GstTask to change the function.
377 * This function will not yet create and start a thread. Use gst_task_start() or
378 * gst_task_pause() to create and start the GThread.
380 * Before the task can be used, a #GStaticRecMutex must be configured using the
381 * gst_task_set_lock() function. This lock will always be acquired while
384 * Returns: (transfer full): A new #GstTask.
389 gst_task_new (GstTaskFunction func, gpointer data)
393 task = g_object_newv (GST_TYPE_TASK, 0, NULL);
397 GST_DEBUG ("Created task %p", task);
404 * @task: The #GstTask to use
405 * @mutex: The #GRecMutex to use
407 * Set the mutex used by the task. The mutex will be acquired before
408 * calling the #GstTaskFunction.
410 * This function has to be called before calling gst_task_pause() or
416 gst_task_set_lock (GstTask * task, GRecMutex * mutex)
418 GST_OBJECT_LOCK (task);
419 if (G_UNLIKELY (task->running))
421 GST_INFO ("setting stream lock %p on task %p", mutex, task);
422 GST_TASK_GET_LOCK (task) = mutex;
423 GST_OBJECT_UNLOCK (task);
430 GST_OBJECT_UNLOCK (task);
431 g_warning ("cannot call set_lock on a running task");
439 * Get the #GstTaskPool that this task will use for its streaming
444 * Returns: (transfer full): the #GstTaskPool used by @task. gst_object_unref()
450 gst_task_get_pool (GstTask * task)
453 GstTaskPrivate *priv;
455 g_return_val_if_fail (GST_IS_TASK (task), NULL);
459 GST_OBJECT_LOCK (task);
460 result = gst_object_ref (priv->pool);
461 GST_OBJECT_UNLOCK (task);
469 * @pool: (transfer none): a #GstTaskPool
471 * Set @pool as the new GstTaskPool for @task. Any new streaming threads that
472 * will be created by @task will now use @pool.
479 gst_task_set_pool (GstTask * task, GstTaskPool * pool)
482 GstTaskPrivate *priv;
484 g_return_if_fail (GST_IS_TASK (task));
485 g_return_if_fail (GST_IS_TASK_POOL (pool));
489 GST_OBJECT_LOCK (task);
490 if (priv->pool != pool) {
492 priv->pool = gst_object_ref (pool);
495 GST_OBJECT_UNLOCK (task);
498 gst_object_unref (old);
503 * gst_task_set_thread_callbacks:
504 * @task: The #GstTask to use
505 * @callbacks: (in): a #GstTaskThreadCallbacks pointer
506 * @user_data: (closure): user data passed to the callbacks
507 * @notify: called when @user_data is no longer referenced
509 * Set callbacks which will be executed when a new thread is needed, the thread
510 * function is entered and left and when the thread is joined.
512 * By default a thread for @task will be created from a default thread pool.
514 * Objects can use custom GThreads or can perform additional configuration of
515 * the threads (such as changing the thread priority) by installing callbacks.
522 gst_task_set_thread_callbacks (GstTask * task,
523 GstTaskThreadCallbacks * callbacks, gpointer user_data,
524 GDestroyNotify notify)
526 GDestroyNotify old_notify;
528 g_return_if_fail (task != NULL);
529 g_return_if_fail (GST_IS_TASK (task));
530 g_return_if_fail (callbacks != NULL);
532 GST_OBJECT_LOCK (task);
533 old_notify = task->priv->thr_notify;
538 old_data = task->priv->thr_user_data;
540 task->priv->thr_user_data = NULL;
541 task->priv->thr_notify = NULL;
542 GST_OBJECT_UNLOCK (task);
544 old_notify (old_data);
546 GST_OBJECT_LOCK (task);
548 task->priv->thr_callbacks = *callbacks;
549 task->priv->thr_user_data = user_data;
550 task->priv->thr_notify = notify;
551 GST_OBJECT_UNLOCK (task);
555 * gst_task_get_state:
556 * @task: The #GstTask to query
558 * Get the current state of the task.
560 * Returns: The #GstTaskState of the task
565 gst_task_get_state (GstTask * task)
569 g_return_val_if_fail (GST_IS_TASK (task), GST_TASK_STOPPED);
571 result = GET_TASK_STATE (task);
576 /* make sure the task is running and start a thread if it's not.
577 * This function must be called with the task LOCK. */
579 start_task (GstTask * task)
582 GError *error = NULL;
583 GstTaskPrivate *priv;
587 /* new task, We ref before so that it remains alive while
588 * the thread is running. */
589 gst_object_ref (task);
590 /* mark task as running so that a join will wait until we schedule
591 * and exit the task function. */
592 task->running = TRUE;
594 /* push on the thread pool, we remember the original pool because the user
595 * could change it later on and then we join to the wrong pool. */
596 priv->pool_id = gst_object_ref (priv->pool);
598 gst_task_pool_push (priv->pool_id, (GstTaskPoolFunction) gst_task_func,
602 g_warning ("failed to create thread: %s", error->message);
603 g_error_free (error);
611 * gst_task_set_state:
613 * @state: the new task state
615 * Sets the state of @task to @state.
617 * The @task must have a lock associated with it using
618 * gst_task_set_lock() when going to GST_TASK_STARTED or GST_TASK_PAUSED or
619 * this function will return %FALSE.
623 * Returns: %TRUE if the state could be changed.
628 gst_task_set_state (GstTask * task, GstTaskState state)
633 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
635 GST_DEBUG_OBJECT (task, "Changing task %p to state %d", task, state);
637 GST_OBJECT_LOCK (task);
638 if (state != GST_TASK_STOPPED)
639 if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
642 /* if the state changed, do our thing */
643 old = GET_TASK_STATE (task);
645 SET_TASK_STATE (task, state);
647 case GST_TASK_STOPPED:
648 /* If the task already has a thread scheduled we don't have to do
650 if (G_UNLIKELY (!task->running))
651 res = start_task (task);
653 case GST_TASK_PAUSED:
654 /* when we are paused, signal to go to the new state */
655 GST_TASK_SIGNAL (task);
657 case GST_TASK_STARTED:
658 /* if we were started, we'll go to the new state after the next
663 GST_OBJECT_UNLOCK (task);
670 GST_WARNING_OBJECT (task, "state %d set on task without a lock", state);
671 GST_OBJECT_UNLOCK (task);
672 g_warning ("task without a lock can't be set to state %d", state);
679 * @task: The #GstTask to start
681 * Starts @task. The @task must have a lock associated with it using
682 * gst_task_set_lock() or this function will return %FALSE.
684 * Returns: %TRUE if the task could be started.
689 gst_task_start (GstTask * task)
691 return gst_task_set_state (task, GST_TASK_STARTED);
696 * @task: The #GstTask to stop
698 * Stops @task. This method merely schedules the task to stop and
699 * will not wait for the task to have completely stopped. Use
700 * gst_task_join() to stop and wait for completion.
702 * Returns: %TRUE if the task could be stopped.
707 gst_task_stop (GstTask * task)
709 return gst_task_set_state (task, GST_TASK_STOPPED);
714 * @task: The #GstTask to pause
716 * Pauses @task. This method can also be called on a task in the
717 * stopped state, in which case a thread will be started and will remain
718 * in the paused state. This function does not wait for the task to complete
721 * Returns: %TRUE if the task could be paused.
726 gst_task_pause (GstTask * task)
728 return gst_task_set_state (task, GST_TASK_PAUSED);
733 * @task: The #GstTask to join
735 * Joins @task. After this call, it is safe to unref the task
736 * and clean up the lock set with gst_task_set_lock().
738 * The task will automatically be stopped with this call.
740 * This function cannot be called from within a task function as this
741 * would cause a deadlock. The function will detect this and print a
744 * Returns: %TRUE if the task could be joined.
749 gst_task_join (GstTask * task)
752 GstTaskPrivate *priv;
754 GstTaskPool *pool = NULL;
758 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
760 tself = g_thread_self ();
762 GST_DEBUG_OBJECT (task, "Joining task %p, thread %p", task, tself);
764 /* we don't use a real thread join here because we are using
766 GST_OBJECT_LOCK (task);
767 if (G_UNLIKELY (tself == task->thread))
769 SET_TASK_STATE (task, GST_TASK_STOPPED);
770 /* signal the state change for when it was blocked in PAUSED. */
771 GST_TASK_SIGNAL (task);
772 /* we set the running flag when pushing the task on the thread pool.
773 * This means that the task function might not be called when we try
774 * to join it here. */
775 while (G_LIKELY (task->running))
776 GST_TASK_WAIT (task);
777 /* clean the thread */
779 /* get the id and pool to join */
780 pool = priv->pool_id;
782 priv->pool_id = NULL;
784 GST_OBJECT_UNLOCK (task);
788 gst_task_pool_join (pool, id);
789 gst_object_unref (pool);
792 GST_DEBUG_OBJECT (task, "Joined task %p", task);
799 GST_WARNING_OBJECT (task, "trying to join task from its thread");
800 GST_OBJECT_UNLOCK (task);
801 g_warning ("\nTrying to join task %p from its thread would deadlock.\n"
802 "You cannot change the state of an element from its streaming\n"
803 "thread. Use g_idle_add() or post a GstMessage on the bus to\n"
804 "schedule the state change from the main thread.\n", task);