Class InfinispanLock

java.lang.Object
org.infinispan.util.concurrent.locks.impl.InfinispanLock

public class InfinispanLock extends Object
A special lock for Infinispan cache.

The main different with the traditional Lock is allowing to use any object as lock owner. It is possible to use a Thread as lock owner that makes similar to Lock.

In addition, it has an asynchronous interface. acquire(Object, long, TimeUnit) will not acquire the lock immediately (except if it is free) but will return a ExtendedLockPromise. This promise allow to test if the lock is acquired asynchronously and cancel the lock acquisition, without any blocking.

Since:
8.0
Author:
Pedro Ruivo
  • Constructor Details

    • InfinispanLock

      public InfinispanLock(Executor nonBlockingExecutor, TimeService timeService)
      Creates a new instance.
      Parameters:
      nonBlockingExecutor - executor that is resumed upon after a lock has been acquired or times out if waiting
      timeService - the TimeService to check for timeouts.
    • InfinispanLock

      public InfinispanLock(Executor nonBlockingExecutor, TimeService timeService, Runnable releaseRunnable)
      Creates a new instance.
      Parameters:
      nonBlockingExecutor - executor that is resumed upon after a lock has been acquired or times out if waiting
      timeService - the TimeService to check for timeouts.
      releaseRunnable - a Runnable that is invoked every time this lock is released.
    • InfinispanLock

      public InfinispanLock(Executor nonBlockingExecutor, TimeService timeService, Runnable releaseRunnable, Object owner, ByRef<ExtendedLockPromise> lockPromise)
      Creates a new instance which is acquired by owner.

      The lockPromise stores the reference to the ExtendedLockPromise. The method acquire(Object, long, TimeUnit) is no longer necessary to be invoked by this lock owner.

      Parameters:
      nonBlockingExecutor - executor that is resumed upon after a lock has been acquired or times out if waiting
      timeService - the TimeService to check for timeouts.
      releaseRunnable - a Runnable that is invoked every time this lock is released.
      owner - the lock owner.
      lockPromise - the ByRef to store the ExtendedLockPromise.
  • Method Details

    • setTimeService

      public void setTimeService(TimeService timeService)
      Tests purpose only!
    • acquire

      public ExtendedLockPromise acquire(Object lockOwner, long time, TimeUnit timeUnit)
      It tries to acquire this lock.

      If it is invoked multiple times with the same owner, the same ExtendedLockPromise is returned until it has timed-out or release(Object) is invoked.

      If the lock is free, it is immediately acquired, otherwise the lock owner is queued.

      Parameters:
      lockOwner - the lock owner who needs to acquire the lock.
      time - the timeout value.
      timeUnit - the timeout unit.
      Returns:
      an ExtendedLockPromise.
      Throws:
      NullPointerException - if lockOwner or timeUnit is null.
    • release

      public void release(Object lockOwner)
      It tries to release the lock held by lockOwner.

      If the lock is not acquired (is waiting or timed out/deadlocked) by lockOwner, its ExtendedLockPromise is canceled. If lockOwner is the current lock owner, the lock is released and the next lock owner available will acquire the lock. If the lockOwner never tried to acquire the lock, this method does nothing.

      Parameters:
      lockOwner - the lock owner who wants to release the lock.
      Throws:
      NullPointerException - if lockOwner is null.
    • getLockOwner

      public Object getLockOwner()
      Returns:
      the current lock owner or null if it is not acquired.
    • isLocked

      public boolean isLocked()
      It checks if the lock is acquired.

      A false return value does not mean the lock is free since it may have queued lock owners.

      Returns:
      true if the lock is acquired.
    • deadlockCheck

      public void deadlockCheck(DeadlockChecker deadlockChecker)
      It forces a deadlock checking.
    • containsLockOwner

      public boolean containsLockOwner(Object lockOwner)
      It tests if the lock has the lock owner.

      It return true if the lock owner is the current lock owner or it in the queue.

      Parameters:
      lockOwner - the lock owner to test.
      Returns:
      true if it contains the lock owner.