123456789_123456789_123456789_123456789_123456789_

Class: MonitorMixin::ConditionVariable

Relationships & Source Files
Namespace Children
Exceptions:
Inherits: Object
Defined in: lib/monitor.rb

Overview

FIXME: This isn't documented in Nutshell.

Since MonitorMixin.new_cond returns a ConditionVariable, and the example above calls while_wait and signal, this class should be documented.

Class Method Summary

Instance Method Summary

  • #broadcast

    Wakes up all threads waiting for this lock.

  • #signal

    Wakes up the first thread in line waiting for this lock.

  • #wait(timeout = nil)

    Releases the lock held in the associated monitor and waits; reacquires the lock on wakeup.

  • #wait_until

    Calls wait repeatedly until the given block yields a truthy value.

  • #wait_while

    Calls wait repeatedly while the given block yields a truthy value.

Constructor Details

.new(monitor) ⇒ ConditionVariable (private)

[ GitHub ]

  
# File 'lib/monitor.rb', line 159

def initialize(monitor)
  @monitor = monitor
  @cond = Thread::ConditionVariable.new
end

Instance Method Details

#broadcast

Wakes up all threads waiting for this lock.

[ GitHub ]

  
# File 'lib/monitor.rb', line 152

def broadcast
  @monitor.__send__(:mon_check_owner)
  @cond.broadcast
end

#signal

Wakes up the first thread in line waiting for this lock.

[ GitHub ]

  
# File 'lib/monitor.rb', line 144

def signal
  @monitor.__send__(:mon_check_owner)
  @cond.signal
end

#wait(timeout = nil)

Releases the lock held in the associated monitor and waits; reacquires the lock on wakeup.

If timeout is given, this method returns after timeout seconds passed, even if no other thread doesn't signal.

[ GitHub ]

  
# File 'lib/monitor.rb', line 108

def wait(timeout = nil)
  Thread.handle_interrupt(EXCEPTION_NEVER) do
    @monitor.__send__(:mon_check_owner)
    count = @monitor.__send__(:mon_exit_for_cond)
    begin
      Thread.handle_interrupt(EXCEPTION_IMMEDIATE) do
        @cond.wait(@monitor.instance_variable_get(:@mon_mutex), timeout)
      end
      return true
    ensure
      @monitor.__send__(:mon_enter_for_cond, count)
    end
  end
end

#wait_until

Calls wait repeatedly until the given block yields a truthy value.

[ GitHub ]

  
# File 'lib/monitor.rb', line 135

def wait_until
  until yield
    wait
  end
end

#wait_while

Calls wait repeatedly while the given block yields a truthy value.

[ GitHub ]

  
# File 'lib/monitor.rb', line 126

def wait_while
  while yield
    wait
  end
end