1// RUN: %clangxx_tsan %s -o %t -framework Foundation -fobjc-arc
2// RUN:                                   not %run %t 2>&1 | FileCheck %s
3// RUN: %env_tsan_opts=detect_deadlocks=1 not %run %t 2>&1 | FileCheck %s
4// RUN: %env_tsan_opts=detect_deadlocks=0     %run %t 2>&1 | FileCheck %s --check-prefix=DISABLED
5
6#import <Foundation/Foundation.h>
7
8int main() {
9  @autoreleasepool {
10    NSObject* obj1 = [NSObject new];
11    NSObject* obj2 = [NSObject new];
12
13    // obj1 -> obj2
14    @synchronized(obj1) {
15      @synchronized(obj2) {
16      }
17    }
18
19    // obj1 -> obj1
20    @synchronized(obj2) {
21      @synchronized(obj1) {
22// CHECK: ThreadSanitizer: lock-order-inversion (potential deadlock)
23      }
24    }
25  }
26
27  NSLog(@"PASS");
28// DISABLED-NOT: ThreadSanitizer
29// DISABLED: PASS
30  return 0;
31}
32