1998-01-16 07:19:09 -05:00
|
|
|
# Weak Reference class that does not bother GCing.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# foo = Object.new
|
1999-08-13 01:45:20 -04:00
|
|
|
# foo = Object.new
|
|
|
|
# p foo.to_s # original's class
|
1998-01-16 07:19:09 -05:00
|
|
|
# foo = WeakRef.new(foo)
|
1999-08-13 01:45:20 -04:00
|
|
|
# p foo.to_s # should be same class
|
1998-01-16 07:19:09 -05:00
|
|
|
# ObjectSpace.garbage_collect
|
1999-08-13 01:45:20 -04:00
|
|
|
# p foo.to_s # should raise exception (recycled)
|
1998-01-16 07:19:09 -05:00
|
|
|
|
|
|
|
require "delegate"
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
class WeakRef<Delegator
|
1998-01-16 07:19:09 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
class RefError<StandardError
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
ID_MAP = {} # obj -> [ref,...]
|
|
|
|
ID_REV_MAP = {} # ref -> obj
|
2000-07-17 05: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 01:45:20 -04:00
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
def initialize(orig)
|
|
|
|
super
|
1999-01-19 23:59:39 -05:00
|
|
|
@__id = orig.__id__
|
2000-07-18 02:00:45 -04:00
|
|
|
ObjectSpace.define_finalizer orig, @@final
|
|
|
|
ObjectSpace.define_finalizer self, @@final
|
2000-07-27 05: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-05 23:15:42 -05:00
|
|
|
ID_MAP[@__id].push self.__id__
|
1999-01-19 23:59:39 -05:00
|
|
|
ID_REV_MAP[self.id] = @__id
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def __getobj__
|
1999-01-19 23:59:39 -05:00
|
|
|
unless ID_MAP[@__id]
|
|
|
|
raise RefError, "Illegal Reference - probably recycled", caller(2)
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
ObjectSpace._id2ref(@__id)
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def weakref_alive?
|
1999-01-19 23:59:39 -05:00
|
|
|
if ID_MAP[@__id]
|
1998-01-16 07:19:09 -05:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if __FILE__ == $0
|
1999-08-13 01:45:20 -04:00
|
|
|
require 'thread'
|
1999-01-19 23:59:39 -05:00
|
|
|
foo = Object.new
|
1999-08-13 01:45:20 -04:00
|
|
|
p foo.to_s # original's class
|
1999-01-19 23:59:39 -05:00
|
|
|
foo = WeakRef.new(foo)
|
1999-08-13 01:45:20 -04:00
|
|
|
p foo.to_s # should be same class
|
1999-01-19 23:59:39 -05:00
|
|
|
ObjectSpace.garbage_collect
|
1999-08-13 01:45:20 -04:00
|
|
|
p foo.to_s # should raise exception (recycled)
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|