# $Date: 1998/02/27 05:34:33 $
# add dependency R_method(obj, dependant)
# add(obj, dependant, method = :finalize, *opt)
# add_dependency(obj, dependant, method = :finalize, *opt)
# delete dependency R_method(obj, dependant)
# delete(obj_or_id, dependant, method = :finalize)
# delete_dependency(obj_or_id, dependant, method = :finalize)
# delete dependency R_*(obj, dependant)
# delete_all_dependency(obj_or_id, dependant)
# delete dependency R_method(*, dependant)
# delete_by_dependant(dependant, method = :finalize)
# delete dependency R_*(*, dependant)
# delete_all_by_dependant(dependant)
# delete all dependency R_*(*, *)
# finalize the dependant connected by dependency R_method(obj, dependtant).
# finalize(obj_or_id, dependant, method = :finalize)
# finalize_dependency(obj_or_id, dependant, method = :finalize)
# finalize all dependants connected by dependency R_*(obj, dependtant).
# finalize_all_dependency(obj_or_id, dependant)
# finalize the dependant connected by dependency R_method(*, dependtant).
# finalize_by_dependant(dependant, method = :finalize)
# finalize all dependants connected by dependency R_*(*, dependant).
# finalize_all_by_dependant(dependant)
# finalize all dependency registered to the Finalizer.
# stop invoking Finalizer on GC.
RCS_ID='-$Id: finalize.rb,v 1.4 1998/02/27 05:34:33 keiju Exp keiju $-'
# @dependency: {id => [[dependant, method, *opt], ...], ...}
# add dependency R_method(obj, dependant)
def add_dependency(obj, dependant, method = :finalize, *opt)
ObjectSpace.call_finalizer(obj)
method = method.intern unless method.kind_of?(Integer)
assoc = [dependant, method].concat(opt)
if dep = @dependency[obj.object_id]
@dependency[obj.object_id] = [assoc]
# delete dependency R_method(obj, dependant)
def delete_dependency(id, dependant, method = :finalize)
id = id.object_id unless id.kind_of?(Integer)
method = method.intern unless method.kind_of?(Integer)
for assoc in @dependency[id]
d == dependant && m == method
@dependency.delete(id) if assoc.empty?
alias delete delete_dependency
# delete dependency R_*(obj, dependant)
def delete_all_dependency(id, dependant)
id = id.object_id unless id.kind_of?(Integer)
method = method.intern unless method.kind_of?(Integer)
for assoc in @dependency[id]
@dependency.delete(id) if assoc.empty?
# delete dependency R_method(*, dependant)
def delete_by_dependant(dependant, method = :finalize)
method = method.intern unless method.kind_of?(Integer)
for id in @dependency.keys
delete(id, dependant, method)
# delete dependency R_*(*, dependant)
def delete_all_by_dependant(dependant)
for id in @dependency.keys
delete_all_dependency(id, dependant)
# finalize the depandant connected by dependency R_method(obj, dependtant)
def finalize_dependency(id, dependant, method = :finalize)
id = id.object_id unless id.kind_of?(Integer)
method = method.intern unless method.kind_of?(Integer)
for assocs in @dependency[id]
d.send(m, id, *o) if ret = d == dependant && m == method
@dependency.delete(id) if assoc.empty?
alias finalize finalize_dependency
# finalize all dependants connected by dependency R_*(obj, dependtant)
def finalize_all_dependency(id, dependant)
id = id.object_id unless id.kind_of?(Integer)
method = method.intern unless method.kind_of?(Integer)
for assoc in @dependency[id]
d.send(m, id, *o) if ret = d == dependant
@dependency.delete(id) if assoc.empty?
# finalize the dependant connected by dependency R_method(*, dependtant)
def finalize_by_dependant(dependant, method = :finalize)
method = method.intern unless method.kind_of?(Integer)
for id in @dependency.keys
finalize(id, dependant, method)
# finalize all dependants connected by dependency R_*(*, dependtant)
def finalize_all_by_dependant(dependant)
for id in @dependency.keys
finalize_all_dependency(id, dependant)
# finalize all dependants registered to the Finalizer.
for id, assocs in @dependency
for dependant, method, *opt in assocs
dependant.send(method, id, *opt)
# method to call finalize_* safely.
old_status = Thread.critical
ObjectSpace.remove_finalizer(@proc)
ObjectSpace.add_finalizer(@proc)
Thread.critical = old_status
# registering function to ObjectSpace#add_finalizer
if assocs = @dependency.delete(id)
for dependant, method, *opt in assocs
dependant.send(method, id, *opt)
@proc = proc{|id| final_of(id)}
ObjectSpace.add_finalizer(@proc)