Update contrib.
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
2 #undef G_DISABLE_ASSERT
11 #include <glib_global.h>
12 #include "mrt2_glib2_test.h"
16 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
18 #define WAIT 5 /* seconds */
19 #define MAX_THREADS 10
21 /* if > 0 the test will run continously (since the test ends when
22 * thread count is 0), if -1 it means no limit to unused threads
23 * if 0 then no unused threads are possible */
24 #define MAX_UNUSED_THREADS -1
26 G_LOCK_DEFINE_STATIC (thread_counter_pools);
28 static gulong abs_thread_counter = 0;
29 static gulong running_thread_counter = 0;
30 static gulong leftover_task_counter = 0;
32 G_LOCK_DEFINE_STATIC (last_thread);
34 static guint last_thread_id = 0;
36 G_LOCK_DEFINE_STATIC (thread_counter_sort);
38 static gulong sort_thread_counter = 0;
40 static GThreadPool *idle_pool = NULL;
42 static GMainLoop *main_loop = NULL;
45 test_thread_functions (void)
47 gint max_unused_threads;
52 /* This function attempts to call functions which don't need a
53 * threadpool to operate to make sure no uninitialised pointers
54 * accessed and no errors occur.
57 max_unused_threads = 3;
59 DEBUG_MSG (("[funcs] Setting max unused threads to %d",
61 g_thread_pool_set_max_unused_threads (max_unused_threads);
63 DEBUG_MSG (("[funcs] Getting max unused threads = %d",
64 g_thread_pool_get_max_unused_threads ()));
65 g_assert (g_thread_pool_get_max_unused_threads() == max_unused_threads);
67 DEBUG_MSG (("[funcs] Getting num unused threads = %d",
68 g_thread_pool_get_num_unused_threads ()));
69 g_assert (g_thread_pool_get_num_unused_threads () == 0);
71 DEBUG_MSG (("[funcs] Stopping unused threads"));
72 g_thread_pool_stop_unused_threads ();
74 max_idle_time = 10 * G_USEC_PER_SEC;
76 DEBUG_MSG (("[funcs] Setting max idle time to %d",
78 g_thread_pool_set_max_idle_time (max_idle_time);
80 DEBUG_MSG (("[funcs] Getting max idle time = %d",
81 g_thread_pool_get_max_idle_time ()));
82 g_assert (g_thread_pool_get_max_idle_time () == max_idle_time);
84 DEBUG_MSG (("[funcs] Setting max idle time to 0"));
85 g_thread_pool_set_max_idle_time (0);
87 DEBUG_MSG (("[funcs] Getting max idle time = %d",
88 g_thread_pool_get_max_idle_time ()));
89 g_assert (g_thread_pool_get_max_idle_time () == 0);
93 test_count_threads_foreach (GThread *thread,
100 test_count_threads (void)
104 g_thread_foreach ((GFunc) test_count_threads_foreach, &count);
106 /* Exclude main thread */
111 test_thread_stop_unused (void)
117 /* Spawn a few threads. */
118 g_thread_pool_set_max_unused_threads (-1);
119 pool = g_thread_pool_new ((GFunc) g_usleep, NULL, -1, FALSE, NULL);
121 for (i = 0; i < limit; i++)
122 g_thread_pool_push (pool, GUINT_TO_POINTER (1000), NULL);
124 DEBUG_MSG (("[unused] ===> pushed %d threads onto the idle pool",
127 /* Wait for the threads to migrate. */
128 g_usleep (G_USEC_PER_SEC);
130 DEBUG_MSG (("[unused] current threads %d",
131 test_count_threads()));
133 DEBUG_MSG (("[unused] stopping unused threads"));
134 g_thread_pool_stop_unused_threads ();
136 DEBUG_MSG (("[unused] waiting ONE second for threads to die"));
138 /* Some time for threads to die. */
139 g_usleep (G_USEC_PER_SEC);
141 DEBUG_MSG (("[unused] stopped idle threads, %d remain, %d threads still exist",
142 g_thread_pool_get_num_unused_threads (),
143 test_count_threads ()));
145 g_assert (g_thread_pool_get_num_unused_threads () == test_count_threads ());
146 g_assert (g_thread_pool_get_num_unused_threads () == 0);
148 g_thread_pool_set_max_unused_threads (MAX_THREADS);
150 DEBUG_MSG (("[unused] cleaning up thread pool"));
151 g_thread_pool_free (pool, FALSE, TRUE);
155 test_thread_pools_entry_func (gpointer data, gpointer user_data)
159 id = GPOINTER_TO_UINT (data);
161 DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id));
163 G_LOCK (thread_counter_pools);
164 abs_thread_counter++;
165 running_thread_counter++;
166 G_UNLOCK (thread_counter_pools);
168 g_usleep (g_random_int_range (0, 4000));
170 G_LOCK (thread_counter_pools);
171 running_thread_counter--;
172 leftover_task_counter--;
174 DEBUG_MSG (("[pool] ---> [%3.3d] exiting thread (abs count:%ld, "
175 "running count:%ld, left over:%ld)",
176 id, abs_thread_counter,
177 running_thread_counter, leftover_task_counter));
178 G_UNLOCK (thread_counter_pools);
182 test_thread_pools (void)
184 GThreadPool *pool1, *pool2, *pool3;
188 pool1 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 3, FALSE, NULL);
189 pool2 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 5, TRUE, NULL);
190 pool3 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 7, TRUE, NULL);
193 for (i = 0; i < runs; i++)
195 g_thread_pool_push (pool1, GUINT_TO_POINTER (i + 1), NULL);
196 g_thread_pool_push (pool2, GUINT_TO_POINTER (i + 1), NULL);
197 g_thread_pool_push (pool3, GUINT_TO_POINTER (i + 1), NULL);
198 leftover_task_counter += 3;
201 g_thread_pool_free (pool1, TRUE, TRUE);
202 g_thread_pool_free (pool2, FALSE, TRUE);
203 g_thread_pool_free (pool3, FALSE, TRUE);
205 g_assert (runs * 3 == abs_thread_counter + leftover_task_counter);
206 g_assert (running_thread_counter == 0);
210 test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
214 id1 = GPOINTER_TO_UINT (a);
215 id2 = GPOINTER_TO_UINT (b);
217 return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
221 test_thread_sort_entry_func (gpointer data, gpointer user_data)
226 G_LOCK (last_thread);
228 thread_id = GPOINTER_TO_UINT (data);
229 is_sorted = GPOINTER_TO_INT (user_data);
231 DEBUG_MSG (("%s ---> entered thread:%2.2d, last thread:%2.2d",
232 is_sorted ? "[ sorted]" : "[unsorted]",
233 thread_id, last_thread_id));
236 static gboolean last_failed = FALSE;
238 if (last_thread_id > thread_id) {
240 g_assert (last_thread_id <= thread_id);
243 /* Here we remember one fail and if it concurrently fails, it
244 * can not be sorted. the last thread id might be < this thread
245 * id if something is added to the queue since threads were
253 last_thread_id = thread_id;
256 G_UNLOCK (last_thread);
258 g_usleep (WAIT * 1000);
262 test_thread_sort (gboolean sort)
269 limit = MAX_THREADS * 10;
274 max_threads = MAX_THREADS;
277 /* It is important that we only have a maximum of 1 thread for this
278 * test since the results can not be guranteed to be sorted if > 1.
280 * Threads are scheduled by the operating system and are executed at
281 * random. It cannot be assumed that threads are executed in the
282 * order they are created. This was discussed in bug #334943.
285 pool = g_thread_pool_new (test_thread_sort_entry_func,
286 GINT_TO_POINTER (sort),
291 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
294 g_thread_pool_set_sort_function (pool,
295 test_thread_sort_compare_func,
296 GUINT_TO_POINTER (69));
299 for (i = 0; i < limit; i++) {
302 id = g_random_int_range (1, limit) + 1;
303 g_thread_pool_push (pool, GUINT_TO_POINTER (id), NULL);
304 DEBUG_MSG (("%s ===> pushed new thread with id:%d, number "
305 "of threads:%d, unprocessed:%d",
306 sort ? "[ sorted]" : "[unsorted]",
308 g_thread_pool_get_num_threads (pool),
309 g_thread_pool_unprocessed (pool)));
312 g_assert (g_thread_pool_get_max_threads (pool) == max_threads);
313 g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool));
317 test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
321 thread_id = GPOINTER_TO_UINT (data);
323 DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
325 g_usleep (WAIT * 1000);
327 DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id));
331 test_thread_idle_timeout (gpointer data)
336 interval = GPOINTER_TO_UINT (data);
338 for (i = 0; i < 2; i++) {
339 g_thread_pool_push (idle_pool, GUINT_TO_POINTER (100 + i), NULL);
340 DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, number "
341 "of threads:%d, unprocessed:%d",
343 g_thread_pool_get_num_threads (idle_pool),
344 g_thread_pool_unprocessed (idle_pool)));
352 test_thread_idle_time ()
355 guint interval = 10000;
358 idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func,
364 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
365 g_thread_pool_set_max_idle_time (interval);
367 g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);
368 g_assert (g_thread_pool_get_max_idle_time () == interval);
370 for (i = 0; i < limit; i++) {
371 g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL);
372 DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, "
373 "number of threads:%d, unprocessed:%d",
375 g_thread_pool_get_num_threads (idle_pool),
376 g_thread_pool_unprocessed (idle_pool)));
379 g_timeout_add ((interval - 1000),
380 test_thread_idle_timeout,
381 GUINT_TO_POINTER (interval));
385 test_check_start_and_stop (gpointer user_data)
387 static guint test_number = 0;
388 static gboolean run_next = FALSE;
389 gboolean continue_timeout = TRUE;
390 gboolean quit = TRUE;
392 if (test_number == 0) {
394 DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number));
400 switch (test_number) {
402 test_thread_functions ();
405 test_thread_stop_unused ();
408 test_thread_pools ();
411 test_thread_sort (FALSE);
414 test_thread_sort (TRUE);
417 test_thread_idle_time ();
420 DEBUG_MSG (("***** END OF TESTS *****"));
421 g_main_loop_quit (main_loop);
422 continue_timeout = FALSE;
430 if (test_number == 3) {
431 G_LOCK (thread_counter_pools);
432 quit &= running_thread_counter <= 0;
433 DEBUG_MSG (("***** POOL RUNNING THREAD COUNT:%ld",
434 running_thread_counter));
435 G_UNLOCK (thread_counter_pools);
438 if (test_number == 4 || test_number == 5) {
439 G_LOCK (thread_counter_sort);
440 quit &= sort_thread_counter <= 0;
441 DEBUG_MSG (("***** POOL SORT THREAD COUNT:%ld",
442 sort_thread_counter));
443 G_UNLOCK (thread_counter_sort);
446 if (test_number == 6) {
449 idle = g_thread_pool_get_num_unused_threads ();
451 DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d",
452 idle, g_thread_pool_unprocessed (idle_pool)));
459 return continue_timeout;
463 main (int argc, char *argv[])
465 /* Only run the test, if threads are enabled and a default thread
466 implementation is available */
468 guint g_thread_pool_unprocessed_op = -1;
472 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
473 g_thread_init (NULL);
475 DEBUG_MSG (("Starting... (in one second)"));
476 g_timeout_add (1000, test_check_start_and_stop, NULL);
478 main_loop = g_main_loop_new (NULL, FALSE);
479 g_main_loop_run (main_loop);
482 testResultXml("threadpool-test");
483 #endif /* EMULATOR */