summaryrefslogtreecommitdiffstats
path: root/src/pulsecore
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2009-01-23 23:57:47 +0100
committerLennart Poettering <lennart@poettering.net>2009-01-23 23:57:47 +0100
commite9601250110decc8f4323c76be8549acff091966 (patch)
tree7a2ab72fd7e69e6c5696a790597dfcee2cf04e63 /src/pulsecore
parentdb27c6347ebe3316bb5f80518c6cc86eea67779b (diff)
add support for static mutexes
Diffstat (limited to 'src/pulsecore')
-rw-r--r--src/pulsecore/mutex-posix.c20
-rw-r--r--src/pulsecore/mutex.h7
2 files changed, 27 insertions, 0 deletions
diff --git a/src/pulsecore/mutex-posix.c b/src/pulsecore/mutex-posix.c
index 35465b7b..c3ead97d 100644
--- a/src/pulsecore/mutex-posix.c
+++ b/src/pulsecore/mutex-posix.c
@@ -138,3 +138,23 @@ int pa_cond_wait(pa_cond *c, pa_mutex *m) {
return pthread_cond_wait(&c->cond, &m->mutex);
}
+
+pa_mutex* pa_static_mutex_get(pa_static_mutex *s, pa_bool_t recursive, pa_bool_t inherit_priority) {
+ pa_mutex *m;
+
+ pa_assert(s);
+
+ /* First, check if already initialized and short cut */
+ if ((m = pa_atomic_ptr_load(&s->ptr)))
+ return m;
+
+ /* OK, not initialized, so let's allocate, and fill in */
+ m = pa_mutex_new(recursive, inherit_priority);
+ if ((pa_atomic_ptr_cmpxchg(&s->ptr, NULL, m)))
+ return m;
+
+ /* Him, filling in failed, so someone else must have filled in
+ * already */
+ pa_assert_se(m = pa_atomic_ptr_load(&s->ptr));
+ return m;
+}
diff --git a/src/pulsecore/mutex.h b/src/pulsecore/mutex.h
index 36e1d635..8e0b1f2e 100644
--- a/src/pulsecore/mutex.h
+++ b/src/pulsecore/mutex.h
@@ -23,6 +23,7 @@
***/
#include <pulsecore/macro.h>
+#include <pulsecore/atomic.h>
typedef struct pa_mutex pa_mutex;
@@ -45,4 +46,10 @@ void pa_cond_free(pa_cond *c);
void pa_cond_signal(pa_cond *c, int broadcast);
int pa_cond_wait(pa_cond *c, pa_mutex *m);
+typedef struct pa_static_mutex {
+ pa_atomic_ptr_t ptr;
+} pa_static_mutex;
+
+pa_mutex* pa_static_mutex_get(pa_static_mutex *m, pa_bool_t recursive, pa_bool_t inherit_priority);
+
#endif