diff options
author | Ben Straub <bs@github.com> | 2013-10-03 06:20:20 -0700 |
---|---|---|
committer | Ben Straub <bs@github.com> | 2013-10-03 06:20:20 -0700 |
commit | fc1f7d4f15ecc3f42dbde7fc0a30933cb89152bc (patch) | |
tree | 1446cb018eb05571b4eff101ceae937688e83a1d /src/util.h | |
parent | de8fe729efd350ae5cb1ef25ffb08f92c6f706b9 (diff) | |
parent | ab1368766240cfe861729642abe1cddd01c0203e (diff) | |
download | libgit2-fc1f7d4f15ecc3f42dbde7fc0a30933cb89152bc.tar.gz |
Merge branch 'development' into blame
Conflicts:
include/git2.h
Diffstat (limited to 'src/util.h')
-rw-r--r-- | src/util.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h index bd93b46b5..f9de909e9 100644 --- a/src/util.h +++ b/src/util.h @@ -353,4 +353,65 @@ GIT_INLINE(void) git__memzero(void *data, size_t size) #endif } +#ifdef GIT_WIN32 + +GIT_INLINE(double) git__timer(void) +{ + /* We need the initial tick count to detect if the tick + * count has rolled over. */ + static DWORD initial_tick_count = 0; + + /* GetTickCount returns the number of milliseconds that have + * elapsed since the system was started. */ + DWORD count = GetTickCount(); + + if(initial_tick_count == 0) { + initial_tick_count = count; + } else if (count < initial_tick_count) { + /* The tick count has rolled over - adjust for it. */ + count = (0xFFFFFFFF - initial_tick_count) + count; + } + + return (double) count / (double) 1000; +} + +#elif __APPLE__ + +#include <mach/mach_time.h> + +GIT_INLINE(double) git__timer(void) +{ + uint64_t time = mach_absolute_time(); + static double scaling_factor = 0; + + if (scaling_factor == 0) { + mach_timebase_info_data_t info; + (void)mach_timebase_info(&info); + scaling_factor = (double)info.numer / (double)info.denom; + } + + return (double)time * scaling_factor / 1.0E-9; +} + +#else + +#include <sys/time.h> + +GIT_INLINE(double) git__timer(void) +{ + struct timespec tp; + + if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) { + return (double) tp.tv_sec + (double) tp.tv_nsec / 1E-9; + } else { + /* Fall back to using gettimeofday */ + struct timeval tv; + struct timezone tz; + gettimeofday(&tv, &tz); + return (double)tv.tv_sec + (double)tv.tv_usec / 1E-6; + } +} + +#endif + #endif /* INCLUDE_util_h__ */ |