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