Whitespace fixes
[glib.git] / tests / threadpool-test.c
blob6e1f6fc0ea91dd6621ab23a2f2f0ab7d1f8d5b7c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
4 #include "config.h"
6 #include <glib.h>
8 #define DEBUG_MSG(x)
9 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
11 #define WAIT 5 /* seconds */
12 #define MAX_THREADS 10
14 /* if > 0 the test will run continuously (since the test ends when
15 * thread count is 0), if -1 it means no limit to unused threads
16 * if 0 then no unused threads are possible */
17 #define MAX_UNUSED_THREADS -1
19 G_LOCK_DEFINE_STATIC (thread_counter_pools);
21 static gulong abs_thread_counter = 0;
22 static gulong running_thread_counter = 0;
23 static gulong leftover_task_counter = 0;
25 G_LOCK_DEFINE_STATIC (last_thread);
27 static guint last_thread_id = 0;
29 G_LOCK_DEFINE_STATIC (thread_counter_sort);
31 static gulong sort_thread_counter = 0;
33 static GThreadPool *idle_pool = NULL;
35 static GMainLoop *main_loop = NULL;
37 static void
38 test_thread_functions (void)
40 gint max_unused_threads;
41 guint max_idle_time;
43 /* This function attempts to call functions which don't need a
44 * threadpool to operate to make sure no uninitialised pointers
45 * accessed and no errors occur.
48 max_unused_threads = 3;
50 DEBUG_MSG (("[funcs] Setting max unused threads to %d",
51 max_unused_threads));
52 g_thread_pool_set_max_unused_threads (max_unused_threads);
54 DEBUG_MSG (("[funcs] Getting max unused threads = %d",
55 g_thread_pool_get_max_unused_threads ()));
56 g_assert (g_thread_pool_get_max_unused_threads() == max_unused_threads);
58 DEBUG_MSG (("[funcs] Getting num unused threads = %d",
59 g_thread_pool_get_num_unused_threads ()));
60 g_assert (g_thread_pool_get_num_unused_threads () == 0);
62 DEBUG_MSG (("[funcs] Stopping unused threads"));
63 g_thread_pool_stop_unused_threads ();
65 max_idle_time = 10 * G_USEC_PER_SEC;
67 DEBUG_MSG (("[funcs] Setting max idle time to %d",
68 max_idle_time));
69 g_thread_pool_set_max_idle_time (max_idle_time);
71 DEBUG_MSG (("[funcs] Getting max idle time = %d",
72 g_thread_pool_get_max_idle_time ()));
73 g_assert (g_thread_pool_get_max_idle_time () == max_idle_time);
75 DEBUG_MSG (("[funcs] Setting max idle time to 0"));
76 g_thread_pool_set_max_idle_time (0);
78 DEBUG_MSG (("[funcs] Getting max idle time = %d",
79 g_thread_pool_get_max_idle_time ()));
80 g_assert (g_thread_pool_get_max_idle_time () == 0);
83 static void
84 test_thread_stop_unused (void)
86 GThreadPool *pool;
87 guint i;
88 guint limit = 100;
90 /* Spawn a few threads. */
91 g_thread_pool_set_max_unused_threads (-1);
92 pool = g_thread_pool_new ((GFunc) g_usleep, NULL, -1, FALSE, NULL);
94 for (i = 0; i < limit; i++)
95 g_thread_pool_push (pool, GUINT_TO_POINTER (1000), NULL);
97 DEBUG_MSG (("[unused] ===> pushed %d threads onto the idle pool",
98 limit));
100 /* Wait for the threads to migrate. */
101 g_usleep (G_USEC_PER_SEC);
103 DEBUG_MSG (("[unused] stopping unused threads"));
104 g_thread_pool_stop_unused_threads ();
106 for (i = 0; i < 5; i++)
108 if (g_thread_pool_get_num_unused_threads () == 0)
109 break;
111 DEBUG_MSG (("[unused] waiting ONE second for threads to die"));
113 /* Some time for threads to die. */
114 g_usleep (G_USEC_PER_SEC);
117 DEBUG_MSG (("[unused] stopped idle threads, %d remain",
118 g_thread_pool_get_num_unused_threads ()));
120 g_assert (g_thread_pool_get_num_unused_threads () == 0);
122 g_thread_pool_set_max_unused_threads (MAX_THREADS);
124 DEBUG_MSG (("[unused] cleaning up thread pool"));
125 g_thread_pool_free (pool, FALSE, TRUE);
128 static void
129 test_thread_pools_entry_func (gpointer data, gpointer user_data)
131 guint id = 0;
133 id = GPOINTER_TO_UINT (data);
135 DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id));
137 G_LOCK (thread_counter_pools);
138 abs_thread_counter++;
139 running_thread_counter++;
140 G_UNLOCK (thread_counter_pools);
142 g_usleep (g_random_int_range (0, 4000));
144 G_LOCK (thread_counter_pools);
145 running_thread_counter--;
146 leftover_task_counter--;
148 DEBUG_MSG (("[pool] ---> [%3.3d] exiting thread (abs count:%ld, "
149 "running count:%ld, left over:%ld)",
150 id, abs_thread_counter,
151 running_thread_counter, leftover_task_counter));
152 G_UNLOCK (thread_counter_pools);
155 static void
156 test_thread_pools (void)
158 GThreadPool *pool1, *pool2, *pool3;
159 guint runs;
160 guint i;
162 pool1 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 3, FALSE, NULL);
163 pool2 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 5, TRUE, NULL);
164 pool3 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 7, TRUE, NULL);
166 runs = 300;
167 for (i = 0; i < runs; i++)
169 g_thread_pool_push (pool1, GUINT_TO_POINTER (i + 1), NULL);
170 g_thread_pool_push (pool2, GUINT_TO_POINTER (i + 1), NULL);
171 g_thread_pool_push (pool3, GUINT_TO_POINTER (i + 1), NULL);
173 G_LOCK (thread_counter_pools);
174 leftover_task_counter += 3;
175 G_UNLOCK (thread_counter_pools);
178 g_thread_pool_free (pool1, TRUE, TRUE);
179 g_thread_pool_free (pool2, FALSE, TRUE);
180 g_thread_pool_free (pool3, FALSE, TRUE);
182 g_assert (runs * 3 == abs_thread_counter + leftover_task_counter);
183 g_assert (running_thread_counter == 0);
186 static gint
187 test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
189 guint32 id1, id2;
191 id1 = GPOINTER_TO_UINT (a);
192 id2 = GPOINTER_TO_UINT (b);
194 return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
197 static void
198 test_thread_sort_entry_func (gpointer data, gpointer user_data)
200 guint thread_id;
201 gboolean is_sorted;
203 G_LOCK (last_thread);
205 thread_id = GPOINTER_TO_UINT (data);
206 is_sorted = GPOINTER_TO_INT (user_data);
208 DEBUG_MSG (("%s ---> entered thread:%2.2d, last thread:%2.2d",
209 is_sorted ? "[ sorted]" : "[unsorted]",
210 thread_id, last_thread_id));
212 if (is_sorted) {
213 static gboolean last_failed = FALSE;
215 if (last_thread_id > thread_id) {
216 if (last_failed) {
217 g_assert (last_thread_id <= thread_id);
220 /* Here we remember one fail and if it concurrently fails, it
221 * can not be sorted. the last thread id might be < this thread
222 * id if something is added to the queue since threads were
223 * created
225 last_failed = TRUE;
226 } else {
227 last_failed = FALSE;
230 last_thread_id = thread_id;
233 G_UNLOCK (last_thread);
235 g_usleep (WAIT * 1000);
238 static void
239 test_thread_sort (gboolean sort)
241 GThreadPool *pool;
242 guint limit;
243 guint max_threads;
244 gint i;
246 limit = MAX_THREADS * 10;
248 if (sort) {
249 max_threads = 1;
250 } else {
251 max_threads = MAX_THREADS;
254 /* It is important that we only have a maximum of 1 thread for this
255 * test since the results can not be guaranteed to be sorted if > 1.
257 * Threads are scheduled by the operating system and are executed at
258 * random. It cannot be assumed that threads are executed in the
259 * order they are created. This was discussed in bug #334943.
262 pool = g_thread_pool_new (test_thread_sort_entry_func,
263 GINT_TO_POINTER (sort),
264 max_threads,
265 FALSE,
266 NULL);
268 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
270 if (sort) {
271 g_thread_pool_set_sort_function (pool,
272 test_thread_sort_compare_func,
273 GUINT_TO_POINTER (69));
276 for (i = 0; i < limit; i++) {
277 guint id;
279 id = g_random_int_range (1, limit) + 1;
280 g_thread_pool_push (pool, GUINT_TO_POINTER (id), NULL);
281 DEBUG_MSG (("%s ===> pushed new thread with id:%d, number "
282 "of threads:%d, unprocessed:%d",
283 sort ? "[ sorted]" : "[unsorted]",
285 g_thread_pool_get_num_threads (pool),
286 g_thread_pool_unprocessed (pool)));
289 g_assert (g_thread_pool_get_max_threads (pool) == max_threads);
290 g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool));
293 static void
294 test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
296 guint thread_id;
298 thread_id = GPOINTER_TO_UINT (data);
300 DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
302 g_usleep (WAIT * 1000);
304 DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id));
307 static gboolean
308 test_thread_idle_timeout (gpointer data)
310 guint interval;
311 gint i;
313 interval = GPOINTER_TO_UINT (data);
315 for (i = 0; i < 2; i++) {
316 g_thread_pool_push (idle_pool, GUINT_TO_POINTER (100 + i), NULL);
317 DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, number "
318 "of threads:%d, unprocessed:%d",
319 100 + i,
320 g_thread_pool_get_num_threads (idle_pool),
321 g_thread_pool_unprocessed (idle_pool)));
325 return FALSE;
328 static void
329 test_thread_idle_time ()
331 guint limit = 50;
332 guint interval = 10000;
333 gint i;
335 idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func,
336 NULL,
337 MAX_THREADS,
338 FALSE,
339 NULL);
341 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
342 g_thread_pool_set_max_idle_time (interval);
344 g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);
345 g_assert (g_thread_pool_get_max_idle_time () == interval);
347 for (i = 0; i < limit; i++) {
348 g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL);
349 DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, "
350 "number of threads:%d, unprocessed:%d",
352 g_thread_pool_get_num_threads (idle_pool),
353 g_thread_pool_unprocessed (idle_pool)));
356 g_timeout_add ((interval - 1000),
357 test_thread_idle_timeout,
358 GUINT_TO_POINTER (interval));
361 static gboolean
362 test_check_start_and_stop (gpointer user_data)
364 static guint test_number = 0;
365 static gboolean run_next = FALSE;
366 gboolean continue_timeout = TRUE;
367 gboolean quit = TRUE;
369 if (test_number == 0) {
370 run_next = TRUE;
371 DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number));
374 if (run_next) {
375 test_number++;
377 switch (test_number) {
378 case 1:
379 test_thread_functions ();
380 break;
381 case 2:
382 test_thread_stop_unused ();
383 break;
384 case 3:
385 test_thread_pools ();
386 break;
387 case 4:
388 test_thread_sort (FALSE);
389 break;
390 case 5:
391 test_thread_sort (TRUE);
392 break;
393 case 6:
394 test_thread_stop_unused ();
395 break;
396 case 7:
397 test_thread_idle_time ();
398 break;
399 default:
400 DEBUG_MSG (("***** END OF TESTS *****"));
401 g_main_loop_quit (main_loop);
402 continue_timeout = FALSE;
403 break;
406 run_next = FALSE;
407 return TRUE;
410 if (test_number == 3) {
411 G_LOCK (thread_counter_pools);
412 quit &= running_thread_counter <= 0;
413 DEBUG_MSG (("***** POOL RUNNING THREAD COUNT:%ld",
414 running_thread_counter));
415 G_UNLOCK (thread_counter_pools);
418 if (test_number == 4 || test_number == 5) {
419 G_LOCK (thread_counter_sort);
420 quit &= sort_thread_counter <= 0;
421 DEBUG_MSG (("***** POOL SORT THREAD COUNT:%ld",
422 sort_thread_counter));
423 G_UNLOCK (thread_counter_sort);
426 if (test_number == 7) {
427 guint idle;
429 idle = g_thread_pool_get_num_unused_threads ();
430 quit &= idle < 1;
431 DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d",
432 idle, g_thread_pool_unprocessed (idle_pool)));
435 if (quit) {
436 run_next = TRUE;
439 return continue_timeout;
443 main (int argc, char *argv[])
445 g_thread_init (NULL);
447 DEBUG_MSG (("Starting... (in one second)"));
448 g_timeout_add (1000, test_check_start_and_stop, NULL);
450 main_loop = g_main_loop_new (NULL, FALSE);
451 g_main_loop_run (main_loop);
453 return 0;