[PATCH] D14745: [tsan] Implement basic GCD interceptors for OS X

Dmitry Vyukov via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 17 07:42:05 PST 2015


dvyukov added a comment.

> So at this point, I'd like to implement something that 1) works on current OS X and 2) doesn't produce false positives in system libraries. If you know of a better way (other than disabling reads/writes in the initializer), I'll be glad to consider it.


We can do:

TSAN_INTERCEPTOR(void, dispatch_once_f, dispatch_once_t *predicate, void *context, dispatch_function_t function) {

  SCOPED_INTERCEPTOR_RAW(dispatch_once_f, predicate, context, function);
  atomic_uint32_t *a = static_cast<atomic_uint32_t*>(predicate);
  u32 v = atomic_load(a, memory_order_acquire);
  if (v == 0 && atomic_compare_exchange_strong(a, &v, 1,
                                               memory_order_relaxed)) {
    (*function)(context);
    Release(thr, pc, (uptr)a);
    atomic_store(a, 2, memory_order_release);
  } else {
    while (v != 2) {
      internal_sched_yield();
      v = atomic_load(a, memory_order_acquire);
    }
    Acquire(thr, pc, (uptr)a);
  }
  return 0;

}

Since we don't store ~0 into the predicate, the fast path will never be taken. So we will be able to annotate it even for non-instrumented code. Also, since tsan does not see own atomic stores, the race on predicate won't be reported: the only accesses to it that tsan sees is the load on fast path. Loads don't race.


http://reviews.llvm.org/D14745





More information about the llvm-commits mailing list