[vlc-devel] commit: XDG screensaver: call waitpid immediately ( Rémi Denis-Courmont )

git version control git at videolan.org
Wed Jan 6 21:51:04 CET 2010


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Jan  6 22:50:05 2010 +0200| [64e4f68bfee806c57770f1feca7eca93312cef32] | committer: Rémi Denis-Courmont 

XDG screensaver: call waitpid immediately

This requires one more thread :( but at least people won't report bug
about VLC leaving a defunct child process... Even though I doubt it
would have been a real problem.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=64e4f68bfee806c57770f1feca7eca93312cef32
---

 modules/misc/xdg-screensaver.c |   93 ++++++++++++++++++++++++++--------------
 1 files changed, 61 insertions(+), 32 deletions(-)

diff --git a/modules/misc/xdg-screensaver.c b/modules/misc/xdg-screensaver.c
index 670d26c..429485d 100644
--- a/modules/misc/xdg-screensaver.c
+++ b/modules/misc/xdg-screensaver.c
@@ -42,11 +42,14 @@ vlc_module_end ()
 
 struct vlc_inhibit_sys
 {
-    pid_t pid;
-    char id[11];
+    vlc_thread_t thread;
+    vlc_cond_t wait;
+    vlc_mutex_t lock;
+    bool suspend;
 };
 
 static void Inhibit (vlc_inhibit_t *ih, bool suspend);
+static void *Thread (void *);
 
 static int Open (vlc_object_t *obj)
 {
@@ -58,8 +61,16 @@ static int Open (vlc_object_t *obj)
     ih->p_sys = p_sys;
     ih->inhibit = Inhibit;
 
-    p_sys->pid = 0;
-    snprintf (p_sys->id, sizeof (p_sys->id), "0x%08"PRIx32, ih->window_id);
+    vlc_mutex_init (&p_sys->lock);
+    vlc_cond_init (&p_sys->wait);
+    if (vlc_clone (&p_sys->thread, Thread, ih, VLC_THREAD_PRIORITY_LOW))
+    {
+        vlc_cond_destroy (&p_sys->wait);
+        vlc_mutex_destroy (&p_sys->lock);
+        free (p_sys);
+        return VLC_ENOMEM;
+    }
+    p_sys->suspend = false;
 
     return VLC_SUCCESS;
 }
@@ -69,45 +80,63 @@ static void Close (vlc_object_t *obj)
     vlc_inhibit_t *ih = (vlc_inhibit_t *)obj;
     vlc_inhibit_sys_t *p_sys = ih->p_sys;
 
-    if (p_sys->pid)
-    {
-        int status;
-
-        while (waitpid (p_sys->pid, &status, 0) == -1);
-    }
+    vlc_cancel (p_sys->thread);
+    vlc_join (p_sys->thread, NULL);
+    vlc_cond_destroy (&p_sys->wait);
+    vlc_mutex_destroy (&p_sys->lock);
     free (p_sys);
 }
 
 static void Inhibit (vlc_inhibit_t *ih, bool suspend)
 {
     vlc_inhibit_sys_t *p_sys = ih->p_sys;
-    pid_t pid;
 
     /* xdg-screensaver can take quite a while to start up (e.g. 1 second).
      * So we avoid _waiting_ for it unless we really need to (clean up). */
-    /* TODO: it would be even faster to cache the current state, and
-     * wait in a separate thread until the target state is reached. */
-    if (p_sys->pid)
-    {
-        int status;
+    vlc_mutex_lock (&p_sys->lock);
+    p_sys->suspend = suspend;
+    vlc_cond_signal (&p_sys->wait);
+    vlc_mutex_unlock (&p_sys->lock);
+}
 
-        while (waitpid (p_sys->pid, &status, 0) == -1);
-        p_sys->pid = 0;
-    }
+extern char **environ;
 
-    char *argv[4] = {
-        (char *)"xdg-screensaver",
-        suspend ? (char *)"suspend" : (char *)"resume",
-        p_sys->id,
-        NULL,
-    };
+static void *Thread (void *data)
+{
+    vlc_inhibit_t *ih = data;
+    vlc_inhibit_sys_t *p_sys = ih->p_sys;
+    char id[11];
 
-    extern char **environ;
-    if (posix_spawnp (&pid, "xdg-screensaver", NULL, NULL, argv, environ) == 0)
-    {
-        msg_Dbg (ih, "started xdg-screensaver (PID = %d)", (int)pid);
-        ih->p_sys->pid = pid;
+    snprintf (id, sizeof (id), "0x%08"PRIx32, ih->window_id);
+
+    for (bool suspended = false;;)
+    {   /* TODO: detach the thread, so we don't need one at all time */
+        vlc_mutex_lock (&p_sys->lock);
+        mutex_cleanup_push (&p_sys->lock);
+        while (suspended == p_sys->suspend)
+            vlc_cond_wait (&p_sys->wait, &p_sys->lock);
+        vlc_cleanup_run ();
+
+        char *argv[4] = {
+            (char *)"xdg-screensaver",
+            (char *)(suspended ? "resume" : "suspend"),
+            id,
+            NULL,
+        };
+        pid_t pid;
+        int canc = vlc_savecancel ();
+
+        if (!posix_spawnp (&pid, "xdg-screensaver", NULL, NULL, argv, environ))
+        {
+            int status;
+
+            msg_Dbg (ih, "started xdg-screensaver (PID = %d)", (int)pid);
+            /* Wait for command to complete */
+            while (waitpid (pid, &status, 0) == -1);
+        }
+        else/* We don't handle the error, but busy looping would be worse :( */
+            msg_Warn (ih, "could not start xdg-screensaver");
+        suspended = !suspended;
+        vlc_restorecancel (canc);
     }
-    else
-        msg_Warn (ih, "could not start xdg-screensaver");
 }




More information about the vlc-devel mailing list