Notes from the meeting

Mark Mitchell mark at codesourcery.com
Thu Jul 6 23:45:13 UTC 2000


>>>>> "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.

A non-thread-safe implementation is:

  int __cxx_gv_set_guard_variable (long long* gv) {
    if (*((char*) gv) == 0) {
      *((char*) gv) = 1;
      return 1;
    } else
      return 0;
  } 

A thread-safe implementation is something like:

  int __cxx_gv_set_guard_variable (long long* gv) {
    static mutex m;			 
    int result;

    lock(m);
    if (*((char*) gv) == 0) {
      *((char*) gv) = 1;
      result = 1;
    } else 
      result = 0;
    unlock(m);
    return result;
  } 

Does that make sense?

--
Mark Mitchell                   mark at codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com




More information about the cxx-abi-dev mailing list