Update contrib.
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
2 #undef G_DISABLE_ASSERT
9 #include <glib_global.h>
10 #include "mrt2_glib2_test.h"
16 static GMutex* test_g_mutex_mutex = NULL;
17 static guint test_g_mutex_int = 0;
18 static gboolean test_g_mutex_thread_ready;
19 G_LOCK_DEFINE_STATIC (test_g_mutex);
22 test_g_mutex_thread (gpointer data)
24 g_assert (GPOINTER_TO_INT (data) == 42);
25 g_assert (g_mutex_trylock (test_g_mutex_mutex) == FALSE);
26 g_assert (G_TRYLOCK (test_g_mutex) == FALSE);
27 test_g_mutex_thread_ready = TRUE;
28 g_mutex_lock (test_g_mutex_mutex);
29 g_assert (test_g_mutex_int == 42);
30 g_mutex_unlock (test_g_mutex_mutex);
32 return GINT_TO_POINTER (41);
39 test_g_mutex_mutex = g_mutex_new ();
41 g_assert (g_mutex_trylock (test_g_mutex_mutex));
42 g_assert (G_TRYLOCK (test_g_mutex));
43 test_g_mutex_thread_ready = FALSE;
44 thread = g_thread_create (test_g_mutex_thread, GINT_TO_POINTER (42),
46 /* This busy wait is only for testing purposes and not an example of
48 while (!test_g_mutex_thread_ready)
49 g_usleep (G_USEC_PER_SEC / 5);
50 test_g_mutex_int = 42;
51 G_UNLOCK (test_g_mutex);
52 g_mutex_unlock (test_g_mutex_mutex);
53 g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 41);
54 g_mutex_free (test_g_mutex_mutex);
59 static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
60 static guint test_g_static_rec_mutex_int = 0;
61 static gboolean test_g_static_rec_mutex_thread_ready;
64 test_g_static_rec_mutex_thread (gpointer data)
66 g_assert (GPOINTER_TO_INT (data) == 42);
67 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)
69 test_g_static_rec_mutex_thread_ready = TRUE;
70 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
71 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
72 g_assert (test_g_static_rec_mutex_int == 42);
73 test_g_static_rec_mutex_thread_ready = FALSE;
74 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
75 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
77 g_thread_exit (GINT_TO_POINTER (43));
79 g_assert_not_reached ();
84 test_g_static_rec_mutex (void)
88 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
89 test_g_static_rec_mutex_thread_ready = FALSE;
90 thread = g_thread_create (test_g_static_rec_mutex_thread,
91 GINT_TO_POINTER (42), TRUE, NULL);
92 /* This busy wait is only for testing purposes and not an example of
94 while (!test_g_static_rec_mutex_thread_ready)
95 g_usleep (G_USEC_PER_SEC / 5);
97 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
98 test_g_static_rec_mutex_int = 41;
99 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
100 test_g_static_rec_mutex_int = 42;
101 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
103 /* This busy wait is only for testing purposes and not an example of
105 while (test_g_static_rec_mutex_thread_ready)
106 g_usleep (G_USEC_PER_SEC / 5);
108 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
109 test_g_static_rec_mutex_int = 0;
110 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
112 g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43);
119 static GStaticPrivate test_g_static_private_private1 = G_STATIC_PRIVATE_INIT;
120 static GStaticPrivate test_g_static_private_private2 = G_STATIC_PRIVATE_INIT;
121 static GStaticMutex test_g_static_private_mutex = G_STATIC_MUTEX_INIT;
122 static guint test_g_static_private_counter = 0;
123 static guint test_g_static_private_ready = 0;
126 test_g_static_private_constructor (void)
128 g_static_mutex_lock (&test_g_static_private_mutex);
129 test_g_static_private_counter++;
130 g_static_mutex_unlock (&test_g_static_private_mutex);
131 return g_new (guint,1);
135 test_g_static_private_destructor (gpointer data)
137 g_static_mutex_lock (&test_g_static_private_mutex);
138 test_g_static_private_counter--;
139 g_static_mutex_unlock (&test_g_static_private_mutex);
145 test_g_static_private_thread (gpointer data)
147 guint number = GPOINTER_TO_INT (data);
149 guint *private1, *private2;
150 for (i = 0; i < 10; i++)
152 number = number * 11 + 1; /* A very simple and bad RNG ;-) */
153 private1 = g_static_private_get (&test_g_static_private_private1);
154 if (!private1 || number % 7 > 3)
156 private1 = test_g_static_private_constructor ();
157 g_static_private_set (&test_g_static_private_private1, private1,
158 test_g_static_private_destructor);
161 private2 = g_static_private_get (&test_g_static_private_private2);
162 if (!private2 || number % 13 > 5)
164 private2 = test_g_static_private_constructor ();
165 g_static_private_set (&test_g_static_private_private2, private2,
166 test_g_static_private_destructor);
168 *private2 = number * 2;
169 g_usleep (G_USEC_PER_SEC / 5);
170 g_assert (number == *private1);
171 g_assert (number * 2 == *private2);
173 g_static_mutex_lock (&test_g_static_private_mutex);
174 test_g_static_private_ready++;
175 g_static_mutex_unlock (&test_g_static_private_mutex);
177 /* Busy wait is not nice but that's just a test */
178 while (test_g_static_private_ready != 0)
179 g_usleep (G_USEC_PER_SEC / 5);
181 for (i = 0; i < 10; i++)
183 private2 = g_static_private_get (&test_g_static_private_private2);
184 number = number * 11 + 1; /* A very simple and bad RNG ;-) */
185 if (!private2 || number % 13 > 5)
187 private2 = test_g_static_private_constructor ();
188 g_static_private_set (&test_g_static_private_private2, private2,
189 test_g_static_private_destructor);
191 *private2 = number * 2;
192 g_usleep (G_USEC_PER_SEC / 5);
193 g_assert (number * 2 == *private2);
196 return GINT_TO_POINTER (GPOINTER_TO_INT (data) * 3);
200 test_g_static_private (void)
202 GThread *threads[THREADS];
205 test_g_static_private_ready = 0;
207 for (i = 0; i < THREADS; i++)
209 threads[i] = g_thread_create (test_g_static_private_thread,
210 GINT_TO_POINTER (i), TRUE, NULL);
213 /* Busy wait is not nice but that's just a test */
214 while (test_g_static_private_ready != THREADS)
215 g_usleep (G_USEC_PER_SEC / 5);
217 /* Reuse the static private */
218 g_static_private_free (&test_g_static_private_private2);
219 g_static_private_init (&test_g_static_private_private2);
221 test_g_static_private_ready = 0;
223 for (i = 0; i < THREADS; i++)
224 g_assert (GPOINTER_TO_INT (g_thread_join (threads[i])) == i * 3);
226 g_assert (test_g_static_private_counter == 0);
231 /* -1 = writing; >0 = # of readers */
232 static gint test_g_static_rw_lock_state = 0;
233 G_LOCK_DEFINE (test_g_static_rw_lock_state);
235 static gboolean test_g_static_rw_lock_run = TRUE;
236 static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
239 test_g_static_rw_lock_thread (gpointer data)
241 while (test_g_static_rw_lock_run)
243 if (g_random_double() > .2) /* I'm a reader */
246 if (g_random_double() > .2) /* I'll block */
247 g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
248 else /* I'll only try */
249 if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
251 G_LOCK (test_g_static_rw_lock_state);
252 g_assert (test_g_static_rw_lock_state >= 0);
253 test_g_static_rw_lock_state++;
254 G_UNLOCK (test_g_static_rw_lock_state);
256 g_usleep (g_random_int_range (20,1000));
258 G_LOCK (test_g_static_rw_lock_state);
259 test_g_static_rw_lock_state--;
260 G_UNLOCK (test_g_static_rw_lock_state);
262 g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
264 else /* I'm a writer */
267 if (g_random_double() > .2) /* I'll block */
268 g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
269 else /* I'll only try */
270 if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
272 G_LOCK (test_g_static_rw_lock_state);
273 g_assert (test_g_static_rw_lock_state == 0);
274 test_g_static_rw_lock_state = -1;
275 G_UNLOCK (test_g_static_rw_lock_state);
277 g_usleep (g_random_int_range (20,1000));
279 G_LOCK (test_g_static_rw_lock_state);
280 test_g_static_rw_lock_state = 0;
281 G_UNLOCK (test_g_static_rw_lock_state);
283 g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
290 test_g_static_rw_lock ()
292 GThread *threads[THREADS];
294 for (i = 0; i < THREADS; i++)
296 threads[i] = g_thread_create (test_g_static_rw_lock_thread,
299 g_usleep (G_USEC_PER_SEC * 5);
300 test_g_static_rw_lock_run = FALSE;
301 for (i = 0; i < THREADS; i++)
303 g_thread_join (threads[i]);
305 g_assert (test_g_static_rw_lock_state == 0);
308 #define G_ONCE_SIZE 100
309 #define G_ONCE_THREADS 10
311 G_LOCK_DEFINE (test_g_once);
312 static guint test_g_once_guint_array[G_ONCE_SIZE];
313 static GOnce test_g_once_array[G_ONCE_SIZE];
316 test_g_once_init_func(gpointer arg)
319 g_usleep (g_random_int_range (20,1000));
321 g_usleep (g_random_int_range (20,1000));
326 test_g_once_thread (gpointer ignore)
329 G_LOCK (test_g_once);
330 /* Don't start before all threads are created */
331 G_UNLOCK (test_g_once);
332 for (i = 0; i < 1000; i++)
334 guint pos = g_random_int_range (0, G_ONCE_SIZE);
335 gpointer ret = g_once (test_g_once_array + pos, test_g_once_init_func,
336 test_g_once_guint_array + pos);
337 g_assert (ret == test_g_once_guint_array + pos);
340 /* Make sure, that all counters are touched at least once */
341 for (i = 0; i < G_ONCE_SIZE; i++)
343 gpointer ret = g_once (test_g_once_array + i, test_g_once_init_func,
344 test_g_once_guint_array + i);
345 g_assert (ret == test_g_once_guint_array + i);
352 test_g_thread_once (void)
354 static GOnce once_init = G_ONCE_INIT;
355 GThread *threads[G_ONCE_THREADS];
357 for (i = 0; i < G_ONCE_SIZE; i++)
359 test_g_once_array[i] = once_init;
360 test_g_once_guint_array[i] = i;
362 G_LOCK (test_g_once);
363 for (i = 0; i < G_ONCE_THREADS; i++)
365 threads[i] = g_thread_create (test_g_once_thread, GUINT_TO_POINTER(i%2),
368 G_UNLOCK (test_g_once);
369 for (i = 0; i < G_ONCE_THREADS; i++)
371 g_thread_join (threads[i]);
374 for (i = 0; i < G_ONCE_SIZE; i++)
376 g_assert (test_g_once_guint_array[i] == i + 1);
380 /* run all the tests */
385 test_g_static_rec_mutex ();
386 test_g_static_private ();
387 test_g_static_rw_lock ();
388 test_g_thread_once ();
397 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
398 g_set_print_handler(mrtPrintHandler);
402 /* Only run the test, if threads are enabled and a default thread
403 implementation is available */
404 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
405 g_thread_init (NULL);
408 /* Now we rerun all tests, but this time we fool the system into
409 * thinking, that the available thread system is not native, but
412 g_thread_use_default_impl = FALSE;
418 testResultXml("thread-test");
419 #endif /* EMULATOR */