src/JavaMonitor.st
branchjk_new_structure
changeset 1691 826f8d7dc0df
parent 1690 2dbce03c979a
--- a/src/JavaMonitor.st	Tue Aug 28 16:09:58 2012 +0000
+++ b/src/JavaMonitor.st	Mon Sep 03 14:57:38 2012 +0000
@@ -68,6 +68,12 @@
     ^ self basicNew initializeFor: owningObject.
 
     "Created: / 30-11-2011 / 20:39:55 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+for: owningObject thread: threadOrNil nestedLockCount: count
+    ^ self basicNew initializeFor: owningObject thread: threadOrNil nestedLockCount: count
+
+    "Created: / 26-08-2012 / 17:01:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaMonitor methodsFor:'accessing'!
@@ -82,6 +88,10 @@
     "Created: / 20-11-2011 / 14:45:24 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
+count
+    ^ count
+!
+
 processesEntered
     "dont do anything to me, access needs to be sync'd"
     ^ processesEntered.
@@ -133,6 +143,7 @@
     instVarAccess critical: [ count := count + 1 ].
 
     "Created: / 22-11-2011 / 10:49:07 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 27-08-2012 / 10:42:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 owningProcess: aProcess 
@@ -206,16 +217,43 @@
 !
 
 initializeFor: owningObject
-    owningProcess := nil.
+    self initializeFor: owningObject thread: 0 nestedLockCount: 0.
+
+    "Created: / 30-11-2011 / 20:39:31 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 26-08-2012 / 17:25:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+initializeFor:owningObject thread:threadId nestedLockCount:cnt 
+
+"/    self assert: (owningObject isJavaObject or:[owningObject isJavaClass]).
+
     processesEntered := OrderedCollection new.
-    monitorSema := Semaphore new: 1.
+    waitEnabled := true.
     processesWaiting := Dictionary new.
-    waitingSema := Semaphore new: 0.
-    waitEnabled := true.
+    waitingSema := Semaphore new:0.
     ownerPrintString := owningObject class name , '@' , owningObject identityHash printString.
 
-    "Created: / 30-11-2011 / 20:39:31 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
-    "Modified: / 10-05-2012 / 10:12:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "/Not locked...    
+    threadId == 0 ifTrue:[
+        monitorSema := Semaphore new:1.
+        count := 0.
+        ^self.
+    ] ifFalse: [
+        "threadId is not zero (zero is threadId of scheduler process, which will never try to acquire the monitor)"
+        "so it means it is possible that the thin lock is already locked for other thread, we must be careful " 
+
+        owningProcess := Process findProcessWithId:threadId.
+        self assert: owningProcess notNil.
+        processesEntered add: owningProcess.  
+        cnt timesRepeat:[
+"/            JavaVM enteredMonitorsOf:  owningProcess add: owningObject.
+"/            JavaVM acquiredMonitorsOf: owningProcess add: owningObject.
+        ].
+        monitorSema := Semaphore new: 0.
+        count := cnt.
+    ].
+
+    "Created: / 26-08-2012 / 17:02:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaMonitor methodsFor:'public'!
@@ -229,9 +267,10 @@
     ].
     monitorSema wait.
     self owningProcess: thisProcess.
-    self reinitCounter.
+    count := 1.
 
     "Created: / 20-11-2011 / 13:21:46 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 27-08-2012 / 11:45:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 enter
@@ -250,6 +289,7 @@
     processesEntered remove: thisProcess ifAbsent: nil.
 
     "Created: / 20-11-2011 / 13:21:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 26-08-2012 / 19:54:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 notify
@@ -292,12 +332,14 @@
     self assert: (self isOwnedBy: owningProcess).
     count == 1 ifTrue: [
         self clearOwningProcess.
+        count := 0.
         monitorSema signal.
     ] ifFalse: [
         self decrement.       
     ]
 
     "Created: / 20-11-2011 / 13:21:52 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 27-08-2012 / 11:44:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 releaseAll
@@ -376,3 +418,4 @@
 ! !
 
 JavaMonitor initialize!
+