diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-08-25 10:12:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-25 10:12:25 +0200 |
commit | fa8006477918a7c22c9d46db199d7cc4d238856e (patch) | |
tree | 0ec79e3d7f36c48d916bf9acd362e986ff6fb26f /servers/audio | |
parent | 0a5f5c85584df93863f9aed50a1ae3e1aa18f932 (diff) | |
parent | 9d546bf05a31e46f21077de3718d369ee7660b59 (diff) |
Merge pull request #64607 from RandomShaper/safe_audio_threading
Diffstat (limited to 'servers/audio')
-rw-r--r-- | servers/audio/audio_driver_dummy.cpp | 19 | ||||
-rw-r--r-- | servers/audio/audio_driver_dummy.h | 6 |
2 files changed, 12 insertions, 13 deletions
diff --git a/servers/audio/audio_driver_dummy.cpp b/servers/audio/audio_driver_dummy.cpp index 60eb657923..729f278d4b 100644 --- a/servers/audio/audio_driver_dummy.cpp +++ b/servers/audio/audio_driver_dummy.cpp @@ -36,9 +36,8 @@ AudioDriverDummy *AudioDriverDummy::singleton = nullptr; Error AudioDriverDummy::init() { - active = false; - thread_exited = false; - exit_thread = false; + active.clear(); + exit_thread.clear(); samples_in = nullptr; if (mix_rate == -1) { @@ -60,23 +59,23 @@ void AudioDriverDummy::thread_func(void *p_udata) { uint64_t usdelay = (ad->buffer_frames / float(ad->mix_rate)) * 1000000; - while (!ad->exit_thread) { - if (ad->active) { + while (!ad->exit_thread.is_set()) { + if (ad->active.is_set()) { ad->lock(); + ad->start_counting_ticks(); ad->audio_server_process(ad->buffer_frames, ad->samples_in); + ad->stop_counting_ticks(); ad->unlock(); }; OS::get_singleton()->delay_usec(usdelay); }; - - ad->thread_exited = true; }; void AudioDriverDummy::start() { - active = true; + active.set(); }; int AudioDriverDummy::get_mix_rate() const { @@ -113,7 +112,7 @@ uint32_t AudioDriverDummy::get_channels() const { } void AudioDriverDummy::mix_audio(int p_frames, int32_t *p_buffer) { - ERR_FAIL_COND(!active); // If not active, should not mix. + ERR_FAIL_COND(!active.is_set()); // If not active, should not mix. ERR_FAIL_COND(use_threads == true); // If using threads, this will not work well. uint32_t todo = p_frames; @@ -136,7 +135,7 @@ void AudioDriverDummy::mix_audio(int p_frames, int32_t *p_buffer) { void AudioDriverDummy::finish() { if (use_threads) { - exit_thread = true; + exit_thread.set(); thread.wait_to_finish(); } diff --git a/servers/audio/audio_driver_dummy.h b/servers/audio/audio_driver_dummy.h index 8f47e64d8b..46dd2e1439 100644 --- a/servers/audio/audio_driver_dummy.h +++ b/servers/audio/audio_driver_dummy.h @@ -35,6 +35,7 @@ #include "core/os/mutex.h" #include "core/os/thread.h" +#include "core/templates/safe_refcount.h" class AudioDriverDummy : public AudioDriver { Thread thread; @@ -50,9 +51,8 @@ class AudioDriverDummy : public AudioDriver { int channels; - bool active; - bool thread_exited; - mutable bool exit_thread; + SafeFlag active; + SafeFlag exit_thread; bool use_threads = true; |