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>
* test/ruby/enc/test_case_options.rb: adjust test class name

View file

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

View file

@ -12,7 +12,6 @@ class TimerIdConvTest < Test::Unit::TestCase
key = idconv.to_id(self)
assert_equal(key, self.__id__)
sleep(keeping)
assert_equal(idconv.to_id(false), false.__id__)
assert_equal(idconv.to_obj(key), self)
sleep(keeping)
@ -24,11 +23,48 @@ class TimerIdConvTest < Test::Unit::TestCase
sleep(keeping)
assert_raise do
assert_equal(idconv.to_obj(key))
assert_equal(idconv.to_obj(key), self)
end
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