1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tests/onceinit.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,286 @@
1.4 +/* g_once_init_*() test
1.5 + * Copyright (C) 2007 Tim Janik
1.6 + * Portions copyright (c) 2009 Nokia Corporation. All rights reserved.
1.7 + * This work is provided "as is"; redistribution and modification
1.8 + * in whole or in part, in any medium, physical or electronic is
1.9 + * permitted without restriction.
1.10 +
1.11 + * This work is distributed in the hope that it will be useful,
1.12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1.14 +
1.15 + * In no event shall the authors or contributors be liable for any
1.16 + * direct, indirect, incidental, special, exemplary, or consequential
1.17 + * damages (including, but not limited to, procurement of substitute
1.18 + * goods or services; loss of use, data, or profits; or business
1.19 + * interruption) however caused and on any theory of liability, whether
1.20 + * in contract, strict liability, or tort (including negligence or
1.21 + * otherwise) arising in any way out of the use of this software, even
1.22 + * if advised of the possibility of such damage.
1.23 + */
1.24 +#include <glib.h>
1.25 +#include <stdlib.h>
1.26 +#ifdef __SYMBIAN32__
1.27 +#include <glib_global.h>
1.28 +#include "mrt2_glib2_test.h"
1.29 +#endif /*__SYMBIAN32__*/
1.30 +#define N_THREADS (13)
1.31 +
1.32 +static GMutex *tmutex = NULL;
1.33 +static GCond *tcond = NULL;
1.34 +static volatile int thread_call_count = 0;
1.35 +static char dummy_value = 'x';
1.36 +
1.37 +static void
1.38 +assert_singleton_execution1 (void)
1.39 +{
1.40 + static volatile int seen_execution = 0;
1.41 + int old_seen_execution = g_atomic_int_exchange_and_add (&seen_execution, 1);
1.42 + if (old_seen_execution != 0)
1.43 + g_error ("%s: function executed more than once", G_STRFUNC);
1.44 +}
1.45 +
1.46 +static void
1.47 +assert_singleton_execution2 (void)
1.48 +{
1.49 + static volatile int seen_execution = 0;
1.50 + int old_seen_execution = g_atomic_int_exchange_and_add (&seen_execution, 1);
1.51 + if (old_seen_execution != 0)
1.52 + g_error ("%s: function executed more than once", G_STRFUNC);
1.53 +}
1.54 +
1.55 +static void
1.56 +assert_singleton_execution3 (void)
1.57 +{
1.58 + static volatile int seen_execution = 0;
1.59 + int old_seen_execution = g_atomic_int_exchange_and_add (&seen_execution, 1);
1.60 + if (old_seen_execution != 0)
1.61 + g_error ("%s: function executed more than once", G_STRFUNC);
1.62 +}
1.63 +
1.64 +static void
1.65 +initializer1 (void)
1.66 +{
1.67 + static volatile gsize initialized = 0;
1.68 + if (g_once_init_enter (&initialized))
1.69 + {
1.70 + gsize initval = 42;
1.71 + assert_singleton_execution1();
1.72 + g_once_init_leave (&initialized, initval);
1.73 + }
1.74 +}
1.75 +
1.76 +static gpointer
1.77 +initializer2 (void)
1.78 +{
1.79 + static volatile gsize initialized = 0;
1.80 + if (g_once_init_enter (&initialized))
1.81 + {
1.82 + void *pointer_value = &dummy_value;
1.83 + assert_singleton_execution2();
1.84 + g_once_init_leave (&initialized, (gsize) pointer_value);
1.85 + }
1.86 + return (void*) initialized;
1.87 +}
1.88 +
1.89 +static void
1.90 +initializer3 (void)
1.91 +{
1.92 + static volatile gsize initialized = 0;
1.93 + if (g_once_init_enter (&initialized))
1.94 + {
1.95 + gsize initval = 42;
1.96 + assert_singleton_execution3();
1.97 + g_usleep (25 * 1000); /* waste time for multiple threads to wait */
1.98 + g_once_init_leave (&initialized, initval);
1.99 + }
1.100 +}
1.101 +
1.102 +static gpointer
1.103 +tmain_call_initializer3 (gpointer user_data)
1.104 +{
1.105 + g_mutex_lock (tmutex);
1.106 + g_cond_wait (tcond, tmutex);
1.107 + g_mutex_unlock (tmutex);
1.108 + //g_printf ("[");
1.109 + initializer3();
1.110 + //g_printf ("]\n");
1.111 + g_atomic_int_exchange_and_add (&thread_call_count, 1);
1.112 + return NULL;
1.113 +}
1.114 +
1.115 +static void* stress_concurrent_initializers (void*);
1.116 +
1.117 +int
1.118 +main (int argc,
1.119 + char *argv[])
1.120 +{
1.121 + GThread *threads[N_THREADS];
1.122 + int i;
1.123 + void *p;
1.124 + #ifdef __SYMBIAN32__
1.125 + g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
1.126 + g_set_print_handler(mrtPrintHandler);
1.127 + #endif /*__SYMBIAN32__*/
1.128 +
1.129 + /* test simple initializer */
1.130 + initializer1();
1.131 + initializer1();
1.132 + /* test pointer initializer */
1.133 + /*void */p = initializer2();
1.134 + g_assert (p == &dummy_value);
1.135 + p = initializer2();
1.136 + g_assert (p == &dummy_value);
1.137 + /* setup threads */
1.138 + g_thread_init (NULL);
1.139 + tmutex = g_mutex_new ();
1.140 + tcond = g_cond_new ();
1.141 + /* start multiple threads for initializer3() */
1.142 + g_mutex_lock (tmutex);
1.143 + for (i = 0; i < N_THREADS; i++)
1.144 + threads[i] = g_thread_create (tmain_call_initializer3, 0, FALSE, NULL);
1.145 + g_mutex_unlock (tmutex);
1.146 + /* concurrently call initializer3() */
1.147 + g_cond_broadcast (tcond);
1.148 + /* loop until all threads passed the call to initializer3() */
1.149 + while (g_atomic_int_get (&thread_call_count) < i)
1.150 + {
1.151 + if (rand() % 2)
1.152 + g_thread_yield(); /* concurrent shuffling for single core */
1.153 + else
1.154 + g_usleep (1000); /* concurrent shuffling for multi core */
1.155 + g_cond_broadcast (tcond);
1.156 + }
1.157 + /* call multiple (unoptimized) initializers from multiple threads */
1.158 + g_mutex_lock (tmutex);
1.159 + g_atomic_int_set (&thread_call_count, 0);
1.160 + for (i = 0; i < N_THREADS; i++)
1.161 + g_thread_create (stress_concurrent_initializers, 0, FALSE, NULL);
1.162 + g_mutex_unlock (tmutex);
1.163 + while (g_atomic_int_get (&thread_call_count) < 256 * 4 * N_THREADS)
1.164 + g_usleep (50 * 1000); /* wait for all 5 threads to complete */
1.165 + #if __SYMBIAN32__
1.166 + testResultXml("onceinit");
1.167 + #endif /* EMULATOR */
1.168 + return 0;
1.169 +}
1.170 +
1.171 +/* get rid of g_once_init_enter-optimizations in the below definitions
1.172 + * to uncover possible races in the g_once_init_enter_impl()/
1.173 + * g_once_init_leave() implementations
1.174 + */
1.175 +#define g_once_init_enter g_once_init_enter_impl
1.176 +
1.177 +/* define 16 * 16 simple initializers */
1.178 +#define DEFINE_TEST_INITIALIZER(N) \
1.179 + static void \
1.180 + test_initializer_##N (void) \
1.181 + { \
1.182 + static volatile gsize initialized = 0; \
1.183 + if (g_once_init_enter (&initialized)) \
1.184 + { \
1.185 + g_free (g_strdup_printf ("cpuhog%5d", 1)); \
1.186 + g_free (g_strdup_printf ("cpuhog%6d", 2)); \
1.187 + g_free (g_strdup_printf ("cpuhog%7d", 3)); \
1.188 + g_once_init_leave (&initialized, 1); \
1.189 + } \
1.190 + }
1.191 +#define DEFINE_16_TEST_INITIALIZERS(P) \
1.192 + DEFINE_TEST_INITIALIZER (P##0) \
1.193 + DEFINE_TEST_INITIALIZER (P##1) \
1.194 + DEFINE_TEST_INITIALIZER (P##2) \
1.195 + DEFINE_TEST_INITIALIZER (P##3) \
1.196 + DEFINE_TEST_INITIALIZER (P##4) \
1.197 + DEFINE_TEST_INITIALIZER (P##5) \
1.198 + DEFINE_TEST_INITIALIZER (P##6) \
1.199 + DEFINE_TEST_INITIALIZER (P##7) \
1.200 + DEFINE_TEST_INITIALIZER (P##8) \
1.201 + DEFINE_TEST_INITIALIZER (P##9) \
1.202 + DEFINE_TEST_INITIALIZER (P##a) \
1.203 + DEFINE_TEST_INITIALIZER (P##b) \
1.204 + DEFINE_TEST_INITIALIZER (P##c) \
1.205 + DEFINE_TEST_INITIALIZER (P##d) \
1.206 + DEFINE_TEST_INITIALIZER (P##e) \
1.207 + DEFINE_TEST_INITIALIZER (P##f)
1.208 +#define DEFINE_256_TEST_INITIALIZERS(P) \
1.209 + DEFINE_16_TEST_INITIALIZERS (P##_0) \
1.210 + DEFINE_16_TEST_INITIALIZERS (P##_1) \
1.211 + DEFINE_16_TEST_INITIALIZERS (P##_2) \
1.212 + DEFINE_16_TEST_INITIALIZERS (P##_3) \
1.213 + DEFINE_16_TEST_INITIALIZERS (P##_4) \
1.214 + DEFINE_16_TEST_INITIALIZERS (P##_5) \
1.215 + DEFINE_16_TEST_INITIALIZERS (P##_6) \
1.216 + DEFINE_16_TEST_INITIALIZERS (P##_7) \
1.217 + DEFINE_16_TEST_INITIALIZERS (P##_8) \
1.218 + DEFINE_16_TEST_INITIALIZERS (P##_9) \
1.219 + DEFINE_16_TEST_INITIALIZERS (P##_a) \
1.220 + DEFINE_16_TEST_INITIALIZERS (P##_b) \
1.221 + DEFINE_16_TEST_INITIALIZERS (P##_c) \
1.222 + DEFINE_16_TEST_INITIALIZERS (P##_d) \
1.223 + DEFINE_16_TEST_INITIALIZERS (P##_e) \
1.224 + DEFINE_16_TEST_INITIALIZERS (P##_f)
1.225 +
1.226 +/* list 16 * 16 simple initializers */
1.227 +#define LIST_16_TEST_INITIALIZERS(P) \
1.228 + test_initializer_##P##0, \
1.229 + test_initializer_##P##1, \
1.230 + test_initializer_##P##2, \
1.231 + test_initializer_##P##3, \
1.232 + test_initializer_##P##4, \
1.233 + test_initializer_##P##5, \
1.234 + test_initializer_##P##6, \
1.235 + test_initializer_##P##7, \
1.236 + test_initializer_##P##8, \
1.237 + test_initializer_##P##9, \
1.238 + test_initializer_##P##a, \
1.239 + test_initializer_##P##b, \
1.240 + test_initializer_##P##c, \
1.241 + test_initializer_##P##d, \
1.242 + test_initializer_##P##e, \
1.243 + test_initializer_##P##f
1.244 +#define LIST_256_TEST_INITIALIZERS(P) \
1.245 + LIST_16_TEST_INITIALIZERS (P##_0), \
1.246 + LIST_16_TEST_INITIALIZERS (P##_1), \
1.247 + LIST_16_TEST_INITIALIZERS (P##_2), \
1.248 + LIST_16_TEST_INITIALIZERS (P##_3), \
1.249 + LIST_16_TEST_INITIALIZERS (P##_4), \
1.250 + LIST_16_TEST_INITIALIZERS (P##_5), \
1.251 + LIST_16_TEST_INITIALIZERS (P##_6), \
1.252 + LIST_16_TEST_INITIALIZERS (P##_7), \
1.253 + LIST_16_TEST_INITIALIZERS (P##_8), \
1.254 + LIST_16_TEST_INITIALIZERS (P##_9), \
1.255 + LIST_16_TEST_INITIALIZERS (P##_a), \
1.256 + LIST_16_TEST_INITIALIZERS (P##_b), \
1.257 + LIST_16_TEST_INITIALIZERS (P##_c), \
1.258 + LIST_16_TEST_INITIALIZERS (P##_d), \
1.259 + LIST_16_TEST_INITIALIZERS (P##_e), \
1.260 + LIST_16_TEST_INITIALIZERS (P##_f)
1.261 +
1.262 +/* define 4 * 256 initializers */
1.263 +DEFINE_256_TEST_INITIALIZERS (stress1);
1.264 +DEFINE_256_TEST_INITIALIZERS (stress2);
1.265 +DEFINE_256_TEST_INITIALIZERS (stress3);
1.266 +DEFINE_256_TEST_INITIALIZERS (stress4);
1.267 +
1.268 +/* call the above 1024 initializers */
1.269 +static void*
1.270 +stress_concurrent_initializers (void *user_data)
1.271 +{
1.272 + static void (*initializers[]) (void) = {
1.273 + LIST_256_TEST_INITIALIZERS (stress1),
1.274 + LIST_256_TEST_INITIALIZERS (stress2),
1.275 + LIST_256_TEST_INITIALIZERS (stress3),
1.276 + LIST_256_TEST_INITIALIZERS (stress4),
1.277 + };
1.278 + int i;
1.279 + /* sync to main thread */
1.280 + g_mutex_lock (tmutex);
1.281 + g_mutex_unlock (tmutex);
1.282 + /* initialize concurrently */
1.283 + for (i = 0; i < G_N_ELEMENTS (initializers); i++)
1.284 + {
1.285 + initializers[i]();
1.286 + g_atomic_int_exchange_and_add (&thread_call_count, 1);
1.287 + }
1.288 + return NULL;
1.289 +}