diff options
author | Philip Kelley <phkelley@hotmail.com> | 2013-09-21 18:25:54 -0400 |
---|---|---|
committer | Philip Kelley <phkelley@hotmail.com> | 2013-09-21 18:25:54 -0400 |
commit | e4987b6ce2db08d87463ef9291151ed6cb4839f2 (patch) | |
tree | 183c45773bade3212e67a4be3c823804d2302a93 | |
parent | 37bf1bc129e10fec17e9d1cd9674f659fb01670f (diff) | |
download | libgit2-stopwatch.tar.gz |
Improvements to stopwatch implementationstopwatch
-rw-r--r-- | CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/timing.c | 16 | ||||
-rw-r--r-- | src/timing.h | 9 |
3 files changed, 23 insertions, 7 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a1c9e4258..68077a951 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,8 +55,9 @@ FUNCTION(TARGET_OS_LIBRARIES target) 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 cf729eab0..3125678e7 100644 --- a/src/timing.c +++ b/src/timing.c @@ -99,8 +99,14 @@ int git_stopwatch_query(double *out_elapsed_seconds, git_stopwatch *s) 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 @@ int git_stopwatch_query(double *out_elapsed_seconds, git_stopwatch *s) 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 @@ int git_stopwatch_query(double *out_elapsed_seconds, git_stopwatch *s) return 0; } -#endif
\ No newline at end of file +#endif diff --git a/src/timing.h b/src/timing.h index 6ee631a01..3dc18f467 100644 --- a/src/timing.h +++ b/src/timing.h @@ -35,8 +35,17 @@ typedef struct git_stopwatch { #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; |