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