Improve resolution of System.nanoTime

This commit is contained in:
Alexey Andreev 2019-04-29 12:40:20 +03:00
parent 1b23c9194b
commit 093b506c9a
2 changed files with 16 additions and 2 deletions

View File

@ -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,8 +223,15 @@ 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();

View File

@ -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