<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:small">Hi Jaromir,</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">   I *hate* code like oldList class == LinkedList", "context methodClass == Semaphore", and "context methodClass == Mutex", which are all about implementation, not safely extensible, and difficult to understand.  Instead we could introduce abstractions such as "isRunList" vs "isBlockingList", and "isSemaphore" and "isMutex", which would allow us to ast least extend the system if we ever introduced new forms of block (the "new" Mutex implementation was new 14 years ago).  This: "context stackPtr > 0 and: [context top == false]])]<span class="gmail_default">" could be a method on Context.</span></div><div class="gmail_default" style="font-size:small"><span class="gmail_default"><br></span></div><div class="gmail_default" style="font-size:small"><span class="gmail_default">So let's try and rewrite to be more abstract and more intentional.</span></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 1, 2023 at 7:22 AM Jaromir Matas <<a href="mailto:mail@jaromir.net">mail@jaromir.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div class="msg-5058732156362385167">





<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="m_-7094736966756243318WordSection1">
<p class="MsoNormal">Hi Eliot, all,</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I'd like to expand a bit on releasing critical sections during termination presented in Kernel-jar.1498. I *think* the second part of the procedure can be further simplified (and generalized) as follows.
</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">The point is we do not need to check the process is immediately beyond the wait/lock primitive (by checking selectorJustSentOrSelf); instead, just being still inside the #critical context should suffice to assume the process left the condition
 variable's primitive but hasn't made progress and an intervention is required.</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">suspendAndReleaseCriticalSection</p>
<p class="MsoNormal">                "Figure out if we are terminating a process that is in the ensure: block of a critical section.</p>
<p class="MsoNormal">                If it hasn't made progress but is beyond the wait (which we can tell by the oldList being</p>
<p class="MsoNormal">                one of the runnable lists, i.e. a LinkedList, not a Semaphore or Mutex), then the ensure:</p>
<p class="MsoNormal">                block needs to be run."</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">                | oldList |</p>
<p class="MsoNormal">                "Suspend and unblock the receiver from a condition variable using the suspend primitive #88.</p>
<p class="MsoNormal">                It answers the list the receiver was on before the suspension."</p>
<p class="MsoNormal">                oldList := self suspendAndUnblock.</p>
<p class="MsoNormal">                suspendedContext ifNotNil: [:context | </p>
<p class="MsoNormal">                                (context method pragmaAt: #criticalSection) ifNil: [^self].</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">                                (oldList isNil or: [oldList class == LinkedList]) ifFalse: [</p>
<p class="MsoNormal">                                "If still blocked at the condition variable of a critical section, skip the rest of the current context."</p>
<p class="MsoNormal">                                                suspendedContext := context pc: context endPC.</p>
<p class="MsoNormal">                                                ^self].</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">                                "Now we know we are somewhere beyond the wait/lock primitive in a critical section but</p>
<p class="MsoNormal">                                haven't made progress into the ensure block; the only point of the following code is to identify</p>
<p class="MsoNormal">                                the type of condition variable we're in and let the process enter the ensure block when applicable."</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">                                "If still haven't made progress into the ensure block then it has not been activated, so step into it."</p>
<p class="MsoNormal">                                (context methodClass == Semaphore or: [</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">                                "If still haven't made progress into the ensure block and the lock primitive just acquired ownership</p>
<p class="MsoNormal">                                (indicated by it answering false) then the ensure block has not been activated, so step into it."</p>
<p class="MsoNormal">                                (context methodClass == Mutex and: [</p>
<p class="MsoNormal">                                                context stackPtr > 0 and: [context top == false]])]) ifTrue: [</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">                                "In such cases we need to step into the ensure block and let the unwind execute the ensure argument</p>
<p class="MsoNormal">                                block and, if the critical section itself is already inside an unwind block, also the ensure receiver block."</p>
<p class="MsoNormal">                                                suspendedContext := context stepToCallee]]</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">The first part deals with the situation the process is blocked at the condition variable but has been just released from it by suspendAndUnblock (prim 88) to be able to proceed with termination; in this case we do need to skip the critical
 block because the critical block itself can be inside some outer ensure argument block.
</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I hope it's a bit clearer now. Any ideas would be greatly appreciated.</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Best,</p>
<p class="MsoNormal">Jaromir</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">PS: here's Eliot's original explanation of #releaseCriticalSection: <a href="http://forum.world.st/Solving-termination-of-critical-sections-in-the-context-of-priority-inversion-was-SemaphoreTest-fail-td5082184.html" target="_blank">http://forum.world.st/Solving-termination-of-critical-sections-in-the-context-of-priority-inversion-was-SemaphoreTest-fail-td5082184.html</a></p>
<p class="MsoNormal">What's changed since then is we can now safely unwind critical sections and non-local returns inside ensure argument blocks.</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border-style:solid none none;border-top-width:1pt;border-top-color:rgb(225,225,225);padding:3pt 0in 0in">
<p class="MsoNormal" style="border:none;padding:0in"><b>From: </b><a href="mailto:mail@jaromir.net" target="_blank">Jaromir Matas</a><br>
<b>Sent: </b>Monday, January 30, 2023 17:40<br>
<b>To: </b><a href="mailto:squeak-dev@lists.squeakfoundation.org" target="_blank">Squeak Dev</a>;
<a href="mailto:eliot.miranda@gmail.com" target="_blank">Eliot Miranda</a><br>
<b>Subject: </b>[squeak-dev] Improve termination behavior when in #critical section</p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Hi Eliot, Nicolas, or anyone interested in context juggling,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">A few Process/Semaphore/Mutex tests have been left as expected failures in the current image. It's time to fix them. I'd very much appreciate if you could review/merge the improved versions of #terminate and *especially* #suspendAndReleaseCriticalSection
 that finally make all currently failing tests pass. Sent in Kernel-jar.1498 and KernelTests-jar.443.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">In addition, I've added a few Mutex and Semaphore tests to further illustrate the behavior when terminating a process stuck in a #critical section, especially when the critical section itself is inside an unwind block (which has previously
 not been fully addressed). The steps in #suspendAndReleaseCriticalSection should, hopefully, be sufficiently commented.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">In case you'd like to hear more detailed comments or explanations I'll be happy to elaborate.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">The changes have been tested also in Cuis which gives me more confidence they're solid.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Thanks for any comments.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Best,<u></u><u></u></p>
<p class="MsoNormal">Jaromir<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="m_-7094736966756243318MsoNoSpacing"><span lang="CS">--</span></p>
<p class="m_-7094736966756243318MsoNoSpacing"><strong><span style="font-family:"Calibri Light",sans-serif;color:rgb(51,51,51);font-weight:normal">Jaromír Matas</span></strong><span style="font-family:"Calibri Light",sans-serif;color:rgb(85,85,85)"><u></u><u></u></span></p>
<p class="m_-7094736966756243318MsoNoSpacing"><span style="font-family:"Calibri Light",sans-serif;color:rgb(46,117,182)"><a href="mailto:mail@jaromir.net" target="_blank">mail@jaromir.net</a><u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(143,170,220)"><u></u> <u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>

</div></blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" 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>