gdbusconnection: Factor out remove_signal_data_if_unused

No functional change, just removing some nesting. The check for whether
signal_data->subscribers is empty changes from a conditional that tests
whether it is into an early-return if it isn't.

A subsequent commit will add additional conditions that make us consider
a SignalData to be still in use and therefore not eligible to be removed.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2024-03-14 19:51:59 +00:00
parent 5d7ad6897c
commit 7d21b719ed

View File

@ -3660,44 +3660,21 @@ g_dbus_connection_signal_subscribe (GDBusConnection *connection,
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
/* called in any thread */ /*
/* must hold lock when calling this (except if connection->finalizing is TRUE) * Called in any thread.
* returns the number of removed subscribers */ * Must hold the connection lock when calling this, unless
static guint * connection->finalizing is TRUE.
unsubscribe_id_internal (GDBusConnection *connection, * May free signal_data, so do not dereference it after this.
guint subscription_id) */
static void
remove_signal_data_if_unused (GDBusConnection *connection,
SignalData *signal_data)
{ {
SignalData *signal_data;
GPtrArray *signal_data_array; GPtrArray *signal_data_array;
guint n;
guint n_removed = 0;
signal_data = g_hash_table_lookup (connection->map_id_to_signal_data, if (signal_data->subscribers->len != 0)
GUINT_TO_POINTER (subscription_id)); return;
if (signal_data == NULL)
{
/* Don't warn here, we may have thrown all subscriptions out when the connection was closed */
goto out;
}
for (n = 0; n < signal_data->subscribers->len; n++)
{
SignalSubscriber *subscriber = signal_data->subscribers->pdata[n];
if (subscriber->id != subscription_id)
continue;
/* Its OK to rearrange the array order using the fast #GPtrArray
* removal functions, since were going to exit the loop below anyway we
* never move on to the next element. Secondly, subscription IDs are
* guaranteed to be unique. */
g_warn_if_fail (g_hash_table_remove (connection->map_id_to_signal_data,
GUINT_TO_POINTER (subscription_id)));
n_removed++;
g_ptr_array_remove_index_fast (signal_data->subscribers, n);
if (signal_data->subscribers->len == 0)
{
g_warn_if_fail (g_hash_table_remove (connection->map_rule_to_signal_data, signal_data->rule)); g_warn_if_fail (g_hash_table_remove (connection->map_rule_to_signal_data, signal_data->rule));
signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array, signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array,
@ -3727,8 +3704,44 @@ unsubscribe_id_internal (GDBusConnection *connection,
} }
signal_data_free (signal_data); signal_data_free (signal_data);
}
/* called in any thread */
/* must hold lock when calling this (except if connection->finalizing is TRUE)
* returns the number of removed subscribers */
static guint
unsubscribe_id_internal (GDBusConnection *connection,
guint subscription_id)
{
SignalData *signal_data;
guint n;
guint n_removed = 0;
signal_data = g_hash_table_lookup (connection->map_id_to_signal_data,
GUINT_TO_POINTER (subscription_id));
if (signal_data == NULL)
{
/* Don't warn here, we may have thrown all subscriptions out when the connection was closed */
goto out;
} }
for (n = 0; n < signal_data->subscribers->len; n++)
{
SignalSubscriber *subscriber = signal_data->subscribers->pdata[n];
if (subscriber->id != subscription_id)
continue;
/* Its OK to rearrange the array order using the fast #GPtrArray
* removal functions, since were going to exit the loop below anyway we
* never move on to the next element. Secondly, subscription IDs are
* guaranteed to be unique. */
g_warn_if_fail (g_hash_table_remove (connection->map_id_to_signal_data,
GUINT_TO_POINTER (subscription_id)));
n_removed++;
g_ptr_array_remove_index_fast (signal_data->subscribers, n);
/* May free signal_data */
remove_signal_data_if_unused (connection, signal_data);
goto out; goto out;
} }