[1.7.10] Sleep sleeping for incorrect amou...
Cruor 10 Apr 2015
MC 1.7.10, CraftOS 1.6, ComputerCraft1.65
Bug is also in latest beta (1.74pr20)
When sleeping for 0.2s towards 0.65s included, it adds another tick. Meaning a 0.2s sleep actually is 0.25, and so on.
Output from test program, first arg is sample count, second is time. (Yes, it is consistent with higher sample count)
Edit:
After decompiling the source, the problem seems to be in OSAPI.java
Digging deeper, said method is called from ServerComputer.java
Edit 2:
Proof that this is due to the use of doubles, and not tick alignment (or whatever other magical phenomenon you specify it under).
https://ideone.com/hqthf8
Can this be changed to ticks represented in integers instead?
Edited by Cruor, 19 May 2015 - 07:30 PM.
Bug is also in latest beta (1.74pr20)
When sleeping for 0.2s towards 0.65s included, it adds another tick. Meaning a 0.2s sleep actually is 0.25, and so on.
Output from test program, first arg is sample count, second is time. (Yes, it is consistent with higher sample count)
Edit:
After decompiling the source, the problem seems to be in OSAPI.java
public void advance(double dt) { Map var3 = this.m_timers; synchronized(this.m_timers) { this.m_clock += dt; Iterator previousTime = this.m_timers.entrySet().iterator(); while(previousTime.hasNext()) { Entry entry = (Entry)previousTime.next(); OSAPI.Timer previousDay = (OSAPI.Timer)entry.getValue(); previousDay.m_timeLeft -= dt; if(previousDay.m_timeLeft <= 0.0D) { this.queueLuaEvent("timer", new Object[]{entry.getKey()}); previousTime.remove(); } } }The inaccuracy is caused by the use of doubles at the m_timeLeft -= dt;
Digging deeper, said method is called from ServerComputer.java
public void update() { super.update(); this.m_computer.advance(0.05D); this.m_changedLastFrame = this.m_changed || this.m_computer.pollChanged(); this.m_computer.clearChanged(); this.m_changed = false; ++this.m_ticksSincePing; }
Edit 2:
Proof that this is due to the use of doubles, and not tick alignment (or whatever other magical phenomenon you specify it under).
https://ideone.com/hqthf8
Incase Ideone doesn't keep paste around
Can this be changed to ticks represented in integers instead?
Edited by Cruor, 19 May 2015 - 07:30 PM.
theoriginalbit 12 May 2015
Just FYI there isn't really a solution to this it will potentially always be off a little bit because OS timing isn't perfect either, if you tell a thread to sleep for 500 milliseconds (in Java: Thread.sleep(500)) then there is every chance it will sleep too long. If the computer cannot wake the thread at the desired time it cannot wake it instead it must wait until the next available chance, period.
Edited by theoriginalbit, 12 May 2015 - 10:26 PM.
Edited by theoriginalbit, 12 May 2015 - 10:26 PM.
Cruor 19 May 2015
Added Java code too show the problem with using doubles.
Edited by Cruor, 19 May 2015 - 07:31 PM.
Edited by Cruor, 19 May 2015 - 07:31 PM.
theoriginalbit 20 May 2015
Except that doubles have to be used otherwise you cannot sleep for fractions of a second. Also Lua only uses doubles, never ints.