Repair Delay Bug

Archives Forums/Linux Discussion/Repair Delay Bug

morszeck(Posted 2005) [#1]
Whom you test the following code, then the CPU is always working at full capacity 100%.

Graphics 800,600,0


While Not KeyHit(KEY_ESCAPE)

   DrawRect Rnd(0,800),Rnd(0,600),10,10

   
   Flip

   Delay 1000

Wend


Edit the following lines in: ../mod/brl.mod/system.mod/system.linux.c

1. One adds the following line:

#include <unistd.h>

2. Change the following function:

form:
void bbSystemDelay( int millis ){
   int      t;
   t=brl_system_MilliSecs();
   while (brl_system_MilliSecs()-t<millis) {}
//   usleep( millis/1000 );
}


to:

void bbSystemDelay( unsigned long millis ){
	usleep( millis * 1000 );
}


3. And Build Modules. Now works delay correctly.


Craig Watson(Posted 2005) [#2]
The thing about usleep is that it's not a reliable way of sleeping for a specific amount of time. Often it can actually take longer than you specified for the sleep to end.

It may not be a big deal for you if you don't need a very accurate delay, but it means that delay becomes somewhat useless for some tasks.

Probably nanosleep is better for these tasks because it has within 10ms accuracy.


morszeck(Posted 2005) [#3]
Ok, thats right. I tested it:

Strict

Graphics 800,600,0

Local ts:Int


While Not KeyHit(KEY_ESCAPE)

	ts = MilliSecs()

	DrawRect Rnd(0,800),Rnd(0,600),10,10
   
	Flip

	'---Delay 1000---

	DebugLog (1000.0/(MilliSecs() - ts))

Wend


my monitor has a dissolution of 1280x1024@75

Here the output:

58.8235283

62.5000000

58.8235283

58.8235283

62.5000000

58.8235283

55.5555573

62.5000000

58.8235283

58.8235283

62.5000000

62.5000000

55.5555573

62.5000000

58.8235283

58.8235283

62.5000000

62.5000000

55.5555573



morszeck(Posted 2005) [#4]
The delay function now repaired width nanosleep:

Edit into: ../mod/brl.mod/system.mod/system.linux.c
void bbSystemDelay( long millis ){

	struct timeval timeout;
	
	timeout.tv_sec  = millis / 1000000L;
	timeout.tv_usec = millis % 1000000L;
	
	select(0, NULL, NULL, NULL, &timeout);

}


And here: ../mod/brl.mod/system.mod/system.linux.bmx
Type TLinuxSystemDriver Extends TSystemDriver

   ...

	Method Delay( millis )
		bbSystemDelay( millis * 1000 )
	End Method

   ...

End Type


Now, Delay is now very exact. And do not forget: Build Modules.


morszeck(Posted 2005) [#5]
The new version of BlitzMax 1.12 repairs this bug. Do can trash this thread.