Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 8 Oct 2003 12:21:00 +0200 (CEST)
From:      Harti Brandt <brandt@fokus.fraunhofer.de>
To:        Pawel Jakub Dawidek <nick@garage.freebsd.pl>
Cc:        rwatson@freebsd.org
Subject:   Re: Dynamic reads without locking.
Message-ID:  <20031008121734.G63940@beagle.fokus.fraunhofer.de>
In-Reply-To: <20031008101222.GB520@garage.freebsd.pl>
References:  <20031008083059.GA520@garage.freebsd.pl> <20031008101222.GB520@garage.freebsd.pl>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, 8 Oct 2003, Pawel Jakub Dawidek wrote:

PJD>On Wed, Oct 08, 2003 at 11:51:06AM +0200, Harti Brandt wrote:
PJD>+> You need to lock when reading if you insist on consistent data. Even a
PJD>+> simple read may be non-atomic (this should be the case for 64bit
PJD>+> operations on all our platforms). So you need to do
PJD>+>
PJD>+> mtx_lock(&foo_mtx);
PJD>+> bar = foo;
PJD>+> mtx_unlock(&foo_mtx);
PJD>+>
PJD>+> if foo is a datatype that is not guaranteed to be red atomically. For
PJD>+> 8-bit data you should be safe without the lock on any architecture. I'm
PJD>+> not sure for 16 and 32 bit, but for 64-bit you need the look for all
PJD>+> our architectures, I think.
PJD>
PJD>But I'm not talking about non-atomic reads. What I'm want to show is that
PJD>even atomic read (without lock) is dangerous in some cases.
PJD>
PJD>+> If you don't care about occasionally reading false data (for statistics or
PJD>+> such stuff) you can go without the lock.
PJD>
PJD>I'm afraid that many developers thinks that atomic reads are always safe
PJD>without locks (there are many such reads in sources). I hope I'm wrong.

Well, I see your point. If the writer does a non-atomic write by doing:

foo = data;
foo &= mask;

then nothing helps. If he would do

foo = data & mask;

on an atomic object things may work (well, one has to read the C-standard
to find out wether the compiler is allowed to convert the 2nd form to the
first one.).

harti
-- 
harti brandt,
http://www.fokus.fraunhofer.de/research/cc/cats/employees/hartmut.brandt/private
brandt@fokus.fraunhofer.de, harti@freebsd.org



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