blob: 32659d4eec0db7e78b996a2ef2f2e22798bed646 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// RUN: %clangxx_tsan %s -o %t
Stephen Hines6a211c52014-07-21 00:49:56 -07002// RUN: not %run %t 2>&1 | FileCheck %s
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08003// 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
Stephen Hines86277eb2015-03-23 12:06:32 -07005// RUN: echo "deadlock:main" > %t.supp
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08006// RUN: %env_tsan_opts=suppressions='%t.supp' %run %t 2>&1 | FileCheck %s --check-prefix=DISABLED
Stephen Hines86277eb2015-03-23 12:06:32 -07007// RUN: echo "deadlock:zzzz" > %t.supp
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08008// RUN: %env_tsan_opts=suppressions='%t.supp' not %run %t 2>&1 | FileCheck %s
Stephen Hines2d1fdb22014-05-28 23:58:16 -07009#include <pthread.h>
Stephen Hines6a211c52014-07-21 00:49:56 -070010#include <stdio.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070011
12int main() {
13 pthread_mutex_t mu1, mu2;
14 pthread_mutex_init(&mu1, NULL);
15 pthread_mutex_init(&mu2, NULL);
16
17 // mu1 => mu2
18 pthread_mutex_lock(&mu1);
19 pthread_mutex_lock(&mu2);
20 pthread_mutex_unlock(&mu2);
21 pthread_mutex_unlock(&mu1);
22
23 // mu2 => mu1
24 pthread_mutex_lock(&mu2);
25 pthread_mutex_lock(&mu1);
26 // CHECK: ThreadSanitizer: lock-order-inversion (potential deadlock)
Stephen Hines6a211c52014-07-21 00:49:56 -070027 // DISABLED-NOT: ThreadSanitizer
28 // DISABLED: PASS
Stephen Hines2d1fdb22014-05-28 23:58:16 -070029 pthread_mutex_unlock(&mu1);
30 pthread_mutex_unlock(&mu2);
31
32 pthread_mutex_destroy(&mu1);
33 pthread_mutex_destroy(&mu2);
Stephen Hines6a211c52014-07-21 00:49:56 -070034 fprintf(stderr, "PASS\n");
Stephen Hines2d1fdb22014-05-28 23:58:16 -070035}