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

use finalizer trick instead of thread.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55118 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
seki 2016-05-22 11:03:43 +00:00
parent 0bf2e5c70b
commit 021e8ead5c
3 changed files with 67 additions and 24 deletions

View file

@ -1,3 +1,9 @@
Sun May 22 20:01:21 2016 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp>
* lib/drb/timeridconv.rb: use finalizer trick instead of thread.
* test/drb/ut_timerholder.rb: ditto.
Sun May 22 17:25:18 2016 Martin Duerst <duerst@it.aoyama.ac.jp> Sun May 22 17:25:18 2016 Martin Duerst <duerst@it.aoyama.ac.jp>
* test/ruby/enc/test_case_options.rb: adjust test class name * test/ruby/enc/test_case_options.rb: adjust test class name

View file

@ -24,7 +24,7 @@ module DRb
@gc = {} @gc = {}
@renew = {} @renew = {}
@keeping = keeping @keeping = keeping
@expires = Time.now + @keeping @expires = nil
end end
def add(obj) def add(obj)
@ -32,18 +32,16 @@ module DRb
rotate rotate
key = obj.__id__ key = obj.__id__
@renew[key] = obj @renew[key] = obj
invoke_keeper
return key return key
end end
end end
def fetch(key, dv=@sentinel) def fetch(key)
synchronize do synchronize do
rotate rotate
obj = peek(key) obj = peek(key)
if obj == @sentinel raise InvalidIndexError if obj == @sentinel
return dv unless dv == @sentinel
raise InvalidIndexError
end
@renew[key] = obj # KeepIt @renew[key] = obj # KeepIt
return obj return obj
end end
@ -51,25 +49,28 @@ module DRb
private private
def peek(key) def peek(key)
synchronize do return @renew.fetch(key) { @gc.fetch(key, @sentinel) }
return @renew.fetch(key) { @gc.fetch(key, @sentinel) } end
end
def invoke_keeper
return if @expires
@expires = Time.now + @keeping
on_gc
end
def on_gc
return unless Thread.main.alive?
return if @expires.nil?
Thread.new { rotate } if @expires < Time.now
ObjectSpace.define_finalizer(Object.new) {on_gc}
end end
def rotate def rotate
synchronize do synchronize do
return if @expires > Time.now if @expires &.< Time.now
@gc = @renew # GCed @gc = @renew # GCed
@renew = {} @renew = {}
@expires = Time.now + @keeping @expires = @gc.empty? ? nil : Time.now + @keeping
end
end
def keeper
Thread.new do
loop do
rotate
sleep(@keeping)
end end
end end
end end

View file

@ -12,7 +12,6 @@ class TimerIdConvTest < Test::Unit::TestCase
key = idconv.to_id(self) key = idconv.to_id(self)
assert_equal(key, self.__id__) assert_equal(key, self.__id__)
sleep(keeping) sleep(keeping)
assert_equal(idconv.to_id(false), false.__id__) assert_equal(idconv.to_id(false), false.__id__)
assert_equal(idconv.to_obj(key), self) assert_equal(idconv.to_obj(key), self)
sleep(keeping) sleep(keeping)
@ -24,11 +23,48 @@ class TimerIdConvTest < Test::Unit::TestCase
sleep(keeping) sleep(keeping)
assert_raise do assert_raise do
assert_equal(idconv.to_obj(key)) assert_equal(idconv.to_obj(key), self)
end end
assert_raise do assert_raise do
assert_equal(idconv.to_obj(false.__id__)) assert_equal(idconv.to_obj(false.__id__), false)
end
key = idconv.to_id(self)
assert_equal(key, self.__id__)
assert_equal(idconv.to_id(true), true.__id__)
sleep(keeping)
GC.start
sleep(keeping)
GC.start
assert_raise do
assert_equal(idconv.to_obj(key), self)
end
end
def test_usecase_02
keeping = 0.1
idconv = DRb::TimerIdConv.new(keeping)
key = idconv.to_id(self)
assert_equal(key, self.__id__)
sleep(keeping)
GC.start
sleep(keeping)
GC.start
assert_raise do
assert_equal(idconv.to_obj(key), self)
end
GC.start
key = idconv.to_id(self)
assert_equal(key, self.__id__)
sleep(keeping)
GC.start
sleep(keeping)
GC.start
assert_raise do
assert_equal(idconv.to_obj(key), self)
end end
end end
end end