Fix FSF address
[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 #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 GstTaskPool *
458 gst_task_get_pool (GstTask * task)
459 {
460   GstTaskPool *result;
461   GstTaskPrivate *priv;
462
463   g_return_val_if_fail (GST_IS_TASK (task), NULL);
464
465   priv = task->priv;
466
467   GST_OBJECT_LOCK (task);
468   result = gst_object_ref (priv->pool);
469   GST_OBJECT_UNLOCK (task);
470
471   return result;
472 }
473
474 /**
475  * gst_task_set_pool:
476  * @task: a #GstTask
477  * @pool: (transfer none): a #GstTaskPool
478  *
479  * Set @pool as the new GstTaskPool for @task. Any new streaming threads that
480  * will be created by @task will now use @pool.
481  *
482  * MT safe.
483  */
484 void
485 gst_task_set_pool (GstTask * task, GstTaskPool * pool)
486 {
487   GstTaskPool *old;
488   GstTaskPrivate *priv;
489
490   g_return_if_fail (GST_IS_TASK (task));
491   g_return_if_fail (GST_IS_TASK_POOL (pool));
492
493   priv = task->priv;
494
495   GST_OBJECT_LOCK (task);
496   if (priv->pool != pool) {
497     old = priv->pool;
498     priv->pool = gst_object_ref (pool);
499   } else
500     old = NULL;
501   GST_OBJECT_UNLOCK (task);
502
503   if (old)
504     gst_object_unref (old);
505 }
506
507 /**
508  * gst_task_set_enter_callback:
509  * @task: The #GstTask to use
510  * @enter_func: (in): a #GstTaskThreadFunc
511  * @user_data: user data passed to @enter_func
512  * @notify: called when @user_data is no longer referenced
513  *
514  * Call @enter_func when the task function of @task is entered. @user_data will
515  * be passed to @enter_func and @notify will be called when @user_data is no
516  * longer referenced.
517  */
518 void
519 gst_task_set_enter_callback (GstTask * task, GstTaskThreadFunc enter_func,
520     gpointer user_data, GDestroyNotify notify)
521 {
522   GDestroyNotify old_notify;
523
524   g_return_if_fail (task != NULL);
525   g_return_if_fail (GST_IS_TASK (task));
526
527   GST_OBJECT_LOCK (task);
528   if ((old_notify = task->priv->enter_notify)) {
529     gpointer old_data = task->priv->enter_user_data;
530
531     task->priv->enter_user_data = NULL;
532     task->priv->enter_notify = NULL;
533     GST_OBJECT_UNLOCK (task);
534
535     old_notify (old_data);
536
537     GST_OBJECT_LOCK (task);
538   }
539   task->priv->enter_func = enter_func;
540   task->priv->enter_user_data = user_data;
541   task->priv->enter_notify = notify;
542   GST_OBJECT_UNLOCK (task);
543 }
544
545 /**
546  * gst_task_set_leave_callback:
547  * @task: The #GstTask to use
548  * @leave_func: (in): a #GstTaskThreadFunc
549  * @user_data: user data passed to @leave_func
550  * @notify: called when @user_data is no longer referenced
551  *
552  * Call @leave_func when the task function of @task is left. @user_data will
553  * be passed to @leave_func and @notify will be called when @user_data is no
554  * longer referenced.
555  */
556 void
557 gst_task_set_leave_callback (GstTask * task, GstTaskThreadFunc leave_func,
558     gpointer user_data, GDestroyNotify notify)
559 {
560   GDestroyNotify old_notify;
561
562   g_return_if_fail (task != NULL);
563   g_return_if_fail (GST_IS_TASK (task));
564
565   GST_OBJECT_LOCK (task);
566   if ((old_notify = task->priv->leave_notify)) {
567     gpointer old_data = task->priv->leave_user_data;
568
569     task->priv->leave_user_data = NULL;
570     task->priv->leave_notify = NULL;
571     GST_OBJECT_UNLOCK (task);
572
573     old_notify (old_data);
574
575     GST_OBJECT_LOCK (task);
576   }
577   task->priv->leave_func = leave_func;
578   task->priv->leave_user_data = user_data;
579   task->priv->leave_notify = notify;
580   GST_OBJECT_UNLOCK (task);
581 }
582
583 /**
584  * gst_task_get_state:
585  * @task: The #GstTask to query
586  *
587  * Get the current state of the task.
588  *
589  * Returns: The #GstTaskState of the task
590  *
591  * MT safe.
592  */
593 GstTaskState
594 gst_task_get_state (GstTask * task)
595 {
596   GstTaskState result;
597
598   g_return_val_if_fail (GST_IS_TASK (task), GST_TASK_STOPPED);
599
600   result = GET_TASK_STATE (task);
601
602   return result;
603 }
604
605 /* make sure the task is running and start a thread if it's not.
606  * This function must be called with the task LOCK. */
607 static gboolean
608 start_task (GstTask * task)
609 {
610   gboolean res = TRUE;
611   GError *error = NULL;
612   GstTaskPrivate *priv;
613
614   priv = task->priv;
615
616   /* new task, We ref before so that it remains alive while
617    * the thread is running. */
618   gst_object_ref (task);
619   /* mark task as running so that a join will wait until we schedule
620    * and exit the task function. */
621   task->running = TRUE;
622
623   /* push on the thread pool, we remember the original pool because the user
624    * could change it later on and then we join to the wrong pool. */
625   priv->pool_id = gst_object_ref (priv->pool);
626   priv->id =
627       gst_task_pool_push (priv->pool_id, (GstTaskPoolFunction) gst_task_func,
628       task, &error);
629
630   if (error != NULL) {
631     g_warning ("failed to create thread: %s", error->message);
632     g_error_free (error);
633     res = FALSE;
634   }
635   return res;
636 }
637
638
639 /**
640  * gst_task_set_state:
641  * @task: a #GstTask
642  * @state: the new task state
643  *
644  * Sets the state of @task to @state.
645  *
646  * The @task must have a lock associated with it using
647  * gst_task_set_lock() when going to GST_TASK_STARTED or GST_TASK_PAUSED or
648  * this function will return %FALSE.
649  *
650  * MT safe.
651  *
652  * Returns: %TRUE if the state could be changed.
653  */
654 gboolean
655 gst_task_set_state (GstTask * task, GstTaskState state)
656 {
657   GstTaskState old;
658   gboolean res = TRUE;
659
660   g_return_val_if_fail (GST_IS_TASK (task), FALSE);
661
662   GST_DEBUG_OBJECT (task, "Changing task %p to state %d", task, state);
663
664   GST_OBJECT_LOCK (task);
665   if (state != GST_TASK_STOPPED)
666     if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
667       goto no_lock;
668
669   /* if the state changed, do our thing */
670   old = GET_TASK_STATE (task);
671   if (old != state) {
672     SET_TASK_STATE (task, state);
673     switch (old) {
674       case GST_TASK_STOPPED:
675         /* If the task already has a thread scheduled we don't have to do
676          * anything. */
677         if (G_UNLIKELY (!task->running))
678           res = start_task (task);
679         break;
680       case GST_TASK_PAUSED:
681         /* when we are paused, signal to go to the new state */
682         GST_TASK_SIGNAL (task);
683         break;
684       case GST_TASK_STARTED:
685         /* if we were started, we'll go to the new state after the next
686          * iteration. */
687         break;
688     }
689   }
690   GST_OBJECT_UNLOCK (task);
691
692   return res;
693
694   /* ERRORS */
695 no_lock:
696   {
697     GST_WARNING_OBJECT (task, "state %d set on task without a lock", state);
698     GST_OBJECT_UNLOCK (task);
699     g_warning ("task without a lock can't be set to state %d", state);
700     return FALSE;
701   }
702 }
703
704 /**
705  * gst_task_start:
706  * @task: The #GstTask to start
707  *
708  * Starts @task. The @task must have a lock associated with it using
709  * gst_task_set_lock() or this function will return %FALSE.
710  *
711  * Returns: %TRUE if the task could be started.
712  *
713  * MT safe.
714  */
715 gboolean
716 gst_task_start (GstTask * task)
717 {
718   return gst_task_set_state (task, GST_TASK_STARTED);
719 }
720
721 /**
722  * gst_task_stop:
723  * @task: The #GstTask to stop
724  *
725  * Stops @task. This method merely schedules the task to stop and
726  * will not wait for the task to have completely stopped. Use
727  * gst_task_join() to stop and wait for completion.
728  *
729  * Returns: %TRUE if the task could be stopped.
730  *
731  * MT safe.
732  */
733 gboolean
734 gst_task_stop (GstTask * task)
735 {
736   return gst_task_set_state (task, GST_TASK_STOPPED);
737 }
738
739 /**
740  * gst_task_pause:
741  * @task: The #GstTask to pause
742  *
743  * Pauses @task. This method can also be called on a task in the
744  * stopped state, in which case a thread will be started and will remain
745  * in the paused state. This function does not wait for the task to complete
746  * the paused state.
747  *
748  * Returns: %TRUE if the task could be paused.
749  *
750  * MT safe.
751  */
752 gboolean
753 gst_task_pause (GstTask * task)
754 {
755   return gst_task_set_state (task, GST_TASK_PAUSED);
756 }
757
758 /**
759  * gst_task_join:
760  * @task: The #GstTask to join
761  *
762  * Joins @task. After this call, it is safe to unref the task
763  * and clean up the lock set with gst_task_set_lock().
764  *
765  * The task will automatically be stopped with this call.
766  *
767  * This function cannot be called from within a task function as this
768  * would cause a deadlock. The function will detect this and print a
769  * g_warning.
770  *
771  * Returns: %TRUE if the task could be joined.
772  *
773  * MT safe.
774  */
775 gboolean
776 gst_task_join (GstTask * task)
777 {
778   GThread *tself;
779   GstTaskPrivate *priv;
780   gpointer id;
781   GstTaskPool *pool = NULL;
782
783   priv = task->priv;
784
785   g_return_val_if_fail (GST_IS_TASK (task), FALSE);
786
787   tself = g_thread_self ();
788
789   GST_DEBUG_OBJECT (task, "Joining task %p, thread %p", task, tself);
790
791   /* we don't use a real thread join here because we are using
792    * thread pools */
793   GST_OBJECT_LOCK (task);
794   if (G_UNLIKELY (tself == task->thread))
795     goto joining_self;
796   SET_TASK_STATE (task, GST_TASK_STOPPED);
797   /* signal the state change for when it was blocked in PAUSED. */
798   GST_TASK_SIGNAL (task);
799   /* we set the running flag when pushing the task on the thread pool.
800    * This means that the task function might not be called when we try
801    * to join it here. */
802   while (G_LIKELY (task->running))
803     GST_TASK_WAIT (task);
804   /* clean the thread */
805   task->thread = NULL;
806   /* get the id and pool to join */
807   pool = priv->pool_id;
808   id = priv->id;
809   priv->pool_id = NULL;
810   priv->id = NULL;
811   GST_OBJECT_UNLOCK (task);
812
813   if (pool) {
814     if (id)
815       gst_task_pool_join (pool, id);
816     gst_object_unref (pool);
817   }
818
819   GST_DEBUG_OBJECT (task, "Joined task %p", task);
820
821   return TRUE;
822
823   /* ERRORS */
824 joining_self:
825   {
826     GST_WARNING_OBJECT (task, "trying to join task from its thread");
827     GST_OBJECT_UNLOCK (task);
828     g_warning ("\nTrying to join task %p from its thread would deadlock.\n"
829         "You cannot change the state of an element from its streaming\n"
830         "thread. Use g_idle_add() or post a GstMessage on the bus to\n"
831         "schedule the state change from the main thread.\n", task);
832     return FALSE;
833   }
834 }