Skip to content

Commit

Permalink
Make memory fence in ByteBufferGuard explicit (#12290)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvisCraft authored and uschindler committed Jun 1, 2023
1 parent 327997b commit 204acc3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,9 +49,6 @@ static interface BufferCleaner {
/** 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.
Expand All @@ -69,10 +66,9 @@ public void invalidateAndUnmap(ByteBuffer... bufs) throws IOException {
// 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:
Expand Down

0 comments on commit 204acc3

Please sign in to comment.