#include <glib.h>
-#define DEBUG_MSG(x)
-/* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
+/* #define DEBUG 1 */
+
+#ifdef DEBUG
+# define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");
+#else
+# define DEBUG_MSG(x)
+#endif
#define WAIT 5 /* seconds */
#define MAX_THREADS 10
-/* if > 0 the test will run continously (since the test ends when
+/* if > 0 the test will run continuously (since the test ends when
* thread count is 0), if -1 it means no limit to unused threads
* if 0 then no unused threads are possible */
#define MAX_UNUSED_THREADS -1
}
static void
-test_count_threads_foreach (GThread *thread,
- guint *count)
-{
- ++*count;
-}
-
-static guint
-test_count_threads (void)
-{
- guint count = 0;
-
- g_thread_foreach ((GFunc) test_count_threads_foreach, &count);
-
- /* Exclude main thread */
- return count - 1;
-}
-
-static void
test_thread_stop_unused (void)
{
GThreadPool *pool;
/* Wait for the threads to migrate. */
g_usleep (G_USEC_PER_SEC);
- DEBUG_MSG (("[unused] current threads %d",
- test_count_threads()));
-
DEBUG_MSG (("[unused] stopping unused threads"));
g_thread_pool_stop_unused_threads ();
- DEBUG_MSG (("[unused] waiting ONE second for threads to die"));
+ for (i = 0; i < 5; i++)
+ {
+ if (g_thread_pool_get_num_unused_threads () == 0)
+ break;
- /* Some time for threads to die. */
- g_usleep (G_USEC_PER_SEC);
+ DEBUG_MSG (("[unused] waiting ONE second for threads to die"));
- DEBUG_MSG (("[unused] stopped idle threads, %d remain, %d threads still exist",
- g_thread_pool_get_num_unused_threads (),
- test_count_threads ()));
+ /* Some time for threads to die. */
+ g_usleep (G_USEC_PER_SEC);
+ }
+
+ DEBUG_MSG (("[unused] stopped idle threads, %d remain",
+ g_thread_pool_get_num_unused_threads ()));
- g_assert (g_thread_pool_get_num_unused_threads () == test_count_threads ());
g_assert (g_thread_pool_get_num_unused_threads () == 0);
g_thread_pool_set_max_unused_threads (MAX_THREADS);
static void
test_thread_pools_entry_func (gpointer data, gpointer user_data)
{
+#ifdef DEBUG
guint id = 0;
id = GPOINTER_TO_UINT (data);
+#endif
DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id));
}
/* It is important that we only have a maximum of 1 thread for this
- * test since the results can not be guranteed to be sorted if > 1.
+ * test since the results can not be guaranteed to be sorted if > 1.
*
* Threads are scheduled by the operating system and are executed at
* random. It cannot be assumed that threads are executed in the
static void
test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
{
+#ifdef DEBUG
guint thread_id;
thread_id = GPOINTER_TO_UINT (data);
+#endif
DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
static gboolean
test_thread_idle_timeout (gpointer data)
{
- guint interval;
gint i;
- interval = GPOINTER_TO_UINT (data);
-
for (i = 0; i < 2; i++) {
g_thread_pool_push (idle_pool, GUINT_TO_POINTER (100 + i), NULL);
DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, number "
}
static void
-test_thread_idle_time ()
+test_thread_idle_time (void)
{
guint limit = 50;
guint interval = 10000;
idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func,
NULL,
- MAX_THREADS,
+ 0,
FALSE,
NULL);
+ g_thread_pool_set_max_threads (idle_pool, MAX_THREADS, NULL);
g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
g_thread_pool_set_max_idle_time (interval);
+ g_assert (g_thread_pool_get_max_threads (idle_pool) == MAX_THREADS);
g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);
g_assert (g_thread_pool_get_max_idle_time () == interval);
g_thread_pool_unprocessed (idle_pool)));
}
+ g_assert_cmpint (g_thread_pool_unprocessed (idle_pool), <=, limit);
+
g_timeout_add ((interval - 1000),
test_thread_idle_timeout,
GUINT_TO_POINTER (interval));
int
main (int argc, char *argv[])
{
- /* Only run the test, if threads are enabled and a default thread
- implementation is available */
-
-#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
g_thread_init (NULL);
DEBUG_MSG (("Starting... (in one second)"));
main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (main_loop);
-#endif
return 0;
}