From 093b506c9a570c4c90b8f9f86b48a55ccaa2d4a8 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 29 Apr 2019 12:40:20 +0300 Subject: [PATCH] Improve resolution of System.nanoTime --- .../java/org/teavm/classlib/java/lang/TSystem.java | 10 +++++++++- core/src/main/resources/org/teavm/backend/c/runtime.c | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java index 820140a30..beb54c301 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java @@ -28,6 +28,7 @@ import org.teavm.interop.DelegateTo; import org.teavm.interop.Import; import org.teavm.interop.NoSideEffects; import org.teavm.interop.Unmanaged; +import org.teavm.jso.browser.Performance; import org.teavm.runtime.Allocator; import org.teavm.runtime.GC; import org.teavm.runtime.RuntimeArray; @@ -222,9 +223,16 @@ public final class TSystem extends TObject { } public static long nanoTime() { - return currentTimeMillis() * 1000000; + if (PlatformDetector.isLowLevel()) { + return nanoTimeLowLevel(); + } else { + return (long) (Performance.now() * 1000000); + } } + @Import(name = "currentTimeNano") + private static native long nanoTimeLowLevel(); + public static int identityHashCode(Object x) { return ((TObject) x).identity(); } diff --git a/core/src/main/resources/org/teavm/backend/c/runtime.c b/core/src/main/resources/org/teavm/backend/c/runtime.c index db0e9606e..a4b9295d5 100644 --- a/core/src/main/resources/org/teavm/backend/c/runtime.c +++ b/core/src/main/resources/org/teavm/backend/c/runtime.c @@ -232,7 +232,13 @@ static int64_t currentTimeMillis() { struct timespec time; clock_gettime(CLOCK_REALTIME, &time); - return time.tv_sec * 1000 + (int64_t) round(time.tv_nsec / 1000000); + return time.tv_sec * INT64_C(1000) + (int64_t) round(time.tv_nsec / 1000000); +} +static int64_t currentTimeNano() { + struct timespec time; + clock_gettime(CLOCK_REALTIME, &time); + + return time.tv_sec * INT64_C(1000000000) + (int64_t) round(time.tv_nsec); } #endif