Light-weight monitor implementation?
Lukas Renggli
renggli at gmail.com
Thu Nov 3 09:05:42 UTC 2005
> > > What do other people think? Is there any reason for Monitor being such a
> > > heavy-weight object by default? Is it necessary that a monitor support
> > > all of the extra functionality besides the basic "allow a process to
> > > enter it multiple times"?
>
> Yeah, I vote for a minimal implementation as well. I don't like to use
> stuff that I cannot easily convince myself it does what I need. In
> VisualWorks there is such a thing called RecursionLock, implemented
> using only two simple methods: #initialize and #critical:. We should
> reinvent that ;-)
Ahh, ehh ... and another thing going with this: i am used to add a
conveniance method to the class Semaphore over and over again to do a
critical section with a timeout block. I am using the following code
(maybe for the current Squeak the use of a duration would be better):
Semaphore>>critical: aCriticalBlock timeout: anInteger ifTimeout: aTimeoutBlock
| timestamp |
timestamp := Time millisecondClockValue.
self waitTimeoutMSecs: anInteger.
^ Time millisecondClockValue - timestamp < anInteger
ifTrue: [ aCriticalBlock ensure: [ self signal ] ]
ifFalse: [ aTimeoutBlock value ].
A simple test:
| s |
s := Semaphore forMutualExclusion.
self assert: (s critical: [ 'ok' ] timeout: 10 ifTimeout: [ self
assert: false ]) = 'ok'.
s wait.
self assert: (s critical: [ self assert: false ] timeout: 10
ifTimeout: [ 'ok' ]) = 'ok'.
Yet another thing I would love to have are Promises:
result := [ self someCalc ] promise.
self someOtherCalc.
result value
Lukas
--
Lukas Renggli
http://www.lukas-renggli.ch
More information about the Squeak-dev
mailing list
|