Merge branch 'g-clear-pointer-no-side-effects' into 'master'
[glib.git] / tests / refcount / signals.c
blobd1a0447c9e6caaac127746a53aae8b1ac586ccf0
1 #include <glib.h>
2 #include <glib-object.h>
4 #ifdef G_OS_UNIX
5 #include <unistd.h>
6 #endif
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))
15 static GRand *grand;
17 typedef struct _GTest GTest;
18 typedef struct _GTestClass GTestClass;
20 struct _GTest
22 GObject object;
24 gint value;
27 struct _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 */
40 enum
42 TEST_SIGNAL1,
43 TEST_SIGNAL2,
44 TEST_SIGNAL3,
45 /* add more above */
46 LAST_SIGNAL
49 enum
51 ARG_0,
52 ARG_TEST_PROP
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 };
71 static GType
72 my_test_get_type (void)
74 static GType test_type = 0;
76 if (!test_type) {
77 const GTypeInfo test_info = {
78 sizeof (GTestClass),
79 NULL,
80 NULL,
81 (GClassInitFunc) my_test_class_init,
82 NULL,
83 NULL,
84 sizeof (GTest),
86 (GInstanceInitFunc) my_test_init,
87 NULL
90 grand = g_rand_new();
92 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
93 &test_info, 0);
95 return test_type;
98 static void
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;
132 static void
133 my_test_init (GTest * test)
135 g_print ("init %p\n", test);
137 test->value = 0;
140 static void
141 my_test_dispose (GObject * object)
143 GTest *test;
145 test = MY_TEST (object);
147 g_print ("dispose %p!\n", test);
149 G_OBJECT_CLASS (parent_class)->dispose (object);
152 static void
153 my_test_set_property (GObject * object, guint prop_id,
154 const GValue * value, GParamSpec * pspec)
156 GTest *test;
158 test = MY_TEST (object);
160 switch (prop_id) {
161 case ARG_TEST_PROP:
162 test->value = g_value_get_int (value);
163 break;
164 default:
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 break;
170 static void
171 my_test_get_property (GObject * object, guint prop_id,
172 GValue * value, GParamSpec * pspec)
174 GTest *test;
176 test = MY_TEST (object);
178 switch (prop_id) {
179 case ARG_TEST_PROP:
180 g_value_set_int (value, test->value);
181 break;
182 default:
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184 break;
188 static void
189 my_test_do_signal1 (GTest * test)
191 g_signal_emit (G_OBJECT (test), my_test_signals[TEST_SIGNAL1], 0, 0);
194 static void
195 signal2_handler (GTest * test, gint anint)
199 static void
200 my_test_do_signal2 (GTest * test)
202 g_signal_emit (G_OBJECT (test), my_test_signals[TEST_SIGNAL2], 0, 0);
205 static gchar *
206 signal3_handler (GTest * test, gint anint)
208 return g_strdup ("test");
211 static void
212 my_test_do_signal3 (GTest * test)
214 gchar *res;
216 g_signal_emit (G_OBJECT (test), my_test_signals[TEST_SIGNAL3], 0, 0, &res);
217 g_assert (res);
218 g_free (res);
221 static void
222 my_test_do_prop (GTest * test)
224 test->value = g_rand_int (grand);
225 g_object_notify (G_OBJECT (test), "test-prop");
228 static gpointer
229 run_thread (GTest * test)
231 gint i = 1;
233 while (!stopping) {
234 if (TESTNUM == 1)
235 my_test_do_signal1 (test);
236 if (TESTNUM == 2)
237 my_test_do_signal2 (test);
238 if (TESTNUM == 3)
239 my_test_do_prop (test);
240 if (TESTNUM == 4)
241 my_test_do_signal3 (test);
242 if ((i++ % 10000) == 0) {
243 g_print (".");
244 g_thread_yield(); /* force context switch */
248 return NULL;
251 static void
252 notify (GObject *object, GParamSpec *spec, gpointer user_data)
254 gint value;
256 g_object_get (object, "test-prop", &value, NULL);
257 /*g_print ("+ %d", value);*/
261 main (int argc, char **argv)
263 gint i;
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 *));
280 stopping = FALSE;
282 for (i = 0; i < n_threads; i++) {
283 GThread *thread;
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);
291 g_usleep (5000000);
293 stopping = TRUE;
295 g_print ("\nstopping\n");
297 /* join all threads */
298 for (i = 0; i < 2 * n_threads; i++) {
299 GThread *thread;
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);
311 return 0;