From b994cde2214fa0de99ec4dec26d67557bdc8bc1f Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 22 May 2012 22:42:02 +0200 Subject: [PATCH] Add workaround to let the sleep work correctly in windows too. See #356 --- .../org/jboss/netty/util/HashedWheelTimer.java | 13 +++++++++++-- .../jboss/netty/util/internal/DetectionUtil.java | 16 +++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jboss/netty/util/HashedWheelTimer.java b/src/main/java/org/jboss/netty/util/HashedWheelTimer.java index e1149f1a9c..f9aa9623b4 100644 --- a/src/main/java/org/jboss/netty/util/HashedWheelTimer.java +++ b/src/main/java/org/jboss/netty/util/HashedWheelTimer.java @@ -32,6 +32,7 @@ import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLoggerFactory; import org.jboss.netty.util.internal.ConcurrentIdentityHashMap; +import org.jboss.netty.util.internal.DetectionUtil; import org.jboss.netty.util.internal.ReusableIterator; import org.jboss.netty.util.internal.SharedResourceMisuseDetector; @@ -445,11 +446,19 @@ public class HashedWheelTimer implements Timer { for (;;) { final long currentTime = System.currentTimeMillis(); - final long sleepTime = tickDuration * tick - (currentTime - startTime); - + long sleepTime = tickDuration * tick - (currentTime - startTime); + if (sleepTime <= 0) { break; } + // Check if we run on windows, as if thats the case we will need + // to round the sleepTime as workaround for a bug that only affect + // the JVM if it runs on windows. + // + // See https://github.com/netty/netty/issues/356 + if (DetectionUtil.isWindows()) { + sleepTime = (sleepTime / 10) * 10; + } try { Thread.sleep(sleepTime); diff --git a/src/main/java/org/jboss/netty/util/internal/DetectionUtil.java b/src/main/java/org/jboss/netty/util/internal/DetectionUtil.java index 7f5a98398d..f7d98ec1f0 100644 --- a/src/main/java/org/jboss/netty/util/internal/DetectionUtil.java +++ b/src/main/java/org/jboss/netty/util/internal/DetectionUtil.java @@ -36,7 +36,21 @@ public final class DetectionUtil { private static final int JAVA_VERSION = javaVersion0(); private static final boolean HAS_UNSAFE = hasUnsafe(AtomicInteger.class.getClassLoader()); - + private static final boolean IS_WINDOWS; + static { + String os = System.getProperty("os.name").toLowerCase(); + // windows + IS_WINDOWS = os.indexOf("win") >= 0; + } + + /** + * Return true if the JVM is running on Windows + * + */ + public static boolean isWindows() { + return IS_WINDOWS; + } + public static boolean hasUnsafe() { return HAS_UNSAFE; }