]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/lock-autospawn.c
Remove unnecessary #includes
[pulseaudio] / src / pulsecore / lock-autospawn.c
index d36b669ea536584d7c3d4e0bc0a5884e49e1f165..40aa5e927b1c750ebd6f2551761bd4bcfffe6d58 100644 (file)
@@ -5,7 +5,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
 #include <config.h>
 #endif
 
-#include <fcntl.h>
 #include <errno.h>
 #include <string.h>
-#include <sys/poll.h>
 #include <signal.h>
+
+#ifdef HAVE_PTHREAD
 #include <pthread.h>
+#endif
 
+#include <pulse/gccmacro.h>
 #include <pulse/i18n.h>
 #include <pulse/xmalloc.h>
 
+#include <pulsecore/poll.h>
 #include <pulsecore/mutex.h>
 #include <pulsecore/thread.h>
 #include <pulsecore/core-util.h>
@@ -55,10 +58,16 @@ static pa_mutex *mutex;
 static unsigned n_ref = 0;
 static int lock_fd = -1;
 static pa_mutex *lock_fd_mutex = NULL;
-static pa_bool_t taken = FALSE;
-static pa_thread *thread;
+static pa_thread *thread = NULL;
 static int pipe_fd[2] = { -1, -1 };
 
+static enum {
+    STATE_IDLE,
+    STATE_OWNING,
+    STATE_TAKEN,
+    STATE_FAILED
+} state = STATE_IDLE;
+
 static void destroy_mutex(void) PA_GCC_DESTRUCTOR;
 
 static int ref(void) {
@@ -67,30 +76,28 @@ static int ref(void) {
 
         pa_assert(pipe_fd[0] >= 0);
         pa_assert(pipe_fd[1] >= 0);
+        pa_assert(lock_fd_mutex);
 
         n_ref++;
 
         return 0;
     }
 
-    pa_assert(lock_fd < 0);
     pa_assert(!lock_fd_mutex);
-    pa_assert(!taken);
+    pa_assert(state == STATE_IDLE);
+    pa_assert(lock_fd < 0);
     pa_assert(!thread);
     pa_assert(pipe_fd[0] < 0);
     pa_assert(pipe_fd[1] < 0);
 
-    if (pipe(pipe_fd) < 0)
+    if (pa_pipe_cloexec(pipe_fd) < 0)
         return -1;
 
-    lock_fd_mutex = pa_mutex_new(FALSE, FALSE);
-
-    pa_make_fd_cloexec(pipe_fd[0]);
-    pa_make_fd_cloexec(pipe_fd[1]);
-
     pa_make_fd_nonblock(pipe_fd[1]);
     pa_make_fd_nonblock(pipe_fd[0]);
 
+    lock_fd_mutex = pa_mutex_new(FALSE, FALSE);
+
     n_ref = 1;
     return 0;
 }
@@ -107,15 +114,18 @@ static void unref(pa_bool_t after_fork) {
     if (n_ref > 0)
         return;
 
-    pa_assert(!taken);
-
     if (thread) {
         pa_thread_free(thread);
         thread = NULL;
     }
 
     pa_mutex_lock(lock_fd_mutex);
-    if (lock_fd >= 0) {
+
+    pa_assert(state != STATE_TAKEN);
+
+    if (state == STATE_OWNING) {
+
+        pa_assert(lock_fd >= 0);
 
         if (after_fork)
             pa_close(lock_fd);
@@ -127,10 +137,12 @@ static void unref(pa_bool_t after_fork) {
 
             pa_unlock_lockfile(lf, lock_fd);
             pa_xfree(lf);
-
-            lock_fd = -1;
         }
     }
+
+    lock_fd = -1;
+    state = STATE_IDLE;
+
     pa_mutex_unlock(lock_fd_mutex);
 
     pa_mutex_free(lock_fd_mutex);
@@ -149,7 +161,7 @@ static void ping(void) {
     for (;;) {
         char x = 'x';
 
-        if ((s = write(pipe_fd[1], &x, 1)) == 1)
+        if ((s = pa_write(pipe_fd[1], &x, 1, NULL)) == 1)
             break;
 
         pa_assert(s < 0);
@@ -173,10 +185,10 @@ static void wait_for_ping(void) {
     pfd.fd = pipe_fd[0];
     pfd.events = POLLIN;
 
-    if ((k = poll(&pfd, 1, -1)) != 1) {
+    if ((k = pa_poll(&pfd, 1, -1)) != 1) {
         pa_assert(k < 0);
         pa_assert(errno == EINTR);
-    } else if ((s = read(pipe_fd[0], &x, 1)) != 1) {
+    } else if ((s = pa_read(pipe_fd[0], &x, 1, NULL)) != 1) {
         pa_assert(s < 0);
         pa_assert(errno == EAGAIN);
     }
@@ -188,7 +200,7 @@ static void empty_pipe(void) {
 
     pa_assert(pipe_fd[0] >= 0);
 
-    if ((s = read(pipe_fd[0], &x, sizeof(x))) < 1) {
+    if ((s = pa_read(pipe_fd[0], &x, sizeof(x), NULL)) < 1) {
         pa_assert(s < 0);
         pa_assert(errno == EAGAIN);
     }
@@ -197,23 +209,35 @@ static void empty_pipe(void) {
 static void thread_func(void *u) {
     int fd;
     char *lf;
+
+#ifdef HAVE_PTHREAD
     sigset_t fullset;
 
     /* No signals in this thread please */
     sigfillset(&fullset);
     pthread_sigmask(SIG_BLOCK, &fullset, NULL);
+#endif
 
     if (!(lf = pa_runtime_path(AUTOSPAWN_LOCK))) {
         pa_log_warn(_("Cannot access autospawn lock."));
-        goto finish;
+        goto fail;
     }
 
     if ((fd = pa_lock_lockfile(lf)) < 0)
-        goto finish;
+        goto fail;
 
     pa_mutex_lock(lock_fd_mutex);
-    pa_assert(lock_fd < 0);
+    pa_assert(state == STATE_IDLE);
     lock_fd = fd;
+    state = STATE_OWNING;
+    pa_mutex_unlock(lock_fd_mutex);
+
+    goto finish;
+
+fail:
+    pa_mutex_lock(lock_fd_mutex);
+    pa_assert(state == STATE_IDLE);
+    state = STATE_FAILED;
     pa_mutex_unlock(lock_fd_mutex);
 
 finish:
@@ -225,7 +249,7 @@ finish:
 static int start_thread(void) {
 
     if (!thread)
-        if (!(thread = pa_thread_new(thread_func, NULL)))
+        if (!(thread = pa_thread_new("autospawn", thread_func, NULL)))
             return -1;
 
     return 0;
@@ -238,12 +262,10 @@ static void create_mutex(void) {
 }
 
 static void destroy_mutex(void) {
-
     if (mutex)
         pa_mutex_free(mutex);
 }
 
-
 int pa_autospawn_lock_init(void) {
     int ret = -1;
 
@@ -273,13 +295,18 @@ int pa_autospawn_lock_acquire(pa_bool_t block) {
 
         empty_pipe();
 
-        if (lock_fd >= 0 && !taken) {
-            taken = TRUE;
+        if (state == STATE_OWNING) {
+            state = STATE_TAKEN;
             ret = 1;
             break;
         }
 
-        if (lock_fd < 0)
+        if (state == STATE_FAILED) {
+            ret = -1;
+            break;
+        }
+
+        if (state == STATE_IDLE)
             if (start_thread() < 0)
                 break;
 
@@ -310,8 +337,8 @@ void pa_autospawn_lock_release(void) {
     pa_mutex_lock(mutex);
     pa_assert(n_ref >= 1);
 
-    pa_assert(taken);
-    taken = FALSE;
+    pa_assert(state == STATE_TAKEN);
+    state = STATE_OWNING;
 
     ping();