Class 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 |
         +----------------+---------------+---------+----------+
     
    • Constructor Detail

      • OffheapReadWriteLock

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

      • 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.