sl@0: /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
sl@0: #undef G_DISABLE_ASSERT
sl@0: #undef G_LOG_DOMAIN
sl@0: 
sl@0: #include "config.h"
sl@0: 
sl@0: #include <glib.h>
sl@0: 
sl@0: #ifdef __SYMBIAN32__
sl@0: #include <glib_global.h>
sl@0: #include "mrt2_glib2_test.h"
sl@0: #endif /*__SYMBIAN32__*/
sl@0: #define DEBUG_MSG(x)
sl@0: /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");  */
sl@0: 
sl@0: #define WAIT                5    /* seconds */
sl@0: #define MAX_THREADS         10
sl@0: 
sl@0: /* if > 0 the test will run continously (since the test ends when
sl@0:  * thread count is 0), if -1 it means no limit to unused threads
sl@0:  * if 0 then no unused threads are possible */
sl@0: #define MAX_UNUSED_THREADS -1
sl@0: 
sl@0: G_LOCK_DEFINE_STATIC (thread_counter_pools);
sl@0: 
sl@0: static gulong abs_thread_counter = 0;
sl@0: static gulong running_thread_counter = 0;
sl@0: static gulong leftover_task_counter = 0;
sl@0: 
sl@0: G_LOCK_DEFINE_STATIC (last_thread);
sl@0: 
sl@0: static guint last_thread_id = 0;
sl@0: 
sl@0: G_LOCK_DEFINE_STATIC (thread_counter_sort);
sl@0: 
sl@0: static gulong sort_thread_counter = 0;
sl@0: 
sl@0: static GThreadPool *idle_pool = NULL;
sl@0: 
sl@0: static GMainLoop *main_loop = NULL;
sl@0: 
sl@0: static void
sl@0: test_thread_functions (void)
sl@0: {
sl@0:   gint max_unused_threads;
sl@0:   guint max_idle_time;
sl@0: 
sl@0:   /* This function attempts to call functions which don't need a
sl@0:    * threadpool to operate to make sure no uninitialised pointers
sl@0:    * accessed and no errors occur.
sl@0:    */
sl@0: 
sl@0:   max_unused_threads = 3;
sl@0: 
sl@0:   DEBUG_MSG (("[funcs] Setting max unused threads to %d",
sl@0: 	      max_unused_threads));
sl@0:   g_thread_pool_set_max_unused_threads (max_unused_threads);
sl@0: 
sl@0:   DEBUG_MSG (("[funcs] Getting max unused threads = %d",
sl@0: 	     g_thread_pool_get_max_unused_threads ()));
sl@0:   g_assert (g_thread_pool_get_max_unused_threads() == max_unused_threads);
sl@0: 
sl@0:   DEBUG_MSG (("[funcs] Getting num unused threads = %d",
sl@0: 	     g_thread_pool_get_num_unused_threads ()));
sl@0:   g_assert (g_thread_pool_get_num_unused_threads () == 0);
sl@0: 
sl@0:   DEBUG_MSG (("[funcs] Stopping unused threads"));
sl@0:   g_thread_pool_stop_unused_threads ();
sl@0: 
sl@0:   max_idle_time = 10 * G_USEC_PER_SEC;
sl@0: 
sl@0:   DEBUG_MSG (("[funcs] Setting max idle time to %d",
sl@0: 	      max_idle_time));
sl@0:   g_thread_pool_set_max_idle_time (max_idle_time);
sl@0: 
sl@0:   DEBUG_MSG (("[funcs] Getting max idle time = %d",
sl@0: 	     g_thread_pool_get_max_idle_time ()));
sl@0:   g_assert (g_thread_pool_get_max_idle_time () == max_idle_time);
sl@0: 
sl@0:   DEBUG_MSG (("[funcs] Setting max idle time to 0"));
sl@0:   g_thread_pool_set_max_idle_time (0);
sl@0: 
sl@0:   DEBUG_MSG (("[funcs] Getting max idle time = %d",
sl@0: 	     g_thread_pool_get_max_idle_time ()));
sl@0:   g_assert (g_thread_pool_get_max_idle_time () == 0);
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_count_threads_foreach (GThread *thread,
sl@0: 			    guint   *count)
sl@0: {
sl@0:    ++*count;
sl@0: }
sl@0: 
sl@0: static guint
sl@0: test_count_threads (void)
sl@0: {
sl@0:   guint count = 0;
sl@0: 
sl@0:   g_thread_foreach ((GFunc) test_count_threads_foreach, &count);
sl@0: 
sl@0:   /* Exclude main thread */
sl@0:   return count - 1;
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_thread_stop_unused (void)
sl@0: {
sl@0:    GThreadPool *pool;
sl@0:    guint i;
sl@0:    guint limit = 100;
sl@0: 
sl@0:    /* Spawn a few threads. */
sl@0:    g_thread_pool_set_max_unused_threads (-1);
sl@0:    pool = g_thread_pool_new ((GFunc) g_usleep, NULL, -1, FALSE, NULL);
sl@0: 
sl@0:    for (i = 0; i < limit; i++)
sl@0:      g_thread_pool_push (pool, GUINT_TO_POINTER (1000), NULL);
sl@0: 
sl@0:    DEBUG_MSG (("[unused] ===> pushed %d threads onto the idle pool",
sl@0: 	       limit));
sl@0: 
sl@0:    /* Wait for the threads to migrate. */
sl@0:    g_usleep (G_USEC_PER_SEC);
sl@0: 
sl@0:    DEBUG_MSG (("[unused] current threads %d",
sl@0: 	       test_count_threads()));
sl@0: 
sl@0:    DEBUG_MSG (("[unused] stopping unused threads"));
sl@0:    g_thread_pool_stop_unused_threads ();
sl@0: 
sl@0:    DEBUG_MSG (("[unused] waiting ONE second for threads to die"));
sl@0: 
sl@0:    /* Some time for threads to die. */
sl@0:    g_usleep (G_USEC_PER_SEC);
sl@0: 
sl@0:    DEBUG_MSG (("[unused] stopped idle threads, %d remain, %d threads still exist",
sl@0: 	       g_thread_pool_get_num_unused_threads (),
sl@0: 	       test_count_threads ()));
sl@0: 
sl@0:    g_assert (g_thread_pool_get_num_unused_threads () == test_count_threads ());
sl@0:    g_assert (g_thread_pool_get_num_unused_threads () == 0);
sl@0: 
sl@0:    g_thread_pool_set_max_unused_threads (MAX_THREADS);
sl@0: 
sl@0:    DEBUG_MSG (("[unused] cleaning up thread pool"));
sl@0:    g_thread_pool_free (pool, FALSE, TRUE);
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_thread_pools_entry_func (gpointer data, gpointer user_data)
sl@0: {
sl@0:   guint id = 0;
sl@0: 
sl@0:   id = GPOINTER_TO_UINT (data);
sl@0: 
sl@0:   DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id));
sl@0: 
sl@0:   G_LOCK (thread_counter_pools);
sl@0:   abs_thread_counter++;
sl@0:   running_thread_counter++;
sl@0:   G_UNLOCK (thread_counter_pools);
sl@0: 
sl@0:   g_usleep (g_random_int_range (0, 4000));
sl@0: 
sl@0:   G_LOCK (thread_counter_pools);
sl@0:   running_thread_counter--;
sl@0:   leftover_task_counter--;
sl@0: 
sl@0:   DEBUG_MSG (("[pool] ---> [%3.3d] exiting thread (abs count:%ld, "
sl@0: 	      "running count:%ld, left over:%ld)",
sl@0: 	      id, abs_thread_counter,
sl@0: 	      running_thread_counter, leftover_task_counter));
sl@0:   G_UNLOCK (thread_counter_pools);
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_thread_pools (void)
sl@0: {
sl@0:   GThreadPool *pool1, *pool2, *pool3;
sl@0:   guint runs;
sl@0:   guint i;
sl@0: 
sl@0:   pool1 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 3, FALSE, NULL);
sl@0:   pool2 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 5, TRUE, NULL);
sl@0:   pool3 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 7, TRUE, NULL);
sl@0: 
sl@0:   runs = 300;
sl@0:   for (i = 0; i < runs; i++)
sl@0:     {
sl@0:       g_thread_pool_push (pool1, GUINT_TO_POINTER (i + 1), NULL);
sl@0:       g_thread_pool_push (pool2, GUINT_TO_POINTER (i + 1), NULL);
sl@0:       g_thread_pool_push (pool3, GUINT_TO_POINTER (i + 1), NULL);
sl@0: 
sl@0:       G_LOCK (thread_counter_pools);
sl@0:       leftover_task_counter += 3;
sl@0:       G_UNLOCK (thread_counter_pools);
sl@0:     }
sl@0: 
sl@0:   g_thread_pool_free (pool1, TRUE, TRUE);
sl@0:   g_thread_pool_free (pool2, FALSE, TRUE);
sl@0:   g_thread_pool_free (pool3, FALSE, TRUE);
sl@0: 
sl@0:   g_assert (runs * 3 == abs_thread_counter + leftover_task_counter);
sl@0:   g_assert (running_thread_counter == 0);
sl@0: }
sl@0: 
sl@0: static gint
sl@0: test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
sl@0: {
sl@0:   guint32 id1, id2;
sl@0: 
sl@0:   id1 = GPOINTER_TO_UINT (a);
sl@0:   id2 = GPOINTER_TO_UINT (b);
sl@0: 
sl@0:   return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_thread_sort_entry_func (gpointer data, gpointer user_data)
sl@0: {
sl@0:   guint thread_id;
sl@0:   gboolean is_sorted;
sl@0: 
sl@0:   G_LOCK (last_thread);
sl@0: 
sl@0:   thread_id = GPOINTER_TO_UINT (data);
sl@0:   is_sorted = GPOINTER_TO_INT (user_data);
sl@0: 
sl@0:   DEBUG_MSG (("%s ---> entered thread:%2.2d, last thread:%2.2d",
sl@0: 	      is_sorted ? "[  sorted]" : "[unsorted]",
sl@0: 	      thread_id, last_thread_id));
sl@0: 
sl@0:   if (is_sorted) {
sl@0:     static gboolean last_failed = FALSE;
sl@0: 
sl@0:     if (last_thread_id > thread_id) {
sl@0:       if (last_failed) {
sl@0: 	g_assert (last_thread_id <= thread_id);
sl@0:       }
sl@0: 
sl@0:       /* Here we remember one fail and if it concurrently fails, it
sl@0:        * can not be sorted. the last thread id might be < this thread
sl@0:        * id if something is added to the queue since threads were
sl@0:        * created
sl@0:        */
sl@0:       last_failed = TRUE;
sl@0:     } else {
sl@0:       last_failed = FALSE;
sl@0:     }
sl@0: 
sl@0:     last_thread_id = thread_id;
sl@0:   }
sl@0: 
sl@0:   G_UNLOCK (last_thread);
sl@0: 
sl@0:   g_usleep (WAIT * 1000);
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_thread_sort (gboolean sort)
sl@0: {
sl@0:   GThreadPool *pool;
sl@0:   guint limit;
sl@0:   guint max_threads;
sl@0:   gint i;
sl@0: 
sl@0:   limit = MAX_THREADS * 10;
sl@0: 
sl@0:   if (sort) {
sl@0:     max_threads = 1;
sl@0:   } else {
sl@0:     max_threads = MAX_THREADS;
sl@0:   }
sl@0: 
sl@0:   /* It is important that we only have a maximum of 1 thread for this
sl@0:    * test since the results can not be guranteed to be sorted if > 1.
sl@0:    *
sl@0:    * Threads are scheduled by the operating system and are executed at
sl@0:    * random. It cannot be assumed that threads are executed in the
sl@0:    * order they are created. This was discussed in bug #334943.
sl@0:    */
sl@0: 
sl@0:   pool = g_thread_pool_new (test_thread_sort_entry_func,
sl@0: 			    GINT_TO_POINTER (sort),
sl@0: 			    max_threads,
sl@0: 			    FALSE,
sl@0: 			    NULL);
sl@0: 
sl@0:   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
sl@0: 
sl@0:   if (sort) {
sl@0:     g_thread_pool_set_sort_function (pool,
sl@0: 				     test_thread_sort_compare_func,
sl@0: 				     GUINT_TO_POINTER (69));
sl@0:   }
sl@0: 
sl@0:   for (i = 0; i < limit; i++) {
sl@0:     guint id;
sl@0: 
sl@0:     id = g_random_int_range (1, limit) + 1;
sl@0:     g_thread_pool_push (pool, GUINT_TO_POINTER (id), NULL);
sl@0:     DEBUG_MSG (("%s ===> pushed new thread with id:%d, number "
sl@0: 		"of threads:%d, unprocessed:%d",
sl@0: 		sort ? "[  sorted]" : "[unsorted]",
sl@0: 		id,
sl@0: 		g_thread_pool_get_num_threads (pool),
sl@0: 		g_thread_pool_unprocessed (pool)));
sl@0:   }
sl@0: 
sl@0:   g_assert (g_thread_pool_get_max_threads (pool) == max_threads);
sl@0:   g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool));
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
sl@0: {
sl@0:   guint thread_id;
sl@0: 
sl@0:   thread_id = GPOINTER_TO_UINT (data);
sl@0: 
sl@0:   DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
sl@0: 
sl@0:   g_usleep (WAIT * 1000);
sl@0: 
sl@0:   DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id));
sl@0: }
sl@0: 
sl@0: static gboolean
sl@0: test_thread_idle_timeout (gpointer data)
sl@0: {
sl@0:   guint interval;
sl@0:   gint i;
sl@0: 
sl@0:   interval = GPOINTER_TO_UINT (data);
sl@0: 
sl@0:   for (i = 0; i < 2; i++) {
sl@0:     g_thread_pool_push (idle_pool, GUINT_TO_POINTER (100 + i), NULL);
sl@0:     DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, number "
sl@0: 		"of threads:%d, unprocessed:%d",
sl@0: 		100 + i,
sl@0: 		g_thread_pool_get_num_threads (idle_pool),
sl@0: 		g_thread_pool_unprocessed (idle_pool)));
sl@0:   }
sl@0: 
sl@0: 
sl@0:   return FALSE;
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_thread_idle_time ()
sl@0: {
sl@0:   guint limit = 50;
sl@0:   guint interval = 10000;
sl@0:   gint i;
sl@0: 
sl@0:   idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func,
sl@0: 				 NULL,
sl@0: 				 MAX_THREADS,
sl@0: 				 FALSE,
sl@0: 				 NULL);
sl@0: 
sl@0:   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
sl@0:   g_thread_pool_set_max_idle_time (interval);
sl@0: 
sl@0:   g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);
sl@0:   g_assert (g_thread_pool_get_max_idle_time () == interval);
sl@0: 
sl@0:   for (i = 0; i < limit; i++) {
sl@0:     g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL);
sl@0:     DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, "
sl@0: 		"number of threads:%d, unprocessed:%d",
sl@0: 		i,
sl@0: 		g_thread_pool_get_num_threads (idle_pool),
sl@0: 		g_thread_pool_unprocessed (idle_pool)));
sl@0:   }
sl@0: 
sl@0:   g_timeout_add ((interval - 1000),
sl@0: 		 test_thread_idle_timeout,
sl@0: 		 GUINT_TO_POINTER (interval));
sl@0: }
sl@0: 
sl@0: static gboolean
sl@0: test_check_start_and_stop (gpointer user_data)
sl@0: {
sl@0:   static guint test_number = 0;
sl@0:   static gboolean run_next = FALSE;
sl@0:   gboolean continue_timeout = TRUE;
sl@0:   gboolean quit = TRUE;
sl@0: 
sl@0:   if (test_number == 0) {
sl@0:     run_next = TRUE;
sl@0:     DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number));
sl@0:   }
sl@0: 
sl@0:   if (run_next) {
sl@0:     test_number++;
sl@0: 
sl@0:     switch (test_number) {
sl@0:     case 1:
sl@0:       test_thread_functions ();
sl@0:       break;
sl@0:     case 2:
sl@0:       test_thread_stop_unused ();
sl@0:       break;
sl@0:     case 3:
sl@0:       test_thread_pools ();
sl@0:       break;
sl@0:     case 4:
sl@0:       test_thread_sort (FALSE);
sl@0:       break;
sl@0:     case 5:
sl@0:       test_thread_sort (TRUE);
sl@0:       break;
sl@0:     case 6:
sl@0:       test_thread_stop_unused ();
sl@0:       break;
sl@0:     case 7:
sl@0:       test_thread_idle_time ();
sl@0:       break;
sl@0:     default:
sl@0:       DEBUG_MSG (("***** END OF TESTS *****"));
sl@0:       g_main_loop_quit (main_loop);
sl@0:       continue_timeout = FALSE;
sl@0:       break;
sl@0:     }
sl@0: 
sl@0:     run_next = FALSE;
sl@0:     return TRUE;
sl@0:   }
sl@0: 
sl@0:   if (test_number == 3) {
sl@0:     G_LOCK (thread_counter_pools);
sl@0:     quit &= running_thread_counter <= 0;
sl@0:     DEBUG_MSG (("***** POOL RUNNING THREAD COUNT:%ld",
sl@0: 		running_thread_counter));
sl@0:     G_UNLOCK (thread_counter_pools);
sl@0:   }
sl@0: 
sl@0:   if (test_number == 4 || test_number == 5) {
sl@0:     G_LOCK (thread_counter_sort);
sl@0:     quit &= sort_thread_counter <= 0;
sl@0:     DEBUG_MSG (("***** POOL SORT THREAD COUNT:%ld",
sl@0: 		sort_thread_counter));
sl@0:     G_UNLOCK (thread_counter_sort);
sl@0:   }
sl@0: 
sl@0:   if (test_number == 7) {
sl@0:     guint idle;
sl@0: 
sl@0:     idle = g_thread_pool_get_num_unused_threads ();
sl@0:     quit &= idle < 1;
sl@0:     DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d",
sl@0: 		idle, g_thread_pool_unprocessed (idle_pool)));
sl@0:   }
sl@0: 
sl@0:   if (quit) {
sl@0:     run_next = TRUE;
sl@0:   }
sl@0: 
sl@0:   return continue_timeout;
sl@0: }
sl@0: 
sl@0: int
sl@0: main (int argc, char *argv[])
sl@0: {
sl@0:   /* Only run the test, if threads are enabled and a default thread
sl@0:      implementation is available */
sl@0:   #ifdef __SYMBIAN32__
sl@0:   guint g_thread_pool_unprocessed_op = -1;
sl@0:   guint res;
sl@0:   #endif //__SYMBIAN32__
sl@0: 
sl@0: #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
sl@0:   g_thread_init (NULL);
sl@0: 
sl@0:   DEBUG_MSG (("Starting... (in one second)"));
sl@0:   g_timeout_add (1000, test_check_start_and_stop, NULL);
sl@0: 
sl@0:   main_loop = g_main_loop_new (NULL, FALSE);
sl@0:   g_main_loop_run (main_loop);
sl@0: #endif
sl@0: #ifdef __SYMBIAN32__
sl@0:   testResultXml("threadpool-test");
sl@0: #endif /* EMULATOR */
sl@0: 
sl@0:   return 0;
sl@0: }