Add a function to obtain relative millisecond-timestamps

'Relative' in the sense that they can only sensibly compared to
another time value obtained using the same function during the
same program invocation.
master
Rogier 2016-08-03 09:35:20 +02:00
parent cbfb7e6979
commit 2f4a917bb1
5 changed files with 47 additions and 8 deletions

View File

@ -178,6 +178,7 @@
<ClInclude Include="..\PixelAttributes.h" />
<ClInclude Include="..\PlayerAttributes.h" />
<ClInclude Include="..\porting.h" />
<ClInclude Include="..\porting_win32.h" />
<ClInclude Include="..\Settings.h" />
<ClInclude Include="..\TileGenerator.h" />
<ClInclude Include="..\types.h" />

View File

@ -54,6 +54,9 @@
<ClInclude Include="..\porting.h">
<Filter>Header files</Filter>
</ClInclude>
<ClInclude Include="..\porting_win32.h">
<Filter>Header files</Filter>
</ClInclude>
<ClInclude Include="resource.h">
<Filter>Header files</Filter>
</ClInclude>

View File

@ -2,15 +2,9 @@
#define _PORTING_H
#ifdef _WIN32
#include <windows.h>
#define sleepMs(x) Sleep(x)
#include "porting_win32.h"
#else
#include <unistd.h>
#define sleepMs(x) usleep(x*1000)
#include "porting_posix.h"
#endif
#ifdef _MSC_VER
@ -20,3 +14,4 @@
#endif
#endif // _PORTING_H

30
porting_posix.h Normal file
View File

@ -0,0 +1,30 @@
#include <ctime>
#include <cerrno>
#include <unistd.h>
#include <sys/time.h>
#define sleepMs(x) usleep((x)*1000)
inline uint64_t getRelativeTimeStampMs()
{
int rv = -1;
struct timespec ts;
#ifdef CLOCK_MONOTONIC_RAW
if (rv == -1)
rv = clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
#endif
#ifdef _POSIX_MONOTONIC_CLOCK
if (rv == -1)
rv = clock_gettime(CLOCK_MONOTONIC, &ts);
#endif
if (rv == -1) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
else {
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
}

10
porting_win32.h Normal file
View File

@ -0,0 +1,10 @@
#include <windows.h>
#define sleepMs(x) Sleep(x)
inline uint64_t getRelativeTimeStampMs()
{
return GetTickCount64();
}