changeset 234:e7be2971a45d

Replace blocking synchronized waits to busy waits on atomics.
author shade
date Sat, 09 Nov 2013 10:26:22 +0100
parents 776ad81a60ca
children 303e6fb2e9ab
files jmh-core/src/main/java/org/openjdk/jmh/processor/internal/StateObjectHandler.java
diffstat 1 files changed, 21 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/StateObjectHandler.java	Wed Nov 06 20:58:29 2013 +0100
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/StateObjectHandler.java	Sat Nov 09 10:26:22 2013 +0100
@@ -256,32 +256,38 @@
             if (!hasHelpers.contains(so)) continue;
 
             if (type == HelperType.SETUP) {
-                result.add("if (!" + so.localIdentifier + ".ready" + helperLevel + ") {");
-                result.add("    synchronized(" + so.localIdentifier + ") {");
-                result.add("        if (!" + so.localIdentifier + ".ready" + helperLevel + ") {");
+                result.add("while(!" + so.localIdentifier + ".setup" + helperLevel + "Mutex.compareAndSet(0, 1)) {");
+                result.add("    if (Thread.interrupted()) throw new InterruptedException();");
+                result.add("}");
+                result.add("try {");
+                result.add("    if (!" + so.localIdentifier + ".ready" + helperLevel + ") {");
                 for (HelperMethodInvocation mi : helpersByState.get(so)) {
                     if (mi.helperLevel == helperLevel && mi.type == HelperType.SETUP) {
                         result.add("        " + so.localIdentifier + "." + mi.name + "();");
                     }
                 }
                 result.add("        " + so.localIdentifier + ".ready" + helperLevel + " = true;");
-                result.add("        }");
                 result.add("    }");
+                result.add("} finally {");
+                result.add("    " + so.localIdentifier + ".setup" + helperLevel + "Mutex.set(0);");
                 result.add("}");
             }
 
             if (type == HelperType.TEARDOWN) {
-                result.add("if (" + so.localIdentifier + ".ready" + helperLevel + ") {");
-                result.add("    synchronized(" + so.localIdentifier + ") {");
-                result.add("        if (" + so.localIdentifier + ".ready" + helperLevel + ") {");
+                result.add("while(!" + so.localIdentifier + ".tear" + helperLevel + "Mutex.compareAndSet(0, 1)) {");
+                result.add("    if (Thread.interrupted()) throw new InterruptedException();");
+                result.add("}");
+                result.add("try {");
+                result.add("    if (" + so.localIdentifier + ".ready" + helperLevel + ") {");
                 for (HelperMethodInvocation mi : helpersByState.get(so)) {
                     if (mi.helperLevel == helperLevel && mi.type == HelperType.TEARDOWN) {
-                        result.add("            " + so.localIdentifier + "." + mi.name + "();");
+                        result.add("        " + so.localIdentifier + "." + mi.name + "();");
                     }
                 }
                 result.add("        " + so.localIdentifier + ".ready" + helperLevel + " = false;");
-                result.add("        }");
                 result.add("    }");
+                result.add("} finally {");
+                result.add("    " + so.localIdentifier + ".tear" + helperLevel + "Mutex.set(0);");
                 result.add("}");
             }
         }
@@ -415,6 +421,12 @@
             }
             result.add("");
 
+            for (Level level : Level.values()) {
+                result.add("    final AtomicInteger setup" + level + "Mutex = new AtomicInteger();");
+                result.add("    final AtomicInteger tear" + level + "Mutex = new AtomicInteger();");
+                result.add("");
+            }
+
             switch (so.scope) {
                 case Benchmark:
                 case Group: