Module: MonitorMixin
| Relationships & Source Files | |
| Defined in: | lib/monitor.rb |
Overview
monitor.rb
Copyright © 2001 Shugo Maeda <shugo@ruby-lang.org>
This library is distributed under the terms of the Ruby license. You can freely distribute/modify this library.
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.
Constant Summary
-
ConditionVariable =
Internal use only
# File 'lib/monitor.rb', line 89Monitor::ConditionVariable
Class Method Summary
-
.extend_object(obj)
Internal use only
FIXME: This isn’t documented in Nutshell.
Instance Attribute Summary
-
#mon_locked? ⇒ Boolean
readonly
Returns true if this monitor is locked by any thread.
-
#mon_owned? ⇒ Boolean
readonly
Returns true if this monitor is locked by current thread.
Instance Method Summary
-
#mon_enter
Enters exclusive section.
-
#mon_exit
Leaves exclusive section.
-
#mon_synchronize(&b)
(also: #synchronize)
Enters exclusive section and executes the block.
-
#mon_try_enter
(also: #try_mon_enter)
Attempts to enter exclusive section.
-
#new_cond
Creates a new ConditionVariable associated with the
::Monitorobject. -
#synchronize(&b)
Alias for #mon_synchronize.
-
#try_mon_enter
Alias for #mon_try_enter.
-
#initialize
private
Use
extend MonitorMixinorinclude MonitorMixininstead of this constructor. -
#mon_check_owner
private
Ensures that the
MonitorMixinis owned by the current thread, otherwise raises an exception. -
#mon_initialize
private
Initializes the
MonitorMixinafter being included in a class or when an object has been extended with theMonitorMixin.
Class Method Details
.extend_object(obj)
FIXME: This isn’t documented in Nutshell.
Since #new_cond returns a ConditionVariable, and the example above calls while_wait and signal, this class should be documented.
# File 'lib/monitor.rb', line 98
def self.extend_object(obj) # :nodoc: super(obj) obj.__send__(:mon_initialize) end
Instance Attribute Details
#mon_locked? ⇒ Boolean (readonly)
Returns true if this monitor is locked by any thread
# File 'lib/monitor.rb', line 130
def mon_locked? @mon_data.mon_locked? end
#mon_owned? ⇒ Boolean (readonly)
Returns true if this monitor is locked by current thread.
# File 'lib/monitor.rb', line 137
def mon_owned? @mon_data.mon_owned? end
Instance Method Details
#initialize (private)
Use extend MonitorMixin or include MonitorMixin instead of this constructor. Have look at the examples above to understand how to use this module.
# File 'lib/monitor.rb', line 168
def initialize(...) super mon_initialize end
#mon_check_owner (private)
Ensures that the MonitorMixin is owned by the current thread, otherwise raises an exception.
# File 'lib/monitor.rb', line 189
def mon_check_owner @mon_data.mon_check_owner end
#mon_enter
Enters exclusive section.
# File 'lib/monitor.rb', line 115
def mon_enter @mon_data.enter end
#mon_exit
Leaves exclusive section.
# File 'lib/monitor.rb', line 122
def mon_exit mon_check_owner @mon_data.exit end
#mon_initialize (private)
Initializes the MonitorMixin after being included in a class or when an object has been extended with the MonitorMixin
# File 'lib/monitor.rb', line 175
def mon_initialize if defined?(@mon_data) if defined?(@mon_initialized_by_new_cond) return # already initialized. elsif @mon_data_owner_object_id == self.object_id raise ThreadError, "already initialized" end end @mon_data = ::Monitor.new @mon_data_owner_object_id = self.object_id end
#mon_synchronize(&b) Also known as: #synchronize
Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin.
# File 'lib/monitor.rb', line 146
def mon_synchronize(&b) @mon_data.synchronize(&b) end
#mon_try_enter Also known as: #try_mon_enter
Attempts to enter exclusive section. Returns false if lock fails.
# File 'lib/monitor.rb', line 106
def mon_try_enter @mon_data.try_enter end
#new_cond
Creates a new ConditionVariable associated with the ::Monitor object.
# File 'lib/monitor.rb', line 155
def new_cond unless defined?(@mon_data) mon_initialize @mon_initialized_by_new_cond = true end return ConditionVariable.new(@mon_data) end
#synchronize(&b)
Alias for #mon_synchronize.
# File 'lib/monitor.rb', line 149
alias synchronize mon_synchronize
#try_mon_enter
Alias for #mon_try_enter.
# File 'lib/monitor.rb', line 110
alias try_mon_enter mon_try_enter