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   /* configured pool */
100   GstTaskPool *pool;
101
102   /* remember the pool and id that is currently running. */
103   gpointer id;
104   GstTaskPool *pool_id;
105 };
106
107 #ifdef _MSC_VER
108 #include <windows.h>
109
110 struct _THREADNAME_INFO
111 {
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
116 };
117 typedef struct _THREADNAME_INFO THREADNAME_INFO;
118
119 void
120 SetThreadName (DWORD dwThreadID, LPCSTR szThreadName)
121 {
122   THREADNAME_INFO info;
123   info.dwType = 0x1000;
124   info.szName = szThreadName;
125   info.dwThreadID = dwThreadID;
126   info.dwFlags = 0;
127
128   __try {
129     RaiseException (0x406D1388, 0, sizeof (info) / sizeof (DWORD),
130         (DWORD *) & info);
131   }
132   __except (EXCEPTION_CONTINUE_EXECUTION) {
133   }
134 }
135 #endif
136
137 static void gst_task_finalize (GObject * object);
138
139 static void gst_task_func (GstTask * task);
140
141 static GMutex pool_lock;
142
143 #define _do_init \
144 { \
145   GST_DEBUG_CATEGORY_INIT (task_debug, "task", 0, "Processing tasks"); \
146 }
147
148 G_DEFINE_TYPE_WITH_CODE (GstTask, gst_task, GST_TYPE_OBJECT, _do_init);
149
150 static void
151 init_klass_pool (GstTaskClass * klass)
152 {
153   g_mutex_lock (&pool_lock);
154   if (klass->pool) {
155     gst_task_pool_cleanup (klass->pool);
156     gst_object_unref (klass->pool);
157   }
158   klass->pool = gst_task_pool_new ();
159   gst_task_pool_prepare (klass->pool, NULL);
160   g_mutex_unlock (&pool_lock);
161 }
162
163 static void
164 gst_task_class_init (GstTaskClass * klass)
165 {
166   GObjectClass *gobject_class;
167
168   gobject_class = (GObjectClass *) klass;
169
170   g_type_class_add_private (klass, sizeof (GstTaskPrivate));
171
172   gobject_class->finalize = gst_task_finalize;
173
174   init_klass_pool (klass);
175 }
176
177 static void
178 gst_task_init (GstTask * task)
179 {
180   GstTaskClass *klass;
181
182   klass = GST_TASK_GET_CLASS (task);
183
184   task->priv = GST_TASK_GET_PRIVATE (task);
185   task->running = FALSE;
186   task->thread = NULL;
187   task->lock = NULL;
188   g_cond_init (&task->cond);
189   SET_TASK_STATE (task, GST_TASK_STOPPED);
190
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);
196 }
197
198 static void
199 gst_task_finalize (GObject * object)
200 {
201   GstTask *task = GST_TASK (object);
202   GstTaskPrivate *priv = task->priv;
203
204   GST_DEBUG ("task %p finalize", task);
205
206   if (priv->thr_notify)
207     priv->thr_notify (priv->thr_user_data);
208   priv->thr_notify = NULL;
209   priv->thr_user_data = NULL;
210
211   gst_object_unref (priv->pool);
212
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);
216
217   G_OBJECT_CLASS (gst_task_parent_class)->finalize (object);
218 }
219
220 /* should be called with the object LOCK */
221 static void
222 gst_task_configure_name (GstTask * task)
223 {
224 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME)
225   const gchar *name;
226   gchar thread_name[17] = { 0, };
227
228   GST_OBJECT_LOCK (task);
229   name = GST_OBJECT_NAME (task);
230
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);
234   } else {
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");
238   }
239   GST_OBJECT_UNLOCK (task);
240 #endif
241 #ifdef _MSC_VER
242   const gchar *name;
243   name = GST_OBJECT_NAME (task);
244
245   /* set the thread name to something easily identifiable */
246   GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", name);
247   SetThreadName (-1, name);
248 #endif
249 }
250
251 static void
252 gst_task_func (GstTask * task)
253 {
254   GRecMutex *lock;
255   GThread *tself;
256   GstTaskPrivate *priv;
257
258   priv = task->priv;
259
260   tself = g_thread_self ();
261
262   GST_DEBUG ("Entering task %p, thread %p", task, tself);
263
264   /* we have to grab the lock to get the mutex. We also
265    * mark our state running so that nobody can mess with
266    * the mutex. */
267   GST_OBJECT_LOCK (task);
268   if (GET_TASK_STATE (task) == GST_TASK_STOPPED)
269     goto exit;
270   lock = GST_TASK_GET_LOCK (task);
271   if (G_UNLIKELY (lock == NULL))
272     goto no_lock;
273   task->thread = tself;
274   GST_OBJECT_UNLOCK (task);
275
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);
279
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);
284
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);
290
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);
298
299         GST_OBJECT_LOCK (task);
300         if (G_UNLIKELY (GET_TASK_STATE (task) == GST_TASK_STOPPED)) {
301           GST_OBJECT_UNLOCK (task);
302           goto done;
303         }
304       }
305       GST_OBJECT_UNLOCK (task);
306     }
307
308     task->func (task->data);
309   }
310 done:
311   g_rec_mutex_unlock (lock);
312
313   GST_OBJECT_LOCK (task);
314   task->thread = NULL;
315
316 exit:
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);
323   }
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
326    * complete.
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);
334
335   GST_DEBUG ("Exit task %p, thread %p", task, g_thread_self ());
336
337   gst_object_unref (task);
338   return;
339
340 no_lock:
341   {
342     g_warning ("starting task without a lock");
343     goto exit;
344   }
345 }
346
347 /**
348  * gst_task_cleanup_all:
349  *
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.
352  *
353  * MT safe.
354  */
355 void
356 gst_task_cleanup_all (void)
357 {
358   GstTaskClass *klass;
359
360   if ((klass = g_type_class_peek (GST_TYPE_TASK))) {
361     init_klass_pool (klass);
362   }
363 }
364
365 /**
366  * gst_task_new:
367  * @func: The #GstTaskFunction to use
368  * @data: (closure): User data to pass to @func
369  *
370  * Create a new Task that will repeatedly call the provided @func
371  * with @data as a parameter. Typically the task will run in
372  * a new thread.
373  *
374  * The function cannot be changed after the task has been created. You
375  * must create a new #GstTask to change the function.
376  *
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.
379  *
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
382  * @func is called.
383  *
384  * Returns: (transfer full): A new #GstTask.
385  *
386  * MT safe.
387  */
388 GstTask *
389 gst_task_new (GstTaskFunction func, gpointer data)
390 {
391   GstTask *task;
392
393   task = g_object_newv (GST_TYPE_TASK, 0, NULL);
394   task->func = func;
395   task->data = data;
396
397   GST_DEBUG ("Created task %p", task);
398
399   return task;
400 }
401
402 /**
403  * gst_task_set_lock:
404  * @task: The #GstTask to use
405  * @mutex: The #GRecMutex to use
406  *
407  * Set the mutex used by the task. The mutex will be acquired before
408  * calling the #GstTaskFunction.
409  *
410  * This function has to be called before calling gst_task_pause() or
411  * gst_task_start().
412  *
413  * MT safe.
414  */
415 void
416 gst_task_set_lock (GstTask * task, GRecMutex * mutex)
417 {
418   GST_OBJECT_LOCK (task);
419   if (G_UNLIKELY (task->running))
420     goto is_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);
424
425   return;
426
427   /* ERRORS */
428 is_running:
429   {
430     GST_OBJECT_UNLOCK (task);
431     g_warning ("cannot call set_lock on a running task");
432   }
433 }
434
435 /**
436  * gst_task_get_pool:
437  * @task: a #GstTask
438  *
439  * Get the #GstTaskPool that this task will use for its streaming
440  * threads.
441  *
442  * MT safe.
443  *
444  * Returns: (transfer full): the #GstTaskPool used by @task. gst_object_unref()
445  * after usage.
446  *
447  * Since: 0.10.24
448  */
449 GstTaskPool *
450 gst_task_get_pool (GstTask * task)
451 {
452   GstTaskPool *result;
453   GstTaskPrivate *priv;
454
455   g_return_val_if_fail (GST_IS_TASK (task), NULL);
456
457   priv = task->priv;
458
459   GST_OBJECT_LOCK (task);
460   result = gst_object_ref (priv->pool);
461   GST_OBJECT_UNLOCK (task);
462
463   return result;
464 }
465
466 /**
467  * gst_task_set_pool:
468  * @task: a #GstTask
469  * @pool: (transfer none): a #GstTaskPool
470  *
471  * Set @pool as the new GstTaskPool for @task. Any new streaming threads that
472  * will be created by @task will now use @pool.
473  *
474  * MT safe.
475  *
476  * Since: 0.10.24
477  */
478 void
479 gst_task_set_pool (GstTask * task, GstTaskPool * pool)
480 {
481   GstTaskPool *old;
482   GstTaskPrivate *priv;
483
484   g_return_if_fail (GST_IS_TASK (task));
485   g_return_if_fail (GST_IS_TASK_POOL (pool));
486
487   priv = task->priv;
488
489   GST_OBJECT_LOCK (task);
490   if (priv->pool != pool) {
491     old = priv->pool;
492     priv->pool = gst_object_ref (pool);
493   } else
494     old = NULL;
495   GST_OBJECT_UNLOCK (task);
496
497   if (old)
498     gst_object_unref (old);
499 }
500
501
502 /**
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
508  *
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.
511  *
512  * By default a thread for @task will be created from a default thread pool.
513  *
514  * Objects can use custom GThreads or can perform additional configuration of
515  * the threads (such as changing the thread priority) by installing callbacks.
516  *
517  * MT safe.
518  *
519  * Since: 0.10.24
520  */
521 void
522 gst_task_set_thread_callbacks (GstTask * task,
523     GstTaskThreadCallbacks * callbacks, gpointer user_data,
524     GDestroyNotify notify)
525 {
526   GDestroyNotify old_notify;
527
528   g_return_if_fail (task != NULL);
529   g_return_if_fail (GST_IS_TASK (task));
530   g_return_if_fail (callbacks != NULL);
531
532   GST_OBJECT_LOCK (task);
533   old_notify = task->priv->thr_notify;
534
535   if (old_notify) {
536     gpointer old_data;
537
538     old_data = task->priv->thr_user_data;
539
540     task->priv->thr_user_data = NULL;
541     task->priv->thr_notify = NULL;
542     GST_OBJECT_UNLOCK (task);
543
544     old_notify (old_data);
545
546     GST_OBJECT_LOCK (task);
547   }
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);
552 }
553
554 /**
555  * gst_task_get_state:
556  * @task: The #GstTask to query
557  *
558  * Get the current state of the task.
559  *
560  * Returns: The #GstTaskState of the task
561  *
562  * MT safe.
563  */
564 GstTaskState
565 gst_task_get_state (GstTask * task)
566 {
567   GstTaskState result;
568
569   g_return_val_if_fail (GST_IS_TASK (task), GST_TASK_STOPPED);
570
571   result = GET_TASK_STATE (task);
572
573   return result;
574 }
575
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. */
578 static gboolean
579 start_task (GstTask * task)
580 {
581   gboolean res = TRUE;
582   GError *error = NULL;
583   GstTaskPrivate *priv;
584
585   priv = task->priv;
586
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;
593
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);
597   priv->id =
598       gst_task_pool_push (priv->pool_id, (GstTaskPoolFunction) gst_task_func,
599       task, &error);
600
601   if (error != NULL) {
602     g_warning ("failed to create thread: %s", error->message);
603     g_error_free (error);
604     res = FALSE;
605   }
606   return res;
607 }
608
609
610 /**
611  * gst_task_set_state:
612  * @task: a #GstTask
613  * @state: the new task state
614  *
615  * Sets the state of @task to @state.
616  *
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.
620  *
621  * MT safe.
622  *
623  * Returns: %TRUE if the state could be changed.
624  *
625  * Since: 0.10.24
626  */
627 gboolean
628 gst_task_set_state (GstTask * task, GstTaskState state)
629 {
630   GstTaskState old;
631   gboolean res = TRUE;
632
633   g_return_val_if_fail (GST_IS_TASK (task), FALSE);
634
635   GST_DEBUG_OBJECT (task, "Changing task %p to state %d", task, state);
636
637   GST_OBJECT_LOCK (task);
638   if (state != GST_TASK_STOPPED)
639     if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
640       goto no_lock;
641
642   /* if the state changed, do our thing */
643   old = GET_TASK_STATE (task);
644   if (old != state) {
645     SET_TASK_STATE (task, state);
646     switch (old) {
647       case GST_TASK_STOPPED:
648         /* If the task already has a thread scheduled we don't have to do
649          * anything. */
650         if (G_UNLIKELY (!task->running))
651           res = start_task (task);
652         break;
653       case GST_TASK_PAUSED:
654         /* when we are paused, signal to go to the new state */
655         GST_TASK_SIGNAL (task);
656         break;
657       case GST_TASK_STARTED:
658         /* if we were started, we'll go to the new state after the next
659          * iteration. */
660         break;
661     }
662   }
663   GST_OBJECT_UNLOCK (task);
664
665   return res;
666
667   /* ERRORS */
668 no_lock:
669   {
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);
673     return FALSE;
674   }
675 }
676
677 /**
678  * gst_task_start:
679  * @task: The #GstTask to start
680  *
681  * Starts @task. The @task must have a lock associated with it using
682  * gst_task_set_lock() or this function will return %FALSE.
683  *
684  * Returns: %TRUE if the task could be started.
685  *
686  * MT safe.
687  */
688 gboolean
689 gst_task_start (GstTask * task)
690 {
691   return gst_task_set_state (task, GST_TASK_STARTED);
692 }
693
694 /**
695  * gst_task_stop:
696  * @task: The #GstTask to stop
697  *
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.
701  *
702  * Returns: %TRUE if the task could be stopped.
703  *
704  * MT safe.
705  */
706 gboolean
707 gst_task_stop (GstTask * task)
708 {
709   return gst_task_set_state (task, GST_TASK_STOPPED);
710 }
711
712 /**
713  * gst_task_pause:
714  * @task: The #GstTask to pause
715  *
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
719  * the paused state.
720  *
721  * Returns: %TRUE if the task could be paused.
722  *
723  * MT safe.
724  */
725 gboolean
726 gst_task_pause (GstTask * task)
727 {
728   return gst_task_set_state (task, GST_TASK_PAUSED);
729 }
730
731 /**
732  * gst_task_join:
733  * @task: The #GstTask to join
734  *
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().
737  *
738  * The task will automatically be stopped with this call.
739  *
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
742  * g_warning.
743  *
744  * Returns: %TRUE if the task could be joined.
745  *
746  * MT safe.
747  */
748 gboolean
749 gst_task_join (GstTask * task)
750 {
751   GThread *tself;
752   GstTaskPrivate *priv;
753   gpointer id;
754   GstTaskPool *pool = NULL;
755
756   priv = task->priv;
757
758   g_return_val_if_fail (GST_IS_TASK (task), FALSE);
759
760   tself = g_thread_self ();
761
762   GST_DEBUG_OBJECT (task, "Joining task %p, thread %p", task, tself);
763
764   /* we don't use a real thread join here because we are using
765    * thread pools */
766   GST_OBJECT_LOCK (task);
767   if (G_UNLIKELY (tself == task->thread))
768     goto joining_self;
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 */
778   task->thread = NULL;
779   /* get the id and pool to join */
780   pool = priv->pool_id;
781   id = priv->id;
782   priv->pool_id = NULL;
783   priv->id = NULL;
784   GST_OBJECT_UNLOCK (task);
785
786   if (pool) {
787     if (id)
788       gst_task_pool_join (pool, id);
789     gst_object_unref (pool);
790   }
791
792   GST_DEBUG_OBJECT (task, "Joined task %p", task);
793
794   return TRUE;
795
796   /* ERRORS */
797 joining_self:
798   {
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);
805     return FALSE;
806   }
807 }