diff options
author | sapier <Sapier at GMX dot net> | 2014-06-22 23:19:10 +0200 |
---|---|---|
committer | sapier <Sapier at GMX dot net> | 2014-06-23 00:26:57 +0200 |
commit | f6fc39e69a0a621480bec9ceafd154ec83cf0100 (patch) | |
tree | 603c593d6207478e19b731423e0fe436424bf16f /src/environment.cpp | |
parent | b3a2ef1a911b5ee22ff6478b5a3f9a12f6e40121 (diff) | |
download | minetest-f6fc39e69a0a621480bec9ceafd154ec83cf0100.tar.gz minetest-f6fc39e69a0a621480bec9ceafd154ec83cf0100.tar.bz2 minetest-f6fc39e69a0a621480bec9ceafd154ec83cf0100.zip |
Fix race condition on accessing m_time_of_day_speed causing day night race on some architectures
Diffstat (limited to 'src/environment.cpp')
-rw-r--r-- | src/environment.cpp | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/environment.cpp b/src/environment.cpp index 8103998c3..6bbc715d0 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -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) |