8316337: (bf) Concurrency issue in DirectByteBuffer.Deallocator

Reviewed-by: alanb, liach
This commit is contained in:
Per Minborg 2023-09-19 13:10:51 +00:00
parent 4461eeb31d
commit cf74b8c2a3
3 changed files with 31 additions and 45 deletions

View file

@ -75,31 +75,15 @@ class Direct$Type$Buffer$RW$$BO$
#if[byte]
private static class Deallocator
implements Runnable
{
private long address;
private long size;
private int capacity;
private Deallocator(long address, long size, int capacity) {
assert (address != 0);
this.address = address;
this.size = size;
this.capacity = capacity;
private record Deallocator(long address, long size, int capacity) implements Runnable {
private Deallocator {
assert address != 0;
}
public void run() {
if (address == 0) {
// Paranoia
return;
}
UNSAFE.freeMemory(address);
address = 0;
Bits.unreserveMemory(size, capacity);
}
}
private final Cleaner cleaner;

View file

@ -116,28 +116,33 @@ public abstract sealed class MappedByteBuffer
}
UnmapperProxy unmapper() {
return fd != null ?
new UnmapperProxy() {
@Override
public long address() {
return address;
}
return fd == null
? null
: new UnmapperProxy() {
@Override
public FileDescriptor fileDescriptor() {
return fd;
}
// Ensure safe publication as MappedByteBuffer.this.address is not final
private final long addr = address;
@Override
public boolean isSync() {
return isSync;
}
@Override
public long address() {
return addr;
}
@Override
public void unmap() {
Unsafe.getUnsafe().invokeCleaner(MappedByteBuffer.this);
}
} : null;
@Override
public FileDescriptor fileDescriptor() {
return fd;
}
@Override
public boolean isSync() {
return isSync;
}
@Override
public void unmap() {
Unsafe.getUnsafe().invokeCleaner(MappedByteBuffer.this);
}
};
}
/**