Package dev.gmitch215.bytebox.locks
Class ReentrantLock
java.lang.Object
dev.gmitch215.bytebox.locks.ReentrantLock
- All Implemented Interfaces:
Lock
java.util.concurrent.locks.ReentrantLock, which the class library does not carry at all.
A second caller blocks until the owner leaves, the way it does on a JVM: the wait is
Object.wait, which suspends the calling fiber and lets the owner run. A call needs a fiber
under it: the generated entry point builds the handler, a Durable Object and a Workflow on one, so
their constructors and the static initializers they trigger may take the lock. A call made while no
fiber is running traps, whether or not it would have waited.
Fairness is not modelled: the argument is accepted and ignored, and waiters wake in whatever order the monitor admits them.
- Since:
- 1.0.2
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintReturns how many times the calling fiber has taken this lock without releasing it.booleanReturns whether the calling fiber holds this lock.booleanisLocked()Returns whether any fiber holds this lock.voidlock()Takes the lock, suspending the caller while another holds it.voidTakes the lock, giving up if the caller is interrupted first.Returns a condition bound to this lock.booleantryLock()Returns whether the lock was free and has now been taken.booleanTakes the lock, waiting up to the time given for its owner to leave.voidunlock()Releases the lock, and wakes the callers waiting for it once the last hold is gone.
-
Constructor Details
-
ReentrantLock
public ReentrantLock()A lock nothing holds. -
ReentrantLock
public ReentrantLock(boolean fair) - Parameters:
fair- ignored; waiters are admitted in whatever order the monitor wakes them
-
-
Method Details
-
lock
public void lock()Takes the lock, suspending the caller while another holds it. -
lockInterruptibly
Takes the lock, giving up if the caller is interrupted first.- Specified by:
lockInterruptiblyin interfaceLock- Throws:
InterruptedException- if the caller was interrupted before or while waiting
-
tryLock
public boolean tryLock()Returns whether the lock was free and has now been taken. -
tryLock
Takes the lock, waiting up to the time given for its owner to leave.- Specified by:
tryLockin interfaceLock- Parameters:
time- the longest to waitunit- the unit oftime- Returns:
- whether it was taken in time
- Throws:
InterruptedException- if the caller was interrupted before or while waiting
-
unlock
public void unlock()Releases the lock, and wakes the callers waiting for it once the last hold is gone. -
newCondition
Returns a condition bound to this lock.- Specified by:
newConditionin interfaceLock- Returns:
- a condition bound to this lock
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()Returns whether the calling fiber holds this lock.- Returns:
- whether the calling fiber holds this lock
-
isLocked
public boolean isLocked()Returns whether any fiber holds this lock.- Returns:
- whether any fiber holds this lock
-
getHoldCount
public int getHoldCount()Returns how many times the calling fiber has taken this lock without releasing it.- Returns:
- how many times the calling fiber has taken this lock without releasing it
-