Module: MonitorMixin

Included in:
Logger::LogDevice
Defined in:
ext/monitor/lib/monitor.rb

Overview

In concurrent programming, a monitor is an object or module intended to be used safely by more than one thread. The defining characteristic of a monitor is that its methods are executed with mutual exclusion. That is, at each point in time, at most one thread may be executing any of its methods. This mutual exclusion greatly simplifies reasoning about the implementation of monitors compared to reasoning about parallel code that updates a data structure.

You can read more about the general principles on the Wikipedia page for Monitors.

Examples

Simple object.extend

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?. The producer thread (main thread) reads a line from ARGF and pushes it into buf then calls empty_cond.signal to notify the consumer thread of new data.

Simple Class include

require 'monitor'

class SynchronizedArray < Array

  include MonitorMixin

  def initialize(*args)
    super(*args)
  end

  alias :old_shift :shift
  alias :old_unshift :unshift

  def shift(n=1)
    self.synchronize do
      self.old_shift(n)
    end
  end

  def unshift(item)
    self.synchronize do
      self.old_unshift(item)
    end
  end

  # other methods ...
end

SynchronizedArray implements an Array with synchronized access to items. This Class is implemented as subclass of Array which includes the MonitorMixin module.

Defined Under Namespace

Classes: ConditionVariable

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extend_object(obj) ⇒ Object



152
153
154
155
# File 'ext/monitor/lib/monitor.rb', line 152

def self.extend_object(obj)
  super(obj)
  obj.__send__(:mon_initialize)
end

Instance Method Details

#mon_enterObject

Enters exclusive section.



169
170
171
# File 'ext/monitor/lib/monitor.rb', line 169

def mon_enter
  @mon_data.enter
end

#mon_exitObject

Leaves exclusive section.



176
177
178
179
# File 'ext/monitor/lib/monitor.rb', line 176

def mon_exit
  mon_check_owner
  @mon_data.exit
end

#mon_locked?Boolean

Returns true if this monitor is locked by any thread

Returns:

  • (Boolean)


184
185
186
# File 'ext/monitor/lib/monitor.rb', line 184

def mon_locked?
  @mon_data.mon_locked?
end

#mon_owned?Boolean

Returns true if this monitor is locked by current thread.

Returns:

  • (Boolean)


191
192
193
# File 'ext/monitor/lib/monitor.rb', line 191

def mon_owned?
  @mon_data.mon_owned?
end

#mon_synchronize(&b) ⇒ Object Also known as: synchronize

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



200
201
202
# File 'ext/monitor/lib/monitor.rb', line 200

def mon_synchronize(&b)
  @mon_data.synchronize(&b)
end

#mon_try_enterObject Also known as: try_mon_enter

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



160
161
162
# File 'ext/monitor/lib/monitor.rb', line 160

def mon_try_enter
  @mon_data.try_enter
end

#new_condObject

Creates a new MonitorMixin::ConditionVariable associated with the Monitor object.



209
210
211
212
213
214
215
# File 'ext/monitor/lib/monitor.rb', line 209

def new_cond
  unless defined?(@mon_data)
    mon_initialize
    @mon_initialized_by_new_cond = true
  end
  return ConditionVariable.new(@mon_data)
end