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