Tizen 2.1 base
[platform/upstream/glib2.0.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 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
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_thread_stop_unused (void)
85 {
86    GThreadPool *pool;
87    guint i;
88    guint limit = 100;
89
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);
93
94    for (i = 0; i < limit; i++)
95      g_thread_pool_push (pool, GUINT_TO_POINTER (1000), NULL);
96
97    DEBUG_MSG (("[unused] ===> pushed %d threads onto the idle pool",
98                limit));
99
100    /* Wait for the threads to migrate. */
101    g_usleep (G_USEC_PER_SEC);
102
103    DEBUG_MSG (("[unused] stopping unused threads"));
104    g_thread_pool_stop_unused_threads ();
105
106    for (i = 0; i < 5; i++)
107      {
108        if (g_thread_pool_get_num_unused_threads () == 0)
109          break;
110
111        DEBUG_MSG (("[unused] waiting ONE second for threads to die"));
112
113        /* Some time for threads to die. */
114        g_usleep (G_USEC_PER_SEC);
115      }
116
117    DEBUG_MSG (("[unused] stopped idle threads, %d remain",
118                g_thread_pool_get_num_unused_threads ()));
119
120    g_assert (g_thread_pool_get_num_unused_threads () == 0);
121
122    g_thread_pool_set_max_unused_threads (MAX_THREADS);
123
124    DEBUG_MSG (("[unused] cleaning up thread pool"));
125    g_thread_pool_free (pool, FALSE, TRUE);
126 }
127
128 static void
129 test_thread_pools_entry_func (gpointer data, gpointer user_data)
130 {
131   guint id = 0;
132
133   id = GPOINTER_TO_UINT (data);
134
135   DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id));
136
137   G_LOCK (thread_counter_pools);
138   abs_thread_counter++;
139   running_thread_counter++;
140   G_UNLOCK (thread_counter_pools);
141
142   g_usleep (g_random_int_range (0, 4000));
143
144   G_LOCK (thread_counter_pools);
145   running_thread_counter--;
146   leftover_task_counter--;
147
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);
153 }
154
155 static void
156 test_thread_pools (void)
157 {
158   GThreadPool *pool1, *pool2, *pool3;
159   guint runs;
160   guint i;
161
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);
165
166   runs = 300;
167   for (i = 0; i < runs; i++)
168     {
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);
172
173       G_LOCK (thread_counter_pools);
174       leftover_task_counter += 3;
175       G_UNLOCK (thread_counter_pools);
176     }
177
178   g_thread_pool_free (pool1, TRUE, TRUE);
179   g_thread_pool_free (pool2, FALSE, TRUE);
180   g_thread_pool_free (pool3, FALSE, TRUE);
181
182   g_assert (runs * 3 == abs_thread_counter + leftover_task_counter);
183   g_assert (running_thread_counter == 0);
184 }
185
186 static gint
187 test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
188 {
189   guint32 id1, id2;
190
191   id1 = GPOINTER_TO_UINT (a);
192   id2 = GPOINTER_TO_UINT (b);
193
194   return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
195 }
196
197 static void
198 test_thread_sort_entry_func (gpointer data, gpointer user_data)
199 {
200   guint thread_id;
201   gboolean is_sorted;
202
203   G_LOCK (last_thread);
204
205   thread_id = GPOINTER_TO_UINT (data);
206   is_sorted = GPOINTER_TO_INT (user_data);
207
208   DEBUG_MSG (("%s ---> entered thread:%2.2d, last thread:%2.2d",
209               is_sorted ? "[  sorted]" : "[unsorted]",
210               thread_id, last_thread_id));
211
212   if (is_sorted) {
213     static gboolean last_failed = FALSE;
214
215     if (last_thread_id > thread_id) {
216       if (last_failed) {
217         g_assert (last_thread_id <= thread_id);
218       }
219
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
224        */
225       last_failed = TRUE;
226     } else {
227       last_failed = FALSE;
228     }
229
230     last_thread_id = thread_id;
231   }
232
233   G_UNLOCK (last_thread);
234
235   g_usleep (WAIT * 1000);
236 }
237
238 static void
239 test_thread_sort (gboolean sort)
240 {
241   GThreadPool *pool;
242   guint limit;
243   guint max_threads;
244   gint i;
245
246   limit = MAX_THREADS * 10;
247
248   if (sort) {
249     max_threads = 1;
250   } else {
251     max_threads = MAX_THREADS;
252   }
253
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.
256    *
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.
260    */
261
262   pool = g_thread_pool_new (test_thread_sort_entry_func,
263                             GINT_TO_POINTER (sort),
264                             max_threads,
265                             FALSE,
266                             NULL);
267
268   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
269
270   if (sort) {
271     g_thread_pool_set_sort_function (pool,
272                                      test_thread_sort_compare_func,
273                                      GUINT_TO_POINTER (69));
274   }
275
276   for (i = 0; i < limit; i++) {
277     guint id;
278
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]",
284                 id,
285                 g_thread_pool_get_num_threads (pool),
286                 g_thread_pool_unprocessed (pool)));
287   }
288
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));
291 }
292
293 static void
294 test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
295 {
296   guint thread_id;
297
298   thread_id = GPOINTER_TO_UINT (data);
299
300   DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
301
302   g_usleep (WAIT * 1000);
303
304   DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id));
305 }
306
307 static gboolean
308 test_thread_idle_timeout (gpointer data)
309 {
310   guint interval;
311   gint i;
312
313   interval = GPOINTER_TO_UINT (data);
314
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)));
322   }
323
324
325   return FALSE;
326 }
327
328 static void
329 test_thread_idle_time ()
330 {
331   guint limit = 50;
332   guint interval = 10000;
333   gint i;
334
335   idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func,
336                                  NULL,
337                                  MAX_THREADS,
338                                  FALSE,
339                                  NULL);
340
341   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
342   g_thread_pool_set_max_idle_time (interval);
343
344   g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);
345   g_assert (g_thread_pool_get_max_idle_time () == interval);
346
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",
351                 i,
352                 g_thread_pool_get_num_threads (idle_pool),
353                 g_thread_pool_unprocessed (idle_pool)));
354   }
355
356   g_timeout_add ((interval - 1000),
357                  test_thread_idle_timeout,
358                  GUINT_TO_POINTER (interval));
359 }
360
361 static gboolean
362 test_check_start_and_stop (gpointer user_data)
363 {
364   static guint test_number = 0;
365   static gboolean run_next = FALSE;
366   gboolean continue_timeout = TRUE;
367   gboolean quit = TRUE;
368
369   if (test_number == 0) {
370     run_next = TRUE;
371     DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number));
372   }
373
374   if (run_next) {
375     test_number++;
376
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;
404     }
405
406     run_next = FALSE;
407     return TRUE;
408   }
409
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);
416   }
417
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);
424   }
425
426   if (test_number == 7) {
427     guint idle;
428
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)));
433   }
434
435   if (quit) {
436     run_next = TRUE;
437   }
438
439   return continue_timeout;
440 }
441
442 int
443 main (int argc, char *argv[])
444 {
445   g_thread_init (NULL);
446
447   DEBUG_MSG (("Starting... (in one second)"));
448   g_timeout_add (1000, test_check_start_and_stop, NULL);
449
450   main_loop = g_main_loop_new (NULL, FALSE);
451   g_main_loop_run (main_loop);
452
453   return 0;
454 }