Skip to content

Commit ac5f5d1

Browse files
committed
[tsan] Add interceptors and sychronization for libdispatch semaphores on OS X
This patch adds release and acquire semantics for libdispatch semaphores and a test case. Differential Revision: http://reviews.llvm.org/D14992 llvm-svn: 254412
1 parent 0d0692d commit ac5f5d1

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

compiler-rt/lib/tsan/rtl/tsan_libdispatch_mac.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <dispatch/dispatch.h>
2626
#include <pthread.h>
2727

28+
typedef long long_t; // NOLINT
29+
2830
namespace __tsan {
2931

3032
typedef struct {
@@ -166,6 +168,21 @@ TSAN_INTERCEPTOR(void, dispatch_once_f, dispatch_once_t *predicate,
166168
});
167169
}
168170

171+
TSAN_INTERCEPTOR(long_t, dispatch_semaphore_signal,
172+
dispatch_semaphore_t dsema) {
173+
SCOPED_TSAN_INTERCEPTOR(dispatch_semaphore_signal, dsema);
174+
Release(thr, pc, (uptr)dsema);
175+
return REAL(dispatch_semaphore_signal)(dsema);
176+
}
177+
178+
TSAN_INTERCEPTOR(long_t, dispatch_semaphore_wait, dispatch_semaphore_t dsema,
179+
dispatch_time_t timeout) {
180+
SCOPED_TSAN_INTERCEPTOR(dispatch_semaphore_wait, dsema, timeout);
181+
long_t result = REAL(dispatch_semaphore_wait)(dsema, timeout);
182+
if (result == 0) Acquire(thr, pc, (uptr)dsema);
183+
return result;
184+
}
185+
169186
} // namespace __tsan
170187

171188
#endif // SANITIZER_MAC
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang_tsan %s -o %t -framework Foundation
2+
// RUN: %run %t 2>&1
3+
4+
#import <Foundation/Foundation.h>
5+
6+
long global;
7+
8+
int main() {
9+
NSLog(@"Hello world.");
10+
11+
global = 42;
12+
13+
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
14+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
15+
16+
global = 43;
17+
dispatch_semaphore_signal(sem);
18+
});
19+
20+
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
21+
global = 44;
22+
23+
NSLog(@"Done.");
24+
return 0;
25+
}
26+
27+
// CHECK: Hello world.
28+
// CHECK: Done.
29+
// CHECK-NOT: WARNING: ThreadSanitizer

0 commit comments

Comments
 (0)