1 /* Unit tests for GRecMutex
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
35 test_rec_mutex1 (void)
39 g_rec_mutex_init (&mutex);
40 g_rec_mutex_lock (&mutex);
41 g_rec_mutex_unlock (&mutex);
42 g_rec_mutex_lock (&mutex);
43 g_rec_mutex_unlock (&mutex);
44 g_rec_mutex_clear (&mutex);
48 test_rec_mutex2 (void)
50 static GRecMutex mutex;
52 g_rec_mutex_lock (&mutex);
53 g_rec_mutex_unlock (&mutex);
54 g_rec_mutex_lock (&mutex);
55 g_rec_mutex_unlock (&mutex);
59 test_rec_mutex3 (void)
61 static GRecMutex mutex;
64 ret = g_rec_mutex_trylock (&mutex);
67 ret = g_rec_mutex_trylock (&mutex);
70 g_rec_mutex_unlock (&mutex);
71 g_rec_mutex_unlock (&mutex);
76 unsigned int n_iterations;
78 GThread **threads; /* (array length=n_threads) */
79 GThread **owners; /* (array length=n_locks), each element is locked by the corresponding mutex in @locks */
80 GRecMutex *locks; /* (array length=n_locks) */
84 thread_test_data_clear (ThreadTestData *data)
87 g_free (data->owners);
88 g_free (data->threads);
92 acquire (ThreadTestData *data,
97 self = g_thread_self ();
99 if (!g_rec_mutex_trylock (&data->locks[nr]))
101 if (g_test_verbose ())
102 g_printerr ("thread %p going to block on lock %d\n", self, nr);
104 g_rec_mutex_lock (&data->locks[nr]);
107 g_assert_null (data->owners[nr]); /* hopefully nobody else is here */
108 data->owners[nr] = self;
110 /* let some other threads try to ruin our day */
114 g_assert_true (data->owners[nr] == self); /* hopefully this is still us... */
116 if (g_test_verbose ())
117 g_printerr ("thread %p recursively taking lock %d\n", self, nr);
119 g_rec_mutex_lock (&data->locks[nr]); /* we're recursive, after all */
121 g_assert_true (data->owners[nr] == self); /* hopefully this is still us... */
123 g_rec_mutex_unlock (&data->locks[nr]);
128 g_assert_true (data->owners[nr] == self); /* hopefully this is still us... */
129 data->owners[nr] = NULL; /* make way for the next guy */
131 g_rec_mutex_unlock (&data->locks[nr]);
135 thread_func (gpointer user_data)
137 ThreadTestData *data = user_data;
140 rand = g_rand_new ();
142 for (unsigned int i = 0; i < data->n_iterations; i++)
143 acquire (data, g_rand_int_range (rand, 0, data->n_locks));
151 test_rec_mutex4 (void)
156 data.n_iterations = 10000;
157 data.n_threads = 100;
159 /* Some CI runners have a hard time with this much contention, so tone it down
164 data.n_iterations /= 10;
165 data.n_threads /= 10;
168 data.threads = g_new0 (GThread*, data.n_threads);
169 data.owners = g_new0 (GThread*, data.n_locks);
170 data.locks = g_new0 (GRecMutex, data.n_locks);
172 for (size_t i = 0; i < data.n_locks; i++)
173 g_rec_mutex_init (&data.locks[i]);
175 for (size_t i = 0; i < data.n_threads; i++)
176 data.threads[i] = g_thread_new ("test", thread_func, &data);
178 for (size_t i = 0; i < data.n_threads; i++)
179 g_thread_join (data.threads[i]);
181 for (size_t i = 0; i < data.n_locks; i++)
182 g_rec_mutex_clear (&data.locks[i]);
184 for (size_t i = 0; i < data.n_locks; i++)
185 g_assert_null (data.owners[i]);
187 thread_test_data_clear (&data);
190 static gint count_to = 0;
194 do_addition (gint *value)
196 static GRecMutex lock;
200 /* test performance of "good" cases (ie: short critical sections) */
201 for (i = 0; i < depth; i++)
202 g_rec_mutex_lock (&lock);
204 if ((more = *value != count_to))
208 for (i = 0; i < depth; i++)
209 g_rec_mutex_unlock (&lock);
215 addition_thread (gpointer value)
217 while (do_addition (value));
223 test_mutex_perf (gconstpointer data)
225 gint c = GPOINTER_TO_INT (data);
226 GThread *threads[100];
235 count_to = g_test_perf () ? 100000000 : n_threads + 1;
237 for (i = 0; i < n_threads - 1; i++)
238 threads[i] = g_thread_new ("test", addition_thread, &x);
240 /* avoid measuring thread setup/teardown time */
241 start_time = g_get_monotonic_time ();
242 g_atomic_int_set (&x, 0);
243 addition_thread (&x);
244 g_assert_cmpint (g_atomic_int_get (&x), ==, count_to);
245 rate = g_get_monotonic_time () - start_time;
248 for (i = 0; i < n_threads - 1; i++)
249 g_thread_join (threads[i]);
251 g_test_maximized_result (rate, "%f mips", rate);
256 main (int argc, char *argv[])
258 g_test_init (&argc, &argv, NULL);
260 g_test_add_func ("/thread/rec-mutex1", test_rec_mutex1);
261 g_test_add_func ("/thread/rec-mutex2", test_rec_mutex2);
262 g_test_add_func ("/thread/rec-mutex3", test_rec_mutex3);
263 g_test_add_func ("/thread/rec-mutex4", test_rec_mutex4);
268 for (i = 0; i < 5; i++)
269 for (j = 1; j <= 5; j++)
277 sprintf (name, "/thread/rec-mutex/perf/contended%d/depth%d", i, j);
279 sprintf (name, "/thread/rec-mutex/perf/uncontended/depth%d", j);
281 g_test_add_data_func (name, GINT_TO_POINTER (c), test_mutex_perf);
285 return g_test_run ();