Home » U++ Library support » U++ Core » Missing GetTickCount64 (Added function on posix platforms for cross compilation)
Missing GetTickCount64 [message #43811] |
Thu, 23 October 2014 11:50 |
|
Windows Vista and newer has got a GetTickCount64 function, I was recently making a long running application and had a use for a non overflowing timer value.
So here is a simple expansion of the existing block in Core/Utils:
#ifdef PLATFORM_POSIX
dword GetTickCount() {
#if _POSIX_C_SOURCE >= 199309L
struct timespec tp;
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
{
return (dword)((tp.tv_sec * 1000) + (tp.tv_nsec / 1000000));
}
return 0; // ?? (errno is set)
#else
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
}
qword GetTickCount64() {
#if _POSIX_C_SOURCE >= 199309L
struct timespec tp;
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
{
return (qword)((tp.tv_sec * 1000) + (tp.tv_nsec / 1000000));
}
return 0; // ?? (errno is set)
#else
struct timeval tv[1];
struct timezone tz[1];
memset(tz, 0, sizeof(tz));
gettimeofday(tv, tz);
return (qword)tv->tv_sec * 1000 + tv->tv_usec / 1000;
#endif
}
#endif
|
|
|
|
Re: Missing GetTickCount64 [message #43825 is a reply to message #43820] |
Wed, 29 October 2014 10:27 |
Tom1
Messages: 1281 Registered: March 2007
|
Senior Contributor |
|
|
Hi,
This is not an easy task. I have spent days or maybe weeks over the last 20 years to get a reliable, precise monotonic timer that works. This relatively recent Microsoft article sheds some light on the task:
http://msdn.microsoft.com/en-us/library/windows/desktop/dn55 3408%28v=vs.85%29.aspx
I guess QueryPerformanceCounter() is the way to go, but it is slow to call and has not been entirely trouble free either. I have solved the problem using the 32 bit timer and some overflow detection solutions to build monotonic timer for longer periods. (Sleeping and hibernating might still be a problem though.)
Best regards,
Tom
|
|
|
Goto Forum:
Current Time: Mon Dec 09 11:22:19 CET 2024
Total time taken to generate the page: 0.02725 seconds
|