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.
26 * @short_description: Abstraction of GStreamer streaming threads.
27 * @see_also: #GstElement, #GstPad
29 * #GstTask is used by #GstElement and #GstPad to provide the data passing
30 * threads in a #GstPipeline.
32 * A #GstPad will typically start a #GstTask to push or pull data to/from the
33 * peer pads. Most source elements start a #GstTask to push data. In some cases
34 * a demuxer element can start a #GstTask to pull data from a peer element. This
35 * is typically done when the demuxer can perform random access on the upstream
36 * peer element for improved performance.
38 * Although convenience functions exist on #GstPad to start/pause/stop tasks, it
39 * might sometimes be needed to create a #GstTask manually if it is not related to
42 * Before the #GstTask can be run, it needs a #GRecMutex that can be set with
43 * gst_task_set_lock().
45 * The task can be started, paused and stopped with gst_task_start(), gst_task_pause()
46 * and gst_task_stop() respectively or with the gst_task_set_state() function.
48 * A #GstTask will repeatedly call the #GstTaskFunction with the user data
49 * that was provided when creating the task with gst_task_new(). While calling
50 * the function it will acquire the provided lock. The provided lock is released
51 * when the task pauses or stops.
53 * Stopping a task with gst_task_stop() will not immediately make sure the task is
54 * not running anymore. Use gst_task_join() to make sure the task is completely
55 * stopped and the thread is stopped.
57 * After creating a #GstTask, use gst_object_unref() to free its resources. This can
58 * only be done when the task is not running anymore.
60 * Task functions can send a #GstMessage to send out-of-band data to the
61 * application. The application can receive messages from the #GstBus in its
64 * For debugging purposes, the task will configure its object name as the thread
65 * name on Linux. Please note that the object name should be configured before the
66 * task is started; changing the object name after the task has been started, has
67 * no effect on the thread name.
70 #include "gst_private.h"
74 #include "glib-compat-private.h"
78 #ifdef HAVE_SYS_PRCTL_H
79 #include <sys/prctl.h>
82 #ifdef HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID
86 GST_DEBUG_CATEGORY_STATIC (task_debug);
87 #define GST_CAT_DEFAULT (task_debug)
89 #define SET_TASK_STATE(t,s) (g_atomic_int_set (&GST_TASK_STATE(t), (s)))
90 #define GET_TASK_STATE(t) ((GstTaskState) g_atomic_int_get (&GST_TASK_STATE(t)))
92 #define GST_TASK_GET_PRIVATE(obj) \
93 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_TASK, GstTaskPrivate))
95 struct _GstTaskPrivate
97 /* callbacks for managing the thread of this task */
98 GstTaskThreadFunc enter_func;
99 gpointer enter_user_data;
100 GDestroyNotify enter_notify;
102 GstTaskThreadFunc leave_func;
103 gpointer leave_user_data;
104 GDestroyNotify leave_notify;
106 /* configured pool */
109 /* remember the pool and id that is currently running. */
111 GstTaskPool *pool_id;
115 #define WIN32_LEAN_AND_MEAN
118 struct _THREADNAME_INFO
120 DWORD dwType; // must be 0x1000
121 LPCSTR szName; // pointer to name (in user addr space)
122 DWORD dwThreadID; // thread ID (-1=caller thread)
123 DWORD dwFlags; // reserved for future use, must be zero
125 typedef struct _THREADNAME_INFO THREADNAME_INFO;
128 SetThreadName (DWORD dwThreadID, LPCSTR szThreadName)
130 THREADNAME_INFO info;
131 info.dwType = 0x1000;
132 info.szName = szThreadName;
133 info.dwThreadID = dwThreadID;
137 RaiseException (0x406D1388, 0, sizeof (info) / sizeof (DWORD),
138 (const ULONG_PTR *) &info);
140 __except (EXCEPTION_CONTINUE_EXECUTION) {
145 static void gst_task_finalize (GObject * object);
147 static void gst_task_func (GstTask * task);
149 static GMutex pool_lock;
153 GST_DEBUG_CATEGORY_INIT (task_debug, "task", 0, "Processing tasks"); \
156 G_DEFINE_TYPE_WITH_CODE (GstTask, gst_task, GST_TYPE_OBJECT, _do_init);
159 init_klass_pool (GstTaskClass * klass)
161 g_mutex_lock (&pool_lock);
163 gst_task_pool_cleanup (klass->pool);
164 gst_object_unref (klass->pool);
166 klass->pool = gst_task_pool_new ();
167 /* Classes are never destroyed so this ref will never be dropped */
168 GST_OBJECT_FLAG_SET (klass->pool, GST_OBJECT_FLAG_MAY_BE_LEAKED);
169 gst_task_pool_prepare (klass->pool, NULL);
170 g_mutex_unlock (&pool_lock);
174 gst_task_class_init (GstTaskClass * klass)
176 GObjectClass *gobject_class;
178 gobject_class = (GObjectClass *) klass;
180 g_type_class_add_private (klass, sizeof (GstTaskPrivate));
182 gobject_class->finalize = gst_task_finalize;
184 init_klass_pool (klass);
188 gst_task_init (GstTask * task)
192 klass = GST_TASK_GET_CLASS (task);
194 task->priv = GST_TASK_GET_PRIVATE (task);
195 task->running = FALSE;
198 g_cond_init (&task->cond);
199 SET_TASK_STATE (task, GST_TASK_STOPPED);
201 /* use the default klass pool for this task, users can
202 * override this later */
203 g_mutex_lock (&pool_lock);
204 task->priv->pool = gst_object_ref (klass->pool);
205 g_mutex_unlock (&pool_lock);
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);
388 /* GstElement owns a GThreadPool */
389 _priv_gst_element_cleanup ();
394 * @func: The #GstTaskFunction to use
395 * @user_data: User data to pass to @func
396 * @notify: the function to call when @user_data is no longer needed.
398 * Create a new Task that will repeatedly call the provided @func
399 * with @user_data as a parameter. Typically the task will run in
402 * The function cannot be changed after the task has been created. You
403 * must create a new #GstTask to change the function.
405 * This function will not yet create and start a thread. Use gst_task_start() or
406 * gst_task_pause() to create and start the GThread.
408 * Before the task can be used, a #GRecMutex must be configured using the
409 * gst_task_set_lock() function. This lock will always be acquired while
412 * Returns: (transfer full): A new #GstTask.
417 gst_task_new (GstTaskFunction func, gpointer user_data, GDestroyNotify notify)
421 g_return_val_if_fail (func != NULL, NULL);
423 task = g_object_new (GST_TYPE_TASK, NULL);
425 task->user_data = user_data;
426 task->notify = notify;
428 GST_DEBUG ("Created task %p", task);
430 /* clear floating flag */
431 gst_object_ref_sink (task);
438 * @task: The #GstTask to use
439 * @mutex: The #GRecMutex to use
441 * Set the mutex used by the task. The mutex will be acquired before
442 * calling the #GstTaskFunction.
444 * This function has to be called before calling gst_task_pause() or
450 gst_task_set_lock (GstTask * task, GRecMutex * mutex)
452 g_return_if_fail (GST_IS_TASK (task));
454 GST_OBJECT_LOCK (task);
455 if (G_UNLIKELY (task->running))
457 GST_INFO ("setting stream lock %p on task %p", mutex, task);
458 GST_TASK_GET_LOCK (task) = mutex;
459 GST_OBJECT_UNLOCK (task);
466 GST_OBJECT_UNLOCK (task);
467 g_warning ("cannot call set_lock on a running task");
475 * Get the #GstTaskPool that this task will use for its streaming
480 * Returns: (transfer full): the #GstTaskPool used by @task. gst_object_unref()
484 gst_task_get_pool (GstTask * task)
487 GstTaskPrivate *priv;
489 g_return_val_if_fail (GST_IS_TASK (task), NULL);
493 GST_OBJECT_LOCK (task);
494 result = gst_object_ref (priv->pool);
495 GST_OBJECT_UNLOCK (task);
503 * @pool: (transfer none): a #GstTaskPool
505 * Set @pool as the new GstTaskPool for @task. Any new streaming threads that
506 * will be created by @task will now use @pool.
511 gst_task_set_pool (GstTask * task, GstTaskPool * pool)
514 GstTaskPrivate *priv;
516 g_return_if_fail (GST_IS_TASK (task));
517 g_return_if_fail (GST_IS_TASK_POOL (pool));
521 GST_OBJECT_LOCK (task);
522 if (priv->pool != pool) {
524 priv->pool = gst_object_ref (pool);
527 GST_OBJECT_UNLOCK (task);
530 gst_object_unref (old);
534 * gst_task_set_enter_callback:
535 * @task: The #GstTask to use
536 * @enter_func: (in): a #GstTaskThreadFunc
537 * @user_data: user data passed to @enter_func
538 * @notify: called when @user_data is no longer referenced
540 * Call @enter_func when the task function of @task is entered. @user_data will
541 * be passed to @enter_func and @notify will be called when @user_data is no
545 gst_task_set_enter_callback (GstTask * task, GstTaskThreadFunc enter_func,
546 gpointer user_data, GDestroyNotify notify)
548 GDestroyNotify old_notify;
550 g_return_if_fail (task != NULL);
551 g_return_if_fail (GST_IS_TASK (task));
553 GST_OBJECT_LOCK (task);
554 if ((old_notify = task->priv->enter_notify)) {
555 gpointer old_data = task->priv->enter_user_data;
557 task->priv->enter_user_data = NULL;
558 task->priv->enter_notify = NULL;
559 GST_OBJECT_UNLOCK (task);
561 old_notify (old_data);
563 GST_OBJECT_LOCK (task);
565 task->priv->enter_func = enter_func;
566 task->priv->enter_user_data = user_data;
567 task->priv->enter_notify = notify;
568 GST_OBJECT_UNLOCK (task);
572 * gst_task_set_leave_callback:
573 * @task: The #GstTask to use
574 * @leave_func: (in): a #GstTaskThreadFunc
575 * @user_data: user data passed to @leave_func
576 * @notify: called when @user_data is no longer referenced
578 * Call @leave_func when the task function of @task is left. @user_data will
579 * be passed to @leave_func and @notify will be called when @user_data is no
583 gst_task_set_leave_callback (GstTask * task, GstTaskThreadFunc leave_func,
584 gpointer user_data, GDestroyNotify notify)
586 GDestroyNotify old_notify;
588 g_return_if_fail (task != NULL);
589 g_return_if_fail (GST_IS_TASK (task));
591 GST_OBJECT_LOCK (task);
592 if ((old_notify = task->priv->leave_notify)) {
593 gpointer old_data = task->priv->leave_user_data;
595 task->priv->leave_user_data = NULL;
596 task->priv->leave_notify = NULL;
597 GST_OBJECT_UNLOCK (task);
599 old_notify (old_data);
601 GST_OBJECT_LOCK (task);
603 task->priv->leave_func = leave_func;
604 task->priv->leave_user_data = user_data;
605 task->priv->leave_notify = notify;
606 GST_OBJECT_UNLOCK (task);
610 * gst_task_get_state:
611 * @task: The #GstTask to query
613 * Get the current state of the task.
615 * Returns: The #GstTaskState of the task
620 gst_task_get_state (GstTask * task)
624 g_return_val_if_fail (GST_IS_TASK (task), GST_TASK_STOPPED);
626 result = GET_TASK_STATE (task);
631 /* make sure the task is running and start a thread if it's not.
632 * This function must be called with the task LOCK. */
634 start_task (GstTask * task)
637 GError *error = NULL;
638 GstTaskPrivate *priv;
642 /* new task, We ref before so that it remains alive while
643 * the thread is running. */
644 gst_object_ref (task);
645 /* mark task as running so that a join will wait until we schedule
646 * and exit the task function. */
647 task->running = TRUE;
649 /* push on the thread pool, we remember the original pool because the user
650 * could change it later on and then we join to the wrong pool. */
651 priv->pool_id = gst_object_ref (priv->pool);
653 gst_task_pool_push (priv->pool_id, (GstTaskPoolFunction) gst_task_func,
657 g_warning ("failed to create thread: %s", error->message);
658 g_error_free (error);
666 * gst_task_set_state:
668 * @state: the new task state
670 * Sets the state of @task to @state.
672 * The @task must have a lock associated with it using
673 * gst_task_set_lock() when going to GST_TASK_STARTED or GST_TASK_PAUSED or
674 * this function will return %FALSE.
678 * Returns: %TRUE if the state could be changed.
681 gst_task_set_state (GstTask * task, GstTaskState state)
686 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
688 GST_DEBUG_OBJECT (task, "Changing task %p to state %d", task, state);
690 GST_OBJECT_LOCK (task);
691 if (state != GST_TASK_STOPPED)
692 if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
695 /* if the state changed, do our thing */
696 old = GET_TASK_STATE (task);
698 SET_TASK_STATE (task, state);
700 case GST_TASK_STOPPED:
701 /* If the task already has a thread scheduled we don't have to do
703 if (G_UNLIKELY (!task->running))
704 res = start_task (task);
706 case GST_TASK_PAUSED:
707 /* when we are paused, signal to go to the new state */
708 GST_TASK_SIGNAL (task);
710 case GST_TASK_STARTED:
711 /* if we were started, we'll go to the new state after the next
716 GST_OBJECT_UNLOCK (task);
723 GST_WARNING_OBJECT (task, "state %d set on task without a lock", state);
724 GST_OBJECT_UNLOCK (task);
725 g_warning ("task without a lock can't be set to state %d", state);
732 * @task: The #GstTask to start
734 * Starts @task. The @task must have a lock associated with it using
735 * gst_task_set_lock() or this function will return %FALSE.
737 * Returns: %TRUE if the task could be started.
742 gst_task_start (GstTask * task)
744 return gst_task_set_state (task, GST_TASK_STARTED);
749 * @task: The #GstTask to stop
751 * Stops @task. This method merely schedules the task to stop and
752 * will not wait for the task to have completely stopped. Use
753 * gst_task_join() to stop and wait for completion.
755 * Returns: %TRUE if the task could be stopped.
760 gst_task_stop (GstTask * task)
762 return gst_task_set_state (task, GST_TASK_STOPPED);
767 * @task: The #GstTask to pause
769 * Pauses @task. This method can also be called on a task in the
770 * stopped state, in which case a thread will be started and will remain
771 * in the paused state. This function does not wait for the task to complete
774 * Returns: %TRUE if the task could be paused.
779 gst_task_pause (GstTask * task)
781 return gst_task_set_state (task, GST_TASK_PAUSED);
786 * @task: The #GstTask to join
788 * Joins @task. After this call, it is safe to unref the task
789 * and clean up the lock set with gst_task_set_lock().
791 * The task will automatically be stopped with this call.
793 * This function cannot be called from within a task function as this
794 * would cause a deadlock. The function will detect this and print a
797 * Returns: %TRUE if the task could be joined.
802 gst_task_join (GstTask * task)
805 GstTaskPrivate *priv;
807 GstTaskPool *pool = NULL;
809 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
813 tself = g_thread_self ();
815 GST_DEBUG_OBJECT (task, "Joining task %p, thread %p", task, tself);
817 /* we don't use a real thread join here because we are using
819 GST_OBJECT_LOCK (task);
820 if (G_UNLIKELY (tself == task->thread))
822 SET_TASK_STATE (task, GST_TASK_STOPPED);
823 /* signal the state change for when it was blocked in PAUSED. */
824 GST_TASK_SIGNAL (task);
825 /* we set the running flag when pushing the task on the thread pool.
826 * This means that the task function might not be called when we try
827 * to join it here. */
828 while (G_LIKELY (task->running))
829 GST_TASK_WAIT (task);
830 /* clean the thread */
832 /* get the id and pool to join */
833 pool = priv->pool_id;
835 priv->pool_id = NULL;
837 GST_OBJECT_UNLOCK (task);
841 gst_task_pool_join (pool, id);
842 gst_object_unref (pool);
845 GST_DEBUG_OBJECT (task, "Joined task %p", task);
852 GST_WARNING_OBJECT (task, "trying to join task from its thread");
853 GST_OBJECT_UNLOCK (task);
854 g_warning ("\nTrying to join task %p from its thread would deadlock.\n"
855 "You cannot change the state of an element from its streaming\n"
856 "thread. Use g_idle_add() or post a GstMessage on the bus to\n"
857 "schedule the state change from the main thread.\n", task);