From be517370a69ab6d8b35ffff58c6c5fc5068257ec Mon Sep 17 00:00:00 2001 From: lethosor Date: Mon, 4 May 2020 18:38:54 -0400 Subject: [PATCH] rendermax: move to std::mutex fast_mutex can deadlock on Linux with optimization enabled --- plugins/rendermax/renderer_light.cpp | 4 ++-- plugins/rendermax/renderer_light.hpp | 24 +++++++++++++----------- plugins/rendermax/renderer_opengl.hpp | 22 ++++++++++------------ plugins/rendermax/rendermax.cpp | 24 ++++++++++-------------- 4 files changed, 35 insertions(+), 39 deletions(-) diff --git a/plugins/rendermax/renderer_light.cpp b/plugins/rendermax/renderer_light.cpp index dda928afa..d76889220 100644 --- a/plugins/rendermax/renderer_light.cpp +++ b/plugins/rendermax/renderer_light.cpp @@ -270,7 +270,7 @@ rgbf blend(const rgbf& a,const rgbf& b) void lightingEngineViewscreen::clear() { lightMap.assign(lightMap.size(),rgbf(1,1,1)); - tthread::lock_guard guard(myRenderer->dataMutex); + std::lock_guard guard{myRenderer->dataMutex}; if(lightMap.size()==myRenderer->lightGrid.size()) { std::swap(myRenderer->lightGrid,lightMap); @@ -299,7 +299,7 @@ void lightingEngineViewscreen::calculate() } void lightingEngineViewscreen::updateWindow() { - tthread::lock_guard guard(myRenderer->dataMutex); + std::lock_guard guard{myRenderer->dataMutex}; if(lightMap.size()!=myRenderer->lightGrid.size()) { reinit(); diff --git a/plugins/rendermax/renderer_light.hpp b/plugins/rendermax/renderer_light.hpp index b0fb8c5d5..4e779f3fa 100644 --- a/plugins/rendermax/renderer_light.hpp +++ b/plugins/rendermax/renderer_light.hpp @@ -1,11 +1,14 @@ -#ifndef RENDERER_LIGHT_INCLUDED -#define RENDERER_LIGHT_INCLUDED -#include "renderer_opengl.hpp" -#include "Types.h" -#include -#include +#pragma once + #include +#include +#include +#include #include + +#include "renderer_opengl.hpp" +#include "Types.h" + // we are not using boost so let's cheat: template inline void hash_combine(std::size_t & seed, const T & v) @@ -91,7 +94,7 @@ private: } void reinitLightGrid(int w,int h) { - tthread::lock_guard guard(dataMutex); + std::lock_guard guard{dataMutex}; lightGrid.resize(w*h,rgbf(1,1,1)); } void reinitLightGrid() @@ -100,7 +103,7 @@ private: } public: - tthread::fast_mutex dataMutex; + std::mutex dataMutex; std::vector lightGrid; renderer_light(renderer* parent):renderer_wrap(parent),light_adaptation(1) { @@ -108,12 +111,12 @@ public: } virtual void update_tile(int32_t x, int32_t y) { renderer_wrap::update_tile(x,y); - tthread::lock_guard guard(dataMutex); + std::lock_guard guard{dataMutex}; colorizeTile(x,y); }; virtual void update_all() { renderer_wrap::update_all(); - tthread::lock_guard guard(dataMutex); + std::lock_guard guard{dataMutex}; for (int x = 0; x < df::global::gps->dimx; x++) for (int y = 0; y < df::global::gps->dimy; y++) colorizeTile(x,y); @@ -374,4 +377,3 @@ private: }; rgbf blend(const rgbf& a,const rgbf& b); rgbf blendMax(const rgbf& a,const rgbf& b); -#endif diff --git a/plugins/rendermax/renderer_opengl.hpp b/plugins/rendermax/renderer_opengl.hpp index 76a45918c..a0a920e69 100644 --- a/plugins/rendermax/renderer_opengl.hpp +++ b/plugins/rendermax/renderer_opengl.hpp @@ -1,9 +1,7 @@ //original file from https://github.com/Baughn/Dwarf-Fortress--libgraphics- -#ifndef RENDERER_OPENGL_INCLUDED -#define RENDERER_OPENGL_INCLUDED +#pragma once #include "tinythread.h" -#include "fast_mutex.h" #include "Core.h" #include @@ -15,6 +13,7 @@ #include "df/graphic.h" #include #include +#include using df::renderer; using df::init; @@ -281,7 +280,7 @@ private: } void reinitLightGrid(int w,int h) { - tthread::lock_guard guard(dataMutex); + std::lock_guard guard{dataMutex}; lightGrid.resize(w*h); } void reinitLightGrid() @@ -289,7 +288,7 @@ private: reinitLightGrid(df::global::gps->dimy,df::global::gps->dimx); } public: - tthread::fast_mutex dataMutex; + std::mutex dataMutex; std::vector lightGrid; renderer_test(renderer* parent):renderer_wrap(parent) { @@ -297,14 +296,14 @@ public: } virtual void update_tile(int32_t x, int32_t y) { renderer_wrap::update_tile(x,y); - tthread::lock_guard guard(dataMutex); + std::lock_guard guard{dataMutex}; colorizeTile(x,y); //some sort of mutex or sth? //and then map read }; virtual void update_all() { renderer_wrap::update_all(); - tthread::lock_guard guard(dataMutex); + std::lock_guard guard{dataMutex}; for (int x = 0; x < df::global::gps->dimx; x++) for (int y = 0; y < df::global::gps->dimy; y++) colorizeTile(x,y); @@ -366,7 +365,7 @@ private: } void reinitGrids(int w,int h) { - tthread::lock_guard guard(dataMutex); + std::lock_guard guard{dataMutex}; foreOffset.resize(w*h); foreMult.resize(w*h); backOffset.resize(w*h); @@ -377,7 +376,7 @@ private: reinitGrids(df::global::gps->dimy,df::global::gps->dimx); } public: - tthread::fast_mutex dataMutex; + std::mutex dataMutex; std::vector foreOffset,foreMult; std::vector backOffset,backMult; inline int xyToTile(int x, int y) @@ -390,14 +389,14 @@ public: } virtual void update_tile(int32_t x, int32_t y) { renderer_wrap::update_tile(x,y); - tthread::lock_guard guard(dataMutex); + std::lock_guard guard{dataMutex}; overwriteTile(x,y); //some sort of mutex or sth? //and then map read }; virtual void update_all() { renderer_wrap::update_all(); - tthread::lock_guard guard(dataMutex); + std::lock_guard guard{dataMutex}; for (int x = 0; x < df::global::gps->dimx; x++) for (int y = 0; y < df::global::gps->dimy; y++) overwriteTile(x,y); @@ -414,4 +413,3 @@ public: reinitGrids(w,h); } }; -#endif \ No newline at end of file diff --git a/plugins/rendermax/rendermax.cpp b/plugins/rendermax/rendermax.cpp index be8be07ba..17fd6fa95 100644 --- a/plugins/rendermax/rendermax.cpp +++ b/plugins/rendermax/rendermax.cpp @@ -1,27 +1,23 @@ -#include +#include +#include #include +#include -#include - -#include - -#include "Core.h" #include "Console.h" +#include "Core.h" #include "Export.h" +#include "LuaTools.h" #include "PluginManager.h" +#include "VTableInterpose.h" -#include -#include "df/renderer.h" #include "df/enabler.h" +#include "df/renderer.h" +#include "df/viewscreen_dungeonmodest.h" +#include "df/viewscreen_dwarfmodest.h" #include "renderer_opengl.hpp" #include "renderer_light.hpp" -#include "df/viewscreen_dwarfmodest.h" -#include "df/viewscreen_dungeonmodest.h" - -#include - using df::viewscreen_dungeonmodest; using df::viewscreen_dwarfmodest; @@ -367,7 +363,7 @@ static command_result rendermax(color_ostream &out, vector & parameters cur=blue; renderer_test* r=reinterpret_cast(enabler->renderer); - tthread::lock_guard guard(r->dataMutex); + std::lock_guard guard{r->dataMutex}; int h=gps->dimy; int w=gps->dimx; int cx=w/2;