author | Claus Gittinger <cg@exept.de> |
Wed, 19 Mar 2003 11:06:54 +0100 | |
changeset 1718 | 7da6bd953eb9 |
parent 1557 | e5e78ee63670 |
child 1803 | d69cb54a3b56 |
permissions | -rw-r--r-- |
1557
e5e78ee63670
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
1257
diff
changeset
|
1 |
"{ Package: 'stx:libview2' }" |
e5e78ee63670
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
1257
diff
changeset
|
2 |
|
1257 | 3 |
ValueHolder subclass:#ValueHolderWithWeakDependents |
4 |
instanceVariableNames:'' |
|
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
7 |
category:'Interface-Support-Models' |
|
8 |
! |
|
9 |
||
10 |
||
11 |
!ValueHolderWithWeakDependents methodsFor:'dependents access'! |
|
12 |
||
13 |
addDependent:anObject |
|
14 |
"make the argument, anObject be a weak dependent of the receiver" |
|
15 |
||
16 |
|wasBlocked| |
|
17 |
||
18 |
wasBlocked := OperatingSystem blockInterrupts. |
|
19 |
[ |
|
20 |
|deps dep| |
|
21 |
||
22 |
deps := dependents. |
|
23 |
"/ |
|
24 |
"/ store the very first dependent directly in |
|
25 |
"/ the dependents instVar |
|
26 |
"/ |
|
27 |
(deps isNil and:[anObject isCollection not]) ifTrue:[ |
|
28 |
dependents := WeakArray with:anObject |
|
29 |
] ifFalse:[ |
|
30 |
deps class == WeakArray ifTrue:[ |
|
31 |
dep := deps at:1. |
|
32 |
dep ~~ anObject ifTrue:[ |
|
33 |
(dep isNil or:[dep == 0]) ifTrue:[ |
|
34 |
deps at:1 put:anObject |
|
35 |
] ifFalse:[ |
|
36 |
self dependents:(WeakIdentitySet with:dep with:anObject) |
|
37 |
] |
|
38 |
] |
|
39 |
] ifFalse:[ |
|
40 |
deps add:anObject |
|
41 |
] |
|
42 |
] |
|
1557
e5e78ee63670
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
1257
diff
changeset
|
43 |
] ensure:[ |
1257 | 44 |
wasBlocked ifFalse:[ |
45 |
OperatingSystem unblockInterrupts |
|
46 |
] |
|
47 |
] |
|
48 |
||
49 |
"Modified: 8.1.1997 / 23:40:30 / cg" |
|
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 |
] |
|
1557
e5e78ee63670
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
1257
diff
changeset
|
113 |
] ensure:[ |
1257 | 114 |
wasBlocked ifFalse:[ |
115 |
OperatingSystem unblockInterrupts |
|
116 |
] |
|
117 |
] |
|
118 |
||
119 |
"Modified: / 26.1.1998 / 19:51:50 / cg" |
|
120 |
! ! |
|
121 |
||
122 |
!ValueHolderWithWeakDependents class methodsFor:'documentation'! |
|
123 |
||
124 |
version |
|
1557
e5e78ee63670
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
1257
diff
changeset
|
125 |
^ '$Header: /cvs/stx/stx/libview2/ValueHolderWithWeakDependents.st,v 1.2 2002-02-26 12:59:22 cg Exp $' |
1257 | 126 |
! ! |