From: Břetislav Štec Date: Sun, 2 Aug 2015 13:08:39 +0000 (+0200) Subject: src/util/numeric.{cpp,h}: Fix FacePositionCache data race X-Git-Tag: 0.4.13~53 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=abe6c072d66777b171399f42548bad75f618f1a3;p=oweals%2Fminetest.git src/util/numeric.{cpp,h}: Fix FacePositionCache data race --- diff --git a/src/util/numeric.cpp b/src/util/numeric.cpp index 4ddfede92..3fd1c9cf9 100644 --- a/src/util/numeric.cpp +++ b/src/util/numeric.cpp @@ -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 #include std::map > 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 FacePositionCache::getFacePositions(u16 d) { + JMutexAutoLock cachelock(m_cache_mutex); if (m_cache.find(d) != m_cache.end()) return m_cache[d]; diff --git a/src/util/numeric.h b/src/util/numeric.h index 0eba01359..9fe08434f 100644 --- a/src/util/numeric.h +++ b/src/util/numeric.h @@ -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 #include #include @@ -41,6 +42,7 @@ public: private: static void generateFacePosition(u16 d); static std::map > m_cache; + static JMutex m_cache_mutex; }; class IndentationRaiser