20 " |
20 " |
21 "{ Package: 'stx:libjava' }" |
21 "{ Package: 'stx:libjava' }" |
22 |
22 |
23 Object subclass:#JavaMonitor |
23 Object subclass:#JavaMonitor |
24 instanceVariableNames:'owningProcess processesEntered monitorSema count waitingSema |
24 instanceVariableNames:'owningProcess processesEntered monitorSema count waitingSema |
25 processesWaiting waitEnabled ownerPrintString instVarAccess' |
25 processesWaiting waitEnabled ownerPrintString' |
26 classVariableNames:'' |
26 classVariableNames:'instVarAccess' |
27 poolDictionaries:'' |
27 poolDictionaries:'' |
28 category:'Languages-Java-Support' |
28 category:'Languages-Java-Support' |
29 ! |
29 ! |
30 |
30 |
31 !JavaMonitor class methodsFor:'documentation'! |
31 !JavaMonitor class methodsFor:'documentation'! |
55 ! ! |
55 ! ! |
56 |
56 |
57 !JavaMonitor class methodsFor:'initialization'! |
57 !JavaMonitor class methodsFor:'initialization'! |
58 |
58 |
59 initialize |
59 initialize |
|
60 instVarAccess := RecursionLock forMutualExclusion. |
60 |
61 |
61 "Created: / 29-11-2011 / 11:23:56 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
62 "Created: / 29-11-2011 / 11:23:56 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
62 "Modified: / 28-08-2012 / 18:01:33 / m" |
|
63 ! ! |
63 ! ! |
64 |
64 |
65 !JavaMonitor class methodsFor:'instance creation'! |
65 !JavaMonitor class methodsFor:'instance creation'! |
66 |
66 |
67 for: owningObject |
67 for: owningObject |
68 ^ self basicNew initializeFor: owningObject. |
68 ^ self basicNew initializeFor: owningObject. |
69 |
69 |
70 "Created: / 30-11-2011 / 20:39:55 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
70 "Created: / 30-11-2011 / 20:39:55 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
71 ! |
|
72 |
|
73 for: owningObject thread: threadOrNil nestedLockCount: count |
|
74 ^ self basicNew initializeFor: owningObject thread: threadOrNil nestedLockCount: count |
|
75 |
|
76 "Created: / 26-08-2012 / 17:01:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
77 ! ! |
71 ! ! |
78 |
72 |
79 !JavaMonitor methodsFor:'accessing'! |
73 !JavaMonitor methodsFor:'accessing'! |
80 |
74 |
81 activeProcess |
75 activeProcess |
141 do we have to release it" |
131 do we have to release it" |
142 |
132 |
143 instVarAccess critical: [ count := count + 1 ]. |
133 instVarAccess critical: [ count := count + 1 ]. |
144 |
134 |
145 "Created: / 22-11-2011 / 10:49:07 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
135 "Created: / 22-11-2011 / 10:49:07 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
146 "Modified: / 27-08-2012 / 10:42:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
147 ! |
136 ! |
148 |
137 |
149 owningProcess: aProcess |
138 owningProcess: aProcess |
150 instVarAccess critical: [ owningProcess := aProcess ]. |
139 instVarAccess critical: [ owningProcess := aProcess ]. |
151 |
140 |
215 |
204 |
216 "Created: / 20-11-2011 / 13:28:28 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
205 "Created: / 20-11-2011 / 13:28:28 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
217 ! |
206 ! |
218 |
207 |
219 initializeFor: owningObject |
208 initializeFor: owningObject |
220 self initializeFor: owningObject thread: 0 nestedLockCount: 0. |
209 owningProcess := nil. |
221 |
|
222 "Created: / 30-11-2011 / 20:39:31 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
223 "Modified: / 26-08-2012 / 17:25:55 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
224 ! |
|
225 |
|
226 initializeFor: owningObject thread: threadId nestedLockCount: cnt |
|
227 "/ self assert: (owningObject isJavaObject or:[owningObject isJavaClass]). |
|
228 instVarAccess := RecursionLock forMutualExclusion. |
|
229 processesEntered := OrderedCollection new. |
210 processesEntered := OrderedCollection new. |
230 waitEnabled := true. |
211 monitorSema := Semaphore new: 1. |
231 processesWaiting := Dictionary new. |
212 processesWaiting := Dictionary new. |
232 waitingSema := Semaphore new: 0. |
213 waitingSema := Semaphore new: 0. |
233 ownerPrintString := owningObject class name , '@' |
214 waitEnabled := true. |
234 , owningObject identityHash printString. |
215 ownerPrintString := owningObject class name , '@' , owningObject identityHash printString. |
235 |
216 |
236 "/Not locked... |
217 "Created: / 30-11-2011 / 20:39:31 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
237 |
218 "Modified: / 10-05-2012 / 10:12:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
238 threadId == 0 ifTrue: [ |
|
239 monitorSema := Semaphore new: 1. |
|
240 count := 0. |
|
241 ^ self. |
|
242 ] ifFalse: [ |
|
243 "threadId is not zero (zero is threadId of scheduler process, which will never try to acquire the monitor)" |
|
244 "so it means it is possible that the thin lock is already locked for other thread, we must be careful " |
|
245 owningProcess := Process findProcessWithId: threadId. |
|
246 self assert: owningProcess notNil. |
|
247 processesEntered add: owningProcess. |
|
248 cnt timesRepeat: [ |
|
249 "/ JavaVM enteredMonitorsOf: owningProcess add: owningObject. |
|
250 "/ JavaVM acquiredMonitorsOf: owningProcess add: owningObject. |
|
251 ]. |
|
252 monitorSema := Semaphore new: 0. |
|
253 count := cnt. |
|
254 ]. |
|
255 |
|
256 "Created: / 26-08-2012 / 17:02:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
257 "Modified: / 28-08-2012 / 18:01:47 / m" |
|
258 ! ! |
219 ! ! |
259 |
220 |
260 !JavaMonitor methodsFor:'public'! |
221 !JavaMonitor methodsFor:'public'! |
261 |
222 |
262 acquire |
223 acquire |
288 thisProcess := self activeProcess. |
248 thisProcess := self activeProcess. |
289 (self isOwnedBy: thisProcess) ifTrue: [ self release. ]. |
249 (self isOwnedBy: thisProcess) ifTrue: [ self release. ]. |
290 processesEntered remove: thisProcess ifAbsent: nil. |
250 processesEntered remove: thisProcess ifAbsent: nil. |
291 |
251 |
292 "Created: / 20-11-2011 / 13:21:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
252 "Created: / 20-11-2011 / 13:21:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
293 "Modified: / 26-08-2012 / 19:54:14 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
294 ! |
253 ! |
295 |
254 |
296 notify |
255 notify |
297 "wakeup one waiting process" |
256 "wakeup one waiting process" |
298 |
257 |
331 | thisProcess | |
290 | thisProcess | |
332 thisProcess := self activeProcess. |
291 thisProcess := self activeProcess. |
333 self assert: (self isOwnedBy: owningProcess). |
292 self assert: (self isOwnedBy: owningProcess). |
334 count == 1 ifTrue: [ |
293 count == 1 ifTrue: [ |
335 self clearOwningProcess. |
294 self clearOwningProcess. |
336 count := 0. |
|
337 monitorSema signal. |
295 monitorSema signal. |
338 ] ifFalse: [ |
296 ] ifFalse: [ |
339 self decrement. |
297 self decrement. |
340 ] |
298 ] |
341 |
299 |
342 "Created: / 20-11-2011 / 13:21:52 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
300 "Created: / 20-11-2011 / 13:21:52 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
343 "Modified: / 27-08-2012 / 11:44:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
344 ! |
301 ! |
345 |
302 |
346 releaseAll |
303 releaseAll |
347 count timesRepeat: [self release]. |
304 count timesRepeat: [self release]. |
348 |
305 |