1998-01-16 15:19:09 +03:00
|
|
|
# Weak Reference class that does not bother GCing.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# foo = Object.new
|
1999-08-13 09:45:20 +04:00
|
|
|
# foo = Object.new
|
|
|
|
# p foo.to_s # original's class
|
1998-01-16 15:19:09 +03:00
|
|
|
# foo = WeakRef.new(foo)
|
1999-08-13 09:45:20 +04:00
|
|
|
# p foo.to_s # should be same class
|
1998-01-16 15:19:09 +03:00
|
|
|
# ObjectSpace.garbage_collect
|
1999-08-13 09:45:20 +04:00
|
|
|
# p foo.to_s # should raise exception (recycled)
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
require "delegate"
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
class WeakRef<Delegator
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
class RefError<StandardError
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
ID_MAP = {} # obj -> [ref,...]
|
|
|
|
ID_REV_MAP = {} # ref -> obj
|
2000-07-17 13:38:10 +04:00
|
|
|
@@final = lambda{|id|
|
|
|
|
__old_status = Thread.critical
|
|
|
|
Thread.critical = true
|
|
|
|
begin
|
|
|
|
rids = ID_MAP[id]
|
|
|
|
if rids
|
|
|
|
for rid in rids
|
|
|
|
ID_REV_MAP[rid] = nil
|
|
|
|
end
|
|
|
|
ID_MAP[id] = nil
|
|
|
|
end
|
|
|
|
rid = ID_REV_MAP[id]
|
|
|
|
if rid
|
|
|
|
ID_REV_MAP[id] = nil
|
|
|
|
ID_MAP[rid].delete(id)
|
|
|
|
ID_MAP[rid] = nil if ID_MAP[rid].empty?
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Thread.critical = __old_status
|
|
|
|
end
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
def initialize(orig)
|
|
|
|
super
|
1999-01-20 07:59:39 +03:00
|
|
|
@__id = orig.__id__
|
2000-07-18 10:00:45 +04:00
|
|
|
ObjectSpace.define_finalizer orig, @@final
|
|
|
|
ObjectSpace.define_finalizer self, @@final
|
2000-07-27 13:49:34 +04:00
|
|
|
__old_status = Thread.critical
|
|
|
|
begin
|
|
|
|
Thread.critical = true
|
|
|
|
ID_MAP[@__id] = [] unless ID_MAP[@__id]
|
|
|
|
ensure
|
|
|
|
Thread.critical = __old_status
|
|
|
|
end
|
2000-03-06 07:15:42 +03:00
|
|
|
ID_MAP[@__id].push self.__id__
|
1999-01-20 07:59:39 +03:00
|
|
|
ID_REV_MAP[self.id] = @__id
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def __getobj__
|
1999-01-20 07:59:39 +03:00
|
|
|
unless ID_MAP[@__id]
|
|
|
|
raise RefError, "Illegal Reference - probably recycled", caller(2)
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2001-11-08 09:43:14 +03:00
|
|
|
begin
|
|
|
|
ObjectSpace._id2ref(@__id)
|
|
|
|
rescue RangeError
|
|
|
|
raise RefError, "Illegal Reference - probably recycled", caller(2)
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def weakref_alive?
|
1999-01-20 07:59:39 +03:00
|
|
|
if ID_MAP[@__id]
|
1998-01-16 15:19:09 +03:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if __FILE__ == $0
|
1999-08-13 09:45:20 +04:00
|
|
|
require 'thread'
|
1999-01-20 07:59:39 +03:00
|
|
|
foo = Object.new
|
1999-08-13 09:45:20 +04:00
|
|
|
p foo.to_s # original's class
|
1999-01-20 07:59:39 +03:00
|
|
|
foo = WeakRef.new(foo)
|
1999-08-13 09:45:20 +04:00
|
|
|
p foo.to_s # should be same class
|
1999-01-20 07:59:39 +03:00
|
|
|
ObjectSpace.garbage_collect
|
1999-08-13 09:45:20 +04:00
|
|
|
p foo.to_s # should raise exception (recycled)
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|