BUG-1120: expose AbstractRegistration state 68/7568/2
authorRobert Varga <rovarga@cisco.com>
Sun, 1 Jun 2014 13:31:26 +0000 (15:31 +0200)
committerRobert Varga <rovarga@cisco.com>
Sun, 1 Jun 2014 17:41:35 +0000 (19:41 +0200)
This patch allows subclasses to infer whether the registration has been
closed.  At the same time we optimize the memory overhead associated with
each instance by not using an AtomicBoolean, but rather a componation of
a volatile int and an AtomicIntegerFieldUpdater.

Change-Id: I672216939f002e42dddef8e6fea73ef8cd109788
Signed-off-by: Robert Varga <rovarga@cisco.com>
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractRegistration.java
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java

index 50f19fc9e3ef596c56754909889b3d308a66e673..1a2c7f32f824cb9732cdaaa4068bd78378e5ac20 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.yangtools.concepts;
 
-import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
 
 /**
  * Utility registration handle. It is a convenience for register-style method
@@ -16,8 +16,10 @@ import java.util.concurrent.atomic.AtomicBoolean;
  * installed.
  */
 public abstract class AbstractRegistration implements AutoCloseable {
-    private AtomicBoolean closed = new AtomicBoolean();
-    
+    private static final AtomicIntegerFieldUpdater<AbstractRegistration> CLOSED_UPDATER =
+            AtomicIntegerFieldUpdater.newUpdater(AbstractRegistration.class, "closed");
+    private volatile int closed = 0;
+
     /**
      * Remove the state referenced by this registration. This method is
      * guaranteed to be called at most once. The referenced state must be
@@ -25,9 +27,19 @@ public abstract class AbstractRegistration implements AutoCloseable {
      */
     protected abstract void removeRegistration();
 
+    /**
+     * Query the state of this registration. Returns true if it was
+     * closed.
+     *
+     * @return true if the registration was closed, false otherwise.
+     */
+    protected final boolean isClosed() {
+        return CLOSED_UPDATER.get(this) != 0;
+    }
+
     @Override
     public final void close() {
-        if (closed.compareAndSet(false, true)) {
+        if (CLOSED_UPDATER.compareAndSet(this, 0, 1)) {
             removeRegistration();
         }
     }
index 8fe2d8ff8b1bfce523077599f315e5038196f54e..a798f2ef6d0053707bc0693dd4b4f5cd467fcd96 100644 (file)
@@ -18,7 +18,7 @@ public interface ListenerRegistration<T extends EventListener> extends ObjectReg
     /**
      * Unregister the listener. No events should be delivered to the listener
      * once this method returns successfully. While the interface contract
-     * allows an implementation to ignore the occurence of RuntimeExceptions,
+     * allows an implementation to ignore the occurrence of RuntimeExceptions,
      * implementations are strongly encouraged to deal with such exceptions
      * internally and to ensure invocations of this method do not fail in such
      * circumstances.