You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2014/11/12 20:16:25 UTC
svn commit: r1638961 -
/httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
Author: olegk
Date: Wed Nov 12 19:16:24 2014
New Revision: 1638961
URL: http://svn.apache.org/r1638961
Log:
IOSessionImpl: use double checks whenever it is possible to reduce synchronized scope
Contributed by Dmitry Potapov <dpotapov at yandex-team.ru>
Modified:
httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
Modified: httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java?rev=1638961&r1=1638960&r2=1638961&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java (original)
+++ httpcomponents/httpcore/branches/4.4.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java Wed Nov 12 19:16:24 2014
@@ -135,7 +135,7 @@ public class IOSessionImpl implements IO
}
@Override
- public synchronized int getEventMask() {
+ public int getEventMask() {
return this.interestOpsCallback != null ? this.currentEventMask : this.key.interestOps();
}
@@ -213,23 +213,27 @@ public class IOSessionImpl implements IO
}
@Override
- public synchronized void close() {
- if (this.status == CLOSED) {
- return;
- }
- this.status = CLOSED;
- this.key.cancel();
- try {
- this.key.channel().close();
- } catch (final IOException ex) {
- // Munching exceptions is not nice
- // but in this case it is justified
- }
- if (this.sessionClosedCallback != null) {
- this.sessionClosedCallback.sessionClosed(this);
- }
- if (this.key.selector().isOpen()) {
- this.key.selector().wakeup();
+ public void close() {
+ synchronized (this) {
+ if (this.status == CLOSED) {
+ return;
+ }
+ this.status = CLOSED;
+ }
+ synchronized (this.key) {
+ this.key.cancel();
+ try {
+ this.key.channel().close();
+ } catch (final IOException ex) {
+ // Munching exceptions is not nice
+ // but in this case it is justified
+ }
+ if (this.sessionClosedCallback != null) {
+ this.sessionClosedCallback.sessionClosed(this);
+ }
+ if (this.key.selector().isOpen()) {
+ this.key.selector().wakeup();
+ }
}
}
@@ -338,36 +342,38 @@ public class IOSessionImpl implements IO
}
@Override
- public synchronized String toString() {
+ public String toString() {
final StringBuilder buffer = new StringBuilder();
- final SocketAddress remoteAddress = getRemoteAddress();
- final SocketAddress localAddress = getLocalAddress();
- if (remoteAddress != null && localAddress != null) {
- formatAddress(buffer, localAddress);
- buffer.append("<->");
- formatAddress(buffer, remoteAddress);
- }
- buffer.append("[");
- switch (this.status) {
- case ACTIVE:
- buffer.append("ACTIVE");
- break;
- case CLOSING:
- buffer.append("CLOSING");
- break;
- case CLOSED:
- buffer.append("CLOSED");
- break;
- }
- buffer.append("][");
- if (this.key.isValid()) {
- formatOps(buffer, this.interestOpsCallback != null ?
- this.currentEventMask : this.key.interestOps());
- buffer.append(":");
- formatOps(buffer, this.key.readyOps());
+ synchronized (this.key) {
+ final SocketAddress remoteAddress = getRemoteAddress();
+ final SocketAddress localAddress = getLocalAddress();
+ if (remoteAddress != null && localAddress != null) {
+ formatAddress(buffer, localAddress);
+ buffer.append("<->");
+ formatAddress(buffer, remoteAddress);
+ }
+ buffer.append('[');
+ switch (this.status) {
+ case ACTIVE:
+ buffer.append("ACTIVE");
+ break;
+ case CLOSING:
+ buffer.append("CLOSING");
+ break;
+ case CLOSED:
+ buffer.append("CLOSED");
+ break;
+ }
+ buffer.append("][");
+ if (this.key.isValid()) {
+ formatOps(buffer, this.interestOpsCallback != null ?
+ this.currentEventMask : this.key.interestOps());
+ buffer.append(':');
+ formatOps(buffer, this.key.readyOps());
+ }
}
- buffer.append("]");
- return buffer.toString();
+ buffer.append(']');
+ return new String(buffer);
}
@Override