Merge branch 'refcount-fixups' into 'master'

Move closures refcount test to gobject/tests/

See merge request GNOME/glib!696
This commit is contained in:
Philip Withnall 2019-02-27 16:47:58 +00:00
commit 7536bacb3d
3 changed files with 129 additions and 88 deletions

View File

@ -49,32 +49,42 @@ typedef struct {
static GType my_test_get_type (void);
G_DEFINE_TYPE (GTest, my_test, G_TYPE_OBJECT)
/* --- variables --- */
static volatile gboolean stopping = FALSE;
static guint test_signal1 = 0;
static guint test_signal2 = 0;
static gboolean seen_signal_handler = FALSE;
static gboolean seen_cleanup = FALSE;
static gboolean seen_test_int1 = FALSE;
static gboolean seen_test_int2 = FALSE;
static gboolean seen_thread1 = FALSE;
static gboolean seen_thread2 = FALSE;
/* Test state */
typedef struct
{
GClosure *closure; /* (unowned) */
gboolean stopping;
gboolean seen_signal_handler;
gboolean seen_cleanup;
gboolean seen_test_int1;
gboolean seen_test_int2;
gboolean seen_thread1;
gboolean seen_thread2;
} TestClosureRefcountData;
/* --- functions --- */
static void
my_test_init (GTest * test)
{
g_print ("init %p\n", test);
g_test_message ("Init %p", test);
test->value = 0;
test->test_pointer1 = TEST_POINTER1;
test->test_pointer2 = TEST_POINTER2;
}
enum {
ARG_0,
ARG_TEST_PROP
};
typedef enum
{
PROP_TEST_PROP = 1,
} MyTestProperty;
typedef enum
{
SIGNAL_TEST_SIGNAL1,
SIGNAL_TEST_SIGNAL2,
} MyTestSignal;
static guint signals[SIGNAL_TEST_SIGNAL2 + 1] = { 0, };
static void
my_test_set_property (GObject *object,
@ -85,7 +95,7 @@ my_test_set_property (GObject *object,
GTest *test = MY_TEST (object);
switch (prop_id)
{
case ARG_TEST_PROP:
case PROP_TEST_PROP:
test->value = g_value_get_int (value);
break;
default:
@ -103,7 +113,7 @@ my_test_get_property (GObject *object,
GTest *test = MY_TEST (object);
switch (prop_id)
{
case ARG_TEST_PROP:
case PROP_TEST_PROP:
g_value_set_int (value, test->value);
break;
default:
@ -122,14 +132,14 @@ static void
my_test_emit_test_signal1 (GTest *test,
gint vint)
{
g_signal_emit (G_OBJECT (test), test_signal1, 0, vint);
g_signal_emit (G_OBJECT (test), signals[SIGNAL_TEST_SIGNAL1], 0, vint);
}
static void
my_test_emit_test_signal2 (GTest *test,
gint vint)
{
g_signal_emit (G_OBJECT (test), test_signal2, 0, vint);
g_signal_emit (G_OBJECT (test), signals[SIGNAL_TEST_SIGNAL2], 0, vint);
}
static void
@ -140,14 +150,16 @@ my_test_class_init (GTestClass *klass)
gobject_class->set_property = my_test_set_property;
gobject_class->get_property = my_test_get_property;
test_signal1 = g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GTestClass, test_signal1), NULL, NULL,
g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
test_signal2 = g_signal_new ("test-signal2", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GTestClass, test_signal2), NULL, NULL,
g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
signals[SIGNAL_TEST_SIGNAL1] =
g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GTestClass, test_signal1), NULL, NULL,
g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
signals[SIGNAL_TEST_SIGNAL2] =
g_signal_new ("test-signal2", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GTestClass, test_signal2), NULL, NULL,
g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TEST_PROP,
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TEST_PROP,
g_param_spec_int ("test-prop", "Test Prop", "Test property",
0, 1, 0, G_PARAM_READWRITE));
klass->test_signal2 = my_test_test_signal2;
@ -166,36 +178,38 @@ test_closure (GClosure *closure)
}
static gpointer
thread1_main (gpointer data)
thread1_main (gpointer user_data)
{
GClosure *closure = data;
while (!stopping)
TestClosureRefcountData *data = user_data;
guint i = 0;
for (i = 0; !g_atomic_int_get (&data->stopping); i++)
{
static guint count = 0;
test_closure (closure);
if (++count % 10000 == 0)
test_closure (data->closure);
if (i % 10000 == 0)
{
g_printerr ("c");
g_thread_yield(); /* force context switch */
seen_thread1 = TRUE;
g_test_message ("Yielding from thread1");
g_thread_yield (); /* force context switch */
g_atomic_int_set (&data->seen_thread1, TRUE);
}
}
return NULL;
}
static gpointer
thread2_main (gpointer data)
thread2_main (gpointer user_data)
{
GClosure *closure = data;
while (!stopping)
TestClosureRefcountData *data = user_data;
guint i = 0;
for (i = 0; !g_atomic_int_get (&data->stopping); i++)
{
static guint count = 0;
test_closure (closure);
if (++count % 10000 == 0)
test_closure (data->closure);
if (i % 10000 == 0)
{
g_printerr ("C");
g_thread_yield(); /* force context switch */
seen_thread2 = TRUE;
g_test_message ("Yielding from thread2");
g_thread_yield (); /* force context switch */
g_atomic_int_set (&data->seen_thread2, TRUE);
}
}
return NULL;
@ -204,21 +218,26 @@ thread2_main (gpointer data)
static void
test_signal_handler (GTest *test,
gint vint,
gpointer data)
gpointer user_data)
{
g_assert (data == TEST_POINTER2);
g_assert (test->test_pointer1 == TEST_POINTER1);
seen_signal_handler = TRUE;
seen_test_int1 |= vint == TEST_INT1;
seen_test_int2 |= vint == TEST_INT2;
TestClosureRefcountData *data = user_data;
g_assert_true (test->test_pointer1 == TEST_POINTER1);
data->seen_signal_handler = TRUE;
data->seen_test_int1 |= vint == TEST_INT1;
data->seen_test_int2 |= vint == TEST_INT2;
}
static void
destroy_data (gpointer data,
destroy_data (gpointer user_data,
GClosure *closure)
{
seen_cleanup = data == TEST_POINTER2;
g_assert (closure->ref_count == 0);
TestClosureRefcountData *data = user_data;
data->seen_cleanup = TRUE;
g_assert_true (data->closure == closure);
g_assert_cmpint (closure->ref_count, ==, 0);
}
static void
@ -228,28 +247,30 @@ test_emissions (GTest *test)
my_test_emit_test_signal2 (test, TEST_INT2);
}
int
main (int argc,
char **argv)
/* Test that closure refcounting works even when high contested between three
* threads (the main thread, thread1 and thread2). Both child threads are
* contesting refs/unrefs, while the main thread periodically emits signals
* which also do refs/unrefs on closures. */
static void
test_closure_refcount (void)
{
GThread *thread1, *thread2;
TestClosureRefcountData test_data = { 0, };
GClosure *closure;
GTest *object;
guint i;
g_print ("START: %s\n", argv[0]);
g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
guint i, n_iterations;
object = g_object_new (G_TYPE_TEST, NULL);
closure = g_cclosure_new (G_CALLBACK (test_signal_handler), TEST_POINTER2, destroy_data);
closure = g_cclosure_new (G_CALLBACK (test_signal_handler), &test_data, destroy_data);
g_signal_connect_closure (object, "test-signal1", closure, FALSE);
g_signal_connect_closure (object, "test-signal2", closure, FALSE);
stopping = FALSE;
test_data.stopping = FALSE;
test_data.closure = closure;
thread1 = g_thread_create (thread1_main, closure, TRUE, NULL);
thread2 = g_thread_create (thread2_main, closure, TRUE, NULL);
thread1 = g_thread_new ("thread1", thread1_main, &test_data);
thread2 = g_thread_new ("thread2", thread2_main, &test_data);
/* The 16-bit compare-and-swap operations currently used for closure
* refcounts are really slow on some ARM CPUs, notably Cortex-A57.
@ -260,22 +281,32 @@ main (int argc,
* https://gitlab.gnome.org/GNOME/glib/issues/1316
* aka https://bugs.debian.org/880883 */
#if defined(__aarch64__) || defined(__arm__)
for (i = 0; i < 100000; i++)
n_iterations = 100000;
#else
for (i = 0; i < 1000000; i++)
n_iterations = 1000000;
#endif
/* Run the test for a reasonably high number of iterations, and ensure we
* dont terminate until at least 10000 iterations have completed in both
* thread1 and thread2. Even though @n_iterations is high, we cant guarantee
* that the scheduler allocates time fairly (or at all!) to thread1 or
* thread2. */
for (i = 0;
i < n_iterations ||
!g_atomic_int_get (&test_data.seen_thread1) ||
!g_atomic_int_get (&test_data.seen_thread2);
i++)
{
static guint count = 0;
test_emissions (object);
if (++count % 10000 == 0)
if (i % 10000 == 0)
{
g_printerr (".\n");
g_thread_yield(); /* force context switch */
g_test_message ("Yielding from main thread");
g_thread_yield (); /* force context switch */
}
}
stopping = TRUE;
g_print ("\nstopping\n");
g_atomic_int_set (&test_data.stopping, TRUE);
g_test_message ("Stopping");
/* wait for thread shutdown */
g_thread_join (thread1);
@ -284,14 +315,23 @@ main (int argc,
/* finalize object, destroy signals, run cleanup code */
g_object_unref (object);
g_print ("stopped\n");
g_test_message ("Stopped");
g_assert (seen_thread1 != FALSE);
g_assert (seen_thread2 != FALSE);
g_assert (seen_test_int1 != FALSE);
g_assert (seen_test_int2 != FALSE);
g_assert (seen_signal_handler != FALSE);
g_assert (seen_cleanup != FALSE);
return 0;
g_assert_true (g_atomic_int_get (&test_data.seen_thread1));
g_assert_true (g_atomic_int_get (&test_data.seen_thread2));
g_assert_true (test_data.seen_test_int1);
g_assert_true (test_data.seen_test_int2);
g_assert_true (test_data.seen_signal_handler);
g_assert_true (test_data.seen_cleanup);
}
int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/closure/refcount", test_closure_refcount);
return g_test_run ();
}

View File

@ -43,6 +43,7 @@ gobject_tests = {
'source' : 'private.c',
},
'closure' : {},
'closure-refcount' : { 'suite': ['slow'] },
'object' : {},
'signal-handler' : {},
'ifaceproperties' : {},
@ -95,6 +96,13 @@ foreach test_name, extra_args : gobject_tests
suite = ['gobject'] + extra_args.get('suite', [])
timeout = suite.contains('slow') ? test_timeout_slow : test_timeout
# FIXME: https://gitlab.gnome.org/GNOME/glib/issues/1316
# aka https://bugs.debian.org/880883
if test_name == 'closure-refcount' and ['arm', 'aarch64'].contains(host_cpu_family)
timeout = timeout * 10
endif
test(test_name, exe, env : test_env, timeout : timeout, suite : suite,
args: ['--tap'])
endforeach

View File

@ -1,5 +1,4 @@
refcount_tests = {
'closures' : {'suite' : ['slow']},
'objects' : {},
'objects2' : {'suite' : ['slow']},
'properties' : {},
@ -56,12 +55,6 @@ foreach test_name, extra_args : refcount_tests
suite = ['refcount'] + extra_args.get('suite', [])
timeout = suite.contains('slow') ? test_timeout_slow : test_timeout
# https://gitlab.gnome.org/GNOME/glib/issues/1316
# aka https://bugs.debian.org/880883
if test_name == 'closures' and ['arm', 'aarch64'].contains(host_cpu_family)
timeout = timeout * 10
endif
# FIXME? TESTS_ENVIRONMENT = LIBCHARSET_ALIAS_DIR=$(top_builddir)/glib/libcharset
test(test_name, exe, env : test_env, timeout : timeout, suite : suite)
endforeach