mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-08 13:29:59 -05:00
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:
parent
4c65e58325
commit
1ba655560d
2 changed files with 17 additions and 34 deletions
|
|
@ -24,46 +24,33 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <pulsecore/macro.h>
|
#include <pulsecore/macro.h>
|
||||||
#include <pulsecore/mutex.h>
|
|
||||||
|
|
||||||
#include "once.h"
|
#include "once.h"
|
||||||
|
|
||||||
|
/* See http://www.hpl.hp.com/research/linux/atomic_ops/example.php4 for the
|
||||||
|
* reference algorithm used here. */
|
||||||
|
|
||||||
pa_bool_t pa_once_begin(pa_once *control) {
|
pa_bool_t pa_once_begin(pa_once *control) {
|
||||||
|
pa_mutex *m;
|
||||||
|
|
||||||
pa_assert(control);
|
pa_assert(control);
|
||||||
|
|
||||||
if (pa_atomic_load(&control->done))
|
if (pa_atomic_load(&control->done))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
pa_atomic_inc(&control->ref);
|
|
||||||
|
|
||||||
/* Caveat: We have to make sure that the once func has completed
|
/* Caveat: We have to make sure that the once func has completed
|
||||||
* before returning, even if the once func is not actually
|
* before returning, even if the once func is not actually
|
||||||
* executed by us. Hence the awkward locking. */
|
* executed by us. Hence the awkward locking. */
|
||||||
|
|
||||||
for (;;) {
|
m = pa_static_mutex_get(&control->mutex, FALSE, FALSE);
|
||||||
pa_mutex *m;
|
pa_mutex_lock(m);
|
||||||
|
|
||||||
if ((m = pa_atomic_ptr_load(&control->mutex))) {
|
|
||||||
|
|
||||||
/* The mutex is stored in locked state, hence let's just
|
|
||||||
* wait until it is unlocked */
|
|
||||||
pa_mutex_lock(m);
|
|
||||||
|
|
||||||
pa_assert(pa_atomic_load(&control->done));
|
|
||||||
|
|
||||||
pa_once_end(control);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
pa_assert_se(m = pa_mutex_new(FALSE, FALSE));
|
|
||||||
pa_mutex_lock(m);
|
|
||||||
|
|
||||||
if (pa_atomic_ptr_cmpxchg(&control->mutex, NULL, m))
|
|
||||||
return TRUE;
|
|
||||||
|
|
||||||
|
if (pa_atomic_load(&control->done)) {
|
||||||
pa_mutex_unlock(m);
|
pa_mutex_unlock(m);
|
||||||
pa_mutex_free(m);
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pa_once_end(pa_once *control) {
|
void pa_once_end(pa_once *control) {
|
||||||
|
|
@ -71,15 +58,11 @@ void pa_once_end(pa_once *control) {
|
||||||
|
|
||||||
pa_assert(control);
|
pa_assert(control);
|
||||||
|
|
||||||
|
pa_assert(!pa_atomic_load(&control->done));
|
||||||
pa_atomic_store(&control->done, 1);
|
pa_atomic_store(&control->done, 1);
|
||||||
|
|
||||||
pa_assert_se(m = pa_atomic_ptr_load(&control->mutex));
|
m = pa_static_mutex_get(&control->mutex, FALSE, FALSE);
|
||||||
pa_mutex_unlock(m);
|
pa_mutex_unlock(m);
|
||||||
|
|
||||||
if (pa_atomic_dec(&control->ref) <= 1) {
|
|
||||||
pa_assert_se(pa_atomic_ptr_cmpxchg(&control->mutex, m, NULL));
|
|
||||||
pa_mutex_free(m);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Not reentrant -- how could it be? */
|
/* Not reentrant -- how could it be? */
|
||||||
|
|
|
||||||
|
|
@ -23,16 +23,16 @@
|
||||||
***/
|
***/
|
||||||
|
|
||||||
#include <pulsecore/atomic.h>
|
#include <pulsecore/atomic.h>
|
||||||
|
#include <pulsecore/mutex.h>
|
||||||
|
|
||||||
typedef struct pa_once {
|
typedef struct pa_once {
|
||||||
pa_atomic_ptr_t mutex;
|
pa_static_mutex mutex;
|
||||||
pa_atomic_t ref, done;
|
pa_atomic_t done;
|
||||||
} pa_once;
|
} pa_once;
|
||||||
|
|
||||||
#define PA_ONCE_INIT \
|
#define PA_ONCE_INIT \
|
||||||
{ \
|
{ \
|
||||||
.mutex = PA_ATOMIC_PTR_INIT(NULL), \
|
.mutex = PA_STATIC_MUTEX_INIT, \
|
||||||
.ref = PA_ATOMIC_INIT(0), \
|
|
||||||
.done = PA_ATOMIC_INIT(0) \
|
.done = PA_ATOMIC_INIT(0) \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue