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

improved keeper thread

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8040 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
seki 2005-02-28 15:41:14 +00:00
parent 8687b8ffe8
commit 6fca4ecdd1
3 changed files with 54 additions and 17 deletions

View file

@ -1,3 +1,10 @@
Tue Mar 1 00:40:35 2005 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp>
* lib/rinda/tuplespace.rb (Rinda::TupleSpace): improved keeper thread.
* test/rinda/test_rinda.rb: ditto.
Mon Feb 28 23:10:13 2005 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp>
* ext/socket/socket.c (Init_socket): IPv6 is not supported although

View file

@ -220,6 +220,15 @@ module Rinda
@hash = {}
end
def has_expires?
@hash.each do |k, v|
v.each do |tuple|
return true if tuple.expires
end
end
false
end
# Add the object to the TupleBag.
def push(ary)
size = ary.size
@ -288,12 +297,13 @@ module Rinda
@take_waiter = TupleBag.new
@notify_waiter = TupleBag.new
@period = period
@keeper = keeper
@keeper = nil
end
# Put a tuple into the tuplespace.
def write(tuple, sec=nil)
entry = TupleEntry.new(tuple, sec)
start_keeper
synchronize do
if entry.expired?
@read_waiter.find_all_template(entry).each do |template|
@ -323,6 +333,7 @@ module Rinda
def move(port, tuple, sec=nil)
template = WaitTemplateEntry.new(self, tuple, sec)
yield(template) if block_given?
start_keeper
synchronize do
entry = @bag.find(template)
if entry
@ -356,6 +367,7 @@ module Rinda
def read(tuple, sec=nil)
template = WaitTemplateEntry.new(self, tuple, sec)
yield(template) if block_given?
start_keeper
synchronize do
entry = @bag.find(template)
return entry.value if entry
@ -416,13 +428,21 @@ module Rinda
end
end
def keeper
Thread.new do
loop do
sleep(@period)
keep_clean
def start_keeper
return if @keeper && @keeper.alive?
@keeper = Thread.new do
while need_keeper?
keep_clean
sleep(@period)
end
end
end
def need_keeper?
return true if @bag.has_expires?
return true if @read_waiter.has_expires?
return true if @take_waiter.has_expires?
return true if @notify_waiter.has_expires?
end
end
end

View file

@ -6,6 +6,8 @@ require 'rinda/tuplespace'
require 'singleton'
require 'weakref'
module Rinda
class MockClock
@ -499,6 +501,14 @@ class TupleSpaceTest < Test::Unit::TestCase
ThreadGroup.new.add(Thread.current)
@ts = Rinda::TupleSpace.new(1)
end
def test_gc
w = WeakRef.new(Rinda::TupleSpace.new)
GC.start
assert_raises(WeakRef::RefError) do
w.__getobj__
end
end
end
class TupleSpaceProxyTest < Test::Unit::TestCase