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