Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -1600,13 +1600,13 @@ public int doFinal(ByteBuffer src, ByteBuffer dst)
Arrays.fill(dst.array(), ofs, ofs + len,
(byte) 0);
} else {
NIO_ACCESS.acquireSession(dst);
int ticket = NIO_ACCESS.acquireSession(dst);
try {
Unsafe.getUnsafe().setMemory(
NIO_ACCESS.getBufferAddress(dst),
len + dst.position(), (byte) 0);
} finally {
NIO_ACCESS.releaseSession(dst);
NIO_ACCESS.releaseSession(dst, ticket);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/lang/ClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1075,13 +1075,13 @@ protected final Class<?> defineClass(String name, ByteBuffer b,
private Class<?> defineClass(String name, ByteBuffer b, int len, ProtectionDomain pd) {
pd = preDefineClass(name, pd);
String source = defineClassSourceLocation(pd);
SharedSecrets.getJavaNioAccess().acquireSession(b);
int ticket = SharedSecrets.getJavaNioAccess().acquireSession(b);
try {
Class<?> c = defineClass2(this, name, b, b.position(), len, pd, source);
postDefineClass(c, pd);
return c;
} finally {
SharedSecrets.getJavaNioAccess().releaseSession(b);
SharedSecrets.getJavaNioAccess().releaseSession(b, ticket);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/java.base/share/classes/java/nio/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -878,19 +878,20 @@ public MemorySegment bufferSegment(Buffer buffer) {
}

@Override
public void acquireSession(Buffer buffer) {
public int acquireSession(Buffer buffer) {
var scope = buffer.session();
if (scope != null) {
scope.acquire0();
return scope.acquire0();
}
return 0;
}

@Override
public void releaseSession(Buffer buffer) {
public void releaseSession(Buffer buffer, int ticket) {
try {
var scope = buffer.session();
if (scope != null) {
scope.release0();
scope.release0(ticket);
}
} finally {
Reference.reachabilityFence(buffer);
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/zip/Adler32.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public void update(ByteBuffer buffer) {
if (rem <= 0)
return;
if (buffer.isDirect()) {
NIO_ACCESS.acquireSession(buffer);
int ticket = NIO_ACCESS.acquireSession(buffer);
try {
adler = updateByteBuffer(adler, NIO_ACCESS.getBufferAddress(buffer), pos, rem);
} finally {
NIO_ACCESS.releaseSession(buffer);
NIO_ACCESS.releaseSession(buffer, ticket);
}
} else if (buffer.hasArray()) {
adler = updateBytes(adler, buffer.array(), pos + buffer.arrayOffset(), rem);
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/zip/CRC32.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public void update(ByteBuffer buffer) {
if (rem <= 0)
return;
if (buffer.isDirect()) {
NIO_ACCESS.acquireSession(buffer);
int ticket = NIO_ACCESS.acquireSession(buffer);
try {
crc = updateByteBuffer(crc, NIO_ACCESS.getBufferAddress(buffer), pos, rem);
} finally {
NIO_ACCESS.releaseSession(buffer);
NIO_ACCESS.releaseSession(buffer, ticket);
}
} else if (buffer.hasArray()) {
crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(), rem);
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/zip/CRC32C.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ public void update(ByteBuffer buffer) {
}

if (buffer.isDirect()) {
NIO_ACCESS.acquireSession(buffer);
int ticket = NIO_ACCESS.acquireSession(buffer);
try {
crc = updateDirectByteBuffer(crc, NIO_ACCESS.getBufferAddress(buffer),
pos, limit);
} finally {
NIO_ACCESS.releaseSession(buffer);
NIO_ACCESS.releaseSession(buffer, ticket);
}
} else if (buffer.hasArray()) {
crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(),
Expand Down
24 changes: 12 additions & 12 deletions src/java.base/share/classes/java/util/zip/Deflater.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ public void setDictionary(ByteBuffer dictionary) {
int remaining = Math.max(dictionary.limit() - position, 0);
ensureOpen();
if (dictionary.isDirect()) {
NIO_ACCESS.acquireSession(dictionary);
int ticket = NIO_ACCESS.acquireSession(dictionary);
try {
long address = NIO_ACCESS.getBufferAddress(dictionary);
setDictionaryBuffer(zsRef.address(), address + position, remaining);
} finally {
NIO_ACCESS.releaseSession(dictionary);
NIO_ACCESS.releaseSession(dictionary, ticket);
}
} else {
byte[] array = ZipUtils.getBufferArray(dictionary);
Expand Down Expand Up @@ -574,15 +574,15 @@ public int deflate(byte[] output, int off, int len, int flush) {
inputPos = input.position();
int inputRem = Math.max(input.limit() - inputPos, 0);
if (input.isDirect()) {
NIO_ACCESS.acquireSession(input);
int ticket = NIO_ACCESS.acquireSession(input);
try {
long inputAddress = NIO_ACCESS.getBufferAddress(input);
result = deflateBufferBytes(zsRef.address(),
inputAddress + inputPos, inputRem,
output, off, len,
flush, params);
} finally {
NIO_ACCESS.releaseSession(input);
NIO_ACCESS.releaseSession(input, ticket);
}
} else {
byte[] inputArray = ZipUtils.getBufferArray(input);
Expand Down Expand Up @@ -698,15 +698,15 @@ public int deflate(ByteBuffer output, int flush) {
if (input == null) {
inputPos = this.inputPos;
if (output.isDirect()) {
NIO_ACCESS.acquireSession(output);
int ticket = NIO_ACCESS.acquireSession(output);
try {
long outputAddress = NIO_ACCESS.getBufferAddress(output);
result = deflateBytesBuffer(zsRef.address(),
inputArray, inputPos, inputLim - inputPos,
outputAddress + outputPos, outputRem,
flush, params);
} finally {
NIO_ACCESS.releaseSession(output);
NIO_ACCESS.releaseSession(output, ticket);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand All @@ -720,19 +720,19 @@ public int deflate(ByteBuffer output, int flush) {
inputPos = input.position();
int inputRem = Math.max(input.limit() - inputPos, 0);
if (input.isDirect()) {
NIO_ACCESS.acquireSession(input);
int ticket = NIO_ACCESS.acquireSession(input);
try {
long inputAddress = NIO_ACCESS.getBufferAddress(input);
if (output.isDirect()) {
NIO_ACCESS.acquireSession(output);
int ticket2 = NIO_ACCESS.acquireSession(output);
try {
long outputAddress = outputPos + NIO_ACCESS.getBufferAddress(output);
result = deflateBufferBuffer(zsRef.address(),
inputAddress + inputPos, inputRem,
outputAddress, outputRem,
flush, params);
} finally {
NIO_ACCESS.releaseSession(output);
NIO_ACCESS.releaseSession(output, ticket2);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand All @@ -743,21 +743,21 @@ public int deflate(ByteBuffer output, int flush) {
flush, params);
}
} finally {
NIO_ACCESS.releaseSession(input);
NIO_ACCESS.releaseSession(input, ticket);
}
} else {
byte[] inputArray = ZipUtils.getBufferArray(input);
int inputOffset = ZipUtils.getBufferOffset(input);
if (output.isDirect()) {
NIO_ACCESS.acquireSession(output);
int ticket = NIO_ACCESS.acquireSession(output);
try {
long outputAddress = NIO_ACCESS.getBufferAddress(output);
result = deflateBytesBuffer(zsRef.address(),
inputArray, inputOffset + inputPos, inputRem,
outputAddress + outputPos, outputRem,
flush, params);
} finally {
NIO_ACCESS.releaseSession(output);
NIO_ACCESS.releaseSession(output, ticket);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand Down
24 changes: 12 additions & 12 deletions src/java.base/share/classes/java/util/zip/Inflater.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ public void setDictionary(ByteBuffer dictionary) {
int remaining = Math.max(dictionary.limit() - position, 0);
ensureOpen();
if (dictionary.isDirect()) {
NIO_ACCESS.acquireSession(dictionary);
int ticket = NIO_ACCESS.acquireSession(dictionary);
try {
long address = NIO_ACCESS.getBufferAddress(dictionary);
setDictionaryBuffer(zsRef.address(), address + position, remaining);
} finally {
NIO_ACCESS.releaseSession(dictionary);
NIO_ACCESS.releaseSession(dictionary, ticket);
}
} else {
byte[] array = ZipUtils.getBufferArray(dictionary);
Expand Down Expand Up @@ -363,14 +363,14 @@ public int inflate(byte[] output, int off, int len)
try {
int inputRem = Math.max(input.limit() - inputPos, 0);
if (input.isDirect()) {
NIO_ACCESS.acquireSession(input);
int ticket = NIO_ACCESS.acquireSession(input);
try {
long inputAddress = NIO_ACCESS.getBufferAddress(input);
result = inflateBufferBytes(zsRef.address(),
inputAddress + inputPos, inputRem,
output, off, len);
} finally {
NIO_ACCESS.releaseSession(input);
NIO_ACCESS.releaseSession(input, ticket);
}
} else {
byte[] inputArray = ZipUtils.getBufferArray(input);
Expand Down Expand Up @@ -500,14 +500,14 @@ public int inflate(ByteBuffer output) throws DataFormatException {
inputPos = this.inputPos;
try {
if (output.isDirect()) {
NIO_ACCESS.acquireSession(output);
int ticket = NIO_ACCESS.acquireSession(output);
try {
long outputAddress = NIO_ACCESS.getBufferAddress(output);
result = inflateBytesBuffer(zsRef.address(),
inputArray, inputPos, inputLim - inputPos,
outputAddress + outputPos, outputRem);
} finally {
NIO_ACCESS.releaseSession(output);
NIO_ACCESS.releaseSession(output, ticket);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand All @@ -525,18 +525,18 @@ public int inflate(ByteBuffer output) throws DataFormatException {
int inputRem = Math.max(input.limit() - inputPos, 0);
try {
if (input.isDirect()) {
NIO_ACCESS.acquireSession(input);
int ticket = NIO_ACCESS.acquireSession(input);
try {
long inputAddress = NIO_ACCESS.getBufferAddress(input);
if (output.isDirect()) {
NIO_ACCESS.acquireSession(output);
int ticket2 = NIO_ACCESS.acquireSession(output);
try {
long outputAddress = NIO_ACCESS.getBufferAddress(output);
result = inflateBufferBuffer(zsRef.address(),
inputAddress + inputPos, inputRem,
outputAddress + outputPos, outputRem);
} finally {
NIO_ACCESS.releaseSession(output);
NIO_ACCESS.releaseSession(output, ticket2);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand All @@ -546,20 +546,20 @@ public int inflate(ByteBuffer output) throws DataFormatException {
outputArray, outputOffset + outputPos, outputRem);
}
} finally {
NIO_ACCESS.releaseSession(input);
NIO_ACCESS.releaseSession(input, ticket);
}
} else {
byte[] inputArray = ZipUtils.getBufferArray(input);
int inputOffset = ZipUtils.getBufferOffset(input);
if (output.isDirect()) {
NIO_ACCESS.acquireSession(output);
int ticket = NIO_ACCESS.acquireSession(output);
try {
long outputAddress = NIO_ACCESS.getBufferAddress(output);
result = inflateBytesBuffer(zsRef.address(),
inputArray, inputOffset + inputPos, inputRem,
outputAddress + outputPos, outputRem);
} finally {
NIO_ACCESS.releaseSession(output);
NIO_ACCESS.releaseSession(output, ticket);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ public interface JavaNioAccess {
* Used by operations to make a buffer's session non-closeable
* (for the duration of the operation) by acquiring the session.
* {@snippet lang = java:
* acquireSession(buffer);
* int ticket = acquireSession(buffer);
* try {
* performOperation(buffer);
* } finally {
* releaseSession(buffer);
* releaseSession(buffer, ticket);
* }
*}
*
* @see #releaseSession(Buffer)
* @see #releaseSession(Buffer, int)
*/
void acquireSession(Buffer buffer);
int acquireSession(Buffer buffer);

void releaseSession(Buffer buffer);
void releaseSession(Buffer buffer, int ticket);

boolean isThreadConfined(Buffer buffer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@ public ConfinedSession(Thread owner) {

@Override
@ForceInline
public void acquire0() {
public int acquire0() {
checkValidState();
if (acquireCount == MAX_FORKS) {
throw tooManyAcquires();
}
acquireCount++;
return 0;
}

@Override
@ForceInline
public void release0() {
public void release0(int ticket) {
if (Thread.currentThread() == owner) {
acquireCount--;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ public GlobalSession() {

@Override
@ForceInline
public void release0() {
public void release0(int ticket) {
// do nothing
}

@Override
@ForceInline
public void acquire0() {
public int acquire0() {
// do nothing
return 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,23 @@
* {@link DirectBuffer#address()}, where obtaining an address of a buffer instance associated
* with a potentially closeable session is forbidden.
*/
final class ImplicitSession extends SharedSession {
final class ImplicitSession extends MemorySessionImpl {

public ImplicitSession(Cleaner cleaner) {
super();
super(null, new SharedResourceList());
this.state = NONCLOSEABLE;
cleaner.register(this, resourceList);
}

@Override
public void release0() {
public void release0(int ticket) {
Reference.reachabilityFence(this);
}

@Override
public void acquire0() {
public int acquire0() {
// do nothing
return 0;
}

@Override
Expand Down
Loading