glib-aux: add nm_g_bus_get_blocking() helper

This commit is contained in:
Thomas Haller 2021-08-03 10:07:13 +02:00
parent b7c77d51eb
commit 133dc3d43c
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 52 additions and 0 deletions

View file

@ -403,3 +403,53 @@ _nm_dbus_error_is(GError *error, ...)
return FALSE;
}
/*****************************************************************************/
typedef struct {
GDBusConnection **p_dbus_connection;
GError ** p_error;
} BusGetData;
static void
_bus_get_cb(GObject *source, GAsyncResult *result, gpointer user_data)
{
BusGetData *data = user_data;
*data->p_dbus_connection = g_bus_get_finish(result, data->p_error);
}
/**
* nm_g_bus_get_blocking:
* @cancellable: (allow-none): a #GCancellable to abort the operation.
* @error: (allow-none): the error.
*
* This calls g_bus_get(), but iterates the current (thread-default) GMainContext
* until the response is ready. As such, it's similar to g_bus_get_sync(),
* but it allows to cancel the operation (without having multiple threads).
*
* Returns: (transfer full): the new #GDBusConnection or %NULL on error.
*/
GDBusConnection *
nm_g_bus_get_blocking(GCancellable *cancellable, GError **error)
{
gs_free_error GError *local_error = NULL;
gs_unref_object GDBusConnection *dbus_connection = NULL;
GMainContext * main_context = g_main_context_get_thread_default();
BusGetData data = {
.p_dbus_connection = &dbus_connection,
.p_error = &local_error,
};
g_bus_get(G_BUS_TYPE_SYSTEM, cancellable, _bus_get_cb, &data);
while (!dbus_connection && !local_error)
g_main_context_iteration(main_context, TRUE);
if (!dbus_connection) {
g_propagate_error(error, g_steal_pointer(&local_error));
return NULL;
}
return g_steal_pointer(&dbus_connection);
}

View file

@ -215,4 +215,6 @@ gboolean _nm_dbus_error_is(GError *error, ...) G_GNUC_NULL_TERMINATED;
/*****************************************************************************/
GDBusConnection *nm_g_bus_get_blocking(GCancellable *cancellable, GError **error);
#endif /* __NM_DBUS_AUX_H__ */