d5fa56e1ce0b9d26f06008c221e904575b6cbe0a
[platform/upstream/glib.git] / tests / threadpool-test.c
1 #include <glib.h>
2
3 #define RUNS 100
4
5 G_LOCK_DEFINE_STATIC (thread_counter);
6 gulong abs_thread_counter;
7 gulong running_thread_counter;
8
9 void
10 thread_pool_func (gpointer a, gpointer b)
11 {
12   G_LOCK (thread_counter);
13   abs_thread_counter++;
14   running_thread_counter++;
15   G_UNLOCK (thread_counter);
16
17   g_usleep (g_random_int_range (0, 4000));
18
19   G_LOCK (thread_counter);
20   running_thread_counter--;
21   G_UNLOCK (thread_counter);
22 }
23
24 int 
25 main (int   argc,
26       char *argv[])
27 {
28   /* Only run the test, if threads are enabled and a default thread
29      implementation is available */
30 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
31   GThreadPool *pool1, *pool2, *pool3;
32   guint i;
33   g_thread_init (NULL);
34   
35   pool1 = g_thread_pool_new (thread_pool_func, NULL, 3, FALSE, NULL);
36   pool2 = g_thread_pool_new (thread_pool_func, NULL, 5, TRUE, NULL);
37   pool3 = g_thread_pool_new (thread_pool_func, NULL, 7, TRUE, NULL);
38
39   for (i = 0; i < RUNS; i++)
40     {
41       g_thread_pool_push (pool1, GUINT_TO_POINTER (1), NULL);
42       g_thread_pool_push (pool2, GUINT_TO_POINTER (1), NULL);
43       g_thread_pool_push (pool3, GUINT_TO_POINTER (1), NULL);
44     } 
45   
46   g_thread_pool_free (pool1, FALSE, TRUE);
47   g_thread_pool_free (pool2, FALSE, TRUE);
48   g_thread_pool_free (pool3, FALSE, TRUE);
49
50   g_assert (RUNS * 3 == abs_thread_counter);
51   g_assert (running_thread_counter == 0);  
52 #endif
53   return 0;
54 }