1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/gobject/tests/threadtests.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,266 @@
1.4 +/* GLib testing framework examples and tests
1.5 + * Copyright (C) 2008 Imendio AB
1.6 + * Authors: Tim Janik
1.7 + * Portions copyright (c) 2009 Nokia Corporation. All rights reserved.
1.8 + * This work is provided "as is"; redistribution and modification
1.9 + * in whole or in part, in any medium, physical or electronic is
1.10 + * permitted without restriction.
1.11 + *
1.12 + * This work is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1.15 + *
1.16 + * In no event shall the authors or contributors be liable for any
1.17 + * direct, indirect, incidental, special, exemplary, or consequential
1.18 + * damages (including, but not limited to, procurement of substitute
1.19 + * goods or services; loss of use, data, or profits; or business
1.20 + * interruption) however caused and on any theory of liability, whether
1.21 + * in contract, strict liability, or tort (including negligence or
1.22 + * otherwise) arising in any way out of the use of this software, even
1.23 + * if advised of the possibility of such damage.
1.24 + */
1.25 +#include <glib.h>
1.26 +#include <glib-object.h>
1.27 +#ifdef __SYMBIAN32__
1.28 +#include <glib_global.h>
1.29 +#include "mrt2_glib2_test.h"
1.30 +#endif /*__SYMBIAN32__*/
1.31 +#define G_DEFINE_INTERFACE(TN, t_n, T_P) G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, ;)
1.32 +#define G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, _C_) _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TN, t_n, T_P) {_C_;} _G_DEFINE_INTERFACE_EXTENDED_END()
1.33 +/* _default_init, ##Interface, if(TYPE_PREREQ); */
1.34 +#define _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PREREQ) \
1.35 +static void type_name##_default_init (TypeName##Interface *klass); \
1.36 +GType \
1.37 +type_name##_get_type (void) \
1.38 +{ \
1.39 + static volatile gsize g_define_type_id__volatile = 0; \
1.40 + if (g_once_init_enter (&g_define_type_id__volatile)) \
1.41 + { \
1.42 + GType g_define_type_id = \
1.43 + g_type_register_static_simple (G_TYPE_INTERFACE, \
1.44 + g_intern_static_string (#TypeName), \
1.45 + sizeof (TypeName##Interface), \
1.46 + (GClassInitFunc) type_name##_default_init, \
1.47 + 0, \
1.48 + (GInstanceInitFunc) NULL, \
1.49 + (GTypeFlags) 0); \
1.50 + if (TYPE_PREREQ) \
1.51 + g_type_interface_add_prerequisite (g_define_type_id, TYPE_PREREQ); \
1.52 + { /* custom code follows */
1.53 +#define _G_DEFINE_INTERFACE_EXTENDED_END() \
1.54 + /* following custom code */ \
1.55 + } \
1.56 + g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); \
1.57 + } \
1.58 + return g_define_type_id__volatile; \
1.59 +} /* closes type_name##_get_type() */
1.60 +
1.61 +static volatile int mtsafe_call_counter = 0; /* multi thread safe call counter */
1.62 +static int unsafe_call_counter = 0; /* single-threaded call counter */
1.63 +
1.64 +#ifdef __SYMBIAN32__
1.65 +#define NUM_COUNTER_INCREMENTS 1000
1.66 +#else
1.67 +#define NUM_COUNTER_INCREMENTS 100000
1.68 +#endif//__SYMBIAN32__
1.69 +
1.70 +static void
1.71 +call_counter_init (gpointer tclass)
1.72 +{
1.73 + int i;
1.74 + for (i = 0; i < NUM_COUNTER_INCREMENTS; i++)
1.75 + {
1.76 + int saved_unsafe_call_counter = unsafe_call_counter;
1.77 + g_atomic_int_add (&mtsafe_call_counter, 1); // real call count update
1.78 + g_thread_yield(); // let concurrent threads corrupt the unsafe_call_counter state
1.79 + unsafe_call_counter = 1 + saved_unsafe_call_counter; // non-atomic counter update
1.80 + }
1.81 +}
1.82 +
1.83 +static void interface_per_class_init () { call_counter_init (NULL); }
1.84 +
1.85 +/* define 3 test interfaces */
1.86 +typedef GTypeInterface MyFace0Interface;
1.87 +G_DEFINE_INTERFACE (MyFace0, my_face0, G_TYPE_OBJECT);
1.88 +static void my_face0_default_init (MyFace0Interface *iface) { call_counter_init (iface); }
1.89 +typedef GTypeInterface MyFace1Interface;
1.90 +G_DEFINE_INTERFACE (MyFace1, my_face1, G_TYPE_OBJECT);
1.91 +static void my_face1_default_init (MyFace1Interface *iface) { call_counter_init (iface); }
1.92 +typedef GTypeInterface MyFace2Interface;
1.93 +G_DEFINE_INTERFACE (MyFace2, my_face2, G_TYPE_OBJECT);
1.94 +static void my_face2_default_init (MyFace2Interface *iface) { call_counter_init (iface); }
1.95 +
1.96 +/* define 3 test objects, adding interfaces 0 & 1, and adding interface 2 after class initialization */
1.97 +typedef GObject MyTester0;
1.98 +typedef GObjectClass MyTester0Class;
1.99 +G_DEFINE_TYPE_WITH_CODE (MyTester0, my_tester0, G_TYPE_OBJECT,
1.100 + G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init);
1.101 + G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init);
1.102 + );
1.103 +static void my_tester0_init (MyTester0*t) {}
1.104 +static void my_tester0_class_init (MyTester0Class*c) { call_counter_init (c); }
1.105 +typedef GObject MyTester1;
1.106 +typedef GObjectClass MyTester1Class;
1.107 +G_DEFINE_TYPE_WITH_CODE (MyTester1, my_tester1, G_TYPE_OBJECT,
1.108 + G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init);
1.109 + G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init);
1.110 + );
1.111 +static void my_tester1_init (MyTester1*t) {}
1.112 +static void my_tester1_class_init (MyTester1Class*c) { call_counter_init (c); }
1.113 +typedef GObject MyTester2;
1.114 +typedef GObjectClass MyTester2Class;
1.115 +G_DEFINE_TYPE_WITH_CODE (MyTester2, my_tester2, G_TYPE_OBJECT,
1.116 + G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init);
1.117 + G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init);
1.118 + );
1.119 +static void my_tester2_init (MyTester2*t) {}
1.120 +static void my_tester2_class_init (MyTester2Class*c) { call_counter_init (c); }
1.121 +
1.122 +static GCond *sync_cond = NULL;
1.123 +static GMutex *sync_mutex = NULL;
1.124 +
1.125 +static gpointer
1.126 +tester_init_thread (gpointer data)
1.127 +{
1.128 + const GInterfaceInfo face2_interface_info = { (GInterfaceInitFunc) interface_per_class_init, NULL, NULL };
1.129 + gpointer klass;
1.130 + /* first, syncronize with other threads,
1.131 + * then run interface and class initializers,
1.132 + * using unsafe_call_counter concurrently
1.133 + */
1.134 + g_mutex_lock (sync_mutex);
1.135 + g_mutex_unlock (sync_mutex);
1.136 + /* test default interface initialization for face0 */
1.137 + g_type_default_interface_unref (g_type_default_interface_ref (my_face0_get_type()));
1.138 + /* test class initialization, face0 per-class initializer, face1 default and per-class initializer */
1.139 + klass = g_type_class_ref ((GType) data);
1.140 + /* test face2 default and per-class initializer, after class_init */
1.141 + g_type_add_interface_static (G_TYPE_FROM_CLASS (klass), my_face2_get_type(), &face2_interface_info);
1.142 + /* cleanups */
1.143 + g_type_class_unref (klass);
1.144 + return NULL;
1.145 +}
1.146 +
1.147 +static void
1.148 +test_threaded_class_init (void)
1.149 +{
1.150 + GThread *threads[3] = { NULL, };
1.151 + /* pause newly created threads */
1.152 + g_mutex_lock (sync_mutex);
1.153 + /* create threads */
1.154 + threads[0] = g_thread_create (tester_init_thread, (gpointer) my_tester0_get_type(), TRUE, NULL);
1.155 + threads[1] = g_thread_create (tester_init_thread, (gpointer) my_tester1_get_type(), TRUE, NULL);
1.156 + threads[2] = g_thread_create (tester_init_thread, (gpointer) my_tester2_get_type(), TRUE, NULL);
1.157 + /* execute threads */
1.158 + g_mutex_unlock (sync_mutex);
1.159 + while (g_atomic_int_get (&mtsafe_call_counter) < (3 + 3 + 3 * 3) * NUM_COUNTER_INCREMENTS)
1.160 + {
1.161 + if (g_test_verbose())
1.162 + g_print ("Initializers counted: %u\n", g_atomic_int_get (&mtsafe_call_counter));
1.163 + g_usleep (50 * 1000); /* wait for threads to complete */
1.164 + }
1.165 + if (g_test_verbose())
1.166 + g_print ("Total initializers: %u\n", g_atomic_int_get (&mtsafe_call_counter));
1.167 + /* ensure non-corrupted counter updates */
1.168 +#ifndef __SYMBIAN32__
1.169 + g_assert_cmpint (g_atomic_int_get (&mtsafe_call_counter), ==, unsafe_call_counter);
1.170 +#else
1.171 +/*How can we be so sure of g_atomic_int_get (&mtsafe_call_counter) == unsafe_call_counter ? */
1.172 +/*Needs to be verified with glib community */
1.173 + g_assert(g_atomic_int_get (&mtsafe_call_counter) != unsafe_call_counter);
1.174 +#endif
1.175 +}
1.176 +
1.177 +typedef struct {
1.178 + GObject parent;
1.179 + char *name;
1.180 +} PropTester;
1.181 +typedef GObjectClass PropTesterClass;
1.182 +G_DEFINE_TYPE (PropTester, prop_tester, G_TYPE_OBJECT);
1.183 +#define PROP_NAME 1
1.184 +static void
1.185 +prop_tester_init (PropTester* t)
1.186 +{
1.187 + if (t->name == NULL)
1.188 + ; // neds unit test framework initialization: g_test_bug ("race initializing properties");
1.189 +}
1.190 +static void
1.191 +prop_tester_set_property (GObject *object,
1.192 + guint property_id,
1.193 + const GValue *value,
1.194 + GParamSpec *pspec)
1.195 +{}
1.196 +static void
1.197 +prop_tester_class_init (PropTesterClass *c)
1.198 +{
1.199 + int i;
1.200 + GParamSpec *param;
1.201 + GObjectClass *gobject_class = G_OBJECT_CLASS (c);
1.202 +
1.203 + gobject_class->set_property = prop_tester_set_property; /* silence GObject checks */
1.204 +
1.205 + g_mutex_lock (sync_mutex);
1.206 + g_cond_signal (sync_cond);
1.207 + g_mutex_unlock (sync_mutex);
1.208 +
1.209 + for (i = 0; i < 100; i++) /* wait a bit. */
1.210 + g_thread_yield();
1.211 +
1.212 + call_counter_init (c);
1.213 + param = g_param_spec_string ("name", "name_i18n",
1.214 + "yet-more-wasteful-i18n",
1.215 + NULL,
1.216 + G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE |
1.217 + G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB |
1.218 + G_PARAM_STATIC_NICK);
1.219 + g_object_class_install_property (gobject_class, PROP_NAME, param);
1.220 +}
1.221 +
1.222 +static gpointer
1.223 +object_create (gpointer data)
1.224 +{
1.225 + GObject *obj = g_object_new (prop_tester_get_type(), "name", "fish", NULL);
1.226 + g_object_unref (obj);
1.227 + return NULL;
1.228 +}
1.229 +
1.230 +static void
1.231 +test_threaded_object_init (void)
1.232 +{
1.233 + GThread *creator;
1.234 + g_mutex_lock (sync_mutex);
1.235 +
1.236 + creator = g_thread_create (object_create, NULL, TRUE, NULL);
1.237 + /* really provoke the race */
1.238 + g_cond_wait (sync_cond, sync_mutex);
1.239 +
1.240 + object_create (NULL);
1.241 + g_mutex_unlock (sync_mutex);
1.242 +
1.243 + g_thread_join (creator);
1.244 +}
1.245 +
1.246 +int
1.247 +main (int argc,
1.248 + char *argv[])
1.249 +{
1.250 +#ifdef __SYMBIAN32__
1.251 + 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.252 + g_set_print_handler(mrtPrintHandler);
1.253 +#endif /*__SYMBIAN32__*/
1.254 + g_thread_init (NULL);
1.255 + g_test_init (&argc, &argv, NULL);
1.256 + g_type_init ();
1.257 +
1.258 + sync_cond = g_cond_new();
1.259 + sync_mutex = g_mutex_new();
1.260 +
1.261 + g_test_add_func ("/GObject/threaded-class-init", test_threaded_class_init);
1.262 + g_test_add_func ("/GObject/threaded-object-init", test_threaded_object_init);
1.263 +
1.264 + /*return*/ g_test_run();
1.265 +#ifdef __SYMBIAN32__
1.266 + testResultXml("threadtests");
1.267 +#endif /* EMULATOR */
1.268 + return 0;
1.269 +}