author | Claus Gittinger <cg@exept.de> |
Mon, 18 Mar 2019 11:03:24 +0100 | |
changeset 4881 | bdff8a3ce9d6 |
parent 4710 | 6a37e4e17f25 |
child 4940 | d5d7391bc875 |
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 |
||
104 |
synchronizationSemaphore |
|
105 |
^ accessLock |
|
106 |
! ! |
|
107 |
||
1634 | 108 |
!SharedCollection methodsFor:'converting'! |
109 |
||
110 |
asSharedCollection |
|
111 |
^ self. |
|
112 |
! ! |
|
113 |
||
3212 | 114 |
!SharedCollection methodsFor:'copying'! |
115 |
||
116 |
shallowCopy |
|
117 |
"analog to species - copy the real collection" |
|
118 |
||
4291 | 119 |
^ accessLock critical:[ |
3213 | 120 |
"get a consistent copy" |
4291 | 121 |
realCollection shallowCopy |
3213 | 122 |
]. |
4291 | 123 |
|
124 |
"Modified: / 02-02-2017 / 17:03:55 / stefan" |
|
3212 | 125 |
! ! |
126 |
||
1632 | 127 |
!SharedCollection methodsFor:'initialization'! |
128 |
||
129 |
initializeFor:aCollection |
|
4499 | 130 |
accessLock := RecursionLock name:'SharedCollection'. |
1632 | 131 |
realCollection := aCollection. |
4355 | 132 |
|
133 |
"Modified: / 23-02-2017 / 12:41:45 / stefan" |
|
4499 | 134 |
"Modified: / 09-08-2017 / 11:59:29 / cg" |
1632 | 135 |
! ! |
136 |
||
137 |
!SharedCollection methodsFor:'message forwarding'! |
|
138 |
||
1929 | 139 |
add:anElement |
2927 | 140 |
"add the argument, anObject to the receiver. |
141 |
Return the added element." |
|
142 |
||
4291 | 143 |
^ accessLock critical:[ |
144 |
realCollection add:anElement |
|
145 |
]. |
|
1929 | 146 |
|
4291 | 147 |
"Modified: / 02-02-2017 / 17:01:08 / stefan" |
1929 | 148 |
! |
149 |
||
1632 | 150 |
at:index |
4291 | 151 |
^ accessLock critical:[ |
152 |
realCollection at:index |
|
153 |
]. |
|
1632 | 154 |
|
4291 | 155 |
"Modified: / 02-02-2017 / 17:01:23 / stefan" |
1632 | 156 |
! |
157 |
||
158 |
at:index put:value |
|
4291 | 159 |
^ accessLock critical:[ |
160 |
realCollection at:index put:value |
|
161 |
]. |
|
1632 | 162 |
|
4291 | 163 |
"Modified: / 02-02-2017 / 17:01:35 / stefan" |
1632 | 164 |
! |
165 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
166 |
do:aBlock |
4291 | 167 |
^ accessLock critical:[ |
168 |
realCollection do:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
169 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
170 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
171 |
"Created: / 22-11-2010 / 21:01:21 / cg" |
4291 | 172 |
"Modified: / 02-02-2017 / 17:02:00 / stefan" |
2515
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 |
|
1632 | 175 |
doesNotUnderstand:aMessage |
176 |
"catches everything not understood by the collection protocol" |
|
177 |
||
4291 | 178 |
^ accessLock critical:[ |
179 |
aMessage sendTo:realCollection |
|
1632 | 180 |
]. |
1794 | 181 |
|
182 |
"Modified: / 07-12-2006 / 17:38:30 / cg" |
|
4291 | 183 |
"Modified: / 02-02-2017 / 17:02:18 / stefan" |
1632 | 184 |
! |
185 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
186 |
isEmpty |
4291 | 187 |
^ accessLock critical:[ |
188 |
realCollection isEmpty |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
189 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
190 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
191 |
"Created: / 22-11-2010 / 20:59:01 / cg" |
4291 | 192 |
"Modified: / 02-02-2017 / 11:40:56 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
193 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
194 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
195 |
notEmpty |
4291 | 196 |
^ accessLock critical:[ |
197 |
realCollection notEmpty |
|
2515
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
200 |
"Created: / 22-11-2010 / 20:59:06 / cg" |
4291 | 201 |
"Modified: / 02-02-2017 / 17:00:36 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
202 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
203 |
|
4286 | 204 |
remove:someElement ifAbsent:aBlock |
4291 | 205 |
^ accessLock critical:[ |
206 |
realCollection remove:someElement ifAbsent:aBlock |
|
4286 | 207 |
]. |
208 |
||
209 |
"Created: / 25-01-2017 / 22:57:32 / stefan" |
|
4291 | 210 |
"Modified: / 02-02-2017 / 17:00:54 / stefan" |
4286 | 211 |
! |
212 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
213 |
removeAllSuchThat:aBlock |
4291 | 214 |
^ accessLock critical:[ |
215 |
realCollection removeAllSuchThat:aBlock |
|
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
218 |
"Created: / 22-11-2010 / 20:59:27 / cg" |
4291 | 219 |
"Modified: / 02-02-2017 / 17:02:38 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
220 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
221 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
222 |
removeIdentical:someElement ifAbsent:aBlock |
4291 | 223 |
^ accessLock critical:[ |
224 |
realCollection removeIdentical:someElement ifAbsent:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
225 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
226 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
227 |
"Created: / 22-11-2010 / 21:00:33 / cg" |
4291 | 228 |
"Modified: / 02-02-2017 / 17:02:52 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
229 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
230 |
|
1635 | 231 |
size |
4291 | 232 |
^ accessLock critical:[ |
233 |
realCollection size |
|
234 |
]. |
|
1635 | 235 |
|
4291 | 236 |
"Modified: / 02-02-2017 / 17:03:06 / stefan" |
1635 | 237 |
! |
238 |
||
1632 | 239 |
subclassResponsibility |
240 |
"catches every required message of the collection protocol" |
|
241 |
||
4291 | 242 |
|msg| |
1632 | 243 |
|
244 |
msg := thisContext sender message. |
|
4291 | 245 |
^ accessLock critical:[ |
246 |
msg sendTo:realCollection |
|
1632 | 247 |
]. |
4291 | 248 |
|
249 |
"Modified: / 02-02-2017 / 17:03:25 / stefan" |
|
1632 | 250 |
! ! |
251 |
||
3212 | 252 |
!SharedCollection methodsFor:'queries'! |
1637 | 253 |
|
254 |
species |
|
3860 | 255 |
"returns non shared collection's species" |
1637 | 256 |
|
257 |
^ realCollection species |
|
258 |
! ! |
|
259 |
||
3013 | 260 |
!SharedCollection methodsFor:'testing'! |
261 |
||
262 |
isFixedSize |
|
263 |
"return true if the receiver cannot grow" |
|
264 |
||
265 |
^ realCollection isFixedSize |
|
266 |
! ! |
|
267 |
||
1632 | 268 |
!SharedCollection class methodsFor:'documentation'! |
269 |
||
270 |
version |
|
3860 | 271 |
^ '$Header$' |
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 |
version_CVS |
3860 | 275 |
^ '$Header$' |
1632 | 276 |
! ! |
2927 | 277 |