Dynamic reads without locking.

Bernd Walter ticso at cicely12.cicely.de
Thu Oct 9 10:24:34 PDT 2003


On Thu, Oct 09, 2003 at 07:37:42PM +1000, Peter Jeremy wrote:
> On Wed, Oct 08, 2003 at 11:51:06AM +0200, Harti Brandt wrote:
> >You need to lock when reading if you insist on consistent data. Even a
> >simple read may be non-atomic (this should be the case for 64bit
> >operations on all our platforms). So you need to do
> >
> >mtx_lock(&foo_mtx);
> >bar = foo;
> >mtx_unlock(&foo_mtx);
> >
> >if foo is a datatype that is not guaranteed to be red atomically. For
> >8-bit data you should be safe without the lock on any architecture. I'm
> >not sure for 16 and 32 bit, but for 64-bit you need the look for all
> >our architectures, I think.
> 
> AFAIK, aligned 64-bit reads should be atomic on all 64-bit
> architectures, ie everything except i386.  Smaller aligned reads
> should be atomic on all supported architectures.  Unaligned reads
> are not atomic anywhere.
> 
> Note that, possibly contrary to expectations, 8-bit and 16-bit
> _writes_ are not atomic on many (all?) the 64-bit architectures.
> Small writes are generally done by doing a 64-bit read, insert
> under mask and 64-bit write.

The mask case is true - e.g. on alpha <=ev5, but it's still atomic.
You write the 8 or 16 bit in a single step, but the other bits of the
same 32bit memory location are loaded into a register as well and
masked.
Note that this is semanticaly in no way different from a CPU loading
a whole cacheline to change a single byte which is what every modern
system does.

-- 
B.Walter                   BWCT                http://www.bwct.de
ticso at bwct.de                                  info at bwct.de



More information about the freebsd-hackers mailing list