Protect Player::hud from concurrent modifications
authorLoic Blot <loic.blot@unix-experience.fr>
Sun, 22 Mar 2015 19:09:44 +0000 (20:09 +0100)
committerLoic Blot <loic.blot@unix-experience.fr>
Sun, 22 Mar 2015 19:47:07 +0000 (20:47 +0100)
Sometimes HUD can be modified by ServerThread and EmergeThread results in a crash on client side because the HUD is not correct

src/player.cpp
src/player.h

index 0da761eedef27549505913453100d387e98d01dd..88c5634e35441aeb9da937a5431f6a32e35454d4 100644 (file)
@@ -240,6 +240,7 @@ void Player::deSerialize(std::istream &is, std::string playername)
 
 u32 Player::addHud(HudElement *toadd)
 {
+       JMutexAutoLock lock(m_mutex);
        u32 id = getFreeHudID();
 
        if (id < hud.size())
@@ -252,6 +253,8 @@ u32 Player::addHud(HudElement *toadd)
 
 HudElement* Player::getHud(u32 id)
 {
+       JMutexAutoLock lock(m_mutex);
+
        if (id < hud.size())
                return hud[id];
 
@@ -260,6 +263,8 @@ HudElement* Player::getHud(u32 id)
 
 HudElement* Player::removeHud(u32 id)
 {
+       JMutexAutoLock lock(m_mutex);
+
        HudElement* retval = NULL;
        if (id < hud.size()) {
                retval = hud[id];
@@ -270,6 +275,8 @@ HudElement* Player::removeHud(u32 id)
 
 void Player::clearHud()
 {
+       JMutexAutoLock lock(m_mutex);
+
        while(!hud.empty()) {
                delete hud.back();
                hud.pop_back();
index def428847e35d5f90655869a360a15a950c4d8ff..f38bd5615e463e672083af242773c72b014e4c16 100644 (file)
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "irrlichttypes_bloated.h"
 #include "inventory.h"
 #include "constants.h" // BS
+#include "jthread/jmutexautolock.h"
 #include <list>
 
 #define PLAYERNAME_SIZE 20
@@ -202,7 +203,8 @@ public:
                return m_collisionbox;
        }
 
-       u32 getFreeHudID() const {
+       u32 getFreeHudID() {
+               JMutexAutoLock lock(m_mutex);
                size_t size = hud.size();
                for (size_t i = 0; i != size; i++) {
                        if (!hud[i])
@@ -318,6 +320,11 @@ protected:
        bool m_dirty;
 
        std::vector<HudElement *> hud;
+private:
+       // Protect some critical areas
+       // hud for example can be modified by EmergeThread
+       // and ServerThread
+       JMutex m_mutex;
 };