Skip to content

Commit

Permalink
Add test for CLOCK_MONOTONIC support
Browse files Browse the repository at this point in the history
Instead of always using CLOCK_MONOTONIC, use POSIX feature testing to
determine support at compile-time, or if necessary at run-time using
sysconf. If not supported, falls back to CLOCK_REALTIME.
  • Loading branch information
lawadr committed Mar 24, 2023
1 parent db9df99 commit 4e32d57
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mz_os_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,15 @@ uint64_t mz_os_ms_time(void) {

ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
#else
#elif !defined(_POSIX_MONOTONIC_CLOCK) || _POSIX_MONOTONIC_CLOCK < 0
clock_gettime(CLOCK_REALTIME, &ts);
#elif _POSIX_MONOTONIC_CLOCK > 0
clock_gettime(CLOCK_MONOTONIC, &ts);
#else
if (sysconf(_SC_MONOTONIC_CLOCK) > 0)
clock_gettime(CLOCK_MONOTONIC, &ts);
else
clock_gettime(CLOCK_REALTIME, &ts);
#endif

return ((uint64_t)ts.tv_sec * 1000) + ((uint64_t)ts.tv_nsec / 1000000);
Expand Down

0 comments on commit 4e32d57

Please sign in to comment.