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