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