Module MonitorMixin
In: monitor.rb

Adds monitor functionality to an arbitrary object by mixing the module with include. For example:

   require 'monitor.rb'

   buf = []
   buf.extend(MonitorMixin)
   empty_cond = buf.new_cond

   # consumer
   Thread.start do
     loop do
       buf.synchronize do
         empty_cond.wait_while { buf.empty? }
         print buf.shift
       end
     end
   end

   # producer
   while line = ARGF.gets
     buf.synchronize do
       buf.push(line)
       empty_cond.signal
     end
   end

The consumer thread waits for the producer thread to push a line to buf while buf.empty?, and the producer thread (main thread) reads a line from ARGF and push it to buf, then call empty_cond.signal.

Methods

Classes and Modules

Class MonitorMixin::ConditionVariable

Public Class methods

[Source]

# File monitor.rb, line 185
  def self.extend_object(obj)
    super(obj)
    obj.instance_eval {mon_initialize()}
  end

[Source]

# File monitor.rb, line 261
  def initialize(*args)
    super
    mon_initialize
  end

Public Instance methods

Enters exclusive section.

[Source]

# File monitor.rb, line 212
  def mon_enter
    Thread.critical = true
    mon_acquire(@mon_entering_queue)
    @mon_count += 1
  ensure
    Thread.critical = false
  end

Leaves exclusive section.

[Source]

# File monitor.rb, line 223
  def mon_exit
    mon_check_owner
    Thread.critical = true
    @mon_count -= 1
    if @mon_count == 0
      mon_release
    end
    Thread.critical = false
    Thread.pass
  end

Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin.

[Source]

# File monitor.rb, line 239
  def mon_synchronize
    mon_enter
    begin
      yield
    ensure
      mon_exit
    end
  end

Attempts to enter exclusive section. Returns false if lock fails.

[Source]

# File monitor.rb, line 193
  def mon_try_enter
    result = false
    Thread.critical = true
    if @mon_owner.nil?
      @mon_owner = Thread.current
    end
    if @mon_owner == Thread.current
      @mon_count += 1
      result = true
    end
    Thread.critical = false
    return result
  end

FIXME: This isn‘t documented in Nutshell.

Create a new condition variable for this monitor. This facilitates control of the monitor with signal and wait.

[Source]

# File monitor.rb, line 255
  def new_cond
    return ConditionVariable.new(self)
  end
synchronize()

Alias for mon_synchronize

try_mon_enter()

Alias for mon_try_enter

[Validate]

ruby-doc.org is hosted and maintained by James Britt and Happy Camper Studios, a Ruby application development company in Phoenix, Arizona. The site was created in 2002 as part of the Ruby Documentation Project to promote the Ruby language and to help other Ruby hackers.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.