Merge remote-tracking branch 'origin/0.10'
[platform/upstream/gstreamer.git] / gst / gsttask.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gsttask.c: Streaming tasks
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:gsttask
25  * @short_description: Abstraction of GStreamer streaming threads.
26  * @see_also: #GstElement, #GstPad
27  *
28  * #GstTask is used by #GstElement and #GstPad to provide the data passing
29  * threads in a #GstPipeline.
30  *
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.
36  *
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
39  * a #GstPad.
40  *
41  * Before the #GstTask can be run, it needs a #GStaticRecMutex that can be set with
42  * gst_task_set_lock().
43  *
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.
46  *
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.
51  *
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.
55  *
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.
58  *
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
61  * mainloop.
62  *
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.
67  *
68  * Last reviewed on 2012-03-29 (0.11.3)
69  */
70
71 #include "gst_private.h"
72
73 #include "gstinfo.h"
74 #include "gsttask.h"
75 #include "glib-compat-private.h"
76
77 #include <stdio.h>
78
79 #ifdef HAVE_SYS_PRCTL_H
80 #include <sys/prctl.h>
81 #endif
82
83 GST_DEBUG_CATEGORY_STATIC (task_debug);
84 #define GST_CAT_DEFAULT (task_debug)
85
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)))
88
89 #define GST_TASK_GET_PRIVATE(obj)  \
90    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_TASK, GstTaskPrivate))
91
92 struct _GstTaskPrivate
93 {
94   /* callbacks for managing the thread of this task */
95   GstTaskThreadCallbacks thr_callbacks;
96   gpointer thr_user_data;
97   GDestroyNotify thr_notify;
98
99   gboolean prio_set;
100   GThreadPriority priority;
101
102   /* configured pool */
103   GstTaskPool *pool;
104
105   /* remember the pool and id that is currently running. */
106   gpointer id;
107   GstTaskPool *pool_id;
108 };
109
110 #ifdef _MSC_VER
111 #include <windows.h>
112
113 struct _THREADNAME_INFO
114 {
115   DWORD dwType;                 // must be 0x1000
116   LPCSTR szName;                // pointer to name (in user addr space)
117   DWORD dwThreadID;             // thread ID (-1=caller thread)
118   DWORD dwFlags;                // reserved for future use, must be zero
119 };
120 typedef struct _THREADNAME_INFO THREADNAME_INFO;
121
122 void
123 SetThreadName (DWORD dwThreadID, LPCSTR szThreadName)
124 {
125   THREADNAME_INFO info;
126   info.dwType = 0x1000;
127   info.szName = szThreadName;
128   info.dwThreadID = dwThreadID;
129   info.dwFlags = 0;
130
131   __try {
132     RaiseException (0x406D1388, 0, sizeof (info) / sizeof (DWORD),
133         (DWORD *) & info);
134   }
135   __except (EXCEPTION_CONTINUE_EXECUTION) {
136   }
137 }
138 #endif
139
140 static void gst_task_finalize (GObject * object);
141
142 static void gst_task_func (GstTask * task);
143
144 static GMutex pool_lock;
145
146 #define _do_init \
147 { \
148   GST_DEBUG_CATEGORY_INIT (task_debug, "task", 0, "Processing tasks"); \
149 }
150
151 G_DEFINE_TYPE_WITH_CODE (GstTask, gst_task, GST_TYPE_OBJECT, _do_init);
152
153 static void
154 init_klass_pool (GstTaskClass * klass)
155 {
156   g_mutex_lock (&pool_lock);
157   if (klass->pool) {
158     gst_task_pool_cleanup (klass->pool);
159     gst_object_unref (klass->pool);
160   }
161   klass->pool = gst_task_pool_new ();
162   gst_task_pool_prepare (klass->pool, NULL);
163   g_mutex_unlock (&pool_lock);
164 }
165
166 static void
167 gst_task_class_init (GstTaskClass * klass)
168 {
169   GObjectClass *gobject_class;
170
171   gobject_class = (GObjectClass *) klass;
172
173   g_type_class_add_private (klass, sizeof (GstTaskPrivate));
174
175   gobject_class->finalize = gst_task_finalize;
176
177   init_klass_pool (klass);
178 }
179
180 static void
181 gst_task_init (GstTask * task)
182 {
183   GstTaskClass *klass;
184
185   klass = GST_TASK_GET_CLASS (task);
186
187   task->priv = GST_TASK_GET_PRIVATE (task);
188   task->running = FALSE;
189   task->thread = NULL;
190   task->lock = NULL;
191   g_cond_init (&task->cond);
192   SET_TASK_STATE (task, GST_TASK_STOPPED);
193   task->priv->prio_set = FALSE;
194
195   /* use the default klass pool for this task, users can
196    * override this later */
197   g_mutex_lock (&pool_lock);
198   task->priv->pool = gst_object_ref (klass->pool);
199   g_mutex_unlock (&pool_lock);
200 }
201
202 static void
203 gst_task_finalize (GObject * object)
204 {
205   GstTask *task = GST_TASK (object);
206   GstTaskPrivate *priv = task->priv;
207
208   GST_DEBUG ("task %p finalize", task);
209
210   if (priv->thr_notify)
211     priv->thr_notify (priv->thr_user_data);
212   priv->thr_notify = NULL;
213   priv->thr_user_data = NULL;
214
215   gst_object_unref (priv->pool);
216
217   /* task thread cannot be running here since it holds a ref
218    * to the task so that the finalize could not have happened */
219   g_cond_clear (&task->cond);
220
221   G_OBJECT_CLASS (gst_task_parent_class)->finalize (object);
222 }
223
224 /* should be called with the object LOCK */
225 static void
226 gst_task_configure_name (GstTask * task)
227 {
228 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME)
229   const gchar *name;
230   gchar thread_name[17] = { 0, };
231
232   GST_OBJECT_LOCK (task);
233   name = GST_OBJECT_NAME (task);
234
235   /* set the thread name to something easily identifiable */
236   if (!snprintf (thread_name, 17, "%s", GST_STR_NULL (name))) {
237     GST_DEBUG_OBJECT (task, "Could not create thread name for '%s'", name);
238   } else {
239     GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", thread_name);
240     if (prctl (PR_SET_NAME, (unsigned long int) thread_name, 0, 0, 0))
241       GST_DEBUG_OBJECT (task, "Failed to set thread name");
242   }
243   GST_OBJECT_UNLOCK (task);
244 #endif
245 #ifdef _MSC_VER
246   const gchar *name;
247   name = GST_OBJECT_NAME (task);
248
249   /* set the thread name to something easily identifiable */
250   GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", name);
251   SetThreadName (-1, name);
252 #endif
253 }
254
255 static void
256 gst_task_func (GstTask * task)
257 {
258   GRecMutex *lock;
259   GThread *tself;
260   GstTaskPrivate *priv;
261
262   priv = task->priv;
263
264   tself = g_thread_self ();
265
266   GST_DEBUG ("Entering task %p, thread %p", task, tself);
267
268   /* we have to grab the lock to get the mutex. We also
269    * mark our state running so that nobody can mess with
270    * the mutex. */
271   GST_OBJECT_LOCK (task);
272   if (GET_TASK_STATE (task) == GST_TASK_STOPPED)
273     goto exit;
274   lock = GST_TASK_GET_LOCK (task);
275   if (G_UNLIKELY (lock == NULL))
276     goto no_lock;
277   task->thread = tself;
278   /* only update the priority when it was changed */
279   if (priv->prio_set) {
280     GST_INFO_OBJECT (task, "Thread priorities no longer have any effect");
281   }
282   GST_OBJECT_UNLOCK (task);
283
284   /* fire the enter_thread callback when we need to */
285   if (priv->thr_callbacks.enter_thread)
286     priv->thr_callbacks.enter_thread (task, tself, priv->thr_user_data);
287
288   /* locking order is TASK_LOCK, LOCK */
289   g_rec_mutex_lock (lock);
290   /* configure the thread name now */
291   gst_task_configure_name (task);
292
293   while (G_LIKELY (GET_TASK_STATE (task) != GST_TASK_STOPPED)) {
294     if (G_UNLIKELY (GET_TASK_STATE (task) == GST_TASK_PAUSED)) {
295       GST_OBJECT_LOCK (task);
296       while (G_UNLIKELY (GST_TASK_STATE (task) == GST_TASK_PAUSED)) {
297         g_rec_mutex_unlock (lock);
298
299         GST_TASK_SIGNAL (task);
300         GST_INFO_OBJECT (task, "Task going to paused");
301         GST_TASK_WAIT (task);
302         GST_INFO_OBJECT (task, "Task resume from paused");
303         GST_OBJECT_UNLOCK (task);
304         /* locking order.. */
305         g_rec_mutex_lock (lock);
306
307         GST_OBJECT_LOCK (task);
308         if (G_UNLIKELY (GET_TASK_STATE (task) == GST_TASK_STOPPED)) {
309           GST_OBJECT_UNLOCK (task);
310           goto done;
311         }
312       }
313       GST_OBJECT_UNLOCK (task);
314     }
315
316     task->func (task->data);
317   }
318 done:
319   g_rec_mutex_unlock (lock);
320
321   GST_OBJECT_LOCK (task);
322   task->thread = NULL;
323
324 exit:
325   if (priv->thr_callbacks.leave_thread) {
326     /* fire the leave_thread callback when we need to. We need to do this before
327      * we signal the task and with the task lock released. */
328     GST_OBJECT_UNLOCK (task);
329     priv->thr_callbacks.leave_thread (task, tself, priv->thr_user_data);
330     GST_OBJECT_LOCK (task);
331   }
332   /* now we allow messing with the lock again by setting the running flag to
333    * FALSE. Together with the SIGNAL this is the sign for the _join() to
334    * complete.
335    * Note that we still have not dropped the final ref on the task. We could
336    * check here if there is a pending join() going on and drop the last ref
337    * before releasing the lock as we can be sure that a ref is held by the
338    * caller of the join(). */
339   task->running = FALSE;
340   GST_TASK_SIGNAL (task);
341   GST_OBJECT_UNLOCK (task);
342
343   GST_DEBUG ("Exit task %p, thread %p", task, g_thread_self ());
344
345   gst_object_unref (task);
346   return;
347
348 no_lock:
349   {
350     g_warning ("starting task without a lock");
351     goto exit;
352   }
353 }
354
355 /**
356  * gst_task_cleanup_all:
357  *
358  * Wait for all tasks to be stopped. This is mainly used internally
359  * to ensure proper cleanup of internal data structures in test suites.
360  *
361  * MT safe.
362  */
363 void
364 gst_task_cleanup_all (void)
365 {
366   GstTaskClass *klass;
367
368   if ((klass = g_type_class_peek (GST_TYPE_TASK))) {
369     init_klass_pool (klass);
370   }
371 }
372
373 /**
374  * gst_task_new:
375  * @func: The #GstTaskFunction to use
376  * @data: (closure): User data to pass to @func
377  *
378  * Create a new Task that will repeatedly call the provided @func
379  * with @data as a parameter. Typically the task will run in
380  * a new thread.
381  *
382  * The function cannot be changed after the task has been created. You
383  * must create a new #GstTask to change the function.
384  *
385  * This function will not yet create and start a thread. Use gst_task_start() or
386  * gst_task_pause() to create and start the GThread.
387  *
388  * Before the task can be used, a #GStaticRecMutex must be configured using the
389  * gst_task_set_lock() function. This lock will always be acquired while
390  * @func is called.
391  *
392  * Returns: (transfer full): A new #GstTask.
393  *
394  * MT safe.
395  */
396 GstTask *
397 gst_task_new (GstTaskFunction func, gpointer data)
398 {
399   GstTask *task;
400
401   task = g_object_newv (GST_TYPE_TASK, 0, NULL);
402   task->func = func;
403   task->data = data;
404
405   GST_DEBUG ("Created task %p", task);
406
407   return task;
408 }
409
410 /**
411  * gst_task_set_lock:
412  * @task: The #GstTask to use
413  * @mutex: The #GRecMutex to use
414  *
415  * Set the mutex used by the task. The mutex will be acquired before
416  * calling the #GstTaskFunction.
417  *
418  * This function has to be called before calling gst_task_pause() or
419  * gst_task_start().
420  *
421  * MT safe.
422  */
423 void
424 gst_task_set_lock (GstTask * task, GRecMutex * mutex)
425 {
426   GST_OBJECT_LOCK (task);
427   if (G_UNLIKELY (task->running))
428     goto is_running;
429   GST_INFO ("setting stream lock %p on task %p", mutex, task);
430   GST_TASK_GET_LOCK (task) = mutex;
431   GST_OBJECT_UNLOCK (task);
432
433   return;
434
435   /* ERRORS */
436 is_running:
437   {
438     GST_OBJECT_UNLOCK (task);
439     g_warning ("cannot call set_lock on a running task");
440   }
441 }
442
443 /**
444  * gst_task_set_priority:
445  * @task: a #GstTask
446  * @priority: a new priority for @task
447  *
448  * Changes the priority of @task to @priority.
449  *
450  * Note: try not to depend on task priorities.
451  *
452  * MT safe.
453  *
454  * Since: 0.10.24
455  */
456 /* FIXME 0.11: remove gst_task_set_priority() */
457 void
458 gst_task_set_priority (GstTask * task, GThreadPriority priority)
459 {
460   GstTaskPrivate *priv;
461   GThread *thread;
462
463   g_return_if_fail (GST_IS_TASK (task));
464
465   priv = task->priv;
466
467   GST_OBJECT_LOCK (task);
468   priv->prio_set = TRUE;
469   priv->priority = priority;
470   thread = task->thread;
471   if (thread != NULL) {
472     /* if this task already has a thread, we can configure the priority right
473      * away, else we do that when we assign a thread to the task. */
474     GST_INFO_OBJECT (task, "Thread priorities no longer have any effect");
475   }
476   GST_OBJECT_UNLOCK (task);
477 }
478
479 /**
480  * gst_task_get_pool:
481  * @task: a #GstTask
482  *
483  * Get the #GstTaskPool that this task will use for its streaming
484  * threads.
485  *
486  * MT safe.
487  *
488  * Returns: (transfer full): the #GstTaskPool used by @task. gst_object_unref()
489  * after usage.
490  *
491  * Since: 0.10.24
492  */
493 GstTaskPool *
494 gst_task_get_pool (GstTask * task)
495 {
496   GstTaskPool *result;
497   GstTaskPrivate *priv;
498
499   g_return_val_if_fail (GST_IS_TASK (task), NULL);
500
501   priv = task->priv;
502
503   GST_OBJECT_LOCK (task);
504   result = gst_object_ref (priv->pool);
505   GST_OBJECT_UNLOCK (task);
506
507   return result;
508 }
509
510 /**
511  * gst_task_set_pool:
512  * @task: a #GstTask
513  * @pool: (transfer none): a #GstTaskPool
514  *
515  * Set @pool as the new GstTaskPool for @task. Any new streaming threads that
516  * will be created by @task will now use @pool.
517  *
518  * MT safe.
519  *
520  * Since: 0.10.24
521  */
522 void
523 gst_task_set_pool (GstTask * task, GstTaskPool * pool)
524 {
525   GstTaskPool *old;
526   GstTaskPrivate *priv;
527
528   g_return_if_fail (GST_IS_TASK (task));
529   g_return_if_fail (GST_IS_TASK_POOL (pool));
530
531   priv = task->priv;
532
533   GST_OBJECT_LOCK (task);
534   if (priv->pool != pool) {
535     old = priv->pool;
536     priv->pool = gst_object_ref (pool);
537   } else
538     old = NULL;
539   GST_OBJECT_UNLOCK (task);
540
541   if (old)
542     gst_object_unref (old);
543 }
544
545
546 /**
547  * gst_task_set_thread_callbacks:
548  * @task: The #GstTask to use
549  * @callbacks: (in): a #GstTaskThreadCallbacks pointer
550  * @user_data: (closure): user data passed to the callbacks
551  * @notify: called when @user_data is no longer referenced
552  *
553  * Set callbacks which will be executed when a new thread is needed, the thread
554  * function is entered and left and when the thread is joined.
555  *
556  * By default a thread for @task will be created from a default thread pool.
557  *
558  * Objects can use custom GThreads or can perform additional configuration of
559  * the threads (such as changing the thread priority) by installing callbacks.
560  *
561  * MT safe.
562  *
563  * Since: 0.10.24
564  */
565 void
566 gst_task_set_thread_callbacks (GstTask * task,
567     GstTaskThreadCallbacks * callbacks, gpointer user_data,
568     GDestroyNotify notify)
569 {
570   GDestroyNotify old_notify;
571
572   g_return_if_fail (task != NULL);
573   g_return_if_fail (GST_IS_TASK (task));
574   g_return_if_fail (callbacks != NULL);
575
576   GST_OBJECT_LOCK (task);
577   old_notify = task->priv->thr_notify;
578
579   if (old_notify) {
580     gpointer old_data;
581
582     old_data = task->priv->thr_user_data;
583
584     task->priv->thr_user_data = NULL;
585     task->priv->thr_notify = NULL;
586     GST_OBJECT_UNLOCK (task);
587
588     old_notify (old_data);
589
590     GST_OBJECT_LOCK (task);
591   }
592   task->priv->thr_callbacks = *callbacks;
593   task->priv->thr_user_data = user_data;
594   task->priv->thr_notify = notify;
595   GST_OBJECT_UNLOCK (task);
596 }
597
598 /**
599  * gst_task_get_state:
600  * @task: The #GstTask to query
601  *
602  * Get the current state of the task.
603  *
604  * Returns: The #GstTaskState of the task
605  *
606  * MT safe.
607  */
608 GstTaskState
609 gst_task_get_state (GstTask * task)
610 {
611   GstTaskState result;
612
613   g_return_val_if_fail (GST_IS_TASK (task), GST_TASK_STOPPED);
614
615   result = GET_TASK_STATE (task);
616
617   return result;
618 }
619
620 /* make sure the task is running and start a thread if it's not.
621  * This function must be called with the task LOCK. */
622 static gboolean
623 start_task (GstTask * task)
624 {
625   gboolean res = TRUE;
626   GError *error = NULL;
627   GstTaskPrivate *priv;
628
629   priv = task->priv;
630
631   /* new task, We ref before so that it remains alive while
632    * the thread is running. */
633   gst_object_ref (task);
634   /* mark task as running so that a join will wait until we schedule
635    * and exit the task function. */
636   task->running = TRUE;
637
638   /* push on the thread pool, we remember the original pool because the user
639    * could change it later on and then we join to the wrong pool. */
640   priv->pool_id = gst_object_ref (priv->pool);
641   priv->id =
642       gst_task_pool_push (priv->pool_id, (GstTaskPoolFunction) gst_task_func,
643       task, &error);
644
645   if (error != NULL) {
646     g_warning ("failed to create thread: %s", error->message);
647     g_error_free (error);
648     res = FALSE;
649   }
650   return res;
651 }
652
653
654 /**
655  * gst_task_set_state:
656  * @task: a #GstTask
657  * @state: the new task state
658  *
659  * Sets the state of @task to @state.
660  *
661  * The @task must have a lock associated with it using
662  * gst_task_set_lock() when going to GST_TASK_STARTED or GST_TASK_PAUSED or
663  * this function will return %FALSE.
664  *
665  * MT safe.
666  *
667  * Returns: %TRUE if the state could be changed.
668  *
669  * Since: 0.10.24
670  */
671 gboolean
672 gst_task_set_state (GstTask * task, GstTaskState state)
673 {
674   GstTaskState old;
675   gboolean res = TRUE;
676
677   g_return_val_if_fail (GST_IS_TASK (task), FALSE);
678
679   GST_DEBUG_OBJECT (task, "Changing task %p to state %d", task, state);
680
681   GST_OBJECT_LOCK (task);
682   if (state != GST_TASK_STOPPED)
683     if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
684       goto no_lock;
685
686   /* if the state changed, do our thing */
687   old = GET_TASK_STATE (task);
688   if (old != state) {
689     SET_TASK_STATE (task, state);
690     switch (old) {
691       case GST_TASK_STOPPED:
692         /* If the task already has a thread scheduled we don't have to do
693          * anything. */
694         if (G_UNLIKELY (!task->running))
695           res = start_task (task);
696         break;
697       case GST_TASK_PAUSED:
698         /* when we are paused, signal to go to the new state */
699         GST_TASK_SIGNAL (task);
700         break;
701       case GST_TASK_STARTED:
702         /* if we were started, we'll go to the new state after the next
703          * iteration. */
704         break;
705     }
706   }
707   GST_OBJECT_UNLOCK (task);
708
709   return res;
710
711   /* ERRORS */
712 no_lock:
713   {
714     GST_WARNING_OBJECT (task, "state %d set on task without a lock", state);
715     GST_OBJECT_UNLOCK (task);
716     g_warning ("task without a lock can't be set to state %d", state);
717     return FALSE;
718   }
719 }
720
721 /**
722  * gst_task_start:
723  * @task: The #GstTask to start
724  *
725  * Starts @task. The @task must have a lock associated with it using
726  * gst_task_set_lock() or this function will return %FALSE.
727  *
728  * Returns: %TRUE if the task could be started.
729  *
730  * MT safe.
731  */
732 gboolean
733 gst_task_start (GstTask * task)
734 {
735   return gst_task_set_state (task, GST_TASK_STARTED);
736 }
737
738 /**
739  * gst_task_stop:
740  * @task: The #GstTask to stop
741  *
742  * Stops @task. This method merely schedules the task to stop and
743  * will not wait for the task to have completely stopped. Use
744  * gst_task_join() to stop and wait for completion.
745  *
746  * Returns: %TRUE if the task could be stopped.
747  *
748  * MT safe.
749  */
750 gboolean
751 gst_task_stop (GstTask * task)
752 {
753   return gst_task_set_state (task, GST_TASK_STOPPED);
754 }
755
756 /**
757  * gst_task_pause:
758  * @task: The #GstTask to pause
759  *
760  * Pauses @task. This method can also be called on a task in the
761  * stopped state, in which case a thread will be started and will remain
762  * in the paused state. This function does not wait for the task to complete
763  * the paused state.
764  *
765  * Returns: %TRUE if the task could be paused.
766  *
767  * MT safe.
768  */
769 gboolean
770 gst_task_pause (GstTask * task)
771 {
772   return gst_task_set_state (task, GST_TASK_PAUSED);
773 }
774
775 /**
776  * gst_task_join:
777  * @task: The #GstTask to join
778  *
779  * Joins @task. After this call, it is safe to unref the task
780  * and clean up the lock set with gst_task_set_lock().
781  *
782  * The task will automatically be stopped with this call.
783  *
784  * This function cannot be called from within a task function as this
785  * would cause a deadlock. The function will detect this and print a
786  * g_warning.
787  *
788  * Returns: %TRUE if the task could be joined.
789  *
790  * MT safe.
791  */
792 gboolean
793 gst_task_join (GstTask * task)
794 {
795   GThread *tself;
796   GstTaskPrivate *priv;
797   gpointer id;
798   GstTaskPool *pool = NULL;
799
800   priv = task->priv;
801
802   g_return_val_if_fail (GST_IS_TASK (task), FALSE);
803
804   tself = g_thread_self ();
805
806   GST_DEBUG_OBJECT (task, "Joining task %p, thread %p", task, tself);
807
808   /* we don't use a real thread join here because we are using
809    * thread pools */
810   GST_OBJECT_LOCK (task);
811   if (G_UNLIKELY (tself == task->thread))
812     goto joining_self;
813   SET_TASK_STATE (task, GST_TASK_STOPPED);
814   /* signal the state change for when it was blocked in PAUSED. */
815   GST_TASK_SIGNAL (task);
816   /* we set the running flag when pushing the task on the thread pool.
817    * This means that the task function might not be called when we try
818    * to join it here. */
819   while (G_LIKELY (task->running))
820     GST_TASK_WAIT (task);
821   /* clean the thread */
822   task->thread = NULL;
823   /* get the id and pool to join */
824   pool = priv->pool_id;
825   id = priv->id;
826   priv->pool_id = NULL;
827   priv->id = NULL;
828   GST_OBJECT_UNLOCK (task);
829
830   if (pool) {
831     if (id)
832       gst_task_pool_join (pool, id);
833     gst_object_unref (pool);
834   }
835
836   GST_DEBUG_OBJECT (task, "Joined task %p", task);
837
838   return TRUE;
839
840   /* ERRORS */
841 joining_self:
842   {
843     GST_WARNING_OBJECT (task, "trying to join task from its thread");
844     GST_OBJECT_UNLOCK (task);
845     g_warning ("\nTrying to join task %p from its thread would deadlock.\n"
846         "You cannot change the state of an element from its streaming\n"
847         "thread. Use g_idle_add() or post a GstMessage on the bus to\n"
848         "schedule the state change from the main thread.\n", task);
849     return FALSE;
850   }
851 }