Class JvmGlobalLocks


  • public final class JvmGlobalLocks
    extends java.lang.Object
    Utility class providing JVM-global locks keyed by String. This class is provided to provide an alternative to the following locking invention:
         String foo;
         synchronized (foo.intern()) {
             // ...
         }
     

    As such this is an extremely bad idea, as it does not make it clear what the locking domain is and what code is actually participating on it. Until we can get proper locking in place, the above should be replaced with:

         String foo;
         final ReentrantLock = JvmGlobalLocks.getLockForString(foo);
         lock.lock();
         try {
             // ...
         } finally {
             lock.unlock();
         }
     

    Once all call sites are identified, their use of Strings should be replaced by domain-specific lock identifiers, so that it is obvious which places actually synchronize on a particular identifier. getLockFor(Identifier) and getLockFor(Identifiable) provide this functionality.

    Finally, users of this class should look to downgrade from JVM-global to either an explicit locking service, or to fine-grained locks contained within the component instantiation -- thus rendering this class completely unneeded.

    Author:
    Robert Varga
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static @NonNull java.util.concurrent.locks.ReentrantLock getLockFor​(org.opendaylight.yangtools.yang.binding.Identifiable<?> resource)
      Return a JVM-global ReentrantLock for a particular name.
      static @NonNull java.util.concurrent.locks.ReentrantLock getLockFor​(org.opendaylight.yangtools.yang.binding.Identifier<?> identifier)
      Return a JVM-global ReentrantLock for an identifier.
      static @NonNull java.util.concurrent.locks.ReentrantLock getLockForString​(java.lang.String lockName)
      Deprecated.
      This is provided only for migration purposes until a proper locking scheme is deployed throughout genius and netvirt.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getLockForString

        @Deprecated
        public static @NonNull java.util.concurrent.locks.ReentrantLock getLockForString​(java.lang.String lockName)
        Deprecated.
        This is provided only for migration purposes until a proper locking scheme is deployed throughout genius and netvirt.
        Return a JVM-global ReentrantLock for a particular string.
        Parameters:
        lockName - Name of the lock, must not be null
        Returns:
        A JVM-global reentrant lock.
        Throws:
        java.lang.NullPointerException - if lockName is null
      • getLockFor

        public static @NonNull java.util.concurrent.locks.ReentrantLock getLockFor​(org.opendaylight.yangtools.yang.binding.Identifier<?> identifier)
        Return a JVM-global ReentrantLock for an identifier.
        Parameters:
        identifier - Entity identifier, must not be null
        Returns:
        A JVM-global reentrant lock.
        Throws:
        java.lang.NullPointerException - if identifier is null
      • getLockFor

        public static @NonNull java.util.concurrent.locks.ReentrantLock getLockFor​(org.opendaylight.yangtools.yang.binding.Identifiable<?> resource)
        Return a JVM-global ReentrantLock for a particular name.
        Parameters:
        resource - an Identifiable resource
        Returns:
        A JVM-global reentrant lock.
        Throws:
        java.lang.NullPointerException - if resource is null