2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
5 * gsttask.c: Streaming tasks
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.
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.
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.
23 #include "gst_private.h"
28 GST_DEBUG_CATEGORY (task_debug);
29 #define GST_CAT_DEFAULT (task_debug)
31 static void gst_task_class_init (GstTaskClass * klass);
32 static void gst_task_init (GstTask * task);
33 static void gst_task_finalize (GObject * object);
35 static void gst_task_func (GstTask * task, GstTaskClass * tclass);
37 static GstObjectClass *parent_class = NULL;
39 static GStaticMutex pool_lock = G_STATIC_MUTEX_INIT;
42 gst_task_get_type (void)
44 static GType _gst_task_type = 0;
46 if (!_gst_task_type) {
47 static const GTypeInfo task_info = {
48 sizeof (GstTaskClass),
51 (GClassInitFunc) gst_task_class_init,
56 (GInstanceInitFunc) gst_task_init,
61 g_type_register_static (GST_TYPE_OBJECT, "GstTask", &task_info, 0);
63 GST_DEBUG_CATEGORY_INIT (task_debug, "task", 0, "Processing tasks");
65 return _gst_task_type;
69 gst_task_class_init (GstTaskClass * klass)
71 GObjectClass *gobject_class;
73 gobject_class = (GObjectClass *) klass;
75 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
77 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_task_finalize);
79 klass->pool = g_thread_pool_new (
80 (GFunc) gst_task_func, klass, -1, FALSE, NULL);
84 gst_task_init (GstTask * task)
86 task->running = FALSE;
88 task->cond = g_cond_new ();
89 task->state = GST_TASK_STOPPED;
93 gst_task_finalize (GObject * object)
95 GstTask *task = GST_TASK (object);
97 GST_DEBUG ("task %p finalize", task);
99 g_cond_free (task->cond);
102 G_OBJECT_CLASS (parent_class)->finalize (object);
106 gst_task_func (GstTask * task, GstTaskClass * tclass)
108 GStaticRecMutex *lock;
110 GST_DEBUG ("Entering task %p, thread %p", task, g_thread_self ());
112 /* we have to grab the lock to get the mutex. We also
113 * mark our state running so that nobody can mess with
116 if (task->state == GST_TASK_STOPPED)
118 lock = GST_TASK_GET_LOCK (task);
119 task->running = TRUE;
122 /* locking order is TASK_LOCK, LOCK */
123 g_static_rec_mutex_lock (lock);
125 while (G_LIKELY (task->state != GST_TASK_STOPPED)) {
126 while (G_UNLIKELY (task->state == GST_TASK_PAUSED)) {
129 t = g_static_rec_mutex_unlock_full (lock);
131 g_warning ("wrong STREAM_LOCK count %d", t);
133 GST_TASK_SIGNAL (task);
134 GST_TASK_WAIT (task);
136 /* locking order.. */
138 g_static_rec_mutex_lock_full (lock, t);
141 if (task->state == GST_TASK_STOPPED)
146 task->func (task->data);
152 g_static_rec_mutex_unlock (lock);
154 /* now we allow messing with the lock again */
156 task->running = FALSE;
158 GST_TASK_SIGNAL (task);
161 GST_DEBUG ("Exit task %p, thread %p", task, g_thread_self ());
163 gst_object_unref (task);
167 * gst_task_cleanup_all:
169 * Wait for all tasks to be stopped. This is mainly used internally
170 * to ensure proper cleanup of internal datastructures in testsuites.
175 gst_task_cleanup_all (void)
179 if ((klass = g_type_class_peek (GST_TYPE_TASK))) {
180 g_static_mutex_lock (&pool_lock);
182 /* Shut down all the threads, we still process the ones scheduled
183 * because the unref happens in the thread function.
184 * Also wait for currently running ones to finish. */
185 g_thread_pool_free (klass->pool, FALSE, TRUE);
186 /* create new pool, so we can still do something after this
188 klass->pool = g_thread_pool_new (
189 (GFunc) gst_task_func, klass, -1, FALSE, NULL);
191 g_static_mutex_unlock (&pool_lock);
197 * @func: The #GstTaskFunction to use
198 * @data: User data to pass to @func
200 * Create a new Task that will repeadedly call the provided @func
201 * with @data as a parameter. Typically the task will run in
204 * Returns: A new #GstTask.
209 gst_task_create (GstTaskFunction func, gpointer data)
213 task = g_object_new (GST_TYPE_TASK, NULL);
217 GST_DEBUG ("Created task %p", task);
224 * @task: The #GstTask to use
225 * @mutex: The GMutex to use
227 * Set the mutex used by the task.
232 gst_task_set_lock (GstTask * task, GStaticRecMutex * mutex)
237 GST_TASK_GET_LOCK (task) = mutex;
245 g_warning ("cannot call set_lock on a running task");
252 * gst_task_get_state:
253 * @task: The #GstTask to query
255 * Get the current state of the task.
257 * Returns: The #GstTaskState of the task
262 gst_task_get_state (GstTask * task)
266 g_return_val_if_fail (GST_IS_TASK (task), GST_TASK_STOPPED);
269 result = task->state;
277 * @task: The #GstTask to start
281 * Returns: TRUE if the task could be started.
286 gst_task_start (GstTask * task)
290 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
292 GST_DEBUG_OBJECT (task, "Starting task %p", task);
295 if (G_UNLIKELY (GST_TASK_GET_LOCK (task) == NULL))
299 task->state = GST_TASK_STARTED;
301 case GST_TASK_STOPPED:
303 GstTaskClass *tclass;
305 tclass = GST_TASK_GET_CLASS (task);
307 /* new task, push on threadpool. We ref before so
308 * that it remains alive while on the threadpool. */
309 gst_object_ref (task);
310 g_static_mutex_lock (&pool_lock);
311 g_thread_pool_push (tclass->pool, task, NULL);
312 g_static_mutex_unlock (&pool_lock);
315 case GST_TASK_PAUSED:
316 /* PAUSE to PLAY, signal */
317 GST_TASK_SIGNAL (task);
319 case GST_TASK_STARTED:
330 g_warning ("starting task without a lock");
337 * @task: The #GstTask to stop
341 * Returns: TRUE if the task could be stopped.
346 gst_task_stop (GstTask * task)
348 GstTaskClass *tclass;
351 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
353 tclass = GST_TASK_GET_CLASS (task);
355 GST_DEBUG_OBJECT (task, "Stopping task %p", task);
359 task->state = GST_TASK_STOPPED;
361 case GST_TASK_STOPPED:
363 case GST_TASK_PAUSED:
364 GST_TASK_SIGNAL (task);
366 case GST_TASK_STARTED:
376 * @task: The #GstTask to pause
380 * Returns: TRUE if the task could be paused.
385 gst_task_pause (GstTask * task)
389 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
391 GST_DEBUG_OBJECT (task, "Pausing task %p", task);
395 task->state = GST_TASK_PAUSED;
397 case GST_TASK_STOPPED:
399 GstTaskClass *tclass;
401 tclass = GST_TASK_GET_CLASS (task);
403 gst_object_ref (task);
404 g_static_mutex_lock (&pool_lock);
405 g_thread_pool_push (tclass->pool, task, NULL);
406 g_static_mutex_unlock (&pool_lock);
409 case GST_TASK_PAUSED:
411 case GST_TASK_STARTED:
421 * @task: The #GstTask to join
423 * Joins @task. After this call, it is safe to unref the task
424 * and clean up the lock set with #gst_task_set_lock().
426 * The task will automatically be stopped with this call.
428 * This function cannot be called from within a task function.
430 * Returns: TRUE if the task could be joined.
435 gst_task_join (GstTask * task)
437 g_return_val_if_fail (GST_IS_TASK (task), FALSE);
439 GST_DEBUG_OBJECT (task, "Joining task %p", task);
442 task->state = GST_TASK_STOPPED;
443 GST_TASK_SIGNAL (task);
444 while (task->running)
445 GST_TASK_WAIT (task);