Class Latch.Condition

java.lang.Object
org.cojen.tupl.util.Latch.Condition
Enclosing class:
Latch

public static class Latch.Condition extends Object
Manages a queue of waiting threads, associated with a Latch instance. Unlike the built-in Java Condition class, spurious wakeup does not occur when waiting.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final int
    await(Latch latch)
    Blocks the current thread indefinitely until a signal is received.
    final int
    await(Latch latch, long nanosTimeout)
    Blocks the current thread until a signal is received.
    final int
    await(Latch latch, long nanosTimeout, long nanosEnd)
    Blocks the current thread until a signal is received.
    final int
    await(Latch latch, long timeout, TimeUnit unit)
    Blocks the current thread until a signal is received.
    final int
    awaitTagged(Latch latch, long nanosTimeout)
    Blocks the current thread until a signal is received.
    final int
    awaitTagged(Latch latch, long nanosTimeout, long nanosEnd)
    Blocks the current thread until a signal is received.
    final void
    Clears out all waiters and interrupts those that are threads.
    final boolean
    Returns true if no waiters are enqueued.
    final int
    priorityAwait(Latch latch, long nanosTimeout, long nanosEnd)
    Blocks the current thread until a signal is received.
    final void
    signal(Latch latch)
    Signals the first waiter, of any type.
    final void
    Signals all waiters, of any type.
    final void
    Signals the first waiter, but only if it's a tagged waiter.
    final void
    Invokes the given continuation upon the condition being signaled.
    final void
    Invokes the given continuation upon the condition being signaled.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Condition

      public Condition()
  • Method Details

    • isEmpty

      public final boolean isEmpty()
      Returns true if no waiters are enqueued. Caller must hold shared or exclusive latch.
    • await

      public final int await(Latch latch)
      Blocks the current thread indefinitely until a signal is received. Exclusive latch must be acquired by the caller, which is released and then re-acquired by this method.
      Parameters:
      latch - latch being used by this condition
      Returns:
      -1 if interrupted, or 1 if signaled
    • await

      public final int await(Latch latch, long timeout, TimeUnit unit)
      Blocks the current thread until a signal is received. Exclusive latch must be acquired by the caller, which is released and then re-acquired by this method.
      Parameters:
      latch - latch being used by this condition
      timeout - relative time to wait; infinite if <0
      unit - timeout unit
      Returns:
      -1 if interrupted, 0 if timed out, 1 if signaled
    • await

      public final int await(Latch latch, long nanosTimeout)
      Blocks the current thread until a signal is received. Exclusive latch must be acquired by the caller, which is released and then re-acquired by this method.
      Parameters:
      latch - latch being used by this condition
      nanosTimeout - relative nanosecond time to wait; infinite if <0
      Returns:
      -1 if interrupted, 0 if timed out, 1 if signaled
    • await

      public final int await(Latch latch, long nanosTimeout, long nanosEnd)
      Blocks the current thread until a signal is received. Exclusive latch must be acquired by the caller, which is released and then re-acquired by this method.
      Parameters:
      latch - latch being used by this condition
      nanosTimeout - relative nanosecond time to wait; infinite if <0
      nanosEnd - absolute nanosecond time to wait until; used only with >0 timeout
      Returns:
      -1 if interrupted, 0 if timed out, 1 if signaled
    • awaitTagged

      public final int awaitTagged(Latch latch, long nanosTimeout)
      Blocks the current thread until a signal is received. Exclusive latch must be acquired by the caller, which is released and then re-acquired by this method.
      Parameters:
      latch - latch being used by this condition
      nanosTimeout - relative nanosecond time to wait; infinite if <0
      Returns:
      -1 if interrupted, 0 if timed out, 1 if signaled
      See Also:
    • awaitTagged

      public final int awaitTagged(Latch latch, long nanosTimeout, long nanosEnd)
      Blocks the current thread until a signal is received. Exclusive latch must be acquired by the caller, which is released and then re-acquired by this method.
      Parameters:
      latch - latch being used by this condition
      nanosTimeout - relative nanosecond time to wait; infinite if <0
      nanosEnd - absolute nanosecond time to wait until; used only with >0 timeout
      Returns:
      -1 if interrupted, 0 if timed out, 1 if signaled
      See Also:
    • priorityAwait

      public final int priorityAwait(Latch latch, long nanosTimeout, long nanosEnd)
      Blocks the current thread until a signal is received. This method behaves like regular await method except the thread is signaled ahead of all the other waiting threads. Exclusive latch must be acquired by the caller, which is released and then re-acquired by this method.
      Parameters:
      latch - latch being used by this condition
      nanosTimeout - relative nanosecond time to wait; infinite if <0
      nanosEnd - absolute nanosecond time to wait until; used only with >0 timeout
      Returns:
      -1 if interrupted, 0 if timed out, 1 if signaled
    • uponSignal

      public final void uponSignal(Runnable cont)
      Invokes the given continuation upon the condition being signaled. The exclusive latch must be acquired by the caller, which is retained. When the condition is signaled, the continuation is enqueued to be run by a thread which releases the exclusive latch. The releasing thread actually retains the latch and runs the continuation, effectively transferring latch ownership. The continuation must not explicitly release the latch, and any exception thrown by the continuation is passed to the uncaught exception handler of the running thread.
      Parameters:
      cont - called with latch held
    • uponSignalTagged

      public final void uponSignalTagged(Runnable cont)
      Invokes the given continuation upon the condition being signaled. The exclusive latch must be acquired by the caller, which is retained. When the condition is signaled, the continuation is enqueued to be run by a thread which releases the exclusive latch. The releasing thread actually retains the latch and runs the continuation, effectively transferring latch ownership. The continuation must not explicitly release the latch, and any exception thrown by the continuation is passed to the uncaught exception handler of the running thread.
      Parameters:
      cont - called with latch held
      See Also:
    • signal

      public final void signal(Latch latch)
      Signals the first waiter, of any type. Caller must hold exclusive latch.
    • signalAll

      public final void signalAll(Latch latch)
      Signals all waiters, of any type. Caller must hold exclusive latch.
    • signalTagged

      public final void signalTagged(Latch latch)
      Signals the first waiter, but only if it's a tagged waiter. Caller must hold exclusive latch.
    • clear

      public final void clear()
      Clears out all waiters and interrupts those that are threads. Caller must hold exclusive latch.