Merge pull request #64607 from RandomShaper/safe_audio_threading
This commit is contained in:
commit
fa80064779
13 changed files with 76 additions and 84 deletions
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue