Avoid deadlock when free is called by finalization code (Stefan Vogel)
authorMichael Beyl <mb@exept.de>
Tue, 24 Oct 2006 17:52:18 +0200
changeset 10137 7bfdc51fcb65
parent 10136 5e82e27705cc
child 10138 87c242cb7475
Avoid deadlock when free is called by finalization code (Stefan Vogel)
ExternalBytes.st
--- a/ExternalBytes.st	Tue Oct 24 12:40:39 2006 +0200
+++ b/ExternalBytes.st	Tue Oct 24 17:52:18 2006 +0200
@@ -9,12 +9,11 @@
  other person.  No title to or ownership of the software is
  hereby transferred.
 "
-
 "{ Package: 'stx:libbasic' }"
 
 UninterpretedBytes subclass:#ExternalBytes
 	instanceVariableNames:'address* size'
-	classVariableNames:'AllocatedInstances Lobby DebugMalloc TraceMalloc AccessLock'
+	classVariableNames:'AllocatedInstances Lobby DebugMalloc TraceMalloc'
 	poolDictionaries:''
 	category:'System-Support'
 !
@@ -1196,12 +1195,20 @@
      class variable - this prevents it from ever being finalized by
      the garbage collector, thus protecting the underlying memory."
 
-    AccessLock critical:[
-	AllocatedInstances isNil ifTrue:[
-	    AllocatedInstances := IdentitySet new
-	].
-	AllocatedInstances add:self
-    ]
+    |wasBlocked|
+
+
+    "using a Semaphore can cause a deadlock, since unprotectFromGC may be called by 
+     a finalization method"
+
+    wasBlocked := OperatingSystem blockInterrupts.
+"/    AccessLock critical:[
+        AllocatedInstances isNil ifTrue:[
+            AllocatedInstances := IdentitySet new
+        ].
+        AllocatedInstances add:self.
+"/    ]
+    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
 !
 
 unprotectFromGC
@@ -1210,11 +1217,16 @@
      the next garbage collect will finalize the receiver and the underlying
      memory be freed."
 
-    AllocatedInstances notNil ifTrue:[
-	AccessLock critical:[
-	    AllocatedInstances remove:self ifAbsent:nil
-	]
-    ]
+    |wasBlocked|
+
+    "using a Semaphore can cause a deadlock, since protectFromGC may be interrupted by me
+     being called by a finalization method"
+
+    wasBlocked := OperatingSystem blockInterrupts.
+"/    AccessLock critical:[
+            AllocatedInstances remove:self ifAbsent:nil.
+"/    ]
+    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
 ! !
 
 !ExternalBytes methodsFor:'resizing'!
@@ -1263,7 +1275,7 @@
 !ExternalBytes class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ExternalBytes.st,v 1.64 2006-09-14 19:08:39 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ExternalBytes.st,v 1.65 2006-10-24 15:52:18 mb Exp $'
 ! !
 
 ExternalBytes initialize!