PDA

View Full Version : unix "Sleep" implementation bug


erde
11-13-2009, 07:29 AM
After looking in eqlaunch.cpp i found somethin wired. eqlaunch contains this

if(zones.empty())
Sleep(5000);
else
Sleep(2000);


to wait 5 or 2 seconds but in common/unix.cpp "Sleep" is implemented as

void Sleep(unsigned int x) {
if (x > 0)
usleep(x*1000);
}

so eqlaunch will wait 5*1000 seconds ! this seems to produce an overflow and my NetBSD 64bit QuadCore uses 100% CPU time.

after changing it to


void Sleep(unsigned int x) {
if (x > 0)
usleep(x);
}

everything is ok

erde
11-13-2009, 08:07 AM
Nevermind i missinterpreted something, the high cpu usage might be caused by something else.

erde
11-13-2009, 08:20 AM
Ok, after a bit of reading Sleep should look like this:

void Sleep(unsigned int x) {
if (x <= 0 )
return;
if ( x>=1000 )
sleep(x/1000);
else
usleep(x*1000);
}


From the man page:
The useconds argument must be less than 1,000,000. If the value of useconds is 0, then the call has no effect.

Found here: Source (http://www.opengroup.org/onlinepubs/007908799/xsh/usleep.html)

KLS
11-13-2009, 11:20 PM
Okay I'm seeing what you're saying.

void Sleep(unsigned int x) {
if (x <= 0 )
return;
if ( x>=1000 )
usleep(999999);
else
usleep(x*1000);
}

Would be the implementation I'd use though, slightly faster due to no recursion and the functionality isn't... odd.

erde
11-20-2009, 12:03 PM
There is no recursion ;) Sleep != sleep
I think you already know that, c++ is case sensitive

KLS
11-20-2009, 01:30 PM
Yeah I didn't even notice the non-capital letter.