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