1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

(Rinda::TemplateEntry::initialize): pull up method. Tabs converted to spaces.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@9162 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
seki 2005-09-14 16:43:57 +00:00
parent efa8c3e0d9
commit 7cc7f90498
2 changed files with 105 additions and 108 deletions

View file

@ -1,3 +1,8 @@
Thu Sep 15 01:39:19 2005 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp>
* lib/rinda/tuplespace.rb (Rinda::TemplateEntry::initialize): pull
up method. Tabs converted to spaces.
Thu Sep 15 00:18:24 2005 Yukihiro Matsumoto <matz@ruby-lang.org> Thu Sep 15 00:18:24 2005 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/net/telnet.rb (Net::Telnet::waitfor): replace sysread with * lib/net/telnet.rb (Net::Telnet::waitfor): replace sysread with

View file

@ -21,7 +21,7 @@ module Rinda
def initialize(ary, sec=nil) def initialize(ary, sec=nil)
@cancel = false @cancel = false
@ary = make_tuple(ary) @tuple = make_tuple(ary)
@renewer = nil @renewer = nil
renew(sec) renew(sec)
end end
@ -37,7 +37,7 @@ module Rinda
# Return the object which makes up the tuple itself: the Array # Return the object which makes up the tuple itself: the Array
# or Hash. # or Hash.
def value; @ary.value; end def value; @tuple.value; end
def canceled?; @cancel; end def canceled?; @cancel; end
@ -76,26 +76,26 @@ module Rinda
def make_expires(sec=nil) def make_expires(sec=nil)
case sec case sec
when Numeric when Numeric
Time.now + sec Time.now + sec
when true when true
Time.at(1) Time.at(1)
when nil when nil
Time.at(2**31-1) Time.at(2**31-1)
end end
end end
# Accessor method for the tuple. # Accessor method for the tuple.
def [](key) def [](key)
@ary[key] @tuple[key]
end end
def fetch(key) def fetch(key)
@ary.fetch(key) @tuple.fetch(key)
end end
# The size of the tuple. # The size of the tuple.
def size def size
@ary.size @tuple.size
end end
# Create a new tuple from the supplied object (array-like). # Create a new tuple from the supplied object (array-like).
@ -110,13 +110,13 @@ module Rinda
def get_renewer(it) def get_renewer(it)
case it case it
when Numeric, true, nil when Numeric, true, nil
return it, nil return it, nil
else else
begin begin
return it.renew, it return it.renew, it
rescue Exception rescue Exception
return it, nil return it, nil
end end
end end
end end
end end
@ -125,19 +125,11 @@ module Rinda
# The same as a TupleEntry but with methods to do matching. # The same as a TupleEntry but with methods to do matching.
# #
class TemplateEntry < TupleEntry class TemplateEntry < TupleEntry
def initialize(ary, expires=nil)
super(ary, expires)
@template = Rinda::Template.new(ary)
end
def match(tuple) def match(tuple)
@template.match(tuple) @tuple.match(tuple)
end end
# An alias for #match. alias === match
def ===(tuple)
match(tuple)
end
# Create a new Template from the supplied object. # Create a new Template from the supplied object.
def make_tuple(ary) def make_tuple(ary)
@ -173,7 +165,7 @@ module Rinda
def signal def signal
@place.synchronize do @place.synchronize do
@cond.signal @cond.signal
end end
end end
end end
@ -245,14 +237,14 @@ module Rinda
# Finds all tuples that match the template and are alive. # Finds all tuples that match the template and are alive.
def find_all(template) def find_all(template)
@hash.fetch(template.size, []).find_all do |tuple| @hash.fetch(template.size, []).find_all do |tuple|
tuple.alive? && template.match(tuple) tuple.alive? && template.match(tuple)
end end
end end
# Finds a template that matches and is alive. # Finds a template that matches and is alive.
def find(template) def find(template)
@hash.fetch(template.size, []).find do |tuple| @hash.fetch(template.size, []).find do |tuple|
tuple.alive? && template.match(tuple) tuple.alive? && template.match(tuple)
end end
end end
@ -260,7 +252,7 @@ module Rinda
# templates, match the supplied tuple and are alive. # templates, match the supplied tuple and are alive.
def find_all_template(tuple) def find_all_template(tuple)
@hash.fetch(tuple.size, []).find_all do |template| @hash.fetch(tuple.size, []).find_all do |template|
template.alive? && template.match(tuple) template.alive? && template.match(tuple)
end end
end end
@ -269,15 +261,15 @@ module Rinda
def delete_unless_alive def delete_unless_alive
deleted = [] deleted = []
@hash.keys.each do |size| @hash.keys.each do |size|
ary = [] ary = []
@hash[size].each do |tuple| @hash[size].each do |tuple|
if tuple.alive? if tuple.alive?
ary.push(tuple) ary.push(tuple)
else else
deleted.push(tuple) deleted.push(tuple)
end end
end end
@hash[size] = ary @hash[size] = ary
end end
deleted deleted
end end
@ -305,22 +297,22 @@ module Rinda
entry = TupleEntry.new(tuple, sec) entry = TupleEntry.new(tuple, sec)
start_keeper start_keeper
synchronize do synchronize do
if entry.expired? if entry.expired?
@read_waiter.find_all_template(entry).each do |template| @read_waiter.find_all_template(entry).each do |template|
template.read(tuple) template.read(tuple)
end end
notify_event('write', entry.value) notify_event('write', entry.value)
notify_event('delete', entry.value) notify_event('delete', entry.value)
else else
@bag.push(entry) @bag.push(entry)
@read_waiter.find_all_template(entry).each do |template| @read_waiter.find_all_template(entry).each do |template|
template.read(tuple) template.read(tuple)
end end
@take_waiter.find_all_template(entry).each do |template| @take_waiter.find_all_template(entry).each do |template|
template.signal template.signal
end end
notify_event('write', entry.value) notify_event('write', entry.value)
end end
end end
entry entry
end end
@ -335,32 +327,32 @@ module Rinda
yield(template) if block_given? yield(template) if block_given?
start_keeper start_keeper
synchronize do synchronize do
entry = @bag.find(template) entry = @bag.find(template)
if entry if entry
port.push(entry.value) if port port.push(entry.value) if port
@bag.delete(entry) @bag.delete(entry)
notify_event('take', entry.value) notify_event('take', entry.value)
return entry.value return entry.value
end end
raise RequestExpiredError if template.expired? raise RequestExpiredError if template.expired?
begin begin
@take_waiter.push(template) @take_waiter.push(template)
while true while true
raise RequestCanceledError if template.canceled? raise RequestCanceledError if template.canceled?
raise RequestExpiredError if template.expired? raise RequestExpiredError if template.expired?
entry = @bag.find(template) entry = @bag.find(template)
if entry if entry
port.push(entry.value) if port port.push(entry.value) if port
@bag.delete(entry) @bag.delete(entry)
notify_event('take', entry.value) notify_event('take', entry.value)
return entry.value return entry.value
end end
template.wait template.wait
end end
ensure ensure
@take_waiter.delete(template) @take_waiter.delete(template)
end end
end end
end end
@ -369,36 +361,36 @@ module Rinda
yield(template) if block_given? yield(template) if block_given?
start_keeper start_keeper
synchronize do synchronize do
entry = @bag.find(template) entry = @bag.find(template)
return entry.value if entry return entry.value if entry
raise RequestExpiredError if template.expired? raise RequestExpiredError if template.expired?
begin begin
@read_waiter.push(template) @read_waiter.push(template)
template.wait template.wait
raise RequestCanceledError if template.canceled? raise RequestCanceledError if template.canceled?
raise RequestExpiredError if template.expired? raise RequestExpiredError if template.expired?
return template.found return template.found
ensure ensure
@read_waiter.delete(template) @read_waiter.delete(template)
end end
end end
end end
def read_all(tuple) def read_all(tuple)
template = WaitTemplateEntry.new(self, tuple, nil) template = WaitTemplateEntry.new(self, tuple, nil)
synchronize do synchronize do
entry = @bag.find_all(template) entry = @bag.find_all(template)
entry.collect do |e| entry.collect do |e|
e.value e.value
end end
end end
end end
def notify(event, tuple, sec=nil) def notify(event, tuple, sec=nil)
template = NotifyTemplateEntry.new(self, event, tuple, sec) template = NotifyTemplateEntry.new(self, event, tuple, sec)
synchronize do synchronize do
@notify_waiter.push(template) @notify_waiter.push(template)
end end
template template
end end
@ -406,25 +398,25 @@ module Rinda
private private
def keep_clean def keep_clean
synchronize do synchronize do
@read_waiter.delete_unless_alive.each do |e| @read_waiter.delete_unless_alive.each do |e|
e.signal e.signal
end end
@take_waiter.delete_unless_alive.each do |e| @take_waiter.delete_unless_alive.each do |e|
e.signal e.signal
end end
@notify_waiter.delete_unless_alive.each do |e| @notify_waiter.delete_unless_alive.each do |e|
e.notify(['close']) e.notify(['close'])
end end
@bag.delete_unless_alive.each do |e| @bag.delete_unless_alive.each do |e|
notify_event('delete', e.value) notify_event('delete', e.value)
end end
end end
end end
def notify_event(event, tuple) def notify_event(event, tuple)
ev = [event, tuple] ev = [event, tuple]
@notify_waiter.find_all_template(ev).each do |template| @notify_waiter.find_all_template(ev).each do |template|
template.notify(ev) template.notify(ev)
end end
end end
@ -434,7 +426,7 @@ module Rinda
while need_keeper? while need_keeper?
keep_clean keep_clean
sleep(@period) sleep(@period)
end end
end end
end end