2006-08-04 14:05:50 -04:00
|
|
|
#--
|
1998-01-16 07:19:22 -05:00
|
|
|
# finalizer.rb -
|
1999-01-19 23:59:39 -05:00
|
|
|
# $Release Version: 0.3$
|
|
|
|
# $Revision: 1.4 $
|
|
|
|
# $Date: 1998/02/27 05:34:33 $
|
1998-01-16 07:19:22 -05:00
|
|
|
# by Keiju ISHITSUKA
|
2006-08-04 14:05:50 -04:00
|
|
|
#++
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2006-08-04 14:05:50 -04:00
|
|
|
# Usage:
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2006-08-04 14:05:50 -04:00
|
|
|
# add dependency R_method(obj, dependant)
|
1998-01-16 07:13:05 -05:00
|
|
|
# add(obj, dependant, method = :finalize, *opt)
|
|
|
|
# add_dependency(obj, dependant, method = :finalize, *opt)
|
|
|
|
#
|
2006-08-04 14:05:50 -04:00
|
|
|
# delete dependency R_method(obj, dependant)
|
1998-01-16 07:13:05 -05:00
|
|
|
# delete(obj_or_id, dependant, method = :finalize)
|
|
|
|
# delete_dependency(obj_or_id, dependant, method = :finalize)
|
2006-08-04 14:05:50 -04:00
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# delete dependency R_*(obj, dependant)
|
2006-08-04 14:05:50 -04:00
|
|
|
# delete_all_dependency(obj_or_id, dependant)
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# delete dependency R_method(*, dependant)
|
2006-08-04 14:05:50 -04:00
|
|
|
# delete_by_dependant(dependant, method = :finalize)
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# delete dependency R_*(*, dependant)
|
2006-08-04 14:05:50 -04:00
|
|
|
# delete_all_by_dependant(dependant)
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# delete all dependency R_*(*, *)
|
2006-08-04 14:05:50 -04:00
|
|
|
# delete_all
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2006-08-04 14:05:50 -04:00
|
|
|
# finalize the dependant connected by dependency R_method(obj, dependtant).
|
1998-01-16 07:13:05 -05:00
|
|
|
# finalize(obj_or_id, dependant, method = :finalize)
|
|
|
|
# finalize_dependency(obj_or_id, dependant, method = :finalize)
|
2006-08-04 14:05:50 -04:00
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# finalize all dependants connected by dependency R_*(obj, dependtant).
|
2006-08-04 14:05:50 -04:00
|
|
|
# finalize_all_dependency(obj_or_id, dependant)
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# finalize the dependant connected by dependency R_method(*, dependtant).
|
2006-08-04 14:05:50 -04:00
|
|
|
# finalize_by_dependant(dependant, method = :finalize)
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# finalize all dependants connected by dependency R_*(*, dependant).
|
2006-08-04 14:05:50 -04:00
|
|
|
# finalize_all_by_dependant(dependant)
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# finalize all dependency registered to the Finalizer.
|
2006-08-04 14:05:50 -04:00
|
|
|
# finalize_all
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# stop invoking Finalizer on GC.
|
2006-08-04 14:05:50 -04:00
|
|
|
# safe{..}
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
|
|
|
|
module Finalizer
|
1999-01-19 23:59:39 -05:00
|
|
|
RCS_ID='-$Id: finalize.rb,v 1.4 1998/02/27 05:34:33 keiju Exp keiju $-'
|
2002-08-29 05:08:18 -04:00
|
|
|
|
|
|
|
class <<self
|
|
|
|
# @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)
|
2002-11-07 14:18:16 -05:00
|
|
|
if dep = @dependency[obj.object_id]
|
2002-08-29 05:08:18 -04:00
|
|
|
dep.push assoc
|
|
|
|
else
|
2002-11-07 14:18:16 -05:00
|
|
|
@dependency[obj.object_id] = [assoc]
|
2002-08-29 05:08:18 -04:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
alias add add_dependency
|
|
|
|
|
|
|
|
# delete dependency R_method(obj, dependant)
|
|
|
|
def delete_dependency(id, dependant, method = :finalize)
|
2002-11-07 14:18:16 -05:00
|
|
|
id = id.object_id unless id.kind_of?(Integer)
|
2002-08-29 05:08:18 -04:00
|
|
|
method = method.intern unless method.kind_of?(Integer)
|
|
|
|
for assoc in @dependency[id]
|
|
|
|
assoc.delete_if do
|
|
|
|
|d, m, *o|
|
|
|
|
d == dependant && m == method
|
|
|
|
end
|
|
|
|
@dependency.delete(id) if assoc.empty?
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
alias delete delete_dependency
|
|
|
|
|
|
|
|
# delete dependency R_*(obj, dependant)
|
|
|
|
def delete_all_dependency(id, dependant)
|
2002-11-07 14:18:16 -05:00
|
|
|
id = id.object_id unless id.kind_of?(Integer)
|
2002-08-29 05:08:18 -04:00
|
|
|
method = method.intern unless method.kind_of?(Integer)
|
|
|
|
for assoc in @dependency[id]
|
|
|
|
assoc.delete_if do
|
|
|
|
|d, m, *o|
|
|
|
|
d == dependant
|
|
|
|
end
|
|
|
|
@dependency.delete(id) if assoc.empty?
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
|
|
|
|
# delete dependency R_*(*, dependant)
|
|
|
|
def delete_all_by_dependant(dependant)
|
|
|
|
for id in @dependency.keys
|
|
|
|
delete_all_dependency(id, dependant)
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
|
|
|
|
# finalize the depandant connected by dependency R_method(obj, dependtant)
|
|
|
|
def finalize_dependency(id, dependant, method = :finalize)
|
2002-11-07 14:18:16 -05:00
|
|
|
id = id.object_id unless id.kind_of?(Integer)
|
2002-08-29 05:08:18 -04:00
|
|
|
method = method.intern unless method.kind_of?(Integer)
|
|
|
|
for assocs in @dependency[id]
|
|
|
|
assocs.delete_if do
|
|
|
|
|d, m, *o|
|
|
|
|
d.send(m, id, *o) if ret = d == dependant && m == method
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
@dependency.delete(id) if assoc.empty?
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
alias finalize finalize_dependency
|
|
|
|
|
|
|
|
# finalize all dependants connected by dependency R_*(obj, dependtant)
|
|
|
|
def finalize_all_dependency(id, dependant)
|
2002-11-07 14:18:16 -05:00
|
|
|
id = id.object_id unless id.kind_of?(Integer)
|
2002-08-29 05:08:18 -04:00
|
|
|
method = method.intern unless method.kind_of?(Integer)
|
|
|
|
for assoc in @dependency[id]
|
|
|
|
assoc.delete_if do
|
|
|
|
|d, m, *o|
|
|
|
|
d.send(m, id, *o) if ret = d == dependant
|
|
|
|
end
|
|
|
|
@dependency.delete(id) if assoc.empty?
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
|
|
|
|
# finalize all dependants connected by dependency R_*(*, dependtant)
|
|
|
|
def finalize_all_by_dependant(dependant)
|
|
|
|
for id in @dependency.keys
|
|
|
|
finalize_all_dependency(id, dependant)
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
|
|
|
|
# finalize all dependants registered to the Finalizer.
|
|
|
|
def finalize_all
|
|
|
|
for id, assocs in @dependency
|
|
|
|
for dependant, method, *opt in assocs
|
|
|
|
dependant.send(method, id, *opt)
|
|
|
|
end
|
|
|
|
assocs.clear
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
|
|
|
|
# method to call finalize_* safely.
|
|
|
|
def safe
|
|
|
|
old_status = Thread.critical
|
|
|
|
Thread.critical = true
|
|
|
|
ObjectSpace.remove_finalizer(@proc)
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ObjectSpace.add_finalizer(@proc)
|
|
|
|
Thread.critical = old_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# registering function to ObjectSpace#add_finalizer
|
|
|
|
def final_of(id)
|
|
|
|
if assocs = @dependency.delete(id)
|
|
|
|
for dependant, method, *opt in assocs
|
|
|
|
dependant.send(method, id, *opt)
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2002-08-29 05:08:18 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
@dependency = Hash.new
|
|
|
|
@proc = proc{|id| final_of(id)}
|
|
|
|
ObjectSpace.add_finalizer(@proc)
|
|
|
|
end
|