mirror of
https://github.com/minetest/minetest.git
synced 2024-12-23 22:52:25 +01:00
Fix race condition on accessing m_time_of_day_speed causing day night race on some architectures
This commit is contained in:
parent
b3a2ef1a91
commit
f6fc39e69a
@ -42,6 +42,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
#include "map.h"
|
||||
#include "emerge.h"
|
||||
#include "util/serialize.h"
|
||||
#include "jthread/jmutexautolock.h"
|
||||
|
||||
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
|
||||
|
||||
@ -196,12 +197,30 @@ u32 Environment::getDayNightRatio()
|
||||
return time_to_daynight_ratio(m_time_of_day_f*24000, smooth);
|
||||
}
|
||||
|
||||
void Environment::setTimeOfDaySpeed(float speed)
|
||||
{
|
||||
JMutexAutoLock(this->m_lock);
|
||||
m_time_of_day_speed = speed;
|
||||
}
|
||||
|
||||
float Environment::getTimeOfDaySpeed()
|
||||
{
|
||||
JMutexAutoLock(this->m_lock);
|
||||
float retval = m_time_of_day_speed;
|
||||
return retval;
|
||||
}
|
||||
|
||||
void Environment::stepTimeOfDay(float dtime)
|
||||
{
|
||||
float day_speed = 0;
|
||||
{
|
||||
JMutexAutoLock(this->m_lock);
|
||||
day_speed = m_time_of_day_speed;
|
||||
}
|
||||
|
||||
m_time_counter += dtime;
|
||||
f32 speed = m_time_of_day_speed * 24000./(24.*3600);
|
||||
f32 speed = day_speed * 24000./(24.*3600);
|
||||
u32 units = (u32)(m_time_counter*speed);
|
||||
m_time_counter -= (f32)units / speed;
|
||||
bool sync_f = false;
|
||||
if(units > 0){
|
||||
// Sync at overflow
|
||||
@ -211,8 +230,11 @@ void Environment::stepTimeOfDay(float dtime)
|
||||
if(sync_f)
|
||||
m_time_of_day_f = (float)m_time_of_day / 24000.0;
|
||||
}
|
||||
if (speed > 0) {
|
||||
m_time_counter -= (f32)units / speed;
|
||||
}
|
||||
if(!sync_f){
|
||||
m_time_of_day_f += m_time_of_day_speed/24/3600*dtime;
|
||||
m_time_of_day_f += day_speed/24/3600*dtime;
|
||||
if(m_time_of_day_f > 1.0)
|
||||
m_time_of_day_f -= 1.0;
|
||||
if(m_time_of_day_f < 0.0)
|
||||
|
@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
#include "util/numeric.h"
|
||||
#include "mapnode.h"
|
||||
#include "mapblock.h"
|
||||
#include "jthread/jmutex.h"
|
||||
|
||||
class ServerEnvironment;
|
||||
class ActiveBlockModifier;
|
||||
@ -93,11 +94,9 @@ public:
|
||||
|
||||
void stepTimeOfDay(float dtime);
|
||||
|
||||
void setTimeOfDaySpeed(float speed)
|
||||
{ m_time_of_day_speed = speed; }
|
||||
void setTimeOfDaySpeed(float speed);
|
||||
|
||||
float getTimeOfDaySpeed()
|
||||
{ return m_time_of_day_speed; }
|
||||
float getTimeOfDaySpeed();
|
||||
|
||||
void setDayNightRatioOverride(bool enable, u32 value)
|
||||
{
|
||||
@ -121,6 +120,9 @@ protected:
|
||||
// Overriding the day-night ratio is useful for custom sky visuals
|
||||
bool m_enable_day_night_ratio_override;
|
||||
u32 m_day_night_ratio_override;
|
||||
|
||||
private:
|
||||
JMutex m_lock;
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user