1 /* Unit tests for GMutex
2 * Copyright (C) 2011 Red Hat, Inc
3 * Author: Matthias Clasen
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
25 /* We are testing some deprecated APIs here */
26 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
27 #define GLIB_DISABLE_DEPRECATION_WARNINGS
39 g_mutex_init (&mutex);
40 g_mutex_lock (&mutex);
41 g_mutex_unlock (&mutex);
42 g_mutex_lock (&mutex);
43 g_mutex_unlock (&mutex);
44 g_mutex_clear (&mutex);
52 g_mutex_lock (&mutex);
53 g_mutex_unlock (&mutex);
54 g_mutex_lock (&mutex);
55 g_mutex_unlock (&mutex);
63 mutex = g_mutex_new ();
65 g_mutex_unlock (mutex);
67 g_mutex_unlock (mutex);
77 ret = g_mutex_trylock (&mutex);
80 /* no guarantees that mutex is recursive, so could return 0 or 1 */
81 if (g_mutex_trylock (&mutex))
82 g_mutex_unlock (&mutex);
84 g_mutex_unlock (&mutex);
88 #define ITERATIONS 10000
92 GThread *owners[LOCKS];
100 self = g_thread_self ();
102 if (!g_mutex_trylock (&locks[nr]))
104 if (g_test_verbose ())
105 g_printerr ("thread %p going to block on lock %d\n", self, nr);
107 g_mutex_lock (&locks[nr]);
110 g_assert (owners[nr] == NULL); /* hopefully nobody else is here */
113 /* let some other threads try to ruin our day */
118 g_assert (owners[nr] == self); /* hopefully this is still us... */
119 owners[nr] = NULL; /* make way for the next guy */
121 g_mutex_unlock (&locks[nr]);
125 thread_func (gpointer data)
130 rand = g_rand_new ();
132 for (i = 0; i < ITERATIONS; i++)
133 acquire (g_rand_int_range (rand, 0, LOCKS));
144 GThread *threads[THREADS];
146 for (i = 0; i < LOCKS; i++)
147 g_mutex_init (&locks[i]);
149 for (i = 0; i < THREADS; i++)
150 threads[i] = g_thread_create (thread_func, NULL, TRUE, NULL);
152 for (i = 0; i < THREADS; i++)
153 g_thread_join (threads[i]);
155 for (i = 0; i < LOCKS; i++)
156 g_mutex_clear (&locks[i]);
158 for (i = 0; i < LOCKS; i++)
159 g_assert (owners[i] == NULL);
163 test_mutex_errno_func (gpointer data)
167 g_test_summary ("Validates that errno is not touched upon return");
168 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3034");
170 for (unsigned int i = 0; i < 1000; i++)
174 g_assert_cmpint (errno, ==, 0);
180 g_assert_cmpint (errno, ==, 0);
183 if (g_mutex_trylock (m))
185 g_assert_cmpint (errno, ==, 0);
191 g_assert_cmpint (errno, ==, 0);
199 test_mutex_errno (void)
202 GThread *threads[THREADS];
207 for (i = 0; i < G_N_ELEMENTS (threads); i++)
209 threads[i] = g_thread_new ("test_mutex_errno",
210 test_mutex_errno_func, &m);
213 for (i = 0; i < G_N_ELEMENTS (threads); i++)
215 g_thread_join (threads[i]);
219 static gint count_to = 0;
222 do_addition (gint *value)
227 /* test performance of "good" cases (ie: short critical sections) */
228 g_mutex_lock (&lock);
229 if ((more = *value != count_to))
232 g_mutex_unlock (&lock);
238 addition_thread (gpointer value)
240 while (do_addition (value));
246 test_mutex_perf (gconstpointer data)
248 const guint n_threads = GPOINTER_TO_UINT (data);
249 GThread *threads[THREADS];
255 count_to = g_test_perf () ? 100000000 : n_threads + 1;
257 g_assert (n_threads <= G_N_ELEMENTS (threads));
259 for (i = 0; n_threads > 0 && i < n_threads - 1; i++)
260 threads[i] = g_thread_create (addition_thread, &x, TRUE, NULL);
262 /* avoid measuring thread setup/teardown time */
263 start_time = g_get_monotonic_time ();
264 g_atomic_int_set (&x, 0);
265 addition_thread (&x);
266 g_assert_cmpint (g_atomic_int_get (&x), ==, count_to);
267 rate = g_get_monotonic_time () - start_time;
270 for (i = 0; n_threads > 0 && i < n_threads - 1; i++)
271 g_thread_join (threads[i]);
273 g_test_maximized_result (rate, "%f mips", rate);
277 main (int argc, char *argv[])
279 g_test_init (&argc, &argv, NULL);
281 g_test_add_func ("/thread/mutex1", test_mutex1);
282 g_test_add_func ("/thread/mutex2", test_mutex2);
283 g_test_add_func ("/thread/mutex3", test_mutex3);
284 g_test_add_func ("/thread/mutex4", test_mutex4);
285 g_test_add_func ("/thread/mutex5", test_mutex5);
286 g_test_add_func ("/thread/mutex/errno", test_mutex_errno);
291 g_test_add_data_func ("/thread/mutex/perf/uncontended", GUINT_TO_POINTER (0), test_mutex_perf);
293 for (i = 1; i <= 10; i++)
296 sprintf (name, "/thread/mutex/perf/contended/%u", i);
297 g_test_add_data_func (name, GUINT_TO_POINTER (i), test_mutex_perf);
301 return g_test_run ();