From owner-freebsd-hackers@FreeBSD.ORG Wed Oct 8 03:21:04 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 5FB8516A4B3; Wed, 8 Oct 2003 03:21:04 -0700 (PDT) Received: from mailhub.fokus.fraunhofer.de (mailhub.fokus.fraunhofer.de [193.174.154.14]) by mx1.FreeBSD.org (Postfix) with ESMTP id ACD3543F3F; Wed, 8 Oct 2003 03:21:02 -0700 (PDT) (envelope-from brandt@fokus.fraunhofer.de) Received: from beagle (beagle [193.175.132.100])h98AL0S18200; Wed, 8 Oct 2003 12:21:00 +0200 (MEST) Date: Wed, 8 Oct 2003 12:21:00 +0200 (CEST) From: Harti Brandt To: Pawel Jakub Dawidek In-Reply-To: <20031008101222.GB520@garage.freebsd.pl> Message-ID: <20031008121734.G63940@beagle.fokus.fraunhofer.de> References: <20031008083059.GA520@garage.freebsd.pl> <20031008101222.GB520@garage.freebsd.pl> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-hackers@freebsd.org cc: hsu@freebsd.org cc: rwatson@freebsd.org Subject: Re: Dynamic reads without locking. X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Oct 2003 10:21:04 -0000 X-List-Received-Date: Wed, 08 Oct 2003 10:21:04 -0000 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