5 static GMutex* test_g_mutex_mutex = NULL;
6 static guint test_g_mutex_int = 0;
7 static gboolean test_g_mutex_thread_ready;
8 G_LOCK_DEFINE_STATIC (test_g_mutex);
11 test_g_mutex_thread (gpointer data)
13 g_assert (GPOINTER_TO_INT (data) == 42);
14 g_assert (g_mutex_trylock (test_g_mutex_mutex) == FALSE);
15 g_assert (G_TRYLOCK (test_g_mutex) == FALSE);
16 test_g_mutex_thread_ready = TRUE;
17 g_mutex_lock (test_g_mutex_mutex);
18 g_assert (test_g_mutex_int == 42);
19 g_mutex_unlock (test_g_mutex_mutex);
21 return GINT_TO_POINTER (41);
28 test_g_mutex_mutex = g_mutex_new ();
30 g_assert (g_mutex_trylock (test_g_mutex_mutex));
31 g_assert (G_TRYLOCK (test_g_mutex));
32 test_g_mutex_thread_ready = FALSE;
33 thread = g_thread_create (test_g_mutex_thread, GINT_TO_POINTER (42),
35 /* This busy wait is only for testing purposes and not an example of
37 while (!test_g_mutex_thread_ready)
38 g_usleep (G_USEC_PER_SEC / 5);
39 test_g_mutex_int = 42;
40 G_UNLOCK (test_g_mutex);
41 g_mutex_unlock (test_g_mutex_mutex);
42 g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 41);
43 g_mutex_free (test_g_mutex_mutex);
48 static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
49 static guint test_g_static_rec_mutex_int = 0;
50 static gboolean test_g_static_rec_mutex_thread_ready;
53 test_g_static_rec_mutex_thread (gpointer data)
55 g_assert (GPOINTER_TO_INT (data) == 42);
56 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)
58 test_g_static_rec_mutex_thread_ready = TRUE;
59 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
60 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
61 g_assert (test_g_static_rec_mutex_int == 42);
62 test_g_static_rec_mutex_thread_ready = FALSE;
63 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
64 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
66 g_thread_exit (GINT_TO_POINTER (43));
68 g_assert_not_reached ();
73 test_g_static_rec_mutex (void)
77 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
78 test_g_static_rec_mutex_thread_ready = FALSE;
79 thread = g_thread_create (test_g_static_rec_mutex_thread,
80 GINT_TO_POINTER (42), TRUE, NULL);
81 /* This busy wait is only for testing purposes and not an example of
83 while (!test_g_static_rec_mutex_thread_ready)
84 g_usleep (G_USEC_PER_SEC / 5);
86 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
87 test_g_static_rec_mutex_int = 41;
88 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
89 test_g_static_rec_mutex_int = 42;
90 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
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_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
98 test_g_static_rec_mutex_int = 0;
99 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
101 g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43);
108 static GStaticPrivate test_g_static_private_private1 = G_STATIC_PRIVATE_INIT;
109 static GStaticPrivate test_g_static_private_private2 = G_STATIC_PRIVATE_INIT;
110 static GStaticMutex test_g_static_private_mutex = G_STATIC_MUTEX_INIT;
111 static guint test_g_static_private_counter = 0;
112 static guint test_g_static_private_ready = 0;
115 test_g_static_private_constructor (void)
117 g_static_mutex_lock (&test_g_static_private_mutex);
118 test_g_static_private_counter++;
119 g_static_mutex_unlock (&test_g_static_private_mutex);
120 return g_new (guint,1);
124 test_g_static_private_destructor (gpointer data)
126 g_static_mutex_lock (&test_g_static_private_mutex);
127 test_g_static_private_counter--;
128 g_static_mutex_unlock (&test_g_static_private_mutex);
134 test_g_static_private_thread (gpointer data)
136 guint number = GPOINTER_TO_INT (data);
138 guint *private1, *private2;
139 for (i = 0; i < 10; i++)
141 number = number * 11 + 1; /* A very simple and bad RNG ;-) */
142 private1 = g_static_private_get (&test_g_static_private_private1);
143 if (!private1 || number % 7 > 3)
145 private1 = test_g_static_private_constructor ();
146 g_static_private_set (&test_g_static_private_private1, private1,
147 test_g_static_private_destructor);
150 private2 = g_static_private_get (&test_g_static_private_private2);
151 if (!private2 || number % 13 > 5)
153 private2 = test_g_static_private_constructor ();
154 g_static_private_set (&test_g_static_private_private2, private2,
155 test_g_static_private_destructor);
157 *private2 = number * 2;
158 g_usleep (G_USEC_PER_SEC / 5);
159 g_assert (number == *private1);
160 g_assert (number * 2 == *private2);
162 g_static_mutex_lock (&test_g_static_private_mutex);
163 test_g_static_private_ready++;
164 g_static_mutex_unlock (&test_g_static_private_mutex);
166 /* Busy wait is not nice but that's just a test */
167 while (test_g_static_private_ready != 0)
168 g_usleep (G_USEC_PER_SEC / 5);
170 for (i = 0; i < 10; i++)
172 private2 = g_static_private_get (&test_g_static_private_private2);
173 number = number * 11 + 1; /* A very simple and bad RNG ;-) */
174 if (!private2 || number % 13 > 5)
176 private2 = test_g_static_private_constructor ();
177 g_static_private_set (&test_g_static_private_private2, private2,
178 test_g_static_private_destructor);
180 *private2 = number * 2;
181 g_usleep (G_USEC_PER_SEC / 5);
182 g_assert (number * 2 == *private2);
185 return GINT_TO_POINTER (GPOINTER_TO_INT (data) * 3);
189 test_g_static_private (void)
191 GThread *threads[THREADS];
194 test_g_static_private_ready = 0;
196 for (i = 0; i < THREADS; i++)
198 threads[i] = g_thread_create (test_g_static_private_thread,
199 GINT_TO_POINTER (i), TRUE, NULL);
202 /* Busy wait is not nice but that's just a test */
203 while (test_g_static_private_ready != THREADS)
204 g_usleep (G_USEC_PER_SEC / 5);
206 /* Reuse the static private */
207 g_static_private_free (&test_g_static_private_private2);
208 g_static_private_init (&test_g_static_private_private2);
210 test_g_static_private_ready = 0;
212 for (i = 0; i < THREADS; i++)
213 g_assert (GPOINTER_TO_INT (g_thread_join (threads[i])) == i * 3);
215 g_assert (test_g_static_private_counter == 0);
220 /* -1 = writing; >0 = # of readers */
221 static gint test_g_static_rw_lock_state = 0;
222 G_LOCK_DEFINE (test_g_static_rw_lock_state);
224 static gboolean test_g_static_rw_lock_run = TRUE;
225 static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
228 test_g_static_rw_lock_thread (gpointer data)
230 while (test_g_static_rw_lock_run)
232 if (g_random_double() > .2) /* I'm a reader */
235 if (g_random_double() > .2) /* I'll block */
236 g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
237 else /* I'll only try */
238 if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
240 G_LOCK (test_g_static_rw_lock_state);
241 g_assert (test_g_static_rw_lock_state >= 0);
242 test_g_static_rw_lock_state++;
243 G_UNLOCK (test_g_static_rw_lock_state);
245 g_usleep (g_random_int_range (20,1000));
247 G_LOCK (test_g_static_rw_lock_state);
248 test_g_static_rw_lock_state--;
249 G_UNLOCK (test_g_static_rw_lock_state);
251 g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
253 else /* I'm a writer */
256 if (g_random_double() > .2) /* I'll block */
257 g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
258 else /* I'll only try */
259 if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
261 G_LOCK (test_g_static_rw_lock_state);
262 g_assert (test_g_static_rw_lock_state == 0);
263 test_g_static_rw_lock_state = -1;
264 G_UNLOCK (test_g_static_rw_lock_state);
266 g_usleep (g_random_int_range (20,1000));
268 G_LOCK (test_g_static_rw_lock_state);
269 test_g_static_rw_lock_state = 0;
270 G_UNLOCK (test_g_static_rw_lock_state);
272 g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
279 test_g_static_rw_lock ()
281 GThread *threads[THREADS];
283 for (i = 0; i < THREADS; i++)
285 threads[i] = g_thread_create (test_g_static_rw_lock_thread,
288 g_usleep (G_USEC_PER_SEC * 5);
289 test_g_static_rw_lock_run = FALSE;
290 for (i = 0; i < THREADS; i++)
292 g_thread_join (threads[i]);
294 g_assert (test_g_static_rw_lock_state == 0);
297 /* run all the tests */
302 test_g_static_rec_mutex ();
303 test_g_static_private ();
304 test_g_static_rw_lock ();
311 /* Only run the test, if threads are enabled and a default thread
312 implementation is available */
313 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
314 g_thread_init (NULL);
317 /* Now we rerun all tests, but this time we fool the system into
318 * thinking, that the available thread system is not native, but
321 g_thread_use_default_impl = FALSE;