summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2009-07-01 20:22:38 +0200
committerLennart Poettering <lennart@poettering.net>2009-07-01 20:22:38 +0200
commit7dabe051cdd3980887a78a6cfe8e06666e2b21c9 (patch)
treec0153e70557bdab56208c9b57aa98dda1e95c96c /src
parent28e4625a52dedbf9f3697b3b3bb5b6ea59f336c9 (diff)
memtrap: properly lock access to memtrap changer
Diffstat (limited to 'src')
-rw-r--r--src/pulsecore/memtrap.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/pulsecore/memtrap.c b/src/pulsecore/memtrap.c
index e06f60ca..7d917450 100644
--- a/src/pulsecore/memtrap.c
+++ b/src/pulsecore/memtrap.c
@@ -37,6 +37,7 @@
#include <pulsecore/aupdate.h>
#include <pulsecore/atomic.h>
#include <pulsecore/once.h>
+#include <pulsecore/mutex.h>
#include "memtrap.h"
@@ -49,6 +50,7 @@ struct pa_memtrap {
static pa_memtrap *memtraps[2] = { NULL, NULL };
static pa_aupdate *aupdate;
+static pa_static_mutex mutex = PA_STATIC_MUTEX_INIT; /* only required to serialize access to the write side */
static void allocate_aupdate(void) {
PA_ONCE_BEGIN {
@@ -124,6 +126,7 @@ static void memtrap_unlink(pa_memtrap *m, unsigned j) {
pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
pa_memtrap *m = NULL;
unsigned j;
+ pa_mutex *mx;
pa_assert(start);
pa_assert(size > 0);
@@ -138,33 +141,45 @@ pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
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(mx);
+
return m;
}
void pa_memtrap_remove(pa_memtrap *m) {
unsigned j;
+ pa_mutex *mx;
pa_assert(m);
allocate_aupdate();
+ mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
+ pa_mutex_lock(mx);
+
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_mutex_unlock(mx);
+
pa_xfree(m);
}
pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
unsigned j;
+ pa_mutex *mx;
pa_assert(m);
@@ -176,6 +191,9 @@ pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t 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)
@@ -194,6 +212,8 @@ pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
unlock:
pa_aupdate_write_end(aupdate);
+ pa_mutex_unlock(mx);
+
return m;
}