Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Little problem in util.cpp and simple solution
Little problem in util.cpp and simple solution [message #35984] Sun, 15 April 2012 22:16 Go to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Mirek

In util.cpp there is this:

#ifdef PLATFORM_POSIX
dword GetTickCount() {
	struct timeval tv[1];
	struct timezone tz[1];
	memset(tz, 0, sizeof(tz));
	gettimeofday(tv, tz);
	return (dword)tv->tv_sec * 1000 + tv->tv_usec / 1000;
}

int msecs(int from) { return int((GetTickCount() - (dword)from) & 0x7fffffff); }

#endif
This produces linking problems in MinGW.

Just moving msecs() outside the #ifdef solves the problem:
#ifdef PLATFORM_POSIX
dword GetTickCount() {
	struct timeval tv[1];
	struct timezone tz[1];
	memset(tz, 0, sizeof(tz));
	gettimeofday(tv, tz);
	return (dword)tv->tv_sec * 1000 + tv->tv_usec / 1000;
}

#endif

int msecs(int from) { return int((GetTickCount() - (dword)from) & 0x7fffffff); }


Best regards
IƱaki
Re: Little problem in util.cpp and simple solution [message #35992 is a reply to message #35984] Mon, 16 April 2012 21:55 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Yes. Hopefully, it is the last one because if inet refactoring, as it is almost finished...
Re: Little problem in util.cpp and simple solution [message #35995 is a reply to message #35992] Tue, 17 April 2012 09:45 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Removing sign bit like this:
int msecs(int from) { return int((GetTickCount() - (dword)from) & 0x7fffffff); }
                                                                ^^^^^^^^^^^^^^^^


is not a good idea. There are some places in upp that rely on sign bit (e.g. httcli):

if(msecs(end_time) >= 0) {
^^^^^^^^^^^^^^^^^^^^^^^^^^
	error = NFormat("Timeout reading footer block (%d B).", body.GetLength());
	break;
}


While in other places msecs is used incorrectly (httpcli again):
while(msecs() < end_time) {


Someone forgot that ticks wrap every ~50 days (or ~25 days if sign bit is masked).

Re: Little problem in util.cpp and simple solution [message #35996 is a reply to message #35995] Tue, 17 April 2012 09:52 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Maybe it's time to migrate to 64-bit ticks?

Re: Little problem in util.cpp and simple solution [message #35997 is a reply to message #35995] Tue, 17 April 2012 10:03 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Zbych wrote on Tue, 17 April 2012 03:45

Removing sign bit like this:
int msecs(int from) { return int((GetTickCount() - (dword)from) & 0x7fffffff); }
                                                                ^^^^^^^^^^^^^^^^


is not a good idea. There are some places in upp that rely on sign bit (e.g. httcli):

if(msecs(end_time) >= 0) {
^^^^^^^^^^^^^^^^^^^^^^^^^^
	error = NFormat("Timeout reading footer block (%d B).", body.GetLength());
	break;
}


While in other places msecs is used incorrectly (httpcli again):
while(msecs() < end_time) {


Someone forgot that ticks wrap every ~50 days (or ~25 days if sign bit is masked).




Thanks, correct, reverted and hopefully places where wrap applies fixed (please check).

Mirek
Previous Topic: LaunchWebBrowser() problem and perhaps not the best solution
Next Topic: Jsonize problems with maps
Goto Forum:
  


Current Time: Fri Mar 29 16:16:00 CET 2024

Total time taken to generate the page: 0.02060 seconds