New function to sort tasks pushed into a threadpool. (#324479, Martyn
[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 d(x) x
9
10 #define RUNS 100
11
12 #define WAIT                5    /* seconds */
13 #define MAX_THREADS         10
14 #define MAX_UNUSED_THREADS  2
15
16 G_LOCK_DEFINE_STATIC (thread_counter_pools);
17
18 static gulong abs_thread_counter = 0;
19 static gulong running_thread_counter = 0;
20 static gulong leftover_task_counter = 0;
21
22 G_LOCK_DEFINE_STATIC (last_thread);
23
24 static guint last_thread_id = 0;
25
26 G_LOCK_DEFINE_STATIC (thread_counter_sort);
27
28 static gulong sort_thread_counter = 0;
29
30
31 static GMainLoop *main_loop = NULL;
32
33
34 static void
35 test_thread_pools_entry_func (gpointer data, gpointer user_data)
36 {
37   guint id = 0;
38
39   id = GPOINTER_TO_UINT (data);
40
41   d(g_print ("[pool] ---> [%3.3d] entered thread\n", id));
42
43   G_LOCK (thread_counter_pools);
44   abs_thread_counter++;
45   running_thread_counter++;
46   G_UNLOCK (thread_counter_pools);
47
48   g_usleep (g_random_int_range (0, 4000));
49
50   G_LOCK (thread_counter_pools);
51   running_thread_counter--;
52   leftover_task_counter--;
53
54   g_print ("[pool] ---> [%3.3d] exiting thread (abs count:%ld, running count:%ld, left over:%ld)\n", 
55            id, abs_thread_counter, running_thread_counter, leftover_task_counter); 
56   G_UNLOCK (thread_counter_pools);
57 }
58
59 static void
60 test_thread_pools (void)
61 {
62   GThreadPool *pool1, *pool2, *pool3;
63   guint i;
64   
65   pool1 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 3, FALSE, NULL);
66   pool2 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 5, TRUE, NULL);
67   pool3 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 7, TRUE, NULL);
68
69   for (i = 0; i < RUNS; i++)
70     {
71       g_thread_pool_push (pool1, GUINT_TO_POINTER (i), NULL);
72       g_thread_pool_push (pool2, GUINT_TO_POINTER (i), NULL);
73       g_thread_pool_push (pool3, GUINT_TO_POINTER (i), NULL);
74       leftover_task_counter += 3;
75     } 
76   
77   g_thread_pool_free (pool1, TRUE, TRUE);
78   g_thread_pool_free (pool2, FALSE, TRUE);
79   g_thread_pool_free (pool3, FALSE, TRUE);
80
81   g_assert (RUNS * 3 == abs_thread_counter + leftover_task_counter);
82   g_assert (running_thread_counter == 0);  
83 }
84
85 static gint
86 test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
87 {
88   guint32 id1, id2;
89
90   id1 = GPOINTER_TO_UINT (a);
91   id2 = GPOINTER_TO_UINT (b);
92
93   return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1); 
94 }
95
96 static void
97 test_thread_sort_entry_func (gpointer data, gpointer user_data)
98 {
99   guint thread_id;
100   gboolean is_sorted;
101
102   G_LOCK (last_thread);
103
104   thread_id = GPOINTER_TO_UINT (data);
105   is_sorted = GPOINTER_TO_INT (user_data);
106
107   d(g_print ("%s ---> entered thread:%2.2d, last thread:%2.2d\n", 
108              is_sorted ? "[  sorted]" : "[unsorted]", thread_id, last_thread_id));
109
110   if (is_sorted) {
111     static gboolean last_failed = FALSE;
112
113     if (last_thread_id > thread_id) {
114       if (last_failed) {
115         g_assert (last_thread_id <= thread_id);  
116       }
117
118       /* here we remember one fail and if it concurrently fails, it
119          can not be sorted. the last thread id might be < this thread
120          id if something is added to the queue since threads were
121          created */
122       last_failed = TRUE;
123     } else {
124       last_failed = FALSE;
125     }
126
127     last_thread_id = thread_id;
128   }
129
130   G_UNLOCK (last_thread);
131
132   g_usleep (WAIT * 1000);
133 }
134
135 static void
136 test_thread_sort (gboolean sort)
137 {
138   GThreadPool *pool;
139   guint limit = 20;
140   gint i;
141
142   pool = g_thread_pool_new (test_thread_sort_entry_func, 
143                             GINT_TO_POINTER (sort), 
144                             MAX_THREADS,
145                             FALSE,
146                             NULL);
147
148   g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS); 
149
150   if (sort) {
151     g_thread_pool_set_sort_function (pool, 
152                                      test_thread_sort_compare_func,
153                                      GUINT_TO_POINTER (69));
154   }
155   
156   for (i = 0; i < limit; i++) {
157     guint id;
158
159     id = g_random_int_range (1, limit*2);
160     g_thread_pool_push (pool, GUINT_TO_POINTER (id), NULL);
161   }
162
163   g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool));
164 }
165
166 static gboolean
167 test_check_start_and_stop (gpointer user_data)
168 {
169   static guint test_number = 0;
170   static gboolean run_next = FALSE;
171   gboolean continue_timeout = TRUE;
172   gboolean quit = TRUE;
173
174   if (test_number == 0) {
175     run_next = TRUE;
176     d(g_print ("***** RUNNING TEST %2.2d *****\n", test_number)); 
177   }
178    
179   if (run_next) {
180     test_number++;
181
182     switch (test_number) {
183     case 1:
184       test_thread_pools ();   
185       break;
186     case 2:
187       test_thread_sort (FALSE);  
188       break;
189     case 3:
190       test_thread_sort (TRUE);  
191       break;
192     default:
193       d(g_print ("***** END OF TESTS *****\n")); 
194       g_main_loop_quit (main_loop);
195       continue_timeout = FALSE;
196       break;
197     }
198
199     run_next = FALSE;
200     return TRUE;
201   }
202
203   if (test_number == 1) {
204     G_LOCK (thread_counter_pools); 
205     quit &= running_thread_counter <= 0;
206     d(g_print ("***** POOL RUNNING THREAD COUNT:%ld\n", 
207                running_thread_counter)); 
208     G_UNLOCK (thread_counter_pools); 
209   }
210
211   if (test_number == 2 || test_number == 3) {
212     G_LOCK (thread_counter_sort);
213     quit &= sort_thread_counter <= 0;
214     d(g_print ("***** POOL SORT THREAD COUNT:%ld\n", 
215                sort_thread_counter)); 
216     G_UNLOCK (thread_counter_sort); 
217   }
218
219   if (quit) {
220     run_next = TRUE;
221   }
222
223   return continue_timeout;
224 }
225
226 int 
227 main (int argc, char *argv[])
228 {
229   /* Only run the test, if threads are enabled and a default thread
230      implementation is available */
231
232 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
233   g_thread_init (NULL);
234
235   d(g_print ("Starting... (in one second)\n"));
236   g_timeout_add (1000, test_check_start_and_stop, NULL); 
237   
238   main_loop = g_main_loop_new (NULL, FALSE);
239   g_main_loop_run (main_loop);
240 #endif
241
242   return 0;
243 }