Fixed assertions Java>>removeThread:for:
authorJan Vrany <jan.vrany@fit.cvut.cz>
Thu, 14 Aug 2014 12:55:32 +0100
changeset 3233 7bf7a8831ed9
parent 3232 1ab5987f3982
child 3234 5c9cb5b7a35b
Fixed assertions Java>>removeThread:for: Assertions should be true most of the time, however, when shuting down Java (by means of Java class>>flushAllJavaResources), Threads global might be already nilled out before all threads die. I (JV) would like to keep those assertions at least for a while, so guard assertions by notEmptyOrNil test.
Java.st
--- a/Java.st	Thu Aug 14 12:50:13 2014 +0100
+++ b/Java.st	Thu Aug 14 12:55:32 2014 +0100
@@ -127,9 +127,9 @@
     "/                ext path. They are then loaded by *ext* class loader and thus cannot
     "/                access classes defined in standard class path (as they are loaded by
     "/                system (*app*) class loader.
-"/        , (Array 
+"/        , (Array
 "/                with: ((Smalltalk packageDirectoryForPackageId: 'stx:libjava') / 'java' / 'bin')
-"/                with: ((Smalltalk packageDirectoryForPackageId: 'stx:libjava') / 'java' / 'libs' / 'ext') 
+"/                with: ((Smalltalk packageDirectoryForPackageId: 'stx:libjava') / 'java' / 'libs' / 'ext')
 "/        ).
 
     "Created: / 31-08-2011 / 22:55:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
@@ -319,10 +319,10 @@
 
     "Initialize JITer and Native method optimization"
     (flag := OperatingSystem getEnvironment:'STX_LIBJAVA_ENABLE_JIT') notNil ifTrue:[
-        ObjectMemory javaJustInTimeCompilation: (flag = '1') 
+        ObjectMemory javaJustInTimeCompilation: (flag = '1')
     ].
     (flag := OperatingSystem getEnvironment:'STX_LIBJAVA_ENABLE_NATIVE_METHOD_OPTIMIZATION') notNil ifTrue:[
-        ObjectMemory javaNativeCodeOptimization: (flag = '1') 
+        ObjectMemory javaNativeCodeOptimization: (flag = '1')
     ].
 
     "
@@ -559,7 +559,7 @@
     JavaVM initializeSystemPropertiesInto: props.
     stream nextPutAll: '== Initial system properties =='; cr.
     props keysAndValuesDo:[:key :value|
-        self dumpConfigKey: key value: value on: stream.  
+        self dumpConfigKey: key value: value on: stream.
     ].
 
     "
@@ -763,8 +763,8 @@
 
 cacheDirectory
     CacheDirectory isNil ifTrue:[
-        CacheDirectory := Filename homeDirectory / '.smalltalk' / 'libjava' 
-                / 'caches' 
+        CacheDirectory := Filename homeDirectory / '.smalltalk' / 'libjava'
+                / 'caches'
                     / (((Smalltalk at:#'stx_libjava') svnRepositoryUrlBase copyReplaceAll:$/
                             with:$_) replaceAll:$: with:$_)
     ].
@@ -1163,7 +1163,7 @@
     JavaVM finalizationLobby notNil ifTrue:[
         JavaVM finalizationLobby stopFinalizationProcess
     ].
-    Smalltalk garbageCollect. 
+    Smalltalk garbageCollect.
 
     "
     Java flushAllJavaResources
@@ -1176,7 +1176,7 @@
 
 flushClasses
 
-    | packagesInSystemDictionary |   
+    | packagesInSystemDictionary |
 
     UnresolvedClassRefs := nil.
     JavaVM flushClasses.
@@ -1194,7 +1194,7 @@
     ].
     packagesInSystemDictionary do:[:key|
         Smalltalk removeKey: key.
-    ].             
+    ].
 
     Java_lang_String := Java_lang_Class := nil.
     JavaVM releaseAllJavaResources.
@@ -1274,17 +1274,17 @@
 
 !Java class methodsFor:'source management'!
 
-classSourceStreamFor: aClass 
+classSourceStreamFor: aClass
     | jpackage  dirName  sourceFileName  sourceFile  loader  codeBaseURL  protocol  codeBaseURLIdx  src |
 
     aClass isNil ifTrue: [
         ^ nil
     ].
-    
+
     "/ look at the cache"
 
     "/ maybe it was loaded by a java classLoader ...
-    
+
     (loader := aClass classLoader) notNil ifTrue: [
         codeBaseURLIdx := loader class instVarOffsetOf: 'codeBaseURL'.
         codeBaseURLIdx notNil ifTrue: [
@@ -1302,19 +1302,19 @@
                 ]
             ]
         ].
-        
+
         "/ HACK HACK HACK: The eXpecco JImport plugin load the Java class and
         "/ sets fake classloader - an instance of Expecco::JIClassLoaderPlaceholder
         "/ which in turn references codelibrary in an instvar. If that's the case,
         "/ ask that library for source
-        
+
         loader class name == #'Expecco::JIClassLoaderPlaceholder' ifTrue: [
             ^ (loader library classSourceOf: aClass name) readStream.
         ]
     ].
-    
+
     "/ if that fails, look in standard places
-    
+
     (sourceFile isNil or: [ sourceFile exists not ]) ifTrue: [
         sourceFile := aClass sourceFile.
         sourceFile isNil ifTrue: [
@@ -1322,8 +1322,8 @@
             sourceFile := (aClass lastName upTo: $$) , '.java'.
         ].
         jpackage := aClass javaPackageAsDirname.
-        self effectiveSourceDirectories do: [:dir | 
-            src := self 
+        self effectiveSourceDirectories do: [:dir |
+            src := self
                          classSourceStreamForFile: sourceFile
                          package: jpackage
                          in: dir.
@@ -1344,7 +1344,7 @@
     "Modified: / 07-10-2013 / 17:01:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
-classSourceStreamForFile: filename package: jpackage in: dirOrZipFile 
+classSourceStreamForFile: filename package: jpackage in: dirOrZipFile
     | fn  pn  zar memberName member zipMemberName|
 
     fn := dirOrZipFile asFilename.
@@ -1359,15 +1359,15 @@
             ^ memberName readStream
         ].
     ] ifFalse: [
-        ((pn last == $p and: [ fn hasSuffix: 'zip' ]) 
-            or: [ (pn last == $r and: [ fn hasSuffix: 'jar' ]) ]) 
+        ((pn last == $p and: [ fn hasSuffix: 'zip' ])
+            or: [ (pn last == $r and: [ fn hasSuffix: 'jar' ]) ])
                 ifTrue: [
                     zar := SourceArchiveCache at: dirOrZipFile
                             ifAbsentPut: [ ZipArchive oldFileNamed: fn pathName ].
                     zar notNil ifTrue: [
                         jpackage notNil ifTrue: [
                             OperatingSystem fileSeparator ~~ $/ ifTrue: [
-                                memberName := (jpackage copyReplaceAll: OperatingSystem fileSeparator with: $/) 
+                                memberName := (jpackage copyReplaceAll: OperatingSystem fileSeparator with: $/)
                                         , '/' , filename
                             ] ifFalse: [
                                 memberName := jpackage , '/' , filename.
@@ -1402,14 +1402,14 @@
                                 (cache / memberName) exists ifTrue: [
                                     ^ (cache / memberName) readStream
                                 ].
-                                OperatingSystem 
+                                OperatingSystem
                                     executeCommand: ('unzip "%1" "%2"' bindWith: fn asAbsoluteFilename asString
                                     with: memberName asString)
                                     inDirectory: cache asString.
                                 (memberFileName := cache / memberName) exists ifTrue: [
                                     ^ memberFileName readStream
                                 ].
-                                OperatingSystem 
+                                OperatingSystem
                                     executeCommand: ('unzip "%1" "%2"' bindWith: fn asAbsoluteFilename asString
                                     with: zipMemberName asString)
                                     inDirectory: cache asString.
@@ -1533,7 +1533,7 @@
         self assert: (Threads includesKey: jThread) not.
         jThread == 0 ifTrue: [self breakPoint:#mh].
         Threads at: jThread put: stProcess.
-        stProcess addExitAction: [ 
+        stProcess addExitAction: [
             self removeThread: jThread for: stProcess
         ].
     ]
@@ -1557,14 +1557,23 @@
 
 removeThread: jThread for: stProcess
     ThreadsAccess critical: [
-        self assert: (Threads includesKey: jThread).
-        self assert: (Threads at: jThread) == stProcess.
-        Threads removeKey: jThread.
+        "/ Following assertions should be true most of the time,
+        "/ however, when shuting down Java (by means of
+        "/ Java class>>flushAllJavaResources), Threads global
+        "/ might be nilled out before all thread die.
+        "/ I (JV would like to keep those assertions at least for
+        "/ a while, so guard them no notEmptyOrNil test.
+        Threads notEmptyOrNil ifTrue:[
+            self assert: (Threads includesKey: jThread).
+            self assert: (Threads at: jThread) == stProcess.
+        ].
+        Threads removeKey: jThread ifAbsent:nil.
     ]
 
     "Created: / 26-08-1997 / 19:53:57 / cg"
     "Created: / 09-12-2011 / 12:47:32 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
-    "Modified: / 08-08-2014 / 08:58:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (format): / 14-08-2014 / 09:44:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+
 !
 
 threadForStProcess: stProcess