2011-06-29 20:37:00 -04:00
|
|
|
require "delegate"
|
|
|
|
require 'thread'
|
|
|
|
|
|
|
|
# Weak Reference class that does allows a referenced object to be
|
|
|
|
# garbage-collected. A WeakRef may be used exactly like the object it
|
|
|
|
# references.
|
1998-01-16 07:19:09 -05:00
|
|
|
#
|
|
|
|
# Usage:
|
2011-06-29 20:37:00 -04:00
|
|
|
#
|
1998-01-16 07:19:09 -05:00
|
|
|
# foo = Object.new
|
1999-08-13 01:45:20 -04:00
|
|
|
# foo = Object.new
|
2011-05-18 20:07:25 -04:00
|
|
|
# p foo.to_s # original's class
|
1998-01-16 07:19:09 -05:00
|
|
|
# foo = WeakRef.new(foo)
|
2011-05-18 20:07:25 -04:00
|
|
|
# p foo.to_s # should be same class
|
1998-01-16 07:19:09 -05:00
|
|
|
# ObjectSpace.garbage_collect
|
2011-05-18 20:07:25 -04:00
|
|
|
# p foo.to_s # should raise exception (recycled)
|
1998-01-16 07:19:09 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
class WeakRef < Delegator
|
|
|
|
|
2011-06-29 20:37:00 -04:00
|
|
|
##
|
|
|
|
# RefError is raised when a referenced object has been recycled by the
|
|
|
|
# garbage collector
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
class RefError < StandardError
|
1999-01-19 23:59:39 -05:00
|
|
|
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
|
2006-12-31 10:02:22 -05:00
|
|
|
@@mutex = Mutex.new
|
2005-08-12 03:17:36 -04:00
|
|
|
@@final = lambda {|id|
|
2006-12-31 10:02:22 -05:00
|
|
|
@@mutex.synchronize {
|
2003-12-10 03:26:36 -05:00
|
|
|
rids = @@id_map[id]
|
2000-07-17 05:38:10 -04:00
|
|
|
if rids
|
2011-05-18 17:19:18 -04:00
|
|
|
for rid in rids
|
|
|
|
@@id_rev_map.delete(rid)
|
|
|
|
end
|
|
|
|
@@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
|
2011-05-18 17:19:18 -04: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
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2000-07-17 05:38:10 -04:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2011-06-29 20:37:00 -04:00
|
|
|
##
|
|
|
|
# Creates a weak reference to +orig+
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
def initialize(orig)
|
2006-12-31 10:02:22 -05:00
|
|
|
@__id = orig.object_id
|
|
|
|
ObjectSpace.define_finalizer orig, @@final
|
|
|
|
ObjectSpace.define_finalizer self, @@final
|
|
|
|
@@mutex.synchronize {
|
|
|
|
@@id_map[@__id] = [] unless @@id_map[@__id]
|
|
|
|
}
|
|
|
|
@@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
|
|
|
|
|
2011-06-29 20:37:00 -04:00
|
|
|
def __getobj__ # :nodoc:
|
2004-01-27 01:05:04 -05:00
|
|
|
unless @@id_rev_map[self.object_id] == @__id
|
2007-12-27 03:58:03 -05:00
|
|
|
Kernel::raise RefError, "Invalid 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
|
2007-12-27 03:58:03 -05:00
|
|
|
Kernel::raise RefError, "Invalid Reference - probably recycled", Kernel::caller(2)
|
2001-11-08 01:43:14 -05:00
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
2011-06-29 20:37:00 -04:00
|
|
|
|
|
|
|
def __setobj__(obj) # :nodoc:
|
2005-08-12 03:17:36 -04:00
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
2011-06-29 20:37:00 -04:00
|
|
|
##
|
|
|
|
# Returns true if the referenced object is still alive.
|
|
|
|
|
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
|
2011-05-18 20:07:25 -04:00
|
|
|
p foo.to_s # original's class
|
1999-01-19 23:59:39 -05:00
|
|
|
foo = WeakRef.new(foo)
|
2011-05-18 20:07:25 -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
|
2011-05-18 20:07:25 -04:00
|
|
|
p foo.to_s # should raise exception (recycled)
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|