[PATCH] D14992: [tsan] Add interceptors and sychronization for libdispatch semaphores on OS X

Kuba Brecka via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 25 10:54:49 PST 2015


kubabrecka created this revision.
kubabrecka added reviewers: dvyukov, kcc, glider, samsonov.
kubabrecka added subscribers: llvm-commits, zaks.anna, ismailp.

This patch adds release and acquire semantics for libdispatch semaphores and a test case.

http://reviews.llvm.org/D14992

Files:
  lib/tsan/rtl/tsan_libdispatch_mac.cc
  test/tsan/Darwin/gcd-semaphore-norace.mm

Index: lib/tsan/rtl/tsan_libdispatch_mac.cc
===================================================================
--- lib/tsan/rtl/tsan_libdispatch_mac.cc
+++ lib/tsan/rtl/tsan_libdispatch_mac.cc
@@ -25,6 +25,8 @@
 #include <dispatch/dispatch.h>
 #include <pthread.h>
 
+typedef long long_t;  // NOLINT
+
 namespace __tsan {
 
 typedef struct {
@@ -133,6 +135,21 @@
   });
 }
 
+TSAN_INTERCEPTOR(long_t, dispatch_semaphore_signal,
+                 dispatch_semaphore_t dsema) {
+  SCOPED_TSAN_INTERCEPTOR(dispatch_semaphore_signal, dsema);
+  Release(thr, pc, (uptr)dsema);
+  return REAL(dispatch_semaphore_signal)(dsema);
+}
+
+TSAN_INTERCEPTOR(long_t, dispatch_semaphore_wait, dispatch_semaphore_t dsema,
+                 dispatch_time_t timeout) {
+  SCOPED_TSAN_INTERCEPTOR(dispatch_semaphore_wait, dsema, timeout);
+  long_t result = REAL(dispatch_semaphore_wait)(dsema, timeout);
+  if (result == 0) Acquire(thr, pc, (uptr)dsema);
+  return result;
+}
+
 }  // namespace __tsan
 
 #endif  // SANITIZER_MAC
Index: test/tsan/Darwin/gcd-semaphore-norace.mm
===================================================================
--- test/tsan/Darwin/gcd-semaphore-norace.mm
+++ test/tsan/Darwin/gcd-semaphore-norace.mm
@@ -0,0 +1,29 @@
+// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %run %t 2>&1
+
+#import <Foundation/Foundation.h>
+
+long global;
+
+int main() {
+    NSLog(@"Hello world.");
+
+    global = 42;
+    
+    dispatch_semaphore_t sem = dispatch_semaphore_create(0);
+    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+        
+        global = 43;
+        dispatch_semaphore_signal(sem);
+    });
+    
+    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
+    global = 44;
+
+    NSLog(@"Done.");
+    return 0;
+}
+
+// CHECK: Hello world.
+// CHECK: Done.
+// CHECK-NOT: WARNING: ThreadSanitizer


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14992.41159.patch
Type: text/x-patch
Size: 1871 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151125/cf70ec90/attachment.bin>


More information about the llvm-commits mailing list