author | Jan Vrany <jan.vrany@labware.com> |
Tue, 01 Jun 2021 20:19:13 +0100 | |
branch | jv |
changeset 25424 | 51bd8a6b196f |
parent 25411 | 248600ba8fd9 |
permissions | -rw-r--r-- |
1341 | 1 |
" |
2 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
25411
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
3 |
COPYRIGHT (c) 2021 LabWare |
1341 | 4 |
All Rights Reserved |
5 |
||
6 |
This software is furnished under a license and may be used |
|
7 |
only in accordance with the terms of that license and with the |
|
8 |
inclusion of the above copyright notice. This software may not |
|
9 |
be provided or otherwise made available to, or used by, any |
|
10 |
other person. No title to or ownership of the software is |
|
11 |
hereby transferred. |
|
12 |
" |
|
6041 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
15 |
"{ NameSpace: Smalltalk }" |
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
16 |
|
1341 | 17 |
Dictionary subclass:#WeakValueDictionary |
18 |
instanceVariableNames:'' |
|
19 |
classVariableNames:'' |
|
20 |
poolDictionaries:'' |
|
4350 | 21 |
category:'Collections-Weak' |
1341 | 22 |
! |
23 |
||
24 |
!WeakValueDictionary class methodsFor:'documentation'! |
|
25 |
||
26 |
copyright |
|
27 |
" |
|
28 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
25411
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
29 |
COPYRIGHT (c) 2021 LabWare |
1341 | 30 |
All Rights Reserved |
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
41 |
documentation |
|
42 |
" |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
43 |
WeakValueDictionaries behave like Dictionaries, |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
44 |
as long as the values are still referenced by some |
1341 | 45 |
other (non-weak) object. |
46 |
However, once the last non-weak reference ceases to exist, |
|
47 |
the Dictionary will return nil for the value at position key. |
|
48 |
(with some delay: it will be removed after the next garbage collect). |
|
49 |
||
2307 | 50 |
[Warning:] |
51 |
If you use this, be very careful since the collections size changes |
|
52 |
'magically' - for example, testing for being nonEmpty and then |
|
53 |
removing the first element may fail, since the element may vanish inbetween. |
|
54 |
In general, never trust the value as returned by the size/isEmpty messages. |
|
2260 | 55 |
|
1341 | 56 |
[author:] |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
57 |
Stefan Vogel |
1341 | 58 |
|
59 |
[See also:] |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
60 |
WeakArray WeakIdentityDictionary WeakIdentitySet |
1341 | 61 |
" |
62 |
! ! |
|
63 |
||
64 |
!WeakValueDictionary methodsFor:'adding & removing'! |
|
65 |
||
6330 | 66 |
at:key ifAbsent:somethingRespondingToValue |
19779 | 67 |
"Redefined to handle collected values (which are set to a SmallInteger) |
68 |
snd to block interrupts, to avoid trouble when dependencies |
|
19085 | 69 |
are added within interrupting high prio processes." |
6347 | 70 |
|
6330 | 71 |
|ret| |
72 |
||
19779 | 73 |
OperatingSystem blockInterrupts ifTrue:[ |
74 |
"/ already blocked |
|
75 |
ret := super at:key ifAbsent:0. |
|
76 |
] ifFalse:[ |
|
77 |
"do not use somethingRespondingToValue here, it might be a block answering |
|
78 |
an integer and be therefore called twice" |
|
79 |
[ |
|
80 |
ret := super at:key ifAbsent:0. |
|
81 |
] ensure:[ |
|
82 |
OperatingSystem unblockInterrupts. |
|
83 |
]. |
|
84 |
]. |
|
19085 | 85 |
|
20165 | 86 |
(ret isNil or:[ret class == SmallInteger]) ifTrue:[ |
19085 | 87 |
ret := somethingRespondingToValue value |
88 |
]. |
|
6330 | 89 |
^ ret |
90 |
! |
|
91 |
||
19543 | 92 |
at:key ifAbsentPut:replacementBlock |
6233 | 93 |
"return the element indexed by aKey if present, |
94 |
if not present, store the result of evaluating valueBlock |
|
95 |
under aKey and return it. |
|
6347 | 96 |
|
6233 | 97 |
Redefined to block interrupts, to avoid trouble when dependencies |
98 |
are added within interrupting high prio processes. |
|
99 |
WARNING: do not add elements while iterating over the receiver. |
|
19543 | 100 |
Iterate over a copy to do this." |
6233 | 101 |
|
19543 | 102 |
|val| |
6233 | 103 |
|
19543 | 104 |
OperatingSystem blockInterrupts ifTrue:[ |
105 |
"/ already blocked |
|
106 |
val := super at:key ifAbsentPut:replacementBlock. |
|
107 |
] ifFalse:[ |
|
108 |
[ |
|
109 |
val := super at:key ifAbsentPut:replacementBlock. |
|
110 |
] ensure:[ |
|
111 |
OperatingSystem unblockInterrupts. |
|
112 |
]. |
|
113 |
]. |
|
114 |
||
20165 | 115 |
(val isNil or:[val class == SmallInteger]) ifTrue:[ |
19543 | 116 |
self error:'WeakValueDictionary: invalid value'. |
117 |
]. |
|
118 |
||
119 |
^ val |
|
6233 | 120 |
! |
121 |
||
1341 | 122 |
at:key put:anObject |
123 |
"add the argument anObject under key, aKey to the receiver. |
|
124 |
Return anObject (sigh). |
|
125 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
126 |
are added within interrupting high prio processes." |
|
127 |
||
19543 | 128 |
|val| |
129 |
||
20165 | 130 |
(anObject isNil or:[anObject class == SmallInteger]) ifTrue:[ |
19543 | 131 |
self error:'WeakValueDictionary: invalid value'. |
132 |
]. |
|
133 |
||
134 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
135 |
"/ already blocked |
|
136 |
^ super at:key put:anObject. |
|
137 |
]. |
|
1341 | 138 |
|
1790 | 139 |
[ |
19543 | 140 |
val := super at:key put:anObject. |
141 |
] ensure:[ |
|
142 |
OperatingSystem unblockInterrupts. |
|
143 |
]. |
|
144 |
^ val |
|
1341 | 145 |
|
146 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
19543 | 147 |
"Modified: 29.1.1997 / 15:08:45 / cg" |
1341 | 148 |
! |
149 |
||
20165 | 150 |
removeIdentityValue:aValue ifAbsent:aBlock |
151 |
"remove the association under aValue from the collection, |
|
152 |
return the key previously stored there. |
|
153 |
If it was not in the collection return the result |
|
154 |
from evaluating aBlock. |
|
155 |
||
156 |
Redefined to avoid synchronization problems, in case |
|
157 |
of interrupts (otherwise, there could be some other operation |
|
158 |
on the receiver done by another process, which garbles my contents)." |
|
159 |
||
160 |
|ret| |
|
161 |
||
162 |
(aValue isNil or:[ aValue class == SmallInteger ]) ifTrue:[ |
|
163 |
^ aBlock value. |
|
164 |
]. |
|
165 |
OperatingSystem blockInterrupts ifTrue:[ |
|
166 |
"/ already blocked |
|
167 |
ret := super removeIdentityValue:aValue ifAbsent:aBlock |
|
168 |
] ifFalse:[ |
|
169 |
[ |
|
170 |
ret := super removeIdentityValue:aValue ifAbsent:aBlock |
|
171 |
] ensure:[ OperatingSystem unblockInterrupts. ]. |
|
172 |
]. |
|
173 |
^ ret. |
|
174 |
||
175 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
176 |
"Modified: 8.5.1996 / 14:54:09 / stefan" |
|
177 |
! |
|
178 |
||
1341 | 179 |
removeKey:aKey ifAbsent:aBlock |
180 |
"remove the association under aKey from the collection, |
|
2910 | 181 |
return the value previously stored there. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
182 |
If it was not in the collection return the result |
1341 | 183 |
from evaluating aBlock. |
184 |
||
185 |
Redefined to avoid synchronization problems, in case |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
186 |
of interrupts (otherwise, there could be some other operation |
1341 | 187 |
on the receiver done by another process, which garbles my contents)." |
188 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
189 |
|ret| |
1341 | 190 |
|
20164 | 191 |
OperatingSystem blockInterrupts ifTrue:[ |
192 |
"/ already blocked |
|
193 |
ret := super removeKey:aKey ifAbsent:aBlock |
|
194 |
] ifFalse:[ |
|
195 |
[ |
|
196 |
ret := super removeKey:aKey ifAbsent:aBlock |
|
197 |
] ensure:[ |
|
198 |
OperatingSystem unblockInterrupts. |
|
199 |
]. |
|
200 |
]. |
|
201 |
||
20165 | 202 |
(ret isNil or:[ret class == SmallInteger]) ifTrue:[ |
203 |
^ aBlock value. |
|
204 |
]. |
|
205 |
||
1341 | 206 |
^ ret |
207 |
||
208 |
"Modified: 6.5.1996 / 12:44:07 / stefan" |
|
209 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
210 |
! |
|
211 |
||
20165 | 212 |
removeValue:aValue ifAbsent:aBlock |
1341 | 213 |
"remove the association under aValue from the collection, |
2910 | 214 |
return the key previously stored there. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
215 |
If it was not in the collection return the result |
1341 | 216 |
from evaluating aBlock. |
217 |
||
218 |
Redefined to avoid synchronization problems, in case |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
219 |
of interrupts (otherwise, there could be some other operation |
1341 | 220 |
on the receiver done by another process, which garbles my contents)." |
221 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
222 |
|ret| |
1341 | 223 |
|
20165 | 224 |
(aValue isNil or:[aValue class == SmallInteger]) ifTrue:[ |
225 |
^ aBlock value. |
|
226 |
]. |
|
227 |
||
20164 | 228 |
OperatingSystem blockInterrupts ifTrue:[ |
229 |
"/ already blocked |
|
20165 | 230 |
ret := super removeValue:aValue ifAbsent:aBlock |
20164 | 231 |
] ifFalse:[ |
232 |
[ |
|
20165 | 233 |
ret := super removeValue:aValue ifAbsent:aBlock |
20164 | 234 |
] ensure:[ |
235 |
OperatingSystem unblockInterrupts. |
|
236 |
]. |
|
237 |
]. |
|
238 |
||
1341 | 239 |
^ ret. |
240 |
||
241 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1351 | 242 |
"Modified: 8.5.1996 / 14:54:09 / stefan" |
1341 | 243 |
! ! |
244 |
||
4349 | 245 |
!WeakValueDictionary methodsFor:'element disposal'! |
246 |
||
247 |
update:something with:aParameter from:changedObject |
|
248 |
"an element (either key or value) died - clear out slots for |
|
249 |
disposed keys." |
|
250 |
||
251 |
|wasBlocked| |
|
252 |
||
253 |
something == #ElementExpired ifTrue:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
254 |
self clearDeadSlots. |
4349 | 255 |
] |
256 |
||
257 |
"Created: 7.1.1997 / 16:59:30 / stefan" |
|
258 |
! ! |
|
259 |
||
20163 | 260 |
!WeakValueDictionary methodsFor:'enumerating'! |
261 |
||
262 |
do:aBlock |
|
263 |
super do:[:eachValue| |
|
20165 | 264 |
"garbage collected values will change to nil or a SmallInteger" |
265 |
(eachValue notNil and:[eachValue class ~~ SmallInteger]) ifTrue:[ |
|
20163 | 266 |
aBlock value:eachValue. |
267 |
]. |
|
268 |
]. |
|
269 |
! |
|
270 |
||
271 |
keysAndValuesDo:aBlock |
|
272 |
super keysAndValuesDo:[:eachKey :eachValue| |
|
273 |
"garbage collected values will change to a SmallInteger" |
|
274 |
eachValue class ~~ SmallInteger ifTrue:[ |
|
275 |
aBlock value:eachKey value:eachValue. |
|
276 |
]. |
|
277 |
]. |
|
278 |
! ! |
|
279 |
||
1341 | 280 |
!WeakValueDictionary methodsFor:'private'! |
281 |
||
6041 | 282 |
clearDeadSlots |
283 |
|wasBlocked| |
|
284 |
||
285 |
" |
|
286 |
have to block here - dispose may be done at a low priority |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
287 |
from the background finalizer. If new items are added by a |
6041 | 288 |
higher prio process, the dictionary might get corrupted otherwise |
289 |
" |
|
290 |
wasBlocked := OperatingSystem blockInterrupts. |
|
291 |
||
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
292 |
valueArray |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
293 |
forAllDeadIndicesDo:[:idx | keyArray at:idx put:DeletedEntry. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
294 |
tally := tally - 1. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
295 |
] |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
296 |
replacingCorpsesWith:nil. |
6041 | 297 |
|
298 |
wasBlocked ifFalse:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
299 |
OperatingSystem unblockInterrupts. |
6041 | 300 |
]. |
6330 | 301 |
|
302 |
"Modified: / 13.12.2001 / 14:18:56 / martin" |
|
6041 | 303 |
! |
304 |
||
25411
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
305 |
findKeyOrNilOrDeletedEntry:key |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
306 |
| index value | |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
307 |
|
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
308 |
(OperatingSystem blockInterrupts) ifFalse:[ |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
309 |
"/ |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
310 |
"/ may never be entered with interrupts enabled |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
311 |
"/ |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
312 |
OperatingSystem unblockInterrupts. |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
313 |
self error:'unblocked call of findKeyOrNil'. |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
314 |
]. |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
315 |
|
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
316 |
"/ It may happen that value could have been already |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
317 |
"/ collected by the GC but #clearDeadSlots have not yet |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
318 |
"/ been called. In that case, valueArray would contain |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
319 |
"/ a tombstone (a SmallInteger). |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
320 |
"/ |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
321 |
"/ So, we check whether the value contains tombstone |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
322 |
"/ and if so, clear all dead slots and restart. |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
323 |
[ |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
324 |
index := super findKeyOrNilOrDeletedEntry:key. |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
325 |
value := valueArray at: index. |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
326 |
value class == SmallInteger |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
327 |
] whileTrue: [ |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
328 |
self clearDeadSlots |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
329 |
]. |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
330 |
OperatingSystem unblockInterrupts. |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
331 |
|
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
332 |
^ index |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
333 |
|
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
334 |
"Created: / 25-03-2021 / 20:03:12 / Jan Vrany <jan.vrany@labware.com>" |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
335 |
! |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
336 |
|
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
337 |
possiblyShrink |
6041 | 338 |
"check if the receiver has become too empty (after a remove) |
339 |
and shrink if it makes sense. |
|
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
340 |
Definition of 'too empty' is: 'filled less than 12.5% (i.e. 1/8th)'" |
6041 | 341 |
|
20163 | 342 |
keyArray basicSize > 56 ifTrue:[ |
343 |
self clearDeadSlots. |
|
344 |
super possiblyShrink. |
|
345 |
]. |
|
6041 | 346 |
! |
347 |
||
1341 | 348 |
valueContainerOfSize:n |
349 |
"return a container for values of size n. |
|
350 |
use WeakArrays here." |
|
351 |
||
4349 | 352 |
|a| |
353 |
||
354 |
a := WeakArray new:n. |
|
355 |
a addDependent:self. |
|
356 |
^ a |
|
1341 | 357 |
|
358 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
359 |
! ! |
|
360 |
||
2739 | 361 |
!WeakValueDictionary methodsFor:'testing'! |
362 |
||
363 |
includes:anObject |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
364 |
"redefined to block interrupts |
2739 | 365 |
(avoid change of the dictionary while accessing)" |
366 |
||
20164 | 367 |
|ret wasBlocked| |
2739 | 368 |
|
20165 | 369 |
(anObject isNil or:[anObject class == SmallInteger]) ifTrue:[ |
20164 | 370 |
"Integers cannot be stored into a WeakValueDictionary" |
371 |
^ false. |
|
372 |
]. |
|
373 |
||
374 |
wasBlocked := OperatingSystem blockInterrupts. |
|
375 |
ret := super includes:anObject. |
|
376 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
377 |
^ ret |
|
2739 | 378 |
|
379 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
380 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
381 |
"Created: 1.7.1997 / 15:41:14 / cg" |
|
382 |
! |
|
383 |
||
20165 | 384 |
includesIdentical:anObject |
385 |
"redefined to block interrupts |
|
386 |
(avoid change of the dictionary while accessing)" |
|
387 |
||
388 |
|ret wasBlocked| |
|
389 |
||
390 |
(anObject isNil or:[anObject class == SmallInteger]) ifTrue:[ |
|
391 |
"Integers cannot be stored into a WeakValueDictionary" |
|
392 |
^ false. |
|
393 |
]. |
|
394 |
||
395 |
wasBlocked := OperatingSystem blockInterrupts. |
|
396 |
ret := super includesIdentical:anObject. |
|
397 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
398 |
^ ret |
|
399 |
! |
|
400 |
||
2739 | 401 |
includesKey:key |
20163 | 402 |
"redefined to check for already collected values" |
2739 | 403 |
|
20164 | 404 |
^ (self at:key ifAbsent:DeletedEntry) ~~ DeletedEntry. |
2739 | 405 |
|
406 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
407 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
408 |
"Created: 1.7.1997 / 15:41:32 / cg" |
|
9245 | 409 |
! |
410 |
||
411 |
isWeakCollection |
|
412 |
"return true, if the receiver has weak references to its elements." |
|
413 |
||
414 |
^ true |
|
2739 | 415 |
! ! |
416 |
||
1341 | 417 |
!WeakValueDictionary class methodsFor:'documentation'! |
418 |
||
419 |
version |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
420 |
^ '$Header$' |
25411
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
421 |
! |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
422 |
|
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
423 |
version_HG |
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
424 |
|
248600ba8fd9
Fix unlikely but possible race in `WeakValueDictionary`
Jan Vrany <jan.vrany@labware.com>
parents:
20205
diff
changeset
|
425 |
^ '$Changeset: <not expanded> $' |
1341 | 426 |
! ! |
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
427 |