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