Hugo Beauzée-Luyssen pushed to branch master at VideoLAN / VLC


Commits:
5095a4c9 by Thomas Guillem at 2022-06-30T12:14:54+00:00
secret: move service check in a new function

- - - - -
3ee7d761 by Thomas Guillem at 2022-06-30T12:14:54+00:00
secret: fix check_service_running() stuck without Qt

g_bus_watch_name() need a GMainLoop. It was using the one created by Qt.

Before 8ba757d42d2e45f5feba14d93554a3d9ac29e257, the secret keystore was
never loaded without an UI so the issue was hidden.

Now that it can be loaded anytime, we need to setup a GMainLoop for
g_bus_watch_name().

- - - - -
bdc3d699 by Thomas Guillem at 2022-06-30T12:14:54+00:00
secret: cache check_service_running()

- - - - -


1 changed file:

- modules/keystore/secret.c


Changes:

=====================================
modules/keystore/secret.c
=====================================
@@ -265,7 +265,7 @@ Remove(vlc_keystore *p_keystore, const char *const 
ppsz_values[KEY_MAX])
 
 struct secrets_watch_data
 {
-    vlc_sem_t sem;
+    GMainLoop *loop;
     bool b_running;
 };
 
@@ -276,7 +276,7 @@ dbus_appeared_cb(GDBusConnection *connection, const gchar 
*name,
     (void) connection; (void) name; (void)name_owner;
     struct secrets_watch_data *p_watch_data = user_data;
     p_watch_data->b_running = true;
-    vlc_sem_post(&p_watch_data->sem);
+    g_main_loop_quit(p_watch_data->loop);
 }
 
 static void
@@ -285,8 +285,63 @@ dbus_vanished_cb(GDBusConnection *connection, const gchar 
*name,
 {
     (void) connection; (void) name;
     struct secrets_watch_data *p_watch_data = user_data;
-    p_watch_data->b_running = false;
-    vlc_sem_post(&p_watch_data->sem);
+    g_main_loop_quit(p_watch_data->loop);
+}
+
+static void mainloop_interrupted(void *user_data)
+{
+    struct secrets_watch_data *p_watch_data = user_data;
+    g_main_loop_quit(p_watch_data->loop);
+}
+
+static int
+check_service_running(void)
+{
+    /* Cache the return of this function that may take up to 200ms. This atomic
+     * doesn't prevent a cache miss (unlikely). This function can be called
+     * safely from any threads. */
+    static atomic_int cache_running = ATOMIC_VAR_INIT(0);
+    int running = atomic_load_explicit(&cache_running, memory_order_relaxed);
+    if (running != 0)
+        return running == 1 ? VLC_SUCCESS : VLC_EGENERIC;
+
+    /* First, check if secrets service is running using g_bus_watch_name().
+     * Indeed, secret_service_get_sync will spawn a service if it's not
+     * running, even on non Gnome environments */
+
+    GMainContext *ctx = g_main_context_default();
+    if (unlikely(ctx == NULL))
+        return VLC_ENOMEM;
+
+    GMainLoop *loop = g_main_loop_new(ctx, FALSE);
+    if (unlikely(loop == NULL))
+        return VLC_ENOMEM;
+
+    struct secrets_watch_data watch_data = {
+        .loop = loop,
+        .b_running = false,
+    };
+
+    guint i_id = g_bus_watch_name(G_BUS_TYPE_SESSION,
+                                  "org.freedesktop.secrets",
+                                  G_BUS_NAME_WATCHER_FLAGS_NONE,
+                                  dbus_appeared_cb, dbus_vanished_cb,
+                                  &watch_data, NULL);
+
+    vlc_interrupt_register(mainloop_interrupted, &watch_data);
+
+    g_main_loop_run(loop);
+
+    vlc_interrupt_unregister();
+
+    g_bus_unwatch_name(i_id);
+
+    g_main_loop_unref(loop);
+
+    atomic_store_explicit(&cache_running, watch_data.b_running ? 1 : -1,
+                          memory_order_relaxed);
+
+    return watch_data.b_running ? VLC_SUCCESS : VLC_EGENERIC;
 }
 
 static int
@@ -294,27 +349,9 @@ Open(vlc_object_t *p_this)
 {
     if (!p_this->force)
     {
-        /* First, check if secrets service is running using g_bus_watch_name().
-         * Indeed, secret_service_get_sync will spawn a service if it's not
-         * running, even on non Gnome environments */
-        struct secrets_watch_data watch_data;
-        watch_data.b_running = false;
-        vlc_sem_init(&watch_data.sem, 0);
-
-        guint i_id = g_bus_watch_name(G_BUS_TYPE_SESSION,
-                                      "org.freedesktop.secrets",
-                                      G_BUS_NAME_WATCHER_FLAGS_NONE,
-                                      dbus_appeared_cb, dbus_vanished_cb,
-                                      &watch_data, NULL);
-
-        /* We are guaranteed that one of the callbacks will be invoked after
-         * calling g_bus_watch_name */
-        vlc_sem_wait_i11e(&watch_data.sem);
-
-        g_bus_unwatch_name(i_id);
-
-        if (!watch_data.b_running)
-            return VLC_EGENERIC;
+        int ret = check_service_running();
+        if (ret != VLC_SUCCESS)
+            return ret;
     }
 
     GCancellable *p_canc = cancellable_register();



View it on GitLab: 
https://code.videolan.org/videolan/vlc/-/compare/402b07d5bc407b5bcb760973d307e96e9c64185a...bdc3d6993aec948186aad3eb24dc8dd96ff6f769

-- 
View it on GitLab: 
https://code.videolan.org/videolan/vlc/-/compare/402b07d5bc407b5bcb760973d307e96e9c64185a...bdc3d6993aec948186aad3eb24dc8dd96ff6f769
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance
_______________________________________________
vlc-commits mailing list
vlc-commits@videolan.org
https://mailman.videolan.org/listinfo/vlc-commits

Reply via email to