summaryrefslogtreecommitdiffstats
path: root/audio/out/pull.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-05-21 01:04:47 +0200
committerwm4 <wm4@nowhere>2014-05-21 02:21:18 +0200
commit8e7cf4bc992f13dbb523bb42d6b9de4bc2f486c2 (patch)
tree77f21515a336c368d2bb97eab11d950bed9f5c4d /audio/out/pull.c
parent2f65f0e2548f95b3b8ba6620efe6c0e3cb02420b (diff)
downloadmpv-8e7cf4bc992f13dbb523bb42d6b9de4bc2f486c2.tar.bz2
mpv-8e7cf4bc992f13dbb523bb42d6b9de4bc2f486c2.tar.xz
atomics: switch to C11 stdatomic.h
In my opinion, we shouldn't use atomics at all, but ok. This switches the mpv code to use C11 stdatomic.h, and for compilers that don't support stdatomic.h yet, we emulate the subset used by mpv using the builtins commonly provided by gcc and clang. This supersedes an earlier similar attempt by Kovensky. That attempt unfortunately relied on a big copypasted freebsd header (which also depended on much more highly compiler-specific functionality, defined reserved symbols, etc.), so it had to be NIH'ed. Some issues: - C11 says default initialization of atomics "produces a valid state", but it's not sure whether the stored value is really 0. But we rely on this. - I'm pretty sure our use of the __atomic... builtins is/was incorrect. We don't use atomic load/store intrinsics, and access stuff directly. - Our wrapper actually does stricter typechecking than the stdatomic.h implementation by gcc 4.9. We make the atomic types incompatible with normal types by wrapping them into structs. (The FreeBSD wrapper does the same.) - I couldn't test on MinGW.
Diffstat (limited to 'audio/out/pull.c')
-rw-r--r--audio/out/pull.c44
1 files changed, 17 insertions, 27 deletions
diff --git a/audio/out/pull.c b/audio/out/pull.c
index 4b0dc6e890..71f14f38dc 100644
--- a/audio/out/pull.c
+++ b/audio/out/pull.c
@@ -45,13 +45,13 @@ struct ao_pull_state {
struct mp_ring *buffers[MP_NUM_CHANNELS];
// AO_STATE_*
- int state;
+ atomic_int state;
// Whether buffers[] can be accessed.
- int ready;
+ atomic_bool ready;
// Device delay of the last written sample, in realtime.
- int64_t end_time_us;
+ atomic_llong end_time_us;
};
static int get_space(struct ao *ao)
@@ -77,10 +77,9 @@ static int play(struct ao *ao, void **data, int samples, int flags)
int r = mp_ring_write(p->buffers[n], data[n], write_bytes);
assert(r == write_bytes);
}
- if (p->state != AO_STATE_PLAY) {
- p->end_time_us = 0;
- p->state = AO_STATE_PLAY;
- mp_memory_barrier();
+ if (atomic_load(&p->state) != AO_STATE_PLAY) {
+ atomic_store(&p->end_time_us, 0);
+ atomic_store(&p->state, AO_STATE_PLAY);
if (ao->driver->resume)
ao->driver->resume(ao);
}
@@ -103,8 +102,7 @@ int ao_read_data(struct ao *ao, void **data, int samples, int64_t out_time_us)
struct ao_pull_state *p = ao->api_priv;
int full_bytes = samples * ao->sstride;
- mp_memory_barrier();
- if (!p->ready) {
+ if (!atomic_load(&p->ready)) {
for (int n = 0; n < ao->num_planes; n++)
af_fill_silence(data[n], full_bytes, ao->format);
return 0;
@@ -116,10 +114,9 @@ int ao_read_data(struct ao *ao, void **data, int samples, int64_t out_time_us)
int bytes = MPMIN(buffered_bytes, full_bytes);
if (bytes > 0)
- p->end_time_us = out_time_us;
+ atomic_store(&p->end_time_us, out_time_us);
- mp_memory_barrier();
- if (p->state == AO_STATE_PAUSE)
+ if (atomic_load(&p->state) == AO_STATE_PAUSE)
bytes = 0;
for (int n = 0; n < ao->num_planes; n++) {
@@ -154,8 +151,7 @@ static float get_delay(struct ao *ao)
{
struct ao_pull_state *p = ao->api_priv;
- mp_memory_barrier();
- int64_t end = p->end_time_us;
+ int64_t end = atomic_load(&p->end_time_us);
int64_t now = mp_time_us();
double driver_delay = MPMAX(0, (end - now) / (1000.0 * 1000.0));
return mp_ring_buffered(p->buffers[0]) / (double)ao->bps + driver_delay;
@@ -168,15 +164,12 @@ static void reset(struct ao *ao)
ao->driver->reset(ao);
// Not like this is race-condition free. It will work if ->reset
// stops the audio callback, though.
- p->ready = 0;
- p->state = AO_STATE_NONE;
- mp_memory_barrier();
+ atomic_store(&p->ready, false);
+ atomic_store(&p->state, AO_STATE_NONE);
for (int n = 0; n < ao->num_planes; n++)
mp_ring_reset(p->buffers[n]);
- p->end_time_us = 0;
- mp_memory_barrier();
- p->ready = 1;
- mp_memory_barrier();
+ atomic_store(&p->end_time_us, 0);
+ atomic_store(&p->ready, true);
}
static void pause(struct ao *ao)
@@ -184,15 +177,13 @@ static void pause(struct ao *ao)
struct ao_pull_state *p = ao->api_priv;
if (ao->driver->pause)
ao->driver->pause(ao);
- p->state = AO_STATE_PAUSE;
- mp_memory_barrier();
+ atomic_store(&p->state, AO_STATE_PAUSE);
}
static void resume(struct ao *ao)
{
struct ao_pull_state *p = ao->api_priv;
- p->state = AO_STATE_PLAY;
- mp_memory_barrier();
+ atomic_store(&p->state, AO_STATE_PLAY);
if (ao->driver->resume)
ao->driver->resume(ao);
}
@@ -207,8 +198,7 @@ static int init(struct ao *ao)
struct ao_pull_state *p = ao->api_priv;
for (int n = 0; n < ao->num_planes; n++)
p->buffers[n] = mp_ring_new(ao, ao->buffer * ao->sstride);
- p->ready = 1;
- mp_memory_barrier();
+ atomic_store(&p->ready, true);
return 0;
}