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