1 #undef G_DISABLE_ASSERT
12 #define WAIT 5 /* seconds */
13 #define MAX_THREADS 10
14 #define MAX_UNUSED_THREADS 2
16 G_LOCK_DEFINE_STATIC (thread_counter_pools);
18 static gulong abs_thread_counter = 0;
19 static gulong running_thread_counter = 0;
20 static gulong leftover_task_counter = 0;
22 G_LOCK_DEFINE_STATIC (last_thread);
24 static guint last_thread_id = 0;
26 G_LOCK_DEFINE_STATIC (thread_counter_sort);
28 static gulong sort_thread_counter = 0;
31 static GMainLoop *main_loop = NULL;
35 test_thread_pools_entry_func (gpointer data, gpointer user_data)
39 id = GPOINTER_TO_UINT (data);
41 d(g_print ("[pool] ---> [%3.3d] entered thread\n", id));
43 G_LOCK (thread_counter_pools);
45 running_thread_counter++;
46 G_UNLOCK (thread_counter_pools);
48 g_usleep (g_random_int_range (0, 4000));
50 G_LOCK (thread_counter_pools);
51 running_thread_counter--;
52 leftover_task_counter--;
54 g_print ("[pool] ---> [%3.3d] exiting thread (abs count:%ld, running count:%ld, left over:%ld)\n",
55 id, abs_thread_counter, running_thread_counter, leftover_task_counter);
56 G_UNLOCK (thread_counter_pools);
60 test_thread_pools (void)
62 GThreadPool *pool1, *pool2, *pool3;
65 pool1 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 3, FALSE, NULL);
66 pool2 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 5, TRUE, NULL);
67 pool3 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 7, TRUE, NULL);
69 for (i = 0; i < RUNS; i++)
71 g_thread_pool_push (pool1, GUINT_TO_POINTER (i), NULL);
72 g_thread_pool_push (pool2, GUINT_TO_POINTER (i), NULL);
73 g_thread_pool_push (pool3, GUINT_TO_POINTER (i), NULL);
74 leftover_task_counter += 3;
77 g_thread_pool_free (pool1, TRUE, TRUE);
78 g_thread_pool_free (pool2, FALSE, TRUE);
79 g_thread_pool_free (pool3, FALSE, TRUE);
81 g_assert (RUNS * 3 == abs_thread_counter + leftover_task_counter);
82 g_assert (running_thread_counter == 0);
86 test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
90 id1 = GPOINTER_TO_UINT (a);
91 id2 = GPOINTER_TO_UINT (b);
93 return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
97 test_thread_sort_entry_func (gpointer data, gpointer user_data)
102 G_LOCK (last_thread);
104 thread_id = GPOINTER_TO_UINT (data);
105 is_sorted = GPOINTER_TO_INT (user_data);
107 d(g_print ("%s ---> entered thread:%2.2d, last thread:%2.2d\n",
108 is_sorted ? "[ sorted]" : "[unsorted]", thread_id, last_thread_id));
111 static gboolean last_failed = FALSE;
113 if (last_thread_id > thread_id) {
115 g_assert (last_thread_id <= thread_id);
118 /* here we remember one fail and if it concurrently fails, it
119 can not be sorted. the last thread id might be < this thread
120 id if something is added to the queue since threads were
127 last_thread_id = thread_id;
130 G_UNLOCK (last_thread);
132 g_usleep (WAIT * 1000);
136 test_thread_sort (gboolean sort)
142 pool = g_thread_pool_new (test_thread_sort_entry_func,
143 GINT_TO_POINTER (sort),
148 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
151 g_thread_pool_set_sort_function (pool,
152 test_thread_sort_compare_func,
153 GUINT_TO_POINTER (69));
156 for (i = 0; i < limit; i++) {
159 id = g_random_int_range (1, limit*2);
160 g_thread_pool_push (pool, GUINT_TO_POINTER (id), NULL);
163 g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool));
167 test_check_start_and_stop (gpointer user_data)
169 static guint test_number = 0;
170 static gboolean run_next = FALSE;
171 gboolean continue_timeout = TRUE;
172 gboolean quit = TRUE;
174 if (test_number == 0) {
176 d(g_print ("***** RUNNING TEST %2.2d *****\n", test_number));
182 switch (test_number) {
184 test_thread_pools ();
187 test_thread_sort (FALSE);
190 test_thread_sort (TRUE);
193 d(g_print ("***** END OF TESTS *****\n"));
194 g_main_loop_quit (main_loop);
195 continue_timeout = FALSE;
203 if (test_number == 1) {
204 G_LOCK (thread_counter_pools);
205 quit &= running_thread_counter <= 0;
206 d(g_print ("***** POOL RUNNING THREAD COUNT:%ld\n",
207 running_thread_counter));
208 G_UNLOCK (thread_counter_pools);
211 if (test_number == 2 || test_number == 3) {
212 G_LOCK (thread_counter_sort);
213 quit &= sort_thread_counter <= 0;
214 d(g_print ("***** POOL SORT THREAD COUNT:%ld\n",
215 sort_thread_counter));
216 G_UNLOCK (thread_counter_sort);
223 return continue_timeout;
227 main (int argc, char *argv[])
229 /* Only run the test, if threads are enabled and a default thread
230 implementation is available */
232 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
233 g_thread_init (NULL);
235 d(g_print ("Starting... (in one second)\n"));
236 g_timeout_add (1000, test_check_start_and_stop, NULL);
238 main_loop = g_main_loop_new (NULL, FALSE);
239 g_main_loop_run (main_loop);