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, GINT_TO_POINTER (42),
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);
42 static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
43 static guint test_g_static_rec_mutex_int = 0;
46 test_g_static_rec_mutex_thread (gpointer data)
48 g_assert (GPOINTER_TO_INT (data) == 42);
49 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)
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);
57 g_thread_exit (GINT_TO_POINTER (43));
59 g_assert_not_reached ();
64 test_g_static_rec_mutex (void)
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);
83 g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43);
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;
97 test_g_static_private_constructor (void)
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);
106 test_g_static_private_destructor (gpointer data)
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);
116 test_g_static_private_thread (gpointer data)
118 guint number = GPOINTER_TO_INT (data);
120 guint *private1, *private2;
121 for (i = 0; i < 10; i++)
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)
127 private1 = test_g_static_private_constructor ();
128 g_static_private_set (&test_g_static_private_private1, private1,
129 test_g_static_private_destructor);
132 private2 = g_static_private_get (&test_g_static_private_private2);
133 if (!private2 || number % 13 > 5)
135 private2 = test_g_static_private_constructor ();
136 g_static_private_set (&test_g_static_private_private2, private2,
137 test_g_static_private_destructor);
139 *private2 = number * 2;
140 g_usleep (G_USEC_PER_SEC / 5);
141 g_assert (number == *private1);
142 g_assert (number * 2 == *private2);
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);
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);
152 for (i = 0; i < 10; i++)
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)
158 private2 = test_g_static_private_constructor ();
159 g_static_private_set (&test_g_static_private_private2, private2,
160 test_g_static_private_destructor);
162 *private2 = number * 2;
163 g_usleep (G_USEC_PER_SEC / 5);
164 g_assert (number * 2 == *private2);
167 return GINT_TO_POINTER (GPOINTER_TO_INT (data) * 3);
171 test_g_static_private (void)
173 GThread *threads[THREADS];
176 test_g_static_private_ready = 0;
178 for (i = 0; i < THREADS; i++)
180 threads[i] = g_thread_create (test_g_static_private_thread,
181 GINT_TO_POINTER (i), TRUE, NULL);
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);
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);
192 test_g_static_private_ready = 0;
194 for (i = 0; i < THREADS; i++)
195 g_assert (GPOINTER_TO_INT (g_thread_join (threads[i])) == i * 3);
197 g_assert (test_g_static_private_counter == 0);
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);
206 static gboolean test_g_static_rw_lock_run = TRUE;
207 static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
210 test_g_static_rw_lock_thread (gpointer data)
212 while (test_g_static_rw_lock_run)
214 if (g_random_double() > .2) /* I'm a reader */
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))
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);
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);
233 g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
235 else /* I'm a writer */
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))
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);
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);
254 g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
261 test_g_static_rw_lock ()
263 GThread *threads[THREADS];
265 for (i = 0; i < THREADS; i++)
267 threads[i] = g_thread_create (test_g_static_rw_lock_thread,
270 g_usleep (G_USEC_PER_SEC);
271 test_g_static_rw_lock_run = FALSE;
272 for (i = 0; i < THREADS; i++)
274 g_thread_join (threads[i]);
276 g_assert (test_g_static_rw_lock_state == 0);
279 /* run all the tests */
284 test_g_static_rec_mutex ();
285 test_g_static_private ();
286 test_g_static_rw_lock ();
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);
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
303 g_thread_use_default_impl = FALSE;