Include gerror.h before it is used for some g_thread_* functions.
[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   GThreadPool *pool1, *pool2, *pool3;
29   guint i;
30   /* Only run the test, if threads are enabled and a default thread
31      implementation is available */
32 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
33   g_thread_init (NULL);
34   
35   pool1 = g_thread_pool_new (thread_pool_func, 3, 0, FALSE, 
36                             G_THREAD_PRIORITY_NORMAL, FALSE, NULL, NULL);
37   pool2 = g_thread_pool_new (thread_pool_func, 5, 0, FALSE, 
38                              G_THREAD_PRIORITY_LOW, FALSE, NULL, NULL);
39   pool3 = g_thread_pool_new (thread_pool_func, 7, 0, FALSE, 
40                              G_THREAD_PRIORITY_LOW, TRUE, NULL, NULL);
41
42   for (i = 0; i < RUNS; i++)
43     {
44       g_thread_pool_push (pool1, GUINT_TO_POINTER (1), NULL);
45       g_thread_pool_push (pool2, GUINT_TO_POINTER (1), NULL);
46       g_thread_pool_push (pool3, GUINT_TO_POINTER (1), NULL);
47     } 
48   
49   g_thread_pool_free (pool1, FALSE, TRUE);
50   g_thread_pool_free (pool2, FALSE, TRUE);
51   g_thread_pool_free (pool3, FALSE, TRUE);
52
53   g_assert (RUNS * 3 == abs_thread_counter);
54   g_assert (running_thread_counter == 0);  
55 #endif
56   return 0;
57 }