From 9f14ad29bf2d652c11328479ccb11030408c0543 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 8 Nov 2011 04:52:40 +0100 Subject: Platform: Add 'getCurrentSleepJitter()' --- src/java/com/jogamp/common/os/Platform.java | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/java/com') diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java index 27d556c..2c1a18e 100644 --- a/src/java/com/jogamp/common/os/Platform.java +++ b/src/java/com/jogamp/common/os/Platform.java @@ -35,6 +35,7 @@ import java.nio.IntBuffer; import java.nio.ShortBuffer; import java.security.AccessController; import java.security.PrivilegedAction; +import java.util.concurrent.TimeUnit; import java.util.jar.JarFile; import com.jogamp.common.nio.Buffers; @@ -560,6 +561,30 @@ public class Platform { */ public static MachineDescription getMachineDescription() { return machineDescription; - } + } + + // + // time / jitter + // + + /** + * Returns the estimated sleep jitter value in nanoseconds. + *

+ * Includes a warm-up path, allowing hotspot to optimize the code. + *

+ */ + public static synchronized long getCurrentSleepJitter() { + getCurrentSleepJitterImpl(TimeUnit.MILLISECONDS.toNanos(10), 10); // warm-up + return getCurrentSleepJitterImpl(TimeUnit.MILLISECONDS.toNanos(10), 10); + } + private static long getCurrentSleepJitterImpl(final long nsDuration, final int splitInLoops) { + final long nsPeriod = nsDuration / splitInLoops; + final long t0_ns = System.nanoTime(); + for(int i=splitInLoops; i>0; i--) { + try { TimeUnit.NANOSECONDS.sleep(nsPeriod); } catch (InterruptedException e) { } + } + return ( ( System.nanoTime() - t0_ns ) - nsDuration ) / splitInLoops; + } + } -- cgit v1.2.3