Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make memory fence in ByteBufferGuard explicit #12290

Merged
merged 5 commits into from
Jun 1, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions lucene/core/src/java/org/apache/lucene/store/ByteBufferGuard.java
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 @@ -65,14 +62,8 @@ public ByteBufferGuard(String resourceDescription, BufferCleaner cleaner) {
public void invalidateAndUnmap(ByteBuffer... bufs) throws IOException {
if (cleaner != null) {
invalidated = true;
// This call should hopefully flush any CPU caches and as a result make
// 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);
// Makes "invalidated" field visible to other threads.
VarHandle.fullFence();
uschindler marked this conversation as resolved.
Show resolved Hide resolved
// we give other threads a bit of time to finish reads on their ByteBuffer...:
Thread.yield();
// finally unmap the ByteBuffers:
Expand Down