More Delay/Semaphore "fun"

Paolo Bonzini bonzini at gnu.org
Mon Oct 8 13:53:40 UTC 2007


Gary Chambers wrote:
> Well, we're finding that you fixes are helping a lot (maybe not 100% watertight but much better than without!).
> 
> For a more difficult challenge:
> 
> Monitor>>critical:

Not really... It would be more complicated to support *everything* in 
Monitor.  But if all you want is a recursion-safe mutex, you can inline 
enter and exit (and the nestingLevel variable is useless now):

| us |
us := Processor activeProcess.
ownerProcess == us
     ifTrue: [ ^aBlock value ]
     ifFalse: [
	mutex critical: [
	    ["When we enter, the mutex is free so ownerProcess is nil.
               So the unwinding does not mess up anything."
             ownerProcess := us.
	    blockValue := aBlock value ]
		ensure: [ ownerProcess := nil ] ] ].
     ^blockValue

The complicated stuff is done in Semaphore>>#critical: and Monitor can 
simply leverage that.

Alternatively, you could have a primitive to notify a waiter on a 
semaphore without adding a signal (a no-op if there is no one waiting). 
    This simplifies everything because the excess signals are always 
zero!  Then the code would look like this:

reset := false.
[ ownerProcess == us ] whileFalse: [
     semaphore wait.
     ownerProcess isNil ifTrue: [ownerProcess := us. reset := true]].
[ blockValue := aBlock value ]
     ensure: [reset ifTrue: [ownerProcess := nil. semaphore notify]].
^blockValue

Paolo




More information about the Squeak-dev mailing list