src/util/numeric.{cpp,h}: Fix FacePositionCache data race
authorBřetislav Štec <valsiterb@gmail.com>
Sun, 2 Aug 2015 13:08:39 +0000 (15:08 +0200)
committerest31 <MTest31@outlook.com>
Sun, 2 Aug 2015 17:42:22 +0000 (19:42 +0200)
src/util/numeric.cpp
src/util/numeric.h

index 4ddfede92b045f189119f22c4e093b7aca7041a2..3fd1c9cf9ef44574f12760eef69d1a586511ee2e 100644 (file)
@@ -23,13 +23,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "log.h"
 #include "../constants.h" // BS, MAP_BLOCKSIZE
 #include "../noise.h" // PseudoRandom, PcgRandom
+#include "../jthread/jmutexautolock.h"
 #include <string.h>
 #include <iostream>
 
 std::map<u16, std::vector<v3s16> > FacePositionCache::m_cache;
+JMutex FacePositionCache::m_cache_mutex;
 // 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)
 {
+       JMutexAutoLock cachelock(m_cache_mutex);
        if (m_cache.find(d) != m_cache.end())
                return m_cache[d];
 
index 0eba01359ea6db92c9cfcf67c0ac157df04ca738..9fe08434f5df6af6b6132accf0cd01600bdb718d 100644 (file)
@@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "../irr_v2d.h"
 #include "../irr_v3d.h"
 #include "../irr_aabb3d.h"
+#include "../jthread/jmutex.h"
 #include <list>
 #include <map>
 #include <vector>
@@ -41,6 +42,7 @@ public:
 private:
        static void generateFacePosition(u16 d);
        static std::map<u16, std::vector<v3s16> > m_cache;
+       static JMutex m_cache_mutex;
 };
 
 class IndentationRaiser