5 static GMutex* test_g_mutex_mutex = NULL;
6 static guint test_g_mutex_int = 0;
9 test_g_mutex_thread (gpointer data)
11 g_assert (GPOINTER_TO_INT (data) == 42);
12 g_assert (g_mutex_trylock (test_g_mutex_mutex) == FALSE);
13 g_mutex_lock (test_g_mutex_mutex);
14 g_assert (test_g_mutex_int == 42);
15 g_mutex_unlock (test_g_mutex_mutex);
22 test_g_mutex_mutex = g_mutex_new ();
24 g_assert (g_mutex_trylock (test_g_mutex_mutex));
25 thread = g_thread_create (test_g_mutex_thread,
27 0, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL);
28 g_usleep (G_MICROSEC);
29 test_g_mutex_int = 42;
30 g_mutex_unlock (test_g_mutex_mutex);
31 g_thread_join (thread);
32 g_mutex_free (test_g_mutex_mutex);
37 static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
38 static guint test_g_static_rec_mutex_int = 0;
41 test_g_static_rec_mutex_thread (gpointer data)
43 g_assert (GPOINTER_TO_INT (data) == 42);
44 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)
46 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
47 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
48 g_assert (test_g_static_rec_mutex_int == 42);
49 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
50 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
54 test_g_static_rec_mutex (void)
58 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
59 thread = g_thread_create (test_g_static_rec_mutex_thread,
61 0, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL);
62 g_usleep (G_MICROSEC);
63 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
64 g_usleep (G_MICROSEC);
65 test_g_static_rec_mutex_int = 41;
66 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
67 test_g_static_rec_mutex_int = 42;
68 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
69 g_usleep (G_MICROSEC);
70 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
71 test_g_static_rec_mutex_int = 0;
72 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
73 g_thread_join (thread);
80 static GStaticPrivate test_g_static_private_private = G_STATIC_PRIVATE_INIT;
81 static GStaticMutex test_g_static_private_mutex = G_STATIC_MUTEX_INIT;
82 static guint test_g_static_private_counter = 0;
85 test_g_static_private_constructor (void)
87 g_static_mutex_lock (&test_g_static_private_mutex);
88 test_g_static_private_counter++;
89 g_static_mutex_unlock (&test_g_static_private_mutex);
90 return g_new (guint,1);
94 test_g_static_private_destructor (gpointer data)
96 g_static_mutex_lock (&test_g_static_private_mutex);
97 test_g_static_private_counter--;
98 g_static_mutex_unlock (&test_g_static_private_mutex);
104 test_g_static_private_thread (gpointer data)
106 guint number = GPOINTER_TO_INT (data);
109 for (i = 0; i < 10; i++)
111 number = number * 11 + 1; /* A very simple and bad RNG ;-) */
112 private = g_static_private_get (&test_g_static_private_private);
113 if (!private || number % 7 > 3)
115 private = test_g_static_private_constructor ();
116 g_static_private_set (&test_g_static_private_private, private,
117 test_g_static_private_destructor);
120 g_usleep (G_MICROSEC / 5);
121 g_assert (number == *private);
126 test_g_static_private (void)
128 GThread *threads[THREADS];
130 for (i = 0; i < THREADS; i++)
132 threads[i] = g_thread_create (test_g_static_private_thread,
135 G_THREAD_PRIORITY_NORMAL);
137 for (i = 0; i < THREADS; i++)
139 g_thread_join (threads[i]);
141 g_assert (test_g_static_private_counter == 0);
146 /* -1 = writing; >0 = # of readers */
147 static gint test_g_static_rw_lock_state = 0;
148 G_LOCK_DEFINE (test_g_static_rw_lock_state);
150 static gboolean test_g_static_rw_lock_run = TRUE;
151 static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
154 test_g_static_rw_lock_thread (gpointer data)
156 while (test_g_static_rw_lock_run)
158 if (g_random_double() > .2) /* I'm a reader */
161 if (g_random_double() > .2) /* I'll block */
162 g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
163 else /* I'll only try */
164 if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
166 G_LOCK (test_g_static_rw_lock_state);
167 g_assert (test_g_static_rw_lock_state >= 0);
168 test_g_static_rw_lock_state++;
169 G_UNLOCK (test_g_static_rw_lock_state);
173 G_LOCK (test_g_static_rw_lock_state);
174 test_g_static_rw_lock_state--;
175 G_UNLOCK (test_g_static_rw_lock_state);
177 g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
179 else /* I'm a writer */
182 if (g_random_double() > .2) /* I'll block */
183 g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
184 else /* I'll only try */
185 if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
187 G_LOCK (test_g_static_rw_lock_state);
188 g_assert (test_g_static_rw_lock_state == 0);
189 test_g_static_rw_lock_state = -1;
190 G_UNLOCK (test_g_static_rw_lock_state);
194 G_LOCK (test_g_static_rw_lock_state);
195 test_g_static_rw_lock_state = 0;
196 G_UNLOCK (test_g_static_rw_lock_state);
198 g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
204 test_g_static_rw_lock ()
206 GThread *threads[THREADS];
208 for (i = 0; i < THREADS; i++)
210 threads[i] = g_thread_create (test_g_static_rw_lock_thread,
212 G_THREAD_PRIORITY_NORMAL);
214 g_usleep (G_MICROSEC);
215 test_g_static_rw_lock_run = FALSE;
216 for (i = 0; i < THREADS; i++)
218 g_thread_join (threads[i]);
220 g_assert (test_g_static_rw_lock_state == 0);
223 /* run all the tests */
228 test_g_static_rec_mutex ();
229 test_g_static_private ();
230 test_g_static_rw_lock ();
237 /* Only run the test, if threads are enabled and a default thread
238 implementation is available */
239 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
240 g_thread_init (NULL);
243 /* Now we rerun all tests, but this time we fool the system into
244 * thinking, that the available thread system is not native, but
247 g_thread_use_default_impl = FALSE;