'From Cuis 2.6 of 10 August 2010 [latest update: #540] on 29 August 2010 at 3:59:18 pm'!
!classDefinition: #WeakArray category: #'Collections-Weak'!
Array weakSubclass: #WeakArray
	instanceVariableNames: ''
	classVariableNames: 'FinalizationDependents FinalizationLock FinalizationProcess FinalizationSemaphore IsFinalizationSupported '
	poolDictionaries: ''
	category: 'Collections-Weak'!

!WeakArray class methodsFor: 'accessing' stamp: 'jmv 8/29/2010 15:53'!
addWeakDependent: anObject
	FinalizationLock critical: [
		| finished index weakDependent |
		finished := false.
		index := 0.
		[index := index + 1.
		finished not and:[index <= FinalizationDependents size]] whileTrue:[
			weakDependent := FinalizationDependents at: index.
			weakDependent ifNil:[
				FinalizationDependents at: index put: anObject.
				finished := true.
			].
		].
		finished ifFalse:[
			"Grow linearly"
			FinalizationDependents := FinalizationDependents, (WeakArray new: 10).
			FinalizationDependents at: index put: anObject.
		].
	] ifError:[:msg :rcvr| rcvr error: msg].! !

!WeakArray class methodsFor: 'accessing' stamp: 'ul 8/27/2010 23:28'!
isFinalizationSupported
	"This method is only here for backwards compatibility, all closure VMs support finalization"
	
	^true! !

!WeakArray class methodsFor: 'accessing' stamp: 'ul 8/27/2010 23:25'!
removeWeakDependent: anObject

	FinalizationLock critical:[
		1 to: FinalizationDependents size do:[:i|
			((FinalizationDependents at: i) == anObject) ifTrue:[
				FinalizationDependents at: i put: nil.
			].
		].
	] ifError:[:msg :rcvr| rcvr error: msg].! !

!WeakArray class methodsFor: 'private' stamp: 'nice 1/1/2010 00:56'!
finalizationProcess

	[true] whileTrue:
		[FinalizationSemaphore wait.
		FinalizationLock critical:
			[FinalizationDependents do:
				[:weakDependent |
				weakDependent ifNotNil:
					[weakDependent finalizeValues]]]
			ifError:
			[:msg :rcvr | rcvr error: msg].
		].
! !

!WeakArray class methodsFor: 'private' stamp: 'jmv 8/29/2010 15:50'!
restartFinalizationProcess
	"kill any old process, just in case"
	FinalizationProcess
		ifNotNil: [ 
			FinalizationProcess terminate.
			FinalizationProcess := nil ].

	FinalizationSemaphore := Smalltalk specialObjectsArray at: 42.
	FinalizationDependents ifNil: [FinalizationDependents := WeakArray new: 10].
	FinalizationLock := Semaphore forMutualExclusion.
	FinalizationProcess := [ self finalizationProcess ] newProcess.
	FinalizationProcess priority: Processor userInterruptPriority.
	FinalizationProcess resume! !

!classDefinition: #WeakArray category: #'Collections-Weak'!
Array weakSubclass: #WeakArray
	instanceVariableNames: ''
	classVariableNames: 'FinalizationDependents FinalizationLock FinalizationProcess FinalizationSemaphore'
	poolDictionaries: ''
	category: 'Collections-Weak'!
"Postscript:
Leave the line above, and replace the rest of this comment by a useful one.
Executable statements should follow this comment, and should
be separated by periods, with no exclamation points (!!).
Be sure to put any further comments in double-quotes, like this one."
WeakArray initialize!

