From a880fe871dc76806c4547c9b86d3c3f22fea6654 Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Tue, 28 Apr 2015 22:32:59 -0400 Subject: [PATCH] Remove dependency on MonoTime.ticksPerSecond as it's not always ready. --- src/gc/gc.d | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/gc/gc.d b/src/gc/gc.d index 446246b52d7..8a2229600e8 100644 --- a/src/gc/gc.d +++ b/src/gc/gc.d @@ -61,7 +61,35 @@ else import core.stdc.stdio : sprintf, printf; // needed to ou import core.time; alias currTime = MonoTime.currTime; -long currTicks() { return MonoTime.ticksPerSecond ? MonoTime.currTime.ticks : 0; } +debug(PROFILE_API) +{ + long currTicks() nothrow @nogc + { + // MonoTime.ticksPerSecond may not be ready yet. So just avoid it altogether + // this is copied from core.time.MonoTime.currTime + //return MonoTime.ticksPerSecond ? MonoTime.currTime.ticks : 0; + version(Windows) + { + long ticks; + if(QueryPerformanceCounter(&ticks) == 0) + { + // This probably cannot happen on Windows 95 or later + assert(0, "Call to QueryPerformanceCounter failed."); + } + return ticks; + } + else version(OSX) + return mach_absolute_time(); + else version(Posix) + { + timespec ts; + if(clock_gettime(CLOCK_MONOTONIC, &ts) != 0) + assert(0, "Call to clock_gettime failed."); + + return ts.tv_sec * 1_000_000_000L + ts.tv_nsec; + } + } +} debug(PRINTF_TO_FILE) {