Skip to content

Commit 0e5e497

Browse files
committedMar 22, 2015
Protect Player::hud from concurrent modifications
Sometimes HUD can be modified by ServerThread and EmergeThread results in a crash on client side because the HUD is not correct
1 parent d6638b4 commit 0e5e497

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed
 

Diff for: ‎src/player.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ void Player::deSerialize(std::istream &is, std::string playername)
240240

241241
u32 Player::addHud(HudElement *toadd)
242242
{
243+
JMutexAutoLock lock(m_mutex);
243244
u32 id = getFreeHudID();
244245

245246
if (id < hud.size())
@@ -252,6 +253,8 @@ u32 Player::addHud(HudElement *toadd)
252253

253254
HudElement* Player::getHud(u32 id)
254255
{
256+
JMutexAutoLock lock(m_mutex);
257+
255258
if (id < hud.size())
256259
return hud[id];
257260

@@ -260,6 +263,8 @@ HudElement* Player::getHud(u32 id)
260263

261264
HudElement* Player::removeHud(u32 id)
262265
{
266+
JMutexAutoLock lock(m_mutex);
267+
263268
HudElement* retval = NULL;
264269
if (id < hud.size()) {
265270
retval = hud[id];
@@ -270,6 +275,8 @@ HudElement* Player::removeHud(u32 id)
270275

271276
void Player::clearHud()
272277
{
278+
JMutexAutoLock lock(m_mutex);
279+
273280
while(!hud.empty()) {
274281
delete hud.back();
275282
hud.pop_back();

Diff for: ‎src/player.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include "irrlichttypes_bloated.h"
2424
#include "inventory.h"
2525
#include "constants.h" // BS
26+
#include "jthread/jmutexautolock.h"
2627
#include <list>
2728

2829
#define PLAYERNAME_SIZE 20
@@ -202,7 +203,8 @@ class Player
202203
return m_collisionbox;
203204
}
204205

205-
u32 getFreeHudID() const {
206+
u32 getFreeHudID() {
207+
JMutexAutoLock lock(m_mutex);
206208
size_t size = hud.size();
207209
for (size_t i = 0; i != size; i++) {
208210
if (!hud[i])
@@ -318,6 +320,11 @@ class Player
318320
bool m_dirty;
319321

320322
std::vector<HudElement *> hud;
323+
private:
324+
// Protect some critical areas
325+
// hud for example can be modified by EmergeThread
326+
// and ServerThread
327+
JMutex m_mutex;
321328
};
322329

323330

0 commit comments

Comments
 (0)