summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/timing.c16
-rw-r--r--src/timing.h9
2 files changed, 20 insertions, 5 deletions
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;