hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1574553 - /httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/AbstractExecutionAwareRequest.java
Date Wed, 05 Mar 2014 16:05:43 GMT
Author: olegk
Date: Wed Mar  5 16:05:43 2014
New Revision: 1574553

URL: http://svn.apache.org/r1574553
Log:
Use atomic variables instead of a lock

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/AbstractExecutionAwareRequest.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/AbstractExecutionAwareRequest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/AbstractExecutionAwareRequest.java?rev=1574553&r1=1574552&r2=1574553&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/AbstractExecutionAwareRequest.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/methods/AbstractExecutionAwareRequest.java
Wed Mar  5 16:05:43 2014
@@ -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,87 +41,60 @@ import org.apache.http.message.AbstractH
 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() {
-
-                @Override
-                public boolean cancel() {
-                    connRequest.abortRequest();
-                    return true;
-                }
+        setCancellable(new Cancellable() {
 
-            };
-        } finally {
-            this.abortLock.unlock();
-        }
+            @Override
+            public boolean cancel() {
+                connRequest.abortRequest();
+                return true;
+            }
+
+        });
     }
 
     @Override
     @Deprecated
     public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) {
-        if (this.aborted) {
-            return;
-        }
-        this.abortLock.lock();
-        try {
-            this.cancellable = new Cancellable() {
-
-                @Override
-                public boolean cancel() {
-                    try {
-                        releaseTrigger.abortConnection();
-                        return true;
-                    } catch (final IOException ex) {
-                        return false;
-                    }
-                }
+        setCancellable(new Cancellable() {
 
-            };
-        } finally {
-            this.abortLock.unlock();
-        }
-    }
+            @Override
+            public boolean cancel() {
+                try {
+                    releaseTrigger.abortConnection();
+                    return true;
+                } catch (final IOException ex) {
+                    return false;
+                }
+            }
 
-    private void cancelExecution() {
-        if (this.cancellable != null) {
-            this.cancellable.cancel();
-            this.cancellable = null;
-        }
+        });
     }
 
     @Override
     public void abort() {
-        if (this.aborted) {
-            return;
-        }
-        this.abortLock.lock();
-        try {
-            this.aborted = true;
-            cancelExecution();
-        } finally {
-            this.abortLock.unlock();
+        if (this.aborted.compareAndSet(false, true)) {
+            final Cancellable cancellable = this.cancellableRef.getAndSet(null);
+            if (cancellable != null) {
+                cancellable.cancel();
+            }
         }
     }
 
     @Override
     public boolean isAborted() {
-        return this.aborted;
+        return this.aborted.get();
     }
 
     /**
@@ -129,14 +102,8 @@ public abstract class AbstractExecutionA
      */
     @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 AbstractExecutionA
         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 AbstractExecutionA
      * @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 AbstractExecutionA
      * @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);
     }
 
 }



Mime
View raw message