Work around deprecated thread API in glib master
[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_create(). 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->abidata.ABI.thread = NULL;
190   task->lock = NULL;
191   task->cond = g_cond_new ();
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_free (task->cond);
220   task->cond = NULL;
221
222   G_OBJECT_CLASS (gst_task_parent_class)->finalize (object);
223 }
224
225 /* should be called with the object LOCK */
226 static void
227 gst_task_configure_name (GstTask * task)
228 {
229 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME)
230   const gchar *name;
231   gchar thread_name[17] = { 0, };
232
233   GST_OBJECT_LOCK (task);
234   name = GST_OBJECT_NAME (task);
235
236   /* set the thread name to something easily identifiable */
237   if (!snprintf (thread_name, 17, "%s", GST_STR_NULL (name))) {
238     GST_DEBUG_OBJECT (task, "Could not create thread name for '%s'", name);
239   } else {
240     GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", thread_name);
241     if (prctl (PR_SET_NAME, (unsigned long int) thread_name, 0, 0, 0))
242       GST_DEBUG_OBJECT (task, "Failed to set thread name");
243   }
244   GST_OBJECT_UNLOCK (task);
245 #endif
246 #ifdef _MSC_VER
247   const gchar *name;
248   name = GST_OBJECT_NAME (task);
249
250   /* set the thread name to something easily identifiable */
251   GST_DEBUG_OBJECT (task, "Setting thread name to '%s'", name);
252   SetThreadName (-1, name);
253 #endif
254 }
255
256 static void
257 gst_task_func (GstTask * task)
258 {
259   GStaticRecMutex *lock;
260   GThread *tself;
261   GstTaskPrivate *priv;
262
263   priv = task->priv;
264
265   tself = g_thread_self ();
266
267   GST_DEBUG ("Entering task %p, thread %p", task, tself);
268
269   /* we have to grab the lock to get the mutex. We also
270    * mark our state running so that nobody can mess with
271    * the mutex. */
272   GST_OBJECT_LOCK (task);
273   if (GET_TASK_STATE (task) == GST_TASK_STOPPED)
274     goto exit;
275   lock = GST_TASK_GET_LOCK (task);
276   if (G_UNLIKELY (lock == NULL))
277     goto no_lock;
278   task->abidata.ABI.thread = tself;
279   /* only update the priority when it was changed */
280   if (priv->prio_set) {
281 #if !GLIB_CHECK_VERSION (2, 31, 0)
282     g_thread_set_priority (tself, priv->priority);
283 #else
284     GST_INFO_OBJECT (task, "Thread priorities no longer have any effect");
285 #endif
286   }
287   GST_OBJECT_UNLOCK (task);
288
289   /* fire the enter_thread callback when we need to */
290   if (priv->thr_callbacks.enter_thread)
291     priv->thr_callbacks.enter_thread (task, tself, priv->thr_user_data);
292
293   /* locking order is TASK_LOCK, LOCK */
294   g_static_rec_mutex_lock (lock);
295   /* configure the thread name now */
296   gst_task_configure_name (task);
297
298   while (G_LIKELY (GET_TASK_STATE (task) != GST_TASK_STOPPED)) {
299     if (G_UNLIKELY (GET_TASK_STATE (task) == GST_TASK_PAUSED)) {
300       GST_OBJECT_LOCK (task);
301       while (G_UNLIKELY (GST_TASK_STATE (task) == GST_TASK_PAUSED)) {
302         gint t;
303
304         t = g_static_rec_mutex_unlock_full (lock);
305         if (t <= 0) {
306           g_warning ("wrong STREAM_LOCK count %d", t);
307         }
308         GST_TASK_SIGNAL (task);
309         GST_TASK_WAIT (task);
310         GST_OBJECT_UNLOCK (task);
311         /* locking order.. */
312         if (t > 0)
313           g_static_rec_mutex_lock_full (lock, t);
314
315         GST_OBJECT_LOCK (task);
316         if (G_UNLIKELY (GET_TASK_STATE (task) == GST_TASK_STOPPED)) {
317           GST_OBJECT_UNLOCK (task);
318           goto done;
319         }
320       }
321       GST_OBJECT_UNLOCK (task);
322     }
323
324     task->func (task->data);
325   }
326 done:
327   g_static_rec_mutex_unlock (lock);
328
329   GST_OBJECT_LOCK (task);
330   task->abidata.ABI.thread = NULL;
331
332 exit:
333   if (priv->thr_callbacks.leave_thread) {
334     /* fire the leave_thread callback when we need to. We need to do this before
335      * we signal the task and with the task lock released. */
336     GST_OBJECT_UNLOCK (task);
337     priv->thr_callbacks.leave_thread (task, tself, priv->thr_user_data);
338     GST_OBJECT_LOCK (task);
339   } else {
340     /* restore normal priority when releasing back into the pool, we will not
341      * touch the priority when a custom callback has been installed. */
342 #if !GLIB_CHECK_VERSION (2, 31, 0)
343     g_thread_set_priority (tself, G_THREAD_PRIORITY_NORMAL);
344 #endif
345   }
346   /* now we allow messing with the lock again by setting the running flag to
347    * FALSE. Together with the SIGNAL this is the sign for the _join() to
348    * complete.
349    * Note that we still have not dropped the final ref on the task. We could
350    * check here if there is a pending join() going on and drop the last ref
351    * before releasing the lock as we can be sure that a ref is held by the
352    * caller of the join(). */
353   task->running = FALSE;
354   GST_TASK_SIGNAL (task);
355   GST_OBJECT_UNLOCK (task);
356
357   GST_DEBUG ("Exit task %p, thread %p", task, g_thread_self ());
358
359   gst_object_unref (task);
360   return;
361
362 no_lock:
363   {
364     g_warning ("starting task without a lock");
365     goto exit;
366   }
367 }
368
369 /**
370  * gst_task_cleanup_all:
371  *
372  * Wait for all tasks to be stopped. This is mainly used internally
373  * to ensure proper cleanup of internal data structures in test suites.
374  *
375  * MT safe.
376  */
377 void
378 gst_task_cleanup_all (void)
379 {
380   GstTaskClass *klass;
381
382   if ((klass = g_type_class_peek (GST_TYPE_TASK))) {
383     init_klass_pool (klass);
384   }
385 }
386
387 /**
388  * gst_task_create:
389  * @func: The #GstTaskFunction to use
390  * @data: (closure): User data to pass to @func
391  *
392  * Create a new Task that will repeatedly call the provided @func
393  * with @data as a parameter. Typically the task will run in
394  * a new thread.
395  *
396  * The function cannot be changed after the task has been created. You
397  * must create a new #GstTask to change the function.
398  *
399  * This function will not yet create and start a thread. Use gst_task_start() or
400  * gst_task_pause() to create and start the GThread.
401  *
402  * Before the task can be used, a #GStaticRecMutex must be configured using the
403  * gst_task_set_lock() function. This lock will always be acquired while
404  * @func is called.
405  *
406  * Returns: (transfer full): A new #GstTask.
407  *
408  * MT safe.
409  */
410 GstTask *
411 gst_task_create (GstTaskFunction func, gpointer data)
412 {
413   GstTask *task;
414
415   task = g_object_newv (GST_TYPE_TASK, 0, NULL);
416   task->func = func;
417   task->data = data;
418
419   GST_DEBUG ("Created task %p", task);
420
421   return task;
422 }
423
424 /**
425  * gst_task_set_lock:
426  * @task: The #GstTask to use
427  * @mutex: The #GMutex to use
428  *
429  * Set the mutex used by the task. The mutex will be acquired before
430  * calling the #GstTaskFunction.
431  *
432  * This function has to be called before calling gst_task_pause() or
433  * gst_task_start().
434  *
435  * MT safe.
436  */
437 void
438 gst_task_set_lock (GstTask * task, GStaticRecMutex * mutex)
439 {
440   GST_OBJECT_LOCK (task);
441   if (G_UNLIKELY (task->running))
442     goto is_running;
443   GST_TASK_GET_LOCK (task) = mutex;
444   GST_OBJECT_UNLOCK (task);
445
446   return;
447
448   /* ERRORS */
449 is_running:
450   {
451     GST_OBJECT_UNLOCK (task);
452     g_warning ("cannot call set_lock on a running task");
453   }
454 }
455
456 /**
457  * gst_task_set_priority:
458  * @task: a #GstTask
459  * @priority: a new priority for @task
460  *
461  * Changes the priority of @task to @priority.
462  *
463  * Note: try not to depend on task priorities.
464  *
465  * MT safe.
466  *
467  * Since: 0.10.24
468  */
469 void
470 gst_task_set_priority (GstTask * task, GThreadPriority priority)
471 {
472   GstTaskPrivate *priv;
473   GThread *thread;
474
475   g_return_if_fail (GST_IS_TASK (task));
476
477   priv = task->priv;
478
479   GST_OBJECT_LOCK (task);
480   priv->prio_set = TRUE;
481   priv->priority = priority;
482   thread = task->abidata.ABI.thread;
483   if (thread != NULL) {
484     /* if this task already has a thread, we can configure the priority right
485      * away, else we do that when we assign a thread to the task. */
486 #if !GLIB_CHECK_VERSION (2, 31, 0)
487     g_thread_set_priority (thread, priority);
488 #else
489     GST_INFO_OBJECT (task, "Thread priorities no longer have any effect");
490 #endif
491   }
492   GST_OBJECT_UNLOCK (task);
493 }
494
495 /**
496  * gst_task_get_pool:
497  * @task: a #GstTask
498  *
499  * Get the #GstTaskPool that this task will use for its streaming
500  * threads.
501  *
502  * MT safe.
503  *
504  * Returns: (transfer full): the #GstTaskPool used by @task. gst_object_unref()
505  * after usage.
506  *
507  * Since: 0.10.24
508  */
509 GstTaskPool *
510 gst_task_get_pool (GstTask * task)
511 {
512   GstTaskPool *result;
513   GstTaskPrivate *priv;
514
515   g_return_val_if_fail (GST_IS_TASK (task), NULL);
516
517   priv = task->priv;
518
519   GST_OBJECT_LOCK (task);
520   result = gst_object_ref (priv->pool);
521   GST_OBJECT_UNLOCK (task);
522
523   return result;
524 }
525
526 /**
527  * gst_task_set_pool:
528  * @task: a #GstTask
529  * @pool: (transfer none): a #GstTaskPool
530  *
531  * Set @pool as the new GstTaskPool for @task. Any new streaming threads that
532  * will be created by @task will now use @pool.
533  *
534  * MT safe.
535  *
536  * Since: 0.10.24
537  */
538 void
539 gst_task_set_pool (GstTask * task, GstTaskPool * pool)
540 {
541   GstTaskPool *old;
542   GstTaskPrivate *priv;
543
544   g_return_if_fail (GST_IS_TASK (task));
545   g_return_if_fail (GST_IS_TASK_POOL (pool));
546
547   priv = task->priv;
548
549   GST_OBJECT_LOCK (task);
550   if (priv->pool != pool) {
551     old = priv->pool;
552     priv->pool = gst_object_ref (pool);
553   } else
554     old = NULL;
555   GST_OBJECT_UNLOCK (task);
556
557   if (old)
558     gst_object_unref (old);
559 }
560
561
562 /**
563  * gst_task_set_thread_callbacks:
564  * @task: The #GstTask to use
565  * @callbacks: (in): a #GstTaskThreadCallbacks pointer
566  * @user_data: (closure): user data passed to the callbacks
567  * @notify: called when @user_data is no longer referenced
568  *
569  * Set callbacks which will be executed when a new thread is needed, the thread
570  * function is entered and left and when the thread is joined.
571  *
572  * By default a thread for @task will be created from a default thread pool.
573  *
574  * Objects can use custom GThreads or can perform additional configuration of
575  * the threads (such as changing the thread priority) by installing callbacks.
576  *
577  * MT safe.
578  *
579  * Since: 0.10.24
580  */
581 void
582 gst_task_set_thread_callbacks (GstTask * task,
583     GstTaskThreadCallbacks * callbacks, gpointer user_data,
584     GDestroyNotify notify)
585 {
586   GDestroyNotify old_notify;
587
588   g_return_if_fail (task != NULL);
589   g_return_if_fail (GST_IS_TASK (task));
590   g_return_if_fail (callbacks != NULL);
591
592   GST_OBJECT_LOCK (task);
593   old_notify = task->priv->thr_notify;
594
595   if (old_notify) {
596     gpointer old_data;
597
598     old_data = task->priv->thr_user_data;
599
600     task->priv->thr_user_data = NULL;
601     task->priv->thr_notify = NULL;
602     GST_OBJECT_UNLOCK (task);
603
604     old_notify (old_data);
605
606     GST_OBJECT_LOCK (task);
607   }
608   task->priv->thr_callbacks = *callbacks;
609   task->priv->thr_user_data = user_data;
610   task->priv->thr_notify = notify;
611   GST_OBJECT_UNLOCK (task);
612 }
613
614 /**
615  * gst_task_get_state:
616  * @task: The #GstTask to query
617  *
618  * Get the current state of the task.
619  *
620  * Returns: The #GstTaskState of the task
621  *
622  * MT safe.
623  */
624 GstTaskState
625 gst_task_get_state (GstTask * task)
626 {
627   GstTaskState result;
628
629   g_return_val_if_fail (GST_IS_TASK (task), GST_TASK_STOPPED);
630
631   result = GET_TASK_STATE (task);
632
633   return result;
634 }
635
636 /* make sure the task is running and start a thread if it's not.
637  * This function must be called with the task LOCK. */
638 static gboolean
639 start_task (GstTask * task)
640 {
641   gboolean res = TRUE;
642   GError *error = NULL;
643   GstTaskPrivate *priv;
644
645   priv = task->priv;
646
647   /* new task, We ref before so that it remains alive while
648    * the thread is running. */
649   gst_object_ref (task);
650   /* mark task as running so that a join will wait until we schedule
651    * and exit the task function. */
652   task->running = TRUE;
653
654   /* push on the thread pool, we remember the original pool because the user
655    * could change it later on and then we join to the wrong pool. */
656   priv->pool_id = gst_object_ref (priv->pool);
657   priv->id =
658       gst_task_pool_push (priv->pool_id, (GstTaskPoolFunction) gst_task_func,
659       task, &error);
660
661   if (error != NULL) {
662     g_warning ("failed to create thread: %s", error->message);
663     g_error_free (error);
664     res = FALSE;
665   }
666   return res;
667 }
668
669
670 /**
671  * gst_task_set_state:
672  * @task: a #GstTask
673  * @state: the new task state
674  *
675  * Sets the state of @task to @state.
676  *
677  * The @task must have a lock associated with it using
678  * gst_task_set_lock() when going to GST_TASK_STARTED or GST_TASK_PAUSED or
679  * this function will return %FALSE.
680  *
681  * MT safe.
682  *
683  * Returns: %TRUE if the state could be changed.
684  *
685  * Since: 0.10.24
686  */
687 gboolean
688 gst_task_set_state (GstTask * task, GstTaskState state)
689 {
690   GstTaskState old;
691   gboolean res = TRUE;
692
693   g_return_val_if_fail (GST_IS_TASK (task), FALSE);
694
695   GST_DEBUG_OBJECT (task, "Changing task %p to state %d", task, state);
696
697   GST_OBJECT_LOCK (task);
698   if (state != GST_TASK_STOPPED)
699     if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
700       goto no_lock;
701
702   /* if the state changed, do our thing */
703   old = GET_TASK_STATE (task);
704   if (old != state) {
705     SET_TASK_STATE (task, state);
706     switch (old) {
707       case GST_TASK_STOPPED:
708         /* If the task already has a thread scheduled we don't have to do
709          * anything. */
710         if (G_UNLIKELY (!task->running))
711           res = start_task (task);
712         break;
713       case GST_TASK_PAUSED:
714         /* when we are paused, signal to go to the new state */
715         GST_TASK_SIGNAL (task);
716         break;
717       case GST_TASK_STARTED:
718         /* if we were started, we'll go to the new state after the next
719          * iteration. */
720         break;
721     }
722   }
723   GST_OBJECT_UNLOCK (task);
724
725   return res;
726
727   /* ERRORS */
728 no_lock:
729   {
730     GST_WARNING_OBJECT (task, "state %d set on task without a lock", state);
731     GST_OBJECT_UNLOCK (task);
732     g_warning ("task without a lock can't be set to state %d", state);
733     return FALSE;
734   }
735 }
736
737 /**
738  * gst_task_start:
739  * @task: The #GstTask to start
740  *
741  * Starts @task. The @task must have a lock associated with it using
742  * gst_task_set_lock() or this function will return %FALSE.
743  *
744  * Returns: %TRUE if the task could be started.
745  *
746  * MT safe.
747  */
748 gboolean
749 gst_task_start (GstTask * task)
750 {
751   return gst_task_set_state (task, GST_TASK_STARTED);
752 }
753
754 /**
755  * gst_task_stop:
756  * @task: The #GstTask to stop
757  *
758  * Stops @task. This method merely schedules the task to stop and
759  * will not wait for the task to have completely stopped. Use
760  * gst_task_join() to stop and wait for completion.
761  *
762  * Returns: %TRUE if the task could be stopped.
763  *
764  * MT safe.
765  */
766 gboolean
767 gst_task_stop (GstTask * task)
768 {
769   return gst_task_set_state (task, GST_TASK_STOPPED);
770 }
771
772 /**
773  * gst_task_pause:
774  * @task: The #GstTask to pause
775  *
776  * Pauses @task. This method can also be called on a task in the
777  * stopped state, in which case a thread will be started and will remain
778  * in the paused state. This function does not wait for the task to complete
779  * the paused state.
780  *
781  * Returns: %TRUE if the task could be paused.
782  *
783  * MT safe.
784  */
785 gboolean
786 gst_task_pause (GstTask * task)
787 {
788   return gst_task_set_state (task, GST_TASK_PAUSED);
789 }
790
791 /**
792  * gst_task_join:
793  * @task: The #GstTask to join
794  *
795  * Joins @task. After this call, it is safe to unref the task
796  * and clean up the lock set with gst_task_set_lock().
797  *
798  * The task will automatically be stopped with this call.
799  *
800  * This function cannot be called from within a task function as this
801  * would cause a deadlock. The function will detect this and print a
802  * g_warning.
803  *
804  * Returns: %TRUE if the task could be joined.
805  *
806  * MT safe.
807  */
808 gboolean
809 gst_task_join (GstTask * task)
810 {
811   GThread *tself;
812   GstTaskPrivate *priv;
813   gpointer id;
814   GstTaskPool *pool = NULL;
815
816   priv = task->priv;
817
818   g_return_val_if_fail (GST_IS_TASK (task), FALSE);
819
820   tself = g_thread_self ();
821
822   GST_DEBUG_OBJECT (task, "Joining task %p, thread %p", task, tself);
823
824   /* we don't use a real thread join here because we are using
825    * thread pools */
826   GST_OBJECT_LOCK (task);
827   if (G_UNLIKELY (tself == task->abidata.ABI.thread))
828     goto joining_self;
829   SET_TASK_STATE (task, GST_TASK_STOPPED);
830   /* signal the state change for when it was blocked in PAUSED. */
831   GST_TASK_SIGNAL (task);
832   /* we set the running flag when pushing the task on the thread pool.
833    * This means that the task function might not be called when we try
834    * to join it here. */
835   while (G_LIKELY (task->running))
836     GST_TASK_WAIT (task);
837   /* clean the thread */
838   task->abidata.ABI.thread = NULL;
839   /* get the id and pool to join */
840   pool = priv->pool_id;
841   id = priv->id;
842   priv->pool_id = NULL;
843   priv->id = NULL;
844   GST_OBJECT_UNLOCK (task);
845
846   if (pool) {
847     if (id)
848       gst_task_pool_join (pool, id);
849     gst_object_unref (pool);
850   }
851
852   GST_DEBUG_OBJECT (task, "Joined task %p", task);
853
854   return TRUE;
855
856   /* ERRORS */
857 joining_self:
858   {
859     GST_WARNING_OBJECT (task, "trying to join task from its thread");
860     GST_OBJECT_UNLOCK (task);
861     g_warning ("\nTrying to join task %p from its thread would deadlock.\n"
862         "You cannot change the state of an element from its streaming\n"
863         "thread. Use g_idle_add() or post a GstMessage on the bus to\n"
864         "schedule the state change from the main thread.\n", task);
865     return FALSE;
866   }
867 }