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
[EMAIL PROTECTED], [EMAIL PROTECTED]
_______________________________________________
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to