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