Subject: Re: pthreads
To: None <amiga@NetBSD.ORG>
From: Ty Sarna <tsarna@endicor.com>
List: amiga
Date: 06/13/1995 18:26:28
In article <199506130230.WAA05874@pain.lcs.mit.edu>,
Chris G Demetriou  <Chris_G_Demetriou@lagavulin.pdl.cs.cmu.edu> wrote:
> > Some kind of TAS-like (or atomic mem<->reg/mem<->mem exchange or
> > whatever) is needed.  However, I never noticed (until Timo pointed it
> > out) that the BCLR/BSET instructions set condition codes based on the
> > contenets BEFORE the operation, rather than the result like most normal
> > instructions, and can thus be used as a TAS. So my answer was bogus.
> 
> Umm...  If TAS can't be used, then how could BCLR?
> 
> TAS uses a RMW memory cycle to insure atomicity.
> 
> as far as i can tell, BCLR does _NOT_ use a RMW memory cycle, and
> thus is not necessarily atomic.

>From what I gather, TAS (CAS, etc) is bus-atomic, while the BSET/BCLR
are cpu-atomic. Meaning that NOTHING can interfere with memory while TAS
is running, while other things can access memory while BSET is running,
but the instruction is atomic as far as the CPU is concerned.

At least I think so. I've been wrong before.

Now, if we ever want to support multiprocesors, that isn't sufficient.
But then again, if we do, we don't really want to use this incarnation
of libpthread anyway.

> You should use TAS (and CAS and CAS2) for atomic operations if you
> need them.  If they don't work (perhaps because the amiga doesn't
> implement RMW memory cycles?)  then you're out of luck, and MUST make
> a kernel call for atomic modifications.  (i can't think of any other

Yes, it is a hardware issue. TAS screws up DMA in the chip memory space.
It is also not safe to use it outside of chip sapce as well, according
to Dave Haynie, because a lot of memory (and CPU board) designs ignore
the issues. ("Hey, the manuals say TAS isn't allowed, so I guess we can
just ignore it". An understandble position). I bet GVP boards rank top
among them :-)

> reason they wouldn't work, and somebody [i forget who] said that they
> weren't available.) 
> 
> If BCLR _did_ use an RMW memory cycle, and it worked properly on the
> amiga, then TAS would also work...  So, since TAS is documented to
> use a RMW cycle (and thus be atomic), you should use it.

I think for safety, this is probably the best bet. I think making the
TAS/syscall decision at runtime would be a win though, at least for
those who can use TAS.

Oh, on the '060 (and '040??) TAS/CAS/CAS2 are not implemented anyway,
they have to be emulated, so one might as well use the syscall in those
cases, since all that overhead is there for TAS as wel in that case.