mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/monitor.rb: Improve documentation. Patch by Sandor Szucs.
[Ruby 1.9 - Bug #4823] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32119 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
09b830c4b2
commit
14d0f7aa48
2 changed files with 100 additions and 72 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Thu Jun 16 15:09:29 2011 Eric Hodel <drbrain@segment7.net>
|
||||||
|
|
||||||
|
* lib/monitor.rb: Improve documentation. Patch by Sandor Szucs.
|
||||||
|
[Ruby 1.9 - Bug #4823]
|
||||||
|
|
||||||
Thu Jun 16 14:54:09 2011 Eric Hodel <drbrain@segment7.net>
|
Thu Jun 16 14:54:09 2011 Eric Hodel <drbrain@segment7.net>
|
||||||
|
|
||||||
* lib/webrick/utils.rb: Document WEBrick::Utils. Patch by Olivier
|
* lib/webrick/utils.rb: Document WEBrick::Utils. Patch by Olivier
|
||||||
|
|
167
lib/monitor.rb
167
lib/monitor.rb
|
@ -1,81 +1,91 @@
|
||||||
=begin
|
# = monitor.rb
|
||||||
|
#
|
||||||
= monitor.rb
|
# Copyright (C) 2001 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
#
|
||||||
Copyright (C) 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.
|
||||||
This library is distributed under the terms of the Ruby license.
|
#
|
||||||
You can freely distribute/modify this library.
|
|
||||||
|
|
||||||
== example
|
|
||||||
|
|
||||||
This is a simple 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.
|
|
||||||
|
|
||||||
=end
|
|
||||||
|
|
||||||
require 'thread'
|
require 'thread'
|
||||||
|
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
#
|
#
|
||||||
# Adds monitor functionality to an arbitrary object by mixing the module with
|
# You can read more about the general principles on the Wikipedia page for
|
||||||
# +include+. For example:
|
# Monitors[http://en.wikipedia.org/wiki/Monitor_%28synchronization%29]
|
||||||
|
#
|
||||||
|
# == 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 <tt>buf.empty?</tt>. The producer thread (main thread) reads a
|
||||||
|
# line from ARGF and pushes it into buf then calls <tt>empty_cond.signal</tt>
|
||||||
|
# to notify the consumer thread of new data.
|
||||||
|
#
|
||||||
|
# === Simple Class include
|
||||||
#
|
#
|
||||||
# require 'monitor'
|
# require 'monitor'
|
||||||
|
#
|
||||||
|
# class SynchronizedArray < Array
|
||||||
#
|
#
|
||||||
# buf = []
|
# include MonitorMixin
|
||||||
# buf.extend(MonitorMixin)
|
|
||||||
# empty_cond = buf.new_cond
|
|
||||||
#
|
#
|
||||||
# # consumer
|
# def initialize(*args)
|
||||||
# Thread.start do
|
# super(*args)
|
||||||
# loop do
|
# end
|
||||||
# buf.synchronize do
|
#
|
||||||
# empty_cond.wait_while { buf.empty? }
|
# alias :old_shift :shift
|
||||||
# print buf.shift
|
# alias :old_unshift :unshift
|
||||||
# end
|
#
|
||||||
# end
|
# def shift(n=1)
|
||||||
# end
|
# self.synchronize do
|
||||||
|
# self.old_shift(n)
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# def unshift(item)
|
||||||
|
# self.synchronize do
|
||||||
|
# self.old_unshift(item)
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# # other methods ...
|
||||||
|
# end
|
||||||
#
|
#
|
||||||
# # producer
|
# +SynchronizedArray+ implements an Array with synchronized access to items.
|
||||||
# while line = ARGF.gets
|
# This Class is implemented as subclass of Array which includes the
|
||||||
# buf.synchronize do
|
# MonitorMixin module.
|
||||||
# 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.
|
|
||||||
#
|
#
|
||||||
module MonitorMixin
|
module MonitorMixin
|
||||||
#
|
#
|
||||||
|
@ -215,11 +225,16 @@ module MonitorMixin
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
# Use <tt>extend MonitorMixin</tt> or <tt>include MonitorMixin</tt> instead
|
||||||
|
# of this constructor. Have look at the examples above to understand how to
|
||||||
|
# use this module.
|
||||||
def initialize(*args)
|
def initialize(*args)
|
||||||
super
|
super
|
||||||
mon_initialize
|
mon_initialize
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Initializes the MonitorMixin after being included in a class or when an
|
||||||
|
# object has been extended with the MonitorMixin
|
||||||
def mon_initialize
|
def mon_initialize
|
||||||
@mon_owner = nil
|
@mon_owner = nil
|
||||||
@mon_count = 0
|
@mon_count = 0
|
||||||
|
@ -245,6 +260,16 @@ module MonitorMixin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Use the Monitor class when you want to have a lock object for blocks with
|
||||||
|
# mutual exclusion.
|
||||||
|
#
|
||||||
|
# require 'monitor'
|
||||||
|
#
|
||||||
|
# lock = Monitor.new
|
||||||
|
# lock.synchronize do
|
||||||
|
# # exclusive access
|
||||||
|
# end
|
||||||
|
#
|
||||||
class Monitor
|
class Monitor
|
||||||
include MonitorMixin
|
include MonitorMixin
|
||||||
alias try_enter try_mon_enter
|
alias try_enter try_mon_enter
|
||||||
|
@ -260,8 +285,6 @@ end
|
||||||
# - All the internals (internal modules Accessible and Initializable, class
|
# - All the internals (internal modules Accessible and Initializable, class
|
||||||
# ConditionVariable) appear in RDoc. It might be good to hide them, by
|
# ConditionVariable) appear in RDoc. It might be good to hide them, by
|
||||||
# making them private, or marking them :nodoc:, etc.
|
# making them private, or marking them :nodoc:, etc.
|
||||||
# - The entire example from the RD section at the top is replicated in the RDoc
|
|
||||||
# comment for MonitorMixin. Does the RD section need to remain?
|
|
||||||
# - RDoc doesn't recognise aliases, so we have mon_synchronize documented, but
|
# - RDoc doesn't recognise aliases, so we have mon_synchronize documented, but
|
||||||
# not synchronize.
|
# not synchronize.
|
||||||
# - mon_owner is in Nutshell, but appears as an accessor in a separate module
|
# - mon_owner is in Nutshell, but appears as an accessor in a separate module
|
||||||
|
|
Loading…
Add table
Reference in a new issue