1 #undef G_DISABLE_ASSERT
8 /* #define DEBUG_MSG(x) */
9 #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");
13 #define WAIT 5 /* seconds */
14 #define MAX_THREADS 10
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
21 G_LOCK_DEFINE_STATIC (thread_counter_pools);
23 static gulong abs_thread_counter = 0;
24 static gulong running_thread_counter = 0;
25 static gulong leftover_task_counter = 0;
27 G_LOCK_DEFINE_STATIC (last_thread);
29 static guint last_thread_id = 0;
31 G_LOCK_DEFINE_STATIC (thread_counter_sort);
33 static gulong sort_thread_counter = 0;
35 static GThreadPool *idle_pool = NULL;
37 static GMainLoop *main_loop = NULL;
41 test_thread_pools_entry_func (gpointer data, gpointer user_data)
45 id = GPOINTER_TO_UINT (data);
47 DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id));
49 G_LOCK (thread_counter_pools);
51 running_thread_counter++;
52 G_UNLOCK (thread_counter_pools);
54 g_usleep (g_random_int_range (0, 4000));
56 G_LOCK (thread_counter_pools);
57 running_thread_counter--;
58 leftover_task_counter--;
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);
68 test_thread_pools (void)
70 GThreadPool *pool1, *pool2, *pool3;
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);
77 for (i = 0; i < RUNS; i++)
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;
85 g_thread_pool_free (pool1, TRUE, TRUE);
86 g_thread_pool_free (pool2, FALSE, TRUE);
87 g_thread_pool_free (pool3, FALSE, TRUE);
89 g_assert (RUNS * 3 == abs_thread_counter + leftover_task_counter);
90 g_assert (running_thread_counter == 0);
94 test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
98 id1 = GPOINTER_TO_UINT (a);
99 id2 = GPOINTER_TO_UINT (b);
101 return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
105 test_thread_sort_entry_func (gpointer data, gpointer user_data)
110 G_LOCK (last_thread);
112 thread_id = GPOINTER_TO_UINT (data);
113 is_sorted = GPOINTER_TO_INT (user_data);
115 DEBUG_MSG (("%s ---> entered thread:%2.2d, last thread:%2.2d",
116 is_sorted ? "[ sorted]" : "[unsorted]",
117 thread_id, last_thread_id));
120 static gboolean last_failed = FALSE;
122 if (last_thread_id > thread_id) {
124 g_assert (last_thread_id <= thread_id);
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
136 last_thread_id = thread_id;
139 G_UNLOCK (last_thread);
141 g_usleep (WAIT * 1000);
145 test_thread_sort (gboolean sort)
151 pool = g_thread_pool_new (test_thread_sort_entry_func,
152 GINT_TO_POINTER (sort),
157 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
160 g_thread_pool_set_sort_function (pool,
161 test_thread_sort_compare_func,
162 GUINT_TO_POINTER (69));
165 for (i = 0; i < limit; i++) {
168 id = g_random_int_range (1, limit*2);
169 g_thread_pool_push (pool, GUINT_TO_POINTER (id + 1), NULL);
172 g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool));
176 test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
180 thread_id = GPOINTER_TO_UINT (data);
182 DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
184 g_usleep (WAIT * 1000);
186 DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id));
190 test_thread_idle_timeout (gpointer data)
195 interval = GPOINTER_TO_UINT (data);
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",
202 g_thread_pool_get_num_threads (idle_pool),
203 g_thread_pool_unprocessed (idle_pool)));
211 test_thread_idle_time ()
214 guint interval = 10000;
217 idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func,
223 g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
224 g_thread_pool_set_max_idle_time (interval);
226 g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);
227 g_assert (g_thread_pool_get_max_idle_time () == interval);
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",
234 g_thread_pool_get_num_threads (idle_pool),
235 g_thread_pool_unprocessed (idle_pool)));
238 g_timeout_add ((interval - 1000),
239 test_thread_idle_timeout,
240 GUINT_TO_POINTER (interval));
244 test_check_start_and_stop (gpointer user_data)
246 static guint test_number = 0;
247 static gboolean run_next = FALSE;
248 gboolean continue_timeout = TRUE;
249 gboolean quit = TRUE;
251 if (test_number == 0) {
253 DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number));
259 switch (test_number) {
261 test_thread_pools ();
264 test_thread_sort (FALSE);
267 test_thread_sort (TRUE);
270 test_thread_idle_time ();
273 DEBUG_MSG (("***** END OF TESTS *****"));
274 g_main_loop_quit (main_loop);
275 continue_timeout = FALSE;
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);
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);
299 if (test_number == 4) {
302 idle = g_thread_pool_get_num_unused_threads ();
304 DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d",
305 idle, g_thread_pool_unprocessed (idle_pool)));
312 return continue_timeout;
316 main (int argc, char *argv[])
318 /* Only run the test, if threads are enabled and a default thread
319 implementation is available */
321 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
322 g_thread_init (NULL);
324 DEBUG_MSG (("Starting... (in one second)"));
325 g_timeout_add (1000, test_check_start_and_stop, NULL);
327 main_loop = g_main_loop_new (NULL, FALSE);
328 g_main_loop_run (main_loop);