From: Lennart Poettering Date: Fri, 23 Jan 2009 22:57:47 +0000 (+0100) Subject: add support for static mutexes X-Git-Tag: v0.9.15-test1~110 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e9601250110decc8f4323c76be8549acff091966;p=platform%2Fupstream%2Fpulseaudio.git add support for static mutexes --- diff --git a/src/pulsecore/mutex-posix.c b/src/pulsecore/mutex-posix.c index 35465b7..c3ead97 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 36e1d63..8e0b1f2 100644 --- a/src/pulsecore/mutex.h +++ b/src/pulsecore/mutex.h @@ -23,6 +23,7 @@ ***/ #include +#include 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