From 2ea7c184fa28c47e390ea26ace13bbfd9a7868a3 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 9 Sep 2016 22:30:20 +0200 Subject: [PATCH] Switch to using StampedLock Instead of using ReentrantReadWriteLock, use StampedLock, which has better performance. Change-Id: Ifb8c3c90fb05a37264e006acc7d94b4bde5d2893 Signed-off-by: Robert Varga (cherry picked from commit 24f1985c8a8f8aa4afaafc2cec7a1908890c993a) --- .../dom/spi/AbstractRegistrationTree.java | 33 ++++++++++--------- .../dom/spi/AbstractRegistrationTreeTest.java | 3 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/AbstractRegistrationTree.java b/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/AbstractRegistrationTree.java index 9da8f18cfa..c98826d6b2 100644 --- a/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/AbstractRegistrationTree.java +++ b/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/AbstractRegistrationTree.java @@ -7,8 +7,8 @@ */ package org.opendaylight.mdsal.dom.spi; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.StampedLock; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; @@ -18,18 +18,22 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum * @param Type of registered object */ public abstract class AbstractRegistrationTree { - private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true); private final RegistrationTreeNode rootNode = new RegistrationTreeNode<>(null, null); + private final Lock writeLock; + private final Lock readLock; protected AbstractRegistrationTree() { - + final StampedLock lock = new StampedLock(); + readLock = lock.asReadLock(); + writeLock = lock.asWriteLock(); } /** - * Acquire the read-write lock. This should be done before invoking {@link #findNodeFor(Iterable)}. + * Acquire the read-write lock. This should be done before invoking {@link #findNodeFor(Iterable)}. This method + * must not be called when the lock is already held by this thread. */ protected final void takeLock() { - rwLock.writeLock().lock(); + writeLock.lock(); } /** @@ -37,12 +41,11 @@ public abstract class AbstractRegistrationTree { * and modification of the returned node. Note that callers should do so in a finally block. */ protected final void releaseLock() { - rwLock.writeLock().unlock(); + writeLock.unlock(); } /** - * Find an existing, or allocate a fresh, node for a particular path. Must be called with the - * read-write lock held. + * Find an existing, or allocate a fresh, node for a particular path. Must be called with the lock held. * * @param path Path to find a node for * @return A registration node for the specified path @@ -68,20 +71,19 @@ public abstract class AbstractRegistrationTree { } /** - * Remove a registration from a particular node. This method must not be called while the read-write lock - * is held. + * Remove a registration from a particular node. This method must not be called while the lock is held. * * @param node Tree node * @param registration Registration instance */ protected final void removeRegistration(@Nonnull final RegistrationTreeNode node, @Nonnull final T registration) { // Take the write lock - rwLock.writeLock().lock(); + writeLock.lock(); try { node.removeRegistration(registration); } finally { // Always release the lock - rwLock.writeLock().unlock(); + writeLock.unlock(); } } @@ -93,8 +95,7 @@ public abstract class AbstractRegistrationTree { * @return A snapshot instance. */ @Nonnull public final RegistrationTreeSnapshot takeSnapshot() { - final RegistrationTreeSnapshot ret = new RegistrationTreeSnapshot<>(rwLock.readLock(), rootNode); - rwLock.readLock().lock(); - return ret; + readLock.lock(); + return new RegistrationTreeSnapshot<>(readLock, rootNode); } } diff --git a/dom/mdsal-dom-spi/src/test/java/org/opendaylight/mdsal/dom/spi/AbstractRegistrationTreeTest.java b/dom/mdsal-dom-spi/src/test/java/org/opendaylight/mdsal/dom/spi/AbstractRegistrationTreeTest.java index 530359f7c2..bd895dcaee 100644 --- a/dom/mdsal-dom-spi/src/test/java/org/opendaylight/mdsal/dom/spi/AbstractRegistrationTreeTest.java +++ b/dom/mdsal-dom-spi/src/test/java/org/opendaylight/mdsal/dom/spi/AbstractRegistrationTreeTest.java @@ -29,6 +29,7 @@ public class AbstractRegistrationTreeTest extends AbstractRegistrationTree