Cairo exports and more (was Re: [cairo] Re: Munging header files for export (and other) attributes)

Robert O'Callahan rocallahan at novell.com
Mon Sep 19 18:31:48 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.

It's still potentially a problem, due to the fact that compilers and
cache subsystems can reorder stores. I.e., one processor might do
"initialize_stuff(); been_here = true;" but the other processor might
see the write to "been_here" become visible *before* the writes in
initialize_stuff().

Rob



More information about the cairo mailing list