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