public class ScalableRWLock extends Object implements ReadWriteLock, Serializable
readersStateList
containing all the references to
ReadersEntry (Reader's states), which the Writer scans to determine if the Readers have completed
or not. After a thread terminates, the finalize()
of the associated ReaderEntry
instance will be called, which will remove the Reader's state reference from the readersStateList
, to avoid memory leaking. Advantages:
java.util.concurrent.locks.ReadWriteLock
lockInterruptibly()
newCondition()
sharedLock()
is two synchronized
calls: an AtomicInteger.set()
on a cache line that is held in exclusive mode by the core
where the current thread is running, and an AtomicLong.get()
on a shared cache line.Constructor and Description |
---|
ScalableRWLock()
Default constructor
|
Modifier and Type | Method and Description |
---|---|
void |
exclusiveLock()
Acquires the write lock.
|
boolean |
exclusiveTryLock()
Acquires the write lock only if it is not held by another thread at the time of invocation.
|
boolean |
exclusiveTryLockNanos(long nanosTimeout)
Acquires the write lock if it is not held by another thread within the given waiting time.
|
void |
exclusiveUnlock()
Attempts to release the write lock.
|
Lock |
readLock() |
protected void |
removeState(AtomicInteger state)
This function should be called only from ReadersEntry.finalize()
|
void |
sharedLock()
Acquires the read lock.
|
boolean |
sharedTryLock()
Acquires the read lock only if the write lock is not held by another thread at the time of
invocation.
|
boolean |
sharedTryLockNanos(long nanosTimeout)
Acquires the read lock if the write lock is not held by another thread within the given waiting
time.
|
void |
sharedUnlock()
Attempts to release the read lock.
|
Lock |
writeLock() |
public Lock readLock()
readLock
in interface ReadWriteLock
public Lock writeLock()
writeLock
in interface ReadWriteLock
protected void removeState(AtomicInteger state)
state
- The reader's state that we wish to remove from the ConcurrentLinkedQueuepublic void sharedLock()
Acquires the read lock if the write lock is not held by another thread and returns immediately.
If the write lock is held by another thread then the current thread yields until the write lock is released.
public void sharedUnlock()
If the current thread is the holder of this lock then the reentrantReaderCount
is
decremented. If the reentrantReaderCount
is now zero then the lock is released. If the
current thread is not the holder of this lock then IllegalMonitorStateException
is
thrown.
IllegalMonitorStateException
- if the current thread does not hold this lock.public void exclusiveLock()
Acquires the write lock if neither the read nor write lock are held by another thread and
returns immediately, setting the write lock reentrantWriterCount
to one.
If the current thread already holds the write lock then the reentrantWriterCount
is
incremented by one and the method returns immediately.
If the lock is held by another thread, then the current thread yields and lies dormant until
the write lock has been acquired, at which time the reentrantWriterCount
is set to one.
public void exclusiveUnlock()
If the current thread is the holder of this lock then the reentrantWriterCount
is
decremented. If reentrantWriterCount
is now zero then the lock is released. If the
current thread is not the holder of this lock then IllegalMonitorStateException
is
thrown.
IllegalMonitorStateException
- if the current thread does not hold this lock.public boolean sharedTryLock()
Acquires the read lock if the write lock is not held by another thread and returns
immediately with the value true
.
If the write lock is held by another thread then this method will return immediately with
the value false
.
true
if the read lock was acquiredpublic boolean sharedTryLockNanos(long nanosTimeout)
Acquires the read lock if the write lock is not held by another thread and returns
immediately with the value true
.
If the write lock is held by another thread then the current thread yields execution until one of two things happens:
If the read lock is acquired then the value true
is returned.
nanosTimeout
- the time to wait for the read lock in nanosecondstrue
if the read lock was acquiredpublic boolean exclusiveTryLock()
Acquires the write lock if the write lock is not held by another thread and returns
immediately with the value true
if and only if no other thread is attempting a read
lock, setting the write lock writerLoop
count to one.
If the current thread already holds this lock then the reentrantWriterCount
count is
incremented by one and the method returns true
.
If the write lock is held by another thread then this method will return immediately with
the value false
.
true
if the write lock was free and was acquired by the current thread, or the
write lock was already held by the current thread; and false
otherwise.public boolean exclusiveTryLockNanos(long nanosTimeout) throws InterruptedException
Acquires the write lock if the write lock is not held by another thread and returns
immediately with the value true
if and only if no other thread is attempting a read
lock, setting the write lock reentrantWriterCount
to one. If another thread is
attempting a read lock, this function may yield until the read lock is released.
If the current thread already holds this lock then the reentrantWriterCount
is
incremented by one and the method returns true
.
If the write lock is held by another thread then the current thread yields and lies dormant until one of two things happens:
If the write lock is acquired then the value true
is returned and the write lock
reentrantWriterCount
is set to one.
nanosTimeout
- the time to wait for the write lock in nanosecondstrue
if the lock was free and was acquired by the current thread, or the write
lock was already held by the current thread; and false
if the waiting time elapsed
before the lock could be acquired.InterruptedException
Copyright © 2009–2020 OrientDB. All rights reserved.