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