Modernize Thread

- Based on C++11's `thread` and `thread_local`
- No more need to allocate-deallocate or check for null
- No pointer anymore, just a member variable
- Platform-specific implementations no longer needed (except for the few cases of non-portable functions)
- Simpler for `NO_THREADS`
- Thread ids are now the same across platforms (main is 1; others follow)
This commit is contained in:
Pedro J. Estébanez
2021-01-19 13:29:41 +01:00
parent 6ddfc8e718
commit 99fe462452
87 changed files with 385 additions and 1056 deletions

View File

@@ -48,7 +48,7 @@ Error AudioDriverDummy::init() {
samples_in = memnew_arr(int32_t, buffer_frames * channels);
thread = Thread::create(AudioDriverDummy::thread_func, this);
thread.start(AudioDriverDummy::thread_func, this);
return OK;
};
@@ -86,31 +86,18 @@ AudioDriver::SpeakerMode AudioDriverDummy::get_speaker_mode() const {
};
void AudioDriverDummy::lock() {
if (!thread) {
return;
}
mutex.lock();
};
void AudioDriverDummy::unlock() {
if (!thread) {
return;
}
mutex.unlock();
};
void AudioDriverDummy::finish() {
if (!thread) {
return;
}
exit_thread = true;
Thread::wait_to_finish(thread);
thread.wait_to_finish();
if (samples_in) {
memdelete_arr(samples_in);
};
memdelete(thread);
thread = nullptr;
};

View File

@@ -37,7 +37,7 @@
#include "core/os/thread.h"
class AudioDriverDummy : public AudioDriver {
Thread *thread = nullptr;
Thread thread;
Mutex mutex;
int32_t *samples_in;

View File

@@ -118,7 +118,7 @@ void AudioEffectRecordInstance::init() {
#ifdef NO_THREADS
AudioServer::get_singleton()->add_update_callback(&AudioEffectRecordInstance::_update, this);
#else
io_thread = Thread::create(_thread_callback, this);
io_thread.start(_thread_callback, this);
#endif
}
@@ -126,9 +126,7 @@ void AudioEffectRecordInstance::finish() {
#ifdef NO_THREADS
AudioServer::get_singleton()->remove_update_callback(&AudioEffectRecordInstance::_update, this);
#else
if (thread_active) {
Thread::wait_to_finish(io_thread);
}
io_thread.wait_to_finish();
#endif
}

View File

@@ -48,7 +48,7 @@ class AudioEffectRecordInstance : public AudioEffectInstance {
Ref<AudioEffectRecord> base;
bool is_recording;
Thread *io_thread;
Thread io_thread;
bool thread_active = false;
Vector<AudioFrame> ring_buffer;