once: Fix race causing pa_once to sometimes run twice

There was a race in the existing code that could cause the pa_once code
to be run twice, see:
http://lists.freedesktop.org/archives/pulseaudio-discuss/2012-April/013354.html

Therefore the existing implementation was rewritten to instead look like
the reference implementation here:
http://www.hpl.hp.com/research/linux/atomic_ops/example.php4

Signed-off-by: David Henningsson <david.henningsson@canonical.com>
This commit is contained in:
David Henningsson 2012-05-18 22:29:41 +02:00
parent 4c65e58325
commit 1ba655560d
2 changed files with 17 additions and 34 deletions

View file

@ -23,16 +23,16 @@
***/
#include <pulsecore/atomic.h>
#include <pulsecore/mutex.h>
typedef struct pa_once {
pa_atomic_ptr_t mutex;
pa_atomic_t ref, done;
pa_static_mutex mutex;
pa_atomic_t done;
} pa_once;
#define PA_ONCE_INIT \
{ \
.mutex = PA_ATOMIC_PTR_INIT(NULL), \
.ref = PA_ATOMIC_INIT(0), \
.mutex = PA_STATIC_MUTEX_INIT, \
.done = PA_ATOMIC_INIT(0) \
}