Skip to content

Commit f6fc39e

Browse files
sapiersapier
sapier
authored and
sapier
committed
Fix race condition on accessing m_time_of_day_speed causing day night race on some architectures
1 parent b3a2ef1 commit f6fc39e

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/environment.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
4242
#include "map.h"
4343
#include "emerge.h"
4444
#include "util/serialize.h"
45+
#include "jthread/jmutexautolock.h"
4546

4647
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
4748

@@ -196,12 +197,30 @@ u32 Environment::getDayNightRatio()
196197
return time_to_daynight_ratio(m_time_of_day_f*24000, smooth);
197198
}
198199

200+
void Environment::setTimeOfDaySpeed(float speed)
201+
{
202+
JMutexAutoLock(this->m_lock);
203+
m_time_of_day_speed = speed;
204+
}
205+
206+
float Environment::getTimeOfDaySpeed()
207+
{
208+
JMutexAutoLock(this->m_lock);
209+
float retval = m_time_of_day_speed;
210+
return retval;
211+
}
212+
199213
void Environment::stepTimeOfDay(float dtime)
200214
{
215+
float day_speed = 0;
216+
{
217+
JMutexAutoLock(this->m_lock);
218+
day_speed = m_time_of_day_speed;
219+
}
220+
201221
m_time_counter += dtime;
202-
f32 speed = m_time_of_day_speed * 24000./(24.*3600);
222+
f32 speed = day_speed * 24000./(24.*3600);
203223
u32 units = (u32)(m_time_counter*speed);
204-
m_time_counter -= (f32)units / speed;
205224
bool sync_f = false;
206225
if(units > 0){
207226
// Sync at overflow
@@ -211,8 +230,11 @@ void Environment::stepTimeOfDay(float dtime)
211230
if(sync_f)
212231
m_time_of_day_f = (float)m_time_of_day / 24000.0;
213232
}
233+
if (speed > 0) {
234+
m_time_counter -= (f32)units / speed;
235+
}
214236
if(!sync_f){
215-
m_time_of_day_f += m_time_of_day_speed/24/3600*dtime;
237+
m_time_of_day_f += day_speed/24/3600*dtime;
216238
if(m_time_of_day_f > 1.0)
217239
m_time_of_day_f -= 1.0;
218240
if(m_time_of_day_f < 0.0)

src/environment.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3838
#include "util/numeric.h"
3939
#include "mapnode.h"
4040
#include "mapblock.h"
41+
#include "jthread/jmutex.h"
4142

4243
class ServerEnvironment;
4344
class ActiveBlockModifier;
@@ -93,11 +94,9 @@ class Environment
9394

9495
void stepTimeOfDay(float dtime);
9596

96-
void setTimeOfDaySpeed(float speed)
97-
{ m_time_of_day_speed = speed; }
97+
void setTimeOfDaySpeed(float speed);
9898

99-
float getTimeOfDaySpeed()
100-
{ return m_time_of_day_speed; }
99+
float getTimeOfDaySpeed();
101100

102101
void setDayNightRatioOverride(bool enable, u32 value)
103102
{
@@ -121,6 +120,9 @@ class Environment
121120
// Overriding the day-night ratio is useful for custom sky visuals
122121
bool m_enable_day_night_ratio_override;
123122
u32 m_day_night_ratio_override;
123+
124+
private:
125+
JMutex m_lock;
124126

125127
};
126128

0 commit comments

Comments
 (0)