rendermax: move to std::mutex

fast_mutex can deadlock on Linux with optimization enabled
develop
lethosor 2020-05-04 18:38:54 -04:00
parent af33f71aa2
commit be517370a6
4 changed files with 35 additions and 39 deletions

@ -270,7 +270,7 @@ rgbf blend(const rgbf& a,const rgbf& b)
void lightingEngineViewscreen::clear() void lightingEngineViewscreen::clear()
{ {
lightMap.assign(lightMap.size(),rgbf(1,1,1)); lightMap.assign(lightMap.size(),rgbf(1,1,1));
tthread::lock_guard<tthread::fast_mutex> guard(myRenderer->dataMutex); std::lock_guard<std::mutex> guard{myRenderer->dataMutex};
if(lightMap.size()==myRenderer->lightGrid.size()) if(lightMap.size()==myRenderer->lightGrid.size())
{ {
std::swap(myRenderer->lightGrid,lightMap); std::swap(myRenderer->lightGrid,lightMap);
@ -299,7 +299,7 @@ void lightingEngineViewscreen::calculate()
} }
void lightingEngineViewscreen::updateWindow() void lightingEngineViewscreen::updateWindow()
{ {
tthread::lock_guard<tthread::fast_mutex> guard(myRenderer->dataMutex); std::lock_guard<std::mutex> guard{myRenderer->dataMutex};
if(lightMap.size()!=myRenderer->lightGrid.size()) if(lightMap.size()!=myRenderer->lightGrid.size())
{ {
reinit(); reinit();

@ -1,11 +1,14 @@
#ifndef RENDERER_LIGHT_INCLUDED #pragma once
#define RENDERER_LIGHT_INCLUDED
#include "renderer_opengl.hpp"
#include "Types.h"
#include <tuple>
#include <stack>
#include <memory> #include <memory>
#include <mutex>
#include <stack>
#include <tuple>
#include <unordered_map> #include <unordered_map>
#include "renderer_opengl.hpp"
#include "Types.h"
// we are not using boost so let's cheat: // we are not using boost so let's cheat:
template <class T> template <class T>
inline void hash_combine(std::size_t & seed, const T & v) inline void hash_combine(std::size_t & seed, const T & v)
@ -91,7 +94,7 @@ private:
} }
void reinitLightGrid(int w,int h) void reinitLightGrid(int w,int h)
{ {
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex); std::lock_guard<std::mutex> guard{dataMutex};
lightGrid.resize(w*h,rgbf(1,1,1)); lightGrid.resize(w*h,rgbf(1,1,1));
} }
void reinitLightGrid() void reinitLightGrid()
@ -100,7 +103,7 @@ private:
} }
public: public:
tthread::fast_mutex dataMutex; std::mutex dataMutex;
std::vector<rgbf> lightGrid; std::vector<rgbf> lightGrid;
renderer_light(renderer* parent):renderer_wrap(parent),light_adaptation(1) 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) { virtual void update_tile(int32_t x, int32_t y) {
renderer_wrap::update_tile(x,y); renderer_wrap::update_tile(x,y);
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex); std::lock_guard<std::mutex> guard{dataMutex};
colorizeTile(x,y); colorizeTile(x,y);
}; };
virtual void update_all() { virtual void update_all() {
renderer_wrap::update_all(); renderer_wrap::update_all();
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex); std::lock_guard<std::mutex> guard{dataMutex};
for (int x = 0; x < df::global::gps->dimx; x++) for (int x = 0; x < df::global::gps->dimx; x++)
for (int y = 0; y < df::global::gps->dimy; y++) for (int y = 0; y < df::global::gps->dimy; y++)
colorizeTile(x,y); colorizeTile(x,y);
@ -374,4 +377,3 @@ private:
}; };
rgbf blend(const rgbf& a,const rgbf& b); rgbf blend(const rgbf& a,const rgbf& b);
rgbf blendMax(const rgbf& a,const rgbf& b); rgbf blendMax(const rgbf& a,const rgbf& b);
#endif

@ -1,9 +1,7 @@
//original file from https://github.com/Baughn/Dwarf-Fortress--libgraphics- //original file from https://github.com/Baughn/Dwarf-Fortress--libgraphics-
#ifndef RENDERER_OPENGL_INCLUDED #pragma once
#define RENDERER_OPENGL_INCLUDED
#include "tinythread.h" #include "tinythread.h"
#include "fast_mutex.h"
#include "Core.h" #include "Core.h"
#include <VTableInterpose.h> #include <VTableInterpose.h>
@ -15,6 +13,7 @@
#include "df/graphic.h" #include "df/graphic.h"
#include <math.h> #include <math.h>
#include <cmath> #include <cmath>
#include <mutex>
using df::renderer; using df::renderer;
using df::init; using df::init;
@ -281,7 +280,7 @@ private:
} }
void reinitLightGrid(int w,int h) void reinitLightGrid(int w,int h)
{ {
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex); std::lock_guard<std::mutex> guard{dataMutex};
lightGrid.resize(w*h); lightGrid.resize(w*h);
} }
void reinitLightGrid() void reinitLightGrid()
@ -289,7 +288,7 @@ private:
reinitLightGrid(df::global::gps->dimy,df::global::gps->dimx); reinitLightGrid(df::global::gps->dimy,df::global::gps->dimx);
} }
public: public:
tthread::fast_mutex dataMutex; std::mutex dataMutex;
std::vector<rgbf> lightGrid; std::vector<rgbf> lightGrid;
renderer_test(renderer* parent):renderer_wrap(parent) renderer_test(renderer* parent):renderer_wrap(parent)
{ {
@ -297,14 +296,14 @@ public:
} }
virtual void update_tile(int32_t x, int32_t y) { virtual void update_tile(int32_t x, int32_t y) {
renderer_wrap::update_tile(x,y); renderer_wrap::update_tile(x,y);
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex); std::lock_guard<std::mutex> guard{dataMutex};
colorizeTile(x,y); colorizeTile(x,y);
//some sort of mutex or sth? //some sort of mutex or sth?
//and then map read //and then map read
}; };
virtual void update_all() { virtual void update_all() {
renderer_wrap::update_all(); renderer_wrap::update_all();
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex); std::lock_guard<std::mutex> guard{dataMutex};
for (int x = 0; x < df::global::gps->dimx; x++) for (int x = 0; x < df::global::gps->dimx; x++)
for (int y = 0; y < df::global::gps->dimy; y++) for (int y = 0; y < df::global::gps->dimy; y++)
colorizeTile(x,y); colorizeTile(x,y);
@ -366,7 +365,7 @@ private:
} }
void reinitGrids(int w,int h) void reinitGrids(int w,int h)
{ {
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex); std::lock_guard<std::mutex> guard{dataMutex};
foreOffset.resize(w*h); foreOffset.resize(w*h);
foreMult.resize(w*h); foreMult.resize(w*h);
backOffset.resize(w*h); backOffset.resize(w*h);
@ -377,7 +376,7 @@ private:
reinitGrids(df::global::gps->dimy,df::global::gps->dimx); reinitGrids(df::global::gps->dimy,df::global::gps->dimx);
} }
public: public:
tthread::fast_mutex dataMutex; std::mutex dataMutex;
std::vector<rgbf> foreOffset,foreMult; std::vector<rgbf> foreOffset,foreMult;
std::vector<rgbf> backOffset,backMult; std::vector<rgbf> backOffset,backMult;
inline int xyToTile(int x, int y) inline int xyToTile(int x, int y)
@ -390,14 +389,14 @@ public:
} }
virtual void update_tile(int32_t x, int32_t y) { virtual void update_tile(int32_t x, int32_t y) {
renderer_wrap::update_tile(x,y); renderer_wrap::update_tile(x,y);
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex); std::lock_guard<std::mutex> guard{dataMutex};
overwriteTile(x,y); overwriteTile(x,y);
//some sort of mutex or sth? //some sort of mutex or sth?
//and then map read //and then map read
}; };
virtual void update_all() { virtual void update_all() {
renderer_wrap::update_all(); renderer_wrap::update_all();
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex); std::lock_guard<std::mutex> guard{dataMutex};
for (int x = 0; x < df::global::gps->dimx; x++) for (int x = 0; x < df::global::gps->dimx; x++)
for (int y = 0; y < df::global::gps->dimy; y++) for (int y = 0; y < df::global::gps->dimy; y++)
overwriteTile(x,y); overwriteTile(x,y);
@ -414,4 +413,3 @@ public:
reinitGrids(w,h); reinitGrids(w,h);
} }
}; };
#endif

@ -1,27 +1,23 @@
#include <vector> #include <mutex>
#include <sstream>
#include <string> #include <string>
#include <vector>
#include <LuaTools.h>
#include <VTableInterpose.h>
#include "Core.h"
#include "Console.h" #include "Console.h"
#include "Core.h"
#include "Export.h" #include "Export.h"
#include "LuaTools.h"
#include "PluginManager.h" #include "PluginManager.h"
#include "VTableInterpose.h"
#include <VTableInterpose.h>
#include "df/renderer.h"
#include "df/enabler.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_opengl.hpp"
#include "renderer_light.hpp" #include "renderer_light.hpp"
#include "df/viewscreen_dwarfmodest.h"
#include "df/viewscreen_dungeonmodest.h"
#include <sstream>
using df::viewscreen_dungeonmodest; using df::viewscreen_dungeonmodest;
using df::viewscreen_dwarfmodest; using df::viewscreen_dwarfmodest;
@ -367,7 +363,7 @@ static command_result rendermax(color_ostream &out, vector <string> & parameters
cur=blue; cur=blue;
renderer_test* r=reinterpret_cast<renderer_test*>(enabler->renderer); renderer_test* r=reinterpret_cast<renderer_test*>(enabler->renderer);
tthread::lock_guard<tthread::fast_mutex> guard(r->dataMutex); std::lock_guard<std::mutex> guard{r->dataMutex};
int h=gps->dimy; int h=gps->dimy;
int w=gps->dimx; int w=gps->dimx;
int cx=w/2; int cx=w/2;