<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'><div>Is this a correct idiom?</div><div>If so, I think it is fairly reasonable.</div><div><br></div><div><br></div><div>That is:</div><div> be very willing to "early return"</div><div> put all cleanup in finally</div><div> raise errors in finally</div><div><br></div><div><br></div><div>In particular, I don't want to repeat the cleanup.</div><div>I don't want to raise before leaving critical sections.</div><div><br></div><div><br></div><div>I don't intend this to raise within a raise, but only</div><div>from the "normal" exit of the finally block.</div><div><br></div><div><br></div><div>I also don't want extra local booleans, i.e. raise: boolean to indicate failure.</div><div>Just the one to indicate success.</div><div><br></div><div><br></div><div>PROCEDURE InitMutex (mutex: Mutex) =</div><div> VAR</div><div> lock: PCRITICAL_SECTION := NIL;</div><div> locked: PCRITICAL_SECTION := NIL;</div><div><br></div><div> BEGIN</div><div> IF mutex.initialized THEN RETURN END;</div><div><br></div><div> TRY</div><div><br></div><div> lock := NewCriticalSection();</div><div> IF lock = NIL THEN RETURN; END;</div><div><br></div><div> EnterCriticalSection(ADR(initLock));</div><div> locked := ADR(initLock);</div><div><br></div><div> IF mutex.initialized THEN RETURN END;</div><div><br></div><div> (* We won the race. *)</div><div> RTHeapRep.RegisterFinalCleanup (mutex, CleanMutex);</div><div> mutex.lock := lock;</div><div> lock := NIL;</div><div> mutex.initialized := TRUE;</div><div><br></div><div> FINALLY</div><div> IF locked # NIL THEN LeaveCriticalSection(locked); END;</div><div> DelCriticalSection(lock);</div><div> IF NOT mutex.initialized THEN (* Raise after leaving critical section. *)</div><div> RuntimeError.Raise (RuntimeError.T.OutOfMemory);</div><div> END;</div><div> END;</div><div><br></div><div> END InitMutex;</div><div><br></div><br>Thank you,<div> - Jay</div> </div></body>
</html>