[ENH] RecursionLock

Ragnar Hojland Espinosa ragnar at linalco.com
Tue Feb 3 12:22:32 UTC 2004


I don't know if there's something like this in squeak, but I was 
reading Smalltalk By Example and I found it missing.  Its noob work,
so beware :)
-- 
Ragnar Hojland - Project Manager
Linalco "Specialists in Linux and Free Software"
http://www.linalco.com  Tel: +34-91-4561700
-------------- next part --------------
'From Squeak3.6 of ''6 October 2003'' [latest update: #5429] on 3 February 2004 at 1:15:45 pm'!
Object subclass: #RecursionLock
	instanceVariableNames: 'lockedByProcess semaphore '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LDAP'!
!RecursionLock commentStamp: 'RHE 2/3/2004 13:09' prior: 0!
I work as Semaphore forMutualExclusion but I won't block in critical: if I was already locked by the same process running me now.	

	|lock|

	lock _ RecursionLock new.
	lock critical:[
		Transcript show: 'Locked.';cr.
		lock critical:[
			Transcript show: 'By now I would be  deadlocked if I was a Semaphore.';cr. ] ]!


!RecursionLock methodsFor: 'mutual-exclusion' stamp: 'RHE 2/3/2004 13:04'!
critical: protectedBlock
	| blockValue |
	lockedByProcess = Processor activeProcess
		ifTrue: [
			"If lockedByProcess matches, its obvously not nil and thereforey we were already locked.
			So skip the semaphore."
			blockValue _ protectedBlock value ]
		ifFalse: [ 
			"Otherwise lock and proceed as usual making sure we reset the lock once its over"
			semaphore critical: [
				lockedByProcess _ Processor activeProcess.
				blockValue _  [protectedBlock value] ensure: [lockedByProcess _ nil] ] ].
	^ blockValue
! !


!RecursionLock methodsFor: 'initialize-release' stamp: 'RHE 2/3/2004 11:39'!
initialize
	semaphore _ Semaphore forMutualExclusion! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

RecursionLock class
	instanceVariableNames: ''!

!RecursionLock class methodsFor: 'instance creation' stamp: 'RHE 2/3/2004 12:02'!
new
	^ self basicNew initialize! !


More information about the Squeak-dev mailing list