Wrap atomic_flag usage behind SoundIo macros and types

As done for other atomic types, we hide atomic_flag real type in
    order to use both the C and the C++ standard libraries.
This commit is contained in:
Raphaël Londeix 2015-12-05 13:25:44 +00:00
parent af98891b4f
commit 406ffa668c
14 changed files with 87 additions and 73 deletions

View file

@ -51,7 +51,7 @@ static void destroy_alsa(struct SoundIoPrivate *si) {
struct SoundIoAlsa *sia = &si->backend_data.alsa; struct SoundIoAlsa *sia = &si->backend_data.alsa;
if (sia->thread) { if (sia->thread) {
atomic_flag_clear(&sia->abort_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(sia->abort_flag);
wakeup_device_poll(sia); wakeup_device_poll(sia);
soundio_os_thread_destroy(sia->thread); soundio_os_thread_destroy(sia->thread);
} }
@ -804,7 +804,7 @@ static void device_thread_run(void *arg) {
int err; int err;
for (;;) { for (;;) {
int poll_num = poll(fds, 2, -1); int poll_num = poll(fds, 2, -1);
if (!atomic_flag_test_and_set(&sia->abort_flag)) if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(sia->abort_flag))
break; break;
if (poll_num == -1) { if (poll_num == -1) {
if (errno == EINTR) if (errno == EINTR)
@ -980,7 +980,7 @@ static void outstream_destroy_alsa(struct SoundIoPrivate *si, struct SoundIoOutS
struct SoundIoOutStreamAlsa *osa = &os->backend_data.alsa; struct SoundIoOutStreamAlsa *osa = &os->backend_data.alsa;
if (osa->thread) { if (osa->thread) {
atomic_flag_clear(&osa->thread_exit_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(osa->thread_exit_flag);
wakeup_outstream_poll(osa); wakeup_outstream_poll(osa);
soundio_os_thread_destroy(osa->thread); soundio_os_thread_destroy(osa->thread);
osa->thread = NULL; osa->thread = NULL;
@ -1049,7 +1049,7 @@ static int outstream_wait_for_poll(struct SoundIoOutStreamPrivate *os) {
if ((err = poll(osa->poll_fds, osa->poll_fd_count_with_extra, -1)) < 0) { if ((err = poll(osa->poll_fds, osa->poll_fd_count_with_extra, -1)) < 0) {
return SoundIoErrorStreaming; return SoundIoErrorStreaming;
} }
if (!atomic_flag_test_and_set(&osa->thread_exit_flag)) if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osa->thread_exit_flag))
return SoundIoErrorInterrupted; return SoundIoErrorInterrupted;
if ((err = snd_pcm_poll_descriptors_revents(osa->handle, if ((err = snd_pcm_poll_descriptors_revents(osa->handle,
osa->poll_fds, osa->poll_fd_count, &revents)) < 0) osa->poll_fds, osa->poll_fd_count, &revents)) < 0)
@ -1113,7 +1113,7 @@ static void outstream_thread_run(void *arg) {
if ((snd_pcm_uframes_t)avail == osa->buffer_size_frames) { if ((snd_pcm_uframes_t)avail == osa->buffer_size_frames) {
outstream->write_callback(outstream, 0, avail); outstream->write_callback(outstream, 0, avail);
if (!atomic_flag_test_and_set(&osa->thread_exit_flag)) if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osa->thread_exit_flag))
return; return;
continue; continue;
} }
@ -1133,9 +1133,9 @@ static void outstream_thread_run(void *arg) {
outstream->error_callback(outstream, err); outstream->error_callback(outstream, err);
return; return;
} }
if (!atomic_flag_test_and_set(&osa->thread_exit_flag)) if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osa->thread_exit_flag))
return; return;
if (!atomic_flag_test_and_set(&osa->clear_buffer_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osa->clear_buffer_flag)) {
if ((err = snd_pcm_drop(osa->handle)) < 0) { if ((err = snd_pcm_drop(osa->handle)) < 0) {
outstream->error_callback(outstream, SoundIoErrorStreaming); outstream->error_callback(outstream, SoundIoErrorStreaming);
return; return;
@ -1213,12 +1213,12 @@ static void instream_thread_run(void *arg) {
case SND_PCM_STATE_PAUSED: case SND_PCM_STATE_PAUSED:
{ {
if ((err = instream_wait_for_poll(is)) < 0) { if ((err = instream_wait_for_poll(is)) < 0) {
if (!atomic_flag_test_and_set(&isa->thread_exit_flag)) if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isa->thread_exit_flag))
return; return;
instream->error_callback(instream, SoundIoErrorStreaming); instream->error_callback(instream, SoundIoErrorStreaming);
return; return;
} }
if (!atomic_flag_test_and_set(&isa->thread_exit_flag)) if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isa->thread_exit_flag))
return; return;
snd_pcm_sframes_t avail = snd_pcm_avail_update(isa->handle); snd_pcm_sframes_t avail = snd_pcm_avail_update(isa->handle);
@ -1261,7 +1261,7 @@ static int outstream_open_alsa(struct SoundIoPrivate *si, struct SoundIoOutStrea
struct SoundIoOutStream *outstream = &os->pub; struct SoundIoOutStream *outstream = &os->pub;
struct SoundIoDevice *device = outstream->device; struct SoundIoDevice *device = outstream->device;
atomic_flag_test_and_set(&osa->clear_buffer_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osa->clear_buffer_flag);
if (outstream->software_latency == 0.0) if (outstream->software_latency == 0.0)
outstream->software_latency = 1.0; outstream->software_latency = 1.0;
@ -1426,7 +1426,7 @@ static int outstream_start_alsa(struct SoundIoPrivate *si, struct SoundIoOutStre
assert(!osa->thread); assert(!osa->thread);
int err; int err;
atomic_flag_test_and_set(&osa->thread_exit_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osa->thread_exit_flag);
if ((err = soundio_os_thread_create(outstream_thread_run, os, soundio->emit_rtprio_warning, &osa->thread))) if ((err = soundio_os_thread_create(outstream_thread_run, os, soundio->emit_rtprio_warning, &osa->thread)))
return err; return err;
@ -1515,7 +1515,7 @@ static int outstream_clear_buffer_alsa(struct SoundIoPrivate *si,
struct SoundIoOutStreamPrivate *os) struct SoundIoOutStreamPrivate *os)
{ {
struct SoundIoOutStreamAlsa *osa = &os->backend_data.alsa; struct SoundIoOutStreamAlsa *osa = &os->backend_data.alsa;
atomic_flag_clear(&osa->clear_buffer_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(osa->clear_buffer_flag);
return 0; return 0;
} }
@ -1560,7 +1560,7 @@ static void instream_destroy_alsa(struct SoundIoPrivate *si, struct SoundIoInStr
struct SoundIoInStreamAlsa *isa = &is->backend_data.alsa; struct SoundIoInStreamAlsa *isa = &is->backend_data.alsa;
if (isa->thread) { if (isa->thread) {
atomic_flag_clear(&isa->thread_exit_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(isa->thread_exit_flag);
soundio_os_thread_destroy(isa->thread); soundio_os_thread_destroy(isa->thread);
isa->thread = NULL; isa->thread = NULL;
} }
@ -1727,7 +1727,7 @@ static int instream_start_alsa(struct SoundIoPrivate *si, struct SoundIoInStream
assert(!isa->thread); assert(!isa->thread);
atomic_flag_test_and_set(&isa->thread_exit_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isa->thread_exit_flag);
int err; int err;
if ((err = soundio_os_thread_create(instream_thread_run, is, soundio->emit_rtprio_warning, &isa->thread))) { if ((err = soundio_os_thread_create(instream_thread_run, is, soundio->emit_rtprio_warning, &isa->thread))) {
instream_destroy_alsa(si, is); instream_destroy_alsa(si, is);
@ -1857,7 +1857,7 @@ int soundio_alsa_init(struct SoundIoPrivate *si) {
sia->notify_fd = -1; sia->notify_fd = -1;
sia->notify_wd = -1; sia->notify_wd = -1;
atomic_flag_test_and_set(&sia->abort_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(sia->abort_flag);
sia->mutex = soundio_os_mutex_create(); sia->mutex = soundio_os_mutex_create();
if (!sia->mutex) { if (!sia->mutex) {

View file

@ -32,7 +32,7 @@ struct SoundIoAlsa {
struct SoundIoOsCond *cond; struct SoundIoOsCond *cond;
struct SoundIoOsThread *thread; struct SoundIoOsThread *thread;
atomic_flag abort_flag; struct SoundIoAtomicFlag abort_flag;
int notify_fd; int notify_fd;
int notify_wd; int notify_wd;
bool have_devices_flag; bool have_devices_flag;
@ -60,11 +60,11 @@ struct SoundIoOutStreamAlsa {
struct pollfd *poll_fds; struct pollfd *poll_fds;
int poll_exit_pipe_fd[2]; int poll_exit_pipe_fd[2];
struct SoundIoOsThread *thread; struct SoundIoOsThread *thread;
atomic_flag thread_exit_flag; struct SoundIoAtomicFlag thread_exit_flag;
snd_pcm_uframes_t period_size; snd_pcm_uframes_t period_size;
int write_frame_count; int write_frame_count;
bool is_paused; bool is_paused;
atomic_flag clear_buffer_flag; struct SoundIoAtomicFlag clear_buffer_flag;
struct SoundIoChannelArea areas[SOUNDIO_MAX_CHANNELS]; struct SoundIoChannelArea areas[SOUNDIO_MAX_CHANNELS];
}; };
@ -79,7 +79,7 @@ struct SoundIoInStreamAlsa {
int poll_fd_count; int poll_fd_count;
struct pollfd *poll_fds; struct pollfd *poll_fds;
struct SoundIoOsThread *thread; struct SoundIoOsThread *thread;
atomic_flag thread_exit_flag; struct SoundIoAtomicFlag thread_exit_flag;
int period_size; int period_size;
int read_frame_count; int read_frame_count;
bool is_paused; bool is_paused;

View file

@ -27,10 +27,17 @@ struct SoundIoAtomicBool {
std::atomic<bool> x; std::atomic<bool> x;
}; };
struct SoundIoAtomicFlag {
std::atomic_flag x;
};
#define SOUNDIO_ATOMIC_LOAD(a) (a.x.load()) #define SOUNDIO_ATOMIC_LOAD(a) (a.x.load())
#define SOUNDIO_ATOMIC_FETCH_ADD(a, delta) (a.x.fetch_add(delta)) #define SOUNDIO_ATOMIC_FETCH_ADD(a, delta) (a.x.fetch_add(delta))
#define SOUNDIO_ATOMIC_STORE(a, value) (a.x.store(value)) #define SOUNDIO_ATOMIC_STORE(a, value) (a.x.store(value))
#define SOUNDIO_ATOMIC_EXCHANGE(a, value) (a.x.exchange(value)) #define SOUNDIO_ATOMIC_EXCHANGE(a, value) (a.x.exchange(value))
#define SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(a) (a.x.test_and_set())
#define SOUNDIO_ATOMIC_FLAG_CLEAR(a) (a.x.clear())
#define SOUNDIO_ATOMIC_FLAG_INIT {ATOMIC_FLAG_INIT}
#else #else
@ -48,10 +55,17 @@ struct SoundIoAtomicBool {
atomic_bool x; atomic_bool x;
}; };
struct SoundIoAtomicFlag {
atomic_flag x;
};
#define SOUNDIO_ATOMIC_LOAD(a) atomic_load(&a.x) #define SOUNDIO_ATOMIC_LOAD(a) atomic_load(&a.x)
#define SOUNDIO_ATOMIC_FETCH_ADD(a, delta) atomic_fetch_add(&a.x, delta) #define SOUNDIO_ATOMIC_FETCH_ADD(a, delta) atomic_fetch_add(&a.x, delta)
#define SOUNDIO_ATOMIC_STORE(a, value) atomic_store(&a.x, value) #define SOUNDIO_ATOMIC_STORE(a, value) atomic_store(&a.x, value)
#define SOUNDIO_ATOMIC_EXCHANGE(a, value) atomic_exchange(&a.x, value) #define SOUNDIO_ATOMIC_EXCHANGE(a, value) atomic_exchange(&a.x, value)
#define SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(a) atomic_flag_test_and_set(&a.x)
#define SOUNDIO_ATOMIC_FLAG_CLEAR(a) atomic_flag_clear(&a.x)
#define SOUNDIO_ATOMIC_FLAG_INIT {ATOMIC_FLAG_INIT}
#endif #endif

View file

@ -152,7 +152,7 @@ static void destroy_ca(struct SoundIoPrivate *si) {
SoundIoListAudioDeviceID_deinit(&sica->registered_listeners); SoundIoListAudioDeviceID_deinit(&sica->registered_listeners);
if (sica->thread) { if (sica->thread) {
atomic_flag_clear(&sica->abort_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(sica->abort_flag);
soundio_os_cond_signal(sica->scan_devices_cond, NULL); soundio_os_cond_signal(sica->scan_devices_cond, NULL);
soundio_os_thread_destroy(sica->thread); soundio_os_thread_destroy(sica->thread);
} }
@ -846,7 +846,7 @@ static void device_thread_run(void *arg) {
int err; int err;
for (;;) { for (;;) {
if (!atomic_flag_test_and_set(&sica->abort_flag)) if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(sica->abort_flag))
break; break;
if (SOUNDIO_ATOMIC_LOAD(sica->service_restarted)) { if (SOUNDIO_ATOMIC_LOAD(sica->service_restarted)) {
shutdown_backend(si, SoundIoErrorBackendDisconnected); shutdown_backend(si, SoundIoErrorBackendDisconnected);
@ -1122,7 +1122,7 @@ static OSStatus read_callback_ca(void *userdata, AudioUnitRenderActionFlags *io_
} }
OSStatus os_err; OSStatus os_err;
if ((os_err = AudioUnitRender(isca->instance, io_action_flags, in_time_stamp, if ((os_err = AudioUnitRender(isca->instance, io_action_flags, in_time_stamp,
in_bus_number, in_number_frames, isca->buffer_list))) in_bus_number, in_number_frames, isca->buffer_list)))
{ {
instream->error_callback(instream, SoundIoErrorStreaming); instream->error_callback(instream, SoundIoErrorStreaming);
@ -1348,7 +1348,7 @@ int soundio_coreaudio_init(struct SoundIoPrivate *si) {
SOUNDIO_ATOMIC_STORE(sica->have_devices_flag, false); SOUNDIO_ATOMIC_STORE(sica->have_devices_flag, false);
SOUNDIO_ATOMIC_STORE(sica->device_scan_queued, true); SOUNDIO_ATOMIC_STORE(sica->device_scan_queued, true);
SOUNDIO_ATOMIC_STORE(sica->service_restarted, false); SOUNDIO_ATOMIC_STORE(sica->service_restarted, false);
atomic_flag_test_and_set(&sica->abort_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(sica->abort_flag);
sica->mutex = soundio_os_mutex_create(); sica->mutex = soundio_os_mutex_create();
if (!sica->mutex) { if (!sica->mutex) {

View file

@ -30,7 +30,7 @@ struct SoundIoCoreAudio {
struct SoundIoOsMutex *mutex; struct SoundIoOsMutex *mutex;
struct SoundIoOsCond *cond; struct SoundIoOsCond *cond;
struct SoundIoOsThread *thread; struct SoundIoOsThread *thread;
atomic_flag abort_flag; struct SoundIoAtomicFlag abort_flag;
// this one is ready to be read with flush_events. protected by mutex // this one is ready to be read with flush_events. protected by mutex
struct SoundIoDevicesInfo *ready_devices_info; struct SoundIoDevicesInfo *ready_devices_info;

View file

@ -25,14 +25,14 @@ static void playback_thread_run(void *arg) {
double start_time = soundio_os_get_time(); double start_time = soundio_os_get_time();
long frames_consumed = 0; long frames_consumed = 0;
while (atomic_flag_test_and_set(&osd->abort_flag)) { while (SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osd->abort_flag)) {
double now = soundio_os_get_time(); double now = soundio_os_get_time();
double time_passed = now - start_time; double time_passed = now - start_time;
double next_period = start_time + double next_period = start_time +
ceil_dbl(time_passed / osd->period_duration) * osd->period_duration; ceil_dbl(time_passed / osd->period_duration) * osd->period_duration;
double relative_time = next_period - now; double relative_time = next_period - now;
soundio_os_cond_timed_wait(osd->cond, NULL, relative_time); soundio_os_cond_timed_wait(osd->cond, NULL, relative_time);
if (!atomic_flag_test_and_set(&osd->clear_buffer_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osd->clear_buffer_flag)) {
soundio_ring_buffer_clear(&osd->ring_buffer); soundio_ring_buffer_clear(&osd->ring_buffer);
int free_bytes = soundio_ring_buffer_capacity(&osd->ring_buffer); int free_bytes = soundio_ring_buffer_capacity(&osd->ring_buffer);
int free_frames = free_bytes / outstream->bytes_per_frame; int free_frames = free_bytes / outstream->bytes_per_frame;
@ -84,7 +84,7 @@ static void capture_thread_run(void *arg) {
long frames_consumed = 0; long frames_consumed = 0;
double start_time = soundio_os_get_time(); double start_time = soundio_os_get_time();
while (atomic_flag_test_and_set(&isd->abort_flag)) { while (SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isd->abort_flag)) {
double now = soundio_os_get_time(); double now = soundio_os_get_time();
double time_passed = now - start_time; double time_passed = now - start_time;
double next_period = start_time + double next_period = start_time +
@ -161,7 +161,7 @@ static void outstream_destroy_dummy(struct SoundIoPrivate *si, struct SoundIoOut
struct SoundIoOutStreamDummy *osd = &os->backend_data.dummy; struct SoundIoOutStreamDummy *osd = &os->backend_data.dummy;
if (osd->thread) { if (osd->thread) {
atomic_flag_clear(&osd->abort_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(osd->abort_flag);
soundio_os_cond_signal(osd->cond, NULL); soundio_os_cond_signal(osd->cond, NULL);
soundio_os_thread_destroy(osd->thread); soundio_os_thread_destroy(osd->thread);
osd->thread = NULL; osd->thread = NULL;
@ -177,7 +177,7 @@ static int outstream_open_dummy(struct SoundIoPrivate *si, struct SoundIoOutStre
struct SoundIoOutStream *outstream = &os->pub; struct SoundIoOutStream *outstream = &os->pub;
struct SoundIoDevice *device = outstream->device; struct SoundIoDevice *device = outstream->device;
atomic_flag_test_and_set(&osd->clear_buffer_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osd->clear_buffer_flag);
SOUNDIO_ATOMIC_STORE(osd->pause_requested, false); SOUNDIO_ATOMIC_STORE(osd->pause_requested, false);
if (outstream->software_latency == 0.0) { if (outstream->software_latency == 0.0) {
@ -216,7 +216,7 @@ static int outstream_start_dummy(struct SoundIoPrivate *si, struct SoundIoOutStr
struct SoundIoOutStreamDummy *osd = &os->backend_data.dummy; struct SoundIoOutStreamDummy *osd = &os->backend_data.dummy;
struct SoundIo *soundio = &si->pub; struct SoundIo *soundio = &si->pub;
assert(!osd->thread); assert(!osd->thread);
atomic_flag_test_and_set(&osd->abort_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osd->abort_flag);
int err; int err;
if ((err = soundio_os_thread_create(playback_thread_run, os, if ((err = soundio_os_thread_create(playback_thread_run, os,
soundio->emit_rtprio_warning, &osd->thread))) soundio->emit_rtprio_warning, &osd->thread)))
@ -257,7 +257,7 @@ static int outstream_end_write_dummy(struct SoundIoPrivate *si, struct SoundIoOu
static int outstream_clear_buffer_dummy(struct SoundIoPrivate *si, struct SoundIoOutStreamPrivate *os) { static int outstream_clear_buffer_dummy(struct SoundIoPrivate *si, struct SoundIoOutStreamPrivate *os) {
struct SoundIoOutStreamDummy *osd = &os->backend_data.dummy; struct SoundIoOutStreamDummy *osd = &os->backend_data.dummy;
atomic_flag_clear(&osd->clear_buffer_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(osd->clear_buffer_flag);
soundio_os_cond_signal(osd->cond, NULL); soundio_os_cond_signal(osd->cond, NULL);
return 0; return 0;
} }
@ -275,7 +275,7 @@ static void instream_destroy_dummy(struct SoundIoPrivate *si, struct SoundIoInSt
struct SoundIoInStreamDummy *isd = &is->backend_data.dummy; struct SoundIoInStreamDummy *isd = &is->backend_data.dummy;
if (isd->thread) { if (isd->thread) {
atomic_flag_clear(&isd->abort_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(isd->abort_flag);
soundio_os_cond_signal(isd->cond, NULL); soundio_os_cond_signal(isd->cond, NULL);
soundio_os_thread_destroy(isd->thread); soundio_os_thread_destroy(isd->thread);
isd->thread = NULL; isd->thread = NULL;
@ -331,7 +331,7 @@ static int instream_start_dummy(struct SoundIoPrivate *si, struct SoundIoInStrea
struct SoundIoInStreamDummy *isd = &is->backend_data.dummy; struct SoundIoInStreamDummy *isd = &is->backend_data.dummy;
struct SoundIo *soundio = &si->pub; struct SoundIo *soundio = &si->pub;
assert(!isd->thread); assert(!isd->thread);
atomic_flag_test_and_set(&isd->abort_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isd->abort_flag);
int err; int err;
if ((err = soundio_os_thread_create(capture_thread_run, is, if ((err = soundio_os_thread_create(capture_thread_run, is,
soundio->emit_rtprio_warning, &isd->thread))) soundio->emit_rtprio_warning, &isd->thread)))

View file

@ -27,14 +27,14 @@ struct SoundIoDeviceDummy { int make_the_struct_not_empty; };
struct SoundIoOutStreamDummy { struct SoundIoOutStreamDummy {
struct SoundIoOsThread *thread; struct SoundIoOsThread *thread;
struct SoundIoOsCond *cond; struct SoundIoOsCond *cond;
atomic_flag abort_flag; struct SoundIoAtomicFlag abort_flag;
double period_duration; double period_duration;
int buffer_frame_count; int buffer_frame_count;
int frames_left; int frames_left;
int write_frame_count; int write_frame_count;
struct SoundIoRingBuffer ring_buffer; struct SoundIoRingBuffer ring_buffer;
double playback_start_time; double playback_start_time;
atomic_flag clear_buffer_flag; struct SoundIoAtomicFlag clear_buffer_flag;
struct SoundIoAtomicBool pause_requested; struct SoundIoAtomicBool pause_requested;
struct SoundIoChannelArea areas[SOUNDIO_MAX_CHANNELS]; struct SoundIoChannelArea areas[SOUNDIO_MAX_CHANNELS];
}; };
@ -42,7 +42,7 @@ struct SoundIoOutStreamDummy {
struct SoundIoInStreamDummy { struct SoundIoInStreamDummy {
struct SoundIoOsThread *thread; struct SoundIoOsThread *thread;
struct SoundIoOsCond *cond; struct SoundIoOsCond *cond;
atomic_flag abort_flag; struct SoundIoAtomicFlag abort_flag;
double period_duration; double period_duration;
int frames_left; int frames_left;
int read_frame_count; int read_frame_count;

View file

@ -11,7 +11,7 @@
#include <stdio.h> #include <stdio.h>
static atomic_flag global_msg_callback_flag = ATOMIC_FLAG_INIT; static struct SoundIoAtomicFlag global_msg_callback_flag = SOUNDIO_ATOMIC_FLAG_INIT;
struct SoundIoJackPort { struct SoundIoJackPort {
const char *full_name; const char *full_name;
@ -315,9 +315,9 @@ static void my_flush_events(struct SoundIoPrivate *si, bool wait) {
if (cb_shutdown) { if (cb_shutdown) {
soundio->on_backend_disconnect(soundio, SoundIoErrorBackendDisconnected); soundio->on_backend_disconnect(soundio, SoundIoErrorBackendDisconnected);
} else { } else {
if (!atomic_flag_test_and_set(&sij->refresh_devices_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(sij->refresh_devices_flag)) {
if ((err = refresh_devices(si))) { if ((err = refresh_devices(si))) {
atomic_flag_clear(&sij->refresh_devices_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(sij->refresh_devices_flag);
} else { } else {
soundio->on_devices_change(soundio); soundio->on_devices_change(soundio);
} }
@ -344,7 +344,7 @@ static void wakeup_jack(struct SoundIoPrivate *si) {
static void force_device_scan_jack(struct SoundIoPrivate *si) { static void force_device_scan_jack(struct SoundIoPrivate *si) {
struct SoundIo *soundio = &si->pub; struct SoundIo *soundio = &si->pub;
struct SoundIoJack *sij = &si->backend_data.jack; struct SoundIoJack *sij = &si->backend_data.jack;
atomic_flag_clear(&sij->refresh_devices_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(sij->refresh_devices_flag);
soundio_os_mutex_lock(sij->mutex); soundio_os_mutex_lock(sij->mutex);
soundio_os_cond_signal(sij->cond, sij->mutex); soundio_os_cond_signal(sij->cond, sij->mutex);
soundio->on_events_signal(soundio); soundio->on_events_signal(soundio);
@ -806,7 +806,7 @@ static int instream_get_latency_jack(struct SoundIoPrivate *si, struct SoundIoIn
static void notify_devices_change(struct SoundIoPrivate *si) { static void notify_devices_change(struct SoundIoPrivate *si) {
struct SoundIo *soundio = &si->pub; struct SoundIo *soundio = &si->pub;
struct SoundIoJack *sij = &si->backend_data.jack; struct SoundIoJack *sij = &si->backend_data.jack;
atomic_flag_clear(&sij->refresh_devices_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(sij->refresh_devices_flag);
soundio_os_mutex_lock(sij->mutex); soundio_os_mutex_lock(sij->mutex);
soundio_os_cond_signal(sij->cond, sij->mutex); soundio_os_cond_signal(sij->cond, sij->mutex);
soundio->on_events_signal(soundio); soundio->on_events_signal(soundio);
@ -869,12 +869,12 @@ int soundio_jack_init(struct SoundIoPrivate *si) {
struct SoundIoJack *sij = &si->backend_data.jack; struct SoundIoJack *sij = &si->backend_data.jack;
struct SoundIo *soundio = &si->pub; struct SoundIo *soundio = &si->pub;
if (!atomic_flag_test_and_set(&global_msg_callback_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(global_msg_callback_flag)) {
if (soundio->jack_error_callback) if (soundio->jack_error_callback)
jack_set_error_function(soundio->jack_error_callback); jack_set_error_function(soundio->jack_error_callback);
if (soundio->jack_info_callback) if (soundio->jack_info_callback)
jack_set_info_function(soundio->jack_info_callback); jack_set_info_function(soundio->jack_info_callback);
atomic_flag_clear(&global_msg_callback_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(global_msg_callback_flag);
} }
sij->mutex = soundio_os_mutex_create(); sij->mutex = soundio_os_mutex_create();
@ -923,7 +923,7 @@ int soundio_jack_init(struct SoundIoPrivate *si) {
} }
jack_on_shutdown(sij->client, shutdown_callback, si); jack_on_shutdown(sij->client, shutdown_callback, si);
atomic_flag_clear(&sij->refresh_devices_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(sij->refresh_devices_flag);
sij->period_size = jack_get_buffer_size(sij->client); sij->period_size = jack_get_buffer_size(sij->client);
sij->sample_rate = jack_get_sample_rate(sij->client); sij->sample_rate = jack_get_sample_rate(sij->client);

View file

@ -38,7 +38,7 @@ struct SoundIoJack {
jack_client_t *client; jack_client_t *client;
struct SoundIoOsMutex *mutex; struct SoundIoOsMutex *mutex;
struct SoundIoOsCond *cond; struct SoundIoOsCond *cond;
atomic_flag refresh_devices_flag; struct SoundIoAtomicFlag refresh_devices_flag;
int sample_rate; int sample_rate;
int period_size; int period_size;
bool is_shutdown; bool is_shutdown;

View file

@ -667,7 +667,7 @@ static int outstream_open_pa(struct SoundIoPrivate *si, struct SoundIoOutStreamP
struct SoundIoPulseAudio *sipa = &si->backend_data.pulseaudio; struct SoundIoPulseAudio *sipa = &si->backend_data.pulseaudio;
SOUNDIO_ATOMIC_STORE(ospa->stream_ready, false); SOUNDIO_ATOMIC_STORE(ospa->stream_ready, false);
atomic_flag_test_and_set(&ospa->clear_buffer_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(ospa->clear_buffer_flag);
assert(sipa->pulse_context); assert(sipa->pulse_context);
@ -783,7 +783,7 @@ static int outstream_end_write_pa(struct SoundIoPrivate *si, struct SoundIoOutSt
struct SoundIoOutStreamPulseAudio *ospa = &os->backend_data.pulseaudio; struct SoundIoOutStreamPulseAudio *ospa = &os->backend_data.pulseaudio;
pa_stream *stream = ospa->stream; pa_stream *stream = ospa->stream;
pa_seek_mode_t seek_mode = atomic_flag_test_and_set(&ospa->clear_buffer_flag) ? PA_SEEK_RELATIVE : PA_SEEK_RELATIVE_ON_READ; pa_seek_mode_t seek_mode = SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(ospa->clear_buffer_flag) ? PA_SEEK_RELATIVE : PA_SEEK_RELATIVE_ON_READ;
if (pa_stream_write(stream, ospa->write_ptr, ospa->write_byte_count, NULL, 0, seek_mode)) if (pa_stream_write(stream, ospa->write_ptr, ospa->write_byte_count, NULL, 0, seek_mode))
return SoundIoErrorStreaming; return SoundIoErrorStreaming;
@ -794,7 +794,7 @@ static int outstream_clear_buffer_pa(struct SoundIoPrivate *si,
struct SoundIoOutStreamPrivate *os) struct SoundIoOutStreamPrivate *os)
{ {
struct SoundIoOutStreamPulseAudio *ospa = &os->backend_data.pulseaudio; struct SoundIoOutStreamPulseAudio *ospa = &os->backend_data.pulseaudio;
atomic_flag_clear(&ospa->clear_buffer_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(ospa->clear_buffer_flag);
return 0; return 0;
} }

View file

@ -46,7 +46,7 @@ struct SoundIoOutStreamPulseAudio {
pa_buffer_attr buffer_attr; pa_buffer_attr buffer_attr;
char *write_ptr; char *write_ptr;
size_t write_byte_count; size_t write_byte_count;
atomic_flag clear_buffer_flag; struct SoundIoAtomicFlag clear_buffer_flag;
struct SoundIoChannelArea areas[SOUNDIO_MAX_CHANNELS]; struct SoundIoChannelArea areas[SOUNDIO_MAX_CHANNELS];
}; };

View file

@ -171,9 +171,9 @@ static void default_backend_disconnect_cb(struct SoundIo *soundio, int err) {
soundio_panic("libsoundio: backend disconnected: %s", soundio_strerror(err)); soundio_panic("libsoundio: backend disconnected: %s", soundio_strerror(err));
} }
static atomic_flag rtprio_seen = ATOMIC_FLAG_INIT; static struct SoundIoAtomicFlag rtprio_seen = SOUNDIO_ATOMIC_FLAG_INIT;
static void default_emit_rtprio_warning(void) { static void default_emit_rtprio_warning(void) {
if (!atomic_flag_test_and_set(&rtprio_seen)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(rtprio_seen)) {
fprintf(stderr, "warning: unable to set high priority thread: Operation not permitted\n"); fprintf(stderr, "warning: unable to set high priority thread: Operation not permitted\n");
fprintf(stderr, "See " fprintf(stderr, "See "
"https://github.com/andrewrk/genesis/wiki/warning:-unable-to-set-high-priority-thread:-Operation-not-permitted\n"); "https://github.com/andrewrk/genesis/wiki/warning:-unable-to-set-high-priority-thread:-Operation-not-permitted\n");

View file

@ -1048,7 +1048,7 @@ static void outstream_destroy_wasapi(struct SoundIoPrivate *si, struct SoundIoOu
struct SoundIoOutStreamWasapi *osw = &os->backend_data.wasapi; struct SoundIoOutStreamWasapi *osw = &os->backend_data.wasapi;
if (osw->thread) { if (osw->thread) {
atomic_flag_clear(&osw->thread_exit_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(osw->thread_exit_flag);
if (osw->h_event) if (osw->h_event)
SetEvent(osw->h_event); SetEvent(osw->h_event);
@ -1268,13 +1268,13 @@ static void outstream_shared_run(struct SoundIoOutStreamPrivate *os) {
double wait_time = time_until_underrun / 2.0; double wait_time = time_until_underrun / 2.0;
soundio_os_mutex_lock(osw->mutex); soundio_os_mutex_lock(osw->mutex);
soundio_os_cond_timed_wait(osw->cond, osw->mutex, wait_time); soundio_os_cond_timed_wait(osw->cond, osw->mutex, wait_time);
if (!atomic_flag_test_and_set(&osw->thread_exit_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osw->thread_exit_flag)) {
soundio_os_mutex_unlock(osw->mutex); soundio_os_mutex_unlock(osw->mutex);
return; return;
} }
soundio_os_mutex_unlock(osw->mutex); soundio_os_mutex_unlock(osw->mutex);
bool reset_buffer = false; bool reset_buffer = false;
if (!atomic_flag_test_and_set(&osw->clear_buffer_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osw->clear_buffer_flag)) {
if (!osw->is_paused) { if (!osw->is_paused) {
if (FAILED(hr = IAudioClient_Stop(osw->audio_client))) { if (FAILED(hr = IAudioClient_Stop(osw->audio_client))) {
outstream->error_callback(outstream, SoundIoErrorStreaming); outstream->error_callback(outstream, SoundIoErrorStreaming);
@ -1286,10 +1286,10 @@ static void outstream_shared_run(struct SoundIoOutStreamPrivate *os) {
outstream->error_callback(outstream, SoundIoErrorStreaming); outstream->error_callback(outstream, SoundIoErrorStreaming);
return; return;
} }
atomic_flag_clear(&osw->pause_resume_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(osw->pause_resume_flag);
reset_buffer = true; reset_buffer = true;
} }
if (!atomic_flag_test_and_set(&osw->pause_resume_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osw->pause_resume_flag)) {
bool pause = SOUNDIO_ATOMIC_LOAD(osw->desired_pause_state); bool pause = SOUNDIO_ATOMIC_LOAD(osw->desired_pause_state);
if (pause && !osw->is_paused) { if (pause && !osw->is_paused) {
if (FAILED(hr = IAudioClient_Stop(osw->audio_client))) { if (FAILED(hr = IAudioClient_Stop(osw->audio_client))) {
@ -1335,9 +1335,9 @@ static void outstream_raw_run(struct SoundIoOutStreamPrivate *os) {
for (;;) { for (;;) {
WaitForSingleObject(osw->h_event, INFINITE); WaitForSingleObject(osw->h_event, INFINITE);
if (!atomic_flag_test_and_set(&osw->thread_exit_flag)) if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osw->thread_exit_flag))
return; return;
if (!atomic_flag_test_and_set(&osw->pause_resume_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osw->pause_resume_flag)) {
bool pause = SOUNDIO_ATOMIC_LOAD(osw->desired_pause_state); bool pause = SOUNDIO_ATOMIC_LOAD(osw->desired_pause_state);
if (pause && !osw->is_paused) { if (pause && !osw->is_paused) {
if (FAILED(hr = IAudioClient_Stop(osw->audio_client))) { if (FAILED(hr = IAudioClient_Stop(osw->audio_client))) {
@ -1382,7 +1382,7 @@ static void outstream_thread_run(void *arg) {
osw->open_complete = true; osw->open_complete = true;
soundio_os_cond_signal(osw->cond, osw->mutex); soundio_os_cond_signal(osw->cond, osw->mutex);
for (;;) { for (;;) {
if (!atomic_flag_test_and_set(&osw->thread_exit_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osw->thread_exit_flag)) {
soundio_os_mutex_unlock(osw->mutex); soundio_os_mutex_unlock(osw->mutex);
return; return;
} }
@ -1407,8 +1407,8 @@ static int outstream_open_wasapi(struct SoundIoPrivate *si, struct SoundIoOutStr
struct SoundIoDevice *device = outstream->device; struct SoundIoDevice *device = outstream->device;
struct SoundIo *soundio = &si->pub; struct SoundIo *soundio = &si->pub;
atomic_flag_test_and_set(&osw->pause_resume_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osw->pause_resume_flag);
atomic_flag_test_and_set(&osw->clear_buffer_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osw->clear_buffer_flag);
SOUNDIO_ATOMIC_STORE(osw->desired_pause_state, false); SOUNDIO_ATOMIC_STORE(osw->desired_pause_state, false);
// All the COM functions are supposed to be called from the same thread. libsoundio API does not // All the COM functions are supposed to be called from the same thread. libsoundio API does not
@ -1443,7 +1443,7 @@ static int outstream_open_wasapi(struct SoundIoPrivate *si, struct SoundIoOutStr
} }
} }
atomic_flag_test_and_set(&osw->thread_exit_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osw->thread_exit_flag);
int err; int err;
if ((err = soundio_os_thread_create(outstream_thread_run, os, if ((err = soundio_os_thread_create(outstream_thread_run, os,
soundio->emit_rtprio_warning, &osw->thread))) soundio->emit_rtprio_warning, &osw->thread)))
@ -1469,7 +1469,7 @@ static int outstream_pause_wasapi(struct SoundIoPrivate *si, struct SoundIoOutSt
struct SoundIoOutStreamWasapi *osw = &os->backend_data.wasapi; struct SoundIoOutStreamWasapi *osw = &os->backend_data.wasapi;
SOUNDIO_ATOMIC_STORE(osw->desired_pause_state, pause); SOUNDIO_ATOMIC_STORE(osw->desired_pause_state, pause);
atomic_flag_clear(&osw->pause_resume_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(osw->pause_resume_flag);
if (osw->h_event) { if (osw->h_event) {
SetEvent(osw->h_event); SetEvent(osw->h_event);
} else { } else {
@ -1534,7 +1534,7 @@ static int outstream_clear_buffer_wasapi(struct SoundIoPrivate *si, struct Sound
if (osw->h_event) { if (osw->h_event) {
return SoundIoErrorIncompatibleDevice; return SoundIoErrorIncompatibleDevice;
} else { } else {
atomic_flag_clear(&osw->clear_buffer_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(osw->clear_buffer_flag);
soundio_os_mutex_lock(osw->mutex); soundio_os_mutex_lock(osw->mutex);
soundio_os_cond_signal(osw->cond, osw->mutex); soundio_os_cond_signal(osw->cond, osw->mutex);
soundio_os_mutex_unlock(osw->mutex); soundio_os_mutex_unlock(osw->mutex);
@ -1573,7 +1573,7 @@ static void instream_destroy_wasapi(struct SoundIoPrivate *si, struct SoundIoInS
struct SoundIoInStreamWasapi *isw = &is->backend_data.wasapi; struct SoundIoInStreamWasapi *isw = &is->backend_data.wasapi;
if (isw->thread) { if (isw->thread) {
atomic_flag_clear(&isw->thread_exit_flag); SOUNDIO_ATOMIC_FLAG_CLEAR(isw->thread_exit_flag);
if (isw->h_event) if (isw->h_event)
SetEvent(isw->h_event); SetEvent(isw->h_event);
@ -1750,7 +1750,7 @@ static void instream_raw_run(struct SoundIoInStreamPrivate *is) {
for (;;) { for (;;) {
WaitForSingleObject(isw->h_event, INFINITE); WaitForSingleObject(isw->h_event, INFINITE);
if (!atomic_flag_test_and_set(&isw->thread_exit_flag)) if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isw->thread_exit_flag))
return; return;
instream->read_callback(instream, isw->buffer_frame_count, isw->buffer_frame_count); instream->read_callback(instream, isw->buffer_frame_count, isw->buffer_frame_count);
@ -1771,7 +1771,7 @@ static void instream_shared_run(struct SoundIoInStreamPrivate *is) {
for (;;) { for (;;) {
soundio_os_mutex_lock(isw->mutex); soundio_os_mutex_lock(isw->mutex);
soundio_os_cond_timed_wait(isw->cond, isw->mutex, instream->software_latency / 2.0); soundio_os_cond_timed_wait(isw->cond, isw->mutex, instream->software_latency / 2.0);
if (!atomic_flag_test_and_set(&isw->thread_exit_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isw->thread_exit_flag)) {
soundio_os_mutex_unlock(isw->mutex); soundio_os_mutex_unlock(isw->mutex);
return; return;
} }
@ -1813,7 +1813,7 @@ static void instream_thread_run(void *arg) {
isw->open_complete = true; isw->open_complete = true;
soundio_os_cond_signal(isw->cond, isw->mutex); soundio_os_cond_signal(isw->cond, isw->mutex);
for (;;) { for (;;) {
if (!atomic_flag_test_and_set(&isw->thread_exit_flag)) { if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isw->thread_exit_flag)) {
soundio_os_mutex_unlock(isw->mutex); soundio_os_mutex_unlock(isw->mutex);
return; return;
} }
@ -1870,7 +1870,7 @@ static int instream_open_wasapi(struct SoundIoPrivate *si, struct SoundIoInStrea
} }
} }
atomic_flag_test_and_set(&isw->thread_exit_flag); SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isw->thread_exit_flag);
int err; int err;
if ((err = soundio_os_thread_create(instream_thread_run, is, if ((err = soundio_os_thread_create(instream_thread_run, is,
soundio->emit_rtprio_warning, &isw->thread))) soundio->emit_rtprio_warning, &isw->thread)))

View file

@ -63,15 +63,15 @@ struct SoundIoOutStreamWasapi {
struct SoundIoOsMutex *mutex; struct SoundIoOsMutex *mutex;
struct SoundIoOsCond *cond; struct SoundIoOsCond *cond;
struct SoundIoOsCond *start_cond; struct SoundIoOsCond *start_cond;
atomic_flag thread_exit_flag; struct SoundIoAtomicFlag thread_exit_flag;
bool is_raw; bool is_raw;
int writable_frame_count; int writable_frame_count;
UINT32 buffer_frame_count; UINT32 buffer_frame_count;
int write_frame_count; int write_frame_count;
HANDLE h_event; HANDLE h_event;
struct SoundIoAtomicBool desired_pause_state; struct SoundIoAtomicBool desired_pause_state;
atomic_flag pause_resume_flag; struct SoundIoAtomicFlag pause_resume_flag;
atomic_flag clear_buffer_flag; struct SoundIoAtomicFlag clear_buffer_flag;
bool is_paused; bool is_paused;
bool open_complete; bool open_complete;
int open_err; int open_err;
@ -89,7 +89,7 @@ struct SoundIoInStreamWasapi {
struct SoundIoOsMutex *mutex; struct SoundIoOsMutex *mutex;
struct SoundIoOsCond *cond; struct SoundIoOsCond *cond;
struct SoundIoOsCond *start_cond; struct SoundIoOsCond *start_cond;
atomic_flag thread_exit_flag; struct SoundIoAtomicFlag thread_exit_flag;
bool is_raw; bool is_raw;
int readable_frame_count; int readable_frame_count;
UINT32 buffer_frame_count; UINT32 buffer_frame_count;