Cairo exports and more (was Re: [cairo]
Re: Munging header files for export (and other) attributes)
Alexander Larsson
alexl at redhat.com
Mon Sep 19 00:07:21 PDT 2005
On Fri, 2005-09-16 at 15:02 -0700, Bill Spitzak wrote:
>
> mental at rydia.net wrote:
> > Quoting Bill Spitzak <spitzak at d2.com>:
> >
> >
> >>_cairo_backend_foo() {
> >> static char been_here = false;
> >> if (!been_here) {
> >> lock(backend_internal_mutex);
> >> if (!been_here) {
> >> initialize_stuff();
> >> been_here = true;
> >> }
> >> unlock(backend_internal_mutex);
> >> }
> >>}
> >
> >
> > I got my knuckles rapped for doing that kind of thing once.
> >
> > Turns out, that outer test of !been_here isn't safe on
> > multi-processor systems: without the memory barrier presumably
> > introduced by lock(), you can't assume that the value of been_here
> > will reflect the value visible to the other CPU(s) due to cache
> > issues, or that the compiler won't play nasty tricks on you.
> >
> > Now, especially if you make been_here volatile, you can _get away
> > with it_ on some compilers and platforms. But it's not portable or
> > guaranteed.
> >
> > Basically you must only test been_here within the protection of the
> > lock.
>
> It's true that it should be marked violatile, but I think you missed the
> fact that I re-tested the variable inside the lock.
This method is called double checked locking, and it doesn't in general
work for all hardware architectures. See e.g.
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
glib uses this on architectures where it works. Looking at its
configure.in it seems it doesn't work on old sparcs, alpha, ppc and
ia64.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Alexander Larsson Red Hat, Inc
alexl at redhat.com alla at lysator.liu.se
He's a short-sighted coffee-fuelled werewolf whom everyone believes is mad.
She's a cold-hearted punk femme fatale with an incredible destiny. They fight
crime!
More information about the cairo
mailing list