Class ReentrantLock

java.lang.Object
dev.gmitch215.bytebox.locks.ReentrantLock
All Implemented Interfaces:
Lock

public class ReentrantLock extends Object implements 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
    Constructor
    Description
    A lock nothing holds.
    ReentrantLock(boolean fair)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns how many times the calling fiber has taken this lock without releasing it.
    boolean
    Returns whether the calling fiber holds this lock.
    boolean
    Returns whether any fiber holds this lock.
    void
    Takes the lock, suspending the caller while another holds it.
    void
    Takes the lock, giving up if the caller is interrupted first.
    Returns a condition bound to this lock.
    boolean
    Returns whether the lock was free and has now been taken.
    boolean
    tryLock(long time, TimeUnit unit)
    Takes the lock, waiting up to the time given for its owner to leave.
    void
    Releases the lock, and wakes the callers waiting for it once the last hold is gone.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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.
      Specified by:
      lock in interface Lock
    • lockInterruptibly

      public void lockInterruptibly() throws InterruptedException
      Takes the lock, giving up if the caller is interrupted first.
      Specified by:
      lockInterruptibly in interface Lock
      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.
      Specified by:
      tryLock in interface Lock
      Returns:
      whether the lock was free and has now been taken
    • tryLock

      public boolean tryLock(long time, TimeUnit unit) throws InterruptedException
      Takes the lock, waiting up to the time given for its owner to leave.
      Specified by:
      tryLock in interface Lock
      Parameters:
      time - the longest to wait
      unit - the unit of time
      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.
      Specified by:
      unlock in interface Lock
    • newCondition

      public Condition newCondition()
      Returns a condition bound to this lock.
      Specified by:
      newCondition in interface Lock
      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