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