- JavaFinalizationRegistry jk_new_structure
authorvranyj1
Tue, 24 Jul 2012 14:37:07 +0000
branchjk_new_structure
changeset 1551 2af2aa41bddb
parent 1550 fde50161f2b4
child 1552 89c63e6303f1
- JavaFinalizationRegistry class definition added:6 methods changed: #initialize #registerChange: category of: #collectionCycle #registerChange: - JavaVM changed: #commonOpenStreamUsing: #initializeVM - Java changed: #flushAllJavaResources - extensions ...
src/Java.st
src/JavaFinalizationRegistry.st
src/JavaVM.st
src/libjava.rc
--- a/src/Java.st	Tue Jul 24 10:22:56 2012 +0000
+++ b/src/Java.st	Tue Jul 24 14:37:07 2012 +0000
@@ -1077,14 +1077,17 @@
     GroovyEvaluator flushWorkspaceBinding .
     GroovyCompiler  flushGroovyClassLoader.
     JavaClass flushClassesInitOrder.
-    self flushClasses
+    self flushClasses.
+    JavaVM finalizationLobby notNil ifTrue:[
+        JavaVM finalizationLobby stopFinalizationProcess
+    ]
 
     "
      Java flushAllJavaResources"
 
     "Modified: / 06-11-2001 / 09:49:37 / cg"
     "Modified: / 02-11-2011 / 21:34:13 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
-    "Modified: / 18-02-2012 / 22:25:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 24-07-2012 / 15:33:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 flushClasses
--- a/src/JavaFinalizationRegistry.st	Tue Jul 24 10:22:56 2012 +0000
+++ b/src/JavaFinalizationRegistry.st	Tue Jul 24 14:37:07 2012 +0000
@@ -21,7 +21,7 @@
 "{ Package: 'stx:libjava' }"
 
 Object subclass:#JavaFinalizationRegistry
-	instanceVariableNames:'activeReferees'
+	instanceVariableNames:'activeReferees finalizationSemaphore finalizationProcess'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Languages-Java-Support'
@@ -79,29 +79,9 @@
     ^ self basicNew initialize.
 ! !
 
-!JavaFinalizationRegistry methodsFor:'* As yet uncategorized *'!
-
-registerChange: anObject
-    "/Nothing to do"
-
-    "Created: / 24-07-2012 / 03:31:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaFinalizationRegistry methodsFor:'initialization'!
+!JavaFinalizationRegistry methodsFor:'finalization'!
 
-initialize
-    "Invoked when a new instance is created."
-
-    "/ please change as required (and remove this comment)
-    activeReferees := Array new: 200.
-    "/ super initialize.   -- commented since inherited method does nothing
-
-    "Modified (comment): / 24-07-2012 / 02:05:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaFinalizationRegistry methodsFor:'private'!
-
-collectionCycle
+finalizationCycle
 
     | referees references living wasBlocked firstPendingReference lastPendingReference ref |
 
@@ -143,9 +123,33 @@
 
     self informReferenceHandler: firstPendingReference
 
-    "Created: / 24-07-2012 / 01:30:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 24-07-2012 / 15:14:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+finalizationLoop
+
+    [ 
+        finalizationSemaphore waitWithTimeoutMs: 10000"10sec".
+        self finalizationCycle    
+    ] loop
+
+    "Created: / 24-07-2012 / 15:16:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaFinalizationRegistry methodsFor:'initialization'!
+
+initialize
+    "Invoked when a new instance is created."
+
+    "/ please change as required (and remove this comment)
+    activeReferees := Array new: 200.
+    "/ super initialize.   -- commented since inherited method does nothing
+
+    "Modified: / 24-07-2012 / 15:23:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaFinalizationRegistry methodsFor:'private'!
+
 firstFreeIndex
     activeReferees withIndexDo:[:value :index|
         value isNil ifTrue:[
@@ -343,6 +347,64 @@
         valueWithReceiver: finalizedClass arguments: (Array with: object)
 
     "Created: / 24-07-2012 / 01:14:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+registerChange: anObject
+    "/Nothing to do, to be polymprph with Registry"
+
+    "Created: / 24-07-2012 / 03:31:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaFinalizationRegistry methodsFor:'start & stop'!
+
+startFinalizationProcessAt:aPriority
+
+    |p|
+
+    finalizationProcess notNil ifTrue:[
+        finalizationProcess priority:aPriority.
+        ^ self
+    ].
+
+    finalizationSemaphore := Semaphore new name:'FinalizationSemaphore (Java)'.
+
+    p :=
+        [
+            [
+                self finalizationLoop
+            ] ifCurtailed:[
+                finalizationProcess := nil.
+                finalizationSemaphore := nil
+            ]
+        ] newProcess.
+    p name:'background finalizer (Java)'.
+    p priority:aPriority.
+    p restartable:true.
+    p beSystemProcess.
+    p resume.
+    finalizationProcess := p
+
+    "Created: / 24-07-2012 / 15:25:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+stopFinalizationProcess
+    "stop the background finalizer"
+
+    finalizationProcess notNil ifTrue:[
+        finalizationProcess terminate.
+        finalizationProcess := nil
+    ].
+
+    "Created: / 24-07-2012 / 15:26:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaFinalizationRegistry methodsFor:'utilities'!
+
+finalizeNow
+    "Force finalization to run now"
+    finalizationSemaphore signal
+
+    "Created: / 24-07-2012 / 15:28:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaFinalizationRegistry class methodsFor:'documentation'!
--- a/src/JavaVM.st	Tue Jul 24 10:22:56 2012 +0000
+++ b/src/JavaVM.st	Tue Jul 24 14:37:07 2012 +0000
@@ -1922,7 +1922,7 @@
     self initializeSystemClassLoader.
     ObjectMemory addDependent: self.
     StartupTime := OperatingSystem getOSTime.
-
+    FinalizationLobby startFinalizationProcessAt: 5.
 
 
     "
@@ -1934,7 +1934,7 @@
     "Modified: / 15-10-2010 / 15:27:45 / Jan Kurs <kurs.jan@post.cz>"
     "Modified: / 24-02-2012 / 13:59:29 / Marcel Hlopko <hlopik@gmail.com>"
     "Modified: / 24-02-2012 / 14:37:06 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
-    "Modified: / 24-07-2012 / 02:44:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (format): / 24-07-2012 / 15:31:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 initializeVMIfNoEventThreadRunning
@@ -4438,8 +4438,13 @@
     retry := false.
     [ stream := aBlock value ] on:OpenError do:[:ex|
         (ex errorCode) == (OperatingSystem errorNumberFor:#EMFILE) ifTrue:[
-            ObjectMemory garbageCollect.
-            retry := true.
+            [
+                FinalizationLobby finalizeNow.
+                ObjectMemory garbageCollect.
+                retry := true.
+            ] on: Error do:[:ex|
+                Logger log: 'Failed to force finalization: ', ex description severity: #error facility: #JVM
+            ].
         ] ifFalse:[
             ex pass.
         ].
--- a/src/libjava.rc	Tue Jul 24 10:22:56 2012 +0000
+++ b/src/libjava.rc	Tue Jul 24 14:37:07 2012 +0000
@@ -3,7 +3,7 @@
 // automagically generated from the projectDefinition: stx_libjava.
 //
 VS_VERSION_INFO VERSIONINFO
-  FILEVERSION     6,2,1903,1903
+  FILEVERSION     6,2,1905,1905
   PRODUCTVERSION  6,2,1,1
 #if (__BORLANDC__)
   FILEFLAGSMASK   VS_FF_DEBUG | VS_FF_PRERELEASE
@@ -20,12 +20,12 @@
     BEGIN
       VALUE "CompanyName", "eXept Software AG\0"
       VALUE "FileDescription", "Smalltalk/X Class library (LIB)\0"
-      VALUE "FileVersion", "6.2.1903.1903\0"
+      VALUE "FileVersion", "6.2.1905.1905\0"
       VALUE "InternalName", "stx:libjava\0"
       VALUE "LegalCopyright", "Copyright Claus Gittinger 1988-2011\nCopyright eXept Software AG 1998-2011\nCopyright Jan Vrany, Jan Kurs and Marcel Hlopko\b          SWING Research Group, Czech Technical University In Prague\0"
       VALUE "ProductName", "Smalltalk/X\0"
       VALUE "ProductVersion", "6.2.1.1\0"
-      VALUE "ProductDate", "Tue, 24 Jul 2012 10:25:45 GMT\0"
+      VALUE "ProductDate", "Tue, 24 Jul 2012 14:41:08 GMT\0"
     END
 
   END