diff options
author | paradust7 <102263465+paradust7@users.noreply.github.com> | 2022-05-23 13:50:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-23 22:50:25 +0200 |
commit | 367a2d4b29a4865a8f2d5b9717786a8660f226bc (patch) | |
tree | 71a89df0512ec999f40684e4bbe9ff0cd9b796ac /src/log.h | |
parent | 0f9c78c3ebf920fac65030e66367b9940055075f (diff) | |
download | minetest-367a2d4b29a4865a8f2d5b9717786a8660f226bc.tar.gz minetest-367a2d4b29a4865a8f2d5b9717786a8660f226bc.tar.bz2 minetest-367a2d4b29a4865a8f2d5b9717786a8660f226bc.zip |
Add missing concurrency protection in logger (#12325)
Diffstat (limited to 'src/log.h')
-rw-r--r-- | src/log.h | 12 |
1 files changed, 10 insertions, 2 deletions
@@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #if !defined(_WIN32) // POSIX #include <unistd.h> #endif +#include "threading/mutex_auto_lock.h" #include "util/basic_macros.h" #include "util/stream.h" #include "irrlichttypes.h" @@ -168,24 +169,31 @@ public: void clear() { + MutexAutoLock lock(m_buffer_mutex); m_buffer = std::queue<std::string>(); } bool empty() const { + MutexAutoLock lock(m_buffer_mutex); return m_buffer.empty(); } std::string get() { - if (empty()) + MutexAutoLock lock(m_buffer_mutex); + if (m_buffer.empty()) return ""; - std::string s = m_buffer.front(); + std::string s = std::move(m_buffer.front()); m_buffer.pop(); return s; } private: + // g_logger serializes calls to logRaw() with a mutex, but that + // doesn't prevent get() / clear() from being called on top of it. + // This mutex prevents that. + mutable std::mutex m_buffer_mutex; std::queue<std::string> m_buffer; Logger &m_logger; }; |