Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / Darwin / deadlock.mm
blob36ddfad54f7c97e9055a779d569147c78ae4d85d
1 // RUN: %clang_tsan %s -o %t -framework Foundation
2 // RUN: %deflake %run %t 2>&1 | FileCheck %s
4 #import <Foundation/Foundation.h>
6 #import "../test.h"
8 pthread_mutex_t m1;
9 pthread_mutex_t m2;
11 int main(int argc, const char *argv[]) {
12   barrier_init(&barrier, 2);
13   fprintf(stderr, "Hello world.\n");
15   pthread_mutex_init(&m1, NULL);
16   pthread_mutex_init(&m2, NULL);
17   
18   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
19     pthread_mutex_lock(&m1);
20     pthread_mutex_lock(&m2);
21     pthread_mutex_unlock(&m2);
22     pthread_mutex_unlock(&m1);
24     barrier_wait(&barrier);
25   });
26   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
27     barrier_wait(&barrier);
29     pthread_mutex_lock(&m2);
30     pthread_mutex_lock(&m1);
31     pthread_mutex_unlock(&m1);
32     pthread_mutex_unlock(&m2);
34     dispatch_sync(dispatch_get_main_queue(), ^{
35       CFRunLoopStop(CFRunLoopGetCurrent());
36     });
37   });
39   CFRunLoopRun();
40   
41   fprintf(stderr, "Done.\n");
42   return 0;
45 // CHECK: Hello world.
46 // CHECK: WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock)
47 // CHECK: Done.