2006-07-31 02:31:38 -04:00
|
|
|
require "delegate"
|
|
|
|
|
2006-08-04 14:05:50 -04:00
|
|
|
# WeakRef is a class to represent a reference to an object that is not seen by
|
|
|
|
# the tracing phase of the garbage collector. This allows the referenced
|
|
|
|
# object to be garbage collected as if nothing is referring to it. Because
|
|
|
|
# WeakRef delegates method calls to the referenced object, it may be used in
|
|
|
|
# place of that object, i.e. it is of the same duck type.
|
1998-01-16 07:19:09 -05:00
|
|
|
#
|
|
|
|
# Usage:
|
2006-08-04 14:05:50 -04:00
|
|
|
#
|
1998-01-16 07:19:09 -05:00
|
|
|
# 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)
|
1999-01-19 23:59:39 -05:00
|
|
|
class WeakRef<Delegator
|
1998-01-16 07:19:09 -05:00
|
|
|
|
2006-07-31 02:31:38 -04:00
|
|
|
# RefError is raised if an object cannot be referenced by a WeakRef.
|
1999-01-19 23:59:39 -05:00
|
|
|
class RefError<StandardError
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
2003-12-10 03:26:36 -05:00
|
|
|
@@id_map = {} # obj -> [ref,...]
|
|
|
|
@@id_rev_map = {} # ref -> obj
|
2005-08-12 03:17:36 -04:00
|
|
|
@@final = lambda {|id|
|
|
|
|
printf "final: %p\n", id
|
2000-07-17 05:38:10 -04:00
|
|
|
__old_status = Thread.critical
|
|
|
|
Thread.critical = true
|
|
|
|
begin
|
2003-12-10 03:26:36 -05:00
|
|
|
rids = @@id_map[id]
|
2000-07-17 05:38:10 -04:00
|
|
|
if rids
|
|
|
|
for rid in rids
|
2003-12-10 03:26:36 -05:00
|
|
|
@@id_rev_map.delete(rid)
|
2000-07-17 05:38:10 -04:00
|
|
|
end
|
2003-12-10 03:26:36 -05:00
|
|
|
@@id_map.delete(id)
|
2000-07-17 05:38:10 -04:00
|
|
|
end
|
2003-12-10 03:26:36 -05:00
|
|
|
rid = @@id_rev_map[id]
|
2000-07-17 05:38:10 -04:00
|
|
|
if rid
|
2003-12-10 03:26:36 -05:00
|
|
|
@@id_rev_map.delete(id)
|
|
|
|
@@id_map[rid].delete(id)
|
|
|
|
@@id_map.delete(rid) if @@id_map[rid].empty?
|
2000-07-17 05:38:10 -04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Thread.critical = __old_status
|
|
|
|
end
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2006-07-31 02:31:38 -04:00
|
|
|
# Create a new WeakRef from +orig+.
|
1998-01-16 07:19:09 -05:00
|
|
|
def initialize(orig)
|
2004-01-27 01:05:04 -05:00
|
|
|
@__id = orig.object_id
|
2005-08-12 03:17:36 -04:00
|
|
|
printf "orig: %p\n", @__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
|
2003-12-10 03:26:36 -05:00
|
|
|
@@id_map[@__id] = [] unless @@id_map[@__id]
|
2000-07-27 05:49:34 -04:00
|
|
|
ensure
|
|
|
|
Thread.critical = __old_status
|
|
|
|
end
|
2004-01-27 01:05:04 -05:00
|
|
|
@@id_map[@__id].push self.object_id
|
|
|
|
@@id_rev_map[self.object_id] = @__id
|
2005-08-12 03:17:36 -04:00
|
|
|
super
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
|
2006-08-04 14:05:50 -04:00
|
|
|
# Return the object this WeakRef references. Raises RefError if the object
|
|
|
|
# has been garbage collected. The object returned is the object to which
|
|
|
|
# method calls are delegated (see Delegator).
|
1998-01-16 07:19:09 -05:00
|
|
|
def __getobj__
|
2004-01-27 01:05:04 -05:00
|
|
|
unless @@id_rev_map[self.object_id] == @__id
|
|
|
|
Kernel::raise RefError, "Illegal Reference - probably recycled", Kernel::caller(2)
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
2001-11-08 01:43:14 -05:00
|
|
|
begin
|
|
|
|
ObjectSpace._id2ref(@__id)
|
|
|
|
rescue RangeError
|
2004-01-27 01:05:04 -05:00
|
|
|
Kernel::raise RefError, "Illegal Reference - probably recycled", Kernel::caller(2)
|
2001-11-08 01:43:14 -05:00
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
2006-08-04 14:05:50 -04:00
|
|
|
|
2005-08-12 03:17:36 -04:00
|
|
|
def __setobj__(obj)
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
2006-08-04 14:05:50 -04:00
|
|
|
# Returns true if the referenced object still exists, and false if it has
|
|
|
|
# been garbage collected.
|
1998-01-16 07:19:09 -05:00
|
|
|
def weakref_alive?
|
2004-01-27 01:05:04 -05:00
|
|
|
@@id_rev_map[self.object_id] == @__id
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if __FILE__ == $0
|
2005-08-12 03:17:36 -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
|
2005-08-12 03:17:36 -04: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
|