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))
14 typedef struct _GTest GTest;
15 typedef struct _GTestClass GTestClass;
26 GObjectClass parent_class;
28 void (*test_signal1) (GTest * test, gint an_int);
29 void (*test_signal2) (GTest * test, gint an_int);
32 static GType my_test_get_type (void);
33 static volatile gboolean stopping;
35 /* Element signals and args */
50 static void my_test_class_init (GTestClass * klass);
51 static void my_test_init (GTest * test);
52 static void my_test_dispose (GObject * object);
54 static void signal2_handler (GTest * test, gint anint);
56 static void my_test_set_property (GObject * object, guint prop_id,
57 const GValue * value, GParamSpec * pspec);
58 static void my_test_get_property (GObject * object, guint prop_id,
59 GValue * value, GParamSpec * pspec);
61 static GObjectClass *parent_class = NULL;
63 static guint my_test_signals[LAST_SIGNAL] = { 0 };
66 my_test_get_type (void)
68 static GType test_type = 0;
71 static const GTypeInfo test_info = {
75 (GClassInitFunc) my_test_class_init,
80 (GInstanceInitFunc) my_test_init,
86 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
93 my_test_class_init (GTestClass * klass)
95 GObjectClass *gobject_class;
97 gobject_class = (GObjectClass *) klass;
99 parent_class = g_type_class_ref (G_TYPE_OBJECT);
101 if (!g_thread_supported ())
102 g_thread_init (NULL);
104 gobject_class->dispose = my_test_dispose;
105 gobject_class->set_property = my_test_set_property;
106 gobject_class->get_property = my_test_get_property;
108 my_test_signals[TEST_SIGNAL1] =
109 g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass),
110 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GTestClass, test_signal1), NULL,
111 NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
112 my_test_signals[TEST_SIGNAL2] =
113 g_signal_new ("test-signal2", G_TYPE_FROM_CLASS (klass),
114 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GTestClass, test_signal2), NULL,
115 NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
117 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TEST_PROP,
118 g_param_spec_int ("test-prop", "Test Prop", "Test property",
119 0, 1, 0, G_PARAM_READWRITE));
121 klass->test_signal2 = signal2_handler;
125 my_test_init (GTest * test)
127 g_print ("init %p\n", test);
133 my_test_dispose (GObject * object)
137 test = MY_TEST (object);
139 g_print ("dispose %p!\n", object);
141 G_OBJECT_CLASS (parent_class)->dispose (object);
145 my_test_set_property (GObject * object, guint prop_id,
146 const GValue * value, GParamSpec * pspec)
150 test = MY_TEST (object);
154 test->value = g_value_get_int (value);
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163 my_test_get_property (GObject * object, guint prop_id,
164 GValue * value, GParamSpec * pspec)
168 test = MY_TEST (object);
172 g_value_set_int (value, test->value);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 my_test_do_signal1 (GTest * test)
183 g_signal_emit (G_OBJECT (test), my_test_signals[TEST_SIGNAL1], 0, 0);
187 signal2_handler (GTest * test, gint anint)
192 my_test_do_signal2 (GTest * test)
194 g_signal_emit (G_OBJECT (test), my_test_signals[TEST_SIGNAL2], 0, 0);
198 my_test_do_prop (GTest * test)
200 test->value = g_rand_int (rand);
201 g_object_notify (G_OBJECT (test), "test-prop");
205 run_thread (GTest * test)
211 my_test_do_signal1 (test);
213 my_test_do_signal2 (test);
215 my_test_do_prop (test);
216 if ((i++ % 10000) == 0) {
218 g_thread_yield(); /* force context switch */
226 notify (GObject *object, GParamSpec *spec, gpointer user_data)
230 g_object_get (object, "test-prop", &value, NULL);
231 /*g_print ("+ %d", value);*/
235 main (int argc, char **argv)
238 GTest *test1, *test2;
239 GArray *test_threads;
240 const gint n_threads = 1;
242 g_thread_init (NULL);
243 g_print ("START: %s\n", argv[0]);
244 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
247 test1 = g_object_new (G_TYPE_TEST, NULL);
248 test2 = g_object_new (G_TYPE_TEST, NULL);
250 g_signal_connect (test1, "notify::test-prop", G_CALLBACK (notify), NULL);
251 g_signal_connect (test1, "test-signal1", G_CALLBACK (notify), NULL);
252 g_signal_connect (test1, "test-signal2", G_CALLBACK (notify), NULL);
254 test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
258 for (i = 0; i < n_threads; i++) {
261 thread = g_thread_create ((GThreadFunc) run_thread, test1, TRUE, NULL);
262 g_array_append_val (test_threads, thread);
264 thread = g_thread_create ((GThreadFunc) run_thread, test2, TRUE, NULL);
265 g_array_append_val (test_threads, thread);
271 g_print ("\nstopping\n");
273 /* join all threads */
274 for (i = 0; i < 2 * n_threads; i++) {
277 thread = g_array_index (test_threads, GThread *, i);
278 g_thread_join (thread);
281 g_print ("stopped\n");