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