ThreadSanitaizer does not detect lock_cmpxchg and warn about data race

138 Views Asked by At

the following code acquire a mutex using lock_cmpxchg on an index of an array, then two threads write and read to the same index, but still thread sanitizer say there is a data race. how can I tell him the fact there is a lock between the threads because it seem it does not detect it.

 aquire_mutex(bool* lock_ptr)
 {
   retval = _lock_cmpxchg_8bit(0, 1, lock_ptr); //lock_cmpxchg implemented in inline assembly
   return (retval == 0) ? 1: 0;
 }

foo()
{
  if (aquire_mutex(global_lock[index])!= 1)
  {
    return error;
  }
  uint x = array[index];
  ...
  array[index] = random_value;
}

the flow is: a lot of threads are running with different indexes or the same index, the only data race appear if two threads are with the same index, but there is a lock for it. but thread sanitizer warn that there is a read in line uint x = array[index]; by thread 1 and there is a write in line array[index] = random_value ; Im not sure why it does detect data race, Thanks for the help!!

1

There are 1 best solutions below

0
On

As Hasturkun suggested, the problem was that Thread Sanitizer know nothing about the inline assembly hence he does not detect it as a lock, I used instead __sync_bool_compare_and_swap built GCC intrinsic, and now thread sanitizer does not report any false negative!!

according to Dmitry Vyukov https://github.com/google/sanitizers/issues/1443