Code review request: small optimization to localtime.c

M. Warner Losh imp at bsdimp.com
Sun Dec 16 18:19:22 PST 2007


In message: <20071204085502.N83722 at tribble.ilrt.bris.ac.uk>
            Jan Grant <jan.grant at 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


More information about the freebsd-arch mailing list