1d203df4879612fb9700d5a3aef598fa75aba4fa
[platform/upstream/glib.git] / tests / thread-test.c
1 #include <glib.h>
2
3 /* GMutex */
4
5 static GMutex* test_g_mutex_mutex = NULL;
6 static guint test_g_mutex_int = 0;
7 G_LOCK_DEFINE_STATIC (test_g_mutex);
8
9 static void
10 test_g_mutex_thread (gpointer data)
11 {
12   g_assert (GPOINTER_TO_INT (data) == 42);
13   g_assert (g_mutex_trylock (test_g_mutex_mutex) == FALSE);
14   g_assert (G_TRYLOCK (test_g_mutex) == FALSE);
15   g_mutex_lock (test_g_mutex_mutex);
16   g_assert (test_g_mutex_int == 42);
17   g_mutex_unlock (test_g_mutex_mutex);
18 }
19
20 static void
21 test_g_mutex (void)
22 {
23   GThread *thread;
24   test_g_mutex_mutex = g_mutex_new ();
25
26   g_assert (g_mutex_trylock (test_g_mutex_mutex));
27   g_assert (G_TRYLOCK (test_g_mutex));
28   thread = g_thread_create (test_g_mutex_thread, 
29                             GINT_TO_POINTER (42),
30                             0, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL);
31   g_usleep (G_MICROSEC);
32   test_g_mutex_int = 42;
33   G_UNLOCK (test_g_mutex);
34   g_mutex_unlock (test_g_mutex_mutex);
35   g_thread_join (thread);
36   g_mutex_free (test_g_mutex_mutex);
37 }
38
39 /* GStaticRecMutex */
40
41 static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
42 static guint test_g_static_rec_mutex_int = 0;
43
44 static void
45 test_g_static_rec_mutex_thread (gpointer data)
46 {
47   g_assert (GPOINTER_TO_INT (data) == 42);
48   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex) 
49             == FALSE);
50   g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
51   g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
52   g_assert (test_g_static_rec_mutex_int == 42);
53   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
54   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
55 }
56
57 static void
58 test_g_static_rec_mutex (void)
59 {
60   GThread *thread;
61
62   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
63   thread = g_thread_create (test_g_static_rec_mutex_thread, 
64                             GINT_TO_POINTER (42),
65                             0, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL);
66   g_usleep (G_MICROSEC);
67   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
68   g_usleep (G_MICROSEC);
69   test_g_static_rec_mutex_int = 41;
70   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
71   test_g_static_rec_mutex_int = 42;  
72   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
73   g_usleep (G_MICROSEC);
74   g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
75   test_g_static_rec_mutex_int = 0;  
76   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
77   g_thread_join (thread);
78 }
79
80 /* GStaticPrivate */
81
82 #define THREADS 10
83
84 static GStaticPrivate test_g_static_private_private = G_STATIC_PRIVATE_INIT;
85 static GStaticMutex test_g_static_private_mutex = G_STATIC_MUTEX_INIT;
86 static guint test_g_static_private_counter = 0;
87
88 static gpointer
89 test_g_static_private_constructor (void)
90 {
91   g_static_mutex_lock (&test_g_static_private_mutex);
92   test_g_static_private_counter++;
93   g_static_mutex_unlock (&test_g_static_private_mutex);  
94   return g_new (guint,1);
95 }
96
97 static void
98 test_g_static_private_destructor (gpointer data)
99 {
100   g_static_mutex_lock (&test_g_static_private_mutex);
101   test_g_static_private_counter--;
102   g_static_mutex_unlock (&test_g_static_private_mutex);  
103   g_free (data);
104 }
105
106
107 static void
108 test_g_static_private_thread (gpointer data)
109 {
110   guint number = GPOINTER_TO_INT (data);
111   guint i;
112   guint* private;
113   for (i = 0; i < 10; i++)
114     {
115       number = number * 11 + 1; /* A very simple and bad RNG ;-) */
116       private = g_static_private_get (&test_g_static_private_private);
117       if (!private || number % 7 > 3)
118         {
119           private = test_g_static_private_constructor ();
120           g_static_private_set (&test_g_static_private_private, private,
121                                 test_g_static_private_destructor);
122         }
123       *private = number;
124       g_usleep (G_MICROSEC / 5);
125       g_assert (number == *private);
126     }
127 }
128
129 static void
130 test_g_static_private (void)
131 {
132   GThread *threads[THREADS];
133   guint i;
134   for (i = 0; i < THREADS; i++)
135     {
136       threads[i] = g_thread_create (test_g_static_private_thread, 
137                                     GINT_TO_POINTER (i),
138                                     0, TRUE, TRUE, 
139                                     G_THREAD_PRIORITY_NORMAL);      
140     }
141   for (i = 0; i < THREADS; i++)
142     {
143       g_thread_join (threads[i]);
144     }
145   g_assert (test_g_static_private_counter == 0); 
146 }
147
148 /* GStaticRWLock */
149
150 /* -1 = writing; >0 = # of readers */
151 static gint test_g_static_rw_lock_state = 0; 
152 G_LOCK_DEFINE (test_g_static_rw_lock_state);
153
154 static gboolean test_g_static_rw_lock_run = TRUE; 
155 static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
156
157 static void
158 test_g_static_rw_lock_thread (gpointer data)
159 {
160   while (test_g_static_rw_lock_run)
161     {
162       if (g_random_double() > .2) /* I'm a reader */
163         {
164           
165           if (g_random_double() > .2) /* I'll block */
166             g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
167           else /* I'll only try */
168             if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
169               continue;
170           G_LOCK (test_g_static_rw_lock_state);
171           g_assert (test_g_static_rw_lock_state >= 0);
172           test_g_static_rw_lock_state++;
173           G_UNLOCK (test_g_static_rw_lock_state);
174
175           g_usleep (10);
176
177           G_LOCK (test_g_static_rw_lock_state);
178           test_g_static_rw_lock_state--;
179           G_UNLOCK (test_g_static_rw_lock_state);
180
181           g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
182         }
183       else /* I'm a writer */
184         {
185           
186           if (g_random_double() > .2) /* I'll block */ 
187             g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
188           else /* I'll only try */
189             if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
190               continue;
191           G_LOCK (test_g_static_rw_lock_state);
192           g_assert (test_g_static_rw_lock_state == 0);
193           test_g_static_rw_lock_state = -1;
194           G_UNLOCK (test_g_static_rw_lock_state);
195
196           g_usleep (10);
197
198           G_LOCK (test_g_static_rw_lock_state);
199           test_g_static_rw_lock_state = 0;
200           G_UNLOCK (test_g_static_rw_lock_state);
201
202           g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
203         }
204     }
205 }
206
207 static void
208 test_g_static_rw_lock ()
209 {
210   GThread *threads[THREADS];
211   guint i;
212   for (i = 0; i < THREADS; i++)
213     {
214       threads[i] = g_thread_create (test_g_static_rw_lock_thread, 
215                                     0, 0, TRUE, TRUE, 
216                                     G_THREAD_PRIORITY_NORMAL);      
217     }
218   g_usleep (G_MICROSEC);
219   test_g_static_rw_lock_run = FALSE;
220   for (i = 0; i < THREADS; i++)
221     {
222       g_thread_join (threads[i]);
223     }
224   g_assert (test_g_static_rw_lock_state == 0);
225 }
226
227 /* run all the tests */
228 void
229 run_all_tests()
230 {
231   test_g_mutex ();
232   test_g_static_rec_mutex ();
233   test_g_static_private ();
234   test_g_static_rw_lock ();  
235 }
236
237 int 
238 main (int   argc,
239       char *argv[])
240 {
241   /* Only run the test, if threads are enabled and a default thread
242      implementation is available */
243 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
244   g_thread_init (NULL);
245   run_all_tests ();
246
247   /* Now we rerun all tests, but this time we fool the system into
248    * thinking, that the available thread system is not native, but
249    * userprovided. */
250
251   g_thread_use_default_impl = FALSE;
252   run_all_tests ();
253   
254 #endif
255   return 0;
256 }