Skip to content

Commit

Permalink
8343039: Remove jdk.internal.misc.InternalLock and usages from java.io
Browse files Browse the repository at this point in the history
Reviewed-by: liach, alanb
  • Loading branch information
Brian Burkhalter committed Nov 15, 2024
1 parent 3c38ed4 commit 0b9b82a
Show file tree
Hide file tree
Showing 19 changed files with 541 additions and 1,782 deletions.
130 changes: 9 additions & 121 deletions src/java.base/share/classes/java/io/BufferedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Arrays;
import java.util.Objects;

import jdk.internal.misc.InternalLock;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.ArraysSupport;

Expand Down Expand Up @@ -74,9 +73,6 @@ public class BufferedInputStream extends FilterInputStream {
private static final long BUF_OFFSET
= U.objectFieldOffset(BufferedInputStream.class, "buf");

// initialized to null when BufferedInputStream is sub-classed
private final InternalLock lock;

// initial buffer size (DEFAULT_BUFFER_SIZE or size specified to constructor)
private final int initialSize;

Expand Down Expand Up @@ -243,20 +239,17 @@ public BufferedInputStream(InputStream in, int size) {
}
initialSize = size;
if (getClass() == BufferedInputStream.class) {
// use internal lock and lazily create buffer when not subclassed
lock = InternalLock.newLockOrNull();
// lazily create buffer when not subclassed
buf = EMPTY;
} else {
// use monitors and eagerly create buffer when subclassed
lock = null;
buf = new byte[size];
}
}

/**
* Fills the buffer with more data, taking into account
* shuffling and other tricks for dealing with marks.
* Assumes that it is being called by a locked method.
* Assumes that it is being called by a synchronized method.
* This method also assumes that all data has already been read in,
* hence pos > count.
*/
Expand Down Expand Up @@ -310,22 +303,7 @@ else if (pos >= buffer.length) { /* no room left in buffer */
* or an I/O error occurs.
* @see java.io.FilterInputStream#in
*/
public int read() throws IOException {
if (lock != null) {
lock.lock();
try {
return implRead();
} finally {
lock.unlock();
}
} else {
synchronized (this) {
return implRead();
}
}
}

private int implRead() throws IOException {
public synchronized int read() throws IOException {
if (pos >= count) {
fill();
if (pos >= count)
Expand Down Expand Up @@ -397,22 +375,7 @@ private int read1(byte[] b, int off, int len) throws IOException {
* or an I/O error occurs.
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public int read(byte[] b, int off, int len) throws IOException {
if (lock != null) {
lock.lock();
try {
return implRead(b, off, len);
} finally {
lock.unlock();
}
} else {
synchronized (this) {
return implRead(b, off, len);
}
}
}

private int implRead(byte[] b, int off, int len) throws IOException {
public synchronized int read(byte[] b, int off, int len) throws IOException {
ensureOpen();
if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
throw new IndexOutOfBoundsException();
Expand Down Expand Up @@ -444,22 +407,7 @@ private int implRead(byte[] b, int off, int len) throws IOException {
* {@code in.skip(n)} throws an IOException,
* or an I/O error occurs.
*/
public long skip(long n) throws IOException {
if (lock != null) {
lock.lock();
try {
return implSkip(n);
} finally {
lock.unlock();
}
} else {
synchronized (this) {
return implSkip(n);
}
}
}

private long implSkip(long n) throws IOException {
public synchronized long skip(long n) throws IOException {
ensureOpen();
if (n <= 0) {
return 0;
Expand Down Expand Up @@ -500,22 +448,7 @@ private long implSkip(long n) throws IOException {
* invoking its {@link #close()} method,
* or an I/O error occurs.
*/
public int available() throws IOException {
if (lock != null) {
lock.lock();
try {
return implAvailable();
} finally {
lock.unlock();
}
} else {
synchronized (this) {
return implAvailable();
}
}
}

private int implAvailable() throws IOException {
public synchronized int available() throws IOException {
int n = count - pos;
int avail = getInIfOpen().available();
return n > (Integer.MAX_VALUE - avail)
Expand All @@ -531,22 +464,7 @@ private int implAvailable() throws IOException {
* the mark position becomes invalid.
* @see java.io.BufferedInputStream#reset()
*/
public void mark(int readlimit) {
if (lock != null) {
lock.lock();
try {
implMark(readlimit);
} finally {
lock.unlock();
}
} else {
synchronized (this) {
implMark(readlimit);
}
}
}

private void implMark(int readlimit) {
public synchronized void mark(int readlimit) {
marklimit = readlimit;
markpos = pos;
}
Expand All @@ -567,22 +485,7 @@ private void implMark(int readlimit) {
* method, or an I/O error occurs.
* @see java.io.BufferedInputStream#mark(int)
*/
public void reset() throws IOException {
if (lock != null) {
lock.lock();
try {
implReset();
} finally {
lock.unlock();
}
} else {
synchronized (this) {
implReset();
}
}
}

private void implReset() throws IOException {
public synchronized void reset() throws IOException {
ensureOpen();
if (markpos < 0)
throw new IOException("Resetting to invalid mark");
Expand Down Expand Up @@ -628,23 +531,8 @@ public void close() throws IOException {
}

@Override
public long transferTo(OutputStream out) throws IOException {
public synchronized long transferTo(OutputStream out) throws IOException {
Objects.requireNonNull(out, "out");
if (lock != null) {
lock.lock();
try {
return implTransferTo(out);
} finally {
lock.unlock();
}
} else {
synchronized (this) {
return implTransferTo(out);
}
}
}

private long implTransferTo(OutputStream out) throws IOException {
if (getClass() == BufferedInputStream.class && markpos == -1) {
int avail = count - pos;
if (avail > 0) {
Expand Down
64 changes: 5 additions & 59 deletions src/java.base/share/classes/java/io/BufferedOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
package java.io;

import java.util.Arrays;
import jdk.internal.misc.InternalLock;
import jdk.internal.misc.VM;

/**
Expand All @@ -47,9 +46,6 @@ public class BufferedOutputStream extends FilterOutputStream {
private static final int DEFAULT_INITIAL_BUFFER_SIZE = 512;
private static final int DEFAULT_MAX_BUFFER_SIZE = 8192;

// initialized to null when BufferedOutputStream is sub-classed
private final InternalLock lock;

/**
* The internal buffer where data is stored.
*/
Expand Down Expand Up @@ -90,12 +86,9 @@ private BufferedOutputStream(OutputStream out, int initialSize, int maxSize) {
}

if (getClass() == BufferedOutputStream.class) {
// use InternalLock and resizable buffer when not sub-classed
this.lock = InternalLock.newLockOrNull();
this.buf = new byte[initialSize]; // resizable
// resizable when not sub-classed
this.buf = new byte[initialSize];
} else {
// use monitors and no resizing when sub-classed
this.lock = null;
this.buf = new byte[maxSize];
}
this.maxBufSize = maxSize;
Expand Down Expand Up @@ -136,8 +129,6 @@ private void flushBuffer() throws IOException {
* Grow buf to fit an additional len bytes if needed.
* If possible, it grows by len+1 to avoid flushing when len bytes
* are added. A no-op if the buffer is not resizable.
*
* This method should only be called while holding the lock.
*/
private void growIfNeeded(int len) {
int neededSize = count + len + 1;
Expand All @@ -157,22 +148,7 @@ private void growIfNeeded(int len) {
* @throws IOException if an I/O error occurs.
*/
@Override
public void write(int b) throws IOException {
if (lock != null) {
lock.lock();
try {
implWrite(b);
} finally {
lock.unlock();
}
} else {
synchronized (this) {
implWrite(b);
}
}
}

private void implWrite(int b) throws IOException {
public synchronized void write(int b) throws IOException {
growIfNeeded(1);
if (count >= buf.length) {
flushBuffer();
Expand All @@ -198,22 +174,7 @@ private void implWrite(int b) throws IOException {
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (lock != null) {
lock.lock();
try {
implWrite(b, off, len);
} finally {
lock.unlock();
}
} else {
synchronized (this) {
implWrite(b, off, len);
}
}
}

private void implWrite(byte[] b, int off, int len) throws IOException {
public synchronized void write(byte[] b, int off, int len) throws IOException {
if (len >= maxBufSize) {
/* If the request length exceeds the max size of the output buffer,
flush the output buffer and then write the data directly.
Expand All @@ -238,22 +199,7 @@ private void implWrite(byte[] b, int off, int len) throws IOException {
* @see java.io.FilterOutputStream#out
*/
@Override
public void flush() throws IOException {
if (lock != null) {
lock.lock();
try {
implFlush();
} finally {
lock.unlock();
}
} else {
synchronized (this) {
implFlush();
}
}
}

private void implFlush() throws IOException {
public synchronized void flush() throws IOException {
flushBuffer();
out.flush();
}
Expand Down
Loading

1 comment on commit 0b9b82a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.