1 /* Copyright (C) 2005 Imendio AB
3 * SPDX-License-Identifier: LicenseRef-old-glib-tests
5 * This software is provided "as is"; redistribution and modification
6 * is permitted, provided that the following disclaimer is retained.
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 * In no event shall the authors or contributors be liable for any
12 * direct, indirect, incidental, special, exemplary, or consequential
13 * damages (including, but not limited to, procurement of substitute
14 * goods or services; loss of use, data, or profits; or business
15 * interruption) however caused and on any theory of liability, whether
16 * in contract, strict liability, or tort (including negligence or
17 * otherwise) arising in any way out of the use of this software, even
18 * if advised of the possibility of such damage.
20 #include <glib-object.h>
26 #define TEST_POINTER1 ((gpointer) 47)
27 #define TEST_POINTER2 ((gpointer) 49)
28 #define TEST_INT1 (-77)
29 #define TEST_INT2 (78)
31 /* --- GTest class --- */
35 gpointer test_pointer1;
36 gpointer test_pointer2;
39 GObjectClass parent_class;
40 void (*test_signal1) (GTest * test, gint an_int);
41 void (*test_signal2) (GTest * test, gint an_int);
44 #define G_TYPE_TEST (my_test_get_type ())
45 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
46 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
47 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
48 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
49 #define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
51 static GType my_test_get_type (void);
52 G_DEFINE_TYPE (GTest, my_test, G_TYPE_OBJECT)
57 GClosure *closure; /* (unowned) */
59 gboolean seen_signal_handler;
60 gboolean seen_cleanup;
61 gboolean seen_test_int1;
62 gboolean seen_test_int2;
63 gboolean seen_thread1;
64 gboolean seen_thread2;
65 } TestClosureRefcountData;
67 /* --- functions --- */
69 my_test_init (GTest * test)
71 g_test_message ("Init %p", test);
74 test->test_pointer1 = TEST_POINTER1;
75 test->test_pointer2 = TEST_POINTER2;
89 static guint signals[SIGNAL_TEST_SIGNAL2 + 1] = { 0, };
92 my_test_set_property (GObject *object,
97 GTest *test = MY_TEST (object);
101 test->value = g_value_get_int (value);
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
110 my_test_get_property (GObject *object,
115 GTest *test = MY_TEST (object);
119 g_value_set_int (value, test->value);
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 my_test_test_signal2 (GTest *test,
134 my_test_emit_test_signal1 (GTest *test,
137 g_signal_emit (G_OBJECT (test), signals[SIGNAL_TEST_SIGNAL1], 0, vint);
141 my_test_emit_test_signal2 (GTest *test,
144 g_signal_emit (G_OBJECT (test), signals[SIGNAL_TEST_SIGNAL2], 0, vint);
148 my_test_class_init (GTestClass *klass)
150 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
152 gobject_class->set_property = my_test_set_property;
153 gobject_class->get_property = my_test_get_property;
155 signals[SIGNAL_TEST_SIGNAL1] =
156 g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
157 G_STRUCT_OFFSET (GTestClass, test_signal1), NULL, NULL,
158 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
159 signals[SIGNAL_TEST_SIGNAL2] =
160 g_signal_new ("test-signal2", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
161 G_STRUCT_OFFSET (GTestClass, test_signal2), NULL, NULL,
162 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
164 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TEST_PROP,
165 g_param_spec_int ("test-prop", "Test Prop", "Test property",
166 0, 1, 0, G_PARAM_READWRITE));
167 klass->test_signal2 = my_test_test_signal2;
171 test_closure (GClosure *closure)
173 /* try to produce high contention in closure->ref_count */
174 guint i = 0, n = g_random_int () % 199;
175 for (i = 0; i < n; i++)
176 g_closure_ref (closure);
177 g_closure_sink (closure); /* NOP */
178 for (i = 0; i < n; i++)
179 g_closure_unref (closure);
183 thread1_main (gpointer user_data)
185 TestClosureRefcountData *data = user_data;
188 for (i = 1; !g_atomic_int_get (&data->stopping); i++)
190 test_closure (data->closure);
193 g_test_message ("Yielding from thread1");
194 g_thread_yield (); /* force context switch */
195 g_atomic_int_set (&data->seen_thread1, TRUE);
202 thread2_main (gpointer user_data)
204 TestClosureRefcountData *data = user_data;
207 for (i = 1; !g_atomic_int_get (&data->stopping); i++)
209 test_closure (data->closure);
212 g_test_message ("Yielding from thread2");
213 g_thread_yield (); /* force context switch */
214 g_atomic_int_set (&data->seen_thread2, TRUE);
221 test_signal_handler (GTest *test,
225 TestClosureRefcountData *data = user_data;
227 g_assert_true (test->test_pointer1 == TEST_POINTER1);
229 data->seen_signal_handler = TRUE;
230 data->seen_test_int1 |= vint == TEST_INT1;
231 data->seen_test_int2 |= vint == TEST_INT2;
235 destroy_data (gpointer user_data,
238 TestClosureRefcountData *data = user_data;
240 data->seen_cleanup = TRUE;
241 g_assert_true (data->closure == closure);
242 g_assert_cmpint (closure->ref_count, ==, 0);
246 test_emissions (GTest *test)
248 my_test_emit_test_signal1 (test, TEST_INT1);
249 my_test_emit_test_signal2 (test, TEST_INT2);
252 /* Test that closure refcounting works even when high contested between three
253 * threads (the main thread, thread1 and thread2). Both child threads are
254 * contesting refs/unrefs, while the main thread periodically emits signals
255 * which also do refs/unrefs on closures. */
257 test_closure_refcount (void)
259 GThread *thread1, *thread2;
260 TestClosureRefcountData test_data = { 0, };
263 guint i, n_iterations;
265 object = g_object_new (G_TYPE_TEST, NULL);
266 closure = g_cclosure_new (G_CALLBACK (test_signal_handler), &test_data, destroy_data);
268 g_signal_connect_closure (object, "test-signal1", closure, FALSE);
269 g_signal_connect_closure (object, "test-signal2", closure, FALSE);
271 test_data.stopping = FALSE;
272 test_data.closure = closure;
274 thread1 = g_thread_new ("thread1", thread1_main, &test_data);
275 thread2 = g_thread_new ("thread2", thread2_main, &test_data);
277 /* The 16-bit compare-and-swap operations currently used for closure
278 * refcounts are really slow on some ARM CPUs, notably Cortex-A57.
279 * Reduce the number of iterations so that the test completes in a
280 * finite time, but don't reduce it so much that the main thread
281 * starves the other threads and causes a test failure.
283 * https://gitlab.gnome.org/GNOME/glib/issues/1316
284 * aka https://bugs.debian.org/880883 */
285 #if defined(__aarch64__) || defined(__arm__)
286 n_iterations = 100000;
288 n_iterations = 1000000;
291 /* Run the test for a reasonably high number of iterations, and ensure we
292 * don’t terminate until at least 10000 iterations have completed in both
293 * thread1 and thread2. Even though @n_iterations is high, we can’t guarantee
294 * that the scheduler allocates time fairly (or at all!) to thread1 or
298 !g_atomic_int_get (&test_data.seen_thread1) ||
299 !g_atomic_int_get (&test_data.seen_thread2);
302 test_emissions (object);
305 g_test_message ("Yielding from main thread");
306 g_thread_yield (); /* force context switch */
310 g_atomic_int_set (&test_data.stopping, TRUE);
311 g_test_message ("Stopping");
313 /* wait for thread shutdown */
314 g_thread_join (thread1);
315 g_thread_join (thread2);
317 /* finalize object, destroy signals, run cleanup code */
318 g_object_unref (object);
320 g_test_message ("Stopped");
322 g_assert_true (g_atomic_int_get (&test_data.seen_thread1));
323 g_assert_true (g_atomic_int_get (&test_data.seen_thread2));
324 g_assert_true (test_data.seen_test_int1);
325 g_assert_true (test_data.seen_test_int2);
326 g_assert_true (test_data.seen_signal_handler);
327 g_assert_true (test_data.seen_cleanup);
334 g_test_init (&argc, &argv, NULL);
336 g_test_add_func ("/closure/refcount", test_closure_refcount);
338 return g_test_run ();