[Vm-dev] Where to get Monitor implementation based on primitives?

Ben Coman btc at openinworld.com
Sat Feb 6 07:16:29 UTC 2016


> 2016-01-11 14:58 GMT+01:00 Denis Kudriashov <dionisiydk at gmail.com>:
>>
>> Eliot I found that there are no methods for simulation code (if it right name for it). So stepping over new primitives failed (which is infinite recursion for Pharo case)
>>
>> Context>>doPrimitive: primitiveIndex method: meth receiver: aReceiver args: arguments
>> ...
>> "Mutex>>primitiveEnterCriticalSection
>> Mutex>>primitiveTestAndSetOwnershipOfCriticalSection"
>> (primitiveIndex = 186 or: [primitiveIndex = 187]) ifTrue:
>> [| active effective |
>> active := Processor activeProcess.
>> effective := active effectiveProcess.
>> "active == effective"
>> value := primitiveIndex = 186
>> ifTrue: [aReceiver primitiveEnterCriticalSectionOnBehalfOf: effective]
>> ifFalse: [aReceiver primitiveTestAndSetOwnershipOfCriticalSectionOnBehalfOf: effective].
>> ^(self isPrimFailToken: value)
>> ifTrue: [value]
>> ifFalse: [self push: value]].
>>
>> How this methods should be implemented?

On Mon, Jan 11, 2016 at 10:54 PM, Denis Kudriashov <dionisiydk at gmail.com> wrote:
>
> I write it such way:
>
> primitiveEnterCriticalSectionOnBehalfOf: activeProcess
> [owningProcess ifNil:
> [owningProcess := activeProcess.
> ^false].
>  owningProcess = activeProcess ifTrue:
> [^true].
>  self addLast: Processor activeProcess.
> activeProcess suspend] valueUnpreemptively
>
> primitiveTestAndSetOwnershipOfCriticalSectionOnBehalfOf: activeProcess
> [owningProcess ifNil:
> [owningProcess := activeProcess.
> ^false].
>  owningProcess = activeProcess ifTrue: [^true].
>  ^nil] valueUnpreemptively

@Dennis, Why not...
    primitiveEnterCriticalSectionOnBehalfOf: activeProcess
         <primitive: 186>
         self primitiveFailed

I had been meaning to ask what this code at the top of the primitive was for...
    argumentCount > 0
        ifTrue:
            [criticalSection := self stackValue: 1.  "rcvr"
             activeProc := self stackTop]
        ifFalse:
            [criticalSection := self stackTop.  "rcvr"
             activeProc := self activeProcess].

but took a wild guess its to support onBehalfOf type calls.
So I tried...

    | cs p1 p2 p3 |
    cs := CriticalSection new.
    p1 := [ cs critical: [ Transcript crShow: 1 ] ] newProcess priority: 41.
    p2 := [ cs critical: [ Transcript crShow: 2 ] ] newProcess priority: 41.
    p3 := [ cs critical: [ Transcript crShow: 3 ] ] newProcess priority: 41.
    cs primitiveEnterCriticalSectionOnBehalfOf: p3.
    p1 resume. p2 resume. p3 resume.
    cs primitiveExitCriticalSection.

and indeed the result is...
3
1
2

cheers -ben


More information about the Vm-dev mailing list