1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #include "config.h"
5
6 #include <glib.h>
7
8 /* #define DEBUG 1 */
9
10 #ifdef DEBUG
11 # define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");
12 #else
13 # define DEBUG_MSG(x)
14 #endif
15
16 #define WAIT 5 /* seconds */
17 #define MAX_THREADS 10
18
19 /* if > 0 the test will run continuously (since the test ends when
20 * thread count is 0), if -1 it means no limit to unused threads
21 * if 0 then no unused threads are possible */
22 #define MAX_UNUSED_THREADS -1
23
24 G_LOCK_DEFINE_STATIC (thread_counter_pools);
25
26 static gulong abs_thread_counter = 0;
27 static gulong running_thread_counter = 0;
28 static gulong leftover_task_counter = 0;
29
30 G_LOCK_DEFINE_STATIC (last_thread);
31
32 static guint last_thread_id = 0;
33
34 G_LOCK_DEFINE_STATIC (thread_counter_sort);
35
36 static gulong sort_thread_counter = 0;
37
38 static GThreadPool *idle_pool = NULL;
39
40 static GMainLoop *main_loop = NULL;
41
42 static void
test_thread_functions(void)43 test_thread_functions (void)
44 {
45 gint max_unused_threads;
46 guint max_idle_time;
47
48 /* This function attempts to call functions which don't need a
49 * threadpool to operate to make sure no uninitialised pointers
50 * accessed and no errors occur.
51 */
52
53 max_unused_threads = 3;
54
55 DEBUG_MSG (("[funcs] Setting max unused threads to %d",
56 max_unused_threads));
57 g_thread_pool_set_max_unused_threads (max_unused_threads);
58
59 DEBUG_MSG (("[funcs] Getting max unused threads = %d",
60 g_thread_pool_get_max_unused_threads ()));
61 g_assert (g_thread_pool_get_max_unused_threads() == max_unused_threads);
62
63 DEBUG_MSG (("[funcs] Getting num unused threads = %d",
64 g_thread_pool_get_num_unused_threads ()));
65 g_assert (g_thread_pool_get_num_unused_threads () == 0);
66
67 DEBUG_MSG (("[funcs] Stopping unused threads"));
68 g_thread_pool_stop_unused_threads ();
69
70 max_idle_time = 10 * G_USEC_PER_SEC;
71
72 DEBUG_MSG (("[funcs] Setting max idle time to %d",
73 max_idle_time));
74 g_thread_pool_set_max_idle_time (max_idle_time);
75
76 DEBUG_MSG (("[funcs] Getting max idle time = %d",
77 g_thread_pool_get_max_idle_time ()));
78 g_assert (g_thread_pool_get_max_idle_time () == max_idle_time);
79
80 DEBUG_MSG (("[funcs] Setting max idle time to 0"));
81 g_thread_pool_set_max_idle_time (0);
82
83 DEBUG_MSG (("[funcs] Getting max idle time = %d",
84 g_thread_pool_get_max_idle_time ()));
85 g_assert (g_thread_pool_get_max_idle_time () == 0);
86 }
87
88 static void
test_thread_stop_unused(void)89 test_thread_stop_unused (void)
90 {
91 GThreadPool *pool;
92 guint i;
93 guint limit = 100;
94
95 /* Spawn a few threads. */
96 g_thread_pool_set_max_unused_threads (-1);
97 pool = g_thread_pool_new ((GFunc) g_usleep, NULL, -1, FALSE, NULL);
98
99 for (i = 0; i < limit; i++)
100 g_thread_pool_push (pool, GUINT_TO_POINTER (1000), NULL);
101
102 DEBUG_MSG (("[unused] ===> pushed %d threads onto the idle pool",
103 limit));
104
105 /* Wait for the threads to migrate. */
106 g_usleep (G_USEC_PER_SEC);
107
108 DEBUG_MSG (("[unused] stopping unused threads"));
109 g_thread_pool_stop_unused_threads ();
110
111 for (i = 0; i < 5; i++)
112 {
113 if (g_thread_pool_get_num_unused_threads () == 0)
114 break;
115
116 DEBUG_MSG (("[unused] waiting ONE second for threads to die"));
117
118 /* Some time for threads to die. */
119 g_usleep (G_USEC_PER_SEC);
120 }
121
122 DEBUG_MSG (("[unused] stopped idle threads, %d remain",
123 g_thread_pool_get_num_unused_threads ()));
124
125 g_assert (g_thread_pool_get_num_unused_threads () == 0);
126
127 g_thread_pool_set_max_unused_threads (MAX_THREADS);
128
129 DEBUG_MSG (("[unused] cleaning up thread pool"));
130 g_thread_pool_free (pool, FALSE, TRUE);
131 }
132
133 static void
test_thread_pools_entry_func(gpointer data,gpointer user_data)134 test_thread_pools_entry_func (gpointer data, gpointer user_data)
135 {
136 #ifdef DEBUG
137 guint id = 0;
138
139 id = GPOINTER_TO_UINT (data);
140 #endif
141
142 DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id));
143
144 G_LOCK (thread_counter_pools);
145 abs_thread_counter++;
146 running_thread_counter++;
147 G_UNLOCK (thread_counter_pools);
148
149 g_usleep (g_random_int_range (0, 4000));
150
151 G_LOCK (thread_counter_pools);
152 running_thread_counter--;
153 leftover_task_counter--;
154
155 DEBUG_MSG (("[pool] ---> [%3.3d] exiting thread (abs count:%ld, "
156 "running count:%ld, left over:%ld)",
157 id, abs_thread_counter,
158 running_thread_counter, leftover_task_counter));
159 G_UNLOCK (thread_counter_pools);
160 }
161
162 static void
test_thread_pools(void)163 test_thread_pools (void)
164 {
165 GThreadPool *pool1, *pool2, *pool3;
166 guint runs;
167 guint i;
168
169 pool1 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 3, FALSE, NULL);
170 pool2 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 5, TRUE, NULL);
171 pool3 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 7, TRUE, NULL);
172
173 runs = 300;
174 for (i = 0; i < runs; i++)
175 {
176 g_thread_pool_push (pool1, GUINT_TO_POINTER (i + 1), NULL);
177 g_thread_pool_push (pool2, GUINT_TO_POINTER (i + 1), NULL);
178 g_thread_pool_push (pool3, GUINT_TO_POINTER (i + 1), NULL);
179
180 G_LOCK (thread_counter_pools);
181 leftover_task_counter += 3;
182 G_UNLOCK (thread_counter_pools);
183 }
184
185 g_thread_pool_free (pool1, TRUE, TRUE);
186 g_thread_pool_free (pool2, FALSE, TRUE);
187 g_thread_pool_free (pool3, FALSE, TRUE);
188
189 g_assert (runs * 3 == abs_thread_counter + leftover_task_counter);
190 g_assert (running_thread_counter == 0);
191 }
192
193 static gint
test_thread_sort_compare_func(gconstpointer a,gconstpointer b,gpointer user_data)194 test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
195 {
196 guint32 id1, id2;
197
198 id1 = GPOINTER_TO_UINT (a);
199 id2 = GPOINTER_TO_UINT (b);
200
201 return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
202 }
203
204 static void
test_thread_sort_entry_func(gpointer data,gpointer user_data)205 test_thread_sort_entry_func (gpointer data, gpointer user_data)
206 {
207 guint thread_id;
208 gboolean is_sorted;
209
210 G_LOCK (last_thread);
211
212 thread_id = GPOINTER_TO_UINT (data);
213 is_sorted = GPOINTER_TO_INT (user_data);
214
215 DEBUG_MSG (("%s ---> entered thread:%2.2d, last thread:%2.2d",
216 is_sorted ? "[ sorted]" : "[unsorted]",
217 thread_id, last_thread_id));
218
219 if (is_sorted) {
220 static gboolean last_failed = FALSE;
221
222 if (last_thread_id > thread_id) {
223 if (last_failed) {
224 g_assert (last_thread_id <= thread_id);
225 }
226
227 /* Here we remember one fail and if it concurrently fails, it
228 * can not be sorted. the last thread id might be < this thread
229 * id if something is added to the queue since threads were
230 * created
231 */
232 last_failed = TRUE;
233 } else {
234 last_failed = FALSE;
235 }
236
237 last_thread_id = thread_id;
238 }
239
240 G_UNLOCK (last_thread);
241
242 g_usleep (WAIT * 1000);
243 }
244
245 static void
test_thread_sort(gboolean sort)246 test_thread_sort (gboolean sort)
247 {
248 GThreadPool *pool;
249 guint limit;
250 guint max_threads;
251 gint i;
252
253 limit = MAX_THREADS * 10;
254
255 if (sort) {
256 max_threads = 1;
257 } else {
258 max_threads = MAX_THREADS;
259 }
260
261 /* It is important that we only have a maximum of 1 thread for this
262 * test since the results can not be guaranteed to be sorted if > 1.
263 *
264 * Threads are scheduled by the operating system and are executed at
265 * random. It cannot be assumed that threads are executed in the
266 * order they are created. This was discussed in bug #334943.
267 */
268
269 pool = g_thread_pool_new (test_thread_sort_entry_func,
270 GINT_TO_POINTER (sort),
271 max_threads,
272 FALSE,
273 NULL);
274
275 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
276
277 if (sort) {
278 g_thread_pool_set_sort_function (pool,
279 test_thread_sort_compare_func,
280 GUINT_TO_POINTER (69));
281 }
282
283 for (i = 0; i < limit; i++) {
284 guint id;
285
286 id = g_random_int_range (1, limit) + 1;
287 g_thread_pool_push (pool, GUINT_TO_POINTER (id), NULL);
288 DEBUG_MSG (("%s ===> pushed new thread with id:%d, number "
289 "of threads:%d, unprocessed:%d",
290 sort ? "[ sorted]" : "[unsorted]",
291 id,
292 g_thread_pool_get_num_threads (pool),
293 g_thread_pool_unprocessed (pool)));
294 }
295
296 g_assert (g_thread_pool_get_max_threads (pool) == max_threads);
297 g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool));
298 g_thread_pool_free (pool, TRUE, TRUE);
299 }
300
301 static void
test_thread_idle_time_entry_func(gpointer data,gpointer user_data)302 test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
303 {
304 #ifdef DEBUG
305 guint thread_id;
306
307 thread_id = GPOINTER_TO_UINT (data);
308 #endif
309
310 DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
311
312 g_usleep (WAIT * 1000);
313
314 DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id));
315 }
316
317 static gboolean
test_thread_idle_timeout(gpointer data)318 test_thread_idle_timeout (gpointer data)
319 {
320 gint i;
321
322 for (i = 0; i < 2; i++) {
323 g_thread_pool_push (idle_pool, GUINT_TO_POINTER (100 + i), NULL);
324 DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, number "
325 "of threads:%d, unprocessed:%d",
326 100 + i,
327 g_thread_pool_get_num_threads (idle_pool),
328 g_thread_pool_unprocessed (idle_pool)));
329 }
330
331
332 return FALSE;
333 }
334
335 static void
test_thread_idle_time(void)336 test_thread_idle_time (void)
337 {
338 guint limit = 50;
339 guint interval = 10000;
340 gint i;
341
342 idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func,
343 NULL,
344 0,
345 FALSE,
346 NULL);
347
348 g_thread_pool_set_max_threads (idle_pool, MAX_THREADS, NULL);
349 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
350 g_thread_pool_set_max_idle_time (interval);
351
352 g_assert (g_thread_pool_get_max_threads (idle_pool) == MAX_THREADS);
353 g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);
354 g_assert (g_thread_pool_get_max_idle_time () == interval);
355
356 for (i = 0; i < limit; i++) {
357 g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL);
358 DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, "
359 "number of threads:%d, unprocessed:%d",
360 i,
361 g_thread_pool_get_num_threads (idle_pool),
362 g_thread_pool_unprocessed (idle_pool)));
363 }
364
365 g_assert_cmpint (g_thread_pool_unprocessed (idle_pool), <=, limit);
366
367 g_timeout_add ((interval - 1000),
368 test_thread_idle_timeout,
369 GUINT_TO_POINTER (interval));
370 }
371
372 static gboolean
test_check_start_and_stop(gpointer user_data)373 test_check_start_and_stop (gpointer user_data)
374 {
375 static guint test_number = 0;
376 static gboolean run_next = FALSE;
377 gboolean continue_timeout = TRUE;
378 gboolean quit = TRUE;
379
380 if (test_number == 0) {
381 run_next = TRUE;
382 DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number));
383 }
384
385 if (run_next) {
386 test_number++;
387
388 switch (test_number) {
389 case 1:
390 test_thread_functions ();
391 break;
392 case 2:
393 test_thread_stop_unused ();
394 break;
395 case 3:
396 test_thread_pools ();
397 break;
398 case 4:
399 test_thread_sort (FALSE);
400 break;
401 case 5:
402 test_thread_sort (TRUE);
403 break;
404 case 6:
405 test_thread_stop_unused ();
406 break;
407 case 7:
408 test_thread_idle_time ();
409 break;
410 default:
411 DEBUG_MSG (("***** END OF TESTS *****"));
412 g_main_loop_quit (main_loop);
413 continue_timeout = FALSE;
414 break;
415 }
416
417 run_next = FALSE;
418 return continue_timeout;
419 }
420
421 if (test_number == 3) {
422 G_LOCK (thread_counter_pools);
423 quit &= running_thread_counter <= 0;
424 DEBUG_MSG (("***** POOL RUNNING THREAD COUNT:%ld",
425 running_thread_counter));
426 G_UNLOCK (thread_counter_pools);
427 }
428
429 if (test_number == 4 || test_number == 5) {
430 G_LOCK (thread_counter_sort);
431 quit &= sort_thread_counter <= 0;
432 DEBUG_MSG (("***** POOL SORT THREAD COUNT:%ld",
433 sort_thread_counter));
434 G_UNLOCK (thread_counter_sort);
435 }
436
437 if (test_number == 7) {
438 guint idle;
439
440 idle = g_thread_pool_get_num_unused_threads ();
441 quit &= idle < 1;
442 DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d",
443 idle, g_thread_pool_unprocessed (idle_pool)));
444 }
445
446 if (quit) {
447 run_next = TRUE;
448 }
449
450 return continue_timeout;
451 }
452
453 int
main(int argc,char * argv[])454 main (int argc, char *argv[])
455 {
456 DEBUG_MSG (("Starting... (in one second)"));
457 g_timeout_add (1000, test_check_start_and_stop, NULL);
458
459 main_loop = g_main_loop_new (NULL, FALSE);
460 g_main_loop_run (main_loop);
461 g_main_loop_unref (main_loop);
462
463 g_thread_pool_free (idle_pool, FALSE, TRUE);
464 return 0;
465 }
466