#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
}
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 ();
for (i = 0; i < 5; i++)
{
- if (g_thread_pool_get_num_unused_threads () == 0 &&
- test_count_threads () == 0)
+ if (g_thread_pool_get_num_unused_threads () == 0)
break;
DEBUG_MSG (("[unused] waiting ONE second for threads to die"));
g_usleep (G_USEC_PER_SEC);
}
- DEBUG_MSG (("[unused] stopped idle threads, %d remain, %d threads still exist",
- g_thread_pool_get_num_unused_threads (),
- test_count_threads ()));
+ 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));
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));