Initial commit
[platform/upstream/glib2.0.git] / gobject / tests / dynamictests.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 /* This test tests the macros for defining dynamic types.
26  */
27
28 static GMutex *sync_mutex = NULL;
29 static gboolean loaded = FALSE;
30
31 /* MODULE */
32 typedef struct _TestModule      TestModule;
33 typedef struct _TestModuleClass TestModuleClass;
34
35 #define TEST_TYPE_MODULE              (test_module_get_type ())
36 #define TEST_MODULE(module)           (G_TYPE_CHECK_INSTANCE_CAST ((module), TEST_TYPE_MODULE, TestModule))
37 #define TEST_MODULE_CLASS(class)      (G_TYPE_CHECK_CLASS_CAST ((class), TEST_TYPE_MODULE, TestModuleClass))
38 #define TEST_IS_MODULE(module)        (G_TYPE_CHECK_INSTANCE_TYPE ((module), TEST_TYPE_MODULE))
39 #define TEST_IS_MODULE_CLASS(class)   (G_TYPE_CHECK_CLASS_TYPE ((class), TEST_TYPE_MODULE))
40 #define TEST_MODULE_GET_CLASS(module) (G_TYPE_INSTANCE_GET_CLASS ((module), TEST_TYPE_MODULE, TestModuleClass))
41 typedef void (*TestModuleRegisterFunc) (GTypeModule *module);
42
43 struct _TestModule
44 {
45   GTypeModule parent_instance;
46
47   TestModuleRegisterFunc register_func;
48 };
49
50 struct _TestModuleClass
51 {
52   GTypeModuleClass parent_class;
53 };
54
55 static GType test_module_get_type (void);
56
57 static gboolean
58 test_module_load (GTypeModule *module)
59 {
60   TestModule *test_module = TEST_MODULE (module);
61
62   test_module->register_func (module);
63
64   return TRUE;
65 }
66
67 static void
68 test_module_unload (GTypeModule *module)
69 {
70 }
71
72 static void
73 test_module_class_init (TestModuleClass *class)
74 {
75   GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
76
77   module_class->load = test_module_load;
78   module_class->unload = test_module_unload;
79 }
80
81 static GType test_module_get_type (void)
82 {
83   static GType object_type = 0;
84
85   if (!object_type) {
86     static const GTypeInfo object_info =
87       {
88         sizeof (TestModuleClass),
89         (GBaseInitFunc) NULL,
90         (GBaseFinalizeFunc) NULL,
91         (GClassInitFunc) test_module_class_init,
92         (GClassFinalizeFunc) NULL,
93         NULL,
94         sizeof (TestModule),
95         0,
96         (GInstanceInitFunc)NULL
97       };
98     object_type = g_type_register_static (G_TYPE_TYPE_MODULE, "TestModule", &object_info, 0);
99   }
100   return object_type;
101 }
102
103
104 GTypeModule *
105 test_module_new (TestModuleRegisterFunc register_func)
106 {
107   TestModule *test_module = g_object_new (TEST_TYPE_MODULE, NULL);
108   GTypeModule *module = G_TYPE_MODULE (test_module);
109
110   test_module->register_func = register_func;
111
112   /* Register the types initially */
113   g_type_module_use (module);
114   g_type_module_unuse (module);
115
116   return G_TYPE_MODULE (module);
117 }
118
119
120
121 #define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
122
123 typedef GObject DynamicObject;
124 typedef struct _DynamicObjectClass DynamicObjectClass;
125
126 struct _DynamicObjectClass
127 {
128   GObjectClass parent_class;
129   guint val;
130 };
131
132 G_DEFINE_DYNAMIC_TYPE(DynamicObject, dynamic_object, G_TYPE_OBJECT);
133
134 static void
135 dynamic_object_class_init (DynamicObjectClass *class)
136 {
137   class->val = 42;
138   g_assert (loaded == FALSE);
139   loaded = TRUE;
140 }
141
142 static void
143 dynamic_object_class_finalize (DynamicObjectClass *class)
144 {
145   g_assert (loaded == TRUE);
146   loaded = FALSE;
147 }
148
149 static void
150 dynamic_object_init (DynamicObject *dynamic_object)
151 {
152 }
153
154
155 static void
156 module_register (GTypeModule *module)
157 {
158   dynamic_object_register_type (module);
159 }
160
161 #define N_THREADS 100
162 #define N_REFS 10000
163
164 static gpointer
165 ref_unref_thread (gpointer data)
166 {
167   gint i;
168   /* first, syncronize with other threads,
169    */
170   if (g_test_verbose())
171     g_print ("WAITING!\n");
172   g_mutex_lock (sync_mutex);
173   g_mutex_unlock (sync_mutex);
174   if (g_test_verbose ())
175     g_print ("STARTING\n");
176
177   /* ref/unref the klass 10000000 times */
178   for (i = N_REFS; i; i--) {
179     if (g_test_verbose ())
180       if (i % 10)
181         g_print ("%d\n", i);
182     g_type_class_unref (g_type_class_ref ((GType) data));
183   }
184
185   if (g_test_verbose())
186     g_print ("DONE !\n");
187
188   return NULL;
189 }
190
191 static void
192 test_multithreaded_dynamic_type_init (void)
193 {
194   GTypeModule *module;
195   DynamicObjectClass *class;
196   /* Create N_THREADS threads that are going to just ref/unref a class */
197   GThread *threads[N_THREADS];
198   guint i;
199
200   module = test_module_new (module_register);
201   /* Not loaded until we call ref for the first time */
202   class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
203   g_assert (class == NULL);
204   g_assert (!loaded);
205
206   /* pause newly created threads */
207   g_mutex_lock (sync_mutex);
208
209   /* create threads */
210   for (i = 0; i < N_THREADS; i++) {
211     threads[i] = g_thread_create (ref_unref_thread, (gpointer) DYNAMIC_OBJECT_TYPE, TRUE, NULL);
212   }
213
214   /* execute threads */
215   g_mutex_unlock (sync_mutex);
216
217   for (i = 0; i < N_THREADS; i++) {
218     g_thread_join (threads[i]);
219   }
220 }
221
222 int
223 main (int   argc,
224       char *argv[])
225 {
226   g_thread_init (NULL);
227   g_test_init (&argc, &argv, NULL);
228   g_type_init ();
229
230   sync_mutex = g_mutex_new();
231
232   g_test_add_func ("/GObject/threaded-dynamic-ref-unref-init", test_multithreaded_dynamic_type_init);
233
234   return g_test_run();
235 }