You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2023/06/01 11:43:59 UTC

[lucene] branch branch_9x updated: Make memory fence in `ByteBufferGuard` explicit (#12290)

This is an automated email from the ASF dual-hosted git repository.

uschindler pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 204acc3570b Make memory fence in `ByteBufferGuard` explicit (#12290)
204acc3570b is described below

commit 204acc3570bcbe81af8509ad2b1f8d104e03b8f4
Author: Petr Portnov | PROgrm_JARvis <pp...@ozon.ru>
AuthorDate: Thu Jun 1 14:41:06 2023 +0300

    Make memory fence in `ByteBufferGuard` explicit (#12290)
---
 lucene/CHANGES.txt                                           |  2 ++
 .../src/java/org/apache/lucene/store/ByteBufferGuard.java    | 12 ++++--------
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 3c31d772a69..1c69846f150 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -47,6 +47,8 @@ Improvements
 * GITHUB#12333: NumericLeafComparator#competitiveIterator makes better use of a "search after" value when paginating.
   (Chaitanya Gohel)
 
+* GITHUB#12290: Make memory fence in ByteBufferGuard explicit using `VarHandle.fullFence()`
+
 Optimizations
 ---------------------
 
diff --git a/lucene/core/src/java/org/apache/lucene/store/ByteBufferGuard.java b/lucene/core/src/java/org/apache/lucene/store/ByteBufferGuard.java
index 2d75597f9de..a9e65ffa06c 100644
--- a/lucene/core/src/java/org/apache/lucene/store/ByteBufferGuard.java
+++ b/lucene/core/src/java/org/apache/lucene/store/ByteBufferGuard.java
@@ -17,11 +17,11 @@
 package org.apache.lucene.store;
 
 import java.io.IOException;
+import java.lang.invoke.VarHandle;
 import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
 import java.nio.IntBuffer;
 import java.nio.LongBuffer;
-import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * A guard that is created for every {@link ByteBufferIndexInput} that tries on best effort to
@@ -49,9 +49,6 @@ final class ByteBufferGuard {
   /** Not volatile; see comments on visibility below! */
   private boolean invalidated = false;
 
-  /** Used as a store-store barrier; see comments below! */
-  private final AtomicInteger barrier = new AtomicInteger();
-
   /**
    * Creates an instance to be used for a single {@link ByteBufferIndexInput} which must be shared
    * by all of its clones.
@@ -69,10 +66,9 @@ final class ByteBufferGuard {
       // the "invalidated" field update visible to other threads. We specifically
       // don't make "invalidated" field volatile for performance reasons, hoping the
       // JVM won't optimize away reads of that field and hardware should ensure
-      // caches are in sync after this call. This isn't entirely "fool-proof"
-      // (see LUCENE-7409 discussion), but it has been shown to work in practice
-      // and we count on this behavior.
-      barrier.lazySet(0);
+      // caches are in sync after this call.
+      // For previous implementation (based on `AtomicInteger#lazySet(0)`) see LUCENE-7409.
+      VarHandle.fullFence();
       // we give other threads a bit of time to finish reads on their ByteBuffer...:
       Thread.yield();
       // finally unmap the ByteBuffers: