[maemo-commits] [maemo-commits] r15962 - in projects/haf/branches/hildon-thumbnail/daemonize: . daemon

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Tue Sep 2 18:43:17 EEST 2008
Author: pvanhoof
Date: 2008-09-02 18:43:15 +0300 (Tue, 02 Sep 2008)
New Revision: 15962

Modified:
   projects/haf/branches/hildon-thumbnail/daemonize/ChangeLog
   projects/haf/branches/hildon-thumbnail/daemonize/daemon/thumbnailer.c
Log:
2008-09-02  Philip Van Hoof  <philip at codeminded.be>

        * daemon/thumbnailer.c: Made the threadpool a LIFO



Modified: projects/haf/branches/hildon-thumbnail/daemonize/ChangeLog
===================================================================
--- projects/haf/branches/hildon-thumbnail/daemonize/ChangeLog	2008-09-02 15:22:52 UTC (rev 15961)
+++ projects/haf/branches/hildon-thumbnail/daemonize/ChangeLog	2008-09-02 15:43:15 UTC (rev 15962)
@@ -1,5 +1,9 @@
 2008-09-02  Philip Van Hoof  <philip at codeminded.be>
 
+	* daemon/thumbnailer.c: Made the threadpool a LIFO
+
+2008-09-02  Philip Van Hoof  <philip at codeminded.be>
+
 	* daemon/hildon-thumbnail-plugin.h
 	* daemon/hildon-thumbnail-daemon.c
 	* daemon/thumbnailer.h

Modified: projects/haf/branches/hildon-thumbnail/daemonize/daemon/thumbnailer.c
===================================================================
--- projects/haf/branches/hildon-thumbnail/daemonize/daemon/thumbnailer.c	2008-09-02 15:22:52 UTC (rev 15961)
+++ projects/haf/branches/hildon-thumbnail/daemonize/daemon/thumbnailer.c	2008-09-02 15:43:15 UTC (rev 15962)
@@ -27,6 +27,7 @@
 	DBusGConnection *connection;
 	Manager *manager;
 	GHashTable *plugins;
+	GThreadPool *pool;
 } ThumbnailerPrivate;
 
 enum {
@@ -112,19 +113,60 @@
 		g_list_free (value);
 }
 
+typedef struct {
+	Thumbnailer *object;
+	GStrv urls;
+	DBusGMethodInvocation *context;
+	guint num;
+} WorkTask;
+
+
+static gint 
+pool_sort_compare (gconstpointer a, gconstpointer b, gpointer user_data)
+{
+	WorkTask *task_a = (WorkTask *) a;
+	WorkTask *task_b = (WorkTask *) b;
+
+	/* This makes pool a LIFO */
+
+	return task_b->num - task_a->num;
+}
+
 void
 thumbnailer_create (Thumbnailer *object, GStrv urls, DBusGMethodInvocation *context)
 {
 	ThumbnailerPrivate *priv = THUMBNAILER_GET_PRIVATE (object);
+	WorkTask *task = g_slice_new (WorkTask);
+	guint urls_size = g_strv_length (urls), i = 0;
+	static gint num = 0;
+
+	task->num = num++;
+	task->object = g_object_ref (object);
+	task->urls = (GStrv) g_malloc0 (sizeof (gchar *) * (urls_size + 1));
+
+	while (urls[i] != NULL) {
+		task->urls[i] = g_strdup (urls[i]);
+		i++;
+	}
+
+	task->urls[i] = NULL;
+	task->context = context;
+
+	g_thread_pool_push (priv->pool, task, NULL);
+}
+
+static void 
+do_the_work (WorkTask *task, gpointer user_data)
+{
 	GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
 	guint i = 0;
 	GHashTableIter iter;
 	gpointer key, value;
 	gboolean had_error = FALSE;
+	ThumbnailerPrivate *priv = THUMBNAILER_GET_PRIVATE (task->object);
+	GStrv urls = task->urls;
+	DBusGMethodInvocation *context = task->context;
 
-	// TODO: dispatch this to an asynchronous handler that deals with items
-	// in a LIFO way. For example a GThreadPool
-
 	while (urls[i] != NULL) {
 		GList *urls_for_mime;
 		gchar *mime_type = get_mime_type (urls[i]);
@@ -143,7 +185,7 @@
 		i = 0;
 
 		while (copy) {
-			urlss[i] = copy->data;
+			urlss[i] = g_strdup ((gchar *) copy->data);
 			i++;
 			copy = g_list_next (copy);
 		}
@@ -204,6 +246,13 @@
 
 	g_hash_table_unref (hash);
 
+	/* task->context will always be returned by now */
+
+	g_object_unref (task->object);
+	g_strfreev (task->urls);
+	g_slice_free (WorkTask, task);
+
+	return;
 }
 
 void
@@ -221,11 +270,14 @@
 {
 	ThumbnailerPrivate *priv = THUMBNAILER_GET_PRIVATE (object);
 
+	g_thread_pool_free (priv->pool, TRUE, TRUE);
+
 	g_object_unref (priv->manager);
 	g_object_unref (priv->proxy);
 	g_object_unref (priv->connection);
 	g_hash_table_unref (priv->plugins);
 
+
 	G_OBJECT_CLASS (thumbnailer_parent_class)->finalize (object);
 }
 
@@ -351,6 +403,13 @@
 	priv->plugins = g_hash_table_new_full (g_str_hash, g_str_equal,
 					       (GDestroyNotify) g_free,
 					       NULL);
+
+	priv->pool = g_thread_pool_new ((GFunc) do_the_work, 
+					NULL, 1, TRUE, NULL);
+
+	g_thread_pool_set_sort_function (priv->pool, 
+					 pool_sort_compare, NULL);
+
 }
 
 


More information about the maemo-commits mailing list