1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/thread-test.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,421 @@
1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
1.5 +#undef G_DISABLE_ASSERT
1.6 +#undef G_LOG_DOMAIN
1.7 +
1.8 +#include <glib.h>
1.9 +#include <stdio.h>
1.10 +
1.11 +#ifdef SYMBIAN
1.12 +#include <glib_global.h>
1.13 +#include "mrt2_glib2_test.h"
1.14 +#endif /*SYMBIAN*/
1.15 +
1.16 +
1.17 +/* GMutex */
1.18 +
1.19 +static GMutex* test_g_mutex_mutex = NULL;
1.20 +static guint test_g_mutex_int = 0;
1.21 +static gboolean test_g_mutex_thread_ready;
1.22 +G_LOCK_DEFINE_STATIC (test_g_mutex);
1.23 +
1.24 +static gpointer
1.25 +test_g_mutex_thread (gpointer data)
1.26 +{
1.27 + g_assert (GPOINTER_TO_INT (data) == 42);
1.28 + g_assert (g_mutex_trylock (test_g_mutex_mutex) == FALSE);
1.29 + g_assert (G_TRYLOCK (test_g_mutex) == FALSE);
1.30 + test_g_mutex_thread_ready = TRUE;
1.31 + g_mutex_lock (test_g_mutex_mutex);
1.32 + g_assert (test_g_mutex_int == 42);
1.33 + g_mutex_unlock (test_g_mutex_mutex);
1.34 +
1.35 + return GINT_TO_POINTER (41);
1.36 +}
1.37 +
1.38 +static void
1.39 +test_g_mutex (void)
1.40 +{
1.41 + GThread *thread;
1.42 + test_g_mutex_mutex = g_mutex_new ();
1.43 +
1.44 + g_assert (g_mutex_trylock (test_g_mutex_mutex));
1.45 + g_assert (G_TRYLOCK (test_g_mutex));
1.46 + test_g_mutex_thread_ready = FALSE;
1.47 + thread = g_thread_create (test_g_mutex_thread, GINT_TO_POINTER (42),
1.48 + TRUE, NULL);
1.49 + /* This busy wait is only for testing purposes and not an example of
1.50 + * good code!*/
1.51 + while (!test_g_mutex_thread_ready)
1.52 + g_usleep (G_USEC_PER_SEC / 5);
1.53 + test_g_mutex_int = 42;
1.54 + G_UNLOCK (test_g_mutex);
1.55 + g_mutex_unlock (test_g_mutex_mutex);
1.56 + g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 41);
1.57 + g_mutex_free (test_g_mutex_mutex);
1.58 +}
1.59 +
1.60 +/* GStaticRecMutex */
1.61 +
1.62 +static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
1.63 +static guint test_g_static_rec_mutex_int = 0;
1.64 +static gboolean test_g_static_rec_mutex_thread_ready;
1.65 +
1.66 +static gpointer
1.67 +test_g_static_rec_mutex_thread (gpointer data)
1.68 +{
1.69 + g_assert (GPOINTER_TO_INT (data) == 42);
1.70 + g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)
1.71 + == FALSE);
1.72 + test_g_static_rec_mutex_thread_ready = TRUE;
1.73 + g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
1.74 + g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
1.75 + g_assert (test_g_static_rec_mutex_int == 42);
1.76 + test_g_static_rec_mutex_thread_ready = FALSE;
1.77 + g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
1.78 + g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
1.79 +
1.80 + g_thread_exit (GINT_TO_POINTER (43));
1.81 +
1.82 + g_assert_not_reached ();
1.83 + return NULL;
1.84 +}
1.85 +
1.86 +static void
1.87 +test_g_static_rec_mutex (void)
1.88 +{
1.89 + GThread *thread;
1.90 +
1.91 + g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
1.92 + test_g_static_rec_mutex_thread_ready = FALSE;
1.93 + thread = g_thread_create (test_g_static_rec_mutex_thread,
1.94 + GINT_TO_POINTER (42), TRUE, NULL);
1.95 + /* This busy wait is only for testing purposes and not an example of
1.96 + * good code!*/
1.97 + while (!test_g_static_rec_mutex_thread_ready)
1.98 + g_usleep (G_USEC_PER_SEC / 5);
1.99 +
1.100 + g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
1.101 + test_g_static_rec_mutex_int = 41;
1.102 + g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
1.103 + test_g_static_rec_mutex_int = 42;
1.104 + g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
1.105 +
1.106 + /* This busy wait is only for testing purposes and not an example of
1.107 + * good code!*/
1.108 + while (test_g_static_rec_mutex_thread_ready)
1.109 + g_usleep (G_USEC_PER_SEC / 5);
1.110 +
1.111 + g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
1.112 + test_g_static_rec_mutex_int = 0;
1.113 + g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
1.114 +
1.115 + g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43);
1.116 +}
1.117 +
1.118 +/* GStaticPrivate */
1.119 +
1.120 +#define THREADS 10
1.121 +
1.122 +static GStaticPrivate test_g_static_private_private1 = G_STATIC_PRIVATE_INIT;
1.123 +static GStaticPrivate test_g_static_private_private2 = G_STATIC_PRIVATE_INIT;
1.124 +static GStaticMutex test_g_static_private_mutex = G_STATIC_MUTEX_INIT;
1.125 +static guint test_g_static_private_counter = 0;
1.126 +static guint test_g_static_private_ready = 0;
1.127 +
1.128 +static gpointer
1.129 +test_g_static_private_constructor (void)
1.130 +{
1.131 + g_static_mutex_lock (&test_g_static_private_mutex);
1.132 + test_g_static_private_counter++;
1.133 + g_static_mutex_unlock (&test_g_static_private_mutex);
1.134 + return g_new (guint,1);
1.135 +}
1.136 +
1.137 +static void
1.138 +test_g_static_private_destructor (gpointer data)
1.139 +{
1.140 + g_static_mutex_lock (&test_g_static_private_mutex);
1.141 + test_g_static_private_counter--;
1.142 + g_static_mutex_unlock (&test_g_static_private_mutex);
1.143 + g_free (data);
1.144 +}
1.145 +
1.146 +
1.147 +static gpointer
1.148 +test_g_static_private_thread (gpointer data)
1.149 +{
1.150 + guint number = GPOINTER_TO_INT (data);
1.151 + guint i;
1.152 + guint *private1, *private2;
1.153 + for (i = 0; i < 10; i++)
1.154 + {
1.155 + number = number * 11 + 1; /* A very simple and bad RNG ;-) */
1.156 + private1 = g_static_private_get (&test_g_static_private_private1);
1.157 + if (!private1 || number % 7 > 3)
1.158 + {
1.159 + private1 = test_g_static_private_constructor ();
1.160 + g_static_private_set (&test_g_static_private_private1, private1,
1.161 + test_g_static_private_destructor);
1.162 + }
1.163 + *private1 = number;
1.164 + private2 = g_static_private_get (&test_g_static_private_private2);
1.165 + if (!private2 || number % 13 > 5)
1.166 + {
1.167 + private2 = test_g_static_private_constructor ();
1.168 + g_static_private_set (&test_g_static_private_private2, private2,
1.169 + test_g_static_private_destructor);
1.170 + }
1.171 + *private2 = number * 2;
1.172 + g_usleep (G_USEC_PER_SEC / 5);
1.173 + g_assert (number == *private1);
1.174 + g_assert (number * 2 == *private2);
1.175 + }
1.176 + g_static_mutex_lock (&test_g_static_private_mutex);
1.177 + test_g_static_private_ready++;
1.178 + g_static_mutex_unlock (&test_g_static_private_mutex);
1.179 +
1.180 + /* Busy wait is not nice but that's just a test */
1.181 + while (test_g_static_private_ready != 0)
1.182 + g_usleep (G_USEC_PER_SEC / 5);
1.183 +
1.184 + for (i = 0; i < 10; i++)
1.185 + {
1.186 + private2 = g_static_private_get (&test_g_static_private_private2);
1.187 + number = number * 11 + 1; /* A very simple and bad RNG ;-) */
1.188 + if (!private2 || number % 13 > 5)
1.189 + {
1.190 + private2 = test_g_static_private_constructor ();
1.191 + g_static_private_set (&test_g_static_private_private2, private2,
1.192 + test_g_static_private_destructor);
1.193 + }
1.194 + *private2 = number * 2;
1.195 + g_usleep (G_USEC_PER_SEC / 5);
1.196 + g_assert (number * 2 == *private2);
1.197 + }
1.198 +
1.199 + return GINT_TO_POINTER (GPOINTER_TO_INT (data) * 3);
1.200 +}
1.201 +
1.202 +static void
1.203 +test_g_static_private (void)
1.204 +{
1.205 + GThread *threads[THREADS];
1.206 + guint i;
1.207 +
1.208 + test_g_static_private_ready = 0;
1.209 +
1.210 + for (i = 0; i < THREADS; i++)
1.211 + {
1.212 + threads[i] = g_thread_create (test_g_static_private_thread,
1.213 + GINT_TO_POINTER (i), TRUE, NULL);
1.214 + }
1.215 +
1.216 + /* Busy wait is not nice but that's just a test */
1.217 + while (test_g_static_private_ready != THREADS)
1.218 + g_usleep (G_USEC_PER_SEC / 5);
1.219 +
1.220 + /* Reuse the static private */
1.221 + g_static_private_free (&test_g_static_private_private2);
1.222 + g_static_private_init (&test_g_static_private_private2);
1.223 +
1.224 + test_g_static_private_ready = 0;
1.225 +
1.226 + for (i = 0; i < THREADS; i++)
1.227 + g_assert (GPOINTER_TO_INT (g_thread_join (threads[i])) == i * 3);
1.228 +
1.229 + g_assert (test_g_static_private_counter == 0);
1.230 +}
1.231 +
1.232 +/* GStaticRWLock */
1.233 +
1.234 +/* -1 = writing; >0 = # of readers */
1.235 +static gint test_g_static_rw_lock_state = 0;
1.236 +G_LOCK_DEFINE (test_g_static_rw_lock_state);
1.237 +
1.238 +static gboolean test_g_static_rw_lock_run = TRUE;
1.239 +static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
1.240 +
1.241 +static gpointer
1.242 +test_g_static_rw_lock_thread (gpointer data)
1.243 +{
1.244 + while (test_g_static_rw_lock_run)
1.245 + {
1.246 + if (g_random_double() > .2) /* I'm a reader */
1.247 + {
1.248 +
1.249 + if (g_random_double() > .2) /* I'll block */
1.250 + g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
1.251 + else /* I'll only try */
1.252 + if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
1.253 + continue;
1.254 + G_LOCK (test_g_static_rw_lock_state);
1.255 + g_assert (test_g_static_rw_lock_state >= 0);
1.256 + test_g_static_rw_lock_state++;
1.257 + G_UNLOCK (test_g_static_rw_lock_state);
1.258 +
1.259 + g_usleep (g_random_int_range (20,1000));
1.260 +
1.261 + G_LOCK (test_g_static_rw_lock_state);
1.262 + test_g_static_rw_lock_state--;
1.263 + G_UNLOCK (test_g_static_rw_lock_state);
1.264 +
1.265 + g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
1.266 + }
1.267 + else /* I'm a writer */
1.268 + {
1.269 +
1.270 + if (g_random_double() > .2) /* I'll block */
1.271 + g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
1.272 + else /* I'll only try */
1.273 + if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
1.274 + continue;
1.275 + G_LOCK (test_g_static_rw_lock_state);
1.276 + g_assert (test_g_static_rw_lock_state == 0);
1.277 + test_g_static_rw_lock_state = -1;
1.278 + G_UNLOCK (test_g_static_rw_lock_state);
1.279 +
1.280 + g_usleep (g_random_int_range (20,1000));
1.281 +
1.282 + G_LOCK (test_g_static_rw_lock_state);
1.283 + test_g_static_rw_lock_state = 0;
1.284 + G_UNLOCK (test_g_static_rw_lock_state);
1.285 +
1.286 + g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
1.287 + }
1.288 + }
1.289 + return NULL;
1.290 +}
1.291 +
1.292 +static void
1.293 +test_g_static_rw_lock ()
1.294 +{
1.295 + GThread *threads[THREADS];
1.296 + guint i;
1.297 + for (i = 0; i < THREADS; i++)
1.298 + {
1.299 + threads[i] = g_thread_create (test_g_static_rw_lock_thread,
1.300 + NULL, TRUE, NULL);
1.301 + }
1.302 + g_usleep (G_USEC_PER_SEC * 5);
1.303 + test_g_static_rw_lock_run = FALSE;
1.304 + for (i = 0; i < THREADS; i++)
1.305 + {
1.306 + g_thread_join (threads[i]);
1.307 + }
1.308 + g_assert (test_g_static_rw_lock_state == 0);
1.309 +}
1.310 +
1.311 +#define G_ONCE_SIZE 100
1.312 +#define G_ONCE_THREADS 10
1.313 +
1.314 +G_LOCK_DEFINE (test_g_once);
1.315 +static guint test_g_once_guint_array[G_ONCE_SIZE];
1.316 +static GOnce test_g_once_array[G_ONCE_SIZE];
1.317 +
1.318 +static gpointer
1.319 +test_g_once_init_func(gpointer arg)
1.320 +{
1.321 + guint *count = arg;
1.322 + g_usleep (g_random_int_range (20,1000));
1.323 + (*count)++;
1.324 + g_usleep (g_random_int_range (20,1000));
1.325 + return arg;
1.326 +}
1.327 +
1.328 +static gpointer
1.329 +test_g_once_thread (gpointer ignore)
1.330 +{
1.331 + guint i;
1.332 + G_LOCK (test_g_once);
1.333 + /* Don't start before all threads are created */
1.334 + G_UNLOCK (test_g_once);
1.335 + for (i = 0; i < 1000; i++)
1.336 + {
1.337 + guint pos = g_random_int_range (0, G_ONCE_SIZE);
1.338 + gpointer ret = g_once (test_g_once_array + pos, test_g_once_init_func,
1.339 + test_g_once_guint_array + pos);
1.340 + g_assert (ret == test_g_once_guint_array + pos);
1.341 + }
1.342 +
1.343 + /* Make sure, that all counters are touched at least once */
1.344 + for (i = 0; i < G_ONCE_SIZE; i++)
1.345 + {
1.346 + gpointer ret = g_once (test_g_once_array + i, test_g_once_init_func,
1.347 + test_g_once_guint_array + i);
1.348 + g_assert (ret == test_g_once_guint_array + i);
1.349 + }
1.350 +
1.351 + return NULL;
1.352 +}
1.353 +
1.354 +static void
1.355 +test_g_thread_once (void)
1.356 +{
1.357 + static GOnce once_init = G_ONCE_INIT;
1.358 + GThread *threads[G_ONCE_THREADS];
1.359 + guint i;
1.360 + for (i = 0; i < G_ONCE_SIZE; i++)
1.361 + {
1.362 + test_g_once_array[i] = once_init;
1.363 + test_g_once_guint_array[i] = i;
1.364 + }
1.365 + G_LOCK (test_g_once);
1.366 + for (i = 0; i < G_ONCE_THREADS; i++)
1.367 + {
1.368 + threads[i] = g_thread_create (test_g_once_thread, GUINT_TO_POINTER(i%2),
1.369 + TRUE, NULL);
1.370 + }
1.371 + G_UNLOCK (test_g_once);
1.372 + for (i = 0; i < G_ONCE_THREADS; i++)
1.373 + {
1.374 + g_thread_join (threads[i]);
1.375 + }
1.376 +
1.377 + for (i = 0; i < G_ONCE_SIZE; i++)
1.378 + {
1.379 + g_assert (test_g_once_guint_array[i] == i + 1);
1.380 + }
1.381 +}
1.382 +
1.383 +/* run all the tests */
1.384 +void
1.385 +run_all_tests()
1.386 +{
1.387 + test_g_mutex ();
1.388 + test_g_static_rec_mutex ();
1.389 + test_g_static_private ();
1.390 + test_g_static_rw_lock ();
1.391 + test_g_thread_once ();
1.392 +}
1.393 +
1.394 +int
1.395 +main (int argc,
1.396 + char *argv[])
1.397 +{
1.398 + #ifdef SYMBIAN
1.399 +
1.400 + 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.401 + g_set_print_handler(mrtPrintHandler);
1.402 + #endif /*SYMBIAN*/
1.403 +
1.404 +
1.405 + /* Only run the test, if threads are enabled and a default thread
1.406 + implementation is available */
1.407 +#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
1.408 + g_thread_init (NULL);
1.409 + run_all_tests ();
1.410 +
1.411 + /* Now we rerun all tests, but this time we fool the system into
1.412 + * thinking, that the available thread system is not native, but
1.413 + * userprovided. */
1.414 +
1.415 + g_thread_use_default_impl = FALSE;
1.416 + run_all_tests ();
1.417 +
1.418 +#endif
1.419 +
1.420 +#ifdef SYMBIAN
1.421 + testResultXml("thread-test");
1.422 +#endif /* EMULATOR */
1.423 + return 0;
1.424 +}