Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 9 Oct 2003 19:37:42 +1000
From:      Peter Jeremy <peterjeremy@optushome.com.au>
To:        Harti Brandt <brandt@fokus.fraunhofer.de>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Dynamic reads without locking.
Message-ID:  <20031009093741.GA69062@server.c211-28-27-130.belrs2.nsw.optusnet.com.au>
In-Reply-To: <20031008114506.I63940@beagle.fokus.fraunhofer.de>
References:  <20031008083059.GA520@garage.freebsd.pl> <20031008114506.I63940@beagle.fokus.fraunhofer.de>

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20031009093741.GA69062>