<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"Calibri Light";
        panose-1:2 15 3 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
p.MsoNoSpacing, li.MsoNoSpacing, div.MsoNoSpacing
        {mso-style-priority:1;
        margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Hi Eliot, all,</p>
<p class="MsoNormal"><o:p> </o:p></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"><o:p> </o:p></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"><o:p> </o:p></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"><o:p> </o:p></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"><o:p> </o:p></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"><o:p> </o:p></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"><o:p> </o:p></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"><o:p> </o:p></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"><o:p> </o:p></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"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></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"><o:p> </o:p></p>
<p class="MsoNormal">I hope it's a bit clearer now. Any ideas would be greatly appreciated.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Best,</p>
<p class="MsoNormal">Jaromir</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">PS: here's Eliot's original explanation of #releaseCriticalSection: http://forum.world.st/Solving-termination-of-critical-sections-in-the-context-of-priority-inversion-was-SemaphoreTest-fail-td5082184.html</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"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="mso-element:para-border-div;border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="border:none;padding:0in"><b>From: </b><a href="mailto:mail@jaromir.net">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">Squeak Dev</a>;
<a href="mailto:eliot.miranda@gmail.com">Eliot Miranda</a><br>
<b>Subject: </b>[squeak-dev] Improve termination behavior when in #critical section</p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Hi Eliot, Nicolas, or anyone interested in context juggling,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></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.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></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.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In case you'd like to hear more detailed comments or explanations I'll be happy to elaborate.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The changes have been tested also in Cuis which gives me more confidence they're solid.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks for any comments.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Best,<o:p></o:p></p>
<p class="MsoNormal">Jaromir<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNoSpacing"><span lang="CS">--</span></p>
<p class="MsoNoSpacing"><strong><span style="font-family:"Calibri Light",sans-serif;color:#333333;font-weight:normal">Jaromír Matas</span></strong><span style="font-family:"Calibri Light",sans-serif;color:#555555"><o:p></o:p></span></p>
<p class="MsoNoSpacing"><span style="font-family:"Calibri Light",sans-serif;color:#2E75B6">mail@jaromir.net<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#8FAADC"><o:p> </o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>