1 #undef G_DISABLE_ASSERT
9 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
13 #define WAIT 5 /* seconds */
14 #define MAX_THREADS 10
16 /* if > 0 the test will run continously (since the test ends when
17 * thread count is 0), if -1 it means no limit to unused threads
18 * if 0 then no unused threads are possible */
19 #define MAX_UNUSED_THREADS -1
21 G_LOCK_DEFINE_STATIC (thread_counter_pools);
23 static gulong abs_thread_counter = 0;
24 static gulong running_thread_counter = 0;
25 static gulong leftover_task_counter = 0;
27 G_LOCK_DEFINE_STATIC (last_thread);
29 static guint last_thread_id = 0;
31 G_LOCK_DEFINE_STATIC (thread_counter_sort);
33 static gulong sort_thread_counter = 0;
35 static GThreadPool *idle_pool = NULL;
37 static GMainLoop *main_loop = NULL;
41 test_thread_pools_entry_func (gpointer data, gpointer user_data)
45 id = GPOINTER_TO_UINT (data);
47 DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id));
49 G_LOCK (thread_counter_pools);
51 running_thread_counter++;
52 G_UNLOCK (thread_counter_pools);
54 g_usleep (g_random_int_range (0, 4000));
56 G_LOCK (thread_counter_pools);
57 running_thread_counter--;
58 leftover_task_counter--;
60 DEBUG_MSG (("[pool] ---> [%3.3d] exiting thread (abs count:%ld, "
61 "running count:%ld, left over:%ld)",
62 id, abs_thread_counter,
63 running_thread_counter, leftover_task_counter));
64 G_UNLOCK (thread_counter_pools);
68 test_thread_pools (void)
70 GThreadPool *pool1, *pool2, *pool3;
73 pool1 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 3, FALSE, NULL);
74 pool2 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 5, TRUE, NULL);
75 pool3 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 7, TRUE, NULL);
77 for (i = 0; i < RUNS; i++)
79 g_thread_pool_push (pool1, GUINT_TO_POINTER (i + 1), NULL);
80 g_thread_pool_push (pool2, GUINT_TO_POINTER (i + 1), NULL);
81 g_thread_pool_push (pool3, GUINT_TO_POINTER (i + 1), NULL);
82 leftover_task_counter += 3;
85 g_thread_pool_free (pool1, TRUE, TRUE);
86 g_thread_pool_free (pool2, FALSE, TRUE);
87 g_thread_pool_free (pool3, FALSE, TRUE);
89 g_assert (RUNS * 3 == abs_thread_counter + leftover_task_counter);
90 g_assert (running_thread_counter == 0);
94 test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
98 id1 = GPOINTER_TO_UINT (a);
99 id2 = GPOINTER_TO_UINT (b);
101 return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
105 test_thread_sort_entry_func (gpointer data, gpointer user_data)
110 G_LOCK (last_thread);
112 thread_id = GPOINTER_TO_UINT (data);
113 is_sorted = GPOINTER_TO_INT (user_data);
115 DEBUG_MSG (("%s ---> entered thread:%2.2d, last thread:%2.2d",
116 is_sorted ? "[ sorted]" : "[unsorted]",
117 thread_id, last_thread_id));
120 static gboolean last_failed = FALSE;
122 if (last_thread_id > thread_id) {
124 g_assert (last_thread_id <= thread_id);
127 /* Here we remember one fail and if it concurrently fails, it
128 * can not be sorted. the last thread id might be < this thread
129 * id if something is added to the queue since threads were
137 last_thread_id = thread_id;
140 G_UNLOCK (last_thread);
142 g_usleep (WAIT * 1000);
146 test_thread_sort (gboolean sort)
153 limit = MAX_THREADS * 10;
158 max_threads = MAX_THREADS;
161 /* It is important that we only have a maximum of 1 thread for this
162 * test since the results can not be guranteed to be sorted if > 1.
164 * Threads are scheduled by the operating system and are executed at
165 * random. It cannot be assumed that threads are executed in the
166 * order they are created. This was discussed in bug #334943.
169 pool = g_thread_pool_new (test_thread_sort_entry_func,
170 GINT_TO_POINTER (sort),
175 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
178 g_thread_pool_set_sort_function (pool,
179 test_thread_sort_compare_func,
180 GUINT_TO_POINTER (69));
183 for (i = 0; i < limit; i++) {
186 id = g_random_int_range (1, limit) + 1;
187 g_thread_pool_push (pool, GUINT_TO_POINTER (id), NULL);
188 DEBUG_MSG (("%s ===> pushed new thread with id:%d, number "
189 "of threads:%d, unprocessed:%d",
190 sort ? "[ sorted]" : "[unsorted]",
192 g_thread_pool_get_num_threads (pool),
193 g_thread_pool_unprocessed (pool)));
196 g_assert (g_thread_pool_get_max_threads (pool) == max_threads);
197 g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool));
201 test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
205 thread_id = GPOINTER_TO_UINT (data);
207 DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
209 g_usleep (WAIT * 1000);
211 DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id));
215 test_thread_idle_timeout (gpointer data)
220 interval = GPOINTER_TO_UINT (data);
222 for (i = 0; i < 2; i++) {
223 g_thread_pool_push (idle_pool, GUINT_TO_POINTER (100 + i), NULL);
224 DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, number "
225 "of threads:%d, unprocessed:%d",
227 g_thread_pool_get_num_threads (idle_pool),
228 g_thread_pool_unprocessed (idle_pool)));
236 test_thread_idle_time ()
239 guint interval = 10000;
242 idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func,
248 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
249 g_thread_pool_set_max_idle_time (interval);
251 g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);
252 g_assert (g_thread_pool_get_max_idle_time () == interval);
254 for (i = 0; i < limit; i++) {
255 g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL);
256 DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, "
257 "number of threads:%d, unprocessed:%d",
259 g_thread_pool_get_num_threads (idle_pool),
260 g_thread_pool_unprocessed (idle_pool)));
263 g_timeout_add ((interval - 1000),
264 test_thread_idle_timeout,
265 GUINT_TO_POINTER (interval));
269 test_check_start_and_stop (gpointer user_data)
271 static guint test_number = 0;
272 static gboolean run_next = FALSE;
273 gboolean continue_timeout = TRUE;
274 gboolean quit = TRUE;
276 if (test_number == 0) {
278 DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number));
284 switch (test_number) {
286 test_thread_pools ();
289 test_thread_sort (FALSE);
292 test_thread_sort (TRUE);
295 test_thread_idle_time ();
298 DEBUG_MSG (("***** END OF TESTS *****"));
299 g_main_loop_quit (main_loop);
300 continue_timeout = FALSE;
308 if (test_number == 1) {
309 G_LOCK (thread_counter_pools);
310 quit &= running_thread_counter <= 0;
311 DEBUG_MSG (("***** POOL RUNNING THREAD COUNT:%ld",
312 running_thread_counter));
313 G_UNLOCK (thread_counter_pools);
316 if (test_number == 2 || test_number == 3) {
317 G_LOCK (thread_counter_sort);
318 quit &= sort_thread_counter <= 0;
319 DEBUG_MSG (("***** POOL SORT THREAD COUNT:%ld",
320 sort_thread_counter));
321 G_UNLOCK (thread_counter_sort);
324 if (test_number == 4) {
327 idle = g_thread_pool_get_num_unused_threads ();
329 DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d",
330 idle, g_thread_pool_unprocessed (idle_pool)));
337 return continue_timeout;
341 main (int argc, char *argv[])
343 /* Only run the test, if threads are enabled and a default thread
344 implementation is available */
346 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
347 g_thread_init (NULL);
349 DEBUG_MSG (("Starting... (in one second)"));
350 g_timeout_add (1000, test_check_start_and_stop, NULL);
352 main_loop = g_main_loop_new (NULL, FALSE);
353 g_main_loop_run (main_loop);