1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-08 12:40:44 +00:00

Rationalize busy waits

This commit is contained in:
Pedro J. Estébanez
2024-10-31 11:09:20 +01:00
parent 44fa552343
commit 4f8dd96b3d
7 changed files with 30 additions and 22 deletions

View File

@@ -2903,9 +2903,9 @@ void EditorFileSystem::reimport_file_with_custom_parameters(const String &p_file
}
void EditorFileSystem::_reimport_thread(uint32_t p_index, ImportThreadData *p_import_data) {
int current_max = p_import_data->reimport_from + int(p_index);
p_import_data->max_index.exchange_if_greater(current_max);
_reimport_file(p_import_data->reimport_files[current_max].path);
int file_idx = p_import_data->reimport_from + int(p_index);
_reimport_file(p_import_data->reimport_files[file_idx].path);
p_import_data->imported_sem->post();
}
void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
@@ -2984,6 +2984,7 @@ void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
#endif
int from = 0;
Semaphore imported_sem;
for (int i = 0; i < reimport_files.size(); i++) {
if (groups_to_reimport.has(reimport_files[i].path)) {
from = i + 1;
@@ -3007,21 +3008,27 @@ void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
importer->import_threaded_begin();
ImportThreadData tdata;
tdata.max_index.set(from);
tdata.reimport_from = from;
tdata.reimport_files = reimport_files.ptr();
tdata.imported_sem = &imported_sem;
WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &EditorFileSystem::_reimport_thread, &tdata, i - from + 1, -1, false, vformat(TTR("Import resources of type: %s"), reimport_files[from].importer));
int current_index = from - 1;
do {
if (current_index < tdata.max_index.get()) {
current_index = tdata.max_index.get();
ep->step(reimport_files[current_index].path.get_file(), current_index, false);
int item_count = i - from + 1;
WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &EditorFileSystem::_reimport_thread, &tdata, item_count, -1, false, vformat(TTR("Import resources of type: %s"), reimport_files[from].importer));
int imported_count = 0;
while (true) {
ep->step(reimport_files[imported_count].path.get_file(), from + imported_count, false);
imported_sem.wait();
do {
imported_count++;
} while (imported_sem.try_wait());
if (imported_count == item_count) {
break;
}
OS::get_singleton()->delay_usec(1);
} while (!WorkerThreadPool::get_singleton()->is_group_task_completed(group_task));
}
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
DEV_ASSERT(!imported_sem.try_wait());
importer->import_threaded_end();
}