2 #include <glib-object.h>
8 #define G_TYPE_TEST (my_test_get_type ())
9 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
10 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
11 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
12 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
13 #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
;
29 GObjectClass parent_class
;
31 void (*test_signal1
) (GTest
* test
, gint an_int
);
32 void (*test_signal2
) (GTest
* test
, gint an_int
);
33 gchar
* (*test_signal3
) (GTest
* test
, gint an_int
);
36 static GType
my_test_get_type (void);
37 static volatile gboolean stopping
;
39 /* Element signals and args */
55 static void my_test_class_init (GTestClass
* klass
);
56 static void my_test_init (GTest
* test
);
57 static void my_test_dispose (GObject
* object
);
59 static void signal2_handler (GTest
* test
, gint anint
);
60 static gchar
* signal3_handler (GTest
* test
, gint anint
);
62 static void my_test_set_property (GObject
* object
, guint prop_id
,
63 const GValue
* value
, GParamSpec
* pspec
);
64 static void my_test_get_property (GObject
* object
, guint prop_id
,
65 GValue
* value
, GParamSpec
* pspec
);
67 static GObjectClass
*parent_class
= NULL
;
69 static guint my_test_signals
[LAST_SIGNAL
] = { 0 };
72 my_test_get_type (void)
74 static GType test_type
= 0;
77 const GTypeInfo test_info
= {
81 (GClassInitFunc
) my_test_class_init
,
86 (GInstanceInitFunc
) my_test_init
,
92 test_type
= g_type_register_static (G_TYPE_OBJECT
, "GTest",
99 my_test_class_init (GTestClass
* klass
)
101 GObjectClass
*gobject_class
;
103 gobject_class
= (GObjectClass
*) klass
;
105 parent_class
= g_type_class_ref (G_TYPE_OBJECT
);
107 gobject_class
->dispose
= my_test_dispose
;
108 gobject_class
->set_property
= my_test_set_property
;
109 gobject_class
->get_property
= my_test_get_property
;
111 my_test_signals
[TEST_SIGNAL1
] =
112 g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass
),
113 G_SIGNAL_RUN_LAST
, G_STRUCT_OFFSET (GTestClass
, test_signal1
), NULL
,
114 NULL
, g_cclosure_marshal_VOID__INT
, G_TYPE_NONE
, 1, G_TYPE_INT
);
115 my_test_signals
[TEST_SIGNAL2
] =
116 g_signal_new ("test-signal2", G_TYPE_FROM_CLASS (klass
),
117 G_SIGNAL_RUN_LAST
, G_STRUCT_OFFSET (GTestClass
, test_signal2
), NULL
,
118 NULL
, g_cclosure_marshal_VOID__INT
, G_TYPE_NONE
, 1, G_TYPE_INT
);
119 my_test_signals
[TEST_SIGNAL3
] =
120 g_signal_new ("test-signal3", G_TYPE_FROM_CLASS (klass
),
121 G_SIGNAL_RUN_LAST
, G_STRUCT_OFFSET (GTestClass
, test_signal3
), NULL
,
122 NULL
, g_cclosure_marshal_generic
, G_TYPE_STRING
, 1, G_TYPE_INT
);
124 g_object_class_install_property (G_OBJECT_CLASS (klass
), ARG_TEST_PROP
,
125 g_param_spec_int ("test-prop", "Test Prop", "Test property",
126 0, 1, 0, G_PARAM_READWRITE
));
128 klass
->test_signal2
= signal2_handler
;
129 klass
->test_signal3
= signal3_handler
;
133 my_test_init (GTest
* test
)
135 g_print ("init %p\n", test
);
141 my_test_dispose (GObject
* object
)
145 test
= MY_TEST (object
);
147 g_print ("dispose %p!\n", test
);
149 G_OBJECT_CLASS (parent_class
)->dispose (object
);
153 my_test_set_property (GObject
* object
, guint prop_id
,
154 const GValue
* value
, GParamSpec
* pspec
)
158 test
= MY_TEST (object
);
162 test
->value
= g_value_get_int (value
);
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
171 my_test_get_property (GObject
* object
, guint prop_id
,
172 GValue
* value
, GParamSpec
* pspec
)
176 test
= MY_TEST (object
);
180 g_value_set_int (value
, test
->value
);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
189 my_test_do_signal1 (GTest
* test
)
191 g_signal_emit (G_OBJECT (test
), my_test_signals
[TEST_SIGNAL1
], 0, 0);
195 signal2_handler (GTest
* test
, gint anint
)
200 my_test_do_signal2 (GTest
* test
)
202 g_signal_emit (G_OBJECT (test
), my_test_signals
[TEST_SIGNAL2
], 0, 0);
206 signal3_handler (GTest
* test
, gint anint
)
208 return g_strdup ("test");
212 my_test_do_signal3 (GTest
* test
)
216 g_signal_emit (G_OBJECT (test
), my_test_signals
[TEST_SIGNAL3
], 0, 0, &res
);
222 my_test_do_prop (GTest
* test
)
224 test
->value
= g_rand_int (grand
);
225 g_object_notify (G_OBJECT (test
), "test-prop");
229 run_thread (GTest
* test
)
235 my_test_do_signal1 (test
);
237 my_test_do_signal2 (test
);
239 my_test_do_prop (test
);
241 my_test_do_signal3 (test
);
242 if ((i
++ % 10000) == 0) {
244 g_thread_yield(); /* force context switch */
252 notify (GObject
*object
, GParamSpec
*spec
, gpointer user_data
)
256 g_object_get (object
, "test-prop", &value
, NULL
);
257 /*g_print ("+ %d", value);*/
261 main (int argc
, char **argv
)
264 GTest
*test1
, *test2
;
265 GArray
*test_threads
;
266 const gint n_threads
= 1;
268 g_print ("START: %s\n", argv
[0]);
269 g_log_set_always_fatal (G_LOG_LEVEL_WARNING
| G_LOG_LEVEL_CRITICAL
| g_log_set_always_fatal (G_LOG_FATAL_MASK
));
271 test1
= g_object_new (G_TYPE_TEST
, NULL
);
272 test2
= g_object_new (G_TYPE_TEST
, NULL
);
274 g_signal_connect (test1
, "notify::test-prop", G_CALLBACK (notify
), NULL
);
275 g_signal_connect (test1
, "test-signal1", G_CALLBACK (notify
), NULL
);
276 g_signal_connect (test1
, "test-signal2", G_CALLBACK (notify
), NULL
);
278 test_threads
= g_array_new (FALSE
, FALSE
, sizeof (GThread
*));
282 for (i
= 0; i
< n_threads
; i
++) {
285 thread
= g_thread_create ((GThreadFunc
) run_thread
, test1
, TRUE
, NULL
);
286 g_array_append_val (test_threads
, thread
);
288 thread
= g_thread_create ((GThreadFunc
) run_thread
, test2
, TRUE
, NULL
);
289 g_array_append_val (test_threads
, thread
);
295 g_print ("\nstopping\n");
297 /* join all threads */
298 for (i
= 0; i
< 2 * n_threads
; i
++) {
301 thread
= g_array_index (test_threads
, GThread
*, i
);
302 g_thread_join (thread
);
305 g_print ("stopped\n");
307 g_array_free (test_threads
, TRUE
);
308 g_object_unref (test1
);
309 g_object_unref (test2
);