Dynamic reads without locking.

Peter Jeremy peterjeremy at optushome.com.au
Thu Oct 9 02:40:53 PDT 2003


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.

Peter


More information about the freebsd-hackers mailing list