1 /* g_once_init_*() test
2 * Copyright (C) 2007 Tim Janik
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
24 #define N_THREADS (13)
28 static volatile int thread_call_count = 0;
29 static char dummy_value = 'x';
32 assert_singleton_execution1 (void)
34 static volatile int seen_execution = 0;
35 int old_seen_execution = g_atomic_int_add (&seen_execution, 1);
36 if (old_seen_execution != 0)
37 g_error ("%s: function executed more than once", G_STRFUNC);
41 assert_singleton_execution2 (void)
43 static volatile int seen_execution = 0;
44 int old_seen_execution = g_atomic_int_add (&seen_execution, 1);
45 if (old_seen_execution != 0)
46 g_error ("%s: function executed more than once", G_STRFUNC);
50 assert_singleton_execution3 (void)
52 static volatile int seen_execution = 0;
53 int old_seen_execution = g_atomic_int_add (&seen_execution, 1);
54 if (old_seen_execution != 0)
55 g_error ("%s: function executed more than once", G_STRFUNC);
61 static volatile gsize initialized = 0;
62 if (g_once_init_enter (&initialized))
65 assert_singleton_execution1();
66 g_once_init_leave (&initialized, initval);
73 static volatile gsize initialized = 0;
74 if (g_once_init_enter (&initialized))
76 void *pointer_value = &dummy_value;
77 assert_singleton_execution2();
78 g_once_init_leave (&initialized, (gsize) pointer_value);
80 return (void*) initialized;
86 static volatile gsize initialized = 0;
87 if (g_once_init_enter (&initialized))
90 assert_singleton_execution3();
91 g_usleep (25 * 1000); /* waste time for multiple threads to wait */
92 g_once_init_leave (&initialized, initval);
97 tmain_call_initializer3 (gpointer user_data)
99 g_mutex_lock (&tmutex);
100 g_cond_wait (&tcond, &tmutex);
101 g_mutex_unlock (&tmutex);
105 g_atomic_int_add (&thread_call_count, 1);
109 static void* stress_concurrent_initializers (void*);
115 G_GNUC_UNUSED GThread *threads[N_THREADS];
119 /* test simple initializer */
122 /* test pointer initializer */
124 g_assert (p == &dummy_value);
126 g_assert (p == &dummy_value);
128 g_thread_init (NULL);
129 /* start multiple threads for initializer3() */
130 g_mutex_lock (&tmutex);
131 for (i = 0; i < N_THREADS; i++)
132 threads[i] = g_thread_create (tmain_call_initializer3, 0, FALSE, NULL);
133 g_mutex_unlock (&tmutex);
134 /* concurrently call initializer3() */
135 g_cond_broadcast (&tcond);
136 /* loop until all threads passed the call to initializer3() */
137 while (g_atomic_int_get (&thread_call_count) < i)
140 g_thread_yield(); /* concurrent shuffling for single core */
142 g_usleep (1000); /* concurrent shuffling for multi core */
143 g_cond_broadcast (&tcond);
145 /* call multiple (unoptimized) initializers from multiple threads */
146 g_mutex_lock (&tmutex);
147 g_atomic_int_set (&thread_call_count, 0);
148 for (i = 0; i < N_THREADS; i++)
149 g_thread_create (stress_concurrent_initializers, 0, FALSE, NULL);
150 g_mutex_unlock (&tmutex);
151 while (g_atomic_int_get (&thread_call_count) < 256 * 4 * N_THREADS)
152 g_usleep (50 * 1000); /* wait for all 5 threads to complete */
156 /* get rid of g_once_init_enter-optimizations in the below definitions
157 * to uncover possible races in the g_once_init_enter_impl()/
158 * g_once_init_leave() implementations
160 #undef g_once_init_enter
161 #undef g_once_init_leave
163 /* define 16 * 16 simple initializers */
164 #define DEFINE_TEST_INITIALIZER(N) \
166 test_initializer_##N (void) \
168 static volatile gsize initialized = 0; \
169 if (g_once_init_enter (&initialized)) \
171 g_free (g_strdup_printf ("cpuhog%5d", 1)); \
172 g_free (g_strdup_printf ("cpuhog%6d", 2)); \
173 g_free (g_strdup_printf ("cpuhog%7d", 3)); \
174 g_once_init_leave (&initialized, 1); \
177 #define DEFINE_16_TEST_INITIALIZERS(P) \
178 DEFINE_TEST_INITIALIZER (P##0) \
179 DEFINE_TEST_INITIALIZER (P##1) \
180 DEFINE_TEST_INITIALIZER (P##2) \
181 DEFINE_TEST_INITIALIZER (P##3) \
182 DEFINE_TEST_INITIALIZER (P##4) \
183 DEFINE_TEST_INITIALIZER (P##5) \
184 DEFINE_TEST_INITIALIZER (P##6) \
185 DEFINE_TEST_INITIALIZER (P##7) \
186 DEFINE_TEST_INITIALIZER (P##8) \
187 DEFINE_TEST_INITIALIZER (P##9) \
188 DEFINE_TEST_INITIALIZER (P##a) \
189 DEFINE_TEST_INITIALIZER (P##b) \
190 DEFINE_TEST_INITIALIZER (P##c) \
191 DEFINE_TEST_INITIALIZER (P##d) \
192 DEFINE_TEST_INITIALIZER (P##e) \
193 DEFINE_TEST_INITIALIZER (P##f)
194 #define DEFINE_256_TEST_INITIALIZERS(P) \
195 DEFINE_16_TEST_INITIALIZERS (P##_0) \
196 DEFINE_16_TEST_INITIALIZERS (P##_1) \
197 DEFINE_16_TEST_INITIALIZERS (P##_2) \
198 DEFINE_16_TEST_INITIALIZERS (P##_3) \
199 DEFINE_16_TEST_INITIALIZERS (P##_4) \
200 DEFINE_16_TEST_INITIALIZERS (P##_5) \
201 DEFINE_16_TEST_INITIALIZERS (P##_6) \
202 DEFINE_16_TEST_INITIALIZERS (P##_7) \
203 DEFINE_16_TEST_INITIALIZERS (P##_8) \
204 DEFINE_16_TEST_INITIALIZERS (P##_9) \
205 DEFINE_16_TEST_INITIALIZERS (P##_a) \
206 DEFINE_16_TEST_INITIALIZERS (P##_b) \
207 DEFINE_16_TEST_INITIALIZERS (P##_c) \
208 DEFINE_16_TEST_INITIALIZERS (P##_d) \
209 DEFINE_16_TEST_INITIALIZERS (P##_e) \
210 DEFINE_16_TEST_INITIALIZERS (P##_f)
212 /* list 16 * 16 simple initializers */
213 #define LIST_16_TEST_INITIALIZERS(P) \
214 test_initializer_##P##0, \
215 test_initializer_##P##1, \
216 test_initializer_##P##2, \
217 test_initializer_##P##3, \
218 test_initializer_##P##4, \
219 test_initializer_##P##5, \
220 test_initializer_##P##6, \
221 test_initializer_##P##7, \
222 test_initializer_##P##8, \
223 test_initializer_##P##9, \
224 test_initializer_##P##a, \
225 test_initializer_##P##b, \
226 test_initializer_##P##c, \
227 test_initializer_##P##d, \
228 test_initializer_##P##e, \
229 test_initializer_##P##f
230 #define LIST_256_TEST_INITIALIZERS(P) \
231 LIST_16_TEST_INITIALIZERS (P##_0), \
232 LIST_16_TEST_INITIALIZERS (P##_1), \
233 LIST_16_TEST_INITIALIZERS (P##_2), \
234 LIST_16_TEST_INITIALIZERS (P##_3), \
235 LIST_16_TEST_INITIALIZERS (P##_4), \
236 LIST_16_TEST_INITIALIZERS (P##_5), \
237 LIST_16_TEST_INITIALIZERS (P##_6), \
238 LIST_16_TEST_INITIALIZERS (P##_7), \
239 LIST_16_TEST_INITIALIZERS (P##_8), \
240 LIST_16_TEST_INITIALIZERS (P##_9), \
241 LIST_16_TEST_INITIALIZERS (P##_a), \
242 LIST_16_TEST_INITIALIZERS (P##_b), \
243 LIST_16_TEST_INITIALIZERS (P##_c), \
244 LIST_16_TEST_INITIALIZERS (P##_d), \
245 LIST_16_TEST_INITIALIZERS (P##_e), \
246 LIST_16_TEST_INITIALIZERS (P##_f)
248 /* define 4 * 256 initializers */
249 DEFINE_256_TEST_INITIALIZERS (stress1);
250 DEFINE_256_TEST_INITIALIZERS (stress2);
251 DEFINE_256_TEST_INITIALIZERS (stress3);
252 DEFINE_256_TEST_INITIALIZERS (stress4);
254 /* call the above 1024 initializers */
256 stress_concurrent_initializers (void *user_data)
258 static void (*initializers[]) (void) = {
259 LIST_256_TEST_INITIALIZERS (stress1),
260 LIST_256_TEST_INITIALIZERS (stress2),
261 LIST_256_TEST_INITIALIZERS (stress3),
262 LIST_256_TEST_INITIALIZERS (stress4),
265 /* sync to main thread */
266 g_mutex_lock (&tmutex);
267 g_mutex_unlock (&tmutex);
268 /* initialize concurrently */
269 for (i = 0; i < G_N_ELEMENTS (initializers); i++)
272 g_atomic_int_add (&thread_call_count, 1);