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