Improvements to stopwatch implementation
diff --git a/CMakeLists.txt b/CMakeLists.txt index a1c9e42..68077a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -55,8 +55,9 @@ IF(WIN32) TARGET_LINK_LIBRARIES(${target} ws2_32) ELSEIF(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") - TARGET_LINK_LIBRARIES(${target} socket nsl rt) - ELSE() + TARGET_LINK_LIBRARIES(${target} socket nsl) + ENDIF() + IF(NOT APPLE AND NOT WIN32) TARGET_LINK_LIBRARIES(${target} rt) ENDIF() IF(THREADSAFE)
diff --git a/src/timing.c b/src/timing.c index cf729ea..3125678 100644 --- a/src/timing.c +++ b/src/timing.c
@@ -99,8 +99,14 @@ int git_stopwatch_start(git_stopwatch *s) { - if (clock_gettime(CLOCK_MONOTONIC, &s->start)) { - giterr_set(GITERR_OS, "Unable to get the monotonic timer"); + s->clk_id = GIT_PREFERRED_CLOCK; + + /* On EINVAL for the preferred clock, fall back to CLOCK_REALTIME */ + if (clock_gettime(s->clk_id, &s->start) && + (CLOCK_REALTIME == GIT_PREFERRED_CLOCK || errno != EINVAL || + clock_gettime(s->clk_id = CLOCK_REALTIME, &s->start))) { + giterr_set(GITERR_OS, "Unable to read the clock"); + s->running = 0; return -1; } @@ -119,8 +125,8 @@ return -1; } - if (clock_gettime(CLOCK_MONOTONIC, &end)) { - giterr_set(GITERR_OS, "Unable to get the monotonic timer"); + if (clock_gettime(s->clk_id, &end)) { + giterr_set(GITERR_OS, "Unable to read the clock"); return -1; } @@ -145,4 +151,4 @@ return 0; } -#endif \ No newline at end of file +#endif
diff --git a/src/timing.h b/src/timing.h index 6ee631a..3dc18f4 100644 --- a/src/timing.h +++ b/src/timing.h
@@ -35,8 +35,17 @@ #else +/* On systems which support CLOCK_MONOTONIC, prefer it + * over CLOCK_REALTIME */ +#ifdef CLOCK_MONOTONIC +# define GIT_PREFERRED_CLOCK CLOCK_MONOTONIC +#else +# define GIT_PREFERRED_CLOCK CLOCK_REALTIME +#endif + typedef struct git_stopwatch { struct timespec start; + clockid_t clk_id; unsigned running : 1; } git_stopwatch;