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