]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/memtrap.c
resampler: Add support for resamplers that consume less data than asked.
[pulseaudio] / src / pulsecore / memtrap.c
index e04e838e60b2f3ecf2227409328efdb90ec1c0ab..4236934ea53dc829cd8761e5e1877d11c32bcf47 100644 (file)
 #endif
 
 #include <signal.h>
+
+#ifdef HAVE_SYS_MMAN_H
 #include <sys/mman.h>
+#endif
+
+/* This is deprecated on glibc but is still used by FreeBSD */
+#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
+# define MAP_ANONYMOUS MAP_ANON
+#endif
 
 #include <pulse/xmalloc.h>
 
-#include <pulsecore/semaphore.h>
-#include <pulsecore/macro.h>
-#include <pulsecore/mutex.h>
 #include <pulsecore/core-util.h>
+#include <pulsecore/aupdate.h>
+#include <pulsecore/atomic.h>
+#include <pulsecore/once.h>
+#include <pulsecore/mutex.h>
 
 #include "memtrap.h"
 
@@ -43,13 +52,14 @@ struct pa_memtrap {
 };
 
 static pa_memtrap *memtraps[2] = { NULL, NULL };
-static pa_atomic_t read_lock = PA_ATOMIC_INIT(0);
-static pa_static_semaphore semaphore = PA_STATIC_SEMAPHORE_INIT;
-static pa_static_mutex write_lock = PA_STATIC_MUTEX_INIT;
+static pa_aupdate *aupdate;
+static pa_static_mutex mutex = PA_STATIC_MUTEX_INIT; /* only required to serialize access to the write side */
 
-#define MSB (1U << (sizeof(unsigned)*8U-1))
-#define WHICH(n) (!!((n) & MSB))
-#define COUNTER(n) ((n) & ~MSB)
+static void allocate_aupdate(void) {
+    PA_ONCE_BEGIN {
+        aupdate = pa_aupdate_new();
+    } PA_ONCE_END;
+}
 
 pa_bool_t pa_memtrap_is_good(pa_memtrap *m) {
     pa_assert(m);
@@ -57,24 +67,18 @@ pa_bool_t pa_memtrap_is_good(pa_memtrap *m) {
     return !pa_atomic_load(&m->bad);
 }
 
+#ifdef HAVE_SIGACTION
 static void sigsafe_error(const char *s) {
-    write(STDERR_FILENO, s, strlen(s));
+    size_t ret PA_GCC_UNUSED;
+    ret = write(STDERR_FILENO, s, strlen(s));
 }
 
 static void signal_handler(int sig, siginfo_t* si, void *data) {
-    unsigned n, j;
+    unsigned j;
     pa_memtrap *m;
     void *r;
 
-    /* Increase the lock counter */
-    n = (unsigned) pa_atomic_inc(&read_lock);
-
-    /* The uppermost bit tells us which list to look at */
-    j = WHICH(n);
-
-    /* When n is 0 we have about 2^31 threads running that
-     * all got a sigbus at the same time, oh my! */
-    pa_assert(COUNTER(n)+1 > 0);
+    j = pa_aupdate_read_begin(aupdate);
 
     for (m = memtraps[j]; m; m = m->next[j])
         if (si->si_addr >= m->start &&
@@ -94,38 +98,25 @@ static void signal_handler(int sig, siginfo_t* si, void *data) {
 
     pa_assert(r == m->start);
 
-    pa_atomic_dec(&read_lock);
-
-    /* Post the semaphore */
-    pa_semaphore_post(pa_static_semaphore_get(&semaphore, 0));
-
+    pa_aupdate_read_end(aupdate);
     return;
 
 fail:
+    pa_aupdate_read_end(aupdate);
+
     sigsafe_error("Failed to handle SIGBUS.\n");
-    pa_atomic_dec(&read_lock);
     abort();
 }
-
-static void memtrap_swap(unsigned n) {
-
-    for (;;) {
-
-        /* If the read counter is > 0 wait; if it is 0 try to swap the lists */
-        if (COUNTER(n) > 0)
-            pa_semaphore_wait(pa_static_semaphore_get(&semaphore, 0));
-        else if (pa_atomic_cmpxchg(&read_lock, (int) n, (int) (n ^ MSB)))
-            break;
-
-        n = (unsigned) pa_atomic_load(&read_lock);
-    }
-}
+#endif
 
 static void memtrap_link(pa_memtrap *m, unsigned j) {
     pa_assert(m);
 
     m->prev[j] = NULL;
-    m->next[j] = memtraps[j];
+
+    if ((m->next[j] = memtraps[j]))
+        m->next[j]->prev[j] = m;
+
     memtraps[j] = m;
 }
 
@@ -143,107 +134,108 @@ static void memtrap_unlink(pa_memtrap *m, unsigned j) {
 
 pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
     pa_memtrap *m = NULL;
-    pa_mutex *lock;
-    unsigned n, j;
+    unsigned j;
+    pa_mutex *mx;
 
     pa_assert(start);
     pa_assert(size > 0);
-    pa_assert(PA_PAGE_ALIGN_PTR(start) == start);
-    pa_assert(PA_PAGE_ALIGN(size) == size);
-
-    lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
-    pa_mutex_lock(lock);
 
-    n = (unsigned) pa_atomic_load(&read_lock);
-    j = WHICH(n);
+    start = PA_PAGE_ALIGN_PTR(start);
+    size = PA_PAGE_ALIGN(size);
 
     m = pa_xnew(pa_memtrap, 1);
     m->start = (void*) start;
     m->size = size;
     pa_atomic_store(&m->bad, 0);
 
-    memtrap_link(m, !j);
-    memtrap_swap(n);
+    allocate_aupdate();
+
+    mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
+    pa_mutex_lock(mx);
+
+    j = pa_aupdate_write_begin(aupdate);
     memtrap_link(m, j);
+    j = pa_aupdate_write_swap(aupdate);
+    memtrap_link(m, j);
+    pa_aupdate_write_end(aupdate);
 
-    pa_mutex_unlock(lock);
+    pa_mutex_unlock(mx);
 
     return m;
 }
 
 void pa_memtrap_remove(pa_memtrap *m) {
-    unsigned n, j;
-    pa_mutex *lock;
+    unsigned j;
+    pa_mutex *mx;
 
     pa_assert(m);
 
-    lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
-    pa_mutex_lock(lock);
+    allocate_aupdate();
 
-    n = (unsigned) pa_atomic_load(&read_lock);
-    j = WHICH(n);
+    mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
+    pa_mutex_lock(mx);
 
-    memtrap_unlink(m, !j);
-    memtrap_swap(n);
+    j = pa_aupdate_write_begin(aupdate);
+    memtrap_unlink(m, j);
+    j = pa_aupdate_write_swap(aupdate);
     memtrap_unlink(m, j);
+    pa_aupdate_write_end(aupdate);
 
-    pa_xfree(m);
+    pa_mutex_unlock(mx);
 
-    pa_mutex_unlock(lock);
+    pa_xfree(m);
 }
 
 pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
-    unsigned n, j;
-    pa_mutex *lock;
+    unsigned j;
+    pa_mutex *mx;
 
     pa_assert(m);
 
     pa_assert(start);
     pa_assert(size > 0);
-    pa_assert(PA_PAGE_ALIGN_PTR(start) == start);
-    pa_assert(PA_PAGE_ALIGN(size) == size);
 
-    lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
-    pa_mutex_lock(lock);
+    start = PA_PAGE_ALIGN_PTR(start);
+    size = PA_PAGE_ALIGN(size);
+
+    allocate_aupdate();
+
+    mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
+    pa_mutex_lock(mx);
+
+    j = pa_aupdate_write_begin(aupdate);
 
     if (m->start == start && m->size == size)
         goto unlock;
 
-    n = (unsigned) pa_atomic_load(&read_lock);
-    j = WHICH(n);
-
-    memtrap_unlink(m, !j);
-    memtrap_swap(n);
     memtrap_unlink(m, j);
+    pa_aupdate_write_swap(aupdate);
 
     m->start = (void*) start;
     m->size = size;
     pa_atomic_store(&m->bad, 0);
 
-    n = (unsigned) pa_atomic_load(&read_lock);
-    j = WHICH(n);
-
-    memtrap_link(m, !j);
-    memtrap_swap(n);
+    pa_assert_se(pa_aupdate_write_swap(aupdate) == j);
     memtrap_link(m, j);
 
 unlock:
-    pa_mutex_unlock(lock);
+    pa_aupdate_write_end(aupdate);
+
+    pa_mutex_unlock(mx);
 
     return m;
 }
 
 void pa_memtrap_install(void) {
+#ifdef HAVE_SIGACTION
     struct sigaction sa;
 
-    /* Before we install the signal handler, make sure the semaphore
-     * is valid so that the initialization of the semaphore
-     * doesn't have to happen from the signal handler */
-    pa_static_semaphore_get(&semaphore, 0);
+    allocate_aupdate();
 
     memset(&sa, 0, sizeof(sa));
     sa.sa_sigaction = signal_handler;
     sa.sa_flags = SA_RESTART|SA_SIGINFO;
 
     pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
+#endif
 }