author | Stefan Vogel <sv@exept.de> |
Tue, 07 Feb 2017 22:24:49 +0100 | |
changeset 4302 | f50a1263f3ce |
parent 4291 | a67c299b4c60 |
child 4336 | f330feaa0125 |
permissions | -rw-r--r-- |
1632 | 1 |
" |
2 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
"{ Package: 'stx:libbasic2' }" |
|
13 |
||
3860 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1632 | 16 |
Collection subclass:#SharedCollection |
17 |
instanceVariableNames:'accessLock realCollection' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
20 |
category:'Collections-Support' |
1632 | 21 |
! |
22 |
||
23 |
!SharedCollection class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
Instances of this class provide synchronized access (of multiple processes) |
|
42 |
to a collection. |
|
1636 | 43 |
|
4148 | 44 |
Notice: |
45 |
the message-forwarding is done by catching subclassResponsibility and |
|
46 |
doesNotUnderstand errors. |
|
47 |
||
1636 | 48 |
For performance, and for more complex operation-atomicy, more messages might need |
4148 | 49 |
an explicit handling. |
50 |
See the implementation of #at: / #at:put: and #size for examples. |
|
51 |
||
52 |
[auhor:] |
|
53 |
Claus Gittinger |
|
54 |
||
55 |
[see also:] |
|
56 |
Semaphore RecursionLock |
|
57 |
SharedQueue |
|
58 |
#synchronized: method in Object. |
|
1632 | 59 |
" |
60 |
! |
|
61 |
||
62 |
examples |
|
63 |
" |
|
64 |
[exBegin] |
|
65 |
|c| |
|
66 |
||
67 |
c := SharedCollection for:(OrderedCollection new). |
|
68 |
c add:1. |
|
69 |
c add:2. |
|
70 |
c add:3. |
|
71 |
c addAll:#(4 5 6). |
|
72 |
c removeFirst. |
|
73 |
c removeLast. |
|
74 |
c inspect. |
|
75 |
[exEnd] |
|
76 |
||
77 |
[exBegin] |
|
78 |
|c| |
|
79 |
||
80 |
c := SharedCollection for:(Array new:10). |
|
81 |
c at:1 put:5. |
|
82 |
c replaceFrom:2 to:5 with:#(20 30 40 50). |
|
83 |
c inspect. |
|
84 |
[exEnd] |
|
85 |
" |
|
86 |
! ! |
|
87 |
||
88 |
!SharedCollection class methodsFor:'instance creation'! |
|
89 |
||
90 |
for:aCollection |
|
91 |
^ self new initializeFor:aCollection |
|
92 |
! ! |
|
93 |
||
1634 | 94 |
!SharedCollection methodsFor:'converting'! |
95 |
||
96 |
asSharedCollection |
|
97 |
^ self. |
|
98 |
! ! |
|
99 |
||
3212 | 100 |
!SharedCollection methodsFor:'copying'! |
101 |
||
102 |
shallowCopy |
|
103 |
"analog to species - copy the real collection" |
|
104 |
||
4291 | 105 |
^ accessLock critical:[ |
3213 | 106 |
"get a consistent copy" |
4291 | 107 |
realCollection shallowCopy |
3213 | 108 |
]. |
4291 | 109 |
|
110 |
"Modified: / 02-02-2017 / 17:03:55 / stefan" |
|
3212 | 111 |
! ! |
112 |
||
1632 | 113 |
!SharedCollection methodsFor:'initialization'! |
114 |
||
115 |
initializeFor:aCollection |
|
116 |
accessLock := RecursionLock new. |
|
117 |
realCollection := aCollection. |
|
118 |
! ! |
|
119 |
||
120 |
!SharedCollection methodsFor:'message forwarding'! |
|
121 |
||
1929 | 122 |
add:anElement |
2927 | 123 |
"add the argument, anObject to the receiver. |
124 |
Return the added element." |
|
125 |
||
4291 | 126 |
^ accessLock critical:[ |
127 |
realCollection add:anElement |
|
128 |
]. |
|
1929 | 129 |
|
4291 | 130 |
"Modified: / 02-02-2017 / 17:01:08 / stefan" |
1929 | 131 |
! |
132 |
||
1632 | 133 |
at:index |
4291 | 134 |
^ accessLock critical:[ |
135 |
realCollection at:index |
|
136 |
]. |
|
1632 | 137 |
|
4291 | 138 |
"Modified: / 02-02-2017 / 17:01:23 / stefan" |
1632 | 139 |
! |
140 |
||
141 |
at:index put:value |
|
4291 | 142 |
^ accessLock critical:[ |
143 |
realCollection at:index put:value |
|
144 |
]. |
|
1632 | 145 |
|
4291 | 146 |
"Modified: / 02-02-2017 / 17:01:35 / stefan" |
1632 | 147 |
! |
148 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
149 |
do:aBlock |
4291 | 150 |
^ accessLock critical:[ |
151 |
realCollection do:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
152 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
153 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
154 |
"Created: / 22-11-2010 / 21:01:21 / cg" |
4291 | 155 |
"Modified: / 02-02-2017 / 17:02:00 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
156 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
157 |
|
1632 | 158 |
doesNotUnderstand:aMessage |
159 |
"catches everything not understood by the collection protocol" |
|
160 |
||
4291 | 161 |
^ accessLock critical:[ |
162 |
aMessage sendTo:realCollection |
|
1632 | 163 |
]. |
1794 | 164 |
|
165 |
"Modified: / 07-12-2006 / 17:38:30 / cg" |
|
4291 | 166 |
"Modified: / 02-02-2017 / 17:02:18 / stefan" |
1632 | 167 |
! |
168 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
169 |
isEmpty |
4291 | 170 |
^ accessLock critical:[ |
171 |
realCollection isEmpty |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
172 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
173 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
174 |
"Created: / 22-11-2010 / 20:59:01 / cg" |
4291 | 175 |
"Modified: / 02-02-2017 / 11:40:56 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
176 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
177 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
178 |
notEmpty |
4291 | 179 |
^ accessLock critical:[ |
180 |
realCollection notEmpty |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
181 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
182 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
183 |
"Created: / 22-11-2010 / 20:59:06 / cg" |
4291 | 184 |
"Modified: / 02-02-2017 / 17:00:36 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
185 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
186 |
|
4286 | 187 |
remove:someElement ifAbsent:aBlock |
4291 | 188 |
^ accessLock critical:[ |
189 |
realCollection remove:someElement ifAbsent:aBlock |
|
4286 | 190 |
]. |
191 |
||
192 |
"Created: / 25-01-2017 / 22:57:32 / stefan" |
|
4291 | 193 |
"Modified: / 02-02-2017 / 17:00:54 / stefan" |
4286 | 194 |
! |
195 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
196 |
removeAllSuchThat:aBlock |
4291 | 197 |
^ accessLock critical:[ |
198 |
realCollection removeAllSuchThat:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
199 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
200 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
201 |
"Created: / 22-11-2010 / 20:59:27 / cg" |
4291 | 202 |
"Modified: / 02-02-2017 / 17:02:38 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
203 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
204 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
205 |
removeIdentical:someElement ifAbsent:aBlock |
4291 | 206 |
^ accessLock critical:[ |
207 |
realCollection removeIdentical:someElement ifAbsent:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
208 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
209 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
210 |
"Created: / 22-11-2010 / 21:00:33 / cg" |
4291 | 211 |
"Modified: / 02-02-2017 / 17:02:52 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
212 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
213 |
|
1635 | 214 |
size |
4291 | 215 |
^ accessLock critical:[ |
216 |
realCollection size |
|
217 |
]. |
|
1635 | 218 |
|
4291 | 219 |
"Modified: / 02-02-2017 / 17:03:06 / stefan" |
1635 | 220 |
! |
221 |
||
1632 | 222 |
subclassResponsibility |
223 |
"catches every required message of the collection protocol" |
|
224 |
||
4291 | 225 |
|msg| |
1632 | 226 |
|
227 |
msg := thisContext sender message. |
|
4291 | 228 |
^ accessLock critical:[ |
229 |
msg sendTo:realCollection |
|
1632 | 230 |
]. |
4291 | 231 |
|
232 |
"Modified: / 02-02-2017 / 17:03:25 / stefan" |
|
1632 | 233 |
! ! |
234 |
||
3212 | 235 |
!SharedCollection methodsFor:'queries'! |
1637 | 236 |
|
237 |
species |
|
3860 | 238 |
"returns non shared collection's species" |
1637 | 239 |
|
240 |
^ realCollection species |
|
241 |
! ! |
|
242 |
||
3013 | 243 |
!SharedCollection methodsFor:'testing'! |
244 |
||
245 |
isFixedSize |
|
246 |
"return true if the receiver cannot grow" |
|
247 |
||
248 |
^ realCollection isFixedSize |
|
249 |
! ! |
|
250 |
||
1632 | 251 |
!SharedCollection class methodsFor:'documentation'! |
252 |
||
253 |
version |
|
3860 | 254 |
^ '$Header$' |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
255 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
256 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
257 |
version_CVS |
3860 | 258 |
^ '$Header$' |
1632 | 259 |
! ! |
2927 | 260 |