GDBusConnection: use GWeakRef to make the singletons thread-safe

Bug: https://bugzilla.gnome.org/show_bug.cgi?id=665211

https://bugzilla.gnome.org/show_bug.cgi?id=548954
This commit is contained in:
Simon McVittie 2011-12-06 12:44:36 +00:00 committed by Ryan Lortie
parent 146aa7aa17
commit 1e09bfc77c

View File

@ -208,8 +208,8 @@ struct _GDBusConnectionClass
G_LOCK_DEFINE_STATIC (message_bus_lock); G_LOCK_DEFINE_STATIC (message_bus_lock);
static GDBusConnection *the_session_bus = NULL; static GWeakRef the_session_bus;
static GDBusConnection *the_system_bus = NULL; static GWeakRef the_system_bus;
/* Extra pseudo-member of GDBusSendMessageFlags. /* Extra pseudo-member of GDBusSendMessageFlags.
* Set by initable_init() to indicate that despite not being initialized yet, * Set by initable_init() to indicate that despite not being initialized yet,
@ -606,14 +606,6 @@ g_dbus_connection_dispose (GObject *object)
GDBusConnection *connection = G_DBUS_CONNECTION (object); GDBusConnection *connection = G_DBUS_CONNECTION (object);
G_LOCK (message_bus_lock); G_LOCK (message_bus_lock);
if (connection == the_session_bus)
{
the_session_bus = NULL;
}
else if (connection == the_system_bus)
{
the_system_bus = NULL;
}
CONNECTION_LOCK (connection); CONNECTION_LOCK (connection);
if (connection->worker != NULL) if (connection->worker != NULL)
{ {
@ -6652,11 +6644,11 @@ distribute_method_call (GDBusConnection *connection,
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
/* Called in any user thread, with the message_bus_lock held. */ /* Called in any user thread, with the message_bus_lock held. */
static GDBusConnection ** static GWeakRef *
message_bus_get_singleton (GBusType bus_type, message_bus_get_singleton (GBusType bus_type,
GError **error) GError **error)
{ {
GDBusConnection **ret; GWeakRef *ret;
const gchar *starter_bus; const gchar *starter_bus;
ret = NULL; ret = NULL;
@ -6720,7 +6712,7 @@ get_uninitialized_connection (GBusType bus_type,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
GDBusConnection **singleton; GWeakRef *singleton;
GDBusConnection *ret; GDBusConnection *ret;
ret = NULL; ret = NULL;
@ -6730,24 +6722,24 @@ get_uninitialized_connection (GBusType bus_type,
if (singleton == NULL) if (singleton == NULL)
goto out; goto out;
if (*singleton == NULL) ret = g_weak_ref_get (singleton);
if (ret == NULL)
{ {
gchar *address; gchar *address;
address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error); address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
if (address == NULL) if (address == NULL)
goto out; goto out;
ret = *singleton = g_object_new (G_TYPE_DBUS_CONNECTION, ret = g_object_new (G_TYPE_DBUS_CONNECTION,
"address", address, "address", address,
"flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | "flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION, G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
"exit-on-close", TRUE, "exit-on-close", TRUE,
NULL); NULL);
g_weak_ref_set (singleton, ret);
g_free (address); g_free (address);
} }
else
{
ret = g_object_ref (*singleton);
}
g_assert (ret != NULL); g_assert (ret != NULL);