From 406ffa668c5d5bf9fb90f6ae0bc242628d33dca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Londeix?= Date: Sat, 5 Dec 2015 13:25:44 +0000 Subject: [PATCH] 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. --- src/alsa.c | 30 +++++++++++++++--------------- src/alsa.h | 8 ++++---- src/atomics.h | 14 ++++++++++++++ src/coreaudio.c | 8 ++++---- src/coreaudio.h | 2 +- src/dummy.c | 18 +++++++++--------- src/dummy.h | 6 +++--- src/jack.c | 16 ++++++++-------- src/jack.h | 2 +- src/pulseaudio.c | 6 +++--- src/pulseaudio.h | 2 +- src/soundio.c | 4 ++-- src/wasapi.c | 36 ++++++++++++++++++------------------ src/wasapi.h | 8 ++++---- 14 files changed, 87 insertions(+), 73 deletions(-) diff --git a/src/alsa.c b/src/alsa.c index 11da972..58cee88 100644 --- a/src/alsa.c +++ b/src/alsa.c @@ -51,7 +51,7 @@ static void destroy_alsa(struct SoundIoPrivate *si) { struct SoundIoAlsa *sia = &si->backend_data.alsa; if (sia->thread) { - atomic_flag_clear(&sia->abort_flag); + SOUNDIO_ATOMIC_FLAG_CLEAR(sia->abort_flag); wakeup_device_poll(sia); soundio_os_thread_destroy(sia->thread); } @@ -804,7 +804,7 @@ static void device_thread_run(void *arg) { int err; for (;;) { 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; if (poll_num == -1) { if (errno == EINTR) @@ -980,7 +980,7 @@ static void outstream_destroy_alsa(struct SoundIoPrivate *si, struct SoundIoOutS struct SoundIoOutStreamAlsa *osa = &os->backend_data.alsa; if (osa->thread) { - atomic_flag_clear(&osa->thread_exit_flag); + SOUNDIO_ATOMIC_FLAG_CLEAR(osa->thread_exit_flag); wakeup_outstream_poll(osa); soundio_os_thread_destroy(osa->thread); 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) { 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; if ((err = snd_pcm_poll_descriptors_revents(osa->handle, 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) { 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; continue; } @@ -1133,9 +1133,9 @@ static void outstream_thread_run(void *arg) { outstream->error_callback(outstream, err); return; } - if (!atomic_flag_test_and_set(&osa->thread_exit_flag)) + if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osa->thread_exit_flag)) 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) { outstream->error_callback(outstream, SoundIoErrorStreaming); return; @@ -1213,12 +1213,12 @@ static void instream_thread_run(void *arg) { case SND_PCM_STATE_PAUSED: { 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; instream->error_callback(instream, SoundIoErrorStreaming); return; } - if (!atomic_flag_test_and_set(&isa->thread_exit_flag)) + if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isa->thread_exit_flag)) return; 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 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) outstream->software_latency = 1.0; @@ -1426,7 +1426,7 @@ static int outstream_start_alsa(struct SoundIoPrivate *si, struct SoundIoOutStre assert(!osa->thread); 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))) return err; @@ -1515,7 +1515,7 @@ static int outstream_clear_buffer_alsa(struct SoundIoPrivate *si, struct SoundIoOutStreamPrivate *os) { struct SoundIoOutStreamAlsa *osa = &os->backend_data.alsa; - atomic_flag_clear(&osa->clear_buffer_flag); + SOUNDIO_ATOMIC_FLAG_CLEAR(osa->clear_buffer_flag); return 0; } @@ -1560,7 +1560,7 @@ static void instream_destroy_alsa(struct SoundIoPrivate *si, struct SoundIoInStr struct SoundIoInStreamAlsa *isa = &is->backend_data.alsa; if (isa->thread) { - atomic_flag_clear(&isa->thread_exit_flag); + SOUNDIO_ATOMIC_FLAG_CLEAR(isa->thread_exit_flag); soundio_os_thread_destroy(isa->thread); isa->thread = NULL; } @@ -1727,7 +1727,7 @@ static int instream_start_alsa(struct SoundIoPrivate *si, struct SoundIoInStream assert(!isa->thread); - atomic_flag_test_and_set(&isa->thread_exit_flag); + SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isa->thread_exit_flag); int err; if ((err = soundio_os_thread_create(instream_thread_run, is, soundio->emit_rtprio_warning, &isa->thread))) { instream_destroy_alsa(si, is); @@ -1857,7 +1857,7 @@ int soundio_alsa_init(struct SoundIoPrivate *si) { sia->notify_fd = -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(); if (!sia->mutex) { diff --git a/src/alsa.h b/src/alsa.h index c358085..e2b1764 100644 --- a/src/alsa.h +++ b/src/alsa.h @@ -32,7 +32,7 @@ struct SoundIoAlsa { struct SoundIoOsCond *cond; struct SoundIoOsThread *thread; - atomic_flag abort_flag; + struct SoundIoAtomicFlag abort_flag; int notify_fd; int notify_wd; bool have_devices_flag; @@ -60,11 +60,11 @@ struct SoundIoOutStreamAlsa { struct pollfd *poll_fds; int poll_exit_pipe_fd[2]; struct SoundIoOsThread *thread; - atomic_flag thread_exit_flag; + struct SoundIoAtomicFlag thread_exit_flag; snd_pcm_uframes_t period_size; int write_frame_count; bool is_paused; - atomic_flag clear_buffer_flag; + struct SoundIoAtomicFlag clear_buffer_flag; struct SoundIoChannelArea areas[SOUNDIO_MAX_CHANNELS]; }; @@ -79,7 +79,7 @@ struct SoundIoInStreamAlsa { int poll_fd_count; struct pollfd *poll_fds; struct SoundIoOsThread *thread; - atomic_flag thread_exit_flag; + struct SoundIoAtomicFlag thread_exit_flag; int period_size; int read_frame_count; bool is_paused; diff --git a/src/atomics.h b/src/atomics.h index 7e31cd3..d236435 100644 --- a/src/atomics.h +++ b/src/atomics.h @@ -27,10 +27,17 @@ struct SoundIoAtomicBool { std::atomic x; }; +struct SoundIoAtomicFlag { + std::atomic_flag x; +}; + #define SOUNDIO_ATOMIC_LOAD(a) (a.x.load()) #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_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 @@ -48,10 +55,17 @@ struct SoundIoAtomicBool { atomic_bool x; }; +struct SoundIoAtomicFlag { + atomic_flag 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_STORE(a, value) atomic_store(&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 diff --git a/src/coreaudio.c b/src/coreaudio.c index f625af0..7c96121 100644 --- a/src/coreaudio.c +++ b/src/coreaudio.c @@ -152,7 +152,7 @@ static void destroy_ca(struct SoundIoPrivate *si) { SoundIoListAudioDeviceID_deinit(&sica->registered_listeners); 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_thread_destroy(sica->thread); } @@ -846,7 +846,7 @@ static void device_thread_run(void *arg) { int err; for (;;) { - if (!atomic_flag_test_and_set(&sica->abort_flag)) + if (!SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(sica->abort_flag)) break; if (SOUNDIO_ATOMIC_LOAD(sica->service_restarted)) { shutdown_backend(si, SoundIoErrorBackendDisconnected); @@ -1122,7 +1122,7 @@ static OSStatus read_callback_ca(void *userdata, AudioUnitRenderActionFlags *io_ } 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))) { 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->device_scan_queued, true); 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(); if (!sica->mutex) { diff --git a/src/coreaudio.h b/src/coreaudio.h index a919f0e..784a71c 100644 --- a/src/coreaudio.h +++ b/src/coreaudio.h @@ -30,7 +30,7 @@ struct SoundIoCoreAudio { struct SoundIoOsMutex *mutex; struct SoundIoOsCond *cond; struct SoundIoOsThread *thread; - atomic_flag abort_flag; + struct SoundIoAtomicFlag abort_flag; // this one is ready to be read with flush_events. protected by mutex struct SoundIoDevicesInfo *ready_devices_info; diff --git a/src/dummy.c b/src/dummy.c index 3ae5d78..f1a3828 100644 --- a/src/dummy.c +++ b/src/dummy.c @@ -25,14 +25,14 @@ static void playback_thread_run(void *arg) { double start_time = soundio_os_get_time(); 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 time_passed = now - start_time; double next_period = start_time + ceil_dbl(time_passed / osd->period_duration) * osd->period_duration; double relative_time = next_period - now; 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); int free_bytes = soundio_ring_buffer_capacity(&osd->ring_buffer); int free_frames = free_bytes / outstream->bytes_per_frame; @@ -84,7 +84,7 @@ static void capture_thread_run(void *arg) { long frames_consumed = 0; 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 time_passed = now - 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; 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_thread_destroy(osd->thread); osd->thread = NULL; @@ -177,7 +177,7 @@ static int outstream_open_dummy(struct SoundIoPrivate *si, struct SoundIoOutStre struct SoundIoOutStream *outstream = &os->pub; 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); 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 SoundIo *soundio = &si->pub; assert(!osd->thread); - atomic_flag_test_and_set(&osd->abort_flag); + SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osd->abort_flag); int err; if ((err = soundio_os_thread_create(playback_thread_run, os, 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) { 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); return 0; } @@ -275,7 +275,7 @@ static void instream_destroy_dummy(struct SoundIoPrivate *si, struct SoundIoInSt struct SoundIoInStreamDummy *isd = &is->backend_data.dummy; 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_thread_destroy(isd->thread); 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 SoundIo *soundio = &si->pub; assert(!isd->thread); - atomic_flag_test_and_set(&isd->abort_flag); + SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(isd->abort_flag); int err; if ((err = soundio_os_thread_create(capture_thread_run, is, soundio->emit_rtprio_warning, &isd->thread))) diff --git a/src/dummy.h b/src/dummy.h index 6ea4761..d50d12e 100644 --- a/src/dummy.h +++ b/src/dummy.h @@ -27,14 +27,14 @@ struct SoundIoDeviceDummy { int make_the_struct_not_empty; }; struct SoundIoOutStreamDummy { struct SoundIoOsThread *thread; struct SoundIoOsCond *cond; - atomic_flag abort_flag; + struct SoundIoAtomicFlag abort_flag; double period_duration; int buffer_frame_count; int frames_left; int write_frame_count; struct SoundIoRingBuffer ring_buffer; double playback_start_time; - atomic_flag clear_buffer_flag; + struct SoundIoAtomicFlag clear_buffer_flag; struct SoundIoAtomicBool pause_requested; struct SoundIoChannelArea areas[SOUNDIO_MAX_CHANNELS]; }; @@ -42,7 +42,7 @@ struct SoundIoOutStreamDummy { struct SoundIoInStreamDummy { struct SoundIoOsThread *thread; struct SoundIoOsCond *cond; - atomic_flag abort_flag; + struct SoundIoAtomicFlag abort_flag; double period_duration; int frames_left; int read_frame_count; diff --git a/src/jack.c b/src/jack.c index 0d6882a..c56bf15 100644 --- a/src/jack.c +++ b/src/jack.c @@ -11,7 +11,7 @@ #include -static atomic_flag global_msg_callback_flag = ATOMIC_FLAG_INIT; +static struct SoundIoAtomicFlag global_msg_callback_flag = SOUNDIO_ATOMIC_FLAG_INIT; struct SoundIoJackPort { const char *full_name; @@ -315,9 +315,9 @@ static void my_flush_events(struct SoundIoPrivate *si, bool wait) { if (cb_shutdown) { soundio->on_backend_disconnect(soundio, SoundIoErrorBackendDisconnected); } 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))) { - atomic_flag_clear(&sij->refresh_devices_flag); + SOUNDIO_ATOMIC_FLAG_CLEAR(sij->refresh_devices_flag); } else { 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) { struct SoundIo *soundio = &si->pub; 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_cond_signal(sij->cond, sij->mutex); 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) { struct SoundIo *soundio = &si->pub; 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_cond_signal(sij->cond, sij->mutex); soundio->on_events_signal(soundio); @@ -869,12 +869,12 @@ int soundio_jack_init(struct SoundIoPrivate *si) { struct SoundIoJack *sij = &si->backend_data.jack; 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) jack_set_error_function(soundio->jack_error_callback); if (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(); @@ -923,7 +923,7 @@ int soundio_jack_init(struct SoundIoPrivate *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->sample_rate = jack_get_sample_rate(sij->client); diff --git a/src/jack.h b/src/jack.h index 5381360..e96cf9b 100644 --- a/src/jack.h +++ b/src/jack.h @@ -38,7 +38,7 @@ struct SoundIoJack { jack_client_t *client; struct SoundIoOsMutex *mutex; struct SoundIoOsCond *cond; - atomic_flag refresh_devices_flag; + struct SoundIoAtomicFlag refresh_devices_flag; int sample_rate; int period_size; bool is_shutdown; diff --git a/src/pulseaudio.c b/src/pulseaudio.c index 293ec5c..d594c3b 100644 --- a/src/pulseaudio.c +++ b/src/pulseaudio.c @@ -667,7 +667,7 @@ static int outstream_open_pa(struct SoundIoPrivate *si, struct SoundIoOutStreamP struct SoundIoPulseAudio *sipa = &si->backend_data.pulseaudio; 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); @@ -783,7 +783,7 @@ static int outstream_end_write_pa(struct SoundIoPrivate *si, struct SoundIoOutSt struct SoundIoOutStreamPulseAudio *ospa = &os->backend_data.pulseaudio; 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)) return SoundIoErrorStreaming; @@ -794,7 +794,7 @@ static int outstream_clear_buffer_pa(struct SoundIoPrivate *si, struct SoundIoOutStreamPrivate *os) { struct SoundIoOutStreamPulseAudio *ospa = &os->backend_data.pulseaudio; - atomic_flag_clear(&ospa->clear_buffer_flag); + SOUNDIO_ATOMIC_FLAG_CLEAR(ospa->clear_buffer_flag); return 0; } diff --git a/src/pulseaudio.h b/src/pulseaudio.h index 7feff6c..a216e26 100644 --- a/src/pulseaudio.h +++ b/src/pulseaudio.h @@ -46,7 +46,7 @@ struct SoundIoOutStreamPulseAudio { pa_buffer_attr buffer_attr; char *write_ptr; size_t write_byte_count; - atomic_flag clear_buffer_flag; + struct SoundIoAtomicFlag clear_buffer_flag; struct SoundIoChannelArea areas[SOUNDIO_MAX_CHANNELS]; }; diff --git a/src/soundio.c b/src/soundio.c index 1ed2ff2..5e12418 100644 --- a/src/soundio.c +++ b/src/soundio.c @@ -171,9 +171,9 @@ static void default_backend_disconnect_cb(struct SoundIo *soundio, int 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) { - 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, "See " "https://github.com/andrewrk/genesis/wiki/warning:-unable-to-set-high-priority-thread:-Operation-not-permitted\n"); diff --git a/src/wasapi.c b/src/wasapi.c index 09d52db..46c2a2e 100644 --- a/src/wasapi.c +++ b/src/wasapi.c @@ -1048,7 +1048,7 @@ static void outstream_destroy_wasapi(struct SoundIoPrivate *si, struct SoundIoOu struct SoundIoOutStreamWasapi *osw = &os->backend_data.wasapi; if (osw->thread) { - atomic_flag_clear(&osw->thread_exit_flag); + SOUNDIO_ATOMIC_FLAG_CLEAR(osw->thread_exit_flag); if (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; soundio_os_mutex_lock(osw->mutex); 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); return; } soundio_os_mutex_unlock(osw->mutex); 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 (FAILED(hr = IAudioClient_Stop(osw->audio_client))) { outstream->error_callback(outstream, SoundIoErrorStreaming); @@ -1286,10 +1286,10 @@ static void outstream_shared_run(struct SoundIoOutStreamPrivate *os) { outstream->error_callback(outstream, SoundIoErrorStreaming); return; } - atomic_flag_clear(&osw->pause_resume_flag); + SOUNDIO_ATOMIC_FLAG_CLEAR(osw->pause_resume_flag); 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); if (pause && !osw->is_paused) { if (FAILED(hr = IAudioClient_Stop(osw->audio_client))) { @@ -1335,9 +1335,9 @@ static void outstream_raw_run(struct SoundIoOutStreamPrivate *os) { for (;;) { 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; - 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); if (pause && !osw->is_paused) { if (FAILED(hr = IAudioClient_Stop(osw->audio_client))) { @@ -1382,7 +1382,7 @@ static void outstream_thread_run(void *arg) { osw->open_complete = true; soundio_os_cond_signal(osw->cond, osw->mutex); 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); return; } @@ -1407,8 +1407,8 @@ static int outstream_open_wasapi(struct SoundIoPrivate *si, struct SoundIoOutStr struct SoundIoDevice *device = outstream->device; struct SoundIo *soundio = &si->pub; - 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->pause_resume_flag); + SOUNDIO_ATOMIC_FLAG_TEST_AND_SET(osw->clear_buffer_flag); 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 @@ -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; if ((err = soundio_os_thread_create(outstream_thread_run, os, 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; 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) { SetEvent(osw->h_event); } else { @@ -1534,7 +1534,7 @@ static int outstream_clear_buffer_wasapi(struct SoundIoPrivate *si, struct Sound if (osw->h_event) { return SoundIoErrorIncompatibleDevice; } else { - atomic_flag_clear(&osw->clear_buffer_flag); + SOUNDIO_ATOMIC_FLAG_CLEAR(osw->clear_buffer_flag); soundio_os_mutex_lock(osw->mutex); soundio_os_cond_signal(osw->cond, 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; if (isw->thread) { - atomic_flag_clear(&isw->thread_exit_flag); + SOUNDIO_ATOMIC_FLAG_CLEAR(isw->thread_exit_flag); if (isw->h_event) SetEvent(isw->h_event); @@ -1750,7 +1750,7 @@ static void instream_raw_run(struct SoundIoInStreamPrivate *is) { for (;;) { 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; 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 (;;) { soundio_os_mutex_lock(isw->mutex); 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); return; } @@ -1813,7 +1813,7 @@ static void instream_thread_run(void *arg) { isw->open_complete = true; soundio_os_cond_signal(isw->cond, isw->mutex); 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); 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; if ((err = soundio_os_thread_create(instream_thread_run, is, soundio->emit_rtprio_warning, &isw->thread))) diff --git a/src/wasapi.h b/src/wasapi.h index 240c5bf..5df3d44 100644 --- a/src/wasapi.h +++ b/src/wasapi.h @@ -63,15 +63,15 @@ struct SoundIoOutStreamWasapi { struct SoundIoOsMutex *mutex; struct SoundIoOsCond *cond; struct SoundIoOsCond *start_cond; - atomic_flag thread_exit_flag; + struct SoundIoAtomicFlag thread_exit_flag; bool is_raw; int writable_frame_count; UINT32 buffer_frame_count; int write_frame_count; HANDLE h_event; struct SoundIoAtomicBool desired_pause_state; - atomic_flag pause_resume_flag; - atomic_flag clear_buffer_flag; + struct SoundIoAtomicFlag pause_resume_flag; + struct SoundIoAtomicFlag clear_buffer_flag; bool is_paused; bool open_complete; int open_err; @@ -89,7 +89,7 @@ struct SoundIoInStreamWasapi { struct SoundIoOsMutex *mutex; struct SoundIoOsCond *cond; struct SoundIoOsCond *start_cond; - atomic_flag thread_exit_flag; + struct SoundIoAtomicFlag thread_exit_flag; bool is_raw; int readable_frame_count; UINT32 buffer_frame_count;