changeset 1029:b79d21dc1789

7901095: Blackhole.consume(Object) methods should use volatile reads
author shade
date Wed, 12 Nov 2014 17:39:25 +0300
parents 66bf33036cda
children 2fd66b6d8f47
files jmh-core/src/main/java/org/openjdk/jmh/infra/Blackhole.java
diffstat 1 files changed, 7 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/infra/Blackhole.java	Fri Nov 07 17:13:16 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/infra/Blackhole.java	Wed Nov 12 17:39:25 2014 +0300
@@ -73,7 +73,7 @@
     public volatile Object[] objs1;
     public volatile BlackholeL2 nullBait = null;
     public int tlr;
-    public int tlrMask;
+    public volatile int tlrMask;
 
     public BlackholeL2() {
         Random r = new Random(System.nanoTime());
@@ -208,6 +208,9 @@
      * generating the slow path, and apply the previous logic to constant-fold
      * the condition to "false". We are warming up the slow-path in the beginning
      * to evade that effect.
+     * <p/>
+     * In all cases, consumes do the volatile reads to have a consistent memory
+     * semantics across all consume methods.
      */
 
     static {
@@ -271,11 +274,12 @@
      * @param obj object to consume.
      */
     public final void consume(Object obj) {
+        int tlrMask = this.tlrMask; // volatile read
         int tlr = (this.tlr = (this.tlr * 1664525 + 1013904223));
         if ((tlr & tlrMask) == 0) {
             // SHOULD ALMOST NEVER HAPPEN IN MEASUREMENT
             this.obj1 = obj;
-            this.tlrMask = (this.tlrMask << 1) + 1;
+            this.tlrMask = (tlrMask << 1) + 1;
         }
     }
 
@@ -285,6 +289,7 @@
      * @param objs objects to consume.
      */
     public final void consume(Object[] objs) {
+        int tlrMask = this.tlrMask; // volatile read
         int tlr = (this.tlr = (this.tlr * 1664525 + 1013904223));
         if ((tlr & tlrMask) == 0) {
             // SHOULD ALMOST NEVER HAPPEN IN MEASUREMENT