Notes from the meeting

Jim Dehnert dehnert at baalbek.engr.sgi.com
Fri Jul 7 00:11:58 UTC 2000


> From: Mark Mitchell <mark at codesourcery.com>
> 
> >>>>> "Jim" == Jim Dehnert <dehnert at baalbek.engr.sgi.com> writes:
> 
>     Jim> 	extern "C" int __cxx_gv_test_and_set ( long long *gv
>     Jim> );
> 
>     Jim> The routine returns "true" to only one caller, and "false" to
>     Jim> all others after the first has released the lock.  We
>     Jim> neglected to note that the release also needs specification.
>     Jim> How about:
> 
>     Jim> 	extern "C" void __cxx_gv_release ( long long *gv );
> 
>     Jim> Comments?
> 
> I suggest a simpler interface, and make it mandatory:
> 
>   extern "C" int __cxx_gv_set_guard_variable (long long *gv);
> 
> This function returns true if the caller is the first to call the
> function; false otherwise.  The right code to generate would be:
> 
>   if (/* low-order byte of guard variable not already set*
>       && __cxx_gv_set_guard_variable (&gv)) {
>     // Do initialization
>   }
> 
> This causes you to take the hit of the function call only the first
> time through, except that if multiple threads reach this point all at
> once the first time, several of them may call the function.  (A
> correct compiler can skip the test, but not the call, at the cost of
> more function calls) So, the function itself must be thread safe, if
> it cares.

This is all fine, except that the threads that get here after the first
one, but before it has initialized the object, must wait until the
initialization is complete before proceeding.  That means that they
can't return from __cxx_gv_set_guard_variable (&gv) until the
initialization is done, and the initialization must be completed before
setting the low-order byte of the guard variable.  To accomplish this,
the first thread must have a way of signalling its completion, e.g.:

  if (/* low-order byte of guard variable not already set*
      && __cxx_gv_set_guard_variable (&gv)) {
    // Do initialization
    ...
    __cxx_gv_release (&gv);
  }

and __cxx_gv_set_guard_variable for the non-first waits for the lock
release by the first before returning for the others.

Jim

-	    Jim Dehnert		dehnert at sgi.com
				(650)933-4272




More information about the cxx-abi-dev mailing list