2005-08-01 23:47:15 +02:00
|
|
|
|
/* Copyright (C) 2005 Imendio AB
|
2022-11-02 13:34:19 +01:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: LicenseRef-old-glib-tests
|
2005-08-01 23:47:15 +02:00
|
|
|
|
*
|
|
|
|
|
* This software is provided "as is"; redistribution and modification
|
|
|
|
|
* is permitted, provided that the following disclaimer is retained.
|
|
|
|
|
*
|
|
|
|
|
* This software is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
* In no event shall the authors or contributors be liable for any
|
|
|
|
|
* direct, indirect, incidental, special, exemplary, or consequential
|
|
|
|
|
* damages (including, but not limited to, procurement of substitute
|
|
|
|
|
* goods or services; loss of use, data, or profits; or business
|
|
|
|
|
* interruption) however caused and on any theory of liability, whether
|
|
|
|
|
* in contract, strict liability, or tort (including negligence or
|
|
|
|
|
* otherwise) arising in any way out of the use of this software, even
|
|
|
|
|
* if advised of the possibility of such damage.
|
|
|
|
|
*/
|
|
|
|
|
#include <glib-object.h>
|
2013-11-05 09:08:45 +01:00
|
|
|
|
|
|
|
|
|
#ifdef G_OS_UNIX
|
2005-08-01 23:47:15 +02:00
|
|
|
|
#include <unistd.h>
|
2013-11-05 09:08:45 +01:00
|
|
|
|
#endif
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
|
|
|
|
#define TEST_POINTER1 ((gpointer) 47)
|
|
|
|
|
#define TEST_POINTER2 ((gpointer) 49)
|
|
|
|
|
#define TEST_INT1 (-77)
|
|
|
|
|
#define TEST_INT2 (78)
|
|
|
|
|
|
|
|
|
|
/* --- GTest class --- */
|
|
|
|
|
typedef struct {
|
|
|
|
|
GObject object;
|
|
|
|
|
gint value;
|
|
|
|
|
gpointer test_pointer1;
|
|
|
|
|
gpointer test_pointer2;
|
|
|
|
|
} GTest;
|
|
|
|
|
typedef struct {
|
|
|
|
|
GObjectClass parent_class;
|
|
|
|
|
void (*test_signal1) (GTest * test, gint an_int);
|
|
|
|
|
void (*test_signal2) (GTest * test, gint an_int);
|
|
|
|
|
} GTestClass;
|
|
|
|
|
|
2007-11-20 16:00:20 +01:00
|
|
|
|
#define G_TYPE_TEST (my_test_get_type ())
|
|
|
|
|
#define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
|
|
|
|
|
#define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
|
|
|
|
|
#define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
|
|
|
|
|
#define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
|
|
|
|
|
#define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
2012-11-02 00:36:15 +01:00
|
|
|
|
static GType my_test_get_type (void);
|
2014-10-17 12:54:02 +02:00
|
|
|
|
G_DEFINE_TYPE (GTest, my_test, G_TYPE_OBJECT)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
2019-02-27 13:18:41 +01:00
|
|
|
|
/* 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;
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
|
|
|
|
/* --- functions --- */
|
|
|
|
|
static void
|
2007-11-20 16:00:20 +01:00
|
|
|
|
my_test_init (GTest * test)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 12:52:26 +01:00
|
|
|
|
g_test_message ("Init %p", test);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
|
|
|
|
test->value = 0;
|
|
|
|
|
test->test_pointer1 = TEST_POINTER1;
|
|
|
|
|
test->test_pointer2 = TEST_POINTER2;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 12:54:19 +01:00
|
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
PROP_TEST_PROP = 1,
|
|
|
|
|
} MyTestProperty;
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
SIGNAL_TEST_SIGNAL1,
|
|
|
|
|
SIGNAL_TEST_SIGNAL2,
|
|
|
|
|
} MyTestSignal;
|
|
|
|
|
|
|
|
|
|
static guint signals[SIGNAL_TEST_SIGNAL2 + 1] = { 0, };
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
|
|
|
|
static void
|
2007-11-20 16:00:20 +01:00
|
|
|
|
my_test_set_property (GObject *object,
|
2005-08-01 23:47:15 +02:00
|
|
|
|
guint prop_id,
|
|
|
|
|
const GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
|
|
|
|
{
|
2007-11-20 16:00:20 +01:00
|
|
|
|
GTest *test = MY_TEST (object);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
switch (prop_id)
|
|
|
|
|
{
|
2019-02-27 12:54:19 +01:00
|
|
|
|
case PROP_TEST_PROP:
|
2005-08-01 23:47:15 +02:00
|
|
|
|
test->value = g_value_get_int (value);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2007-11-20 16:00:20 +01:00
|
|
|
|
my_test_get_property (GObject *object,
|
2005-08-01 23:47:15 +02:00
|
|
|
|
guint prop_id,
|
|
|
|
|
GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
|
|
|
|
{
|
2007-11-20 16:00:20 +01:00
|
|
|
|
GTest *test = MY_TEST (object);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
switch (prop_id)
|
|
|
|
|
{
|
2019-02-27 12:54:19 +01:00
|
|
|
|
case PROP_TEST_PROP:
|
2005-08-01 23:47:15 +02:00
|
|
|
|
g_value_set_int (value, test->value);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2007-11-20 16:00:20 +01:00
|
|
|
|
my_test_test_signal2 (GTest *test,
|
|
|
|
|
gint an_int)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2007-11-20 16:00:20 +01:00
|
|
|
|
my_test_emit_test_signal1 (GTest *test,
|
|
|
|
|
gint vint)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 12:54:19 +01:00
|
|
|
|
g_signal_emit (G_OBJECT (test), signals[SIGNAL_TEST_SIGNAL1], 0, vint);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2007-11-20 16:00:20 +01:00
|
|
|
|
my_test_emit_test_signal2 (GTest *test,
|
|
|
|
|
gint vint)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 12:54:19 +01:00
|
|
|
|
g_signal_emit (G_OBJECT (test), signals[SIGNAL_TEST_SIGNAL2], 0, vint);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2007-11-20 16:00:20 +01:00
|
|
|
|
my_test_class_init (GTestClass *klass)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
2007-11-20 16:00:20 +01:00
|
|
|
|
gobject_class->set_property = my_test_set_property;
|
|
|
|
|
gobject_class->get_property = my_test_get_property;
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
2019-02-27 12:54:19 +01:00
|
|
|
|
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);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
2019-02-27 12:54:19 +01:00
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TEST_PROP,
|
2005-08-01 23:47:15 +02:00
|
|
|
|
g_param_spec_int ("test-prop", "Test Prop", "Test property",
|
|
|
|
|
0, 1, 0, G_PARAM_READWRITE));
|
2007-11-20 16:00:20 +01:00
|
|
|
|
klass->test_signal2 = my_test_test_signal2;
|
2005-08-01 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_closure (GClosure *closure)
|
|
|
|
|
{
|
|
|
|
|
/* try to produce high contention in closure->ref_count */
|
2018-11-04 01:00:00 +01:00
|
|
|
|
guint i = 0, n = g_random_int () % 199;
|
2005-08-01 23:47:15 +02:00
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
|
g_closure_ref (closure);
|
|
|
|
|
g_closure_sink (closure); /* NOP */
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
|
g_closure_unref (closure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gpointer
|
2019-02-27 13:18:41 +01:00
|
|
|
|
thread1_main (gpointer user_data)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 13:18:41 +01:00
|
|
|
|
TestClosureRefcountData *data = user_data;
|
2019-02-27 12:54:42 +01:00
|
|
|
|
guint i = 0;
|
|
|
|
|
|
2019-02-28 07:05:27 +01:00
|
|
|
|
for (i = 1; !g_atomic_int_get (&data->stopping); i++)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 13:18:41 +01:00
|
|
|
|
test_closure (data->closure);
|
2019-02-27 12:54:42 +01:00
|
|
|
|
if (i % 10000 == 0)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 12:52:26 +01:00
|
|
|
|
g_test_message ("Yielding from thread1");
|
2019-02-27 12:55:21 +01:00
|
|
|
|
g_thread_yield (); /* force context switch */
|
2019-02-27 13:18:41 +01:00
|
|
|
|
g_atomic_int_set (&data->seen_thread1, TRUE);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gpointer
|
2019-02-27 13:18:41 +01:00
|
|
|
|
thread2_main (gpointer user_data)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 13:18:41 +01:00
|
|
|
|
TestClosureRefcountData *data = user_data;
|
2019-02-27 12:54:42 +01:00
|
|
|
|
guint i = 0;
|
|
|
|
|
|
2019-02-28 07:05:27 +01:00
|
|
|
|
for (i = 1; !g_atomic_int_get (&data->stopping); i++)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 13:18:41 +01:00
|
|
|
|
test_closure (data->closure);
|
2019-02-27 12:54:42 +01:00
|
|
|
|
if (i % 10000 == 0)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 12:52:26 +01:00
|
|
|
|
g_test_message ("Yielding from thread2");
|
2019-02-27 12:55:21 +01:00
|
|
|
|
g_thread_yield (); /* force context switch */
|
2019-02-27 13:18:41 +01:00
|
|
|
|
g_atomic_int_set (&data->seen_thread2, TRUE);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_signal_handler (GTest *test,
|
|
|
|
|
gint vint,
|
2019-02-27 13:18:41 +01:00
|
|
|
|
gpointer user_data)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 13:18:41 +01:00
|
|
|
|
TestClosureRefcountData *data = user_data;
|
|
|
|
|
|
2019-02-27 12:55:36 +01:00
|
|
|
|
g_assert_true (test->test_pointer1 == TEST_POINTER1);
|
2019-02-27 13:18:41 +01:00
|
|
|
|
|
|
|
|
|
data->seen_signal_handler = TRUE;
|
|
|
|
|
data->seen_test_int1 |= vint == TEST_INT1;
|
|
|
|
|
data->seen_test_int2 |= vint == TEST_INT2;
|
2005-08-01 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2019-02-27 13:18:41 +01:00
|
|
|
|
destroy_data (gpointer user_data,
|
2005-08-01 23:47:15 +02:00
|
|
|
|
GClosure *closure)
|
|
|
|
|
{
|
2019-02-27 13:18:41 +01:00
|
|
|
|
TestClosureRefcountData *data = user_data;
|
|
|
|
|
|
|
|
|
|
data->seen_cleanup = TRUE;
|
|
|
|
|
g_assert_true (data->closure == closure);
|
2019-02-27 12:55:36 +01:00
|
|
|
|
g_assert_cmpint (closure->ref_count, ==, 0);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_emissions (GTest *test)
|
|
|
|
|
{
|
2007-11-20 16:00:20 +01:00
|
|
|
|
my_test_emit_test_signal1 (test, TEST_INT1);
|
|
|
|
|
my_test_emit_test_signal2 (test, TEST_INT2);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 12:53:55 +01:00
|
|
|
|
/* 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)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
|
|
|
|
GThread *thread1, *thread2;
|
2019-02-27 13:18:41 +01:00
|
|
|
|
TestClosureRefcountData test_data = { 0, };
|
2005-08-01 23:47:15 +02:00
|
|
|
|
GClosure *closure;
|
|
|
|
|
GTest *object;
|
2019-02-27 13:26:59 +01:00
|
|
|
|
guint i, n_iterations;
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
|
|
|
|
object = g_object_new (G_TYPE_TEST, NULL);
|
2019-02-27 13:18:41 +01:00
|
|
|
|
closure = g_cclosure_new (G_CALLBACK (test_signal_handler), &test_data, destroy_data);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
|
|
|
|
g_signal_connect_closure (object, "test-signal1", closure, FALSE);
|
|
|
|
|
g_signal_connect_closure (object, "test-signal2", closure, FALSE);
|
|
|
|
|
|
2019-02-27 13:18:41 +01:00
|
|
|
|
test_data.stopping = FALSE;
|
|
|
|
|
test_data.closure = closure;
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
2019-02-27 13:18:41 +01:00
|
|
|
|
thread1 = g_thread_new ("thread1", thread1_main, &test_data);
|
|
|
|
|
thread2 = g_thread_new ("thread2", thread2_main, &test_data);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
2017-12-18 11:47:29 +01:00
|
|
|
|
/* The 16-bit compare-and-swap operations currently used for closure
|
|
|
|
|
* refcounts are really slow on some ARM CPUs, notably Cortex-A57.
|
|
|
|
|
* Reduce the number of iterations so that the test completes in a
|
|
|
|
|
* finite time, but don't reduce it so much that the main thread
|
|
|
|
|
* starves the other threads and causes a test failure.
|
|
|
|
|
*
|
|
|
|
|
* https://gitlab.gnome.org/GNOME/glib/issues/1316
|
|
|
|
|
* aka https://bugs.debian.org/880883 */
|
|
|
|
|
#if defined(__aarch64__) || defined(__arm__)
|
2019-02-27 13:26:59 +01:00
|
|
|
|
n_iterations = 100000;
|
2017-12-18 11:47:29 +01:00
|
|
|
|
#else
|
2019-02-27 13:26:59 +01:00
|
|
|
|
n_iterations = 1000000;
|
2017-12-18 11:47:29 +01:00
|
|
|
|
#endif
|
2019-02-27 13:26:59 +01:00
|
|
|
|
|
|
|
|
|
/* Run the test for a reasonably high number of iterations, and ensure we
|
|
|
|
|
* don’t terminate until at least 10000 iterations have completed in both
|
|
|
|
|
* thread1 and thread2. Even though @n_iterations is high, we can’t guarantee
|
|
|
|
|
* that the scheduler allocates time fairly (or at all!) to thread1 or
|
|
|
|
|
* thread2. */
|
2019-02-28 07:05:27 +01:00
|
|
|
|
for (i = 1;
|
2019-02-27 13:26:59 +01:00
|
|
|
|
i < n_iterations ||
|
|
|
|
|
!g_atomic_int_get (&test_data.seen_thread1) ||
|
|
|
|
|
!g_atomic_int_get (&test_data.seen_thread2);
|
|
|
|
|
i++)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
|
|
|
|
test_emissions (object);
|
2019-02-27 12:54:42 +01:00
|
|
|
|
if (i % 10000 == 0)
|
2005-08-01 23:47:15 +02:00
|
|
|
|
{
|
2019-02-27 12:52:26 +01:00
|
|
|
|
g_test_message ("Yielding from main thread");
|
2019-02-27 12:55:21 +01:00
|
|
|
|
g_thread_yield (); /* force context switch */
|
2005-08-01 23:47:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 13:18:41 +01:00
|
|
|
|
g_atomic_int_set (&test_data.stopping, TRUE);
|
2019-02-27 12:52:26 +01:00
|
|
|
|
g_test_message ("Stopping");
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
|
|
|
|
/* wait for thread shutdown */
|
|
|
|
|
g_thread_join (thread1);
|
|
|
|
|
g_thread_join (thread2);
|
|
|
|
|
|
|
|
|
|
/* finalize object, destroy signals, run cleanup code */
|
|
|
|
|
g_object_unref (object);
|
|
|
|
|
|
2019-02-27 12:52:26 +01:00
|
|
|
|
g_test_message ("Stopped");
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
2019-02-27 13:18:41 +01:00
|
|
|
|
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);
|
2019-02-27 12:53:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (int argc,
|
|
|
|
|
char *argv[])
|
|
|
|
|
{
|
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
|
|
|
|
|
g_test_add_func ("/closure/refcount", test_closure_refcount);
|
2005-08-01 23:47:15 +02:00
|
|
|
|
|
2019-02-27 12:53:55 +01:00
|
|
|
|
return g_test_run ();
|
2005-08-01 23:47:15 +02:00
|
|
|
|
}
|