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>
24 #define TEST_POINTER1 ((gpointer) 47)
25 #define TEST_POINTER2 ((gpointer) 49)
26 #define TEST_INT1 (-77)
27 #define TEST_INT2 (78)
29 /* --- GTest class --- */
33 gpointer test_pointer1
;
34 gpointer test_pointer2
;
37 GObjectClass parent_class
;
38 void (*test_signal1
) (GTest
* test
, gint an_int
);
39 void (*test_signal2
) (GTest
* test
, gint an_int
);
42 #define G_TYPE_TEST (my_test_get_type ())
43 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
44 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
45 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
46 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
47 #define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
49 static GType
my_test_get_type (void);
50 G_DEFINE_TYPE (GTest
, my_test
, G_TYPE_OBJECT
)
52 /* --- variables --- */
53 static volatile gboolean stopping
= FALSE
;
54 static guint test_signal1
= 0;
55 static guint test_signal2
= 0;
56 static gboolean seen_signal_handler
= FALSE
;
57 static gboolean seen_cleanup
= FALSE
;
58 static gboolean seen_test_int1
= FALSE
;
59 static gboolean seen_test_int2
= FALSE
;
60 static gboolean seen_thread1
= FALSE
;
61 static gboolean seen_thread2
= FALSE
;
63 /* --- functions --- */
65 my_test_init (GTest
* test
)
67 g_print ("init %p\n", test
);
70 test
->test_pointer1
= TEST_POINTER1
;
71 test
->test_pointer2
= TEST_POINTER2
;
80 my_test_set_property (GObject
*object
,
85 GTest
*test
= MY_TEST (object
);
89 test
->value
= g_value_get_int (value
);
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
98 my_test_get_property (GObject
*object
,
103 GTest
*test
= MY_TEST (object
);
107 g_value_set_int (value
, test
->value
);
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
116 my_test_test_signal2 (GTest
*test
,
122 my_test_emit_test_signal1 (GTest
*test
,
125 g_signal_emit (G_OBJECT (test
), test_signal1
, 0, vint
);
129 my_test_emit_test_signal2 (GTest
*test
,
132 g_signal_emit (G_OBJECT (test
), test_signal2
, 0, vint
);
136 my_test_class_init (GTestClass
*klass
)
138 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
140 gobject_class
->set_property
= my_test_set_property
;
141 gobject_class
->get_property
= my_test_get_property
;
143 test_signal1
= g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass
), G_SIGNAL_RUN_LAST
,
144 G_STRUCT_OFFSET (GTestClass
, test_signal1
), NULL
, NULL
,
145 g_cclosure_marshal_VOID__INT
, G_TYPE_NONE
, 1, G_TYPE_INT
);
146 test_signal2
= g_signal_new ("test-signal2", G_TYPE_FROM_CLASS (klass
), G_SIGNAL_RUN_LAST
,
147 G_STRUCT_OFFSET (GTestClass
, test_signal2
), NULL
, NULL
,
148 g_cclosure_marshal_VOID__INT
, G_TYPE_NONE
, 1, G_TYPE_INT
);
150 g_object_class_install_property (G_OBJECT_CLASS (klass
), ARG_TEST_PROP
,
151 g_param_spec_int ("test-prop", "Test Prop", "Test property",
152 0, 1, 0, G_PARAM_READWRITE
));
153 klass
->test_signal2
= my_test_test_signal2
;
156 static inline guint32
159 static guint32 accu
= 2147483563;
160 accu
= 1664525 * accu
+ 1013904223;
165 test_closure (GClosure
*closure
)
167 /* try to produce high contention in closure->ref_count */
168 guint i
= 0, n
= quick_rand32() % 199;
169 for (i
= 0; i
< n
; i
++)
170 g_closure_ref (closure
);
171 g_closure_sink (closure
); /* NOP */
172 for (i
= 0; i
< n
; i
++)
173 g_closure_unref (closure
);
177 thread1_main (gpointer data
)
179 GClosure
*closure
= data
;
182 static guint count
= 0;
183 test_closure (closure
);
184 if (++count
% 10000 == 0)
187 g_thread_yield(); /* force context switch */
195 thread2_main (gpointer data
)
197 GClosure
*closure
= data
;
200 static guint count
= 0;
201 test_closure (closure
);
202 if (++count
% 10000 == 0)
205 g_thread_yield(); /* force context switch */
213 test_signal_handler (GTest
*test
,
217 g_assert (data
== TEST_POINTER2
);
218 g_assert (test
->test_pointer1
== TEST_POINTER1
);
219 seen_signal_handler
= TRUE
;
220 seen_test_int1
|= vint
== TEST_INT1
;
221 seen_test_int2
|= vint
== TEST_INT2
;
225 destroy_data (gpointer data
,
228 seen_cleanup
= data
== TEST_POINTER2
;
229 g_assert (closure
->ref_count
== 0);
233 test_emissions (GTest
*test
)
235 my_test_emit_test_signal1 (test
, TEST_INT1
);
236 my_test_emit_test_signal2 (test
, TEST_INT2
);
243 GThread
*thread1
, *thread2
;
248 g_print ("START: %s\n", argv
[0]);
249 g_log_set_always_fatal (G_LOG_LEVEL_WARNING
| G_LOG_LEVEL_CRITICAL
| g_log_set_always_fatal (G_LOG_FATAL_MASK
));
251 object
= g_object_new (G_TYPE_TEST
, NULL
);
252 closure
= g_cclosure_new (G_CALLBACK (test_signal_handler
), TEST_POINTER2
, destroy_data
);
254 g_signal_connect_closure (object
, "test-signal1", closure
, FALSE
);
255 g_signal_connect_closure (object
, "test-signal2", closure
, FALSE
);
259 thread1
= g_thread_create (thread1_main
, closure
, TRUE
, NULL
);
260 thread2
= g_thread_create (thread2_main
, closure
, TRUE
, NULL
);
262 for (i
= 0; i
< 1000000; i
++)
264 static guint count
= 0;
265 test_emissions (object
);
266 if (++count
% 10000 == 0)
269 g_thread_yield(); /* force context switch */
274 g_print ("\nstopping\n");
276 /* wait for thread shutdown */
277 g_thread_join (thread1
);
278 g_thread_join (thread2
);
280 /* finalize object, destroy signals, run cleanup code */
281 g_object_unref (object
);
283 g_print ("stopped\n");
285 g_assert (seen_thread1
!= FALSE
);
286 g_assert (seen_thread2
!= FALSE
);
287 g_assert (seen_test_int1
!= FALSE
);
288 g_assert (seen_test_int2
!= FALSE
);
289 g_assert (seen_signal_handler
!= FALSE
);
290 g_assert (seen_cleanup
!= FALSE
);