Make audio thread control flags safe
This commit is contained in:
@ -168,9 +168,8 @@ Error AudioDriverALSA::init() {
|
||||
return ERR_CANT_OPEN;
|
||||
}
|
||||
|
||||
active = false;
|
||||
thread_exited = false;
|
||||
exit_thread = false;
|
||||
active.clear();
|
||||
exit_thread.clear();
|
||||
|
||||
Error err = init_device();
|
||||
if (err == OK) {
|
||||
@ -183,11 +182,11 @@ Error AudioDriverALSA::init() {
|
||||
void AudioDriverALSA::thread_func(void *p_udata) {
|
||||
AudioDriverALSA *ad = static_cast<AudioDriverALSA *>(p_udata);
|
||||
|
||||
while (!ad->exit_thread) {
|
||||
while (!ad->exit_thread.is_set()) {
|
||||
ad->lock();
|
||||
ad->start_counting_ticks();
|
||||
|
||||
if (!ad->active) {
|
||||
if (!ad->active.is_set()) {
|
||||
for (uint64_t i = 0; i < ad->period_size * ad->channels; i++) {
|
||||
ad->samples_out.write[i] = 0;
|
||||
}
|
||||
@ -203,7 +202,7 @@ void AudioDriverALSA::thread_func(void *p_udata) {
|
||||
int todo = ad->period_size;
|
||||
int total = 0;
|
||||
|
||||
while (todo && !ad->exit_thread) {
|
||||
while (todo && !ad->exit_thread.is_set()) {
|
||||
int16_t *src = (int16_t *)ad->samples_out.ptr();
|
||||
int wrote = snd_pcm_writei(ad->pcm_handle, (void *)(src + (total * ad->channels)), todo);
|
||||
|
||||
@ -222,8 +221,8 @@ void AudioDriverALSA::thread_func(void *p_udata) {
|
||||
wrote = snd_pcm_recover(ad->pcm_handle, wrote, 0);
|
||||
if (wrote < 0) {
|
||||
ERR_PRINT("ALSA: Failed and can't recover: " + String(snd_strerror(wrote)));
|
||||
ad->active = false;
|
||||
ad->exit_thread = true;
|
||||
ad->active.clear();
|
||||
ad->exit_thread.set();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -241,8 +240,8 @@ void AudioDriverALSA::thread_func(void *p_udata) {
|
||||
|
||||
err = ad->init_device();
|
||||
if (err != OK) {
|
||||
ad->active = false;
|
||||
ad->exit_thread = true;
|
||||
ad->active.clear();
|
||||
ad->exit_thread.set();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -250,12 +249,10 @@ void AudioDriverALSA::thread_func(void *p_udata) {
|
||||
ad->stop_counting_ticks();
|
||||
ad->unlock();
|
||||
}
|
||||
|
||||
ad->thread_exited = true;
|
||||
}
|
||||
|
||||
void AudioDriverALSA::start() {
|
||||
active = true;
|
||||
active.set();
|
||||
}
|
||||
|
||||
int AudioDriverALSA::get_mix_rate() const {
|
||||
@ -327,7 +324,7 @@ void AudioDriverALSA::finish_device() {
|
||||
}
|
||||
|
||||
void AudioDriverALSA::finish() {
|
||||
exit_thread = true;
|
||||
exit_thread.set();
|
||||
thread.wait_to_finish();
|
||||
|
||||
finish_device();
|
||||
|
||||
@ -35,6 +35,7 @@
|
||||
|
||||
#include "core/os/mutex.h"
|
||||
#include "core/os/thread.h"
|
||||
#include "core/templates/safe_refcount.h"
|
||||
#include "servers/audio_server.h"
|
||||
|
||||
#include "asound-so_wrap.h"
|
||||
@ -64,9 +65,8 @@ class AudioDriverALSA : public AudioDriver {
|
||||
snd_pcm_uframes_t period_size;
|
||||
int channels = 0;
|
||||
|
||||
bool active = false;
|
||||
bool thread_exited = false;
|
||||
mutable bool exit_thread = false;
|
||||
SafeFlag active;
|
||||
SafeFlag exit_thread;
|
||||
|
||||
public:
|
||||
const char *get_name() const {
|
||||
|
||||
Reference in New Issue
Block a user