[RFC] g_thread_pool_unprocessed ()



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi

In the GThreadPool API I'm missing a function which checks if all tasks
have been processed.

There is g_thread_pool_unprocessed. From the docs: "Returns the number of
tasks still unprocessed in pool." But the function doesn't account
currently running task.

I this a bug or should I add a new function to the API?

The attached patch adds a "busy_threads" member to the internal
GRealThreadPool struct which takes running tasks into account when
returning the number of unprocessed tasks.

Jan-Marek Glogowski
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDiszSj6MK58wZA3cRAvtWAJ9Mqs3QtiBk4PSuM30MKarv2fEc+gCcDykK
6ZW1sEYmwwuXgsVAqDkTims=
=X7EX
-----END PGP SIGNATURE-----
--- gthreadpool.c.orig	2005-11-27 17:08:06.059272440 +0100
+++ gthreadpool.c	2005-11-27 17:25:38.413290312 +0100
@@ -38,6 +38,7 @@
   GAsyncQueue* queue;
   gint max_threads;
   gint num_threads;
+  gint busy_threads;
   gboolean running;
   gboolean immediate;
   gboolean waiting;
@@ -110,9 +111,11 @@
 		}
 	      else if (pool->running || !pool->immediate)
 		{
+		  pool->busy_threads++;
 		  g_async_queue_unlock (pool->queue);
 		  pool->pool.func (task, pool->pool.user_data);
 		  g_async_queue_lock (pool->queue);
+		  pool->busy_threads--;
 		}
 	    }
 	  len = g_async_queue_length_unlocked (pool->queue);
@@ -292,6 +295,7 @@
   retval->queue = g_async_queue_new ();
   retval->max_threads = max_threads;
   retval->num_threads = 0;
+  retval->busy_threads = 0;
   retval->running = TRUE;
 
   G_LOCK (init);
@@ -497,9 +501,12 @@
   g_return_val_if_fail (real, 0);
   g_return_val_if_fail (real->running, 0);
 
-  unprocessed = g_async_queue_length (real->queue);
+  g_async_queue_lock (real->queue);
+  unprocessed = g_async_queue_length_unlocked (real->queue);
+  unprocessed = real->busy_threads + MAX (unprocessed, 0);
+  g_async_queue_unlock (real->queue);
 
-  return MAX (unprocessed, 0);
+  return unprocessed;
 }
 
 /**


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]