taskpool: add new object to manage threads
authorWim Taymans <wim.taymans@collabora.co.uk>
Thu, 23 Apr 2009 13:44:13 +0000 (15:44 +0200)
committerWim Taymans <wim@metal.(none)>
Mon, 11 May 2009 22:22:19 +0000 (00:22 +0200)
Add a new object GstTaskPool to manage the streaming threads.
This will allow us to create and use custom configured threads.

docs/gst/gstreamer-docs.sgml
docs/gst/gstreamer-sections.txt
gst/Makefile.am
gst/gst.h
gst/gsttaskpool.c [new file with mode: 0644]
gst/gsttaskpool.h [new file with mode: 0644]

index 76e9378..c0051e5 100644 (file)
@@ -86,6 +86,7 @@ Windows.  It is released under the GNU Library General Public License
     <xi:include href="xml/gsttaglist.xml" />
     <xi:include href="xml/gsttagsetter.xml" />
     <xi:include href="xml/gsttask.xml" />
+    <xi:include href="xml/gsttaskpool.xml" />
     <xi:include href="xml/gsttypefind.xml" />
     <xi:include href="xml/gsttypefindfactory.xml" />
     <xi:include href="xml/gsturihandler.xml" />
index c696d89..8bd2a58 100644 (file)
@@ -2168,6 +2168,29 @@ GST_TYPE_TAG_SETTER
 gst_tag_setter_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gsttaskpool</FILE>
+<TITLE>GstTaskPool</TITLE>
+GstTaskPool
+GstTaskPoolClass
+gst_task_pool_new
+gst_task_pool_set_func
+gst_task_pool_prepare
+gst_task_pool_join
+gst_task_pool_push
+gst_task_pool_cleanup
+<SUBSECTION Standard>
+GST_IS_TASK_POOL
+GST_IS_TASK_POOL_CLASS
+GST_TASK_POOL
+GST_TASK_POOL_CAST
+GST_TASK_POOL_CLASS
+GST_TASK_POOL_GET_CLASS
+GST_TYPE_TASK_POOL
+<SUBSECTION Private>
+gst_task_pool_get_type
+</SECTION>
+
 
 <SECTION>
 <FILE>gsttask</FILE>
index d67ea80..ebf64d4 100644 (file)
@@ -96,6 +96,7 @@ libgstreamer_@GST_MAJORMINOR@_la_SOURCES = \
        gsttaglist.c            \
        gsttagsetter.c          \
        gsttask.c               \
+       gsttaskpool.c           \
        $(GST_TRACE_SRC)        \
        gsttypefind.c           \
        gsttypefindfactory.c    \
@@ -181,6 +182,7 @@ gst_headers =                       \
        gsttaglist.h            \
        gsttagsetter.h          \
        gsttask.h               \
+       gsttaskpool.h           \
        gsttrace.h              \
        gsttypefind.h           \
        gsttypefindfactory.h    \
index 9c5200a..8920de9 100644 (file)
--- a/gst/gst.h
+++ b/gst/gst.h
@@ -64,6 +64,7 @@
 #include <gst/gsttaglist.h>
 #include <gst/gsttagsetter.h>
 #include <gst/gsttask.h>
+#include <gst/gsttaskpool.h>
 #include <gst/gsttrace.h>
 #include <gst/gsttypefind.h>
 #include <gst/gsttypefindfactory.h>
diff --git a/gst/gsttaskpool.c b/gst/gsttaskpool.c
new file mode 100644 (file)
index 0000000..a123e25
--- /dev/null
@@ -0,0 +1,249 @@
+/* GStreamer
+ * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
+ *
+ * gsttaskpool.c: Pool for streaming threads
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * SECTION:gsttaskpool
+ * @short_description: Pool of GStreamer streaming threads
+ * @see_also: #GstTask, #GstPad
+ *
+ * This object provides an abstraction for creating threads. The default
+ * implementation uses a regular GThreadPool to start tasks.
+ *
+ * Subclasses can be made to create custom threads.
+ *
+ * Last reviewed on 2009-04-23 (0.10.24)
+ */
+
+#include "gst_private.h"
+
+#include "gstinfo.h"
+#include "gsttaskpool.h"
+
+GST_DEBUG_CATEGORY_STATIC (taskpool_debug);
+#define GST_CAT_DEFAULT (taskpool_debug)
+
+static void gst_task_pool_class_init (GstTaskPoolClass * klass);
+static void gst_task_pool_init (GstTaskPool * pool);
+static void gst_task_pool_finalize (GObject * object);
+
+#define _do_init \
+{ \
+  GST_DEBUG_CATEGORY_INIT (taskpool_debug, "taskpool", 0, "Thread pool"); \
+}
+
+G_DEFINE_TYPE_WITH_CODE (GstTaskPool, gst_task_pool, GST_TYPE_OBJECT, _do_init);
+
+static void
+default_prepare (GstTaskPool * pool, GFunc func, gpointer user_data,
+    GError ** error)
+{
+  GST_OBJECT_LOCK (pool);
+  pool->pool = g_thread_pool_new ((GFunc) func, user_data, -1, FALSE, NULL);
+  GST_OBJECT_UNLOCK (pool);
+}
+
+static void
+default_cleanup (GstTaskPool * pool)
+{
+  GST_OBJECT_LOCK (pool);
+  if (pool->pool) {
+    /* Shut down all the threads, we still process the ones scheduled
+     * because the unref happens in the thread function.
+     * Also wait for currently running ones to finish. */
+    g_thread_pool_free (pool->pool, FALSE, TRUE);
+    pool->pool = NULL;
+  }
+  GST_OBJECT_UNLOCK (pool);
+}
+
+static GThread *
+default_push (GstTaskPool * pool, gpointer data, GError ** error)
+{
+  GST_OBJECT_LOCK (pool);
+  if (pool->pool)
+    g_thread_pool_push (pool->pool, data, error);
+  GST_OBJECT_UNLOCK (pool);
+
+  return NULL;
+}
+
+static void
+default_join (GstTaskPool * pool, GThread * thread)
+{
+  /* does nothing, we can't join for threads from the threadpool */
+}
+
+static void
+gst_task_pool_class_init (GstTaskPoolClass * klass)
+{
+  GObjectClass *gobject_class;
+  GstTaskPoolClass *gsttaskpool_class;
+
+  gobject_class = (GObjectClass *) klass;
+  gsttaskpool_class = (GstTaskPoolClass *) klass;
+
+  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_task_pool_finalize);
+
+  gsttaskpool_class->prepare = default_prepare;
+  gsttaskpool_class->cleanup = default_cleanup;
+  gsttaskpool_class->push = default_push;
+  gsttaskpool_class->join = default_join;
+}
+
+static void
+gst_task_pool_init (GstTaskPool * pool)
+{
+}
+
+static void
+gst_task_pool_finalize (GObject * object)
+{
+  GstTaskPool *pool = GST_TASK_POOL (object);
+
+  GST_DEBUG ("taskpool %p finalize", pool);
+
+  G_OBJECT_CLASS (gst_task_pool_parent_class)->finalize (object);
+}
+
+/**
+ * gst_task_pool_new:
+ *
+ * Create a new default task pool. The default task pool will use a regular
+ * GThreadPool for threads.
+ *
+ * Returns: a new #GstTaskPool. gst_object_unref() after usage.
+ */
+GstTaskPool *
+gst_task_pool_new (void)
+{
+  GstTaskPool *pool;
+
+  pool = g_object_new (GST_TYPE_TASK_POOL, NULL);
+
+  return pool;
+}
+
+void
+gst_task_pool_set_func (GstTaskPool * pool, GFunc func, gpointer user_data)
+{
+  g_return_if_fail (GST_IS_TASK_POOL (pool));
+
+  pool->func = func;
+  pool->user_data = user_data;
+}
+
+
+/**
+ * gst_task_pool_prepare:
+ * @pool: a #GstTaskPool
+ * @error: an error return location
+ *
+ * Prepare the taskpool for accepting gst_task_pool_push() operations.
+ *
+ * MT safe.
+ */
+void
+gst_task_pool_prepare (GstTaskPool * pool, GError ** error)
+{
+  GstTaskPoolClass *klass;
+
+  g_return_if_fail (GST_IS_TASK_POOL (pool));
+
+  klass = GST_TASK_POOL_GET_CLASS (pool);
+
+  if (klass->prepare)
+    klass->prepare (pool, pool->func, pool->user_data, error);
+}
+
+/**
+ * gst_task_pool_cleanup:
+ * @pool: a #GstTaskPool
+ *
+ * Wait for all tasks to be stopped. This is mainly used internally
+ * to ensure proper cleanup of internal data structures in test suites.
+ *
+ * MT safe.
+ */
+void
+gst_task_pool_cleanup (GstTaskPool * pool)
+{
+  GstTaskPoolClass *klass;
+
+  g_return_if_fail (GST_IS_TASK_POOL (pool));
+
+  klass = GST_TASK_POOL_GET_CLASS (pool);
+
+  if (klass->cleanup)
+    klass->cleanup (pool);
+}
+
+/**
+ * gst_task_pool_push:
+ * @pool: a #GstTaskPool
+ * @data: data to pass to the thread function
+ * @error: return location for an error
+ *
+ * Start the execution of a new thread from @pool.
+ *
+ * Returns: a #GThread or NULL when the GThread is not yet known. You must check
+ * @error to detect errors.
+ */
+GThread *
+gst_task_pool_push (GstTaskPool * pool, gpointer data, GError ** error)
+{
+  GstTaskPoolClass *klass;
+
+  g_return_val_if_fail (GST_IS_TASK_POOL (pool), NULL);
+
+  klass = GST_TASK_POOL_GET_CLASS (pool);
+
+  if (klass->push == NULL)
+    goto not_supported;
+
+  return klass->push (pool, data, error);
+
+  /* ERRORS */
+not_supported:
+  {
+    g_warning ("pushing tasks on pool %p is not supported", pool);
+    return NULL;
+  }
+}
+
+/**
+ * gst_task_pool_join:
+ * @pool: a #GstTaskPool
+ * @thread: a #GThread
+ *
+ * Join a GThread or return it to the pool.
+ */
+void
+gst_task_pool_join (GstTaskPool * pool, GThread * thread)
+{
+  GstTaskPoolClass *klass;
+
+  g_return_if_fail (GST_IS_TASK_POOL (pool));
+
+  klass = GST_TASK_POOL_GET_CLASS (pool);
+
+  if (klass->join)
+    klass->join (pool, thread);
+}
diff --git a/gst/gsttaskpool.h b/gst/gsttaskpool.h
new file mode 100644 (file)
index 0000000..35bb348
--- /dev/null
@@ -0,0 +1,98 @@
+/* GStreamer
+ * Copyright (C) <2009> Wim Taymans <wim.taymans@gmail.com>
+ *
+ * gsttaskpool.h: Pool for creating streaming threads
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GST_TASK_POOL_H__
+#define __GST_TASK_POOL_H__
+
+#include <gst/gstobject.h>
+
+G_BEGIN_DECLS
+
+/* --- standard type macros --- */
+#define GST_TYPE_TASK_POOL             (gst_task_pool_get_type ())
+#define GST_TASK_POOL(pool)            (G_TYPE_CHECK_INSTANCE_CAST ((pool), GST_TYPE_TASK_POOL, GstTaskPool))
+#define GST_IS_TASK_POOL(pool)         (G_TYPE_CHECK_INSTANCE_TYPE ((pool), GST_TYPE_TASK_POOL))
+#define GST_TASK_POOL_CLASS(pclass)    (G_TYPE_CHECK_CLASS_CAST ((pclass), GST_TYPE_TASK_POOL, GstTaskPoolClass))
+#define GST_IS_TASK_POOL_CLASS(pclass) (G_TYPE_CHECK_CLASS_TYPE ((pclass), GST_TYPE_TASK_POOL))
+#define GST_TASK_POOL_GET_CLASS(pool)  (G_TYPE_INSTANCE_GET_CLASS ((pool), GST_TYPE_TASK_POOL, GstTaskPoolClass))
+#define GST_TASK_POOL_CAST(pool)       ((GstTaskPool*)(pool))
+
+typedef struct _GstTaskPool GstTaskPool;
+typedef struct _GstTaskPoolClass GstTaskPoolClass;
+
+/**
+ * GstTaskPool:
+ *
+ * The #GstTaskPool object.
+ */
+struct _GstTaskPool {
+  GstObject      object;
+
+  /*< private >*/
+  GFunc          func;
+  gpointer       user_data;
+
+  GThreadPool   *pool;
+
+  gpointer _gst_reserved[GST_PADDING];
+};
+
+/**
+ * GstTaskPoolClass:
+ * @init: initialize the threadpool
+ * @cleanup: make sure all threads are stopped
+ * @push: start a new thread
+ * @join: join a thread
+ *
+ * The #GstTaskPoolClass object.
+ */
+struct _GstTaskPoolClass {
+  GstObjectClass parent_class;
+
+  void      (*prepare)  (GstTaskPool *pool, GFunc func,
+                         gpointer user_data, GError **error);
+  void      (*cleanup)  (GstTaskPool *pool);
+
+  GThread * (*push)     (GstTaskPool *pool, gpointer data, GError **error);
+  void      (*join)     (GstTaskPool *pool, GThread *thread);
+
+  /*< private >*/
+  gpointer _gst_reserved[GST_PADDING];
+};
+
+GType           gst_task_pool_get_type    (void);
+
+GstTaskPool *   gst_task_pool_new         (void);
+
+void            gst_task_pool_set_func    (GstTaskPool *pool,
+                                           GFunc func, gpointer user_data);
+
+void            gst_task_pool_prepare     (GstTaskPool *pool, GError **error);
+
+GThread *       gst_task_pool_push        (GstTaskPool *pool, gpointer data,
+                                           GError **error);
+void            gst_task_pool_join        (GstTaskPool *pool, GThread *thread);
+
+void           gst_task_pool_cleanup     (GstTaskPool *pool);
+
+G_END_DECLS
+
+#endif /* __GST_TASK_H__ */