summaryrefslogtreecommitdiffstats
path: root/audio
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
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')
-rw-r--r--audio/out/ao_wasapi.c9
-rwxr-xr-xaudio/out/ao_wasapi.h4
-rwxr-xr-xaudio/out/ao_wasapi_utils.c2
-rw-r--r--audio/out/pull.c44
4 files changed, 24 insertions, 35 deletions
diff --git a/audio/out/ao_wasapi.c b/audio/out/ao_wasapi.c
index 3d59554dcd..431b24c3a1 100644
--- a/audio/out/ao_wasapi.c
+++ b/audio/out/ao_wasapi.c
@@ -33,7 +33,6 @@
#include "audio/out/ao_wasapi_utils.h"
#include "audio/format.h"
-#include "compat/atomics.h"
#include "osdep/timer.h"
#include "osdep/io.h"
@@ -43,7 +42,7 @@
do { if ((unk) != NULL) { release; (unk) = NULL; } } while(0)
static double get_device_delay(struct wasapi_state *state) {
- UINT64 sample_count = state->sample_count;
+ UINT64 sample_count = atomic_load(&state->sample_count);
UINT64 position, qpc_position;
HRESULT hr;
@@ -100,8 +99,7 @@ static void thread_feed(struct ao *ao)
frame_count, 0);
EXIT_ON_ERROR(hr);
- mp_atomic_add_and_fetch(&state->sample_count, frame_count);
- mp_memory_barrier();
+ atomic_fetch_add(&state->sample_count, frame_count);
return;
exit_label:
@@ -318,8 +316,7 @@ static void audio_pause(struct ao *ao)
IAudioClient_Stop(state->pAudioClientProxy);
IAudioClient_Reset(state->pAudioClientProxy);
- state->sample_count = 0;
- mp_memory_barrier();
+ atomic_store(&state->sample_count, 0);
}
static void audio_resume(struct ao *ao)
diff --git a/audio/out/ao_wasapi.h b/audio/out/ao_wasapi.h
index 4fe80a6c8f..52813b586d 100755
--- a/audio/out/ao_wasapi.h
+++ b/audio/out/ao_wasapi.h
@@ -28,6 +28,8 @@
#include <mmdeviceapi.h>
#include <avrt.h>
+#include "compat/atomics.h"
+
typedef struct wasapi_state {
struct mp_log *log;
HANDLE threadLoop;
@@ -85,7 +87,7 @@ typedef struct wasapi_state {
/* WASAPI internal clock information, for estimating delay */
IAudioClock *pAudioClock;
UINT64 clock_frequency; /* scale for the "samples" returned by the clock */
- UINT64 sample_count; /* the amount of samples per channel written to a GetBuffer buffer */
+ atomic_ullong sample_count; /* the amount of samples per channel written to a GetBuffer buffer */
LARGE_INTEGER qpc_frequency; /* frequency of windows' high resolution timer */
int opt_exclusive;
diff --git a/audio/out/ao_wasapi_utils.c b/audio/out/ao_wasapi_utils.c
index c81562f5f3..95d2642780 100755
--- a/audio/out/ao_wasapi_utils.c
+++ b/audio/out/ao_wasapi_utils.c
@@ -426,7 +426,7 @@ static int init_clock(struct wasapi_state *state) {
QueryPerformanceFrequency(&state->qpc_frequency);
- state->sample_count = 0;
+ atomic_store(&state->sample_count, 0);
MP_VERBOSE(state, "IAudioClock::GetFrequency gave a frequency of %"PRIu64".\n", (uint64_t) state->clock_frequency);
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;
}