Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • monitor.rb

MonitorMixin::ConditionVariable

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.

Public Class Methods

new(monitor) click to toggle source
 
               # File monitor.rb, line 165
def initialize(monitor)
  @monitor = monitor
  @waiters = []
end
            

Public Instance Methods

broadcast() click to toggle source

Wake up all the waiters.

 
               # File monitor.rb, line 148
def broadcast
  @monitor.instance_eval {mon_check_owner()}
  Thread.critical = true
  for t in @waiters
    t.wakeup
  end
  @waiters.clear
  Thread.critical = false
  Thread.pass
end
            
count_waiters() click to toggle source
 
               # File monitor.rb, line 159
def count_waiters
  return @waiters.length
end
            
signal() click to toggle source

Wake up and run the next waiter

 
               # File monitor.rb, line 138
def signal
  @monitor.instance_eval {mon_check_owner()}
  Thread.critical = true
  t = @waiters.shift
  t.wakeup if t
  Thread.critical = false
  Thread.pass
end
            
wait(timeout = nil) click to toggle source

Create a new timer with the argument timeout, and add the current thread to the list of waiters. Then the thread is stopped. It will be resumed when a corresponding signal occurs.

 
               # File monitor.rb, line 93
def wait(timeout = nil)
  @monitor.instance_eval {mon_check_owner()}
  timer = create_timer(timeout)
  
  Thread.critical = true
  count = @monitor.instance_eval {mon_exit_for_cond()}
  @waiters.push(Thread.current)

  begin
    Thread.stop
    return true
  rescue Timeout
    return false
  ensure
    Thread.critical = true
    begin
      if timer && timer.alive?
        Thread.kill(timer)
      end
      if @waiters.include?(Thread.current)  # interrupted?
        @waiters.delete(Thread.current)
      end
      @monitor.instance_eval {mon_enter_for_cond(count)}
    ensure
      Thread.critical = false
    end
  end
end
            
wait_until() click to toggle source

call wait until the supplied block returns true.

 
               # File monitor.rb, line 131
def wait_until
  until yield
    wait
  end
end
            
wait_while() click to toggle source

call wait while the supplied block returns true.

 
               # File monitor.rb, line 124
def wait_while
  while yield
    wait
  end
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus