1257
|
1 |
ValueHolder subclass:#ValueHolderWithWeakDependents
|
|
2 |
instanceVariableNames:''
|
|
3 |
classVariableNames:''
|
|
4 |
poolDictionaries:''
|
|
5 |
category:'Interface-Support-Models'
|
|
6 |
!
|
|
7 |
|
|
8 |
|
|
9 |
!ValueHolderWithWeakDependents methodsFor:'dependents access'!
|
|
10 |
|
|
11 |
addDependent:anObject
|
|
12 |
"make the argument, anObject be a weak dependent of the receiver"
|
|
13 |
|
|
14 |
|wasBlocked|
|
|
15 |
|
|
16 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
17 |
[
|
|
18 |
|deps dep|
|
|
19 |
|
|
20 |
deps := dependents.
|
|
21 |
"/
|
|
22 |
"/ store the very first dependent directly in
|
|
23 |
"/ the dependents instVar
|
|
24 |
"/
|
|
25 |
(deps isNil and:[anObject isCollection not]) ifTrue:[
|
|
26 |
dependents := WeakArray with:anObject
|
|
27 |
] ifFalse:[
|
|
28 |
deps class == WeakArray ifTrue:[
|
|
29 |
dep := deps at:1.
|
|
30 |
dep ~~ anObject ifTrue:[
|
|
31 |
(dep isNil or:[dep == 0]) ifTrue:[
|
|
32 |
deps at:1 put:anObject
|
|
33 |
] ifFalse:[
|
|
34 |
self dependents:(WeakIdentitySet with:dep with:anObject)
|
|
35 |
]
|
|
36 |
]
|
|
37 |
] ifFalse:[
|
|
38 |
deps add:anObject
|
|
39 |
]
|
|
40 |
]
|
|
41 |
] valueNowOrOnUnwindDo:[
|
|
42 |
wasBlocked ifFalse:[
|
|
43 |
OperatingSystem unblockInterrupts
|
|
44 |
]
|
|
45 |
]
|
|
46 |
|
|
47 |
"Modified: 8.1.1997 / 23:40:30 / cg"
|
|
48 |
|
|
49 |
|
|
50 |
!
|
|
51 |
|
|
52 |
dependentsDo:aBlock
|
|
53 |
"evaluate aBlock for all of my dependents"
|
|
54 |
|
|
55 |
|deps|
|
|
56 |
|
|
57 |
deps := dependents.
|
|
58 |
deps size ~~ 0 ifTrue:[
|
|
59 |
deps do:[:d |
|
|
60 |
(d notNil and:[d ~~ 0]) ifTrue:[
|
|
61 |
aBlock value:d
|
|
62 |
]
|
|
63 |
]
|
|
64 |
].
|
|
65 |
|
|
66 |
!
|
|
67 |
|
|
68 |
removeDependent:anObject
|
|
69 |
"make the argument, anObject be independent of the receiver"
|
|
70 |
|
|
71 |
|wasBlocked|
|
|
72 |
|
|
73 |
"/ must do this save from interrupts, since the dependents collection
|
|
74 |
"/ is possibly accessed from multiple threads.
|
|
75 |
"/ Used to use #valueUninterruptably here; inlined that code for slightly
|
|
76 |
"/ faster execution.
|
|
77 |
|
|
78 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
79 |
[
|
|
80 |
|deps n d|
|
|
81 |
|
|
82 |
deps := dependents.
|
|
83 |
deps size ~~ 0 ifTrue:[
|
|
84 |
|
|
85 |
"/ to save a fair amount of memory in case of
|
|
86 |
"/ many dependencies, we store a single dependent in
|
|
87 |
"/ a WeakArray, and switch to a WeakSet if more dependents are
|
|
88 |
"/ added. Here we have to do the inverse ...
|
|
89 |
|
|
90 |
deps class == WeakArray ifTrue:[
|
|
91 |
((d := deps at:1) == anObject
|
|
92 |
or:[d isNil
|
|
93 |
or:[d == 0]]) ifTrue:[
|
|
94 |
dependents := nil
|
|
95 |
]
|
|
96 |
] ifFalse:[
|
|
97 |
deps remove:anObject ifAbsent:[].
|
|
98 |
(n := deps size) == 0 ifTrue:[
|
|
99 |
dependents := nil
|
|
100 |
] ifFalse:[
|
|
101 |
n == 1 ifTrue:[
|
|
102 |
d := deps firstIfEmpty:nil.
|
|
103 |
d notNil ifTrue:[
|
|
104 |
deps := WeakArray with:d
|
|
105 |
] ifFalse:[
|
|
106 |
deps := nil
|
|
107 |
].
|
|
108 |
dependents := deps.
|
|
109 |
]
|
|
110 |
]
|
|
111 |
]
|
|
112 |
]
|
|
113 |
] valueNowOrOnUnwindDo:[
|
|
114 |
wasBlocked ifFalse:[
|
|
115 |
OperatingSystem unblockInterrupts
|
|
116 |
]
|
|
117 |
]
|
|
118 |
|
|
119 |
"Modified: / 26.1.1998 / 19:51:50 / cg"
|
|
120 |
|
|
121 |
! !
|
|
122 |
|
|
123 |
!ValueHolderWithWeakDependents class methodsFor:'documentation'!
|
|
124 |
|
|
125 |
version
|
|
126 |
^ '$Header: /cvs/stx/stx/libview2/ValueHolderWithWeakDependents.st,v 1.1 1999-10-25 14:52:53 cg Exp $'
|
|
127 |
! !
|