Skip to content

Commit

Permalink
Do not reuse buffers as locks
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed Sep 11, 2014
1 parent 639d2bd commit 2bdbbf2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
23 changes: 13 additions & 10 deletions api/src/main/java/org/xnio/channels/FramedMessageChannel.java
Expand Up @@ -46,6 +46,9 @@ public class FramedMessageChannel extends TranslatingSuspendableChannel<Connecte
private final Pooled<ByteBuffer> receiveBuffer;
private final Pooled<ByteBuffer> transmitBuffer;

private final Object readLock = new Object();
private final Object writeLock = new Object();

/**
* Construct a new instance.
*
Expand Down Expand Up @@ -76,7 +79,7 @@ public FramedMessageChannel(final ConnectedStreamChannel channel, final Pooled<B

/** {@inheritDoc} */
public int receive(final ByteBuffer buffer) throws IOException {
synchronized (receiveBuffer) {
synchronized (readLock) {
if (isReadShutDown()) {
return -1;
}
Expand Down Expand Up @@ -142,7 +145,7 @@ public long receive(final ByteBuffer[] buffers) throws IOException {

/** {@inheritDoc} */
public long receive(final ByteBuffer[] buffers, final int offs, final int len) throws IOException {
synchronized (receiveBuffer) {
synchronized (readLock) {
if (isReadShutDown()) {
return -1;
}
Expand Down Expand Up @@ -201,7 +204,7 @@ public long receive(final ByteBuffer[] buffers, final int offs, final int len) t
}

protected void shutdownReadsAction(final boolean writeComplete) throws IOException {
synchronized (receiveBuffer) {
synchronized (readLock) {
log.tracef("Shutting down reads on %s", this);
try {
receiveBuffer.getResource().clear();
Expand All @@ -217,7 +220,7 @@ protected void shutdownReadsAction(final boolean writeComplete) throws IOExcepti

/** {@inheritDoc} */
public boolean send(final ByteBuffer buffer) throws IOException {
synchronized (transmitBuffer) {
synchronized (writeLock) {
if (isWriteShutDown()) {
throw new EOFException("Writes have been shut down");
}
Expand Down Expand Up @@ -249,7 +252,7 @@ public boolean send(final ByteBuffer[] buffers) throws IOException {

/** {@inheritDoc} */
public boolean send(final ByteBuffer[] buffers, final int offs, final int len) throws IOException {
synchronized (transmitBuffer) {
synchronized (writeLock) {
if (isWriteShutDown()) {
throw new EOFException("Writes have been shut down");
}
Expand All @@ -275,13 +278,13 @@ public boolean send(final ByteBuffer[] buffers, final int offs, final int len) t
}

protected boolean flushAction(final boolean shutDown) throws IOException {
synchronized (transmitBuffer) {
synchronized (writeLock) {
return (doFlushBuffer()) && channel.flush();
}
}

protected void shutdownWritesComplete(final boolean readShutDown) throws IOException {
synchronized (transmitBuffer) {
synchronized (writeLock) {
log.tracef("Finished shutting down writes on %s", this);
try {
transmitBuffer.free();
Expand All @@ -291,7 +294,7 @@ protected void shutdownWritesComplete(final boolean readShutDown) throws IOExcep
}

private boolean doFlushBuffer() throws IOException {
assert Thread.holdsLock(transmitBuffer);
assert Thread.holdsLock(writeLock);
final ByteBuffer buffer = transmitBuffer.getResource();
buffer.flip();
try {
Expand All @@ -316,7 +319,7 @@ private boolean doFlush() throws IOException {
protected void closeAction(final boolean readShutDown, final boolean writeShutDown) throws IOException {
boolean error = false;
if (! writeShutDown) {
synchronized (transmitBuffer) {
synchronized (writeLock) {
try {
if (! doFlush()) error = true;
} catch (Throwable t) {
Expand All @@ -329,7 +332,7 @@ protected void closeAction(final boolean readShutDown, final boolean writeShutDo
}
}
if (! readShutDown) {
synchronized (receiveBuffer) {
synchronized (readLock) {
try {
receiveBuffer.free();
} catch (Throwable t) {
Expand Down
Expand Up @@ -72,6 +72,9 @@ final class JsseConnectedSslStreamChannel extends TranslatingSuspendableChannel<
/** The buffer into which inbound clear data is written. */
private final Pooled<ByteBuffer> readBuffer;

private final Object readLock = new Object();
private final Object writeLock = new Object();

// state

private volatile boolean tls;
Expand Down Expand Up @@ -560,11 +563,11 @@ public SSLSession getSslSession() {
}

protected Object getReadLock() {
return receiveBuffer;
return readLock;
}

protected Object getWriteLock() {
return sendBuffer;
return writeLock;
}

protected void shutdownReadsAction(final boolean writeComplete) throws IOException {
Expand Down

0 comments on commit 2bdbbf2

Please sign in to comment.