author | Claus Gittinger <cg@exept.de> |
Mon, 06 May 2019 18:51:41 +0200 | |
changeset 4941 | 148b29bdda05 |
parent 4940 | d5d7391bc875 |
child 5138 | 0793a0374d2a |
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 |
" |
|
43 |
Instances of this class provide synchronized access (of multiple processes) |
|
44 |
to a collection. |
|
1636 | 45 |
|
4148 | 46 |
Notice: |
47 |
the message-forwarding is done by catching subclassResponsibility and |
|
48 |
doesNotUnderstand errors. |
|
49 |
||
1636 | 50 |
For performance, and for more complex operation-atomicy, more messages might need |
4148 | 51 |
an explicit handling. |
52 |
See the implementation of #at: / #at:put: and #size for examples. |
|
53 |
||
54 |
[auhor:] |
|
55 |
Claus Gittinger |
|
56 |
||
57 |
[see also:] |
|
58 |
Semaphore RecursionLock |
|
59 |
SharedQueue |
|
60 |
#synchronized: method in Object. |
|
1632 | 61 |
" |
62 |
! |
|
63 |
||
64 |
examples |
|
65 |
" |
|
66 |
[exBegin] |
|
67 |
|c| |
|
68 |
||
69 |
c := SharedCollection for:(OrderedCollection new). |
|
70 |
c add:1. |
|
71 |
c add:2. |
|
72 |
c add:3. |
|
73 |
c addAll:#(4 5 6). |
|
74 |
c removeFirst. |
|
75 |
c removeLast. |
|
76 |
c inspect. |
|
77 |
[exEnd] |
|
78 |
||
79 |
[exBegin] |
|
80 |
|c| |
|
81 |
||
82 |
c := SharedCollection for:(Array new:10). |
|
83 |
c at:1 put:5. |
|
84 |
c replaceFrom:2 to:5 with:#(20 30 40 50). |
|
85 |
c inspect. |
|
86 |
[exEnd] |
|
87 |
" |
|
88 |
! ! |
|
89 |
||
90 |
!SharedCollection class methodsFor:'instance creation'! |
|
91 |
||
92 |
for:aCollection |
|
4336 | 93 |
"create and return a new shareCollection which protects |
94 |
access to aCollection. |
|
95 |
I.e. to return a threadSave accessor on it." |
|
96 |
||
1632 | 97 |
^ self new initializeFor:aCollection |
4336 | 98 |
|
99 |
"Modified (comment): / 18-02-2017 / 10:49:03 / cg" |
|
1632 | 100 |
! ! |
101 |
||
4710 | 102 |
!SharedCollection methodsFor:'accessing'! |
103 |
||
4940 | 104 |
accessLock |
105 |
"for protocol compatibility with SharedQueue" |
|
106 |
||
107 |
^ accessLock |
|
108 |
||
109 |
"Created: / 04-05-2019 / 12:32:15 / Claus Gittinger" |
|
110 |
! |
|
111 |
||
4710 | 112 |
synchronizationSemaphore |
113 |
^ accessLock |
|
114 |
! ! |
|
115 |
||
1634 | 116 |
!SharedCollection methodsFor:'converting'! |
117 |
||
118 |
asSharedCollection |
|
119 |
^ self. |
|
120 |
! ! |
|
121 |
||
3212 | 122 |
!SharedCollection methodsFor:'copying'! |
123 |
||
124 |
shallowCopy |
|
125 |
"analog to species - copy the real collection" |
|
126 |
||
4291 | 127 |
^ accessLock critical:[ |
3213 | 128 |
"get a consistent copy" |
4291 | 129 |
realCollection shallowCopy |
3213 | 130 |
]. |
4291 | 131 |
|
132 |
"Modified: / 02-02-2017 / 17:03:55 / stefan" |
|
3212 | 133 |
! ! |
134 |
||
1632 | 135 |
!SharedCollection methodsFor:'initialization'! |
136 |
||
137 |
initializeFor:aCollection |
|
4499 | 138 |
accessLock := RecursionLock name:'SharedCollection'. |
1632 | 139 |
realCollection := aCollection. |
4355 | 140 |
|
141 |
"Modified: / 23-02-2017 / 12:41:45 / stefan" |
|
4499 | 142 |
"Modified: / 09-08-2017 / 11:59:29 / cg" |
1632 | 143 |
! ! |
144 |
||
145 |
!SharedCollection methodsFor:'message forwarding'! |
|
146 |
||
1929 | 147 |
add:anElement |
2927 | 148 |
"add the argument, anObject to the receiver. |
149 |
Return the added element." |
|
150 |
||
4291 | 151 |
^ accessLock critical:[ |
152 |
realCollection add:anElement |
|
153 |
]. |
|
1929 | 154 |
|
4291 | 155 |
"Modified: / 02-02-2017 / 17:01:08 / stefan" |
1929 | 156 |
! |
157 |
||
1632 | 158 |
at:index |
4291 | 159 |
^ accessLock critical:[ |
160 |
realCollection at:index |
|
161 |
]. |
|
1632 | 162 |
|
4291 | 163 |
"Modified: / 02-02-2017 / 17:01:23 / stefan" |
1632 | 164 |
! |
165 |
||
166 |
at:index put:value |
|
4291 | 167 |
^ accessLock critical:[ |
168 |
realCollection at:index put:value |
|
169 |
]. |
|
1632 | 170 |
|
4291 | 171 |
"Modified: / 02-02-2017 / 17:01:35 / stefan" |
1632 | 172 |
! |
173 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
174 |
do:aBlock |
4291 | 175 |
^ accessLock critical:[ |
176 |
realCollection do:aBlock |
|
2515
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
179 |
"Created: / 22-11-2010 / 21:01:21 / cg" |
4291 | 180 |
"Modified: / 02-02-2017 / 17:02:00 / stefan" |
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 |
|
1632 | 183 |
doesNotUnderstand:aMessage |
184 |
"catches everything not understood by the collection protocol" |
|
185 |
||
4291 | 186 |
^ accessLock critical:[ |
187 |
aMessage sendTo:realCollection |
|
1632 | 188 |
]. |
1794 | 189 |
|
190 |
"Modified: / 07-12-2006 / 17:38:30 / cg" |
|
4291 | 191 |
"Modified: / 02-02-2017 / 17:02:18 / stefan" |
1632 | 192 |
! |
193 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
194 |
isEmpty |
4291 | 195 |
^ accessLock critical:[ |
196 |
realCollection isEmpty |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
197 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
198 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
199 |
"Created: / 22-11-2010 / 20:59:01 / cg" |
4291 | 200 |
"Modified: / 02-02-2017 / 11:40:56 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
201 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
202 |
|
4941 | 203 |
last |
204 |
^ accessLock critical:[ |
|
205 |
realCollection last |
|
206 |
]. |
|
207 |
||
208 |
"Created: / 06-05-2019 / 18:51:26 / Claus Gittinger" |
|
209 |
! |
|
210 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
211 |
notEmpty |
4291 | 212 |
^ accessLock critical:[ |
213 |
realCollection notEmpty |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
214 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
215 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
216 |
"Created: / 22-11-2010 / 20:59:06 / cg" |
4291 | 217 |
"Modified: / 02-02-2017 / 17:00:36 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
218 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
219 |
|
4286 | 220 |
remove:someElement ifAbsent:aBlock |
4291 | 221 |
^ accessLock critical:[ |
222 |
realCollection remove:someElement ifAbsent:aBlock |
|
4286 | 223 |
]. |
224 |
||
225 |
"Created: / 25-01-2017 / 22:57:32 / stefan" |
|
4291 | 226 |
"Modified: / 02-02-2017 / 17:00:54 / stefan" |
4286 | 227 |
! |
228 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
229 |
removeAllSuchThat:aBlock |
4291 | 230 |
^ accessLock critical:[ |
231 |
realCollection removeAllSuchThat:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
232 |
]. |
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 |
"Created: / 22-11-2010 / 20:59:27 / cg" |
4291 | 235 |
"Modified: / 02-02-2017 / 17:02:38 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
236 |
! |
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 |
removeIdentical:someElement ifAbsent:aBlock |
4291 | 239 |
^ accessLock critical:[ |
240 |
realCollection removeIdentical:someElement ifAbsent:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
241 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
242 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
243 |
"Created: / 22-11-2010 / 21:00:33 / cg" |
4291 | 244 |
"Modified: / 02-02-2017 / 17:02:52 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
245 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
246 |
|
1635 | 247 |
size |
4291 | 248 |
^ accessLock critical:[ |
249 |
realCollection size |
|
250 |
]. |
|
1635 | 251 |
|
4291 | 252 |
"Modified: / 02-02-2017 / 17:03:06 / stefan" |
1635 | 253 |
! |
254 |
||
1632 | 255 |
subclassResponsibility |
256 |
"catches every required message of the collection protocol" |
|
257 |
||
4291 | 258 |
|msg| |
1632 | 259 |
|
260 |
msg := thisContext sender message. |
|
4291 | 261 |
^ accessLock critical:[ |
262 |
msg sendTo:realCollection |
|
1632 | 263 |
]. |
4291 | 264 |
|
265 |
"Modified: / 02-02-2017 / 17:03:25 / stefan" |
|
1632 | 266 |
! ! |
267 |
||
3212 | 268 |
!SharedCollection methodsFor:'queries'! |
1637 | 269 |
|
270 |
species |
|
3860 | 271 |
"returns non shared collection's species" |
1637 | 272 |
|
273 |
^ realCollection species |
|
274 |
! ! |
|
275 |
||
3013 | 276 |
!SharedCollection methodsFor:'testing'! |
277 |
||
278 |
isFixedSize |
|
279 |
"return true if the receiver cannot grow" |
|
280 |
||
281 |
^ realCollection isFixedSize |
|
282 |
! ! |
|
283 |
||
1632 | 284 |
!SharedCollection class methodsFor:'documentation'! |
285 |
||
286 |
version |
|
3860 | 287 |
^ '$Header$' |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
288 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
289 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
290 |
version_CVS |
3860 | 291 |
^ '$Header$' |
1632 | 292 |
! ! |
2927 | 293 |