3 #include <glib-object.h>
5 #define G_TYPE_TEST (my_test_get_type ())
6 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
7 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
8 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
9 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
10 #define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
17 typedef struct _GTest GTest;
18 typedef struct _GTestClass GTestClass;
31 GObjectClass parent_class;
34 static GType my_test_get_type (void);
35 static volatile gboolean stopping;
37 static void my_test_class_init (GTestClass * klass);
38 static void my_test_init (GTest * test);
39 static void my_test_dispose (GObject * object);
40 static void my_test_get_property (GObject *object,
44 static void my_test_set_property (GObject *object,
49 static GObjectClass *parent_class = NULL;
52 my_test_get_type (void)
54 static GType test_type = 0;
57 const GTypeInfo test_info = {
61 (GClassInitFunc) my_test_class_init,
66 (GInstanceInitFunc) my_test_init,
70 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest", &test_info, 0);
76 my_test_class_init (GTestClass * klass)
78 GObjectClass *gobject_class;
80 gobject_class = (GObjectClass *) klass;
82 parent_class = g_type_class_ref (G_TYPE_OBJECT);
84 gobject_class->dispose = my_test_dispose;
85 gobject_class->get_property = my_test_get_property;
86 gobject_class->set_property = my_test_set_property;
88 g_object_class_install_property (gobject_class,
90 g_param_spec_int ("dummy",
98 my_test_init (GTest * test)
100 static guint static_id = 1;
101 test->id = static_id++;
105 my_test_dispose (GObject * object)
107 G_OBJECT_CLASS (parent_class)->dispose (object);
111 my_test_get_property (GObject *object,
118 test = MY_TEST (object);
123 g_value_set_int (value, test->dummy);
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132 my_test_set_property (GObject *object,
139 test = MY_TEST (object);
144 test->dummy = g_value_get_int (value);
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153 dummy_notify (GObject *object,
158 test = MY_TEST (object);
164 my_test_do_property (GTest * test)
168 g_object_get (test, "dummy", &dummy, NULL);
169 g_object_set (test, "dummy", dummy + 1, NULL);
173 run_thread (GTest * test)
178 my_test_do_property (test);
179 if ((i++ % 10000) == 0)
181 g_print (".%c", 'a' + test->id);
182 g_thread_yield(); /* force context switch */
190 main (int argc, char **argv)
193 GArray *test_objects;
194 GArray *test_threads;
195 const gint n_threads = 5;
197 g_thread_init (NULL);
198 g_print ("START: %s\n", argv[0]);
199 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
202 test_objects = g_array_new (FALSE, FALSE, sizeof (GTest *));
204 for (i = 0; i < n_threads; i++) {
207 test = g_object_new (G_TYPE_TEST, NULL);
208 g_array_append_val (test_objects, test);
210 g_assert (test->count == test->dummy);
211 g_signal_connect (test, "notify::dummy", G_CALLBACK (dummy_notify), NULL);
214 test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
218 for (i = 0; i < n_threads; i++) {
222 test = g_array_index (test_objects, GTest *, i);
224 thread = g_thread_create ((GThreadFunc) run_thread, test, TRUE, NULL);
225 g_array_append_val (test_threads, thread);
230 g_print ("\nstopping\n");
232 /* join all threads */
233 for (i = 0; i < n_threads; i++) {
236 thread = g_array_index (test_threads, GThread *, i);
237 g_thread_join (thread);
240 g_print ("stopped\n");
242 for (i = 0; i < n_threads; i++) {
245 test = g_array_index (test_objects, GTest *, i);
247 g_assert (test->count == test->dummy);