X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fcds-access-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Faccess%2Fclient%2FInversibleLock.java;fp=opendaylight%2Fmd-sal%2Fcds-access-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Faccess%2Fclient%2FInversibleLock.java;h=7e684453969e3702af4b5027730b51ed946ab2f7;hp=08bc05346b65d1ce53faa7d3e64e8f38fc6a5359;hb=139f7a412c8d96fa9d03113439d622c30218c7ce;hpb=a2aa9ed45fe099f6d3611fc22338bfcaebd6d084 diff --git a/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/InversibleLock.java b/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/InversibleLock.java index 08bc05346b..7e68445396 100644 --- a/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/InversibleLock.java +++ b/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/InversibleLock.java @@ -7,25 +7,32 @@ */ package org.opendaylight.controller.cluster.access.client; -import com.google.common.annotations.Beta; -import com.google.common.base.Verify; +import static com.google.common.base.Verify.verify; +import static com.google.common.base.Verify.verifyNotNull; + +import java.lang.invoke.MethodHandles; +import java.lang.invoke.VarHandle; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; import java.util.concurrent.locks.StampedLock; /** * A lock implementation which allows users to perform optimistic reads and validate them in a fashion similar * to {@link StampedLock}. In case a read is contented with a write, the read side will throw * an {@link InversibleLockException}, which the caller can catch and use to wait for the write to resolve. - * - * @author Robert Varga */ -@Beta public final class InversibleLock { - private static final AtomicReferenceFieldUpdater LATCH_UPDATER = - AtomicReferenceFieldUpdater.newUpdater(InversibleLock.class, CountDownLatch.class, "latch"); + private static final VarHandle LATCH; + + static { + try { + LATCH = MethodHandles.lookup().findVarHandle(InversibleLock.class, "latch", CountDownLatch.class); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new ExceptionInInitializerError(e); + } + } private final StampedLock lock = new StampedLock(); + private volatile CountDownLatch latch; /** @@ -43,7 +50,7 @@ public final class InversibleLock { // Write-locked. Read the corresponding latch and if present report an exception, which will propagate // and force release of locks. - final CountDownLatch local = latch; + final var local = latch; if (local != null) { throw new InversibleLockException(local); } @@ -57,18 +64,14 @@ public final class InversibleLock { } public long writeLock() { - final CountDownLatch local = new CountDownLatch(1); - final boolean taken = LATCH_UPDATER.compareAndSet(this, null, local); - Verify.verify(taken); - + verify(LATCH.compareAndSet(this, null, new CountDownLatch(1))); return lock.writeLock(); } public void unlockWrite(final long stamp) { - final CountDownLatch local = LATCH_UPDATER.getAndSet(this, null); - Verify.verifyNotNull(local); + final CountDownLatch local = (CountDownLatch) LATCH.getAndSet(this, null); + verifyNotNull(local); lock.unlockWrite(stamp); local.countDown(); } - }