From f1b03b48e9fcf97d53653ccc91c3b802e0b529c6 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Thu, 16 Mar 2017 17:53:12 +0100 Subject: [PATCH] - add thread safety to GetSpecialLights --- src/swrenderer/r_swcolormaps.cpp | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/swrenderer/r_swcolormaps.cpp b/src/swrenderer/r_swcolormaps.cpp index 3e39cde21a..306caf647c 100644 --- a/src/swrenderer/r_swcolormaps.cpp +++ b/src/swrenderer/r_swcolormaps.cpp @@ -70,12 +70,16 @@ TArray SpecialSWColormaps; // //========================================================================== -FDynamicColormap *GetSpecialLights (PalEntry color, PalEntry fade, int desaturate) +static FDynamicColormap *CreateSpecialLights (PalEntry color, PalEntry fade, int desaturate) { - FDynamicColormap *colormap; + // GetSpecialLights is called by the scene worker threads. + // If we didn't find the colormap, search again, but this time one thread at a time + static std::mutex buildmapmutex; + std::unique_lock lock(buildmapmutex); // If this colormap has already been created, just return it - for (colormap = &NormalLight; colormap != NULL; colormap = colormap->Next) + // This may happen if another thread beat us to it + for (FDynamicColormap *colormap = &NormalLight; colormap != NULL; colormap = colormap->Next) { if (color == colormap->Color && fade == colormap->Fade && @@ -86,7 +90,7 @@ FDynamicColormap *GetSpecialLights (PalEntry color, PalEntry fade, int desaturat } // Not found. Create it. - colormap = new FDynamicColormap; + FDynamicColormap *colormap = new FDynamicColormap; colormap->Next = NormalLight.Next; colormap->Color = color; colormap->Fade = fade; @@ -98,6 +102,22 @@ FDynamicColormap *GetSpecialLights (PalEntry color, PalEntry fade, int desaturat return colormap; } +FDynamicColormap *GetSpecialLights (PalEntry color, PalEntry fade, int desaturate) +{ + // If this colormap has already been created, just return it + for (FDynamicColormap *colormap = &NormalLight; colormap != NULL; colormap = colormap->Next) + { + if (color == colormap->Color && + fade == colormap->Fade && + desaturate == colormap->Desaturate) + { + return colormap; + } + } + + return CreateSpecialLights(color, fade, desaturate); +} + //========================================================================== // // Free all lights created with GetSpecialLights