author | Claus Gittinger <cg@exept.de> |
Wed, 09 Aug 2017 14:12:50 +0200 | |
changeset 4499 | d163f325074d |
parent 4355 | 9d6595f00a6e |
child 4709 | b26d6c21d910 |
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 |
|
4336 | 91 |
"create and return a new shareCollection which protects |
92 |
access to aCollection. |
|
93 |
I.e. to return a threadSave accessor on it." |
|
94 |
||
1632 | 95 |
^ self new initializeFor:aCollection |
4336 | 96 |
|
97 |
"Modified (comment): / 18-02-2017 / 10:49:03 / cg" |
|
1632 | 98 |
! ! |
99 |
||
1634 | 100 |
!SharedCollection methodsFor:'converting'! |
101 |
||
102 |
asSharedCollection |
|
103 |
^ self. |
|
104 |
! ! |
|
105 |
||
3212 | 106 |
!SharedCollection methodsFor:'copying'! |
107 |
||
108 |
shallowCopy |
|
109 |
"analog to species - copy the real collection" |
|
110 |
||
4291 | 111 |
^ accessLock critical:[ |
3213 | 112 |
"get a consistent copy" |
4291 | 113 |
realCollection shallowCopy |
3213 | 114 |
]. |
4291 | 115 |
|
116 |
"Modified: / 02-02-2017 / 17:03:55 / stefan" |
|
3212 | 117 |
! ! |
118 |
||
1632 | 119 |
!SharedCollection methodsFor:'initialization'! |
120 |
||
121 |
initializeFor:aCollection |
|
4499 | 122 |
accessLock := RecursionLock name:'SharedCollection'. |
1632 | 123 |
realCollection := aCollection. |
4355 | 124 |
|
125 |
"Modified: / 23-02-2017 / 12:41:45 / stefan" |
|
4499 | 126 |
"Modified: / 09-08-2017 / 11:59:29 / cg" |
1632 | 127 |
! ! |
128 |
||
129 |
!SharedCollection methodsFor:'message forwarding'! |
|
130 |
||
1929 | 131 |
add:anElement |
2927 | 132 |
"add the argument, anObject to the receiver. |
133 |
Return the added element." |
|
134 |
||
4291 | 135 |
^ accessLock critical:[ |
136 |
realCollection add:anElement |
|
137 |
]. |
|
1929 | 138 |
|
4291 | 139 |
"Modified: / 02-02-2017 / 17:01:08 / stefan" |
1929 | 140 |
! |
141 |
||
1632 | 142 |
at:index |
4291 | 143 |
^ accessLock critical:[ |
144 |
realCollection at:index |
|
145 |
]. |
|
1632 | 146 |
|
4291 | 147 |
"Modified: / 02-02-2017 / 17:01:23 / stefan" |
1632 | 148 |
! |
149 |
||
150 |
at:index put:value |
|
4291 | 151 |
^ accessLock critical:[ |
152 |
realCollection at:index put:value |
|
153 |
]. |
|
1632 | 154 |
|
4291 | 155 |
"Modified: / 02-02-2017 / 17:01:35 / stefan" |
1632 | 156 |
! |
157 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
158 |
do:aBlock |
4291 | 159 |
^ accessLock critical:[ |
160 |
realCollection do:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
161 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
162 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
163 |
"Created: / 22-11-2010 / 21:01:21 / cg" |
4291 | 164 |
"Modified: / 02-02-2017 / 17:02:00 / stefan" |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
165 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
166 |
|
1632 | 167 |
doesNotUnderstand:aMessage |
168 |
"catches everything not understood by the collection protocol" |
|
169 |
||
4291 | 170 |
^ accessLock critical:[ |
171 |
aMessage sendTo:realCollection |
|
1632 | 172 |
]. |
1794 | 173 |
|
174 |
"Modified: / 07-12-2006 / 17:38:30 / cg" |
|
4291 | 175 |
"Modified: / 02-02-2017 / 17:02:18 / stefan" |
1632 | 176 |
! |
177 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
178 |
isEmpty |
4291 | 179 |
^ accessLock critical:[ |
180 |
realCollection isEmpty |
|
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:01 / cg" |
4291 | 184 |
"Modified: / 02-02-2017 / 11:40:56 / 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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
187 |
notEmpty |
4291 | 188 |
^ accessLock critical:[ |
189 |
realCollection notEmpty |
|
2515
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
192 |
"Created: / 22-11-2010 / 20:59:06 / cg" |
4291 | 193 |
"Modified: / 02-02-2017 / 17:00:36 / stefan" |
2515
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 |
|
4286 | 196 |
remove:someElement ifAbsent:aBlock |
4291 | 197 |
^ accessLock critical:[ |
198 |
realCollection remove:someElement ifAbsent:aBlock |
|
4286 | 199 |
]. |
200 |
||
201 |
"Created: / 25-01-2017 / 22:57:32 / stefan" |
|
4291 | 202 |
"Modified: / 02-02-2017 / 17:00:54 / stefan" |
4286 | 203 |
! |
204 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
205 |
removeAllSuchThat:aBlock |
4291 | 206 |
^ accessLock critical:[ |
207 |
realCollection removeAllSuchThat: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 / 20:59:27 / cg" |
4291 | 211 |
"Modified: / 02-02-2017 / 17:02:38 / 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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
214 |
removeIdentical:someElement ifAbsent:aBlock |
4291 | 215 |
^ accessLock critical:[ |
216 |
realCollection removeIdentical:someElement ifAbsent:aBlock |
|
2515
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
219 |
"Created: / 22-11-2010 / 21:00:33 / cg" |
4291 | 220 |
"Modified: / 02-02-2017 / 17:02:52 / stefan" |
2515
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 |
|
1635 | 223 |
size |
4291 | 224 |
^ accessLock critical:[ |
225 |
realCollection size |
|
226 |
]. |
|
1635 | 227 |
|
4291 | 228 |
"Modified: / 02-02-2017 / 17:03:06 / stefan" |
1635 | 229 |
! |
230 |
||
1632 | 231 |
subclassResponsibility |
232 |
"catches every required message of the collection protocol" |
|
233 |
||
4291 | 234 |
|msg| |
1632 | 235 |
|
236 |
msg := thisContext sender message. |
|
4291 | 237 |
^ accessLock critical:[ |
238 |
msg sendTo:realCollection |
|
1632 | 239 |
]. |
4291 | 240 |
|
241 |
"Modified: / 02-02-2017 / 17:03:25 / stefan" |
|
1632 | 242 |
! ! |
243 |
||
3212 | 244 |
!SharedCollection methodsFor:'queries'! |
1637 | 245 |
|
246 |
species |
|
3860 | 247 |
"returns non shared collection's species" |
1637 | 248 |
|
249 |
^ realCollection species |
|
250 |
! ! |
|
251 |
||
3013 | 252 |
!SharedCollection methodsFor:'testing'! |
253 |
||
254 |
isFixedSize |
|
255 |
"return true if the receiver cannot grow" |
|
256 |
||
257 |
^ realCollection isFixedSize |
|
258 |
! ! |
|
259 |
||
1632 | 260 |
!SharedCollection class methodsFor:'documentation'! |
261 |
||
262 |
version |
|
3860 | 263 |
^ '$Header$' |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
264 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
265 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
266 |
version_CVS |
3860 | 267 |
^ '$Header$' |
1632 | 268 |
! ! |
2927 | 269 |