src/JavaMonitor.st
branchjk_new_structure
changeset 1145 8728f5373a48
parent 1144 c1c59b13340e
child 1146 e458dd16772e
--- a/src/JavaMonitor.st	Sun Nov 20 20:05:29 2011 +0000
+++ b/src/JavaMonitor.st	Tue Nov 22 10:12:20 2011 +0000
@@ -43,7 +43,7 @@
 
 Object subclass:#JavaMonitor
 	instanceVariableNames:'owningProcess processesEntered monitorSema processesEnteredAccess
-		owningProcessAccess'
+		owningProcessAccess count countAccess'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Languages-Java-Support'
@@ -137,14 +137,39 @@
     "Created: / 20-11-2011 / 20:34:27 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
+decrement
+    "owning process released monitor, lets lower our counter so we know how many times
+     do we have to release it"
+    
+    countAccess critical: [ count := count - 1 ].
+
+    "Created: / 22-11-2011 / 10:49:33 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+increment
+    "owning process entered monitor again (recursion ...), lets raise our counter so we know how many times
+    do we have to release it"
+    countAccess critical: [ count := count + 1 ].
+
+    "Created: / 22-11-2011 / 10:49:07 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
 owningProcess: aProcess
 owningProcessAccess critical: [owningProcess := aProcess].
 
     "Created: / 20-11-2011 / 20:32:26 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
-removeProcess: aProcess
-processesEnteredAccess critical: [processesEntered remove: aProcess].
+reinitCounter
+    "owning process is different from previous, lets start counting from beginning"
+    
+    countAccess critical: [ count := 1 ].
+
+    "Created: / 22-11-2011 / 10:51:09 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+removeProcess: aProcess 
+    processesEnteredAccess critical: [ self assert: processesEntered last == aProcess. processesEntered remove: aProcess ].
 
     "Created: / 20-11-2011 / 20:28:37 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 ! !
@@ -157,6 +182,7 @@
     processesEntered := OrderedCollection new.
     processesEnteredAccess := Semaphore forMutualExclusion.
     monitorSema := Semaphore new: 1.
+    countAccess := Semaphore forMutualExclusion.
 
     "Created: / 20-11-2011 / 13:28:28 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 ! !
@@ -166,9 +192,13 @@
 acquire
     | thisProcess |
     thisProcess := self activeProcess.
-    (self isOwnedBy: thisProcess) ifTrue: [ ^ self ].
+    (self isOwnedBy: thisProcess) ifTrue: [
+        self increment.
+        ^ self
+    ].
     monitorSema wait.
     self owningProcess: thisProcess.
+    self reinitCounter.
 
     "Created: / 20-11-2011 / 13:21:46 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
@@ -194,9 +224,13 @@
 release
     | thisProcess |
     thisProcess := self activeProcess.
-    self assert: thisProcess == owningProcess.
-    self clearOwningProcess.
-    monitorSema signal.
+    self assert: (self isOwnedBy: owningProcess).
+    count == 1 ifTrue: [
+        self clearOwningProcess.
+        monitorSema signal.
+    ] ifFalse: [
+        self decrement.       
+    ]
 
     "Created: / 20-11-2011 / 13:21:52 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 ! !