Added functions g_static_rec_mutex_init, g_static_rec_mutex_free,
[glib.git] / tests / thread-test.c
blob3e689b53ec733681858043d7b6bf6189a39e5c46
1 #include <glib.h>
3 /* GMutex */
5 static GMutex* test_g_mutex_mutex = NULL;
6 static guint test_g_mutex_int = 0;
7 G_LOCK_DEFINE_STATIC (test_g_mutex);
9 static void
10 test_g_mutex_thread (gpointer data)
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);
20 static void
21 test_g_mutex (void)
23 GThread *thread;
24 test_g_mutex_mutex = g_mutex_new ();
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);
39 /* GStaticRecMutex */
41 static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
42 static guint test_g_static_rec_mutex_int = 0;
44 static void
45 test_g_static_rec_mutex_thread (gpointer data)
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);
57 static void
58 test_g_static_rec_mutex (void)
60 GThread *thread;
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);
80 /* GStaticPrivate */
82 #define THREADS 10
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;
90 static gpointer
91 test_g_static_private_constructor (void)
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);
99 static void
100 test_g_static_private_destructor (gpointer data)
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);
109 static void
110 test_g_static_private_thread (gpointer data)
112 guint number = GPOINTER_TO_INT (data);
113 guint i;
114 guint *private1, *private2;
115 for (i = 0; i < 10; i++)
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)
121 private1 = test_g_static_private_constructor ();
122 g_static_private_set (&test_g_static_private_private1, private1,
123 test_g_static_private_destructor);
125 *private1 = number;
126 private2 = g_static_private_get (&test_g_static_private_private2);
127 if (!private2 || number % 13 > 5)
129 private2 = test_g_static_private_constructor ();
130 g_static_private_set (&test_g_static_private_private2, private2,
131 test_g_static_private_destructor);
133 *private2 = number * 2;
134 g_usleep (G_USEC_PER_SEC / 5);
135 g_assert (number == *private1);
136 g_assert (number * 2 == *private2);
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);
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);
146 for (i = 0; i < 10; i++)
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)
152 private2 = test_g_static_private_constructor ();
153 g_static_private_set (&test_g_static_private_private2, private2,
154 test_g_static_private_destructor);
156 *private2 = number * 2;
157 g_usleep (G_USEC_PER_SEC / 5);
158 g_assert (number * 2 == *private2);
162 static void
163 test_g_static_private (void)
165 GThread *threads[THREADS];
166 guint i;
168 test_g_static_private_ready = 0;
170 for (i = 0; i < THREADS; i++)
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);
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);
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);
186 test_g_static_private_ready = 0;
188 for (i = 0; i < THREADS; i++)
190 g_thread_join (threads[i]);
192 g_assert (test_g_static_private_counter == 0);
195 /* GStaticRWLock */
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);
201 static gboolean test_g_static_rw_lock_run = TRUE;
202 static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
204 static void
205 test_g_static_rw_lock_thread (gpointer data)
207 while (test_g_static_rw_lock_run)
209 if (g_random_double() > .2) /* I'm a reader */
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);
222 g_usleep (10);
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);
228 g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
230 else /* I'm a writer */
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);
243 g_usleep (10);
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);
249 g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
254 static void
255 test_g_static_rw_lock ()
257 GThread *threads[THREADS];
258 guint i;
259 for (i = 0; i < THREADS; i++)
261 threads[i] = g_thread_create (test_g_static_rw_lock_thread,
262 0, 0, TRUE, TRUE,
263 G_THREAD_PRIORITY_NORMAL, NULL);
265 g_usleep (G_USEC_PER_SEC);
266 test_g_static_rw_lock_run = FALSE;
267 for (i = 0; i < THREADS; i++)
269 g_thread_join (threads[i]);
271 g_assert (test_g_static_rw_lock_state == 0);
274 /* run all the tests */
275 void
276 run_all_tests()
278 test_g_mutex ();
279 test_g_static_rec_mutex ();
280 test_g_static_private ();
281 test_g_static_rw_lock ();
284 int
285 main (int argc,
286 char *argv[])
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 ();
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. */
298 g_thread_use_default_impl = FALSE;
299 run_all_tests ();
301 #endif
302 return 0;