5 static GMutex* test_g_mutex_mutex = NULL;
6 static guint test_g_mutex_int = 0;
7 G_LOCK_DEFINE_STATIC (test_g_mutex);
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);
19 return GINT_TO_POINTER (41);
26 test_g_mutex_mutex = g_mutex_new ();
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,
32 0, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL, NULL);
33 g_usleep (G_USEC_PER_SEC);
34 test_g_mutex_int = 42;
35 G_UNLOCK (test_g_mutex);
36 g_mutex_unlock (test_g_mutex_mutex);
37 g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 41);
38 g_mutex_free (test_g_mutex_mutex);
43 static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
44 static guint test_g_static_rec_mutex_int = 0;
47 test_g_static_rec_mutex_thread (gpointer data)
49 g_assert (GPOINTER_TO_INT (data) == 42);
50 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)
52 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
53 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
54 g_assert (test_g_static_rec_mutex_int == 42);
55 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
56 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
58 g_thread_exit (GINT_TO_POINTER (43));
60 g_assert_not_reached ();
65 test_g_static_rec_mutex (void)
69 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
70 thread = g_thread_create (test_g_static_rec_mutex_thread,
72 0, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL, NULL);
73 g_usleep (G_USEC_PER_SEC);
74 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
75 g_usleep (G_USEC_PER_SEC);
76 test_g_static_rec_mutex_int = 41;
77 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
78 test_g_static_rec_mutex_int = 42;
79 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
80 g_usleep (G_USEC_PER_SEC);
81 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
82 test_g_static_rec_mutex_int = 0;
83 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
85 g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43);
92 static GStaticPrivate test_g_static_private_private1 = G_STATIC_PRIVATE_INIT;
93 static GStaticPrivate test_g_static_private_private2 = G_STATIC_PRIVATE_INIT;
94 static GStaticMutex test_g_static_private_mutex = G_STATIC_MUTEX_INIT;
95 static guint test_g_static_private_counter = 0;
96 static guint test_g_static_private_ready = 0;
99 test_g_static_private_constructor (void)
101 g_static_mutex_lock (&test_g_static_private_mutex);
102 test_g_static_private_counter++;
103 g_static_mutex_unlock (&test_g_static_private_mutex);
104 return g_new (guint,1);
108 test_g_static_private_destructor (gpointer data)
110 g_static_mutex_lock (&test_g_static_private_mutex);
111 test_g_static_private_counter--;
112 g_static_mutex_unlock (&test_g_static_private_mutex);
118 test_g_static_private_thread (gpointer data)
120 guint number = GPOINTER_TO_INT (data);
122 guint *private1, *private2;
123 for (i = 0; i < 10; i++)
125 number = number * 11 + 1; /* A very simple and bad RNG ;-) */
126 private1 = g_static_private_get (&test_g_static_private_private1);
127 if (!private1 || number % 7 > 3)
129 private1 = test_g_static_private_constructor ();
130 g_static_private_set (&test_g_static_private_private1, private1,
131 test_g_static_private_destructor);
134 private2 = g_static_private_get (&test_g_static_private_private2);
135 if (!private2 || number % 13 > 5)
137 private2 = test_g_static_private_constructor ();
138 g_static_private_set (&test_g_static_private_private2, private2,
139 test_g_static_private_destructor);
141 *private2 = number * 2;
142 g_usleep (G_USEC_PER_SEC / 5);
143 g_assert (number == *private1);
144 g_assert (number * 2 == *private2);
146 g_static_mutex_lock (&test_g_static_private_mutex);
147 test_g_static_private_ready++;
148 g_static_mutex_unlock (&test_g_static_private_mutex);
150 /* Busy wait is not nice but that's just a test */
151 while (test_g_static_private_ready != 0)
152 g_usleep (G_USEC_PER_SEC / 5);
154 for (i = 0; i < 10; i++)
156 private2 = g_static_private_get (&test_g_static_private_private2);
157 number = number * 11 + 1; /* A very simple and bad RNG ;-) */
158 if (!private2 || number % 13 > 5)
160 private2 = test_g_static_private_constructor ();
161 g_static_private_set (&test_g_static_private_private2, private2,
162 test_g_static_private_destructor);
164 *private2 = number * 2;
165 g_usleep (G_USEC_PER_SEC / 5);
166 g_assert (number * 2 == *private2);
169 return GINT_TO_POINTER (GPOINTER_TO_INT (data) * 3);
173 test_g_static_private (void)
175 GThread *threads[THREADS];
178 test_g_static_private_ready = 0;
180 for (i = 0; i < THREADS; i++)
182 threads[i] = g_thread_create (test_g_static_private_thread,
185 G_THREAD_PRIORITY_NORMAL, NULL);
188 /* Busy wait is not nice but that's just a test */
189 while (test_g_static_private_ready != THREADS)
190 g_usleep (G_USEC_PER_SEC / 5);
192 /* Reuse the static private */
193 g_static_private_free (&test_g_static_private_private2);
194 g_static_private_init (&test_g_static_private_private2);
196 test_g_static_private_ready = 0;
198 for (i = 0; i < THREADS; i++)
199 g_assert (GPOINTER_TO_INT (g_thread_join (threads[i])) == i * 3);
201 g_assert (test_g_static_private_counter == 0);
206 /* -1 = writing; >0 = # of readers */
207 static gint test_g_static_rw_lock_state = 0;
208 G_LOCK_DEFINE (test_g_static_rw_lock_state);
210 static gboolean test_g_static_rw_lock_run = TRUE;
211 static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
214 test_g_static_rw_lock_thread (gpointer data)
216 while (test_g_static_rw_lock_run)
218 if (g_random_double() > .2) /* I'm a reader */
221 if (g_random_double() > .2) /* I'll block */
222 g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
223 else /* I'll only try */
224 if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
226 G_LOCK (test_g_static_rw_lock_state);
227 g_assert (test_g_static_rw_lock_state >= 0);
228 test_g_static_rw_lock_state++;
229 G_UNLOCK (test_g_static_rw_lock_state);
233 G_LOCK (test_g_static_rw_lock_state);
234 test_g_static_rw_lock_state--;
235 G_UNLOCK (test_g_static_rw_lock_state);
237 g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
239 else /* I'm a writer */
242 if (g_random_double() > .2) /* I'll block */
243 g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
244 else /* I'll only try */
245 if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
247 G_LOCK (test_g_static_rw_lock_state);
248 g_assert (test_g_static_rw_lock_state == 0);
249 test_g_static_rw_lock_state = -1;
250 G_UNLOCK (test_g_static_rw_lock_state);
254 G_LOCK (test_g_static_rw_lock_state);
255 test_g_static_rw_lock_state = 0;
256 G_UNLOCK (test_g_static_rw_lock_state);
258 g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
265 test_g_static_rw_lock ()
267 GThread *threads[THREADS];
269 for (i = 0; i < THREADS; i++)
271 threads[i] = g_thread_create (test_g_static_rw_lock_thread,
273 G_THREAD_PRIORITY_NORMAL, NULL);
275 g_usleep (G_USEC_PER_SEC);
276 test_g_static_rw_lock_run = FALSE;
277 for (i = 0; i < THREADS; i++)
279 g_thread_join (threads[i]);
281 g_assert (test_g_static_rw_lock_state == 0);
284 /* run all the tests */
289 test_g_static_rec_mutex ();
290 test_g_static_private ();
291 test_g_static_rw_lock ();
298 /* Only run the test, if threads are enabled and a default thread
299 implementation is available */
300 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
301 g_thread_init (NULL);
304 /* Now we rerun all tests, but this time we fool the system into
305 * thinking, that the available thread system is not native, but
308 g_thread_use_default_impl = FALSE;