Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!BARILVM.BITNET!P85025 From: P85025@BARILVM.BITNET (Doron Shikmoni) Newsgroups: comp.lang.asm370 Subject: Re: STOSM/STNSM Message-ID: <8911121448.AA18138@brazos.rice.edu> Date: 12 Nov 89 12:19:45 GMT Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: IBM 370 Assembly Programming Discussion List Distribution: inet Organization: The Internet Lines: 60 From Valdis: >CS is pretty simple really - there's a good description on page A-40 >of the S/370 PrincOp (GA22-7000-10). All it does is fetch a value, >compare it, and if it's the same update. It's used for serializing >code. This is a dangerously incomplete description. See below. > Example (cribbing from A-40.. :) - you have a lock word, that >if it's a 0 you can proceed, but if it's a 1 you have to wait. The >person who gets the lock sets it to 1 when he enters, and to 0 when he leaves. > > SR R1,R1 get a zero. > LA R2,1 get a one. >LOOP CS R1,R2,LOCKWORD do it > BC 4,LOOP if somebody else got it, spin. >... locked code here > ST R1,LOCKWORD clear the flag This code demonstrates one of the common bugs introduced via CS. It does not maintain any serialization!!!! See below. >What the CS will do is: > Fetch LOCKWORD > Compare to R1 > If EQUAL, STORE R2 at LONGWORD > else set condition code. There's your bug. The last line should be replaced with the following two: else LOAD LOCKWORD into R1. set condition code. NOW, please read your code section again. Assuming that you meant "0" to read "unlocked" and "1" to read "locked", you can see that your code allows everyone to be in the critical section at the same time. No spin will ever occur; if get bad CC on the first try, R1 is loaded with "1"; then, the next (single) spin will get a CC of zero, and your lock has been picked... This is a very common error done with CS and CDS; and since actual lock clashes are rare, statistically, such bugs may be sitting in many programs, waiting for the best time to incarnate, a-la Murphy.... The corrected code will look like this (making minimal change to your example): LA R2,1 get a one. LOOP SR R1,R1 get a zero. CS R1,R2,LOCKWORD do it BC 4,LOOP if somebody else got it, spin. ... locked code here ST R1,LOCKWORD clear the flag As a matter of taste, I'd also change the "BC 4" to "BNE". But this is only a matter of taste, which is not arguable. Regards Doron