Skip to content

Commit 9c40351

Browse files
authored
Merge pull request #248 from Libvisual/input-pulseaudio-c11-atomics
Plugins (pulseaudio): Use C11 atomics in place of GCC/Clang extensions.
2 parents b953de6 + fdce3ec commit 9c40351

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

libvisual-plugins/plugins/input/pulseaudio/input_pulseaudio.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <pulse/stream.h>
2020
#include <pulse/thread-mainloop.h>
2121
#include <assert.h>
22+
#include <stdatomic.h>
2223

2324
VISUAL_PLUGIN_API_VERSION_VALIDATOR
2425

@@ -34,13 +35,6 @@ VISUAL_PLUGIN_API_VERSION_VALIDATOR
3435
#define CHUNK_SIZE_BYTES (FRAMES * CHANNELS * sizeof(SAMPLE_TYPE))
3536
#define CHUNKS (2 * SAMPLE_RATE / FRAMES + 1) // i.e. 2+ seconds of audio
3637

37-
#if defined(__clang__) || defined(__GNUC__)
38-
# define ATOMIC_CONSUMER_LOAD_64(source, target) __atomic_load(&source, &target, __ATOMIC_ACQUIRE)
39-
# define ATOMIC_PRODUCER_STORE_64(target, source) __atomic_store(&target, &source, __ATOMIC_RELEASE)
40-
#else
41-
# error We need GCC or Clang for __atomic_load and __atomic_store
42-
#endif
43-
4438
pa_sample_spec sample_spec = {
4539
.format = SAMPLE_FORMAT_PA,
4640
.rate = SAMPLE_RATE,
@@ -53,7 +47,7 @@ typedef struct {
5347
pa_stream * input_stream;
5448

5549
SAMPLE_TYPE pcm_data[CHUNKS][FRAMES * CHANNELS]; // ringbuffer of chunks
56-
uint64_t chunks_written;
50+
atomic_uint_fast64_t chunks_written;
5751
uint64_t chunk_write_offset_bytes;
5852
uint64_t chunks_read;
5953
} pulseaudio_priv_t;
@@ -157,8 +151,7 @@ static int inp_pulseaudio_upload( VisPluginData *plugin, VisAudio *audio )
157151
// so we make a snapshot to work with a single consistent value below.
158152
// Also, plain reads to 64bit are not atomic on 32bit platforms, so we add protection.
159153
// This is attomic `priv->chunks_written = frozen_chunks_written`.
160-
uint64_t frozen_chunks_written;
161-
ATOMIC_CONSUMER_LOAD_64(priv->chunks_written, frozen_chunks_written);
154+
uint64_t frozen_chunks_written = atomic_load_explicit(&priv->chunks_written, memory_order_acquire);
162155

163156
assert(priv->chunks_read <= frozen_chunks_written);
164157
if (priv->chunks_read == frozen_chunks_written) {
@@ -228,7 +221,7 @@ static void on_input_stream_data(pa_stream *p, size_t nbytes, void *userdata) {
228221

229222
// This is atomic `priv->chunks_written++`
230223
uint64_t new_chunks_written = priv->chunks_written + 1;
231-
ATOMIC_PRODUCER_STORE_64(priv->chunks_written, new_chunks_written);
224+
atomic_store_explicit(&priv->chunks_written, new_chunks_written, memory_order_release);
232225
}
233226

234227
nbytes -= round_nbytes;

0 commit comments

Comments
 (0)