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);
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,
30 0, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL);
31 g_usleep (G_MICROSEC);
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);
41 static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
42 static guint test_g_static_rec_mutex_int = 0;
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)
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);
58 test_g_static_rec_mutex (void)
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,
65 0, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL);
66 g_usleep (G_MICROSEC);
67 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
68 g_usleep (G_MICROSEC);
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_MICROSEC);
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);
84 static GStaticPrivate test_g_static_private_private = G_STATIC_PRIVATE_INIT;
85 static GStaticMutex test_g_static_private_mutex = G_STATIC_MUTEX_INIT;
86 static guint test_g_static_private_counter = 0;
89 test_g_static_private_constructor (void)
91 g_static_mutex_lock (&test_g_static_private_mutex);
92 test_g_static_private_counter++;
93 g_static_mutex_unlock (&test_g_static_private_mutex);
94 return g_new (guint,1);
98 test_g_static_private_destructor (gpointer data)
100 g_static_mutex_lock (&test_g_static_private_mutex);
101 test_g_static_private_counter--;
102 g_static_mutex_unlock (&test_g_static_private_mutex);
108 test_g_static_private_thread (gpointer data)
110 guint number = GPOINTER_TO_INT (data);
113 for (i = 0; i < 10; i++)
115 number = number * 11 + 1; /* A very simple and bad RNG ;-) */
116 private = g_static_private_get (&test_g_static_private_private);
117 if (!private || number % 7 > 3)
119 private = test_g_static_private_constructor ();
120 g_static_private_set (&test_g_static_private_private, private,
121 test_g_static_private_destructor);
124 g_usleep (G_MICROSEC / 5);
125 g_assert (number == *private);
130 test_g_static_private (void)
132 GThread *threads[THREADS];
134 for (i = 0; i < THREADS; i++)
136 threads[i] = g_thread_create (test_g_static_private_thread,
139 G_THREAD_PRIORITY_NORMAL);
141 for (i = 0; i < THREADS; i++)
143 g_thread_join (threads[i]);
145 g_assert (test_g_static_private_counter == 0);
150 /* -1 = writing; >0 = # of readers */
151 static gint test_g_static_rw_lock_state = 0;
152 G_LOCK_DEFINE (test_g_static_rw_lock_state);
154 static gboolean test_g_static_rw_lock_run = TRUE;
155 static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
158 test_g_static_rw_lock_thread (gpointer data)
160 while (test_g_static_rw_lock_run)
162 if (g_random_double() > .2) /* I'm a reader */
165 if (g_random_double() > .2) /* I'll block */
166 g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
167 else /* I'll only try */
168 if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
170 G_LOCK (test_g_static_rw_lock_state);
171 g_assert (test_g_static_rw_lock_state >= 0);
172 test_g_static_rw_lock_state++;
173 G_UNLOCK (test_g_static_rw_lock_state);
177 G_LOCK (test_g_static_rw_lock_state);
178 test_g_static_rw_lock_state--;
179 G_UNLOCK (test_g_static_rw_lock_state);
181 g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
183 else /* I'm a writer */
186 if (g_random_double() > .2) /* I'll block */
187 g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
188 else /* I'll only try */
189 if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
191 G_LOCK (test_g_static_rw_lock_state);
192 g_assert (test_g_static_rw_lock_state == 0);
193 test_g_static_rw_lock_state = -1;
194 G_UNLOCK (test_g_static_rw_lock_state);
198 G_LOCK (test_g_static_rw_lock_state);
199 test_g_static_rw_lock_state = 0;
200 G_UNLOCK (test_g_static_rw_lock_state);
202 g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
208 test_g_static_rw_lock ()
210 GThread *threads[THREADS];
212 for (i = 0; i < THREADS; i++)
214 threads[i] = g_thread_create (test_g_static_rw_lock_thread,
216 G_THREAD_PRIORITY_NORMAL);
218 g_usleep (G_MICROSEC);
219 test_g_static_rw_lock_run = FALSE;
220 for (i = 0; i < THREADS; i++)
222 g_thread_join (threads[i]);
224 g_assert (test_g_static_rw_lock_state == 0);
227 /* run all the tests */
232 test_g_static_rec_mutex ();
233 test_g_static_private ();
234 test_g_static_rw_lock ();
241 /* Only run the test, if threads are enabled and a default thread
242 implementation is available */
243 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
244 g_thread_init (NULL);
247 /* Now we rerun all tests, but this time we fool the system into
248 * thinking, that the available thread system is not native, but
251 g_thread_use_default_impl = FALSE;