Updated the documentation to explain that when the maximum threads is > 1
[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 RUNS 100
12
13 #define WAIT                5    /* seconds */
14 #define MAX_THREADS         10
15
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    
20
21 G_LOCK_DEFINE_STATIC (thread_counter_pools);
22
23 static gulong abs_thread_counter = 0;
24 static gulong running_thread_counter = 0;
25 static gulong leftover_task_counter = 0;
26
27 G_LOCK_DEFINE_STATIC (last_thread);
28
29 static guint last_thread_id = 0;
30
31 G_LOCK_DEFINE_STATIC (thread_counter_sort);
32
33 static gulong sort_thread_counter = 0;
34
35 static GThreadPool *idle_pool = NULL;
36
37 static GMainLoop *main_loop = NULL;
38
39
40 static void
41 test_thread_pools_entry_func (gpointer data, gpointer user_data)
42 {
43   guint id = 0;
44
45   id = GPOINTER_TO_UINT (data);
46
47   DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id));
48
49   G_LOCK (thread_counter_pools);
50   abs_thread_counter++;
51   running_thread_counter++;
52   G_UNLOCK (thread_counter_pools);
53
54   g_usleep (g_random_int_range (0, 4000));
55
56   G_LOCK (thread_counter_pools);
57   running_thread_counter--;
58   leftover_task_counter--;
59
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);
65 }
66
67 static void
68 test_thread_pools (void)
69 {
70   GThreadPool *pool1, *pool2, *pool3;
71   guint i;
72   
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);
76
77   for (i = 0; i < RUNS; i++)
78     {
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;
83     } 
84   
85   g_thread_pool_free (pool1, TRUE, TRUE);
86   g_thread_pool_free (pool2, FALSE, TRUE);
87   g_thread_pool_free (pool3, FALSE, TRUE);
88
89   g_assert (RUNS * 3 == abs_thread_counter + leftover_task_counter);
90   g_assert (running_thread_counter == 0);  
91 }
92
93 static gint
94 test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
95 {
96   guint32 id1, id2;
97
98   id1 = GPOINTER_TO_UINT (a);
99   id2 = GPOINTER_TO_UINT (b);
100
101   return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1); 
102 }
103
104 static void
105 test_thread_sort_entry_func (gpointer data, gpointer user_data)
106 {
107   guint thread_id;
108   gboolean is_sorted;
109
110   G_LOCK (last_thread);
111
112   thread_id = GPOINTER_TO_UINT (data);
113   is_sorted = GPOINTER_TO_INT (user_data);
114
115   DEBUG_MSG (("%s ---> entered thread:%2.2d, last thread:%2.2d", 
116               is_sorted ? "[  sorted]" : "[unsorted]", 
117               thread_id, last_thread_id));
118
119   if (is_sorted) {
120     static gboolean last_failed = FALSE;
121
122     if (last_thread_id > thread_id) {
123       if (last_failed) {
124         g_assert (last_thread_id <= thread_id);  
125       }
126
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
130        * created  
131        */
132       last_failed = TRUE;
133     } else {
134       last_failed = FALSE;
135     }
136
137     last_thread_id = thread_id;
138   }
139
140   G_UNLOCK (last_thread);
141
142   g_usleep (WAIT * 1000);
143 }
144
145 static void
146 test_thread_sort (gboolean sort)
147 {
148   GThreadPool *pool;
149   guint limit;
150   guint max_threads;
151   gint i;
152
153   limit = MAX_THREADS * 10;
154
155   if (sort) {
156     max_threads = 1;
157   } else {
158     max_threads = MAX_THREADS;
159   }
160
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.
163    * 
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.
167    */
168   
169   pool = g_thread_pool_new (test_thread_sort_entry_func, 
170                             GINT_TO_POINTER (sort), 
171                             max_threads, 
172                             FALSE,
173                             NULL);
174
175   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS); 
176
177   if (sort) {
178     g_thread_pool_set_sort_function (pool, 
179                                      test_thread_sort_compare_func,
180                                      GUINT_TO_POINTER (69));
181   }
182   
183   for (i = 0; i < limit; i++) {
184     guint id;
185
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]", 
191                 id, 
192                 g_thread_pool_get_num_threads (pool),
193                 g_thread_pool_unprocessed (pool)));
194   }
195
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));
198 }
199
200 static void
201 test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
202 {
203   guint thread_id;
204   
205   thread_id = GPOINTER_TO_UINT (data);
206
207   DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
208
209   g_usleep (WAIT * 1000);
210
211   DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id));
212 }
213
214 static gboolean 
215 test_thread_idle_timeout (gpointer data)
216 {
217   guint interval;
218   gint i;
219
220   interval = GPOINTER_TO_UINT (data);
221   
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",
226                 100 + i, 
227                 g_thread_pool_get_num_threads (idle_pool),
228                 g_thread_pool_unprocessed (idle_pool)));
229   }
230   
231
232   return FALSE;
233 }
234
235 static void
236 test_thread_idle_time ()
237 {
238   guint limit = 50;
239   guint interval = 10000;
240   gint i;
241
242   idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func, 
243                                  NULL, 
244                                  MAX_THREADS,
245                                  FALSE,
246                                  NULL);
247
248   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);  
249   g_thread_pool_set_max_idle_time (interval); 
250
251   g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);   
252   g_assert (g_thread_pool_get_max_idle_time () == interval);
253
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",
258                 i,
259                 g_thread_pool_get_num_threads (idle_pool),
260                 g_thread_pool_unprocessed (idle_pool)));
261   }
262
263   g_timeout_add ((interval - 1000),
264                  test_thread_idle_timeout, 
265                  GUINT_TO_POINTER (interval));
266 }
267
268 static gboolean
269 test_check_start_and_stop (gpointer user_data)
270 {
271   static guint test_number = 0;
272   static gboolean run_next = FALSE;
273   gboolean continue_timeout = TRUE;
274   gboolean quit = TRUE;
275
276   if (test_number == 0) {
277     run_next = TRUE;
278     DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number)); 
279   }
280    
281   if (run_next) {
282     test_number++;
283
284     switch (test_number) {
285     case 1:
286       test_thread_pools ();   
287       break;
288     case 2:
289       test_thread_sort (FALSE);  
290       break;
291     case 3:
292       test_thread_sort (TRUE);  
293       break;
294     case 4:
295       test_thread_idle_time ();   
296       break;
297     default:
298       DEBUG_MSG (("***** END OF TESTS *****")); 
299       g_main_loop_quit (main_loop);
300       continue_timeout = FALSE;
301       break;
302     }
303
304     run_next = FALSE;
305     return TRUE;
306   }
307
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); 
314   }
315
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); 
322   }
323
324   if (test_number == 4) {
325     guint idle;
326
327     idle = g_thread_pool_get_num_unused_threads ();
328     quit &= idle < 1;
329     DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d",
330                 idle, g_thread_pool_unprocessed (idle_pool)));
331   }    
332
333   if (quit) {
334     run_next = TRUE;
335   }
336
337   return continue_timeout;
338 }
339
340 int 
341 main (int argc, char *argv[])
342 {
343   /* Only run the test, if threads are enabled and a default thread
344      implementation is available */
345
346 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
347   g_thread_init (NULL);
348
349   DEBUG_MSG (("Starting... (in one second)"));
350   g_timeout_add (1000, test_check_start_and_stop, NULL); 
351   
352   main_loop = g_main_loop_new (NULL, FALSE);
353   g_main_loop_run (main_loop);
354 #endif
355
356   return 0;
357 }