2008-02-05 18:42:09 +01:00
|
|
|
/* GLib testing framework examples and tests
|
|
|
|
* Copyright (C) 2008 Imendio AB
|
|
|
|
* Authors: Tim Janik
|
|
|
|
*
|
2022-11-02 13:34:19 +01:00
|
|
|
* SPDX-License-Identifier: LicenseRef-old-glib-tests
|
|
|
|
*
|
2008-02-05 18:42:09 +01:00
|
|
|
* This work is provided "as is"; redistribution and modification
|
|
|
|
* in whole or in part, in any medium, physical or electronic is
|
|
|
|
* permitted without restriction.
|
|
|
|
*
|
|
|
|
* This work 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.
|
|
|
|
*/
|
2020-07-27 16:40:13 +02:00
|
|
|
|
|
|
|
#ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
|
2011-10-12 06:37:02 +02:00
|
|
|
#define GLIB_DISABLE_DEPRECATION_WARNINGS
|
2020-07-27 16:40:13 +02:00
|
|
|
#endif
|
|
|
|
|
2008-02-05 18:42:09 +01:00
|
|
|
#include <glib.h>
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
2020-11-11 20:22:49 +01:00
|
|
|
static int mtsafe_call_counter = 0; /* multi thread safe call counter, must be accessed atomically */
|
|
|
|
static int unsafe_call_counter = 0; /* single-threaded call counter */
|
2012-11-05 17:07:16 +01:00
|
|
|
static GCond sync_cond;
|
|
|
|
static GMutex sync_mutex;
|
2008-02-05 18:42:09 +01:00
|
|
|
|
|
|
|
#define NUM_COUNTER_INCREMENTS 100000
|
|
|
|
|
|
|
|
static void
|
|
|
|
call_counter_init (gpointer tclass)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < NUM_COUNTER_INCREMENTS; i++)
|
|
|
|
{
|
|
|
|
int saved_unsafe_call_counter = unsafe_call_counter;
|
2010-01-20 18:53:08 +01:00
|
|
|
g_atomic_int_add (&mtsafe_call_counter, 1); /* real call count update */
|
|
|
|
g_thread_yield(); /* let concurrent threads corrupt the unsafe_call_counter state */
|
|
|
|
unsafe_call_counter = 1 + saved_unsafe_call_counter; /* non-atomic counter update */
|
2008-02-05 18:42:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 00:36:52 +01:00
|
|
|
static void interface_per_class_init (void) { call_counter_init (NULL); }
|
2008-02-05 18:42:09 +01:00
|
|
|
|
|
|
|
/* define 3 test interfaces */
|
|
|
|
typedef GTypeInterface MyFace0Interface;
|
2011-10-17 03:11:11 +02:00
|
|
|
static GType my_face0_get_type (void);
|
2014-10-17 12:54:02 +02:00
|
|
|
G_DEFINE_INTERFACE (MyFace0, my_face0, G_TYPE_OBJECT)
|
2008-02-05 18:42:09 +01:00
|
|
|
static void my_face0_default_init (MyFace0Interface *iface) { call_counter_init (iface); }
|
|
|
|
typedef GTypeInterface MyFace1Interface;
|
2011-10-17 03:11:11 +02:00
|
|
|
static GType my_face1_get_type (void);
|
2014-10-17 12:54:02 +02:00
|
|
|
G_DEFINE_INTERFACE (MyFace1, my_face1, G_TYPE_OBJECT)
|
2008-02-05 18:42:09 +01:00
|
|
|
static void my_face1_default_init (MyFace1Interface *iface) { call_counter_init (iface); }
|
|
|
|
|
|
|
|
/* define 3 test objects, adding interfaces 0 & 1, and adding interface 2 after class initialization */
|
|
|
|
typedef GObject MyTester0;
|
|
|
|
typedef GObjectClass MyTester0Class;
|
2011-10-17 03:11:11 +02:00
|
|
|
static GType my_tester0_get_type (void);
|
2008-02-05 18:42:09 +01:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (MyTester0, my_tester0, G_TYPE_OBJECT,
|
2014-10-17 12:54:02 +02:00
|
|
|
G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init)
|
|
|
|
G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init))
|
2008-02-05 18:42:09 +01:00
|
|
|
static void my_tester0_init (MyTester0*t) {}
|
|
|
|
static void my_tester0_class_init (MyTester0Class*c) { call_counter_init (c); }
|
|
|
|
typedef GObject MyTester1;
|
|
|
|
typedef GObjectClass MyTester1Class;
|
2012-11-05 17:07:16 +01:00
|
|
|
|
|
|
|
/* Disabled for now (see https://bugzilla.gnome.org/show_bug.cgi?id=687659) */
|
|
|
|
#if 0
|
|
|
|
typedef GTypeInterface MyFace2Interface;
|
|
|
|
static GType my_face2_get_type (void);
|
2014-10-17 12:54:02 +02:00
|
|
|
G_DEFINE_INTERFACE (MyFace2, my_face2, G_TYPE_OBJECT)
|
2012-11-05 17:07:16 +01:00
|
|
|
static void my_face2_default_init (MyFace2Interface *iface) { call_counter_init (iface); }
|
|
|
|
|
2011-10-17 03:11:11 +02:00
|
|
|
static GType my_tester1_get_type (void);
|
2008-02-05 18:42:09 +01:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (MyTester1, my_tester1, G_TYPE_OBJECT,
|
2014-10-17 12:54:02 +02:00
|
|
|
G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init)
|
|
|
|
G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init))
|
2008-02-05 18:42:09 +01:00
|
|
|
static void my_tester1_init (MyTester1*t) {}
|
|
|
|
static void my_tester1_class_init (MyTester1Class*c) { call_counter_init (c); }
|
|
|
|
typedef GObject MyTester2;
|
|
|
|
typedef GObjectClass MyTester2Class;
|
2011-10-17 03:11:11 +02:00
|
|
|
static GType my_tester2_get_type (void);
|
2008-02-05 18:42:09 +01:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (MyTester2, my_tester2, G_TYPE_OBJECT,
|
2014-10-17 12:54:02 +02:00
|
|
|
G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init)
|
|
|
|
G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init))
|
2008-02-05 18:42:09 +01:00
|
|
|
static void my_tester2_init (MyTester2*t) {}
|
|
|
|
static void my_tester2_class_init (MyTester2Class*c) { call_counter_init (c); }
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
tester_init_thread (gpointer data)
|
|
|
|
{
|
|
|
|
const GInterfaceInfo face2_interface_info = { (GInterfaceInitFunc) interface_per_class_init, NULL, NULL };
|
|
|
|
gpointer klass;
|
2020-06-12 15:02:30 +02:00
|
|
|
/* first, synchronize with other threads,
|
2008-02-05 18:42:09 +01:00
|
|
|
* then run interface and class initializers,
|
|
|
|
* using unsafe_call_counter concurrently
|
|
|
|
*/
|
2011-10-05 01:04:41 +02:00
|
|
|
g_mutex_lock (&sync_mutex);
|
|
|
|
g_mutex_unlock (&sync_mutex);
|
2008-02-05 18:42:09 +01:00
|
|
|
/* test default interface initialization for face0 */
|
|
|
|
g_type_default_interface_unref (g_type_default_interface_ref (my_face0_get_type()));
|
|
|
|
/* test class initialization, face0 per-class initializer, face1 default and per-class initializer */
|
|
|
|
klass = g_type_class_ref ((GType) data);
|
|
|
|
/* test face2 default and per-class initializer, after class_init */
|
|
|
|
g_type_add_interface_static (G_TYPE_FROM_CLASS (klass), my_face2_get_type(), &face2_interface_info);
|
|
|
|
/* cleanups */
|
|
|
|
g_type_class_unref (klass);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_threaded_class_init (void)
|
|
|
|
{
|
2013-11-07 22:57:27 +01:00
|
|
|
GThread *t1, *t2, *t3;
|
2012-08-23 22:53:13 +02:00
|
|
|
|
2008-02-05 18:42:09 +01:00
|
|
|
/* pause newly created threads */
|
2011-10-05 01:04:41 +02:00
|
|
|
g_mutex_lock (&sync_mutex);
|
2012-08-23 22:53:13 +02:00
|
|
|
|
2008-02-05 18:42:09 +01:00
|
|
|
/* create threads */
|
2013-11-07 22:57:27 +01:00
|
|
|
t1 = g_thread_create (tester_init_thread, (gpointer) my_tester0_get_type(), TRUE, NULL);
|
|
|
|
t2 = g_thread_create (tester_init_thread, (gpointer) my_tester1_get_type(), TRUE, NULL);
|
|
|
|
t3 = g_thread_create (tester_init_thread, (gpointer) my_tester2_get_type(), TRUE, NULL);
|
2012-08-23 22:53:13 +02:00
|
|
|
|
2008-02-05 18:42:09 +01:00
|
|
|
/* execute threads */
|
2011-10-05 01:04:41 +02:00
|
|
|
g_mutex_unlock (&sync_mutex);
|
2008-02-05 18:42:09 +01:00
|
|
|
while (g_atomic_int_get (&mtsafe_call_counter) < (3 + 3 + 3 * 3) * NUM_COUNTER_INCREMENTS)
|
|
|
|
{
|
|
|
|
if (g_test_verbose())
|
2015-05-11 18:03:00 +02:00
|
|
|
g_printerr ("Initializers counted: %u\n", g_atomic_int_get (&mtsafe_call_counter));
|
2008-02-05 18:42:09 +01:00
|
|
|
g_usleep (50 * 1000); /* wait for threads to complete */
|
|
|
|
}
|
|
|
|
if (g_test_verbose())
|
2015-05-11 18:03:00 +02:00
|
|
|
g_printerr ("Total initializers: %u\n", g_atomic_int_get (&mtsafe_call_counter));
|
2008-02-05 18:42:09 +01:00
|
|
|
/* ensure non-corrupted counter updates */
|
|
|
|
g_assert_cmpint (g_atomic_int_get (&mtsafe_call_counter), ==, unsafe_call_counter);
|
2013-11-07 22:57:27 +01:00
|
|
|
|
|
|
|
g_thread_join (t1);
|
|
|
|
g_thread_join (t2);
|
|
|
|
g_thread_join (t3);
|
2008-02-05 18:42:09 +01:00
|
|
|
}
|
2012-11-05 17:07:16 +01:00
|
|
|
#endif
|
2008-02-05 18:42:09 +01:00
|
|
|
|
2008-06-10 13:35:46 +02:00
|
|
|
typedef struct {
|
|
|
|
GObject parent;
|
|
|
|
char *name;
|
|
|
|
} PropTester;
|
|
|
|
typedef GObjectClass PropTesterClass;
|
2011-10-17 03:11:11 +02:00
|
|
|
static GType prop_tester_get_type (void);
|
2014-10-17 12:54:02 +02:00
|
|
|
G_DEFINE_TYPE (PropTester, prop_tester, G_TYPE_OBJECT)
|
2008-06-10 13:35:46 +02:00
|
|
|
#define PROP_NAME 1
|
|
|
|
static void
|
|
|
|
prop_tester_init (PropTester* t)
|
|
|
|
{
|
|
|
|
if (t->name == NULL)
|
2018-10-04 13:02:18 +02:00
|
|
|
{ } /* needs unit test framework initialization: g_test_bug ("race initializing properties"); */
|
2008-06-10 13:35:46 +02:00
|
|
|
}
|
|
|
|
static void
|
|
|
|
prop_tester_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{}
|
|
|
|
static void
|
|
|
|
prop_tester_class_init (PropTesterClass *c)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
GParamSpec *param;
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (c);
|
|
|
|
|
|
|
|
gobject_class->set_property = prop_tester_set_property; /* silence GObject checks */
|
|
|
|
|
2011-10-05 01:04:41 +02:00
|
|
|
g_mutex_lock (&sync_mutex);
|
|
|
|
g_cond_signal (&sync_cond);
|
|
|
|
g_mutex_unlock (&sync_mutex);
|
2008-06-10 13:35:46 +02:00
|
|
|
|
|
|
|
for (i = 0; i < 100; i++) /* wait a bit. */
|
|
|
|
g_thread_yield();
|
|
|
|
|
|
|
|
call_counter_init (c);
|
|
|
|
param = g_param_spec_string ("name", "name_i18n",
|
|
|
|
"yet-more-wasteful-i18n",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE |
|
|
|
|
G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB |
|
|
|
|
G_PARAM_STATIC_NICK);
|
|
|
|
g_object_class_install_property (gobject_class, PROP_NAME, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
object_create (gpointer data)
|
|
|
|
{
|
|
|
|
GObject *obj = g_object_new (prop_tester_get_type(), "name", "fish", NULL);
|
|
|
|
g_object_unref (obj);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_threaded_object_init (void)
|
|
|
|
{
|
|
|
|
GThread *creator;
|
2011-10-05 01:04:41 +02:00
|
|
|
g_mutex_lock (&sync_mutex);
|
2008-06-10 13:35:46 +02:00
|
|
|
|
|
|
|
creator = g_thread_create (object_create, NULL, TRUE, NULL);
|
|
|
|
/* really provoke the race */
|
2011-10-05 01:04:41 +02:00
|
|
|
g_cond_wait (&sync_cond, &sync_mutex);
|
2008-06-10 13:35:46 +02:00
|
|
|
|
|
|
|
object_create (NULL);
|
2011-10-05 01:04:41 +02:00
|
|
|
g_mutex_unlock (&sync_mutex);
|
2008-06-10 13:35:46 +02:00
|
|
|
|
|
|
|
g_thread_join (creator);
|
|
|
|
}
|
|
|
|
|
2011-12-07 16:04:13 +01:00
|
|
|
typedef struct {
|
|
|
|
MyTester0 *strong;
|
|
|
|
guint unref_delay;
|
|
|
|
} UnrefInThreadData;
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
unref_in_thread (gpointer p)
|
|
|
|
{
|
|
|
|
UnrefInThreadData *data = p;
|
|
|
|
|
|
|
|
g_usleep (data->unref_delay);
|
|
|
|
g_object_unref (data->strong);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* undefine to see this test fail without GWeakRef */
|
|
|
|
#define HAVE_G_WEAK_REF
|
|
|
|
|
|
|
|
#define SLEEP_MIN_USEC 1
|
|
|
|
#define SLEEP_MAX_USEC 10
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_threaded_weak_ref (void)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
guint get_wins = 0, unref_wins = 0;
|
|
|
|
guint n;
|
|
|
|
|
|
|
|
if (g_test_thorough ())
|
|
|
|
n = NUM_COUNTER_INCREMENTS;
|
|
|
|
else
|
|
|
|
n = NUM_COUNTER_INCREMENTS / 20;
|
|
|
|
|
2018-05-10 20:30:49 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* On Windows usleep has millisecond resolution and gets rounded up
|
|
|
|
* leading to the test running for a long time. */
|
|
|
|
n /= 10;
|
|
|
|
#endif
|
|
|
|
|
2011-12-07 16:04:13 +01:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
UnrefInThreadData data;
|
|
|
|
#ifdef HAVE_G_WEAK_REF
|
|
|
|
/* GWeakRef<MyTester0> in C++ terms */
|
|
|
|
GWeakRef weak;
|
|
|
|
#else
|
|
|
|
gpointer weak;
|
|
|
|
#endif
|
|
|
|
MyTester0 *strengthened;
|
|
|
|
guint get_delay;
|
|
|
|
GThread *thread;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (g_test_verbose () && (i % (n/20)) == 0)
|
2015-05-11 18:03:00 +02:00
|
|
|
g_printerr ("%u%%\n", ((i * 100) / n));
|
2011-12-07 16:04:13 +01:00
|
|
|
|
|
|
|
/* Have an object and a weak ref to it */
|
|
|
|
data.strong = g_object_new (my_tester0_get_type (), NULL);
|
|
|
|
|
|
|
|
#ifdef HAVE_G_WEAK_REF
|
|
|
|
g_weak_ref_init (&weak, data.strong);
|
|
|
|
#else
|
|
|
|
weak = data.strong;
|
|
|
|
g_object_add_weak_pointer ((GObject *) weak, &weak);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Delay for a random time on each side of the race, to perturb the
|
|
|
|
* timing. Ideally, we want each side to win half the races; on
|
|
|
|
* smcv's laptop, these timings are about right.
|
|
|
|
*/
|
|
|
|
data.unref_delay = g_random_int_range (SLEEP_MIN_USEC / 2, SLEEP_MAX_USEC / 2);
|
|
|
|
get_delay = g_random_int_range (SLEEP_MIN_USEC, SLEEP_MAX_USEC);
|
|
|
|
|
|
|
|
/* One half of the race is to unref the shared object */
|
|
|
|
thread = g_thread_create (unref_in_thread, &data, TRUE, &error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
/* The other half of the race is to get the object from the "global
|
|
|
|
* singleton"
|
|
|
|
*/
|
|
|
|
g_usleep (get_delay);
|
|
|
|
|
|
|
|
#ifdef HAVE_G_WEAK_REF
|
|
|
|
strengthened = g_weak_ref_get (&weak);
|
|
|
|
#else
|
|
|
|
/* Spot the unsafe pointer access! In GDBusConnection this is rather
|
|
|
|
* better-hidden, but ends up with essentially the same thing, albeit
|
|
|
|
* cleared in dispose() rather than by a traditional weak pointer
|
|
|
|
*/
|
|
|
|
strengthened = weak;
|
|
|
|
|
|
|
|
if (strengthened != NULL)
|
|
|
|
g_object_ref (strengthened);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (strengthened != NULL)
|
|
|
|
g_assert (G_IS_OBJECT (strengthened));
|
|
|
|
|
|
|
|
/* Wait for the thread to run */
|
|
|
|
g_thread_join (thread);
|
|
|
|
|
|
|
|
if (strengthened != NULL)
|
|
|
|
{
|
|
|
|
get_wins++;
|
|
|
|
g_assert (G_IS_OBJECT (strengthened));
|
|
|
|
g_object_unref (strengthened);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unref_wins++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_G_WEAK_REF
|
|
|
|
g_weak_ref_clear (&weak);
|
|
|
|
#else
|
|
|
|
if (weak != NULL)
|
|
|
|
g_object_remove_weak_pointer (weak, &weak);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_test_verbose ())
|
2015-05-11 18:03:00 +02:00
|
|
|
g_printerr ("Race won by get %u times, unref %u times\n",
|
2011-12-07 16:04:13 +01:00
|
|
|
get_wins, unref_wins);
|
|
|
|
}
|
|
|
|
|
2021-04-24 04:39:17 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GObject *object;
|
|
|
|
GWeakRef *weak;
|
|
|
|
gint started; /* (atomic) */
|
|
|
|
gint finished; /* (atomic) */
|
|
|
|
gint disposing; /* (atomic) */
|
|
|
|
} ThreadedWeakRefData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_weak_ref_disposed (gpointer data,
|
|
|
|
GObject *gobj)
|
|
|
|
{
|
|
|
|
ThreadedWeakRefData *thread_data = data;
|
|
|
|
|
|
|
|
/* Wait until the thread has started */
|
|
|
|
while (!g_atomic_int_get (&thread_data->started))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
g_atomic_int_set (&thread_data->disposing, 1);
|
|
|
|
|
|
|
|
/* Wait for the thread to act, so that the object is still valid */
|
|
|
|
while (!g_atomic_int_get (&thread_data->finished))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
g_atomic_int_set (&thread_data->disposing, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
on_other_thread_weak_ref (gpointer user_data)
|
|
|
|
{
|
|
|
|
ThreadedWeakRefData *thread_data = user_data;
|
|
|
|
GObject *object = thread_data->object;
|
|
|
|
|
|
|
|
g_atomic_int_set (&thread_data->started, 1);
|
|
|
|
|
|
|
|
/* Ensure we've started disposal */
|
|
|
|
while (!g_atomic_int_get (&thread_data->disposing))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
g_object_ref (object);
|
|
|
|
g_weak_ref_set (thread_data->weak, object);
|
|
|
|
g_object_unref (object);
|
|
|
|
|
|
|
|
g_assert_cmpint (thread_data->disposing, ==, 1);
|
|
|
|
g_atomic_int_set (&thread_data->finished, 1);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_threaded_weak_ref_finalization (void)
|
|
|
|
{
|
|
|
|
GObject *obj = g_object_new (G_TYPE_OBJECT, NULL);
|
|
|
|
GWeakRef weak = { { GUINT_TO_POINTER (0xDEADBEEFU) } };
|
|
|
|
ThreadedWeakRefData thread_data = {
|
|
|
|
.object = obj, .weak = &weak, .started = 0, .finished = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2390");
|
|
|
|
g_test_summary ("Test that a weak ref added by another thread during dispose "
|
|
|
|
"of a GObject is cleared during finalisation. "
|
|
|
|
"Use on_weak_ref_disposed() to synchronize the other thread "
|
|
|
|
"with the dispose vfunc.");
|
|
|
|
|
|
|
|
g_weak_ref_init (&weak, NULL);
|
|
|
|
g_object_weak_ref (obj, on_weak_ref_disposed, &thread_data);
|
|
|
|
|
|
|
|
g_assert_cmpint (obj->ref_count, ==, 1);
|
|
|
|
g_thread_unref (g_thread_new ("on_other_thread",
|
|
|
|
on_other_thread_weak_ref,
|
|
|
|
&thread_data));
|
|
|
|
g_object_unref (obj);
|
|
|
|
|
|
|
|
/* This is what this test is about: at this point the weak reference
|
|
|
|
* should have been unset (and not point to a dead object either). */
|
|
|
|
g_assert_null (g_weak_ref_get (&weak));
|
|
|
|
}
|
|
|
|
|
2021-04-28 21:54:45 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GObject *object;
|
|
|
|
int done; /* (atomic) */
|
|
|
|
int toggles; /* (atomic) */
|
|
|
|
} ToggleNotifyThreadData;
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
on_reffer_thread (gpointer user_data)
|
|
|
|
{
|
|
|
|
ToggleNotifyThreadData *thread_data = user_data;
|
|
|
|
|
|
|
|
while (!g_atomic_int_get (&thread_data->done))
|
|
|
|
{
|
|
|
|
g_object_ref (thread_data->object);
|
|
|
|
g_object_unref (thread_data->object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_toggle_notify (gpointer data,
|
|
|
|
GObject *object,
|
|
|
|
gboolean is_last_ref)
|
|
|
|
{
|
|
|
|
/* Anything could be put here, but we don't care for this test.
|
|
|
|
* Actually having this empty made the bug to happen more frequently (being
|
|
|
|
* timing related).
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
on_toggler_thread (gpointer user_data)
|
|
|
|
{
|
|
|
|
ToggleNotifyThreadData *thread_data = user_data;
|
|
|
|
|
|
|
|
while (!g_atomic_int_get (&thread_data->done))
|
|
|
|
{
|
|
|
|
g_object_ref (thread_data->object);
|
|
|
|
g_object_remove_toggle_ref (thread_data->object, on_toggle_notify, thread_data);
|
|
|
|
g_object_add_toggle_ref (thread_data->object, on_toggle_notify, thread_data);
|
|
|
|
g_object_unref (thread_data->object);
|
|
|
|
g_atomic_int_add (&thread_data->toggles, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_threaded_toggle_notify (void)
|
|
|
|
{
|
|
|
|
GObject *object = g_object_new (G_TYPE_OBJECT, NULL);
|
|
|
|
ToggleNotifyThreadData data = { object, FALSE, 0 };
|
|
|
|
GThread *threads[3];
|
|
|
|
gsize i;
|
|
|
|
|
|
|
|
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/2394");
|
|
|
|
g_test_summary ("Test that toggle reference notifications can be changed "
|
|
|
|
"safely from another (the main) thread without causing the "
|
|
|
|
"notifying thread to abort");
|
|
|
|
|
|
|
|
g_object_add_toggle_ref (object, on_toggle_notify, &data);
|
|
|
|
g_object_unref (object);
|
|
|
|
|
|
|
|
g_assert_cmpint (object->ref_count, ==, 1);
|
|
|
|
threads[0] = g_thread_new ("on_reffer_thread", on_reffer_thread, &data);
|
|
|
|
threads[1] = g_thread_new ("on_another_reffer_thread", on_reffer_thread, &data);
|
|
|
|
threads[2] = g_thread_new ("on_main_toggler_thread", on_toggler_thread, &data);
|
|
|
|
|
|
|
|
/* We need to wait here for the threads to run for a bit in order to make the
|
|
|
|
* race to happen, so we wait for an high number of toggle changes to be met
|
|
|
|
* so that we can be consistent on each platform.
|
|
|
|
*/
|
|
|
|
while (g_atomic_int_get (&data.toggles) < 1000000)
|
|
|
|
;
|
|
|
|
g_atomic_int_set (&data.done, TRUE);
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (threads); i++)
|
|
|
|
g_thread_join (threads[i]);
|
|
|
|
|
|
|
|
g_assert_cmpint (object->ref_count, ==, 1);
|
|
|
|
g_clear_object (&object);
|
|
|
|
}
|
|
|
|
|
2008-02-05 18:42:09 +01:00
|
|
|
int
|
|
|
|
main (int argc,
|
|
|
|
char *argv[])
|
|
|
|
{
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
2012-11-05 17:07:16 +01:00
|
|
|
/* g_test_add_func ("/GObject/threaded-class-init", test_threaded_class_init); */
|
2008-06-10 13:35:46 +02:00
|
|
|
g_test_add_func ("/GObject/threaded-object-init", test_threaded_object_init);
|
2011-12-07 16:04:13 +01:00
|
|
|
g_test_add_func ("/GObject/threaded-weak-ref", test_threaded_weak_ref);
|
2021-04-24 04:39:17 +02:00
|
|
|
g_test_add_func ("/GObject/threaded-weak-ref/on-finalization",
|
|
|
|
test_threaded_weak_ref_finalization);
|
2021-04-28 21:54:45 +02:00
|
|
|
g_test_add_func ("/GObject/threaded-toggle-notify",
|
|
|
|
test_threaded_toggle_notify);
|
2008-02-05 18:42:09 +01:00
|
|
|
|
|
|
|
return g_test_run();
|
|
|
|
}
|