From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fac41,9a0ff0bffdf63657 X-Google-Attributes: gidfac41,public X-Google-Thread: 103376,4b06f8f15f01a568 X-Google-Attributes: gid103376,public X-Google-Thread: f43e6,9a0ff0bffdf63657 X-Google-Attributes: gidf43e6,public X-Google-Thread: 1108a1,9a0ff0bffdf63657 X-Google-Attributes: gid1108a1,public From: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: Software landmines (loops) Date: 1998/09/01 Message-ID: #1/1 X-Deja-AN: 386928004 References: <902934874.2099.0.nnrp-10.c246a717@news.demon.co.uk> <6r1glm$bvh$1@nnrp1.dejanews.com> <6r9f8h$jtm$1@nnrp1.dejanews.com> <6renh8$ga7$1@nnrp1.dejanews.com> <6rf59b$2ud$1@nnrp1.dejanews.com> <6rfra4$rul$1@nnrp1.dejanews.com> <35DBDD24.D003404D@calfp.co.uk> <6sbuod$fra$1@hirame.wwa.com> <35f51e53.48044143@ <904556531.666222@miso.it.uq.edu.au> <6sf87j$47n$1@hirame.wwa.com> Organization: The Mitre Corp., Bedford, MA. Newsgroups: comp.lang.eiffel,comp.object,comp.software-eng,comp.lang.ada Date: 1998-09-01T00:00:00+00:00 List-Id: In article <6sf87j$47n$1@hirame.wwa.com> "Robert Martin" writes: > If this is more complex (something that is arguable) it is not *much* more > complex. On the other hand, it is easier to maintain. The thread-safety > issue I talked about above would be easier to add to this function than to > the one with multiple returns. And as far as some of us are concerned, there is no difference, because the way to implement the guarded function is to wrap the guard around the test: function "=" (L, R : Guarded_Stack_Type) return Boolean is Temp: Boolean; begin Seize(L.Guard); Seize(R.Guard); Temp := Stack_Type(L) = Stack_Type(R); Release(R.Guard); Release(L.Guard); return Temp; end "="; With the obvious type declaration for the guarded stack: type Guarded_Stack_Type is new Stack with Guard: Semaphore; This implements the guarded equality in terms of the (known good) test for the parent type. Of course, this particular implementation is subject to deadlock, so I would either use a single semaphore for all stacks, or a more complex locking strategy, probably locking the stack with the lower address first: function "=" (L, R : Guarded_Stack_Type) return Boolean is Temp: Boolean; begin if L'Address = R'Address then return True; end if; if L'Address < R'Address then Seize(L.Guard); Seize(R.Guard); else Seize(R.Guard); Seize(L.Guard); end if; Temp := Stack_Type(L) = Stack_Type(R); Release(R.Guard); Release(L.Guard); -- release order doesn't matter. return Temp; end "="; Now we have a tasking/thread safe comparison, but we didn't have to mix up the three cases here with the three cases in the original "=". So I have six cases to test, not nine. Actually, I get more benefit, because I only have three NEW cases to test. Note to Ada programmers: At first it seems right to make the entire stack type a protected object. But how would you define the "=" and any other two stack operations in that case? Once you realize that there needs to be a pair of seize and release operations, you might as well associate them with a new component of the object anyway. Also--for Ada and non-Ada programmers--the norm would be to do all this in the body of the package which defined Guarded_Stack_Type as a private extension of Stack_Type. Now the interface profile of the two types can be the same, since all the semaphore manipulations can be hidden from view. -- Robert I. Eachus with Standard_Disclaimer; use Standard_Disclaimer; function Message (Text: in Clever_Ideas) return Better_Ideas is...