Skip to content

Commit

Permalink
Merge pull request #85295 from jsjtxietian/use-mutex-protect-max_inde…
Browse files Browse the repository at this point in the history
…x-in-ImportThreadData

Use `SafeNumeric` to protect `max_index` in ImportThreadData
  • Loading branch information
akien-mga committed May 2, 2024
2 parents bbb8667 + 4861ab4 commit a21824b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2328,8 +2328,9 @@ void EditorFileSystem::reimport_file_with_custom_parameters(const String &p_file
}

void EditorFileSystem::_reimport_thread(uint32_t p_index, ImportThreadData *p_import_data) {
p_import_data->max_index = MAX(p_import_data->reimport_from + int(p_index), p_import_data->max_index);
_reimport_file(p_import_data->reimport_files[p_import_data->reimport_from + p_index].path);
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);
}

void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
Expand Down Expand Up @@ -2409,15 +2410,15 @@ void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
importer->import_threaded_begin();

ImportThreadData tdata;
tdata.max_index = from;
tdata.max_index.set(from);
tdata.reimport_from = from;
tdata.reimport_files = reimport_files.ptr();

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) {
current_index = tdata.max_index;
if (current_index < tdata.max_index.get()) {
current_index = tdata.max_index.get();
pr.step(reimport_files[current_index].path.get_file(), current_index);
}
OS::get_singleton()->delay_usec(1);
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class EditorFileSystem : public Node {
struct ImportThreadData {
const ImportFile *reimport_files;
int reimport_from;
int max_index = 0;
SafeNumeric<int> max_index;
};

void _reimport_thread(uint32_t p_index, ImportThreadData *p_import_data);
Expand Down

0 comments on commit a21824b

Please sign in to comment.