To avoid deadlocks get rid of the settings G_LOCK. Use the
[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       last_failed = TRUE;
132     } else {
133       last_failed = FALSE;
134     }
135
136     last_thread_id = thread_id;
137   }
138
139   G_UNLOCK (last_thread);
140
141   g_usleep (WAIT * 1000);
142 }
143
144 static void
145 test_thread_sort (gboolean sort)
146 {
147   GThreadPool *pool;
148   guint limit = 20;
149   gint i;
150
151   pool = g_thread_pool_new (test_thread_sort_entry_func, 
152                             GINT_TO_POINTER (sort), 
153                             MAX_THREADS,
154                             FALSE,
155                             NULL);
156
157   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS); 
158
159   if (sort) {
160     g_thread_pool_set_sort_function (pool, 
161                                      test_thread_sort_compare_func,
162                                      GUINT_TO_POINTER (69));
163   }
164   
165   for (i = 0; i < limit; i++) {
166     guint id;
167
168     id = g_random_int_range (1, limit*2);
169     g_thread_pool_push (pool, GUINT_TO_POINTER (id + 1), NULL);
170   }
171
172   g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool));
173 }
174
175 static void
176 test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
177 {
178   guint thread_id;
179   
180   thread_id = GPOINTER_TO_UINT (data);
181
182   DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
183
184   g_usleep (WAIT * 1000);
185
186   DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id));
187 }
188
189 static gboolean 
190 test_thread_idle_timeout (gpointer data)
191 {
192   guint interval;
193   gint i;
194
195   interval = GPOINTER_TO_UINT (data);
196   
197   for (i = 0; i < 2; i++) {
198     g_thread_pool_push (idle_pool, GUINT_TO_POINTER (100 + i), NULL); 
199     DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, number "
200                 "of threads:%d, unprocessed:%d",
201                 100 + i, 
202                 g_thread_pool_get_num_threads (idle_pool),
203                 g_thread_pool_unprocessed (idle_pool)));
204   }
205   
206
207   return FALSE;
208 }
209
210 static void
211 test_thread_idle_time ()
212 {
213   guint limit = 50;
214   guint interval = 10000;
215   gint i;
216
217   idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func, 
218                                  NULL, 
219                                  MAX_THREADS,
220                                  FALSE,
221                                  NULL);
222
223   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);  
224   g_thread_pool_set_max_idle_time (interval); 
225
226   g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);   
227   g_assert (g_thread_pool_get_max_idle_time () == interval);
228
229   for (i = 0; i < limit; i++) {
230     g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL); 
231     DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, "
232                 "number of threads:%d, unprocessed:%d",
233                 i,
234                 g_thread_pool_get_num_threads (idle_pool),
235                 g_thread_pool_unprocessed (idle_pool)));
236   }
237
238   g_timeout_add ((interval - 1000),
239                  test_thread_idle_timeout, 
240                  GUINT_TO_POINTER (interval));
241 }
242
243 static gboolean
244 test_check_start_and_stop (gpointer user_data)
245 {
246   static guint test_number = 0;
247   static gboolean run_next = FALSE;
248   gboolean continue_timeout = TRUE;
249   gboolean quit = TRUE;
250
251   if (test_number == 0) {
252     run_next = TRUE;
253     DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number)); 
254   }
255    
256   if (run_next) {
257     test_number++;
258
259     switch (test_number) {
260     case 1:
261       test_thread_pools ();   
262       break;
263     case 2:
264       test_thread_sort (FALSE);  
265       break;
266     case 3:
267       test_thread_sort (TRUE);  
268       break;
269     case 4:
270       test_thread_idle_time ();   
271       break;
272     default:
273       DEBUG_MSG (("***** END OF TESTS *****")); 
274       g_main_loop_quit (main_loop);
275       continue_timeout = FALSE;
276       break;
277     }
278
279     run_next = FALSE;
280     return TRUE;
281   }
282
283   if (test_number == 1) {
284     G_LOCK (thread_counter_pools); 
285     quit &= running_thread_counter <= 0;
286     DEBUG_MSG (("***** POOL RUNNING THREAD COUNT:%ld", 
287                 running_thread_counter)); 
288     G_UNLOCK (thread_counter_pools); 
289   }
290
291   if (test_number == 2 || test_number == 3) {
292     G_LOCK (thread_counter_sort);
293     quit &= sort_thread_counter <= 0;
294     DEBUG_MSG (("***** POOL SORT THREAD COUNT:%ld", 
295                 sort_thread_counter)); 
296     G_UNLOCK (thread_counter_sort); 
297   }
298
299   if (test_number == 4) {
300     guint idle;
301
302     idle = g_thread_pool_get_num_unused_threads ();
303     quit &= idle < 1;
304     DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d",
305                 idle, g_thread_pool_unprocessed (idle_pool)));
306   }    
307
308   if (quit) {
309     run_next = TRUE;
310   }
311
312   return continue_timeout;
313 }
314
315 int 
316 main (int argc, char *argv[])
317 {
318   /* Only run the test, if threads are enabled and a default thread
319      implementation is available */
320
321 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
322   g_thread_init (NULL);
323
324   DEBUG_MSG (("Starting... (in one second)"));
325   g_timeout_add (1000, test_check_start_and_stop, NULL); 
326   
327   main_loop = g_main_loop_new (NULL, FALSE);
328   g_main_loop_run (main_loop);
329 #endif
330
331   return 0;
332 }