Date: Sun, 16 Dec 2007 19:16:41 -0700 (MST) From: "M. Warner Losh" <imp@bsdimp.com> To: jan.grant@bristol.ac.uk Cc: BearPerson@gmx.net, alfred@freebsd.org, freebsd-arch@freebsd.org Subject: Re: Code review request: small optimization to localtime.c Message-ID: <20071216.191641.-1402074010.imp@bsdimp.com> In-Reply-To: <20071204085502.N83722@tribble.ilrt.bris.ac.uk> References: <20071203235929.685d3674@Karsten.Behrmanns.Kasten> <20071204014614.GE76623@elvis.mu.org> <20071204085502.N83722@tribble.ilrt.bris.ac.uk>
next in thread | previous in thread | raw e-mail | index | archive | help
In message: <20071204085502.N83722@tribble.ilrt.bris.ac.uk> Jan Grant <jan.grant@bristol.ac.uk> writes: : On Mon, 3 Dec 2007, Alfred Perlstein wrote: : : [on the double-checked locking idiom] : : > Karsten, _typically_ (but not always) an "unlock" operation : > requires that writes prior to the unlock be globally visible. : > : > This is why it works almost everywhere. : : Perhaps, but if you use it you should probably mark the code with : /* XXX not guaranteed to be correct by POSIX */ : : Double-checked locking is broken without an appropriate barrier. : "Correctness over speed" should surely be our watchword :-) Actually, the code I posted for review *IS* posixly correct. It doesn't matter if the write posts or not. If it doesn't post, then we know the guard variable will be false still and we take out the lock, test it see that it is true (since nothing would work well if the lock/unlock pairs didn't force a consistent variable after the lock is released). If it is posted, we don't take the branch. Since these variables are initialized to zero and set exactly once to true, the above is true. pthread_once() is more optimal, but a larger code change. Warner
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20071216.191641.-1402074010.imp>