author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2020 21:40:13 +0200 | |
changeset 5476 | 7355a4b11cb6 |
parent 5469 | d78065ee4cff |
permissions | -rw-r--r-- |
4709 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1632 | 3 |
" |
4 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
14 |
"{ Package: 'stx:libbasic2' }" |
|
15 |
||
3860 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1632 | 18 |
Collection subclass:#SharedCollection |
19 |
instanceVariableNames:'accessLock realCollection' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
22 |
category:'Collections-Support' |
1632 | 23 |
! |
24 |
||
25 |
!SharedCollection class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
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 |
" |
|
5138 | 43 |
Instances of this class provide synchronized access (of multiple processes) to a collection. |
44 |
Any message sent to instances are protected by an internal access lock, |
|
45 |
to prevent simultaneous access from different threads. |
|
1636 | 46 |
|
4148 | 47 |
Notice: |
48 |
the message-forwarding is done by catching subclassResponsibility and |
|
49 |
doesNotUnderstand errors. |
|
5138 | 50 |
For performance, and for more complex operation-atomicy, |
51 |
more messages might need an explicit handling. |
|
52 |
See the implementation of #at: / #at:put: and #size for examples. |
|
4148 | 53 |
|
54 |
[auhor:] |
|
55 |
Claus Gittinger |
|
56 |
||
57 |
[see also:] |
|
5138 | 58 |
Semaphore RecursionLock SharedQueue |
4148 | 59 |
#synchronized: method in Object. |
1632 | 60 |
" |
61 |
! |
|
62 |
||
63 |
examples |
|
64 |
" |
|
65 |
[exBegin] |
|
66 |
|c| |
|
67 |
||
68 |
c := SharedCollection for:(OrderedCollection new). |
|
69 |
c add:1. |
|
70 |
c add:2. |
|
71 |
c add:3. |
|
72 |
c addAll:#(4 5 6). |
|
73 |
c removeFirst. |
|
74 |
c removeLast. |
|
75 |
c inspect. |
|
76 |
[exEnd] |
|
77 |
||
78 |
[exBegin] |
|
79 |
|c| |
|
80 |
||
81 |
c := SharedCollection for:(Array new:10). |
|
82 |
c at:1 put:5. |
|
83 |
c replaceFrom:2 to:5 with:#(20 30 40 50). |
|
84 |
c inspect. |
|
85 |
[exEnd] |
|
86 |
" |
|
87 |
! ! |
|
88 |
||
89 |
!SharedCollection class methodsFor:'instance creation'! |
|
90 |
||
91 |
for:aCollection |
|
4336 | 92 |
"create and return a new shareCollection which protects |
93 |
access to aCollection. |
|
94 |
I.e. to return a threadSave accessor on it." |
|
95 |
||
1632 | 96 |
^ self new initializeFor:aCollection |
4336 | 97 |
|
98 |
"Modified (comment): / 18-02-2017 / 10:49:03 / cg" |
|
1632 | 99 |
! ! |
100 |
||
4710 | 101 |
!SharedCollection methodsFor:'accessing'! |
102 |
||
4940 | 103 |
accessLock |
5138 | 104 |
"returns the internal lock (an instance of RecursionLock). |
105 |
For protocol compatibility with SharedQueue" |
|
4940 | 106 |
|
107 |
^ accessLock |
|
108 |
||
109 |
"Created: / 04-05-2019 / 12:32:15 / Claus Gittinger" |
|
110 |
! |
|
111 |
||
4710 | 112 |
synchronizationSemaphore |
5138 | 113 |
"returns the internal lock (an instance of RecursionLock)" |
114 |
||
4710 | 115 |
^ accessLock |
116 |
! ! |
|
117 |
||
1634 | 118 |
!SharedCollection methodsFor:'converting'! |
119 |
||
120 |
asSharedCollection |
|
5138 | 121 |
"return a shared collection on the receiver. |
122 |
because the receiver is already synchronized, itself is returned." |
|
123 |
||
1634 | 124 |
^ self. |
125 |
! ! |
|
126 |
||
3212 | 127 |
!SharedCollection methodsFor:'copying'! |
128 |
||
129 |
shallowCopy |
|
130 |
"analog to species - copy the real collection" |
|
131 |
||
4291 | 132 |
^ accessLock critical:[ |
3213 | 133 |
"get a consistent copy" |
4291 | 134 |
realCollection shallowCopy |
3213 | 135 |
]. |
4291 | 136 |
|
137 |
"Modified: / 02-02-2017 / 17:03:55 / stefan" |
|
3212 | 138 |
! ! |
139 |
||
1632 | 140 |
!SharedCollection methodsFor:'initialization'! |
141 |
||
142 |
initializeFor:aCollection |
|
5138 | 143 |
"private; initializes the private access lock" |
144 |
||
4499 | 145 |
accessLock := RecursionLock name:'SharedCollection'. |
1632 | 146 |
realCollection := aCollection. |
4355 | 147 |
|
148 |
"Modified: / 23-02-2017 / 12:41:45 / stefan" |
|
4499 | 149 |
"Modified: / 09-08-2017 / 11:59:29 / cg" |
1632 | 150 |
! ! |
151 |
||
152 |
!SharedCollection methodsFor:'message forwarding'! |
|
153 |
||
1929 | 154 |
add:anElement |
2927 | 155 |
"add the argument, anObject to the receiver. |
156 |
Return the added element." |
|
157 |
||
4291 | 158 |
^ accessLock critical:[ |
159 |
realCollection add:anElement |
|
160 |
]. |
|
1929 | 161 |
|
4291 | 162 |
"Modified: / 02-02-2017 / 17:01:08 / stefan" |
1929 | 163 |
! |
164 |
||
5469 | 165 |
addFirst:anElement |
166 |
"add the argument, anObject to the front of the receiver. |
|
167 |
Return the added element." |
|
168 |
||
169 |
^ accessLock critical:[ |
|
170 |
realCollection addFirst:anElement |
|
171 |
]. |
|
172 |
||
173 |
"Created: / 19-03-2020 / 18:28:50 / Stefan Vogel" |
|
174 |
! |
|
175 |
||
176 |
addLast:anElement |
|
177 |
"add the argument, anObject to the end of the receiver. |
|
178 |
Return the added element." |
|
179 |
||
180 |
^ accessLock critical:[ |
|
181 |
realCollection addLast:anElement |
|
182 |
]. |
|
183 |
||
184 |
"Created: / 19-03-2020 / 18:28:03 / Stefan Vogel" |
|
185 |
! |
|
186 |
||
1632 | 187 |
at:index |
5138 | 188 |
"retrieve the element at index while locked" |
189 |
||
4291 | 190 |
^ accessLock critical:[ |
191 |
realCollection at:index |
|
192 |
]. |
|
1632 | 193 |
|
4291 | 194 |
"Modified: / 02-02-2017 / 17:01:23 / stefan" |
1632 | 195 |
! |
196 |
||
197 |
at:index put:value |
|
5138 | 198 |
"update the element at index while locked" |
199 |
||
4291 | 200 |
^ accessLock critical:[ |
201 |
realCollection at:index put:value |
|
202 |
]. |
|
1632 | 203 |
|
4291 | 204 |
"Modified: / 02-02-2017 / 17:01:35 / stefan" |
1632 | 205 |
! |
206 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
207 |
do:aBlock |
5138 | 208 |
"enumerate the elements while locked" |
209 |
||
5143 | 210 |
accessLock critical:[ |
4291 | 211 |
realCollection do:aBlock |
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
214 |
"Created: / 22-11-2010 / 21:01:21 / cg" |
4291 | 215 |
"Modified: / 02-02-2017 / 17:02:00 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
216 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
217 |
|
1632 | 218 |
doesNotUnderstand:aMessage |
5138 | 219 |
"catches everything not understood by the collection protocol, |
220 |
and forwards the message to the underlying collection while locked" |
|
1632 | 221 |
|
4291 | 222 |
^ accessLock critical:[ |
223 |
aMessage sendTo:realCollection |
|
1632 | 224 |
]. |
1794 | 225 |
|
226 |
"Modified: / 07-12-2006 / 17:38:30 / cg" |
|
4291 | 227 |
"Modified: / 02-02-2017 / 17:02:18 / stefan" |
1632 | 228 |
! |
229 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
230 |
isEmpty |
4291 | 231 |
^ accessLock critical:[ |
232 |
realCollection isEmpty |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
233 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
234 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
235 |
"Created: / 22-11-2010 / 20:59:01 / cg" |
4291 | 236 |
"Modified: / 02-02-2017 / 11:40:56 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
237 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
238 |
|
4941 | 239 |
last |
240 |
^ accessLock critical:[ |
|
241 |
realCollection last |
|
242 |
]. |
|
243 |
||
244 |
"Created: / 06-05-2019 / 18:51:26 / Claus Gittinger" |
|
245 |
! |
|
246 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
247 |
notEmpty |
4291 | 248 |
^ accessLock critical:[ |
249 |
realCollection notEmpty |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
250 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
251 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
252 |
"Created: / 22-11-2010 / 20:59:06 / cg" |
4291 | 253 |
"Modified: / 02-02-2017 / 17:00:36 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
254 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
255 |
|
4286 | 256 |
remove:someElement ifAbsent:aBlock |
4291 | 257 |
^ accessLock critical:[ |
258 |
realCollection remove:someElement ifAbsent:aBlock |
|
4286 | 259 |
]. |
260 |
||
261 |
"Created: / 25-01-2017 / 22:57:32 / stefan" |
|
4291 | 262 |
"Modified: / 02-02-2017 / 17:00:54 / stefan" |
4286 | 263 |
! |
264 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
265 |
removeAllSuchThat:aBlock |
4291 | 266 |
^ accessLock critical:[ |
267 |
realCollection removeAllSuchThat:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
268 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
269 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
270 |
"Created: / 22-11-2010 / 20:59:27 / cg" |
4291 | 271 |
"Modified: / 02-02-2017 / 17:02:38 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
272 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
273 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
274 |
removeIdentical:someElement ifAbsent:aBlock |
4291 | 275 |
^ accessLock critical:[ |
276 |
realCollection removeIdentical:someElement ifAbsent:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
277 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
278 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
279 |
"Created: / 22-11-2010 / 21:00:33 / cg" |
4291 | 280 |
"Modified: / 02-02-2017 / 17:02:52 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
281 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
282 |
|
1635 | 283 |
size |
4291 | 284 |
^ accessLock critical:[ |
285 |
realCollection size |
|
286 |
]. |
|
1635 | 287 |
|
4291 | 288 |
"Modified: / 02-02-2017 / 17:03:06 / stefan" |
1635 | 289 |
! |
290 |
||
1632 | 291 |
subclassResponsibility |
292 |
"catches every required message of the collection protocol" |
|
293 |
||
4291 | 294 |
|msg| |
1632 | 295 |
|
296 |
msg := thisContext sender message. |
|
4291 | 297 |
^ accessLock critical:[ |
298 |
msg sendTo:realCollection |
|
1632 | 299 |
]. |
4291 | 300 |
|
301 |
"Modified: / 02-02-2017 / 17:03:25 / stefan" |
|
1632 | 302 |
! ! |
303 |
||
3212 | 304 |
!SharedCollection methodsFor:'queries'! |
1637 | 305 |
|
306 |
species |
|
3860 | 307 |
"returns non shared collection's species" |
1637 | 308 |
|
309 |
^ realCollection species |
|
310 |
! ! |
|
311 |
||
3013 | 312 |
!SharedCollection methodsFor:'testing'! |
313 |
||
314 |
isFixedSize |
|
315 |
"return true if the receiver cannot grow" |
|
316 |
||
317 |
^ realCollection isFixedSize |
|
318 |
! ! |
|
319 |
||
1632 | 320 |
!SharedCollection class methodsFor:'documentation'! |
321 |
||
322 |
version |
|
3860 | 323 |
^ '$Header$' |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
324 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
325 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
326 |
version_CVS |
3860 | 327 |
^ '$Header$' |
1632 | 328 |
! ! |
2927 | 329 |