src/JavaProcess.st
branchjk_new_structure
changeset 752 ff7bc6428c9c
child 877 f5a5b93e1c78
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/JavaProcess.st	Fri Apr 08 12:02:36 2011 +0000
@@ -0,0 +1,230 @@
+"
+ COPYRIGHT (c) 1997 by eXept Software AG
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+"{ Package: 'stx:libjava' }"
+
+Process subclass:#JavaProcess
+	instanceVariableNames:'suspendedContext'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Languages-Java-Classes'
+!
+
+Object subclass:#Helper
+	instanceVariableNames:'javaThreadObject javaProcess'
+	classVariableNames:''
+	poolDictionaries:''
+	privateIn:JavaProcess
+!
+
+!JavaProcess class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1997 by eXept Software AG
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+! !
+
+!JavaProcess class methodsFor:'instance creation'!
+
+newHelper
+
+    ^Helper new
+
+    "Created: / 15-12-2010 / 11:11:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaProcess methodsFor:'accessing'!
+
+suspendedContext
+    |con|
+
+"/    suspendedContext notNil ifTrue:[^ suspendedContext].
+"/    state ~~ #debug ifTrue:[
+"/        "/ hide non-JAVA contexts ...
+"/        con := super suspendedContext.
+"/        [con notNil] whileTrue:[
+"/            (con isMemberOf:JavaContext) ifTrue:[^ con].
+"/            con := con sender.
+"/        ].
+"/    ].
+    ^ super suspendedContext
+
+    "Created: / 7.5.1996 / 09:02:03 / cg"
+    "Modified: / 4.2.1998 / 20:44:31 / cg"
+!
+
+suspendedContext:aContext
+    suspendedContext := aContext
+
+    "Created: 7.5.1996 / 09:02:12 / cg"
+! !
+
+!JavaProcess methodsFor:'interrupts'!
+
+clearInterruptActions
+    interruptActions := nil
+
+    "Created: 14.8.1997 / 17:27:22 / cg"
+!
+
+isInterrupted
+    ^ interruptActions notNil
+
+    "Created: 14.8.1997 / 17:26:40 / cg"
+!
+
+markInterrupted
+    super interruptWith:[]
+
+    "Modified: / 10.4.1998 / 15:20:58 / cg"
+    "Created: / 10.4.1998 / 15:21:15 / cg"
+! !
+
+!JavaProcess methodsFor:'suspend & resume'!
+
+terminateNoSignal
+
+    | jThread |
+    jThread := Java threads keyAtValue: self.
+    JavaVM wakeup: jThread.
+
+    super terminateNoSignal
+
+    "Created: / 15-12-2010 / 00:18:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 15-12-2010 / 11:19:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaProcess::Helper methodsFor:'initialization'!
+
+javaProcess:something
+    javaProcess := something.
+!
+
+javaThreadObject:something
+    javaThreadObject := something.
+! !
+
+!JavaProcess::Helper methodsFor:'support'!
+
+value
+    |procName|
+
+    Object abortSignal handle:
+            [:ex | 
+            procName := javaProcess name.
+            (procName startsWith:'JAVA-AWT-EventQueue') 
+                ifTrue:
+                    [ ('JAVA [info]: thread ' , procName , ' aborted - restarting process.') 
+                        infoPrintCR.
+                    ex restart. ]
+                ifFalse:
+                    [ (javaProcess == (Smalltalk at:#'JavaVM:JavaScreenUpdaterThread') 
+                        or:[ javaProcess == (Smalltalk at:#'JavaVM:JavaEventQueueThread') ]) 
+                            ifTrue:
+                                [ ('JAVA [info]: thread ' , procName , ' aborted - restarting process.') 
+                                    infoPrintCR.
+                                ex restart ]
+                            ifFalse:[ ('JAVA [info]: thread ' , procName , ' aborted.') infoPrintCR. ] ]. ]
+        do:
+            [ 
+            [ JavaVM javaExceptionSignal handle:
+                    [:ex | 
+                    |exClass|
+
+                    procName := javaProcess name.
+                    exClass := ex parameter class.
+                    exClass == (Java at:'java.lang.ThreadDeath') 
+                        ifTrue:[ ('JAVA: thread ' , procName , ' terminated') infoPrintCR. ]
+                        ifFalse:
+                            [ Transcript 
+                                showCR:('JAVA: thread ''' , procName , ''' terminated with exception: ' 
+                                        , exClass name). ].
+                    ex return. ]
+                do:
+                    [ Object messageNotUnderstoodSignal handle:
+                            [:ex | 
+                            |
+                            "/ remap doesNotUnderstand with nil-receiver to
+                            "/ a nullPointerException ...
+                             con m|
+
+                            con := ex suspendedContext.
+                            con receiver isNil 
+                                ifTrue:
+                                    [ ((m := con sender method) notNil and:[ m isJavaMethod ]) 
+                                        ifTrue:
+                                            [ JavaVM throwNullPointerException.
+                                            AbortSignal raise.
+                                            "/ ex proceed.
+                                             ] ].
+                            ex reject. ]
+                        do:
+                            [ "/ Transcript showCR:(Timestamp now printString , 'start thread: ', stProcess name).
+                            javaThreadObject perform:#'run()V'.
+                            (Smalltalk at:#'JavaVM:ThreadTrace') == true 
+                                ifTrue:[ ('JAVA: thread ' , javaProcess name , ' terminated') infoPrintCR. ].
+                            javaThreadObject perform:#'exit()V'.
+                            (Smalltalk at:#'JavaVM:ThreadTrace') == true 
+                                ifTrue:[ ('JAVA: after exit of thread ' , javaProcess name) infoPrintCR. ] ] ] ] ensure:
+                        [ |monitors|
+
+                        monitors := (Smalltalk at:#'JavaVM:EnteredMonitorsPerProcess') 
+                                    at:javaProcess
+                                    ifAbsent:nil.
+                        monitors notNil 
+                            ifTrue:
+                                [ monitors do:
+                                        [:obj | 
+                                        |mon|
+
+                                        mon := JavaVM monitorFor:obj.
+                                        mon notNil 
+                                            ifTrue:
+                                                [ mon owningProcess == javaProcess 
+                                                    ifTrue:
+                                                        [ ('JAVA: release monitor owned by dying thread: ' , javaProcess name) 
+                                                            infoPrintCR.
+                                                        mon exit ]. ]. ].
+                                (Smalltalk at:#'JavaVM:EnteredMonitorsPerProcess') removeKey:javaProcess.
+                                javaProcess == (Smalltalk at:#'JavaVM:JavaScreenUpdaterThread') 
+                                    ifTrue:[ Smalltalk at:#'JavaVM:JavaScreenUpdaterThread' put:nil. ].
+                                javaProcess == (Smalltalk at:#'JavaVM:JavaEventQueueThread') 
+                                    ifTrue:[ Smalltalk at:#'JavaVM:JavaEventQueueThread' put:nil. ].
+                                
+"/                                    screenUpdaterClass := Java at:'sun.awt.ScreenUpdater'.    
+"/                                    screenUpdaterClass notNil ifTrue:[
+"/                                        screenUpdaterClass instVarNamed:'updater' put:nil.
+"/                                    ].
+                                 ].
+                        Java threads removeKey:javaThreadObject ifAbsent:[]. ] ]
+
+    "Created: / 15-12-2010 / 11:06:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaProcess class methodsFor:'documentation'!
+
+version
+    ^ '$Id$'
+!
+
+version_SVN
+    ^ '$Id$'
+! !