Class OffheapReadWriteLock

java.lang.Object
org.apache.ignite.internal.util.OffheapReadWriteLock

public class OffheapReadWriteLock extends Object
Lock state structure is as follows:
     +----------------+---------------+---------+----------+
     | WRITE WAIT CNT | READ WAIT CNT |   TAG   | LOCK CNT |
     +----------------+---------------+---------+----------+
     |     2 bytes    |     2 bytes   | 2 bytes |  2 bytes |
     +----------------+---------------+---------+----------+
 
  • Field Details

    • DFLT_OFFHEAP_RWLOCK_SPIN_COUNT

      public static final int DFLT_OFFHEAP_RWLOCK_SPIN_COUNT
      See Also:
    • SPIN_CNT

      public static final int SPIN_CNT
      TODO benchmark optimal spin count.
      See Also:
    • USE_RANDOM_RW_POLICY

      public static final boolean USE_RANDOM_RW_POLICY
      See Also:
    • TAG_LOCK_ALWAYS

      public static final int TAG_LOCK_ALWAYS
      Always lock tag.
      See Also:
    • LOCK_SIZE

      public static final int LOCK_SIZE
      Lock size.
      See Also:
    • MAX_WAITERS

      public static final int MAX_WAITERS
      Maximum number of waiting threads, read or write.
      See Also:
  • Constructor Details

    • OffheapReadWriteLock

      public OffheapReadWriteLock(int concLvl)
      Parameters:
      concLvl - Concurrency level, must be a power of two.
  • Method Details

    • init

      public void init(long lock, int tag)
      Parameters:
      lock - Lock pointer to initialize.
    • readLock

      public boolean readLock(long lock, int tag)
      Parameters:
      lock - Lock address.
    • readUnlock

      public void readUnlock(long lock)
      Parameters:
      lock - Lock address.
    • tryWriteLock

      public boolean tryWriteLock(long lock, int tag)
      Parameters:
      lock - Lock address.
    • writeLock

      public boolean writeLock(long lock, int tag)
      Parameters:
      lock - Lock address.
    • isWriteLocked

      public boolean isWriteLocked(long lock)
      Parameters:
      lock - Lock to check.
      Returns:
      True if write lock is held by any thread for the given offheap RW lock.
    • isReadLocked

      public boolean isReadLocked(long lock)
      Parameters:
      lock - Lock to check.
      Returns:
      True if at least one read lock is held by any thread for the given offheap RW lock.
    • writeUnlock

      public void writeUnlock(long lock, int tag)
      Parameters:
      lock - Lock address.
    • upgradeToWriteLock

      public Boolean upgradeToWriteLock(long lock, int tag)
      Upgrades a read lock to a write lock. If this thread is the only read-owner of the read lock, this method will atomically upgrade the read lock to the write lock. In this case true will be returned. If not, the read lock will be released and write lock will be acquired, leaving a potential gap for other threads to modify a protected resource. In this case this method will return false.

      After this method has been called, there is no need to call to readUnlock(long) because read lock will be released in any case.

      Parameters:
      lock - Lock to upgrade.
      Returns:
      null if tag validation failed, true if successfully traded the read lock to the write lock without leaving a gap. Returns false otherwise, in this case the resource state must be re-validated.