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