Use atomic variables instead of a lock

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1574553 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-03-05 16:05:43 +00:00
parent 406e3aaa15
commit 9ec038bea1
1 changed files with 36 additions and 79 deletions

View File

@ -27,8 +27,8 @@
package org.apache.http.client.methods; package org.apache.http.client.methods;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.locks.Lock; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.atomic.AtomicReference;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.apache.http.client.utils.CloneUtils; import org.apache.http.client.utils.CloneUtils;
@ -41,87 +41,60 @@ import org.apache.http.message.AbstractHttpMessage;
public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage implements public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage implements
HttpExecutionAware, AbortableHttpRequest, Cloneable, HttpRequest { HttpExecutionAware, AbortableHttpRequest, Cloneable, HttpRequest {
private Lock abortLock; private final AtomicBoolean aborted;
private volatile boolean aborted; private final AtomicReference<Cancellable> cancellableRef;
private volatile Cancellable cancellable;
protected AbstractExecutionAwareRequest() { protected AbstractExecutionAwareRequest() {
super(); super();
this.abortLock = new ReentrantLock(); this.aborted = new AtomicBoolean(false);
this.cancellableRef = new AtomicReference<Cancellable>(null);
} }
@Override @Override
@Deprecated @Deprecated
public void setConnectionRequest(final ClientConnectionRequest connRequest) { public void setConnectionRequest(final ClientConnectionRequest connRequest) {
if (this.aborted) { setCancellable(new Cancellable() {
return;
}
this.abortLock.lock();
try {
this.cancellable = new Cancellable() {
@Override @Override
public boolean cancel() { public boolean cancel() {
connRequest.abortRequest(); connRequest.abortRequest();
return true; return true;
} }
}; });
} finally {
this.abortLock.unlock();
}
} }
@Override @Override
@Deprecated @Deprecated
public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) { public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) {
if (this.aborted) { setCancellable(new Cancellable() {
return;
}
this.abortLock.lock();
try {
this.cancellable = new Cancellable() {
@Override @Override
public boolean cancel() { public boolean cancel() {
try { try {
releaseTrigger.abortConnection(); releaseTrigger.abortConnection();
return true; return true;
} catch (final IOException ex) { } catch (final IOException ex) {
return false; return false;
}
} }
}
}; });
} finally {
this.abortLock.unlock();
}
}
private void cancelExecution() {
if (this.cancellable != null) {
this.cancellable.cancel();
this.cancellable = null;
}
} }
@Override @Override
public void abort() { public void abort() {
if (this.aborted) { if (this.aborted.compareAndSet(false, true)) {
return; final Cancellable cancellable = this.cancellableRef.getAndSet(null);
} if (cancellable != null) {
this.abortLock.lock(); cancellable.cancel();
try { }
this.aborted = true;
cancelExecution();
} finally {
this.abortLock.unlock();
} }
} }
@Override @Override
public boolean isAborted() { public boolean isAborted() {
return this.aborted; return this.aborted.get();
} }
/** /**
@ -129,14 +102,8 @@ public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage
*/ */
@Override @Override
public void setCancellable(final Cancellable cancellable) { public void setCancellable(final Cancellable cancellable) {
if (this.aborted) { if (!this.aborted.get()) {
return; this.cancellableRef.set(cancellable);
}
this.abortLock.lock();
try {
this.cancellable = cancellable;
} finally {
this.abortLock.unlock();
} }
} }
@ -145,9 +112,6 @@ public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage
final AbstractExecutionAwareRequest clone = (AbstractExecutionAwareRequest) super.clone(); final AbstractExecutionAwareRequest clone = (AbstractExecutionAwareRequest) super.clone();
clone.headergroup = CloneUtils.cloneObject(this.headergroup); clone.headergroup = CloneUtils.cloneObject(this.headergroup);
clone.params = CloneUtils.cloneObject(this.params); clone.params = CloneUtils.cloneObject(this.params);
clone.abortLock = new ReentrantLock();
clone.cancellable = null;
clone.aborted = false;
return clone; return clone;
} }
@ -155,12 +119,7 @@ public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage
* @since 4.2 * @since 4.2
*/ */
public void completed() { public void completed() {
this.abortLock.lock(); this.cancellableRef.set(null);
try {
this.cancellable = null;
} finally {
this.abortLock.unlock();
}
} }
/** /**
@ -169,13 +128,11 @@ public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage
* @since 4.2 * @since 4.2
*/ */
public void reset() { public void reset() {
this.abortLock.lock(); final Cancellable cancellable = this.cancellableRef.getAndSet(null);
try { if (cancellable != null) {
cancelExecution(); cancellable.cancel();
this.aborted = false;
} finally {
this.abortLock.unlock();
} }
this.aborted.set(false);
} }
} }