src/JavaMonitor.st
branchjk_new_structure
changeset 1146 e458dd16772e
parent 1145 8728f5373a48
child 1148 15b6074a4841
--- a/src/JavaMonitor.st	Tue Nov 22 10:12:20 2011 +0000
+++ b/src/JavaMonitor.st	Tue Nov 22 12:04:31 2011 +0000
@@ -43,7 +43,8 @@
 
 Object subclass:#JavaMonitor
 	instanceVariableNames:'owningProcess processesEntered monitorSema processesEnteredAccess
-		owningProcessAccess count countAccess'
+		owningProcessAccess count countAccess waitingSema
+		processesWaitingAccess processesWaiting'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Languages-Java-Support'
@@ -160,6 +161,24 @@
     "Created: / 20-11-2011 / 20:32:26 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
+processesWaitingAdd: aProcess 
+    processesWaitingAccess 
+        critical: [ self assert: (processesWaiting includesKey: aProcess) not.processesWaiting at: aProcess put: count ].
+
+    "Created: / 22-11-2011 / 11:57:50 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+processesWaitingRestore: aProcess 
+   processesWaitingAccess 
+   critical: [
+   self assert: (processesWaiting includesKey: aProcess).
+   self reinitCounter: (processesWaiting at: aProcess).   
+   processesWaiting removeKey: aProcess
+           ].
+
+    "Created: / 22-11-2011 / 12:59:14 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
 reinitCounter
     "owning process is different from previous, lets start counting from beginning"
     
@@ -168,8 +187,18 @@
     "Created: / 22-11-2011 / 10:51:09 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
+reinitCounter: newCount 
+    countAccess critical: [ count := newCount ].
+
+    "Created: / 22-11-2011 / 13:00:51 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
 removeProcess: aProcess 
-    processesEnteredAccess critical: [ self assert: processesEntered last == aProcess. processesEntered remove: aProcess ].
+    processesEnteredAccess 
+        critical: [
+            self assert: processesEntered last == aProcess.
+            processesEntered remove: aProcess
+        ].
 
     "Created: / 20-11-2011 / 20:28:37 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 ! !
@@ -183,6 +212,9 @@
     processesEnteredAccess := Semaphore forMutualExclusion.
     monitorSema := Semaphore new: 1.
     countAccess := Semaphore forMutualExclusion.
+    processesWaiting := Dictionary new.
+    processesWaitingAccess := Semaphore forMutualExclusion.
+    waitingSema := Semaphore new: 0.
 
     "Created: / 20-11-2011 / 13:28:28 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 ! !
@@ -221,6 +253,28 @@
     "Created: / 20-11-2011 / 13:21:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
+notify
+    "wakeup one waiting process"
+    
+    | thisProcess |
+    thisProcess := Processor activeProcess.
+    self assert: (self isOwnedBy: thisProcess).        
+    waitingSema signal.
+
+    "Created: / 22-11-2011 / 12:14:23 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+notifyAll
+    "wakeup one waiting process"
+    
+    | thisProcess |
+    thisProcess := Processor activeProcess.
+    self assert: (self isOwnedBy: thisProcess).
+    waitingSema signalForAll.
+
+    "Created: / 22-11-2011 / 12:14:36 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
 release
     | thisProcess |
     thisProcess := self activeProcess.
@@ -233,6 +287,39 @@
     ]
 
     "Created: / 20-11-2011 / 13:21:52 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+releaseAll
+    count timesRepeat: [self release].
+
+    "Created: / 22-11-2011 / 13:05:20 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+wait
+    "make owning process to go to wait"
+    
+    self waitForMilliseconds: nil.
+
+    "Created: / 22-11-2011 / 11:57:56 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
+waitForMilliseconds: timeOut 
+    "make owning process to go to wait, but not longer than timeout"
+    
+    | thisProcess |
+    thisProcess := Processor activeProcess.
+    self assert: (self isOwnedBy: thisProcess).
+    self processesWaitingAdd: thisProcess.
+    self releaseAll.
+    waitingSema waitWithTimeoutMs: timeOut.
+    Logger 
+        log: 'Process has been notified'
+        severity: #debug
+        facility: #JVM.
+    self acquire.
+    self processesWaitingRestore: thisProcess.
+
+    "Created: / 22-11-2011 / 12:52:45 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 ! !
 
 !JavaMonitor methodsFor:'queries'!