src/util/numeric.{cpp,h}: Fix FacePositionCache data race

master
Břetislav Štec 2015-08-02 15:08:39 +02:00 committed by est31
parent 060e56b24c
commit abe6c072d6
2 changed files with 6 additions and 0 deletions

View File

@ -23,13 +23,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h" #include "log.h"
#include "../constants.h" // BS, MAP_BLOCKSIZE #include "../constants.h" // BS, MAP_BLOCKSIZE
#include "../noise.h" // PseudoRandom, PcgRandom #include "../noise.h" // PseudoRandom, PcgRandom
#include "../jthread/jmutexautolock.h"
#include <string.h> #include <string.h>
#include <iostream> #include <iostream>
std::map<u16, std::vector<v3s16> > FacePositionCache::m_cache; std::map<u16, std::vector<v3s16> > FacePositionCache::m_cache;
JMutex FacePositionCache::m_cache_mutex;
// Calculate the borders of a "d-radius" cube // Calculate the borders of a "d-radius" cube
// TODO: Make it work without mutex and data races, probably thread-local
std::vector<v3s16> FacePositionCache::getFacePositions(u16 d) std::vector<v3s16> FacePositionCache::getFacePositions(u16 d)
{ {
JMutexAutoLock cachelock(m_cache_mutex);
if (m_cache.find(d) != m_cache.end()) if (m_cache.find(d) != m_cache.end())
return m_cache[d]; return m_cache[d];

View File

@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "../irr_v2d.h" #include "../irr_v2d.h"
#include "../irr_v3d.h" #include "../irr_v3d.h"
#include "../irr_aabb3d.h" #include "../irr_aabb3d.h"
#include "../jthread/jmutex.h"
#include <list> #include <list>
#include <map> #include <map>
#include <vector> #include <vector>
@ -41,6 +42,7 @@ public:
private: private:
static void generateFacePosition(u16 d); static void generateFacePosition(u16 d);
static std::map<u16, std::vector<v3s16> > m_cache; static std::map<u16, std::vector<v3s16> > m_cache;
static JMutex m_cache_mutex;
}; };
class IndentationRaiser class IndentationRaiser