gkdbus: Fix underflow and unreachable code bug
[platform/upstream/glib.git] / glib / tests / rec-mutex.c
1 /* Unit tests for GRecMutex
2  * Copyright (C) 2011 Red Hat, Inc
3  * Author: Matthias Clasen
4  *
5  * SPDX-License-Identifier: LicenseRef-old-glib-tests
6  *
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.
10  *
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.
14  *
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.
23  */
24
25 /* We are testing some deprecated APIs here */
26 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
27 #define GLIB_DISABLE_DEPRECATION_WARNINGS
28 #endif
29
30 #include <glib.h>
31
32 #include <stdio.h>
33
34 static void
35 test_rec_mutex1 (void)
36 {
37   GRecMutex mutex;
38
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);
45 }
46
47 static void
48 test_rec_mutex2 (void)
49 {
50   static GRecMutex mutex;
51
52   g_rec_mutex_lock (&mutex);
53   g_rec_mutex_unlock (&mutex);
54   g_rec_mutex_lock (&mutex);
55   g_rec_mutex_unlock (&mutex);
56 }
57
58 static void
59 test_rec_mutex3 (void)
60 {
61   static GRecMutex mutex;
62   gboolean ret;
63
64   ret = g_rec_mutex_trylock (&mutex);
65   g_assert_true (ret);
66
67   ret = g_rec_mutex_trylock (&mutex);
68   g_assert_true (ret);
69
70   g_rec_mutex_unlock (&mutex);
71   g_rec_mutex_unlock (&mutex);
72 }
73
74 typedef struct {
75   size_t n_locks;
76   unsigned int n_iterations;
77   size_t n_threads;
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) */
81 } ThreadTestData;
82
83 static void
84 thread_test_data_clear (ThreadTestData *data)
85 {
86   g_free (data->locks);
87   g_free (data->owners);
88   g_free (data->threads);
89 }
90
91 static void
92 acquire (ThreadTestData *data,
93          unsigned int    nr)
94 {
95   GThread *self;
96
97   self = g_thread_self ();
98
99   if (!g_rec_mutex_trylock (&data->locks[nr]))
100     {
101       if (g_test_verbose ())
102         g_printerr ("thread %p going to block on lock %d\n", self, nr);
103
104       g_rec_mutex_lock (&data->locks[nr]);
105     }
106
107   g_assert_null (data->owners[nr]);   /* hopefully nobody else is here */
108   data->owners[nr] = self;
109
110   /* let some other threads try to ruin our day */
111   g_thread_yield ();
112   g_thread_yield ();
113
114   g_assert_true (data->owners[nr] == self);   /* hopefully this is still us... */
115
116   if (g_test_verbose ())
117     g_printerr ("thread %p recursively taking lock %d\n", self, nr);
118
119   g_rec_mutex_lock (&data->locks[nr]);  /* we're recursive, after all */
120
121   g_assert_true (data->owners[nr] == self);   /* hopefully this is still us... */
122
123   g_rec_mutex_unlock (&data->locks[nr]);
124
125   g_thread_yield ();
126   g_thread_yield ();
127
128   g_assert_true (data->owners[nr] == self);   /* hopefully this is still us... */
129   data->owners[nr] = NULL;               /* make way for the next guy */
130
131   g_rec_mutex_unlock (&data->locks[nr]);
132 }
133
134 static gpointer
135 thread_func (gpointer user_data)
136 {
137   ThreadTestData *data = user_data;
138   GRand *rand;
139
140   rand = g_rand_new ();
141
142   for (unsigned int i = 0; i < data->n_iterations; i++)
143     acquire (data, g_rand_int_range (rand, 0, data->n_locks));
144
145   g_rand_free (rand);
146
147   return NULL;
148 }
149
150 static void
151 test_rec_mutex4 (void)
152 {
153   ThreadTestData data;
154
155   data.n_locks = 48;
156   data.n_iterations = 10000;
157   data.n_threads = 100;
158
159   /* Some CI runners have a hard time with this much contention, so tone it down
160    * a bit for CI. */
161   if (!g_test_perf ())
162     {
163       data.n_locks /= 10;
164       data.n_iterations /= 10;
165       data.n_threads /= 10;
166     }
167
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);
171
172   for (size_t i = 0; i < data.n_locks; i++)
173     g_rec_mutex_init (&data.locks[i]);
174
175   for (size_t i = 0; i < data.n_threads; i++)
176     data.threads[i] = g_thread_new ("test", thread_func, &data);
177
178   for (size_t i = 0; i < data.n_threads; i++)
179     g_thread_join (data.threads[i]);
180
181   for (size_t i = 0; i < data.n_locks; i++)
182     g_rec_mutex_clear (&data.locks[i]);
183
184   for (size_t i = 0; i < data.n_locks; i++)
185     g_assert_null (data.owners[i]);
186
187   thread_test_data_clear (&data);
188 }
189
190 static gint count_to = 0;
191 static gint depth;
192
193 static gboolean
194 do_addition (gint *value)
195 {
196   static GRecMutex lock;
197   gboolean more;
198   gint i;
199
200   /* test performance of "good" cases (ie: short critical sections) */
201   for (i = 0; i < depth; i++)
202     g_rec_mutex_lock (&lock);
203
204   if ((more = *value != count_to))
205     if (*value != -1)
206       (*value)++;
207
208   for (i = 0; i < depth; i++)
209     g_rec_mutex_unlock (&lock);
210
211   return more;
212 }
213
214 static gpointer
215 addition_thread (gpointer value)
216 {
217   while (do_addition (value));
218
219   return NULL;
220 }
221
222 static void
223 test_mutex_perf (gconstpointer data)
224 {
225   gint c = GPOINTER_TO_INT (data);
226   GThread *threads[100];
227   gint64 start_time;
228   gint n_threads;
229   gdouble rate;
230   gint x = -1;
231   gint i;
232
233   n_threads = c / 256;
234   depth = c % 256;
235   count_to = g_test_perf () ? 100000000 : n_threads + 1;
236
237   for (i = 0; i < n_threads - 1; i++)
238     threads[i] = g_thread_new ("test", addition_thread, &x);
239
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;
246   rate = x / rate;
247
248   for (i = 0; i < n_threads - 1; i++)
249     g_thread_join (threads[i]);
250
251   g_test_maximized_result (rate, "%f mips", rate);
252 }
253
254
255 int
256 main (int argc, char *argv[])
257 {
258   g_test_init (&argc, &argv, NULL);
259
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);
264
265     {
266       gint i, j;
267
268       for (i = 0; i < 5; i++)
269         for (j = 1; j <= 5; j++)
270           {
271             gchar name[80];
272             guint c;
273
274             c = i * 256 + j;
275
276             if (i)
277               sprintf (name, "/thread/rec-mutex/perf/contended%d/depth%d", i, j);
278             else
279               sprintf (name, "/thread/rec-mutex/perf/uncontended/depth%d", j);
280
281             g_test_add_data_func (name, GINT_TO_POINTER (c), test_mutex_perf);
282           }
283     }
284
285   return g_test_run ();
286 }