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