class Pry::InputLock

def __with_ownership

the input through interruptible_region().
Adds ourselves to the ownership list. The last one in the list may access
def __with_ownership
  @mutex.synchronize do
    # Three cases:
    # 1) There are no owners, in this case we are good to go.
    # 2) The current owner of the input is not reading the input (it might
    #    just be evaluating some ruby that the user typed).
    #    The current owner will figure out that it cannot go back to reading
    #    the input since we are adding ourselves to the @owners list, which
    #    in turns makes us the current owner.
    # 3) The owner of the input is in the interruptible region, reading from
    #    the input. It's safe to send an Interrupt exception to interrupt
    #    the owner. It will then proceed like in case 2).
    #    We wait until the owner sets the interruptible flag back
    #    to false, meaning that he's out of the interruptible region.
    #    Note that the owner may receive multiple interrupts since, but that
    #    should be okay (and trying to avoid it is futile anyway).
    while @interruptible
      @owners.last.raise Interrupt
      @cond.wait(@mutex)
    end
    @owners << Thread.current
  end
  yield
ensure
  @mutex.synchronize do
    # We are releasing any desire to have the input ownership by removing
    # ourselves from the list.
    @owners.delete(Thread.current)
    # We need to wake up the thread at the end of the @owners list, but
    # sadly Ruby doesn't allow us to choose which one we wake up, so we wake
    # them all up.
    @cond.broadcast
  end
end