Skip to content

ThreadSanitizerAtomicOperations

Alexander Potapenko edited this page Aug 31, 2015 · 1 revision

Introduction

ThreadSanitizer understands various flavors of compiler built-in atomic operations:

  1. __sync_fetch_and_add. This does not support atomic load operation and precise memory ordering.
  2. __c11_atomic_fetch_add. This is supported only by clang.
  3. __atomic_load_n. This requires relatively fresh compiler (at least gcc 4.7).

If std::atomic<> type is implemented using some sort of compiler built-ins (e.g. libc++), then ThreadSanitizer will understand it as well.

Also ThreadSanitizer runtime provides own set of atomic operations of the form:

__tsan_atomic8 __tsan_atomic8_load(const volatile __tsan_atomic8 *a, __tsan_memory_order mo);

The full list is available here.

So there are 3 options:

  1. Use only compiler intrinsics (or std::atomic<>).
  2. Use home-grown atomic operations for old compilers, for newer compilers use atomic_load_n atomic operations.
  3. Use home-grown atomic operations for normal build, under tsan use tsan_atomic8_load atomic operations.

You can see an example of option 2 here.

Clone this wiki locally