1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Imendio AB
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.
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.
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.
23 #include <glib-object.h>
25 /* This test tests the macros for defining dynamic types.
28 static GMutex *sync_mutex = NULL;
29 static gboolean loaded = FALSE;
32 typedef struct _TestModule TestModule;
33 typedef struct _TestModuleClass TestModuleClass;
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);
45 GTypeModule parent_instance;
47 TestModuleRegisterFunc register_func;
50 struct _TestModuleClass
52 GTypeModuleClass parent_class;
55 static GType test_module_get_type (void);
58 test_module_load (GTypeModule *module)
60 TestModule *test_module = TEST_MODULE (module);
62 test_module->register_func (module);
68 test_module_unload (GTypeModule *module)
73 test_module_class_init (TestModuleClass *class)
75 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
77 module_class->load = test_module_load;
78 module_class->unload = test_module_unload;
81 static GType test_module_get_type (void)
83 static GType object_type = 0;
86 static const GTypeInfo object_info =
88 sizeof (TestModuleClass),
90 (GBaseFinalizeFunc) NULL,
91 (GClassInitFunc) test_module_class_init,
92 (GClassFinalizeFunc) NULL,
96 (GInstanceInitFunc)NULL
98 object_type = g_type_register_static (G_TYPE_TYPE_MODULE, "TestModule", &object_info, 0);
105 test_module_new (TestModuleRegisterFunc register_func)
107 TestModule *test_module = g_object_new (TEST_TYPE_MODULE, NULL);
108 GTypeModule *module = G_TYPE_MODULE (test_module);
110 test_module->register_func = register_func;
112 /* Register the types initially */
113 g_type_module_use (module);
114 g_type_module_unuse (module);
116 return G_TYPE_MODULE (module);
121 #define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
123 typedef GObject DynamicObject;
124 typedef struct _DynamicObjectClass DynamicObjectClass;
126 struct _DynamicObjectClass
128 GObjectClass parent_class;
132 G_DEFINE_DYNAMIC_TYPE(DynamicObject, dynamic_object, G_TYPE_OBJECT);
135 dynamic_object_class_init (DynamicObjectClass *class)
138 g_assert (loaded == FALSE);
143 dynamic_object_class_finalize (DynamicObjectClass *class)
145 g_assert (loaded == TRUE);
150 dynamic_object_init (DynamicObject *dynamic_object)
156 module_register (GTypeModule *module)
158 dynamic_object_register_type (module);
161 #define N_THREADS 100
165 ref_unref_thread (gpointer data)
168 /* first, syncronize with other threads,
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");
177 /* ref/unref the klass 10000000 times */
178 for (i = N_REFS; i; i--) {
179 if (g_test_verbose ())
182 g_type_class_unref (g_type_class_ref ((GType) data));
185 if (g_test_verbose())
186 g_print ("DONE !\n");
192 test_multithreaded_dynamic_type_init (void)
195 DynamicObjectClass *class;
196 /* Create N_THREADS threads that are going to just ref/unref a class */
197 GThread *threads[N_THREADS];
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);
206 /* pause newly created threads */
207 g_mutex_lock (sync_mutex);
210 for (i = 0; i < N_THREADS; i++) {
211 threads[i] = g_thread_create (ref_unref_thread, (gpointer) DYNAMIC_OBJECT_TYPE, TRUE, NULL);
214 /* execute threads */
215 g_mutex_unlock (sync_mutex);
217 for (i = 0; i < N_THREADS; i++) {
218 g_thread_join (threads[i]);
226 g_thread_init (NULL);
227 g_test_init (&argc, &argv, NULL);
230 sync_mutex = g_mutex_new();
232 g_test_add_func ("/GObject/threaded-dynamic-ref-unref-init", test_multithreaded_dynamic_type_init);