*/
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
* 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
*/
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();
}
}
/**
* 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.