| 
									
										
										
										
											2002-07-04 15:19:30 +00:00
										 |  |  | #undef G_DISABLE_ASSERT
 | 
					
						
							|  |  |  | #undef G_LOG_DOMAIN
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-22 15:10:51 +00:00
										 |  |  | #include "config.h"
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  | #include <glib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-30 06:31:20 -04:00
										 |  |  | /* #define DEBUG 1 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef DEBUG
 | 
					
						
							|  |  |  | # define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | # define DEBUG_MSG(x)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define WAIT                5    /* seconds */
 | 
					
						
							|  |  |  | #define MAX_THREADS         10
 | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-29 14:49:32 -04:00
										 |  |  | /* if > 0 the test will run continuously (since the test ends when
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |  * thread count is 0), if -1 it means no limit to unused threads | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |  * if 0 then no unused threads are possible */ | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | #define MAX_UNUSED_THREADS -1
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | G_LOCK_DEFINE_STATIC (thread_counter_pools); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static gulong abs_thread_counter = 0; | 
					
						
							|  |  |  | static gulong running_thread_counter = 0; | 
					
						
							|  |  |  | static gulong leftover_task_counter = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | G_LOCK_DEFINE_STATIC (last_thread); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static guint last_thread_id = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | G_LOCK_DEFINE_STATIC (thread_counter_sort); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static gulong sort_thread_counter = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | static GThreadPool *idle_pool = NULL; | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | static GMainLoop *main_loop = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | test_thread_functions (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   gint max_unused_threads; | 
					
						
							|  |  |  |   guint max_idle_time; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* This function attempts to call functions which don't need a
 | 
					
						
							|  |  |  |    * threadpool to operate to make sure no uninitialised pointers | 
					
						
							|  |  |  |    * accessed and no errors occur. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   max_unused_threads = 3; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   DEBUG_MSG (("[funcs] Setting max unused threads to %d", | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  | 	      max_unused_threads)); | 
					
						
							|  |  |  |   g_thread_pool_set_max_unused_threads (max_unused_threads); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   DEBUG_MSG (("[funcs] Getting max unused threads = %d", | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  | 	     g_thread_pool_get_max_unused_threads ())); | 
					
						
							|  |  |  |   g_assert (g_thread_pool_get_max_unused_threads() == max_unused_threads); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   DEBUG_MSG (("[funcs] Getting num unused threads = %d", | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  | 	     g_thread_pool_get_num_unused_threads ())); | 
					
						
							|  |  |  |   g_assert (g_thread_pool_get_num_unused_threads () == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   DEBUG_MSG (("[funcs] Stopping unused threads")); | 
					
						
							|  |  |  |   g_thread_pool_stop_unused_threads (); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   max_idle_time = 10 * G_USEC_PER_SEC; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   DEBUG_MSG (("[funcs] Setting max idle time to %d", | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  | 	      max_idle_time)); | 
					
						
							|  |  |  |   g_thread_pool_set_max_idle_time (max_idle_time); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   DEBUG_MSG (("[funcs] Getting max idle time = %d", | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  | 	     g_thread_pool_get_max_idle_time ())); | 
					
						
							|  |  |  |   g_assert (g_thread_pool_get_max_idle_time () == max_idle_time); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   DEBUG_MSG (("[funcs] Setting max idle time to 0")); | 
					
						
							|  |  |  |   g_thread_pool_set_max_idle_time (0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   DEBUG_MSG (("[funcs] Getting max idle time = %d", | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  | 	     g_thread_pool_get_max_idle_time ())); | 
					
						
							|  |  |  |   g_assert (g_thread_pool_get_max_idle_time () == 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | test_thread_stop_unused (void) | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |    GThreadPool *pool; | 
					
						
							|  |  |  |    guint i; | 
					
						
							|  |  |  |    guint limit = 100; | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |    /* Spawn a few threads. */ | 
					
						
							|  |  |  |    g_thread_pool_set_max_unused_threads (-1); | 
					
						
							|  |  |  |    pool = g_thread_pool_new ((GFunc) g_usleep, NULL, -1, FALSE, NULL); | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |    for (i = 0; i < limit; i++) | 
					
						
							|  |  |  |      g_thread_pool_push (pool, GUINT_TO_POINTER (1000), NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    DEBUG_MSG (("[unused] ===> pushed %d threads onto the idle pool", | 
					
						
							|  |  |  | 	       limit)); | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |    /* Wait for the threads to migrate. */ | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |    g_usleep (G_USEC_PER_SEC); | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    DEBUG_MSG (("[unused] stopping unused threads")); | 
					
						
							|  |  |  |    g_thread_pool_stop_unused_threads (); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-20 10:49:48 +02:00
										 |  |  |    for (i = 0; i < 5; i++) | 
					
						
							|  |  |  |      { | 
					
						
							| 
									
										
										
										
											2011-10-02 22:31:45 -04:00
										 |  |  |        if (g_thread_pool_get_num_unused_threads () == 0) | 
					
						
							| 
									
										
										
										
											2011-07-20 10:49:48 +02:00
										 |  |  |          break; | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-20 10:49:48 +02:00
										 |  |  |        DEBUG_MSG (("[unused] waiting ONE second for threads to die")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        /* Some time for threads to die. */ | 
					
						
							|  |  |  |        g_usleep (G_USEC_PER_SEC); | 
					
						
							|  |  |  |      } | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-02 22:31:45 -04:00
										 |  |  |    DEBUG_MSG (("[unused] stopped idle threads, %d remain", | 
					
						
							|  |  |  | 	       g_thread_pool_get_num_unused_threads ())); | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |    g_assert (g_thread_pool_get_num_unused_threads () == 0); | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |    g_thread_pool_set_max_unused_threads (MAX_THREADS); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    DEBUG_MSG (("[unused] cleaning up thread pool")); | 
					
						
							|  |  |  |    g_thread_pool_free (pool, FALSE, TRUE); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | test_thread_pools_entry_func (gpointer data, gpointer user_data) | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-04-30 06:31:20 -04:00
										 |  |  | #ifdef DEBUG
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   guint id = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   id = GPOINTER_TO_UINT (data); | 
					
						
							| 
									
										
										
										
											2012-04-30 06:31:20 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  |   DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id)); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   G_LOCK (thread_counter_pools); | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  |   abs_thread_counter++; | 
					
						
							|  |  |  |   running_thread_counter++; | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   G_UNLOCK (thread_counter_pools); | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   g_usleep (g_random_int_range (0, 4000)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   G_LOCK (thread_counter_pools); | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  |   running_thread_counter--; | 
					
						
							| 
									
										
										
										
											2005-07-20 17:06:02 +00:00
										 |  |  |   leftover_task_counter--; | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  |   DEBUG_MSG (("[pool] ---> [%3.3d] exiting thread (abs count:%ld, " | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 	      "running count:%ld, left over:%ld)", | 
					
						
							|  |  |  | 	      id, abs_thread_counter, | 
					
						
							|  |  |  | 	      running_thread_counter, leftover_task_counter)); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   G_UNLOCK (thread_counter_pools); | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | test_thread_pools (void) | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2000-09-28 07:54:40 +00:00
										 |  |  |   GThreadPool *pool1, *pool2, *pool3; | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |   guint runs; | 
					
						
							| 
									
										
										
										
											2000-09-28 07:54:40 +00:00
										 |  |  |   guint i; | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   pool1 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 3, FALSE, NULL); | 
					
						
							|  |  |  |   pool2 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 5, TRUE, NULL); | 
					
						
							|  |  |  |   pool3 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 7, TRUE, NULL); | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |   runs = 300; | 
					
						
							|  |  |  |   for (i = 0; i < runs; i++) | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  |       g_thread_pool_push (pool1, GUINT_TO_POINTER (i + 1), NULL); | 
					
						
							|  |  |  |       g_thread_pool_push (pool2, GUINT_TO_POINTER (i + 1), NULL); | 
					
						
							|  |  |  |       g_thread_pool_push (pool3, GUINT_TO_POINTER (i + 1), NULL); | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       G_LOCK (thread_counter_pools); | 
					
						
							| 
									
										
										
										
											2005-07-20 17:06:02 +00:00
										 |  |  |       leftover_task_counter += 3; | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |       G_UNLOCK (thread_counter_pools); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-20 17:06:02 +00:00
										 |  |  |   g_thread_pool_free (pool1, TRUE, TRUE); | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  |   g_thread_pool_free (pool2, FALSE, TRUE); | 
					
						
							|  |  |  |   g_thread_pool_free (pool3, FALSE, TRUE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |   g_assert (runs * 3 == abs_thread_counter + leftover_task_counter); | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   g_assert (running_thread_counter == 0); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static gint | 
					
						
							|  |  |  | test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   guint32 id1, id2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   id1 = GPOINTER_TO_UINT (a); | 
					
						
							|  |  |  |   id2 = GPOINTER_TO_UINT (b); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | test_thread_sort_entry_func (gpointer data, gpointer user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   guint thread_id; | 
					
						
							|  |  |  |   gboolean is_sorted; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   G_LOCK (last_thread); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   thread_id = GPOINTER_TO_UINT (data); | 
					
						
							|  |  |  |   is_sorted = GPOINTER_TO_INT (user_data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   DEBUG_MSG (("%s ---> entered thread:%2.2d, last thread:%2.2d", | 
					
						
							|  |  |  | 	      is_sorted ? "[  sorted]" : "[unsorted]", | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  | 	      thread_id, last_thread_id)); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (is_sorted) { | 
					
						
							|  |  |  |     static gboolean last_failed = FALSE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (last_thread_id > thread_id) { | 
					
						
							|  |  |  |       if (last_failed) { | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 	g_assert (last_thread_id <= thread_id); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-24 15:21:28 +00:00
										 |  |  |       /* Here we remember one fail and if it concurrently fails, it
 | 
					
						
							|  |  |  |        * can not be sorted. the last thread id might be < this thread | 
					
						
							|  |  |  |        * id if something is added to the queue since threads were | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |        * created | 
					
						
							| 
									
										
										
										
											2006-03-24 15:21:28 +00:00
										 |  |  |        */ | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |       last_failed = TRUE; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       last_failed = FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     last_thread_id = thread_id; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   G_UNLOCK (last_thread); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_usleep (WAIT * 1000); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | test_thread_sort (gboolean sort) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GThreadPool *pool; | 
					
						
							| 
									
										
										
										
											2006-03-24 15:21:28 +00:00
										 |  |  |   guint limit; | 
					
						
							|  |  |  |   guint max_threads; | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   gint i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-24 15:21:28 +00:00
										 |  |  |   limit = MAX_THREADS * 10; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (sort) { | 
					
						
							|  |  |  |     max_threads = 1; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     max_threads = MAX_THREADS; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* It is important that we only have a maximum of 1 thread for this
 | 
					
						
							| 
									
										
										
										
											2011-08-29 14:49:32 -04:00
										 |  |  |    * test since the results can not be guaranteed to be sorted if > 1. | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |    * | 
					
						
							| 
									
										
										
										
											2006-03-24 15:21:28 +00:00
										 |  |  |    * Threads are scheduled by the operating system and are executed at | 
					
						
							|  |  |  |    * random. It cannot be assumed that threads are executed in the | 
					
						
							|  |  |  |    * order they are created. This was discussed in bug #334943. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   pool = g_thread_pool_new (test_thread_sort_entry_func, | 
					
						
							|  |  |  | 			    GINT_TO_POINTER (sort), | 
					
						
							|  |  |  | 			    max_threads, | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 			    FALSE, | 
					
						
							|  |  |  | 			    NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (sort) { | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |     g_thread_pool_set_sort_function (pool, | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 				     test_thread_sort_compare_func, | 
					
						
							|  |  |  | 				     GUINT_TO_POINTER (69)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   for (i = 0; i < limit; i++) { | 
					
						
							|  |  |  |     guint id; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-24 15:21:28 +00:00
										 |  |  |     id = g_random_int_range (1, limit) + 1; | 
					
						
							|  |  |  |     g_thread_pool_push (pool, GUINT_TO_POINTER (id), NULL); | 
					
						
							|  |  |  |     DEBUG_MSG (("%s ===> pushed new thread with id:%d, number " | 
					
						
							|  |  |  | 		"of threads:%d, unprocessed:%d", | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 		sort ? "[  sorted]" : "[unsorted]", | 
					
						
							|  |  |  | 		id, | 
					
						
							| 
									
										
										
										
											2006-03-24 15:21:28 +00:00
										 |  |  | 		g_thread_pool_get_num_threads (pool), | 
					
						
							|  |  |  | 		g_thread_pool_unprocessed (pool))); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-24 15:21:28 +00:00
										 |  |  |   g_assert (g_thread_pool_get_max_threads (pool) == max_threads); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | test_thread_idle_time_entry_func (gpointer data, gpointer user_data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-04-30 06:31:20 -04:00
										 |  |  | #ifdef DEBUG
 | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |   guint thread_id; | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |   thread_id = GPOINTER_TO_UINT (data); | 
					
						
							| 
									
										
										
										
											2012-04-30 06:31:20 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  |   DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id)); | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   g_usleep (WAIT * 1000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  |   DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id)); | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | static gboolean | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | test_thread_idle_timeout (gpointer data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   gint i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (i = 0; i < 2; i++) { | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |     g_thread_pool_push (idle_pool, GUINT_TO_POINTER (100 + i), NULL); | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  |     DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, number " | 
					
						
							|  |  |  | 		"of threads:%d, unprocessed:%d", | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 		100 + i, | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  | 		g_thread_pool_get_num_threads (idle_pool), | 
					
						
							|  |  |  | 		g_thread_pool_unprocessed (idle_pool))); | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return FALSE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2012-11-02 15:45:14 +00:00
										 |  |  | test_thread_idle_time (void) | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   guint limit = 50; | 
					
						
							|  |  |  |   guint interval = 10000; | 
					
						
							|  |  |  |   gint i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func, | 
					
						
							|  |  |  | 				 NULL, | 
					
						
							| 
									
										
										
										
											2012-08-18 14:38:53 -04:00
										 |  |  | 				 0, | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | 				 FALSE, | 
					
						
							|  |  |  | 				 NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-18 14:38:53 -04:00
										 |  |  |   g_thread_pool_set_max_threads (idle_pool, MAX_THREADS, NULL); | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS); | 
					
						
							|  |  |  |   g_thread_pool_set_max_idle_time (interval); | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-18 14:38:53 -04:00
										 |  |  |   g_assert (g_thread_pool_get_max_threads (idle_pool) == MAX_THREADS); | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS); | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |   g_assert (g_thread_pool_get_max_idle_time () == interval); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (i = 0; i < limit; i++) { | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |     g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL); | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  |     DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, " | 
					
						
							|  |  |  | 		"number of threads:%d, unprocessed:%d", | 
					
						
							|  |  |  | 		i, | 
					
						
							|  |  |  | 		g_thread_pool_get_num_threads (idle_pool), | 
					
						
							|  |  |  | 		g_thread_pool_unprocessed (idle_pool))); | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-18 14:38:53 -04:00
										 |  |  |   g_assert_cmpint (g_thread_pool_unprocessed (idle_pool), <=, limit); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |   g_timeout_add ((interval - 1000), | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 		 test_thread_idle_timeout, | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | 		 GUINT_TO_POINTER (interval)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | static gboolean | 
					
						
							|  |  |  | test_check_start_and_stop (gpointer user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   static guint test_number = 0; | 
					
						
							|  |  |  |   static gboolean run_next = FALSE; | 
					
						
							|  |  |  |   gboolean continue_timeout = TRUE; | 
					
						
							|  |  |  |   gboolean quit = TRUE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (test_number == 0) { | 
					
						
							|  |  |  |     run_next = TRUE; | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |     DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number)); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   if (run_next) { | 
					
						
							|  |  |  |     test_number++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (test_number) { | 
					
						
							|  |  |  |     case 1: | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |       test_thread_functions (); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |       break; | 
					
						
							|  |  |  |     case 2: | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |       test_thread_stop_unused (); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |       break; | 
					
						
							|  |  |  |     case 3: | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |       test_thread_pools (); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |     case 4: | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |       test_thread_sort (FALSE); | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |       break; | 
					
						
							|  |  |  |     case 5: | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |       test_thread_sort (TRUE); | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |       break; | 
					
						
							|  |  |  |     case 6: | 
					
						
							| 
									
										
										
										
											2007-05-03 04:48:10 +00:00
										 |  |  |       test_thread_stop_unused (); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     case 7: | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |       test_thread_idle_time (); | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |       DEBUG_MSG (("***** END OF TESTS *****")); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |       g_main_loop_quit (main_loop); | 
					
						
							|  |  |  |       continue_timeout = FALSE; | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     run_next = FALSE; | 
					
						
							|  |  |  |     return TRUE; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |   if (test_number == 3) { | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |     G_LOCK (thread_counter_pools); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |     quit &= running_thread_counter <= 0; | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |     DEBUG_MSG (("***** POOL RUNNING THREAD COUNT:%ld", | 
					
						
							|  |  |  | 		running_thread_counter)); | 
					
						
							|  |  |  |     G_UNLOCK (thread_counter_pools); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-07 09:23:42 +00:00
										 |  |  |   if (test_number == 4 || test_number == 5) { | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |     G_LOCK (thread_counter_sort); | 
					
						
							|  |  |  |     quit &= sort_thread_counter <= 0; | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |     DEBUG_MSG (("***** POOL SORT THREAD COUNT:%ld", | 
					
						
							|  |  |  | 		sort_thread_counter)); | 
					
						
							|  |  |  |     G_UNLOCK (thread_counter_sort); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-03 04:48:10 +00:00
										 |  |  |   if (test_number == 7) { | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |     guint idle; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  |     idle = g_thread_pool_get_num_unused_threads (); | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  |     quit &= idle < 1; | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  |     DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d", | 
					
						
							|  |  |  | 		idle, g_thread_pool_unprocessed (idle_pool))); | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2006-01-03 15:09:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   if (quit) { | 
					
						
							|  |  |  |     run_next = TRUE; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return continue_timeout; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | main (int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-01-17 20:06:27 +00:00
										 |  |  |   DEBUG_MSG (("Starting... (in one second)")); | 
					
						
							| 
									
										
										
										
											2008-01-29 10:07:07 +00:00
										 |  |  |   g_timeout_add (1000, test_check_start_and_stop, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  |   main_loop = g_main_loop_new (NULL, FALSE); | 
					
						
							|  |  |  |   g_main_loop_run (main_loop); | 
					
						
							| 
									
										
										
										
											2013-11-09 20:53:32 +01:00
										 |  |  |   g_main_loop_unref (main_loop); | 
					
						
							| 
									
										
										
										
											2005-12-21 04:45:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-09 20:53:32 +01:00
										 |  |  |   g_thread_pool_free (idle_pool, FALSE, TRUE); | 
					
						
							| 
									
										
										
										
											2000-04-28 12:24:53 +00:00
										 |  |  |   return 0; | 
					
						
							|  |  |  | } |