1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#
# finalize.rb -
# $Release Version: $
# $Revision: 1.2 $
# $Date: 1997/07/25 02:43:00 $
# by Keiju ISHITSUKA(SHL Japan Inc.)
#
# --
#
# Usage:
#
# add(obj, dependant, method = :finalize, *opt)
# add_dependency(obj, dependant, method = :finalize, *opt)
# 依存関係 R_method(obj, dependant) の追加
#
# delete(obj_or_id, dependant, method = :finalize)
# delete_dependency(obj_or_id, dependant, method = :finalize)
# 依存関係 R_method(obj, dependant) の削除
# delete_all_dependency(obj_or_id, dependant)
# 依存関係 R_*(obj, dependant) の削除
# delete_by_dependant(dependant, method = :finalize)
# 依存関係 R_method(*, dependant) の削除
# delete_all_by_dependant(dependant)
# 依存関係 R_*(*, dependant) の削除
# delete_all
# 全ての依存関係の削除.
#
# finalize(obj_or_id, dependant, method = :finalize)
# finalize_dependency(obj_or_id, dependant, method = :finalize)
# 依存関連 R_method(obj, dependtant) で結ばれるdependantを
# finalizeする.
# finalize_all_dependency(obj_or_id, dependant)
# 依存関連 R_*(obj, dependtant) で結ばれるdependantをfinalizeする.
# finalize_by_dependant(dependant, method = :finalize)
# 依存関連 R_method(*, dependtant) で結ばれるdependantをfinalizeする.
# fainalize_all_by_dependant(dependant)
# 依存関連 R_*(*, dependtant) で結ばれるdependantをfinalizeする.
# finalize_all
# Finalizerに登録される全てのdependantをfinalizeする
#
# safe{..}
# gc時にFinalizerが起動するのを止める.
#
#
module Finalizer
RCS_ID='-$Header: /home/keiju/var/src/var.lib/ruby/RCS/finalize.rb,v 1.2 1997/07/25 02:43:00 keiju Exp keiju $-'
# @dependency: {id => [[dependant, method, *opt], ...], ...}
# 依存関係 R_method(obj, dependant) の追加
def add_dependency(obj, dependant, method = :finalize, *opt)
ObjectSpace.call_finalizer(obj)
method = method.id unless method.kind_of?(Fixnum)
assoc = [dependant, method].concat(opt)
if dep = @dependency[obj.id]
dep.push assoc
else
@dependency[obj.id] = [assoc]
end
end
alias add add_dependency
# 依存関係 R_method(obj, dependant) の削除
def delete_dependency(id, dependant, method = :finalize)
id = id.id unless id.kind_of?(Fixnum)
method = method.id unless method.kind_of?(Fixnum)
for assoc in @dependency[id]
assoc.delete_if do
|d, m, *o|
d == dependant && m == method
end
@dependency.delete(id) if assoc.empty?
end
end
alias delete delete_dependency
# 依存関係 R_*(obj, dependant) の削除
def delete_all_dependency(id, dependant)
id = id.id unless id.kind_of?(Fixnum)
method = method.id unless method.kind_of?(Fixnum)
for assoc in @dependency[id]
assoc.delete_if do
|d, m, *o|
d == dependant
end
@dependency.delete(id) if assoc.empty?
end
end
# 依存関係 R_method(*, dependant) の削除
def delete_by_dependant(dependant, method = :finalize)
method = method.id unless method.kind_of?(Fixnum)
for id in @dependency.keys
delete(id, dependant, method)
end
end
# 依存関係 R_*(*, dependant) の削除
def delete_all_by_dependant(dependant)
for id in @dependency.keys
delete_all_dependency(id, dependant)
end
end
# 依存関連 R_method(obj, dependtant) で結ばれるdependantをfinalizeす
# る.
def finalize_dependency(id, dependant, method = :finalize)
id = id.id unless id.kind_of?(Fixnum)
method = method.id unless method.kind_of?(Fixnum)
for assocs in @dependency[id]
assocs.delete_if do
|d, m, *o|
d.send(m, *o) if ret = d == dependant && m == method
ret
end
@dependency.delete(id) if assoc.empty?
end
end
alias finalize finalize_dependency
# 依存関連 R_*(obj, dependtant) で結ばれるdependantをfinalizeする.
def finalize_all_dependency(id, dependant)
id = id.id unless id.kind_of?(Fixnum)
method = method.id unless method.kind_of?(Fixnum)
for assoc in @dependency[id]
assoc.delete_if do
|d, m, *o|
d.send(m, *o) if ret = d == dependant
end
@dependency.delete(id) if assoc.empty?
end
end
# 依存関連 R_method(*, dependtant) で結ばれるdependantをfinalizeする.
def finalize_by_dependant(dependant, method = :finalize)
method = method.id unless method.kind_of?(Fixnum)
for id in @dependency.keys
finalize(id, dependant, method)
end
end
# 依存関連 R_*(*, dependtant) で結ばれるdependantをfinalizeする.
def fainalize_all_by_dependant(dependant)
for id in @dependency.keys
finalize_all_dependency(id, dependant)
end
end
# Finalizerに登録されている全てのdependantをfinalizeする
def finalize_all
for id, assocs in @dependency
for dependant, method, *opt in assocs
dependant.send(method, id, *opt)
end
assocs.clear
end
end
# finalize_* を安全に呼び出すためのイテレータ
def safe
old_status = Thread.critical
Thread.critical = TRUE
ObjectSpace.remove_finalizer(@proc)
yield
ObjectSpace.add_finalizer(@proc)
Thread.critical = old_status
end
# ObjectSpace#add_finalizerへの登録関数
def final_of(id)
if assocs = @dependency.delete(id)
for dependant, method, *opt in assocs
dependant.send(method, id, *opt)
end
end
end
@dependency = Hash.new
@proc = proc{|id| final_of(id)}
ObjectSpace.add_finalizer(@proc)
module_function :add
module_function :add_dependency
module_function :delete
module_function :delete_dependency
module_function :delete_all_dependency
module_function :delete_by_dependant
module_function :delete_all_by_dependant
module_function :finalize
module_function :finalize_dependency
module_function :finalize_all_dependency
module_function :finalize_by_dependant
module_function :fainalize_all_by_dependant
module_function :finalize_all
module_function :safe
module_function :final_of
private_class_method :final_of
end
|