[M3devel] idioms for tracking initialization state and raising errors?

Jay K jay.krell at cornell.edu
Sun Jul 31 11:07:12 CEST 2016


Is this a correct idiom?If so, I think it is fairly reasonable.

That is: be very willing to "early return" put all cleanup in finally raise errors in finally

In particular, I don't want to repeat the cleanup.I don't want to raise before leaving critical sections.

I don't intend this to raise within a raise, but onlyfrom the "normal" exit of the finally block.

I also don't want extra local booleans, i.e. raise: boolean to indicate failure.Just the one to indicate success.

PROCEDURE InitMutex (mutex: Mutex) =  VAR    lock: PCRITICAL_SECTION := NIL;    locked: PCRITICAL_SECTION := NIL;
  BEGIN    IF mutex.initialized THEN RETURN END;
    TRY
      lock := NewCriticalSection();      IF lock = NIL THEN RETURN; END;
      EnterCriticalSection(ADR(initLock));      locked := ADR(initLock);
      IF mutex.initialized THEN RETURN END;
      (* We won the race. *)      RTHeapRep.RegisterFinalCleanup (mutex, CleanMutex);      mutex.lock := lock;      lock := NIL;      mutex.initialized := TRUE;
    FINALLY      IF locked # NIL THEN LeaveCriticalSection(locked); END;      DelCriticalSection(lock);      IF NOT mutex.initialized THEN (* Raise after leaving critical section. *)        RuntimeError.Raise (RuntimeError.T.OutOfMemory);      END;    END;
  END InitMutex;

Thank you, - Jay 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20160731/cb15430a/attachment-0001.html>


More information about the M3devel mailing list