Added functions g_static_rec_mutex_init, g_static_rec_mutex_free,
[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, NULL);
31   g_usleep (G_USEC_PER_SEC);
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, NULL);
66   g_usleep (G_USEC_PER_SEC);
67   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
68   g_usleep (G_USEC_PER_SEC);
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_USEC_PER_SEC);
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_private1 = G_STATIC_PRIVATE_INIT;
85 static GStaticPrivate test_g_static_private_private2 = G_STATIC_PRIVATE_INIT;
86 static GStaticMutex test_g_static_private_mutex = G_STATIC_MUTEX_INIT;
87 static guint test_g_static_private_counter = 0;
88 static guint test_g_static_private_ready = 0;
89
90 static gpointer
91 test_g_static_private_constructor (void)
92 {
93   g_static_mutex_lock (&test_g_static_private_mutex);
94   test_g_static_private_counter++;
95   g_static_mutex_unlock (&test_g_static_private_mutex);  
96   return g_new (guint,1);
97 }
98
99 static void
100 test_g_static_private_destructor (gpointer data)
101 {
102   g_static_mutex_lock (&test_g_static_private_mutex);
103   test_g_static_private_counter--;
104   g_static_mutex_unlock (&test_g_static_private_mutex);  
105   g_free (data);
106 }
107
108
109 static void
110 test_g_static_private_thread (gpointer data)
111 {
112   guint number = GPOINTER_TO_INT (data);
113   guint i;
114   guint *private1, *private2;
115   for (i = 0; i < 10; i++)
116     {
117       number = number * 11 + 1; /* A very simple and bad RNG ;-) */
118       private1 = g_static_private_get (&test_g_static_private_private1);
119       if (!private1 || number % 7 > 3)
120         {
121           private1 = test_g_static_private_constructor ();
122           g_static_private_set (&test_g_static_private_private1, private1,
123                                 test_g_static_private_destructor);
124         }
125       *private1 = number;
126       private2 = g_static_private_get (&test_g_static_private_private2);
127       if (!private2 || number % 13 > 5)
128         {
129           private2 = test_g_static_private_constructor ();
130           g_static_private_set (&test_g_static_private_private2, private2,
131                                 test_g_static_private_destructor);
132         }
133       *private2 = number * 2;
134       g_usleep (G_USEC_PER_SEC / 5);
135       g_assert (number == *private1);
136       g_assert (number * 2 == *private2);      
137     }
138   g_static_mutex_lock (&test_g_static_private_mutex);
139   test_g_static_private_ready++;
140   g_static_mutex_unlock (&test_g_static_private_mutex);  
141
142   /* Busy wait is not nice but that's just a test */
143   while (test_g_static_private_ready != 0)
144     g_usleep (G_USEC_PER_SEC / 5);  
145
146   for (i = 0; i < 10; i++)
147     {
148       private2 = g_static_private_get (&test_g_static_private_private2);
149       number = number * 11 + 1; /* A very simple and bad RNG ;-) */
150       if (!private2 || number % 13 > 5)
151         {
152           private2 = test_g_static_private_constructor ();
153           g_static_private_set (&test_g_static_private_private2, private2,
154                                 test_g_static_private_destructor);
155         }      
156       *private2 = number * 2;
157       g_usleep (G_USEC_PER_SEC / 5);
158       g_assert (number * 2 == *private2);      
159     }
160 }
161
162 static void
163 test_g_static_private (void)
164 {
165   GThread *threads[THREADS];
166   guint i;
167
168   test_g_static_private_ready = 0;
169
170   for (i = 0; i < THREADS; i++)
171     {
172       threads[i] = g_thread_create (test_g_static_private_thread, 
173                                     GINT_TO_POINTER (i),
174                                     0, TRUE, TRUE, 
175                                     G_THREAD_PRIORITY_NORMAL, NULL);      
176     }
177
178   /* Busy wait is not nice but that's just a test */
179   while (test_g_static_private_ready != THREADS)
180     g_usleep (G_USEC_PER_SEC / 5);
181
182   /* Reuse the static private */
183   g_static_private_free (&test_g_static_private_private2);
184   g_static_private_init (&test_g_static_private_private2);
185   
186   test_g_static_private_ready = 0;
187
188   for (i = 0; i < THREADS; i++)
189     {
190       g_thread_join (threads[i]);
191     }
192   g_assert (test_g_static_private_counter == 0); 
193 }
194
195 /* GStaticRWLock */
196
197 /* -1 = writing; >0 = # of readers */
198 static gint test_g_static_rw_lock_state = 0; 
199 G_LOCK_DEFINE (test_g_static_rw_lock_state);
200
201 static gboolean test_g_static_rw_lock_run = TRUE; 
202 static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
203
204 static void
205 test_g_static_rw_lock_thread (gpointer data)
206 {
207   while (test_g_static_rw_lock_run)
208     {
209       if (g_random_double() > .2) /* I'm a reader */
210         {
211           
212           if (g_random_double() > .2) /* I'll block */
213             g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
214           else /* I'll only try */
215             if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
216               continue;
217           G_LOCK (test_g_static_rw_lock_state);
218           g_assert (test_g_static_rw_lock_state >= 0);
219           test_g_static_rw_lock_state++;
220           G_UNLOCK (test_g_static_rw_lock_state);
221
222           g_usleep (10);
223
224           G_LOCK (test_g_static_rw_lock_state);
225           test_g_static_rw_lock_state--;
226           G_UNLOCK (test_g_static_rw_lock_state);
227
228           g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
229         }
230       else /* I'm a writer */
231         {
232           
233           if (g_random_double() > .2) /* I'll block */ 
234             g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
235           else /* I'll only try */
236             if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
237               continue;
238           G_LOCK (test_g_static_rw_lock_state);
239           g_assert (test_g_static_rw_lock_state == 0);
240           test_g_static_rw_lock_state = -1;
241           G_UNLOCK (test_g_static_rw_lock_state);
242
243           g_usleep (10);
244
245           G_LOCK (test_g_static_rw_lock_state);
246           test_g_static_rw_lock_state = 0;
247           G_UNLOCK (test_g_static_rw_lock_state);
248
249           g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
250         }
251     }
252 }
253
254 static void
255 test_g_static_rw_lock ()
256 {
257   GThread *threads[THREADS];
258   guint i;
259   for (i = 0; i < THREADS; i++)
260     {
261       threads[i] = g_thread_create (test_g_static_rw_lock_thread, 
262                                     0, 0, TRUE, TRUE, 
263                                     G_THREAD_PRIORITY_NORMAL, NULL);      
264     }
265   g_usleep (G_USEC_PER_SEC);
266   test_g_static_rw_lock_run = FALSE;
267   for (i = 0; i < THREADS; i++)
268     {
269       g_thread_join (threads[i]);
270     }
271   g_assert (test_g_static_rw_lock_state == 0);
272 }
273
274 /* run all the tests */
275 void
276 run_all_tests()
277 {
278   test_g_mutex ();
279   test_g_static_rec_mutex ();
280   test_g_static_private ();
281   test_g_static_rw_lock ();
282 }
283
284 int 
285 main (int   argc,
286       char *argv[])
287 {
288   /* Only run the test, if threads are enabled and a default thread
289      implementation is available */
290 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
291   g_thread_init (NULL);
292   run_all_tests ();
293
294   /* Now we rerun all tests, but this time we fool the system into
295    * thinking, that the available thread system is not native, but
296    * userprovided. */
297
298   g_thread_use_default_impl = FALSE;
299   run_all_tests ();
300   
301 #endif
302   return 0;
303 }