From 8f989f7ae09680780e93a95e80487ae8628e78c0 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 3 Aug 2021 00:24:15 +0200 Subject: [PATCH] Fix potential race condition. --- world/jobs/terra_terrarin_job.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/world/jobs/terra_terrarin_job.cpp b/world/jobs/terra_terrarin_job.cpp index 18b78f2..623651a 100644 --- a/world/jobs/terra_terrarin_job.cpp +++ b/world/jobs/terra_terrarin_job.cpp @@ -22,9 +22,9 @@ SOFTWARE. #include "terra_terrarin_job.h" +#include "../../library/terra_material_cache.h" #include "../../library/terra_surface.h" #include "../../library/terraman_library.h" -#include "../../library/terra_material_cache.h" #include "../../meshers/blocky/terra_mesher_blocky.h" #include "../../meshers/default/terra_mesher_default.h" @@ -99,7 +99,23 @@ void TerraTerrarinJob::phase_library_setup() { } if (lib->supports_caching()) { - lib->material_cache_get_key(_chunk); + if (!_chunk->material_cache_key_has()) { + lib->material_cache_get_key(_chunk); + } else { + Ref cache = lib->material_cache_get(_chunk->material_cache_key_get()); + + if (!cache.is_valid()) { + next_phase(); + return; + } + + if (!cache->get_initialized()) { + //Means it's currently merging the atlases on a different thread. + //Let's just wait -> return. + //This method will get called again later. + return; + } + } } next_phase();