GVariant: use GVariantTypeInfo on internal constructors
[platform/upstream/glib.git] / glib / tests / mutex.c
1 /* Unit tests for GMutex
2  * Copyright (C) 2011 Red Hat, Inc
3  * Author: Matthias Clasen
4  *
5  * This work is provided "as is"; redistribution and modification
6  * in whole or in part, in any medium, physical or electronic is
7  * permitted without restriction.
8  *
9  * This work is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * In no event shall the authors or contributors be liable for any
14  * direct, indirect, incidental, special, exemplary, or consequential
15  * damages (including, but not limited to, procurement of substitute
16  * goods or services; loss of use, data, or profits; or business
17  * interruption) however caused and on any theory of liability, whether
18  * in contract, strict liability, or tort (including negligence or
19  * otherwise) arising in any way out of the use of this software, even
20  * if advised of the possibility of such damage.
21  */
22
23 /* We are testing some deprecated APIs here */
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
25
26 #include <glib.h>
27
28 #include <stdio.h>
29
30 static void
31 test_mutex1 (void)
32 {
33   GMutex mutex;
34
35   g_mutex_init (&mutex);
36   g_mutex_lock (&mutex);
37   g_mutex_unlock (&mutex);
38   g_mutex_lock (&mutex);
39   g_mutex_unlock (&mutex);
40   g_mutex_clear (&mutex);
41 }
42
43 static void
44 test_mutex2 (void)
45 {
46   static GMutex mutex;
47
48   g_mutex_lock (&mutex);
49   g_mutex_unlock (&mutex);
50   g_mutex_lock (&mutex);
51   g_mutex_unlock (&mutex);
52 }
53
54 static void
55 test_mutex3 (void)
56 {
57   GMutex *mutex;
58
59   mutex = g_mutex_new ();
60   g_mutex_lock (mutex);
61   g_mutex_unlock (mutex);
62   g_mutex_lock (mutex);
63   g_mutex_unlock (mutex);
64   g_mutex_free (mutex);
65 }
66
67 static void
68 test_mutex4 (void)
69 {
70   static GMutex mutex;
71   gboolean ret;
72
73   ret = g_mutex_trylock (&mutex);
74   g_assert (ret);
75
76   /* no guarantees that mutex is recursive, so could return 0 or 1 */
77   if (g_mutex_trylock (&mutex))
78     g_mutex_unlock (&mutex);
79
80   g_mutex_unlock (&mutex);
81 }
82
83 #define LOCKS      48
84 #define ITERATIONS 10000
85 #define THREADS    100
86
87
88 GThread *owners[LOCKS];
89 GMutex   locks[LOCKS];
90
91 static void
92 acquire (gint nr)
93 {
94   GThread *self;
95
96   self = g_thread_self ();
97
98   if (!g_mutex_trylock (&locks[nr]))
99     {
100       if (g_test_verbose ())
101         g_print ("thread %p going to block on lock %d\n", self, nr);
102
103       g_mutex_lock (&locks[nr]);
104     }
105
106   g_assert (owners[nr] == NULL);   /* hopefully nobody else is here */
107   owners[nr] = self;
108
109   /* let some other threads try to ruin our day */
110   g_thread_yield ();
111   g_thread_yield ();
112   g_thread_yield ();
113
114   g_assert (owners[nr] == self);   /* hopefully this is still us... */
115   owners[nr] = NULL;               /* make way for the next guy */
116
117   g_mutex_unlock (&locks[nr]);
118 }
119
120 static gpointer
121 thread_func (gpointer data)
122 {
123   gint i;
124   GRand *rand;
125
126   rand = g_rand_new ();
127
128   for (i = 0; i < ITERATIONS; i++)
129     acquire (g_rand_int_range (rand, 0, LOCKS));
130
131   g_rand_free (rand);
132
133   return NULL;
134 }
135
136 static void
137 test_mutex5 (void)
138 {
139   gint i;
140   GThread *threads[THREADS];
141
142   for (i = 0; i < LOCKS; i++)
143     g_mutex_init (&locks[i]);
144
145   for (i = 0; i < THREADS; i++)
146     threads[i] = g_thread_create (thread_func, NULL, TRUE, NULL);
147
148   for (i = 0; i < THREADS; i++)
149     g_thread_join (threads[i]);
150
151   for (i = 0; i < LOCKS; i++)
152     g_mutex_clear (&locks[i]);
153
154   for (i = 0; i < LOCKS; i++)
155     g_assert (owners[i] == NULL);
156 }
157
158 #define COUNT_TO 100000000
159
160 static gboolean
161 do_addition (gint *value)
162 {
163   static GMutex lock;
164   gboolean more;
165
166   /* test performance of "good" cases (ie: short critical sections) */
167   g_mutex_lock (&lock);
168   if ((more = *value != COUNT_TO))
169     if (*value != -1)
170       (*value)++;
171   g_mutex_unlock (&lock);
172
173   return more;
174 }
175
176 static gpointer
177 addition_thread (gpointer value)
178 {
179   while (do_addition (value));
180
181   return NULL;
182 }
183
184 static void
185 test_mutex_perf (gconstpointer data)
186 {
187   gint n_threads = GPOINTER_TO_INT (data);
188   GThread *threads[THREADS];
189   gint64 start_time;
190   gdouble rate;
191   gint x = -1;
192   gint i;
193
194   for (i = 0; i < n_threads - 1; i++)
195     threads[i] = g_thread_create (addition_thread, &x, TRUE, NULL);
196
197   /* avoid measuring thread setup/teardown time */
198   start_time = g_get_monotonic_time ();
199   g_atomic_int_set (&x, 0);
200   addition_thread (&x);
201   g_assert_cmpint (g_atomic_int_get (&x), ==, COUNT_TO);
202   rate = g_get_monotonic_time () - start_time;
203   rate = x / rate;
204
205   for (i = 0; i < n_threads - 1; i++)
206     g_thread_join (threads[i]);
207
208   g_test_maximized_result (rate, "%f mips", rate);
209 }
210
211 int
212 main (int argc, char *argv[])
213 {
214   g_test_init (&argc, &argv, NULL);
215
216   g_test_add_func ("/thread/mutex1", test_mutex1);
217   g_test_add_func ("/thread/mutex2", test_mutex2);
218   g_test_add_func ("/thread/mutex3", test_mutex3);
219   g_test_add_func ("/thread/mutex4", test_mutex4);
220   g_test_add_func ("/thread/mutex5", test_mutex5);
221
222   if (g_test_perf ())
223     {
224       gint i;
225
226       g_test_add_data_func ("/thread/mutex/perf/uncontended", NULL, test_mutex_perf);
227
228       for (i = 1; i <= 10; i++)
229         {
230           gchar name[80];
231           sprintf (name, "/thread/mutex/perf/contended/%d", i);
232           g_test_add_data_func (name, GINT_TO_POINTER (i), test_mutex_perf);
233         }
234     }
235
236   return g_test_run ();
237 }