<div dir="ltr">Hi Ben,<br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 8, 2016 at 7:25 AM, Ben Coman <span dir="ltr">&lt;<a href="mailto:btc@openinworld.com" target="_blank">btc@openinworld.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br>
On Fri, Jan 8, 2016 at 9:39 PM, Ben Coman &lt;<a href="mailto:btc@openinworld.com">btc@openinworld.com</a>&gt; wrote:<br>
&gt; On Fri, Jan 8, 2016 at 5:42 PM, stephane ducasse<br>
&gt; &lt;<a href="mailto:stephane.ducasse@gmail.com">stephane.ducasse@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; I have a (stupid) question.<br>
&gt;&gt; Is the code running without the primitives?<br>
&gt;&gt; Are the code below the primitives correct?<br>
&gt;&gt; I asked that because we can have 100 eyes and brains on the smalltalk level and far less on the VM primitive level.<br>
&gt;<br>
&gt; Because:<br>
&gt; 1. Concurrency bugs can be subtle and the *exact* conditions can be<br>
&gt; hard to reproduce for debugging.  For example, the solution to a<br>
&gt; couple of problems with Delay [1] [2] were solved by moving away from<br>
&gt; Semaphore&gt;&gt;critical: to use signalling.<br>
&gt;<br>
&gt; 2. The in-image atomicity of determining whether a signal was actually<br>
&gt; consumed or not during process suspension/termination is awkward.  Its<br>
&gt; seems hard to *really* know for sure it right (but I haven&#39;t looked in<br>
&gt; depth into Denis&#39; latest proposals.)<br>
&gt;<br>
&gt; 3. The existing in-image implementation of Semaphore&gt;&gt;critical messes<br>
&gt; around in  Process&gt;&gt;terminate in a *special* way that is not easy for<br>
&gt; those 100 eyes to understand. For example, I personally am not<br>
&gt; comfortable with understanding how the special Semaphore handling in<br>
&gt; Process&gt;&gt;terminate works, but I can easily follow how<br>
&gt; primitiveEnterCriticalSection just looking at the code [3].<br>
<br>
</span>Points 2 &amp; 3 might possibly be addressed by having new primitiveWaitReturned<br>
*always* return true, so if the process is terminated while waiting,<br>
the assignment to signalConsumed doesn&#39;t occur...<br>
<br>
  critical: mutuallyExcludedBlock<br>
    signalConsumed := false.<br>
    [<br>
        signalConsumed := self primitiveWaitReturned.<br>
        blockValue := mutuallyExcludedBlock value<br>
    ] ensure: [ signalConsumed ifTrue: [self signal] ].<br>
    ^blockValue<br>
<br>
where primitiveWait (<a href="https://git.io/vuDjd" rel="noreferrer" target="_blank">https://git.io/vuDjd</a>) is copied<br>
and (just guessing) the marked line added...<br></blockquote><div><br></div><div>That looks like a good idea.  I&#39;ll try and take a look early next week.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
  primitiveWaitReturned<br>
      | sema excessSignals activeProc inInterpreter |<br>
      sema := self stackTop. &quot;rcvr&quot;<br>
&quot;==&gt;&gt;&quot; self pop: argumentCount + 1 thenPush: objectMemory trueObject. &quot;&lt;&lt;==&quot;<br>
     excessSignals := self fetchInteger: ExcessSignalsIndex ofObject: sema.<br>
      excessSignals &gt; 0<br>
          ifTrue:<br>
            [self storeInteger: ExcessSignalsIndex<br>
                        ofObject: sema<br>
                        withValue: excessSignals - 1]<br>
          ifFalse:<br>
            inInterpreter := instructionPointer &gt;= objectMemory startOfMemory.<br>
            activeProc := self activeProcess.<br>
            self addLastLink: activeProc toList: sema.<br>
            self transferTo: self wakeHighestPriority from: CSWait.<br>
            self<br>
forProcessPrimitiveReturnToExecutivePostContextSwitch: inInterpreter]<br>
<br>
which I guess could be added quickly if Esteban could compile the<br>
latest pharo-spur-vm ;)<br></blockquote><div><br></div><div>If you built a Squeak VMMaker image via <a href="http://www.squeakvm.org/svn/squeak/branches/Cog/image/BuildSqueakSpurTrunkVMMakerImage.st">http://www.squeakvm.org/svn/squeak/branches/Cog/image/BuildSqueakSpurTrunkVMMakerImage.st</a> and built using the svn source tree you could do this yourself now.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
cheers -ben<br>
<br>
&gt; 4. Its faster<br>
<div class=""><div class="h5">&gt;<br>
&gt; btw, Looking at the following...<br>
&gt;   CriticalSection&gt;&gt;critical: aBlock<br>
&gt;       ^self primitiveEnterCriticalSection<br>
&gt;           ifTrue: [aBlock value]<br>
&gt;           ifFalse: [aBlock ensure: [self primitiveExitCriticalSection]]<br>
&gt;<br>
&gt; without intimate VM knowledge but seeing Eliot recently say<br>
&gt; &quot;invocations of methods with primitives [...] are not suspension<br>
&gt; points, unless their primitives fail&quot; -- I&#39;m paranoid that since<br>
&gt; #ensure: primitive 198 always fail, there might be some some small<br>
&gt; window for a race where a process might be terminated before<br>
&gt; #primitiveExitCriticalSection can be executed. I&#39;ll take a refutation<br>
&gt; of this to improve the method comment.<br>
&gt;<br>
&gt; The following instils more confidence...<br>
&gt;<br>
&gt;   CriticalSection2&gt;&gt;critical: aBlock<br>
&gt;       | reEntered |<br>
&gt;       reEntered := false.<br>
&gt;       [ reEntered := self primitiveEnterCriticalSection.<br>
&gt;          aBlock value ] ensure:<br>
&gt;          [ reEntered ifFalse: [ self primitiveExitCriticalSection] ]<br>
&gt;<br>
&gt; but performance is reduced...<br>
&gt; &quot;#(&#39;Mutex -&gt; 282,511 per second&#39;<br>
&gt;   &#39;Monitor -&gt; 479,217 per second&#39;<br>
&gt; &#39;CriticalSection -&gt; 729,049 per second&#39;<br>
&gt; &#39;CriticalSection2 -&gt; 571,631 per second&#39;)&quot;<br>
&gt;<br>
&gt; Then again, any termination from within a critical section is anyhow<br>
&gt; problematic since potentially data structures are left in a<br>
&gt; indeterminate state.<br>
&gt;<br>
&gt; [1] <a href="http://forum.world.st/Super-fast-delay-td4787257.html" rel="noreferrer" target="_blank">http://forum.world.st/Super-fast-delay-td4787257.html</a><br>
&gt; [2] <a href="https://pharo.fogbugz.com/default.asp?13755" rel="noreferrer" target="_blank">https://pharo.fogbugz.com/default.asp?13755</a><br>
&gt; [3] <a href="https://git.io/vuDnA" rel="noreferrer" target="_blank">https://git.io/vuDnA</a><br>
&gt;<br>
&gt; cheers -ben<br>
&gt;<br>
&gt;&gt;<br>
&gt;&gt; Stef<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Hi Ben,<br>
&gt;&gt;<br>
&gt;&gt; On Thu, Jan 7, 2016 at 10:39 AM, Ben Coman &lt;<a href="mailto:btc@openinworld.com">btc@openinworld.com</a>&gt;wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On Fri, Jan 8, 2016 at 1:20 AM, Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt; wrote:<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; and here&#39;s a version with a better class comment<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; On Thu, Jan 7, 2016 at 9:12 AM, Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt; wrote:<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; Hi Denis, Hi Clément,  Hi Frank,<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; On Thu, Jan 7, 2016 at 5:34 AM, Clément Bera &lt;<a href="mailto:bera.clement@gmail.com">bera.clement@gmail.com</a>&gt; wrote:<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; Hello,<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; Eliot, please, you told me you had the code and Denis is interested.<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; It uses 3 primitives for performance.<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; Forgive the delay.  I thought it proper to ask permission since the code was written while I was at Qwaq. I&#39;m attaching the code in a fairly raw state, see the attached.  The code is MIT, but copyright 3DICC.<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; It is a plugin replacement for Squeak&#39;s Mutex, and with a little ingenuity could be a replacement for Squeak&#39;s Monitor.  It is quicker because it uses three new primitives to manage entering a critical section and setting the owner, exiting the critical section and releasing the owner, and testing if a critical section, entering if the section is unowned.  The use of the primitives means fewer block activations and ensure: blocks in entering and exiting the critical section, and that&#39;s the actual cause of the speed-up.<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; You can benchmark the code as is.  Here are some results on 32-bit Spur, on my 2.2GHz Core i7<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; {Mutex new. Monitor new. CriticalSection new} collect:<br>
&gt;&gt;&gt; &gt;&gt; [:cs| | n |<br>
&gt;&gt;&gt; &gt;&gt; n := 0.<br>
&gt;&gt;&gt; &gt;&gt; [cs critical: [n := n + 1]. cs critical: [n := n + 1]. cs critical: [n := n + 1]. cs critical: [n := n + 1]. cs critical: [n := n + 1].<br>
&gt;&gt;&gt; &gt;&gt; cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1].<br>
&gt;&gt;&gt; &gt;&gt; n ] bench]<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; {Mutex new. Monitor new. CriticalSection new} collect:<br>
&gt;&gt;&gt; &gt;&gt; [:cs| | n |<br>
&gt;&gt;&gt; &gt;&gt; n := 0.<br>
&gt;&gt;&gt; &gt;&gt; cs class name, &#39; -&gt; &#39;,<br>
&gt;&gt;&gt; &gt;&gt; [cs critical: [n := n + 1]. cs critical: [n := n + 1]. cs critical: [n := n + 1]. cs critical: [n := n + 1]. cs critical: [n := n + 1].<br>
&gt;&gt;&gt; &gt;&gt; cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1].<br>
&gt;&gt;&gt; &gt;&gt; n ] bench]<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; #( &#39;Mutex -&gt; 440,000 per second. 2.27 microseconds per run.&#39;<br>
&gt;&gt;&gt; &gt;&gt; &#39;Monitor -&gt; 688,000 per second. 1.45 microseconds per run.&#39;<br>
&gt;&gt;&gt; &gt;&gt; &#39;CriticalSection -&gt; 1,110,000 per second. 900 nanoseconds per run.&#39;)<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; This is great Eliot. Thank you and 3DICC.  After loading the changeset<br>
&gt;&gt;&gt; into Pharo-50515 (32 bit Spur) I get the following results on my<br>
&gt;&gt;&gt; laptop i5-2520M @ 2.50GHz<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; #(&#39;Mutex -&gt; 254,047 per second&#39;<br>
&gt;&gt;&gt;  &#39;Monitor -&gt; 450,442 per second&#39;<br>
&gt;&gt;&gt;  &#39;CriticalSection -&gt; 683,393 per second&#39;)<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; In a fresh Image &quot;Mutex allInstances basicInspect&quot; lists just two mutexes...<br>
&gt;&gt;&gt; 1. NetNameResolver--&gt;ResolverMutex<br>
&gt;&gt;&gt; 2. ThreadSafeTranscript--&gt;accessSemaphore<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; I hate myself for getting distracted but I&#39;m finding this is un.  One can migrate to the new representation using normal Monticello loads by<br>
&gt;&gt;<br>
&gt;&gt; In the first version redefine Mutex and Monitor to subclass LinkedList and have their owner/ownerProcess inst var first (actually third after firstLink &amp; lastLink), and add the primitives.<br>
&gt;&gt;<br>
&gt;&gt; In the next version check that all Mutex and Monitor instanes are unowned and then redefine to discard excess inst vars<br>
&gt;&gt;<br>
&gt;&gt; Let me test this before committing, and see that all tests are ok.<br>
&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; cheers -ben<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; Replacement is probably trivial; rename Mutex to OldMutex, rename CriticalSection to Mutex, recompile.  But there are lots of mutexes in the system and these are potentially owned.  Transforming unowned ones is trivial, but transforming owned ones is, I think, impossible.  But at least in my system there are no owned mutexes or monitors.<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; Frank (or anyone else), would you be interested in creating a replacement for Squeak&#39;s Monitor based on CriticalSection?<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; Here are the two business methods:<br>
&gt;&gt;&gt; &gt;&gt; CriticalSection methods for mutual exclusion<br>
&gt;&gt;&gt; &gt;&gt; critical: aBlock<br>
&gt;&gt;&gt; &gt;&gt; &quot;Evaluate aBlock protected by the receiver.&quot;<br>
&gt;&gt;&gt; &gt;&gt; ^self primitiveEnterCriticalSection<br>
&gt;&gt;&gt; &gt;&gt; ifTrue: [aBlock value]<br>
&gt;&gt;&gt; &gt;&gt; ifFalse: [aBlock ensure: [self primitiveExitCriticalSection]]<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; critical: aBlock ifLocked: lockedBlock<br>
&gt;&gt;&gt; &gt;&gt; &quot;Answer the evaluation of aBlock protected by the receiver.  If it is already in a critical<br>
&gt;&gt;&gt; &gt;&gt; section on behalf of some other process answer the evaluation of lockedBlock.&quot;<br>
&gt;&gt;&gt; &gt;&gt; ^self primitiveTestAndSetOwnershipOfCriticalSection<br>
&gt;&gt;&gt; &gt;&gt; ifNil: [lockedBlock value]<br>
&gt;&gt;&gt; &gt;&gt; ifNotNil:[:alreadyOwner|<br>
&gt;&gt;&gt; &gt;&gt; alreadyOwner<br>
&gt;&gt;&gt; &gt;&gt; ifTrue: [aBlock value]<br>
&gt;&gt;&gt; &gt;&gt; ifFalse: [aBlock ensure: [self primitiveExitCriticalSection]]]<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; and the primitives:<br>
&gt;&gt;&gt; &gt;&gt; CriticalSection methods for private-primitives<br>
&gt;&gt;&gt; &gt;&gt; primitiveEnterCriticalSection<br>
&gt;&gt;&gt; &gt;&gt; &quot;Primitive. The receiver must be unowned or owned by the current process to proceed.<br>
&gt;&gt;&gt; &gt;&gt; Answer if the process is owned by the current process.&quot;<br>
&gt;&gt;&gt; &gt;&gt; &lt;primitive: 186&gt;<br>
&gt;&gt;&gt; &gt;&gt; self primitiveFailed<br>
&gt;&gt;&gt; &gt;&gt; &quot;In the spirit of the following&quot;<br>
&gt;&gt;&gt; &gt;&gt; &quot;[owner ifNil:<br>
&gt;&gt;&gt; &gt;&gt; [owner := Processor activeProcess.<br>
&gt;&gt;&gt; &gt;&gt; ^false].<br>
&gt;&gt;&gt; &gt;&gt;  owner = Processor activeProcess ifTrue:<br>
&gt;&gt;&gt; &gt;&gt; [^true].<br>
&gt;&gt;&gt; &gt;&gt;  self addLast: Processor activeProcess.<br>
&gt;&gt;&gt; &gt;&gt;  Processor activeProcess suspend] valueUnpreemptively&quot;<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; primitiveExitCriticalSection<br>
&gt;&gt;&gt; &gt;&gt; &quot;Primitive. Set te receiver to unowned and if any processes are waiting on<br>
&gt;&gt;&gt; &gt;&gt; the receiver then proceed the first one, indicating that the receiver is unowned.&quot;<br>
&gt;&gt;&gt; &gt;&gt; &lt;primitive: 185&gt;<br>
&gt;&gt;&gt; &gt;&gt; self primitiveFailed<br>
&gt;&gt;&gt; &gt;&gt; &quot;In the spirit of the following&quot;<br>
&gt;&gt;&gt; &gt;&gt; &quot;[owner := nil.<br>
&gt;&gt;&gt; &gt;&gt;  self isEmpty ifFalse:<br>
&gt;&gt;&gt; &gt;&gt; [process := self removeFirst.<br>
&gt;&gt;&gt; &gt;&gt; process resume]] valueUnpreemptively&quot;<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; primitiveTestAndSetOwnershipOfCriticalSection<br>
&gt;&gt;&gt; &gt;&gt; &quot;Primitive. Attempt to set the ownership of the receiver.<br>
&gt;&gt;&gt; &gt;&gt; If the receiver is unowned set its owningProcess to the<br>
&gt;&gt;&gt; &gt;&gt; activeProcess and answer false.  If the receiver is owned<br>
&gt;&gt;&gt; &gt;&gt; by the activeProcess answer true.  If the receiver is owned<br>
&gt;&gt;&gt; &gt;&gt; by some other process answer nil.&quot;<br>
&gt;&gt;&gt; &gt;&gt; &lt;primitive: 187&gt;<br>
&gt;&gt;&gt; &gt;&gt; self primitiveFail<br>
&gt;&gt;&gt; &gt;&gt; &quot;In the spirit of the following&quot;<br>
&gt;&gt;&gt; &gt;&gt; &quot;[owner ifNil:<br>
&gt;&gt;&gt; &gt;&gt; [owningProcess := Processor activeProcess.<br>
&gt;&gt;&gt; &gt;&gt; ^false].<br>
&gt;&gt;&gt; &gt;&gt;  owner = Processor activeProcess ifTrue: [^true].<br>
&gt;&gt;&gt; &gt;&gt;  ^nil] valueUnpreemptively&quot;<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; 2016-01-07 13:24 GMT+01:00 Denis Kudriashov &lt;<a href="mailto:dionisiydk@gmail.com">dionisiydk@gmail.com</a>&gt;:<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; Hello.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; I hear about new Monitor implementation based on new primitives.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; Where to get it?<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div>
</div></div>