Monitor.st
changeset 718 ba2260d7863f
parent 716 242781d82881
child 768 671ad758b7da
--- a/Monitor.st	Thu Jan 07 15:45:44 1999 +0100
+++ b/Monitor.st	Fri Jan 08 14:00:17 1999 +0100
@@ -133,24 +133,21 @@
         ^ self
     ].
 
-    owningProcess isDead ifTrue:[
-        'Java [warning]: entering monitor owned by dead process' infoPrintCR.
-        "/ self halt.
-        owningProcess := thisProcess.
-        count := 1.
-        ^ self
-    ].
-
     wasBlocked := OperatingSystem blockInterrupts.
     [
-        [owningProcess notNil] whileTrue:[
-            thisProcess state:#monWait.
-            (sema waitWithTimeout:10) isNil ifTrue:[
-                (owningProcess notNil and:[owningProcess isDead]) ifTrue:[
-                    'Monitor [warning]: aquire monitor from dead process' errorPrintCR.
-                    owningProcess := nil.
+        owningProcess isDead ifTrue:[
+            'Monitor [warning]: entering monitor owned by dead process' errorPrintCR.
+            "/ self halt.
+        ] ifFalse:[
+            [owningProcess notNil] whileTrue:[
+                thisProcess state:#monWait.
+                (sema waitWithTimeout:10) isNil ifTrue:[
+                    (owningProcess notNil and:[owningProcess isDead]) ifTrue:[
+                        'Monitor [warning]: aquire monitor from dead process' errorPrintCR.
+                        owningProcess := nil.
+                    ]
                 ]
-            ]
+            ].
         ].
         count := 1.
         owningProcess := thisProcess.
@@ -158,7 +155,7 @@
         wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
     ]
 
-    "Modified: / 30.12.1998 / 19:32:24 / cg"
+    "Modified: / 8.1.1999 / 13:56:28 / cg"
 !
 
 exit
@@ -190,6 +187,48 @@
     sema signal.
 
     "Modified: 21.8.1997 / 16:44:17 / cg"
+!
+
+fakeEnter:aProcess count:additionalCount
+    "(fake-)enter the monitor, without blocking.
+     Raises an error, if the monitor is not free and owned by another process"
+
+    |wasBlocked|
+
+    "
+     this works only since interrupts are only serviced at 
+     message send and method-return time ....
+     If you add a message send into the ifTrue:-block, things will
+     go mad ... (especially be careful when adding a debugPrint-here)
+    "
+    owningProcess isNil ifTrue:[
+        count := additionalCount.
+        owningProcess := aProcess.
+        ^ self
+    ].
+
+    wasBlocked := OperatingSystem blockInterrupts.
+
+    owningProcess == aProcess ifTrue:[
+        count := count + additionalCount.
+        wasBlocked ifTrue:[ OperatingSystem unblockInterrupts].
+        ^ self
+    ].
+
+    owningProcess isDead ifTrue:[
+        'Monitor [warning]: (fake)entering monitor owned by dead process' errorPrintCR.
+        "/ self halt.
+        owningProcess := aProcess.
+        count := additionalCount.
+        wasBlocked ifTrue:[ OperatingSystem unblockInterrupts].
+        ^ self
+    ].
+
+    wasBlocked ifTrue:[ OperatingSystem unblockInterrupts].
+    self error:'Cannot fakeEnter monitor owned by another process'.
+
+    "Created: / 8.1.1999 / 13:54:44 / cg"
+    "Modified: / 8.1.1999 / 13:57:42 / cg"
 ! !
 
 !Monitor methodsFor:'initialization'!
@@ -203,24 +242,36 @@
 
 !Monitor methodsFor:'queries'!
 
+count
+    owningProcess isNil ifTrue:[^ 0].
+    ^ count
+
+    "Created: / 8.1.1999 / 13:59:30 / cg"
+    "Modified: / 8.1.1999 / 14:00:01 / cg"
+!
+
 isFree
     "return true, if the monitor is free
      (i.e. noone waits and count is zero)"
 
     |wasBlocked ret|
 
+    owningProcess isNil ifTrue:[^ true].
+    count == 0 ifTrue:[^ true].
+
     ret := true.
 
     wasBlocked := OperatingSystem blockInterrupts.
     owningProcess notNil ifTrue:[
-	ret := false
+        ret := false
     ] ifFalse:[
-	sema numberOfWaitingProcesses ~~ 0 ifTrue:[ret := false].
+        sema numberOfWaitingProcesses ~~ 0 ifTrue:[ret := false].
     ].
     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
     ^ ret.
 
-    "Created: 3.5.1996 / 18:08:38 / cg"
+    "Created: / 3.5.1996 / 18:08:38 / cg"
+    "Modified: / 8.1.1999 / 13:59:53 / cg"
 !
 
 owningProcess
@@ -234,5 +285,5 @@
 !Monitor class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Monitor.st,v 1.13 1998-12-30 18:32:45 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Monitor.st,v 1.14 1999-01-08 13:00:17 cg Exp $'
 ! !