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

mental at rydia.net mental at rydia.net
Fri Sep 16 12:17:13 PDT 2005


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.

-mental


More information about the cairo mailing list