2798
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2012 by Claus Gittinger
|
|
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 |
|
|
14 |
Object subclass:#BoltLock
|
2799
|
15 |
instanceVariableNames:'state numReaders waitingProcesses name'
|
2801
|
16 |
classVariableNames:'StateFree StateLocked StateBusy StateWantLock'
|
2798
|
17 |
poolDictionaries:''
|
|
18 |
category:'Kernel-Processes'
|
|
19 |
!
|
|
20 |
|
|
21 |
!BoltLock class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 2012 by Claus Gittinger
|
|
26 |
All Rights Reserved
|
|
27 |
|
|
28 |
This software is furnished under a license and may be used
|
|
29 |
only in accordance with the terms of that license and with the
|
|
30 |
inclusion of the above copyright notice. This software may not
|
|
31 |
be provided or otherwise made available to, or used by, any
|
|
32 |
other person. No title to or ownership of the software is
|
|
33 |
hereby transferred.
|
|
34 |
"
|
|
35 |
!
|
|
36 |
|
|
37 |
documentation
|
|
38 |
"
|
3143
|
39 |
A BoltLock (aka BoltVariable) is a 3-state semaphore,
|
2798
|
40 |
useful to protect a resource against single writers, while allowing multiple
|
|
41 |
readers to access the resource simultaneously.
|
|
42 |
|
|
43 |
The lock is in one of 3 states:
|
|
44 |
A; free - can be changed by a writer into the locked state, or by a reader
|
|
45 |
into the busy state.
|
|
46 |
|
|
47 |
B; busy - can only be entered by another reader. left into free by last reader.
|
|
48 |
|
|
49 |
C; locked - a single writer has the lock; no other reader or writer is allowed
|
|
50 |
to aquire it
|
|
51 |
|
2801
|
52 |
[notice:]
|
3143
|
53 |
the original BoltLock was unfair in that new incoming readers could lock up a waiting writer.
|
2801
|
54 |
This has been fixed by adding a fourth state (wantToLock), which is set by a write-waiter to prevent
|
|
55 |
new readers from getting the lock.
|
2798
|
56 |
|
|
57 |
[instance variables:]
|
|
58 |
state <Symbol>
|
|
59 |
|
|
60 |
numReaders <Integer> number of readers holding onto the lock
|
|
61 |
|
|
62 |
waitingProcesses <OrderedCollection> waiting processes - will be served first
|
|
63 |
come first served when signalled.
|
|
64 |
|
|
65 |
name <String> a debugging aid: an optional userFriendly
|
|
66 |
name; helps to identify a semaphore easier.
|
|
67 |
|
|
68 |
[see also:]
|
|
69 |
Semaphore SemaphoreSet RecursionLock Monitor
|
|
70 |
SharedQueue Delay
|
|
71 |
Process ProcessorScheduler
|
|
72 |
|
|
73 |
[author:]
|
|
74 |
Claus Gittinger
|
|
75 |
"
|
|
76 |
!
|
|
77 |
|
|
78 |
examples
|
|
79 |
"
|
|
80 |
many processes synchronizing on a boltLock:
|
|
81 |
[exBegin]
|
2801
|
82 |
|lock readers readWriters currentWriter processes|
|
2798
|
83 |
|
|
84 |
lock := BoltLock new.
|
|
85 |
|
|
86 |
readWriters := (1 to:10) collect:[:tNo |
|
|
87 |
[
|
|
88 |
10 timesRepeat:[
|
|
89 |
(Random nextIntegerBetween:1 and:6) == 1 ifTrue:[
|
|
90 |
Transcript showCR:('thread %1: want to write...' bindWith:tNo).
|
|
91 |
lock waitForWrite.
|
2801
|
92 |
currentWriter notNil ifTrue:[Transcript showCR:('ouch %1: writer is %2' bindWith:tNo with:currentWriter).self halt].
|
|
93 |
currentWriter := tNo.
|
2798
|
94 |
Transcript showCR:('thread %1: **** write' bindWith:tNo).
|
|
95 |
Delay waitForSeconds:(Random nextIntegerBetween:1 and:4).
|
|
96 |
Transcript showCR:('thread %1: done writing.' bindWith:tNo).
|
2801
|
97 |
currentWriter := nil.
|
2798
|
98 |
lock release.
|
|
99 |
Delay waitForSeconds:(Random nextIntegerBetween:1 and:4).
|
|
100 |
] ifFalse:[
|
|
101 |
Transcript showCR:('thread %1: want to read...' bindWith:tNo).
|
|
102 |
lock waitForRead.
|
2801
|
103 |
currentWriter notNil ifTrue:[Transcript showCR:('ouch %1: writer is %2' bindWith:tNo with:currentWriter).self halt].
|
2798
|
104 |
Transcript showCR:('thread %1: ---- read' bindWith:tNo).
|
|
105 |
Delay waitForSeconds:(Random nextIntegerBetween:1 and:4).
|
|
106 |
Transcript showCR:('thread %1: done reading.' bindWith:tNo).
|
|
107 |
lock release.
|
|
108 |
Delay waitForSeconds:(Random nextIntegerBetween:1 and:4).
|
|
109 |
].
|
|
110 |
].
|
|
111 |
Transcript showCR:('thread %1: finished.').
|
|
112 |
] newProcess name:('rw%1' bindWith:tNo).
|
|
113 |
].
|
|
114 |
|
|
115 |
readers := (11 to:20) collect:[:tNo |
|
|
116 |
[
|
|
117 |
10 timesRepeat:[
|
|
118 |
Transcript showCR:('thread %1: want to read...' bindWith:tNo).
|
|
119 |
lock waitForRead.
|
2801
|
120 |
currentWriter notNil ifTrue:[Transcript showCR:('ouch %1: writer is %2' bindWith:tNo with:currentWriter).self halt].
|
2798
|
121 |
Transcript showCR:('thread %1: ---- read' bindWith:tNo).
|
|
122 |
Delay waitForSeconds:(Random nextIntegerBetween:1 and:4).
|
|
123 |
Transcript showCR:('thread %1: done.' bindWith:tNo).
|
|
124 |
lock release.
|
|
125 |
Delay waitForSeconds:(Random nextIntegerBetween:1 and:4).
|
|
126 |
].
|
|
127 |
Transcript showCR:('thread %1: finished.').
|
|
128 |
] newProcess name:('r%1' bindWith:tNo).
|
|
129 |
].
|
|
130 |
|
2801
|
131 |
processes := readWriters , readers.
|
2798
|
132 |
readWriters do:[:t | t resume].
|
|
133 |
readers do:[:t | t resume].
|
|
134 |
[exEnd]
|
|
135 |
"
|
|
136 |
! !
|
|
137 |
|
|
138 |
!BoltLock class methodsFor:'instance creation'!
|
|
139 |
|
|
140 |
new
|
|
141 |
"return an initialized instance"
|
|
142 |
|
|
143 |
^ self basicNew initialize.
|
|
144 |
! !
|
|
145 |
|
|
146 |
!BoltLock class methodsFor:'class initialization'!
|
|
147 |
|
|
148 |
initialize
|
|
149 |
StateFree := #free.
|
|
150 |
StateBusy := #busy.
|
|
151 |
StateLocked := #locked.
|
2801
|
152 |
StateWantLock := #wantLock.
|
2798
|
153 |
|
|
154 |
"Created: / 06-08-2012 / 15:49:44 / cg"
|
|
155 |
! !
|
|
156 |
|
|
157 |
!BoltLock methodsFor:'initialization'!
|
|
158 |
|
|
159 |
initialize
|
|
160 |
"Invoked when a new instance is created."
|
|
161 |
|
|
162 |
state := StateFree.
|
|
163 |
numReaders := 0.
|
|
164 |
waitingProcesses := OrderedCollection new.
|
|
165 |
|
|
166 |
"Modified: / 06-08-2012 / 16:03:06 / cg"
|
|
167 |
! !
|
|
168 |
|
|
169 |
!BoltLock methodsFor:'private'!
|
|
170 |
|
|
171 |
addWaitingProcess:aProcess
|
|
172 |
"add aProcess to the list of waiting processes.
|
|
173 |
all processes are ordered first-come-first-serve.
|
|
174 |
|
|
175 |
NOTE: must be called with blocked interrupts"
|
|
176 |
|
|
177 |
"for now"
|
|
178 |
waitingProcesses isNil ifTrue:[
|
|
179 |
waitingProcesses := Array with:aProcess
|
|
180 |
] ifFalse:[
|
|
181 |
waitingProcesses isArray ifTrue:[
|
|
182 |
waitingProcesses := OrderedCollection withAll:waitingProcesses.
|
|
183 |
].
|
|
184 |
waitingProcesses add:aProcess.
|
|
185 |
].
|
|
186 |
|
|
187 |
"Created: / 06-08-2012 / 16:05:38 / cg"
|
|
188 |
!
|
|
189 |
|
|
190 |
removeWaitingProcess:aProcess
|
|
191 |
"remove aProcess from the list of waiting processes
|
|
192 |
NO action if it is not in the list.
|
|
193 |
|
|
194 |
NOTE: must be called with blocked interrupts"
|
|
195 |
|
|
196 |
|nWaiting|
|
|
197 |
|
|
198 |
nWaiting := waitingProcesses size.
|
|
199 |
nWaiting == 0 ifTrue:[^ self].
|
|
200 |
|
|
201 |
nWaiting == 1 ifTrue:[
|
|
202 |
(waitingProcesses at:1) == aProcess ifTrue:[
|
|
203 |
waitingProcesses := nil.
|
|
204 |
].
|
|
205 |
^ self.
|
|
206 |
].
|
|
207 |
waitingProcesses removeIdentical:aProcess ifAbsent:[].
|
|
208 |
|
|
209 |
"Created: / 06-08-2012 / 16:06:00 / cg"
|
|
210 |
!
|
|
211 |
|
|
212 |
wakeupWaiters
|
|
213 |
"remove all waiting processes from the list of waiting processes
|
|
214 |
and resume them.
|
|
215 |
NOTE: Must be called when known that waitingProcesses is nonNil and
|
|
216 |
also with blocked interrupts"
|
|
217 |
|
|
218 |
|processes|
|
|
219 |
|
|
220 |
processes := waitingProcesses.
|
|
221 |
"/ do not set to nil - a waiting process may be suspended and will not be resumed...
|
|
222 |
"/ waitingProcesses := nil.
|
|
223 |
|
|
224 |
"/ todo: resume by priority; higher first.
|
|
225 |
processes do:[:eachProcess |
|
2802
|
226 |
"/ Transcript showCR:(' wakeup thread %1' bindWith:eachProcess name).
|
2798
|
227 |
Processor resume:eachProcess
|
|
228 |
].
|
|
229 |
|
|
230 |
"Created: / 06-08-2012 / 16:05:01 / cg"
|
|
231 |
! !
|
|
232 |
|
|
233 |
!BoltLock methodsFor:'waiting'!
|
|
234 |
|
|
235 |
release
|
|
236 |
"release the lock"
|
|
237 |
|
|
238 |
|activeProcess wasBlocked|
|
|
239 |
|
|
240 |
wasBlocked := OperatingSystem blockInterrupts.
|
2802
|
241 |
"/ Transcript showCR:' release in state ',state.
|
2798
|
242 |
activeProcess := Processor activeProcess.
|
|
243 |
state == StateLocked ifTrue:[
|
|
244 |
"I am the writer"
|
|
245 |
state := StateFree.
|
|
246 |
] ifFalse:[
|
|
247 |
"I am a reader"
|
|
248 |
numReaders := numReaders - 1.
|
|
249 |
numReaders == 0 ifTrue:[
|
|
250 |
state := StateFree.
|
|
251 |
]
|
|
252 |
].
|
|
253 |
state == StateFree ifTrue:[
|
|
254 |
waitingProcesses notEmptyOrNil ifTrue:[
|
|
255 |
self wakeupWaiters.
|
|
256 |
].
|
|
257 |
].
|
|
258 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
259 |
|
|
260 |
"Created: / 06-08-2012 / 16:00:04 / cg"
|
|
261 |
!
|
|
262 |
|
|
263 |
waitForRead
|
|
264 |
"wait for the lock in order to read"
|
|
265 |
|
|
266 |
|activeProcess wasBlocked|
|
|
267 |
|
|
268 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
269 |
|
2802
|
270 |
"/ Transcript showCR:' waitForRead in state ',state.
|
2801
|
271 |
(state == StateFree or:[state == StateBusy]) ifFalse:[
|
2798
|
272 |
"being written; wait until released"
|
|
273 |
activeProcess := Processor activeProcess.
|
|
274 |
[
|
|
275 |
self addWaitingProcess:activeProcess.
|
|
276 |
[
|
|
277 |
activeProcess suspendWithState:#waitForRead
|
|
278 |
] ifCurtailed:[
|
|
279 |
"interrupts are not blocked when entered through Processor>>#interruptActive"
|
|
280 |
OperatingSystem blockInterrupts.
|
|
281 |
self removeWaitingProcess:activeProcess.
|
|
282 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
283 |
].
|
|
284 |
self removeWaitingProcess:activeProcess.
|
2801
|
285 |
] doUntil:[ state == StateFree or:[state == StateBusy] ].
|
2798
|
286 |
].
|
|
287 |
|
|
288 |
numReaders := numReaders + 1.
|
|
289 |
state := StateBusy.
|
|
290 |
|
|
291 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
292 |
|
|
293 |
"Created: / 06-08-2012 / 15:49:08 / cg"
|
|
294 |
!
|
|
295 |
|
|
296 |
waitForWrite
|
|
297 |
"wait for the lock in order to read"
|
|
298 |
|
|
299 |
|activeProcess wasBlocked|
|
|
300 |
|
|
301 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
302 |
|
2802
|
303 |
"/ Transcript showCR:' waitForWrite in state ',state.
|
2798
|
304 |
state ~~ StateFree ifTrue:[
|
|
305 |
"being read or written"
|
2801
|
306 |
state == StateBusy ifTrue:[
|
|
307 |
"/ no new readers
|
|
308 |
state := StateWantLock
|
|
309 |
].
|
|
310 |
|
2798
|
311 |
activeProcess := Processor activeProcess.
|
|
312 |
[
|
|
313 |
self addWaitingProcess:activeProcess.
|
|
314 |
[
|
|
315 |
activeProcess suspendWithState:#waitForWrite
|
|
316 |
] ifCurtailed:[
|
|
317 |
"interrupts are not blocked when entered through Processor>>#interruptActive"
|
|
318 |
OperatingSystem blockInterrupts.
|
|
319 |
self removeWaitingProcess:activeProcess.
|
|
320 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
321 |
].
|
|
322 |
self removeWaitingProcess:activeProcess.
|
|
323 |
] doUntil:[ state == StateFree].
|
|
324 |
].
|
|
325 |
|
|
326 |
state := StateLocked.
|
|
327 |
|
|
328 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
329 |
|
|
330 |
"Created: / 06-08-2012 / 15:54:05 / cg"
|
|
331 |
! !
|
|
332 |
|
|
333 |
!BoltLock class methodsFor:'documentation'!
|
|
334 |
|
|
335 |
version
|
3143
|
336 |
^ '$Header: /cvs/stx/stx/libbasic2/BoltLock.st,v 1.5 2013-12-24 10:42:49 cg Exp $'
|
2798
|
337 |
!
|
|
338 |
|
|
339 |
version_CVS
|
3143
|
340 |
^ '$Header: /cvs/stx/stx/libbasic2/BoltLock.st,v 1.5 2013-12-24 10:42:49 cg Exp $'
|
2798
|
341 |
! !
|
|
342 |
|
3143
|
343 |
|
2798
|
344 |
BoltLock initialize!
|