diff --git a/utest.h b/utest.h index 1cbe3df..b610804 100644 --- a/utest.h +++ b/utest.h @@ -56,21 +56,25 @@ #pragma warning(pop) #elif defined(__linux__) + // slightly obscure include here - we need to include glibc's features.h, but // we don't want to just include a header that might not be defined for other // c libraries like musl. Instead we include limits.h, which we know on all // glibc distributions includes features.h #include -#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) && \ - ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) -// glibc is version 2.17 or above, so we can just use clock_gettime +#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) #include -#define UTEST_USE_CLOCKGETTIME -#endif -#else -#error Unknown platform! +#if ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) +// glibc is version 2.17 or above, so we can just use clock_gettime +#define UTEST_USE_CLOCKGETTIME +#else // ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) +#include +#include +#endif // ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) +#endif // defined(__GLIBC__) && defined(__GLIBC_MINOR__) + #endif #if defined(_MSC_VER) @@ -83,7 +87,7 @@ static void __cdecl f(void) #else #define UTEST_INLINE inline - + #define UTEST_INITIALIZER(f) \ static void f(void) __attribute__((constructor)); \ static void f(void) @@ -103,14 +107,14 @@ #if defined(_MSC_VER) return 0; #elif defined(__linux) -#if defined(UTEST_USE_CLOCKGETTIME) struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - return UTEST_CAST(long, ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec; + const clockid_t cid = CLOCK_REALTIME; +#if defined(UTEST_USE_CLOCKGETTIME) + clock_gettime(cid, &ts); #else - // no support yet on linux for glibc < 2.17 - return 0; + syscall(SYS_clock_gettime, cid, &ts); #endif + return UTEST_CAST(long, ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec; #endif }