Newsgroups: comp.sys.apple2.programmer Path: blue.weeg.uiowa.edu!news.uiowa.edu!uunet!MathWorks.Com!europa.eng.gtefsd.com!howland.reston.ans.net!wupost!waikato!comp.vuw.ac.nz!actrix.gen.nz!dempson From: dempson@actrix.gen.nz (David Empson) Subject: Re: Mut-ex outside GNO Message-ID: Organization: Actrix Information Exchange References: <375dg0$5gp@alpha.vaxxine.com> <377008$16t@gap.cco.caltech.edu> Date: Wed, 12 Oct 1994 12:04:42 GMT Lines: 69 In article <377008$16t@gap.cco.caltech.edu>, Nathan Mates wrote: > > The best way to make sure that only one process is within a segment > is to use a varb, sort of like the busy flag. By setting the interrupt > flag in the P-reg, you can make sure that nobody else can tweak this > varb while you're checking it. Here's a short code snippet: > > * > * > CheckFlag start > * > * Returns cs if someone's already in the lib, cc if nobody was > * (and therefore a go-ahead to enter it) > * > php ;save old p-reg, including interrupt flag > sei ;nobody bother us. We're working. > lda MyFlag > bne WereBusy ;someone's in. Wait for it to be freed > inc MyFlag ;now, we're in control of the lib > plp ;restore old I > clc ;flag we got control > rts > > WereBusy plp ;restore old I > sec ;flag someone else's in it. > rts > end If you use something like TSB or TRB, you don't even need to disable interrupts. These instructions were specifically intended for use as indivisible flag test and manipulation (the standard mutex semaphone "test and set" operation). phb phk plb lda #1 tsb InUse ; Test bit 0 and set it beq Yippee ; If it was clear, we're in plb sec ; Otherwise, the routine is already in use. rts Yippee * Do whatever is required stz InUse ; Release the routine for others to use ; Could also use lda #1, trb InUse. This could be useful if other ; bits in InUse are used for other routines which are not mutually ; exclusive with this routine. plb clc rts Note that TSB and STZ don't have long modes, so you must have the data bank register set correctly (I'm assuming the InUse variable is in the code bank above). > But, within any call, if 100% of your variable access is stack-based, > it can be re-entered as many times as you want without any problems. Correct, as long as you don't have self-modifying code, and don't access _any_ external data that might have mutual access problems (e.g. looking up the message center to get a handle). -- David Empson dempson@actrix.gen.nz Snail mail: P.O. Box 27-103, Wellington, New Zealand