introspection: add missing array annotation to gst_formats_contains
[platform/upstream/gstreamer.git] / gst / gsttask.c
index 5ebed39..34d4ffb 100644 (file)
  * stopped and the thread is stopped.
  *
  * After creating a #GstTask, use gst_object_unref() to free its resources. This can
- * only be done it the task is not running anymore.
+ * only be done when the task is not running anymore.
  *
  * Task functions can send a #GstMessage to send out-of-band data to the
  * application. The application can receive messages from the #GstBus in its
  * mainloop.
  *
- * For debugging perposes, the task will configure its object name as the thread
+ * For debugging purposes, the task will configure its object name as the thread
  * name on Linux. Please note that the object name should be configured before the
  * task is started; changing the object name after the task has been started, has
  * no effect on the thread name.
  *
- * Last reviewed on 2010-03-15 (0.10.29)
+ * Last reviewed on 2012-03-29 (0.11.3)
  */
 
-/* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
- * with newer GLib versions (>= 2.31.0) */
-#define GLIB_DISABLE_DEPRECATION_WARNINGS
 #include "gst_private.h"
 
 #include "gstinfo.h"
@@ -95,12 +92,13 @@ GST_DEBUG_CATEGORY_STATIC (task_debug);
 struct _GstTaskPrivate
 {
   /* callbacks for managing the thread of this task */
-  GstTaskThreadCallbacks thr_callbacks;
-  gpointer thr_user_data;
-  GDestroyNotify thr_notify;
+  GstTaskThreadFunc enter_func;
+  gpointer enter_user_data;
+  GDestroyNotify enter_notify;
 
-  gboolean prio_set;
-  GThreadPriority priority;
+  GstTaskThreadFunc leave_func;
+  gpointer leave_user_data;
+  GDestroyNotify leave_notify;
 
   /* configured pool */
   GstTaskPool *pool;
@@ -144,7 +142,7 @@ static void gst_task_finalize (GObject * object);
 
 static void gst_task_func (GstTask * task);
 
-static GStaticMutex pool_lock = G_STATIC_MUTEX_INIT;
+static GMutex pool_lock;
 
 #define _do_init \
 { \
@@ -156,14 +154,14 @@ G_DEFINE_TYPE_WITH_CODE (GstTask, gst_task, GST_TYPE_OBJECT, _do_init);
 static void
 init_klass_pool (GstTaskClass * klass)
 {
-  g_static_mutex_lock (&pool_lock);
+  g_mutex_lock (&pool_lock);
   if (klass->pool) {
     gst_task_pool_cleanup (klass->pool);
     gst_object_unref (klass->pool);
   }
   klass->pool = gst_task_pool_new ();
   gst_task_pool_prepare (klass->pool, NULL);
-  g_static_mutex_unlock (&pool_lock);
+  g_mutex_unlock (&pool_lock);
 }
 
 static void
@@ -191,15 +189,14 @@ gst_task_init (GstTask * task)
   task->running = FALSE;
   task->thread = NULL;
   task->lock = NULL;
-  task->cond = g_cond_new ();
+  g_cond_init (&task->cond);
   SET_TASK_STATE (task, GST_TASK_STOPPED);
-  task->priv->prio_set = FALSE;
 
   /* use the default klass pool for this task, users can
    * override this later */
-  g_static_mutex_lock (&pool_lock);
+  g_mutex_lock (&pool_lock);
   task->priv->pool = gst_object_ref (klass->pool);
-  g_static_mutex_unlock (&pool_lock);
+  g_mutex_unlock (&pool_lock);
 }
 
 static void
@@ -210,17 +207,20 @@ gst_task_finalize (GObject * object)
 
   GST_DEBUG ("task %p finalize", task);
 
-  if (priv->thr_notify)
-    priv->thr_notify (priv->thr_user_data);
-  priv->thr_notify = NULL;
-  priv->thr_user_data = NULL;
+  if (priv->enter_notify)
+    priv->enter_notify (priv->enter_user_data);
+
+  if (priv->leave_notify)
+    priv->leave_notify (priv->leave_user_data);
+
+  if (task->notify)
+    task->notify (task->user_data);
 
   gst_object_unref (priv->pool);
 
   /* task thread cannot be running here since it holds a ref
    * to the task so that the finalize could not have happened */
-  g_cond_free (task->cond);
-  task->cond = NULL;
+  g_cond_clear (&task->cond);
 
   G_OBJECT_CLASS (gst_task_parent_class)->finalize (object);
 }
@@ -259,7 +259,7 @@ gst_task_configure_name (GstTask * task)
 static void
 gst_task_func (GstTask * task)
 {
-  GStaticRecMutex *lock;
+  GRecMutex *lock;
   GThread *tself;
   GstTaskPrivate *priv;
 
@@ -279,22 +279,14 @@ gst_task_func (GstTask * task)
   if (G_UNLIKELY (lock == NULL))
     goto no_lock;
   task->thread = tself;
-  /* only update the priority when it was changed */
-  if (priv->prio_set) {
-#if !GLIB_CHECK_VERSION (2, 31, 0)
-    g_thread_set_priority (tself, priv->priority);
-#else
-    GST_INFO_OBJECT (task, "Thread priorities no longer have any effect");
-#endif
-  }
   GST_OBJECT_UNLOCK (task);
 
-  /* fire the enter_thread callback when we need to */
-  if (priv->thr_callbacks.enter_thread)
-    priv->thr_callbacks.enter_thread (task, tself, priv->thr_user_data);
+  /* fire the enter_func callback when we need to */
+  if (priv->enter_func)
+    priv->enter_func (task, tself, priv->enter_user_data);
 
   /* locking order is TASK_LOCK, LOCK */
-  g_static_rec_mutex_lock (lock);
+  g_rec_mutex_lock (lock);
   /* configure the thread name now */
   gst_task_configure_name (task);
 
@@ -302,13 +294,15 @@ gst_task_func (GstTask * task)
     if (G_UNLIKELY (GET_TASK_STATE (task) == GST_TASK_PAUSED)) {
       GST_OBJECT_LOCK (task);
       while (G_UNLIKELY (GST_TASK_STATE (task) == GST_TASK_PAUSED)) {
-        g_static_rec_mutex_unlock (lock);
+        g_rec_mutex_unlock (lock);
 
         GST_TASK_SIGNAL (task);
+        GST_INFO_OBJECT (task, "Task going to paused");
         GST_TASK_WAIT (task);
+        GST_INFO_OBJECT (task, "Task resume from paused");
         GST_OBJECT_UNLOCK (task);
         /* locking order.. */
-        g_static_rec_mutex_lock (lock);
+        g_rec_mutex_lock (lock);
 
         GST_OBJECT_LOCK (task);
         if (G_UNLIKELY (GET_TASK_STATE (task) == GST_TASK_STOPPED)) {
@@ -319,27 +313,21 @@ gst_task_func (GstTask * task)
       GST_OBJECT_UNLOCK (task);
     }
 
-    task->func (task->data);
+    task->func (task->user_data);
   }
 done:
-  g_static_rec_mutex_unlock (lock);
+  g_rec_mutex_unlock (lock);
 
   GST_OBJECT_LOCK (task);
   task->thread = NULL;
 
 exit:
-  if (priv->thr_callbacks.leave_thread) {
-    /* fire the leave_thread callback when we need to. We need to do this before
+  if (priv->leave_func) {
+    /* fire the leave_func callback when we need to. We need to do this before
      * we signal the task and with the task lock released. */
     GST_OBJECT_UNLOCK (task);
-    priv->thr_callbacks.leave_thread (task, tself, priv->thr_user_data);
+    priv->leave_func (task, tself, priv->leave_user_data);
     GST_OBJECT_LOCK (task);
-  } else {
-    /* restore normal priority when releasing back into the pool, we will not
-     * touch the priority when a custom callback has been installed. */
-#if !GLIB_CHECK_VERSION (2, 31, 0)
-    g_thread_set_priority (tself, G_THREAD_PRIORITY_NORMAL);
-#endif
   }
   /* now we allow messing with the lock again by setting the running flag to
    * FALSE. Together with the SIGNAL this is the sign for the _join() to
@@ -385,10 +373,11 @@ gst_task_cleanup_all (void)
 /**
  * gst_task_new:
  * @func: The #GstTaskFunction to use
- * @data: (closure): User data to pass to @func
+ * @user_data: User data to pass to @func
+ * @notify: the function to call when @user_data is no longer needed.
  *
  * Create a new Task that will repeatedly call the provided @func
- * with @data as a parameter. Typically the task will run in
+ * with @user_data as a parameter. Typically the task will run in
  * a new thread.
  *
  * The function cannot be changed after the task has been created. You
@@ -406,13 +395,14 @@ gst_task_cleanup_all (void)
  * MT safe.
  */
 GstTask *
-gst_task_new (GstTaskFunction func, gpointer data)
+gst_task_new (GstTaskFunction func, gpointer user_data, GDestroyNotify notify)
 {
   GstTask *task;
 
   task = g_object_newv (GST_TYPE_TASK, 0, NULL);
   task->func = func;
-  task->data = data;
+  task->user_data = user_data;
+  task->notify = notify;
 
   GST_DEBUG ("Created task %p", task);
 
@@ -422,7 +412,7 @@ gst_task_new (GstTaskFunction func, gpointer data)
 /**
  * gst_task_set_lock:
  * @task: The #GstTask to use
- * @mutex: The #GMutex to use
+ * @mutex: The #GRecMutex to use
  *
  * Set the mutex used by the task. The mutex will be acquired before
  * calling the #GstTaskFunction.
@@ -433,11 +423,12 @@ gst_task_new (GstTaskFunction func, gpointer data)
  * MT safe.
  */
 void
-gst_task_set_lock (GstTask * task, GStaticRecMutex * mutex)
+gst_task_set_lock (GstTask * task, GRecMutex * mutex)
 {
   GST_OBJECT_LOCK (task);
   if (G_UNLIKELY (task->running))
     goto is_running;
+  GST_INFO ("setting stream lock %p on task %p", mutex, task);
   GST_TASK_GET_LOCK (task) = mutex;
   GST_OBJECT_UNLOCK (task);
 
@@ -452,45 +443,6 @@ is_running:
 }
 
 /**
- * gst_task_set_priority:
- * @task: a #GstTask
- * @priority: a new priority for @task
- *
- * Changes the priority of @task to @priority.
- *
- * Note: try not to depend on task priorities.
- *
- * MT safe.
- *
- * Since: 0.10.24
- */
-void
-gst_task_set_priority (GstTask * task, GThreadPriority priority)
-{
-  GstTaskPrivate *priv;
-  GThread *thread;
-
-  g_return_if_fail (GST_IS_TASK (task));
-
-  priv = task->priv;
-
-  GST_OBJECT_LOCK (task);
-  priv->prio_set = TRUE;
-  priv->priority = priority;
-  thread = task->thread;
-  if (thread != NULL) {
-    /* if this task already has a thread, we can configure the priority right
-     * away, else we do that when we assign a thread to the task. */
-#if !GLIB_CHECK_VERSION (2, 31, 0)
-    g_thread_set_priority (thread, priority);
-#else
-    GST_INFO_OBJECT (task, "Thread priorities no longer have any effect");
-#endif
-  }
-  GST_OBJECT_UNLOCK (task);
-}
-
-/**
  * gst_task_get_pool:
  * @task: a #GstTask
  *
@@ -501,8 +453,6 @@ gst_task_set_priority (GstTask * task, GThreadPriority priority)
  *
  * Returns: (transfer full): the #GstTaskPool used by @task. gst_object_unref()
  * after usage.
- *
- * Since: 0.10.24
  */
 GstTaskPool *
 gst_task_get_pool (GstTask * task)
@@ -530,8 +480,6 @@ gst_task_get_pool (GstTask * task)
  * will be created by @task will now use @pool.
  *
  * MT safe.
- *
- * Since: 0.10.24
  */
 void
 gst_task_set_pool (GstTask * task, GstTaskPool * pool)
@@ -556,56 +504,79 @@ gst_task_set_pool (GstTask * task, GstTaskPool * pool)
     gst_object_unref (old);
 }
 
-
 /**
- * gst_task_set_thread_callbacks:
+ * gst_task_set_enter_callback:
  * @task: The #GstTask to use
- * @callbacks: (in): a #GstTaskThreadCallbacks pointer
- * @user_data: (closure): user data passed to the callbacks
+ * @enter_func: (in): a #GstTaskThreadFunc
+ * @user_data: user data passed to @enter_func
  * @notify: called when @user_data is no longer referenced
  *
- * Set callbacks which will be executed when a new thread is needed, the thread
- * function is entered and left and when the thread is joined.
- *
- * By default a thread for @task will be created from a default thread pool.
- *
- * Objects can use custom GThreads or can perform additional configuration of
- * the threads (such as changing the thread priority) by installing callbacks.
- *
- * MT safe.
- *
- * Since: 0.10.24
+ * Call @enter_func when the task function of @task is entered. @user_data will
+ * be passed to @enter_func and @notify will be called when @user_data is no
+ * longer referenced.
  */
 void
-gst_task_set_thread_callbacks (GstTask * task,
-    GstTaskThreadCallbacks * callbacks, gpointer user_data,
-    GDestroyNotify notify)
+gst_task_set_enter_callback (GstTask * task, GstTaskThreadFunc enter_func,
+    gpointer user_data, GDestroyNotify notify)
 {
   GDestroyNotify old_notify;
 
   g_return_if_fail (task != NULL);
   g_return_if_fail (GST_IS_TASK (task));
-  g_return_if_fail (callbacks != NULL);
 
   GST_OBJECT_LOCK (task);
-  old_notify = task->priv->thr_notify;
+  if ((old_notify = task->priv->enter_notify)) {
+    gpointer old_data = task->priv->enter_user_data;
 
-  if (old_notify) {
-    gpointer old_data;
+    task->priv->enter_user_data = NULL;
+    task->priv->enter_notify = NULL;
+    GST_OBJECT_UNLOCK (task);
 
-    old_data = task->priv->thr_user_data;
+    old_notify (old_data);
 
-    task->priv->thr_user_data = NULL;
-    task->priv->thr_notify = NULL;
+    GST_OBJECT_LOCK (task);
+  }
+  task->priv->enter_func = enter_func;
+  task->priv->enter_user_data = user_data;
+  task->priv->enter_notify = notify;
+  GST_OBJECT_UNLOCK (task);
+}
+
+/**
+ * gst_task_set_leave_callback:
+ * @task: The #GstTask to use
+ * @leave_func: (in): a #GstTaskThreadFunc
+ * @user_data: user data passed to @leave_func
+ * @notify: called when @user_data is no longer referenced
+ *
+ * Call @leave_func when the task function of @task is left. @user_data will
+ * be passed to @leave_func and @notify will be called when @user_data is no
+ * longer referenced.
+ */
+void
+gst_task_set_leave_callback (GstTask * task, GstTaskThreadFunc leave_func,
+    gpointer user_data, GDestroyNotify notify)
+{
+  GDestroyNotify old_notify;
+
+  g_return_if_fail (task != NULL);
+  g_return_if_fail (GST_IS_TASK (task));
+
+  GST_OBJECT_LOCK (task);
+  if ((old_notify = task->priv->leave_notify)) {
+    gpointer old_data = task->priv->leave_user_data;
+
+    task->priv->leave_user_data = NULL;
+    task->priv->leave_notify = NULL;
     GST_OBJECT_UNLOCK (task);
 
     old_notify (old_data);
 
     GST_OBJECT_LOCK (task);
   }
-  task->priv->thr_callbacks = *callbacks;
-  task->priv->thr_user_data = user_data;
-  task->priv->thr_notify = notify;
+  task->priv->leave_func = leave_func;
+  task->priv->leave_user_data = user_data;
+  task->priv->leave_notify = notify;
   GST_OBJECT_UNLOCK (task);
 }
 
@@ -679,8 +650,6 @@ start_task (GstTask * task)
  * MT safe.
  *
  * Returns: %TRUE if the state could be changed.
- *
- * Since: 0.10.24
  */
 gboolean
 gst_task_set_state (GstTask * task, GstTaskState state)