Created
January 21, 2024 00:16
-
-
Save Masterxilo/eb4280e79fc0f9f5d89242053d15292d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <time.h> | |
#ifdef _MSC_VER | |
/** | |
* implementation of clock_gettime(CLOCK_MONOTONIC, tv) from unistd.h for Windows | |
*/ | |
#define WIN32_LEAN_AND_MEAN | |
#include <Windows.h> | |
#define MS_PER_SEC 1000ULL // MS = milliseconds | |
#define US_PER_MS 1000ULL // US = microseconds | |
#define HNS_PER_US 10ULL // HNS = hundred-nanoseconds (e.g., 1 hns = 100 ns) | |
#define NS_PER_US 1000ULL | |
#define HNS_PER_SEC (MS_PER_SEC * US_PER_MS * HNS_PER_US) | |
#define NS_PER_HNS (100ULL) // NS = nanoseconds | |
#define NS_PER_SEC (MS_PER_SEC * US_PER_MS * NS_PER_US) | |
// https://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows | |
void clock_gettime_monotonic(struct timespec *tv) | |
{ | |
static LARGE_INTEGER ticksPerSec; | |
LARGE_INTEGER ticks; | |
if (!ticksPerSec.QuadPart) | |
{ | |
QueryPerformanceFrequency(&ticksPerSec); | |
if (!ticksPerSec.QuadPart) | |
{ | |
errno = ENOTSUP; | |
fprintf(stderr, "clock_gettime_monotonic: QueryPerformanceFrequency failed\n"); | |
exit(-1); | |
} | |
} | |
QueryPerformanceCounter(&ticks); | |
tv->tv_sec = (long)(ticks.QuadPart / ticksPerSec.QuadPart); | |
tv->tv_nsec = (long)(((ticks.QuadPart % ticksPerSec.QuadPart) * NS_PER_SEC) / ticksPerSec.QuadPart); | |
} | |
#else | |
#include <unistd.h> | |
void clock_gettime_monotonic(struct timespec *tv) { | |
clock_gettime(CLOCK_MONOTONIC, tv); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment