changeset 276:e0c28f4e127d

Generators: use the StateHolder fields to track update progress, eliminating the need for A*FU.
author shade
date Mon, 30 May 2016 19:49:07 +0300
parents bec06db416c7
children 4c65422d5a54
files jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/StateHolder.java
diffstat 2 files changed, 23 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java	Mon May 30 18:30:11 2016 +0300
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java	Mon May 30 19:49:07 2016 +0300
@@ -317,8 +317,6 @@
         pw.println("public class " + className + " extends Runner<" + r + "> {");
         pw.println();
 
-        pw.println("    static final AtomicIntegerFieldUpdater<" + className + "> EPOCH = AtomicIntegerFieldUpdater.newUpdater(" + className + ".class, \"epoch\");");
-
         for (ExecutableElement a : info.getActors()) {
             pw.println("    OpenAddressHashCounter<" + r + "> counter_" + a.getSimpleName() + ";");
         }
@@ -447,6 +445,7 @@
         pw.println();
 
         pw.println("    public final void jcstress_updateHolder(StateHolder<Pair> holder) {");
+        pw.println("        if (!holder.tryStartUpdate()) return;");
         pw.println("        Pair[] pairs = holder.pairs;");
         pw.println("        int len = pairs.length;");
         pw.println();
@@ -464,14 +463,13 @@
         pw.println("         }");
         pw.println();
         pw.println("        version = new StateHolder<>(control.isStopped, newPairs, " + actorsCount + ", config.spinLoopStyle);");
-        pw.println("        holder.notConsumed = false;");
+        pw.println("        holder.finishUpdate();");
         pw.println("   }");
 
         int n = 0;
         for (ExecutableElement a : info.getActors()) {
             pw.println();
             pw.println("    public final Void " + a.getSimpleName() + "() {");
-            pw.println("        int curEpoch = 0;");
             pw.println();
             if (!isStateItself) {
                 pw.println("        " + t + " lt = test;");
@@ -514,16 +512,9 @@
             pw.println("            holder.postRun();");
             pw.println();
             pw.println("            jcstress_consume(holder, counter_" + a.getSimpleName() + ", " + n + ", " + actorsCount + ");");
+            pw.println("            jcstress_updateHolder(holder);");
             pw.println();
-            pw.println("            int ticket = EPOCH.incrementAndGet(this);");
-            pw.println("            if (ticket == curEpoch + " + actorsCount + ") {");
-            pw.println("                jcstress_updateHolder(holder);");
-            pw.println("                EPOCH.incrementAndGet(this);");
-            pw.println("            }");
-            pw.println();
-            pw.println("            curEpoch += " + (actorsCount + 1) + ";");
-            pw.println();
-            pw.println("            holder.postConsume();");
+            pw.println("            holder.postUpdate();");
             pw.println("        }");
             pw.println("    }");
             n++;
@@ -862,11 +853,9 @@
     private void printImports(PrintWriter pw, TestInfo info) {
         Class<?>[] imports = new Class<?>[] {
                 ArrayList.class, Arrays.class, Collection.class,
-                Callable.class, ExecutorService.class, Future.class, TimeUnit.class,
-                AtomicIntegerFieldUpdater.class,
+                ExecutorService.class, Future.class, TimeUnit.class,
                 TestConfig.class, TestResultCollector.class,
-                Control.class, Runner.class, StateHolder.class,
-                ArrayUtils.class, Counter.class,
+                Runner.class, StateHolder.class, Counter.class,
                 WhiteBoxSupport.class, OpenAddressHashCounter.class, ExecutionException.class
         };
 
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/StateHolder.java	Mon May 30 18:30:11 2016 +0300
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/StateHolder.java	Mon May 30 19:49:07 2016 +0300
@@ -24,6 +24,7 @@
  */
 package org.openjdk.jcstress.infra.runners;
 
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 
 /**
@@ -34,8 +35,8 @@
     public final P[] pairs;
     public final int countWorkers;
     public final SpinLoopStyle spinStyle;
-    public final AtomicInteger started, ready, finished;
-    public volatile boolean notAllStarted, notAllReady, notAllFinished, notConsumed;
+    public final AtomicInteger started, ready, finished, consumed;
+    public volatile boolean notAllStarted, notAllReady, notAllFinished, notUpdated;
     public volatile boolean hasLaggedWorkers;
 
     public StateHolder(boolean stopped, P[] pairs, int expectedWorkers, SpinLoopStyle spinStyle) {
@@ -46,10 +47,11 @@
         this.ready = new AtomicInteger(expectedWorkers);
         this.started = new AtomicInteger(expectedWorkers);
         this.finished = new AtomicInteger(expectedWorkers);
+        this.consumed = new AtomicInteger(expectedWorkers);
         this.notAllReady = true;
         this.notAllFinished = true;
         this.notAllStarted = true;
-        this.notConsumed = true;
+        this.notUpdated = true;
     }
 
     public void preRun() {
@@ -92,18 +94,25 @@
         }
     }
 
-    public void postConsume() {
+    public boolean tryStartUpdate()  {
+        return (consumed.decrementAndGet() == 0);
+    }
+
+    public void finishUpdate() {
+        notUpdated = false;
+    }
+
+    public void postUpdate() {
         switch (spinStyle) {
             case THREAD_YIELD:
-                while (notConsumed) Thread.yield();
+                while (notUpdated) Thread.yield();
                 break;
             case THREAD_SPIN_WAIT:
-                while (notConsumed) Thread.onSpinWait();
+                while (notUpdated) Thread.onSpinWait();
                 break;
             default:
-                while (notConsumed);
+                while (notUpdated);
         }
-
     }
 
 }