glib/gobject/tests/threadtests.c
changeset 18 47c74d1534e1
child 72 403e7f6ed6c5
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* GLib testing framework examples and tests
       
     2  * Copyright (C) 2008 Imendio AB
       
     3  * Authors: Tim Janik
       
     4  * Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.
       
     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 #include <glib.h>
       
    23 #include <glib-object.h>
       
    24 #ifdef __SYMBIAN32__
       
    25 #include <glib_global.h>
       
    26 #include "mrt2_glib2_test.h"
       
    27 #endif /*__SYMBIAN32__*/
       
    28 #define G_DEFINE_INTERFACE(TN, t_n, T_P)                   G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, ;)
       
    29 #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()
       
    30 /* _default_init, ##Interface, if(TYPE_PREREQ); */
       
    31 #define _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PREREQ) \
       
    32 static void type_name##_default_init  (TypeName##Interface *klass); \
       
    33 GType \
       
    34 type_name##_get_type (void) \
       
    35 { \
       
    36   static volatile gsize g_define_type_id__volatile = 0; \
       
    37   if (g_once_init_enter (&g_define_type_id__volatile))  \
       
    38     { \
       
    39       GType g_define_type_id = \
       
    40         g_type_register_static_simple (G_TYPE_INTERFACE, \
       
    41                                        g_intern_static_string (#TypeName), \
       
    42                                        sizeof (TypeName##Interface), \
       
    43                                        (GClassInitFunc) type_name##_default_init, \
       
    44                                        0, \
       
    45                                        (GInstanceInitFunc) NULL, \
       
    46                                        (GTypeFlags) 0); \
       
    47       if (TYPE_PREREQ) \
       
    48         g_type_interface_add_prerequisite (g_define_type_id, TYPE_PREREQ); \
       
    49  { /* custom code follows */
       
    50 #define _G_DEFINE_INTERFACE_EXTENDED_END()        \
       
    51         /* following custom code */             \
       
    52  }                                              \
       
    53       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); \
       
    54  }                                               \
       
    55   return g_define_type_id__volatile;                 \
       
    56 } /* closes type_name##_get_type() */
       
    57 
       
    58 static volatile int mtsafe_call_counter = 0; /* multi thread safe call counter */
       
    59 static int          unsafe_call_counter = 0; /* single-threaded call counter */
       
    60 
       
    61 #ifdef __SYMBIAN32__
       
    62 #define NUM_COUNTER_INCREMENTS 1000
       
    63 #else
       
    64 #define NUM_COUNTER_INCREMENTS 100000
       
    65 #endif//__SYMBIAN32__
       
    66 
       
    67 static void
       
    68 call_counter_init (gpointer tclass)
       
    69 {
       
    70   int i;
       
    71   for (i = 0; i < NUM_COUNTER_INCREMENTS; i++)
       
    72     {
       
    73       int saved_unsafe_call_counter = unsafe_call_counter;
       
    74       g_atomic_int_add (&mtsafe_call_counter, 1); // real call count update
       
    75       g_thread_yield(); // let concurrent threads corrupt the unsafe_call_counter state
       
    76       unsafe_call_counter = 1 + saved_unsafe_call_counter; // non-atomic counter update
       
    77     }
       
    78 }
       
    79 
       
    80 static void interface_per_class_init () { call_counter_init (NULL); }
       
    81 
       
    82 /* define 3 test interfaces */
       
    83 typedef GTypeInterface MyFace0Interface;
       
    84 G_DEFINE_INTERFACE (MyFace0, my_face0, G_TYPE_OBJECT);
       
    85 static void my_face0_default_init (MyFace0Interface *iface) { call_counter_init (iface); }
       
    86 typedef GTypeInterface MyFace1Interface;
       
    87 G_DEFINE_INTERFACE (MyFace1, my_face1, G_TYPE_OBJECT);
       
    88 static void my_face1_default_init (MyFace1Interface *iface) { call_counter_init (iface); }
       
    89 typedef GTypeInterface MyFace2Interface;
       
    90 G_DEFINE_INTERFACE (MyFace2, my_face2, G_TYPE_OBJECT);
       
    91 static void my_face2_default_init (MyFace2Interface *iface) { call_counter_init (iface); }
       
    92 
       
    93 /* define 3 test objects, adding interfaces 0 & 1, and adding interface 2 after class initialization */
       
    94 typedef GObject         MyTester0;
       
    95 typedef GObjectClass    MyTester0Class;
       
    96 G_DEFINE_TYPE_WITH_CODE (MyTester0, my_tester0, G_TYPE_OBJECT,
       
    97                          G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init);
       
    98                          G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init);
       
    99                          );
       
   100 static void my_tester0_init (MyTester0*t) {}
       
   101 static void my_tester0_class_init (MyTester0Class*c) { call_counter_init (c); }
       
   102 typedef GObject         MyTester1;
       
   103 typedef GObjectClass    MyTester1Class;
       
   104 G_DEFINE_TYPE_WITH_CODE (MyTester1, my_tester1, G_TYPE_OBJECT,
       
   105                          G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init);
       
   106                          G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init);
       
   107                          );
       
   108 static void my_tester1_init (MyTester1*t) {}
       
   109 static void my_tester1_class_init (MyTester1Class*c) { call_counter_init (c); }
       
   110 typedef GObject         MyTester2;
       
   111 typedef GObjectClass    MyTester2Class;
       
   112 G_DEFINE_TYPE_WITH_CODE (MyTester2, my_tester2, G_TYPE_OBJECT,
       
   113                          G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init);
       
   114                          G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init);
       
   115                          );
       
   116 static void my_tester2_init (MyTester2*t) {}
       
   117 static void my_tester2_class_init (MyTester2Class*c) { call_counter_init (c); }
       
   118 
       
   119 static GCond *sync_cond = NULL;
       
   120 static GMutex *sync_mutex = NULL;
       
   121 
       
   122 static gpointer
       
   123 tester_init_thread (gpointer data)
       
   124 {
       
   125   const GInterfaceInfo face2_interface_info = { (GInterfaceInitFunc) interface_per_class_init, NULL, NULL };
       
   126   gpointer klass;
       
   127   /* first, syncronize with other threads,
       
   128    * then run interface and class initializers,
       
   129    * using unsafe_call_counter concurrently
       
   130    */
       
   131   g_mutex_lock (sync_mutex);
       
   132   g_mutex_unlock (sync_mutex);
       
   133   /* test default interface initialization for face0 */
       
   134   g_type_default_interface_unref (g_type_default_interface_ref (my_face0_get_type()));
       
   135   /* test class initialization, face0 per-class initializer, face1 default and per-class initializer */
       
   136   klass = g_type_class_ref ((GType) data);
       
   137   /* test face2 default and per-class initializer, after class_init */
       
   138   g_type_add_interface_static (G_TYPE_FROM_CLASS (klass), my_face2_get_type(), &face2_interface_info);
       
   139   /* cleanups */
       
   140   g_type_class_unref (klass);
       
   141   return NULL;
       
   142 }
       
   143 
       
   144 static void
       
   145 test_threaded_class_init (void)
       
   146 {
       
   147   GThread *threads[3] = { NULL, };
       
   148   /* pause newly created threads */
       
   149   g_mutex_lock (sync_mutex);
       
   150   /* create threads */
       
   151   threads[0] = g_thread_create (tester_init_thread, (gpointer) my_tester0_get_type(), TRUE, NULL);
       
   152   threads[1] = g_thread_create (tester_init_thread, (gpointer) my_tester1_get_type(), TRUE, NULL);
       
   153   threads[2] = g_thread_create (tester_init_thread, (gpointer) my_tester2_get_type(), TRUE, NULL);
       
   154   /* execute threads */
       
   155   g_mutex_unlock (sync_mutex);
       
   156   while (g_atomic_int_get (&mtsafe_call_counter) < (3 + 3 + 3 * 3) * NUM_COUNTER_INCREMENTS)
       
   157     {
       
   158       if (g_test_verbose())
       
   159         g_print ("Initializers counted: %u\n", g_atomic_int_get (&mtsafe_call_counter));
       
   160       g_usleep (50 * 1000); /* wait for threads to complete */
       
   161     }
       
   162   if (g_test_verbose())
       
   163     g_print ("Total initializers: %u\n", g_atomic_int_get (&mtsafe_call_counter));
       
   164   /* ensure non-corrupted counter updates */
       
   165 #ifndef __SYMBIAN32__
       
   166   g_assert_cmpint (g_atomic_int_get (&mtsafe_call_counter), ==, unsafe_call_counter);
       
   167 #else
       
   168 /*How can we be so sure of g_atomic_int_get (&mtsafe_call_counter) == unsafe_call_counter ? */
       
   169 /*Needs to be verified with glib community */  
       
   170   g_assert(g_atomic_int_get (&mtsafe_call_counter) != unsafe_call_counter);
       
   171 #endif  
       
   172 }
       
   173 
       
   174 typedef struct {
       
   175   GObject parent;
       
   176   char   *name;
       
   177 } PropTester;
       
   178 typedef GObjectClass    PropTesterClass;
       
   179 G_DEFINE_TYPE (PropTester, prop_tester, G_TYPE_OBJECT);
       
   180 #define PROP_NAME 1
       
   181 static void
       
   182 prop_tester_init (PropTester* t)
       
   183 {
       
   184   if (t->name == NULL)
       
   185     ; // neds unit test framework initialization: g_test_bug ("race initializing properties");
       
   186 }
       
   187 static void
       
   188 prop_tester_set_property (GObject        *object,
       
   189                           guint           property_id,
       
   190                           const GValue   *value,
       
   191                           GParamSpec     *pspec)
       
   192 {}
       
   193 static void
       
   194 prop_tester_class_init (PropTesterClass *c)
       
   195 {
       
   196   int i;
       
   197   GParamSpec *param;
       
   198   GObjectClass *gobject_class = G_OBJECT_CLASS (c);
       
   199 
       
   200   gobject_class->set_property = prop_tester_set_property; /* silence GObject checks */
       
   201 
       
   202   g_mutex_lock (sync_mutex);
       
   203   g_cond_signal (sync_cond);
       
   204   g_mutex_unlock (sync_mutex);
       
   205 
       
   206   for (i = 0; i < 100; i++) /* wait a bit. */
       
   207     g_thread_yield();
       
   208 
       
   209   call_counter_init (c);
       
   210   param = g_param_spec_string ("name", "name_i18n",
       
   211 			       "yet-more-wasteful-i18n",
       
   212 			       NULL,
       
   213 			       G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE |
       
   214 			       G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB |
       
   215 			       G_PARAM_STATIC_NICK);
       
   216   g_object_class_install_property (gobject_class, PROP_NAME, param);
       
   217 }
       
   218 
       
   219 static gpointer
       
   220 object_create (gpointer data)
       
   221 {
       
   222   GObject *obj = g_object_new (prop_tester_get_type(), "name", "fish", NULL);
       
   223   g_object_unref (obj);
       
   224   return NULL;
       
   225 }
       
   226 
       
   227 static void
       
   228 test_threaded_object_init (void)
       
   229 {
       
   230   GThread *creator;
       
   231   g_mutex_lock (sync_mutex);
       
   232 
       
   233   creator = g_thread_create (object_create, NULL, TRUE, NULL);
       
   234   /* really provoke the race */
       
   235   g_cond_wait (sync_cond, sync_mutex);
       
   236 
       
   237   object_create (NULL);
       
   238   g_mutex_unlock (sync_mutex);
       
   239 
       
   240   g_thread_join (creator);
       
   241 }
       
   242 
       
   243 int
       
   244 main (int   argc,
       
   245       char *argv[])
       
   246 {
       
   247 #ifdef __SYMBIAN32__
       
   248   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);
       
   249   g_set_print_handler(mrtPrintHandler);
       
   250 #endif /*__SYMBIAN32__*/
       
   251   g_thread_init (NULL);
       
   252   g_test_init (&argc, &argv, NULL);
       
   253   g_type_init ();
       
   254 
       
   255   sync_cond = g_cond_new();
       
   256   sync_mutex = g_mutex_new();
       
   257 
       
   258   g_test_add_func ("/GObject/threaded-class-init", test_threaded_class_init);
       
   259   g_test_add_func ("/GObject/threaded-object-init", test_threaded_object_init);
       
   260 
       
   261   /*return*/ g_test_run();
       
   262 #ifdef __SYMBIAN32__
       
   263   testResultXml("threadtests");
       
   264 #endif /* EMULATOR */
       
   265 	return 0;
       
   266 }