1 /* Copyright (C) 2005 Imendio AB
3 * This software is provided "as is"; redistribution and modification
4 * is permitted, provided that the following disclaimer is retained.
6 * This software is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 * In no event shall the authors or contributors be liable for any
10 * direct, indirect, incidental, special, exemplary, or consequential
11 * damages (including, but not limited to, procurement of substitute
12 * goods or services; loss of use, data, or profits; or business
13 * interruption) however caused and on any theory of liability, whether
14 * in contract, strict liability, or tort (including negligence or
15 * otherwise) arising in any way out of the use of this software, even
16 * if advised of the possibility of such damage.
18 #include <glib-object.h>
21 #define TEST_POINTER1 ((gpointer) 47)
22 #define TEST_POINTER2 ((gpointer) 49)
23 #define TEST_INT1 (-77)
24 #define TEST_INT2 (78)
26 /* --- GTest class --- */
30 gpointer test_pointer1;
31 gpointer test_pointer2;
34 GObjectClass parent_class;
35 void (*test_signal1) (GTest * test, gint an_int);
36 void (*test_signal2) (GTest * test, gint an_int);
39 #define G_TYPE_TEST (my_test_get_type ())
40 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
41 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
42 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
43 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
44 #define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
46 static GType my_test_get_type (void);
47 G_DEFINE_TYPE (GTest, my_test, G_TYPE_OBJECT);
49 /* --- variables --- */
50 static volatile gboolean stopping = FALSE;
51 static guint test_signal1 = 0;
52 static guint test_signal2 = 0;
53 static gboolean seen_signal_handler = FALSE;
54 static gboolean seen_cleanup = FALSE;
55 static gboolean seen_test_int1 = FALSE;
56 static gboolean seen_test_int2 = FALSE;
57 static gboolean seen_thread1 = FALSE;
58 static gboolean seen_thread2 = FALSE;
60 /* --- functions --- */
62 my_test_init (GTest * test)
64 g_print ("init %p\n", test);
67 test->test_pointer1 = TEST_POINTER1;
68 test->test_pointer2 = TEST_POINTER2;
77 my_test_set_property (GObject *object,
82 GTest *test = MY_TEST (object);
86 test->value = g_value_get_int (value);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 my_test_get_property (GObject *object,
100 GTest *test = MY_TEST (object);
104 g_value_set_int (value, test->value);
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 my_test_test_signal2 (GTest *test,
119 my_test_emit_test_signal1 (GTest *test,
122 g_signal_emit (G_OBJECT (test), test_signal1, 0, vint);
126 my_test_emit_test_signal2 (GTest *test,
129 g_signal_emit (G_OBJECT (test), test_signal2, 0, vint);
133 my_test_class_init (GTestClass *klass)
135 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
137 gobject_class->set_property = my_test_set_property;
138 gobject_class->get_property = my_test_get_property;
140 test_signal1 = g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
141 G_STRUCT_OFFSET (GTestClass, test_signal1), NULL, NULL,
142 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
143 test_signal2 = g_signal_new ("test-signal2", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
144 G_STRUCT_OFFSET (GTestClass, test_signal2), NULL, NULL,
145 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
147 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TEST_PROP,
148 g_param_spec_int ("test-prop", "Test Prop", "Test property",
149 0, 1, 0, G_PARAM_READWRITE));
150 klass->test_signal2 = my_test_test_signal2;
153 static inline guint32
156 static guint32 accu = 2147483563;
157 accu = 1664525 * accu + 1013904223;
162 test_closure (GClosure *closure)
164 /* try to produce high contention in closure->ref_count */
165 guint i = 0, n = quick_rand32() % 199;
166 for (i = 0; i < n; i++)
167 g_closure_ref (closure);
168 g_closure_sink (closure); /* NOP */
169 for (i = 0; i < n; i++)
170 g_closure_unref (closure);
174 thread1_main (gpointer data)
176 GClosure *closure = data;
179 static guint count = 0;
180 test_closure (closure);
181 if (++count % 10000 == 0)
184 g_thread_yield(); /* force context switch */
192 thread2_main (gpointer data)
194 GClosure *closure = data;
197 static guint count = 0;
198 test_closure (closure);
199 if (++count % 10000 == 0)
202 g_thread_yield(); /* force context switch */
210 test_signal_handler (GTest *test,
214 g_assert (data == TEST_POINTER2);
215 g_assert (test->test_pointer1 == TEST_POINTER1);
216 seen_signal_handler = TRUE;
217 seen_test_int1 |= vint == TEST_INT1;
218 seen_test_int2 |= vint == TEST_INT2;
222 destroy_data (gpointer data,
225 seen_cleanup = data == TEST_POINTER2;
226 g_assert (closure->ref_count == 0);
230 test_emissions (GTest *test)
232 my_test_emit_test_signal1 (test, TEST_INT1);
233 my_test_emit_test_signal2 (test, TEST_INT2);
240 GThread *thread1, *thread2;
245 g_print ("START: %s\n", argv[0]);
246 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
248 object = g_object_new (G_TYPE_TEST, NULL);
249 closure = g_cclosure_new (G_CALLBACK (test_signal_handler), TEST_POINTER2, destroy_data);
251 g_signal_connect_closure (object, "test-signal1", closure, FALSE);
252 g_signal_connect_closure (object, "test-signal2", closure, FALSE);
256 thread1 = g_thread_create (thread1_main, closure, TRUE, NULL);
257 thread2 = g_thread_create (thread2_main, closure, TRUE, NULL);
259 for (i = 0; i < 1000000; i++)
261 static guint count = 0;
262 test_emissions (object);
263 if (++count % 10000 == 0)
266 g_thread_yield(); /* force context switch */
271 g_print ("\nstopping\n");
273 /* wait for thread shutdown */
274 g_thread_join (thread1);
275 g_thread_join (thread2);
277 /* finalize object, destroy signals, run cleanup code */
278 g_object_unref (object);
280 g_print ("stopped\n");
282 g_assert (seen_thread1 != FALSE);
283 g_assert (seen_thread2 != FALSE);
284 g_assert (seen_test_int1 != FALSE);
285 g_assert (seen_test_int2 != FALSE);
286 g_assert (seen_signal_handler != FALSE);
287 g_assert (seen_cleanup != FALSE);