org.apache.ignite.internal.util

## 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 Summary

Fields
`static int` `LOCK_SIZE` Lock size.
`static int` `MAX_WAITERS` Maximum number of waiting threads, read or write.
`static int` `SPIN_CNT` TODO benchmark optimal spin count.
`static int` `TAG_LOCK_ALWAYS` Always lock tag.
`static boolean` `USE_RANDOM_RW_POLICY`

### Constructor Summary

Constructors
Constructor and Description
`OffheapReadWriteLock(int concLvl)`

### Method Summary

Methods
`void` `init(long lock, int tag)`
`boolean` `isReadLocked(long lock)`
`boolean` `isWriteLocked(long lock)`
`boolean` `readLock(long lock, int tag)`
`void` `readUnlock(long lock)`
`boolean` `tryWriteLock(long lock, int tag)`
`Boolean` `upgradeToWriteLock(long lock, int tag)` Upgrades a read lock to a write lock.
`boolean` `writeLock(long lock, int tag)`
`void` `writeUnlock(long lock, int tag)`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### SPIN_CNT

    public static final int SPIN_CNT

TODO benchmark optimal spin count.

#### USE_RANDOM_RW_POLICY

    public static final boolean USE_RANDOM_RW_POLICY

#### TAG_LOCK_ALWAYS

    public static final int TAG_LOCK_ALWAYS

Always lock tag.

See Also:
  Constant Field Values

#### LOCK_SIZE

    public static final int LOCK_SIZE

Lock size.

See Also:
  Constant Field Values

#### MAX_WAITERS

    public static final int MAX_WAITERS

Maximum number of waiting threads, read or write.

See Also:
  Constant Field Values

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

2017 Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Fabric | : ver. 2.2.0
Release Date | : September 15 2017