1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-14 13:41:12 +00:00

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-27 20:10:10 +01:00
parent 8f6a636ae7
commit 6d89f675b1
84 changed files with 370 additions and 1080 deletions

View File

@@ -609,7 +609,7 @@ void EditorFileSystem::scan() {
if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/)
return;
if (scanning || scanning_changes || thread)
if (scanning || scanning_changes || thread.is_started())
return;
_update_extensions();
@@ -632,13 +632,13 @@ void EditorFileSystem::scan() {
first_scan = false;
} else {
ERR_FAIL_COND(thread);
ERR_FAIL_COND(thread.is_started());
set_process(true);
Thread::Settings s;
scanning = true;
scan_total = 0;
s.priority = Thread::PRIORITY_LOW;
thread = Thread::create(_thread_func, this, s);
thread.start(_thread_func, this, s);
//tree->hide();
//progress->show();
}
@@ -1067,7 +1067,7 @@ void EditorFileSystem::get_changed_sources(List<String> *r_changed) {
void EditorFileSystem::scan_changes() {
if (first_scan || // Prevent a premature changes scan from inhibiting the first full scan
scanning || scanning_changes || thread) {
scanning || scanning_changes || thread.is_started()) {
scan_changes_pending = true;
set_process(true);
return;
@@ -1097,12 +1097,12 @@ void EditorFileSystem::scan_changes() {
emit_signal("sources_changed", sources_changed.size() > 0);
} else {
ERR_FAIL_COND(thread_sources);
ERR_FAIL_COND(thread_sources.is_started());
set_process(true);
scan_total = 0;
Thread::Settings s;
s.priority = Thread::PRIORITY_LOW;
thread_sources = Thread::create(_thread_func_sources, this, s);
thread_sources.start(_thread_func_sources, this, s);
}
}
@@ -1116,17 +1116,14 @@ void EditorFileSystem::_notification(int p_what) {
} break;
case NOTIFICATION_EXIT_TREE: {
Thread *active_thread = thread ? thread : thread_sources;
if (use_threads && active_thread) {
Thread &active_thread = thread.is_started() ? thread : thread_sources;
if (use_threads && active_thread.is_started()) {
//abort thread if in progress
abort_scan = true;
while (scanning) {
OS::get_singleton()->delay_usec(1000);
}
Thread::wait_to_finish(active_thread);
memdelete(active_thread);
thread = NULL;
thread_sources = NULL;
active_thread.wait_to_finish();
WARN_PRINT("Scan thread aborted...");
set_process(false);
}
@@ -1151,9 +1148,7 @@ void EditorFileSystem::_notification(int p_what) {
set_process(false);
Thread::wait_to_finish(thread_sources);
memdelete(thread_sources);
thread_sources = NULL;
thread_sources.wait_to_finish();
if (_update_scan_actions())
emit_signal("filesystem_changed");
emit_signal("sources_changed", sources_changed.size() > 0);
@@ -1168,9 +1163,7 @@ void EditorFileSystem::_notification(int p_what) {
memdelete(filesystem);
filesystem = new_filesystem;
new_filesystem = NULL;
Thread::wait_to_finish(thread);
memdelete(thread);
thread = NULL;
thread.wait_to_finish();
_update_scan_actions();
emit_signal("filesystem_changed");
emit_signal("sources_changed", sources_changed.size() > 0);
@@ -2135,11 +2128,9 @@ EditorFileSystem::EditorFileSystem() {
filesystem = memnew(EditorFileSystemDirectory); //like, empty
filesystem->parent = NULL;
thread = NULL;
scanning = false;
importing = false;
use_threads = true;
thread_sources = NULL;
new_filesystem = NULL;
abort_scan = false;