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