Jump to content




[1.7.10] Sleep sleeping for incorrect amount of ticks


6 replies to this topic

#1 Cruor

  • Members
  • 413 posts
  • LocationNorway

Posted 10 April 2015 - 05:19 PM

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)
Posted Image


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.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 10 April 2015 - 11:07 PM

Also related.

#3 Cruor

  • Members
  • 413 posts
  • LocationNorway

Posted 12 May 2015 - 04:08 PM

Added more details and potential solution.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 May 2015 - 10:25 PM

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.


#5 Cruor

  • Members
  • 413 posts
  • LocationNorway

Posted 19 May 2015 - 07:31 PM

Added Java code too show the problem with using doubles.

Edited by Cruor, 19 May 2015 - 07:31 PM.


#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 May 2015 - 12:16 AM

Except that doubles have to be used otherwise you cannot sleep for fractions of a second. Also Lua only uses doubles, never ints.

#7 dan200

  • Administrators
  • 542 posts
  • LocationCambridge, England

Posted 20 May 2015 - 07:45 PM

Fixed in next version





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users