author | Claus Gittinger <cg@exept.de> |
Sat, 18 Feb 2017 10:49:20 +0100 | |
changeset 4336 | f330feaa0125 |
parent 4291 | a67c299b4c60 |
child 4355 | 9d6595f00a6e |
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 |
|
122 |
accessLock := RecursionLock new. |
|
123 |
realCollection := aCollection. |
|
124 |
! ! |
|
125 |
||
126 |
!SharedCollection methodsFor:'message forwarding'! |
|
127 |
||
1929 | 128 |
add:anElement |
2927 | 129 |
"add the argument, anObject to the receiver. |
130 |
Return the added element." |
|
131 |
||
4291 | 132 |
^ accessLock critical:[ |
133 |
realCollection add:anElement |
|
134 |
]. |
|
1929 | 135 |
|
4291 | 136 |
"Modified: / 02-02-2017 / 17:01:08 / stefan" |
1929 | 137 |
! |
138 |
||
1632 | 139 |
at:index |
4291 | 140 |
^ accessLock critical:[ |
141 |
realCollection at:index |
|
142 |
]. |
|
1632 | 143 |
|
4291 | 144 |
"Modified: / 02-02-2017 / 17:01:23 / stefan" |
1632 | 145 |
! |
146 |
||
147 |
at:index put:value |
|
4291 | 148 |
^ accessLock critical:[ |
149 |
realCollection at:index put:value |
|
150 |
]. |
|
1632 | 151 |
|
4291 | 152 |
"Modified: / 02-02-2017 / 17:01:35 / stefan" |
1632 | 153 |
! |
154 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
155 |
do:aBlock |
4291 | 156 |
^ accessLock critical:[ |
157 |
realCollection do:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
158 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
159 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
160 |
"Created: / 22-11-2010 / 21:01:21 / cg" |
4291 | 161 |
"Modified: / 02-02-2017 / 17:02:00 / stefan" |
2515
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 |
|
1632 | 164 |
doesNotUnderstand:aMessage |
165 |
"catches everything not understood by the collection protocol" |
|
166 |
||
4291 | 167 |
^ accessLock critical:[ |
168 |
aMessage sendTo:realCollection |
|
1632 | 169 |
]. |
1794 | 170 |
|
171 |
"Modified: / 07-12-2006 / 17:38:30 / cg" |
|
4291 | 172 |
"Modified: / 02-02-2017 / 17:02:18 / stefan" |
1632 | 173 |
! |
174 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
175 |
isEmpty |
4291 | 176 |
^ accessLock critical:[ |
177 |
realCollection isEmpty |
|
2515
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
180 |
"Created: / 22-11-2010 / 20:59:01 / cg" |
4291 | 181 |
"Modified: / 02-02-2017 / 11:40:56 / stefan" |
2515
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
184 |
notEmpty |
4291 | 185 |
^ accessLock critical:[ |
186 |
realCollection notEmpty |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
187 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
188 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
189 |
"Created: / 22-11-2010 / 20:59:06 / cg" |
4291 | 190 |
"Modified: / 02-02-2017 / 17:00:36 / stefan" |
2515
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 |
|
4286 | 193 |
remove:someElement ifAbsent:aBlock |
4291 | 194 |
^ accessLock critical:[ |
195 |
realCollection remove:someElement ifAbsent:aBlock |
|
4286 | 196 |
]. |
197 |
||
198 |
"Created: / 25-01-2017 / 22:57:32 / stefan" |
|
4291 | 199 |
"Modified: / 02-02-2017 / 17:00:54 / stefan" |
4286 | 200 |
! |
201 |
||
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
202 |
removeAllSuchThat:aBlock |
4291 | 203 |
^ accessLock critical:[ |
204 |
realCollection removeAllSuchThat:aBlock |
|
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
205 |
]. |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
206 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
207 |
"Created: / 22-11-2010 / 20:59:27 / cg" |
4291 | 208 |
"Modified: / 02-02-2017 / 17:02:38 / stefan" |
2515
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 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
211 |
removeIdentical:someElement ifAbsent:aBlock |
4291 | 212 |
^ accessLock critical:[ |
213 |
realCollection removeIdentical:someElement ifAbsent:aBlock |
|
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 / 21:00:33 / cg" |
4291 | 217 |
"Modified: / 02-02-2017 / 17:02:52 / 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 |
|
1635 | 220 |
size |
4291 | 221 |
^ accessLock critical:[ |
222 |
realCollection size |
|
223 |
]. |
|
1635 | 224 |
|
4291 | 225 |
"Modified: / 02-02-2017 / 17:03:06 / stefan" |
1635 | 226 |
! |
227 |
||
1632 | 228 |
subclassResponsibility |
229 |
"catches every required message of the collection protocol" |
|
230 |
||
4291 | 231 |
|msg| |
1632 | 232 |
|
233 |
msg := thisContext sender message. |
|
4291 | 234 |
^ accessLock critical:[ |
235 |
msg sendTo:realCollection |
|
1632 | 236 |
]. |
4291 | 237 |
|
238 |
"Modified: / 02-02-2017 / 17:03:25 / stefan" |
|
1632 | 239 |
! ! |
240 |
||
3212 | 241 |
!SharedCollection methodsFor:'queries'! |
1637 | 242 |
|
243 |
species |
|
3860 | 244 |
"returns non shared collection's species" |
1637 | 245 |
|
246 |
^ realCollection species |
|
247 |
! ! |
|
248 |
||
3013 | 249 |
!SharedCollection methodsFor:'testing'! |
250 |
||
251 |
isFixedSize |
|
252 |
"return true if the receiver cannot grow" |
|
253 |
||
254 |
^ realCollection isFixedSize |
|
255 |
! ! |
|
256 |
||
1632 | 257 |
!SharedCollection class methodsFor:'documentation'! |
258 |
||
259 |
version |
|
3860 | 260 |
^ '$Header$' |
2515
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
261 |
! |
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
262 |
|
104080163067
more operations without dnu-overhead
Claus Gittinger <cg@exept.de>
parents:
1929
diff
changeset
|
263 |
version_CVS |
3860 | 264 |
^ '$Header$' |
1632 | 265 |
! ! |
2927 | 266 |