Interface WaitQueue

  • All Known Implementing Classes:
    WaitQueue.Standard

    public interface WaitQueue

    A relatively easy to use utility for general purpose thread signalling.

    Usage on a thread awaiting a state change using a WaitQueue q is:

     
          while (!conditionMet())
              Signal s = q.register();
                  if (!conditionMet())    // or, perhaps more correctly, !conditionChanged()
                      s.await();
                  else
                      s.cancel();
     
     
    A signalling thread, AFTER changing the state, then calls q.signal() to wake up one, or q.signalAll() to wake up all, waiting threads.

    To understand intuitively how this class works, the idea is simply that a thread, once it considers itself incapable of making progress, registers to be awoken once that changes. Since this could have changed between checking and registering (in which case the thread that made this change would have been unable to signal it), it checks the condition again, sleeping only if it hasn't changed/still is not met.

    This thread synchronisation scheme has some advantages over Condition objects and Object.wait/notify in that no monitor acquisition is necessary and, in fact, besides the actual waiting on a signal, all operations are non-blocking. As a result consumers can never block producers, nor each other, or vice versa, from making progress. Threads that are signalled are also put into a RUNNABLE state almost simultaneously, so they can all immediately make progress without having to serially acquire the monitor/lock, reducing scheduler delay incurred.

    A few notes on utilisation:

    1. A thread will only exit await() when it has been signalled, but this does not guarantee the condition has not been altered since it was signalled, and depending on your design it is likely the outer condition will need to be checked in a loop, though this is not always the case.

    2. Each signal is single use, so must be re-registered after each await(). This is true even if it times out.

    3. If you choose not to wait on the signal (because the condition has been met before you waited on it) you must cancel() the signal if the signalling thread uses signal() to awake waiters; otherwise signals will be lost. If signalAll() is used but infrequent, and register() is frequent, cancel() should still be used to prevent the queue growing unboundedly. Similarly, if you provide a TimerContext, cancel should be used to ensure it is not erroneously counted towards wait time.

    4. Care must be taken when selecting conditionMet() to ensure we are waiting on the condition that actually indicates progress is possible. In some complex cases it may be tempting to wait on a condition that is only indicative of local progress, not progress on the task we are aiming to complete, and a race may leave us waiting for a condition to be met that we no longer need.

    5. This scheme is not fair

    6. Only the thread that calls register() may call await()

    TODO: this class should not be backed by CLQ (should use an intrusive linked-list with lower overhead)
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static interface  WaitQueue.Signal
      A Signal is a one-time-use mechanism for a thread to wait for notification that some condition state has transitioned that it may be interested in (and hence should check if it is).
      static class  WaitQueue.Standard  
    • Method Detail

      • register

        WaitQueue.Signal register()
        The calling thread MUST be the thread that uses the signal
      • register

        <V> WaitQueue.Signal register​(V supplyOnDone,
                                      java.util.function.Consumer<V> receiveOnDone)
        The calling thread MUST be the thread that uses the signal. If the Signal is waited on, context.stop() will be called when the wait times out, the Signal is signalled, or the waiting thread is interrupted.
      • signal

        boolean signal()
        Signal one waiting thread
      • signalAll

        void signalAll()
        Signal all waiting threads
      • hasWaiters

        boolean hasWaiters()
        getWaiting() > 0
      • getWaiting

        int getWaiting()
        Return how many threads are waiting
      • newWaitQueue

        static WaitQueue newWaitQueue()
        Factory method used to capture and redirect instantiations for simulation
      • waitOnCondition

        static void waitOnCondition​(java.util.function.BooleanSupplier condition,
                                    WaitQueue queue)
                             throws java.lang.InterruptedException
        Loops waiting on the supplied condition and WaitQueue and will not return until the condition is true
        Throws:
        java.lang.InterruptedException